Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/lib/actions/paramInput.ts
Original file line number Diff line number Diff line change
@@ -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:
* <input use:paramInput />
* <input onchange={(e) => commit(e.currentTarget.value)} use:paramInput />
* <input use:paramInput={{ blurOnEnter: true }} />
* <textarea 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;
Expand All @@ -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();
}

Expand All @@ -43,6 +57,7 @@ export function paramInput(
}
}

node.addEventListener('focus', handleFocus);
node.addEventListener('keydown', handleKeydown);
node.setAttribute('autocomplete', 'off');

Expand All @@ -51,6 +66,7 @@ export function paramInput(
Object.assign(opts, defaultOptions, newOptions);
},
destroy() {
node.removeEventListener('focus', handleFocus);
node.removeEventListener('keydown', handleKeydown);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/dialogs/BlockPropertiesDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
/>
<span class="node-type">{typeDef.name}</span>
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/dialogs/EventPropertiesDialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
/>
<span class="node-type">{typeDef.name}</span>
Expand Down Expand Up @@ -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
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/nodes/BaseNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/lib/components/panels/SimulationPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
/>
Expand All @@ -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
/>
Expand Down Expand Up @@ -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
/>
Expand All @@ -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
/>
Expand All @@ -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
/>
Expand All @@ -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
/>
Expand All @@ -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
/>
Expand Down
9 changes: 9 additions & 0 deletions src/lib/utils/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?.();
}
8 changes: 7 additions & 1 deletion src/routes/editor/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
}
Expand All @@ -782,6 +783,7 @@
switch (event.key.toLowerCase()) {
case 's':
event.preventDefault();
commitFocusedEntry(event);
if (event.shiftKey) {
handleSaveAs();
} else {
Expand All @@ -794,6 +796,7 @@
return;
case 'e':
event.preventDefault();
commitFocusedEntry(event);
exportDialogOpen = true;
return;
case 'f':
Expand Down Expand Up @@ -831,6 +834,7 @@
return;
case 'z':
event.preventDefault();
commitFocusedEntry(event);
if (event.shiftKey) {
historyStore.redo();
} else {
Expand All @@ -839,10 +843,12 @@
return;
case 'y':
event.preventDefault();
commitFocusedEntry(event);
historyStore.redo();
return;
case 'enter':
event.preventDefault();
commitFocusedEntry(event);
handleRun();
return;
}
Expand Down
Loading