瀏覽代碼

feat: copy multiple block refs

Also, fixed a bug that block refs count is not updated reactively.
Tienson Qin 4 年之前
父節點
當前提交
d5578a2b22

+ 3 - 2
src/main/frontend/components/block.cljs

@@ -1889,7 +1889,7 @@
                          [:a.fade-link
                           summary]]))))
 
-        (let [block-refs-count (count (:block/_refs (db/entity (:db/id block))))]
+        (let [block-refs-count (count (:block/_refs block))]
           (when (and block-refs-count (> block-refs-count 0))
             [:div
              [:a.open-block-ref-link.bg-base-2.text-sm.ml-2
@@ -2084,7 +2084,8 @@
    :should-update (fn [old-state new-state]
                     (let [compare-keys [:block/uuid :block/properties
                                         :block/parent :block/left
-                                        :block/children :block/content]
+                                        :block/children :block/content
+                                        :block/_refs]
                           config-compare-keys [:show-cloze?]]
                       (or
                        (not= (select-keys (second (:rum/args old-state)) compare-keys)

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

@@ -65,7 +65,11 @@
                   (let [block-uuids (editor-handler/get-selected-toplevel-block-uuids)]
                     (state/set-modal!
                      #(export/export-blocks block-uuids))))}
-     "Copy as")]])
+     "Copy as")
+    (ui/menu-link
+     {:key "copy block refs"
+      :on-click editor-handler/copy-block-refs}
+     "Copy block refs")]])
 
 ;; FIXME: Make it configurable
 (def block-background-colors

+ 3 - 2
src/main/frontend/db/model.cljs

@@ -20,7 +20,7 @@
 ;; correponding handlers.
 
 ;; Use it as an input argument for datalog queries
-(defonce block-attrs
+(def block-attrs
   '[:db/id
     :block/uuid
     :block/type
@@ -28,6 +28,7 @@
     :block/format
     :block/title
     :block/refs
+    :block/_refs
     :block/path-refs
     :block/tags
     :block/content
@@ -473,7 +474,7 @@
    (get-page-blocks repo-url page nil))
   ([repo-url page {:keys [use-cache? pull-keys]
                    :or {use-cache? true
-                        pull-keys '[*]}}]
+                        pull-keys block-attrs}}]
    (let [page (string/lower-case (string/trim page))
          page-entity (or (db-utils/entity repo-url [:block/name page])
                          (db-utils/entity repo-url [:block/original-name page]))

+ 21 - 3
src/main/frontend/handler/editor.cljs

@@ -1033,12 +1033,16 @@
             (state/set-edit-content! input-id new-content)
             (save-block-if-changed! block new-content)))))))
 
+(defn- set-block-id!
+  [block-id]
+  (let [block (db/entity [:block/uuid block-id])]
+    (when-not (:block/pre-block? block)
+      (set-block-property! block-id "id" (str block-id)))))
+
 (defn copy-block-ref!
   ([block-id] (copy-block-ref! block-id #(str %)))
   ([block-id tap-clipboard]
-   (let [block (db/entity [:block/uuid block-id])]
-     (when-not (:block/pre-block? block)
-       (set-block-property! block-id "id" (str block-id))))
+   (set-block-id! block-id)
    (util/copy-to-clipboard! (tap-clipboard block-id))))
 
 (defn select-block!
@@ -1131,6 +1135,20 @@
       (state/set-copied-blocks content tree)
       (notification/show! "Copied!" :success))))
 
+(defn copy-block-refs
+  []
+  (when-let [blocks (seq (get-selected-blocks-with-children))]
+    (let [repo (state/get-current-repo)
+          ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")]
+                                     (uuid id)) blocks))
+                   (remove nil?))
+          ids-str (some->> ids
+                           (map (fn [id] (util/format "((%s))" id)))
+                           (string/join "\n\n"))]
+      (doseq [id ids]
+        (set-block-id! id))
+      (util/copy-to-clipboard! ids-str))))
+
 (defn get-selected-toplevel-block-uuids
   []
   (when-let [blocks (seq (get-selected-blocks-with-children))]