diff --git a/libs/src/LSPlugin.ts b/libs/src/LSPlugin.ts index f7448e1dce..f2a969b4b8 100644 --- a/libs/src/LSPlugin.ts +++ b/libs/src/LSPlugin.ts @@ -634,10 +634,15 @@ export interface IEditorProxy extends Record { }> ) => Promise + /** + * @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, - opts?: Partial<{ before: boolean; sibling: boolean }> + opts?: Partial<{ before: boolean; sibling: boolean, keepUUID: boolean }> ) => Promise | null> updateBlock: ( diff --git a/src/main/frontend/extensions/srs.cljs b/src/main/frontend/extensions/srs.cljs index 5cd2a0cda3..2cc781b984 100644 --- a/src/main/frontend/extensions/srs.cljs +++ b/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 diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index f375e67411..fcdcdf5409 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/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 diff --git a/src/main/logseq/api.cljs b/src/main/logseq/api.cljs index e45fb51226..cba8eca4e0 100644 --- a/src/main/logseq/api.cljs +++ b/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