'use client' import { useEditor, EditorContent } from '@tiptap/react' import StarterKit from '@tiptap/starter-kit' import { Bold, Italic, Strikethrough, Paperclip, Underline } from 'lucide-react' import { TextStyle } from '@tiptap/extension-text-style' import { Color } from '@tiptap/extension-color' import UnderlineExtension from '@tiptap/extension-underline' import { Extension } from '@tiptap/core' const FontSizeExtension = Extension.create({ name: 'fontSize', addOptions() { return { types: ['textStyle'], } }, addGlobalAttributes() { return [ { types: this.options.types, attributes: { fontSize: { default: null, parseHTML: element => element.style.fontSize, renderHTML: attributes => { if (!attributes.fontSize) { return {} } return { style: `font-size: ${attributes.fontSize}`, } }, }, }, }, ] }, addCommands() { return { setFontSize: (fontSize: string) => ({ chain }) => { return chain() .setMark('textStyle', { fontSize: `${fontSize}px` }) .run() }, unsetFontSize: () => ({ chain }) => { return chain() .setMark('textStyle', { fontSize: null }) .run() }, } }, }) const Tiptap = ({ content, onChange }: { content: string, onChange: (richText: string) => void }) => { const editor = useEditor({ extensions: [ StarterKit.configure(), TextStyle.configure(), Color.configure(), UnderlineExtension, FontSizeExtension, ], content: content, editorProps: { attributes: { class: 'prose dark:prose-invert prose-sm sm:prose-base lg:prose-lg xl:prose-2xl m-5 focus:outline-none', }, }, onUpdate({ editor }) { onChange(editor.getHTML()) }, immediatelyRender: false, }) if (!editor) { return null } const handleFontSizeChange = (e: React.ChangeEvent) => { const size = e.target.value; if (size) { // @ts-ignore editor.chain().focus().setFontSize(size).run(); } else { // @ts-ignore editor.chain().focus().unsetFontSize().run(); } }; const handleColorChange = (e: React.ChangeEvent) => { editor.chain().focus().setColor(e.target.value).run() }; return (
) } export default Tiptap