소스 검색

133069 add early return

soumaeda 2 년 전
부모
커밋
fefa008dd3
1개의 변경된 파일5개의 추가작업 그리고 8개의 파일을 삭제
  1. 5 8
      packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-markdown-text.ts

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

@@ -15,16 +15,13 @@ export const useInsertMarkdownText = (view?: EditorView): InsertMarkdownText =>
       view?.state.selection.main.to,
     );
     const cursorPos = view?.state.selection.main.head;
-    let curPosAfterReplacing = {};
     const insertText = view?.state.replaceSelection(prefix + selection + suffix);
 
-    if (insertText) {
-      view?.dispatch(insertText);
-      if (cursorPos) {
-        curPosAfterReplacing = cursorPos + prefix.length;
-      }
-      view?.dispatch({ selection: { anchor: curPosAfterReplacing as number } });
-      view?.focus();
+    if (insertText == null || cursorPos == null) {
+      return;
     }
+    view?.dispatch(insertText);
+    view?.dispatch({ selection: { anchor: cursorPos + prefix.length } });
+    view?.focus();
   }, [view]);
 };