فهرست منبع

improve(plugin): add editting cursor api & types

charlie 4 سال پیش
والد
کامیت
ba1cc8f752
4فایلهای تغییر یافته به همراه23 افزوده شده و 8 حذف شده
  1. 9 3
      libs/src/LSPlugin.core.ts
  2. 4 2
      libs/src/LSPlugin.d.ts
  3. 2 2
      libs/src/LSPlugin.user.ts
  4. 8 1
      src/main/logseq/api.cljs

+ 9 - 3
libs/src/LSPlugin.core.ts

@@ -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')
     }
   })
 

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

@@ -71,6 +71,7 @@ type BlockUUIDTuple = ['uuid', BlockUUID]
 type IEntityID = { id: BlockID }
 
 interface AppUserConfigs {
+  preferredThemeMode: 'dark' | 'light'
   preferredFormat: 'markdown' | 'org'
   preferredLanguage: string
   preferredWorkflow: string
@@ -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>
@@ -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>
@@ -244,7 +246,7 @@ interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
 
   showMainUI (): void
 
-  hideMainUI (): void
+  hideMainUI (opts?: { restoreEditingCursor: boolean }): void
 
   toggleMainUI (): void
 

+ 2 - 2
libs/src/LSPlugin.user.ts

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

+ 8 - 1
src/main/logseq/api.cljs

@@ -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)
@@ -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 []