Przeglądaj źródła

fix modules to ignore "TypeError: this is undefined"

Yuki Takei 2 lat temu
rodzic
commit
ea98e11d42

+ 3 - 5
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/append-extension.ts

@@ -8,11 +8,9 @@ export type AppendExtension = (extension: Extension) => CleanupFunction | undefi
 
 export const useAppendExtension = (view?: EditorView): AppendExtension => {
 
-  const { dispatch } = view ?? {};
-
   return useCallback((extension) => {
     const compartment = new Compartment();
-    dispatch?.({
+    view?.dispatch({
       effects: StateEffect.appendConfig.of(
         compartment.of(extension),
       ),
@@ -20,10 +18,10 @@ export const useAppendExtension = (view?: EditorView): AppendExtension => {
 
     // return cleanup function
     return () => {
-      dispatch?.({
+      view?.dispatch({
         effects: compartment.reconfigure([]),
       });
     };
-  }, [dispatch]);
+  }, [view]);
 
 };

+ 0 - 2
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/focus.ts

@@ -8,8 +8,6 @@ export const useFocus = (view?: EditorView): Focus => {
 
   return useCallback(() => {
     view?.focus?.();
-    // compromise to put the view into deps
-    //   in order to ignore "TypeError: this is undefined" caused by invoking focus()
   }, [view]);
 
 };

+ 2 - 4
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/get-doc.ts

@@ -6,10 +6,8 @@ export type GetDoc = () => string;
 
 export const useGetDoc = (view?: EditorView): GetDoc => {
 
-  const doc = view?.state.doc ?? '';
-
   return useCallback(() => {
-    return doc.toString();
-  }, [doc]);
+    return view?.state.doc.toString() ?? '';
+  }, [view]);
 
 };

+ 3 - 6
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/init-doc.ts

@@ -7,18 +7,15 @@ export type InitDoc = (doc?: string) => void;
 
 export const useInitDoc = (view?: EditorView): InitDoc => {
 
-  const { dispatch } = view ?? {};
-  const docLength = view?.state.doc.length;
-
   return useCallback((doc) => {
-    dispatch?.({
+    view?.dispatch({
       changes: {
         from: 0,
-        to: docLength,
+        to: view?.state.doc.length,
         insert: doc,
       },
       annotations: Transaction.addToHistory.of(false),
     });
-  }, [docLength, dispatch]);
+  }, [view]);
 
 };

+ 5 - 9
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/set-caret-line.ts

@@ -6,16 +6,15 @@ export type SetCaretLine = (lineNumber?: number) => void;
 
 export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
 
-  const dispatch = view?.dispatch;
-  const doc = view?.state.doc;
-
   return useCallback((lineNumber) => {
-    if (dispatch == null || doc == null) {
+    const doc = view?.state.doc;
+
+    if (doc == null) {
       return;
     }
 
     const posOfLineEnd = doc.line(lineNumber ?? 1).to;
-    dispatch({
+    view?.dispatch({
       selection: {
         anchor: posOfLineEnd,
         head: posOfLineEnd,
@@ -23,9 +22,6 @@ export const useSetCaretLine = (view?: EditorView): SetCaretLine => {
     });
     // focus
     view?.focus();
-
-    // compromise to put the view into deps
-    //   in order to ignore "TypeError: this is undefined" caused by invoking focus()
-  }, [dispatch, doc, view]);
+  }, [view]);
 
 };