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> ) => 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: ( insertBatchBlock: (
srcBlock: BlockIdentity, srcBlock: BlockIdentity,
batch: IBatchBlock | Array<IBatchBlock>, batch: IBatchBlock | Array<IBatchBlock>,
opts?: Partial<{ before: boolean; sibling: boolean }> opts?: Partial<{ before: boolean; sibling: boolean, keepUUID: boolean }>
) => Promise<Array<BlockEntity> | null> ) => Promise<Array<BlockEntity> | null>
updateBlock: ( updateBlock: (

View File

@@ -370,7 +370,8 @@
(util/format "Remembered: %d (%d%%)" score-remembered-count (* 100 (/ score-remembered-count review-count)))} (util/format "Remembered: %d (%d%%)" score-remembered-count (* 100 (/ score-remembered-count review-count)))}
{:content {:content
(util/format "Forgotten : %d (%d%%)" score-forgotten-count (* 100 (/ score-forgotten-count review-count)))}]}] (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 ;;; UI

View File

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