@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,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The state of one drag or resize gesture in a node editor, shared by the
|
|
3
|
+
* framework adapters. A gesture owns a snapper and the guides it last
|
|
4
|
+
* produced; the adapter reads boxes out of its editor, passes them in, and
|
|
5
|
+
* writes back what the gesture says to write. Guides go out through
|
|
6
|
+
* `onGuides`, so each adapter can keep them in its own reactive state.
|
|
7
|
+
*/
|
|
8
|
+
import { createSnapper } from "../core/index.js";
|
|
9
|
+
/** The guide state with no gesture going. */
|
|
10
|
+
export const NO_GUIDES = { guides: [], gaps: [] };
|
|
11
|
+
const ZERO = { x: 0, y: 0 };
|
|
12
|
+
const moved = (offset) => offset.x !== 0 || offset.y !== 0;
|
|
13
|
+
export const createDragGesture = (options, onGuides) => {
|
|
14
|
+
const snapper = createSnapper(options);
|
|
15
|
+
const suspend = options.suspend ?? ((event) => event.altKey);
|
|
16
|
+
let last = ZERO;
|
|
17
|
+
const stop = () => {
|
|
18
|
+
snapper.reset();
|
|
19
|
+
onGuides(NO_GUIDES);
|
|
20
|
+
last = ZERO;
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
move: (event, moving, references, zoom) => {
|
|
24
|
+
if (suspend(event)) {
|
|
25
|
+
stop();
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (moving.length === 0)
|
|
29
|
+
return null;
|
|
30
|
+
const result = snapper.snap(moving, references(), zoom);
|
|
31
|
+
onGuides({ guides: result.guides, gaps: result.gaps });
|
|
32
|
+
last = result.offset;
|
|
33
|
+
return moved(last) ? last : null;
|
|
34
|
+
},
|
|
35
|
+
end: () => {
|
|
36
|
+
const offset = moved(last) ? last : null;
|
|
37
|
+
stop();
|
|
38
|
+
return offset;
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export const createResizeGesture = (options, onGuides) => {
|
|
43
|
+
const snapper = createSnapper(options);
|
|
44
|
+
const suspend = options.suspend ?? ((event) => event.altKey);
|
|
45
|
+
let target = null;
|
|
46
|
+
/** The last snapped box, `null` while snapping is off or nothing snapped. */
|
|
47
|
+
let snapped = null;
|
|
48
|
+
const stop = () => {
|
|
49
|
+
snapper.reset();
|
|
50
|
+
onGuides(NO_GUIDES);
|
|
51
|
+
snapped = null;
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
get target() {
|
|
55
|
+
return target;
|
|
56
|
+
},
|
|
57
|
+
start: (next) => {
|
|
58
|
+
stop();
|
|
59
|
+
target = next;
|
|
60
|
+
},
|
|
61
|
+
move: (event, box, references, zoom) => {
|
|
62
|
+
if (!target)
|
|
63
|
+
return null;
|
|
64
|
+
if (suspend(event)) {
|
|
65
|
+
stop();
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const result = snapper.resize(box, target.handle, references(), zoom);
|
|
69
|
+
onGuides({ guides: result.guides, gaps: result.gaps });
|
|
70
|
+
snapped = moved(result.offset) ? result.box : null;
|
|
71
|
+
return snapped;
|
|
72
|
+
},
|
|
73
|
+
end: () => {
|
|
74
|
+
const last = target && snapped ? { id: target.id, box: snapped } : null;
|
|
75
|
+
stop();
|
|
76
|
+
target = null;
|
|
77
|
+
return last;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Framework-free pieces of the node-editor adapters: reading boxes and resize handles, the drag and resize gesture state, and the xyflow reads and writes. */
|
|
2
|
+
export { handleOfClasses, referenceBoxes, resizeTarget } from "./boxes.js";
|
|
3
|
+
export { createDragGesture, createResizeGesture, NO_GUIDES, type AdapterOptions, type GuideState } from "./gestures.js";
|
|
4
|
+
export { xyflowDraggedBox, xyflowDragItems, xyflowParentOrigin, xyflowReferences, xyflowResizeChanges, type XyInternalNode, } from "./xyflow.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Framework-free pieces of the node-editor adapters: reading boxes and resize handles, the drag and resize gesture state, and the xyflow reads and writes. */
|
|
2
|
+
export { handleOfClasses, referenceBoxes, resizeTarget } from "./boxes.js";
|
|
3
|
+
export { createDragGesture, createResizeGesture, NO_GUIDES } from "./gestures.js";
|
|
4
|
+
export { xyflowDraggedBox, xyflowDragItems, xyflowParentOrigin, xyflowReferences, xyflowResizeChanges, } from "./xyflow.js";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Box, Vec } from "../core/index.js";
|
|
2
|
+
import { type SizedNode } from "./boxes.js";
|
|
3
|
+
/** The part of an xyflow internal node (`nodeLookup` entry) the adapters read. */
|
|
4
|
+
export interface XyInternalNode<UserNode> extends SizedNode {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly hidden?: boolean | undefined;
|
|
7
|
+
readonly parentId?: string | undefined;
|
|
8
|
+
readonly internals: {
|
|
9
|
+
readonly positionAbsolute: {
|
|
10
|
+
readonly x: number;
|
|
11
|
+
readonly y: number;
|
|
12
|
+
};
|
|
13
|
+
readonly userNode: UserNode;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** The reference boxes of an xyflow `nodeLookup`, with `snapTo` asked about the user's node. */
|
|
17
|
+
export declare const xyflowReferences: <UserNode>(lookup: ReadonlyMap<string, XyInternalNode<UserNode>>, exclude: readonly string[], snapTo: (node: UserNode) => boolean) => Box[];
|
|
18
|
+
/** The absolute top-left of `id`'s parent in an xyflow `nodeLookup`, the origin when it has none. */
|
|
19
|
+
export declare const xyflowParentOrigin: <UserNode>(lookup: ReadonlyMap<string, XyInternalNode<UserNode>>, id: string) => {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
23
|
+
/** The part of a user node a drag hands the adapters. */
|
|
24
|
+
export interface XyUserNode {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
/** Relative to the parent node, or to the flow without one. */
|
|
27
|
+
readonly position: Vec;
|
|
28
|
+
}
|
|
29
|
+
/** A dragged node's absolute box, from the position xyflow just computed for it; `null` before it is measured. */
|
|
30
|
+
export declare const xyflowDraggedBox: <UserNode>(lookup: ReadonlyMap<string, XyInternalNode<UserNode>>, node: XyUserNode) => Box | null;
|
|
31
|
+
/** One entry of the map `updateNodePositions` takes, as far as xyflow reads it. */
|
|
32
|
+
export interface XyDragItem {
|
|
33
|
+
readonly id: string;
|
|
34
|
+
readonly position: Vec;
|
|
35
|
+
readonly distance: Vec;
|
|
36
|
+
readonly measured: {
|
|
37
|
+
readonly width: number;
|
|
38
|
+
readonly height: number;
|
|
39
|
+
};
|
|
40
|
+
readonly internals: {
|
|
41
|
+
readonly positionAbsolute: Vec;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The drag items for `nodes` moved by `offset`, in the shape the store's
|
|
46
|
+
* `updateNodePositions` takes. A node missing from `lookup` or not measured
|
|
47
|
+
* yet is left out.
|
|
48
|
+
*/
|
|
49
|
+
export declare const xyflowDragItems: <UserNode>(lookup: ReadonlyMap<string, XyInternalNode<UserNode>>, nodes: readonly XyUserNode[], offset: Vec) => Map<string, XyDragItem>;
|
|
50
|
+
/** A node change of the kinds `xyflowResizeChanges` makes, as xyflow's `NodeChange` takes them. */
|
|
51
|
+
export type XyResizeChange = {
|
|
52
|
+
readonly id: string;
|
|
53
|
+
readonly type: "position";
|
|
54
|
+
readonly position: Vec;
|
|
55
|
+
} | {
|
|
56
|
+
readonly id: string;
|
|
57
|
+
readonly type: "dimensions";
|
|
58
|
+
readonly resizing: boolean;
|
|
59
|
+
readonly setAttributes: true;
|
|
60
|
+
readonly dimensions: {
|
|
61
|
+
readonly width: number;
|
|
62
|
+
readonly height: number;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The node changes that put node `id` at `box` (absolute), with its parent's
|
|
67
|
+
* absolute top-left read from `lookup`: a position and a dimensions change,
|
|
68
|
+
* the pair xyflow's own resizer emits.
|
|
69
|
+
*/
|
|
70
|
+
export declare const xyflowResizeChanges: <UserNode>(lookup: ReadonlyMap<string, XyInternalNode<UserNode>>, id: string, box: Box, resizing: boolean) => XyResizeChange[];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { nodeSize, referenceBoxes } from "./boxes.js";
|
|
2
|
+
/** The absolute box of an xyflow internal node, `null` while unmeasured or hidden. */
|
|
3
|
+
const xyflowBox = (internal) => {
|
|
4
|
+
const dims = nodeSize(internal);
|
|
5
|
+
if (!dims || internal.hidden)
|
|
6
|
+
return null;
|
|
7
|
+
const { x, y } = internal.internals.positionAbsolute;
|
|
8
|
+
return { x, y, ...dims, id: internal.id };
|
|
9
|
+
};
|
|
10
|
+
/** The reference boxes of an xyflow `nodeLookup`, with `snapTo` asked about the user's node. */
|
|
11
|
+
export const xyflowReferences = (lookup, exclude, snapTo) => referenceBoxes(lookup.values(), xyflowBox, exclude, (internal) => snapTo(internal.internals.userNode));
|
|
12
|
+
/** The absolute top-left of `id`'s parent in an xyflow `nodeLookup`, the origin when it has none. */
|
|
13
|
+
export const xyflowParentOrigin = (lookup, id) => {
|
|
14
|
+
const parentId = lookup.get(id)?.parentId;
|
|
15
|
+
return (parentId === undefined ? undefined : lookup.get(parentId)?.internals.positionAbsolute) ?? { x: 0, y: 0 };
|
|
16
|
+
};
|
|
17
|
+
const ZERO = { x: 0, y: 0 };
|
|
18
|
+
/** A dragged node's absolute box, from the position xyflow just computed for it; `null` before it is measured. */
|
|
19
|
+
export const xyflowDraggedBox = (lookup, node) => {
|
|
20
|
+
const measured = nodeSize(lookup.get(node.id) ?? {});
|
|
21
|
+
const origin = xyflowParentOrigin(lookup, node.id);
|
|
22
|
+
return measured ? { x: origin.x + node.position.x, y: origin.y + node.position.y, ...measured, id: node.id } : null;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The drag items for `nodes` moved by `offset`, in the shape the store's
|
|
26
|
+
* `updateNodePositions` takes. A node missing from `lookup` or not measured
|
|
27
|
+
* yet is left out.
|
|
28
|
+
*/
|
|
29
|
+
export const xyflowDragItems = (lookup, nodes, offset) => {
|
|
30
|
+
const items = new Map();
|
|
31
|
+
for (const node of nodes) {
|
|
32
|
+
const measured = nodeSize(lookup.get(node.id) ?? {});
|
|
33
|
+
if (!measured)
|
|
34
|
+
continue;
|
|
35
|
+
const origin = xyflowParentOrigin(lookup, node.id);
|
|
36
|
+
const position = { x: node.position.x + offset.x, y: node.position.y + offset.y };
|
|
37
|
+
items.set(node.id, { id: node.id, position, distance: ZERO, measured, internals: { positionAbsolute: { x: origin.x + position.x, y: origin.y + position.y } } });
|
|
38
|
+
}
|
|
39
|
+
return items;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The node changes that put node `id` at `box` (absolute), with its parent's
|
|
43
|
+
* absolute top-left read from `lookup`: a position and a dimensions change,
|
|
44
|
+
* the pair xyflow's own resizer emits.
|
|
45
|
+
*/
|
|
46
|
+
export const xyflowResizeChanges = (lookup, id, box, resizing) => {
|
|
47
|
+
const origin = xyflowParentOrigin(lookup, id);
|
|
48
|
+
return [
|
|
49
|
+
{ id, type: "position", position: { x: box.x - origin.x, y: box.y - origin.y } },
|
|
50
|
+
{ id, type: "dimensions", resizing, setAttributes: true, dimensions: { width: box.width, height: box.height } },
|
|
51
|
+
];
|
|
52
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The live guides of a gesture as refs, shared by the drag and resize composables. */
|
|
2
|
+
import { type ComputedRef } from "vue";
|
|
3
|
+
import type { GapGuide, Guide } from "../core/index.js";
|
|
4
|
+
import type { GuideState } from "../flow/index.js";
|
|
5
|
+
export interface GuideRefs {
|
|
6
|
+
/** The guides to draw now; empty between gestures. */
|
|
7
|
+
readonly guides: ComputedRef<readonly Guide[]>;
|
|
8
|
+
/** The equal gaps to draw now; empty between gestures. */
|
|
9
|
+
readonly gaps: ComputedRef<readonly GapGuide[]>;
|
|
10
|
+
}
|
|
11
|
+
/** Refs for the guides, and the setter to give a gesture as its `onGuides`. */
|
|
12
|
+
export declare const createGuideRefs: () => GuideRefs & {
|
|
13
|
+
readonly set: (state: GuideState) => void;
|
|
14
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The live guides of a gesture as refs, shared by the drag and resize composables. */
|
|
2
|
+
import { computed, shallowRef } from "vue";
|
|
3
|
+
/** Refs for the guides, and the setter to give a gesture as its `onGuides`. */
|
|
4
|
+
export const createGuideRefs = () => {
|
|
5
|
+
// Shallow: the guide objects are replaced as a whole on every frame.
|
|
6
|
+
const guides = shallowRef([]);
|
|
7
|
+
const gaps = shallowRef([]);
|
|
8
|
+
return {
|
|
9
|
+
guides: computed(() => guides.value),
|
|
10
|
+
gaps: computed(() => gaps.value),
|
|
11
|
+
set: (state) => {
|
|
12
|
+
guides.value = state.guides;
|
|
13
|
+
gaps.value = state.gaps;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Vue Flow adapter: `useNodeSnap` and `useNodeResizeSnap` for node drags and resizes, `SnapGuides` to draw. */
|
|
2
|
+
export { SnapGuides } from "./snap-guides.js";
|
|
3
|
+
export { useNodeSnap, type NodeSnap, type NodeSnapOptions } from "./use-node-snap.js";
|
|
4
|
+
export { useNodeResizeSnap, type NodeResizeSnap, type NodeResizeSnapOptions } from "./use-node-resize-snap.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Vue Flow adapter: `useNodeSnap` and `useNodeResizeSnap` for node drags and resizes, `SnapGuides` to draw. */
|
|
2
|
+
export { SnapGuides } from "./snap-guides.js";
|
|
3
|
+
export { useNodeSnap } from "./use-node-snap.js";
|
|
4
|
+
export { useNodeResizeSnap } from "./use-node-resize-snap.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Box, Vec } from "../core/index.js";
|
|
2
|
+
/** The part of a Vue Flow `GraphNode` the adapter reads. */
|
|
3
|
+
export interface PlacedNode {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly hidden?: boolean | undefined;
|
|
6
|
+
/** Relative to the parent node, or to the flow without one. */
|
|
7
|
+
readonly position: Vec;
|
|
8
|
+
/** Absolute; lags `position` until Vue's next post-flush. */
|
|
9
|
+
readonly computedPosition: Vec;
|
|
10
|
+
readonly dimensions: {
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly height: number;
|
|
13
|
+
};
|
|
14
|
+
readonly parentNode?: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** The origin of a node's coordinate space: its parent's absolute top-left, or the flow's. */
|
|
17
|
+
export declare const parentOrigin: (parent: PlacedNode | undefined) => Vec;
|
|
18
|
+
/** A node's settled absolute box, `null` while unmeasured (a size of 0) or hidden. */
|
|
19
|
+
export declare const vueFlowBox: (node: PlacedNode) => Box | null;
|
|
20
|
+
/** The absolute box of a node a gesture just moved, from its up-to-date relative `position`. */
|
|
21
|
+
export declare const vueFlowLiveBox: (node: PlacedNode, parent: PlacedNode | undefined) => Box | null;
|
|
22
|
+
/** The reference boxes of `nodes`: measured, visible, not in `exclude`, and accepted by `snapTo`. */
|
|
23
|
+
export declare const vueFlowReferences: <Node extends PlacedNode>(nodes: Iterable<Node>, exclude: readonly string[], snapTo: (node: Node) => boolean) => Box[];
|
|
24
|
+
/**
|
|
25
|
+
* The pointer event under a d3-drag event (its `sourceEvent` is typed `any`),
|
|
26
|
+
* `null` when it is not one. Checked by shape (`altKey` is a boolean, as on every
|
|
27
|
+
* mouse and touch event), so it does not depend on a `TouchEvent` global.
|
|
28
|
+
*/
|
|
29
|
+
export declare const pointerEventOf: (sourceEvent: unknown) => MouseEvent | TouchEvent | null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { referenceBoxes } from "../flow/index.js";
|
|
2
|
+
/** The origin of a node's coordinate space: its parent's absolute top-left, or the flow's. */
|
|
3
|
+
export const parentOrigin = (parent) => parent?.computedPosition ?? { x: 0, y: 0 };
|
|
4
|
+
/** A node's settled absolute box, `null` while unmeasured (a size of 0) or hidden. */
|
|
5
|
+
export const vueFlowBox = (node) => {
|
|
6
|
+
const { width, height } = node.dimensions;
|
|
7
|
+
return width === 0 || height === 0 || node.hidden
|
|
8
|
+
? null
|
|
9
|
+
: { x: node.computedPosition.x, y: node.computedPosition.y, width, height, id: node.id };
|
|
10
|
+
};
|
|
11
|
+
/** The absolute box of a node a gesture just moved, from its up-to-date relative `position`. */
|
|
12
|
+
export const vueFlowLiveBox = (node, parent) => {
|
|
13
|
+
const box = vueFlowBox(node);
|
|
14
|
+
if (!box)
|
|
15
|
+
return null;
|
|
16
|
+
const origin = parentOrigin(parent);
|
|
17
|
+
return { ...box, x: origin.x + node.position.x, y: origin.y + node.position.y };
|
|
18
|
+
};
|
|
19
|
+
/** The reference boxes of `nodes`: measured, visible, not in `exclude`, and accepted by `snapTo`. */
|
|
20
|
+
export const vueFlowReferences = (nodes, exclude, snapTo) => referenceBoxes(nodes, vueFlowBox, exclude, snapTo);
|
|
21
|
+
/**
|
|
22
|
+
* The pointer event under a d3-drag event (its `sourceEvent` is typed `any`),
|
|
23
|
+
* `null` when it is not one. Checked by shape (`altKey` is a boolean, as on every
|
|
24
|
+
* mouse and touch event), so it does not depend on a `TouchEvent` global.
|
|
25
|
+
*/
|
|
26
|
+
export const pointerEventOf = (sourceEvent) => typeof sourceEvent === "object" && sourceEvent !== null && "altKey" in sourceEvent && typeof sourceEvent.altKey === "boolean"
|
|
27
|
+
? sourceEvent
|
|
28
|
+
: null;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type PropType } from "vue";
|
|
2
|
+
import { type GapGuide, type Guide } from "../core/index.js";
|
|
3
|
+
export declare const SnapGuides: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
4
|
+
guides: {
|
|
5
|
+
type: PropType<readonly Guide[]>;
|
|
6
|
+
required: true;
|
|
7
|
+
};
|
|
8
|
+
gaps: {
|
|
9
|
+
type: PropType<readonly GapGuide[]>;
|
|
10
|
+
default: () => never[];
|
|
11
|
+
};
|
|
12
|
+
}>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}>, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
15
|
+
guides: {
|
|
16
|
+
type: PropType<readonly Guide[]>;
|
|
17
|
+
required: true;
|
|
18
|
+
};
|
|
19
|
+
gaps: {
|
|
20
|
+
type: PropType<readonly GapGuide[]>;
|
|
21
|
+
default: () => never[];
|
|
22
|
+
};
|
|
23
|
+
}>> & Readonly<{}>, {
|
|
24
|
+
gaps: readonly GapGuide[];
|
|
25
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Draws the guides from `useNodeSnap` and `useNodeResizeSnap` on a Vue Flow
|
|
3
|
+
* canvas, in flow coordinates, so they pan and zoom with the nodes. Lines stay
|
|
4
|
+
* one screen pixel wide at any zoom; each aligned point gets a small cross, as
|
|
5
|
+
* in Excalidraw; centre guides are dashed. Equal gaps (`gaps`) are drawn as
|
|
6
|
+
* lines with a tick across each end.
|
|
7
|
+
*
|
|
8
|
+
* Vue Flow has no portal into its viewport, so this draws its own SVG and
|
|
9
|
+
* applies the viewport transform to it, the same `translate(x, y) scale(zoom)`
|
|
10
|
+
* the transformation pane gets (vue-flow-core.mjs:8397-8412). Put it in the
|
|
11
|
+
* default slot of `<VueFlow>`, like `<MiniMap>`: it then sits above the
|
|
12
|
+
* viewport (z-index 4 in Vue Flow's stylesheet) and ignores the pointer. Plain
|
|
13
|
+
* `h()` instead of an SFC, because the package is built without a Vue
|
|
14
|
+
* compiler.
|
|
15
|
+
*
|
|
16
|
+
* Colour: set `--snap-guide-color` on any ancestor (default a magenta that
|
|
17
|
+
* reads on light and dark canvases). Stacking: `--snap-guide-z` (default 5,
|
|
18
|
+
* the level of Vue Flow's panels).
|
|
19
|
+
*/
|
|
20
|
+
import { useVueFlow } from "@vue-flow/core";
|
|
21
|
+
import { defineComponent, h } from "vue";
|
|
22
|
+
import { guideMarks } from "../core/index.js";
|
|
23
|
+
const SVG_STYLE = {
|
|
24
|
+
position: "absolute",
|
|
25
|
+
insetBlockStart: 0,
|
|
26
|
+
insetInlineStart: 0,
|
|
27
|
+
inlineSize: "1px",
|
|
28
|
+
blockSize: "1px",
|
|
29
|
+
overflow: "visible",
|
|
30
|
+
pointerEvents: "none",
|
|
31
|
+
fill: "none",
|
|
32
|
+
stroke: "var(--snap-guide-color, #e0447c)",
|
|
33
|
+
strokeWidth: "1px",
|
|
34
|
+
transformOrigin: "0 0",
|
|
35
|
+
zIndex: "var(--snap-guide-z, 5)",
|
|
36
|
+
};
|
|
37
|
+
const STROKE = { vectorEffect: "non-scaling-stroke" };
|
|
38
|
+
export const SnapGuides = defineComponent({
|
|
39
|
+
name: "SnapGuides",
|
|
40
|
+
props: {
|
|
41
|
+
guides: { type: Array, required: true },
|
|
42
|
+
gaps: { type: Array, default: () => [] },
|
|
43
|
+
},
|
|
44
|
+
setup(props) {
|
|
45
|
+
// Injected from the enclosing <VueFlow>.
|
|
46
|
+
const { viewport } = useVueFlow();
|
|
47
|
+
return () => {
|
|
48
|
+
const { x, y, zoom } = viewport.value;
|
|
49
|
+
return h("svg", { class: "snap-guides", "aria-hidden": "true", style: { ...SVG_STYLE, transform: `translate(${String(x)}px, ${String(y)}px) scale(${String(zoom)})` } }, guideMarks(props.guides, props.gaps, zoom).map((mark) => mark.kind === "line"
|
|
50
|
+
? h("line", { key: mark.key, x1: mark.from.x, y1: mark.from.y, x2: mark.to.x, y2: mark.to.y, "stroke-dasharray": mark.dash?.join(" "), style: STROKE })
|
|
51
|
+
: h("path", { key: mark.key, d: mark.d, style: STROKE })));
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type GraphNode } from "@vue-flow/core";
|
|
2
|
+
import type { OnResize, OnResizeEnd, OnResizeStart } from "@vue-flow/node-resizer";
|
|
3
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
4
|
+
import { type GuideRefs } from "./guide-state.js";
|
|
5
|
+
export interface NodeResizeSnapOptions extends AdapterOptions<GraphNode, MouseEvent | TouchEvent> {
|
|
6
|
+
/** The id of the flow the nodes are in, as for `useVueFlow(id)`. Default: the flow the calling component belongs to. */
|
|
7
|
+
readonly flowId?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface NodeResizeSnap extends GuideRefs {
|
|
10
|
+
/** Pass to `<NodeResizer @resize-start>`. */
|
|
11
|
+
readonly onResizeStart: (event: OnResizeStart) => void;
|
|
12
|
+
/** Pass to `<NodeResizer @resize>`. */
|
|
13
|
+
readonly onResize: (event: OnResize) => void;
|
|
14
|
+
/** Pass to `<NodeResizer @resize-end>`. */
|
|
15
|
+
readonly onResizeEnd: (event: OnResizeEnd) => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create the resize handlers and the live guides. Call it in the setup of a
|
|
19
|
+
* component inside `<VueFlow>`, or the one that renders it, as for
|
|
20
|
+
* `useVueFlow()`. Pass the same `<SnapGuides>` the drag guides go to, e.g.
|
|
21
|
+
* `:guides="[...drag.guides.value, ...resize.guides.value]"`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const useNodeResizeSnap: (options?: NodeResizeSnapOptions) => NodeResizeSnap;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for Vue Flow nodes resized with `<NodeResizer>`, built on the
|
|
3
|
+
* shared resize gesture.
|
|
4
|
+
*
|
|
5
|
+
* The resizer (@vue-flow/node-resizer 1.5.1, vue-flow-node-resizer.mjs) keeps
|
|
6
|
+
* its own state from the pointer (`startValues`, `prevValues`, lines 1053-1054
|
|
7
|
+
* and 1072-1083) and on every move emits `resize` with the unsnapped box
|
|
8
|
+
* (line 1184) and then triggers the `nodesChange` hook with it (line 1185),
|
|
9
|
+
* which Vue Flow applies at once. A change triggered from `onResize` would
|
|
10
|
+
* therefore be overwritten by the one that follows it in the same task, so the
|
|
11
|
+
* snapped box is triggered in a microtask instead: it runs after that task's
|
|
12
|
+
* unsnapped write and before the browser renders, so the DOM is never painted
|
|
13
|
+
* unsnapped; Vue Flow's ResizeObserver then measures the node from the DOM, at
|
|
14
|
+
* the snapped size (vue-flow-core.mjs:9782-9791). The resizer
|
|
15
|
+
* diffs against its own `prevValues` (lines 1131-1132), not against the node,
|
|
16
|
+
* so what is written over the node does not change what it writes next frame.
|
|
17
|
+
* The snapped change carries `updateStyle`, as the resizer's own does (line
|
|
18
|
+
* 1157), because the node's width and height come from its inline style.
|
|
19
|
+
*
|
|
20
|
+
* When the resize ends the resizer triggers a dimension change with only
|
|
21
|
+
* `resizing: false` (lines 1190-1195), which `applyChanges` reads as "leave the
|
|
22
|
+
* size alone" (vue-flow-core.mjs:4014-4020). The last snapped box stays, and
|
|
23
|
+
* `onResizeEnd` only resets the gesture and clears the guides. (Svelte Flow
|
|
24
|
+
* writes the unsnapped size again at the end, so its adapter writes the
|
|
25
|
+
* snapped one again.)
|
|
26
|
+
*
|
|
27
|
+
* The callbacks are shared by every `<NodeResizer>` they are given to and
|
|
28
|
+
* say neither which node nor which edges are dragged, so `onResizeStart`
|
|
29
|
+
* reads both from the DOM element the gesture began on. Assumes the default
|
|
30
|
+
* node origin; `keepAspectRatio` and min/max sizes can be overridden, since the
|
|
31
|
+
* snapped box is written over the resizer's.
|
|
32
|
+
*/
|
|
33
|
+
import { useVueFlow } from "@vue-flow/core";
|
|
34
|
+
import { createResizeGesture, resizeTarget } from "../flow/index.js";
|
|
35
|
+
import { createGuideRefs } from "./guide-state.js";
|
|
36
|
+
import { pointerEventOf, parentOrigin, vueFlowReferences } from "./nodes.js";
|
|
37
|
+
/**
|
|
38
|
+
* Create the resize handlers and the live guides. Call it in the setup of a
|
|
39
|
+
* component inside `<VueFlow>`, or the one that renders it, as for
|
|
40
|
+
* `useVueFlow()`. Pass the same `<SnapGuides>` the drag guides go to, e.g.
|
|
41
|
+
* `:guides="[...drag.guides.value, ...resize.guides.value]"`.
|
|
42
|
+
*/
|
|
43
|
+
export const useNodeResizeSnap = (options = {}) => {
|
|
44
|
+
const flow = useVueFlow(options.flowId);
|
|
45
|
+
const snapTo = options.snapTo ?? (() => true);
|
|
46
|
+
const { set, ...refs } = createGuideRefs();
|
|
47
|
+
const gesture = createResizeGesture(options, set);
|
|
48
|
+
const parentOf = (node) => (node.parentNode === undefined ? undefined : flow.findNode(node.parentNode));
|
|
49
|
+
/** Trigger `box` (absolute) as the node's position and size, after the resizer's own change. */
|
|
50
|
+
const place = (box) => {
|
|
51
|
+
queueMicrotask(() => {
|
|
52
|
+
const node = box.id === undefined ? undefined : flow.findNode(box.id);
|
|
53
|
+
if (!node)
|
|
54
|
+
return;
|
|
55
|
+
const origin = parentOrigin(parentOf(node));
|
|
56
|
+
void flow.emits.nodesChange([
|
|
57
|
+
{ id: node.id, type: "position", from: { x: node.position.x, y: node.position.y }, position: { x: box.x - origin.x, y: box.y - origin.y } },
|
|
58
|
+
{ id: node.id, type: "dimensions", resizing: true, updateStyle: true, dimensions: { width: box.width, height: box.height } },
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
...refs,
|
|
64
|
+
onResizeStart: ({ event }) => {
|
|
65
|
+
gesture.start(resizeTarget(pointerEventOf(event.sourceEvent)?.target ?? null, "vue-flow"));
|
|
66
|
+
},
|
|
67
|
+
onResize: ({ event, params }) => {
|
|
68
|
+
const id = gesture.target?.id;
|
|
69
|
+
const source = pointerEventOf(event.sourceEvent);
|
|
70
|
+
const node = id === undefined ? undefined : flow.findNode(id);
|
|
71
|
+
if (id === undefined || !source || !node)
|
|
72
|
+
return;
|
|
73
|
+
const origin = parentOrigin(parentOf(node));
|
|
74
|
+
const box = { x: origin.x + params.x, y: origin.y + params.y, width: params.width, height: params.height, id };
|
|
75
|
+
const snapped = gesture.move(source, box, () => vueFlowReferences(flow.nodes.value, [id], snapTo), flow.viewport.value.zoom);
|
|
76
|
+
if (snapped)
|
|
77
|
+
place(snapped);
|
|
78
|
+
},
|
|
79
|
+
onResizeEnd: () => {
|
|
80
|
+
gesture.end();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type GraphNode, type MouseTouchEvent } from "@vue-flow/core";
|
|
2
|
+
import { type AdapterOptions } from "../flow/index.js";
|
|
3
|
+
import { type GuideRefs } from "./guide-state.js";
|
|
4
|
+
export interface NodeSnapOptions extends AdapterOptions<GraphNode, MouseTouchEvent> {
|
|
5
|
+
/** The id of the flow to hook into, as for `useVueFlow(id)`. Default: the flow the calling component belongs to. */
|
|
6
|
+
readonly flowId?: string;
|
|
7
|
+
}
|
|
8
|
+
export type NodeSnap = GuideRefs;
|
|
9
|
+
/**
|
|
10
|
+
* Hook snapping into a flow's node drags and return the live guides. Call it
|
|
11
|
+
* in the setup of the component that renders `<VueFlow>` (or one inside it),
|
|
12
|
+
* as for `useVueFlow()`; the hooks go away with that component. Hand the
|
|
13
|
+
* guides to `<SnapGuides>`.
|
|
14
|
+
*/
|
|
15
|
+
export declare const useNodeSnap: (options?: NodeSnapOptions) => NodeSnap;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Snapping for dragged Vue Flow nodes, built on the shared drag gesture.
|
|
3
|
+
*
|
|
4
|
+
* Vue Flow's own drag (useDrag, vue-flow-core.mjs, v1.48.2) recomputes every
|
|
5
|
+
* dragged node's position from the pointer on each move. `updateNodes` first
|
|
6
|
+
* triggers the `nodesChange` hook with the unsnapped positions (line 4940),
|
|
7
|
+
* which the default handler applies to the nodes at once (the hook calls its
|
|
8
|
+
* listeners synchronously, line 4132; `applyChanges` writes `node.position`,
|
|
9
|
+
* lines 3998-4001), and only then calls `onDrag` (line 4948), which is the
|
|
10
|
+
* `nodeDrag` hook. So a handler on `nodeDrag` runs after the unsnapped write,
|
|
11
|
+
* in the same task and before anything is rendered: the snapped position it
|
|
12
|
+
* triggers is the last write of the frame and wins. Vue Flow keeps its own
|
|
13
|
+
* pointer-derived position in its drag items (`n.position`, line 4933), not in
|
|
14
|
+
* the node, so the next frame starts from the pointer again and the offset
|
|
15
|
+
* never accumulates.
|
|
16
|
+
*
|
|
17
|
+
* When the drag ends Vue Flow does not write the unsnapped positions again:
|
|
18
|
+
* it triggers a position change with no `position` (`changed` is false, lines
|
|
19
|
+
* 5046 and 6283-6297), which `applyChanges` reads as "only clear `dragging`"
|
|
20
|
+
* (lines 3998-4004). The last snapped position therefore stays, and
|
|
21
|
+
* `nodeDragStop` only resets the gesture and clears the guides. (Svelte Flow
|
|
22
|
+
* does write the unsnapped positions at the end, so its adapter writes the
|
|
23
|
+
* snapped ones again.)
|
|
24
|
+
*
|
|
25
|
+
* The handlers are registered through `useVueFlow()`'s `onNodeDrag` and
|
|
26
|
+
* `onNodeDragStop` hooks, which run for a single node's drag and for a drag of
|
|
27
|
+
* the selection box alike (NodesSelection emits `nodeDrag` as well, line
|
|
28
|
+
* 8130), and which a `@node-drag` listener on `<VueFlow>` does not replace.
|
|
29
|
+
*
|
|
30
|
+
* Assumes Vue Flow applies its changes itself (the default `applyDefault`),
|
|
31
|
+
* so `node.position` is current inside `nodeDrag`; the snapped position is
|
|
32
|
+
* written over `extent` and `snapToGrid`, which are not applied again.
|
|
33
|
+
*/
|
|
34
|
+
import { useVueFlow } from "@vue-flow/core";
|
|
35
|
+
import { createDragGesture } from "../flow/index.js";
|
|
36
|
+
import { createGuideRefs } from "./guide-state.js";
|
|
37
|
+
import { vueFlowLiveBox, vueFlowReferences } from "./nodes.js";
|
|
38
|
+
/**
|
|
39
|
+
* Hook snapping into a flow's node drags and return the live guides. Call it
|
|
40
|
+
* in the setup of the component that renders `<VueFlow>` (or one inside it),
|
|
41
|
+
* as for `useVueFlow()`; the hooks go away with that component. Hand the
|
|
42
|
+
* guides to `<SnapGuides>`.
|
|
43
|
+
*/
|
|
44
|
+
export const useNodeSnap = (options = {}) => {
|
|
45
|
+
const flow = useVueFlow(options.flowId);
|
|
46
|
+
const snapTo = options.snapTo ?? (() => true);
|
|
47
|
+
const { set, ...refs } = createGuideRefs();
|
|
48
|
+
const gesture = createDragGesture(options, set);
|
|
49
|
+
flow.onNodeDrag(({ event, nodes }) => {
|
|
50
|
+
const ids = nodes.map((node) => node.id);
|
|
51
|
+
const moving = nodes.flatMap((node) => vueFlowLiveBox(node, node.parentNode === undefined ? undefined : flow.findNode(node.parentNode)) ?? []);
|
|
52
|
+
const offset = gesture.move(event, moving, () => vueFlowReferences(flow.nodes.value, ids, snapTo), flow.viewport.value.zoom);
|
|
53
|
+
if (!offset)
|
|
54
|
+
return;
|
|
55
|
+
void flow.emits.nodesChange(nodes.map((node) => ({
|
|
56
|
+
id: node.id,
|
|
57
|
+
type: "position",
|
|
58
|
+
dragging: true,
|
|
59
|
+
from: { x: node.computedPosition.x, y: node.computedPosition.y },
|
|
60
|
+
position: { x: node.position.x + offset.x, y: node.position.y + offset.y },
|
|
61
|
+
})));
|
|
62
|
+
});
|
|
63
|
+
flow.onNodeDragStop(() => {
|
|
64
|
+
gesture.end();
|
|
65
|
+
});
|
|
66
|
+
return refs;
|
|
67
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** React Flow (xyflow) adapter: `useNodeSnap` and `useNodeResizeSnap` for the drag and resize handlers, `<SnapGuides>` to draw. */
|
|
2
|
+
export { SnapGuides, type SnapGuidesProps } from "./snap-guides.js";
|
|
3
|
+
export { useNodeSnap, type NodeSnap, type NodeSnapOptions } from "./use-node-snap.js";
|
|
4
|
+
export { useNodeResizeSnap, type NodeResizeSnap, type NodeResizeSnapOptions } from "./use-node-resize-snap.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** React Flow (xyflow) adapter: `useNodeSnap` and `useNodeResizeSnap` for the drag and resize handlers, `<SnapGuides>` to draw. */
|
|
2
|
+
export { SnapGuides } from "./snap-guides.js";
|
|
3
|
+
export { useNodeSnap } from "./use-node-snap.js";
|
|
4
|
+
export { useNodeResizeSnap } from "./use-node-resize-snap.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
import { type GapGuide, type Guide } from "../core/index.js";
|
|
3
|
+
export interface SnapGuidesProps {
|
|
4
|
+
readonly guides: readonly Guide[];
|
|
5
|
+
readonly gaps?: readonly GapGuide[];
|
|
6
|
+
}
|
|
7
|
+
/** The SVG elements for `guides` and `gaps` at `zoom`: lines, crosses and gap ticks. */
|
|
8
|
+
export declare const snapGuideMarks: (guides: readonly Guide[], gaps: readonly GapGuide[], zoom: number) => ReactElement[];
|
|
9
|
+
/** The guides and equal gaps of a gesture, drawn in front of the nodes. Render it as a child of `<ReactFlow>`. */
|
|
10
|
+
export declare const SnapGuides: ({ guides, gaps }: SnapGuidesProps) => ReactElement;
|