[IMP] web: onClose Command Palette

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 <ged@odoo.com>
This commit is contained in:
FrancoisGe
2022-02-18 14:28:49 +00:00
parent 94c5caa4d6
commit d84cc6329a
2 changed files with 23 additions and 3 deletions
@@ -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();
}
},
}
);
@@ -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"]);
});