fix defaultEdge options and add elevateEdgesOnSelect

This commit is contained in:
peterkogo
2025-04-15 15:56:57 +02:00
parent 1fa3b50366
commit 4a42a1a2e2
6 changed files with 47 additions and 11 deletions
@@ -2,9 +2,8 @@
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
import type { Edge } from '$lib/types'; import type { Edge } from '$lib/types';
import { XYHandle, type HandleType, type XYPosition } from '@xyflow/system'; import { XYHandle, type HandleType, type XYPosition } from '@xyflow/system';
import { getContext, type Snippet } from 'svelte'; import { getContext } from 'svelte';
import type { ClassValue } from 'svelte/elements'; import { EdgeLabel } from '../EdgeLabel';
import EdgeLabel from '../EdgeLabel/EdgeLabel.svelte';
import type { EdgeReconnectAnchorProps } from './types'; import type { EdgeReconnectAnchorProps } from './types';
let { let {
@@ -44,7 +44,9 @@
zIndex, zIndex,
class: className, class: className,
ariaLabel ariaLabel
} = $derived(store.defaultEdgeOptions ? { ...store.defaultEdgeOptions, ...edge } : edge); } = $derived(edge);
$inspect(edge);
const { id } = edge; const { id } = edge;
setContext('svelteflow__edge_id', id); setContext('svelteflow__edge_id', id);
@@ -73,6 +73,7 @@
style, style,
defaultEdgeOptions, defaultEdgeOptions,
elevateNodesOnSelect, elevateNodesOnSelect,
elevateEdgesOnSelect,
nodesDraggable, nodesDraggable,
nodesConnectable, nodesConnectable,
elementsSelectable, elementsSelectable,
@@ -337,6 +337,19 @@ export type SvelteFlowProps = NodeEvents &
* @default true * @default true
*/ */
elevateNodesOnSelect?: boolean; elevateNodesOnSelect?: boolean;
/**
* Enabling this option will raise the z-index of edges when they are selected,
* or when the connected nodes are selected.
* @default true
*/
elevateEdgesOnSelect?: boolean;
/**
* This callback can be used to validate a new connection
*
* If you return `false`, the edge will not be added to your flow.
* If you have custom connection logic its preferred to use this callback over the
* `isValidConnection` prop on the handle component for performance reasons.
*/
isValidConnection?: IsValidConnection; isValidConnection?: IsValidConnection;
/** This event handler is called when the user begins to pan or zoom the viewport */ /** This event handler is called when the user begins to pan or zoom the viewport */
onmovestart?: OnMoveStart; onmovestart?: OnMoveStart;
@@ -151,13 +151,22 @@ export const getInitialStore = (signals: StoreSignals) => {
nodeLookup, nodeLookup,
connectionMode, connectionMode,
onerror, onerror,
onlyRenderVisibleElements onlyRenderVisibleElements,
defaultEdgeOptions
} = this; } = this;
let visibleNodes: Map<string, InternalNode>; let visibleNodes: Map<string, InternalNode>;
let visibleEdges: Map<string, EdgeLayouted>; let visibleEdges: Map<string, EdgeLayouted>;
const options = { edges, previousEdges, nodeLookup, connectionMode, onerror }; const options = {
edges,
defaultEdgeOptions,
previousEdges,
nodeLookup,
connectionMode,
elevateEdgesOnSelect: signals.props.elevateEdgesOnSelect ?? true,
onerror
};
if (onlyRenderVisibleElements) { if (onlyRenderVisibleElements) {
// We only subscribe to viewport, width, height if onlyRenderVisibleElements is true // We only subscribe to viewport, width, height if onlyRenderVisibleElements is true
@@ -1,4 +1,4 @@
import type { Edge, EdgeLayouted, InternalNode } from '$lib/types'; import type { DefaultEdgeOptions, Edge, EdgeLayouted, InternalNode } from '$lib/types';
import { import {
ConnectionMode, ConnectionMode,
getEdgePosition, getEdgePosition,
@@ -27,6 +27,8 @@ export function getVisibleNodes(
export interface EdgeLayoutBaseOptions { export interface EdgeLayoutBaseOptions {
edges: Edge[]; edges: Edge[];
defaultEdgeOptions: DefaultEdgeOptions;
elevateEdgesOnSelect: boolean;
previousEdges: Map<string, EdgeLayouted>; previousEdges: Map<string, EdgeLayouted>;
nodeLookup: NodeLookup; nodeLookup: NodeLookup;
connectionMode: ConnectionMode; connectionMode: ConnectionMode;
@@ -52,7 +54,16 @@ export interface EdgeLayoutOnlyVisibleOptions extends EdgeLayoutBaseOptions {
export type EdgeLayoutOptions = EdgeLayoutAllOptions | EdgeLayoutOnlyVisibleOptions; export type EdgeLayoutOptions = EdgeLayoutAllOptions | EdgeLayoutOnlyVisibleOptions;
export function getLayoutedEdges(options: EdgeLayoutOptions): Map<string, EdgeLayouted> { export function getLayoutedEdges(options: EdgeLayoutOptions): Map<string, EdgeLayouted> {
const { edges, nodeLookup, previousEdges, connectionMode, onerror, onlyRenderVisible } = options; const {
edges,
defaultEdgeOptions,
nodeLookup,
previousEdges,
connectionMode,
onerror,
onlyRenderVisible,
elevateEdgesOnSelect
} = options;
const layoutedEdges = new Map<string, EdgeLayouted>(); const layoutedEdges = new Map<string, EdgeLayouted>();
for (const edge of edges) { for (const edge of edges) {
const sourceNode = nodeLookup.get(edge.source); const sourceNode = nodeLookup.get(edge.source);
@@ -107,15 +118,16 @@ export function getLayoutedEdges(options: EdgeLayoutOptions): Map<string, EdgeLa
if (edgePosition) { if (edgePosition) {
layoutedEdges.set(edge.id, { layoutedEdges.set(edge.id, {
...defaultEdgeOptions,
...edge, ...edge,
...edgePosition,
zIndex: getElevatedEdgeZIndex({ zIndex: getElevatedEdgeZIndex({
selected: edge.selected, selected: edge.selected,
zIndex: edge.zIndex, zIndex: edge.zIndex ?? defaultEdgeOptions.zIndex,
sourceNode, sourceNode,
targetNode, targetNode,
elevateOnSelect: false elevateOnSelect: elevateEdgesOnSelect
}), }),
...edgePosition,
sourceNode, sourceNode,
targetNode, targetNode,
edge edge