fix(svelte): handle nodeOrigin prop
This commit is contained in:
@@ -34,7 +34,7 @@ export default function drag(domNode: Element, params: UseDragParams) {
|
|||||||
nodeExtent: get(store.nodeExtent),
|
nodeExtent: get(store.nodeExtent),
|
||||||
snapGrid: snapGrid ? snapGrid : [0, 0],
|
snapGrid: snapGrid ? snapGrid : [0, 0],
|
||||||
snapToGrid: !!snapGrid,
|
snapToGrid: !!snapGrid,
|
||||||
nodeOrigin: [0, 0],
|
nodeOrigin: get(store.nodeOrigin),
|
||||||
multiSelectionActive: get(store.multiselectionKeyPressed),
|
multiSelectionActive: get(store.multiselectionKeyPressed),
|
||||||
domNode: get(store.domNode),
|
domNode: get(store.domNode),
|
||||||
transform: [vp.x, vp.y, vp.zoom],
|
transform: [vp.x, vp.y, vp.zoom],
|
||||||
|
|||||||
@@ -11,12 +11,14 @@
|
|||||||
export let initialWidth: $$Props['initialWidth'] = undefined;
|
export let initialWidth: $$Props['initialWidth'] = undefined;
|
||||||
export let initialHeight: $$Props['initialHeight'] = undefined;
|
export let initialHeight: $$Props['initialHeight'] = undefined;
|
||||||
export let fitView: $$Props['fitView'] = undefined;
|
export let fitView: $$Props['fitView'] = undefined;
|
||||||
|
export let nodeOrigin: $$Props['nodeOrigin'] = undefined;
|
||||||
|
|
||||||
const store = createStore({
|
const store = createStore({
|
||||||
nodes: initialNodes,
|
nodes: initialNodes,
|
||||||
edges: initialEdges,
|
edges: initialEdges,
|
||||||
width: initialWidth,
|
width: initialWidth,
|
||||||
height: initialHeight,
|
height: initialHeight,
|
||||||
|
nodeOrigin,
|
||||||
fitView
|
fitView
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Edge, Node } from '$lib/types';
|
import type { Edge, Node } from '$lib/types';
|
||||||
|
import type { NodeOrigin } from '@xyflow/system';
|
||||||
|
|
||||||
export type SvelteFlowProviderProps = {
|
export type SvelteFlowProviderProps = {
|
||||||
initialNodes?: Node[];
|
initialNodes?: Node[];
|
||||||
@@ -6,4 +7,5 @@ export type SvelteFlowProviderProps = {
|
|||||||
initialWidth?: number;
|
initialWidth?: number;
|
||||||
initialHeight?: number;
|
initialHeight?: number;
|
||||||
fitView?: boolean;
|
fitView?: boolean;
|
||||||
|
nodeOrigin?: NodeOrigin;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -78,6 +78,7 @@
|
|||||||
export let onconnectend: $$Props['onconnectend'] = undefined;
|
export let onconnectend: $$Props['onconnectend'] = undefined;
|
||||||
export let onbeforedelete: $$Props['onbeforedelete'] = undefined;
|
export let onbeforedelete: $$Props['onbeforedelete'] = undefined;
|
||||||
export let oninit: $$Props['oninit'] = undefined;
|
export let oninit: $$Props['oninit'] = undefined;
|
||||||
|
export let nodeOrigin: $$Props['nodeOrigin'] = undefined;
|
||||||
|
|
||||||
export let defaultMarkerColor = '#b1b1b7';
|
export let defaultMarkerColor = '#b1b1b7';
|
||||||
|
|
||||||
@@ -93,7 +94,14 @@
|
|||||||
|
|
||||||
const store = hasContext(key)
|
const store = hasContext(key)
|
||||||
? useStore()
|
? useStore()
|
||||||
: createStoreContext({ nodes: get(nodes), edges: get(edges), width, height, fitView });
|
: createStoreContext({
|
||||||
|
nodes: get(nodes),
|
||||||
|
edges: get(edges),
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
fitView,
|
||||||
|
nodeOrigin
|
||||||
|
});
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
store.width.set(clientWidth);
|
store.width.set(clientWidth);
|
||||||
@@ -168,7 +176,8 @@
|
|||||||
onconnect,
|
onconnect,
|
||||||
onconnectstart,
|
onconnectstart,
|
||||||
onconnectend,
|
onconnectend,
|
||||||
onbeforedelete
|
onbeforedelete,
|
||||||
|
nodeOrigin
|
||||||
};
|
};
|
||||||
|
|
||||||
updateStoreByKeys(store, updatableProps);
|
updateStoreByKeys(store, updatableProps);
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ export type UpdatableStoreProps = {
|
|||||||
onconnectstart?: UnwrapWritable<SvelteFlowStore['onconnectstart']>;
|
onconnectstart?: UnwrapWritable<SvelteFlowStore['onconnectstart']>;
|
||||||
onconnectend?: UnwrapWritable<SvelteFlowStore['onconnectend']>;
|
onconnectend?: UnwrapWritable<SvelteFlowStore['onconnectend']>;
|
||||||
onbeforedelete?: UnwrapWritable<SvelteFlowStore['onbeforedelete']>;
|
onbeforedelete?: UnwrapWritable<SvelteFlowStore['onbeforedelete']>;
|
||||||
|
nodeOrigin?: UnwrapWritable<SvelteFlowStore['nodeOrigin']>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function updateStoreByKeys(store: SvelteFlowStore, keys: UpdatableStoreProps) {
|
export function updateStoreByKeys(store: SvelteFlowStore, keys: UpdatableStoreProps) {
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import {
|
|||||||
type XYPosition,
|
type XYPosition,
|
||||||
type CoordinateExtent,
|
type CoordinateExtent,
|
||||||
type UpdateConnection,
|
type UpdateConnection,
|
||||||
errorMessages
|
errorMessages,
|
||||||
|
type NodeOrigin
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions, ConnectionData } from '$lib/types';
|
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions, ConnectionData } from '$lib/types';
|
||||||
@@ -32,15 +33,24 @@ export function createStore({
|
|||||||
edges,
|
edges,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
fitView: fitViewOnCreate
|
fitView: fitViewOnCreate,
|
||||||
|
nodeOrigin
|
||||||
}: {
|
}: {
|
||||||
nodes?: Node[];
|
nodes?: Node[];
|
||||||
edges?: Edge[];
|
edges?: Edge[];
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
fitView?: boolean;
|
fitView?: boolean;
|
||||||
|
nodeOrigin?: NodeOrigin;
|
||||||
}): SvelteFlowStore {
|
}): SvelteFlowStore {
|
||||||
const store = getInitialStore({ nodes, edges, width, height, fitView: fitViewOnCreate });
|
const store = getInitialStore({
|
||||||
|
nodes,
|
||||||
|
edges,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
fitView: fitViewOnCreate,
|
||||||
|
nodeOrigin
|
||||||
|
});
|
||||||
|
|
||||||
function setNodeTypes(nodeTypes: NodeTypes) {
|
function setNodeTypes(nodeTypes: NodeTypes) {
|
||||||
store.nodeTypes.set({
|
store.nodeTypes.set({
|
||||||
@@ -438,15 +448,17 @@ export function createStoreContext({
|
|||||||
edges,
|
edges,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
fitView
|
fitView,
|
||||||
|
nodeOrigin
|
||||||
}: {
|
}: {
|
||||||
nodes?: Node[];
|
nodes?: Node[];
|
||||||
edges?: Edge[];
|
edges?: Edge[];
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
fitView?: boolean;
|
fitView?: boolean;
|
||||||
|
nodeOrigin?: NodeOrigin;
|
||||||
}) {
|
}) {
|
||||||
const store = createStore({ nodes, edges, width, height, fitView });
|
const store = createStore({ nodes, edges, width, height, fitView, nodeOrigin });
|
||||||
|
|
||||||
setContext(key, {
|
setContext(key, {
|
||||||
getStore: () => store
|
getStore: () => store
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import {
|
|||||||
ConnectionLineType,
|
ConnectionLineType,
|
||||||
devWarn,
|
devWarn,
|
||||||
adoptUserNodes,
|
adoptUserNodes,
|
||||||
getNodesBounds,
|
|
||||||
getViewportForBounds,
|
getViewportForBounds,
|
||||||
updateConnectionLookup,
|
updateConnectionLookup,
|
||||||
type SelectionRect,
|
type SelectionRect,
|
||||||
@@ -22,7 +21,9 @@ import {
|
|||||||
type OnConnectStart,
|
type OnConnectStart,
|
||||||
type OnConnectEnd,
|
type OnConnectEnd,
|
||||||
type NodeLookup,
|
type NodeLookup,
|
||||||
type EdgeLookup
|
type EdgeLookup,
|
||||||
|
type ParentLookup,
|
||||||
|
getInternalNodesBounds
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import DefaultNode from '$lib/components/nodes/DefaultNode.svelte';
|
import DefaultNode from '$lib/components/nodes/DefaultNode.svelte';
|
||||||
@@ -72,18 +73,21 @@ export const getInitialStore = ({
|
|||||||
edges = [],
|
edges = [],
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
fitView
|
fitView,
|
||||||
|
nodeOrigin
|
||||||
}: {
|
}: {
|
||||||
nodes?: Node[];
|
nodes?: Node[];
|
||||||
edges?: Edge[];
|
edges?: Edge[];
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
fitView?: boolean;
|
fitView?: boolean;
|
||||||
|
nodeOrigin?: NodeOrigin;
|
||||||
}) => {
|
}) => {
|
||||||
const nodeLookup: NodeLookup = new Map();
|
const nodeLookup: NodeLookup = new Map();
|
||||||
const parentLookup = new Map();
|
const parentLookup = new Map();
|
||||||
|
const storeNodeOrigin = nodeOrigin ?? [0, 0];
|
||||||
adoptUserNodes(nodes, nodeLookup, parentLookup, {
|
adoptUserNodes(nodes, nodeLookup, parentLookup, {
|
||||||
nodeOrigin: [0, 0],
|
nodeOrigin: storeNodeOrigin,
|
||||||
elevateNodesOnSelect: false,
|
elevateNodesOnSelect: false,
|
||||||
checkEquality: false
|
checkEquality: false
|
||||||
});
|
});
|
||||||
@@ -94,20 +98,17 @@ export const getInitialStore = ({
|
|||||||
let viewport: Viewport = { x: 0, y: 0, zoom: 1 };
|
let viewport: Viewport = { x: 0, y: 0, zoom: 1 };
|
||||||
|
|
||||||
if (fitView && width && height) {
|
if (fitView && width && height) {
|
||||||
const nodesWithDimensions = nodes.filter(
|
const bounds = getInternalNodesBounds(nodeLookup, {
|
||||||
(node) => (node.width && node.height) || (node.initialWidth && node.initialHeight)
|
filter: (node) => !!((node.width || node.initialWidth) && (node.height || node.initialHeight))
|
||||||
);
|
});
|
||||||
|
|
||||||
// @todo users nodeOrigin should be used here
|
|
||||||
const bounds = getNodesBounds(nodesWithDimensions, { nodeOrigin: [0, 0] });
|
|
||||||
viewport = getViewportForBounds(bounds, width, height, 0.5, 2, 0.1);
|
viewport = getViewportForBounds(bounds, width, height, 0.5, 2, 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
flowId: writable<string | null>(null),
|
flowId: writable<string | null>(null),
|
||||||
nodes: createNodesStore(nodes, nodeLookup, parentLookup),
|
nodes: createNodesStore(nodes, nodeLookup, parentLookup, storeNodeOrigin),
|
||||||
nodeLookup: readable<NodeLookup<InternalNode>>(nodeLookup),
|
nodeLookup: readable<NodeLookup<InternalNode>>(nodeLookup),
|
||||||
parentLookup: readable<Map<string, Map<string, InternalNode>>>(parentLookup),
|
parentLookup: readable<ParentLookup<InternalNode>>(parentLookup),
|
||||||
edgeLookup: readable<EdgeLookup<Edge>>(edgeLookup),
|
edgeLookup: readable<EdgeLookup<Edge>>(edgeLookup),
|
||||||
visibleNodes: readable<InternalNode[]>([]),
|
visibleNodes: readable<InternalNode[]>([]),
|
||||||
edges: createEdgesStore(edges, connectionLookup, edgeLookup),
|
edges: createEdgesStore(edges, connectionLookup, edgeLookup),
|
||||||
@@ -117,7 +118,7 @@ export const getInitialStore = ({
|
|||||||
width: writable<number>(500),
|
width: writable<number>(500),
|
||||||
minZoom: writable<number>(0.5),
|
minZoom: writable<number>(0.5),
|
||||||
maxZoom: writable<number>(2),
|
maxZoom: writable<number>(2),
|
||||||
nodeOrigin: writable<NodeOrigin>([0, 0]),
|
nodeOrigin: writable<NodeOrigin>(storeNodeOrigin),
|
||||||
nodeDragThreshold: writable<number>(1),
|
nodeDragThreshold: writable<number>(1),
|
||||||
nodeExtent: writable<CoordinateExtent>(infiniteExtent),
|
nodeExtent: writable<CoordinateExtent>(infiniteExtent),
|
||||||
translateExtent: writable<CoordinateExtent>(infiniteExtent),
|
translateExtent: writable<CoordinateExtent>(infiniteExtent),
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import {
|
|||||||
type ConnectionLookup,
|
type ConnectionLookup,
|
||||||
type EdgeLookup,
|
type EdgeLookup,
|
||||||
type NodeLookup,
|
type NodeLookup,
|
||||||
type ParentLookup
|
type ParentLookup,
|
||||||
|
type NodeOrigin
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, InternalNode, Node } from '$lib/types';
|
import type { DefaultEdgeOptions, DefaultNodeOptions, Edge, InternalNode, Node } from '$lib/types';
|
||||||
@@ -129,7 +130,8 @@ export type NodeStoreOptions = {
|
|||||||
export const createNodesStore = (
|
export const createNodesStore = (
|
||||||
nodes: Node[],
|
nodes: Node[],
|
||||||
nodeLookup: NodeLookup<InternalNode>,
|
nodeLookup: NodeLookup<InternalNode>,
|
||||||
parentLookup: ParentLookup<InternalNode>
|
parentLookup: ParentLookup<InternalNode>,
|
||||||
|
nodeOrigin: NodeOrigin = [0, 0]
|
||||||
): {
|
): {
|
||||||
subscribe: (this: void, run: Subscriber<Node[]>) => Unsubscriber;
|
subscribe: (this: void, run: Subscriber<Node[]>) => Unsubscriber;
|
||||||
update: (this: void, updater: Updater<Node[]>) => void;
|
update: (this: void, updater: Updater<Node[]>) => void;
|
||||||
@@ -145,6 +147,7 @@ export const createNodesStore = (
|
|||||||
const _set = (nds: Node[]): Node[] => {
|
const _set = (nds: Node[]): Node[] => {
|
||||||
adoptUserNodes(nds, nodeLookup, parentLookup, {
|
adoptUserNodes(nds, nodeLookup, parentLookup, {
|
||||||
elevateNodesOnSelect,
|
elevateNodesOnSelect,
|
||||||
|
nodeOrigin,
|
||||||
defaults,
|
defaults,
|
||||||
checkEquality: false
|
checkEquality: false
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user