Skip to content

Commit

Permalink
improve(plugin): add editting cursor api & types
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 committed Jun 3, 2021
1 parent 755afdb commit ba1cc8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
12 changes: 9 additions & 3 deletions libs/src/LSPlugin.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,19 @@ function initUserSettingsHandlers (pluginLocal: PluginLocal) {
function initMainUIHandlers (pluginLocal: PluginLocal) {
const _ = (label: string): any => `main-ui:${label}`

pluginLocal.on(_('visible'), ({ visible, toggle }) => {
pluginLocal.on(_('visible'), ({ visible, toggle, cursor }) => {
const el = pluginLocal.getMainUI()
el?.classList[toggle ? 'toggle' : (visible ? 'add' : 'remove')]('visible')
// pluginLocal.caller!.callUserModel(LSPMSG, { type: _('visible'), payload: visible })
// auto focus frame
if (!pluginLocal.shadow && el) {
(el as HTMLIFrameElement).contentWindow?.focus()
if (visible) {
if (!pluginLocal.shadow && el) {
(el as HTMLIFrameElement).contentWindow?.focus()
}
}

if (cursor) {
invokeHostExportedApi('restore_editing_cursor')
}
})

Expand Down
6 changes: 4 additions & 2 deletions libs/src/LSPlugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type BlockUUIDTuple = ['uuid', BlockUUID]
type IEntityID = { id: BlockID }

interface AppUserConfigs {
preferredThemeMode: 'dark' | 'light'
preferredFormat: 'markdown' | 'org'
preferredLanguage: string
preferredWorkflow: string
Expand Down Expand Up @@ -108,7 +109,7 @@ type SlashCommandActionCmd =
| 'editor/restore-saved-cursor'
type SlashCommandAction = [cmd: SlashCommandActionCmd, ...args: any]
type BlockCommandCallback = (e: IHookEvent & { uuid: BlockUUID }) => Promise<void>
type BlockCursorPosition = { left: number, top: number, height: number, pos: number, react: DOMRect }
type BlockCursorPosition = { left: number, top: number, height: number, pos: number, rect: DOMRect }

interface IAppProxy {
getUserInfo: () => Promise<any>
Expand Down Expand Up @@ -136,6 +137,7 @@ interface IEditorProxy {
// block related APIs
checkEditing: () => Promise<BlockUUID | boolean>
insertAtEditingCursor: (content: string) => Promise<void>
restoreEditingCursor: () => Promise<void>
getEditingCursorPosition: () => Promise<BlockCursorPosition | null>
getCurrentPage: () => Promise<Partial<BlockEntity> | null>
getCurrentBlock: () => Promise<BlockEntity | null>
Expand Down Expand Up @@ -244,7 +246,7 @@ interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {

showMainUI (): void

hideMainUI (): void
hideMainUI (opts?: { restoreEditingCursor: boolean }): void

toggleMainUI (): void

Expand Down
4 changes: 2 additions & 2 deletions libs/src/LSPlugin.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export class LSPluginUser extends EventEmitter<LSPluginUserEvents> implements IL
this.caller.call('main-ui:style', style)
}

hideMainUI (): void {
const payload = { key: KEY_MAIN_UI, visible: false }
hideMainUI (opts?: { restoreEditingCursor: boolean }): void {
const payload = { key: KEY_MAIN_UI, visible: false, cursor: opts?.restoreEditingCursor }
this.caller.call('main-ui:visible', payload)
this.emit('ui:visible:changed', payload)
this._ui.set(payload.key, payload)
Expand Down
9 changes: 8 additions & 1 deletion src/main/logseq/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
(bean/->js
(normalize-keyword-for-json
{:preferred-language (:preferred-language @state/state)
:preferred-theme-mode (:ui/theme @state/state)
:preferred-format (state/get-preferred-format)
:preferred-workflow (state/get-preferred-workflow)
:preferred-todo (state/get-preferred-todo)
Expand Down Expand Up @@ -161,7 +162,13 @@
(def ^:export insert_at_editing_cursor
(fn [content]
(when-let [input-id (state/get-edit-input-id)]
(commands/simple-insert! input-id content {}))))
(commands/simple-insert! input-id content {})
(.focus (gdom/getElement input-id)))))

(def ^:export restore_editing_cursor
(fn []
(when-let [input-id (state/get-edit-input-id)]
(.focus (gdom/getElement input-id)))))

(def ^:export get_editing_cursor_position
(fn []
Expand Down

0 comments on commit ba1cc8f

Please sign in to comment.