diff --git a/samples/calc-02/index.js b/samples/calc-02/index.js index 88ba986..cd289a2 100644 --- a/samples/calc-02/index.js +++ b/samples/calc-02/index.js @@ -95,6 +95,7 @@ function focusInput() { let statusTimer = null; let editCursor = 0; +let editRestoreValue = null; function setStatus(message, isError = false, timeoutMs = 1400) { if (!statusLine) return; @@ -173,6 +174,14 @@ function stopEditing(clearValue = false) { editCursor = 0; } +function cancelEditing() { + if (editRestoreValue !== null) { + calc.push(editRestoreValue); + } + editRestoreValue = null; + stopEditing(true); +} + function moveEditCursor(delta) { editCursor = Math.max(0, Math.min(calc.inputValue.length, editCursor + delta)); } @@ -182,6 +191,7 @@ function pushEditingValueIfNeeded() { if (calc.inputValue !== '') { calc.push(calc.parseInputValue(calc.inputValue)); } + editRestoreValue = null; calc.inputValue = ''; calc.isEditing = false; editCursor = 0; @@ -190,6 +200,7 @@ function pushEditingValueIfNeeded() { function startEditingFromStackTop() { if (!calc.isValidIndex(0)) return false; const value = calc.stack[0]; + editRestoreValue = value; calc.remove(0); calc.isEditing = true; calc.inputValue = calc.formatNumber(value); @@ -247,7 +258,7 @@ function execute(name) { calc.clear(); stopEditing(true); } else if (name === 'escape') { - stopEditing(true); + cancelEditing(); } else if (name === 'backspace') { if (calc.isEditing) { inputToX('Backspace'); @@ -383,6 +394,11 @@ function handleKeyboard(event) { } if (key === 'ArrowDown') { event.preventDefault(); + if (calc.isEditing) { + cancelEditing(); + render(); + return; + } downButton.click(); return; } @@ -543,6 +559,7 @@ downButton.addEventListener('click', () => { }); + rightButton.addEventListener('click', () => { if (calc.isEditing) { moveEditCursor(1);