Compare commits
50 Commits
324f203d23
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a24142cd72 | |||
| 4df99d0738 | |||
| 1cef2d2d6a | |||
| 442a97d573 | |||
| 44ce67c5cc | |||
| a7411243de | |||
| f8de4e1709 | |||
| 003d4fde1b | |||
| e5f50aee0a | |||
| 6a7a60a9bc | |||
| a37ed59b40 | |||
| c09fdc7e0f | |||
| 849170ea49 | |||
| 0100da80bb | |||
| e1fd730db5 | |||
| ba53f51bf7 | |||
| 534bbc0afb | |||
| 2504716c64 | |||
| 6c8c032f7a | |||
| d8d0556822 | |||
| 426fd326a5 | |||
| 5364208491 | |||
| 1e703bebe8 | |||
| 54797f9dd9 | |||
| 75bf6d69df | |||
| 256e9f2b33 | |||
| 77fb671dcf | |||
| ae11cb8007 | |||
| ba7fc8b4d6 | |||
| b45cfe8091 | |||
| cb45efff43 | |||
| 4e8155b5f0 | |||
| 432523c23f | |||
| 9cbddfa0c2 | |||
| db3bee6e89 | |||
| 62221a9baa | |||
| c47c46ad64 | |||
| 80bcdac320 | |||
| 39659745a6 | |||
| 75fe72412e | |||
| ef0e0c8dd2 | |||
| 6444357444 | |||
| d1a1d44577 | |||
| 02b3b280f8 | |||
| 40e1043a03 | |||
| f679b0d952 | |||
| 2505a102df | |||
| 9bca077347 | |||
| d88722030a | |||
| 48a262eb87 |
+8
-6
@@ -1,6 +1,8 @@
|
|||||||
# Project memory
|
# State
|
||||||
|
- Core engine: `src/rpn-calculator.js`
|
||||||
- RPN calculator JS project.
|
- Reference demo: `samples/calc-02/` (portrait-first HP48GX layout, compact mode/constants popups; Const button comes before Mode in the display row)
|
||||||
- Read `.memory/state.md` for current state.
|
- Important UI behavior: mode button shows the current angle mode; keyboard focus stays on the hidden input on desktop; clipboard paste is supported
|
||||||
- Keep names and commands in English.
|
- Public API: `push`, `pop`, `clear`, `swap`, `remove`, `edit`, `isValidIndex`, `input`, `command`, `getOperationsByCategory`, `getConstants`, `listConstants`, `setConstant`, `removeConstant`, `hasConstant`
|
||||||
- Update memory files based on events: engine, demo, API, commands, exports, docs, or tasks.
|
- 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
|
||||||
|
- Exports: browser `window.RpnCalculator`, CommonJS `module.exports`
|
||||||
|
|||||||
+4
-3
@@ -1,7 +1,8 @@
|
|||||||
# State
|
# State
|
||||||
- Core engine: `src/rpn-calculator.js`
|
- Core engine: `src/rpn-calculator.js`
|
||||||
- Active demo: `samples/dev/` (HP48-style UI)
|
- Default demo: `samples/dev/` classic browser demo; `samples/calc-02/` remains the portrait-first HP48GX-inspired reference
|
||||||
- Public API: `push`, `pop`, `clear`, `swap`, `remove`, `edit`, `isValidIndex`, `input`, `command`, `getOperationsByCategory`, `getConstants`
|
- 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`
|
- Commands: arithmetic, stack, trigonometry, constants `pi`, `e`, `phi`, `g`, and `c`; arithmetic includes `root` with `y√x`, `yroot`, and `nroot` aliases; constants can be added or removed dynamically through the core API
|
||||||
|
- Demo behavior: `samples/dev/` keeps hidden input focus on desktop and supports clipboard paste; `samples/calc-02/` uses compact mode/constants popups, a 4-column display-adjacent row, stack selection/move mode, and paste through the hidden input
|
||||||
- Exports: browser `window.RpnCalculator`, CommonJS `module.exports`
|
- Exports: browser `window.RpnCalculator`, CommonJS `module.exports`
|
||||||
|
|||||||
@@ -1,30 +1,28 @@
|
|||||||
# RPN Virtual Calculator
|
# RPN Virtual Calculator
|
||||||
|
|
||||||
A browser-friendly RPN calculator built around a small, generic JavaScript API.
|
A browser-friendly RPN calculator built around a small JavaScript API.
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
This project provides:
|
This project provides:
|
||||||
|
|
||||||
- a reusable JavaScript RPN engine in `src/rpn-calculator.js`
|
- a reusable JavaScript RPN engine in `src/rpn-calculator.js`
|
||||||
- a browser demo in `samples/dev/`
|
- a classic browser demo in `samples/dev/`
|
||||||
- a command system centralized in a single operation dictionary
|
- a portrait-first HP48GX-inspired demo in `samples/calc-02/`
|
||||||
- a small public API focused on stack operations and generic command dispatch
|
- a centralized command system with aliases
|
||||||
|
- a compact public API focused on stack operations, editing, and command dispatch
|
||||||
|
|
||||||
The main class is `RpnCalculator`.
|
The main class is `RpnCalculator`.
|
||||||
|
|
||||||
## Project structure
|
## Project structure
|
||||||
|
|
||||||
- `src/rpn-calculator.js`: calculator engine
|
- `src/rpn-calculator.js`: calculator engine
|
||||||
- `samples/dev/index.html`: active browser demo entry point
|
- `samples/dev/index.html`: browser demo entry point
|
||||||
- `samples/dev/index.css`: demo styles
|
- `samples/dev/index.css`: demo styles
|
||||||
- `samples/dev/index.js`: demo UI and keyboard logic
|
- `samples/dev/index.js`: demo UI and keyboard logic
|
||||||
- `samples/calc-01/index.html`: active browser demo entry point
|
- `samples/calc-02/index.html`: portrait-first HP48GX-style demo entry point
|
||||||
- `samples/calc-01/index.css`: demo styles
|
- `samples/calc-02/index.css`: portrait-first demo styles
|
||||||
- `samples/calc-01/index.js`: demo UI and keyboard logic
|
- `samples/calc-02/index.js`: portrait-first HP48GX-style demo UI and keyboard logic
|
||||||
- `samples/calc-01/index.html`: alternate browser demo entry point
|
|
||||||
- `samples/calc-01/index.css`: alternate demo styles
|
|
||||||
- `samples/calc-01/index.js`: alternate demo UI and keyboard logic
|
|
||||||
- `samples/calc-XX/`: placeholder name for future demo variants
|
- `samples/calc-XX/`: placeholder name for future demo variants
|
||||||
|
|
||||||
## Public API
|
## Public API
|
||||||
@@ -45,6 +43,10 @@ Instance helpers also available:
|
|||||||
|
|
||||||
- `getOperationsByCategory()`
|
- `getOperationsByCategory()`
|
||||||
- `getConstants()`
|
- `getConstants()`
|
||||||
|
- `listConstants()`
|
||||||
|
- `setConstant(name, value)`
|
||||||
|
- `removeConstant(name)`
|
||||||
|
- `hasConstant(name)`
|
||||||
|
|
||||||
Static helpers also available:
|
Static helpers also available:
|
||||||
|
|
||||||
@@ -84,6 +86,10 @@ Available constants:
|
|||||||
|
|
||||||
- `pi`
|
- `pi`
|
||||||
- `e`
|
- `e`
|
||||||
|
- `phi`
|
||||||
|
- `g`
|
||||||
|
- `c`
|
||||||
|
- plus any user-defined constants added through the engine API
|
||||||
|
|
||||||
They can be used through `command(...)`:
|
They can be used through `command(...)`:
|
||||||
|
|
||||||
@@ -110,6 +116,7 @@ calc.command('e');
|
|||||||
- `div` alias: `/`
|
- `div` alias: `/`
|
||||||
- `mod` alias: `%`
|
- `mod` alias: `%`
|
||||||
- `pow` aliases: `^`, `y^x`
|
- `pow` aliases: `^`, `y^x`
|
||||||
|
- `root` aliases: `y√x`, `yroot`, `nroot`
|
||||||
- `sqr`
|
- `sqr`
|
||||||
- `neg`
|
- `neg`
|
||||||
- `sqrt` alias: `sqrt(x)`
|
- `sqrt` alias: `sqrt(x)`
|
||||||
@@ -134,6 +141,7 @@ calc.command('e');
|
|||||||
- `sqrt` accepts alias `sqrt(x)`
|
- `sqrt` accepts alias `sqrt(x)`
|
||||||
- `recip` accepts alias `1/x`
|
- `recip` accepts alias `1/x`
|
||||||
- `div` and `recip` throw `Division by zero` when needed
|
- `div` and `recip` throw `Division by zero` when needed
|
||||||
|
- `root` computes the y-th root as `x^(1/y)` and throws `Invalid input for root` for invalid inputs
|
||||||
- `sqrt` throws `Invalid input for sqrt` for negative values
|
- `sqrt` throws `Invalid input for sqrt` for negative values
|
||||||
- `asin` and `acos` throw explicit domain errors outside `[-1, 1]`
|
- `asin` and `acos` throw explicit domain errors outside `[-1, 1]`
|
||||||
- `log` throws `Invalid input for log` for values `<= 0`
|
- `log` throws `Invalid input for log` for values `<= 0`
|
||||||
@@ -232,7 +240,7 @@ console.log(calc.pop()); // 0.5
|
|||||||
|
|
||||||
## Demo
|
## Demo
|
||||||
|
|
||||||
The active demo lives in `samples/dev/`.
|
The default demo lives in `samples/dev/`.
|
||||||
|
|
||||||
Main UI features:
|
Main UI features:
|
||||||
|
|
||||||
@@ -242,7 +250,12 @@ Main UI features:
|
|||||||
- angle mode selector for `deg`, `rad`, and `grad`
|
- angle mode selector for `deg`, `rad`, and `grad`
|
||||||
- status pills for `inputValue` and `isEditing`
|
- status pills for `inputValue` and `isEditing`
|
||||||
- grouped panels for `Stack`, `Arithmetic`, `Trigonometry`, and `Constants`
|
- grouped panels for `Stack`, `Arithmetic`, `Trigonometry`, and `Constants`
|
||||||
- error display area
|
- keyboard-friendly hidden input on desktop
|
||||||
|
|
||||||
|
## Calc 02 demo
|
||||||
|
|
||||||
|
`samples/calc-02/` is a portrait-first HP48GX-inspired demo.
|
||||||
|
It keeps the display-adjacent button row aligned in four columns, uses compact popup menus for mode and constants, and supports clipboard paste plus the `y√x` root operation.
|
||||||
|
|
||||||
The demo loads the engine from:
|
The demo loads the engine from:
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
PORT="${1:-8000}"
|
PORT="${1:-3000}"
|
||||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
cd "$ROOT_DIR"
|
cd "$ROOT_DIR"
|
||||||
|
|||||||
@@ -0,0 +1,426 @@
|
|||||||
|
|
||||||
|
:root {
|
||||||
|
--bg0: #10151e;
|
||||||
|
--bg1: #1b2432;
|
||||||
|
--panel: #2c3442;
|
||||||
|
--panel2: #394354;
|
||||||
|
--edge: #0c1118;
|
||||||
|
--display: #cfe0ae;
|
||||||
|
--display2: #b9cd8a;
|
||||||
|
--displayText: #1f2a12;
|
||||||
|
--buttonText: #f4f7fb;
|
||||||
|
--shadow: rgba(0, 0, 0, 0.35);
|
||||||
|
--btnTop: #444c58;
|
||||||
|
--btnBottom: #2f3640;
|
||||||
|
--btnAccentTop: #3f526b;
|
||||||
|
--btnAccentBottom: #2b394c;
|
||||||
|
--btnAltTop: #525c69;
|
||||||
|
--btnAltBottom: #3a434f;
|
||||||
|
--btnDangerTop: #584042;
|
||||||
|
--btnDangerBottom: #402d2f;
|
||||||
|
--btnEscapeTop: #6a4a2a;
|
||||||
|
--btnEscapeBottom: #4a331d;
|
||||||
|
--btnEnterTop: #4f7f4d;
|
||||||
|
--btnEnterBottom: #355a34;
|
||||||
|
--btnText: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
margin: 0;
|
||||||
|
min-height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
min-height: 100vh;
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
color: var(--buttonText);
|
||||||
|
background: var(--bg0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-shell {
|
||||||
|
min-height: 100vh;
|
||||||
|
display: grid;
|
||||||
|
align-items: start;
|
||||||
|
justify-items: center;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.calculator {
|
||||||
|
width: 100%;
|
||||||
|
height: auto;
|
||||||
|
display: grid;
|
||||||
|
padding: 8px;
|
||||||
|
border-radius: 8px;
|
||||||
|
background: var(--panel);
|
||||||
|
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
grid-template-rows: auto auto auto auto auto;
|
||||||
|
row-gap: 6px;
|
||||||
|
grid-template-areas:
|
||||||
|
"display"
|
||||||
|
"display-buttons"
|
||||||
|
"keypad"
|
||||||
|
"functions"
|
||||||
|
"trigo";
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-block,
|
||||||
|
.display-panel,
|
||||||
|
.keypad-panel,
|
||||||
|
.functions-panel,
|
||||||
|
.trigo-panel {
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.06);
|
||||||
|
background: rgba(6, 10, 16, 0.16);
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-buttons-panel {
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.06);
|
||||||
|
background: rgba(6, 10, 16, 0.16);
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-block {
|
||||||
|
grid-area: display;
|
||||||
|
display: grid;
|
||||||
|
gap: 0;
|
||||||
|
align-self: start;
|
||||||
|
justify-items: stretch;
|
||||||
|
grid-template-rows: auto auto;
|
||||||
|
row-gap: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-panel {
|
||||||
|
position: relative;
|
||||||
|
width: 100%;
|
||||||
|
padding: 16px;
|
||||||
|
padding-bottom: 8px;
|
||||||
|
background: var(--display);
|
||||||
|
color: var(--displayText);
|
||||||
|
font-family: "Courier New", monospace;
|
||||||
|
overflow: hidden;
|
||||||
|
box-sizing: border-box;
|
||||||
|
height: 138px;
|
||||||
|
min-height: 138px;
|
||||||
|
max-height: 138px;
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
||||||
|
.display-grid {
|
||||||
|
height: 100%;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr;
|
||||||
|
grid-template-rows: repeat(4, minmax(0, auto));
|
||||||
|
align-content: start;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-cell {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 2.2ch auto minmax(0, 1fr);
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
font-size: 20px;
|
||||||
|
line-height: 1;
|
||||||
|
min-height: 0;
|
||||||
|
padding-block: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-label {
|
||||||
|
text-align: right;
|
||||||
|
opacity: 0.78;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-value {
|
||||||
|
min-height: 0;
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
text-align: right;
|
||||||
|
justify-self: end;
|
||||||
|
font-size: 20px;
|
||||||
|
}
|
||||||
|
.display-buttons-panel {
|
||||||
|
grid-area: display-buttons;
|
||||||
|
padding: 8px;
|
||||||
|
align-self: start;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-buttons-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
|
gap: 8px;
|
||||||
|
align-content: start;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-button-spacer {
|
||||||
|
pointer-events: none;
|
||||||
|
visibility: hidden;
|
||||||
|
background: transparent;
|
||||||
|
border-color: transparent;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-button {
|
||||||
|
background: linear-gradient(180deg, var(--btnTop), var(--btnBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-button-symbol {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-width: 1.2em;
|
||||||
|
font-size: 1.05em;
|
||||||
|
line-height: 1;
|
||||||
|
font-weight: 800;
|
||||||
|
transform: translateY(-0.01em);
|
||||||
|
}
|
||||||
|
|
||||||
|
.paste-symbol {
|
||||||
|
font-size: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.display-buttons-grid > button {
|
||||||
|
padding: 6px 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-popup {
|
||||||
|
position: fixed;
|
||||||
|
z-index: 20;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
padding: 6px;
|
||||||
|
border-radius: 6px;
|
||||||
|
background: rgba(16, 21, 30, 0.96);
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.12);
|
||||||
|
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.32);
|
||||||
|
backdrop-filter: blur(2px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-popup-item {
|
||||||
|
width: 100%;
|
||||||
|
min-width: 0;
|
||||||
|
padding: 6px 10px;
|
||||||
|
border-radius: 5px;
|
||||||
|
background: linear-gradient(180deg, var(--btnAltTop), var(--btnAltBottom));
|
||||||
|
text-align: left;
|
||||||
|
font-weight: 700;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-popup-item.is-active {
|
||||||
|
outline: 1px solid rgba(207, 224, 174, 0.7);
|
||||||
|
outline-offset: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key-escape {
|
||||||
|
background: linear-gradient(180deg, var(--btnEscapeTop), var(--btnEscapeBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.keypad-panel,
|
||||||
|
.functions-panel,
|
||||||
|
.trigo-panel {
|
||||||
|
padding: 8px;
|
||||||
|
align-self: start;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-bar {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
z-index: 2;
|
||||||
|
padding: 8px 12px;
|
||||||
|
min-height: 30px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 700;
|
||||||
|
letter-spacing: 0.02em;
|
||||||
|
color: rgba(31, 42, 18, 0.96);
|
||||||
|
background: rgba(255, 246, 170, 0.92);
|
||||||
|
border-bottom: 1px solid rgba(31, 42, 18, 0.2);
|
||||||
|
transform: translateY(-100%);
|
||||||
|
opacity: 0;
|
||||||
|
transition: transform 180ms ease, opacity 180ms ease;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-bar.is-visible {
|
||||||
|
transform: translateY(0);
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-bar.is-error {
|
||||||
|
color: #fff;
|
||||||
|
background: rgba(72, 14, 14, 0.98);
|
||||||
|
border-bottom-color: rgba(255, 255, 255, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.keypad-grid,
|
||||||
|
.functions-grid,
|
||||||
|
.trigo-grid {
|
||||||
|
display: grid;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.keypad-grid {
|
||||||
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
|
grid-template-rows: repeat(5, minmax(0, 1.7fr));
|
||||||
|
}
|
||||||
|
|
||||||
|
.functions-grid,
|
||||||
|
.trigo-grid {
|
||||||
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
|
grid-template-rows: repeat(2, minmax(0, 1fr));
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
border: 1px solid rgba(14, 18, 25, 0.85);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 8px 8px;
|
||||||
|
|
||||||
|
font: inherit;
|
||||||
|
font-weight: 700;
|
||||||
|
color: var(--btnText);
|
||||||
|
text-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
|
||||||
|
cursor: pointer;
|
||||||
|
box-shadow: none;
|
||||||
|
transition: filter 120ms ease, opacity 120ms ease;
|
||||||
|
line-height: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
filter: brightness(1.06);
|
||||||
|
}
|
||||||
|
|
||||||
|
button:active {
|
||||||
|
transform: none;
|
||||||
|
box-shadow: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button:active {
|
||||||
|
animation: stack-copy-flash 140ms ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes stack-copy-flash {
|
||||||
|
0% {
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
50% {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
100% {
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.stack-copy-button {
|
||||||
|
padding: 4px;
|
||||||
|
min-width: 24px;
|
||||||
|
width: 24px;
|
||||||
|
height: 24px;
|
||||||
|
display: inline-grid;
|
||||||
|
place-items: center;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
transform: scale(1);
|
||||||
|
background: transparent;
|
||||||
|
border: none;
|
||||||
|
box-shadow: none;
|
||||||
|
color: rgba(31, 42, 18, 0.58);
|
||||||
|
margin-left: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-cell:last-child {
|
||||||
|
margin-bottom: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button svg {
|
||||||
|
width: 15px;
|
||||||
|
height: 15px;
|
||||||
|
fill: #0a0a0a;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button.is-visible {
|
||||||
|
opacity: 0.7;
|
||||||
|
pointer-events: auto;
|
||||||
|
transform: scale(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button:hover {
|
||||||
|
opacity: 1;
|
||||||
|
filter: none;
|
||||||
|
color: rgba(31, 42, 18, 0.85);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button:active {
|
||||||
|
transform: scale(1);
|
||||||
|
color: rgba(31, 42, 18, 0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stack-copy-button:focus-visible {
|
||||||
|
outline: 1px solid rgba(31, 42, 18, 0.35);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key-default {
|
||||||
|
background: linear-gradient(180deg, var(--btnTop), var(--btnBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.keypad-grid > button {
|
||||||
|
min-height: 1.7em;
|
||||||
|
}
|
||||||
|
|
||||||
|
#keypadGrid > button {
|
||||||
|
min-height: calc(1.7em * 1.7);
|
||||||
|
padding-top: calc(8px * 1.7);
|
||||||
|
padding-bottom: calc(8px * 1.7);
|
||||||
|
}
|
||||||
|
|
||||||
|
#keypadGrid > button.key-default,
|
||||||
|
#keypadGrid > button.key-accent,
|
||||||
|
#keypadGrid > button.key-danger,
|
||||||
|
#keypadGrid > button.key-enter {
|
||||||
|
min-height: calc(1.7em * 1.7);
|
||||||
|
}
|
||||||
|
|
||||||
|
.key-accent {
|
||||||
|
background: linear-gradient(180deg, var(--btnAccentTop), var(--btnAccentBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key-danger {
|
||||||
|
background: linear-gradient(180deg, var(--btnDangerTop), var(--btnDangerBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.key-enter {
|
||||||
|
background: linear-gradient(180deg, var(--btnEnterTop), var(--btnEnterBottom));
|
||||||
|
color: #eef2f7;
|
||||||
|
padding-inline: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hidden-input {
|
||||||
|
position: absolute;
|
||||||
|
left: -9999px;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>HP48GX RPN Calculator</title>
|
||||||
|
<link rel="stylesheet" href="./index.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main class="app-shell">
|
||||||
|
<section class="calculator calculator-portrait" aria-label="HP48GX style RPN calculator">
|
||||||
|
<div class="display-block">
|
||||||
|
<div class="display-panel">
|
||||||
|
<div class="status-bar" id="statusLine" aria-live="polite"></div>
|
||||||
|
<div class="display-frame">
|
||||||
|
<div class="display-grid">
|
||||||
|
<div class="stack-cell"><span class="stack-label">T:</span><button type="button" class="stack-copy-button" data-copy-stack="T" aria-label="Copy T value"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M15.5 4h-7A2.5 2.5 0 0 0 6 6.5V8H5.5A1.5 1.5 0 0 0 4 9.5v8A2.5 2.5 0 0 0 6.5 20H13a1 1 0 0 0 1-1v-1.5H17.5A2.5 2.5 0 0 0 20 15V6.5A2.5 2.5 0 0 0 17.5 4h-2Zm.5 2h1.5a.5.5 0 0 1 .5.5V14a.5.5 0 0 1-.5.5H13V6.5A2.5 2.5 0 0 0 15.5 6Zm-1.5 12H6.5a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h7.5V18Zm-1.5-8h-4V8.5A.5.5 0 0 1 9 8h4a.5.5 0 0 1 .5.5V10Z"/></svg></button><span id="stackT" class="stack-value"></span></div>
|
||||||
|
<div class="stack-cell"><span class="stack-label">Z:</span><button type="button" class="stack-copy-button" data-copy-stack="Z" aria-label="Copy Z value"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M15.5 4h-7A2.5 2.5 0 0 0 6 6.5V8H5.5A1.5 1.5 0 0 0 4 9.5v8A2.5 2.5 0 0 0 6.5 20H13a1 1 0 0 0 1-1v-1.5H17.5A2.5 2.5 0 0 0 20 15V6.5A2.5 2.5 0 0 0 17.5 4h-2Zm.5 2h1.5a.5.5 0 0 1 .5.5V14a.5.5 0 0 1-.5.5H13V6.5A2.5 2.5 0 0 0 15.5 6Zm-1.5 12H6.5a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h7.5V18Zm-1.5-8h-4V8.5A.5.5 0 0 1 9 8h4a.5.5 0 0 1 .5.5V10Z"/></svg></button><span id="stackZ" class="stack-value"></span></div>
|
||||||
|
<div class="stack-cell"><span class="stack-label">Y:</span><button type="button" class="stack-copy-button" data-copy-stack="Y" aria-label="Copy Y value"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M15.5 4h-7A2.5 2.5 0 0 0 6 6.5V8H5.5A1.5 1.5 0 0 0 4 9.5v8A2.5 2.5 0 0 0 6.5 20H13a1 1 0 0 0 1-1v-1.5H17.5A2.5 2.5 0 0 0 20 15V6.5A2.5 2.5 0 0 0 17.5 4h-2Zm.5 2h1.5a.5.5 0 0 1 .5.5V14a.5.5 0 0 1-.5.5H13V6.5A2.5 2.5 0 0 0 15.5 6Zm-1.5 12H6.5a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h7.5V18Zm-1.5-8h-4V8.5A.5.5 0 0 1 9 8h4a.5.5 0 0 1 .5.5V10Z"/></svg></button><span id="stackY" class="stack-value"></span></div>
|
||||||
|
<div class="stack-cell"><span class="stack-label">X:</span><button type="button" class="stack-copy-button" data-copy-stack="X" aria-label="Copy X value"><svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M15.5 4h-7A2.5 2.5 0 0 0 6 6.5V8H5.5A1.5 1.5 0 0 0 4 9.5v8A2.5 2.5 0 0 0 6.5 20H13a1 1 0 0 0 1-1v-1.5H17.5A2.5 2.5 0 0 0 20 15V6.5A2.5 2.5 0 0 0 17.5 4h-2Zm.5 2h1.5a.5.5 0 0 1 .5.5V14a.5.5 0 0 1-.5.5H13V6.5A2.5 2.5 0 0 0 15.5 6Zm-1.5 12H6.5a.5.5 0 0 1-.5-.5v-8a.5.5 0 0 1 .5-.5h7.5V18Zm-1.5-8h-4V8.5A.5.5 0 0 1 9 8h4a.5.5 0 0 1 .5.5V10Z"/></svg></button><span id="stackX" class="stack-value"></span></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="display-buttons-panel">
|
||||||
|
<div class="display-buttons-grid">
|
||||||
|
<button id="constButton" class="display-button">π</button>
|
||||||
|
<button id="pasteButton" class="display-button"><span class="display-button-symbol paste-symbol">⎘</span></button>
|
||||||
|
<button id="upButton" class="display-button"><span class="display-button-symbol arrow-symbol">↑</span></button>
|
||||||
|
<button id="modeButton" class="display-button">Mode</button>
|
||||||
|
<div class="display-button-spacer"></div>
|
||||||
|
<button id="leftButton" class="display-button display-button-offset"><span class="display-button-symbol arrow-symbol">←</span></button>
|
||||||
|
<button id="downButton" 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 class="keypad-panel">
|
||||||
|
<div class="keypad-grid" id="keypadGrid"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="functions-panel">
|
||||||
|
<div class="functions-grid" id="functionsGrid"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="trigo-panel">
|
||||||
|
<div class="trigo-grid" id="trigoGrid"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<input id="hiddenInput" class="hidden-input" type="text" autocomplete="off" aria-hidden="true" tabindex="-1">
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<script src="../../src/rpn-calculator.js"></script>
|
||||||
|
<script src="./index.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,521 @@
|
|||||||
|
const calc = new RpnCalculator({ angleMode: 'deg' });
|
||||||
|
|
||||||
|
const hiddenInput = document.getElementById('hiddenInput');
|
||||||
|
const modeButton = document.getElementById('modeButton');
|
||||||
|
const pasteButton = document.getElementById('pasteButton');
|
||||||
|
const upButton = document.getElementById('upButton');
|
||||||
|
const constButton = document.getElementById('constButton');
|
||||||
|
const leftButton = document.getElementById('leftButton');
|
||||||
|
const downButton = document.getElementById('downButton');
|
||||||
|
const rightButton = document.getElementById('rightButton');
|
||||||
|
|
||||||
|
const stackEls = {
|
||||||
|
T: document.getElementById('stackT'),
|
||||||
|
Z: document.getElementById('stackZ'),
|
||||||
|
Y: document.getElementById('stackY'),
|
||||||
|
X: document.getElementById('stackX'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const stackCopyButtons = {
|
||||||
|
T: document.querySelector('[data-copy-stack="T"]'),
|
||||||
|
Z: document.querySelector('[data-copy-stack="Z"]'),
|
||||||
|
Y: document.querySelector('[data-copy-stack="Y"]'),
|
||||||
|
X: document.querySelector('[data-copy-stack="X"]'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const keypadGrid = document.getElementById('keypadGrid');
|
||||||
|
const functionsGrid = document.getElementById('functionsGrid');
|
||||||
|
const trigoGrid = document.getElementById('trigoGrid');
|
||||||
|
const calculatorEl = document.querySelector('.calculator');
|
||||||
|
const statusLine = document.getElementById('statusLine');
|
||||||
|
|
||||||
|
const keypadKeys = [
|
||||||
|
{ label: 'ENTER', action: 'enter', className: 'key-enter' },
|
||||||
|
{ label: '⎋', action: 'escape', className: 'key-escape' },
|
||||||
|
{ label: 'C', action: 'clear', className: 'key-danger' },
|
||||||
|
{ label: '⌫', action: 'backspace', className: 'key-danger' },
|
||||||
|
{ label: '7', input: '7', className: 'key-default' },
|
||||||
|
{ label: '8', input: '8', className: 'key-default' },
|
||||||
|
{ label: '9', input: '9', className: 'key-default' },
|
||||||
|
{ label: '÷', action: 'div', className: 'key-accent' },
|
||||||
|
{ label: '4', input: '4', className: 'key-default' },
|
||||||
|
{ label: '5', input: '5', className: 'key-default' },
|
||||||
|
{ label: '6', input: '6', className: 'key-default' },
|
||||||
|
{ label: '×', action: 'mul', className: 'key-accent' },
|
||||||
|
{ label: '1', input: '1', className: 'key-default' },
|
||||||
|
{ label: '2', input: '2', className: 'key-default' },
|
||||||
|
{ label: '3', input: '3', className: 'key-default' },
|
||||||
|
{ label: '−', action: 'sub', className: 'key-accent' },
|
||||||
|
{ label: '0', input: '0', className: 'key-default' },
|
||||||
|
{ label: '.', input: '.', className: 'key-default' },
|
||||||
|
{ label: '±', action: 'neg', className: 'key-default' },
|
||||||
|
{ label: '+', action: 'add', className: 'key-accent' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const functionKeys = [
|
||||||
|
{ label: 'x²', action: 'sqr', className: 'key-default' },
|
||||||
|
{ label: 'yˣ', action: 'pow', className: 'key-default' },
|
||||||
|
{ label: '1/x', action: 'recip', className: 'key-default' },
|
||||||
|
{ label: '%', action: 'mod', className: 'key-default' },
|
||||||
|
{ label: '√x', action: 'sqrt', className: 'key-default' },
|
||||||
|
{ label: 'y√x', action: 'root', className: 'key-default' },
|
||||||
|
{ label: '10ˣ', action: 'pow10', className: 'key-default' },
|
||||||
|
{ label: '', spacer: true },
|
||||||
|
{ label: 'log', action: 'log', className: 'key-default' },
|
||||||
|
{ label: 'ln', action: 'ln', className: 'key-default' },
|
||||||
|
{ label: 'eˣ', action: 'exp', className: 'key-default' },
|
||||||
|
{ label: '', spacer: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
const trigoKeys = [
|
||||||
|
{ label: 'sin', action: 'sin', className: 'key-default' },
|
||||||
|
{ label: 'cos', action: 'cos', className: 'key-default' },
|
||||||
|
{ label: 'tan', action: 'tan', className: 'key-default' },
|
||||||
|
{ label: '', spacer: true },
|
||||||
|
{ label: 'asin', action: 'asin', className: 'key-default' },
|
||||||
|
{ label: 'acos', action: 'acos', className: 'key-default' },
|
||||||
|
{ label: 'atan', action: 'atan', className: 'key-default' },
|
||||||
|
{ label: '', spacer: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
const isTouchDevice = window.matchMedia('(pointer: coarse)').matches || 'ontouchstart' in window;
|
||||||
|
|
||||||
|
function focusInput() {
|
||||||
|
if (!hiddenInput || isTouchDevice) return;
|
||||||
|
hiddenInput.focus({ preventScroll: true });
|
||||||
|
window.requestAnimationFrame(() => {
|
||||||
|
if (document.activeElement !== hiddenInput) {
|
||||||
|
hiddenInput.focus({ preventScroll: true });
|
||||||
|
}
|
||||||
|
if (typeof hiddenInput.select === 'function') {
|
||||||
|
hiddenInput.select();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let statusTimer = null;
|
||||||
|
|
||||||
|
function setStatus(message, isError = false, timeoutMs = 1400) {
|
||||||
|
if (!statusLine) return;
|
||||||
|
clearTimeout(statusTimer);
|
||||||
|
statusLine.textContent = message;
|
||||||
|
statusLine.classList.toggle('is-error', isError);
|
||||||
|
statusLine.classList.toggle('is-visible', Boolean(message));
|
||||||
|
if (!message || timeoutMs <= 0) return;
|
||||||
|
statusTimer = window.setTimeout(() => {
|
||||||
|
statusLine.textContent = '';
|
||||||
|
statusLine.classList.remove('is-visible');
|
||||||
|
statusLine.classList.remove('is-error');
|
||||||
|
}, timeoutMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearStatus() {
|
||||||
|
clearTimeout(statusTimer);
|
||||||
|
statusTimer = null;
|
||||||
|
if (!statusLine) return;
|
||||||
|
statusLine.textContent = '';
|
||||||
|
statusLine.classList.remove('is-visible');
|
||||||
|
statusLine.classList.remove('is-error');
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeStack() {
|
||||||
|
while (calc.stack.length > 4) {
|
||||||
|
calc.stack.shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStackLine(indexFromTop) {
|
||||||
|
return indexFromTop >= 0 && indexFromTop < calc.stack.length ? calc.stack[indexFromTop] : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStackDisplayValue(label) {
|
||||||
|
if (label === 'X') {
|
||||||
|
return calc.isEditing ? calc.inputValue : (calc.formatNumber(getStackLine(0)) || '');
|
||||||
|
}
|
||||||
|
if (label === 'Y') {
|
||||||
|
return calc.isEditing ? (calc.formatNumber(getStackLine(0)) || '') : (calc.formatNumber(getStackLine(1)) || '');
|
||||||
|
}
|
||||||
|
if (label === 'Z') {
|
||||||
|
return calc.isEditing ? (calc.formatNumber(getStackLine(1)) || '') : (calc.formatNumber(getStackLine(2)) || '');
|
||||||
|
}
|
||||||
|
return calc.isEditing ? (calc.formatNumber(getStackLine(2)) || '') : (calc.formatNumber(getStackLine(3)) || '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCopyButtons() {
|
||||||
|
for (const label of ['T', 'Z', 'Y', 'X']) {
|
||||||
|
const value = getStackDisplayValue(label);
|
||||||
|
const button = stackCopyButtons[label];
|
||||||
|
if (!button) continue;
|
||||||
|
button.classList.toggle('is-visible', Boolean(value));
|
||||||
|
button.disabled = !value;
|
||||||
|
button.setAttribute('aria-hidden', value ? 'false' : 'true');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function render() {
|
||||||
|
normalizeStack();
|
||||||
|
const isPortrait = window.matchMedia('(orientation: portrait)').matches || window.innerWidth <= 860;
|
||||||
|
calculatorEl?.classList.toggle('portrait', isPortrait);
|
||||||
|
calculatorEl?.classList.toggle('landscape', !isPortrait);
|
||||||
|
stackEls.X.textContent = getStackDisplayValue('X');
|
||||||
|
stackEls.Y.textContent = getStackDisplayValue('Y');
|
||||||
|
stackEls.Z.textContent = getStackDisplayValue('Z');
|
||||||
|
stackEls.T.textContent = getStackDisplayValue('T');
|
||||||
|
updateCopyButtons();
|
||||||
|
modeButton.textContent = calc.angleMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
function pushEditingValueIfNeeded() {
|
||||||
|
if (!calc.isEditing) return;
|
||||||
|
if (calc.inputValue !== '') {
|
||||||
|
calc.push(calc.parseInputValue(calc.inputValue));
|
||||||
|
}
|
||||||
|
calc.inputValue = '';
|
||||||
|
calc.isEditing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function inputToX(value) {
|
||||||
|
if (!calc.isEditing) {
|
||||||
|
calc.isEditing = true;
|
||||||
|
calc.inputValue = '';
|
||||||
|
}
|
||||||
|
if (value === 'Backspace') {
|
||||||
|
calc.inputValue = calc.inputValue.slice(0, -1);
|
||||||
|
} else {
|
||||||
|
calc.inputValue += value;
|
||||||
|
}
|
||||||
|
if (calc.inputValue === '') {
|
||||||
|
calc.isEditing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pasteTextIntoStack(text) {
|
||||||
|
if (!text) {
|
||||||
|
setStatus('Clipboard empty');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const value = calc.parseInputValue(text);
|
||||||
|
if (!Number.isFinite(value)) {
|
||||||
|
setStatus('Clipboard is not a number');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
calc.push(value);
|
||||||
|
calc.isEditing = false;
|
||||||
|
calc.inputValue = '';
|
||||||
|
setStatus('Pasted');
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function execute(name) {
|
||||||
|
try {
|
||||||
|
if (name === 'enter') {
|
||||||
|
if (calc.isEditing) {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
} else if (calc.isValidIndex(0)) {
|
||||||
|
calc.push(calc.stack[0]);
|
||||||
|
}
|
||||||
|
} else if (name === 'clear') {
|
||||||
|
calc.clear();
|
||||||
|
calc.inputValue = '';
|
||||||
|
calc.isEditing = false;
|
||||||
|
} else if (name === 'escape') {
|
||||||
|
calc.inputValue = '';
|
||||||
|
calc.isEditing = false;
|
||||||
|
} else if (name === 'backspace') {
|
||||||
|
if (calc.isEditing) {
|
||||||
|
inputToX('Backspace');
|
||||||
|
} else if (calc.isValidIndex(0)) {
|
||||||
|
calc.remove(0);
|
||||||
|
}
|
||||||
|
} else if (name === 'swap') {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
if (calc.isValidIndex(1)) {
|
||||||
|
calc.swap(0, 1);
|
||||||
|
}
|
||||||
|
} else if (name === 'neg') {
|
||||||
|
if (calc.isEditing) {
|
||||||
|
calc.inputValue = calc.inputValue.startsWith('-') ? calc.inputValue.slice(1) : `-${calc.inputValue}`;
|
||||||
|
} else {
|
||||||
|
calc.push(calc.pop() * -1);
|
||||||
|
}
|
||||||
|
} 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 {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
calc.command(name);
|
||||||
|
}
|
||||||
|
render();
|
||||||
|
} catch (error) {
|
||||||
|
setStatus(error?.message || 'Operation error', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createKeyButton({ label, input, action, spacer, className }) {
|
||||||
|
if (spacer) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
return div;
|
||||||
|
}
|
||||||
|
const button = document.createElement('button');
|
||||||
|
button.type = 'button';
|
||||||
|
button.textContent = label;
|
||||||
|
button.className = className;
|
||||||
|
button.addEventListener('click', () => {
|
||||||
|
if (input) {
|
||||||
|
inputToX(input);
|
||||||
|
render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
execute(action);
|
||||||
|
});
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildGrid(container, keys) {
|
||||||
|
container.innerHTML = '';
|
||||||
|
keys.forEach((key) => container.appendChild(createKeyButton(key)));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function copyStackValue(label) {
|
||||||
|
const value = getStackDisplayValue(label);
|
||||||
|
if (!value) return;
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(value);
|
||||||
|
clearStatus();
|
||||||
|
} catch (error) {
|
||||||
|
setStatus('Copy unavailable', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyboard(event) {
|
||||||
|
if (event.defaultPrevented) return;
|
||||||
|
const key = event.key;
|
||||||
|
if (/^[0-9.]$/.test(key)) {
|
||||||
|
event.preventDefault();
|
||||||
|
inputToX(key);
|
||||||
|
render();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const map = {
|
||||||
|
Enter: 'enter',
|
||||||
|
Backspace: 'backspace',
|
||||||
|
Escape: 'escape',
|
||||||
|
Delete: 'clear',
|
||||||
|
'+': 'add',
|
||||||
|
'-': 'sub',
|
||||||
|
'*': 'mul',
|
||||||
|
'/': 'div',
|
||||||
|
'%': 'mod',
|
||||||
|
'^': 'pow',
|
||||||
|
};
|
||||||
|
const arrowMap = {
|
||||||
|
ArrowUp: upButton,
|
||||||
|
ArrowDown: downButton,
|
||||||
|
ArrowLeft: leftButton,
|
||||||
|
ArrowRight: rightButton,
|
||||||
|
};
|
||||||
|
if (arrowMap[key]) {
|
||||||
|
event.preventDefault();
|
||||||
|
arrowMap[key].click();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (map[key]) {
|
||||||
|
event.preventDefault();
|
||||||
|
execute(map[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const modeOptions = ['deg', 'rad', 'grad'];
|
||||||
|
let activeMenuEl = null;
|
||||||
|
|
||||||
|
function closeModeMenu() {
|
||||||
|
if (activeMenuEl) {
|
||||||
|
activeMenuEl.remove();
|
||||||
|
activeMenuEl = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function openMenu(anchorButton, items, onSelect) {
|
||||||
|
const rect = anchorButton.getBoundingClientRect();
|
||||||
|
const menu = document.createElement('div');
|
||||||
|
menu.className = 'menu-popup';
|
||||||
|
menu.style.top = `${rect.bottom + 6 + window.scrollY}px`;
|
||||||
|
menu.style.left = `${rect.left + window.scrollX}px`;
|
||||||
|
menu.style.minWidth = `${rect.width}px`;
|
||||||
|
for (const item of items) {
|
||||||
|
const button = document.createElement('button');
|
||||||
|
button.type = 'button';
|
||||||
|
button.className = `menu-popup-item${item.active ? ' is-active' : ''}`;
|
||||||
|
button.textContent = item.label;
|
||||||
|
button.addEventListener('click', () => onSelect(item.value));
|
||||||
|
menu.appendChild(button);
|
||||||
|
}
|
||||||
|
document.body.appendChild(menu);
|
||||||
|
return menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleModeMenu() {
|
||||||
|
if (activeMenuEl) {
|
||||||
|
closeModeMenu();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closeConstMenu();
|
||||||
|
activeMenuEl = openMenu(modeButton, modeOptions.map((mode) => ({
|
||||||
|
label: mode,
|
||||||
|
value: mode,
|
||||||
|
active: mode === calc.angleMode,
|
||||||
|
})), (mode) => {
|
||||||
|
calc.angleMode = mode;
|
||||||
|
render();
|
||||||
|
closeModeMenu();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
modeButton.addEventListener('click', (event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
toggleModeMenu();
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener('resize', () => {
|
||||||
|
closeModeMenu();
|
||||||
|
closeConstMenu();
|
||||||
|
render();
|
||||||
|
});
|
||||||
|
window.addEventListener('scroll', () => {
|
||||||
|
closeModeMenu();
|
||||||
|
closeConstMenu();
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
window.addEventListener('click', (event) => {
|
||||||
|
const stackCopyButton = event.target.closest('.stack-copy-button');
|
||||||
|
if (stackCopyButton) {
|
||||||
|
const label = stackCopyButton.dataset.copyStack;
|
||||||
|
if (label) copyStackValue(label);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (activeMenuEl && !event.target.closest('.menu-popup') && event.target !== modeButton && event.target !== constButton) {
|
||||||
|
closeModeMenu();
|
||||||
|
closeConstMenu();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
pasteButton.addEventListener('click', async () => {
|
||||||
|
try {
|
||||||
|
const text = await navigator.clipboard.readText();
|
||||||
|
pasteTextIntoStack(text);
|
||||||
|
clearStatus();
|
||||||
|
} catch (error) {
|
||||||
|
setStatus('Paste unavailable', true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
hiddenInput.addEventListener('paste', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
const text = event.clipboardData?.getData('text') ?? '';
|
||||||
|
pasteTextIntoStack(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
upButton.addEventListener('click', () => {});
|
||||||
|
|
||||||
|
const constantLabels = {
|
||||||
|
pi: 'π',
|
||||||
|
e: 'e',
|
||||||
|
phi: 'φ',
|
||||||
|
g: 'g',
|
||||||
|
c: 'c',
|
||||||
|
};
|
||||||
|
const constantOrder = ['pi', 'e', 'phi', 'g', 'c'];
|
||||||
|
|
||||||
|
|
||||||
|
function closeConstMenu() {
|
||||||
|
if (activeMenuEl) {
|
||||||
|
activeMenuEl.remove();
|
||||||
|
activeMenuEl = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleConstMenu() {
|
||||||
|
if (activeMenuEl) {
|
||||||
|
closeConstMenu();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closeModeMenu();
|
||||||
|
const availableConstants = calc.listConstants();
|
||||||
|
const keys = [...constantOrder, ...Object.keys(availableConstants).filter((name) => !constantOrder.includes(name))]
|
||||||
|
.filter((name) => Object.prototype.hasOwnProperty.call(availableConstants, name));
|
||||||
|
activeMenuEl = openMenu(constButton, keys.map((name) => ({
|
||||||
|
label: constantLabels[name] ?? name,
|
||||||
|
value: name,
|
||||||
|
})), (name) => {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
|
calc.push(availableConstants[name]);
|
||||||
|
render();
|
||||||
|
clearStatus();
|
||||||
|
closeConstMenu();
|
||||||
|
focusInput();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
constButton.addEventListener('click', (event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
toggleConstMenu();
|
||||||
|
});
|
||||||
|
|
||||||
|
leftButton.addEventListener('click', () => {});
|
||||||
|
downButton.addEventListener('click', () => {
|
||||||
|
if (!calc.isEditing && calc.isValidIndex(0)) {
|
||||||
|
const value = calc.stack[0];
|
||||||
|
calc.remove(0);
|
||||||
|
calc.isEditing = true;
|
||||||
|
calc.inputValue = calc.formatNumber(value);
|
||||||
|
render();
|
||||||
|
focusInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
rightButton.addEventListener('click', () => {
|
||||||
|
execute('swap');
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener('keydown', handleKeyboard, { capture: true });
|
||||||
|
window.addEventListener('load', focusInput);
|
||||||
|
window.addEventListener('pageshow', focusInput);
|
||||||
|
window.addEventListener('focus', focusInput);
|
||||||
|
window.addEventListener('pointerdown', focusInput, true);
|
||||||
|
window.addEventListener('mousedown', focusInput, true);
|
||||||
|
|
||||||
|
hiddenInput.setAttribute('inputmode', 'none');
|
||||||
|
hiddenInput.setAttribute('readonly', 'readonly');
|
||||||
|
hiddenInput.addEventListener('focus', () => {
|
||||||
|
if (isTouchDevice) {
|
||||||
|
hiddenInput.blur();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.requestAnimationFrame(() => {
|
||||||
|
hiddenInput.select();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('click', (event) => {
|
||||||
|
if (!isTouchDevice && !event.target.closest('.menu-popup')) {
|
||||||
|
focusInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
buildGrid(keypadGrid, keypadKeys);
|
||||||
|
buildGrid(functionsGrid, functionKeys);
|
||||||
|
buildGrid(trigoGrid, trigoKeys);
|
||||||
|
render();
|
||||||
|
focusInput();
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
# Calc 02 Visual Portrait
|
||||||
|
|
||||||
|
## Display
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────── Display ────────────┐
|
||||||
|
| T: |
|
||||||
|
| Z: |
|
||||||
|
| Y: |
|
||||||
|
| X: |
|
||||||
|
└─────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
## Display Buttons
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────── Display Buttons ────────┐
|
||||||
|
| Const | Paste | Up | Mode |
|
||||||
|
| | Left | Down | Right |
|
||||||
|
└─────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
## Keypad
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────── Keypad ─────────────┐
|
||||||
|
| +/- | Clear | Esc | backspace |
|
||||||
|
| 7 | 8 | 9 | / |
|
||||||
|
| 4 | 5 | 6 | * |
|
||||||
|
| 1 | 2 | 3 | - |
|
||||||
|
| 0 | . | Enter | + |
|
||||||
|
└─────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────── Functions ──────────┐
|
||||||
|
| x^2 | y^x | 1/x | % |
|
||||||
|
| √x | y√x | 10^x | |
|
||||||
|
| log | ln | | |
|
||||||
|
└─────────────────────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
## Trigo
|
||||||
|
|
||||||
|
```
|
||||||
|
┌──────────── Trigo ──────────────┐
|
||||||
|
| sin | cos | tan | |
|
||||||
|
| asin | acos | atan | |
|
||||||
|
└─────────────────────────────────┘
|
||||||
|
```
|
||||||
+70
-1
@@ -2,6 +2,9 @@ class RpnCalculator {
|
|||||||
static CONSTANTS = {
|
static CONSTANTS = {
|
||||||
pi: Math.PI,
|
pi: Math.PI,
|
||||||
e: Math.E,
|
e: Math.E,
|
||||||
|
phi: (1 + Math.sqrt(5)) / 2,
|
||||||
|
g: 9.80665,
|
||||||
|
c: 299792458,
|
||||||
};
|
};
|
||||||
|
|
||||||
static OPERATIONS = {
|
static OPERATIONS = {
|
||||||
@@ -46,6 +49,20 @@ class RpnCalculator {
|
|||||||
aliases: ['^', 'y^x'],
|
aliases: ['^', 'y^x'],
|
||||||
execute: (calc, a, b) => Math.pow(a, b),
|
execute: (calc, a, b) => Math.pow(a, b),
|
||||||
},
|
},
|
||||||
|
root: {
|
||||||
|
category: 'Arithmetic',
|
||||||
|
argCount: 2,
|
||||||
|
aliases: ['y√x', 'yroot', 'nroot'],
|
||||||
|
execute: (calc, a, b) => {
|
||||||
|
if (b === 0) {
|
||||||
|
throw new Error('Invalid input for root');
|
||||||
|
}
|
||||||
|
if (a < 0 && b % 2 === 0) {
|
||||||
|
throw new Error('Invalid input for root');
|
||||||
|
}
|
||||||
|
return Math.pow(a, 1 / b);
|
||||||
|
},
|
||||||
|
},
|
||||||
sqr: {
|
sqr: {
|
||||||
category: 'Arithmetic',
|
category: 'Arithmetic',
|
||||||
argCount: 1,
|
argCount: 1,
|
||||||
@@ -178,7 +195,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', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
Arithmetic: ['add', 'sub', 'mul', 'div', 'mod', 'pow', 'root', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
||||||
Trigonometry: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan'],
|
Trigonometry: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan'],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -198,6 +215,15 @@ class RpnCalculator {
|
|||||||
this.enabledCommands = new Set(selectedCommands.map((name) => this.normalizeCommandName(name)).filter((name) => RpnCalculator.OPERATIONS[name]));
|
this.enabledCommands = new Set(selectedCommands.map((name) => this.normalizeCommandName(name)).filter((name) => RpnCalculator.OPERATIONS[name]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static isValidConstantName(name) {
|
||||||
|
return typeof name === 'string' && name.trim() !== '';
|
||||||
|
}
|
||||||
|
|
||||||
|
static isReservedName(name) {
|
||||||
|
const normalized = typeof name === 'string' ? name.toLowerCase() : '';
|
||||||
|
return Boolean(RpnCalculator.OPERATIONS[normalized]);
|
||||||
|
}
|
||||||
|
|
||||||
toRadians(value) {
|
toRadians(value) {
|
||||||
if (this.angleMode === 'grad') {
|
if (this.angleMode === 'grad') {
|
||||||
return (value * Math.PI) / 200;
|
return (value * Math.PI) / 200;
|
||||||
@@ -243,6 +269,45 @@ class RpnCalculator {
|
|||||||
return typeof name === 'string' && Object.prototype.hasOwnProperty.call(this.constants, name.toLowerCase());
|
return typeof name === 'string' && Object.prototype.hasOwnProperty.call(this.constants, name.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setConstant(name, value) {
|
||||||
|
if (!RpnCalculator.isValidConstantName(name)) {
|
||||||
|
throw new Error('Invalid constant name');
|
||||||
|
}
|
||||||
|
if (!Number.isFinite(value)) {
|
||||||
|
throw new Error('Invalid constant value');
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = name.trim().toLowerCase();
|
||||||
|
if (RpnCalculator.isReservedName(normalized)) {
|
||||||
|
throw new Error(`Constant name conflicts with a command: ${name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.constants[normalized] = value;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
removeConstant(name) {
|
||||||
|
if (!RpnCalculator.isValidConstantName(name)) {
|
||||||
|
throw new Error('Invalid constant name');
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalized = name.trim().toLowerCase();
|
||||||
|
if (!this.isConstantName(normalized)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete this.constants[normalized];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
hasConstant(name) {
|
||||||
|
if (!RpnCalculator.isValidConstantName(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.isConstantName(name.trim().toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
push(value) {
|
push(value) {
|
||||||
if (this.stack.length >= this.maxSize) {
|
if (this.stack.length >= this.maxSize) {
|
||||||
throw new Error('Stack overflow');
|
throw new Error('Stack overflow');
|
||||||
@@ -394,6 +459,10 @@ class RpnCalculator {
|
|||||||
getConstants() {
|
getConstants() {
|
||||||
return { ...this.constants };
|
return { ...this.constants };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listConstants() {
|
||||||
|
return this.getConstants();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
|
|||||||
Reference in New Issue
Block a user