Browse Source

fix: potential fix for #2249

Tienson Qin 4 years ago
parent
commit
66d2997ca9
2 changed files with 30 additions and 28 deletions
  1. 28 27
      src/main/frontend/diff.cljs
  2. 2 1
      src/main/frontend/handler/editor/lifecycle.cljs

+ 28 - 27
src/main/frontend/diff.cljs

@@ -33,30 +33,31 @@
 ;; (find-position "** hello _w_" "hello w")
 (defn find-position
   [markup text]
-  (try
-    (let [pos (loop [t1 (-> markup string/lower-case seq)
-                     t2 (-> text   string/lower-case seq)
-                     i1 0
-                     i2 0]
-                (let [[h1 & r1] t1
-                      [h2 & r2] t2]
-                  (cond
-                    (or (empty? t1) (empty? t2))
-                    i1
-
-                    (= h1 h2)
-                    (recur r1 r2 (inc i1) (inc i2))
-
-                    (#{\[ \space \]} h2)
-                    (recur t1 r2 i1 (inc i2))
-
-                    :else
-                    (recur r1 t2 (inc i1) i2))))]
-      (if (and (= (util/nth-safe markup pos)
-                  (util/nth-safe markup (inc pos))
-                  "]"))
-        (+ pos 2)
-        pos))
-    (catch js/Error e
-      (log/error :diff/find-position {:error e})
-      (count markup))))
+  (when (and (string? markup) (string? text))
+    (try
+      (let [pos (loop [t1 (-> markup string/lower-case seq)
+                       t2 (-> text   string/lower-case seq)
+                       i1 0
+                       i2 0]
+                  (let [[h1 & r1] t1
+                        [h2 & r2] t2]
+                    (cond
+                      (or (empty? t1) (empty? t2))
+                      i1
+
+                      (= h1 h2)
+                      (recur r1 r2 (inc i1) (inc i2))
+
+                      (#{\[ \space \]} h2)
+                      (recur t1 r2 i1 (inc i2))
+
+                      :else
+                      (recur r1 t2 (inc i1) i2))))]
+        (if (and (= (util/nth-safe markup pos)
+                    (util/nth-safe markup (inc pos))
+                    "]"))
+          (+ pos 2)
+          pos))
+      (catch js/Error e
+        (log/error :diff/find-position {:error e})
+        (count markup)))))

+ 2 - 1
src/main/frontend/handler/editor/lifecycle.cljs

@@ -17,7 +17,8 @@
         content (get-in @state/state [:editor/content id])]
     (when block-parent-id
       (state/set-editing-block-dom-id! block-parent-id))
-    (editor-handler/restore-cursor-pos! id content)
+    (when content
+      (editor-handler/restore-cursor-pos! id content))
 
     ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
     ;; which will hide the editor so no way for editing.