From 3f5ae2400e062db2f88c15f905b48d8c605bd99e Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Mon, 12 Sep 2022 11:02:57 +0000 Subject: [PATCH] [FIX] web_editor: properly set _powerboxBeforeStepIndex Before this commit, `beforeStepIndex` (now `_powerboxBeforeStepIndex`), was set at the wrong time. The callback `onOpen` was called after opening the powerbox from the `_onInput`. The calculation of `beforeStepIndex` presuposed the step that add `/` happened. Thefore the calculation `this._historySteps.length - 2` was made rather than `this._historySteps.length - 1`. But that presuposition is wrong. The best time to know until which step index to revert is before any possible step occurs. task-2980550 closes odoo/odoo#100009 Signed-off-by: David Monjoie (dmo) --- .../src/js/editor/odoo-editor/src/OdooEditor.js | 11 +++++------ .../js/editor/odoo-editor/src/powerbox/Powerbox.js | 6 +----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 907d781f42c..95bf0ef2911 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -409,7 +409,6 @@ export class OdooEditor extends EventTarget { // Powerbox // -------- - let beforeStepIndex; this.powerbox = new Powerbox({ editable: this.editable, getContextFromParentRect: this.options.getContextFromParentRect, @@ -417,13 +416,9 @@ export class OdooEditor extends EventTarget { onShow: () => { this.powerboxTablePicker.hide(); }, - onOpen: () => { - // Undo input '/'. - beforeStepIndex = this._historySteps.length - 2; - }, beforeCommand: () => { if (this._isPowerboxOpenOnInput) { - this._historyRevertUntil(beforeStepIndex); + this._historyRevertUntil(this._powerboxBeforeStepIndex); this.historyStep(true); this._historyStepsStates.set(peek(this._historySteps).id, 'consumed'); setTimeout(() => { @@ -2822,6 +2817,10 @@ export class OdooEditor extends EventTarget { const shouldOpenPowerbox = newSelection.isCollapsed && newSelection.rangeCount && ev.data === '/' && this.powerbox && !this.powerbox.isOpen && (!this.options.getPowerboxElement || !!this.options.getPowerboxElement()); + if (shouldOpenPowerbox) { + // Undo input '/'. + this._powerboxBeforeStepIndex = this._historySteps.length - 1; + } // Record the selection position that was computed on keydown or before // contentEditable execCommand (whatever preceded the 'input' event) this._recordHistorySelection(true); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/powerbox/Powerbox.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/powerbox/Powerbox.js index 1c9ea2ca532..b351ed3b81b 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/powerbox/Powerbox.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/powerbox/Powerbox.js @@ -26,14 +26,13 @@ function cycle(num, max) { export class Powerbox { constructor({ categories, commands, commandFilters, editable, getContextFromParentRect, - onOpen, onShow, onStop, beforeCommand, afterCommand + onShow, onStop, beforeCommand, afterCommand } = {}) { this.categories = categories; this.commands = commands; this.commandFilters = commandFilters || []; this.editable = editable; this.getContextFromParentRect = getContextFromParentRect; - this.onOpen = onOpen; this.onShow = onShow; this.onStop = onStop; this.beforeCommand = beforeCommand; @@ -102,9 +101,6 @@ export class Powerbox { priority: category.priority || 0, })); const order = (a, b) => b.priority - a.priority || a.name.localeCompare(b.name); - if (this.onOpen) { - this.onOpen(); - } // Remove duplicate category names, keeping only last declared version, // and order them. categories = [...categories].reverse().filter((category, index, cats) => (