From 7b386de6f5a9f3ce4bdcffb1788a67cf1fa105fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Fri, 15 Jan 2021 17:23:46 +0000 Subject: [PATCH] [FIX] mail: prevent composer cursor from randomly changing position MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit task-2388600 closes odoo/odoo#64830 X-original-commit: 785fdb6cea6ee7d9a1a4e4c6a44403a275a67763 Signed-off-by: Alexandre Kühn (aku) --- .../composer_text_input.js | 19 +++++++++++-------- .../static/src/models/composer/composer.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/addons/mail/static/src/components/composer_text_input/composer_text_input.js b/addons/mail/static/src/components/composer_text_input/composer_text_input.js index 51a7143ec98..75bce5ac833 100644 --- a/addons/mail/static/src/components/composer_text_input/composer_text_input.js +++ b/addons/mail/static/src/components/composer_text_input/composer_text_input.js @@ -163,14 +163,16 @@ class ComposerTextInput extends Component { if (!this.composer) { return; } - this._textareaRef.el.value = this.composer.textInputContent; - this._mirroredTextareaRef.el.value = this.composer.textInputContent; - if (this.composer.hasFocus) { - this._textareaRef.el.setSelectionRange( - this.composer.textInputCursorStart, - this.composer.textInputCursorEnd, - this.composer.textInputSelectionDirection, - ); + if (this.composer.isLastStateChangeProgrammatic) { + this._textareaRef.el.value = this.composer.textInputContent; + if (this.composer.hasFocus) { + this._textareaRef.el.setSelectionRange( + this.composer.textInputCursorStart, + this.composer.textInputCursorEnd, + this.composer.textInputSelectionDirection, + ); + } + this.composer.update({ isLastStateChangeProgrammatic: false }); } this._updateHeight(); } @@ -181,6 +183,7 @@ class ComposerTextInput extends Component { * @private */ _updateHeight() { + this._mirroredTextareaRef.el.value = this.composer.textInputContent; this._textareaRef.el.style.height = (this._mirroredTextareaRef.el.scrollHeight) + "px"; } diff --git a/addons/mail/static/src/models/composer/composer.js b/addons/mail/static/src/models/composer/composer.js index 7505a57ce30..c3b6e9e840c 100644 --- a/addons/mail/static/src/models/composer/composer.js +++ b/addons/mail/static/src/models/composer/composer.js @@ -160,6 +160,7 @@ function factory(dependencies) { this.textInputContent.length ); this.update({ + isLastStateChangeProgrammatic: true, textInputContent: partA + content + partB, textInputCursorStart: this.textInputCursorStart + content.length, textInputCursorEnd: this.textInputCursorStart + content.length, @@ -208,6 +209,7 @@ function factory(dependencies) { break; } this.update({ + isLastStateChangeProgrammatic: true, textInputContent: textLeft + recordReplacement + ' ' + textRight, textInputCursorEnd: textLeft.length + recordReplacement.length + 1, textInputCursorStart: textLeft.length + recordReplacement.length + 1, @@ -722,6 +724,7 @@ function factory(dependencies) { this.closeSuggestions(); this.update({ attachments: [['unlink-all']], + isLastStateChangeProgrammatic: true, mentionedChannels: [['unlink-all']], mentionedPartners: [['unlink-all']], subjectContent: "", @@ -1011,6 +1014,16 @@ function factory(dependencies) { hasToScrollToActiveSuggestion: attr({ default: false, }), + /** + * Determines whether the last change (since the last render) was + * programmatic. Useful to avoid restoring the state when its change was + * from a user action, in particular to prevent the cursor from jumping + * to its previous position after the user clicked on the textarea while + * it didn't have the focus anymore. + */ + isLastStateChangeProgrammatic: attr({ + default: false, + }), /** * If true composer will log a note, else a comment will be posted. */