@appulsauce/snap-guides 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +34 -0
- package/README.md +99 -2
- package/dist/core/draw.d.ts +60 -0
- package/dist/core/draw.js +57 -0
- package/dist/core/gaps.d.ts +38 -0
- package/dist/core/gaps.js +261 -0
- package/dist/core/index.d.ts +8 -0
- package/dist/core/index.js +7 -0
- package/dist/core/snap.d.ts +49 -0
- package/dist/core/snap.js +362 -0
- package/dist/core/types.d.ts +121 -0
- package/dist/core/types.js +1 -0
- package/dist/flow/boxes.d.ts +40 -0
- package/dist/flow/boxes.js +45 -0
- package/dist/flow/gestures.d.ts +52 -0
- package/dist/flow/gestures.js +80 -0
- package/dist/flow/index.d.ts +4 -0
- package/dist/flow/index.js +4 -0
- package/dist/flow/xyflow.d.ts +70 -0
- package/dist/flow/xyflow.js +52 -0
- package/dist/vue-flow/guide-state.d.ts +14 -0
- package/dist/vue-flow/guide-state.js +16 -0
- package/dist/vue-flow/index.d.ts +4 -0
- package/dist/vue-flow/index.js +4 -0
- package/dist/vue-flow/nodes.d.ts +29 -0
- package/dist/vue-flow/nodes.js +28 -0
- package/dist/vue-flow/snap-guides.d.ts +25 -0
- package/dist/vue-flow/snap-guides.js +54 -0
- package/dist/vue-flow/use-node-resize-snap.d.ts +23 -0
- package/dist/vue-flow/use-node-resize-snap.js +83 -0
- package/dist/vue-flow/use-node-snap.d.ts +15 -0
- package/dist/vue-flow/use-node-snap.js +67 -0
- package/dist/xyflow-react/index.d.ts +4 -0
- package/dist/xyflow-react/index.js +4 -0
- package/dist/xyflow-react/snap-guides.d.ts +10 -0
- package/dist/xyflow-react/snap-guides.js +43 -0
- package/dist/xyflow-react/use-node-resize-snap.d.ts +26 -0
- package/dist/xyflow-react/use-node-resize-snap.js +78 -0
- package/dist/xyflow-react/use-node-snap.d.ts +23 -0
- package/dist/xyflow-react/use-node-snap.js +62 -0
- package/dist/xyflow-svelte/SnapGuides.svelte +54 -0
- package/dist/xyflow-svelte/SnapGuides.svelte.d.ts +8 -0
- package/dist/xyflow-svelte/index.d.ts +4 -0
- package/dist/xyflow-svelte/index.js +4 -0
- package/dist/xyflow-svelte/node-resize-snap.svelte.d.ts +34 -0
- package/dist/xyflow-svelte/node-resize-snap.svelte.js +71 -0
- package/dist/xyflow-svelte/node-snap.svelte.d.ts +23 -0
- package/dist/xyflow-svelte/node-snap.svelte.js +59 -0
- package/package.json +130 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Draws the guides from `useNodeSnap` and `useNodeResizeSnap` on the React Flow
|
|
3
|
+
* canvas, in flow coordinates (a ViewportPortal), so they pan and zoom with the
|
|
4
|
+
* nodes. Lines stay one screen pixel wide at any zoom; each aligned point gets
|
|
5
|
+
* a small cross, as in Excalidraw; centre guides are dashed. Equal gaps are
|
|
6
|
+
* drawn as lines with a tick across each end.
|
|
7
|
+
*
|
|
8
|
+
* Colour: set `--snap-guide-color` on any ancestor (default a magenta that
|
|
9
|
+
* reads on light and dark canvases). Stacking: ViewportPortal's container
|
|
10
|
+
* comes after the nodes and has no z-index, and xyflow lifts a selected node
|
|
11
|
+
* to z-index 1000 (@xyflow/system 1.0.1 src/utils/store.ts:34), so the drawing
|
|
12
|
+
* sits at `--snap-guide-z`, default 1001, to stay in front of the dragged node.
|
|
13
|
+
*
|
|
14
|
+
* Written with `createElement` in a `.ts` file: the library is built by
|
|
15
|
+
* svelte-package, which copies `.tsx` files without compiling them. The
|
|
16
|
+
* styles are inline, so there is no stylesheet to import.
|
|
17
|
+
*/
|
|
18
|
+
import { useViewportStore, ViewportPortal } from "@xyflow/react";
|
|
19
|
+
import { createElement } from "react";
|
|
20
|
+
import { guideMarks } from "../core/index.js";
|
|
21
|
+
const SVG_STYLE = {
|
|
22
|
+
position: "absolute",
|
|
23
|
+
insetBlockStart: 0,
|
|
24
|
+
insetInlineStart: 0,
|
|
25
|
+
inlineSize: 1,
|
|
26
|
+
blockSize: 1,
|
|
27
|
+
overflow: "visible",
|
|
28
|
+
pointerEvents: "none",
|
|
29
|
+
zIndex: "var(--snap-guide-z, 1001)",
|
|
30
|
+
fill: "none",
|
|
31
|
+
stroke: "var(--snap-guide-color, #e0447c)",
|
|
32
|
+
strokeWidth: 1,
|
|
33
|
+
};
|
|
34
|
+
const STROKE = { vectorEffect: "non-scaling-stroke" };
|
|
35
|
+
/** The SVG elements for `guides` and `gaps` at `zoom`: lines, crosses and gap ticks. */
|
|
36
|
+
export const snapGuideMarks = (guides, gaps, zoom) => guideMarks(guides, gaps, zoom).map((mark) => mark.kind === "line"
|
|
37
|
+
? createElement("line", { key: mark.key, x1: mark.from.x, y1: mark.from.y, x2: mark.to.x, y2: mark.to.y, strokeDasharray: mark.dash?.join(" "), ...STROKE })
|
|
38
|
+
: createElement("path", { key: mark.key, d: mark.d, ...STROKE }));
|
|
39
|
+
/** The guides and equal gaps of a gesture, drawn in front of the nodes. Render it as a child of `<ReactFlow>`. */
|
|
40
|
+
export const SnapGuides = ({ guides, gaps = [] }) => {
|
|
41
|
+
const zoom = useViewportStore((state) => state.transform[2]);
|
|
42
|
+
return createElement(ViewportPortal, null, createElement("svg", { "aria-hidden": true, style: SVG_STYLE }, snapGuideMarks(guides, gaps, zoom)));
|
|
43
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Node, type OnResize, type OnResizeEnd, type OnResizeStart } from "@xyflow/react";
|
|
2
|
+
import type { GapGuide, Guide } from "../core/index.js";
|
|
3
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
4
|
+
type ResizeEvent = MouseEvent | TouchEvent;
|
|
5
|
+
/** The options of `useNodeResizeSnap`: the snapping options, `snapTo` and `suspend`. */
|
|
6
|
+
export type NodeResizeSnapOptions<NodeType extends Node = Node> = AdapterOptions<NodeType, ResizeEvent>;
|
|
7
|
+
export interface NodeResizeSnap {
|
|
8
|
+
/** The guides to draw now; hand them to `<SnapGuides>`. Empty between resizes. */
|
|
9
|
+
readonly guides: readonly Guide[];
|
|
10
|
+
/** The equal gaps to draw now; hand them to `<SnapGuides gaps>`. Empty between resizes. */
|
|
11
|
+
readonly gaps: readonly GapGuide[];
|
|
12
|
+
/** Pass to `<NodeResizer onResizeStart>`. Keeps its identity between renders. */
|
|
13
|
+
readonly onResizeStart: OnResizeStart;
|
|
14
|
+
/** Pass to `<NodeResizer onResize>`. Keeps its identity between renders. */
|
|
15
|
+
readonly onResize: OnResize;
|
|
16
|
+
/** Pass to `<NodeResizer onResizeEnd>`. Keeps its identity between renders. */
|
|
17
|
+
readonly onResizeEnd: OnResizeEnd;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create the resize handlers and the live guides. Call it in a component
|
|
21
|
+
* inside `<ReactFlow>` or a `<ReactFlowProvider>`, as for `useNodeSnap`. Pass
|
|
22
|
+
* the same `<SnapGuides>` the drag guides go to, e.g.
|
|
23
|
+
* `guides={[...drag.guides, ...resize.guides]}`.
|
|
24
|
+
*/
|
|
25
|
+
export declare const useNodeResizeSnap: <NodeType extends Node = Node>(options?: NodeResizeSnapOptions<NodeType>) => NodeResizeSnap;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for React Flow nodes resized with `<NodeResizer>`, built on the
|
|
3
|
+
* shared resize gesture.
|
|
4
|
+
*
|
|
5
|
+
* xyflow's resizer keeps its own state from the pointer. On every move it
|
|
6
|
+
* calls `onResize` with the unsnapped box and then, in the same task, emits
|
|
7
|
+
* that box as position and dimension changes (@xyflow/system 1.0.1
|
|
8
|
+
* src/xyresizer/XYResizer.ts:353-354; the emit is @xyflow/react 13.0.3
|
|
9
|
+
* src/additional-components/NodeResizer/NodeResizeControl.tsx:84-155). A
|
|
10
|
+
* change emitted from `onResize` would therefore reach `onNodesChange` before
|
|
11
|
+
* xyflow's own and be overwritten, since `useNodesState` applies changes as
|
|
12
|
+
* functional `setNodes` updates, in the order they were emitted
|
|
13
|
+
* (src/hooks/useNodesEdgesState.ts:59). So the snapped box is emitted in a
|
|
14
|
+
* microtask: it runs after the synchronous emit, and its update comes last.
|
|
15
|
+
* When the resize ends, `onResizeEnd` runs before the resizer emits the
|
|
16
|
+
* unsnapped size once more (XYResizer.ts:357-358, NodeResizeControl.tsx:157-167),
|
|
17
|
+
* so the last snapped box is emitted the same way.
|
|
18
|
+
*
|
|
19
|
+
* `<NodeResizeControl>` sets up its drag handler in an effect that depends on
|
|
20
|
+
* `onResizeStart`, `onResize` and `onResizeEnd`, and the effect's cleanup
|
|
21
|
+
* destroys the handler (NodeResizeControl.tsx:191-203). A handler that
|
|
22
|
+
* changed identity during a resize would end it, so the three handlers
|
|
23
|
+
* returned here keep their identity for the life of the component.
|
|
24
|
+
*
|
|
25
|
+
* The callbacks are shared by every handle of a `<NodeResizer>` and say neither
|
|
26
|
+
* which node nor which handle is dragged, so `onResizeStart` reads both from
|
|
27
|
+
* the DOM element the gesture began on. Assumes the default node origin
|
|
28
|
+
* [0, 0]; a node with children, `keepAspectRatio` or min/max sizes can be
|
|
29
|
+
* pushed past what xyflow would allow, since the snapped box is written over
|
|
30
|
+
* xyflow's.
|
|
31
|
+
*/
|
|
32
|
+
import { useReactFlowStoreApi } from "@xyflow/react";
|
|
33
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
34
|
+
import { createResizeGesture, NO_GUIDES, resizeTarget, xyflowParentOrigin, xyflowReferences, xyflowResizeChanges, } from "../flow/index.js";
|
|
35
|
+
/**
|
|
36
|
+
* Create the resize handlers and the live guides. Call it in a component
|
|
37
|
+
* inside `<ReactFlow>` or a `<ReactFlowProvider>`, as for `useNodeSnap`. Pass
|
|
38
|
+
* the same `<SnapGuides>` the drag guides go to, e.g.
|
|
39
|
+
* `guides={[...drag.guides, ...resize.guides]}`.
|
|
40
|
+
*/
|
|
41
|
+
export const useNodeResizeSnap = (options = {}) => {
|
|
42
|
+
const stores = useReactFlowStoreApi();
|
|
43
|
+
const latest = useRef(options);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
latest.current = options;
|
|
46
|
+
});
|
|
47
|
+
const [state, setState] = useState(NO_GUIDES);
|
|
48
|
+
const [gesture] = useState(() => createResizeGesture({ ...options, suspend: (event) => (latest.current.suspend ?? ((e) => e.altKey))(event) }, setState));
|
|
49
|
+
const snapTo = useCallback((node) => (latest.current.snapTo ?? (() => true))(node), []);
|
|
50
|
+
/** Emit `box` as the node's position and size, after xyflow's own change. */
|
|
51
|
+
const place = useCallback((id, box, resizing) => {
|
|
52
|
+
const changes = xyflowResizeChanges(stores.nodesStore.getState().nodeLookup, id, box, resizing);
|
|
53
|
+
queueMicrotask(() => {
|
|
54
|
+
stores.optionsStore.getState().emitNodeChanges(changes);
|
|
55
|
+
});
|
|
56
|
+
}, [stores]);
|
|
57
|
+
const onResizeStart = useCallback(({ sourceEvent }) => {
|
|
58
|
+
gesture.start(resizeTarget(sourceEvent.target, "react-flow"));
|
|
59
|
+
}, [gesture]);
|
|
60
|
+
const onResize = useCallback(({ sourceEvent }, params) => {
|
|
61
|
+
const id = gesture.target?.id;
|
|
62
|
+
if (id === undefined)
|
|
63
|
+
return;
|
|
64
|
+
const { nodeLookup } = stores.nodesStore.getState();
|
|
65
|
+
const origin = xyflowParentOrigin(nodeLookup, id);
|
|
66
|
+
const box = { x: origin.x + params.x, y: origin.y + params.y, width: params.width, height: params.height, id };
|
|
67
|
+
const zoom = stores.viewportStore.getState().transform[2];
|
|
68
|
+
const snapped = gesture.move(sourceEvent, box, () => xyflowReferences(nodeLookup, [id], snapTo), zoom);
|
|
69
|
+
if (snapped)
|
|
70
|
+
place(id, snapped, true);
|
|
71
|
+
}, [stores, gesture, snapTo, place]);
|
|
72
|
+
const onResizeEnd = useCallback(() => {
|
|
73
|
+
const last = gesture.end();
|
|
74
|
+
if (last)
|
|
75
|
+
place(last.id, last.box, false);
|
|
76
|
+
}, [gesture, place]);
|
|
77
|
+
return useMemo(() => ({ guides: state.guides, gaps: state.gaps, onResizeStart, onResize, onResizeEnd }), [state, onResizeStart, onResize, onResizeEnd]);
|
|
78
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Node, type OnNodeDrag } from "@xyflow/react";
|
|
2
|
+
import type { GapGuide, Guide } from "../core/index.js";
|
|
3
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
4
|
+
type DragEvent = MouseEvent | TouchEvent;
|
|
5
|
+
/** The options of `useNodeSnap`: the snapping options, `snapTo` and `suspend`. */
|
|
6
|
+
export type NodeSnapOptions<NodeType extends Node = Node> = AdapterOptions<NodeType, DragEvent>;
|
|
7
|
+
export interface NodeSnap<NodeType extends Node = Node> {
|
|
8
|
+
/** The guides to draw now; hand them to `<SnapGuides>`. Empty between drags. */
|
|
9
|
+
readonly guides: readonly Guide[];
|
|
10
|
+
/** The equal gaps to draw now; hand them to `<SnapGuides gaps>`. Empty between drags. */
|
|
11
|
+
readonly gaps: readonly GapGuide[];
|
|
12
|
+
/** Pass to `<ReactFlow onNodeDrag>`. */
|
|
13
|
+
readonly onNodeDrag: OnNodeDrag<NodeType>;
|
|
14
|
+
/** Pass to `<ReactFlow onNodeDragStop>`. */
|
|
15
|
+
readonly onNodeDragStop: OnNodeDrag<NodeType>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the drag handlers and the live guides. Call it in a component inside
|
|
19
|
+
* `<ReactFlow>` or a `<ReactFlowProvider>`, as for `useReactFlow`. When the
|
|
20
|
+
* same component renders `<ReactFlow>`, wrap it in a provider one level up.
|
|
21
|
+
*/
|
|
22
|
+
export declare const useNodeSnap: <NodeType extends Node = Node>(options?: NodeSnapOptions<NodeType>) => NodeSnap<NodeType>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for dragged React Flow nodes, built on the shared drag gesture.
|
|
3
|
+
*
|
|
4
|
+
* On every pointer move xyflow's drag helper computes the nodes' positions,
|
|
5
|
+
* emits them as position changes through `updateNodePositions` and only then
|
|
6
|
+
* calls `onNodeDrag` (@xyflow/system 1.0.1 src/xydrag/XYDrag.ts:228 and :238).
|
|
7
|
+
* `emitNodeChanges` hands the changes to `onNodesChange` synchronously
|
|
8
|
+
* (@xyflow/react 13.0.3 src/store/index.ts:268-287), and `useNodesState`
|
|
9
|
+
* applies them as a functional `setNodes` update
|
|
10
|
+
* (src/hooks/useNodesEdgesState.ts:59). React runs queued updaters in order,
|
|
11
|
+
* so the snapped position emitted from `onNodeDrag` is applied after the
|
|
12
|
+
* unsnapped one and is the state that renders. A held snap often leaves the
|
|
13
|
+
* position as it was a frame ago, but xyflow compares each frame with its own
|
|
14
|
+
* drag item, never with the nodes state, so nothing is skipped as unchanged.
|
|
15
|
+
* When the drag ends xyflow emits its own unsnapped positions once more
|
|
16
|
+
* (XYDrag.ts:411) before `onNodeDragStop` (:424); that handler emits the
|
|
17
|
+
* snapped ones again after them.
|
|
18
|
+
*
|
|
19
|
+
* The position given to `onNodeDrag` is the drag item's, not the one in
|
|
20
|
+
* `nodeLookup`: in a controlled flow the lookup only follows the `nodes` prop
|
|
21
|
+
* after React has rendered it, so it lags by a frame. Reference boxes come
|
|
22
|
+
* from the lookup, which holds the other nodes where they are drawn.
|
|
23
|
+
*
|
|
24
|
+
* Assumes the default node origin [0, 0] (a node's position is its top-left).
|
|
25
|
+
* Handlers and the gesture are created once, and the options are read through
|
|
26
|
+
* a ref at event time, so an inline `options` object is fine; the snapping
|
|
27
|
+
* options themselves (`threshold`, `centers`, ...) are read once, at the
|
|
28
|
+
* first render.
|
|
29
|
+
*/
|
|
30
|
+
import { useReactFlowStoreApi } from "@xyflow/react";
|
|
31
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
32
|
+
import { createDragGesture, NO_GUIDES, xyflowDraggedBox, xyflowDragItems, xyflowReferences } from "../flow/index.js";
|
|
33
|
+
/**
|
|
34
|
+
* Create the drag handlers and the live guides. Call it in a component inside
|
|
35
|
+
* `<ReactFlow>` or a `<ReactFlowProvider>`, as for `useReactFlow`. When the
|
|
36
|
+
* same component renders `<ReactFlow>`, wrap it in a provider one level up.
|
|
37
|
+
*/
|
|
38
|
+
export const useNodeSnap = (options = {}) => {
|
|
39
|
+
const stores = useReactFlowStoreApi();
|
|
40
|
+
const latest = useRef(options);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
latest.current = options;
|
|
43
|
+
});
|
|
44
|
+
const [state, setState] = useState(NO_GUIDES);
|
|
45
|
+
const [gesture] = useState(() => createDragGesture({ ...options, suspend: (event) => (latest.current.suspend ?? ((e) => e.altKey))(event) }, setState));
|
|
46
|
+
const snapTo = useCallback((node) => (latest.current.snapTo ?? (() => true))(node), []);
|
|
47
|
+
const onNodeDrag = useCallback((event, _node, nodes) => {
|
|
48
|
+
const { nodeLookup } = stores.nodesStore.getState();
|
|
49
|
+
const moving = nodes.map((node) => xyflowDraggedBox(nodeLookup, node)).filter((box) => box !== null);
|
|
50
|
+
const ids = nodes.map((node) => node.id);
|
|
51
|
+
const zoom = stores.viewportStore.getState().transform[2];
|
|
52
|
+
const offset = gesture.move(event, moving, () => xyflowReferences(nodeLookup, ids, snapTo), zoom);
|
|
53
|
+
if (offset)
|
|
54
|
+
stores.optionsStore.getState().updateNodePositions(xyflowDragItems(nodeLookup, nodes, offset), true);
|
|
55
|
+
}, [stores, gesture, snapTo]);
|
|
56
|
+
const onNodeDragStop = useCallback((_event, _node, nodes) => {
|
|
57
|
+
const offset = gesture.end();
|
|
58
|
+
if (offset)
|
|
59
|
+
stores.optionsStore.getState().updateNodePositions(xyflowDragItems(stores.nodesStore.getState().nodeLookup, nodes, offset), false);
|
|
60
|
+
}, [stores, gesture]);
|
|
61
|
+
return useMemo(() => ({ guides: state.guides, gaps: state.gaps, onNodeDrag, onNodeDragStop }), [state, onNodeDrag, onNodeDragStop]);
|
|
62
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
Draws the guides from `useNodeSnap` on the Svelte Flow canvas, in flow
|
|
3
|
+
coordinates (a ViewportPortal), so they pan and zoom with the nodes. Lines
|
|
4
|
+
stay one screen pixel wide at any zoom; each aligned point gets a small cross,
|
|
5
|
+
as in Excalidraw; centre guides are dashed. Equal gaps (`gaps`) are drawn as
|
|
6
|
+
lines with a tick across each end.
|
|
7
|
+
|
|
8
|
+
Colour: set `--snap-guide-color` on any ancestor (default a magenta that reads
|
|
9
|
+
on light and dark canvases).
|
|
10
|
+
-->
|
|
11
|
+
<script lang="ts">
|
|
12
|
+
import { useSvelteFlowStore, ViewportPortal } from "@xyflow/svelte";
|
|
13
|
+
import { guideMarks, type GapGuide, type Guide } from "../core/index.js";
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
guides: readonly Guide[];
|
|
17
|
+
gaps?: readonly GapGuide[];
|
|
18
|
+
}
|
|
19
|
+
const { guides, gaps = [] }: Props = $props();
|
|
20
|
+
// Derived, so it follows the store a <SvelteFlowProvider> swaps in (see node-snap.svelte.ts).
|
|
21
|
+
const store = $derived(useSvelteFlowStore());
|
|
22
|
+
const zoom = $derived(store.viewport.zoom);
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<ViewportPortal target="front">
|
|
26
|
+
<svg class="snap-guides" aria-hidden="true">
|
|
27
|
+
{#each guideMarks(guides, gaps, zoom) as mark (mark.key)}
|
|
28
|
+
{#if mark.kind === "line"}
|
|
29
|
+
<line x1={mark.from.x} y1={mark.from.y} x2={mark.to.x} y2={mark.to.y} stroke-dasharray={mark.dash?.join(" ")} />
|
|
30
|
+
{:else}
|
|
31
|
+
<path d={mark.d} />
|
|
32
|
+
{/if}
|
|
33
|
+
{/each}
|
|
34
|
+
</svg>
|
|
35
|
+
</ViewportPortal>
|
|
36
|
+
|
|
37
|
+
<style>
|
|
38
|
+
.snap-guides {
|
|
39
|
+
position: absolute;
|
|
40
|
+
inset-block-start: 0;
|
|
41
|
+
inset-inline-start: 0;
|
|
42
|
+
inline-size: 1px;
|
|
43
|
+
block-size: 1px;
|
|
44
|
+
overflow: visible;
|
|
45
|
+
pointer-events: none;
|
|
46
|
+
fill: none;
|
|
47
|
+
stroke: var(--snap-guide-color, #e0447c);
|
|
48
|
+
stroke-width: 1px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.snap-guides :is(line, path) {
|
|
52
|
+
vector-effect: non-scaling-stroke;
|
|
53
|
+
}
|
|
54
|
+
</style>
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type GapGuide, type Guide } from "../core/index.js";
|
|
2
|
+
interface Props {
|
|
3
|
+
guides: readonly Guide[];
|
|
4
|
+
gaps?: readonly GapGuide[];
|
|
5
|
+
}
|
|
6
|
+
declare const SnapGuides: import("svelte").Component<Props, {}, "">;
|
|
7
|
+
type SnapGuides = ReturnType<typeof SnapGuides>;
|
|
8
|
+
export default SnapGuides;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Svelte Flow (xyflow) adapter: `useNodeSnap` and `useNodeResizeSnap` for the drag and resize handlers, `<SnapGuides>` to draw. */
|
|
2
|
+
export { default as SnapGuides } from "./SnapGuides.svelte";
|
|
3
|
+
export { useNodeSnap, type NodeSnap, type NodeSnapOptions } from "./node-snap.svelte.js";
|
|
4
|
+
export { useNodeResizeSnap, type NodeResizeSnap, type NodeResizeSnapOptions } from "./node-resize-snap.svelte.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Svelte Flow (xyflow) adapter: `useNodeSnap` and `useNodeResizeSnap` for the drag and resize handlers, `<SnapGuides>` to draw. */
|
|
2
|
+
export { default as SnapGuides } from "./SnapGuides.svelte";
|
|
3
|
+
export { useNodeSnap } from "./node-snap.svelte.js";
|
|
4
|
+
export { useNodeResizeSnap } from "./node-resize-snap.svelte.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Node } from "@xyflow/svelte";
|
|
2
|
+
import type { GapGuide, Guide } from "../core/index.js";
|
|
3
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
4
|
+
type ResizeEvent = {
|
|
5
|
+
sourceEvent: MouseEvent | TouchEvent;
|
|
6
|
+
};
|
|
7
|
+
type ResizeParams = {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
};
|
|
13
|
+
/** The options of `useNodeResizeSnap`: the snapping options, `snapTo` and `suspend`. */
|
|
14
|
+
export type NodeResizeSnapOptions<NodeType extends Node = Node> = AdapterOptions<NodeType, MouseEvent | TouchEvent>;
|
|
15
|
+
export interface NodeResizeSnap {
|
|
16
|
+
/** The guides to draw now; hand them to `<SnapGuides>`. Empty between resizes. */
|
|
17
|
+
readonly guides: readonly Guide[];
|
|
18
|
+
/** The equal gaps to draw now; hand them to `<SnapGuides gaps>`. Empty between resizes. */
|
|
19
|
+
readonly gaps: readonly GapGuide[];
|
|
20
|
+
/** Pass to `<NodeResizer onResizeStart>`. */
|
|
21
|
+
readonly onresizestart: (event: ResizeEvent, params: ResizeParams) => void;
|
|
22
|
+
/** Pass to `<NodeResizer onResize>`. */
|
|
23
|
+
readonly onresize: (event: ResizeEvent, params: ResizeParams) => void;
|
|
24
|
+
/** Pass to `<NodeResizer onResizeEnd>`. */
|
|
25
|
+
readonly onresizeend: (event: ResizeEvent, params: ResizeParams) => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create the resize handlers and the live guides. Call it during the init of
|
|
29
|
+
* a component inside `<SvelteFlow>` or a `<SvelteFlowProvider>`, as for
|
|
30
|
+
* `useNodeSnap`. Pass the same `<SnapGuides>` the drag guides go to, e.g.
|
|
31
|
+
* `guides={[...drag.guides, ...resize.guides]}`.
|
|
32
|
+
*/
|
|
33
|
+
export declare const useNodeResizeSnap: <NodeType extends Node = Node>(options?: NodeResizeSnapOptions<NodeType>) => NodeResizeSnap;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for Svelte Flow nodes resized with `<NodeResizer>`, built on the
|
|
3
|
+
* shared resize gesture.
|
|
4
|
+
*
|
|
5
|
+
* xyflow's resizer keeps its own state from the pointer and, on every move,
|
|
6
|
+
* calls `onResize` with the unsnapped box and then queues that box as a
|
|
7
|
+
* change, in the same task. A change queued from `onResize` would land before
|
|
8
|
+
* it and be overwritten, so the snapped box is queued in a microtask instead:
|
|
9
|
+
* xyflow flushes its changes in a later microtask, applies them in order, and
|
|
10
|
+
* the snapped ones come last. When the resize ends xyflow queues the unsnapped
|
|
11
|
+
* size once more (after `onResizeEnd`), so the last snapped box is queued
|
|
12
|
+
* again the same way.
|
|
13
|
+
*
|
|
14
|
+
* The callbacks are shared by every handle of a `<NodeResizer>` and say neither
|
|
15
|
+
* which node nor which handle is dragged, so `onresizestart` reads both from
|
|
16
|
+
* the DOM element the gesture began on. Assumes the default node origin
|
|
17
|
+
* [0, 0]; a node with children, `keepAspectRatio` or min/max sizes can be
|
|
18
|
+
* pushed past what xyflow would allow, since the snapped box is written over
|
|
19
|
+
* xyflow's.
|
|
20
|
+
*/
|
|
21
|
+
import { useSvelteFlowStore } from "@xyflow/svelte";
|
|
22
|
+
import { createResizeGesture, resizeTarget, xyflowParentOrigin, xyflowReferences, xyflowResizeChanges } from "../flow/index.js";
|
|
23
|
+
/**
|
|
24
|
+
* Create the resize handlers and the live guides. Call it during the init of
|
|
25
|
+
* a component inside `<SvelteFlow>` or a `<SvelteFlowProvider>`, as for
|
|
26
|
+
* `useNodeSnap`. Pass the same `<SnapGuides>` the drag guides go to, e.g.
|
|
27
|
+
* `guides={[...drag.guides, ...resize.guides]}`.
|
|
28
|
+
*/
|
|
29
|
+
export const useNodeResizeSnap = (options = {}) => {
|
|
30
|
+
const store = $derived(useSvelteFlowStore());
|
|
31
|
+
const snapTo = options.snapTo ?? (() => true);
|
|
32
|
+
let guides = $state.raw([]);
|
|
33
|
+
let gaps = $state.raw([]);
|
|
34
|
+
const gesture = createResizeGesture(options, (state) => {
|
|
35
|
+
guides = state.guides;
|
|
36
|
+
gaps = state.gaps;
|
|
37
|
+
});
|
|
38
|
+
/** Queue `box` (absolute) as the node's position and size, after xyflow's own change. */
|
|
39
|
+
const place = (id, box, resizing) => {
|
|
40
|
+
const changes = xyflowResizeChanges(store.nodeLookup, id, box, resizing);
|
|
41
|
+
queueMicrotask(() => {
|
|
42
|
+
store.queueNodeChanges(changes);
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
get guides() {
|
|
47
|
+
return guides;
|
|
48
|
+
},
|
|
49
|
+
get gaps() {
|
|
50
|
+
return gaps;
|
|
51
|
+
},
|
|
52
|
+
onresizestart: ({ sourceEvent }) => {
|
|
53
|
+
gesture.start(resizeTarget(sourceEvent.target, "svelte-flow"));
|
|
54
|
+
},
|
|
55
|
+
onresize: ({ sourceEvent }, params) => {
|
|
56
|
+
const id = gesture.target?.id;
|
|
57
|
+
if (id === undefined)
|
|
58
|
+
return;
|
|
59
|
+
const origin = xyflowParentOrigin(store.nodeLookup, id);
|
|
60
|
+
const box = { x: origin.x + params.x, y: origin.y + params.y, width: params.width, height: params.height, id };
|
|
61
|
+
const snapped = gesture.move(sourceEvent, box, () => xyflowReferences(store.nodeLookup, [id], snapTo), store.viewport.zoom);
|
|
62
|
+
if (snapped)
|
|
63
|
+
place(id, snapped, true);
|
|
64
|
+
},
|
|
65
|
+
onresizeend: () => {
|
|
66
|
+
const last = gesture.end();
|
|
67
|
+
if (last)
|
|
68
|
+
place(last.id, last.box, false);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Node, type NodeTargetEventWithPointer } from "@xyflow/svelte";
|
|
2
|
+
import type { GapGuide, Guide } from "../core/index.js";
|
|
3
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
4
|
+
type DragEvent = MouseEvent | TouchEvent;
|
|
5
|
+
/** The options of `useNodeSnap`: the snapping options, `snapTo` and `suspend`. */
|
|
6
|
+
export type NodeSnapOptions<NodeType extends Node = Node> = AdapterOptions<NodeType, DragEvent>;
|
|
7
|
+
export interface NodeSnap<NodeType extends Node = Node> {
|
|
8
|
+
/** The guides to draw now; hand them to `<SnapGuides>`. Empty between drags. */
|
|
9
|
+
readonly guides: readonly Guide[];
|
|
10
|
+
/** The equal gaps to draw now; hand them to `<SnapGuides gaps>`. Empty between drags. */
|
|
11
|
+
readonly gaps: readonly GapGuide[];
|
|
12
|
+
/** Pass to `<SvelteFlow onnodedrag>`. */
|
|
13
|
+
readonly onnodedrag: NodeTargetEventWithPointer<DragEvent, NodeType>;
|
|
14
|
+
/** Pass to `<SvelteFlow onnodedragstop>`. */
|
|
15
|
+
readonly onnodedragstop: NodeTargetEventWithPointer<DragEvent, NodeType>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the drag handlers and the live guides. Call it during the init of a
|
|
19
|
+
* component inside `<SvelteFlow>` or a `<SvelteFlowProvider>`, as for
|
|
20
|
+
* `useSvelteFlow`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const useNodeSnap: <NodeType extends Node = Node>(options?: NodeSnapOptions<NodeType>) => NodeSnap<NodeType>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for dragged Svelte Flow nodes, built on the shared drag gesture.
|
|
3
|
+
*
|
|
4
|
+
* xyflow recomputes a dragged node's position from the pointer on every move
|
|
5
|
+
* and queues it as a change; `onnodedrag` runs after that, so a snapped
|
|
6
|
+
* position queued here lands later in the same batch and wins. A held snap
|
|
7
|
+
* often leaves the position unchanged from the last frame, which
|
|
8
|
+
* `useSvelteFlow().updateNode` would skip as a no-op, so this writes through
|
|
9
|
+
* the store's `updateNodePositions`, which always queues. When the drag ends
|
|
10
|
+
* xyflow queues its own unsnapped positions once more, before
|
|
11
|
+
* `onnodedragstop`; that handler queues the snapped ones again after them.
|
|
12
|
+
*
|
|
13
|
+
* Assumes the default node origin [0, 0] (a node's position is its top-left).
|
|
14
|
+
* The guides component reads the zoom the same way, through a derived store.
|
|
15
|
+
*/
|
|
16
|
+
import { useSvelteFlowStore } from "@xyflow/svelte";
|
|
17
|
+
import { createDragGesture, xyflowDraggedBox, xyflowDragItems, xyflowReferences } from "../flow/index.js";
|
|
18
|
+
/**
|
|
19
|
+
* Create the drag handlers and the live guides. Call it during the init of a
|
|
20
|
+
* component inside `<SvelteFlow>` or a `<SvelteFlowProvider>`, as for
|
|
21
|
+
* `useSvelteFlow`.
|
|
22
|
+
*/
|
|
23
|
+
export const useNodeSnap = (options = {}) => {
|
|
24
|
+
// Under a <SvelteFlowProvider> the context first holds a placeholder store,
|
|
25
|
+
// replaced when <SvelteFlow> mounts; a plain call here would keep the empty
|
|
26
|
+
// placeholder. Deriving it re-reads the current one, as useSvelteFlow does.
|
|
27
|
+
const store = $derived(useSvelteFlowStore());
|
|
28
|
+
const snapTo = options.snapTo ?? (() => true);
|
|
29
|
+
let guides = $state.raw([]);
|
|
30
|
+
let gaps = $state.raw([]);
|
|
31
|
+
const gesture = createDragGesture(options, (state) => {
|
|
32
|
+
guides = state.guides;
|
|
33
|
+
gaps = state.gaps;
|
|
34
|
+
});
|
|
35
|
+
/** Queue `nodes` at their positions plus `offset`, after xyflow's own change. */
|
|
36
|
+
const place = (nodes, offset, dragging) => {
|
|
37
|
+
store.updateNodePositions(xyflowDragItems(store.nodeLookup, nodes, offset), dragging);
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
get guides() {
|
|
41
|
+
return guides;
|
|
42
|
+
},
|
|
43
|
+
get gaps() {
|
|
44
|
+
return gaps;
|
|
45
|
+
},
|
|
46
|
+
onnodedrag: ({ nodes, event }) => {
|
|
47
|
+
const ids = nodes.map((n) => n.id);
|
|
48
|
+
const moving = nodes.flatMap((n) => xyflowDraggedBox(store.nodeLookup, n) ?? []);
|
|
49
|
+
const offset = gesture.move(event, moving, () => xyflowReferences(store.nodeLookup, ids, snapTo), store.viewport.zoom);
|
|
50
|
+
if (offset)
|
|
51
|
+
place(nodes, offset, true);
|
|
52
|
+
},
|
|
53
|
+
onnodedragstop: ({ nodes }) => {
|
|
54
|
+
const offset = gesture.end();
|
|
55
|
+
if (offset)
|
|
56
|
+
place(nodes, offset, false);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,132 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appulsauce/snap-guides",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Alignment snapping with guide lines for node editors and canvases: a framework-free core over plain boxes, and adapters for Svelte Flow, React Flow and Vue Flow.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Kai Erik Niermann",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/KaiErikNiermann/snap-guides.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://kaierikniermann.github.io/snap-guides/",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/KaiErikNiermann/snap-guides/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/core/index.d.ts",
|
|
22
|
+
"default": "./dist/core/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./xyflow-svelte": {
|
|
25
|
+
"types": "./dist/xyflow-svelte/index.d.ts",
|
|
26
|
+
"svelte": "./dist/xyflow-svelte/index.js",
|
|
27
|
+
"default": "./dist/xyflow-svelte/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./xyflow-react": {
|
|
30
|
+
"types": "./dist/xyflow-react/index.d.ts",
|
|
31
|
+
"default": "./dist/xyflow-react/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./vue-flow": {
|
|
34
|
+
"types": "./dist/vue-flow/index.d.ts",
|
|
35
|
+
"default": "./dist/vue-flow/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"!dist/**/*.test.*"
|
|
42
|
+
],
|
|
43
|
+
"sideEffects": [
|
|
44
|
+
"**/*.svelte"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "svelte-package -i src -o dist && publint",
|
|
48
|
+
"check": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings",
|
|
49
|
+
"lint": "eslint . --max-warnings=0",
|
|
50
|
+
"lint:fix": "eslint . --fix",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"test:watch": "vitest",
|
|
53
|
+
"verify": "pnpm lint && pnpm check && pnpm test && pnpm build",
|
|
54
|
+
"prepublishOnly": "pnpm build",
|
|
55
|
+
"prepare": "husky"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"@vue-flow/core": "^1.48.0",
|
|
59
|
+
"@vue-flow/node-resizer": "^1.5.0",
|
|
60
|
+
"@xyflow/react": "^13.0.0",
|
|
61
|
+
"@xyflow/svelte": "^2.0.0",
|
|
62
|
+
"react": "^19.0.0",
|
|
63
|
+
"svelte": "^5.15.0",
|
|
64
|
+
"vue": "^3.5.0"
|
|
65
|
+
},
|
|
66
|
+
"peerDependenciesMeta": {
|
|
67
|
+
"@vue-flow/core": {
|
|
68
|
+
"optional": true
|
|
69
|
+
},
|
|
70
|
+
"@vue-flow/node-resizer": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"@xyflow/react": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@xyflow/svelte": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"react": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"svelte": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"vue": {
|
|
86
|
+
"optional": true
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"keywords": [
|
|
90
|
+
"snapping",
|
|
91
|
+
"alignment",
|
|
92
|
+
"guides",
|
|
93
|
+
"smart-guides",
|
|
94
|
+
"node-editor",
|
|
95
|
+
"xyflow",
|
|
96
|
+
"svelte-flow",
|
|
97
|
+
"react-flow",
|
|
98
|
+
"vue-flow"
|
|
99
|
+
],
|
|
100
|
+
"devDependencies": {
|
|
101
|
+
"@antithesishq/bombadil": "0.8.0",
|
|
102
|
+
"@eslint/js": "^10.0.1",
|
|
103
|
+
"@sveltejs/package": "^3.0.3",
|
|
104
|
+
"@types/react": "^19.3.3",
|
|
105
|
+
"@types/react-dom": "^19.3.0",
|
|
106
|
+
"@vue-flow/core": "^1.48.2",
|
|
107
|
+
"@vue-flow/node-resizer": "^1.5.1",
|
|
108
|
+
"@xyflow/react": "^13.0.3",
|
|
109
|
+
"@xyflow/svelte": "^2.0.3",
|
|
110
|
+
"eslint": "^10.15.0",
|
|
111
|
+
"eslint-plugin-security": "^4.2.1",
|
|
112
|
+
"eslint-plugin-sonarjs": "^4.2.3",
|
|
113
|
+
"eslint-plugin-svelte": "^3.24.1",
|
|
114
|
+
"eslint-plugin-unicorn": "^80.0.0",
|
|
115
|
+
"fast-check": "^4.11.0",
|
|
116
|
+
"globals": "^17.14.0",
|
|
117
|
+
"husky": "^9.1.7",
|
|
118
|
+
"jscpd": "^5.4.3",
|
|
119
|
+
"knip": "^6.50.0",
|
|
120
|
+
"konva": "^10.12.0",
|
|
121
|
+
"publint": "^0.3.25",
|
|
122
|
+
"react": "^19.3.1",
|
|
123
|
+
"react-dom": "^19.3.1",
|
|
124
|
+
"svelte": "^5.59.2",
|
|
125
|
+
"svelte-check": "^4.7.8",
|
|
126
|
+
"typescript": "^6.0.3",
|
|
127
|
+
"typescript-eslint": "^8.74.0",
|
|
128
|
+
"vitest": "^5.1.2",
|
|
129
|
+
"vue": "^3.5.47"
|
|
130
|
+
},
|
|
131
|
+
"packageManager": "pnpm@10.32.1"
|
|
132
|
+
}
|