Browse Source

enhance: cycle todos support cmd+enter too

Also, it keeps the selection state after triggered
Tienson Qin 4 years ago
parent
commit
1c78fdc245
2 changed files with 38 additions and 35 deletions
  1. 33 30
      src/main/frontend/handler/editor.cljs
  2. 5 5
      src/main/frontend/modules/shortcut/config.cljs

+ 33 - 30
src/main/frontend/handler/editor.cljs

@@ -837,21 +837,6 @@
         new-content (string/replace-first content "DONE" marker)]
     (save-block-if-changed! block new-content)))
 
-(defn cycle-todo!
-  []
-  (when (state/get-edit-block)
-    (let [edit-input-id (state/get-edit-input-id)
-          current-input (gdom/getElement edit-input-id)
-          content (state/get-edit-content)
-          format (or (db/get-page-format (state/get-current-page))
-                     (state/get-preferred-format))
-          [new-content marker] (marker/cycle-marker content format (state/get-preferred-workflow))
-          new-content (string/triml new-content)
-          new-pos (commands/compute-pos-delta-when-change-marker
-                   content marker (cursor/pos current-input))]
-      (state/set-edit-content! edit-input-id new-content)
-      (cursor/move-cursor-to current-input new-pos))))
-
 (defn set-marker
   [{:block/keys [marker content] :as block} new-marker]
   (let [new-content (->
@@ -861,6 +846,20 @@
                      (string/triml))]
     (save-block-if-changed! block new-content)))
 
+(defn- rehighlight-selected-nodes
+  ([]
+   (rehighlight-selected-nodes (state/get-selection-blocks)))
+  ([blocks]
+   (let [blocks (doall
+                 (map
+                   (fn [block]
+                     (when-let [id (gobj/get block "id")]
+                       (when-let [block (gdom/getElement id)]
+                         (dom/add-class! block "selected noselect")
+                         block)))
+                   blocks))]
+     (state/set-selection-blocks! blocks))))
+
 (defn- get-selected-blocks-with-children
   []
   (when-let [blocks (seq (state/get-selection-blocks))]
@@ -881,7 +880,25 @@
         (let [block (db/pull [:block/uuid id])
               new-marker (marker/cycle-marker-state workflow (:block/marker block))
               new-marker (if new-marker new-marker "")]
-          (set-marker block new-marker))))))
+          (set-marker block new-marker)))
+      (js/setTimeout #(rehighlight-selected-nodes blocks) 0))))
+
+(defn cycle-todo!
+  []
+  (if-let [blocks (seq (get-selected-blocks-with-children))]
+    (cycle-todos!)
+    (when (state/get-edit-block)
+      (let [edit-input-id (state/get-edit-input-id)
+            current-input (gdom/getElement edit-input-id)
+            content (state/get-edit-content)
+            format (or (db/get-page-format (state/get-current-page))
+                       (state/get-preferred-format))
+            [new-content marker] (marker/cycle-marker content format (state/get-preferred-workflow))
+            new-content (string/triml new-content)
+            new-pos (commands/compute-pos-delta-when-change-marker
+                     content marker (cursor/pos current-input))]
+        (state/set-edit-content! edit-input-id new-content)
+        (cursor/move-cursor-to current-input new-pos)))))
 
 (defn set-priority
   [{:block/keys [priority content] :as block} new-priority]
@@ -1838,20 +1855,6 @@
         blocks (db/pull-many repo '[*] lookup-refs)]
     (reorder-blocks blocks)))
 
-(defn- rehighlight-selected-nodes
-  ([]
-   (rehighlight-selected-nodes (state/get-selection-blocks)))
-  ([blocks]
-   (let [blocks (doall
-                 (map
-                   (fn [block]
-                     (when-let [id (gobj/get block "id")]
-                       (when-let [block (gdom/getElement id)]
-                         (dom/add-class! block "selected noselect")
-                         block)))
-                   blocks))]
-     (state/set-selection-blocks! blocks))))
-
 (defn move-up-down
   [up?]
   (fn [event]

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

@@ -117,10 +117,6 @@
      {:desc    "New line in current block"
       :binding "shift+enter"
       :fn      editor-handler/keydown-new-line-handler}
-     :editor/cycle-todo
-     {:desc    "Rotate the TODO state of the current item"
-      :binding "mod+enter"
-      :fn      editor-handler/cycle-todo!}
      :editor/follow-link
      {:desc    "Follow link under cursor"
       :binding "mod+o"
@@ -196,7 +192,11 @@
 
     :shortcut.handler/editor-global
     ^{:before m/enable-when-not-component-editing!}
-    {:editor/up
+    {:editor/cycle-todo
+     {:desc    "Rotate the TODO state of the current item"
+      :binding "mod+enter"
+      :fn      editor-handler/cycle-todo!}
+     :editor/up
      {:desc    "Move cursor up / Select up"
       :binding "up"
       :fn      (editor-handler/shortcut-up-down :up)}