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

Better cursor handling when creating links and pasting URLs #85

Open simonhurtz opened 5 years ago

simonhurtz commented 5 years ago

I'm a happy user of the note taking app Standardnotes. It has integrated a couple of different open source editors. Most of the time, I use its Advanced Markdown Editor which is based on EasyMDE. Yesterday, I opened an issue in the Standardnotes repository. The dev told me that it would make more sense if I requested the feature over here. Here's the (slightly edited) copy:

I need to create a lot of links within my notes. My workflow looks usually like this: I copy the URL of a certain tab in my browser, switch to Standardnotes and mark the text that should link to the website. Then, I press Ctrl+k in order to create a link.

The shortcut creates the markdown syntax perfectly fine – but the handling of the cursor doesn't make sense to me. Instead of leaving the focus on the text, it would be better if the https:// part could get marked. Like this: Currently: [text] (https://) Preferred: [text] (https://)

When using Ctrl+k after selecting text, you normally don't want to change the text again but paste the URL from your clipboard or type it manually. Either way, it would be better if the cursor could be placed within the () instead of the [].

If that's not possible, could you give us an opportunity to deactivate the automatic insertion of https:// within the brackets? Then, I could just move the cursor with the arrow key and paste the link from the clipboard instead of deleting the prefilled text before.

(I know that it sounds like a trifle, but I really paste a lot of links. A little change could greatly improve my workflow.)

simonhurtz commented 5 years ago

Sorry for bringing this up again: Is there any way to change the behavior when creating a link? I use the shortcut a lot, and it really consumes a lot of time to change the focus manually and select the URL instead of the text.

DanielNoord commented 2 years ago

I looked into this and the place to fix this is probably: https://github.com/Ionaru/easy-markdown-editor/blob/f573560bb44886d7cc8d412a0b6712f18c3081f0/src/js/easymde.js#L853

I believe the second parameter (stat.link) probably needs to change as that is what is controlling the final position of the cursor in _replaceSelection. I don't really have the time to look into this myself and make sure this is tested, but I think this should be fairly trivial to fix for somebody with a bit more knowledge about JavaScript specifics. I'd appreciate this as well as I think this is quite an annoying default behaviour.

Ionaru commented 2 years ago

We can probably introduce an option to change default behaviour and target the link instead.