implement onSelectionChanged

This commit is contained in:
peterkogo
2025-04-30 20:32:06 +02:00
parent adb13e261d
commit 47f9118296
13 changed files with 108 additions and 11 deletions
@@ -1,6 +1,6 @@
<script lang="ts" generics="NodeType extends Node = Node, EdgeType extends Edge = Edge">
import type { Edge, Node } from '$lib/types';
import { getContext, setContext, onDestroy } from 'svelte';
import { getContext, setContext, onDestroy, untrack } from 'svelte';
import type { HTMLAttributes } from 'svelte/elements';
import { ConnectionLineType, PanOnScrollMode } from '@xyflow/system';
@@ -110,6 +110,15 @@
}
} satisfies StoreContext<NodeType, EdgeType>);
// handle selection change
$effect(() => {
const params = { nodes: store.selectedNodes, edges: store.selectedEdges };
untrack(() => props.onselectionchanged)?.(params);
for (const handler of store.selectionChangedHandlers.values()) {
handler(params);
}
});
onDestroy(() => {
store.reset();
});
@@ -52,6 +52,7 @@
onclickconnectstart,
onclickconnectend,
oninit,
onselectionchanged,
clickConnect,
fitView,
fitViewOptions,
@@ -36,7 +36,8 @@ import type {
OnBeforeConnect,
OnBeforeDelete,
IsValidConnection,
OnBeforeReconnect
OnBeforeReconnect,
OnSelectionChanged
} from '$lib/types';
import type { Component } from 'svelte';
@@ -391,6 +392,8 @@ export type SvelteFlowProps<
* If you have custom connection logic its preferred to use this callback over the
* `isValidConnection` prop on the handle component for performance reasons.
*/
/** Toggles ability to make connections via clicking the handles */
clickConnect?: boolean;
isValidConnection?: IsValidConnection;
/** This event handler is called when the user begins to pan or zoom the viewport */
onmovestart?: OnMoveStart;
@@ -434,8 +437,8 @@ export type SvelteFlowProps<
onclickconnectstart?: OnConnectStart;
/** A connection is finished by clicking on a handle */
onclickconnectend?: OnConnectEnd;
/** Toggles ability to make connections via clicking the handles */
clickConnect?: boolean;
/** This handler gets called when the flow is finished initializing */
oninit?: () => void;
/** This event handler gets called when the selected nodes & edges change */
onselectionchanged?: OnSelectionChanged<NodeType, EdgeType>;
};