logseq / plugins

Documentation site of Logseq plugins
https://plugins-doc.logseq.com/
21 stars 4 forks source link

🧩 logseq.Editor.insertAtEditingCursor #18

Open utterances-bot opened 6 months ago

utterances-bot commented 6 months ago

https://plugins-doc.logseq.com/logseq/Editor/insertAtEditingCursor

neongreen commented 6 months ago

Is there a command to insert around editing cursor? Eg. I want smth like

insertAroundEditingCursor('[[', ']]')

so that I can create a shortcut for "insert a page reference"

YU000jp commented 5 months ago

@neongreen

Is there a command to insert around editing cursor? Eg. I want smth like

insertAroundEditingCursor('[[', ']]')

so that I can create a shortcut for "insert a page reference"

Several steps are required. The "pos" accept the position of the letter. https://logseq.github.io/plugins/types/BlockCursorPosition.html Thus, the following process is possible.

const blockEntity = await logseq.Editor.getCurrentBlock()
if(blockEntity){
  const blockPos = await logseq.Editor.getEditingCursorPosition()
  if(blockPos){
    await logseq.Editor.insertAtEditingCursor("[[]]")
    setTimeout(() => { 
      await logseq.Editor.editBlock(blockEntity.uuid, { pos: blockPos.pos+2 })
    }, 200)
  }
}

I have never done anything similar so I am guessing. This is a TypeScript expression. (Sorry, I forgot how to write JavaScript)