chore(svelte/nodewrapper): cleanup
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
isInputDOMNode,
|
isInputDOMNode,
|
||||||
nodeHasDimensions,
|
nodeHasDimensions,
|
||||||
Position,
|
Position,
|
||||||
getNodesInside,
|
getNodesInside
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import drag from '$lib/actions/drag';
|
import drag from '$lib/actions/drag';
|
||||||
@@ -200,26 +200,30 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const onFocus = () => {
|
const onFocus = () => {
|
||||||
if (store.disableKeyboardA11y || !store.enablePanOnFocus || !nodeRef?.matches(':focus-visible')) {
|
if (
|
||||||
|
store.disableKeyboardA11y ||
|
||||||
|
!store.enablePanOnFocus ||
|
||||||
|
!nodeRef?.matches(':focus-visible')
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const width = store.width;
|
const { width, height, viewport } = store;
|
||||||
const height = store.height;
|
|
||||||
const viewport: [number, number, number] = [store.viewport.x, store.viewport.y, store.viewport.zoom];
|
|
||||||
const zoom = store.viewport.zoom;
|
|
||||||
|
|
||||||
const visibleNodes = getNodesInside(new Map([[id, node]]), { x: 0, y: 0, width, height }, viewport, true);
|
|
||||||
|
|
||||||
const isNodeVisible = visibleNodes.length > 0;
|
const withinViewport =
|
||||||
|
getNodesInside(
|
||||||
|
new Map([[id, node]]),
|
||||||
|
{ x: 0, y: 0, width, height },
|
||||||
|
[viewport.x, viewport.y, viewport.zoom],
|
||||||
|
true
|
||||||
|
).length > 0;
|
||||||
|
|
||||||
if (!isNodeVisible) {
|
if (!withinViewport) {
|
||||||
store.fitView({
|
store.setCenter(
|
||||||
nodes: [{ id }],
|
node.position.x + (node.measured.width ?? 0) / 2,
|
||||||
duration: 100,
|
node.position.y + (node.measured.height ?? 0) / 2,
|
||||||
minZoom: zoom,
|
{ zoom: viewport.zoom }
|
||||||
maxZoom: zoom,
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -366,28 +366,8 @@ export function useSvelteFlow<NodeType extends Node = Node, EdgeType extends Edg
|
|||||||
return Promise.resolve(true);
|
return Promise.resolve(true);
|
||||||
},
|
},
|
||||||
getViewport: () => $state.snapshot(store.viewport),
|
getViewport: () => $state.snapshot(store.viewport),
|
||||||
setCenter: async (x, y, options) => {
|
setCenter: async (x, y, options) => store.setCenter(x, y, options),
|
||||||
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : store.maxZoom;
|
fitView: (options?: FitViewOptions) => store.fitView(options),
|
||||||
const currentPanZoom = store.panZoom;
|
|
||||||
|
|
||||||
if (!currentPanZoom) {
|
|
||||||
return Promise.resolve(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
await currentPanZoom.setViewport(
|
|
||||||
{
|
|
||||||
x: store.width / 2 - x * nextZoom,
|
|
||||||
y: store.height / 2 - y * nextZoom,
|
|
||||||
zoom: nextZoom
|
|
||||||
},
|
|
||||||
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
|
||||||
);
|
|
||||||
|
|
||||||
return Promise.resolve(true);
|
|
||||||
},
|
|
||||||
fitView: (options?: FitViewOptions) => {
|
|
||||||
return store.fitView(options);
|
|
||||||
},
|
|
||||||
fitBounds: async (bounds: Rect, options?: FitBoundsOptions) => {
|
fitBounds: async (bounds: Rect, options?: FitBoundsOptions) => {
|
||||||
if (!store.panZoom) {
|
if (!store.panZoom) {
|
||||||
return Promise.resolve(false);
|
return Promise.resolve(false);
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import {
|
|||||||
type ConnectionState,
|
type ConnectionState,
|
||||||
updateAbsolutePositions,
|
updateAbsolutePositions,
|
||||||
snapPosition,
|
snapPosition,
|
||||||
calculateNodePosition
|
calculateNodePosition,
|
||||||
|
type SetCenterOptions
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
|
import type { EdgeTypes, NodeTypes, Node, Edge, FitViewOptions } from '$lib/types';
|
||||||
@@ -126,6 +127,26 @@ export function createStore<NodeType extends Node = Node, EdgeType extends Edge
|
|||||||
return fitViewResolver.promise;
|
return fitViewResolver.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setCenter(x: number, y: number, options?: SetCenterOptions) {
|
||||||
|
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : store.maxZoom;
|
||||||
|
const currentPanZoom = store.panZoom;
|
||||||
|
|
||||||
|
if (!currentPanZoom) {
|
||||||
|
return Promise.resolve(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
await currentPanZoom.setViewport(
|
||||||
|
{
|
||||||
|
x: store.width / 2 - x * nextZoom,
|
||||||
|
y: store.height / 2 - y * nextZoom,
|
||||||
|
zoom: nextZoom
|
||||||
|
},
|
||||||
|
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
||||||
|
);
|
||||||
|
|
||||||
|
return Promise.resolve(true);
|
||||||
|
}
|
||||||
|
|
||||||
function zoomBy(factor: number, options?: ViewportHelperFunctionOptions) {
|
function zoomBy(factor: number, options?: ViewportHelperFunctionOptions) {
|
||||||
const panZoom = store.panZoom;
|
const panZoom = store.panZoom;
|
||||||
if (!panZoom) {
|
if (!panZoom) {
|
||||||
@@ -372,6 +393,7 @@ export function createStore<NodeType extends Node = Node, EdgeType extends Edge
|
|||||||
zoomIn,
|
zoomIn,
|
||||||
zoomOut,
|
zoomOut,
|
||||||
fitView,
|
fitView,
|
||||||
|
setCenter,
|
||||||
setMinZoom,
|
setMinZoom,
|
||||||
setMaxZoom,
|
setMaxZoom,
|
||||||
setTranslateExtent,
|
setTranslateExtent,
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import type {
|
|||||||
UpdateNodePositions,
|
UpdateNodePositions,
|
||||||
CoordinateExtent,
|
CoordinateExtent,
|
||||||
UpdateConnection,
|
UpdateConnection,
|
||||||
Viewport
|
Viewport,
|
||||||
|
SetCenter
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type { getInitialStore } from './initial-store.svelte';
|
import type { getInitialStore } from './initial-store.svelte';
|
||||||
@@ -24,6 +25,7 @@ export type SvelteFlowStoreActions<NodeType extends Node = Node, EdgeType extend
|
|||||||
setTranslateExtent: (extent: CoordinateExtent) => void;
|
setTranslateExtent: (extent: CoordinateExtent) => void;
|
||||||
setPaneClickDistance: (distance: number) => void;
|
setPaneClickDistance: (distance: number) => void;
|
||||||
fitView: (options?: FitViewOptions) => Promise<boolean>;
|
fitView: (options?: FitViewOptions) => Promise<boolean>;
|
||||||
|
setCenter: SetCenter;
|
||||||
updateNodePositions: UpdateNodePositions;
|
updateNodePositions: UpdateNodePositions;
|
||||||
updateNodeInternals: (updates: Map<string, InternalNodeUpdate>) => void;
|
updateNodeInternals: (updates: Map<string, InternalNodeUpdate>) => void;
|
||||||
unselectNodesAndEdges: (params?: { nodes?: NodeType[]; edges?: EdgeType[] }) => void;
|
unselectNodesAndEdges: (params?: { nodes?: NodeType[]; edges?: EdgeType[] }) => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user