@appulsauce/snap-guides 0.1.0 → 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.
Files changed (46) hide show
  1. package/README.md +40 -3
  2. package/dist/core/draw.d.ts +22 -0
  3. package/dist/core/draw.js +16 -0
  4. package/dist/core/gaps.d.ts +22 -6
  5. package/dist/core/gaps.js +201 -50
  6. package/dist/core/index.d.ts +3 -3
  7. package/dist/core/index.js +2 -2
  8. package/dist/core/snap.d.ts +16 -1
  9. package/dist/core/snap.js +138 -36
  10. package/dist/core/types.d.ts +13 -0
  11. package/dist/flow/boxes.d.ts +40 -0
  12. package/dist/flow/boxes.js +45 -0
  13. package/dist/flow/gestures.d.ts +52 -0
  14. package/dist/flow/gestures.js +80 -0
  15. package/dist/flow/index.d.ts +4 -0
  16. package/dist/flow/index.js +4 -0
  17. package/dist/flow/xyflow.d.ts +70 -0
  18. package/dist/flow/xyflow.js +52 -0
  19. package/dist/vue-flow/guide-state.d.ts +14 -0
  20. package/dist/vue-flow/guide-state.js +16 -0
  21. package/dist/vue-flow/index.d.ts +4 -0
  22. package/dist/vue-flow/index.js +4 -0
  23. package/dist/vue-flow/nodes.d.ts +29 -0
  24. package/dist/vue-flow/nodes.js +28 -0
  25. package/dist/vue-flow/snap-guides.d.ts +25 -0
  26. package/dist/vue-flow/snap-guides.js +54 -0
  27. package/dist/vue-flow/use-node-resize-snap.d.ts +23 -0
  28. package/dist/vue-flow/use-node-resize-snap.js +83 -0
  29. package/dist/vue-flow/use-node-snap.d.ts +15 -0
  30. package/dist/vue-flow/use-node-snap.js +67 -0
  31. package/dist/xyflow-react/index.d.ts +4 -0
  32. package/dist/xyflow-react/index.js +4 -0
  33. package/dist/xyflow-react/snap-guides.d.ts +10 -0
  34. package/dist/xyflow-react/snap-guides.js +43 -0
  35. package/dist/xyflow-react/use-node-resize-snap.d.ts +26 -0
  36. package/dist/xyflow-react/use-node-resize-snap.js +78 -0
  37. package/dist/xyflow-react/use-node-snap.d.ts +23 -0
  38. package/dist/xyflow-react/use-node-snap.js +62 -0
  39. package/dist/xyflow-svelte/SnapGuides.svelte +6 -11
  40. package/dist/xyflow-svelte/index.d.ts +2 -1
  41. package/dist/xyflow-svelte/index.js +2 -1
  42. package/dist/xyflow-svelte/node-resize-snap.svelte.d.ts +34 -0
  43. package/dist/xyflow-svelte/node-resize-snap.svelte.js +71 -0
  44. package/dist/xyflow-svelte/node-snap.svelte.d.ts +4 -7
  45. package/dist/xyflow-svelte/node-snap.svelte.js +15 -71
  46. package/package.json +56 -15
@@ -1,3 +1,4 @@
1
- /** Svelte Flow (xyflow) adapter: `useNodeSnap` for the drag handlers, `<SnapGuides>` to draw. */
1
+ /** Svelte Flow (xyflow) adapter: `useNodeSnap` and `useNodeResizeSnap` for the drag and resize handlers, `<SnapGuides>` to draw. */
2
2
  export { default as SnapGuides } from "./SnapGuides.svelte";
3
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
+ };
@@ -1,12 +1,9 @@
1
1
  import { type Node, type NodeTargetEventWithPointer } from "@xyflow/svelte";
2
- import { type GapGuide, type Guide, type SnapOptions } from "../core/index.js";
2
+ import type { GapGuide, Guide } from "../core/index.js";
3
+ import { type AdapterOptions } from "../flow/index.js";
3
4
  type DragEvent = MouseEvent | TouchEvent;
