Ionaru / easy-markdown-editor

EasyMDE: A simple, beautiful, and embeddable JavaScript Markdown editor. Delightful editing for beginners and experts alike. Features built-in autosaving and spell checking.
https://stackblitz.com/edit/easymde
MIT License
2.39k stars 315 forks source link

Is it possible to get the position of selected text? #599

Open khughitt opened 1 month ago

khughitt commented 1 month ago

Greetings!

Is there a way to determine the start + end position of selected text?

The goal is to add a custom keyboard short cut which triggers a function that modifies the selected text in a particular way (wraps it in some custom formatting).

The simplest approach seems like it would be to use mde.value() to retrieve the full text, replace the desired subsection, and then call mde.value(..) again to update the content, however, I couldn't figure out a straight-forward way to get the position of the selected region.

Any suggestions?

ghiscoding commented 3 weeks ago

I think you can get what you want from the codemirror instance

image

If you really want the char index, then maybe try the .getCursor() in any case, you really have to use the codemirror instance to get these kind of things, see their manual for all available methods