Selaa lähdekoodia

fix: only restore last editor's position if it's the same block

Tienson Qin 3 vuotta sitten
vanhempi
sitoutus
4eec369740

+ 9 - 6
src/main/frontend/handler/editor.cljs

@@ -175,12 +175,15 @@
 (defn restore-cursor-pos!
   [id markup]
   (when-let [node (gdom/getElement (str id))]
-    (when-let [cursor-range (state/get-cursor-range)]
-      (when-let [range cursor-range]
-        (let [pos (state/get-editor-last-pos)
-              pos (or pos (diff/find-position markup range))]
-          (cursor/move-cursor-to node pos)
-          (state/clear-editor-last-pos!))))))
+    (let [last-edit-block (state/get-last-edit-block)
+          current-edit-block (state/get-edit-block)]
+      (when-let [cursor-range (state/get-cursor-range)]
+        (when-let [range cursor-range]
+          (let [pos (when (= (:db/id last-edit-block) (:db/id current-edit-block))
+                      (state/get-editor-last-pos))
+                pos (or pos (diff/find-position markup range))]
+            (cursor/move-cursor-to node pos)
+            (state/clear-editor-last-pos!)))))))
 
 (defn highlight-block!
   [block-uuid]

+ 2 - 1
src/main/frontend/state.cljs

@@ -868,6 +868,8 @@
   ([edit-input-id content block cursor-range]
    (set-editing! edit-input-id content block cursor-range true))
   ([edit-input-id content block cursor-range move-cursor?]
+   (when-let [editing-block (get-edit-block)]
+     (swap! state assoc :editor/last-edit-block editing-block))
    (when (and edit-input-id block
               (or
                 (publishing-enable-editing?)
@@ -886,7 +888,6 @@
                     (assoc
                      :editor/block block
                      :editor/editing? {edit-input-id true}
-                     :editor/last-edit-block block
                      :editor/last-key-code nil
                      :cursor-range cursor-range))))
        (when-let [input (gdom/getElement edit-input-id)]

+ 1 - 2
src/main/logseq/api.cljs

@@ -523,8 +523,7 @@
     (let [block (state/get-edit-block)
           block (or block (some-> (first (state/get-selection-blocks))
                             (.getAttribute "blockid")
-                            (db-model/get-block-by-uuid)))
-          block (or block (state/get-last-edit-block))]
+                            (db-model/get-block-by-uuid)))]
       (get_block (:db/id block) opts))))
 
 (def ^:export get_previous_sibling_block