Make calculator child drafts atomic
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 18m6s
Validate and deploy Sokko G / deploy (push) Successful in 9s

This commit is contained in:
Shinuwa 2026-10-30 20:02:48 +01:00
parent ef7eba73db
commit 72b3b15c7e
3 changed files with 27 additions and 33 deletions

View file

@ -85,10 +85,10 @@ export function CalculatorModule({ toolboxId, moduleId, context, configurationTa
}, []);
useLayoutEffect(() => {
if (!pendingExpressionFocusRef.current || !editingEntryId) return;
if (!pendingExpressionFocusRef.current || (!editingEntryId && !activeParentId)) return;
pendingExpressionFocusRef.current = false;
window.requestAnimationFrame(() => focusExpressionInputAtEnd(expressionInputRef.current));
}, [editingEntryId]);
}, [activeParentId, editingEntryId]);
function save(entries) {
context.setModuleData(toolboxId, moduleId, { ...data, entries: evaluateCalculatorEntries(entries) });
@ -118,18 +118,22 @@ export function CalculatorModule({ toolboxId, moduleId, context, configurationTa
return;
}
save([
...data.entries,
{
id: context.uid("calc"),
parentId: formulaParentId,
label: cleanLabel,
value: result,
formula: storedFormula,
current: storedCurrent
}
]);
setExpression(formulaParent ? BASE_TOKEN : "");
const newEntry = {
id: context.uid("calc"),
parentId: formulaParentId,
label: cleanLabel,
value: result,
formula: storedFormula,
current: storedCurrent
};
save([...data.entries, newEntry]);
if (formulaParent) {
setEditingEntryId(newEntry.id);
setActiveParentId("");
setLabel(cleanLabel);
return;
}
setExpression("");
setCurrent("");
setShowCurrent(false);
setLabel("");
@ -145,22 +149,9 @@ export function CalculatorModule({ toolboxId, moduleId, context, configurationTa
}
function createChildEntry(entry) {
const childId = context.uid("calc");
save([
...data.entries,
{
id: childId,
parentId: entry.id,
label: "",
value: Number.isFinite(entry.value) ? entry.value : 0,
formula: BASE_TOKEN,
current: 0
}
]);
setEditingEntryId("");
pendingExpressionFocusRef.current = true;
setEditingEntryId(childId);
setActiveParentId("");
setActiveParentId(entry.id);
setExpression(BASE_TOKEN);
setCurrent("");
setShowCurrent(false);