chore(nodewrapper): cleanup
This commit is contained in:
@@ -78,8 +78,6 @@ const A11y = () => {
|
|||||||
<ReactFlow
|
<ReactFlow
|
||||||
defaultNodes={initialNodes}
|
defaultNodes={initialNodes}
|
||||||
defaultEdges={initialEdges}
|
defaultEdges={initialEdges}
|
||||||
minZoom={2}
|
|
||||||
maxZoom={4}
|
|
||||||
enablePanOnFocus={isFocusPannable}
|
enablePanOnFocus={isFocusPannable}
|
||||||
selectNodesOnDrag={false}
|
selectNodesOnDrag={false}
|
||||||
elevateEdgesOnSelect
|
elevateEdgesOnSelect
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import { handleNodeClick } from '../Nodes/utils';
|
|||||||
import { arrowKeyDiffs, builtinNodeTypes, getNodeInlineStyleDimensions } from './utils';
|
import { arrowKeyDiffs, builtinNodeTypes, getNodeInlineStyleDimensions } from './utils';
|
||||||
import { useNodeObserver } from './useNodeObserver';
|
import { useNodeObserver } from './useNodeObserver';
|
||||||
import type { InternalNode, Node, NodeWrapperProps } from '../../types';
|
import type { InternalNode, Node, NodeWrapperProps } from '../../types';
|
||||||
import { useReactFlow } from '../../hooks/useReactFlow';
|
|
||||||
|
|
||||||
export function NodeWrapper<NodeType extends Node>({
|
export function NodeWrapper<NodeType extends Node>({
|
||||||
id,
|
id,
|
||||||
@@ -81,8 +80,6 @@ export function NodeWrapper<NodeType extends Node>({
|
|||||||
nodeClickDistance,
|
nodeClickDistance,
|
||||||
});
|
});
|
||||||
const moveSelectedNodes = useMoveSelectedNodes();
|
const moveSelectedNodes = useMoveSelectedNodes();
|
||||||
const { fitView } = useReactFlow();
|
|
||||||
const { getViewport } = useReactFlow();
|
|
||||||
|
|
||||||
if (node.hidden) {
|
if (node.hidden) {
|
||||||
return null;
|
return null;
|
||||||
@@ -165,26 +162,17 @@ export function NodeWrapper<NodeType extends Node>({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onFocus = () => {
|
const onFocus = () => {
|
||||||
if (disableKeyboardA11y || !enablePanOnFocus) {
|
if (disableKeyboardA11y || !enablePanOnFocus || !nodeRef.current?.matches(':focus-visible')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nodeRef.current?.matches(':focus-visible')) {
|
const { transform, width, height, setCenter } = store.getState();
|
||||||
return;
|
const withinViewport =
|
||||||
}
|
getNodesInside(new Map([[id, node]]), { x: 0, y: 0, width, height }, transform, true).length > 0;
|
||||||
|
|
||||||
const { transform, width, height } = store.getState();
|
if (!withinViewport) {
|
||||||
const visibleNodes = getNodesInside(new Map([[id, node]]), { x: 0, y: 0, width, height }, transform, true);
|
setCenter(node.position.x + nodeDimensions.width / 2, node.position.y + nodeDimensions.height / 2, {
|
||||||
|
zoom: transform[2],
|
||||||
const isNodeVisible = visibleNodes.length > 0;
|
|
||||||
|
|
||||||
if (!isNodeVisible) {
|
|
||||||
const zoomLevel = transform[2];
|
|
||||||
fitView({
|
|
||||||
nodes: [{ id }],
|
|
||||||
duration: 100,
|
|
||||||
minZoom: zoomLevel,
|
|
||||||
maxZoom: zoomLevel,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -63,25 +63,7 @@ const useViewportHelper = (): ViewportHelperFunctions => {
|
|||||||
return { x, y, zoom };
|
return { x, y, zoom };
|
||||||
},
|
},
|
||||||
setCenter: async (x, y, options) => {
|
setCenter: async (x, y, options) => {
|
||||||
const { width, height, maxZoom, panZoom } = store.getState();
|
return store.getState().setCenter(x, y, options);
|
||||||
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : maxZoom;
|
|
||||||
const centerX = width / 2 - x * nextZoom;
|
|
||||||
const centerY = height / 2 - y * nextZoom;
|
|
||||||
|
|
||||||
if (!panZoom) {
|
|
||||||
return Promise.resolve(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
await panZoom.setViewport(
|
|
||||||
{
|
|
||||||
x: centerX,
|
|
||||||
y: centerY,
|
|
||||||
zoom: nextZoom,
|
|
||||||
},
|
|
||||||
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
|
||||||
);
|
|
||||||
|
|
||||||
return Promise.resolve(true);
|
|
||||||
},
|
},
|
||||||
fitBounds: async (bounds, options) => {
|
fitBounds: async (bounds, options) => {
|
||||||
const { width, height, minZoom, maxZoom, panZoom } = store.getState();
|
const { width, height, minZoom, maxZoom, panZoom } = store.getState();
|
||||||
|
|||||||
@@ -360,6 +360,26 @@ const createStore = ({
|
|||||||
|
|
||||||
return panBySystem({ delta, panZoom, transform, translateExtent, width, height });
|
return panBySystem({ delta, panZoom, transform, translateExtent, width, height });
|
||||||
},
|
},
|
||||||
|
setCenter: async (x, y, options) => {
|
||||||
|
const { width, height, maxZoom, panZoom } = get();
|
||||||
|
|
||||||
|
if (!panZoom) {
|
||||||
|
return Promise.resolve(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextZoom = typeof options?.zoom !== 'undefined' ? options.zoom : maxZoom;
|
||||||
|
|
||||||
|
await panZoom.setViewport(
|
||||||
|
{
|
||||||
|
x: width / 2 - x * nextZoom,
|
||||||
|
y: height / 2 - y * nextZoom,
|
||||||
|
zoom: nextZoom,
|
||||||
|
},
|
||||||
|
{ duration: options?.duration, ease: options?.ease, interpolate: options?.interpolate }
|
||||||
|
);
|
||||||
|
|
||||||
|
return Promise.resolve(true);
|
||||||
|
},
|
||||||
cancelConnection: () => {
|
cancelConnection: () => {
|
||||||
set({
|
set({
|
||||||
connection: { ...initialConnection },
|
connection: { ...initialConnection },
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import {
|
|||||||
type EdgeChange,
|
type EdgeChange,
|
||||||
type ParentLookup,
|
type ParentLookup,
|
||||||
type AriaLabelConfig,
|
type AriaLabelConfig,
|
||||||
|
SetCenter,
|
||||||
} from '@xyflow/system';
|
} from '@xyflow/system';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
@@ -172,6 +173,7 @@ export type ReactFlowActions<NodeType extends Node, EdgeType extends Edge> = {
|
|||||||
triggerNodeChanges: (changes: NodeChange<NodeType>[]) => void;
|
triggerNodeChanges: (changes: NodeChange<NodeType>[]) => void;
|
||||||
triggerEdgeChanges: (changes: EdgeChange<EdgeType>[]) => void;
|
triggerEdgeChanges: (changes: EdgeChange<EdgeType>[]) => void;
|
||||||
panBy: PanBy;
|
panBy: PanBy;
|
||||||
|
setCenter: SetCenter;
|
||||||
setPaneClickDistance: (distance: number) => void;
|
setPaneClickDistance: (distance: number) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ module.exports = {
|
|||||||
projectService: true,
|
projectService: true,
|
||||||
},
|
},
|
||||||
rules: {
|
rules: {
|
||||||
'@typescript-eslint/no-deprecated': 'error',
|
|
||||||
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
|
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user