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

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

@@ -7,9 +7,12 @@ export type InsertPrefix = (prefix: string) => void;
 export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
 
   return useCallback((prefix) => {
-    const selection = view?.state.sliceDoc(
-      view?.state.selection.main.from,
-      view?.state.selection.main.to,
+    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;
@@ -19,14 +22,14 @@ export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
     if (insertText && cursorPos) {
       view.dispatch({
         changes: {
-          from: view?.state.selection.main.from,
-          to: view?.state.selection.main.to,
+          from: view.state.selection.main.from,
+          to: view.state.selection.main.to,
           insert: insertText + selection,
         },
         selection: { anchor: cursorPos + insertText.length },
       });
     }
-    view?.focus();
+    view.focus();
   }, [view]);
 
 };