/* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { Color } from '@tiptap/extension-color' import { Image } from '@tiptap/extension-image' import { Link } from '@tiptap/extension-link' import { Placeholder } from '@tiptap/extension-placeholder' import { EditorContent, useEditor } from '@tiptap/react' import { StarterKit } from '@tiptap/starter-kit' import { useEffect } from 'react' import { Markdown } from 'tiptap-markdown' import { TextEditorMenu } from './TextEditorMenu' interface RichTextEditorProps { labelTitle?: string labelStyle?: string containerStyle?: string defaultValue: string placeholder?: string showMenu?: boolean updateFormValue?: (value: string) => void } /** * @category Input */ export function RichTextEditor({ labelTitle, labelStyle, containerStyle, defaultValue, placeholder, showMenu = true, updateFormValue, }: RichTextEditorProps) { const handleChange = () => { let newValue: string | undefined = editor?.storage.markdown.getMarkdown() const regex = /!\[.*?\]\(.*?\)/g newValue = newValue?.replace(regex, (match: string) => match + '\n\n') if (updateFormValue && newValue) { updateFormValue(newValue) } } const editor = useEditor({ extensions: [ Color.configure({ types: ['textStyle', 'listItem'] }), StarterKit.configure({ bulletList: { keepMarks: true, keepAttributes: false, }, orderedList: { keepMarks: true, keepAttributes: false, }, }), Markdown.configure({ linkify: true, transformCopiedText: true, transformPastedText: true, }), Image, Link, Placeholder.configure({ placeholder, emptyEditorClass: 'is-editor-empty', }), ], content: defaultValue, onUpdate: handleChange, editorProps: { attributes: { class: `tw:h-full markdown tw:max-h-full tw:p-2 tw:overflow-y-auto`, }, }, }) useEffect(() => { if (editor?.storage.markdown.getMarkdown() === '' || !editor?.storage.markdown.getMarkdown()) { editor?.commands.setContent(defaultValue) } }, [defaultValue, editor]) return (