|
26 | 26 | </script> |
27 | 27 |
|
28 | 28 | <script lang="ts"> |
29 | | - import { BaseEdge, getSmoothStepPath, type EdgeProps, Position } from '@xyflow/svelte'; |
| 29 | + import { BaseEdge, EdgeLabel, getSmoothStepPath, type EdgeProps, Position } from '@xyflow/svelte'; |
30 | 30 | import { routingStore } from '$lib/stores/routing'; |
| 31 | + import { graphStore } from '$lib/stores/graph'; |
31 | 32 | import { edgeHighlights } from '$lib/stores/edgeHighlight'; |
| 33 | + import { edgeLabelEdit, editEdgeLabel } from '$lib/stores/edgeLabelEdit.svelte'; |
32 | 34 | import { historyStore } from '$lib/stores/history'; |
33 | 35 | import { screenToFlow } from '$lib/utils/viewUtils'; |
34 | 36 | import { GRID_SIZE, EDGE_SOURCE_OFFSET, EDGE_TARGET_OFFSET, EDGE_CORNER_RADIUS } from '$lib/routing/constants'; |
| 37 | + import { EDGE_LABEL } from '$lib/constants/dimensions'; |
35 | 38 | import type { Direction, RouteResult } from '$lib/routing'; |
36 | 39 | import type { Waypoint } from '$lib/types/nodes'; |
37 | 40 |
|
|
295 | 298 | return midpoints; |
296 | 299 | }); |
297 | 300 |
|
| 301 | + // Connection label, shown on the middle of the longest route segment |
| 302 | + const label = $derived((data as { label?: string } | undefined)?.label ?? ''); |
| 303 | + const isEditingLabel = $derived(edgeLabelEdit.connectionId === id); |
| 304 | +
|
| 305 | + // Editor width follows the typed text |
| 306 | + let draftLength = $state(0); |
| 307 | +
|
| 308 | + const labelAnchor = $derived.by(() => { |
| 309 | + if (!label && !isEditingLabel) return null; |
| 310 | + const points = displayedRoute |
| 311 | + ? [adjustedSource, ...displayedRoute.path, adjustedTarget] |
| 312 | + : [adjustedSource, adjustedTarget]; |
| 313 | + let anchor = points[0]; |
| 314 | + let longest = -1; |
| 315 | + for (let i = 0; i < points.length - 1; i++) { |
| 316 | + const length = Math.abs(points[i + 1].x - points[i].x) + Math.abs(points[i + 1].y - points[i].y); |
| 317 | + if (length > longest) { |
| 318 | + longest = length; |
| 319 | + anchor = { x: (points[i].x + points[i + 1].x) / 2, y: (points[i].y + points[i + 1].y) / 2 }; |
| 320 | + } |
| 321 | + } |
| 322 | + return anchor; |
| 323 | + }); |
| 324 | +
|
| 325 | + function handleEdgeDoubleClick(event: MouseEvent) { |
| 326 | + event.stopPropagation(); |
| 327 | + editEdgeLabel(id); |
| 328 | + } |
| 329 | +
|
| 330 | + function commitLabel(text: string) { |
| 331 | + if (edgeLabelEdit.connectionId !== id) return; |
| 332 | + editEdgeLabel(null); |
| 333 | + if (text.trim() === label) return; |
| 334 | + historyStore.mutate(() => graphStore.updateConnectionLabel(id, text)); |
| 335 | + } |
| 336 | +
|
| 337 | + /** |
| 338 | + * Label editor input. Enter commits, Escape cancels, a pointer press outside |
| 339 | + * the input commits. Blur alone never commits, because removing the editor |
| 340 | + * also blurs the input. Keys are stopped at the input so edge keyboard |
| 341 | + * handling and app shortcuts never see them. Focus waits until the input |
| 342 | + * sits in the label layer. |
| 343 | + */ |
| 344 | + function labelEditor(input: HTMLInputElement) { |
| 345 | + const onKeydown = (event: KeyboardEvent) => { |
| 346 | + event.stopPropagation(); |
| 347 | + if (event.key === 'Enter') commitLabel(input.value); |
| 348 | + else if (event.key === 'Escape') editEdgeLabel(null); |
| 349 | + }; |
| 350 | + const onPointerDown = (event: PointerEvent) => { |
| 351 | + if (event.target !== input) commitLabel(input.value); |
| 352 | + }; |
| 353 | + const onInput = () => { |
| 354 | + draftLength = input.value.length; |
| 355 | + }; |
| 356 | + draftLength = input.value.length; |
| 357 | + input.addEventListener('keydown', onKeydown); |
| 358 | + input.addEventListener('input', onInput); |
| 359 | + document.addEventListener('pointerdown', onPointerDown, true); |
| 360 | + requestAnimationFrame(() => { |
| 361 | + input.focus(); |
| 362 | + input.select(); |
| 363 | + }); |
| 364 | + return { |
| 365 | + destroy: () => { |
| 366 | + input.removeEventListener('keydown', onKeydown); |
| 367 | + input.removeEventListener('input', onInput); |
| 368 | + document.removeEventListener('pointerdown', onPointerDown, true); |
| 369 | + } |
| 370 | + }; |
| 371 | + } |
| 372 | +
|
298 | 373 | // Segment drag creates a waypoint, then drags it |
299 | 374 | function handleSegmentPointerDown(event: PointerEvent, segmentIndex: number) { |
300 | 375 | event.stopPropagation(); |
|
341 | 416 | } |
342 | 417 | </script> |
343 | 418 |
|
| 419 | +<!-- svelte-ignore a11y_no_static_element_interactions --> |
344 | 420 | <g |
345 | 421 | class:highlighted={highlightColor !== undefined} |
346 | 422 | style={highlightColor !== undefined ? `--highlight-color: ${highlightColor}` : undefined} |
| 423 | + ondblclick={handleEdgeDoubleClick} |
347 | 424 | > |
348 | 425 | <BaseEdge {id} {path} {style} /> |
349 | 426 |
|
|
386 | 463 | class:highlighted={highlightColor !== undefined} |
387 | 464 | /> |
388 | 465 | </g> |
| 466 | + |
| 467 | + {#if label && !isEditingLabel && labelAnchor} |
| 468 | + <text |
| 469 | + x={labelAnchor.x} |
| 470 | + y={labelAnchor.y} |
| 471 | + class="edge-label" |
| 472 | + class:selected |
| 473 | + class:highlighted={highlightColor !== undefined}>{label}</text |
| 474 | + > |
| 475 | + {/if} |
389 | 476 | </g> |
390 | 477 |
|
| 478 | +{#if isEditingLabel && labelAnchor} |
| 479 | + <EdgeLabel x={labelAnchor.x} y={labelAnchor.y} transparent> |
| 480 | + <input |
| 481 | + class="edge-label-input" |
| 482 | + style="--label-height: {EDGE_LABEL.height}px; --label-padding-x: {EDGE_LABEL.paddingX}px; --label-chars: {Math.max(draftLength, EDGE_LABEL.minChars)};" |
| 483 | + value={label} |
| 484 | + placeholder="Label" |
| 485 | + use:labelEditor |
| 486 | + /> |
| 487 | + </EdgeLabel> |
| 488 | +{/if} |
| 489 | + |
391 | 490 | <style> |
392 | 491 | .edge-arrow { |
393 | 492 | fill: var(--edge); |
|
415 | 514 | stroke: var(--highlight-color, var(--accent)) !important; |
416 | 515 | } |
417 | 516 |
|
| 517 | + /* Connection label with a halo in the canvas color, readable on top of wires. |
| 518 | + * Pointer events pass through to the wire, so hovering the label hovers the edge. */ |
| 519 | + .edge-label { |
| 520 | + font-family: var(--font-ui); |
| 521 | + font-size: var(--font-xs); |
| 522 | + fill: var(--text-muted); |
| 523 | + stroke: var(--surface); |
| 524 | + stroke-width: 3px; |
| 525 | + stroke-linejoin: round; |
| 526 | + paint-order: stroke; |
| 527 | + text-anchor: middle; |
| 528 | + dominant-baseline: central; |
| 529 | + pointer-events: none; |
| 530 | + transition: fill 0.15s ease; |
| 531 | + } |
| 532 | +
|
| 533 | + :global(.svelte-flow__edge:hover) .edge-label, |
| 534 | + .edge-label.selected { |
| 535 | + fill: var(--accent); |
| 536 | + } |
| 537 | +
|
| 538 | + .edge-label.highlighted { |
| 539 | + fill: var(--highlight-color, var(--accent)); |
| 540 | + } |
| 541 | +
|
| 542 | + /* Inline label editor: same capsule, active like a dragged waypoint */ |
| 543 | + .edge-label-input { |
| 544 | + box-sizing: border-box; |
| 545 | + height: var(--label-height); |
| 546 | + width: calc(var(--label-chars) * 1ch + 2 * var(--label-padding-x)); |
| 547 | + padding: 0 var(--label-padding-x); |
| 548 | + border: 1.5px solid var(--accent); |
| 549 | + border-radius: calc(var(--label-height) / 2); |
| 550 | + background: var(--surface); |
| 551 | + color: var(--text); |
| 552 | + font-family: var(--font-ui); |
| 553 | + font-size: var(--font-xs); |
| 554 | + text-align: center; |
| 555 | + outline: none; |
| 556 | + } |
| 557 | +
|
| 558 | + .edge-label-input::placeholder { |
| 559 | + color: var(--text-muted); |
| 560 | + } |
| 561 | +
|
418 | 562 | /* Waypoint group - visibility controlled by inline styles */ |
419 | 563 | .waypoint-group { |
420 | 564 | transition: opacity 0.1s ease; |
|
0 commit comments