0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Correct link shortcut so that it inserts, selects text or moves cursor properly

Closes #2614
 - Determine proper place for selection, or simply move the cursor.
 - When no text is selected insert link markdown and move cursor to a into the [].
 - When text is selected insert link markdown and move selection to http:// so URL can be pasted.
This commit is contained in:
shindakun 2014-04-21 14:55:12 -07:00
parent 3eb284e4b5
commit 98be7b0ac1

View file

@ -15,7 +15,7 @@
self.replace(); self.replace();
}, },
replace: function () { replace: function () {
var text = this.elem.getSelection(), pass = true, cursor = this.elem.getCursor(), line = this.elem.getLine(cursor.line), md, word, letterCount, converter; var text = this.elem.getSelection(), pass = true, cursor = this.elem.getCursor(), line = this.elem.getLine(cursor.line), md, word, letterCount, converter, textIndex, position;
switch (this.style) { switch (this.style) {
case 'h1': case 'h1':
this.elem.setLine(cursor.line, '# ' + line); this.elem.setLine(cursor.line, '# ' + line);
@ -50,7 +50,13 @@
case 'link': case 'link':
md = this.options.syntax.link.replace('$1', text); md = this.options.syntax.link.replace('$1', text);
this.elem.replaceSelection(md, 'end'); this.elem.replaceSelection(md, 'end');
this.elem.setSelection({line: cursor.line, ch: cursor.ch - 8}, {line: cursor.line, ch: cursor.ch - 1}); if (!text) {
this.elem.setCursor(cursor.line, cursor.ch + 1);
} else {
textIndex = line.indexOf(text, cursor.ch - text.length);
position = textIndex + md.length - 1;
this.elem.setSelection({line: cursor.line, ch: position - 7}, {line: cursor.line, ch: position});
}
pass = false; pass = false;
break; break;
case 'image': case 'image':