From 8dc0fa97d4b22d0693362e9dd563c4f9b0f9fd30 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 17 Nov 2015 13:26:34 -0500 Subject: [PATCH] Better behavior When inserting a link with no description --- .../javascripts/discourse/components/d-editor.js.es6 | 12 +++++++++--- test/javascripts/components/d-editor-test.js.es6 | 7 +++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 9c2452f38a..ca76273972 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -407,16 +407,22 @@ export default Ember.Component.extend({ insertLink() { const link = this.get('link'); + const sel = this._lastSel; if (Ember.isEmpty(link)) { return; } const m = / "([^"]+)"/.exec(link); if (m && m.length === 2) { const description = m[1]; const remaining = link.replace(m[0], ''); - this._addText(this._lastSel, `[${description}](${remaining})`); + this._addText(sel, `[${description}](${remaining})`); } else { - const selectedValue = this._lastSel.value || link; - this._addText(this._lastSel, `[${selectedValue}](${link})`); + if (sel.value) { + this._addText(sel, `[${sel.value}](${link})`); + } else { + const desc = I18n.t('composer.link_description'); + this._addText(sel, `[${desc}](${link})`); + this._selectText(sel.start + 1, desc.length); + } } this.set('link', ''); diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index 4d395f45aa..40234c1526 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -183,13 +183,16 @@ testCase('link modal (cancel)', function(assert) { }); }); -testCase('link modal (simple link)', function(assert) { +testCase('link modal (simple link)', function(assert, textarea) { click('button.link'); fillIn('.insert-link input', 'http://eviltrout.com'); click('.insert-link button.btn-primary'); + const desc = I18n.t('composer.link_description'); andThen(() => { assert.equal(this.$('.insert-link.hidden').length, 1); - assert.equal(this.get('value'), 'hello world.[http://eviltrout.com](http://eviltrout.com)'); + assert.equal(this.get('value'), `hello world.[${desc}](http://eviltrout.com)`); + assert.equal(textarea.selectionStart, 13); + assert.equal(textarea.selectionEnd, 13 + desc.length); }); });