fix(calc-02): restore edited value on cancel

Canceling stack-top editing now pushes the original value back before
leaving edit mode, including Escape and ArrowDown handling.
This commit is contained in:
2026-05-17 01:31:43 +02:00
parent 736154110d
commit 5cc97f754d
+18 -1
View File
@@ -95,6 +95,7 @@ function focusInput() {
let statusTimer = null; let statusTimer = null;
let editCursor = 0; let editCursor = 0;
let editRestoreValue = null;
function setStatus(message, isError = false, timeoutMs = 1400) { function setStatus(message, isError = false, timeoutMs = 1400) {
if (!statusLine) return; if (!statusLine) return;
@@ -173,6 +174,14 @@ function stopEditing(clearValue = false) {
editCursor = 0; editCursor = 0;
} }
function cancelEditing() {
if (editRestoreValue !== null) {
calc.push(editRestoreValue);
}
editRestoreValue = null;
stopEditing(true);
}
function moveEditCursor(delta) { function moveEditCursor(delta) {
editCursor = Math.max(0, Math.min(calc.inputValue.length, editCursor + delta)); editCursor = Math.max(0, Math.min(calc.inputValue.length, editCursor + delta));
} }
@@ -182,6 +191,7 @@ function pushEditingValueIfNeeded() {
if (calc.inputValue !== '') { if (calc.inputValue !== '') {
calc.push(calc.parseInputValue(calc.inputValue)); calc.push(calc.parseInputValue(calc.inputValue));
} }
editRestoreValue = null;
calc.inputValue = ''; calc.inputValue = '';
calc.isEditing = false; calc.isEditing = false;
editCursor = 0; editCursor = 0;
@@ -190,6 +200,7 @@ function pushEditingValueIfNeeded() {
function startEditingFromStackTop() { function startEditingFromStackTop() {
if (!calc.isValidIndex(0)) return false; if (!calc.isValidIndex(0)) return false;
const value = calc.stack[0]; const value = calc.stack[0];
editRestoreValue = value;
calc.remove(0); calc.remove(0);
calc.isEditing = true; calc.isEditing = true;
calc.inputValue = calc.formatNumber(value); calc.inputValue = calc.formatNumber(value);
@@ -247,7 +258,7 @@ function execute(name) {
calc.clear(); calc.clear();
stopEditing(true); stopEditing(true);
} else if (name === 'escape') { } else if (name === 'escape') {
stopEditing(true); cancelEditing();
} else if (name === 'backspace') { } else if (name === 'backspace') {
if (calc.isEditing) { if (calc.isEditing) {
inputToX('Backspace'); inputToX('Backspace');
@@ -383,6 +394,11 @@ function handleKeyboard(event) {
} }
if (key === 'ArrowDown') { if (key === 'ArrowDown') {
event.preventDefault(); event.preventDefault();
if (calc.isEditing) {
cancelEditing();
render();
return;
}
downButton.click(); downButton.click();
return; return;
} }
@@ -543,6 +559,7 @@ downButton.addEventListener('click', () => {
}); });
rightButton.addEventListener('click', () => { rightButton.addEventListener('click', () => {
if (calc.isEditing) { if (calc.isEditing) {
moveEditCursor(1); moveEditCursor(1);