improve(plugin): add block entity type

pull/1901/head
charlie 2021-05-10 17:51:05 +08:00
parent 4ad0bd2eae
commit 4043c163fb
2 changed files with 38 additions and 12 deletions

View File

@ -62,10 +62,25 @@ type IHookEvent = {
type IUserHook = (callback: (e: IHookEvent) => void) => void
type IUserSlotHook = (callback: (e: IHookEvent & UISlotIdentity) => void) => void
type IEntityID = { id: number }
interface BlockEntity {
id: number // db id
uuid: string
anchor: string
body: any
children: any
container: string
content: string
format: 'markdown' | 'org'
file: IEntityID
left: IEntityID
level: number
meta: { timestamps: any, properties: any, startPos: number, endPos: number }
page: IEntityID
parent: IEntityID
title: Array<any>
unordered: boolean
[key: string]: any
}
@ -90,13 +105,13 @@ interface IEditorProxy {
registerBlockContextMenu: (this: LSPluginUser, tag: string, action: () => void) => boolean
// TODO: Block related APIs
getCurrentBlock: () => Promise<BlockIdentity>
getCurrentBlock: () => Promise<BlockEntity>
getCurrentPageBlocksTree: <T = any> () => Promise<T>
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<BlockIdentity>
getBlock: (srcBlock: BlockIdentity) => Promise<BlockEntity>
moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts: Partial<{ before: boolean, sibling: boolean }>) => Promise<void>
upsertBlockProperty: (block: BlockIdentity, key: string, value: any) => Promise<void>

View File

@ -94,27 +94,42 @@
(fn [pid ^js cmd-actions]
(when-let [[cmd actions] (bean/->clj cmd-actions)]
(plugin-handler/register-plugin-slash-command
pid [cmd (mapv #(into [(keyword (first %))]
(rest %)) actions)]))))
pid [cmd (mapv #(into [(keyword (first %))]
(rest %)) actions)]))))
(def ^:export register_plugin_simple_command
(fn [pid ^js cmd-action]
(when-let [[cmd action] (bean/->clj cmd-action)]
(plugin-handler/register-plugin-simple-command
pid cmd (assoc action 0 (keyword (first action)))))))
pid cmd (assoc action 0 (keyword (first action)))))))
;; app
(def ^:export push_state
(fn [^js k ^js params]
(rfe/push-state
(keyword k) (bean/->clj params))))
(keyword k) (bean/->clj params))))
(def ^:export replace_state
(fn [^js k ^js params]
(rfe/replace-state
(keyword k) (bean/->clj params))))
(keyword k) (bean/->clj params))))
(defn- normalize-keyword-for-json
[input]
(when input
(walk/postwalk
(fn [a]
(cond
(keyword? a) (csk/->camelCase (name a))
(uuid? a) (str a)
:else a)) input)))
;; editor
(def ^:export get_current_block
(fn []
(when-let [block (state/get-edit-block)]
(bean/->js (normalize-keyword-for-json block)))))
(def ^:export get_current_page_blocks_tree
(fn []
(when-let [page (state/get-current-page)]
@ -125,11 +140,7 @@
blocks)
blocks (outliner-tree/blocks->vec-tree blocks (:db/id (db/get-page (state/get-current-page))))
;; clean key
blocks (walk/postwalk
(fn [a]
(if (keyword? a)
(csk/->camelCase (name a))
a)) blocks)]
blocks (normalize-keyword-for-json blocks)]
(bean/->js blocks)))))
;; db