Compare commits
12 Commits
c69380d057
..
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 9abdc33713 | |||
| 6dd9550890 | |||
| f44fb8c252 | |||
| 5cc97f754d | |||
| 736154110d | |||
| 62a0f447c5 | |||
| 16b567731f | |||
| b710d5f0eb | |||
| 07a4c533fb | |||
| acc075d30c | |||
| 6a37c7c1e8 | |||
| 5f2d18df99 |
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
# State
|
# State
|
||||||
- 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; calc-02 keyboard shortcuts are the reference; 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
|
||||||
|
|||||||
@@ -275,12 +275,15 @@ The current demo supports:
|
|||||||
- `Escape`
|
- `Escape`
|
||||||
- `ArrowUp`, `ArrowDown`, `ArrowRight`
|
- `ArrowUp`, `ArrowDown`, `ArrowRight`
|
||||||
- `+`, `-`, `*`, `/`, `%`, `^`
|
- `+`, `-`, `*`, `/`, `%`, `^`
|
||||||
- `q`, `n`, `r`, `i`, `g`, `l`, `s`, `c`, `S`, `C`
|
- `s`, `S`, `r`, `R`, `v`, `u`
|
||||||
- `x`, `y`, `z`, `t`
|
- `l`, `L`, `n`, `N`, `e`, `E`
|
||||||
|
- `i`, `o`, `a`, `I`, `O`, `A`
|
||||||
|
|
||||||
The demo also implements stack selection and stack-item move mode in its UI layer using the public calculator methods.
|
The demo also implements stack selection and stack-item move mode in its UI layer using the public calculator methods.
|
||||||
It keeps the calculator screen focused and updates the visible stack window as the selection moves.
|
It keeps the calculator screen focused and updates the visible stack window as the selection moves.
|
||||||
|
|
||||||
|
Note: `calc-02` keyboard shortcuts are the reference for this project.
|
||||||
|
|
||||||
## Exports
|
## Exports
|
||||||
|
|
||||||
`RpnCalculator` is exposed in both environments:
|
`RpnCalculator` is exposed in both environments:
|
||||||
|
|||||||
+62
-11
@@ -56,14 +56,15 @@ body {
|
|||||||
height: 100vh;
|
height: 100vh;
|
||||||
max-height: 100vh;
|
max-height: 100vh;
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
|
display: grid;
|
||||||
|
grid-template-rows: auto auto minmax(0, 1fr);
|
||||||
|
gap: 8px;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
background: var(--panel);
|
background: var(--panel);
|
||||||
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
||||||
justify-content: stretch;
|
justify-content: stretch;
|
||||||
align-content: start;
|
align-content: start;
|
||||||
overflow-y: auto;
|
overflow: hidden;
|
||||||
scrollbar-width: thin;
|
|
||||||
scrollbar-color: rgba(46, 105, 135, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.display-block,
|
.display-block,
|
||||||
@@ -122,10 +123,7 @@ body {
|
|||||||
padding-block: 0;
|
padding-block: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.stack-label {
|
/* removed extra first-column styling */
|
||||||
text-align: right;
|
|
||||||
opacity: 0.78;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stack-value {
|
.stack-value {
|
||||||
min-height: 0;
|
min-height: 0;
|
||||||
@@ -136,9 +134,26 @@ body {
|
|||||||
justify-self: end;
|
justify-self: end;
|
||||||
font-size: 20px;
|
font-size: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.stack-value.is-editing {
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.stack-cell.is-selected {
|
||||||
|
background: transparent;
|
||||||
|
border-radius: 6px;
|
||||||
|
outline: 2px solid rgba(31, 42, 18, 0.2);
|
||||||
|
outline-offset: -2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-cell.is-moving {
|
||||||
|
background: rgba(31, 42, 18, 0.18);
|
||||||
|
}
|
||||||
.display-buttons-panel {
|
.display-buttons-panel {
|
||||||
padding: 8px;
|
padding: 8px;
|
||||||
min-height: 0;
|
min-height: 0;
|
||||||
|
background: rgba(6, 10, 16, 0.16);
|
||||||
}
|
}
|
||||||
|
|
||||||
.display-buttons-grid {
|
.display-buttons-grid {
|
||||||
@@ -225,12 +240,17 @@ body {
|
|||||||
|
|
||||||
.calculator > .display-block,
|
.calculator > .display-block,
|
||||||
.calculator > .display-buttons-panel,
|
.calculator > .display-buttons-panel,
|
||||||
.calculator > .keypad-panel,
|
.calculator > .keypad-scroll-area {
|
||||||
.calculator > .functions-panel,
|
|
||||||
.calculator > .trigo-panel {
|
|
||||||
justify-self: stretch;
|
justify-self: stretch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.keypad-scroll-area {
|
||||||
|
min-height: 0;
|
||||||
|
overflow-y: auto;
|
||||||
|
scrollbar-width: thin;
|
||||||
|
scrollbar-color: rgba(46, 105, 135, 1);
|
||||||
|
}
|
||||||
|
|
||||||
.status-bar {
|
.status-bar {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
@@ -287,7 +307,6 @@ button {
|
|||||||
border: 1px solid rgba(14, 18, 25, 0.85);
|
border: 1px solid rgba(14, 18, 25, 0.85);
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
padding: 8px 8px;
|
padding: 8px 8px;
|
||||||
|
|
||||||
font: inherit;
|
font: inherit;
|
||||||
font-weight: 700;
|
font-weight: 700;
|
||||||
color: var(--btnText);
|
color: var(--btnText);
|
||||||
@@ -322,6 +341,7 @@ button:active {
|
|||||||
opacity: 0.7;
|
opacity: 0.7;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.stack-copy-button {
|
.stack-copy-button {
|
||||||
padding: 4px;
|
padding: 4px;
|
||||||
min-width: 24px;
|
min-width: 24px;
|
||||||
@@ -372,6 +392,37 @@ button:active {
|
|||||||
outline-offset: 2px;
|
outline-offset: 2px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.stack-value.is-editing {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: flex-end;
|
||||||
|
gap: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.edit-text {
|
||||||
|
display: inline-block;
|
||||||
|
white-space: pre;
|
||||||
|
}
|
||||||
|
|
||||||
|
.edit-caret {
|
||||||
|
display: inline-block;
|
||||||
|
width: 1px;
|
||||||
|
height: 1em;
|
||||||
|
margin: 0 0.12ch;
|
||||||
|
background: currentColor;
|
||||||
|
animation: caret-blink 1s steps(1, end) infinite;
|
||||||
|
transform: translateY(0.02em);
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes caret-blink {
|
||||||
|
0%, 49% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
50%, 100% {
|
||||||
|
opacity: 0.15;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.key-default {
|
.key-default {
|
||||||
background: linear-gradient(180deg, var(--btnTop), var(--btnBottom));
|
background: linear-gradient(180deg, var(--btnTop), var(--btnBottom));
|
||||||
color: #eef2f7;
|
color: #eef2f7;
|
||||||
|
|||||||
+11
-11
@@ -9,7 +9,6 @@
|
|||||||
<body>
|
<body>
|
||||||
<main class="app-shell">
|
<main class="app-shell">
|
||||||
<section class="calculator" aria-label="HP48GX style RPN calculator">
|
<section class="calculator" aria-label="HP48GX style RPN calculator">
|
||||||
<div>
|
|
||||||
<div class="display-block">
|
<div class="display-block">
|
||||||
<div class="display-panel">
|
<div class="display-panel">
|
||||||
<div class="status-bar" id="statusLine" aria-live="polite"></div>
|
<div class="status-bar" id="statusLine" aria-live="polite"></div>
|
||||||
@@ -36,19 +35,20 @@
|
|||||||
<button id="rightButton" class="display-button"><span class="display-button-symbol arrow-symbol">→</span></button>
|
<button id="rightButton" class="display-button"><span class="display-button-symbol arrow-symbol">→</span></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="keypad-panel">
|
<div class="keypad-scroll-area">
|
||||||
<div class="keypad-grid" id="keypadGrid"></div>
|
<div class="keypad-panel">
|
||||||
</div>
|
<div class="keypad-grid" id="keypadGrid"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="functions-panel">
|
<div class="functions-panel">
|
||||||
<div class="functions-grid" id="functionsGrid"></div>
|
<div class="functions-grid" id="functionsGrid"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="trigo-panel">
|
<div class="trigo-panel">
|
||||||
<div class="trigo-grid" id="trigoGrid"></div>
|
<div class="trigo-grid" id="trigoGrid"></div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<input id="hiddenInput" class="hidden-input" type="text" autocomplete="off" aria-hidden="true" tabindex="-1">
|
<input id="hiddenInput" class="hidden-input" type="text" autocomplete="off" aria-hidden="true" tabindex="-1">
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
+342
-73
@@ -53,28 +53,28 @@ const keypadKeys = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const functionKeys = [
|
const functionKeys = [
|
||||||
{ label: 'x²', action: 'sqr', className: 'key-default' },
|
{ label: 'x²', action: 'sqr', className: 'key-default', title: 's' },
|
||||||
{ label: 'yˣ', action: 'pow', className: 'key-default' },
|
{ label: '√x', action: 'sqrt', className: 'key-default', title: 'r' },
|
||||||
{ label: '1/x', action: 'recip', className: 'key-default' },
|
{ label: '1/x', action: 'recip', className: 'key-default', title: 'x' },
|
||||||
{ label: '%', action: 'mod', className: 'key-default' },
|
{ label: '%', action: 'mod', className: 'key-default' },
|
||||||
{ label: '√x', action: 'sqrt', className: 'key-default' },
|
{ label: 'yˣ', action: 'pow', className: 'key-default', title: 'S' },
|
||||||
{ label: 'y√x', action: 'root', className: 'key-default' },
|
{ label: 'y√x', action: 'root', className: 'key-default', title: 'R' },
|
||||||
{ label: '10ˣ', action: 'pow10', className: 'key-default' },
|
{ label: '10ˣ', action: 'pow10', className: 'key-default', title: 'd' },
|
||||||
{ label: '', spacer: true },
|
{ label: '', spacer: true },
|
||||||
{ label: 'log', action: 'log', className: 'key-default' },
|
{ label: 'log', action: 'log', className: 'key-default', title: 'l / L' },
|
||||||
{ label: 'ln', action: 'ln', className: 'key-default' },
|
{ label: 'ln', action: 'ln', className: 'key-default', title: 'n / N' },
|
||||||
{ label: 'eˣ', action: 'exp', className: 'key-default' },
|
{ label: 'eˣ', action: 'exp', className: 'key-default', title: 'e / E' },
|
||||||
{ label: '', spacer: true },
|
{ label: '', spacer: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
const trigoKeys = [
|
const trigoKeys = [
|
||||||
{ label: 'sin', action: 'sin', className: 'key-default' },
|
{ label: 'sin', action: 'sin', className: 'key-default', title: 'i' },
|
||||||
{ label: 'cos', action: 'cos', className: 'key-default' },
|
{ label: 'cos', action: 'cos', className: 'key-default', title: 'o' },
|
||||||
{ label: 'tan', action: 'tan', className: 'key-default' },
|
{ label: 'tan', action: 'tan', className: 'key-default', title: 'a' },
|
||||||
{ label: '', spacer: true },
|
{ label: '', spacer: true },
|
||||||
{ label: 'asin', action: 'asin', className: 'key-default' },
|
{ label: 'asin', action: 'asin', className: 'key-default', title: 'I' },
|
||||||
{ label: 'acos', action: 'acos', className: 'key-default' },
|
{ label: 'acos', action: 'acos', className: 'key-default', title: 'O' },
|
||||||
{ label: 'atan', action: 'atan', className: 'key-default' },
|
{ label: 'atan', action: 'atan', className: 'key-default', title: 'A' },
|
||||||
{ label: '', spacer: true },
|
{ label: '', spacer: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -94,6 +94,11 @@ function focusInput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let statusTimer = null;
|
let statusTimer = null;
|
||||||
|
let editCursor = 0;
|
||||||
|
let editRestoreValue = null;
|
||||||
|
let stackMode = 'normal';
|
||||||
|
let stackSelection = 0;
|
||||||
|
let stackMoveSnapshot = null;
|
||||||
|
|
||||||
function setStatus(message, isError = false, timeoutMs = 1400) {
|
function setStatus(message, isError = false, timeoutMs = 1400) {
|
||||||
if (!statusLine) return;
|
if (!statusLine) return;
|
||||||
@@ -118,27 +123,41 @@ function clearStatus() {
|
|||||||
statusLine.classList.remove('is-error');
|
statusLine.classList.remove('is-error');
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeStack() {
|
|
||||||
while (calc.stack.length > 4) {
|
|
||||||
calc.stack.shift();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getStackLine(indexFromTop) {
|
function getStackLine(indexFromTop) {
|
||||||
return indexFromTop >= 0 && indexFromTop < calc.stack.length ? calc.stack[indexFromTop] : '';
|
return indexFromTop >= 0 && indexFromTop < calc.stack.length ? calc.stack[indexFromTop] : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStackDisplayValue(label) {
|
function getStackDisplayValue(label) {
|
||||||
if (label === 'X') {
|
if (label === 'X' && calc.isEditing) {
|
||||||
return calc.isEditing ? calc.inputValue : (calc.formatNumber(getStackLine(0)) || '');
|
return calc.inputValue;
|
||||||
}
|
}
|
||||||
if (label === 'Y') {
|
const indexMap = { X: 0, Y: 1, Z: 2, T: 3 };
|
||||||
return calc.isEditing ? (calc.formatNumber(getStackLine(0)) || '') : (calc.formatNumber(getStackLine(1)) || '');
|
const indexFromTop = calc.isEditing ? Math.max(0, indexMap[label] - 1) : indexMap[label];
|
||||||
|
return calc.formatNumber(getStackLine(indexFromTop)) || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getVisibleStackLabel(label) {
|
||||||
|
if (stackMode === 'navigation' || stackMode === 'move') {
|
||||||
|
const indexMap = { X: 0, Y: 1, Z: 2, T: 3 };
|
||||||
|
return String(indexMap[label]);
|
||||||
}
|
}
|
||||||
if (label === 'Z') {
|
return label;
|
||||||
return calc.isEditing ? (calc.formatNumber(getStackLine(1)) || '') : (calc.formatNumber(getStackLine(2)) || '');
|
}
|
||||||
|
|
||||||
|
function updateStackLabels() {
|
||||||
|
const stackLabels = ['T', 'Z', 'Y', 'X'];
|
||||||
|
for (const label of stackLabels) {
|
||||||
|
const stackCell = stackEls[label].parentElement;
|
||||||
|
if (!stackCell) continue;
|
||||||
|
const labelEl = stackCell.querySelector('.stack-label');
|
||||||
|
if (labelEl) {
|
||||||
|
labelEl.textContent = `${getVisibleStackLabel(label)}:`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return calc.isEditing ? (calc.formatNumber(getStackLine(2)) || '') : (calc.formatNumber(getStackLine(3)) || '');
|
}
|
||||||
|
|
||||||
|
function stackModeToLabel(index) {
|
||||||
|
return ['X', 'Y', 'Z', 'T'][Math.max(0, Math.min(3, index))] ?? 'X';
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCopyButtons() {
|
function updateCopyButtons() {
|
||||||
@@ -152,40 +171,191 @@ function updateCopyButtons() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderEditValue() {
|
||||||
|
const cursor = Math.max(0, Math.min(editCursor, calc.inputValue.length));
|
||||||
|
stackEls.X.innerHTML = `<span class="edit-text">${calc.inputValue.slice(0, cursor)}</span><span class="edit-caret" aria-hidden="true"></span><span class="edit-text">${calc.inputValue.slice(cursor)}</span>`;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
stackEls.X.textContent = getStackDisplayValue('X');
|
const stackLabels = ['T', 'Z', 'Y', 'X'];
|
||||||
stackEls.Y.textContent = getStackDisplayValue('Y');
|
for (const label of stackLabels) {
|
||||||
stackEls.Z.textContent = getStackDisplayValue('Z');
|
const isSelected = stackMode !== 'normal' && stackModeToLabel(stackSelection) === label;
|
||||||
stackEls.T.textContent = getStackDisplayValue('T');
|
const value = label === 'X' && calc.isEditing ? '' : getStackDisplayValue(label);
|
||||||
|
stackEls[label].textContent = value;
|
||||||
|
stackEls[label].classList.toggle('is-editing', label === 'X' && calc.isEditing);
|
||||||
|
stackEls[label].classList.toggle('is-caret-visible', label === 'X' && calc.isEditing);
|
||||||
|
stackEls[label].parentElement?.classList.toggle('is-selected', isSelected);
|
||||||
|
stackEls[label].parentElement?.classList.toggle('is-moving', stackMode === 'move' && stackModeToLabel(stackSelection) === label);
|
||||||
|
}
|
||||||
|
if (calc.isEditing) {
|
||||||
|
renderEditValue();
|
||||||
|
}
|
||||||
|
updateStackLabels();
|
||||||
updateCopyButtons();
|
updateCopyButtons();
|
||||||
modeButton.textContent = calc.angleMode;
|
modeButton.textContent = calc.angleMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stopEditing(clearValue = false) {
|
||||||
|
if (clearValue) {
|
||||||
|
calc.inputValue = '';
|
||||||
|
}
|
||||||
|
calc.isEditing = false;
|
||||||
|
editCursor = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancelEditing() {
|
||||||
|
if (editRestoreValue !== null) {
|
||||||
|
calc.push(editRestoreValue);
|
||||||
|
}
|
||||||
|
editRestoreValue = null;
|
||||||
|
stopEditing(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function enterNavigationMode() {
|
||||||
|
if (calc.isEditing) return;
|
||||||
|
stackMode = 'navigation';
|
||||||
|
stackSelection = 0;
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function exitStackMode() {
|
||||||
|
stackMode = 'normal';
|
||||||
|
stackSelection = 0;
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveNavigationSelection(delta) {
|
||||||
|
const maxIndex = Math.max(0, calc.stack.length - 1);
|
||||||
|
stackSelection = Math.max(0, Math.min(maxIndex, stackSelection + delta));
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function beginStackMove() {
|
||||||
|
stackMode = 'move';
|
||||||
|
stackMoveSnapshot = calc.stack.slice();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreStackMoveSnapshot() {
|
||||||
|
if (!stackMoveSnapshot) return;
|
||||||
|
calc.stack = stackMoveSnapshot.slice();
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateStackMove() {
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
stackMode = 'normal';
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveSelectedStackValue(delta) {
|
||||||
|
const index = stackSelection;
|
||||||
|
const target = Math.max(0, Math.min(calc.stack.length - 1, index + delta));
|
||||||
|
if (target === index) return;
|
||||||
|
const value = calc.stack.splice(index, 1)[0];
|
||||||
|
calc.stack.splice(target, 0, value);
|
||||||
|
stackSelection = target;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function enterNavigationMode() {
|
||||||
|
if (calc.isEditing) return;
|
||||||
|
stackMode = 'navigation';
|
||||||
|
stackSelection = 0;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function exitStackMode() {
|
||||||
|
stackMode = 'normal';
|
||||||
|
stackSelection = 0;
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveNavigationSelection(delta) {
|
||||||
|
const maxIndex = Math.max(0, calc.stack.length - 1);
|
||||||
|
stackSelection = Math.max(0, Math.min(maxIndex, stackSelection + delta));
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function beginStackMove() {
|
||||||
|
stackMode = 'move';
|
||||||
|
stackMoveSnapshot = calc.stack.slice();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function restoreStackMoveSnapshot() {
|
||||||
|
if (!stackMoveSnapshot) return;
|
||||||
|
calc.stack = stackMoveSnapshot.slice();
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateStackMove() {
|
||||||
|
stackMoveSnapshot = null;
|
||||||
|
stackMode = 'normal';
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveSelectedStackValue(delta) {
|
||||||
|
const index = stackSelection;
|
||||||
|
const target = Math.max(0, Math.min(calc.stack.length - 1, index + delta));
|
||||||
|
if (target === index) return;
|
||||||
|
const value = calc.stack.splice(index, 1)[0];
|
||||||
|
calc.stack.splice(target, 0, value);
|
||||||
|
stackSelection = target;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveEditCursor(delta) {
|
||||||
|
editCursor = Math.max(0, Math.min(calc.inputValue.length, editCursor + delta));
|
||||||
|
}
|
||||||
|
|
||||||
function pushEditingValueIfNeeded() {
|
function pushEditingValueIfNeeded() {
|
||||||
if (!calc.isEditing) return;
|
if (!calc.isEditing) return;
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
editCursor = calc.inputValue.length;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function inputToX(value) {
|
function inputToX(value) {
|
||||||
if (!calc.isEditing) {
|
if (!calc.isEditing) {
|
||||||
calc.isEditing = true;
|
calc.isEditing = true;
|
||||||
calc.inputValue = '';
|
calc.inputValue = '';
|
||||||
|
editCursor = 0;
|
||||||
}
|
}
|
||||||
if (value === 'Backspace') {
|
if (value === 'Backspace') {
|
||||||
calc.inputValue = calc.inputValue.slice(0, -1);
|
if (editCursor > 0) {
|
||||||
|
calc.inputValue = `${calc.inputValue.slice(0, editCursor - 1)}${calc.inputValue.slice(editCursor)}`;
|
||||||
|
editCursor -= 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
calc.inputValue += value;
|
calc.inputValue = `${calc.inputValue.slice(0, editCursor)}${value}${calc.inputValue.slice(editCursor)}`;
|
||||||
|
editCursor += value.length;
|
||||||
}
|
}
|
||||||
if (calc.inputValue === '') {
|
if (calc.inputValue === '') {
|
||||||
calc.isEditing = false;
|
stopEditing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,11 +387,9 @@ function execute(name) {
|
|||||||
}
|
}
|
||||||
} else if (name === 'clear') {
|
} else if (name === 'clear') {
|
||||||
calc.clear();
|
calc.clear();
|
||||||
calc.inputValue = '';
|
stopEditing(true);
|
||||||
calc.isEditing = false;
|
|
||||||
} else if (name === 'escape') {
|
} else if (name === 'escape') {
|
||||||
calc.inputValue = '';
|
cancelEditing();
|
||||||
calc.isEditing = false;
|
|
||||||
} else if (name === 'backspace') {
|
} else if (name === 'backspace') {
|
||||||
if (calc.isEditing) {
|
if (calc.isEditing) {
|
||||||
inputToX('Backspace');
|
inputToX('Backspace');
|
||||||
@@ -235,35 +403,26 @@ function execute(name) {
|
|||||||
}
|
}
|
||||||
} else if (name === 'neg') {
|
} else if (name === 'neg') {
|
||||||
if (calc.isEditing) {
|
if (calc.isEditing) {
|
||||||
calc.inputValue = calc.inputValue.startsWith('-') ? calc.inputValue.slice(1) : `-${calc.inputValue}`;
|
const hasSign = calc.inputValue.startsWith('-');
|
||||||
|
calc.inputValue = hasSign ? calc.inputValue.slice(1) : `-${calc.inputValue}`;
|
||||||
|
moveEditCursor(hasSign ? -1 : 1);
|
||||||
} else {
|
} else {
|
||||||
calc.push(calc.pop() * -1);
|
calc.command('neg');
|
||||||
}
|
}
|
||||||
} else if (name === 'pow10') {
|
|
||||||
pushEditingValueIfNeeded();
|
|
||||||
const exponent = calc.stack[0];
|
|
||||||
calc.remove(0);
|
|
||||||
calc.push(10);
|
|
||||||
calc.push(exponent);
|
|
||||||
calc.command('pow');
|
|
||||||
} else if (name === 'exp') {
|
|
||||||
pushEditingValueIfNeeded();
|
|
||||||
const exponent = calc.stack[0];
|
|
||||||
calc.remove(0);
|
|
||||||
calc.push(Math.E);
|
|
||||||
calc.push(exponent);
|
|
||||||
calc.command('pow');
|
|
||||||
} else {
|
} else {
|
||||||
pushEditingValueIfNeeded();
|
pushEditingValueIfNeeded();
|
||||||
calc.command(name);
|
calc.command(name);
|
||||||
}
|
}
|
||||||
|
if (!calc.isEditing) {
|
||||||
|
editCursor = 0;
|
||||||
|
}
|
||||||
render();
|
render();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setStatus(error?.message || 'Operation error', true);
|
setStatus(error?.message || 'Operation error', true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createKeyButton({ label, input, action, spacer, className }) {
|
function createKeyButton({ label, input, action, spacer, className, title }) {
|
||||||
if (spacer) {
|
if (spacer) {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
return div;
|
return div;
|
||||||
@@ -272,6 +431,7 @@ function createKeyButton({ label, input, action, spacer, className }) {
|
|||||||
button.type = 'button';
|
button.type = 'button';
|
||||||
button.textContent = label;
|
button.textContent = label;
|
||||||
button.className = className;
|
button.className = className;
|
||||||
|
if (title) button.title = title;
|
||||||
button.addEventListener('click', () => {
|
button.addEventListener('click', () => {
|
||||||
if (input) {
|
if (input) {
|
||||||
inputToX(input);
|
inputToX(input);
|
||||||
@@ -302,6 +462,53 @@ async function copyStackValue(label) {
|
|||||||
function handleKeyboard(event) {
|
function handleKeyboard(event) {
|
||||||
if (event.defaultPrevented) return;
|
if (event.defaultPrevented) return;
|
||||||
const key = event.key;
|
const key = event.key;
|
||||||
|
if (stackMode === 'navigation') {
|
||||||
|
if (key === 'ArrowUp') {
|
||||||
|
event.preventDefault();
|
||||||
|
moveNavigationSelection(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'ArrowDown') {
|
||||||
|
event.preventDefault();
|
||||||
|
moveNavigationSelection(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'Escape' || key === 'ArrowLeft') {
|
||||||
|
event.preventDefault();
|
||||||
|
exitStackMode();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
beginStackMove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (stackMode === 'move') {
|
||||||
|
if (key === 'ArrowUp') {
|
||||||
|
event.preventDefault();
|
||||||
|
moveSelectedStackValue(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'ArrowDown') {
|
||||||
|
event.preventDefault();
|
||||||
|
moveSelectedStackValue(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'Escape') {
|
||||||
|
event.preventDefault();
|
||||||
|
restoreStackMoveSnapshot();
|
||||||
|
exitStackMode();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
validateStackMove();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (/^[0-9.]$/.test(key)) {
|
if (/^[0-9.]$/.test(key)) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
inputToX(key);
|
inputToX(key);
|
||||||
@@ -319,16 +526,58 @@ function handleKeyboard(event) {
|
|||||||
'/': 'div',
|
'/': 'div',
|
||||||
'%': 'mod',
|
'%': 'mod',
|
||||||
'^': 'pow',
|
'^': 'pow',
|
||||||
|
s: 'sqr',
|
||||||
|
S: 'pow',
|
||||||
|
r: 'sqrt',
|
||||||
|
R: 'root',
|
||||||
|
x: 'recip',
|
||||||
|
d: 'pow10',
|
||||||
|
l: 'log',
|
||||||
|
L: 'log',
|
||||||
|
n: 'ln',
|
||||||
|
N: 'ln',
|
||||||
|
e: 'exp',
|
||||||
|
E: 'exp',
|
||||||
|
i: 'sin',
|
||||||
|
o: 'cos',
|
||||||
|
a: 'tan',
|
||||||
|
I: 'asin',
|
||||||
|
O: 'acos',
|
||||||
|
A: 'atan',
|
||||||
};
|
};
|
||||||
const arrowMap = {
|
if (key === 'ArrowLeft') {
|
||||||
ArrowUp: upButton,
|
|
||||||
ArrowDown: downButton,
|
|
||||||
ArrowLeft: leftButton,
|
|
||||||
ArrowRight: rightButton,
|
|
||||||
};
|
|
||||||
if (arrowMap[key]) {
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
arrowMap[key].click();
|
if (calc.isEditing) {
|
||||||
|
moveEditCursor(-1);
|
||||||
|
render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
leftButton.click();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'ArrowRight') {
|
||||||
|
event.preventDefault();
|
||||||
|
if (calc.isEditing) {
|
||||||
|
moveEditCursor(1);
|
||||||
|
render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rightButton.click();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'ArrowUp') {
|
||||||
|
event.preventDefault();
|
||||||
|
upButton.click();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === 'ArrowDown') {
|
||||||
|
event.preventDefault();
|
||||||
|
if (calc.isEditing) {
|
||||||
|
cancelEditing();
|
||||||
|
render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
downButton.click();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (map[key]) {
|
if (map[key]) {
|
||||||
@@ -427,14 +676,19 @@ hiddenInput.addEventListener('paste', (event) => {
|
|||||||
pasteTextIntoStack(text);
|
pasteTextIntoStack(text);
|
||||||
});
|
});
|
||||||
|
|
||||||
upButton.addEventListener('click', () => {});
|
upButton.addEventListener('click', () => {
|
||||||
|
if (!calc.isEditing && stackMode === 'normal') {
|
||||||
|
enterNavigationMode();
|
||||||
|
focusInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const constantLabels = {
|
const constantLabels = {
|
||||||
pi: 'π',
|
pi: 'π',
|
||||||
e: 'e',
|
e: 'e',
|
||||||
phi: 'φ',
|
phi: 'φ',
|
||||||
g: 'g',
|
g: 'g',
|
||||||
c: 'c',
|
c: 'C',
|
||||||
};
|
};
|
||||||
const constantOrder = ['pi', 'e', 'phi', 'g', 'c'];
|
const constantOrder = ['pi', 'e', 'phi', 'g', 'c'];
|
||||||
|
|
||||||
@@ -473,19 +727,34 @@ constButton.addEventListener('click', (event) => {
|
|||||||
toggleConstMenu();
|
toggleConstMenu();
|
||||||
});
|
});
|
||||||
|
|
||||||
leftButton.addEventListener('click', () => {});
|
leftButton.addEventListener('click', () => {
|
||||||
|
if (calc.isEditing) {
|
||||||
|
moveEditCursor(-1);
|
||||||
|
render();
|
||||||
|
focusInput();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (stackMode === 'navigation') {
|
||||||
|
exitStackMode();
|
||||||
|
focusInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
downButton.addEventListener('click', () => {
|
downButton.addEventListener('click', () => {
|
||||||
if (!calc.isEditing && calc.isValidIndex(0)) {
|
if (!calc.isEditing && startEditingFromStackTop()) {
|
||||||
const value = calc.stack[0];
|
|
||||||
calc.remove(0);
|
|
||||||
calc.isEditing = true;
|
|
||||||
calc.inputValue = calc.formatNumber(value);
|
|
||||||
render();
|
render();
|
||||||
focusInput();
|
focusInput();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rightButton.addEventListener('click', () => {
|
rightButton.addEventListener('click', () => {
|
||||||
|
if (calc.isEditing) {
|
||||||
|
moveEditCursor(1);
|
||||||
|
render();
|
||||||
|
focusInput();
|
||||||
|
return;
|
||||||
|
}
|
||||||
execute('swap');
|
execute('swap');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,11 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
┌──────────── Keypad ─────────────┐
|
┌──────────── Keypad ─────────────┐
|
||||||
| +/- | Clear | Esc | backspace |
|
| Clear | Backspace | Esc | Enter |
|
||||||
| 7 | 8 | 9 | / |
|
| 7 | 8 | 9 | / |
|
||||||
| 4 | 5 | 6 | * |
|
| 4 | 5 | 6 | * |
|
||||||
| 1 | 2 | 3 | - |
|
| 1 | 2 | 3 | - |
|
||||||
| 0 | . | Enter | + |
|
| 0 | . | +/- | + |
|
||||||
└─────────────────────────────────┘
|
└─────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -36,9 +36,9 @@
|
|||||||
|
|
||||||
```
|
```
|
||||||
┌──────────── Functions ──────────┐
|
┌──────────── Functions ──────────┐
|
||||||
| x^2 | y^x | 1/x | % |
|
| x^2 | √x | 1/x | % |
|
||||||
| √x | y√x | 10^x | |
|
| y^x | y√x | 10^x | |
|
||||||
| log | ln | | |
|
| log | ln | e^x | |
|
||||||
└─────────────────────────────────┘
|
└─────────────────────────────────┘
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+13
-1
@@ -49,6 +49,18 @@ class RpnCalculator {
|
|||||||
aliases: ['^', 'y^x'],
|
aliases: ['^', 'y^x'],
|
||||||
execute: (calc, a, b) => Math.pow(a, b),
|
execute: (calc, a, b) => Math.pow(a, b),
|
||||||
},
|
},
|
||||||
|
pow10: {
|
||||||
|
category: 'Arithmetic',
|
||||||
|
argCount: 1,
|
||||||
|
aliases: ['10^x'],
|
||||||
|
execute: (calc, a) => Math.pow(10, a),
|
||||||
|
},
|
||||||
|
exp: {
|
||||||
|
category: 'Arithmetic',
|
||||||
|
argCount: 1,
|
||||||
|
aliases: ['e^x'],
|
||||||
|
execute: (calc, a) => Math.exp(a),
|
||||||
|
},
|
||||||
root: {
|
root: {
|
||||||
category: 'Arithmetic',
|
category: 'Arithmetic',
|
||||||
argCount: 2,
|
argCount: 2,
|
||||||
@@ -195,7 +207,7 @@ class RpnCalculator {
|
|||||||
static getOperationsByCategory() {
|
static getOperationsByCategory() {
|
||||||
return {
|
return {
|
||||||
Stack: ['dup', 'drop', 'swap', 'clear', 'enter'],
|
Stack: ['dup', 'drop', 'swap', 'clear', 'enter'],
|
||||||
Arithmetic: ['add', 'sub', 'mul', 'div', 'mod', 'pow', 'root', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
Arithmetic: ['add', 'sub', 'mul', 'div', 'mod', 'pow', 'pow10', 'exp', 'root', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
||||||
Trigonometry: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan'],
|
Trigonometry: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan'],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user