-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into settings-phone-search-option
- Loading branch information
Showing
69 changed files
with
5,916 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletions
13
...modules/object-record/record-field/meta-types/display/components/RichTextFieldDisplay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { useTextFieldDisplay } from '@/object-record/record-field/meta-types/hooks/useTextFieldDisplay'; | ||
import { useRichTextFieldDisplay } from '@/object-record/record-field/meta-types/hooks/useRichTextFieldDisplay'; | ||
import { getFirstNonEmptyLineOfRichText } from '@/ui/input/editor/utils/getFirstNonEmptyLineOfRichText'; | ||
import { PartialBlock } from '@blocknote/core'; | ||
|
||
export const RichTextFieldDisplay = () => { | ||
const { fieldValue } = useTextFieldDisplay(); | ||
const parsedField = | ||
fieldValue === '' ? null : (JSON.parse(fieldValue) as PartialBlock[]); | ||
const { fieldValue } = useRichTextFieldDisplay(); | ||
|
||
return <>{getFirstNonEmptyLineOfRichText(parsedField)}</>; | ||
return ( | ||
<div> | ||
<span>{getFirstNonEmptyLineOfRichText(fieldValue)}</span> | ||
</div> | ||
); | ||
}; |
68 changes: 68 additions & 0 deletions
68
.../twenty-front/src/modules/object-record/record-field/meta-types/hooks/useRichTextField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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 { 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 { isFieldRichTextValue } from '@/object-record/record-field/types/guards/isFieldRichTextValue'; | ||
import { PartialBlock } from '@blocknote/core'; | ||
import { isNonEmptyString } from '@sniptt/guards'; | ||
import { FieldContext } from '../../contexts/FieldContext'; | ||
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata'; | ||
|
||
export const useRichTextField = () => { | ||
const { recordId, fieldDefinition, hotkeyScope, maxWidth } = | ||
useContext(FieldContext); | ||
|
||
assertFieldMetadata( | ||
FieldMetadataType.RichText, | ||
isFieldRichText, | ||
fieldDefinition, | ||
); | ||
|
||
const fieldName = fieldDefinition.metadata.fieldName; | ||
|
||
const [fieldValue, setFieldValue] = useRecoilState<FieldRichTextValue>( | ||
recordStoreFamilySelector({ | ||
recordId, | ||
fieldName: fieldName, | ||
}), | ||
); | ||
const fieldRichTextValue = isFieldRichTextValue(fieldValue) ? fieldValue : ''; | ||
|
||
const { setDraftValue, getDraftValueSelector } = | ||
useRecordFieldInput<FieldRichTextValue>(`${recordId}-${fieldName}`); | ||
|
||
const draftValue = useRecoilValue(getDraftValueSelector()); | ||
|
||
const draftValueParsed: PartialBlock[] = isNonEmptyString(draftValue) | ||
? JSON.parse(draftValue) | ||
: draftValue; | ||
|
||
const persistField = usePersistField(); | ||
|
||
const persistRichTextField = (nextValue: PartialBlock[]) => { | ||
if (!nextValue) { | ||
persistField(null); | ||
} else { | ||
const parsedValueToPersist = JSON.stringify(nextValue); | ||
|
||
persistField(parsedValueToPersist); | ||
} | ||
}; | ||
|
||
return { | ||
draftValue: draftValueParsed, | ||
setDraftValue, | ||
maxWidth, | ||
fieldDefinition, | ||
fieldValue: fieldRichTextValue, | ||
setFieldValue, | ||
hotkeyScope, | ||
persistRichTextField, | ||
}; | ||
}; |
36 changes: 36 additions & 0 deletions
36
...-front/src/modules/object-record/record-field/meta-types/hooks/useRichTextFieldDisplay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { useRecordFieldValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext'; | ||
|
||
import { FieldRichTextValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { assertFieldMetadata } from '@/object-record/record-field/types/guards/assertFieldMetadata'; | ||
import { isFieldRichText } from '@/object-record/record-field/types/guards/isFieldRichText'; | ||
import { PartialBlock } from '@blocknote/core'; | ||
import { FieldMetadataType } from '~/generated-metadata/graphql'; | ||
import { parseJson } from '~/utils/parseJson'; | ||
import { FieldContext } from '../../contexts/FieldContext'; | ||
|
||
export const useRichTextFieldDisplay = () => { | ||
const { recordId, fieldDefinition, hotkeyScope } = useContext(FieldContext); | ||
|
||
assertFieldMetadata( | ||
FieldMetadataType.RichText, | ||
isFieldRichText, | ||
fieldDefinition, | ||
); | ||
|
||
const fieldName = fieldDefinition.metadata.fieldName; | ||
|
||
const fieldValue = useRecordFieldValue<FieldRichTextValue | undefined>( | ||
recordId, | ||
fieldName, | ||
); | ||
|
||
const fieldValueParsed = parseJson<PartialBlock[]>(fieldValue); | ||
|
||
return { | ||
fieldDefinition, | ||
fieldValue: fieldValueParsed, | ||
hotkeyScope, | ||
}; | ||
}; |
60 changes: 57 additions & 3 deletions
60
...src/modules/object-record/record-field/meta-types/input/components/RichTextFieldInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,59 @@ | ||
import { RichTextFieldDisplay } from '@/object-record/record-field/meta-types/display/components/RichTextFieldDisplay'; | ||
import { BLOCK_SCHEMA } from '@/activities/blocks/constants/Schema'; | ||
import { FieldContext } from '@/object-record/record-field/contexts/FieldContext'; | ||
import { useRichTextField } from '@/object-record/record-field/meta-types/hooks/useRichTextField'; | ||
import { FieldInputClickOutsideEvent } from '@/object-record/record-field/meta-types/input/components/DateTimeFieldInput'; | ||
import { useRegisterInputEvents } from '@/object-record/record-field/meta-types/input/hooks/useRegisterInputEvents'; | ||
import { BlockEditor } from '@/ui/input/editor/components/BlockEditor'; | ||
import { BlockEditorComponentInstanceContext } from '@/ui/input/editor/contexts/BlockEditorCompoponeInstanceContext'; | ||
import { PartialBlock } from '@blocknote/core'; | ||
import { useCreateBlockNote } from '@blocknote/react'; | ||
import styled from '@emotion/styled'; | ||
|
||
export const RichTextFieldInput = () => { | ||
return <RichTextFieldDisplay />; | ||
import { useContext, useRef } from 'react'; | ||
|
||
const StyledRichTextContainer = styled.div` | ||
height: 400px; | ||
width: 500px; | ||
overflow: auto; | ||
`; | ||
|
||
export type RichTextFieldInputProps = { | ||
onClickOutside?: FieldInputClickOutsideEvent; | ||
}; | ||
|
||
export const RichTextFieldInput = ({ | ||
onClickOutside, | ||
}: RichTextFieldInputProps) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const { recordId } = useContext(FieldContext); | ||
const { draftValue, hotkeyScope, persistRichTextField, fieldDefinition } = | ||
useRichTextField(); | ||
|
||
const editor = useCreateBlockNote({ | ||
initialContent: draftValue, | ||
domAttributes: { editor: { class: 'editor' } }, | ||
schema: BLOCK_SCHEMA, | ||
}); | ||
|
||
const handleClickOutside = (event: MouseEvent | TouchEvent) => { | ||
onClickOutside?.(() => persistRichTextField(editor.document), event); | ||
}; | ||
|
||
useRegisterInputEvents<PartialBlock[]>({ | ||
inputRef: containerRef, | ||
inputValue: draftValue, | ||
onClickOutside: handleClickOutside, | ||
hotkeyScope, | ||
}); | ||
|
||
return ( | ||
<StyledRichTextContainer ref={containerRef}> | ||
<BlockEditorComponentInstanceContext.Provider | ||
value={{ instanceId: `${recordId}-${fieldDefinition.fieldMetadataId}` }} | ||
> | ||
<BlockEditor editor={editor} /> | ||
</BlockEditorComponentInstanceContext.Provider> | ||
</StyledRichTextContainer> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../twenty-front/src/modules/object-record/record-field/types/guards/isFieldRichTextValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { z } from 'zod'; | ||
import { FieldRichTextValue } from '../FieldMetadata'; | ||
|
||
export const richTextSchema: z.ZodType<FieldRichTextValue> = z.union([ | ||
z.null(), // Exclude literal values other than null | ||
z.string(), | ||
]); | ||
|
||
export const isFieldRichTextValue = ( | ||
fieldValue: unknown, | ||
): fieldValue is FieldRichTextValue => | ||
richTextSchema.safeParse(fieldValue).success; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.