fix: preserve full calc-02 stack while limiting display to top 4

Keep the calculator stack unlimited in the demo and only constrain the rendered stack view. Also restore the edit cursor position when pulling a value into the input from the stack.
This commit is contained in:
2026-05-16 23:40:13 +02:00
parent acc075d30c
commit 07a4c533fb
2 changed files with 12 additions and 12 deletions
+1
View File
@@ -2,6 +2,7 @@
- Core engine: `src/rpn-calculator.js` - Core engine: `src/rpn-calculator.js`
- Reference demo: `samples/calc-02/` (portrait-first HP48GX layout, compact mode/constants popups; Const button comes before Mode in the display row) - Reference demo: `samples/calc-02/` (portrait-first HP48GX layout, compact mode/constants popups; Const button comes before Mode in the display row)
- Important UI behavior: mode button shows the current angle mode; keyboard focus stays on the hidden input on desktop; clipboard paste is supported - Important UI behavior: mode button shows the current angle mode; keyboard focus stays on the hidden input on desktop; clipboard paste is supported
- Note: keep scrolling behavior in mind for the calc-02 demo when changing the stack/UI layout
- Public API: `push`, `pop`, `clear`, `swap`, `remove`, `edit`, `isValidIndex`, `input`, `command`, `getOperationsByCategory`, `getConstants`, `listConstants`, `setConstant`, `removeConstant`, `hasConstant` - Public API: `push`, `pop`, `clear`, `swap`, `remove`, `edit`, `isValidIndex`, `input`, `command`, `getOperationsByCategory`, `getConstants`, `listConstants`, `setConstant`, `removeConstant`, `hasConstant`
- Config: `maxSize`, `base`, `angleMode`, `enabledCommands` - Config: `maxSize`, `base`, `angleMode`, `enabledCommands`
- Commands: arithmetic, stack, trigonometry, constants `pi` and `e`; arithmetic includes `root`; constants can be added or removed dynamically through the core API - Commands: arithmetic, stack, trigonometry, constants `pi` and `e`; arithmetic includes `root`; constants can be added or removed dynamically through the core API
+5 -6
View File
@@ -120,9 +120,7 @@ function clearStatus() {
} }
function normalizeStack() { function normalizeStack() {
while (calc.stack.length > 4) { // Demo display only shows the top 4 stack values; the calculator stack remains unlimited.
calc.stack.shift();
}
} }
function getStackLine(indexFromTop) { function getStackLine(indexFromTop) {
@@ -159,7 +157,6 @@ function updateCopyButtons() {
} }
function render() { function render() {
normalizeStack();
const isPortrait = window.matchMedia('(orientation: portrait)').matches || window.innerWidth <= 860; const isPortrait = window.matchMedia('(orientation: portrait)').matches || window.innerWidth <= 860;
calculatorEl?.classList.toggle('portrait', isPortrait); calculatorEl?.classList.toggle('portrait', isPortrait);
calculatorEl?.classList.toggle('landscape', !isPortrait); calculatorEl?.classList.toggle('landscape', !isPortrait);
@@ -520,16 +517,18 @@ leftButton.addEventListener('click', () => {
} }
}); });
downButton.addEventListener('click', () => { downButton.addEventListener('click', () => {
if (!calc.isEditing && calc.isValidIndex(0)) { if (!calc.isEditing && calc.isValidIndex(0)) {
const value = calc.stack[0]; const value = calc.stack[0];
calc.remove(0); calc.remove(0);
calc.isEditing = true; calc.isEditing = true;
calc.inputValue = calc.formatNumber(value); calc.inputValue = calc.formatNumber(value);
editCursor = calc.inputValue.length;
render(); render();
focusInput(); focusInput();
} }
}); });
rightButton.addEventListener('click', () => { rightButton.addEventListener('click', () => {
if (calc.isEditing) { if (calc.isEditing) {
editCursor = Math.min(calc.inputValue.length, editCursor + 1); editCursor = Math.min(calc.inputValue.length, editCursor + 1);