浏览代码

enhance(ux): batch toggling ordered list blocks within the blocks context menu

charlie 2 年之前
父节点
当前提交
8c4124152f

+ 12 - 0
src/main/frontend/components/content.cljs

@@ -84,6 +84,12 @@
       (t :context-menu/make-a-flashcard)
       nil))
 
+   (ui/menu-link
+     {:key "Toggle number list"
+      :on-click #(state/pub-event! [:editor/toggle-own-number-list (state/get-selection-block-ids)])}
+     (t :context-menu/toggle-number-list)
+     nil)
+
    (ui/menu-link
     {:key "cycle todos"
      :on-click editor-handler/cycle-todos!}
@@ -255,6 +261,12 @@
            :else
            nil)
 
+         (ui/menu-link
+           {:key "Toggle number list"
+            :on-click #(state/pub-event! [:editor/toggle-own-number-list (state/get-selection-block-ids)])}
+           (t :context-menu/toggle-number-list)
+           nil)
+
          [:hr.menu-separator]
 
          (ui/menu-link

+ 1 - 0
src/main/frontend/dicts.cljc

@@ -171,6 +171,7 @@
         :content/open-in-sidebar "Open in sidebar"
         :content/click-to-edit "Click to edit"
         :context-menu/make-a-flashcard "Make a Flashcard"
+        :context-menu/toggle-number-list "Toggle number list"
         :context-menu/preview-flashcard "Preview Flashcard"
         :context-menu/make-a-template "Make a Template"
         :context-menu/input-template-name "What's the template's name?"

+ 12 - 0
src/main/frontend/handler/editor.cljs

@@ -64,6 +64,8 @@
 
 (declare set-block-property!)
 (declare remove-block-property!)
+(declare batch-add-block-property!)
+(declare batch-remove-block-property!)
 
 (defn get-block-own-order-list-type
   [block]
@@ -87,6 +89,16 @@
   [block]
   (some-> block (set-block-own-order-list-type! "number")))
 
+(defn toggle-blocks-as-own-order-list!
+  [blocks]
+  (when (seq blocks)
+    (let [has-ordered?    (some own-order-number-list? blocks)
+          blocks-uuids    (some->> blocks (map :block/uuid) (remove nil?))
+          order-list-prop :logseq.order-list-type]
+      (if has-ordered?
+        (batch-remove-block-property! blocks-uuids order-list-prop)
+        (batch-add-block-property! blocks-uuids order-list-prop "number")))))
+
 (defn get-selection-and-format
   []
   (when-let [block (state/get-edit-block)]

+ 11 - 5
src/main/frontend/handler/events.cljs

@@ -948,11 +948,17 @@
 (defmethod handle :editor/quick-capture [[_ args]]
   (quick-capture/quick-capture args))
 
-(defmethod handle :editor/toggle-own-number-list [[_ block]]
-  (when block
-    (if (editor-handler/own-order-number-list? block)
-      (editor-handler/remove-block-own-order-list-type! block)
-      (editor-handler/make-block-as-own-order-list! block))))
+(defmethod handle :editor/toggle-own-number-list [[_ blocks]]
+  (let [batch? (sequential? blocks)
+        blocks (cond->> blocks
+                  batch?
+                  (map #(cond-> % (or (uuid? %) (string? %)) (db-model/get-block-by-uuid))))]
+    (if (and batch? (> (count blocks) 1))
+      (editor-handler/toggle-blocks-as-own-order-list! blocks)
+      (when-let [block (cond-> blocks batch? (first))]
+        (if (editor-handler/own-order-number-list? block)
+          (editor-handler/remove-block-own-order-list-type! block)
+          (editor-handler/make-block-as-own-order-list! block))))))
 
 (defmethod handle :editor/remove-own-number-list [[_ block]]
   (when (some-> block (editor-handler/own-order-number-list?))