1
0

richTextInput.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Editor, EditorContent, Extension, useEditor } from '@tiptap/react';
  2. import React, { useCallback, useEffect, useMemo, useRef } from 'react';
  3. import Document from '@tiptap/extension-document';
  4. import Text from '@tiptap/extension-text';
  5. import { UndoRedo } from '@tiptap/extensions';
  6. import Paragraph from '@tiptap/extension-paragraph';
  7. import HardBreak from '@tiptap/extension-hard-break';
  8. import { Placeholder } from '@tiptap/extensions';
  9. import InputSlot from './extension/inputSlot';
  10. import SelectSlot from './extension/selectSlot';
  11. import SkillSlot from './extension/skillSlot';
  12. import { strings } from '@douyinfe/semi-foundation/aiChatInput/constants';
  13. import { Content as TiptapContent } from "@tiptap/core";
  14. import { cssClasses } from '@douyinfe/semi-foundation/aiChatInput/constants';
  15. import { EditorView } from '@tiptap/pm/view';
  16. import { handleCompositionEndLogic, handlePasteLogic, handleTextInputLogic, handleZeroWidthCharLogic } from './extension/plugins';
  17. const PREFIX = cssClasses.PREFIX;
  18. export default (props: {
  19. innerRef?: React.Ref<HTMLDivElement>;
  20. defaultContent?: TiptapContent;
  21. placeholder?: string;
  22. setEditor?: (editor: Editor) => void;
  23. onKeyDown?: (e: KeyboardEvent) => void;
  24. onChange?: (content: string) => void;
  25. extensions?: Extension[];
  26. handleKeyDown?: (view: any, event: KeyboardEvent) => boolean;
  27. onPaste?: (files: File[]) => void;
  28. onFocus?: (event: FocusEvent) => void;
  29. onBlur?: (event: FocusEvent) => void;
  30. handleCreate?: () => void
  31. }) => {
  32. const { setEditor, onKeyDown, onChange, placeholder, extensions = [],
  33. defaultContent, onPaste, innerRef, handleKeyDown, onFocus, onBlur, handleCreate } = props;
  34. const isComposing = useRef(false);
  35. const handleCompositionStart = useCallback((view: EditorView) => {
  36. isComposing.current = true;
  37. }, []);
  38. const handleCompositionEnd = useCallback((view: EditorView) => {
  39. isComposing.current = false;
  40. handleCompositionEndLogic(view);
  41. }, []);
  42. const handleTextInput = useCallback((view: EditorView, from: number, to: number, text: string) => {
  43. if (isComposing.current) {
  44. return false;
  45. }
  46. return handleTextInputLogic(view, from, to, text);
  47. }, []);
  48. const allExtensions = useMemo(() => {
  49. return [
  50. Document, Paragraph, Text, UndoRedo, HardBreak,
  51. InputSlot, SelectSlot, SkillSlot,
  52. Placeholder.configure({
  53. placeholder: placeholder,
  54. }),
  55. ...extensions,
  56. ];
  57. }, [extensions, placeholder]);
  58. const editorProps = useMemo(() => {
  59. return {
  60. handleKeyDown: handleKeyDown,
  61. handlePaste: handlePasteLogic,
  62. handleTextInput,
  63. handleDOMEvents: {
  64. compositionstart: handleCompositionStart,
  65. compositionend: handleCompositionEnd,
  66. }
  67. };
  68. }, [handleKeyDown, handleTextInput, handleCompositionStart, handleCompositionEnd]);
  69. // const onSelectionUpdate = useCallback(({ editor }) => {
  70. // // For debug
  71. // const fromPos = editor.state.selection.from;
  72. // const { $from } = editor.state.selection;
  73. // console.log('光标/选区位置', fromPos, editor.state.selection, editor.state.doc);
  74. // // console.log('before', $from.nodeBefore, $from.nodeAfter);
  75. // }, []);
  76. const onCreate = useCallback(({ editor }) => {
  77. const { state, view } = editor;
  78. const tr = handleZeroWidthCharLogic(state);
  79. if (tr) {
  80. // 一次性触发,避免多次触发导致 appendTransaction 被多次调用
  81. view.dispatch(tr);
  82. }
  83. handleCreate();
  84. }, [handleCreate]);
  85. const onUpdate = useCallback(({ editor }) => {
  86. // The content has changed.
  87. const content = editor.getText();
  88. onChange(content);
  89. }, [onChange]);
  90. const handlePaste = useCallback((e) => {
  91. // To support file paste
  92. const items = e.clipboardData?.items as any;
  93. let files = [];
  94. if (items) {
  95. for (const it of items) {
  96. const file = it.getAsFile();
  97. file && files.push(it.getAsFile());
  98. }
  99. }
  100. if (files.length) {
  101. onPaste?.(files);
  102. }
  103. }, [onPaste]);
  104. const editor = useEditor({
  105. extensions: allExtensions as Extension[],
  106. content: defaultContent ?? ``,
  107. editorProps: editorProps,
  108. // onSelectionUpdate,
  109. onCreate,
  110. onUpdate,
  111. onPaste: handlePaste,
  112. });
  113. useEffect(() => {
  114. setEditor(editor);
  115. }, [editor, setEditor]);
  116. return (<>
  117. <EditorContent
  118. editor={editor}
  119. onKeyDown={onKeyDown as any}
  120. onFocus={onFocus as any}
  121. onBlur={onBlur as any}
  122. ref={innerRef}
  123. className={`${PREFIX}-editor-content`}
  124. />
  125. </>);
  126. };