Преглед изворни кода

improve(plugin): initial impls of block related apis

charlie пре 4 година
родитељ
комит
4fe0b806c6
3 измењених фајлова са 58 додато и 8 уклоњено
  1. 7 4
      libs/src/LSPlugin.d.ts
  2. 2 1
      src/main/frontend/handler/editor.cljs
  3. 49 3
      src/main/logseq/api.cljs

+ 7 - 4
libs/src/LSPlugin.d.ts

@@ -99,6 +99,7 @@ interface BlockEntity {
 }
 
 type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
+type BlockPageName = string
 type SlashCommandActionCmd = 'editor/input' | 'editor/hook' | 'editor/clear-current-slash'
 type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
 
@@ -126,19 +127,21 @@ interface IEditorProxy {
   registerSlashCommand: (this: LSPluginUser, tag: string, actions: Array<SlashCommandAction>) => boolean
   registerBlockContextMenu: (this: LSPluginUser, tag: string, action: () => void) => boolean
 
-  // TODO: Block related APIs
+  // block related APIs
   getCurrentPage: () => Promise<Partial<BlockEntity>>
   getCurrentBlock: () => Promise<BlockEntity>
   getCurrentPageBlocksTree: <T = any> () => Promise<T>
 
-  insertBlock: (srcBlock: BlockIdentity, content: string, opts: Partial<{ before: boolean, sibling: boolean, props: {} }>) => Promise<BlockIdentity>
+  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, sibling: boolean }>) => Promise<void>
+  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) => Promise<void>
+  removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
+  getBlockProperty: (block: BlockIdentity, key: string) => Promise<any>
+  getBlockProperties: (block: BlockIdentity) => Promise<any>
 }
 
 interface IDBProxy {

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -637,7 +637,8 @@
             repo (state/get-current-repo)]
         (outliner-insert-block! {} block new-block sibling?)
         (db/refresh! repo {:key :block/insert
-                           :data [new-block]})))))
+                           :data [new-block]})
+        new-block))))
 
 (defn update-timestamps-content!
   [{:block/keys [repeated? marker] :as block} content]

+ 49 - 3
src/main/logseq/api.cljs

@@ -4,6 +4,7 @@
             [frontend.db.utils :as db-utils]
             [frontend.handler.block :as block-handler]
             [frontend.handler.editor :as editor-handler]
+            [frontend.handler.dnd :as editor-dnd-handler]
             [frontend.modules.outliner.tree :as outliner-tree]
             [frontend.util :as util]
             [electron.ipc :as ipc]
@@ -163,10 +164,37 @@
         (bean/->js (normalize-keyword-for-json (db-utils/pull (:db/id page))))))))
 
 (def ^:export insert_block
+  (fn [block-uuid-or-page-name content ^js opts]
+    (let [{:keys [before sibling isPageBlock props]} (bean/->clj opts)
+          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})]
+
+      (bean/->js (normalize-keyword-for-json new-block)))))
+
+(def ^:export remove_block
+  (fn [block-uuid ^js opts]
+    (let [{:keys [includeChildren]} (bean/->clj opts)
+          repo (state/get-current-repo)]
+      (editor-handler/delete-block-aux!
+       {:block/uuid (medley/uuid block-uuid) :repo repo} false includeChildren))))
+
+(def ^:export update_block
   (fn [block-uuid content ^js opts]
-    (when-let [block-uuid (and block-uuid (medley/uuid block-uuid))]
-      (let [{:keys [before sibling props]} (bean/->clj opts)]
-        (editor-handler/api-insert-new-block! content {:block-uuid block-uuid :sibling? sibling})))))
+    (let [opts (and opts (bean/->clj opts))
+          repo (state/get-current-repo)]
+      (editor-handler/save-block! repo (medley/uuid block-uuid) content))))
+
+(def ^:export move_block
+  (fn [src-block-uuid target-block-uuid ^js opts]
+
+    (let [{:keys [before children]} (bean/->clj opts)
+          top? (boolean before)
+          nested? (boolean children)
+          src-block-uuid (db-model/query-block-by-uuid (medley/uuid src-block-uuid))
+          target-block-uuid (db-model/query-block-by-uuid (medley/uuid target-block-uuid))]
+      (editor-dnd-handler/move-block src-block-uuid target-block-uuid top? nested?))))
 
 (def ^:export get_block
   (fn [id-or-uuid]
@@ -176,6 +204,24 @@
       (when (contains? ret :block/uuid)
         (bean/->js (normalize-keyword-for-json ret))))))
 
+(def ^:export upsert_block_property
+  (fn [block-uuid key value]
+    (editor-handler/set-block-property! (medley/uuid block-uuid) key value)))
+
+(def ^:export remove_block_property
+  (fn [block-uuid key]
+    (editor-handler/remove-block-property! (medley/uuid block-uuid) key)))
+
+(def ^:export get_block_property
+  (fn [block-uuid key]
+    (when-let [block (db-model/query-block-by-uuid block-uuid)]
+      (get (:block/properties block) (keyword key)))))
+
+(def ^:export get_block_properties
+  (fn [block-uuid]
+    (when-let [block (db-model/query-block-by-uuid block-uuid)]
+      (bean/->js (normalize-keyword-for-json (:block/properties block))))))
+
 (def ^:export get_current_page_blocks_tree
   (fn []
     (when-let [page (state/get-current-page)]