瀏覽代碼

feat: cycle multiple todos

Tienson Qin 4 年之前
父節點
當前提交
baf6713316
共有 3 個文件被更改,包括 53 次插入11 次删除
  1. 5 1
      src/main/frontend/components/content.cljs
  2. 28 10
      src/main/frontend/handler/editor.cljs
  3. 20 0
      src/main/frontend/util/marker.cljs

+ 5 - 1
src/main/frontend/components/content.cljs

@@ -69,7 +69,11 @@
     (ui/menu-link
      {:key "copy block refs"
       :on-click editor-handler/copy-block-refs}
-     "Copy block refs")]])
+     "Copy block refs")
+    (ui/menu-link
+     {:key "cycle todos"
+      :on-click editor-handler/cycle-todos!}
+     "Cycle todos")]])
 
 ;; FIXME: Make it configurable
 (def block-background-colors

+ 28 - 10
src/main/frontend/handler/editor.cljs

@@ -837,9 +837,36 @@
 
 (defn set-marker
   [{:block/keys [uuid marker content format properties] :as block} new-marker]
-  (let [new-content (string/replace-first content (re-pattern (str "^" marker)) new-marker)]
+  (let [new-content (->
+                     (if marker
+                       (string/replace-first content (re-pattern (str "^" marker)) new-marker)
+                       (str new-marker " " content))
+                     (string/triml))]
     (save-block-if-changed! block new-content)))
 
+(defn- get-selected-blocks-with-children
+  []
+  (when-let [blocks (seq (state/get-selection-blocks))]
+    (->> (mapcat (fn [block]
+                   (cons block
+                         (array-seq (dom/by-class block "ls-block"))))
+                 blocks)
+         distinct)))
+
+(defn cycle-todos!
+  []
+  (when-let [blocks (seq (get-selected-blocks-with-children))]
+    (let [repo (state/get-current-repo)
+          workflow (state/get-preferred-workflow)
+          ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")]
+                                     (uuid id)) blocks))
+                   (remove nil?))]
+      (doseq [id ids]
+        (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))))))
+
 (defn set-priority
   [{:block/keys [uuid marker priority content] :as block} new-priority]
   (let [new-content (string/replace-first content
@@ -1052,15 +1079,6 @@
                        first)]
     (state/exit-editing-and-set-selected-blocks! [block])))
 
-(defn- get-selected-blocks-with-children
-  []
-  (when-let [blocks (seq (state/get-selection-blocks))]
-    (->> (mapcat (fn [block]
-                   (cons block
-                         (array-seq (dom/by-class block "ls-block"))))
-                 blocks)
-         distinct)))
-
 (defn- blocks-with-level
   [blocks]
   (let [level-blocks (mapv #(assoc % :level 1) blocks)

+ 20 - 0
src/main/frontend/util/marker.cljs

@@ -44,6 +44,26 @@
                         (fn [match]
                           (string/replace match old-marker new-marker))))
 
+(defn cycle-marker-state
+  [preferred-workflow marker]
+  (case marker
+    "TODO"
+    "DOING"
+
+    "DOING"
+    "DONE"
+
+    "LATER"
+    "NOW"
+
+    "NOW"
+    "DONE"
+
+    "DONE"
+    nil
+
+    (if (= :now preferred-workflow) "LATER" "TODO")))
+
 (defn cycle-marker
   [content format preferred-workflow]
   (let [markdown? (= :markdown format)