Merge branch 'xyflow' into svelte-flow-get-intersecting-nodes
This commit is contained in:
@@ -2,7 +2,6 @@ import type { ReactNode } from 'react';
|
||||
|
||||
import { useStore } from '../../hooks/useStore';
|
||||
import type { ReactFlowState } from '../../types';
|
||||
import { Viewport } from '@xyflow/system';
|
||||
|
||||
const selector = (s: ReactFlowState) => `translate(${s.transform[0]}px,${s.transform[1]}px) scale(${s.transform[2]})`;
|
||||
|
||||
@@ -10,7 +9,7 @@ type ViewportProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
function Viewport({ children }: ViewportProps) {
|
||||
export default function Viewport({ children }: ViewportProps) {
|
||||
const transform = useStore(selector);
|
||||
|
||||
return (
|
||||
@@ -19,5 +18,3 @@ function Viewport({ children }: ViewportProps) {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Viewport;
|
||||
|
||||
@@ -164,6 +164,8 @@ export default function useReactFlow<NodeData = any, EdgeData = any>(): ReactFlo
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { deletedNodes: matchingNodes, deletedEdges: matchingEdges };
|
||||
}, []);
|
||||
|
||||
const getNodeRect = useCallback(
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { useMemo } from 'react';
|
||||
import { pointToRendererPoint, getTransformForBounds, fitView, type XYPosition } from '@xyflow/system';
|
||||
import {
|
||||
pointToRendererPoint,
|
||||
getTransformForBounds,
|
||||
fitView,
|
||||
type XYPosition,
|
||||
rendererPointToPoint,
|
||||
} from '@xyflow/system';
|
||||
|
||||
import { useStoreApi, useStore } from '../hooks/useStore';
|
||||
import type { ViewportHelperFunctions, ReactFlowState } from '../types';
|
||||
@@ -85,6 +91,36 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
||||
const { transform, snapToGrid, snapGrid } = store.getState();
|
||||
return pointToRendererPoint(position, transform, snapToGrid, snapGrid);
|
||||
},
|
||||
screenToFlowCoordinate: (position: XYPosition) => {
|
||||
const { transform, snapToGrid, snapGrid, domNode } = store.getState();
|
||||
if (domNode) {
|
||||
const { x: domX, y: domY } = domNode.getBoundingClientRect();
|
||||
|
||||
const correctedPosition = {
|
||||
x: position.x - domX,
|
||||
y: position.y - domY,
|
||||
};
|
||||
|
||||
return pointToRendererPoint(correctedPosition, transform, snapToGrid, snapGrid || [1, 1]);
|
||||
}
|
||||
|
||||
return { x: 0, y: 0 };
|
||||
},
|
||||
flowToScreenCoordinate: (position: XYPosition) => {
|
||||
const { transform, domNode } = store.getState();
|
||||
if (domNode) {
|
||||
const { x: domX, y: domY } = domNode.getBoundingClientRect();
|
||||
|
||||
const rendererPosition = rendererPointToPoint(position, transform);
|
||||
|
||||
return {
|
||||
x: rendererPosition.x + domX,
|
||||
y: rendererPosition.y + domY,
|
||||
};
|
||||
}
|
||||
|
||||
return { x: 0, y: 0 };
|
||||
},
|
||||
viewportInitialized: panZoomInitialized,
|
||||
};
|
||||
}, [panZoomInitialized]);
|
||||
|
||||
@@ -55,5 +55,7 @@ export type ViewportHelperFunctions = {
|
||||
setCenter: SetCenter;
|
||||
fitBounds: FitBounds;
|
||||
project: Project;
|
||||
screenToFlowCoordinate: Project;
|
||||
flowToScreenCoordinate: Project;
|
||||
viewportInitialized: boolean;
|
||||
};
|
||||
|
||||
@@ -28,7 +28,10 @@ export namespace Instance {
|
||||
export type GetEdge<EdgeData> = (id: string) => Edge<EdgeData> | undefined;
|
||||
export type AddEdges<EdgeData> = (payload: Edge<EdgeData>[] | Edge<EdgeData>) => void;
|
||||
export type ToObject<NodeData = any, EdgeData = any> = () => ReactFlowJsonObject<NodeData, EdgeData>;
|
||||
export type DeleteElements = ({ nodes, edges }: DeleteElementsOptions) => void;
|
||||
export type DeleteElements = ({ nodes, edges }: DeleteElementsOptions) => {
|
||||
deletedNodes: Node[];
|
||||
deletedEdges: Edge[];
|
||||
};
|
||||
export type GetIntersectingNodes<NodeData> = (
|
||||
node: (Partial<Node<NodeData>> & { id: Node['id'] }) | Rect,
|
||||
partially?: boolean,
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
|
||||
{#if $connection.path}
|
||||
<svg width={$width} height={$height} class="svelte-flow__connectionline" style={containerStyle}>
|
||||
<g class={cc(['svelte-flow__connection', $connection.status])} {style}>
|
||||
<g class={cc(['svelte-flow__connection', $connection.status])}>
|
||||
<slot name="connectionLine" />
|
||||
<!-- slot fallbacks do not work if slots are forwarded in parent -->
|
||||
{#if !isCustomComponent}
|
||||
<path d={$connection.path} fill="none" class="svelte-flow__connection-path" />
|
||||
<path d={$connection.path} {style} fill="none" class="svelte-flow__connection-path" />
|
||||
{/if}
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
<svelte:options immutable />
|
||||
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import { getMarkerId } from '@xyflow/system';
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
<svelte:options immutable />
|
||||
|
||||
<script lang="ts">
|
||||
import {
|
||||
createEventDispatcher,
|
||||
|
||||
@@ -71,13 +71,14 @@
|
||||
export { className as class };
|
||||
|
||||
let domNode: HTMLDivElement;
|
||||
let clientWidth: number;
|
||||
let clientHeight: number;
|
||||
|
||||
const store = hasContext(key) ? useStore() : createStoreContext();
|
||||
|
||||
onMount(() => {
|
||||
const { width, height } = domNode.getBoundingClientRect();
|
||||
store.width.set(width);
|
||||
store.height.set(height);
|
||||
store.width.set(clientWidth);
|
||||
store.height.set(clientHeight);
|
||||
store.domNode.set(domNode);
|
||||
|
||||
store.syncNodeStores(nodes);
|
||||
@@ -141,6 +142,8 @@
|
||||
|
||||
<div
|
||||
bind:this={domNode}
|
||||
bind:clientWidth
|
||||
bind:clientHeight
|
||||
{style}
|
||||
class={cc(['svelte-flow', className])}
|
||||
data-testid="svelte-flow__wrapper"
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { useStore } from '$lib/store';
|
||||
|
||||
export function useNodes() {
|
||||
const { nodes } = useStore();
|
||||
return nodes;
|
||||
}
|
||||
|
||||
export function useEdges() {
|
||||
const { edges } = useStore();
|
||||
return edges;
|
||||
}
|
||||
@@ -4,18 +4,20 @@ import {
|
||||
isRectObject,
|
||||
nodeToRect,
|
||||
pointToRendererPoint,
|
||||
type Project,
|
||||
type Rect,
|
||||
type FitBoundsOptions,
|
||||
type SetCenterOptions,
|
||||
type Viewport,
|
||||
type ViewportHelperFunctionOptions,
|
||||
type XYPosition,
|
||||
type ZoomInOut
|
||||
type ZoomInOut,
|
||||
type Rect,
|
||||
getTransformForBounds,
|
||||
getElementsToRemove,
|
||||
rendererPointToPoint
|
||||
} from '@xyflow/system';
|
||||
|
||||
import { useStore } from '$lib/store';
|
||||
import type { FitViewOptions, Node } from '$lib/types';
|
||||
import type { SvelteFlowStore } from '$lib/store/types';
|
||||
import type { Edge, FitViewOptions, Node } from '$lib/types';
|
||||
|
||||
export function useSvelteFlow(): {
|
||||
zoomIn: ZoomInOut;
|
||||
@@ -27,19 +29,23 @@ export function useSvelteFlow(): {
|
||||
getViewport: () => Viewport;
|
||||
fitView: (options?: FitViewOptions) => void;
|
||||
getIntersectingNodes: (
|
||||
nodeOrRect: (Partial<Node<any>> & { id: Node['id'] }) | Rect,
|
||||
nodeOrRect: (Partial<Node> & { id: Node['id'] }) | Rect,
|
||||
partially?: boolean,
|
||||
nodesToIntersect?: Node[]
|
||||
) => Node[];
|
||||
isNodeIntersecting: (
|
||||
nodeOrRect: (Partial<Node<any>> & { id: Node['id'] }) | Rect,
|
||||
nodeOrRect: (Partial<Node> & { id: Node['id'] }) | Rect,
|
||||
area: Rect,
|
||||
partially?: boolean
|
||||
) => boolean;
|
||||
project: Project;
|
||||
fitBounds: (bounds: Rect, options?: FitBoundsOptions) => void;
|
||||
deleteElements: (
|
||||
nodesToRemove?: Partial<Node> & { id: string }[],
|
||||
edgesToRemove?: Partial<Edge> & { id: string }[]
|
||||
) => { deletedNodes: Node[]; deletedEdges: Edge[] };
|
||||
screenToFlowCoordinate: (position: XYPosition) => XYPosition;
|
||||
flowToScreenCoordinate: (position: XYPosition) => XYPosition;
|
||||
viewport: Writable<Viewport>;
|
||||
nodes: SvelteFlowStore['nodes'];
|
||||
edges: SvelteFlowStore['edges'];
|
||||
} {
|
||||
const {
|
||||
zoomIn,
|
||||
@@ -49,20 +55,22 @@ export function useSvelteFlow(): {
|
||||
viewport,
|
||||
width,
|
||||
height,
|
||||
minZoom,
|
||||
maxZoom,
|
||||
panZoom,
|
||||
nodes,
|
||||
edges
|
||||
edges,
|
||||
domNode
|
||||
} = useStore();
|
||||
|
||||
const getNodeRect = (
|
||||
nodeOrRect: (Partial<Node<any>> & { id: Node['id'] }) | Rect
|
||||
): [Rect | null, Node<any> | null | undefined, boolean] => {
|
||||
nodeOrRect: (Partial<Node> & { id: Node['id'] }) | Rect
|
||||
): [Rect | null, Node | null | undefined, boolean] => {
|
||||
const isRect = isRectObject(nodeOrRect);
|
||||
const node = isRect ? null : get(nodes).find((n) => n.id === nodeOrRect.id);
|
||||
|
||||
if (!isRect && !node) {
|
||||
[null, null, isRect];
|
||||
return [null, null, isRect];
|
||||
}
|
||||
|
||||
const nodeRect = isRect ? nodeOrRect : nodeToRect(node!);
|
||||
@@ -107,19 +115,43 @@ export function useSvelteFlow(): {
|
||||
);
|
||||
},
|
||||
fitView,
|
||||
fitBounds: (bounds: Rect, options?: FitBoundsOptions) => {
|
||||
const _width = get(width);
|
||||
const _height = get(height);
|
||||
const _maxZoom = get(maxZoom);
|
||||
const _minZoom = get(minZoom);
|
||||
|
||||
const [x, y, zoom] = getTransformForBounds(
|
||||
bounds,
|
||||
_width,
|
||||
_height,
|
||||
_minZoom,
|
||||
_maxZoom,
|
||||
options?.padding ?? 0.1
|
||||
);
|
||||
|
||||
get(panZoom)?.setViewport(
|
||||
{
|
||||
x,
|
||||
y,
|
||||
zoom
|
||||
},
|
||||
{ duration: options?.duration }
|
||||
);
|
||||
},
|
||||
getIntersectingNodes: (
|
||||
nodeOrRect: (Partial<Node<any>> & { id: Node['id'] }) | Rect,
|
||||
nodeOrRect: (Partial<Node> & { id: Node['id'] }) | Rect,
|
||||
partially = true,
|
||||
nodesToIntersect?: Node[]
|
||||
) => {
|
||||
const [nodeRect, node, isRect] = getNodeRect(nodeOrRect);
|
||||
|
||||
if (!nodeRect) {
|
||||
if (!nodeRect || !node) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return (nodesToIntersect || get(nodes)).filter((n) => {
|
||||
if (!isRect && (n.id === node!.id || !n.positionAbsolute)) {
|
||||
if (!isRect && (n.id === node.id || !n.positionAbsolute)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -131,7 +163,7 @@ export function useSvelteFlow(): {
|
||||
});
|
||||
},
|
||||
isNodeIntersecting: (
|
||||
nodeOrRect: (Partial<Node<any>> & { id: Node['id'] }) | Rect,
|
||||
nodeOrRect: (Partial<Node> & { id: Node['id'] }) | Rect,
|
||||
area: Rect,
|
||||
partially = true
|
||||
) => {
|
||||
@@ -146,14 +178,70 @@ export function useSvelteFlow(): {
|
||||
|
||||
return partiallyVisible || overlappingArea >= nodeOrRect.width! * nodeOrRect.height!;
|
||||
},
|
||||
project: (position: XYPosition) => {
|
||||
const _snapGrid = get(snapGrid);
|
||||
const { x, y, zoom } = get(viewport);
|
||||
deleteElements: (
|
||||
nodesToRemove: Partial<Node> & { id: string }[] = [],
|
||||
edgesToRemove: Partial<Edge> & { id: string }[] = []
|
||||
) => {
|
||||
const _nodes = get(nodes);
|
||||
const _edges = get(edges);
|
||||
const { matchingNodes, matchingEdges } = getElementsToRemove<Node, Edge>({
|
||||
nodesToRemove,
|
||||
edgesToRemove,
|
||||
nodes: _nodes,
|
||||
edges: _edges
|
||||
});
|
||||
|
||||
return pointToRendererPoint(position, [x, y, zoom], _snapGrid !== null, _snapGrid || [1, 1]);
|
||||
if (matchingNodes) {
|
||||
nodes.set(_nodes.filter((node) => !matchingNodes.some(({ id }) => id === node.id)));
|
||||
}
|
||||
|
||||
if (matchingEdges) {
|
||||
edges.set(_edges.filter((edge) => !matchingEdges.some(({ id }) => id === edge.id)));
|
||||
}
|
||||
|
||||
return {
|
||||
deletedNodes: matchingNodes,
|
||||
deletedEdges: matchingEdges
|
||||
};
|
||||
},
|
||||
screenToFlowCoordinate: (position: XYPosition) => {
|
||||
const _domNode = get(domNode);
|
||||
if (_domNode) {
|
||||
const _snapGrid = get(snapGrid);
|
||||
const { x, y, zoom } = get(viewport);
|
||||
const { x: domX, y: domY } = _domNode.getBoundingClientRect();
|
||||
|
||||
const correctedPosition = {
|
||||
x: position.x - domX,
|
||||
y: position.y - domY
|
||||
};
|
||||
|
||||
return pointToRendererPoint(
|
||||
correctedPosition,
|
||||
[x, y, zoom],
|
||||
_snapGrid !== null,
|
||||
_snapGrid || [1, 1]
|
||||
);
|
||||
}
|
||||
|
||||
return { x: 0, y: 0 };
|
||||
},
|
||||
flowToScreenCoordinate: (position: XYPosition) => {
|
||||
const _domNode = get(domNode);
|
||||
if (_domNode) {
|
||||
const { x, y, zoom } = get(viewport);
|
||||
const { x: domX, y: domY } = _domNode.getBoundingClientRect();
|
||||
|
||||
const rendererPosition = rendererPointToPoint(position, [x, y, zoom]);
|
||||
|
||||
return {
|
||||
x: rendererPosition.x + domX,
|
||||
y: rendererPosition.y + domY
|
||||
};
|
||||
}
|
||||
|
||||
return { x: 0, y: 0 };
|
||||
},
|
||||
nodes,
|
||||
edges,
|
||||
viewport: viewport
|
||||
};
|
||||
}
|
||||
|
||||
@@ -19,9 +19,12 @@ export { useStore } from '$lib/store';
|
||||
|
||||
// utils
|
||||
export * from '$lib/utils';
|
||||
|
||||
//hooks
|
||||
export * from '$lib/hooks/useSvelteFlow';
|
||||
export * from '$lib/hooks/useUpdateNodeInternals';
|
||||
export * from '$lib/hooks/useConnection';
|
||||
export * from '$lib/hooks/useNodesEdges';
|
||||
|
||||
// types
|
||||
export type { Edge, EdgeProps, EdgeTypes, DefaultEdgeOptions } from '$lib/types/edges';
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
.svelte-flow__edge-label {
|
||||
text-align: center;
|
||||
position: absolute;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user