Browse Source

improve(plugin): api of editing cursor position & types

charlie 4 years ago
parent
commit
fd998d26b2
2 changed files with 8 additions and 0 deletions
  1. 2 0
      libs/src/LSPlugin.d.ts
  2. 6 0
      src/main/logseq/api.cljs

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

@@ -108,6 +108,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 }
 
 interface IAppProxy {
   getUserInfo: () => Promise<any>
@@ -135,6 +136,7 @@ interface IEditorProxy {
   // block related APIs
   checkEditing: () => Promise<BlockUUID | boolean>
   insertAtEditingCursor: (content: string) => Promise<void>
+  getEditingCursorPosition: () => Promise<BlockCursorPosition | null>
   getCurrentPage: () => Promise<Partial<BlockEntity> | null>
   getCurrentBlock: () => Promise<BlockEntity | null>
   getCurrentBlockContent: () => Promise<string>

+ 6 - 0
src/main/logseq/api.cljs

@@ -9,6 +9,7 @@
             [frontend.util :as util]
             [electron.ipc :as ipc]
             [promesa.core :as p]
+            [goog.dom :as gdom]
             [sci.core :as sci]
             [lambdaisland.glogi :as log]
             [camel-snake-kebab.core :as csk]
@@ -162,6 +163,11 @@
     (when-let [input-id (state/get-edit-input-id)]
       (commands/simple-insert! input-id content {}))))
 
+(def ^:export get_editing_cursor_position
+  (fn []
+    (when-let [input-id (state/get-edit-input-id)]
+      (bean/->js (normalize-keyword-for-json (util/get-caret-pos (gdom/getElement input-id)))))))
+
 (def ^:export get_current_block
   (fn []
     (let [block (state/get-edit-block)