From 1fd39b35b2154f95214b504d9777a50961983593 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Mon, 10 Jul 2023 12:35:40 +0000 Subject: [PATCH] [FIX] mail: fix suggestion hook char deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, the suggestion list would not work when a string with no match was entered then modified. Steps to reproduce: - Open a channel - Type `/hee` (this string matches no suggestion) - Delete one of the `e` in order to get `/he` - This string should match the `/help` suggestion but nothing is shown. There is several issues here: - The suggestion hook is based on the composer selection but the `selectionChange` event is not triggered when deleting text from the textarea which leads to incorrect selection. - The search is cleared in the `suggestionHook` when nothing is found which leads to incorrect delimiter position after a deletion. This commit fixes both issues. closes odoo/odoo#127997 X-original-commit: b8bf65478cad558e02a5902886b3e4272483944e Signed-off-by: Alexandre Kühn (aku) --- .../static/src/core/common/suggestion_hook.js | 2 +- addons/mail/static/src/utils/common/hooks.js | 2 ++ .../tests/discuss/core/suggestion_tests.js | 27 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/addons/mail/static/src/core/common/suggestion_hook.js b/addons/mail/static/src/core/common/suggestion_hook.js index 5519de70d04..59c3ee5342c 100644 --- a/addons/mail/static/src/core/common/suggestion_hook.js +++ b/addons/mail/static/src/core/common/suggestion_hook.js @@ -136,7 +136,7 @@ export function useSuggestion() { ); const { type, mainSuggestions, extraSuggestions = [] } = suggestions; if (!mainSuggestions.length && !extraSuggestions.length) { - self.clearSearch(); + self.state.items = undefined; return; } // arbitrary limit to avoid displaying too many elements at once diff --git a/addons/mail/static/src/utils/common/hooks.js b/addons/mail/static/src/utils/common/hooks.js index 0671ec83451..8b45ae7bdef 100644 --- a/addons/mail/static/src/utils/common/hooks.js +++ b/addons/mail/static/src/utils/common/hooks.js @@ -288,9 +288,11 @@ export function useSelection({ refName, model, preserveOnClickAwayPredicate = () }); onMounted(() => { document.addEventListener("selectionchange", onSelectionChange); + document.addEventListener("input", onSelectionChange); }); onWillUnmount(() => { document.removeEventListener("selectionchange", onSelectionChange); + document.removeEventListener("input", onSelectionChange); }); return { restore() { diff --git a/addons/mail/static/tests/discuss/core/suggestion_tests.js b/addons/mail/static/tests/discuss/core/suggestion_tests.js index 997a352e791..837b6ce5c32 100644 --- a/addons/mail/static/tests/discuss/core/suggestion_tests.js +++ b/addons/mail/static/tests/discuss/core/suggestion_tests.js @@ -2,7 +2,13 @@ import { Composer } from "@mail/core/common/composer"; import { Command } from "@mail/../tests/helpers/command"; -import { click, insertText, start, startServer } from "@mail/../tests/helpers/test_utils"; +import { + afterNextRender, + click, + insertText, + start, + startServer, +} from "@mail/../tests/helpers/test_utils"; import { patchWithCleanup } from "@web/../tests/helpers/utils"; @@ -124,3 +130,22 @@ QUnit.test("Sort partner suggestions by recent chats", async (assert) => { assert.strictEqual($(".o-mail-Composer-suggestion").eq(1).text(), "User 1"); assert.strictEqual($(".o-mail-Composer-suggestion").eq(2).text(), "User 3"); }); + +QUnit.test("suggestion are shown after deleting a character", async (assert) => { + const pyEnv = await startServer(); + const channelId = pyEnv["discuss.channel"].create({ + name: "General", + channel_type: "channel", + }); + const { openDiscuss } = await start(); + await openDiscuss(channelId); + await insertText(".o-mail-Composer-input", "/"); + await insertText(".o-mail-Composer-input", "hee"); + assert.containsNone($, ".o-mail-Composer-suggestion:contains(help)"); + // Simulate pressing backspace + await afterNextRender(() => { + const textarea = document.querySelector(".o-mail-Composer-input"); + textarea.value = textarea.value.slice(0, -1); + }); + assert.containsOnce($, ".o-mail-Composer-suggestion:contains(help)"); +});