浏览代码

feat(api): add keepUUID option to insertBatchBlock

Manu [tennox] 2 年之前
父节点
当前提交
8a6e0bdcc2
共有 4 个文件被更改,包括 29 次插入13 次删除
  1. 6 1
      libs/src/LSPlugin.ts
  2. 2 1
      src/main/frontend/extensions/srs.cljs
  3. 12 9
      src/main/frontend/handler/editor.cljs
  4. 9 2
      src/main/logseq/api.cljs

+ 6 - 1
libs/src/LSPlugin.ts

@@ -634,10 +634,15 @@ export interface IEditorProxy extends Record<string, any> {
     }>
   ) => Promise<BlockEntity | null>
 
+  /**
+   * @example https://github.com/logseq/logseq-plugin-samples/tree/master/logseq-reddit-hot-news
+   * 
+   * `keepUUID` will allow you to set a custom UUID for blocks by setting their properties.id
+   */
   insertBatchBlock: (
     srcBlock: BlockIdentity,
     batch: IBatchBlock | Array<IBatchBlock>,
-    opts?: Partial<{ before: boolean; sibling: boolean }>
+    opts?: Partial<{ before: boolean; sibling: boolean, keepUUID: boolean }>
   ) => Promise<Array<BlockEntity> | null>
 
   updateBlock: (

+ 2 - 1
src/main/frontend/extensions/srs.cljs

@@ -370,7 +370,8 @@
                      (util/format "Remembered:   %d (%d%%)" score-remembered-count (* 100 (/ score-remembered-count review-count)))}
                     {:content
                      (util/format "Forgotten :   %d (%d%%)" score-forgotten-count (* 100 (/ score-forgotten-count review-count)))}]}]
-       (:block/format card-query-block)))))
+       (:block/format card-query-block)
+       false))))
 
 ;;; ================================================================
 ;;; UI

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

@@ -1875,23 +1875,25 @@
     (cursor/move-cursor-forward input 2)))
 
 (defn- paste-block-cleanup
-  [block page exclude-properties format content-update-fn]
+  [block page exclude-properties format content-update-fn keep-uuid?]
   (let [new-content
         (if content-update-fn
           (content-update-fn (:block/content block))
           (:block/content block))
         new-content
-        (->> new-content
-             (property/remove-property format "id")
-             (property/remove-property format "custom_id"))]
+        (cond->> new-content
+             (not keep-uuid?) (property/remove-property format "id")
+             true             (property/remove-property format "custom_id"))]
     (merge (dissoc block
                    :block/pre-block?
                    :block/meta)
            {:block/page {:db/id (:db/id page)}
             :block/format format
             :block/properties (apply dissoc (:block/properties block)
-                                (concat [:id :custom_id :custom-id]
-                                        exclude-properties))
+                                (concat 
+                                  (when (not keep-uuid?) [:id])
+                                  [:custom_id :custom-id]
+                                  exclude-properties))
             :block/content new-content})))
 
 (defn- edit-last-block-after-inserted!
@@ -1962,7 +1964,7 @@
       (when target-block'
         (let [format (or (:block/format target-block') (state/get-preferred-format))
               blocks' (map (fn [block]
-                             (paste-block-cleanup block page exclude-properties format content-update-fn))
+                             (paste-block-cleanup block page exclude-properties format content-update-fn keep-uuid?))
                            blocks)
               result (outliner-core/insert-blocks! blocks' target-block' {:sibling? sibling?
                                                                           :outliner-op :paste
@@ -2007,9 +2009,10 @@
 (defn insert-block-tree-after-target
   "`tree-vec`: a vector of blocks.
    A block element: {:content :properties :children [block-1, block-2, ...]}"
-  [target-block-id sibling? tree-vec format]
+  [target-block-id sibling? tree-vec format keep-uuid?]
   (insert-block-tree tree-vec format
                      {:target-block (db/pull target-block-id)
+                      :keep-uuid?   keep-uuid?
                       :sibling?     sibling?}))
 
 (defn insert-template!
@@ -2050,7 +2053,7 @@
              page (if (:block/name block) block
                       (when target (:block/page (db/entity (:db/id target)))))
              blocks' (map (fn [block]
-                            (paste-block-cleanup block page exclude-properties format content-update-fn))
+                            (paste-block-cleanup block page exclude-properties format content-update-fn false))
                           blocks)
              sibling? (:sibling? opts)
              sibling?' (cond

+ 9 - 2
src/main/logseq/api.cljs

@@ -621,9 +621,16 @@
     (when-let [block (db-model/query-block-by-uuid (uuid-or-throw-error block-uuid))]
       (when-let [bb (bean/->clj batch-blocks)]
         (let [bb (if-not (vector? bb) (vector bb) bb)
-              {:keys [sibling]} (bean/->clj opts)
+              {:keys [sibling keepUUID]} (bean/->clj opts)
+              keep-uuid? (or keepUUID false)
+              _ (when keep-uuid? (doseq 
+                  [block (outliner/tree-vec-flatten bb :children)]
+                  (let [uuid (:id (:properties block))] 
+                    (when (and uuid (db-model/query-block-by-uuid (uuid-or-throw-error uuid)))
+                      (throw (js/Error.
+                              (util/format "Custom block UUID already exists (%s)." uuid)))))))
               _ (editor-handler/insert-block-tree-after-target
-                  (:db/id block) sibling bb (:block/format block))]
+                 (:db/id block) sibling bb (:block/format block) keep-uuid?)]
           nil)))))
 
 (def ^:export remove_block