Просмотр исходного кода

132486 add space when no sharp

soumaeda 2 лет назад
Родитель
Сommit
3b2ab10078

+ 23 - 16
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-header.ts

@@ -7,26 +7,33 @@ export type InsertHeader = (prefix: string) => void;
 export const useInsertHeader = (view?: EditorView): InsertHeader => {
 
   return useCallback((prefix) => {
+
+    if (view == null) {
+      return;
+    }
+
     const selection = view?.state.sliceDoc(
-      view?.state.selection.main.from,
-      view?.state.selection.main.to,
+      view.state.selection.main.from,
+      view.state.selection.main.to,
     );
 
-    const cursorPos = view?.state.selection.main.head;
-    const space = ' ';
-    const insertText = prefix + space;
-
-    if (insertText && cursorPos) {
-      view.dispatch({
-        changes: {
-          from: view?.state.selection.main.from,
-          to: view?.state.selection.main.from,
-          insert: insertText + selection,
-        },
-        selection: { anchor: cursorPos + insertText.length },
-      });
+    const cursorPos = view.state.selection.main.head;
+    const line = view.state.doc.lineAt(cursorPos);
+    const insertPos = line.text.startsWith('#') ? cursorPos - 1 : cursorPos;
+    let insertText = prefix;
+
+    if (!line.text.startsWith('#')) {
+      insertText += ' ';
     }
+
+    view.dispatch({
+      changes: {
+        from: insertPos,
+        to: insertPos,
+        insert: insertText + selection,
+      },
+      selection: { anchor: cursorPos + insertText.length },
+    });
     view?.focus();
   }, [view]);
-
 };