diff --git a/src/lib/actions/paramInput.ts b/src/lib/actions/paramInput.ts
index 288b34ee..7a91bb0d 100644
--- a/src/lib/actions/paramInput.ts
+++ b/src/lib/actions/paramInput.ts
@@ -1,18 +1,24 @@
/**
- * Svelte action for consistent keyboard handling on parameter inputs.
+ * Svelte action for entry fields: parameters, names and settings.
+ *
+ * An entry is committed by the `change` event of the field, which fires on
+ * Enter or when the field is left, and only if the value changed. Fields
+ * listen to `onchange`, not `oninput`, so a half typed value is never
+ * committed: no undo step, live mutation or evaluation per keystroke.
*
* Features:
- * - Escape: unfocus (blur) the input, stops propagation
+ * - Escape: restore the value the field had when it was focused and unfocus
+ * it, so no `change` fires and nothing is committed; stops propagation
* - Enter: optionally blur (for single-line inputs, not textareas)
*
* Usage:
- *
+ * commit(e.currentTarget.value)} use:paramInput />
*
*
*/
export interface ParamInputOptions {
- /** Blur input on Escape key (default: true) */
+ /** Restore and blur the input on Escape key (default: true) */
blurOnEscape?: boolean;
/** Blur input on Enter key - ignored for textareas (default: false) */
blurOnEnter?: boolean;
@@ -29,10 +35,18 @@ export function paramInput(
) {
const opts = { ...defaultOptions, ...options };
+ // Value at focus, restored on Escape
+ let focusValue = node.value;
+
+ function handleFocus() {
+ focusValue = node.value;
+ }
+
function handleKeydown(e: Event) {
const event = e as KeyboardEvent;
if (event.key === 'Escape' && opts.blurOnEscape) {
event.stopPropagation();
+ node.value = focusValue;
node.blur();
}
@@ -43,6 +57,7 @@ export function paramInput(
}
}
+ node.addEventListener('focus', handleFocus);
node.addEventListener('keydown', handleKeydown);
node.setAttribute('autocomplete', 'off');
@@ -51,6 +66,7 @@ export function paramInput(
Object.assign(opts, defaultOptions, newOptions);
},
destroy() {
+ node.removeEventListener('focus', handleFocus);
node.removeEventListener('keydown', handleKeydown);
}
};
diff --git a/src/lib/components/dialogs/BlockPropertiesDialog.svelte b/src/lib/components/dialogs/BlockPropertiesDialog.svelte
index c9e3e1d3..fe622f60 100644
--- a/src/lib/components/dialogs/BlockPropertiesDialog.svelte
+++ b/src/lib/components/dialogs/BlockPropertiesDialog.svelte
@@ -329,7 +329,7 @@
data-tour="block-name-input"
type="text"
value={node.name}
- oninput={(e) => handleNameChange(e.currentTarget.value)}
+ onchange={(e) => handleNameChange(e.currentTarget.value)}
use:paramInput
/>
{typeDef.name}
@@ -525,7 +525,7 @@
type="text"
value={formatValue(value)}
placeholder={formatDefault(param.default)}
- oninput={(e) => handleParamChange(param.name, e.currentTarget.value)}
+ onchange={(e) => handleParamChange(param.name, e.currentTarget.value)}
use:paramInput
/>
{/if}
diff --git a/src/lib/components/dialogs/EventPropertiesDialog.svelte b/src/lib/components/dialogs/EventPropertiesDialog.svelte
index a07afc65..93db04bf 100644
--- a/src/lib/components/dialogs/EventPropertiesDialog.svelte
+++ b/src/lib/components/dialogs/EventPropertiesDialog.svelte
@@ -134,7 +134,7 @@
class="node-name-input"
type="text"
value={event.name}
- oninput={(e) => handleNameChange(e.currentTarget.value)}
+ onchange={(e) => handleNameChange(e.currentTarget.value)}
use:paramInput
/>
{typeDef.name}
@@ -196,7 +196,7 @@
type="text"
value={formatValue(value)}
placeholder={formatDefault(param.default)}
- oninput={(e) => handleParamChange(param.name, e.currentTarget.value)}
+ onchange={(e) => handleParamChange(param.name, e.currentTarget.value)}
use:paramInput
/>
diff --git a/src/lib/components/nodes/BaseNode.svelte b/src/lib/components/nodes/BaseNode.svelte
index 87bfa22b..cc73be8d 100644
--- a/src/lib/components/nodes/BaseNode.svelte
+++ b/src/lib/components/nodes/BaseNode.svelte
@@ -338,7 +338,7 @@
type="text"
value={formatParamValue(data.params[paramName])}
placeholder={formatDefault(paramDef.default)}
- oninput={(e) => handlePinnedParamChange(paramName, e.currentTarget.value)}
+ onchange={(e) => handlePinnedParamChange(paramName, e.currentTarget.value)}
onmousedown={(e) => e.stopPropagation()}
onfocus={(e) => e.stopPropagation()}
use:paramInput
diff --git a/src/lib/components/panels/SimulationPanel.svelte b/src/lib/components/panels/SimulationPanel.svelte
index 93ff9ba9..5b35fa88 100644
--- a/src/lib/components/panels/SimulationPanel.svelte
+++ b/src/lib/components/panels/SimulationPanel.svelte
@@ -83,7 +83,7 @@
class="code-input"
value={settings.duration}
placeholder={DEFAULT_SIMULATION_SETTINGS.duration}
- oninput={(e) => updateSetting('duration', e.currentTarget.value)}
+ onchange={(e) => updateSetting('duration', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -103,7 +103,7 @@
class="code-input"
value={settings.dt}
placeholder={DEFAULT_SIMULATION_SETTINGS.dt}
- oninput={(e) => updateSetting('dt', e.currentTarget.value)}
+ onchange={(e) => updateSetting('dt', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -199,7 +199,7 @@
class="code-input"
value={settings.rtol}
placeholder={DEFAULT_SIMULATION_SETTINGS.rtol}
- oninput={(e) => updateSetting('rtol', e.currentTarget.value)}
+ onchange={(e) => updateSetting('rtol', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -215,7 +215,7 @@
class="code-input"
value={settings.atol}
placeholder={DEFAULT_SIMULATION_SETTINGS.atol}
- oninput={(e) => updateSetting('atol', e.currentTarget.value)}
+ onchange={(e) => updateSetting('atol', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -231,7 +231,7 @@
class="code-input"
value={settings.ftol}
placeholder={DEFAULT_SIMULATION_SETTINGS.ftol}
- oninput={(e) => updateSetting('ftol', e.currentTarget.value)}
+ onchange={(e) => updateSetting('ftol', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -252,7 +252,7 @@
class="code-input"
value={settings.dt_min}
placeholder={DEFAULT_SIMULATION_SETTINGS.dt_min}
- oninput={(e) => updateSetting('dt_min', e.currentTarget.value)}
+ onchange={(e) => updateSetting('dt_min', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
@@ -271,7 +271,7 @@
class="code-input"
value={settings.dt_max}
placeholder={DEFAULT_SIMULATION_SETTINGS.dt_max}
- oninput={(e) => updateSetting('dt_max', e.currentTarget.value)}
+ onchange={(e) => updateSetting('dt_max', e.currentTarget.value)}
spellcheck="false"
use:paramInput
/>
diff --git a/src/lib/utils/focus.ts b/src/lib/utils/focus.ts
index 07747b0a..3a29d49e 100644
--- a/src/lib/utils/focus.ts
+++ b/src/lib/utils/focus.ts
@@ -9,3 +9,12 @@ export function isInputFocused(event: KeyboardEvent): boolean {
!!(event.target as HTMLElement)?.closest?.('.cm-editor')
);
}
+
+/**
+ * Commit the entry being edited before a shortcut acts on the model. Entry
+ * fields commit on their change event (see the paramInput action), which
+ * leaving the field fires.
+ */
+export function commitFocusedEntry(event: KeyboardEvent): void {
+ if (isInputFocused(event)) (event.target as HTMLElement).blur?.();
+}
diff --git a/src/routes/editor/+page.svelte b/src/routes/editor/+page.svelte
index 69172fd6..7cddc636 100644
--- a/src/routes/editor/+page.svelte
+++ b/src/routes/editor/+page.svelte
@@ -60,7 +60,7 @@
import { iconModeStore } from '$lib/stores/iconMode';
import { clipboardStore } from '$lib/stores/clipboard';
import Tooltip, { tooltip } from '$lib/components/Tooltip.svelte';
- import { isInputFocused } from '$lib/utils/focus';
+ import { isInputFocused, commitFocusedEntry } from '$lib/utils/focus';
import { isTourActive } from '$lib/tours/inputMode';
import { toggleThemeWithTransition } from '$lib/utils/themeTransition';
import { startGuidedTour } from '$lib/tours';
@@ -771,6 +771,7 @@
// Shift+Enter for continue simulation
if (event.shiftKey && event.key === 'Enter') {
event.preventDefault();
+ commitFocusedEntry(event);
if (hasRunSimulation && pyodideReady && !simRunning && !isContinuing) {
handleContinue();
}
@@ -782,6 +783,7 @@
switch (event.key.toLowerCase()) {
case 's':
event.preventDefault();
+ commitFocusedEntry(event);
if (event.shiftKey) {
handleSaveAs();
} else {
@@ -794,6 +796,7 @@
return;
case 'e':
event.preventDefault();
+ commitFocusedEntry(event);
exportDialogOpen = true;
return;
case 'f':
@@ -831,6 +834,7 @@
return;
case 'z':
event.preventDefault();
+ commitFocusedEntry(event);
if (event.shiftKey) {
historyStore.redo();
} else {
@@ -839,10 +843,12 @@
return;
case 'y':
event.preventDefault();
+ commitFocusedEntry(event);
historyStore.redo();
return;
case 'enter':
event.preventDefault();
+ commitFocusedEntry(event);
handleRun();
return;
}