diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index 71c660807..c5113a20c 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -189,7 +189,7 @@ export default Ember.Component.extend({ this.setProperties({ uploadProgress: 0, isUploading: false, isCancellable: false }); } if (removePlaceholder) { - this.set('composer.reply', this.get('composer.reply').replace(this.get('uploadPlaceholder'), "")); + this.appEvents.trigger('composer:replace-text', this.get('uploadPlaceholder'), ""); } }, @@ -219,7 +219,6 @@ export default Ember.Component.extend({ $element.on("fileuploadsend", (e, data) => { this._validUploads++; - // add upload placeholders this.appEvents.trigger('composer:insert-text', uploadPlaceholder); if (data.xhr && data.originalFiles.length === 1) { @@ -244,7 +243,7 @@ export default Ember.Component.extend({ if (upload && upload.url) { if (!this._xhr || !this._xhr._userCancelled) { const markdown = getUploadMarkdown(upload); - this.set('composer.reply', this.get('composer.reply').replace(uploadPlaceholder, markdown)); + this.appEvents.trigger('composer:replace-text', uploadPlaceholder, markdown); this._resetUpload(false); } else { this._resetUpload(true); diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 67c5d0b1b..bd8601f7d 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -215,9 +215,8 @@ export default Ember.Component.extend({ return false; }); - this.appEvents.on('composer:insert-text', text => { - this._addText(this._getSelected(), text); - }); + this.appEvents.on('composer:insert-text', text => this._addText(this._getSelected(), text)); + this.appEvents.on('composer:replace-text', (oldVal, newVal) => this._replaceText(oldVal, newVal)); this._mouseTrap = mouseTrap; }, @@ -225,6 +224,7 @@ export default Ember.Component.extend({ @on('willDestroyElement') _shutDown() { this.appEvents.off('composer:insert-text'); + this.appEvents.off('composer:replace-text'); const mouseTrap = this._mouseTrap; Object.keys(this.get('toolbar.shortcuts')).forEach(sc => mouseTrap.unbind(sc)); @@ -475,6 +475,15 @@ export default Ember.Component.extend({ } }, + _replaceText(oldVal, newVal) { + const val = this.get('value'); + const loc = val.indexOf(oldVal); + if (loc !== -1) { + this.set('value', val.replace(oldVal, newVal)); + this._selectText(loc + newVal.length, 0); + } + }, + _addText(sel, text) { const $textarea = this.$('textarea.d-editor-input'); const insert = `${sel.pre}${text}`; diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index 16bbbd063..abebfe9cf 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -596,3 +596,18 @@ componentTest('emoji', { }); } }); + +testCase("replace-text event", function(assert, textarea) { + + this.set('value', "red green blue"); + + andThen(() => { + this.container.lookup('app-events:main').trigger('composer:replace-text', 'green', 'yellow'); + }); + + andThen(() => { + assert.equal(this.get('value'), 'red yellow blue'); + assert.equal(textarea.selectionStart, 10); + assert.equal(textarea.selectionEnd, 10); + }); +});