fix: preserve editing value in hp48 stack rendering
This commit is contained in:
+55
-19
@@ -286,15 +286,21 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function render() {
|
function getLineValue(line) {
|
||||||
const stack = calc.stack;
|
if (line === 0 && calc.isEditing) {
|
||||||
const names = ['T', 'Z', 'Y', 'X'];
|
return calc.inputValue;
|
||||||
const lines = [];
|
}
|
||||||
for (let i = 3; i >= 0; i -= 1) {
|
return calc.stack[line];
|
||||||
const value = stack[i];
|
|
||||||
lines.push(`<div class="stack-line"><div class="label">${names[3 - i]}</div><div>${value !== undefined ? calc.formatNumber(value) : ''}</div></div>`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function render() {
|
||||||
|
const names = ['T', 'Z', 'Y', 'X'];
|
||||||
|
const lines = [];
|
||||||
|
for (let line = 3; line >= 0; line -= 1) {
|
||||||
|
const value = getLineValue(line);
|
||||||
|
lines.push(`<div class="stack-line"><div class="label">${names[3 - line]}</div><div>${value !== undefined && value !== '' ? calc.formatNumber(value) : ''}</div></div>`);
|
||||||
|
}
|
||||||
|
|
||||||
stackEl.innerHTML = lines.join('');
|
stackEl.innerHTML = lines.join('');
|
||||||
displayEl.textContent = calc.isEditing ? `ENTERING: ${calc.inputValue}` : 'READY';
|
displayEl.textContent = calc.isEditing ? `ENTERING: ${calc.inputValue}` : 'READY';
|
||||||
inputValueLabel.textContent = calc.inputValue || '∅';
|
inputValueLabel.textContent = calc.inputValue || '∅';
|
||||||
@@ -304,22 +310,40 @@
|
|||||||
errorEl.textContent = '';
|
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) {
|
function execute(name) {
|
||||||
try {
|
try {
|
||||||
if (name === 'swap') {
|
if (name === 'swap') {
|
||||||
if (calc.isEditing) calc.command('enter');
|
pushEditingValueIfNeeded();
|
||||||
if (calc.stack.length >= 2) calc.swap(0, 1);
|
if (calc.stack.length >= 2) calc.swap(0, 1);
|
||||||
} else if (name === 'drop') {
|
} else if (name === 'drop') {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
if (calc.stack.length >= 1) calc.remove(0);
|
if (calc.stack.length >= 1) calc.remove(0);
|
||||||
} else if (name === 'clear') {
|
} else if (name === 'clear') {
|
||||||
calc.clear();
|
calc.clear();
|
||||||
} else if (name === 'enter') {
|
} else if (name === 'enter') {
|
||||||
if (calc.isEditing) calc.command('enter');
|
if (calc.isEditing) {
|
||||||
} else if (name === 'pi' || name === 'e') {
|
pushEditingValueIfNeeded();
|
||||||
calc.command(name);
|
}
|
||||||
} else {
|
} else {
|
||||||
|
pushEditingValueIfNeeded();
|
||||||
calc.command(name);
|
calc.command(name);
|
||||||
}
|
}
|
||||||
|
syncInputFromState();
|
||||||
render();
|
render();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errorEl.textContent = error.message;
|
errorEl.textContent = error.message;
|
||||||
@@ -338,13 +362,29 @@
|
|||||||
input.value = calc.inputValue;
|
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) => {
|
screen.addEventListener('keydown', (event) => {
|
||||||
try {
|
try {
|
||||||
if (event.key === 'Enter') {
|
if (event.key === 'Enter') {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (calc.isEditing) {
|
if (calc.isEditing) {
|
||||||
calc.command('enter');
|
calc.command('enter');
|
||||||
syncInputFromState();
|
|
||||||
}
|
}
|
||||||
render();
|
render();
|
||||||
return;
|
return;
|
||||||
@@ -353,9 +393,7 @@
|
|||||||
if (event.key === 'Backspace') {
|
if (event.key === 'Backspace') {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (calc.isEditing) {
|
if (calc.isEditing) {
|
||||||
calc.inputValue = calc.inputValue.slice(0, -1);
|
editXWithKey('Backspace');
|
||||||
if (calc.inputValue === '') calc.isEditing = false;
|
|
||||||
syncInputFromState();
|
|
||||||
render();
|
render();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -363,8 +401,7 @@
|
|||||||
|
|
||||||
if (isInputChar(event.key)) {
|
if (isInputChar(event.key)) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
calc.input(event.key);
|
editXWithKey(event.key);
|
||||||
syncInputFromState();
|
|
||||||
render();
|
render();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -380,8 +417,7 @@
|
|||||||
|
|
||||||
if (keyMap[event.key]) {
|
if (keyMap[event.key]) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
calc.command(keyMap[event.key]);
|
execute(keyMap[event.key]);
|
||||||
render();
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
errorEl.textContent = error.message;
|
errorEl.textContent = error.message;
|
||||||
|
|||||||
Reference in New Issue
Block a user