4
- export interface NodeSnapOptions<NodeType extends Node = Node> extends SnapOptions {
5
- /** The nodes a dragged node may snap to. Default: every node not being dragged and not hidden. */
6
- readonly snapTo?: (node: NodeType) => boolean;
7
- /** While this holds for the drag's pointer event, snapping is off. Default: Alt is held. */
8
- readonly suspend?: (event: DragEvent) => boolean;
9
- }
5
+ /** The options of `useNodeSnap`: the snapping options, `snapTo` and `suspend`. */
6
+ export type NodeSnapOptions<NodeType extends Node = Node> = AdapterOptions<NodeType, DragEvent>;
10
7
  export interface NodeSnap<NodeType extends Node = Node> {
11
8
  /** The guides to draw now; hand them to `<SnapGuides>`. Empty between drags. */
12
9
  readonly guides: readonly Guide[];
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Snapping for dragged Svelte Flow nodes, built on the core snapper.
2
+ * Snapping for dragged Svelte Flow nodes, built on the shared drag gesture.
3
3
  *
4
4
  * xyflow recomputes a dragged node's position from the pointer on every move
5
5
  * and queues it as a change; `onnodedrag` runs after that, so a snapped
@@ -13,9 +13,8 @@
13
13
  * Assumes the default node origin [0, 0] (a node's position is its top-left).
14
14
  * The guides component reads the zoom the same way, through a derived store.
15
15
  */
16
- import { useSvelteFlowStore, } from "@xyflow/svelte";
17
- import { createSnapper } from "../core/index.js";
18
- const ZERO = { x: 0, y: 0 };
16
+ import { useSvelteFlowStore } from "@xyflow/svelte";
17
+ import { createDragGesture, xyflowDraggedBox, xyflowDragItems, xyflowReferences } from "../flow/index.js";
19
18
  /**
20
19
  * Create the drag handlers and the live guides. Call it during the init of a
21
20
  * component inside `<SvelteFlow>` or a `<SvelteFlowProvider>`, as for
@@ -26,62 +25,16 @@ export const useNodeSnap = (options = {}) => {
26
25
  // replaced when <SvelteFlow> mounts; a plain call here would keep the empty
27
26
  // placeholder. Deriving it re-reads the current one, as useSvelteFlow does.
28
27
  const store = $derived(useSvelteFlowStore());
29
- const snapper = createSnapper(options);
30
28
  const snapTo = options.snapTo ?? (() => true);
31
- const suspend = options.suspend ?? ((event) => event.altKey);
32
29
  let guides = $state.raw([]);
33
30
  let gaps = $state.raw([]);
34
- /** The offset the last drag frame applied, re-applied when the drag ends. */
35
- let last = ZERO;
36
- const size = (internal) => {
37
- const width = internal.measured.width ?? internal.width;
38
- const height = internal.measured.height ?? internal.height;
39
- return width === undefined || height === undefined ? null : { width, height };
40
- };
41
- /** A dragged node's box, from the position xyflow just computed for it. */
42
- const draggedBox = (node) => {
43
- const internal = store.nodeLookup.get(node.id);
44
- const dims = internal ? size(internal) : null;
45
- if (!dims)
46
- return null;
47
- const parent = node.parentId === undefined ? undefined : store.nodeLookup.get(node.parentId);
48
- const origin = parent?.internals.positionAbsolute ?? ZERO;
49
- return { x: origin.x + node.position.x, y: origin.y + node.position.y, ...dims, id: node.id };
50
- };
51
- const referenceBoxes = (dragged) => {
52
- const boxes = [];
53
- for (const internal of store.nodeLookup.values()) {
54
- const dims = size(internal);
55
- if (!dims || dragged.includes(internal.id) || internal.hidden || !snapTo(internal.internals.userNode))
56
- continue;
57
- const { x, y } = internal.internals.positionAbsolute;
58
- boxes.push({ x, y, ...dims, id: internal.id });
59
- }
60
- return boxes;
61
- };
31
+ const gesture = createDragGesture(options, (state) => {
32
+ guides = state.guides;
33
+ gaps = state.gaps;
34
+ });
62
35
  /** Queue `nodes` at their positions plus `offset`, after xyflow's own change. */
63
36
  const place = (nodes, offset, dragging) => {
64
- const entries = [];
65
- for (const node of nodes) {
66
- const internal = store.nodeLookup.get(node.id);
67
- const dims = internal ? size(internal) : null;
68
- if (!internal || !dims)
69
- continue;
70
- const position = { x: node.position.x + offset.x, y: node.position.y + offset.y };
71
- entries.push([
72
- node.id,
73
- { id: node.id, position, distance: ZERO, measured: dims, internals: { positionAbsolute: internal.internals.positionAbsolute } },
74
- ]);
75
- }
76
- // A throwaway argument, never read reactively; SvelteMap would only add cost.
77
- // eslint-disable-next-line svelte/prefer-svelte-reactivity
78
- store.updateNodePositions(new Map(entries), dragging);
79
- };
80
- const stop = () => {
81
- snapper.reset();
82
- guides = [];
83
- gaps = [];
84
- last = ZERO;
37
+ store.updateNodePositions(xyflowDragItems(store.nodeLookup, nodes, offset), dragging);
85
38
  };
86
39
  return {
87
40
  get guides() {
@@ -91,25 +44,16 @@ export const useNodeSnap = (options = {}) => {
91
44
  return gaps;
92
45
  },
93
46
  onnodedrag: ({ nodes, event }) => {
94
- if (suspend(event)) {
95
- stop();
96
- return;
97
- }
98
- const moving = nodes.flatMap((n) => draggedBox(n) ?? []);
99
- if (moving.length === 0)
100
- return;
101
47
  const ids = nodes.map((n) => n.id);
102
- const result = snapper.snap(moving, referenceBoxes(ids), store.viewport.zoom);
103
- guides = result.guides;
104
- gaps = result.gaps;
105
- last = result.offset;
106
- if (last.x !== 0 || last.y !== 0)
107
- place(nodes, last, true);
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);
108
52
  },
109
53
  onnodedragstop: ({ nodes }) => {
110
- if (last.x !== 0 || last.y !== 0)
111
- place(nodes, last, false);
112
- stop();
54
+ const offset = gesture.end();
55
+ if (offset)
56
+ place(nodes, offset, false);
113
57
  },
114
58
  };
115
59
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@appulsauce/snap-guides",
3
- "version": "0.1.0",
4
- "description": "Alignment snapping with guide lines for node editors and canvases: a framework-free core over plain boxes, and an adapter for Svelte Flow (xyflow).",
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
5
  "license": "MIT",
6
6
  "author": "Kai Erik Niermann",
7
7
  "repository": {
@@ -26,6 +26,14 @@
26
26
  "svelte": "./dist/xyflow-svelte/index.js",
27
27
  "default": "./dist/xyflow-svelte/index.js"
28
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
+ },
29
37
  "./package.json": "./package.json"
30
38
  },
31
39
  "files": [
@@ -35,16 +43,47 @@
35
43
  "sideEffects": [
36
44
  "**/*.svelte"
37
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
+ },
38
57
  "peerDependencies": {
58
+ "@vue-flow/core": "^1.48.0",
59
+ "@vue-flow/node-resizer": "^1.5.0",
60
+ "@xyflow/react": "^13.0.0",
39
61
  "@xyflow/svelte": "^2.0.0",
40
- "svelte": "^5.15.0"
62
+ "react": "^19.0.0",
63
+ "svelte": "^5.15.0",
64
+ "vue": "^3.5.0"
41
65
  },
42
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
+ },
43
76
  "@xyflow/svelte": {
44
77
  "optional": true
45
78
  },
79
+ "react": {
80
+ "optional": true
81
+ },
46
82
  "svelte": {
47
83
  "optional": true
84
+ },
85
+ "vue": {
86
+ "optional": true
48
87
  }
49
88
  },
