Browse Source

enhance(apis): get blocks from batch blocks insertion

charlie 1 year ago
parent
commit
b755369a0b
3 changed files with 47 additions and 36 deletions
  1. 5 0
      libs/src/LSPlugin.ts
  2. 35 31
      src/main/frontend/handler/editor.cljs
  3. 7 5
      src/main/logseq/api.cljs

+ 5 - 0
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<string, any>
+
   children?: Array<IBatchBlock>
 }
 export type IDatom = [e: number, a: string, v: any, t: number, added: boolean]

+ 35 - 31
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.

+ 7 - 5
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]