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