Skip to content

Commit

Permalink
improve(plugin): more editor api
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 committed May 11, 2021
1 parent 71dbc9d commit d5a1a63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libs/src/LSPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ type IHookEvent = {

type IUserHook = (callback: (e: IHookEvent) => void) => void
type IUserSlotHook = (callback: (e: IHookEvent & UISlotIdentity) => void) => void
type IEntityID = { id: number }

type BlockID = number
type BlockUUID = string

type IEntityID = { id: BlockID }

interface BlockEntity {
id: number // db id
Expand All @@ -85,7 +89,7 @@ interface BlockEntity {
[key: string]: any
}

type BlockIdentity = 'string' | Pick<BlockEntity, 'uuid'>
type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
type SlashCommandActionTag = 'editor/input' | 'editor/hook' | 'editor/clear-current-slash'
type SlashCommandAction = [SlashCommandActionTag, ...args: any]

Expand All @@ -111,7 +115,7 @@ interface IEditorProxy {
insertBlock: (srcBlock: BlockIdentity, content: string, opts: Partial<{ before: boolean, sibling: boolean, props: {} }>) => Promise<BlockIdentity>
updateBlock: (srcBlock: BlockIdentity, content: string, opts: Partial<{ props: {} }>) => Promise<void>
removeBlock: (srcBlock: BlockIdentity, opts: Partial<{ includeChildren: boolean }>) => Promise<void>
getBlock: (srcBlock: BlockIdentity) => Promise<BlockEntity>
getBlock: (srcBlock: BlockIdentity | BlockID) => Promise<BlockEntity>
moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts: Partial<{ before: boolean, sibling: boolean }>) => Promise<void>

upsertBlockProperty: (block: BlockIdentity, key: string, value: any) => Promise<void>
Expand Down
9 changes: 9 additions & 0 deletions src/main/api.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns ^:no-doc api
(:require [frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.db.utils :as db-utils]
[frontend.handler.block :as block-handler]
[frontend.modules.outliner.tree :as outliner-tree]
[frontend.util :as util]
Expand Down Expand Up @@ -141,6 +142,14 @@
(when-let [block (state/get-edit-block)]
(bean/->js (normalize-keyword-for-json block)))))

(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))))))

(def ^:export get_current_page_blocks_tree
(fn []
(when-let [page (state/get-current-page)]
Expand Down

0 comments on commit d5a1a63

Please sign in to comment.