Browse Source

Use useRef for editor.

oldj 3 years ago
parent
commit
0bcc4803ab
2 changed files with 37 additions and 45 deletions
  1. 1 1
      .prettierrc.json
  2. 36 44
      src/renderer/components/Editor/HostsEditor.tsx

+ 1 - 1
.prettierrc.json

@@ -7,7 +7,7 @@
   "insertPragma": false,
   "insertPragma": false,
   "jsxBracketSameLine": false,
   "jsxBracketSameLine": false,
   "jsxSingleQuote": false,
   "jsxSingleQuote": false,
-  "printWidth": 80,
+  "printWidth": 100,
   "proseWrap": "preserve",
   "proseWrap": "preserve",
   "quoteProps": "as-needed",
   "quoteProps": "as-needed",
   "requirePragma": false,
   "requirePragma": false,

+ 36 - 44
src/renderer/components/Editor/HostsEditor.tsx

@@ -37,34 +37,36 @@ const HostsEditor = (props: Props) => {
   const [hosts_id, setHostsId] = useState(hosts.id)
   const [hosts_id, setHostsId] = useState(hosts.id)
   const [content, setContent] = useState(hosts.content || '')
   const [content, setContent] = useState(hosts.content || '')
   const [is_read_only, setIsReadOnly] = useState(true)
   const [is_read_only, setIsReadOnly] = useState(true)
-  const [cm_editor, setCMEditor] =
-    useState<CodeMirror.EditorFromTextArea | null>(null)
-  const el_ref = useRef<HTMLTextAreaElement>(null)
-  const [find_params, setFindParams] = useState<IFindShowSourceParam | null>(
-    null,
-  )
+  const [find_params, setFindParams] = useState<IFindShowSourceParam | null>(null)
+  const ref_el = useRef<HTMLTextAreaElement>(null)
+  const ref_cm = useRef<CodeMirror.EditorFromTextArea | null>(null)
 
 
   const loadContent = async () => {
   const loadContent = async () => {
-    if (!cm_editor) return
+    let cm_editor = ref_cm.current
+    if (!cm_editor) {
+      setTimeout(loadContent, 100)
+      return
+    }
 
 
     let content =
     let content =
-      hosts.id === '0'
-        ? await actions.getSystemHosts()
-        : await actions.getHostsContent(hosts.id)
+      hosts_id === '0' ? await actions.getSystemHosts() : await actions.getHostsContent(hosts_id)
     setContent(content)
     setContent(content)
     cm_editor.setValue(content)
     cm_editor.setValue(content)
     cm_editor.setOption('readOnly', isReadOnly(hosts))
     cm_editor.setOption('readOnly', isReadOnly(hosts))
   }
   }
 
 
   useEffect(() => {
   useEffect(() => {
-    loadContent().catch((e) => console.error(e))
-  }, [hosts_id, cm_editor])
+    loadContent()
+      .then(() => {
+        let cm_editor = ref_cm.current
+        if (cm_editor) {
+          cm_editor.clearHistory()
+        }
+      })
+      .catch((e) => console.error(e))
+  }, [hosts_id])
 
 
   useEffect(() => {
   useEffect(() => {
-    if (cm_editor && hosts_id !== hosts.id) {
-      setTimeout(() => cm_editor.clearHistory(), 300)
-    }
-
     setHostsId(hosts.id)
     setHostsId(hosts.id)
     setIsReadOnly(isReadOnly(hosts))
     setIsReadOnly(isReadOnly(hosts))
   }, [hosts])
   }, [hosts])
@@ -85,6 +87,7 @@ const HostsEditor = (props: Props) => {
   }
   }
 
 
   const toggleComment = () => {
   const toggleComment = () => {
+    let cm_editor = ref_cm.current
     if (is_read_only || !cm_editor) return
     if (is_read_only || !cm_editor) return
     cm_editor.toggleComment()
     cm_editor.toggleComment()
 
 
@@ -95,6 +98,7 @@ const HostsEditor = (props: Props) => {
   }
   }
 
 
   const onGutterClick = (n: number) => {
   const onGutterClick = (n: number) => {
+    let cm_editor = ref_cm.current
     if (is_read_only || !cm_editor) return
     if (is_read_only || !cm_editor) return
 
 
     let info = cm_editor.lineInfo(n)
     let info = cm_editor.lineInfo(n)
@@ -110,22 +114,18 @@ const HostsEditor = (props: Props) => {
 
 
     cm_editor
     cm_editor
       .getDoc()
       .getDoc()
-      .replaceRange(
-        new_line,
-        { line: info.line, ch: 0 },
-        { line: info.line, ch: line.length },
-      )
+      .replaceRange(new_line, { line: info.line, ch: 0 }, { line: info.line, ch: line.length })
   }
   }
 
 
   useEffect(() => {
   useEffect(() => {
-    if (!el_ref.current) return
+    if (!ref_el.current) return
 
 
-    let cm = CodeMirror.fromTextArea(el_ref.current, {
+    let cm = CodeMirror.fromTextArea(ref_el.current, {
       lineNumbers: true,
       lineNumbers: true,
       readOnly: is_read_only,
       readOnly: is_read_only,
       mode: 'hosts',
       mode: 'hosts',
     })
     })
-    setCMEditor(cm)
+    ref_cm.current = cm
 
 
     cm.setSize('100%', '100%')
     cm.setSize('100%', '100%')
 
 
@@ -154,18 +154,13 @@ const HostsEditor = (props: Props) => {
     [hosts, hosts_id, content],
     [hosts, hosts_id, content],
   )
   )
 
 
-  useOnBroadcast(events.editor_gutter_click, onGutterClick, [
-    cm_editor,
-    is_read_only,
-  ])
-
   useOnBroadcast(
   useOnBroadcast(
     events.hosts_refreshed,
     events.hosts_refreshed,
     (h: IHostsListObject) => {
     (h: IHostsListObject) => {
-      if (hosts.id !== '0' && h.id !== hosts.id) return
+      if (hosts_id !== '0' && h.id !== hosts_id) return
       loadContent().catch((e) => console.error(e))
       loadContent().catch((e) => console.error(e))
     },
     },
-    [hosts, hosts_data, cm_editor],
+    [hosts_id],
   )
   )
 
 
   useOnBroadcast(
   useOnBroadcast(
@@ -174,14 +169,9 @@ const HostsEditor = (props: Props) => {
       if (hosts.id !== '0' && id !== hosts.id) return
       if (hosts.id !== '0' && id !== hosts.id) return
       loadContent().catch((e) => console.error(e))
       loadContent().catch((e) => console.error(e))
     },
     },
-    [hosts, hosts_data, cm_editor],
+    [hosts, hosts_data],
   )
   )
 
 
-  useOnBroadcast(events.toggle_comment, toggleComment, [
-    cm_editor,
-    is_read_only,
-  ])
-
   useOnBroadcast(
   useOnBroadcast(
     events.set_hosts_on_status,
     events.set_hosts_on_status,
     () => {
     () => {
@@ -189,10 +179,14 @@ const HostsEditor = (props: Props) => {
         loadContent().catch((e) => console.error(e))
         loadContent().catch((e) => console.error(e))
       }
       }
     },
     },
-    [hosts, cm_editor],
+    [hosts],
   )
   )
 
 
+  useOnBroadcast(events.editor_gutter_click, onGutterClick, [is_read_only])
+  useOnBroadcast(events.toggle_comment, toggleComment, [is_read_only])
+
   const setSelection = async (params: IFindShowSourceParam) => {
   const setSelection = async (params: IFindShowSourceParam) => {
+    let cm_editor = ref_cm.current
     if (!cm_editor) return
     if (!cm_editor) return
     let doc = cm_editor.getDoc()
     let doc = cm_editor.getDoc()
 
 
@@ -218,7 +212,7 @@ const HostsEditor = (props: Props) => {
   useOnBroadcast(
   useOnBroadcast(
     events.show_source,
     events.show_source,
     async (params: IFindShowSourceParam) => {
     async (params: IFindShowSourceParam) => {
-      if (!cm_editor) return
+      if (!ref_cm.current) return
 
 
       if (params.item_id !== hosts.id) {
       if (params.item_id !== hosts.id) {
         setFindParams(params)
         setFindParams(params)
@@ -230,16 +224,14 @@ const HostsEditor = (props: Props) => {
 
 
       setSelection(params).catch((e) => console.error(e))
       setSelection(params).catch((e) => console.error(e))
     },
     },
-    [hosts, cm_editor],
+    [hosts],
   )
   )
 
 
   return (
   return (
     <div className={styles.root}>
     <div className={styles.root}>
-      <div
-        className={clsx(styles.editor, is_read_only && styles.read_only_tag)}
-      >
+      <div className={clsx(styles.editor, is_read_only && styles.read_only_tag)}>
         <textarea
         <textarea
-          ref={el_ref}
+          ref={ref_el}
           defaultValue={content}
           defaultValue={content}
           // onChange={e => onChange(e.target.value)}
           // onChange={e => onChange(e.target.value)}
           // disabled={is_read_only}
           // disabled={is_read_only}