Преглед на файлове

134164 add prefix to line head

soumaeda преди 2 години
родител
ревизия
b87d5aa0e9
променени са 1 файла, в които са добавени 3 реда и са изтрити 9 реда
  1. 3 9
      packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-prefix.ts

+ 3 - 9
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-prefix.ts

@@ -10,25 +10,19 @@ export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
     if (view == null) {
       return;
     }
-    const selection = view.state.sliceDoc(
-      view.state.selection.main.from,
-      view.state.selection.main.to,
-    );
 
     const cursorPos = view.state.selection.main.head;
     const space = ' ';
     const line = view.state.doc.lineAt(cursorPos);
     const insertText = isContinuous && line.text.startsWith(prefix) ? prefix : prefix + space;
-    const insertPos = isContinuous && line.text.startsWith(prefix) ? cursorPos - 1 : cursorPos;
-    const afterInsertPos = cursorPos + insertText.length + selection.length;
 
     view.dispatch({
       changes: {
-        from: insertPos,
-        to: insertPos,
+        from: line.from,
+        to: line.from,
         insert: insertText,
       },
-      selection: { anchor: afterInsertPos, head: afterInsertPos - selection.length },
+      selection: { anchor: line.from + line.text.length + insertText.length },
     });
     view.focus();
   }, [view]);