kosei-n 2 лет назад
Родитель
Сommit
71f7c15798

+ 2 - 4
packages/editor/src/components/CodeMirrorEditor/CodeMirrorEditor.tsx

@@ -58,15 +58,13 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
   const newlineAndIndentContinueMarkdownList = useNewlineAndIndentContinueMarkdownList(editor);
 
   useEffect(() => {
-
     const handleEnterKey = (event: KeyboardEvent) => {
       if (event.key === 'Enter') {
         event.preventDefault();
-        const editor = codeMirrorEditor?.view;
         if (editor == null) {
           return;
         }
-        newlineAndIndentContinueMarkdownList();
+        newlineAndIndentContinueMarkdownList?.(editor);
       }
     };
 
@@ -75,7 +73,7 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
     return () => {
       editor?.dom.removeEventListener('keydown', handleEnterKey);
     };
-  }, [codeMirrorEditor]);
+  }, [codeMirrorEditor, editor, newlineAndIndentContinueMarkdownList]);
 
   useEffect(() => {
     if (indentSize == null) {

+ 13 - 8
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-text.ts

@@ -1,23 +1,28 @@
 import { useCallback } from 'react';
 
-import { EditorView } from '@codemirror/view';
+import { type EditorView } from '@codemirror/view';
 
 export type InsertText = (text: string, from?: number, to?: number) => void;
 
 
-export const useInsertText = (view: EditorView): InsertText => {
-  const insertPos = view.state.selection.main.head;
+export const useInsertText = (view?: EditorView): InsertText => {
+  return useCallback((text, from?, to?) => {
+    if (view == null) {
+      return;
+    }
+    const insertPos = view.state.selection.main.head;
 
-  return useCallback((text, from = insertPos, to = insertPos) => {
+    const fromPos = from ?? insertPos;
+    const toPos = to ?? insertPos;
 
     view.dispatch({
       changes: {
-        from,
-        to,
+        from: fromPos,
+        to: toPos,
         insert: text,
       },
-      selection: { anchor: from },
+      // selection: { anchor: fromPos },
     });
-  }, [insertPos, view]);
+  }, [view]);
 
 };

+ 13 - 20
packages/editor/src/services/list-util/markdown-list-util.ts

@@ -5,7 +5,7 @@ import { EditorView } from '@codemirror/view';
 import { useInsertText } from '../codemirror-editor/use-codemirror-editor/utils/insert-text';
 
 
-export type NewlineAndIndentContinueMarkdownList = () => void;
+export type NewlineAndIndentContinueMarkdownList = ((editor: EditorView) => void) | undefined;
 
 // https://regex101.com/r/7BN2fR/5
 const indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
@@ -21,37 +21,30 @@ export const getLineToCursor = (editor: EditorView, lineNumBeforeCursor = 0): st
   return editor.state.sliceDoc(firstLineToGet, curPos);
 };
 
-
-// const insertText = (editor: EditorView, text: string) => {
-//   const curPos = editor.state.selection.main.head;
-//   const line = editor.state.doc.lineAt(curPos).from;
-//   editor.dispatch({
-//     changes: {
-//       from: line,
-//       to: curPos,
-//       insert: text,
-//     },
-//   });
-// };
-
-export const useNewlineAndIndentContinueMarkdownList = (editor: EditorView): NewlineAndIndentContinueMarkdownList => {
+export const useNewlineAndIndentContinueMarkdownList = (editor?: EditorView): NewlineAndIndentContinueMarkdownList => {
   const insertText = useInsertText(editor);
 
-  const curPos = editor.state.selection.main.head;
-  const line = editor.state.doc.lineAt(curPos).from;
+  const NewlineAndIndentContinueMarkdownList = useCallback((view: EditorView) => {
 
-  return useCallback(() => {
+    const curPos = view?.state.selection.main.head;
+    const line = view?.state.doc.lineAt(curPos).from;
 
-    const getStrFromAboveLine = getLineToCursor(editor, 1);
+    const getStrFromAboveLine = getLineToCursor(view, 1);
 
     const matchResult = getStrFromAboveLine.match(indentAndMarkRE);
 
     if (matchResult != null) {
       const indentAndMark = matchResult[0];
       insertText(indentAndMark, line, curPos);
+      // insertText(editor, indentAndMark);
     }
-  }, [curPos, editor, insertText, line]);
+  }, [insertText]);
+
+  if (editor == null) {
+    return;
+  }
 
+  return () => { NewlineAndIndentContinueMarkdownList(editor) };
 };
 
 export const adjustPasteData = (indentAndMark: string, text: string): string => {