Simplemde-markdown-editor: Add functionality to insert text programatically at cursor position

Created on 8 Sep 2016  路  6Comments  路  Source: sparksuite/simplemde-markdown-editor

Hello!

I'm currently writing a note application, and I want to be able to insert text programatically at the cursor position. I tried it with value(), but that can only insert the text at the end, and as far as I can tell, I couldn't see any proper way to get the cursor position in regards to the raw value, so I can split the raw text and insert the needed addition by myself.
Why would I need it? I'm trying to handle drag&drop functionality also, and upon a file drag I'm inserting the file's path.

idea

Most helpful comment

Did you tried like that?

pos = simplemde.codemirror.getCursor();
simplemde.codemirror.setSelection(pos, pos);
simplemde.codemirror.replaceSelection(image-url);

All 6 comments

I'd like this ability as well, or even just to exposed _replaceSelection() to the outside world. My use case is that my 'Insert Image' button is a custom action that will pop up a modal box allowing the user to search and select images in a custom gallery that we have, and finally return the path of the image back to the application for rendering, which is the part I'm currently stuck at.

Did you tried like that?

pos = simplemde.codemirror.getCursor();
simplemde.codemirror.setSelection(pos, pos);
simplemde.codemirror.replaceSelection(image-url);

codemirror.replaceSelection works fine, thanks. Strangely, I'm sure I had tried this before and it wasn't working. One of those odd problems I guess!

@seamus-45 thanks~ I success

Can also use replaceRange:

const pos = simplemde.codemirror.getCursor();
simplemde.codemirror.replaceRange("hello world", pos);

In the preview it is also possible, or there is another way to intercept the selection in the preview panel ? @seamus-45 @sn3p

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prologic picture prologic  路  3Comments

IonicaBizau picture IonicaBizau  路  3Comments

slince picture slince  路  4Comments

christianmalek picture christianmalek  路  5Comments

prologic picture prologic  路  4Comments