diff --git a/samples/hp48/index.html b/samples/hp48/index.html
index d8eef66..6a5d1f4 100644
--- a/samples/hp48/index.html
+++ b/samples/hp48/index.html
@@ -286,15 +286,21 @@
});
}
- function render() {
- const stack = calc.stack;
- const names = ['T', 'Z', 'Y', 'X'];
- const lines = [];
- for (let i = 3; i >= 0; i -= 1) {
- const value = stack[i];
- lines.push(`
${names[3 - i]}
${value !== undefined ? calc.formatNumber(value) : ''}
`);
+ function getLineValue(line) {
+ if (line === 0 && calc.isEditing) {
+ return calc.inputValue;
+ }
+ return calc.stack[line];
}
+ function render() {
+ const names = ['T', 'Z', 'Y', 'X'];
+ const lines = [];
+ for (let line = 3; line >= 0; line -= 1) {
+ const value = getLineValue(line);
+ lines.push(`${names[3 - line]}
${value !== undefined && value !== '' ? calc.formatNumber(value) : ''}
`);
+ }
+
stackEl.innerHTML = lines.join('');
displayEl.textContent = calc.isEditing ? `ENTERING: ${calc.inputValue}` : 'READY';
inputValueLabel.textContent = calc.inputValue || '∅';
@@ -304,22 +310,40 @@
errorEl.textContent = '';
}
+ function pushEditingValueIfNeeded() {
+ if (!calc.isEditing) return;
+ if (calc.inputValue !== '') {
+ const value = calc.parseInputValue(calc.inputValue);
+ if (calc.stack.length >= calc.maxSize) {
+ throw new Error('Stack overflow');
+ }
+ calc.stack.unshift(value);
+ if (calc.stack.length > 4) calc.stack.length = 4;
+ }
+ calc.inputValue = '';
+ calc.isEditing = false;
+ syncInputFromState();
+ }
+
function execute(name) {
try {
if (name === 'swap') {
- if (calc.isEditing) calc.command('enter');
+ pushEditingValueIfNeeded();
if (calc.stack.length >= 2) calc.swap(0, 1);
} else if (name === 'drop') {
+ pushEditingValueIfNeeded();
if (calc.stack.length >= 1) calc.remove(0);
} else if (name === 'clear') {
calc.clear();
} else if (name === 'enter') {
- if (calc.isEditing) calc.command('enter');
- } else if (name === 'pi' || name === 'e') {
- calc.command(name);
+ if (calc.isEditing) {
+ pushEditingValueIfNeeded();
+ }
} else {
+ pushEditingValueIfNeeded();
calc.command(name);
}
+ syncInputFromState();
render();
} catch (error) {
errorEl.textContent = error.message;
@@ -338,13 +362,29 @@
input.value = calc.inputValue;
}
+ function editXWithKey(key) {
+ if (!calc.isEditing) {
+ pushEditingValueIfNeeded();
+ calc.isEditing = true;
+ calc.inputValue = '';
+ }
+ if (key === 'Backspace') {
+ calc.inputValue = calc.inputValue.slice(0, -1);
+ } else {
+ calc.inputValue += key;
+ }
+ if (calc.inputValue === '') {
+ calc.isEditing = false;
+ }
+ syncInputFromState();
+ }
+
screen.addEventListener('keydown', (event) => {
try {
if (event.key === 'Enter') {
event.preventDefault();
if (calc.isEditing) {
calc.command('enter');
- syncInputFromState();
}
render();
return;
@@ -353,9 +393,7 @@
if (event.key === 'Backspace') {
event.preventDefault();
if (calc.isEditing) {
- calc.inputValue = calc.inputValue.slice(0, -1);
- if (calc.inputValue === '') calc.isEditing = false;
- syncInputFromState();
+ editXWithKey('Backspace');
render();
}
return;
@@ -363,8 +401,7 @@
if (isInputChar(event.key)) {
event.preventDefault();
- calc.input(event.key);
- syncInputFromState();
+ editXWithKey(event.key);
render();
return;
}
@@ -380,8 +417,7 @@
if (keyMap[event.key]) {
event.preventDefault();
- calc.command(keyMap[event.key]);
- render();
+ execute(keyMap[event.key]);
}
} catch (error) {
errorEl.textContent = error.message;