[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) <dmo@odoo.com>
This commit is contained in:
Nicolas Bayet
2022-09-12 16:31:05 +02:00
parent 3bd7eb1442
commit 3f5ae2400e
2 changed files with 6 additions and 11 deletions
@@ -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);
@@ -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) => (