From 8f39f25cad069c53fdac9f5e1eb617e867e2890c Mon Sep 17 00:00:00 2001 From: cpradio Date: Sat, 7 Nov 2015 07:33:24 -0500 Subject: [PATCH] FIX: Use selected text in the link if text is already selected, otherwise, put the URL --- .../discourse/components/d-editor.js.es6 | 3 ++- test/javascripts/components/d-editor-test.js.es6 | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index cccc12c15ae..4a893e9bf3a 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -404,7 +404,8 @@ export default Ember.Component.extend({ const remaining = link.replace(m[0], ''); this._addText(this._lastSel, `[${description}](${remaining})`); } else { - this._addText(this._lastSel, `[${link}](${link})`); + const selectedValue = this._lastSel.value || link; + this._addText(this._lastSel, `[${selectedValue}](${link})`); } this.set('link', ''); diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index c0ab0172382..4d395f45aa3 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -193,6 +193,19 @@ testCase('link modal (simple link)', function(assert) { }); }); +testCase('link modal (simple link) with selected text', function(assert, textarea) { + textarea.selectionStart = 0; + textarea.selectionEnd = 12; + + click('button.link'); + fillIn('.insert-link input', 'http://eviltrout.com'); + click('.insert-link button.btn-primary'); + andThen(() => { + assert.equal(this.$('.insert-link.hidden').length, 1); + assert.equal(this.get('value'), '[hello world.](http://eviltrout.com)'); + }); +}); + testCase('link modal (link with description)', function(assert) { click('button.link'); fillIn('.insert-link input', 'http://eviltrout.com "evil trout"');