Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Markdown and BlockNote in RICH_TEXT fields (#7613) #9279

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/twenty-front/src/generated-metadata/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ export enum FieldMetadataType {
Rating = 'RATING',
RawJson = 'RAW_JSON',
Relation = 'RELATION',
RichTextDeprecated = 'RICH_TEXT_DEPRECATED',
RichText = 'RICH_TEXT',
Select = 'SELECT',
Text = 'TEXT',
Expand Down
3 changes: 2 additions & 1 deletion packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
import { gql } from '@apollo/client';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
Expand Down Expand Up @@ -371,6 +371,7 @@ export enum FieldMetadataType {
Rating = 'RATING',
RawJson = 'RAW_JSON',
Relation = 'RELATION',
RichTextDeprecated = 'RICH_TEXT_DEPRECATED',
RichText = 'RICH_TEXT',
Select = 'SELECT',
Text = 'TEXT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export const ActivityRichTextEditor = ({
upsertActivity({
activity,
input: {
body: newBody,
body: {
blocknote: newBody,
markdown: null,
},
},
});
}
Expand Down Expand Up @@ -158,10 +161,10 @@ export const ActivityRichTextEditor = ({
const initialBody = useMemo(() => {
if (
isDefined(activity) &&
isNonEmptyString(activity.body) &&
activity?.body !== '{}'
isNonEmptyString(activity.body.blocknote) &&
activity?.body.blocknote !== '{}'
) {
return JSON.parse(activity.body);
return JSON.parse(activity.body.blocknote);
}
}, [activity]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ const task = {
createdAt: '2023-04-26T10:12:42.33625+00:00',
updatedAt: '2023-04-26T10:23:42.33625+00:00',
title: 'Task title',
body: null,
body: {
blocknote: null,
markdown: null,
},
assigneeId: null,
status: null,
dueAt: '2023-04-26T10:12:42.33625+00:00',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const NoteCard = ({
const openActivityRightDrawer = useOpenActivityRightDrawer({
objectNameSingular: CoreObjectNameSingular.Note,
});
const body = getActivityPreview(note.body);
const body = getActivityPreview(note.body.blocknote);

const { FieldContextProvider: NoteTargetsContextProvider } = useFieldContext({
objectNameSingular: CoreObjectNameSingular.Note,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const TaskRow = ({ task }: { task: Task }) => {
objectNameSingular: CoreObjectNameSingular.Task,
});

const body = getActivitySummary(task.body);
const body = getActivitySummary(task.body.blocknote);
const { completeTask } = useCompleteTask(task);

const { FieldContextProvider: TaskTargetsContextProvider } = useFieldContext({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const task: Task = {
id: '123',
status: 'DONE',
title: 'Test',
body: 'Test',
body: {
blocknote: 'Test',
markdown: 'Test',
},
dueAt: '2024-03-15T07:33:14.212Z',
createdAt: '2024-03-15T07:33:14.212Z',
updatedAt: '2024-03-15T07:33:14.212Z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export type Activity = {
createdAt: string;
updatedAt: string;
title: string;
body: string | null;
body: {
blocknote: string | null;
markdown: string | null;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const mapFieldMetadataToGraphQLQuery = ({
FieldMetadataType.MultiSelect,
FieldMetadataType.Position,
FieldMetadataType.RawJson,
FieldMetadataType.RichText,
FieldMetadataType.RichTextDeprecated,
FieldMetadataType.Array,
].includes(fieldType);

Expand Down Expand Up @@ -162,5 +162,13 @@ ${mapObjectMetadataToGraphQLQuery({
}`;
}

if (fieldType === FieldMetadataType.RichText) {
return `${field.name}
{
blocknote
markdown
}`;
}

return '';
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { LinksFieldDisplay } from '@/object-record/record-field/meta-types/displ
import { PhonesFieldDisplay } from '@/object-record/record-field/meta-types/display/components/PhonesFieldDisplay';
import { RatingFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RatingFieldDisplay';
import { RelationFromManyFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RelationFromManyFieldDisplay';
import { RichTextDeprecatedFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RichTextDeprecatedFieldDisplay';
import { RichTextFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RichTextFieldDisplay';
import { isFieldIdentifierDisplay } from '@/object-record/record-field/meta-types/display/utils/isFieldIdentifierDisplay';
import { isFieldActor } from '@/object-record/record-field/types/guards/isFieldActor';
Expand All @@ -20,6 +21,7 @@ import { isFieldRating } from '@/object-record/record-field/types/guards/isField
import { isFieldRelationFromManyObjects } from '@/object-record/record-field/types/guards/isFieldRelationFromManyObjects';
import { isFieldRelationToOneObject } from '@/object-record/record-field/types/guards/isFieldRelationToOneObject';
import { isFieldRichText } from '@/object-record/record-field/types/guards/isFieldRichText';
import { isFieldRichTextDeprecated } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecated';
import { FieldContext } from '../contexts/FieldContext';
import { AddressFieldDisplay } from '../meta-types/display/components/AddressFieldDisplay';
import { ChipFieldDisplay } from '../meta-types/display/components/ChipFieldDisplay';
Expand Down Expand Up @@ -88,6 +90,8 @@ export const FieldDisplay = () => {
<BooleanFieldDisplay />
) : isFieldRating(fieldDefinition) ? (
<RatingFieldDisplay />
) : isFieldRichTextDeprecated(fieldDefinition) ? (
<RichTextDeprecatedFieldDisplay />
) : isFieldRichText(fieldDefinition) ? (
<RichTextFieldDisplay />
) : isFieldActor(fieldDefinition) ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { RecordForSelect } from '@/object-record/relation-picker/types/RecordFor

import { isFieldArray } from '@/object-record/record-field/types/guards/isFieldArray';
import { isFieldArrayValue } from '@/object-record/record-field/types/guards/isFieldArrayValue';
import { isFieldRichText } from '@/object-record/record-field/types/guards/isFieldRichText';
import { isFieldRichTextValue } from '@/object-record/record-field/types/guards/isFieldRichTextValue';
import { isFieldRichTextDeprecated } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecated';
import { isFieldRichTextDeprecatedValue } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecatedValue';
import { FieldContext } from '../contexts/FieldContext';
import { isFieldBoolean } from '../types/guards/isFieldBoolean';
import { isFieldBooleanValue } from '../types/guards/isFieldBooleanValue';
Expand Down Expand Up @@ -113,9 +113,9 @@ export const usePersistField = () => {
isFieldRawJson(fieldDefinition) &&
isFieldRawJsonValue(valueToPersist);

const fieldIsRichText =
isFieldRichText(fieldDefinition) &&
isFieldRichTextValue(valueToPersist);
const fieldIsRichTextDeprecated =
isFieldRichTextDeprecated(fieldDefinition) &&
isFieldRichTextDeprecatedValue(valueToPersist);

const fieldIsArray =
isFieldArray(fieldDefinition) && isFieldArrayValue(valueToPersist);
Expand All @@ -138,7 +138,7 @@ export const usePersistField = () => {
fieldIsAddress ||
fieldIsRawJson ||
fieldIsArray ||
fieldIsRichText;
fieldIsRichTextDeprecated;

if (isValuePersistable) {
const fieldName = fieldDefinition.metadata.fieldName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useRichTextDeprecatedFieldDisplay } from '@/object-record/record-field/meta-types/hooks/useRichTextDeprecatedFieldDisplay';
import { getFirstNonEmptyLineOfRichText } from '@/ui/input/editor/utils/getFirstNonEmptyLineOfRichText';

export const RichTextDeprecatedFieldDisplay = () => {
const { fieldValue } = useRichTextDeprecatedFieldDisplay();

return (
<div>
<span>{getFirstNonEmptyLineOfRichText(fieldValue)}</span>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useRichTextFieldDisplay } from '@/object-record/record-field/meta-types/hooks/useRichTextFieldDisplay';
import { getFirstNonEmptyLineOfRichText } from '@/ui/input/editor/utils/getFirstNonEmptyLineOfRichText';
import { PartialBlock } from '@blocknote/core';
import { parseJson } from '~/utils/parseJson';

export const RichTextFieldDisplay = () => {
const { fieldValue } = useRichTextFieldDisplay();

const blocks = parseJson<PartialBlock[]>(fieldValue?.blocknote);

return (
<div>
<span>{getFirstNonEmptyLineOfRichText(fieldValue)}</span>
<span>{getFirstNonEmptyLineOfRichText(blocks)}</span>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useContext } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';

import { useRecordFieldInput } from '@/object-record/record-field/hooks/useRecordFieldInput';
import { FieldRichTextDeprecatedValue } from '@/object-record/record-field/types/FieldMetadata';
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector';
import { FieldMetadataType } from '~/generated-metadata/graphql';

import { usePersistField } from '@/object-record/record-field/hooks/usePersistField';
import { isFieldRichTextDeprecated } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecated';
import { isFieldRichTextDeprecatedValue } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecatedValue';
import { PartialBlock } from '@blocknote/core';
import { isNonEmptyString } from '@sniptt/guards';
import { FieldContext } from '../../contexts/FieldContext';
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata';

export const useRichTextDeprecatedField = () => {
const { recordId, fieldDefinition, hotkeyScope, maxWidth } =
useContext(FieldContext);

assertFieldMetadata(
FieldMetadataType.RichTextDeprecated,
isFieldRichTextDeprecated,
fieldDefinition,
);

const fieldName = fieldDefinition.metadata.fieldName;

const [fieldValue, setFieldValue] =
useRecoilState<FieldRichTextDeprecatedValue>(
recordStoreFamilySelector({
recordId,
fieldName: fieldName,
}),
);
const fieldRichTextDeprecatedValue = isFieldRichTextDeprecatedValue(
fieldValue,
)
? fieldValue
: '';

const { setDraftValue, getDraftValueSelector } =
useRecordFieldInput<FieldRichTextDeprecatedValue>(
`${recordId}-${fieldName}`,
);

const draftValue = useRecoilValue(getDraftValueSelector());

const draftValueParsed: PartialBlock[] = isNonEmptyString(draftValue)
? JSON.parse(draftValue)
: draftValue;

const persistField = usePersistField();

const persistRichTextDeprecatedField = (nextValue: PartialBlock[]) => {
if (!nextValue) {
persistField(null);
} else {
const parsedValueToPersist = JSON.stringify(nextValue);

persistField(parsedValueToPersist);
}
};

return {
draftValue: draftValueParsed,
setDraftValue,
maxWidth,
fieldDefinition,
fieldValue: fieldRichTextDeprecatedValue,
setFieldValue,
hotkeyScope,
persistRichTextDeprecatedField,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useContext } from 'react';

import { useRecordFieldValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext';

import { FieldRichTextDeprecatedValue } from '@/object-record/record-field/types/FieldMetadata';
import { assertFieldMetadata } from '@/object-record/record-field/types/guards/assertFieldMetadata';
import { isFieldRichTextDeprecated } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecated';
import { PartialBlock } from '@blocknote/core';
import { FieldMetadataType } from '~/generated-metadata/graphql';
import { parseJson } from '~/utils/parseJson';
import { FieldContext } from '../../contexts/FieldContext';

export const useRichTextDeprecatedFieldDisplay = () => {
const { recordId, fieldDefinition, hotkeyScope } = useContext(FieldContext);

assertFieldMetadata(
FieldMetadataType.RichTextDeprecated,
isFieldRichTextDeprecated,
fieldDefinition,
);

const fieldName = fieldDefinition.metadata.fieldName;

const fieldValue = useRecordFieldValue<
FieldRichTextDeprecatedValue | undefined
>(recordId, fieldName);

const fieldValueParsed = parseJson<PartialBlock[]>(fieldValue);

return {
fieldDefinition,
fieldValue: fieldValueParsed,
hotkeyScope,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { useContext } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';

import { useRecordFieldInput } from '@/object-record/record-field/hooks/useRecordFieldInput';
import { FieldRichTextValue } from '@/object-record/record-field/types/FieldMetadata';
import {
FieldRichTextDeprecatedValue,
FieldRichTextValue,
} from '@/object-record/record-field/types/FieldMetadata';
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector';
import { FieldMetadataType } from '~/generated-metadata/graphql';

import { usePersistField } from '@/object-record/record-field/hooks/usePersistField';
import { isFieldRichText } from '@/object-record/record-field/types/guards/isFieldRichText';
import { isFieldRichTextDeprecated } from '@/object-record/record-field/types/guards/isFieldRichTextDeprecated';
import { isFieldRichTextValue } from '@/object-record/record-field/types/guards/isFieldRichTextValue';
import { PartialBlock } from '@blocknote/core';
import { isNonEmptyString } from '@sniptt/guards';
Expand All @@ -19,23 +22,28 @@ export const useRichTextField = () => {
useContext(FieldContext);

assertFieldMetadata(
FieldMetadataType.RichText,
isFieldRichText,
FieldMetadataType.RichTextDeprecated,
isFieldRichTextDeprecated,
fieldDefinition,
);

const fieldName = fieldDefinition.metadata.fieldName;

const [fieldValue, setFieldValue] = useRecoilState<FieldRichTextValue>(
recordStoreFamilySelector({
recordId,
fieldName: fieldName,
}),
);
const fieldRichTextValue = isFieldRichTextValue(fieldValue) ? fieldValue : '';
const [fieldValue, setFieldValue] =
useRecoilState<FieldRichTextDeprecatedValue>(
recordStoreFamilySelector({
recordId,
fieldName: fieldName,
}),
);
const fieldRichTextValue = isFieldRichTextValue(fieldValue)
? fieldValue
: ({ blocknote: null, markdown: null } as FieldRichTextValue);

const { setDraftValue, getDraftValueSelector } =
useRecordFieldInput<FieldRichTextValue>(`${recordId}-${fieldName}`);
useRecordFieldInput<FieldRichTextDeprecatedValue>(
`${recordId}-${fieldName}`,
);

const draftValue = useRecoilValue(getDraftValueSelector());

Expand Down
Loading
Loading