From 469138e229a9729d86da30d79f4fb00ac972dcba Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Mon, 25 Mar 2024 17:07:58 +0100 Subject: [PATCH] [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) --- .../static/src/js/editor/snippets.options.js | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index db5f107214a..e84411cd4bc 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -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