From b755369a0b7e5a953a81f617ebce8010f3f389f9 Mon Sep 17 00:00:00 2001 From: charlie Date: Thu, 15 Aug 2024 17:26:51 +0800 Subject: [PATCH] enhance(apis): get blocks from batch blocks insertion --- libs/src/LSPlugin.ts | 5 ++ src/main/frontend/handler/editor.cljs | 66 ++++++++++++++------------- src/main/logseq/api.cljs | 12 +++-- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/libs/src/LSPlugin.ts b/libs/src/LSPlugin.ts index f90999bc28..8e5d34783f 100644 --- a/libs/src/LSPlugin.ts +++ b/libs/src/LSPlugin.ts @@ -127,7 +127,12 @@ export type BlockUUIDTuple = ['uuid', BlockUUID] export type IEntityID = { id: EntityID; [key: string]: any } export type IBatchBlock = { content: string + + /** + * @NOTE: not supported for DB graph + */ properties?: Record + children?: Array } export type IDatom = [e: number, a: string, v: any, t: number, added: boolean] diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 7573224142..aede2f33c4 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -2065,28 +2065,29 @@ sibling? keep-uuid? revert-cut-txs - skip-empty-target?] + skip-empty-target? + ops-only?] :or {exclude-properties []}}] (let [editing-block (when-let [editing-block (state/get-edit-block)] (some-> (db/entity [:block/uuid (:block/uuid editing-block)]) - (assoc :block/title (state/get-edit-content)))) + (assoc :block/title (state/get-edit-content)))) has-unsaved-edits (and editing-block - (not= (:block/title (db/entity (:db/id editing-block))) - (state/get-edit-content))) + (not= (:block/title (db/entity (:db/id editing-block))) + (state/get-edit-content))) target-block (or target-block editing-block) block (db/entity (:db/id target-block)) page (if (:block/name block) block - (when target-block (:block/page (db/entity (:db/id target-block))))) + (when target-block (:block/page (db/entity (:db/id target-block))))) empty-target? (if (true? skip-empty-target?) false - (string/blank? (:block/title target-block))) + (string/blank? (:block/title target-block))) paste-nested-blocks? (nested-blocks blocks) target-block-has-children? (db/has-children? (:block/uuid target-block)) replace-empty-target? (and empty-target? - (or (not target-block-has-children?) - (and target-block-has-children? (= (count blocks) 1)))) + (or (not target-block-has-children?) + (and target-block-has-children? (= (count blocks) 1)))) target-block' (if (and empty-target? target-block-has-children? paste-nested-blocks?) (or (ldb/get-left-sibling target-block) - (:block/parent (db/entity (:db/id target-block)))) + (:block/parent (db/entity (:db/id target-block)))) target-block) sibling? (cond (and paste-nested-blocks? empty-target?) @@ -2099,27 +2100,30 @@ false :else - true)] - - (p/let [_ (when has-unsaved-edits - (ui-outliner-tx/transact! - {:outliner-op :save-block} - (outliner-save-block! editing-block))) - result (ui-outliner-tx/transact! - {:outliner-op :insert-blocks - :additional-tx revert-cut-txs} - (when target-block' - (let [format (or (:block/format target-block') (state/get-preferred-format)) - repo (state/get-current-repo) - blocks' (map (fn [block] - (paste-block-cleanup repo block page exclude-properties format content-update-fn keep-uuid?)) - blocks)] - (outliner-op/insert-blocks! blocks' target-block' {:sibling? sibling? - :outliner-op :paste - :replace-empty-target? replace-empty-target? - :keep-uuid? keep-uuid?}))))] - (state/set-block-op-type! nil) - (when result (edit-last-block-after-inserted! (ldb/read-transit-str result)))))) + true) + transact-blocks! #(ui-outliner-tx/transact! + {:outliner-op :insert-blocks + :additional-tx revert-cut-txs} + (when target-block' + (let [format (or (:block/format target-block') (state/get-preferred-format)) + repo (state/get-current-repo) + blocks' (map (fn [block] + (paste-block-cleanup repo block page exclude-properties format content-update-fn keep-uuid?)) + blocks)] + (outliner-op/insert-blocks! blocks' target-block' {:sibling? sibling? + :outliner-op :paste + :replace-empty-target? replace-empty-target? + :keep-uuid? keep-uuid?}))))] + (if ops-only? + (transact-blocks!) + (p/let [_ (when has-unsaved-edits + (ui-outliner-tx/transact! + {:outliner-op :save-block} + (outliner-save-block! editing-block))) + result (transact-blocks!)] + (state/set-block-op-type! nil) + (when-let [result (some-> result (ldb/read-transit-str))] + (edit-last-block-after-inserted! result) result))))) (defn- block-tree->blocks "keep-uuid? - maintain the existing :uuid in tree vec" @@ -2156,7 +2160,7 @@ {:outliner-op :paste-blocks} (when (seq block-refs) (db/transact! (map (fn [[_ id]] {:block/uuid id}) block-refs))) - (paste-blocks blocks opts)))) + (paste-blocks blocks (merge opts {:ops-only? true}))))) (defn insert-block-tree-after-target "`tree-vec`: a vector of blocks. diff --git a/src/main/logseq/api.cljs b/src/main/logseq/api.cljs index 8e02ec957f..bd8f6d2a1d 100644 --- a/src/main/logseq/api.cljs +++ b/src/main/logseq/api.cljs @@ -736,11 +736,13 @@ (when (and uuid (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error uuid))) (throw (js/Error. (util/format "Custom block UUID already exists (%s)." uuid))))))) - block (if (and before sibling) - (db/pull (:db/id (ldb/get-left-sibling (db/entity (:db/id block))))) block) - _ (editor-handler/insert-block-tree-after-target - (:db/id block) sibling bb (:block/format block) keep-uuid?)] - nil)))))) + block (if before + (db/pull (:db/id (ldb/get-left-sibling (db/entity (:db/id block))))) block)] + (some-> (editor-handler/insert-block-tree-after-target + (:db/id block) sibling bb (:block/format block) keep-uuid?) + (p/then (fn [results] + (some-> results (ldb/read-transit-str) + :blocks (sdk-utils/normalize-keyword-for-json) (bean/->js))))))))))) (def ^:export remove_block (fn [block-uuid ^js _opts]