From b7322b05a026b6c0ea5209174bd5bfb30a7b321f Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 5 May 2017 12:59:34 -0400 Subject: [PATCH] FIX: some changes in composer affect all visible text editors --- .../discourse/components/d-editor.js.es6 | 12 +++++--- .../templates/components/composer-editor.hbs | 3 +- .../components/d-editor-test.js.es6 | 29 +++++++++++++++++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index d5228324966..9307e4cffbc 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -246,15 +246,19 @@ export default Ember.Component.extend({ } }); - this.appEvents.on('composer:insert-text', text => this._addText(this._getSelected(), text)); - this.appEvents.on('composer:replace-text', (oldVal, newVal) => this._replaceText(oldVal, newVal)); + if (this.get('composerEvents')) { + 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; }, @on('willDestroyElement') _shutDown() { - this.appEvents.off('composer:insert-text'); - this.appEvents.off('composer:replace-text'); + if (this.get('composerEvents')) { + 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)); diff --git a/app/assets/javascripts/discourse/templates/components/composer-editor.hbs b/app/assets/javascripts/discourse/templates/components/composer-editor.hbs index 53abe779581..46daaeb10e6 100644 --- a/app/assets/javascripts/discourse/templates/components/composer-editor.hbs +++ b/app/assets/javascripts/discourse/templates/components/composer-editor.hbs @@ -10,7 +10,8 @@ togglePreview="togglePreview" validation=validation loading=composer.loading - forcePreview=forcePreview}} + forcePreview=forcePreview + composerEvents=true}}
{{#if site.mobileView}} diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index 1a594cd2dae..cddd81828d7 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -62,6 +62,19 @@ function testCase(title, testFunc) { }); } +function composerTestCase(title, testFunc) { + componentTest(title, { + template: '{{d-editor value=value composerEvents=true}}', + setup() { + this.set('value', 'hello world.'); + }, + test(assert) { + const textarea = jumpEnd(this.$('textarea.d-editor-input')[0]); + testFunc.call(this, assert, textarea); + } + }); +} + testCase(`selecting the space before a word`, function(assert, textarea) { textarea.selectionStart = 5; textarea.selectionEnd = 7; @@ -760,8 +773,19 @@ componentTest('emoji', { } }); -testCase("replace-text event", function(assert) { +testCase("replace-text event by default", function(assert) { + 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 green blue'); + }); +}); + +composerTestCase("replace-text event for composer", function(assert) { this.set('value', "red green blue"); andThen(() => { @@ -773,6 +797,7 @@ testCase("replace-text event", function(assert) { }); }); + (() => { // Tests to check cursor/selection after replace-text event. const BEFORE = 'red green blue'; @@ -849,7 +874,7 @@ testCase("replace-text event", function(assert) { for (let i = 0; i < CASES.length; i++) { const CASE = CASES[i]; - testCase(`replace-text event: ${CASE.description}`, function(assert, textarea) { + composerTestCase(`replace-text event: ${CASE.description}`, function(assert, textarea) { this.set('value', BEFORE); setSelection(textarea, CASE.before); andThen(() => {