feat(svelte): add onlyRenderVisibleElements option

This commit is contained in:
moklick
2023-06-08 16:31:39 +02:00
parent 7e64969cd3
commit 0739528761
8 changed files with 33 additions and 5 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ A basic flow looks like this:
custom: CustomNode custom: CustomNode
}; };
// We are using writables for the nodes to sync them easily. When a user drags a node for example, Svelte Flow updates its position. // We are using writables for the nodes and edges to sync them easily. When a user drags a node for example, Svelte Flow updates its position.
// This also makes it easier to update nodes in user land. // This also makes it easier to update nodes in user land.
const nodes = writable([ const nodes = writable([
{ {
@@ -5,8 +5,13 @@
import { NodeWrapper } from '$lib/components/NodeWrapper'; import { NodeWrapper } from '$lib/components/NodeWrapper';
import { useStore } from '$lib/store'; import { useStore } from '$lib/store';
const { nodes, nodesDraggable, nodesConnectable, elementsSelectable, updateNodeDimensions } = const {
useStore(); visibleNodes,
nodesDraggable,
nodesConnectable,
elementsSelectable,
updateNodeDimensions
} = useStore();
const resizeObserver: ResizeObserver | null = const resizeObserver: ResizeObserver | null =
typeof ResizeObserver === 'undefined' typeof ResizeObserver === 'undefined'
@@ -26,7 +31,7 @@
</script> </script>
<div class="svelte-flow__nodes"> <div class="svelte-flow__nodes">
{#each $nodes as node (node.id)} {#each $visibleNodes as node (node.id)}
{@const posOrigin = getPositionWithOrigin({ {@const posOrigin = getPositionWithOrigin({
x: node.positionAbsolute?.x ?? 0, x: node.positionAbsolute?.x ?? 0,
y: node.positionAbsolute?.y ?? 0, y: node.positionAbsolute?.y ?? 0,
@@ -42,6 +42,7 @@
export let onMoveEnd: $$Props['onMoveEnd'] = undefined; export let onMoveEnd: $$Props['onMoveEnd'] = undefined;
export let isValidConnection: $$Props['isValidConnection'] = undefined; export let isValidConnection: $$Props['isValidConnection'] = undefined;
export let translateExtent: $$Props['translateExtent'] = undefined; export let translateExtent: $$Props['translateExtent'] = undefined;
export let onlyRenderVisibleElements: $$Props['onlyRenderVisibleElements'] = undefined;
export let panOnScrollMode: PanOnScrollMode = PanOnScrollMode.Free; export let panOnScrollMode: PanOnScrollMode = PanOnScrollMode.Free;
export let preventScrolling: boolean = true; export let preventScrolling: boolean = true;
export let zoomOnScroll: boolean = true; export let zoomOnScroll: boolean = true;
@@ -99,6 +100,7 @@
nodesDraggable, nodesDraggable,
nodesConnectable, nodesConnectable,
elementsSelectable, elementsSelectable,
onlyRenderVisibleElements,
isValidConnection isValidConnection
}; };
@@ -46,6 +46,7 @@ export type SvelteFlowProps = DOMAttributes<HTMLDivElement> & {
zoomOnPinch?: boolean; zoomOnPinch?: boolean;
panOnScroll?: boolean; panOnScroll?: boolean;
panOnDrag?: boolean | number[]; panOnDrag?: boolean | number[];
onlyRenderVisibleElements?: boolean;
class?: string; class?: string;
style?: string; style?: string;
@@ -57,6 +57,7 @@ export type UpdatableStoreProps = {
nodesDraggable?: UnwrapWritable<SvelteFlowStore['nodesDraggable']>; nodesDraggable?: UnwrapWritable<SvelteFlowStore['nodesDraggable']>;
nodesConnectable?: UnwrapWritable<SvelteFlowStore['nodesConnectable']>; nodesConnectable?: UnwrapWritable<SvelteFlowStore['nodesConnectable']>;
elementsSelectable?: UnwrapWritable<SvelteFlowStore['elementsSelectable']>; elementsSelectable?: UnwrapWritable<SvelteFlowStore['elementsSelectable']>;
onlyRenderVisibleElements?: UnwrapWritable<SvelteFlowStore['onlyRenderVisibleElements']>;
isValidConnection?: UnwrapWritable<SvelteFlowStore['isValidConnection']>; isValidConnection?: UnwrapWritable<SvelteFlowStore['isValidConnection']>;
}; };
+2
View File
@@ -29,6 +29,7 @@ import {
import type { SvelteFlowStore } from './types'; import type { SvelteFlowStore } from './types';
import { syncNodeStores, syncEdgeStores } from './utils'; import { syncNodeStores, syncEdgeStores } from './utils';
import { getEdgeTree } from './edge-tree'; import { getEdgeTree } from './edge-tree';
import { getVisibleNodes } from './visible-nodes';
export const key = Symbol(); export const key = Symbol();
@@ -343,6 +344,7 @@ export function createStore(): SvelteFlowStore {
// derived state // derived state
edgeTree: getEdgeTree(store, onError), edgeTree: getEdgeTree(store, onError),
connectionPath: getConnectionPath(store), connectionPath: getConnectionPath(store),
visibleNodes: getVisibleNodes(store),
markers: derived( markers: derived(
[store.edges, store.defaultMarkerColor, store.flowId], [store.edges, store.defaultMarkerColor, store.flowId],
([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id }) ([edges, defaultColor, id]) => createMarkerIds(edges, { defaultColor, id })
@@ -21,7 +21,7 @@ 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 } from '$lib/types'; import type { ConnectionData, NodeTypes, EdgeTypes, EdgeLayouted, Node } from '$lib/types';
import { createNodes, createEdges } from './utils'; import { createNodes, createEdges } from './utils';
export const initConnectionData = { export const initConnectionData = {
@@ -47,6 +47,7 @@ export const initialEdgeTypes = {
export const getInitialStore = () => ({ export const getInitialStore = () => ({
flowId: writable<string | null>(null), flowId: writable<string | null>(null),
nodes: createNodes([]), nodes: createNodes([]),
visibleNodes: readable<Node[]>([]),
edges: createEdges([]), edges: createEdges([]),
edgeTree: readable<GroupedEdges<EdgeLayouted>[]>([]), edgeTree: readable<GroupedEdges<EdgeLayouted>[]>([]),
height: writable<number>(500), height: writable<number>(500),
@@ -0,0 +1,16 @@
import { derived } from 'svelte/store';
import { getNodesInside } from '@xyflow/system';
import type { Node } from '$lib/types';
import type { SvelteFlowStoreState } from './types';
export function getVisibleNodes(store: SvelteFlowStoreState) {
return derived(
[store.nodes, store.onlyRenderVisibleElements, store.width, store.height, store.transform],
([nodes, onlyRenderVisibleElements, width, height, transform]) => {
return onlyRenderVisibleElements
? getNodesInside<Node>(nodes, { x: 0, y: 0, width, height }, transform, true)
: nodes;
}
);
}