sparksuite / simplemde-markdown-editor

A simple, beautiful, and embeddable JavaScript Markdown editor. Delightful editing for beginners and experts alike. Features built-in autosaving and spell checking.
https://simplemde.com
MIT License
9.92k stars 1.12k forks source link

Add functionality to insert text programatically at cursor position #429

Open zeroxx1986 opened 8 years ago

zeroxx1986 commented 8 years ago

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.

chrisemerson commented 7 years ago

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.

seamus-45 commented 7 years ago

Did you tried like that?

pos = simplemde.codemirror.getCursor();
simplemde.codemirror.setSelection(pos, pos);
simplemde.codemirror.replaceSelection(image-url);
chrisemerson commented 7 years ago

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!

yeszao commented 6 years ago

@seamus-45 thanks~ I success

sn3p commented 5 years ago

Can also use replaceRange:

const pos = simplemde.codemirror.getCursor();
simplemde.codemirror.replaceRange("hello world", pos);
Arkango commented 4 years ago

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