50
89
  "keywords": [
@@ -54,12 +93,19 @@
54
93
  "smart-guides",
55
94
  "node-editor",
56
95
  "xyflow",
57
- "svelte-flow"
96
+ "svelte-flow",
97
+ "react-flow",
98
+ "vue-flow"
58
99
  ],
59
100
  "devDependencies": {
60
101
  "@antithesishq/bombadil": "0.8.0",
61
102
  "@eslint/js": "^10.0.1",
62
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",
63
109
  "@xyflow/svelte": "^2.0.3",
64
110
  "eslint": "^10.15.0",
65
111
  "eslint-plugin-security": "^4.2.1",
@@ -73,19 +119,14 @@
73
119
  "knip": "^6.50.0",
74
120
  "konva": "^10.12.0",
75
121
  "publint": "^0.3.25",
122
+ "react": "^19.3.1",
123
+ "react-dom": "^19.3.1",
76
124
  "svelte": "^5.59.2",
77
125
  "svelte-check": "^4.7.8",
78
126
  "typescript": "^6.0.3",
79
127
  "typescript-eslint": "^8.74.0",
80
- "vitest": "^5.1.2"
128
+ "vitest": "^5.1.2",
129
+ "vue": "^3.5.47"
81
130
  },
82
- "scripts": {
83
- "build": "svelte-package -i src -o dist && publint",
84
- "check": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings",
85
- "lint": "eslint . --max-warnings=0",
86
- "lint:fix": "eslint . --fix",
87
- "test": "vitest run",
88
- "test:watch": "vitest",
89
- "verify": "pnpm lint && pnpm check && pnpm test && pnpm build"
90
- }
91
- }
131
+ "packageManager": "pnpm@10.32.1"
132
+ }