[0], event: KeyboardEvent) => {
// The @ list owns up/down, Enter, Tab and Esc while it is open.
if (suggestRef.current.onKeyDown(event)) return true
const chip = chipUnderSelection(view, event)
if (!chip) return false
event.preventDefault()
setTagDetails(chip)
return true
},
[],
)
const mergeTag = React.useMemo(
() => (mergeFields ? createMergeTagExtension(mergeFields) : null),
[mergeFields],
)
/** Stored text to editor markup, and back. Identity when no merge fields are set. */
const toEditor = React.useCallback(
(html: string) => (mergeFields ? tokensToHtml(html, mergeFields) : html),
[mergeFields],
)
const fromEditor = React.useCallback(
(html: string) => (mergeFields ? htmlToTokens(html) : html),
[mergeFields],
)
const aria: EditorAria = {
id,
ariaLabel,
ariaLabelledBy,
ariaDescribedBy,
ariaInvalid,
ariaRequired,
disabled,
readOnly,
}
const editor = useEditor({
immediatelyRender: false,
editable: !disabled && !readOnly,
content: toEditor(value ?? defaultValue),
extensions: [
...(mergeTag ? [mergeTag] : []),
StarterKit.configure({
heading: { levels: [2, 3] },
link: {
openOnClick: false,
autolink: true,
defaultProtocol: "https",
},
}),
Placeholder.configure({ placeholder }),
],
editorProps: {
handleClickOn: handleTagClick,
handleKeyDown: handleTagKey,
attributes: editorAttributes(aria),
},
onSelectionUpdate({ editor: currentEditor }) {
suggestRef.current.update(currentEditor)
},
onBlur() {
suggestRef.current.close()
},
onUpdate({ editor: currentEditor }) {
suggestRef.current.update(currentEditor)
const html = fromEditor(editorHtml(currentEditor))
syncHiddenInput(hiddenInputRef.current, html)
onChangeRef.current?.(html)
},
})
React.useEffect(() => {
if (!editor) return
const nextEditable = !disabled && !readOnly
if (editor.isEditable !== nextEditable) editor.setEditable(nextEditable, false)
editor.setOptions({
editorProps: {
handleClickOn: handleTagClick,
handleKeyDown: handleTagKey,
attributes: editorAttributes({
id,
ariaLabel,
ariaLabelledBy,
ariaDescribedBy,
ariaInvalid,
ariaRequired,
disabled,
readOnly,
}),
},
})
}, [
ariaDescribedBy,
ariaInvalid,
ariaLabel,
ariaLabelledBy,
ariaRequired,
disabled,
editor,
handleTagClick,
handleTagKey,
id,
readOnly,
])
React.useEffect(() => {
suggest.attach(editor)
}, [editor, suggest])
useControlledValueSync(editor, value, hiddenInputRef, toEditor, fromEditor)
const activeFormatting =
useEditorState({
editor,
selector: ({ editor: currentEditor }) =>
activeFormattingKey(currentEditor),
}) ?? ""
const active = new Set(activeFormatting.split("|"))
const blockCommands = buildCommands(BLOCK_SPECS, editor, active)
const markCommands = buildCommands(MARK_SPECS, editor, active)
const canEdit = Boolean(editor) && !disabled && !readOnly
return (
{
onFocus?.(event)
if (!event.defaultPrevented && event.target === event.currentTarget) {
editor?.commands.focus()
}
}}
onBlur={(event) => {
if (!movedWithinEditor(event.currentTarget, event.relatedTarget)) {
onBlur?.(event)
}
}}
className={cn(bare ? ROOT_BARE_CLASS : ROOT_BOX_CLASS, ...ROOT_CLASS, className)}
{...props}
>
{canEdit ? (
) : null}
{editor && canEdit ? (
) : null}
{contentTop}
{contentBottom}
{suggest.popup}
{tagDetails && editor && mergeFields ? (
f.key === tagDetails.key) ?? {
key: tagDetails.key,
label: tagDetails.key,
}
}
anchor={tagDetails.el}
example={describeMergeField?.(tagDetails.key)}
onClose={() => setTagDetails(null)}
onRemove={() => {
editor
.chain()
.focus()
.deleteRange({ from: tagDetails.pos, to: tagDetails.pos + 1 })
.run()
setTagDetails(null)
}}
/>
) : null}
)
},
)
RichTextEditor.displayName = "RichTextEditor"
export { RichTextEditor }
export type { RichTextEditorProps }