From d84cc6329a49a0c44840dbdbf4618d4dbf17badd Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Tue, 15 Feb 2022 13:53:45 +0000 Subject: [PATCH] [IMP] web: onClose Command Palette MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the option to apply an onClose function when the Command Palette closes. closes odoo/odoo#84619 Related: odoo/enterprise#24356 Signed-off-by: Géry Debongnie --- .../static/src/core/commands/command_service.js | 11 ++++++++--- .../tests/core/commands/command_service_tests.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/core/commands/command_service.js b/addons/web/static/src/core/commands/command_service.js index dbac7239ab9..d5bae4b7066 100644 --- a/addons/web/static/src/core/commands/command_service.js +++ b/addons/web/static/src/core/commands/command_service.js @@ -69,9 +69,10 @@ export const commandService = { /** * @param {CommandPaletteConfig} config command palette config merged with default config + * @param {Function} onClose called when the command palette is closed * @returns the actual command palette config if the command palette is already open */ - function openMainPalette(config = {}) { + function openMainPalette(config = {}, onClose) { const configByNamespace = {}; for (const provider of commandProviderRegistry.getAll()) { const namespace = provider.namespace || "default"; @@ -112,14 +113,15 @@ export const commandService = { }, config ); - return openPalette(config); + return openPalette(config, onClose); } /** * @param {CommandPaletteConfig} config + * @param {Function} onClose called when the command palette is closed * @returns config if the command palette is already open */ - function openPalette(config) { + function openPalette(config, onClose) { if (isPaletteOpened) { return config; } @@ -134,6 +136,9 @@ export const commandService = { { onClose: () => { isPaletteOpened = false; + if (onClose) { + onClose(); + } }, } ); diff --git a/addons/web/static/tests/core/commands/command_service_tests.js b/addons/web/static/tests/core/commands/command_service_tests.js index de875eb6eaa..b2ea7c80810 100644 --- a/addons/web/static/tests/core/commands/command_service_tests.js +++ b/addons/web/static/tests/core/commands/command_service_tests.js @@ -938,3 +938,18 @@ QUnit.test("display shortcuts correctly for MacOS with a new overlayModifier", a ["ClickCONTROL + COMMAND + A"] ); }); + +QUnit.test("openMainPalette with onClose", async (assert) => { + const command = env.services.command; + command.openMainPalette({}, () => { + assert.step("onClose"); + }); + await mount(TestComponent, target, { env }); + + await nextTick(); + assert.containsOnce(target, ".o_command_palette"); + + triggerHotkey("escape"); + await nextTick(); + assert.verifySteps(["onClose"]); +});