refactor(svelte): simplify nodes and edges store handling
This commit is contained in:
@@ -1,22 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onDestroy, setContext } from 'svelte';
|
import { onDestroy } from 'svelte';
|
||||||
|
|
||||||
import { createStore, key } from '$lib/store';
|
import { createStoreContext } from '$lib/store';
|
||||||
import type { SvelteFlowProviderProps } from './types';
|
|
||||||
|
|
||||||
type $$Props = SvelteFlowProviderProps;
|
const store = createStoreContext();
|
||||||
|
|
||||||
export let nodes: $$Props['nodes'];
|
|
||||||
export let edges: $$Props['edges'];
|
|
||||||
|
|
||||||
const store = createStore({
|
|
||||||
nodes,
|
|
||||||
edges,
|
|
||||||
});
|
|
||||||
|
|
||||||
setContext(key, {
|
|
||||||
getStore: () => store
|
|
||||||
});
|
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
store.reset();
|
store.reset();
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
export { default as SvelteFlowProvider } from './SvelteFlowProvider.svelte';
|
export { default as SvelteFlowProvider } from './SvelteFlowProvider.svelte';
|
||||||
export type { SvelteFlowProviderProps } from './types';
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import type { Writable } from 'svelte/store';
|
|
||||||
|
|
||||||
import type { Edge } from '$lib/types';
|
|
||||||
import type { createNodes } from '$lib/utils';
|
|
||||||
|
|
||||||
export type SvelteFlowProviderProps = {
|
|
||||||
nodes: ReturnType<typeof createNodes>;
|
|
||||||
edges: Writable<Edge[]>;
|
|
||||||
};
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
import { onMount, hasContext } from 'svelte';
|
||||||
import cc from 'classcat';
|
import cc from 'classcat';
|
||||||
import { PanOnScrollMode, type CoordinateExtent, type Viewport } from '@xyflow/system';
|
import { PanOnScrollMode, type Viewport } from '@xyflow/system';
|
||||||
|
|
||||||
import { Zoom } from '$lib/container/Zoom';
|
import { Zoom } from '$lib/container/Zoom';
|
||||||
import { Pane } from '$lib/container/Pane';
|
import { Pane } from '$lib/container/Pane';
|
||||||
@@ -13,13 +13,15 @@
|
|||||||
import { KeyHandler } from '$lib/components/KeyHandler';
|
import { KeyHandler } from '$lib/components/KeyHandler';
|
||||||
import { ConnectionLine } from '$lib/components/ConnectionLine';
|
import { ConnectionLine } from '$lib/components/ConnectionLine';
|
||||||
import { Attribution } from '$lib/components/Attribution';
|
import { Attribution } from '$lib/components/Attribution';
|
||||||
import { useStore } from '$lib/store';
|
import { key, useStore, createStoreContext } from '$lib/store';
|
||||||
import type { SvelteFlowProps } from './types';
|
import type { SvelteFlowProps } from './types';
|
||||||
import type { EdgeTypes, NodeTypes } from '$lib/types';
|
import { updateStore, updateStoreByKeys, type UpdatableStoreProps } from './utils';
|
||||||
|
|
||||||
type $$Props = SvelteFlowProps;
|
type $$Props = SvelteFlowProps;
|
||||||
|
|
||||||
export let id = '1';
|
export let id = '1';
|
||||||
|
export let nodes: $$Props['nodes'];
|
||||||
|
export let edges: $$Props['edges'];
|
||||||
export let fitView: $$Props['fitView'] = undefined;
|
export let fitView: $$Props['fitView'] = undefined;
|
||||||
export let minZoom: $$Props['minZoom'] = undefined;
|
export let minZoom: $$Props['minZoom'] = undefined;
|
||||||
export let maxZoom: $$Props['maxZoom'] = undefined;
|
export let maxZoom: $$Props['maxZoom'] = undefined;
|
||||||
@@ -53,10 +55,10 @@
|
|||||||
export let style: $$Props['style'] = undefined;
|
export let style: $$Props['style'] = undefined;
|
||||||
let className: $$Props['class'] = undefined;
|
let className: $$Props['class'] = undefined;
|
||||||
export { className as class };
|
export { className as class };
|
||||||
|
|
||||||
let domNode: HTMLDivElement;
|
let domNode: HTMLDivElement;
|
||||||
|
|
||||||
$: flowId = id;
|
const store = hasContext(key) ? useStore() : createStoreContext();
|
||||||
const store = useStore();
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const { width, height } = domNode.getBoundingClientRect();
|
const { width, height } = domNode.getBoundingClientRect();
|
||||||
@@ -64,13 +66,19 @@
|
|||||||
store.height.set(height);
|
store.height.set(height);
|
||||||
store.domNode.set(domNode);
|
store.domNode.set(domNode);
|
||||||
|
|
||||||
updateStore({
|
store.syncNodeStores(nodes)
|
||||||
|
store.syncEdgeStores(edges)
|
||||||
|
|
||||||
|
if (fitView !== undefined) {
|
||||||
|
store.fitViewOnInit.set(fitView);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStore(store, {
|
||||||
nodeTypes,
|
nodeTypes,
|
||||||
edgeTypes,
|
edgeTypes,
|
||||||
minZoom,
|
minZoom,
|
||||||
maxZoom,
|
maxZoom,
|
||||||
translateExtent,
|
translateExtent,
|
||||||
fitView
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -78,70 +86,32 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// this updates the store for simple changes
|
||||||
|
// where the prop names equals the store name
|
||||||
$: {
|
$: {
|
||||||
const updatableProps = {
|
const updatableProps: UpdatableStoreProps = {
|
||||||
flowId,
|
flowId: id,
|
||||||
connectionLineType,
|
connectionLineType,
|
||||||
connectionRadius,
|
connectionRadius,
|
||||||
selectionMode,
|
selectionMode,
|
||||||
snapGrid,
|
snapGrid,
|
||||||
isValidConnection,
|
|
||||||
defaultMarkerColor,
|
defaultMarkerColor,
|
||||||
nodesDraggable,
|
nodesDraggable,
|
||||||
nodesConnectable,
|
nodesConnectable,
|
||||||
elementsSelectable,
|
elementsSelectable,
|
||||||
|
isValidConnection,
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(updatableProps).forEach(prop => {
|
updateStoreByKeys(store, updatableProps);
|
||||||
// @ts-ignore
|
|
||||||
if (updatableProps[prop] !== undefined) {
|
|
||||||
// @ts-ignore
|
|
||||||
store[prop].set(updatableProps[prop]);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateStore({ nodeTypes, edgeTypes, minZoom, maxZoom, translateExtent, fitView }: {
|
$: updateStore(store, {
|
||||||
nodeTypes?: NodeTypes,
|
|
||||||
edgeTypes?: EdgeTypes,
|
|
||||||
minZoom?: number,
|
|
||||||
maxZoom?: number,
|
|
||||||
translateExtent?: CoordinateExtent,
|
|
||||||
fitView?: boolean
|
|
||||||
}) {
|
|
||||||
if (nodeTypes !== undefined) {
|
|
||||||
store.setNodeTypes(nodeTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (edgeTypes !== undefined) {
|
|
||||||
store.setEdgeTypes(edgeTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minZoom !== undefined) {
|
|
||||||
store.setMinZoom(minZoom);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (maxZoom !== undefined) {
|
|
||||||
store.setMaxZoom(maxZoom);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (translateExtent !== undefined) {
|
|
||||||
store.setTranslateExtent(translateExtent)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fitView !== undefined) {
|
|
||||||
store.fitViewOnInit.set(fitView);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$: updateStore({
|
|
||||||
nodeTypes,
|
nodeTypes,
|
||||||
edgeTypes,
|
edgeTypes,
|
||||||
minZoom,
|
minZoom,
|
||||||
maxZoom,
|
maxZoom,
|
||||||
translateExtent,
|
translateExtent
|
||||||
fitView
|
});
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -16,9 +16,12 @@ import type {
|
|||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { Edge, Node, NodeTypes, KeyDefinition, EdgeTypes } from '$lib/types';
|
import type { Edge, Node, NodeTypes, KeyDefinition, EdgeTypes } from '$lib/types';
|
||||||
|
import type { Writable } from 'svelte/store';
|
||||||
|
|
||||||
export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
|
export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
|
||||||
id?: string;
|
id?: string;
|
||||||
|
nodes: Writable<Node[]>;
|
||||||
|
edges: Writable<Edge[]>;
|
||||||
nodeTypes?: NodeTypes;
|
nodeTypes?: NodeTypes;
|
||||||
edgeTypes?: EdgeTypes;
|
edgeTypes?: EdgeTypes;
|
||||||
selectionKey?: KeyDefinition;
|
selectionKey?: KeyDefinition;
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import type { SvelteFlowStore } from '$lib/store/types';
|
||||||
|
import type { EdgeTypes, NodeTypes } from '$lib/types';
|
||||||
|
import type { CoordinateExtent } from '@xyflow/system';
|
||||||
|
import type { Writable } from 'svelte/store';
|
||||||
|
|
||||||
|
// this is helper function for updating the store
|
||||||
|
// for props where we need to call a specific store action
|
||||||
|
export function updateStore(
|
||||||
|
store: SvelteFlowStore,
|
||||||
|
{
|
||||||
|
nodeTypes,
|
||||||
|
edgeTypes,
|
||||||
|
minZoom,
|
||||||
|
maxZoom,
|
||||||
|
translateExtent
|
||||||
|
}: {
|
||||||
|
nodeTypes?: NodeTypes;
|
||||||
|
edgeTypes?: EdgeTypes;
|
||||||
|
minZoom?: number;
|
||||||
|
maxZoom?: number;
|
||||||
|
translateExtent?: CoordinateExtent;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
if (nodeTypes !== undefined) {
|
||||||
|
store.setNodeTypes(nodeTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (edgeTypes !== undefined) {
|
||||||
|
store.setEdgeTypes(edgeTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minZoom !== undefined) {
|
||||||
|
store.setMinZoom(minZoom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxZoom !== undefined) {
|
||||||
|
store.setMaxZoom(maxZoom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (translateExtent !== undefined) {
|
||||||
|
store.setTranslateExtent(translateExtent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getKeys = <T extends object>(obj: T) => Object.keys(obj) as Array<keyof T>;
|
||||||
|
|
||||||
|
type UnwrapWritable<T> = T extends Writable<infer U> ? U : T;
|
||||||
|
|
||||||
|
// @todo there must be a better way to define the types here..
|
||||||
|
export type UpdatableStoreProps = {
|
||||||
|
flowId?: UnwrapWritable<SvelteFlowStore['flowId']>;
|
||||||
|
connectionLineType?: UnwrapWritable<SvelteFlowStore['connectionLineType']>;
|
||||||
|
connectionRadius?: UnwrapWritable<SvelteFlowStore['connectionRadius']>;
|
||||||
|
selectionMode?: UnwrapWritable<SvelteFlowStore['selectionMode']>;
|
||||||
|
snapGrid?: UnwrapWritable<SvelteFlowStore['snapGrid']>;
|
||||||
|
defaultMarkerColor?: UnwrapWritable<SvelteFlowStore['defaultMarkerColor']>;
|
||||||
|
nodesDraggable?: UnwrapWritable<SvelteFlowStore['nodesDraggable']>;
|
||||||
|
nodesConnectable?: UnwrapWritable<SvelteFlowStore['nodesConnectable']>;
|
||||||
|
elementsSelectable?: UnwrapWritable<SvelteFlowStore['elementsSelectable']>;
|
||||||
|
isValidConnection?: UnwrapWritable<SvelteFlowStore['isValidConnection']>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function updateStoreByKeys(store: SvelteFlowStore, keys: UpdatableStoreProps) {
|
||||||
|
getKeys(keys).forEach((prop) => {
|
||||||
|
const update = keys[prop];
|
||||||
|
if (update !== undefined) {
|
||||||
|
// @ts-ignore
|
||||||
|
store[prop].set(update);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getContext } from 'svelte';
|
import { getContext, setContext } from 'svelte';
|
||||||
import { derived, get } from 'svelte/store';
|
import { derived, get } from 'svelte/store';
|
||||||
import {
|
import {
|
||||||
internalsSymbol,
|
internalsSymbol,
|
||||||
@@ -25,19 +25,15 @@ import {
|
|||||||
initConnectionData,
|
initConnectionData,
|
||||||
initialEdgeTypes,
|
initialEdgeTypes,
|
||||||
initialNodeTypes,
|
initialNodeTypes,
|
||||||
initialStoreState
|
getInitialStore
|
||||||
} from './initial-store';
|
} from './initial-store';
|
||||||
import type { SvelteFlowStore } from './types';
|
import type { SvelteFlowStore } from './types';
|
||||||
|
import { syncNodeStores, syncEdgeStores } from './utils';
|
||||||
|
|
||||||
export const key = Symbol();
|
export const key = Symbol();
|
||||||
|
|
||||||
type CreateStoreParams = Pick<SvelteFlowStore, 'nodes' | 'edges'>;
|
export function createStore(): SvelteFlowStore {
|
||||||
|
const store = getInitialStore();
|
||||||
export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|
||||||
const store = {
|
|
||||||
...initialStoreState,
|
|
||||||
...params
|
|
||||||
};
|
|
||||||
|
|
||||||
function setNodeTypes(nodeTypes: NodeTypes) {
|
function setNodeTypes(nodeTypes: NodeTypes) {
|
||||||
store.nodeTypes.set({
|
store.nodeTypes.set({
|
||||||
@@ -55,6 +51,7 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
|
|
||||||
function addEdge(edgeParams: Edge | Connection) {
|
function addEdge(edgeParams: Edge | Connection) {
|
||||||
const edges = get(store.edges);
|
const edges = get(store.edges);
|
||||||
|
|
||||||
store.edges.set(addEdgeUtil(edgeParams, edges));
|
store.edges.set(addEdgeUtil(edgeParams, edges));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -348,6 +345,8 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
|||||||
),
|
),
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
|
syncNodeStores: (nodes) => syncNodeStores(store.nodes, nodes),
|
||||||
|
syncEdgeStores: (edges) => syncEdgeStores(store.edges, edges),
|
||||||
setNodeTypes,
|
setNodeTypes,
|
||||||
setEdgeTypes,
|
setEdgeTypes,
|
||||||
addEdge,
|
addEdge,
|
||||||
@@ -380,3 +379,13 @@ export function useStore(): SvelteFlowStore {
|
|||||||
|
|
||||||
return store.getStore();
|
return store.getStore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createStoreContext() {
|
||||||
|
const store = createStore();
|
||||||
|
|
||||||
|
setContext(key, {
|
||||||
|
getStore: () => store
|
||||||
|
});
|
||||||
|
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ import BezierEdge from '$lib/components/edges/BezierEdge.svelte';
|
|||||||
import StraightEdge from '$lib/components/edges/StraightEdge.svelte';
|
import StraightEdge from '$lib/components/edges/StraightEdge.svelte';
|
||||||
import SmoothStepEdge from '$lib/components/edges/SmoothStepEdge.svelte';
|
import SmoothStepEdge from '$lib/components/edges/SmoothStepEdge.svelte';
|
||||||
import StepEdge from '$lib/components/edges/StepEdge.svelte';
|
import StepEdge from '$lib/components/edges/StepEdge.svelte';
|
||||||
import type { ConnectionData, NodeTypes, EdgeTypes, EdgeLayouted, Edge, Node } from '$lib/types';
|
import type { ConnectionData, NodeTypes, EdgeTypes, EdgeLayouted } from '$lib/types';
|
||||||
|
import { createNodes, createEdges } from './utils';
|
||||||
|
|
||||||
export const initConnectionData = {
|
export const initConnectionData = {
|
||||||
connectionStartHandle: null,
|
connectionStartHandle: null,
|
||||||
@@ -42,10 +43,10 @@ export const initialEdgeTypes = {
|
|||||||
step: StepEdge
|
step: StepEdge
|
||||||
};
|
};
|
||||||
|
|
||||||
export const initialStoreState = {
|
export const getInitialStore = () => ({
|
||||||
flowId: writable<string | null>(null),
|
flowId: writable<string | null>(null),
|
||||||
nodes: writable<Node[]>([]),
|
nodes: createNodes([]),
|
||||||
edges: writable<Edge[]>([]),
|
edges: createEdges([]),
|
||||||
edgesLayouted: readable<EdgeLayouted[]>([]),
|
edgesLayouted: readable<EdgeLayouted[]>([]),
|
||||||
height: writable<number>(500),
|
height: writable<number>(500),
|
||||||
width: writable<number>(500),
|
width: writable<number>(500),
|
||||||
@@ -82,4 +83,4 @@ export const initialStoreState = {
|
|||||||
markers: readable<MarkerProps[]>([]),
|
markers: readable<MarkerProps[]>([]),
|
||||||
defaultMarkerColor: writable<string>('#b1b1b7'),
|
defaultMarkerColor: writable<string>('#b1b1b7'),
|
||||||
lib: readable<string>('svelte')
|
lib: readable<string>('svelte')
|
||||||
};
|
});
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ import type {
|
|||||||
UpdateConnection
|
UpdateConnection
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { initialStoreState } from './initial-store';
|
import type { getInitialStore } from './initial-store';
|
||||||
import type { Node, Edge, NodeTypes, EdgeTypes, FitViewOptions } from '$lib/types';
|
import type { Node, Edge, NodeTypes, EdgeTypes, FitViewOptions } from '$lib/types';
|
||||||
|
|
||||||
export type SvelteFlowStoreActions = {
|
export type SvelteFlowStoreActions = {
|
||||||
|
syncNodeStores: (nodesStore: Writable<Node[]>) => void;
|
||||||
|
syncEdgeStores: (edgeStore: Writable<Edge[]>) => void;
|
||||||
setNodeTypes: (nodeTypes: NodeTypes) => void;
|
setNodeTypes: (nodeTypes: NodeTypes) => void;
|
||||||
setEdgeTypes: (edgeTypes: EdgeTypes) => void;
|
setEdgeTypes: (edgeTypes: EdgeTypes) => void;
|
||||||
addEdge: (edge: Edge | Connection) => void;
|
addEdge: (edge: Edge | Connection) => void;
|
||||||
@@ -33,9 +35,6 @@ export type SvelteFlowStoreActions = {
|
|||||||
reset(): void;
|
reset(): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SvelteFlowStoreState = typeof initialStoreState & {
|
export type SvelteFlowStoreState = ReturnType<typeof getInitialStore>;
|
||||||
nodes: Writable<Node[]>;
|
|
||||||
edges: Writable<Edge[]>;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type SvelteFlowStore = SvelteFlowStoreState & SvelteFlowStoreActions;
|
export type SvelteFlowStore = SvelteFlowStoreState & SvelteFlowStoreActions;
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
import {
|
||||||
|
writable,
|
||||||
|
type Unsubscriber,
|
||||||
|
type Subscriber,
|
||||||
|
type Updater,
|
||||||
|
type Writable,
|
||||||
|
get
|
||||||
|
} from 'svelte/store';
|
||||||
|
import {
|
||||||
|
isNumeric,
|
||||||
|
getNodePositionWithOrigin,
|
||||||
|
internalsSymbol,
|
||||||
|
type XYZPosition
|
||||||
|
} from '@xyflow/system';
|
||||||
|
|
||||||
|
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, Node } from '$lib/types';
|
||||||
|
|
||||||
|
export function syncNodeStores(
|
||||||
|
nodesStore: ReturnType<typeof createNodes>,
|
||||||
|
userNodesStore: Writable<Node[]>
|
||||||
|
) {
|
||||||
|
const nodesStoreSetter = nodesStore.set;
|
||||||
|
const userNodesStoreSetter = userNodesStore.set;
|
||||||
|
|
||||||
|
let val = get(userNodesStore);
|
||||||
|
nodesStore.set(val);
|
||||||
|
|
||||||
|
const _set = (nds: Node[]) => {
|
||||||
|
const updatedNodes = nodesStoreSetter(nds);
|
||||||
|
val = updatedNodes;
|
||||||
|
|
||||||
|
userNodesStoreSetter(val);
|
||||||
|
|
||||||
|
return updatedNodes;
|
||||||
|
};
|
||||||
|
|
||||||
|
nodesStore.set = userNodesStore.set = _set;
|
||||||
|
nodesStore.update = userNodesStore.update = (fn: (nds: Node[]) => Node[]) => _set(fn(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function syncEdgeStores(
|
||||||
|
edgesStore: ReturnType<typeof createEdges>,
|
||||||
|
userEdgesStore: Writable<Edge[]>
|
||||||
|
) {
|
||||||
|
const nodesStoreSetter = edgesStore.set;
|
||||||
|
const userEdgesStoreSetter = userEdgesStore.set;
|
||||||
|
|
||||||
|
let val = get(userEdgesStore);
|
||||||
|
edgesStore.set(val);
|
||||||
|
|
||||||
|
const _set = (eds: Edge[]) => {
|
||||||
|
nodesStoreSetter(eds);
|
||||||
|
userEdgesStoreSetter(eds);
|
||||||
|
val = eds;
|
||||||
|
};
|
||||||
|
|
||||||
|
edgesStore.set = userEdgesStore.set = _set;
|
||||||
|
edgesStore.update = userEdgesStore.update = (fn: (nds: Edge[]) => Edge[]) => _set(fn(val));
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createNodes = (
|
||||||
|
nodes: Node[]
|
||||||
|
): {
|
||||||
|
subscribe: (this: void, run: Subscriber<Node[]>) => Unsubscriber;
|
||||||
|
update: (this: void, updater: Updater<Node[]>) => void;
|
||||||
|
set: (this: void, value: Node[]) => Node[];
|
||||||
|
setDefaultOptions: (opts: DefaultNodeOptions) => void;
|
||||||
|
} => {
|
||||||
|
const { subscribe, set, update } = writable<Node[]>([]);
|
||||||
|
let value = nodes;
|
||||||
|
let defaults = {};
|
||||||
|
|
||||||
|
const _set = (nds: Node[]): Node[] => {
|
||||||
|
const parentNodes: Record<string, boolean> = {};
|
||||||
|
|
||||||
|
const nextNodes = nds.map((n) => {
|
||||||
|
const node: Node = { ...defaults, ...n, positionAbsolute: n.position };
|
||||||
|
const z = (isNumeric(node.zIndex) ? node.zIndex : 0) + (node.selected ? 1 : 0);
|
||||||
|
|
||||||
|
if (node.parentNode) {
|
||||||
|
parentNodes[node.parentNode] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(node, internalsSymbol, {
|
||||||
|
value: {
|
||||||
|
handleBounds: node?.[internalsSymbol]?.handleBounds,
|
||||||
|
z
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
|
||||||
|
const nodesWithPositions = nextNodes.map((node) => {
|
||||||
|
if (node.parentNode && !parentNodes[node.parentNode]) {
|
||||||
|
throw new Error(`Parent node ${node.parentNode} not found`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.parentNode || parentNodes?.[node.id]) {
|
||||||
|
const { x, y, z } = calculateXYZPosition(node, nextNodes, {
|
||||||
|
...node.position,
|
||||||
|
z: node[internalsSymbol]?.z ?? 0
|
||||||
|
});
|
||||||
|
|
||||||
|
node.positionAbsolute = {
|
||||||
|
x,
|
||||||
|
y
|
||||||
|
};
|
||||||
|
|
||||||
|
node[internalsSymbol]!.z = z;
|
||||||
|
|
||||||
|
if (parentNodes?.[node.id]) {
|
||||||
|
node[internalsSymbol]!.isParent = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
|
||||||
|
value = nodesWithPositions;
|
||||||
|
|
||||||
|
set(value);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
const _update: typeof update = (fn: (nds: Node[]) => Node[]) => _set(fn(value));
|
||||||
|
|
||||||
|
const setDefaultOptions = (options: DefaultNodeOptions) => {
|
||||||
|
defaults = options;
|
||||||
|
};
|
||||||
|
|
||||||
|
_set(value);
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
set: _set,
|
||||||
|
update: _update,
|
||||||
|
setDefaultOptions
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createEdges = (
|
||||||
|
edges: Edge[],
|
||||||
|
defaultOptions?: DefaultEdgeOptions
|
||||||
|
): Writable<Edge[]> & { setDefaultOptions: (opts: DefaultEdgeOptions) => void } => {
|
||||||
|
const { subscribe, set, update } = writable<Edge[]>([]);
|
||||||
|
let value = edges;
|
||||||
|
let defaults = defaultOptions || {};
|
||||||
|
|
||||||
|
const _set: typeof set = (eds: Edge[]) => {
|
||||||
|
const nextEdges = defaults ? eds.map((edge) => ({ ...defaults, ...edge })) : eds;
|
||||||
|
value = nextEdges;
|
||||||
|
set(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const _update: typeof update = (fn: (eds: Edge[]) => Edge[]) => _set(fn(value));
|
||||||
|
|
||||||
|
const setDefaultOptions = (options: DefaultEdgeOptions) => {
|
||||||
|
defaults = options;
|
||||||
|
};
|
||||||
|
|
||||||
|
_set(value);
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
set: _set,
|
||||||
|
update: _update,
|
||||||
|
setDefaultOptions
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function calculateXYZPosition(node: Node, nodes: Node[], result: XYZPosition): XYZPosition {
|
||||||
|
if (!node.parentNode) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
const parentNode = nodes.find((n) => n.id === node.parentNode)!;
|
||||||
|
const parentNodePosition = getNodePositionWithOrigin(parentNode, parentNode?.origin);
|
||||||
|
|
||||||
|
return calculateXYZPosition(parentNode, nodes, {
|
||||||
|
x: (result.x ?? 0) + parentNodePosition.x,
|
||||||
|
y: (result.y ?? 0) + parentNodePosition.y,
|
||||||
|
z:
|
||||||
|
(parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0)
|
||||||
|
? parentNode[internalsSymbol]?.z ?? 0
|
||||||
|
: result.z ?? 0
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import { writable, type Writable } from 'svelte/store';
|
|
||||||
import {
|
import {
|
||||||
isNodeBase,
|
isNodeBase,
|
||||||
isEdgeBase,
|
isEdgeBase,
|
||||||
@@ -6,14 +5,10 @@ import {
|
|||||||
getOutgoersBase,
|
getOutgoersBase,
|
||||||
getIncomersBase,
|
getIncomersBase,
|
||||||
updateEdgeBase,
|
updateEdgeBase,
|
||||||
getConnectedEdgesBase,
|
getConnectedEdgesBase
|
||||||
isNumeric,
|
|
||||||
getNodePositionWithOrigin,
|
|
||||||
internalsSymbol,
|
|
||||||
type XYZPosition
|
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, Node } from '$lib/types';
|
import type { Edge, Node } from '$lib/types';
|
||||||
|
|
||||||
export const isNode = isNodeBase<Node, Edge>;
|
export const isNode = isNodeBase<Node, Edge>;
|
||||||
export const isEdge = isEdgeBase<Node, Edge>;
|
export const isEdge = isEdgeBase<Node, Edge>;
|
||||||
@@ -22,126 +17,3 @@ export const getIncomers = getIncomersBase<Node, Edge>;
|
|||||||
export const addEdge = addEdgeBase<Edge>;
|
export const addEdge = addEdgeBase<Edge>;
|
||||||
export const updateEdge = updateEdgeBase<Edge>;
|
export const updateEdge = updateEdgeBase<Edge>;
|
||||||
export const getConnectedEdges = getConnectedEdgesBase<Node, Edge>;
|
export const getConnectedEdges = getConnectedEdgesBase<Node, Edge>;
|
||||||
|
|
||||||
export const createNodes = (
|
|
||||||
nodes: Node[],
|
|
||||||
defaultOptions?: DefaultNodeOptions
|
|
||||||
): Writable<Node[]> & { setDefaultOptions: (opts: DefaultNodeOptions) => void } => {
|
|
||||||
const { subscribe, set, update } = writable<Node[]>([]);
|
|
||||||
let value = nodes;
|
|
||||||
let defaults = defaultOptions || {};
|
|
||||||
|
|
||||||
const _set: typeof set = (nds: Node[]) => {
|
|
||||||
const parentNodes: Record<string, boolean> = {};
|
|
||||||
|
|
||||||
const nextNodes = nds.map((n) => {
|
|
||||||
const node: Node = { ...defaults, ...n, positionAbsolute: n.position };
|
|
||||||
const z = (isNumeric(node.zIndex) ? node.zIndex : 0) + (node.selected ? 1 : 0);
|
|
||||||
|
|
||||||
if (node.parentNode) {
|
|
||||||
parentNodes[node.parentNode] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.defineProperty(node, internalsSymbol, {
|
|
||||||
value: {
|
|
||||||
handleBounds: node?.[internalsSymbol]?.handleBounds,
|
|
||||||
z
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return node;
|
|
||||||
});
|
|
||||||
|
|
||||||
const nodesWithPositions = nextNodes.map((node) => {
|
|
||||||
if (node.parentNode && !parentNodes[node.parentNode]) {
|
|
||||||
throw new Error(`Parent node ${node.parentNode} not found`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.parentNode || parentNodes?.[node.id]) {
|
|
||||||
const { x, y, z } = calculateXYZPosition(node, nextNodes, {
|
|
||||||
...node.position,
|
|
||||||
z: node[internalsSymbol]?.z ?? 0
|
|
||||||
});
|
|
||||||
|
|
||||||
node.positionAbsolute = {
|
|
||||||
x,
|
|
||||||
y
|
|
||||||
};
|
|
||||||
|
|
||||||
node[internalsSymbol]!.z = z;
|
|
||||||
|
|
||||||
if (parentNodes?.[node.id]) {
|
|
||||||
node[internalsSymbol]!.isParent = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
|
||||||
});
|
|
||||||
|
|
||||||
value = nodesWithPositions;
|
|
||||||
|
|
||||||
set(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const _update: typeof update = (fn: (nds: Node[]) => Node[]) => _set(fn(value));
|
|
||||||
|
|
||||||
const setDefaultOptions = (options: DefaultNodeOptions) => {
|
|
||||||
defaults = options;
|
|
||||||
};
|
|
||||||
|
|
||||||
_set(value);
|
|
||||||
|
|
||||||
return {
|
|
||||||
subscribe,
|
|
||||||
set: _set,
|
|
||||||
update: _update,
|
|
||||||
setDefaultOptions
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createEdges = (
|
|
||||||
edges: Edge[],
|
|
||||||
defaultOptions?: DefaultEdgeOptions
|
|
||||||
): Writable<Edge[]> & { setDefaultOptions: (opts: DefaultEdgeOptions) => void } => {
|
|
||||||
const { subscribe, set, update } = writable<Edge[]>([]);
|
|
||||||
let value = edges;
|
|
||||||
let defaults = defaultOptions || {};
|
|
||||||
|
|
||||||
const _set: typeof set = (eds: Edge[]) => {
|
|
||||||
const nextEdges = defaults ? eds.map((edge) => ({ ...defaults, ...edge })) : eds;
|
|
||||||
value = nextEdges;
|
|
||||||
set(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const _update: typeof update = (fn: (eds: Edge[]) => Edge[]) => _set(fn(value));
|
|
||||||
|
|
||||||
const setDefaultOptions = (options: DefaultEdgeOptions) => {
|
|
||||||
defaults = options;
|
|
||||||
};
|
|
||||||
|
|
||||||
_set(value);
|
|
||||||
|
|
||||||
return {
|
|
||||||
subscribe,
|
|
||||||
set: _set,
|
|
||||||
update: _update,
|
|
||||||
setDefaultOptions
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
function calculateXYZPosition(node: Node, nodes: Node[], result: XYZPosition): XYZPosition {
|
|
||||||
if (!node.parentNode) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
const parentNode = nodes.find((n) => n.id === node.parentNode)!;
|
|
||||||
const parentNodePosition = getNodePositionWithOrigin(parentNode, parentNode?.origin);
|
|
||||||
|
|
||||||
return calculateXYZPosition(parentNode, nodes, {
|
|
||||||
x: (result.x ?? 0) + parentNodePosition.x,
|
|
||||||
y: (result.y ?? 0) + parentNodePosition.y,
|
|
||||||
z:
|
|
||||||
(parentNode[internalsSymbol]?.z ?? 0) > (result.z ?? 0)
|
|
||||||
? parentNode[internalsSymbol]?.z ?? 0
|
|
||||||
: result.z ?? 0
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,13 +2,10 @@
|
|||||||
import type { ChangeEventHandler } from 'svelte/elements';
|
import type { ChangeEventHandler } from 'svelte/elements';
|
||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type NodeTypes,
|
type NodeTypes,
|
||||||
Position
|
Position
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
@@ -23,7 +20,7 @@
|
|||||||
const onChange: ChangeEventHandler<HTMLInputElement> = (event) => {
|
const onChange: ChangeEventHandler<HTMLInputElement> = (event) => {
|
||||||
nodes.update((nds) =>
|
nodes.update((nds) =>
|
||||||
nds.map((node) => {
|
nds.map((node) => {
|
||||||
if (node.id !== '2') {
|
if (node.type !== 'colorNode') {
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +39,7 @@
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
@@ -73,7 +70,7 @@
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = writable([
|
||||||
{
|
{
|
||||||
id: 'e1-2',
|
id: 'e1-2',
|
||||||
source: '1',
|
source: '1',
|
||||||
@@ -94,23 +91,20 @@
|
|||||||
target: '4',
|
target: '4',
|
||||||
animated: true,
|
animated: true,
|
||||||
},
|
},
|
||||||
], { animated: true });
|
]);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
{nodeTypes}
|
||||||
|
style="--bgcolor: {$bgColor}"
|
||||||
|
fitView
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
{nodeTypes}
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
style="--bgcolor: {$bgColor}"
|
<MiniMap />
|
||||||
fitView
|
</SvelteFlow>
|
||||||
>
|
|
||||||
<Controls />
|
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
<MiniMap />
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
:global(.svelte-flow) {
|
:global(.svelte-flow) {
|
||||||
|
|||||||
@@ -1,52 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createNodes, createEdges, SvelteFlowProvider } from '../../lib/index';
|
import { SvelteFlowProvider } from '../../lib/index';
|
||||||
|
|
||||||
import Flow from './Flow.svelte';
|
import Flow from './Flow.svelte';
|
||||||
|
|
||||||
const nodes = createNodes(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
id: '1',
|
|
||||||
type: 'input',
|
|
||||||
data: { label: 'Input Node' },
|
|
||||||
position: { x: 150, y: 5 }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
type: 'default',
|
|
||||||
data: { label: 'Node' },
|
|
||||||
position: { x: 0, y: 150 }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '3',
|
|
||||||
type: 'output',
|
|
||||||
data: { label: 'Output Node' },
|
|
||||||
position: { x: 300, y: 150 }
|
|
||||||
}
|
|
||||||
],
|
|
||||||
{ origin: [0.5, 0.5] }
|
|
||||||
);
|
|
||||||
|
|
||||||
const edges = createEdges([
|
|
||||||
{
|
|
||||||
id: '1-2',
|
|
||||||
type: 'default',
|
|
||||||
source: '1',
|
|
||||||
target: '2',
|
|
||||||
label: 'Edge Text'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '1-3',
|
|
||||||
type: 'smoothstep',
|
|
||||||
source: '1',
|
|
||||||
target: '3'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlowProvider>
|
||||||
{nodes}
|
|
||||||
{edges}
|
|
||||||
>
|
|
||||||
<Flow />
|
<Flow />
|
||||||
</SvelteFlowProvider>
|
</SvelteFlowProvider>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
@@ -9,6 +10,45 @@
|
|||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
import Sidebar from './Sidebar.svelte';
|
import Sidebar from './Sidebar.svelte';
|
||||||
|
|
||||||
|
const nodes = writable(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'input',
|
||||||
|
data: { label: 'Input Node' },
|
||||||
|
position: { x: 150, y: 5 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
type: 'default',
|
||||||
|
data: { label: 'Node' },
|
||||||
|
position: { x: 0, y: 150 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
type: 'output',
|
||||||
|
data: { label: 'Output Node' },
|
||||||
|
position: { x: 300, y: 150 }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
const edges = writable([
|
||||||
|
{
|
||||||
|
id: '1-2',
|
||||||
|
type: 'default',
|
||||||
|
source: '1',
|
||||||
|
target: '2',
|
||||||
|
label: 'Edge Text'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '1-3',
|
||||||
|
type: 'smoothstep',
|
||||||
|
source: '1',
|
||||||
|
target: '3'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
const svelteFlow = useSvelteFlow();
|
const svelteFlow = useSvelteFlow();
|
||||||
|
|
||||||
const onDragOver = (event: DragEvent) => {
|
const onDragOver = (event: DragEvent) => {
|
||||||
@@ -46,6 +86,8 @@
|
|||||||
|
|
||||||
<main>
|
<main>
|
||||||
<SvelteFlow
|
<SvelteFlow
|
||||||
|
{nodes}
|
||||||
|
{edges}
|
||||||
fitView
|
fitView
|
||||||
on:dragover={onDragOver}
|
on:dragover={onDragOver}
|
||||||
on:drop={onDrop}
|
on:drop={onDrop}
|
||||||
|
|||||||
@@ -1,163 +1,153 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type NodeTypes,
|
|
||||||
MarkerType
|
MarkerType
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
data: { label: 'Input 1' },
|
data: { label: 'Input 1' },
|
||||||
position: { x: 250, y: 0 },
|
position: { x: 250, y: 0 },
|
||||||
},
|
},
|
||||||
{ id: '2', data: { label: 'Node 2' }, position: { x: 150, y: 100 } },
|
{ id: '2', data: { label: 'Node 2' }, position: { x: 150, y: 100 } },
|
||||||
{ id: '2a', data: { label: 'Node 2a' }, position: { x: 0, y: 180 } },
|
{ id: '2a', data: { label: 'Node 2a' }, position: { x: 0, y: 180 } },
|
||||||
{ id: '2b', data: { label: 'Node 2b' }, position: { x: -40, y: 300 } },
|
{ id: '2b', data: { label: 'Node 2b' }, position: { x: -40, y: 300 } },
|
||||||
{ id: '3', data: { label: 'Node 3' }, position: { x: 250, y: 200 } },
|
{ id: '3', data: { label: 'Node 3' }, position: { x: 250, y: 200 } },
|
||||||
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 300 } },
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 300 } },
|
||||||
{ id: '3a', data: { label: 'Node 3a' }, position: { x: 150, y: 300 } },
|
{ id: '3a', data: { label: 'Node 3a' }, position: { x: 150, y: 300 } },
|
||||||
{ id: '5', data: { label: 'Node 5' }, position: { x: 250, y: 400 } },
|
{ id: '5', data: { label: 'Node 5' }, position: { x: 250, y: 400 } },
|
||||||
{
|
{
|
||||||
id: '6',
|
id: '6',
|
||||||
type: 'output',
|
type: 'output',
|
||||||
data: { label: 'Output 6' },
|
data: { label: 'Output 6' },
|
||||||
position: { x: 50, y: 550 },
|
position: { x: 50, y: 550 },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '7',
|
id: '7',
|
||||||
type: 'output',
|
type: 'output',
|
||||||
data: { label: 'Output 7' },
|
data: { label: 'Output 7' },
|
||||||
position: { x: 250, y: 550 },
|
position: { x: 250, y: 550 },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '8',
|
id: '8',
|
||||||
type: 'output',
|
type: 'output',
|
||||||
data: { label: 'Output 8' },
|
data: { label: 'Output 8' },
|
||||||
position: { x: 525, y: 600 },
|
position: { x: 525, y: 600 },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: '9',
|
id: '9',
|
||||||
type: 'output',
|
type: 'output',
|
||||||
data: { label: 'Output 9' },
|
data: { label: 'Output 9' },
|
||||||
position: { x: 675, y: 500 },
|
position: { x: 675, y: 500 },
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = writable([
|
||||||
{
|
{
|
||||||
id: 'e1-2',
|
id: 'e1-2',
|
||||||
source: '1',
|
source: '1',
|
||||||
target: '2',
|
target: '2',
|
||||||
label: 'bezier edge (default)',
|
label: 'bezier edge (default)',
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e2-2a',
|
|
||||||
source: '2',
|
|
||||||
target: '2a',
|
|
||||||
type: 'smoothstep',
|
|
||||||
label: 'smoothstep edge',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e2a-2b',
|
|
||||||
source: '2a',
|
|
||||||
target: '2b',
|
|
||||||
type: 'simplebezier',
|
|
||||||
label: 'simple bezier edge',
|
|
||||||
},
|
|
||||||
{ id: 'e2-3', source: '2', target: '3', type: 'step', label: 'step edge' },
|
|
||||||
{
|
|
||||||
id: 'e3-4',
|
|
||||||
source: '3',
|
|
||||||
target: '4',
|
|
||||||
type: 'straight',
|
|
||||||
label: 'straight edge',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e3-3a',
|
|
||||||
source: '3',
|
|
||||||
target: '3a',
|
|
||||||
type: 'straight',
|
|
||||||
label: 'label only edge',
|
|
||||||
style: 'stroke: none',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e3-5',
|
|
||||||
source: '4',
|
|
||||||
target: '5',
|
|
||||||
animated: true,
|
|
||||||
label: 'animated styled edge',
|
|
||||||
style: 'stroke: red',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e5-7',
|
|
||||||
source: '5',
|
|
||||||
target: '7',
|
|
||||||
label: 'label with styled bg',
|
|
||||||
labelStyle: 'background: #FFCC00; color: #fff; opacity: 0.7',
|
|
||||||
markerEnd: {
|
|
||||||
type: MarkerType.ArrowClosed,
|
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
{
|
id: 'e2-2a',
|
||||||
id: 'e5-8',
|
source: '2',
|
||||||
source: '5',
|
target: '2a',
|
||||||
target: '8',
|
type: 'smoothstep',
|
||||||
data: { text: 'custom edge' },
|
label: 'smoothstep edge',
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e5-9',
|
|
||||||
source: '5',
|
|
||||||
target: '9',
|
|
||||||
data: { text: 'custom edge 2' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'e5-6',
|
|
||||||
source: '5',
|
|
||||||
target: '6',
|
|
||||||
label: 'hi',
|
|
||||||
labelStyle: 'background: red; font-weight: 700; padding: 5px;',
|
|
||||||
style: 'stroke: #ffcc0',
|
|
||||||
markerEnd: {
|
|
||||||
type: MarkerType.Arrow,
|
|
||||||
color: '#FFCC00',
|
|
||||||
markerUnits: 'userSpaceOnUse',
|
|
||||||
width: 20,
|
|
||||||
height: 20,
|
|
||||||
strokeWidth: 2,
|
|
||||||
},
|
},
|
||||||
markerStart: {
|
{
|
||||||
type: MarkerType.ArrowClosed,
|
id: 'e2a-2b',
|
||||||
color: '#FFCC00',
|
source: '2a',
|
||||||
orient: 'auto-start-reverse',
|
target: '2b',
|
||||||
markerUnits: 'userSpaceOnUse',
|
type: 'simplebezier',
|
||||||
width: 20,
|
label: 'simple bezier edge',
|
||||||
height: 20,
|
|
||||||
},
|
},
|
||||||
},
|
{ id: 'e2-3', source: '2', target: '3', type: 'step', label: 'step edge' },
|
||||||
]);
|
{
|
||||||
|
id: 'e3-4',
|
||||||
|
source: '3',
|
||||||
|
target: '4',
|
||||||
|
type: 'straight',
|
||||||
|
label: 'straight edge',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e3-3a',
|
||||||
|
source: '3',
|
||||||
|
target: '3a',
|
||||||
|
type: 'straight',
|
||||||
|
label: 'label only edge',
|
||||||
|
style: 'stroke: none',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e3-5',
|
||||||
|
source: '4',
|
||||||
|
target: '5',
|
||||||
|
animated: true,
|
||||||
|
label: 'animated styled edge',
|
||||||
|
style: 'stroke: red',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e5-7',
|
||||||
|
source: '5',
|
||||||
|
target: '7',
|
||||||
|
label: 'label with styled bg',
|
||||||
|
labelStyle: 'background: #FFCC00; color: #fff; opacity: 0.7',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e5-8',
|
||||||
|
source: '5',
|
||||||
|
target: '8',
|
||||||
|
data: { text: 'custom edge' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e5-9',
|
||||||
|
source: '5',
|
||||||
|
target: '9',
|
||||||
|
data: { text: 'custom edge 2' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'e5-6',
|
||||||
|
source: '5',
|
||||||
|
target: '6',
|
||||||
|
label: 'hi',
|
||||||
|
labelStyle: 'background: red; font-weight: 700; padding: 5px;',
|
||||||
|
style: 'stroke: #ffcc0',
|
||||||
|
markerEnd: {
|
||||||
|
type: MarkerType.Arrow,
|
||||||
|
color: '#FFCC00',
|
||||||
|
markerUnits: 'userSpaceOnUse',
|
||||||
|
width: 20,
|
||||||
|
height: 20,
|
||||||
|
strokeWidth: 2,
|
||||||
|
},
|
||||||
|
markerStart: {
|
||||||
|
type: MarkerType.ArrowClosed,
|
||||||
|
color: '#FFCC00',
|
||||||
|
orient: 'auto-start-reverse',
|
||||||
|
markerUnits: 'userSpaceOnUse',
|
||||||
|
width: 20,
|
||||||
|
height: 20,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
fitView
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
fitView
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
>
|
<MiniMap />
|
||||||
<Controls />
|
</SvelteFlow>
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
<MiniMap />
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
|
|
||||||
</style>
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Panel,
|
Panel,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
PanOnScrollMode,
|
PanOnScrollMode,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
type OnMoveEnd,
|
type OnMoveEnd,
|
||||||
@@ -12,7 +10,7 @@
|
|||||||
type Edge,
|
type Edge,
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
@@ -24,7 +22,7 @@
|
|||||||
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
|
{ id: '4', data: { label: 'Node 4' }, position: { x: 400, y: 200 } },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = writable([
|
||||||
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
{ id: 'e1-2', source: '1', target: '2', animated: true },
|
||||||
{ id: 'e1-3', source: '1', target: '3' },
|
{ id: 'e1-3', source: '1', target: '3' },
|
||||||
]);
|
]);
|
||||||
@@ -53,163 +51,159 @@
|
|||||||
let captureElementClick = false;
|
let captureElementClick = false;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<SvelteFlow
|
||||||
<SvelteFlowProvider
|
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
elementsSelectable={isSelectable}
|
||||||
|
nodesConnectable={isConnectable}
|
||||||
|
nodesDraggable={isDraggable}
|
||||||
|
zoomOnScroll={zoomOnScroll}
|
||||||
|
zoomOnPinch={zoomOnPinch}
|
||||||
|
panOnScroll={panOnScroll}
|
||||||
|
panOnScrollMode={panOnScrollMode}
|
||||||
|
zoomOnDoubleClick={zoomOnDoubleClick}
|
||||||
|
panOnDrag={panOnDrag}
|
||||||
|
onMoveEnd={onMoveEnd}
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<MiniMap />
|
||||||
elementsSelectable={isSelectable}
|
<Controls />
|
||||||
nodesConnectable={isConnectable}
|
|
||||||
nodesDraggable={isDraggable}
|
|
||||||
zoomOnScroll={zoomOnScroll}
|
|
||||||
zoomOnPinch={zoomOnPinch}
|
|
||||||
panOnScroll={panOnScroll}
|
|
||||||
panOnScrollMode={panOnScrollMode}
|
|
||||||
zoomOnDoubleClick={zoomOnDoubleClick}
|
|
||||||
panOnDrag={panOnDrag}
|
|
||||||
onMoveEnd={onMoveEnd}
|
|
||||||
>
|
|
||||||
<MiniMap />
|
|
||||||
<Controls />
|
|
||||||
|
|
||||||
<Panel position="top-right">
|
<Panel position="top-right">
|
||||||
<div>
|
<div>
|
||||||
<label for="draggable">
|
<label for="draggable">
|
||||||
nodesDraggable
|
nodesDraggable
|
||||||
<input
|
<input
|
||||||
id="draggable"
|
id="draggable"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={isDraggable}
|
bind:checked={isDraggable}
|
||||||
class="react-flow__draggable"
|
class="react-flow__draggable"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="connectable">
|
<label for="connectable">
|
||||||
nodesConnectable
|
nodesConnectable
|
||||||
<input
|
<input
|
||||||
id="connectable"
|
id="connectable"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={isConnectable}
|
bind:checked={isConnectable}
|
||||||
class="react-flow__connectable"
|
class="react-flow__connectable"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="selectable">
|
<label for="selectable">
|
||||||
elementsSelectable
|
elementsSelectable
|
||||||
<input
|
<input
|
||||||
id="selectable"
|
id="selectable"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={isSelectable}
|
bind:checked={isSelectable}
|
||||||
class="react-flow__selectable"
|
class="react-flow__selectable"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="zoomonscroll">
|
<label for="zoomonscroll">
|
||||||
zoomOnScroll
|
zoomOnScroll
|
||||||
<input
|
<input
|
||||||
id="zoomonscroll"
|
id="zoomonscroll"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={zoomOnScroll}
|
bind:checked={zoomOnScroll}
|
||||||
class="react-flow__zoomonscroll"
|
class="react-flow__zoomonscroll"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="zoomonpinch">
|
<label for="zoomonpinch">
|
||||||
zoomOnPinch
|
zoomOnPinch
|
||||||
<input
|
<input
|
||||||
id="zoomonpinch"
|
id="zoomonpinch"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={zoomOnPinch}
|
bind:checked={zoomOnPinch}
|
||||||
class="react-flow__zoomonpinch"
|
class="react-flow__zoomonpinch"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="panonscroll">
|
<label for="panonscroll">
|
||||||
panOnScroll
|
panOnScroll
|
||||||
<input
|
<input
|
||||||
id="panonscroll"
|
id="panonscroll"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={panOnScroll}
|
bind:checked={panOnScroll}
|
||||||
class="react-flow__panonscroll"
|
class="react-flow__panonscroll"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="panonscrollmode">
|
<label for="panonscrollmode">
|
||||||
panOnScrollMode
|
panOnScrollMode
|
||||||
<select
|
<select
|
||||||
id="panonscrollmode"
|
id="panonscrollmode"
|
||||||
bind:value={panOnScrollMode}
|
bind:value={panOnScrollMode}
|
||||||
on:change={(event) => { panOnScrollMode = PanOnScrollMode.Free }}
|
on:change={(event) => { panOnScrollMode = PanOnScrollMode.Free }}
|
||||||
class="react-flow__panonscrollmode"
|
class="react-flow__panonscrollmode"
|
||||||
>
|
>
|
||||||
<option value="free">free</option>
|
<option value="free">free</option>
|
||||||
<option value="horizontal">horizontal</option>
|
<option value="horizontal">horizontal</option>
|
||||||
<option value="vertical">vertical</option>
|
<option value="vertical">vertical</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="zoomondbl">
|
<label for="zoomondbl">
|
||||||
zoomOnDoubleClick
|
zoomOnDoubleClick
|
||||||
<input
|
<input
|
||||||
id="zoomondbl"
|
id="zoomondbl"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={zoomOnDoubleClick}
|
bind:checked={zoomOnDoubleClick}
|
||||||
class="react-flow__zoomondbl"
|
class="react-flow__zoomondbl"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="panondrag">
|
<label for="panondrag">
|
||||||
panOnDrag
|
panOnDrag
|
||||||
<input
|
<input
|
||||||
id="panondrag"
|
id="panondrag"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={panOnDrag}
|
bind:checked={panOnDrag}
|
||||||
class="react-flow__panondrag"
|
class="react-flow__panondrag"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="capturezoompaneclick">
|
<label for="capturezoompaneclick">
|
||||||
capture onPaneClick
|
capture onPaneClick
|
||||||
<input
|
<input
|
||||||
id="capturezoompaneclick"
|
id="capturezoompaneclick"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={captureZoomClick}
|
bind:checked={captureZoomClick}
|
||||||
class="react-flow__capturezoompaneclick"
|
class="react-flow__capturezoompaneclick"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="capturezoompanescroll">
|
<label for="capturezoompanescroll">
|
||||||
capture onPaneScroll
|
capture onPaneScroll
|
||||||
<input
|
<input
|
||||||
id="capturezoompanescroll"
|
id="capturezoompanescroll"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={captureZoomScroll}
|
bind:checked={captureZoomScroll}
|
||||||
class="react-flow__capturezoompanescroll"
|
class="react-flow__capturezoompanescroll"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label for="captureelementclick">
|
<label for="captureelementclick">
|
||||||
capture onElementClick
|
capture onElementClick
|
||||||
<input
|
<input
|
||||||
id="captureelementclick"
|
id="captureelementclick"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
bind:checked={captureElementClick}
|
bind:checked={captureElementClick}
|
||||||
class="react-flow__captureelementclick"
|
class="react-flow__captureelementclick"
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</Panel>
|
</Panel>
|
||||||
</SvelteFlow>
|
</SvelteFlow>
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
Panel,
|
Panel,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type NodeTypes,
|
|
||||||
SelectionMode,
|
SelectionMode,
|
||||||
|
type NodeTypes,
|
||||||
type EdgeTypes
|
type EdgeTypes
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
import { CustomNode } from './CustomNode';
|
import { CustomNode } from './CustomNode';
|
||||||
@@ -23,7 +22,7 @@
|
|||||||
custom: CustomEdge
|
custom: CustomEdge
|
||||||
};
|
};
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
@@ -70,9 +69,9 @@
|
|||||||
data: { label: 'Custom Node' },
|
data: { label: 'Custom Node' },
|
||||||
position: { x: 150, y: 300 }
|
position: { x: 150, y: 300 }
|
||||||
}
|
}
|
||||||
], { style: 'width: 125px;' });
|
]);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = writable([
|
||||||
{
|
{
|
||||||
id: '1-2',
|
id: '1-2',
|
||||||
type: 'default',
|
type: 'default',
|
||||||
@@ -93,7 +92,7 @@
|
|||||||
source: '2',
|
source: '2',
|
||||||
target: '4',
|
target: '4',
|
||||||
}
|
}
|
||||||
], { animated: true });
|
]);
|
||||||
|
|
||||||
function updateNode() {
|
function updateNode() {
|
||||||
nodes.update(nds => nds.map(n => {
|
nodes.update(nds => nds.map(n => {
|
||||||
@@ -113,37 +112,34 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
{nodeTypes}
|
||||||
|
{edgeTypes}
|
||||||
|
fitView
|
||||||
|
minZoom={0.1}
|
||||||
|
maxZoom={2.5}
|
||||||
|
selectionMode={SelectionMode.Full}
|
||||||
|
initialViewport={{ x: 100, y: 100, zoom: 2 }}
|
||||||
|
snapGrid={[25, 25]}
|
||||||
|
on:node:click={(event) => console.log('on node click', event)}
|
||||||
|
on:node:mouseenter={(event) => console.log('on node enter', event)}
|
||||||
|
on:node:mouseleave={(event) => console.log('on node leave', event)}
|
||||||
|
on:edge:click={(event) => console.log('edge click', event)}
|
||||||
|
on:connect:start={(event) => console.log('on connect start', event)}
|
||||||
|
on:connect={(event) => console.log('on connect', event)}
|
||||||
|
on:connect:end={(event) => console.log('on connect end', event)}
|
||||||
|
on:pane:click={(event) => console.log('on pane click', event)}
|
||||||
|
on:pane:contextmenu={(event) => { event.preventDefault(); console.log('on pane contextmenu', event); }}
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
{nodeTypes}
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
{edgeTypes}
|
<MiniMap />
|
||||||
fitView
|
<Panel position="top-right">
|
||||||
minZoom={0.1}
|
<button on:click={updateNode}>update node pos</button>
|
||||||
maxZoom={2.5}
|
</Panel>
|
||||||
selectionMode={SelectionMode.Full}
|
</SvelteFlow>
|
||||||
initialViewport={{ x: 100, y: 100, zoom: 2 }}
|
|
||||||
snapGrid={[25, 25]}
|
|
||||||
on:node:click={(event) => console.log('on node click', event)}
|
|
||||||
on:node:mouseenter={(event) => console.log('on node enter', event)}
|
|
||||||
on:node:mouseleave={(event) => console.log('on node leave', event)}
|
|
||||||
on:edge:click={(event) => console.log('edge click', event)}
|
|
||||||
on:connect:start={(event) => console.log('on connect start', event)}
|
|
||||||
on:connect={(event) => console.log('on connect', event)}
|
|
||||||
on:connect:end={(event) => console.log('on connect end', event)}
|
|
||||||
on:pane:click={(event) => console.log('on pane click', event)}
|
|
||||||
on:pane:contextmenu={(event) => { event.preventDefault(); console.log('on pane contextmenu', event); }}
|
|
||||||
>
|
|
||||||
<Controls />
|
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
<MiniMap />
|
|
||||||
<Panel position="top-right">
|
|
||||||
<button on:click={updateNode}>update node pos</button>
|
|
||||||
</Panel>
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
:global(.svelte-flow .custom-style) {
|
:global(.svelte-flow .custom-style) {
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type Node,
|
type Node,
|
||||||
type Edge
|
type Edge
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
@@ -45,20 +43,17 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = createNodes(nodeItems);
|
const nodes = writable(nodeItems);
|
||||||
const edges = createEdges(edgeItems, { animated: true });
|
const edges = writable(edgeItems);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
fitView
|
||||||
|
minZoom={0.2}
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
fitView
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
minZoom={0.2}
|
<MiniMap />
|
||||||
>
|
</SvelteFlow>
|
||||||
<Controls />
|
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
<MiniMap />
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
MiniMap,
|
MiniMap,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type NodeTypes,
|
type NodeTypes,
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
import { DebugNode } from './DebugNode';
|
import { DebugNode } from './DebugNode';
|
||||||
@@ -15,7 +13,7 @@
|
|||||||
default: DebugNode,
|
default: DebugNode,
|
||||||
};
|
};
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{
|
{
|
||||||
id: '1',
|
id: '1',
|
||||||
type: 'input',
|
type: 'input',
|
||||||
@@ -92,7 +90,7 @@
|
|||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const edges = createEdges([
|
const edges = writable([
|
||||||
{
|
{
|
||||||
id: 'e1-2',
|
id: 'e1-2',
|
||||||
source: '1',
|
source: '1',
|
||||||
@@ -111,28 +109,25 @@
|
|||||||
{ id: 'e4a-4b1', source: '4a', target: '4b1' },
|
{ id: 'e4a-4b1', source: '4a', target: '4b1' },
|
||||||
{ id: 'e4a-4b2', source: '4a', target: '4b2', zIndex: 100 },
|
{ id: 'e4a-4b2', source: '4a', target: '4b2', zIndex: 100 },
|
||||||
{ id: 'e4b1-4b2', source: '4b1', target: '4b2' },
|
{ id: 'e4b1-4b2', source: '4b1', target: '4b2' },
|
||||||
], { animated: true });
|
]);
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
console.log($nodes)
|
console.log($nodes)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
{nodeTypes}
|
||||||
|
fitView
|
||||||
|
minZoom={0.1}
|
||||||
|
maxZoom={2.5}
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
{nodeTypes}
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
fitView
|
<MiniMap />
|
||||||
minZoom={0.1}
|
</SvelteFlow>
|
||||||
maxZoom={2.5}
|
|
||||||
>
|
|
||||||
<Controls />
|
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
<MiniMap />
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
:global(.svelte-flow .svelte-flow__node.parent) {
|
:global(.svelte-flow .svelte-flow__node.parent) {
|
||||||
|
|||||||
@@ -1,52 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { createNodes, createEdges, SvelteFlowProvider } from '../../lib/index';
|
import { SvelteFlowProvider } from '../../lib/index';
|
||||||
|
|
||||||
import Flow from './Flow.svelte';
|
import Flow from './Flow.svelte';
|
||||||
|
|
||||||
const nodes = createNodes(
|
|
||||||
[
|
|
||||||
{
|
|
||||||
id: '1',
|
|
||||||
type: 'input',
|
|
||||||
data: { label: 'Input Node' },
|
|
||||||
position: { x: 150, y: 5 }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
type: 'default',
|
|
||||||
data: { label: 'Node' },
|
|
||||||
position: { x: 0, y: 150 }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '3',
|
|
||||||
type: 'output',
|
|
||||||
data: { label: 'Output Node' },
|
|
||||||
position: { x: 300, y: 150 }
|
|
||||||
}
|
|
||||||
],
|
|
||||||
{ origin: [0.5, 0.5] }
|
|
||||||
);
|
|
||||||
|
|
||||||
const edges = createEdges([
|
|
||||||
{
|
|
||||||
id: '1-2',
|
|
||||||
type: 'default',
|
|
||||||
source: '1',
|
|
||||||
target: '2',
|
|
||||||
label: 'Edge Text'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '1-3',
|
|
||||||
type: 'smoothstep',
|
|
||||||
source: '1',
|
|
||||||
target: '3'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlowProvider>
|
||||||
{nodes}
|
|
||||||
{edges}
|
|
||||||
>
|
|
||||||
<Flow />
|
<Flow />
|
||||||
</SvelteFlowProvider>
|
</SvelteFlowProvider>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
@@ -6,10 +7,49 @@
|
|||||||
MiniMap,
|
MiniMap,
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
import Sidebar from './Sidebar.svelte';
|
import Sidebar from './Sidebar.svelte';
|
||||||
|
|
||||||
|
const nodes = writable([
|
||||||
|
{
|
||||||
|
id: '1',
|
||||||
|
type: 'input',
|
||||||
|
data: { label: 'Input Node' },
|
||||||
|
position: { x: 150, y: 5 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '2',
|
||||||
|
type: 'default',
|
||||||
|
data: { label: 'Node' },
|
||||||
|
position: { x: 0, y: 150 }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '3',
|
||||||
|
type: 'output',
|
||||||
|
data: { label: 'Output Node' },
|
||||||
|
position: { x: 300, y: 150 }
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
const edges = writable([
|
||||||
|
{
|
||||||
|
id: '1-2',
|
||||||
|
type: 'default',
|
||||||
|
source: '1',
|
||||||
|
target: '2',
|
||||||
|
label: 'Edge Text'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: '1-3',
|
||||||
|
type: 'smoothstep',
|
||||||
|
source: '1',
|
||||||
|
target: '3'
|
||||||
|
}
|
||||||
|
]);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<SvelteFlow
|
<SvelteFlow
|
||||||
|
{nodes}
|
||||||
|
{edges}
|
||||||
fitView
|
fitView
|
||||||
>
|
>
|
||||||
<Controls />
|
<Controls />
|
||||||
|
|||||||
@@ -1,44 +1,36 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
import SvelteFlow, {
|
import SvelteFlow, {
|
||||||
SvelteFlowProvider,
|
|
||||||
Controls,
|
Controls,
|
||||||
Background,
|
Background,
|
||||||
BackgroundVariant,
|
BackgroundVariant,
|
||||||
createNodes,
|
|
||||||
createEdges,
|
|
||||||
type IsValidConnection
|
type IsValidConnection
|
||||||
} from '../../lib/index';
|
} from '../../lib/index';
|
||||||
import { Position } from '@xyflow/system';
|
|
||||||
|
|
||||||
const nodes = createNodes([
|
const nodes = writable([
|
||||||
{ id: '0', type: 'default', position: { x: 0, y: 150 }, data: { label: 'only connectable with B' } },
|
{ id: '0', type: 'default', position: { x: 0, y: 150 }, data: { label: 'only connectable with B' } },
|
||||||
{ id: 'A', type: 'default', position: { x: 250, y: 0 }, data: { label: 'A' } },
|
{ id: 'A', type: 'default', position: { x: 250, y: 0 }, data: { label: 'A' } },
|
||||||
{ id: 'B', type: 'default', position: { x: 250, y: 150 }, data: { label: 'B' } },
|
{ id: 'B', type: 'default', position: { x: 250, y: 150 }, data: { label: 'B' } },
|
||||||
{ id: 'C', type: 'default', position: { x: 250, y: 300 }, data: { label: 'C' } }
|
{ id: 'C', type: 'default', position: { x: 250, y: 300 }, data: { label: 'C' } }
|
||||||
], {
|
]);
|
||||||
sourcePosition: Position.Right,
|
|
||||||
targetPosition: Position.Left
|
|
||||||
});
|
|
||||||
|
|
||||||
const edges = createEdges([]);
|
const edges = writable([]);
|
||||||
|
|
||||||
const isValidConnection: IsValidConnection = (connection) => connection.target === 'B'
|
const isValidConnection: IsValidConnection = (connection) => connection.target === 'B';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SvelteFlowProvider
|
<SvelteFlow
|
||||||
{nodes}
|
{nodes}
|
||||||
{edges}
|
{edges}
|
||||||
|
fitView
|
||||||
|
minZoom={0.1}
|
||||||
|
maxZoom={2.5}
|
||||||
|
isValidConnection={isValidConnection}
|
||||||
>
|
>
|
||||||
<SvelteFlow
|
<Controls />
|
||||||
fitView
|
<Background variant={BackgroundVariant.Dots} />
|
||||||
minZoom={0.1}
|
</SvelteFlow>
|
||||||
maxZoom={2.5}
|
|
||||||
isValidConnection={isValidConnection}
|
|
||||||
>
|
|
||||||
<Controls />
|
|
||||||
<Background variant={BackgroundVariant.Dots} />
|
|
||||||
</SvelteFlow>
|
|
||||||
</SvelteFlowProvider>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
:global(.svelte-flow__handle.connecting) {
|
:global(.svelte-flow__handle.connecting) {
|
||||||
|
|||||||
Reference in New Issue
Block a user