|
1 | 1 | <script lang="ts"> |
2 | | - import { getContext, onDestroy, onMount } from "svelte"; |
| 2 | + import { getContext, onDestroy, onMount, tick } from "svelte"; |
3 | 3 | import { cubicInOut } from "svelte/easing"; |
4 | 4 | import { fade } from "svelte/transition"; |
5 | 5 | import NodeCatalog from "/src/components/floating-menus/NodeCatalog.svelte"; |
|
70 | 70 | editingNameExportIndex = index; |
71 | 71 | } |
72 | 72 |
|
73 | | - function focusInput(currentName: string) { |
| 73 | + async function focusInput(currentName: string) { |
74 | 74 | editingNameText = currentName; |
75 | | - setTimeout(() => { |
76 | | - if (inputElement) { |
77 | | - inputElement.focus(); |
78 | | - } |
79 | | - }, 0); |
| 75 | + await tick(); |
| 76 | + inputElement?.focus(); |
80 | 77 | } |
81 | 78 |
|
82 | 79 | function setEditingImportName(event: Event) { |
|
97 | 94 | } |
98 | 95 | } |
99 | 96 |
|
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 | | -
|
110 | 97 | 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; |
116 | 99 |
|
117 | | - function cancelEditingNodeName() { |
| 100 | + editor.setLayerName(editingNameNodeId, event.target.value); |
118 | 101 | editingNameNodeId = undefined; |
119 | 102 | } |
120 | 103 |
|
121 | 104 | 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) => { |
124 | 107 | const node = $nodeGraph.nodes.get(data.nodeId); |
125 | 108 | 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(); |
127 | 117 | }); |
128 | 118 | }); |
129 | 119 |
|
|
640 | 630 | bind:value={editingNameText} |
641 | 631 | on:pointerdown|stopPropagation |
642 | 632 | on:dblclick|stopPropagation |
643 | | - on:blur={cancelEditingNodeName} |
| 633 | + on:blur={commitEditingNodeName} |
644 | 634 | 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 | + } |
647 | 644 | }} |
648 | 645 | /> |
649 | 646 | {:else} |
|
1316 | 1313 | height: 24px; |
1317 | 1314 | border-radius: 2px; |
1318 | 1315 | 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; |
1319 | 1319 | } |
1320 | 1320 | } |
1321 | 1321 |
|
|
0 commit comments