Merge pull request #3681 from xyflow/svelte-on-edge-creation
feat(svelte) added onedgecreate function
This commit is contained in:
@@ -6,7 +6,8 @@
|
|||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
MarkerType
|
MarkerType,
|
||||||
|
type Connection
|
||||||
} from '@xyflow/svelte';
|
} from '@xyflow/svelte';
|
||||||
|
|
||||||
import '@xyflow/svelte/dist/style.css';
|
import '@xyflow/svelte/dist/style.css';
|
||||||
@@ -143,9 +144,28 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$: console.log('edges', $edges);
|
||||||
|
|
||||||
|
function getEdgeId(connection: Connection) {
|
||||||
|
return `edge-${connection.source}-${connection.target}}`;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlow {nodes} {edges} fitView nodeDragThreshold={2}>
|
<SvelteFlow
|
||||||
|
{nodes}
|
||||||
|
{edges}
|
||||||
|
fitView
|
||||||
|
nodeDragThreshold={2}
|
||||||
|
onedgecreate={(connection) => {
|
||||||
|
console.log('on edge create', connection);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...connection,
|
||||||
|
id: getEdgeId(connection)
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
>
|
||||||
<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,13 @@
|
|||||||
cancelConnection,
|
cancelConnection,
|
||||||
panBy,
|
panBy,
|
||||||
onConnect: (connection) => {
|
onConnect: (connection) => {
|
||||||
addEdge(connection);
|
const edge = $onedgecreate ? $onedgecreate(connection) : connection;
|
||||||
|
|
||||||
|
if (!edge) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
addEdge(edge);
|
||||||
// @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)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ export function isEdgeVisible({ sourceNode, targetNode, width, height, transform
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getEdgeId = ({ source, sourceHandle, target, targetHandle }: Connection | EdgeBase): string =>
|
const getEdgeId = ({ source, sourceHandle, target, targetHandle }: Connection | EdgeBase): string =>
|
||||||
`xyflow__edge-${source}${sourceHandle || ''}-${target}${targetHandle || ''}`;
|
`xy-edge__${source}${sourceHandle || ''}-${target}${targetHandle || ''}`;
|
||||||
|
|
||||||
const connectionExists = (edge: EdgeBase, edges: EdgeBase[]) => {
|
const connectionExists = (edge: EdgeBase, edges: EdgeBase[]) => {
|
||||||
return edges.some(
|
return edges.some(
|
||||||
@@ -148,6 +148,14 @@ export const addEdgeBase = <EdgeType extends EdgeBase>(
|
|||||||
return edges;
|
return edges;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (edge.sourceHandle === null) {
|
||||||
|
delete edge.sourceHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (edge.targetHandle === null) {
|
||||||
|
delete edge.targetHandle;
|
||||||
|
}
|
||||||
|
|
||||||
return edges.concat(edge);
|
return edges.concat(edge);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user