Skip to content

Commit bdc6d3a

Browse files
committed
Fix clicking out and intermittent cursor clicking in text field
1 parent bbede53 commit bdc6d3a

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

frontend/src/components/views/Graph.svelte

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { getContext, onDestroy, onMount } from "svelte";
2+
import { getContext, onDestroy, onMount, tick } from "svelte";
33
import { cubicInOut } from "svelte/easing";
44
import { fade } from "svelte/transition";
55
import NodeCatalog from "/src/components/floating-menus/NodeCatalog.svelte";
@@ -70,13 +70,10 @@
7070
editingNameExportIndex = index;
7171
}
7272
73-
function focusInput(currentName: string) {
73+
async function focusInput(currentName: string) {
7474
editingNameText = currentName;
75-
setTimeout(() => {
76-
if (inputElement) {
77-
inputElement.focus();
78-
}
79-
}, 0);
75+
await tick();
76+
inputElement?.focus();
8077
}
8178
8279
function setEditingImportName(event: Event) {
@@ -97,33 +94,26 @@
9794
}
9895
}
9996
100-
function setEditingNodeName(nodeId: bigint, currentName: string) {
101-
editingNameText = currentName;
102-
editingNameNodeId = nodeId;
103-
// Wait for the input to mount before focusing and selecting its contents.
104-
setTimeout(() => {
105-
inputElement?.focus();
106-
inputElement?.select();
107-
}, 0);
108-
}
109-
11097
function commitEditingNodeName(event: Event) {
111-
if (editingNameNodeId === undefined) return;
112-
if (!(event.target instanceof HTMLInputElement)) return;
113-
editor.setLayerName(editingNameNodeId, event.target.value);
114-
editingNameNodeId = undefined;
115-
}
98+
if (editingNameNodeId === undefined || !(event.target instanceof HTMLInputElement)) return;
11699
117-
function cancelEditingNodeName() {
100+
editor.setLayerName(editingNameNodeId, event.target.value);
118101
editingNameNodeId = undefined;
119102
}
120103
121104
onMount(() => {
122-
// Backend dispatches this when the user double-clicks a layer's name area or presses F2 with one layer selected.
123-
subscriptions.subscribeFrontendMessage("TriggerEditLayerNameInGraph", (data) => {
105+
// Backend dispatches this when the user double-clicks a layer's name area
106+
subscriptions.subscribeFrontendMessage("TriggerEditLayerNameInGraph", async (data) => {
124107
const node = $nodeGraph.nodes.get(data.nodeId);
125108
if (!node) return;
126-
setEditingNodeName(data.nodeId, node.displayName);
109+
110+
editingNameText = node.displayName;
111+
editingNameNodeId = data.nodeId;
112+
113+
await tick();
114+
115+
inputElement?.focus();
116+
inputElement?.select();
127117
});
128118
});
129119
@@ -640,10 +630,17 @@
640630
bind:value={editingNameText}
641631
on:pointerdown|stopPropagation
642632
on:dblclick|stopPropagation
643-
on:blur={cancelEditingNodeName}
633+
on:blur={commitEditingNodeName}
644634
on:keydown={(e) => {
645-
if (e.key === "Enter") commitEditingNodeName(e);
646-
else if (e.key === "Escape") cancelEditingNodeName();
635+
// Stop propagation when we handle the key ourselves so the global keyboard forwarder (`shouldRedirectKeyboardEventToBackend`) doesn't also dispatch them.
636+
// Its Escape carve-out would otherwise close the graph view, and Enter could trigger unrelated bindings.
637+
if (e.key === "Enter") {
638+
commitEditingNodeName(e);
639+
e.stopPropagation();
640+
} else if (e.key === "Escape") {
641+
editingNameNodeId = undefined;
642+
e.stopPropagation();
643+
}
647644
}}
648645
/>
649646
{:else}
@@ -1316,6 +1313,9 @@
13161313
height: 24px;
13171314
border-radius: 2px;
13181315
field-sizing: content;
1316+
// Stack above the absolutely-positioned grip/lock/visibility siblings, which can otherwise overlap the input's right edge and hijack clicks there.
1317+
position: relative;
1318+
z-index: 1;
13191319
}
13201320
}
13211321

0 commit comments

Comments
 (0)