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.45k stars 319 forks source link

How to insert text on current cursor #273

Closed Uzay-G closed 3 years ago

Uzay-G commented 3 years ago

Hi,

I was trying to add a button that would insert some text where the user's writing, like what most of the markup commands do.

Something like this:

          let editor = new EasyMDE({
            toolbar: [
              {
                name: "custom-command",
                    action: (editor) => {
                    editor.value(editor.value() + "||")
                }
              }
            ],
          });

Here I'm trying to add a || to where the user is currently writing, but (as it should) it adds it to the end of the editor content.

How would I go about adding it to where the user is writing?

I've really been trying to read the docs but I can't find anything about it. Thanks!

Uzay-G commented 3 years ago

For those wondering, the solution to this is using the replaceSelection function of codemirror

Ex:

          let editor = new EasyMDE({
            toolbar: [
              {
                name: "custom-command",
                    action: (editor) => {
                    editor.codemirror.replaceSelection("||")
                }
              }
            ],
          });