ソースを参照

use useCallback for editorSubstance

Yuki Takei 3 年 前
コミット
9e1bbbc123
1 ファイル変更10 行追加15 行削除
  1. 10 15
      packages/app/src/components/PageEditor/Editor.tsx

+ 10 - 15
packages/app/src/components/PageEditor/Editor.tsx

@@ -60,38 +60,33 @@ const Editor = React.forwardRef((props: EditorPropsType, ref): JSX.Element => {
   const cmEditorRef = useRef<CodeMirrorEditor>(null);
   const cmEditorRef = useRef<CodeMirrorEditor>(null);
   const taEditorRef = useRef<TextAreaEditor>(null);
   const taEditorRef = useRef<TextAreaEditor>(null);
 
 
-  const editorSubstance = isMobile ? taEditorRef.current : cmEditorRef.current;
+  const editorSubstance = useCallback(() => {
+    return isMobile ? taEditorRef.current : cmEditorRef.current;
+  }, [isMobile]);
 
 
   const methods: Partial<IEditorMethods> = useMemo(() => {
   const methods: Partial<IEditorMethods> = useMemo(() => {
     return {
     return {
       forceToFocus: () => {
       forceToFocus: () => {
-        if (editorSubstance == null) { return }
-        editorSubstance.forceToFocus();
+        editorSubstance()?.forceToFocus();
       },
       },
       setValue: (newValue: string) => {
       setValue: (newValue: string) => {
-        if (editorSubstance == null) { return }
-        editorSubstance.setValue(newValue);
+        editorSubstance()?.setValue(newValue);
       },
       },
       setGfmMode: (bool: boolean) => {
       setGfmMode: (bool: boolean) => {
-        if (editorSubstance == null) { return }
-        editorSubstance.setGfmMode(bool);
+        editorSubstance()?.setGfmMode(bool);
       },
       },
       setCaretLine: (line: number) => {
       setCaretLine: (line: number) => {
-        if (editorSubstance == null) { return }
-        editorSubstance.setCaretLine(line);
+        editorSubstance()?.setCaretLine(line);
       },
       },
       setScrollTopByLine: (line: number) => {
       setScrollTopByLine: (line: number) => {
-        if (editorSubstance == null) { return }
-        editorSubstance.setScrollTopByLine(line);
+        editorSubstance()?.setScrollTopByLine(line);
       },
       },
       insertText: (text: string) => {
       insertText: (text: string) => {
-        if (editorSubstance == null) { return }
-        editorSubstance.insertText(text);
+        editorSubstance()?.insertText(text);
       },
       },
       getNavbarItems: (): JSX.Element[] => {
       getNavbarItems: (): JSX.Element[] => {
-        if (editorSubstance == null) { return [] }
         // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
         // concat common items and items specific to CodeMirrorEditor or TextAreaEditor
-        const navbarItems = editorSubstance.getNavbarItems() ?? [];
+        const navbarItems = editorSubstance()?.getNavbarItems() ?? [];
         return navbarItems;
         return navbarItems;
       },
       },
     };
     };