feat(api): add keepUUID option to insertBatchBlock

This commit is contained in:
Manu [tennox]
2022-12-01 14:58:27 +00:00
committed by Tienson Qin
parent 7a959053b9
commit 8a6e0bdcc2
4 changed files with 29 additions and 13 deletions

View File

@@ -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: (

View File

@@ -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

View File

@@ -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

View File

@@ -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