Kaynağa Gözat

fix indent/outdent/move-up-down cursor when there's a link block

This commit also replaces setTimeout with rum/schedule
Tienson Qin 2 yıl önce
ebeveyn
işleme
128e03452f

+ 32 - 16
src/main/frontend/handler/editor.cljs

@@ -1105,7 +1105,7 @@
                           :block/uuid)]
       (let [pos (state/get-edit-pos)]
         (route-handler/redirect-to-page! id)
-        (js/setTimeout #(edit-block! {:block/uuid id} pos id) 50)))
+        (util/schedule #(edit-block! {:block/uuid id} pos id))))
     (js/window.history.forward)))
 
 (defn zoom-out!
@@ -1120,14 +1120,14 @@
                        (:block/uuid block-parent))]
             (do
               (route-handler/redirect-to-page! id)
-              (js/setTimeout #(edit-block! {:block/uuid block-id} :max block-id) 50))
+              (util/schedule #(edit-block! {:block/uuid block-id} :max block-id)))
             (let [page-id (some-> (db/entity [:block/uuid block-id])
                                   :block/page
                                   :db/id)]
 
               (when-let [page-name (:block/name (db/entity page-id))]
                 (route-handler/redirect-to-page! page-name)
-                (js/setTimeout #(edit-block! {:block/uuid block-id} :max block-id) 50)))))))
+                (util/schedule #(edit-block! {:block/uuid block-id} :max block-id))))))))
     (js/window.history.back)))
 
 (defn cut-block!
@@ -1684,12 +1684,17 @@
                          (.scrollIntoView block-node #js {:behavior "smooth" :block "nearest"})))]
       (if edit-block-id
         (when-let [block (db/pull [:block/uuid edit-block-id])]
-          (let [blocks [block]]
-            (move-nodes blocks))
-          (when-let [input-id (state/get-edit-input-id)]
-            (when-let [input (gdom/getElement input-id)]
-              (.focus input)
-              (js/setTimeout #(util/scroll-editor-cursor input) 100))))
+          (let [blocks [block]
+                pos (state/get-edit-pos)]
+            (move-nodes blocks)
+            (when-let [input-id (state/get-edit-input-id)]
+              (when-let [input (gdom/getElement input-id)]
+                (.focus input)
+                (util/scroll-editor-cursor input))
+              (util/schedule (fn []
+                               (when-not (gdom/getElement input-id)
+                                 ;; could be crossing containers
+                                 (edit-block! block pos (:block/uuid block))))))))
         (let [ids (state/get-selection-block-ids)]
           (when (seq ids)
             (let [lookup-refs (map (fn [id] [:block/uuid id]) ids)
@@ -1937,13 +1942,12 @@
 
 (defn- edit-last-block-after-inserted!
   [result]
-  (js/setTimeout
+  (util/schedule
    (fn []
      (when-let [last-block (last (:blocks result))]
        (clear-when-saved!)
        (let [last-block' (db/pull [:block/uuid (:block/uuid last-block)])]
-         (edit-block! last-block' :max (:block/uuid last-block')))))
-   0))
+         (edit-block! last-block' :max (:block/uuid last-block')))))))
 
 (defn- nested-blocks
   [blocks]
@@ -2184,13 +2188,17 @@
       (let [parent-node (tree/-get-parent node)
             target (if (parent-is-page? node)
                      original-block
-                     (:data parent-node))]
+                     (:data parent-node))
+            pos (state/get-edit-pos)
+            block (:data node)]
         (save-current-block!)
         (when target
           (outliner-tx/transact!
            {:outliner-op :move-blocks
             :real-outliner-op :indent-outdent}
-            (outliner-core/move-blocks! [(:data node)] target true)))))))
+           (outliner-core/move-blocks! [block] target true))
+          (when original-block
+            (util/schedule #(edit-block! block pos (:block/uuid block)))))))))
 
 (defn- last-top-level-child?
   [{:keys [id]} current-node]
@@ -2768,14 +2776,22 @@
   [indent?]
   (save-current-block!)
   (state/set-editor-op! :indent-outdent)
-  (let [pos (some-> (state/get-input) cursor/pos)
+  (let [editor (state/get-input)
+        crossing-container? (when editor
+                              (or (and (not indent?) (outliner-core/get-current-editing-original-block))
+                                  (and indent?
+                                       (when-let [sibling (db-model/get-prev-sibling (db/get-db) (:db/id (state/get-edit-block)))]
+                                         (some? (:block/link sibling))))))
+        pos (some-> editor cursor/pos)
         {:keys [block]} (get-state)]
     (when block
       (state/set-editor-last-pos! pos)
       (outliner-tx/transact!
        {:outliner-op :move-blocks
         :real-outliner-op :indent-outdent}
-       (outliner-core/indent-outdent-blocks! [block] indent?)))
+       (outliner-core/indent-outdent-blocks! [block] indent?))
+      (when crossing-container?
+        (util/schedule #(edit-block! block (state/get-edit-pos) (:block/uuid block)))))
     (state/set-editor-op! :nil)))
 
 (defn keydown-tab-handler

+ 7 - 7
src/main/frontend/modules/shortcut/config.cljs

@@ -73,10 +73,10 @@
 
    :whiteboard/select            {:binding ["1" "w s"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "select")}
-   
+
    :whiteboard/pan               {:binding ["2" "w p"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "move")}
-   
+
    :whiteboard/portal            {:binding ["3" "w b"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "logseq-portal")}
 
@@ -85,13 +85,13 @@
 
    :whiteboard/highlighter       {:binding ["5" "w h"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "highlighter")}
-   
+
    :whiteboard/eraser            {:binding ["6" "w e"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "erase")}
-   
+
    :whiteboard/connector         {:binding ["7" "w c"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "line")}
-   
+
    :whiteboard/text              {:binding ["8" "w t"]
                                   :fn      #(.selectTool ^js (state/active-tldraw-app) "text")}
 
@@ -332,7 +332,7 @@
 
    :editor/toggle-undo-redo-mode   {:binding false
                                     :fn      undo-redo/toggle-undo-redo-mode!}
-   
+
    :editor/toggle-number-list      {:binding "t n"
                                     :fn #(state/pub-event! [:editor/toggle-own-number-list (state/get-selection-block-ids)])}
 
@@ -895,7 +895,7 @@
     :whiteboard/group
     :whiteboard/ungroup
     :whiteboard/toggle-grid]
-   
+
    :shortcut.category/others
    [:pdf/previous-page
     :pdf/next-page

+ 38 - 40
src/main/frontend/state.cljs

@@ -1897,46 +1897,44 @@ Similar to re-frame subscriptions"
   ([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?]
-   (util/profile
-    "set-editing!"
-    (if (> (count content)
-           (block-content-max-length (get-current-repo)))
-      (let [elements (array-seq (js/document.getElementsByClassName (str "id" (:block/uuid block))))]
-        (when (first elements)
-          (util/scroll-to-element (gobj/get (first elements) "id")))
-        (exit-editing-and-set-selected-blocks! elements))
-      (when (and edit-input-id block
-                 (or
-                  (publishing-enable-editing?)
-                  (not @publishing?)))
-        (let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
-              container (util/get-block-container block-element)
-              block (if container
-                      (assoc block
-                             :block.temp/container (gobj/get container "id"))
-                      block)
-              content (string/trim (or content ""))]
-          (set-state! :editor/editing edit-input-id)
-          (swap! state
-                 (fn [state]
-                   (-> state
-                       (assoc
-                        :editor/set-timestamp-block nil
-                        :cursor-range cursor-range))))
-          (set-state! :editor/block block)
-          (set-state! :editor/content content :path-in-sub-atom edit-input-id)
-          (set-state! :editor/last-key-code nil)
-
-          (when-let [input (gdom/getElement edit-input-id)]
-            (let [pos (count cursor-range)]
-              (when content
-                (util/set-change-value input content))
-
-              (when move-cursor?
-                (cursor/move-cursor-to input pos))
-
-              (when (or (util/mobile?) (mobile-util/native-platform?))
-                (set-state! :mobile/show-action-bar? false))))))))))
+   (if (> (count content)
+          (block-content-max-length (get-current-repo)))
+     (let [elements (array-seq (js/document.getElementsByClassName (str "id" (:block/uuid block))))]
+       (when (first elements)
+         (util/scroll-to-element (gobj/get (first elements) "id")))
+       (exit-editing-and-set-selected-blocks! elements))
+     (when (and edit-input-id block
+                (or
+                 (publishing-enable-editing?)
+                 (not @publishing?)))
+       (let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
+             container (util/get-block-container block-element)
+             block (if container
+                     (assoc block
+                            :block.temp/container (gobj/get container "id"))
+                     block)
+             content (string/trim (or content ""))]
+         (set-state! :editor/editing edit-input-id)
+         (swap! state
+                (fn [state]
+                  (-> state
+                      (assoc
+                       :editor/set-timestamp-block nil
+                       :cursor-range cursor-range))))
+         (set-state! :editor/block block)
+         (set-state! :editor/content content :path-in-sub-atom edit-input-id)
+         (set-state! :editor/last-key-code nil)
+
+         (when-let [input (gdom/getElement edit-input-id)]
+           (let [pos (count cursor-range)]
+             (when content
+               (util/set-change-value input content))
+
+             (when move-cursor?
+               (cursor/move-cursor-to input pos))
+
+             (when (or (util/mobile?) (mobile-util/native-platform?))
+               (set-state! :mobile/show-action-bar? false)))))))))
 
 (defn remove-watch-state [key]
   (remove-watch state key))

+ 10 - 0
src/main/frontend/util.cljc

@@ -1538,3 +1538,13 @@ Arg *stop: atom, reset to true to stop the loop"
      (if (satisfies? IMeta o)
        (with-meta o meta)
        o)))
+
+   ;; from rum
+#?(:cljs
+   (def schedule
+     (or (and (exists? js/window)
+              (or js/window.requestAnimationFrame
+                  js/window.webkitRequestAnimationFrame
+                  js/window.mozRequestAnimationFrame
+                  js/window.msRequestAnimationFrame))
+         #(js/setTimeout % 16))))