Browse Source

refactor(shortcuts): up/down to navigate to the first/last block

Non-editing mode.
Tienson Qin 4 years ago
parent
commit
57826e0573

+ 22 - 13
src/main/frontend/handler/editor.cljs

@@ -2167,26 +2167,35 @@
   [block-id value]
   (set-block-property! block-id "heading" value))
 
-;; Should preserve the cursor too.
-(defn open-last-block!
-  [journal?]
-  (let [edit-id (state/get-edit-input-id)
-        last-pos (state/get-edit-pos)
-        block-id (when edit-id (subs edit-id (- (count edit-id) 36)))]
-    (let [last-edit-block (first (array-seq (js/document.getElementsByClassName block-id)))
-          first-block (first (array-seq (js/document.getElementsByClassName "ls-block")))
-          node (or last-edit-block
-                   (and (not journal?) first-block))]
+(defn open-block!
+  [first?]
+  (when-not (state/editing?)
+    (let [edit-id (state/get-last-edit-input-id)
+          block-id (when edit-id (subs edit-id (- (count edit-id) 36)))
+          last-edit-block (first (array-seq (js/document.getElementsByClassName block-id)))
+          nodes (array-seq (js/document.getElementsByClassName "ls-block"))
+          first-node (first nodes)
+          node (cond
+                 last-edit-block
+                 last-edit-block
+                 first?
+                 first-node
+                 :else
+                 (when-let [blocks-container (util/rec-get-blocks-container first-node)]
+                   (let [nodes (dom/by-class blocks-container "ls-block")]
+                     (last nodes))))]
       (when node
+        (state/clear-selection!)
+        (unhighlight-block!)
         (let [block-id (and node (d/attr node "blockid"))
               edit-block-id (string/replace (gobj/get node "id") "ls-block" "edit-block")
               block-id (medley/uuid block-id)]
           (when-let [block (db/entity [:block/uuid block-id])]
             (edit-block! block
-                         (or (and last-edit-block last-pos)
-                             :max)
+                         :max
                          (:block/format block)
-                         edit-block-id)))))))
+                         edit-block-id))))
+      false)))
 
 (defn get-search-q
   []

+ 4 - 0
src/main/frontend/keyboards.cljs

@@ -53,6 +53,10 @@
    (or (state/get-shortcut :editor/move-block-up) "alt+shift+up") [(fn [state e] (editor-handler/move-up-down e true)) true]
    (or (state/get-shortcut :editor/move-block-down) "alt+shift+down") [(fn [state e] (editor-handler/move-up-down e false)) true]
    (or (state/get-shortcut :editor/save) "mod+s") [editor-handler/save! true]
+
+   (or (state/get-shortcut :editor/next) "down") (fn [state e] (editor-handler/open-block! true))
+   (or (state/get-shortcut :editor/prev) "up") (fn [state e] (editor-handler/open-block! false))
+
    (or (state/get-shortcut :search/re-index) "mod+c mod+s") [search-handler/rebuild-indices! true]
    (or (state/get-shortcut :ui/toggle-brackets) "mod+c mod+b") [config-handler/toggle-ui-show-brackets! true]})
 

+ 6 - 0
src/main/frontend/state.cljs

@@ -71,6 +71,7 @@
     :editor/show-input nil
     :editor/last-saved-cursor nil
     :editor/editing? nil
+    :editor/last-edit-block-id nil
     :editor/in-composition? false
     :editor/pos 0
     :editor/content {}
@@ -371,6 +372,10 @@
   []
   (ffirst (:editor/editing? @state)))
 
+(defn get-last-edit-input-id
+  []
+  (:editor/last-edit-block-id @state))
+
 (defn editing?
   []
   (some? (get-edit-input-id)))
@@ -667,6 +672,7 @@
                    (assoc
                     :editor/block block
                     :editor/editing? {edit-input-id true}
+                    :editor/last-edit-block-id edit-input-id
                     :cursor-range cursor-range)))))))
 
 (defn clear-edit!