@fragiola/flow 0.0.0-stage → 0.1.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 (52) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +25 -2
  4. package/dist/connect-session-nyEp02_P.js +375 -0
  5. package/dist/connect-session-nyEp02_P.js.map +1 -0
  6. package/dist/connection.d.ts +31 -0
  7. package/dist/connection.js +60 -0
  8. package/dist/connection.js.map +1 -0
  9. package/dist/core-queries-BPNbcM6k.js +452 -0
  10. package/dist/core-queries-BPNbcM6k.js.map +1 -0
  11. package/dist/document-schema-CPK5xkyO.js +369 -0
  12. package/dist/document-schema-CPK5xkyO.js.map +1 -0
  13. package/dist/drag.d.ts +19 -0
  14. package/dist/drag.js +175 -0
  15. package/dist/drag.js.map +1 -0
  16. package/dist/index.d.ts +876 -0
  17. package/dist/index.js +5711 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/keyboard-links-CVaTtlZg.js +8 -0
  20. package/dist/keyboard-links-CVaTtlZg.js.map +1 -0
  21. package/dist/keyboard.d.ts +40 -0
  22. package/dist/keyboard.js +342 -0
  23. package/dist/keyboard.js.map +1 -0
  24. package/dist/minimap.d.ts +26 -0
  25. package/dist/minimap.js +206 -0
  26. package/dist/minimap.js.map +1 -0
  27. package/dist/node-moves-DyWe-4dj.js +36 -0
  28. package/dist/node-moves-DyWe-4dj.js.map +1 -0
  29. package/dist/part-attributes-DuBltA__.js +71 -0
  30. package/dist/part-attributes-DuBltA__.js.map +1 -0
  31. package/dist/path-format-2OU7Eiyy.js +43 -0
  32. package/dist/path-format-2OU7Eiyy.js.map +1 -0
  33. package/dist/reconnection.d.ts +15 -0
  34. package/dist/reconnection.js +138 -0
  35. package/dist/reconnection.js.map +1 -0
  36. package/dist/rect-CmoREaR6.js +92 -0
  37. package/dist/rect-CmoREaR6.js.map +1 -0
  38. package/dist/resize.d.ts +33 -0
  39. package/dist/resize.js +316 -0
  40. package/dist/resize.js.map +1 -0
  41. package/dist/rules-DLOMrPBG.js +102 -0
  42. package/dist/rules-DLOMrPBG.js.map +1 -0
  43. package/dist/selection.d.ts +38 -0
  44. package/dist/selection.js +392 -0
  45. package/dist/selection.js.map +1 -0
  46. package/dist/subflows.d.ts +9 -0
  47. package/dist/subflows.js +237 -0
  48. package/dist/subflows.js.map +1 -0
  49. package/dist/types-CLHcZVCb.d.ts +2660 -0
  50. package/dist/viewport-math-B-9Pn2kN.js +69 -0
  51. package/dist/viewport-math-B-9Pn2kN.js.map +1 -0
  52. package/package.json +94 -3
