feat(svelte) added onedgecreate function

This commit is contained in:
Peter
2023-11-28 15:48:19 +01:00
parent 1eea7209a3
commit 3f94dfa84c
7 changed files with 34 additions and 6 deletions
@@ -145,7 +145,16 @@
]); ]);
</script> </script>
<SvelteFlow {nodes} {edges} fitView nodeDragThreshold={2}> <SvelteFlow
{nodes}
{edges}
fitView
nodeDragThreshold={2}
onedgecreate={(e) => {
console.log('on edge create', e);
return e;
}}
>
<Controls /> <Controls />
<Background variant={BackgroundVariant.Dots} /> <Background variant={BackgroundVariant.Dots} />
<MiniMap /> <MiniMap />
@@ -55,6 +55,7 @@
isValidConnection, isValidConnection,
lib, lib,
addEdge, addEdge,
onedgecreate,
panBy, panBy,
cancelConnection, cancelConnection,
updateConnection, updateConnection,
@@ -80,8 +81,16 @@
cancelConnection, cancelConnection,
panBy, panBy,
onConnect: (connection) => { onConnect: (connection) => {
addEdge(connection); if ($onedgecreate) {
const modifiedConnection = $onedgecreate(connection);
if (modifiedConnection) {
addEdge(modifiedConnection);
dispatch('connect', { connection });
}
return;
}
addEdge(connection);
// @todo: should we change/ improve the stuff we are passing here? // @todo: should we change/ improve the stuff we are passing here?
// instead of source/target we could pass fromNodeId, fromHandleId, etc // instead of source/target we could pass fromNodeId, fromHandleId, etc
dispatch('connect', { connection }); dispatch('connect', { connection });
@@ -71,6 +71,7 @@
export let autoPanOnNodeDrag: $$Props['autoPanOnNodeDrag'] = true; export let autoPanOnNodeDrag: $$Props['autoPanOnNodeDrag'] = true;
export let onerror: $$Props['onerror'] = undefined; export let onerror: $$Props['onerror'] = undefined;
export let ondelete: $$Props['ondelete'] = undefined; export let ondelete: $$Props['ondelete'] = undefined;
export let onedgecreate: $$Props['onedgecreate'] = undefined;
export let attributionPosition: $$Props['attributionPosition'] = undefined; export let attributionPosition: $$Props['attributionPosition'] = undefined;
export let proOptions: $$Props['proOptions'] = undefined; export let proOptions: $$Props['proOptions'] = undefined;
export let defaultEdgeOptions: $$Props['defaultEdgeOptions'] = undefined; export let defaultEdgeOptions: $$Props['defaultEdgeOptions'] = undefined;
@@ -149,6 +150,7 @@
autoPanOnNodeDrag, autoPanOnNodeDrag,
onerror, onerror,
ondelete, ondelete,
onedgecreate,
connectionMode, connectionMode,
nodeDragThreshold nodeDragThreshold
}; };
@@ -26,7 +26,8 @@ import type {
EdgeTypes, EdgeTypes,
DefaultEdgeOptions, DefaultEdgeOptions,
FitViewOptions, FitViewOptions,
OnDelete OnDelete,
OnEdgeCreate
} from '$lib/types'; } from '$lib/types';
import type { Writable } from 'svelte/store'; import type { Writable } from 'svelte/store';
@@ -89,4 +90,6 @@ export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
onMoveEnd?: OnMoveEnd; onMoveEnd?: OnMoveEnd;
onerror?: OnError; onerror?: OnError;
ondelete?: OnDelete; ondelete?: OnDelete;
onedgecreate?: OnEdgeCreate;
}; };
@@ -64,6 +64,7 @@ export type UpdatableStoreProps = {
connectionMode?: UnwrapWritable<SvelteFlowStore['connectionMode']>; connectionMode?: UnwrapWritable<SvelteFlowStore['connectionMode']>;
onerror?: UnwrapWritable<SvelteFlowStore['onerror']>; onerror?: UnwrapWritable<SvelteFlowStore['onerror']>;
ondelete?: UnwrapWritable<SvelteFlowStore['ondelete']>; ondelete?: UnwrapWritable<SvelteFlowStore['ondelete']>;
onedgecreate?: UnwrapWritable<SvelteFlowStore['onedgecreate']>;
nodeDragThreshold?: UnwrapWritable<SvelteFlowStore['nodeDragThreshold']>; nodeDragThreshold?: UnwrapWritable<SvelteFlowStore['nodeDragThreshold']>;
}; };
@@ -35,7 +35,8 @@ import type {
Node, Node,
Edge, Edge,
FitViewOptions, FitViewOptions,
OnDelete OnDelete,
OnEdgeCreate
} from '$lib/types'; } from '$lib/types';
import { createNodesStore, createEdgesStore } from './utils'; import { createNodesStore, createEdgesStore } from './utils';
import { initConnectionProps, type ConnectionProps } from './derived-connection-props'; import { initConnectionProps, type ConnectionProps } from './derived-connection-props';
@@ -130,6 +131,7 @@ export const getInitialStore = ({
lib: readable<string>('svelte'), lib: readable<string>('svelte'),
onlyRenderVisibleElements: writable<boolean>(false), onlyRenderVisibleElements: writable<boolean>(false),
onerror: writable<OnError>(devWarn), onerror: writable<OnError>(devWarn),
ondelete: writable<OnDelete>(undefined) ondelete: writable<OnDelete>(undefined),
onedgecreate: writable<OnEdgeCreate>(undefined)
}; };
}; };
+3 -1
View File
@@ -4,7 +4,8 @@ import type {
HandleType, HandleType,
Position, Position,
XYPosition, XYPosition,
ConnectingHandle ConnectingHandle,
Connection
} from '@xyflow/system'; } from '@xyflow/system';
import type { Node } from './nodes'; import type { Node } from './nodes';
@@ -35,3 +36,4 @@ export type HandleComponentProps = {
export type FitViewOptions = FitViewOptionsBase<Node>; export type FitViewOptions = FitViewOptionsBase<Node>;
export type OnDelete = (params: { nodes: Node[]; edges: Edge[] }) => void; export type OnDelete = (params: { nodes: Node[]; edges: Edge[] }) => void;
export type OnEdgeCreate = (connection: Connection) => Edge | Connection | void;