0

How do I get situation of cursor's node?

enter image description here

<span role="presentation" style="padding-right: 0.1px;">
  <span class="cm-strong">**dfgdf fghh trth**</span>
</span>

I am writing a markdown toolbar.

I try to find out text at current cursor postion is bold or not.

pseudocode:

if cursorNode is bold
  remove bold
else
  add bold

I expect something like getCurrorNode API exist, but just can't find this API.


let cursorPos = editor.getCursor();
editor.getTokenAt(cursorPos); //only get a word

token only get a word, not a sentence.

user2959760
  • 460
  • 4
  • 12

1 Answers1

0

document.elementFromPoint + codemirror's cursorCoords:

document.elementFromPoint(cm.cursorCoords(cm.getCursor()).left, 
                          cm.cursorCoords(cm.getCursor()).top);

ref: Determine which element the mouse pointer is on top of in Javascript


there are still one problem: This is a fake change.
Even you change this element, it doesn't reflect on actually cm. Once you type a random character on this line, the original sentence come back!

user2959760
  • 460
  • 4
  • 12