editor.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use client'
  2. import {
  3. memo,
  4. useCallback,
  5. } from 'react'
  6. import type { EditorState } from 'lexical'
  7. import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin'
  8. import { ContentEditable } from '@lexical/react/LexicalContentEditable'
  9. import { ClickableLinkPlugin } from '@lexical/react/LexicalClickableLinkPlugin'
  10. import { LinkPlugin } from '@lexical/react/LexicalLinkPlugin'
  11. import { ListPlugin } from '@lexical/react/LexicalListPlugin'
  12. import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary'
  13. import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin'
  14. import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'
  15. import LinkEditorPlugin from './plugins/link-editor-plugin'
  16. import FormatDetectorPlugin from './plugins/format-detector-plugin'
  17. // import TreeView from '@/app/components/base/prompt-editor/plugins/tree-view'
  18. import Placeholder from '@/app/components/base/prompt-editor/plugins/placeholder'
  19. type EditorProps = {
  20. placeholder?: string
  21. onChange?: (editorState: EditorState) => void
  22. containerElement: HTMLDivElement | null
  23. }
  24. const Editor = ({
  25. placeholder = 'write you note...',
  26. onChange,
  27. containerElement,
  28. }: EditorProps) => {
  29. const handleEditorChange = useCallback((editorState: EditorState) => {
  30. onChange?.(editorState)
  31. }, [onChange])
  32. return (
  33. <div className='relative'>
  34. <RichTextPlugin
  35. contentEditable={
  36. <div>
  37. <ContentEditable
  38. spellCheck={false}
  39. className='w-full h-full outline-none caret-primary-600'
  40. placeholder={placeholder}
  41. />
  42. </div>
  43. }
  44. placeholder={<Placeholder value={placeholder} compact />}
  45. ErrorBoundary={LexicalErrorBoundary}
  46. />
  47. <ClickableLinkPlugin disabled />
  48. <LinkPlugin />
  49. <ListPlugin />
  50. <LinkEditorPlugin containerElement={containerElement} />
  51. <FormatDetectorPlugin />
  52. <HistoryPlugin />
  53. <OnChangePlugin onChange={handleEditorChange} />
  54. {/* <TreeView /> */}
  55. </div>
  56. )
  57. }
  58. export default memo(Editor)