[FIX] mail: fix suggestion hook char deletion

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) <aku@odoo.com>
This commit is contained in:
tsm-odoo
2023-07-12 07:40:00 +02:00
parent bbd1a48083
commit 1fd39b35b2
3 changed files with 29 additions and 2 deletions
@@ -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
@@ -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() {
@@ -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)");
});