Browse Source

improve(plugin): fix blocks tree & types

charlie 4 years ago
parent
commit
76bfb6d7be
2 changed files with 28 additions and 20 deletions
  1. 10 8
      libs/src/LSPlugin.d.ts
  2. 18 12
      src/main/logseq/api.cljs

+ 10 - 8
libs/src/LSPlugin.d.ts

@@ -66,6 +66,7 @@ type IUserSlotHook<E = any> = (callback: (e: IHookEvent & UISlotIdentity & E) =>
 
 type BlockID = number
 type BlockUUID = string
+type BlockUUIDTuple = ['uuid', BlockUUID]
 
 type IEntityID = { id: BlockID }
 
@@ -82,7 +83,7 @@ interface BlockEntity {
   uuid: string
   anchor: string
   body: any
-  children: any
+  children: Array<BlockEntity | BlockUUIDTuple>
   container: string
   content: string
   format: 'markdown' | 'org'
@@ -130,13 +131,14 @@ interface IEditorProxy {
   // block related APIs
   getCurrentPage: () => Promise<Partial<BlockEntity>>
   getCurrentBlock: () => Promise<BlockEntity>
-  getCurrentPageBlocksTree: <T = any> () => Promise<T>
-
-  insertBlock: (srcBlock: BlockIdentity | BlockPageName, content: string, opts: Partial<{ isPageBlock: boolean, before: boolean, sibling: boolean, props: {} }>) => Promise<BlockEntity | null>
-  updateBlock: (srcBlock: BlockIdentity, content: string, opts: Partial<{ props: {} }>) => Promise<void>
-  removeBlock: (srcBlock: BlockIdentity, opts: Partial<{ includeChildren: boolean }>) => Promise<void>
-  getBlock: (srcBlock: BlockIdentity | BlockID) => Promise<BlockEntity>
-  moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts: Partial<{ before: boolean, children: boolean }>) => Promise<void>
+  getCurrentPageBlocksTree: () => Promise<Array<BlockEntity>>
+  getPageBlocksTree: (pageName: BlockPageName) => Promise<Array<BlockEntity>>
+
+  insertBlock: (srcBlock: BlockIdentity | BlockPageName, content: string, opts?: Partial<{ isPageBlock: boolean, before: boolean, sibling: boolean, props: {} }>) => Promise<BlockEntity | null>
+  updateBlock: (srcBlock: BlockIdentity, content: string, opts?: Partial<{ props: {} }>) => Promise<void>
+  removeBlock: (srcBlock: BlockIdentity, opts?: Partial<{ includeChildren: boolean }>) => Promise<void>
+  getBlock: (srcBlock: BlockIdentity | BlockID, opts?: Partial<{ includeChildren: boolean }>) => Promise<BlockEntity>
+  moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts?: Partial<{ before: boolean, children: boolean }>) => Promise<void>
 
   upsertBlockProperty: (block: BlockIdentity, key: string, value: any) => Promise<void>
   removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>

+ 18 - 12
src/main/logseq/api.cljs

@@ -169,7 +169,7 @@
           page-name (and isPageBlock block-uuid-or-page-name)
           block-uuid (if isPageBlock nil (medley/uuid block-uuid-or-page-name))
           new-block (editor-handler/api-insert-new-block!
-                      content {:block-uuid block-uuid :sibling? sibling :page page-name})]
+                     content {:block-uuid block-uuid :sibling? sibling :page page-name})]
 
       (bean/->js (normalize-keyword-for-json new-block)))))
 
@@ -197,12 +197,14 @@
       (editor-dnd-handler/move-block src-block-uuid target-block-uuid top? nested?))))
 
 (def ^:export get_block
-  (fn [id-or-uuid]
-    (when-let [ret (cond
-                     (number? id-or-uuid) (db-utils/pull id-or-uuid)
-                     (string? id-or-uuid) (db-model/query-block-by-uuid id-or-uuid))]
-      (when (contains? ret :block/uuid)
-        (bean/->js (normalize-keyword-for-json ret))))))
+  (fn [id-or-uuid ^js opts]
+    (when-let [block (cond
+                       (number? id-or-uuid) (db-utils/pull id-or-uuid)
+                       (string? id-or-uuid) (db-model/query-block-by-uuid id-or-uuid))]
+      (when-let [uuid (:block/uuid block)]
+        (let [{:keys [includeChildren]} (bean/->clj opts)
+              block (if (not includeChildren) block (first (outliner-tree/blocks->vec-tree [block] uuid)))]
+          (bean/->js (normalize-keyword-for-json block)))))))
 
 (def ^:export upsert_block_property
   (fn [block-uuid key value]
@@ -226,15 +228,19 @@
   (fn []
     (when-let [page (state/get-current-page)]
       (let [blocks (db-model/get-page-blocks-no-cache page)
-            blocks (mapv #(-> %
-                              (dissoc :block/children)
-                              (assoc :block/uuid (str (:block/uuid %))))
-                         blocks)
-            blocks (outliner-tree/blocks->vec-tree blocks (:db/id (db/get-page (state/get-current-page))))
+            blocks (outliner-tree/blocks->vec-tree blocks page)
             ;; clean key
             blocks (normalize-keyword-for-json blocks)]
         (bean/->js blocks)))))
 
+(def ^:export get_page_blocks_tree
+  (fn [page-name]
+    (when-let [_ (db-model/get-page page-name)]
+      (let [blocks (db-model/get-page-blocks-no-cache page-name)
+            blocks (outliner-tree/blocks->vec-tree blocks page-name)
+            blocks (normalize-keyword-for-json blocks)]
+        (bean/->js blocks)))))
+
 ;; db
 (defn ^:export q
   [query-string]