refactor(screenToFlowPosition): add option to configure snapToGrid #3771

This commit is contained in:
moklick
2024-01-15 12:20:13 +01:00
parent 93d437edb6
commit 0fe2f2ae53
5 changed files with 24 additions and 7 deletions
+8
View File
@@ -2,6 +2,14 @@
## 12.0.0-next.6 ## 12.0.0-next.6
## Minor changes
- pass Node/Edge types to changes thanks @FelipeEmos
- use position instead of positionAbsolute for `getNodesBounds`
- add second option param to `screenToFlowPosition` for configuring if `snapToGrid` should be used
## 12.0.0-next.6
### Minor changes ### Minor changes
- fix `deleteElements` - fix `deleteElements`
@@ -86,8 +86,8 @@ const useViewportHelper = (): ViewportHelperFunctions => {
panZoom?.setViewport(viewport, { duration: options?.duration }); panZoom?.setViewport(viewport, { duration: options?.duration });
}, },
screenToFlowPosition: (position: XYPosition) => { screenToFlowPosition: (position: XYPosition, options: { snapToGrid: boolean } = { snapToGrid: true }) => {
const { transform, snapToGrid, snapGrid, domNode } = store.getState(); const { transform, snapGrid, domNode } = store.getState();
if (!domNode) { if (!domNode) {
return position; return position;
@@ -100,7 +100,7 @@ const useViewportHelper = (): ViewportHelperFunctions => {
y: position.y - domY, y: position.y - domY,
}; };
return pointToRendererPoint(correctedPosition, transform, snapToGrid, snapGrid || [1, 1]); return pointToRendererPoint(correctedPosition, transform, options.snapToGrid, snapGrid);
}, },
flowToScreenPosition: (position: XYPosition) => { flowToScreenPosition: (position: XYPosition) => {
const { transform, domNode } = store.getState(); const { transform, domNode } = store.getState();
+1 -1
View File
@@ -55,7 +55,7 @@ export type ViewportHelperFunctions = {
fitView: FitView; fitView: FitView;
setCenter: SetCenter; setCenter: SetCenter;
fitBounds: FitBounds; fitBounds: FitBounds;
screenToFlowPosition: (position: XYPosition) => XYPosition; screenToFlowPosition: (position: XYPosition, options?: { snapToGrid: boolean }) => XYPosition;
flowToScreenPosition: (position: XYPosition) => XYPosition; flowToScreenPosition: (position: XYPosition) => XYPosition;
viewportInitialized: boolean; viewportInitialized: boolean;
}; };
+6
View File
@@ -1,5 +1,11 @@
# @xyflow/svelte # @xyflow/svelte
## 0.0.34
## Minor changes
- add second option param to `screenToFlowPosition` for configuring if `snapToGrid` should be used
## 0.0.33 ## 0.0.33
### Bugfix ### Bugfix
@@ -53,7 +53,7 @@ export function useSvelteFlow(): {
nodes?: (Node | { id: Node['id'] })[]; nodes?: (Node | { id: Node['id'] })[];
edges?: (Edge | { id: Edge['id'] })[]; edges?: (Edge | { id: Edge['id'] })[];
}) => Promise<{ deletedNodes: Node[]; deletedEdges: Edge[] }>; }) => Promise<{ deletedNodes: Node[]; deletedEdges: Edge[] }>;
screenToFlowPosition: (position: XYPosition) => XYPosition; screenToFlowPosition: (position: XYPosition, options?: { snapToGrid: boolean }) => XYPosition;
flowToScreenPosition: (position: XYPosition) => XYPosition; flowToScreenPosition: (position: XYPosition) => XYPosition;
viewport: Writable<Viewport>; viewport: Writable<Viewport>;
updateNode: ( updateNode: (
@@ -228,14 +228,17 @@ export function useSvelteFlow(): {
deletedEdges: matchingEdges deletedEdges: matchingEdges
}; };
}, },
screenToFlowPosition: (position: XYPosition) => { screenToFlowPosition: (
position: XYPosition,
options: { snapToGrid: boolean } = { snapToGrid: true }
) => {
const _domNode = get(domNode); const _domNode = get(domNode);
if (!_domNode) { if (!_domNode) {
return position; return position;
} }
const _snapGrid = get(snapGrid); const _snapGrid = options.snapToGrid ? get(snapGrid) : false;
const { x, y, zoom } = get(viewport); const { x, y, zoom } = get(viewport);
const { x: domX, y: domY } = _domNode.getBoundingClientRect(); const { x: domX, y: domY } = _domNode.getBoundingClientRect();
const correctedPosition = { const correctedPosition = {