[FIX] web_editor: update all values when using up/down on multi-value

When using the up/down arrows in an `InputUserValueWidget`, only the
first value is updated but the other ones are lost.

This commit adapts the behavior so that all parts of the composite value
are updated.
When decreasing, parts that have reached zero are set to zero.

Steps to reproduce:
- Drop a "Banner" snippet.
- Select the text block.
- Set the "Border" option to "5 10".
- Press the up or down key.

=> Only the "5" was updated, the "10" was lost.

task-3800288

closes odoo/odoo#159385

Signed-off-by: Robin Lejeune (role) <role@odoo.com>
This commit is contained in:
Benoit Socias
2024-03-28 10:19:39 +00:00
parent 5a5b77de85
commit 469138e229
@@ -1433,34 +1433,48 @@ const InputUserValueWidget = UnitUserValueWidget.extend({
case "ArrowUp":
case "ArrowDown": {
const input = ev.currentTarget;
let value = parseFloat(input.value || input.placeholder);
if (isNaN(value)) {
value = 0.0;
let parts = (input.value || input.placeholder).match(/-?\d+\.\d+|-?\d+/g);
if (!parts) {
parts = [input.value || input.placeholder];
}
let step = parseFloat(params.step);
if (isNaN(step)) {
step = 1.0;
if (parts.length > 1 && !('min' in params)) {
// No negative for composite values.
params['min'] = 0;
}
const newValue = parts.map(part => {
let value = parseFloat(part);
if (isNaN(value)) {
value = 0.0;
}
let step = parseFloat(params.step);
if (isNaN(step)) {
step = 1.0;
}
const increasing = ev.key === "ArrowUp";
const hasMin = ('min' in params);
const hasMax = ('max' in params);
const increasing = ev.key === "ArrowUp";
const hasMin = ('min' in params);
const hasMax = ('max' in params);
// If value already at min and trying to decrease, do nothing
if (!increasing && hasMin && Math.abs(value - params.min) < 0.001) {
// If value already at min and trying to decrease, do nothing
if (!increasing && hasMin && Math.abs(value - params.min) < 0.001) {
return value;
}
// If value already at max and trying to increase, do nothing
if (increasing && hasMax && Math.abs(value - params.max) < 0.001) {
return value;
}
// If trying to decrease/increase near min/max, we still need to
// bound the produced value and immediately show the user.
value += (increasing ? step : -step);
value = hasMin ? Math.max(params.min, value) : value;
value = hasMax ? Math.min(value, params.max) : value;
return this._floatToStr(value);
}).join(" ");
if (newValue === (input.value || input.placeholder)) {
return;
}
// If value already at max and trying to increase, do nothing
if (increasing && hasMax && Math.abs(value - params.max) < 0.001) {
return;
}
// If trying to decrease/increase near min/max, we still need to
// bound the produced value and immediately show the user.
value += (increasing ? step : -step);
value = hasMin ? Math.max(params.min, value) : value;
value = hasMax ? Math.min(value, params.max) : value;
input.value = this._floatToStr(value);
input.value = newValue;
// We need to know if the change event will be triggered or not.
// Change is triggered if there has been a "natural" input event