@@ -0,0 +1,92 @@
1
+ // src/geometry/rect.ts
2
+ /** The smallest rectangle holding every rectangle given (`undefined` for none). */
3
+ function boundsOf(rects) {
4
+ let minX = Number.POSITIVE_INFINITY;
5
+ let minY = Number.POSITIVE_INFINITY;
6
+ let maxX = Number.NEGATIVE_INFINITY;
7
+ let maxY = Number.NEGATIVE_INFINITY;
8
+ for (const rect of rects) {
9
+ minX = Math.min(minX, rect.x);
10
+ minY = Math.min(minY, rect.y);
11
+ maxX = Math.max(maxX, rect.x + rect.width);
12
+ maxY = Math.max(maxY, rect.y + rect.height);
13
+ }
14
+ return minX === Number.POSITIVE_INFINITY ? void 0 : {
15
+ x: minX,
16
+ y: minY,
17
+ width: maxX - minX,
18
+ height: maxY - minY
19
+ };
20
+ }
21
+ /** Whether two rectangles share some area, or touch (a zero-size rectangle inside counts). */
22
+ function overlaps(a, b) {
23
+ return a.x <= b.x + b.width && b.x <= a.x + a.width && a.y <= b.y + b.height && b.y <= a.y + a.height;
24
+ }
25
+ /**
26
+ * Whether two rectangles share some area: touching sides do not, a rectangle of no size strictly
27
+ * inside the other does.
28
+ */
29
+ function sharesArea(a, b) {
30
+ return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
31
+ }
32
+ /** Whether `inner` lies wholly inside `outer`. */
33
+ function contains(outer, inner) {
34
+ return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.width <= outer.x + outer.width && inner.y + inner.height <= outer.y + outer.height;
35
+ }
36
+ /** A rectangle from two corners in any order. */
37
+ function rectFromCorners(a, b) {
38
+ return {
39
+ x: Math.min(a.x, b.x),
40
+ y: Math.min(a.y, b.y),
41
+ width: Math.abs(b.x - a.x),
42
+ height: Math.abs(b.y - a.y)
43
+ };
44
+ }
45
+ /**
46
+ * A node's box from its position and its size: the position stands at `origin` of the box (shares
47
+ * of its size, `{ x: 0, y: 0 }` its top-left corner, `{ x: 0.5, y: 0.5 }` its centre). Every box
48
+ * the engine derives from a node goes through it (`nodeOrigin`).
49
+ */
50
+ function nodeBox(position, size, origin) {
51
+ return {
52
+ x: position.x - origin.x * size.width,
53
+ y: position.y - origin.y * size.height,
54
+ width: size.width,
55
+ height: size.height
56
+ };
57
+ }
58
+ /** The intersection of rectangles (`undefined` for none given); an empty one keeps its corner. */
59
+ function intersectionOf(rects) {
60
+ let left = Number.NEGATIVE_INFINITY;
61
+ let top = Number.NEGATIVE_INFINITY;
62
+ let right = Number.POSITIVE_INFINITY;
63
+ let bottom = Number.POSITIVE_INFINITY;
64
+ let found = false;
65
+ for (const rect of rects) {
66
+ found = true;
67
+ left = Math.max(left, rect.x);
68
+ top = Math.max(top, rect.y);
69
+ right = Math.min(right, rect.x + rect.width);
70
+ bottom = Math.min(bottom, rect.y + rect.height);
71
+ }
72
+ return found ? {
73
+ x: left,
74
+ y: top,
75
+ width: Math.max(0, right - left),
76
+ height: Math.max(0, bottom - top)
77
+ } : void 0;
78
+ }
79
+ /**
80
+ * A box's top-left corner moved so the box stays inside `bounds` (as far as it fits: a box larger
81
+ * than the bounds keeps to their top-left corner).
82
+ */
83
+ function cornerInside(corner, size, bounds) {
84
+ const keep = (value, min, max) => Math.max(min, Math.min(value, max));
85
+ return {
86
+ x: keep(corner.x, bounds.x, bounds.x + bounds.width - size.width),
87
+ y: keep(corner.y, bounds.y, bounds.y + bounds.height - size.height)
88
+ };
89
+ }
90
+ export { nodeBox as a, sharesArea as c, intersectionOf as i, contains as n, overlaps as o, cornerInside as r, rectFromCorners as s, boundsOf as t };
91
+
92
+ //# sourceMappingURL=rect-CmoREaR6.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rect-CmoREaR6.js","names":[],"sources":["../src/geometry/rect.ts"],"sourcesContent":["// Rectangle helpers: bounds, overlap and containment, in any one coordinate space.\nimport type { Rect } from \"./types\";\n\n/** The smallest rectangle holding every rectangle given (`undefined` for none). */\nexport function boundsOf(rects: Iterable<Rect>): Rect | undefined {\n let minX = Number.POSITIVE_INFINITY;\n let minY = Number.POSITIVE_INFINITY;\n let maxX = Number.NEGATIVE_INFINITY;\n let maxY = Number.NEGATIVE_INFINITY;\n for (const rect of rects) {\n minX = Math.min(minX, rect.x);\n minY = Math.min(minY, rect.y);\n maxX = Math.max(maxX, rect.x + rect.width);\n maxY = Math.max(maxY, rect.y + rect.height);\n }\n return minX === Number.POSITIVE_INFINITY\n ? undefined\n : { x: minX, y: minY, width: maxX - minX, height: maxY - minY };\n}\n\n/** Whether two rectangles share some area, or touch (a zero-size rectangle inside counts). */\nexport function overlaps(a: Rect, b: Rect): boolean {\n return (\n a.x <= b.x + b.width &&\n b.x <= a.x + a.width &&\n a.y <= b.y + b.height &&\n b.y <= a.y + a.height\n );\n}\n\n/**\n * Whether two rectangles share some area: touching sides do not, a rectangle of no size strictly\n * inside the other does.\n */\nexport function sharesArea(a: Rect, b: Rect): boolean {\n return (\n a.x < b.x + b.width &&\n b.x < a.x + a.width &&\n a.y < b.y + b.height &&\n b.y < a.y + a.height\n );\n}\n\n/** Whether `inner` lies wholly inside `outer`. */\nexport function contains(outer: Rect, inner: Rect): boolean {\n return (\n inner.x >= outer.x &&\n inner.y >= outer.y &&\n inner.x + inner.width <= outer.x + outer.width &&\n inner.y + inner.height <= outer.y + outer.height\n );\n}\n\n/** A rectangle from two corners in any order. */\nexport function rectFromCorners(\n a: { x: number; y: number },\n b: { x: number; y: number },\n): Rect {\n return {\n x: Math.min(a.x, b.x),\n y: Math.min(a.y, b.y),\n width: Math.abs(b.x - a.x),\n height: Math.abs(b.y - a.y),\n };\n}\n\n/**\n * A node's box from its position and its size: the position stands at `origin` of the box (shares\n * of its size, `{ x: 0, y: 0 }` its top-left corner, `{ x: 0.5, y: 0.5 }` its centre). Every box\n * the engine derives from a node goes through it (`nodeOrigin`).\n */\nexport function nodeBox(\n position: { x: number; y: number },\n size: { width: number; height: number },\n origin: { x: number; y: number },\n): Rect {\n return {\n x: position.x - origin.x * size.width,\n y: position.y - origin.y * size.height,\n width: size.width,\n height: size.height,\n };\n}\n\n/** The intersection of rectangles (`undefined` for none given); an empty one keeps its corner. */\nexport function intersectionOf(rects: Iterable<Rect>): Rect | undefined {\n let left = Number.NEGATIVE_INFINITY;\n let top = Number.NEGATIVE_INFINITY;\n let right = Number.POSITIVE_INFINITY;\n let bottom = Number.POSITIVE_INFINITY;\n let found = false;\n for (const rect of rects) {\n found = true;\n left = Math.max(left, rect.x);\n top = Math.max(top, rect.y);\n right = Math.min(right, rect.x + rect.width);\n bottom = Math.min(bottom, rect.y + rect.height);\n }\n return found\n ? {\n x: left,\n y: top,\n width: Math.max(0, right - left),\n height: Math.max(0, bottom - top),\n }\n : undefined;\n}\n\n/**\n * A box's top-left corner moved so the box stays inside `bounds` (as far as it fits: a box larger\n * than the bounds keeps to their top-left corner).\n */\nexport function cornerInside(\n corner: { x: number; y: number },\n size: { width: number; height: number },\n bounds: Rect,\n): { x: number; y: number } {\n const keep = (value: number, min: number, max: number) =>\n Math.max(min, Math.min(value, max));\n return {\n x: keep(corner.x, bounds.x, bounds.x + bounds.width - size.width),\n y: keep(corner.y, bounds.y, bounds.y + bounds.height - size.height),\n };\n}\n"],"mappings":";;AAIA,SAAgB,SAAS,OAAyC;CAC9D,IAAI,OAAO,OAAO;CAClB,IAAI,OAAO,OAAO;CAClB,IAAI,OAAO,OAAO;CAClB,IAAI,OAAO,OAAO;CAClB,KAAK,MAAM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KAAK;EAChC,GAAG,SAAS,IAAI,OAAO,IAAI,KAAK;EAChC,OAAO,KAAK;EACZ,QAAQ,KAAK;CACjB;AACJ;;AAGA,SAAgB,eAAe,OAAyC;CACpE,IAAI,OAAO,OAAO;CAClB,IAAI,MAAM,OAAO;CACjB,IAAI,QAAQ,OAAO;CACnB,IAAI,SAAS,OAAO;CACpB,IAAI,QAAQ;CACZ,KAAK,MAAM,QAAQ,OAAO;EACtB,QAAQ;EACR,OAAO,KAAK,IAAI,MAAM,KAAK,CAAC;EAC5B,MAAM,KAAK,IAAI,KAAK,KAAK,CAAC;EAC1B,QAAQ,KAAK,IAAI,OAAO,KAAK,IAAI,KAAK,KAAK;EAC3C,SAAS,KAAK,IAAI,QAAQ,KAAK,IAAI,KAAK,MAAM;CAClD;CACA,OAAO,QACD;EACI,GAAG;EACH,GAAG;EACH,OAAO,KAAK,IAAI,GAAG,QAAQ,IAAI;EAC/B,QAAQ,KAAK,IAAI,GAAG,SAAS,GAAG;CACpC,IACA,KAAA;AACV;;;;;AAMA,SAAgB,aACZ,QACA,MACA,QACwB;CACxB,MAAM,QAAQ,OAAe,KAAa,QACtC,KAAK,IAAI,KAAK,KAAK,IAAI,OAAO,GAAG,CAAC;CACtC,OAAO;EACH,GAAG,KAAK,OAAO,GAAG,OAAO,GAAG,OAAO,IAAI,OAAO,QAAQ,KAAK,KAAK;EAChE,GAAG,KAAK,OAAO,GAAG,OAAO,GAAG,OAAO,IAAI,OAAO,SAAS,KAAK,MAAM;CACtE;AACJ"}
@@ -0,0 +1,33 @@
1
+ import { f as FlowFeature } from "./types-CLHcZVCb.js";
2
+ /** Options of {@link resize}: the defaults of every resizer, read by its engine side. */
3
+ interface ResizeOptions {
4
+ /** the smallest width; default 10 */
5
+ readonly minWidth: number;
6
+ /** the smallest height; default 10 */
7
+ readonly minHeight: number;
8
+ /** the largest width; default none */
9
+ readonly maxWidth: number;
10
+ /** the largest height; default none */
11
+ readonly maxHeight: number;
12
+ /** whether a resize keeps the node's width-to-height ratio; default false */
13
+ readonly keepAspectRatio: boolean;
14
+ /**
15
+ * the grid (flow units) the moved sides snap to while resizing by pointer, so sizes and
16
+ * positions land on it; none: no snap (the drag feature's `snapGrid` is its own)
17
+ */
18
+ readonly snap: {
19
+ readonly x: number;
20
+ readonly y: number;
21
+ } | undefined;
22
+ }
23
+ /**
24
+ * The sides a resizer moves (its own option, `engine.adapter.setPartOptions("resizer", nodeId,
25
+ * { direction })`): `horizontal` only widths and x, `vertical` only heights and y; none: both.
26
+ */
27
+ type ResizeDirection = "horizontal" | "vertical";
28
+ /** The resize feature, as `resize()` makes it. */
29
+ type ResizeFeature = FlowFeature<"resize", ResizeOptions>;
30
+ /** Resizing nodes: `features: [resize()]` (`@fragiola/flow/resize`). */
31
+ export declare function resize(options?: Partial<ResizeOptions>): ResizeFeature;
32
+ export type { ResizeDirection, ResizeFeature, ResizeOptions };
33
+ //# sourceMappingURL=resize.d.ts.map
package/dist/resize.js ADDED
@@ -0,0 +1,316 @@
1
+ import { f as flag } from "./part-attributes-DuBltA__.js";
2
+ import { t as keyboardLinks } from "./keyboard-links-CVaTtlZg.js";
3
+ // src/resize/resize-engine.ts
4
+ /** The corners and sides a resize handle may be on. */
5
+ const RESIZE_POSITIONS = [
6
+ "top-left",
7
+ "top",
8
+ "top-right",
9
+ "right",
10
+ "bottom-right",
11
+ "bottom",
12
+ "bottom-left",
13
+ "left"
14
+ ];
15
+ const clamp = (value, min, max) => Math.min(Math.max(value, min), Math.max(min, max));
16
+ const snapTo = (value, grid) => grid > 0 ? Math.round(value / grid) * grid : value;
17
+ /**
18
+ * A node's box after a resize from `position` by `delta` (flow units), before the parent's rules:
19
+ * with a `direction`, only its sides move; with `snap`, the moved sides land on the grid.
20
+ */
21
+ function resizedRect(start, position, delta, limits) {
22
+ const { direction, snap } = limits;
23
+ const left = direction !== "vertical" && position.includes("left");
24
+ const right = direction !== "vertical" && position.includes("right");
25
+ const top = direction !== "horizontal" && position.startsWith("top");
26
+ const bottom = direction !== "horizontal" && position.startsWith("bottom");
27
+ const moved = { ...delta };
28
+ if (snap) {
29
+ const edgeX = left ? start.x : start.x + start.width;
30
+ const edgeY = top ? start.y : start.y + start.height;
31
+ moved.x = snapTo(edgeX + delta.x, snap.x) - edgeX;
32
+ moved.y = snapTo(edgeY + delta.y, snap.y) - edgeY;
33
+ }
34
+ let width = start.width + (right ? moved.x : left ? -moved.x : 0);
35
+ let height = start.height + (bottom ? moved.y : top ? -moved.y : 0);
36
+ if (limits.keepAspectRatio && direction === void 0 && start.width > 0 && start.height > 0) {
37
+ const horizontal = left || right;
38
+ let scale = horizontal && (top || bottom) ? Math.abs(width / start.width - 1) >= Math.abs(height / start.height - 1) ? width / start.width : height / start.height : horizontal ? width / start.width : height / start.height;
39
+ scale = clamp(scale, Math.max(limits.minWidth / start.width, limits.minHeight / start.height), Math.min(limits.maxWidth / start.width, limits.maxHeight / start.height));
40
+ width = start.width * scale;
41
+ height = start.height * scale;
42
+ } else {
43
+ width = clamp(width, limits.minWidth, limits.maxWidth);
44
+ height = clamp(height, limits.minHeight, limits.maxHeight);
45
+ }
46
+ return {
47
+ x: left ? start.x + start.width - width : start.x,
48
+ y: top ? start.y + start.height - height : start.y,
49
+ width,
50
+ height
51
+ };
52
+ }
53
+ /** The engine side of `resize()`. */
54
+ function resizeEngine(host, options) {
55
+ const { model } = host;
56
+ let resizing;
57
+ const limitsOf = (nodeId) => {
58
+ const own = host.partOptions("resizer", nodeId) ?? {};
59
+ const pick = (key) => typeof own[key] === typeof options[key] ? own[key] : options[key];
60
+ return {
61
+ minWidth: pick("minWidth"),
62
+ minHeight: pick("minHeight"),
63
+ maxWidth: pick("maxWidth"),
64
+ maxHeight: pick("maxHeight"),
65
+ keepAspectRatio: pick("keepAspectRatio"),
66
+ snap: options.snap,
67
+ direction: own.direction === "horizontal" || own.direction === "vertical" ? own.direction : void 0
68
+ };
69
+ };
70
+ /** The box (absolute) kept to its extent and to its children's rules. */
71
+ const kept = (start, rect, position, limits) => {
72
+ let { x, y, width, height } = rect;
73
+ const left = position.includes("left");
74
+ const top = position.startsWith("top");
75
+ const from = start.rect;
76
+ const bounds = host.extentOf(start.nodeId);
77
+ if (bounds) {
78
+ if (left && x < bounds.x) {
79
+ width -= bounds.x - x;
80
+ x = bounds.x;
81
+ }
82
+ if (top && y < bounds.y) {
83
+ height -= bounds.y - y;
84
+ y = bounds.y;
85
+ }
86
+ width = Math.min(width, bounds.x + bounds.width - x);
87
+ height = Math.min(height, bounds.y + bounds.height - y);
88
+ if (width < 0) width = Math.min(limits.minWidth, rect.width);
89
+ if (height < 0) height = Math.min(limits.minHeight, rect.height);
90
+ if (limits.keepAspectRatio && limits.direction === void 0 && from.width > 0 && from.height > 0 && (width !== rect.width || height !== rect.height)) {
91
+ const ratio = from.width / from.height;
92
+ const fitted = Math.min(width, height * ratio);
93
+ if (left) x += width - fitted;
94
+ if (top) y += height - fitted / ratio;
95
+ width = fitted;
96
+ height = fitted / ratio;
97
+ }
98
+ }
99
+ const fixed = start.children.some((child) => !model.is("node-draggable", { nodeId: child.id }));
100
+ const right = from.x + from.width;
101
+ const bottom = from.y + from.height;
102
+ if (fixed) {
103
+ if (left) {
104
+ x = from.x;
105
+ width = from.width;
106
+ }
107
+ if (top) {
108
+ y = from.y;
109
+ height = from.height;
110
+ }
111
+ }
112
+ const held = start.children.filter((child) => child.extent === "parent");
113
+ for (const child of held) {
114
+ const box = host.nodeRect(child.id);
115
+ if (!box) continue;
116
+ const childLeft = from.x + child.position.x;
117
+ const childTop = from.y + child.position.y;
118
+ if (left && x > childLeft) {
119
+ x = childLeft;
120
+ width = right - x;
121
+ }
122
+ if (top && y > childTop) {
123
+ y = childTop;
124
+ height = bottom - y;
125
+ }
126
+ width = Math.max(width, childLeft + box.width - x);
127
+ height = Math.max(height, childTop + box.height - y);
128
+ }
129
+ return {
130
+ x,
131
+ y,
132
+ width,
133
+ height
134
+ };
135
+ };
136
+ const stepOf = (start, rect) => {
137
+ const dx = rect.x - start.rect.x;
138
+ const dy = rect.y - start.rect.y;
139
+ const back = dx === 0 && dy === 0 && rect.width === start.rect.width && rect.height === start.rect.height;
140
+ const commands = [{
141
+ command: "node.resize",
142
+ payload: {
143
+ nodeId: start.nodeId,
144
+ width: rect.width,
145
+ height: rect.height,
146
+ position: back ? start.position : host.positionFor(start.nodeId, {
147
+ x: rect.x,
148
+ y: rect.y
149
+ }, rect)
150
+ }
151
+ }];
152
+ if (dx !== 0 || dy !== 0) for (const child of start.children) commands.push({
153
+ command: "node.move",
154
+ payload: {
155
+ nodeId: child.id,
156
+ position: {
157
+ x: child.position.x - dx,
158
+ y: child.position.y - dy
159
+ }
160
+ }
161
+ });
162
+ return commands;
163
+ };
164
+ const run = (commands, transient) => commands.length === 1 ? model.run(commands[0]?.command, commands[0]?.payload, { transient }) : model.run("batch", { commands }, { transient });
165
+ const gestureFor = (start, position, client, own) => {
166
+ let active = false;
167
+ let last;
168
+ const resizer = limitsOf(start.nodeId);
169
+ const limits = {
170
+ ...resizer,
171
+ direction: own ?? resizer.direction
172
+ };
173
+ const anchor = host.toFlow(client);
174
+ return {
175
+ autoPan: "resize",
176
+ move: (event) => {
177
+ const pointer = host.toFlow({
178
+ x: event.clientX,
179
+ y: event.clientY
180
+ });
181
+ const delta = {
182
+ x: pointer.x - anchor.x,
183
+ y: pointer.y - anchor.y
184
+ };
185
+ if (!active) {
186
+ if (delta.x === 0 && delta.y === 0) return;
187
+ active = true;
188
+ resizing = start.nodeId;
189
+ host.emit({
190
+ type: "resize-start",
191
+ nodeId: start.nodeId,
192
+ width: start.rect.width,
193
+ height: start.rect.height
194
+ });
195
+ }
196
+ const rect = kept(start, resizedRect(start.rect, position, delta, limits), position, limits);
197
+ last = {
198
+ commands: stepOf(start, rect),
199
+ rect
200
+ };
201
+ run(last.commands, true);
202
+ host.changed();
203
+ },
204
+ end: () => {
205
+ resizing = void 0;
206
+ if (!active || !last) return;
207
+ run(last.commands, false);
208
+ host.changed();
209
+ host.emit({
210
+ type: "resize",
211
+ nodeId: start.nodeId,
212
+ width: last.rect.width,
213
+ height: last.rect.height,
214
+ source: "pointer"
215
+ });
216
+ },
217
+ cancel: () => {
218
+ resizing = void 0;
219
+ if (!active) return;
220
+ run(stepOf(start, start.rect), false);
221
+ host.changed();
222
+ host.emit({
223
+ type: "resize-cancel",
224
+ nodeId: start.nodeId
225
+ });
226
+ },
227
+ active: () => active
228
+ };
229
+ };
230
+ /** Whether a resize may start from a handle at `position` now. */
231
+ const resizable = (position) => RESIZE_POSITIONS.includes(position) && host.interactive() && resizing === void 0;
232
+ /** What a resize of a measured node starts from: its position, its box, its children. */
233
+ const startOf = (nodeId) => {
234
+ const node = model.get("node-by", { id: nodeId });
235
+ const rect = host.nodeRect(nodeId);
236
+ if (!node || !rect || !host.measured(nodeId)) return void 0;
237
+ const children = model.get("all-nodes").filter((child) => child.parentId === nodeId);
238
+ return {
239
+ nodeId,
240
+ position: node.position,
241
+ rect,
242
+ children
243
+ };
244
+ };
245
+ keyboardLinks(host).resize = { step: (nodeId, position, delta, own) => {
246
+ if (!resizable(position)) return false;
247
+ const direction = own ?? limitsOf(nodeId).direction;
248
+ const horizontal = direction !== "vertical" && (position.includes("left") || position.includes("right"));
249
+ const vertical = direction !== "horizontal" && (position.startsWith("top") || position.startsWith("bottom"));
250
+ if (!(horizontal && delta.x !== 0) && !(vertical && delta.y !== 0)) return false;
251
+ const start = startOf(nodeId);
252
+ if (!start) return false;
253
+ const limits = {
254
+ ...limitsOf(nodeId),
255
+ direction,
256
+ snap: void 0
257
+ };
258
+ const rect = kept(start, resizedRect(start.rect, position, delta, limits), position, limits);
259
+ const from = start.rect;
260
+ if (rect.x === from.x && rect.y === from.y && rect.width === from.width && rect.height === from.height) return true;
261
+ if (!run(stepOf(start, rect), false).ok) return true;
262
+ host.changed();
263
+ host.emit({
264
+ type: "resize",
265
+ nodeId,
266
+ width: rect.width,
267
+ height: rect.height,
268
+ source: "keyboard"
269
+ });
270
+ return true;
271
+ } };
272
+ return {
273
+ is: {
274
+ resizing: () => resizing !== void 0,
275
+ "node-resizing": (payload) => resizing === payload.nodeId
276
+ },
277
+ pointerdown: (event, target) => {
278
+ const { nodeId, position } = target;
279
+ if (target.kind !== "resize-handle" || !resizable(position)) return;
280
+ const start = nodeId === void 0 ? void 0 : startOf(nodeId);
281
+ return start ? gestureFor(start, position, {
282
+ x: event.clientX,
283
+ y: event.clientY
284
+ }, target.direction) : void 0;
285
+ },
286
+ attributes: (part, payload, into) => {
287
+ if (part === "node" || part === "resizer") into["data-resizing"] = flag(resizing !== void 0 && resizing === payload.nodeId);
288
+ },
289
+ detached: () => {
290
+ resizing = void 0;
291
+ }
292
+ };
293
+ }
294
+ // src/resize/resize.ts
295
+ /** Resizing nodes: `features: [resize()]` (`@fragiola/flow/resize`). */
296
+ function resize(options = {}) {
297
+ const resolved = Object.freeze({
298
+ minWidth: options.minWidth ?? 10,
299
+ minHeight: options.minHeight ?? 10,
300
+ maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,
301
+ maxHeight: options.maxHeight ?? Number.POSITIVE_INFINITY,
302
+ keepAspectRatio: options.keepAspectRatio ?? false,
303
+ snap: options.snap ? Object.freeze({
304
+ x: options.snap.x,
305
+ y: options.snap.y
306
+ }) : void 0
307
+ });
308
+ return Object.freeze({
309
+ name: "resize",
310
+ options: resolved,
311
+ engine: (host) => resizeEngine(host, resolved)
312
+ });
313
+ }
314
+ export { resize };
315
+
316
+ //# sourceMappingURL=resize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resize.js","names":[],"sources":["../src/resize/resize-engine.ts","../src/resize/resize.ts"],"sourcesContent":["// The resize feature's engine side (the NodeResizer): a press on one of a node's eight resize\n// handles (`data-position`: `top-left`, `top`, `top-right`, `right`, `bottom-right`, `bottom`,\n// `bottom-left`, `left`) resizes it from that corner or side, the opposite one staying put. Its\n// limits are the feature's options, over which a resizer's own (`engine.adapter.setPartOptions(\n// \"resizer\", nodeId, …)`) win; with `keepAspectRatio` the width-to-height ratio stays. A node stays\n// inside its extent (its parent's box with `extent: \"parent\"`, its rectangle `extent`, the engine's\n// `nodeExtent`), and a parent never shrinks past its children with `extent: \"parent\"`; resizing\n// from a top or left edge moves the parent's top-left corner, so its children move back by as much\n// and stay where they are on screen (a parent holding a child that is not draggable keeps its top\n// and left edges). It works on the node's absolute box, so the `nodeOrigin` is applied once, when\n// the box becomes a position. Each step is transient, the release the final one. With\n// `keyboard()`, the arrows on a focused resize handle move its sides by the keyboard's step, the\n// same rules kept, each key one final step (offered through the host's keyboard links). A handle's\n// own direction (`data-direction`, #40) wins over its resizer's, for a press and for a key.\nimport { flag } from \"../attributes/part-attributes\";\nimport type { BatchEntry } from \"../commands/types\";\nimport type { FlowNode, XY } from \"../document/types\";\nimport { keyboardLinks } from \"../features/keyboard-links\";\nimport type {\n FeatureEngine,\n FeatureEngineHost,\n FeatureGesture,\n} from \"../features/types\";\nimport type { Rect } from \"../geometry/types\";\nimport type { ResizeDirection, ResizeOptions } from \"./resize\";\n\n/** What a resize is kept to: the feature's options, a resizer's own over them, and its direction. */\nexport interface ResizeLimits\n extends Omit<ResizeOptions, \"snap\">,\n Partial<Pick<ResizeOptions, \"snap\">> {\n readonly direction?: ResizeDirection | undefined;\n}\n\n/** The corners and sides a resize handle may be on. */\nexport const RESIZE_POSITIONS = [\n \"top-left\",\n \"top\",\n \"top-right\",\n \"right\",\n \"bottom-right\",\n \"bottom\",\n \"bottom-left\",\n \"left\",\n] as const;\n\n/** A resize handle's position. */\nexport type ResizePosition = (typeof RESIZE_POSITIONS)[number];\n\ninterface Start {\n readonly nodeId: string;\n /** its position at the start (a cancel puts it back) */\n readonly position: XY;\n /** its absolute box at the start */\n readonly rect: Rect;\n readonly children: readonly FlowNode[];\n}\n\nconst clamp = (value: number, min: number, max: number) =>\n Math.min(Math.max(value, min), Math.max(min, max));\n\nconst snapTo = (value: number, grid: number) =>\n grid > 0 ? Math.round(value / grid) * grid : value;\n\n/**\n * A node's box after a resize from `position` by `delta` (flow units), before the parent's rules:\n * with a `direction`, only its sides move; with `snap`, the moved sides land on the grid.\n */\nexport function resizedRect(\n start: Rect,\n position: ResizePosition,\n delta: XY,\n limits: ResizeLimits,\n): Rect {\n const { direction, snap } = limits;\n const left = direction !== \"vertical\" && position.includes(\"left\");\n const right = direction !== \"vertical\" && position.includes(\"right\");\n const top = direction !== \"horizontal\" && position.startsWith(\"top\");\n const bottom = direction !== \"horizontal\" && position.startsWith(\"bottom\");\n const moved = { ...delta };\n if (snap) {\n // the moved side's new place, snapped, gives the delta\n const edgeX = left ? start.x : start.x + start.width;\n const edgeY = top ? start.y : start.y + start.height;\n moved.x = snapTo(edgeX + delta.x, snap.x) - edgeX;\n moved.y = snapTo(edgeY + delta.y, snap.y) - edgeY;\n }\n let width = start.width + (right ? moved.x : left ? -moved.x : 0);\n let height = start.height + (bottom ? moved.y : top ? -moved.y : 0);\n if (\n limits.keepAspectRatio &&\n direction === undefined &&\n start.width > 0 &&\n start.height > 0\n ) {\n const horizontal = left || right;\n const vertical = top || bottom;\n // a corner follows the larger change; a side drives the other dimension\n let scale =\n horizontal && vertical\n ? Math.abs(width / start.width - 1) >=\n Math.abs(height / start.height - 1)\n ? width / start.width\n : height / start.height\n : horizontal\n ? width / start.width\n : height / start.height;\n scale = clamp(\n scale,\n Math.max(\n limits.minWidth / start.width,\n limits.minHeight / start.height,\n ),\n Math.min(\n limits.maxWidth / start.width,\n limits.maxHeight / start.height,\n ),\n );\n width = start.width * scale;\n height = start.height * scale;\n } else {\n width = clamp(width, limits.minWidth, limits.maxWidth);\n height = clamp(height, limits.minHeight, limits.maxHeight);\n }\n return {\n x: left ? start.x + start.width - width : start.x,\n y: top ? start.y + start.height - height : start.y,\n width,\n height,\n };\n}\n\n/** The engine side of `resize()`. */\nexport function resizeEngine(\n host: FeatureEngineHost,\n options: ResizeOptions,\n): FeatureEngine {\n const { model } = host;\n let resizing: string | undefined;\n\n const limitsOf = (nodeId: string): ResizeLimits => {\n const own = host.partOptions(\"resizer\", nodeId) ?? {};\n const pick = <K extends keyof ResizeOptions>(\n key: K,\n ): ResizeOptions[K] =>\n (typeof own[key] === typeof options[key]\n ? own[key]\n : options[key]) as ResizeOptions[K];\n return {\n minWidth: pick(\"minWidth\"),\n minHeight: pick(\"minHeight\"),\n maxWidth: pick(\"maxWidth\"),\n maxHeight: pick(\"maxHeight\"),\n keepAspectRatio: pick(\"keepAspectRatio\"),\n snap: options.snap,\n direction:\n own.direction === \"horizontal\" || own.direction === \"vertical\"\n ? own.direction\n : undefined,\n };\n };\n\n /** The box (absolute) kept to its extent and to its children's rules. */\n const kept = (\n start: Start,\n rect: Rect,\n position: ResizePosition,\n limits: ResizeLimits,\n ): Rect => {\n let { x, y, width, height } = rect;\n const left = position.includes(\"left\");\n const top = position.startsWith(\"top\");\n const from = start.rect;\n // the node stays inside its extent\n const bounds = host.extentOf(start.nodeId);\n if (bounds) {\n if (left && x < bounds.x) {\n width -= bounds.x - x;\n x = bounds.x;\n }\n if (top && y < bounds.y) {\n height -= bounds.y - y;\n y = bounds.y;\n }\n width = Math.min(width, bounds.x + bounds.width - x);\n height = Math.min(height, bounds.y + bounds.height - y);\n // a node already past its bounds gets no room there: it keeps its least size, never a\n // negative one\n if (width < 0) width = Math.min(limits.minWidth, rect.width);\n if (height < 0) height = Math.min(limits.minHeight, rect.height);\n // the bound wins over the limits; a kept ratio shrinks the other side to it\n if (\n limits.keepAspectRatio &&\n limits.direction === undefined &&\n from.width > 0 &&\n from.height > 0 &&\n (width !== rect.width || height !== rect.height)\n ) {\n const ratio = from.width / from.height;\n const fitted = Math.min(width, height * ratio);\n if (left) x += width - fitted;\n if (top) y += height - fitted / ratio;\n width = fitted;\n height = fitted / ratio;\n }\n }\n // a parent keeps its children with `extent: \"parent\"` inside, and its children's places\n const fixed = start.children.some(\n (child) => !model.is(\"node-draggable\", { nodeId: child.id }),\n );\n const right = from.x + from.width;\n const bottom = from.y + from.height;\n if (fixed) {\n if (left) {\n x = from.x;\n width = from.width;\n }\n if (top) {\n y = from.y;\n height = from.height;\n }\n }\n const held = start.children.filter(\n (child) => child.extent === \"parent\",\n );\n for (const child of held) {\n const box = host.nodeRect(child.id);\n if (!box) continue;\n // a child's position is relative to its parent's top-left corner\n const childLeft = from.x + child.position.x;\n const childTop = from.y + child.position.y;\n if (left && x > childLeft) {\n x = childLeft;\n width = right - x;\n }\n if (top && y > childTop) {\n y = childTop;\n height = bottom - y;\n }\n width = Math.max(width, childLeft + box.width - x);\n height = Math.max(height, childTop + box.height - y);\n }\n return { x, y, width, height };\n };\n\n const stepOf = (start: Start, rect: Rect): BatchEntry[] => {\n const dx = rect.x - start.rect.x;\n const dy = rect.y - start.rect.y;\n const back =\n dx === 0 &&\n dy === 0 &&\n rect.width === start.rect.width &&\n rect.height === start.rect.height;\n const commands: BatchEntry[] = [\n {\n command: \"node.resize\",\n payload: {\n nodeId: start.nodeId,\n width: rect.width,\n height: rect.height,\n position: back\n ? start.position\n : host.positionFor(\n start.nodeId,\n { x: rect.x, y: rect.y },\n rect,\n ),\n },\n },\n ];\n if (dx !== 0 || dy !== 0) {\n for (const child of start.children) {\n commands.push({\n command: \"node.move\",\n payload: {\n nodeId: child.id,\n position: {\n x: child.position.x - dx,\n y: child.position.y - dy,\n },\n },\n });\n }\n }\n return commands;\n };\n\n const run = (commands: BatchEntry[], transient: boolean) =>\n commands.length === 1\n ? model.run(\n commands[0]?.command as \"node.resize\",\n commands[0]?.payload as object,\n {\n transient,\n },\n )\n : model.run(\"batch\", { commands }, { transient });\n\n const gestureFor = (\n start: Start,\n position: ResizePosition,\n client: XY,\n own: ResizeDirection | undefined,\n ): FeatureGesture => {\n let active = false;\n let last: { commands: BatchEntry[]; rect: Rect } | undefined;\n // the handle's own direction (`data-direction`, read at the press) wins over its resizer's\n const resizer = limitsOf(start.nodeId);\n const limits = { ...resizer, direction: own ?? resizer.direction };\n // the flow point pressed: an auto-pan moves the flow under a still pointer\n const anchor = host.toFlow(client);\n return {\n autoPan: \"resize\",\n move: (event) => {\n const pointer = host.toFlow({\n x: event.clientX,\n y: event.clientY,\n });\n const delta = {\n x: pointer.x - anchor.x,\n y: pointer.y - anchor.y,\n };\n if (!active) {\n if (delta.x === 0 && delta.y === 0) return;\n active = true;\n resizing = start.nodeId;\n host.emit({\n type: \"resize-start\",\n nodeId: start.nodeId,\n width: start.rect.width,\n height: start.rect.height,\n });\n }\n const rect = kept(\n start,\n resizedRect(start.rect, position, delta, limits),\n position,\n limits,\n );\n last = { commands: stepOf(start, rect), rect };\n run(last.commands, true);\n host.changed();\n },\n end: () => {\n resizing = undefined;\n if (!active || !last) return;\n run(last.commands, false);\n host.changed();\n host.emit({\n type: \"resize\",\n nodeId: start.nodeId,\n width: last.rect.width,\n height: last.rect.height,\n source: \"pointer\",\n });\n },\n cancel: () => {\n resizing = undefined;\n if (!active) return;\n run(stepOf(start, start.rect), false);\n // the children go back to where they were\n host.changed();\n host.emit({ type: \"resize-cancel\", nodeId: start.nodeId });\n },\n active: () => active,\n };\n };\n\n /** Whether a resize may start from a handle at `position` now. */\n const resizable = (\n position: string | undefined,\n ): position is ResizePosition =>\n RESIZE_POSITIONS.includes(position as ResizePosition) &&\n host.interactive() &&\n resizing === undefined;\n\n /** What a resize of a measured node starts from: its position, its box, its children. */\n const startOf = (nodeId: string): Start | undefined => {\n const node = model.get(\"node-by\", { id: nodeId }) as\n | FlowNode\n | undefined;\n const rect = host.nodeRect(nodeId);\n if (!node || !rect || !host.measured(nodeId)) return undefined;\n const children = (model.get(\"all-nodes\") as readonly FlowNode[]).filter(\n (child) => child.parentId === nodeId,\n );\n return { nodeId, position: node.position, rect, children };\n };\n\n // with keyboard(): the arrows on a focused resize handle move its sides, one final step each\n keyboardLinks(host).resize = {\n step: (nodeId, position, delta, own) => {\n if (!resizable(position)) return false;\n // the arrow moves none of this handle's sides (its direction kept): not the\n // resizer's key\n const direction = own ?? limitsOf(nodeId).direction;\n const horizontal =\n direction !== \"vertical\" &&\n (position.includes(\"left\") || position.includes(\"right\"));\n const vertical =\n direction !== \"horizontal\" &&\n (position.startsWith(\"top\") || position.startsWith(\"bottom\"));\n if (\n !(horizontal && delta.x !== 0) &&\n !(vertical && delta.y !== 0)\n ) {\n return false;\n }\n const start = startOf(nodeId);\n if (!start) return false;\n // a key moves by the keyboard's step, never snapped back to where it was\n const limits = { ...limitsOf(nodeId), direction, snap: undefined };\n const rect = kept(\n start,\n resizedRect(start.rect, position, delta, limits),\n position,\n limits,\n );\n const from = start.rect;\n // at a limit the key is still the resizer's, and nothing runs\n if (\n rect.x === from.x &&\n rect.y === from.y &&\n rect.width === from.width &&\n rect.height === from.height\n ) {\n return true;\n }\n if (!run(stepOf(start, rect), false).ok) return true;\n host.changed();\n host.emit({\n type: \"resize\",\n nodeId,\n width: rect.width,\n height: rect.height,\n source: \"keyboard\",\n });\n return true;\n },\n };\n\n return {\n is: {\n resizing: () => resizing !== undefined,\n \"node-resizing\": (payload) =>\n resizing === (payload as { nodeId?: unknown }).nodeId,\n },\n pointerdown: (event, target) => {\n const { nodeId, position } = target;\n if (target.kind !== \"resize-handle\" || !resizable(position)) {\n return undefined;\n }\n const start = nodeId === undefined ? undefined : startOf(nodeId);\n return start\n ? gestureFor(\n start,\n position,\n {\n x: event.clientX,\n y: event.clientY,\n },\n target.direction,\n )\n : undefined;\n },\n attributes: (part, payload, into) => {\n if (part === \"node\" || part === \"resizer\") {\n into[\"data-resizing\"] = flag(\n resizing !== undefined && resizing === payload.nodeId,\n );\n }\n },\n detached: () => {\n resizing = undefined;\n },\n };\n}\n","// The resize feature (D2, `@fragiola/flow/resize`): the NodeResizer's gestures, with a minimum and\n// a maximum size and a kept aspect ratio. Its model side is the core's `node.resize` (with a\n// position, for a resize from a top or left edge), run as `transient` steps; its engine side\n// (`resize-engine.ts`: the eight handles, the constraints, resizing a parent with children) reads\n// `options`, over which a resizer's own win.\nimport type { FlowFeature } from \"../features/types\";\nimport { resizeEngine } from \"./resize-engine\";\n\n/** Options of {@link resize}: the defaults of every resizer, read by its engine side. */\nexport interface ResizeOptions {\n /** the smallest width; default 10 */\n readonly minWidth: number;\n /** the smallest height; default 10 */\n readonly minHeight: number;\n /** the largest width; default none */\n readonly maxWidth: number;\n /** the largest height; default none */\n readonly maxHeight: number;\n /** whether a resize keeps the node's width-to-height ratio; default false */\n readonly keepAspectRatio: boolean;\n /**\n * the grid (flow units) the moved sides snap to while resizing by pointer, so sizes and\n * positions land on it; none: no snap (the drag feature's `snapGrid` is its own)\n */\n readonly snap: { readonly x: number; readonly y: number } | undefined;\n}\n\n/**\n * The sides a resizer moves (its own option, `engine.adapter.setPartOptions(\"resizer\", nodeId,\n * { direction })`): `horizontal` only widths and x, `vertical` only heights and y; none: both.\n */\nexport type ResizeDirection = \"horizontal\" | \"vertical\";\n\n/** The resize feature, as `resize()` makes it. */\nexport type ResizeFeature = FlowFeature<\"resize\", ResizeOptions>;\n\n/** Resizing nodes: `features: [resize()]` (`@fragiola/flow/resize`). */\nexport function resize(options: Partial<ResizeOptions> = {}): ResizeFeature {\n const resolved: ResizeOptions = Object.freeze({\n minWidth: options.minWidth ?? 10,\n minHeight: options.minHeight ?? 10,\n maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,\n maxHeight: options.maxHeight ?? Number.POSITIVE_INFINITY,\n keepAspectRatio: options.keepAspectRatio ?? false,\n snap: options.snap\n ? Object.freeze({ x: options.snap.x, y: options.snap.y })\n : undefined,\n });\n return Object.freeze({\n name: \"resize\",\n options: resolved,\n engine: (host) => resizeEngine(host, resolved),\n } satisfies ResizeFeature);\n}\n"],"mappings":";;;;AAkCA,MAAa,mBAAmB;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACJ;AAcA,MAAM,SAAS,OAAe,KAAa,QACvC,KAAK,IAAI,KAAK,IAAI,OAAO,GAAG,GAAG,KAAK,IAAI,KAAK,GAAG,CAAC;AAErD,MAAM,UAAU,OAAe,SAC3B,OAAO,IAAI,KAAK,MAAM,QAAQ,IAAI,IAAI,OAAO;;;;;AAM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AAQ,aAAa;GAC7B,IAAI,OAAO,SAAS,mBAAmB,CAAC,UAAU,QAAQ,GACtD;GAEJ,MAAM,QAAQ,WAAW,KAAA,IAAY,KAAA,IAAY,QAAQ,MAAM;GAC/D,OAAO,QACD,WACI,OACA,UACA;IACI,GAAG,MAAM;IACT,GAAG,MAAM;GACb,GACA,OAAO,SACX,IACA,KAAA;EACV;EACA,aAAa,MAAM,SAAS,SAAS;GACjC,IAAI,SAAS,UAAU,SAAS,WAC5B,KAAK,mBAAmB,KACpB,aAAa,KAAA,KAAa,aAAa,QAAQ,MACnD;EAER;EACA,gBAAgB;GACZ,WAAW,KAAA;EACf;CACJ;AACJ;;;ACtbA,SAAgB,OAAO,UAAkC,CAAC,GAAkB;CACxE,MAAM,WAA0B,OAAO,OAAO;EAC1C,UAAU,QAAQ,YAAY;EAC9B,WAAW,QAAQ,aAAa;EAChC,UAAU,QAAQ,YAAY,OAAO;EACrC,WAAW,QAAQ,aAAa,OAAO;EACvC,iBAAiB,QAAQ,mBAAmB;EAC5C,MAAM,QAAQ,OACR,OAAO,OAAO;GAAE,GAAG,QAAQ,KAAK;GAAG,GAAG,QAAQ,KAAK;EAAE,CAAC,IACtD,KAAA;CACV,CAAC;CACD,OAAO,OAAO,OAAO;EACjB,MAAM;EACN,SAAS;EACT,SAAS,SAAS,aAAa,MAAM,QAAQ;CACjD,CAAyB;AAC7B"}
@@ -0,0 +1,102 @@
1
+ // src/commands/define.ts
2
+ /** A successful result. */
3
+ function ok(value) {
4
+ return {
5
+ ok: true,
6
+ value
7
+ };
8
+ }
9
+ /** A failed result. */
10
+ function fail(code, message, path, issues) {
11
+ const error = {
12
+ code,
13
+ message
14
+ };
15
+ if (path !== void 0) error.path = path;
16
+ if (issues !== void 0) error.issues = issues;
17
+ return {
18
+ ok: false,
19
+ error
20
+ };
21
+ }
22
+ /** The `invalid_payload` failure of schema issues (the first one's message and path), if any. */
23
+ function invalid(issues, prefix = "") {
24
+ const [first] = issues;
25
+ return first && fail("invalid_payload", `${prefix}${first.message}`, first.path, issues);
26
+ }
27
+ /**
28
+ * Declares a command: its payload is typed by its name, and its schemas keep their literal types
29
+ * (the type tests read them with `FromSchema`).
30
+ */
31
+ function defineCommand(definition) {
32
+ return definition;
33
+ }
34
+ // src/document/flags.ts
35
+ /** A node's flag in effect: its own value, else the default (`hidden` false, the others true). */
36
+ function nodeFlag(node, flag) {
37
+ return node[flag] ?? flag !== "hidden";
38
+ }
39
+ /**
40
+ * An edge's flag in effect: its own value, else the default (`hidden` and `animated` false, the
41
+ * others true); `reconnectable` is true unless it is false (one end is still reconnectable).
42
+ */
43
+ function edgeFlag(edge, flag) {
44
+ if (flag === "reconnectable") return edge.reconnectable !== false;
45
+ return edge[flag] ?? (flag !== "hidden" && flag !== "animated");
46
+ }
47
+ /** Whether an edge's end may be moved (`reconnectable`: true, or that end's name). */
48
+ function reconnectableEnd(edge, end) {
49
+ const value = edge.reconnectable ?? true;
50
+ return value === true || value === end;
51
+ }
52
+ // src/commands/rules.ts
53
+ /** Whether a reducer's lookup failed. */
54
+ function isFailure(value) {
55
+ return value.ok === false;
56
+ }
57
+ /** The node an id names, or `not_found` at `path`. */
58
+ function findNode(draft, id, path) {
59
+ return draft.node(id) ?? fail("not_found", `no node "${id}"`, path);
60
+ }
61
+ /** The edge an id names, or `not_found` at `path`. */
62
+ function findEdge(draft, id, path) {
63
+ return draft.edge(id) ?? fail("not_found", `no edge "${id}"`, path);
64
+ }
65
+ /** Whether two edges join the same handles of the same nodes, in the same direction. */
66
+ function sameEnds(edge, connection) {
67
+ return edge.source === connection.source && edge.target === connection.target && edge.sourceHandle === connection.sourceHandle && edge.targetHandle === connection.targetHandle;
68
+ }
69
+ /**
70
+ * Why an edge may not join these ends, or `undefined`: a missing node (`not_found`), a node that is
71
+ * not connectable, or an edge already there between the same handles (`refused`). `ignoreEdgeId`
72
+ * is the edge being reconnected.
73
+ */
74
+ function connectionProblem(draft, connection, ignoreEdgeId) {
75
+ for (const end of ["source", "target"]) {
76
+ const node = findNode(draft, connection[end], `/${end}`);
77
+ if (isFailure(node)) return node;
78
+ if (!nodeFlag(node, "connectable")) return fail("refused", `node "${node.id}" is not connectable`, `/${end}`);
79
+ }
80
+ const twin = draft.edges().find((edge) => edge.id !== ignoreEdgeId && sameEnds(edge, connection));
81
+ return twin ? fail("refused", `edge "${twin.id}" already joins these handles`, "/source") : void 0;
82
+ }
83
+ /** The nodes that go when `nodeId` goes: itself and, through the features, what goes with it. */
84
+ function removalOf(draft, features, nodeId) {
85
+ const ids = /* @__PURE__ */ new Set([nodeId]);
86
+ features.removing(draft, ids);
87
+ return ids;
88
+ }
89
+ /** Removes nodes, their edges and the edges given, in one step: what it removed, in drawing order. */
90
+ function removeAll(draft, nodeIds, edgeIds = /* @__PURE__ */ new Set()) {
91
+ const removedNodeIds = draft.nodes().filter((node) => nodeIds.has(node.id)).map((node) => node.id);
92
+ const edges = draft.edges().filter((edge) => edgeIds.has(edge.id) || nodeIds.has(edge.source) || nodeIds.has(edge.target)).map((edge) => edge.id);
93
+ draft.removeNodes(nodeIds);
94
+ draft.removeEdges(new Set(edges));
95
+ return {
96
+ removedNodeIds,
97
+ removedEdgeIds: edges
98
+ };
99
+ }
100
+ export { removalOf as a, nodeFlag as c, fail as d, invalid as f, isFailure as i, reconnectableEnd as l, findEdge as n, removeAll as o, ok as p, findNode as r, edgeFlag as s, connectionProblem as t, defineCommand as u };
101
+
102
+ //# sourceMappingURL=rules-DLOMrPBG.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rules-DLOMrPBG.js","names":[],"sources":["../src/commands/define.ts","../src/document/flags.ts","../src/commands/rules.ts"],"sourcesContent":["// How a command is declared (Dockable's catalogue, fragiola/dockable,\n// packages/core/src/commands/define.ts, same author and licence): its description for an assistant,\n// its payload and result JSON Schemas, whether it may be a transient step, and its reducer, which\n// applies it to a draft or says why it cannot. Reducers never throw on bad input.\n\nimport type { AnyTypes } from \"../document/types\";\nimport type { ComposedFeatures } from \"../features/registry\";\nimport type { JsonSchema, ValidationIssue } from \"../schema/types\";\nimport type { Draft } from \"../state/draft\";\nimport type {\n CommandError,\n CommandErrorCode,\n CommandName,\n CommandResult,\n PayloadOf,\n ResultOf,\n} from \"./types\";\n\n/** What a reducer can use besides the draft. */\nexport interface ReduceContext {\n readonly draft: Draft;\n /** the model's installed features, composed: the contribution points a reducer calls */\n readonly features: ComposedFeatures;\n /** runs one command of a batch on the same draft, through the middleware chain */\n runInBatch(\n command: string,\n payload: unknown,\n path: string,\n ): CommandResult<unknown>;\n}\n\n/** A command: its description, schemas and reducer. */\nexport interface CommandDefinition<C extends CommandName = CommandName> {\n readonly name: C;\n readonly description: string;\n readonly payloadSchema: JsonSchema;\n readonly resultSchema: JsonSchema;\n /** whether it may run as a transient step of a continuous gesture */\n readonly transient: boolean;\n /**\n * applies the command to the draft, or returns why it cannot (a method: a list of commands\n * holds each one's own payload type)\n */\n reduce(\n payload: PayloadOf<AnyTypes, C>,\n context: ReduceContext,\n ): CommandResult<ResultOf<AnyTypes, C>>;\n}\n\n/** A successful result. */\nexport function ok<R>(value: R): { readonly ok: true; readonly value: R } {\n return { ok: true, value };\n}\n\n/** Why a command did not apply. */\nexport type Failure = { readonly ok: false; readonly error: CommandError };\n\n/** A failed result. */\nexport function fail(\n code: CommandErrorCode,\n message: string,\n path?: string,\n issues?: readonly ValidationIssue[],\n): Failure {\n const error: { -readonly [K in keyof CommandError]: CommandError[K] } = {\n code,\n message,\n };\n if (path !== undefined) {\n error.path = path;\n }\n if (issues !== undefined) {\n error.issues = issues;\n }\n return { ok: false, error };\n}\n\n/** The `invalid_payload` failure of schema issues (the first one's message and path), if any. */\nexport function invalid(\n issues: readonly ValidationIssue[],\n prefix = \"\",\n): Failure | undefined {\n const [first] = issues;\n return (\n first &&\n fail(\"invalid_payload\", `${prefix}${first.message}`, first.path, issues)\n );\n}\n\n/**\n * Declares a command: its payload is typed by its name, and its schemas keep their literal types\n * (the type tests read them with `FromSchema`).\n */\nexport function defineCommand<\n C extends CommandName,\n const P extends JsonSchema,\n const R extends JsonSchema,\n>(\n definition: CommandDefinition<C> & {\n readonly payloadSchema: P;\n readonly resultSchema: R;\n },\n): Omit<CommandDefinition<C>, \"payloadSchema\" | \"resultSchema\"> & {\n readonly payloadSchema: P;\n readonly resultSchema: R;\n} {\n return definition;\n}\n","import type { EdgeFlags, NodeFlags } from \"./types\";\n\n/** A node's flag in effect: its own value, else the default (`hidden` false, the others true). */\nexport function nodeFlag(node: NodeFlags, flag: keyof NodeFlags): boolean {\n return node[flag] ?? flag !== \"hidden\";\n}\n\n/**\n * An edge's flag in effect: its own value, else the default (`hidden` and `animated` false, the\n * others true); `reconnectable` is true unless it is false (one end is still reconnectable).\n */\nexport function edgeFlag(edge: EdgeFlags, flag: keyof EdgeFlags): boolean {\n if (flag === \"reconnectable\") return edge.reconnectable !== false;\n return edge[flag] ?? (flag !== \"hidden\" && flag !== \"animated\");\n}\n\n/** Whether an edge's end may be moved (`reconnectable`: true, or that end's name). */\nexport function reconnectableEnd(\n edge: EdgeFlags,\n end: \"source\" | \"target\",\n): boolean {\n const value = edge.reconnectable ?? true;\n return value === true || value === end;\n}\n","// The rules several commands share: finding what a payload names (`not_found` at its path), the\n// rules of a connection (`edge.add`, `edge.reconnect`: both nodes exist and are connectable, no\n// second edge between the same handles), and a removal (a node takes its edges with it, and with\n// sub-flows its children). Written from the behaviour the flow needs, nothing ported.\nimport { nodeFlag } from \"../document/flags\";\nimport type { FlowEdge, FlowNode } from \"../document/types\";\nimport type { ComposedFeatures } from \"../features/registry\";\nimport type { Draft } from \"../state/draft\";\nimport { type Failure, fail } from \"./define\";\nimport type { ConnectionPayload, RemovedResult } from \"./types\";\n\n/** Whether a reducer's lookup failed. */\nexport function isFailure(value: object): value is Failure {\n return (value as { ok?: unknown }).ok === false;\n}\n\n/** The node an id names, or `not_found` at `path`. */\nexport function findNode(\n draft: Draft,\n id: string,\n path: string,\n): FlowNode | Failure {\n return draft.node(id) ?? fail(\"not_found\", `no node \"${id}\"`, path);\n}\n\n/** The edge an id names, or `not_found` at `path`. */\nexport function findEdge(\n draft: Draft,\n id: string,\n path: string,\n): FlowEdge | Failure {\n return draft.edge(id) ?? fail(\"not_found\", `no edge \"${id}\"`, path);\n}\n\n/** Whether two edges join the same handles of the same nodes, in the same direction. */\nfunction sameEnds(edge: FlowEdge, connection: ConnectionPayload): boolean {\n return (\n edge.source === connection.source &&\n edge.target === connection.target &&\n edge.sourceHandle === connection.sourceHandle &&\n edge.targetHandle === connection.targetHandle\n );\n}\n\n/**\n * Why an edge may not join these ends, or `undefined`: a missing node (`not_found`), a node that is\n * not connectable, or an edge already there between the same handles (`refused`). `ignoreEdgeId`\n * is the edge being reconnected.\n */\nexport function connectionProblem(\n draft: Draft,\n connection: ConnectionPayload,\n ignoreEdgeId?: string,\n): Failure | undefined {\n for (const end of [\"source\", \"target\"] as const) {\n const node = findNode(draft, connection[end], `/${end}`);\n if (isFailure(node)) {\n return node;\n }\n if (!nodeFlag(node, \"connectable\")) {\n return fail(\n \"refused\",\n `node \"${node.id}\" is not connectable`,\n `/${end}`,\n );\n }\n }\n const twin = draft\n .edges()\n .find((edge) => edge.id !== ignoreEdgeId && sameEnds(edge, connection));\n return twin\n ? fail(\n \"refused\",\n `edge \"${twin.id}\" already joins these handles`,\n \"/source\",\n )\n : undefined;\n}\n\n/** The nodes that go when `nodeId` goes: itself and, through the features, what goes with it. */\nexport function removalOf(\n draft: Draft,\n features: ComposedFeatures,\n nodeId: string,\n): Set<string> {\n const ids = new Set([nodeId]);\n features.removing(draft, ids);\n return ids;\n}\n\n/** Removes nodes, their edges and the edges given, in one step: what it removed, in drawing order. */\nexport function removeAll(\n draft: Draft,\n nodeIds: ReadonlySet<string>,\n edgeIds: ReadonlySet<string> = new Set(),\n): RemovedResult {\n const removedNodeIds = draft\n .nodes()\n .filter((node) => nodeIds.has(node.id))\n .map((node) => node.id);\n const edges = draft\n .edges()\n .filter(\n (edge) =>\n edgeIds.has(edge.id) ||\n nodeIds.has(edge.source) ||\n nodeIds.has(edge.target),\n )\n .map((edge) => edge.id);\n draft.removeNodes(nodeIds);\n draft.removeEdges(new Set(edges));\n return { removedNodeIds, removedEdgeIds: edges };\n}\n"],"mappings":";;AAkDA,SAAgB,GAAM,OAAoD;CACtE,OAAO;EAAE,IAAI;EAAM;CAAM;AAC7B;;AAMA,SAAgB,KACZ,MACA,SACA,MACA,QACO;CACP,MAAM,QAAkE;EACpE;EACA;CACJ;CACA,IAAI,SAAS,KAAA,GACT,MAAM,OAAO;CAEjB,IAAI,WAAW,KAAA,GACX,MAAM,SAAS;CAEnB,OAAO;EAAE,IAAI;EAAO;CAAM;AAC9B;;AAGA,SAAgB,QACZ,QACA,SAAS,IACU;CACnB,MAAM,CAAC,SAAS;CAChB,OACI,SACA,KAAK,mBAAmB,GAAG,SAAS,MAAM,WAAW,MAAM,MAAM,MAAM;AAE/E;;;;;AAMA,SAAgB,cAKZ,YAOF;CACE,OAAO;AACX;;;ACxGA,SAAgB,SAAS,MAAiB,MAAgC;CACtE,OAAO,KAAK,SAAS,SAAS;AAClC;;;;;AAMA,SAAgB,SAAS,MAAiB,MAAgC;CACtE,IAAI,SAAS,iBAAiB,OAAO,KAAK,kBAAkB;CAC5D,OAAO,KAAK,UAAU,SAAS,YAAY,SAAS;AACxD;;AAGA,SAAgB,iBACZ,MACA,KACO;CACP,MAAM,QAAQ,KAAK,iBAAiB;CACpC,OAAO,UAAU,QAAQ,UAAU;AACvC;;;ACXA,SAAgB,UAAU,OAAiC;CACvD,OAAQ,MAA2B,OAAO;AAC9C;;AAGA,SAAgB,SACZ,OACA,IACA,MACkB;CAClB,OAAO,MAAM,KAAK,EAAE,KAAK,KAAK,aAAa,YAAY,GAAG,IAAI,IAAI;AACtE;;AAGA,SAAgB,SACZ,OACA,IACA,MACkB;CAClB,OAAO,MAAM,KAAK,EAAE,KAAK,KAAK,aAAa,YAAY,GAAG,IAAI,IAAI;AACtE;;AAGA,SAAS,SAAS,MAAgB,YA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