improve(plguin): apis about insertion related with cursor state & more types

pull/2085/head
charlie 2021-06-01 22:33:41 +08:00
parent 0f39c9631e
commit c9f68296b4
5 changed files with 45 additions and 13 deletions

View File

@ -101,7 +101,11 @@ interface BlockEntity {
type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
type BlockPageName = string
type SlashCommandActionCmd = 'editor/input' | 'editor/hook' | 'editor/clear-current-slash'
type SlashCommandActionCmd =
'editor/input'
| 'editor/hook'
| 'editor/clear-current-slash'
| 'editor/restore-saved-cursor'
type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
type BlockCommandCallback = (e: IHookEvent & { uuid: BlockUUID }) => Promise<void>
@ -119,7 +123,6 @@ interface IAppProxy {
// events
onThemeModeChanged: IUserHook<{ mode: 'dark' | 'light' }>
onPageFileMounted: IUserSlotHook
onBlockRendererMounted: IUserSlotHook<{ uuid: BlockUUID }>
onRouteChanged: IUserHook<{ path: string, template: string }>
onSidebarVisibleChanged: IUserHook<{ visible: boolean }>
@ -130,17 +133,20 @@ interface IEditorProxy {
registerBlockContextMenu: (tag: string, action: BlockCommandCallback) => boolean
// block related APIs
getEditBlockContent: () => Promise<string>
getCurrentPage: () => Promise<Partial<BlockEntity>>
getCurrentBlock: () => Promise<BlockEntity>
checkEditing: () => Promise<BlockUUID | boolean>
insertAtEditingCursor: (content: string) => Promise<void>
getCurrentPage: () => Promise<Partial<BlockEntity> | null>
getCurrentBlock: () => Promise<BlockEntity | null>
getCurrentBlockContent: () => Promise<string>
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>
insertBlock: (srcBlock: BlockIdentity, content: string, opts?: Partial<{ 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>
getBlock: (srcBlock: BlockIdentity, opts?: Partial<{ includeChildren: boolean }>) => Promise<BlockEntity>
moveBlock: (srcBlock: BlockIdentity, targetBlock: BlockIdentity, opts?: Partial<{ before: boolean, children: boolean }>) => Promise<void>
editBlock: (srcBlock: BlockIdentity, opts?: { pos: number }) => Promise<void>
upsertBlockProperty: (block: BlockIdentity, key: string, value: any) => Promise<void>
removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>

View File

@ -39,7 +39,11 @@ const editor: Partial<IEditorProxy> = {
debug('Register slash command #', this.baseInfo.id, tag, actions)
if (typeof actions === 'function') {
actions = [['editor/clear-current-slash'], ['editor/hook', actions]]
actions = [
['editor/clear-current-slash', false],
['editor/restore-saved-cursor'],
['editor/hook', actions]
]
}
actions = actions.map((it) => {

View File

@ -427,13 +427,18 @@
(when-let [current-input (gdom/getElement input-id)]
(util/move-cursor-to-end current-input))))
(defmethod handle-step :editor/clear-current-slash [[_]]
(defmethod handle-step :editor/restore-saved-cursor [[_]]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
(util/move-cursor-to current-input (:editor/last-saved-cursor @state/state)))))
(defmethod handle-step :editor/clear-current-slash [[_ space?]]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
(let [edit-content (gobj/get current-input "value")
current-pos (:pos (util/get-caret-pos current-input))
prefix (subs edit-content 0 current-pos)
prefix (util/replace-last slash prefix "")
prefix (util/replace-last slash prefix "" (boolean space?))
new-value (str prefix
(subs edit-content current-pos))]
(state/set-block-content-and-last-pos! input-id

View File

@ -172,8 +172,8 @@
&.visible {
z-index: 1;
width: 100vw;
height: 100vh;
width: 100%;
height: 100%;
visibility: visible;
}
}

View File

@ -16,6 +16,7 @@
[frontend.state :as state]
[frontend.components.plugins :as plugins]
[frontend.handler.plugin :as plugin-handler]
[frontend.commands :as commands]
[frontend.handler.notification :as notification]
[datascript.core :as d]
[medley.core :as medley]
@ -151,6 +152,16 @@
(keyword k) (bean/->clj params))))
;; editor
(def ^:export check_editing
(fn []
(if (state/get-edit-input-id)
(str (:block/uuid (state/get-edit-block))) false)))
(def ^:export insert_at_editing_cursor
(fn [content]
(when-let [input-id (state/get-edit-input-id)]
(commands/simple-insert! input-id content {}))))
(def ^:export get_current_block
(fn []
(let [block (state/get-edit-block)
@ -158,7 +169,7 @@
block (and block (db-utils/pull (:db/id block)))]
(bean/->js (normalize-keyword-for-json block)))))
(def ^:export get_edit_block_content
(def ^:export get_current_block_content
(fn []
(state/get-edit-content)))
@ -168,6 +179,12 @@
(when-let [page (db-model/get-page page)]
(bean/->js (normalize-keyword-for-json (db-utils/pull (:db/id page))))))))
(def ^:export edit_block
(fn [block-uuid {:keys [pos] :or {pos :max} :as opts}]
(when-let [block-uuid (and block-uuid (medley/uuid block-uuid))]
(when-let [block (db-model/query-block-by-uuid block-uuid)]
(editor-handler/edit-block! block pos nil block-uuid)))))
(def ^:export insert_block
(fn [block-uuid-or-page-name content ^js opts]
(let [{:keys [before sibling isPageBlock props]} (bean/->clj opts)