@dou-ui/flow 0.1.0-alpha.2 → 0.1.0-alpha.4
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/dist/comments.js +177 -0
- package/dist/comments.js.map +1 -0
- package/dist/components/default-node2.js.map +1 -1
- package/dist/components/flow-comments.js +6 -0
- package/dist/components/flow-comments.js.map +1 -0
- package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js +184 -0
- package/dist/components/flow-comments.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/components/port.js +7 -0
- package/dist/components/port.js.map +1 -0
- package/dist/context.js.map +1 -1
- package/dist/core/edge-types.js +107 -3
- package/dist/core/edge-types.js.map +1 -1
- package/dist/core/history.js +26 -0
- package/dist/core/history.js.map +1 -1
- package/dist/core/model.js.map +1 -1
- package/dist/core/node-types.js +8 -1
- package/dist/core/node-types.js.map +1 -1
- package/dist/core/transaction.js +112 -15
- package/dist/core/transaction.js.map +1 -1
- package/dist/document.js +1 -1
- package/dist/export/render.js +78 -0
- package/dist/export/render.js.map +1 -0
- package/dist/flow-editor.js.map +1 -1
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js +276 -23
- package/dist/flow-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/kit.js +1 -1
- package/dist/locale.js +15 -9
- package/dist/locale.js.map +1 -1
- package/dist/style.css +1 -1
- package/dist/style.js +1 -1
- package/dist/types/comments.d.ts +55 -0
- package/dist/types/components/default-node.d.ts +1 -1
- package/dist/types/components/flow-comments.vue.d.ts +28 -0
- package/dist/types/components/port.d.ts +7 -0
- package/dist/types/context.d.ts +37 -1
- package/dist/types/core/history.d.ts +10 -0
- package/dist/types/core/menu.d.ts +5 -0
- package/dist/types/core/model.d.ts +13 -0
- package/dist/types/core/node-types.d.ts +2 -0
- package/dist/types/core/transaction.d.ts +23 -1
- package/dist/types/export/render.d.ts +27 -0
- package/dist/types/flow-editor.vue.d.ts +11 -0
- package/dist/types/index.d.ts +35 -8
- package/dist/types/locale.d.ts +7 -3
- package/dist/whiteboard/board-node.js.map +1 -1
- package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js +3 -3
- package/dist/whiteboard/board-node.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/whiteboard/connector.js +1 -1
- package/dist/whiteboard/kit.js +1 -1
- package/package.json +3 -3
package/dist/core/edge-types.js
CHANGED
|
@@ -54,10 +54,66 @@ function bezierPath(from, fromSide, to, toSide, min = 40) {
|
|
|
54
54
|
}, min);
|
|
55
55
|
return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`;
|
|
56
56
|
}
|
|
57
|
+
/** The point halfway along a run of points: a middle one, or between the two middle ones. */
|
|
58
|
+
function middle(points) {
|
|
59
|
+
const a = points[Math.floor((points.length - 1) / 2)];
|
|
60
|
+
const b = points[Math.ceil((points.length - 1) / 2)];
|
|
61
|
+
return {
|
|
62
|
+
x: (a.x + b.x) / 2,
|
|
63
|
+
y: (a.y + b.y) / 2
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The side a run between two points leaves the first by and the side it
|
|
68
|
+
* enters the second by, along the axis the flow runs in: the one the
|
|
69
|
+
* source's port faces.
|
|
70
|
+
*/
|
|
71
|
+
function sides(a, b, vertical) {
|
|
72
|
+
if (vertical) return b.y >= a.y ? ["bottom", "top"] : ["top", "bottom"];
|
|
73
|
+
return b.x >= a.x ? ["right", "left"] : ["left", "right"];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A curve through the points an edge runs through, as tidying lays them:
|
|
77
|
+
* straight along a rank, curving across the gaps between ranks.
|
|
78
|
+
*/
|
|
79
|
+
function bezierThrough(source, target, points) {
|
|
80
|
+
const vertical = isVertical(source.side);
|
|
81
|
+
const all = [
|
|
82
|
+
source,
|
|
83
|
+
...points.map((p) => ({
|
|
84
|
+
...p,
|
|
85
|
+
side: source.side
|
|
86
|
+
})),
|
|
87
|
+
target
|
|
88
|
+
];
|
|
89
|
+
let path = `M${fmt(source)}`;
|
|
90
|
+
for (let i = 1; i < all.length; i++) {
|
|
91
|
+
const a = all[i - 1];
|
|
92
|
+
const b = all[i];
|
|
93
|
+
if ((vertical ? Math.abs(a.x - b.x) < .5 : Math.abs(a.y - b.y) < .5) && i > 1 && i < all.length - 1) {
|
|
94
|
+
path += `L${fmt(b)}`;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const [out, into] = sides(a, b, vertical);
|
|
98
|
+
const [c1, c2] = bezierControls({
|
|
99
|
+
...a,
|
|
100
|
+
side: i === 1 ? source.side : out
|
|
101
|
+
}, {
|
|
102
|
+
...b,
|
|
103
|
+
side: i === all.length - 1 ? target.side : into
|
|
104
|
+
}, 0);
|
|
105
|
+
path += `C${fmt(c1)} ${fmt(c2)} ${fmt(b)}`;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
path,
|
|
109
|
+
label: middle(points)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
57
112
|
/** The default: a smooth curve out of and into the ports' sides. */
|
|
58
113
|
var bezierEdge = defineEdgeType({
|
|
59
114
|
type: "bezier",
|
|
60
|
-
route(source, target) {
|
|
115
|
+
route(source, target, edge) {
|
|
116
|
+
if (edge?.points?.length) return bezierThrough(source, target, edge.points);
|
|
61
117
|
const [c1, c2] = bezierControls(source, target);
|
|
62
118
|
return {
|
|
63
119
|
path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,
|
|
@@ -70,13 +126,20 @@ var bezierEdge = defineEdgeType({
|
|
|
70
126
|
});
|
|
71
127
|
var straightEdge = defineEdgeType({
|
|
72
128
|
type: "straight",
|
|
73
|
-
route: (source, target) =>
|
|
129
|
+
route: (source, target, edge) => edge?.points?.length ? {
|
|
130
|
+
path: `M${[
|
|
131
|
+
source,
|
|
132
|
+
...edge.points,
|
|
133
|
+
target
|
|
134
|
+
].map(fmt).join("L")}`,
|
|
135
|
+
label: middle(edge.points)
|
|
136
|
+
} : {
|
|
74
137
|
path: `M${fmt(source)}L${fmt(target)}`,
|
|
75
138
|
label: {
|
|
76
139
|
x: (source.x + target.x) / 2,
|
|
77
140
|
y: (source.y + target.y) / 2
|
|
78
141
|
}
|
|
79
|
-
}
|
|
142
|
+
}
|
|
80
143
|
});
|
|
81
144
|
/** How far a step edge runs out of a port before its first turn. */
|
|
82
145
|
var STUB = 20;
|
|
@@ -130,6 +193,7 @@ var flipSide = {
|
|
|
130
193
|
var stepEdge = defineEdgeType({
|
|
131
194
|
type: "step",
|
|
132
195
|
route(source, target, edge) {
|
|
196
|
+
if (edge?.points?.length) return stepThrough(source, target, edge.points);
|
|
133
197
|
const turned = isVertical(source.side) && isVertical(target.side);
|
|
134
198
|
const s = turned ? {
|
|
135
199
|
...flip(source),
|
|
@@ -235,6 +299,46 @@ var stepEdge = defineEdgeType({
|
|
|
235
299
|
};
|
|
236
300
|
}
|
|
237
301
|
});
|
|
302
|
+
/**
|
|
303
|
+
* Right angles through the points an edge runs through: along each rank,
|
|
304
|
+
* across each gap between ranks halfway through it.
|
|
305
|
+
*/
|
|
306
|
+
function stepThrough(source, target, points) {
|
|
307
|
+
const vertical = isVertical(source.side);
|
|
308
|
+
const all = [
|
|
309
|
+
source,
|
|
310
|
+
...points,
|
|
311
|
+
target
|
|
312
|
+
];
|
|
313
|
+
const out = [source];
|
|
314
|
+
for (let i = 1; i < all.length; i++) {
|
|
315
|
+
const a = all[i - 1];
|
|
316
|
+
const b = all[i];
|
|
317
|
+
if (vertical) {
|
|
318
|
+
const y = (a.y + b.y) / 2;
|
|
319
|
+
out.push({
|
|
320
|
+
x: a.x,
|
|
321
|
+
y
|
|
322
|
+
}, {
|
|
323
|
+
x: b.x,
|
|
324
|
+
y
|
|
325
|
+
}, b);
|
|
326
|
+
} else {
|
|
327
|
+
const x = (a.x + b.x) / 2;
|
|
328
|
+
out.push({
|
|
329
|
+
x,
|
|
330
|
+
y: a.y
|
|
331
|
+
}, {
|
|
332
|
+
x,
|
|
333
|
+
y: b.y
|
|
334
|
+
}, b);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
path: rounded(out),
|
|
339
|
+
label: middle(points)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
238
342
|
export { bezierEdge, bezierPath, defineEdgeType, stepEdge, straightEdge };
|
|
239
343
|
|
|
240
344
|
//# sourceMappingURL=edge-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"edge-types.js","names":[],"sources":["../../src/core/edge-types.ts"],"sourcesContent":["import type { CSSProperties } from 'vue'\nimport type { FlowData, FlowEdge, Point, PortSide } from './model'\n\n/** Where an edge leaves or enters a node: a point and the side it faces. */\nexport interface EdgeAnchor extends Point {\n side: PortSide\n}\n\nexport interface EdgeRoute {\n /** The SVG path, in canvas coordinates. */\n path: string\n /** Where the label sits. */\n label: Point\n /**\n * A handle shown on the selected edge that moves part of it, e.g. the\n * middle of a step edge. `axis` is the way it moves; `move` turns the\n * pointer's canvas point into a patch for `edge.data`.\n */\n handle?: { at: Point; axis: 'x' | 'y'; move: (point: Point) => FlowData }\n /**\n * Marks drawn with the edge's stroke but never dashed, e.g. arrowheads:\n * an SVG path in canvas coordinates.\n */\n marks?: string\n}\n\nexport interface EdgeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /** The edge's path between two anchors. `edge` is absent while one is being drawn. */\n route: (source: EdgeAnchor, target: EdgeAnchor, edge?: FlowEdge<Data>) => EdgeRoute\n /** The edge's own look, e.g. `strokeDasharray`, over what its port type gives it. */\n style?: (edge: FlowEdge<Data>) => CSSProperties | undefined\n}\n\n/** Declares an edge type, with `edge.data` typed. */\nexport function defineEdgeType<Data extends FlowData = FlowData>(\n spec: EdgeTypeSpec<Data>,\n): EdgeTypeSpec<Data> {\n return spec\n}\n\nconst direction: Record<PortSide, Point> = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n}\n\nconst round = (value: number) => Math.round(value * 100) / 100\nconst fmt = (p: Point) => `${round(p.x)} ${round(p.y)}`\nconst isVertical = (side: PortSide) => side === 'top' || side === 'bottom'\n\n/**\n * A cubic curve leaving `from` and entering `to` straight out of their\n * sides. The handles are equally long, half the distance along the axis\n * they point in (at least `min`), so a fan of edges from one port is\n * mirror-symmetric and a straight run stays a straight line.\n */\nfunction bezierControls(from: EdgeAnchor, to: EdgeAnchor, min = 40) {\n const a = direction[from.side]\n const b = direction[to.side]\n const span = (dir: Point) => Math.abs(dir.x ? to.x - from.x : to.y - from.y)\n const lenA = Math.max(min, span(a) / 2)\n const lenB = Math.max(min, span(b) / 2)\n return [\n { x: from.x + a.x * lenA, y: from.y + a.y * lenA },\n { x: to.x + b.x * lenB, y: to.y + b.y * lenB },\n ] as const\n}\n\nexport function bezierPath(from: Point, fromSide: PortSide, to: Point, toSide: PortSide, min = 40) {\n const [c1, c2] = bezierControls({ ...from, side: fromSide }, { ...to, side: toSide }, min)\n return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`\n}\n\n/** The default: a smooth curve out of and into the ports' sides. */\nexport const bezierEdge = defineEdgeType({\n type: 'bezier',\n route(source, target) {\n const [c1, c2] = bezierControls(source, target)\n return {\n path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,\n // The curve at its middle.\n label: {\n x: (source.x + 3 * c1.x + 3 * c2.x + target.x) / 8,\n y: (source.y + 3 * c1.y + 3 * c2.y + target.y) / 8,\n },\n }\n },\n})\n\nexport const straightEdge = defineEdgeType({\n type: 'straight',\n route: (source, target) => ({\n path: `M${fmt(source)}L${fmt(target)}`,\n label: { x: (source.x + target.x) / 2, y: (source.y + target.y) / 2 },\n }),\n})\n\n/** How far a step edge runs out of a port before its first turn. */\nconst STUB = 20\nconst RADIUS = 6\n\n/** A polyline with its corners rounded. */\nfunction rounded(points: Point[], radius = RADIUS) {\n // Drop repeated points and points in the middle of a straight run.\n const pts: Point[] = []\n for (const p of points) {\n const last = pts[pts.length - 1]\n if (last && Math.abs(last.x - p.x) < 0.01 && Math.abs(last.y - p.y) < 0.01) continue\n const before = pts[pts.length - 2]\n if (\n before &&\n last &&\n ((Math.abs(before.x - last.x) < 0.01 && Math.abs(last.x - p.x) < 0.01) ||\n (Math.abs(before.y - last.y) < 0.01 && Math.abs(last.y - p.y) < 0.01))\n )\n pts.pop()\n pts.push(p)\n }\n let d = `M${fmt(pts[0]!)}`\n for (let i = 1; i < pts.length - 1; i++) {\n const [a, b, c] = [pts[i - 1]!, pts[i]!, pts[i + 1]!]\n const lenIn = Math.hypot(b.x - a.x, b.y - a.y)\n const lenOut = Math.hypot(c.x - b.x, c.y - b.y)\n const r = Math.min(radius, lenIn / 2, lenOut / 2)\n const p1 = { x: b.x - ((b.x - a.x) / lenIn) * r, y: b.y - ((b.y - a.y) / lenIn) * r }\n const p2 = { x: b.x + ((c.x - b.x) / lenOut) * r, y: b.y + ((c.y - b.y) / lenOut) * r }\n d += `L${fmt(p1)}Q${fmt(b)} ${fmt(p2)}`\n }\n return d + `L${fmt(pts[pts.length - 1]!)}`\n}\n\nconst flip = (p: Point): Point => ({ x: p.y, y: p.x })\nconst flipSide: Record<PortSide, PortSide> = {\n left: 'top',\n right: 'bottom',\n top: 'left',\n bottom: 'right',\n}\n\ninterface StepData extends FlowData {\n /** Where the middle segment sits between the two ends, from 0 to 1. */\n bend?: number\n}\n\n/**\n * Right angles with rounded corners. Between ports that face each other the\n * middle segment can be dragged; its place is kept in `edge.data.bend`.\n */\nexport const stepEdge = defineEdgeType<StepData>({\n type: 'step',\n route(source, target, edge) {\n // Work as if the ports faced left and right, then turn the result back.\n const turned = isVertical(source.side) && isVertical(target.side)\n const s = turned ? { ...flip(source), side: flipSide[source.side] } : source\n const t = turned ? { ...flip(target), side: flipSide[target.side] } : target\n const back = (p: Point) => (turned ? flip(p) : p)\n const ds = direction[s.side]\n const dt = direction[t.side]\n const s1 = { x: s.x + ds.x * STUB, y: s.y + ds.y * STUB }\n const t1 = { x: t.x + dt.x * STUB, y: t.y + dt.y * STUB }\n let points: Point[]\n let label: Point\n let handle: EdgeRoute['handle']\n\n if (ds.x && dt.x && ds.x === -dt.x && (t.x - s.x) * ds.x > 0) {\n // Facing each other: across, along, across.\n const bend = Math.min(0.95, Math.max(0.05, edge?.data?.bend ?? 0.5))\n const mid = s.x + (t.x - s.x) * bend\n points = [s, { x: mid, y: s.y }, { x: mid, y: t.y }, t]\n label = { x: mid, y: (s.y + t.y) / 2 }\n // No handle when the ends line up: the middle segment has no length.\n if (t.x !== s.x && Math.abs(t.y - s.y) >= 1) {\n const span = t.x - s.x\n const at = back(label)\n handle = {\n at,\n axis: turned ? 'y' : 'x',\n move: (point) => {\n const v = turned ? point.y : point.x\n return { bend: Math.min(0.95, Math.max(0.05, (v - s.x) / span)) }\n },\n }\n }\n } else if (ds.x && dt.x) {\n // Facing away, or the same way: out, around through the middle, in.\n const gap = Math.abs(s.y - t.y)\n const my = gap >= STUB * 2 ? (s.y + t.y) / 2 : Math.max(s.y, t.y) + STUB * 2\n const x1 = ds.x === dt.x ? (ds.x > 0 ? Math.max(s1.x, t1.x) : Math.min(s1.x, t1.x)) : s1.x\n const x2 = ds.x === dt.x ? x1 : t1.x\n points = [s, { x: x1, y: s.y }, { x: x1, y: my }, { x: x2, y: my }, { x: x2, y: t.y }, t]\n label = { x: (x1 + x2) / 2, y: my }\n } else {\n // One across, one along: a single corner when the ends allow it.\n const corner = ds.x ? { x: t1.x, y: s1.y } : { x: s1.x, y: t1.y }\n points = [s, s1, corner, t1, t]\n label = corner\n }\n return { path: rounded(points.map(back)), label: back(label), handle }\n },\n})\n"],"mappings":";;AAmCA,SAAgB,eACd,MACoB;CACpB,OAAO;AACT;AAEA,IAAM,YAAqC;CACzC,MAAM;EAAE,GAAG;EAAI,GAAG;CAAE;CACpB,OAAO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GAAG,IAAI,IAAI,KAAK,IAAI,GAAG,GAAG,GAAG,CAAC,IAAI,KAAK,IAAI,GAAG,GAAG,GAAG,CAAC,IAAK,GAAG;GACzF,MAAM,KAAK,GAAG,MAAM,GAAG,IAAI,KAAK,GAAG;GACnC,SAAS;IAAC;IAAG;KAAE,GAAG;KAAI,GAAG,EAAE;IAAE;IAAG;KAAE,GAAG;KAAI,GAAG;IAAG;IAAG;KAAE,GAAG;KAAI,GAAG;IAAG;IAAG;KAAE,GAAG;KAAI,GAAG,EAAE;IAAE;IAAG;GAAC;GACxF,QAAQ;IAAE,IAAI,KAAK,MAAM;IAAG,GAAG;GAAG;EACpC,OAAO;GAEL,MAAM,SAAS,GAAG,IAAI;IAAE,GAAG,GAAG;IAAG,GAAG,GAAG;GAAE,IAAI;IAAE,GAAG,GAAG;IAAG,GAAG,GAAG;GAAE;GAChE,SAAS;IAAC;IAAG;IAAI;IAAQ;IAAI;GAAC;GAC9B,QAAQ;EACV;EACA,OAAO;GAAE,MAAM,QAAQ,OAAO,IAAI,IAAI,CAAC;GAAG,OAAO,KAAK,KAAK;GAAG;EAAO;CACvE;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"edge-types.js","names":[],"sources":["../../src/core/edge-types.ts"],"sourcesContent":["import type { CSSProperties } from 'vue'\nimport type { FlowData, FlowEdge, Point, PortSide } from './model'\n\n/** Where an edge leaves or enters a node: a point and the side it faces. */\nexport interface EdgeAnchor extends Point {\n side: PortSide\n}\n\nexport interface EdgeRoute {\n /** The SVG path, in canvas coordinates. */\n path: string\n /** Where the label sits. */\n label: Point\n /**\n * A handle shown on the selected edge that moves part of it, e.g. the\n * middle of a step edge. `axis` is the way it moves; `move` turns the\n * pointer's canvas point into a patch for `edge.data`.\n */\n handle?: { at: Point; axis: 'x' | 'y'; move: (point: Point) => FlowData }\n /**\n * Marks drawn with the edge's stroke but never dashed, e.g. arrowheads:\n * an SVG path in canvas coordinates.\n */\n marks?: string\n}\n\nexport interface EdgeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /** The edge's path between two anchors. `edge` is absent while one is being drawn. */\n route: (source: EdgeAnchor, target: EdgeAnchor, edge?: FlowEdge<Data>) => EdgeRoute\n /** The edge's own look, e.g. `strokeDasharray`, over what its port type gives it. */\n style?: (edge: FlowEdge<Data>) => CSSProperties | undefined\n}\n\n/** Declares an edge type, with `edge.data` typed. */\nexport function defineEdgeType<Data extends FlowData = FlowData>(\n spec: EdgeTypeSpec<Data>,\n): EdgeTypeSpec<Data> {\n return spec\n}\n\nconst direction: Record<PortSide, Point> = {\n left: { x: -1, y: 0 },\n right: { x: 1, y: 0 },\n top: { x: 0, y: -1 },\n bottom: { x: 0, y: 1 },\n}\n\nconst round = (value: number) => Math.round(value * 100) / 100\nconst fmt = (p: Point) => `${round(p.x)} ${round(p.y)}`\nconst isVertical = (side: PortSide) => side === 'top' || side === 'bottom'\n\n/**\n * A cubic curve leaving `from` and entering `to` straight out of their\n * sides. The handles are equally long, half the distance along the axis\n * they point in (at least `min`), so a fan of edges from one port is\n * mirror-symmetric and a straight run stays a straight line.\n */\nfunction bezierControls(from: EdgeAnchor, to: EdgeAnchor, min = 40) {\n const a = direction[from.side]\n const b = direction[to.side]\n const span = (dir: Point) => Math.abs(dir.x ? to.x - from.x : to.y - from.y)\n const lenA = Math.max(min, span(a) / 2)\n const lenB = Math.max(min, span(b) / 2)\n return [\n { x: from.x + a.x * lenA, y: from.y + a.y * lenA },\n { x: to.x + b.x * lenB, y: to.y + b.y * lenB },\n ] as const\n}\n\nexport function bezierPath(from: Point, fromSide: PortSide, to: Point, toSide: PortSide, min = 40) {\n const [c1, c2] = bezierControls({ ...from, side: fromSide }, { ...to, side: toSide }, min)\n return `M${fmt(from)}C${fmt(c1)} ${fmt(c2)} ${fmt(to)}`\n}\n\n/** The point halfway along a run of points: a middle one, or between the two middle ones. */\nfunction middle(points: Point[]): Point {\n const a = points[Math.floor((points.length - 1) / 2)]!\n const b = points[Math.ceil((points.length - 1) / 2)]!\n return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 }\n}\n\n/**\n * The side a run between two points leaves the first by and the side it\n * enters the second by, along the axis the flow runs in: the one the\n * source's port faces.\n */\nfunction sides(a: Point, b: Point, vertical: boolean): [PortSide, PortSide] {\n if (vertical) return b.y >= a.y ? ['bottom', 'top'] : ['top', 'bottom']\n return b.x >= a.x ? ['right', 'left'] : ['left', 'right']\n}\n\n/**\n * A curve through the points an edge runs through, as tidying lays them:\n * straight along a rank, curving across the gaps between ranks.\n */\nfunction bezierThrough(source: EdgeAnchor, target: EdgeAnchor, points: Point[]): EdgeRoute {\n const vertical = isVertical(source.side)\n const all: EdgeAnchor[] = [source, ...points.map((p) => ({ ...p, side: source.side })), target]\n let path = `M${fmt(source)}`\n for (let i = 1; i < all.length; i++) {\n const a = all[i - 1]!\n const b = all[i]!\n const level = vertical ? Math.abs(a.x - b.x) < 0.5 : Math.abs(a.y - b.y) < 0.5\n if (level && i > 1 && i < all.length - 1) {\n path += `L${fmt(b)}`\n continue\n }\n const [out, into] = sides(a, b, vertical)\n const [c1, c2] = bezierControls(\n { ...a, side: i === 1 ? source.side : out },\n { ...b, side: i === all.length - 1 ? target.side : into },\n 0,\n )\n path += `C${fmt(c1)} ${fmt(c2)} ${fmt(b)}`\n }\n return { path, label: middle(points) }\n}\n\n/** The default: a smooth curve out of and into the ports' sides. */\nexport const bezierEdge = defineEdgeType({\n type: 'bezier',\n route(source, target, edge) {\n if (edge?.points?.length) return bezierThrough(source, target, edge.points)\n const [c1, c2] = bezierControls(source, target)\n return {\n path: `M${fmt(source)}C${fmt(c1)} ${fmt(c2)} ${fmt(target)}`,\n // The curve at its middle.\n label: {\n x: (source.x + 3 * c1.x + 3 * c2.x + target.x) / 8,\n y: (source.y + 3 * c1.y + 3 * c2.y + target.y) / 8,\n },\n }\n },\n})\n\nexport const straightEdge = defineEdgeType({\n type: 'straight',\n route: (source, target, edge) =>\n edge?.points?.length\n ? {\n path: `M${[source, ...edge.points, target].map(fmt).join('L')}`,\n label: middle(edge.points),\n }\n : {\n path: `M${fmt(source)}L${fmt(target)}`,\n label: { x: (source.x + target.x) / 2, y: (source.y + target.y) / 2 },\n },\n})\n\n/** How far a step edge runs out of a port before its first turn. */\nconst STUB = 20\nconst RADIUS = 6\n\n/** A polyline with its corners rounded. */\nfunction rounded(points: Point[], radius = RADIUS) {\n // Drop repeated points and points in the middle of a straight run.\n const pts: Point[] = []\n for (const p of points) {\n const last = pts[pts.length - 1]\n if (last && Math.abs(last.x - p.x) < 0.01 && Math.abs(last.y - p.y) < 0.01) continue\n const before = pts[pts.length - 2]\n if (\n before &&\n last &&\n ((Math.abs(before.x - last.x) < 0.01 && Math.abs(last.x - p.x) < 0.01) ||\n (Math.abs(before.y - last.y) < 0.01 && Math.abs(last.y - p.y) < 0.01))\n )\n pts.pop()\n pts.push(p)\n }\n let d = `M${fmt(pts[0]!)}`\n for (let i = 1; i < pts.length - 1; i++) {\n const [a, b, c] = [pts[i - 1]!, pts[i]!, pts[i + 1]!]\n const lenIn = Math.hypot(b.x - a.x, b.y - a.y)\n const lenOut = Math.hypot(c.x - b.x, c.y - b.y)\n const r = Math.min(radius, lenIn / 2, lenOut / 2)\n const p1 = { x: b.x - ((b.x - a.x) / lenIn) * r, y: b.y - ((b.y - a.y) / lenIn) * r }\n const p2 = { x: b.x + ((c.x - b.x) / lenOut) * r, y: b.y + ((c.y - b.y) / lenOut) * r }\n d += `L${fmt(p1)}Q${fmt(b)} ${fmt(p2)}`\n }\n return d + `L${fmt(pts[pts.length - 1]!)}`\n}\n\nconst flip = (p: Point): Point => ({ x: p.y, y: p.x })\nconst flipSide: Record<PortSide, PortSide> = {\n left: 'top',\n right: 'bottom',\n top: 'left',\n bottom: 'right',\n}\n\ninterface StepData extends FlowData {\n /** Where the middle segment sits between the two ends, from 0 to 1. */\n bend?: number\n}\n\n/**\n * Right angles with rounded corners. Between ports that face each other the\n * middle segment can be dragged; its place is kept in `edge.data.bend`.\n */\nexport const stepEdge = defineEdgeType<StepData>({\n type: 'step',\n route(source, target, edge) {\n if (edge?.points?.length) return stepThrough(source, target, edge.points)\n // Work as if the ports faced left and right, then turn the result back.\n const turned = isVertical(source.side) && isVertical(target.side)\n const s = turned ? { ...flip(source), side: flipSide[source.side] } : source\n const t = turned ? { ...flip(target), side: flipSide[target.side] } : target\n const back = (p: Point) => (turned ? flip(p) : p)\n const ds = direction[s.side]\n const dt = direction[t.side]\n const s1 = { x: s.x + ds.x * STUB, y: s.y + ds.y * STUB }\n const t1 = { x: t.x + dt.x * STUB, y: t.y + dt.y * STUB }\n let points: Point[]\n let label: Point\n let handle: EdgeRoute['handle']\n\n if (ds.x && dt.x && ds.x === -dt.x && (t.x - s.x) * ds.x > 0) {\n // Facing each other: across, along, across.\n const bend = Math.min(0.95, Math.max(0.05, edge?.data?.bend ?? 0.5))\n const mid = s.x + (t.x - s.x) * bend\n points = [s, { x: mid, y: s.y }, { x: mid, y: t.y }, t]\n label = { x: mid, y: (s.y + t.y) / 2 }\n // No handle when the ends line up: the middle segment has no length.\n if (t.x !== s.x && Math.abs(t.y - s.y) >= 1) {\n const span = t.x - s.x\n const at = back(label)\n handle = {\n at,\n axis: turned ? 'y' : 'x',\n move: (point) => {\n const v = turned ? point.y : point.x\n return { bend: Math.min(0.95, Math.max(0.05, (v - s.x) / span)) }\n },\n }\n }\n } else if (ds.x && dt.x) {\n // Facing away, or the same way: out, around through the middle, in.\n const gap = Math.abs(s.y - t.y)\n const my = gap >= STUB * 2 ? (s.y + t.y) / 2 : Math.max(s.y, t.y) + STUB * 2\n const x1 = ds.x === dt.x ? (ds.x > 0 ? Math.max(s1.x, t1.x) : Math.min(s1.x, t1.x)) : s1.x\n const x2 = ds.x === dt.x ? x1 : t1.x\n points = [s, { x: x1, y: s.y }, { x: x1, y: my }, { x: x2, y: my }, { x: x2, y: t.y }, t]\n label = { x: (x1 + x2) / 2, y: my }\n } else {\n // One across, one along: a single corner when the ends allow it.\n const corner = ds.x ? { x: t1.x, y: s1.y } : { x: s1.x, y: t1.y }\n points = [s, s1, corner, t1, t]\n label = corner\n }\n return { path: rounded(points.map(back)), label: back(label), handle }\n },\n})\n\n/**\n * Right angles through the points an edge runs through: along each rank,\n * across each gap between ranks halfway through it.\n */\nfunction stepThrough(source: EdgeAnchor, target: EdgeAnchor, points: Point[]): EdgeRoute {\n const vertical = isVertical(source.side)\n const all = [source, ...points, target]\n const out: Point[] = [source]\n for (let i = 1; i < all.length; i++) {\n const a = all[i - 1]!\n const b = all[i]!\n if (vertical) {\n const y = (a.y + b.y) / 2\n out.push({ x: a.x, y }, { x: b.x, y }, b)\n } else {\n const x = (a.x + b.x) / 2\n out.push({ x, y: a.y }, { x, y: b.y }, b)\n }\n }\n return { path: rounded(out), label: middle(points) }\n}\n"],"mappings":";;AAmCA,SAAgB,eACd,MACoB;CACpB,OAAO;AACT;AAEA,IAAM,YAAqC;CACzC,MAAM;EAAE,GAAG;EAAI,GAAG;CAAE;CACpB,OAAO;EAAE,GAAG;EAAG,GAAG;CAAE;CACpB,KAAK;EAAE,GAAG;EAAG,GAAG;CAAG;CACnB,QAAQ;EAAE,GAAG;EAAG,GAAG;CAAE;AACvB;AAEA,IAAM,SAAS,UAAkB,KAAK,MAAM,QAAQ,GAAG,IAAI;AAC3D,IAAM,OAAO,MAAa,GAAG,MAAM,EAAE,CAAC,EAAE,GAAG,MAAM,EAAE,CAAC;AACpD,IAAM,cAAc,SAAmB,SAAS,SAAS,SAAS;;;;;;;AAQlE,SAAS,eAAe,MAAkB,IAAgB,MAAM,IAAI;CAClE,MAAM,IAAI,UAAU,KAAK;CACzB,MAAM,IAAI,UAAU,GAAG;CACvB,MAAM,QAAQ,QAAe,KAAK,IAAI,IAAI,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI,KAAK,CAAC;CAC3E,MAAM,OAAO,KAAK,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC;CACtC,MAAM,OAAO,KAAK,IAAI,KAAK,KAAK,CAAC,IAAI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6 +69,32 @@ var FlowHistory = class {
|
|
|
69
69
|
selection: entry.selectionAfter
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Gives a change kept out of undo to a node or edge as undo and redo
|
|
74
|
+
* bring it back, too: comments started on a new node stay on it when its
|
|
75
|
+
* adding is undone and redone.
|
|
76
|
+
*/
|
|
77
|
+
carry(target, patch) {
|
|
78
|
+
const merge = (item) => {
|
|
79
|
+
const next = {
|
|
80
|
+
...item,
|
|
81
|
+
...patch
|
|
82
|
+
};
|
|
83
|
+
for (const [key, value] of Object.entries(patch)) if (value === void 0) delete next[key];
|
|
84
|
+
return next;
|
|
85
|
+
};
|
|
86
|
+
const fix = (steps) => steps.map((step) => "node" in target && step.type === "addNode" && step.node.id === target.node ? {
|
|
87
|
+
...step,
|
|
88
|
+
node: merge(step.node)
|
|
89
|
+
} : "edge" in target && step.type === "addEdge" && step.edge.id === target.edge ? {
|
|
90
|
+
...step,
|
|
91
|
+
edge: merge(step.edge)
|
|
92
|
+
} : step);
|
|
93
|
+
for (const entry of [...this.done, ...this.undone]) {
|
|
94
|
+
entry.steps = fix(entry.steps);
|
|
95
|
+
entry.inverse = fix(entry.inverse);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
72
98
|
clear() {
|
|
73
99
|
this.done = [];
|
|
74
100
|
this.undone = [];
|
package/dist/core/history.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","names":[],"sources":["../../src/core/history.ts"],"sourcesContent":["import type { FlowDocument, FlowSelection } from './model'\nimport {\n applyFlowSteps,\n compactFlowSteps,\n type FlowStep,\n type FlowTransaction,\n} from './transaction'\n\ninterface Entry {\n steps: FlowStep[]\n inverse: FlowStep[]\n group?: string\n time: number\n selectionBefore: FlowSelection\n selectionAfter: FlowSelection\n}\n\n/** Transactions in one group merge when they come within this many ms. */\nconst GROUP_WINDOW = 1000\n\nexport class FlowHistory {\n private done: Entry[] = []\n private undone: Entry[] = []\n\n constructor(private depth = 100) {}\n\n get canUndo() {\n return this.done.length > 0\n }\n\n get canRedo() {\n return this.undone.length > 0\n }\n\n record(\n tr: FlowTransaction,\n selectionBefore: FlowSelection,\n selectionAfter: FlowSelection,\n now = Date.now(),\n ) {\n if (!tr.changed || tr.meta.history === false) return\n this.undone = []\n const last = this.done[this.done.length - 1]\n const group = tr.meta.group\n if (last && group && last.group === group && now - last.time < GROUP_WINDOW) {\n last.steps = compactFlowSteps([...last.steps, ...tr.steps])\n last.inverse = compactFlowSteps([...tr.inverse, ...last.inverse])\n last.time = now\n last.selectionAfter = selectionAfter\n return\n }\n this.done.push({\n steps: tr.steps.slice(),\n inverse: tr.inverse.slice(),\n group,\n time: now,\n selectionBefore,\n selectionAfter,\n })\n if (this.done.length > this.depth) this.done.shift()\n }\n\n /** Ends the current group, so the next change starts a new undo step. */\n close() {\n const last = this.done[this.done.length - 1]\n if (last) last.group = undefined\n }\n\n /** Forgets the last entry if it belongs to `group`, e.g. after a cancelled drag. */\n drop(group: string) {\n const last = this.done[this.done.length - 1]\n if (last?.group === group) this.done.pop()\n }\n\n undo(doc: FlowDocument) {\n const entry = this.done.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.inverse)\n this.undone.push(entry)\n return { tr, selection: entry.selectionBefore }\n }\n\n redo(doc: FlowDocument) {\n const entry = this.undone.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.steps)\n this.done.push({ ...entry, group: undefined })\n return { tr, selection: entry.selectionAfter }\n }\n\n clear() {\n this.done = []\n this.undone = []\n }\n}\n"],"mappings":";;;AAkBA,IAAM,eAAe;AAErB,IAAa,cAAb,MAAyB;CAIH;CAHpB,OAAwB,CAAC;CACzB,SAA0B,CAAC;CAE3B,YAAY,QAAgB,KAAK;EAAb,KAAA,QAAA;CAAc;CAElC,IAAI,UAAU;EACZ,OAAO,KAAK,KAAK,SAAS;CAC5B;CAEA,IAAI,UAAU;EACZ,OAAO,KAAK,OAAO,SAAS;CA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KAAK,KAAK,KAAK;GAAE,GAAG;GAAO,OAAO,KAAA;EAAU,CAAC;EAC7C,OAAO;GAAE;GAAI,WAAW,MAAM;EAAe;CAC/C;CAEA,QAAQ;EACN,KAAK,OAAO,CAAC;EACb,KAAK,SAAS,CAAC;CACjB;AACF"}
|
|
1
|
+
{"version":3,"file":"history.js","names":[],"sources":["../../src/core/history.ts"],"sourcesContent":["import type { FlowDocument, FlowSelection } from './model'\nimport {\n applyFlowSteps,\n compactFlowSteps,\n type FlowStep,\n type FlowTransaction,\n} from './transaction'\n\ninterface Entry {\n steps: FlowStep[]\n inverse: FlowStep[]\n group?: string\n time: number\n selectionBefore: FlowSelection\n selectionAfter: FlowSelection\n}\n\n/** Transactions in one group merge when they come within this many ms. */\nconst GROUP_WINDOW = 1000\n\nexport class FlowHistory {\n private done: Entry[] = []\n private undone: Entry[] = []\n\n constructor(private depth = 100) {}\n\n get canUndo() {\n return this.done.length > 0\n }\n\n get canRedo() {\n return this.undone.length > 0\n }\n\n record(\n tr: FlowTransaction,\n selectionBefore: FlowSelection,\n selectionAfter: FlowSelection,\n now = Date.now(),\n ) {\n if (!tr.changed || tr.meta.history === false) return\n this.undone = []\n const last = this.done[this.done.length - 1]\n const group = tr.meta.group\n if (last && group && last.group === group && now - last.time < GROUP_WINDOW) {\n last.steps = compactFlowSteps([...last.steps, ...tr.steps])\n last.inverse = compactFlowSteps([...tr.inverse, ...last.inverse])\n last.time = now\n last.selectionAfter = selectionAfter\n return\n }\n this.done.push({\n steps: tr.steps.slice(),\n inverse: tr.inverse.slice(),\n group,\n time: now,\n selectionBefore,\n selectionAfter,\n })\n if (this.done.length > this.depth) this.done.shift()\n }\n\n /** Ends the current group, so the next change starts a new undo step. */\n close() {\n const last = this.done[this.done.length - 1]\n if (last) last.group = undefined\n }\n\n /** Forgets the last entry if it belongs to `group`, e.g. after a cancelled drag. */\n drop(group: string) {\n const last = this.done[this.done.length - 1]\n if (last?.group === group) this.done.pop()\n }\n\n undo(doc: FlowDocument) {\n const entry = this.done.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.inverse)\n this.undone.push(entry)\n return { tr, selection: entry.selectionBefore }\n }\n\n redo(doc: FlowDocument) {\n const entry = this.undone.pop()\n if (!entry) return null\n const tr = applyFlowSteps(doc, entry.steps)\n this.done.push({ ...entry, group: undefined })\n return { tr, selection: entry.selectionAfter }\n }\n\n /**\n * Gives a change kept out of undo to a node or edge as undo and redo\n * bring it back, too: comments started on a new node stay on it when its\n * adding is undone and redone.\n */\n carry(target: { node: string } | { edge: string }, patch: Record<string, unknown>) {\n const merge = <T extends object>(item: T): T => {\n const next: Record<string, unknown> = { ...item, ...patch }\n for (const [key, value] of Object.entries(patch)) if (value === undefined) delete next[key]\n return next as T\n }\n const fix = (steps: FlowStep[]) =>\n steps.map((step) =>\n 'node' in target && step.type === 'addNode' && step.node.id === target.node\n ? { ...step, node: merge(step.node) }\n : 'edge' in target && step.type === 'addEdge' && step.edge.id === target.edge\n ? { ...step, edge: merge(step.edge) }\n : step,\n )\n for (const entry of [...this.done, ...this.undone]) {\n entry.steps = fix(entry.steps)\n entry.inverse = fix(entry.inverse)\n }\n }\n\n clear() {\n this.done = []\n this.undone = []\n }\n}\n"],"mappings":";;;AAkBA,IAAM,eAAe;AAErB,IAAa,cAAb,MAAyB;CAIH;CAHpB,OAAwB,CAAC;CACzB,SAA0B,CAAC;CAE3B,YAAY,QAAgB,KAAK;EAAb,KAAA,QAAA;CAAc;CAElC,IAAI,UAAU;EACZ,OAAO,KAAK,KAAK,SAAS;CAC5B;CAEA,IAAI,UAAU;EACZ,OAAO,KAAK,OAAO,SAAS;CAC9B;CAEA,OACE,IACA,iBACA,gBACA,MAAM,KAAK,IAAI,GACf;EACA,IAAI,CAAC,GAAG,WAAW,GAAG,KAAK,YAAY,OAAO;EAC9C,KAAK,SAAS,CAAC;EACf,MAAM,OAAO,KAAK,KAAK,KAAK,KAAK,SAAS;EAC1C,MAAM,QAAQ,GAAG,KAAK;EACt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M;GAC1D,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,KAAK,GAAG,IAAI,UAAU,KAAA,GAAW,OAAO,KAAK;GACvF,OAAO;EACT;EACA,MAAM,OAAO,UACX,MAAM,KAAK,SACT,UAAU,UAAU,KAAK,SAAS,aAAa,KAAK,KAAK,OAAO,OAAO,OACnE;GAAE,GAAG;GAAM,MAAM,MAAM,KAAK,IAAI;EAAE,IAClC,UAAU,UAAU,KAAK,SAAS,aAAa,KAAK,KAAK,OAAO,OAAO,OACrE;GAAE,GAAG;GAAM,MAAM,MAAM,KAAK,IAAI;EAAE,IAClC,IACR;EACF,KAAK,MAAM,SAAS,CAAC,GAAG,KAAK,MAAM,GAAG,KAAK,MAAM,GAAG;GAClD,MAAM,QAAQ,IAAI,MAAM,KAAK;GAC7B,MAAM,UAAU,IAAI,MAAM,OAAO;EACnC;CACF;CAEA,QAAQ;EACN,KAAK,OAAO,CAAC;EACb,KAAK,SAAS,CAAC;CACjB;AACF"}
|
package/dist/core/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","names":[],"sources":["../../src/core/model.ts"],"sourcesContent":["/*\n * The document a flow editor edits: plain, serialisable data, bound with\n * `v-model`. Positions are canvas coordinates in px, from the top left of a\n * node. Ports are not stored; a node type declares them, and edges refer to\n * them by id.\n */\n\nexport type FlowData = Record<string, unknown>\n\nexport interface FlowNode<Data extends FlowData = FlowData> {\n id: string\n /** A registered node type; `'default'` when left out. */\n type?: string\n x: number\n y: number\n /** Only stored for nodes that can be resized; otherwise the content decides. */\n width?: number\n height?: number\n data?: Data\n /** A locked node cannot be moved or deleted. */\n locked?: boolean\n /**\n * The container node, e.g. a group, this node sits in. Its `x` and `y`\n * stay canvas coordinates; moving the container moves it along.\n */\n parent?: string\n /**\n * Stacking order among nodes that aren't containers: higher is drawn on\n * top. Nodes without it count as `0`; equal ones keep the document order.\n */\n z?: number\n}\n\nexport interface FlowEndpoint {\n node: string\n /** Leave out to attach to the node itself. */\n port?: string\n}\n\nexport interface FlowEdge<Data extends FlowData = FlowData> {\n id: string\n source: FlowEndpoint\n target: FlowEndpoint\n /** The edge type: `bezier`, `step`, `straight` or one added by an extension. The editor's `edge-type` when left out. */\n type?: string\n /** Text shown on the edge. */\n label?: string\n /** Dashes run along the edge in its direction, e.g. while data flows through it. */\n animated?: boolean\n data?: Data\n}\n\nexport interface FlowDocument {\n /** The version of the format; see `migrateDocument`. */\n version: 1\n nodes: FlowNode[]\n edges: FlowEdge[]\n}\n\nexport type PortSide = 'left' | 'right' | 'top' | 'bottom'\n\nexport interface FlowPortSpec {\n id: string\n /** Inputs receive edges, outputs start them. */\n kind: 'input' | 'output'\n /** Where the port sits; inputs default to the left, outputs to the right. */\n side?: PortSide\n label?: string\n /** What the port carries. Two ports connect when they share a type, or either is `'any'`. */\n dataType?: string | string[]\n /**\n * The most edges the port takes. Inputs take one by default, outputs any\n * number. A new edge on a full port replaces its oldest edge.\n */\n max?: number\n}\n\n/** Where the canvas is shown: `x` and `y` are the screen offset of the canvas origin. */\nexport interface FlowViewport {\n x: number\n y: number\n zoom: number\n}\n\nexport interface FlowSelection {\n nodes: string[]\n edges: string[]\n}\n\nexport interface Point {\n x: number\n y: number\n}\n\nexport interface Rect {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const emptyDocument = (): FlowDocument => ({ version: 1, nodes: [], edges: [] })\nexport const emptySelection = (): FlowSelection => ({ nodes: [], edges: [] })\n\nlet counter = 0\n\n/** A short id, unique within this page and very likely across clients. */\nexport function createId(prefix = 'n') {\n counter = (counter + 1) % 1296\n const time = Date.now().toString(36).slice(-5)\n const random = Math.random().toString(36).slice(2, 6)\n return `${prefix}${time}${counter.toString(36).padStart(2, '0')}${random}`\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"model.js","names":[],"sources":["../../src/core/model.ts"],"sourcesContent":["/*\n * The document a flow editor edits: plain, serialisable data, bound with\n * `v-model`. Positions are canvas coordinates in px, from the top left of a\n * node. Ports are not stored; a node type declares them, and edges refer to\n * them by id.\n */\n\nexport type FlowData = Record<string, unknown>\n\nexport interface FlowNode<Data extends FlowData = FlowData> {\n id: string\n /** A registered node type; `'default'` when left out. */\n type?: string\n x: number\n y: number\n /** Only stored for nodes that can be resized; otherwise the content decides. */\n width?: number\n height?: number\n data?: Data\n /** A locked node cannot be moved or deleted. */\n locked?: boolean\n /**\n * The container node, e.g. a group, this node sits in. Its `x` and `y`\n * stay canvas coordinates; moving the container moves it along.\n */\n parent?: string\n /**\n * Stacking order among nodes that aren't containers: higher is drawn on\n * top. Nodes without it count as `0`; equal ones keep the document order.\n */\n z?: number\n /**\n * The ids of the comment threads about this node, see the editor's\n * `comments`. Kept when the node is deleted and brought back by undo.\n */\n comments?: string[]\n}\n\nexport interface FlowEndpoint {\n node: string\n /** Leave out to attach to the node itself. */\n port?: string\n}\n\nexport interface FlowEdge<Data extends FlowData = FlowData> {\n id: string\n source: FlowEndpoint\n target: FlowEndpoint\n /** The edge type: `bezier`, `step`, `straight` or one added by an extension. The editor's `edge-type` when left out. */\n type?: string\n /** Text shown on the edge. */\n label?: string\n /** Dashes run along the edge in its direction, e.g. while data flows through it. */\n animated?: boolean\n /**\n * Points on the canvas the edge runs through on its way, in order. Tidying\n * sets them on an edge that spans several ranks, so it passes between\n * the nodes in its way; moving either end clears them.\n */\n points?: Point[]\n data?: Data\n /** The ids of the comment threads about this edge, see the editor's `comments`. */\n comments?: string[]\n}\n\nexport interface FlowDocument {\n /** The version of the format; see `migrateDocument`. */\n version: 1\n nodes: FlowNode[]\n edges: FlowEdge[]\n}\n\nexport type PortSide = 'left' | 'right' | 'top' | 'bottom'\n\nexport interface FlowPortSpec {\n id: string\n /** Inputs receive edges, outputs start them. */\n kind: 'input' | 'output'\n /** Where the port sits; inputs default to the left, outputs to the right. */\n side?: PortSide\n label?: string\n /** What the port carries. Two ports connect when they share a type, or either is `'any'`. */\n dataType?: string | string[]\n /**\n * The most edges the port takes. Inputs take one by default, outputs any\n * number. A new edge on a full port replaces its oldest edge.\n */\n max?: number\n}\n\n/** Where the canvas is shown: `x` and `y` are the screen offset of the canvas origin. */\nexport interface FlowViewport {\n x: number\n y: number\n zoom: number\n}\n\nexport interface FlowSelection {\n nodes: string[]\n edges: string[]\n}\n\nexport interface Point {\n x: number\n y: number\n}\n\nexport interface Rect {\n x: number\n y: number\n width: number\n height: number\n}\n\nexport const emptyDocument = (): FlowDocument => ({ version: 1, nodes: [], edges: [] })\nexport const emptySelection = (): FlowSelection => ({ nodes: [], edges: [] })\n\nlet counter = 0\n\n/** A short id, unique within this page and very likely across clients. */\nexport function createId(prefix = 'n') {\n counter = (counter + 1) % 1296\n const time = Date.now().toString(36).slice(-5)\n const random = Math.random().toString(36).slice(2, 6)\n return `${prefix}${time}${counter.toString(36).padStart(2, '0')}${random}`\n}\n"],"mappings":";AAkHA,IAAa,uBAAqC;CAAE,SAAS;CAAG,OAAO,CAAC;CAAG,OAAO,CAAC;AAAE;AACrF,IAAa,wBAAuC;CAAE,OAAO,CAAC;CAAG,OAAO,CAAC;AAAE;AAE3E,IAAI,UAAU;;AAGd,SAAgB,SAAS,SAAS,KAAK;CACrC,WAAW,UAAU,KAAK;CAC1B,MAAM,OAAO,KAAK,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CAC7C,MAAM,SAAS,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC;CACpD,OAAO,GAAG,SAAS,OAAO,QAAQ,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,IAAI;AACpE"}
|
package/dist/core/node-types.js
CHANGED
|
@@ -27,6 +27,13 @@ function labelOf(spec, node, messages) {
|
|
|
27
27
|
function nodeBounds(spec, node, rect) {
|
|
28
28
|
return spec.bounds?.(node, rect) ?? turnedBox(rect, spec.rotation?.(node) ?? 0);
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
var unknownWarned = /* @__PURE__ */ new Set();
|
|
31
|
+
/** Warns once per page about a node type no one registered. Call only while developing. */
|
|
32
|
+
function warnUnknownNodeType(type) {
|
|
33
|
+
if (unknownWarned.has(type)) return;
|
|
34
|
+
unknownWarned.add(type);
|
|
35
|
+
console.warn(`[@dou-ui/flow] node type "${type}" is not registered, so it is drawn as a default node: pass it in nodeTypes (or an extension)`);
|
|
36
|
+
}
|
|
37
|
+
export { defineNodeType, labelOf, nodeBounds, portSide, portsOf, resizeOf, warnUnknownNodeType };
|
|
31
38
|
|
|
32
39
|
//# sourceMappingURL=node-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-types.js","names":[],"sources":["../../src/core/node-types.ts"],"sourcesContent":["import type { FlowDrawContext } from '../export/context'\nimport type { Component } from 'vue'\nimport type { FlowMessages } from '../locale'\nimport { turnedBox } from './geometry'\nimport type { FlowData, FlowNode, FlowPortSpec, PortSide, Rect } from './model'\n\n/** What a node type's component receives. */\nexport interface NodeViewProps<Data extends FlowData = FlowData> {\n node: FlowNode<Data>\n data: Data\n ports: FlowPortSpec[]\n selected: boolean\n readonly: boolean\n /** Merges `patch` into the node's data, as one undo step. */\n updateData: (patch: Partial<Data>) => void\n}\n\nexport interface NodeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /**\n * Renders the node's content. The editor draws the frame, selection and\n * focus ring around it. Place ports with `<DouFlowPort>`; ports the\n * component does not place are spread along their sides.\n */\n component?: Component\n /** The node's ports, fixed or worked out from its data. */\n ports?: FlowPortSpec[] | ((node: FlowNode<Data>) => FlowPortSpec[])\n /** Data for a new node of this type. */\n defaults?: () => Data\n /**\n * The node's accessible name; `data.label` by default. `messages` are the\n * editor's current strings, for names that follow the locale.\n */\n label?: (node: FlowNode<Data>, messages: FlowMessages) => string\n /**\n * How the type is offered by `paletteTypes` and in the list that opens\n * when an edge is dropped on the empty canvas. Types without it aren't offered.\n */\n palette?: NodePaletteSpec\n /**\n * Nodes dropped inside become its children (`parent`) and move with it;\n * with `data.collapsed` they are hidden and their edges end on it.\n * Containers are drawn under edges and other nodes.\n */\n container?: boolean\n /**\n * A handle on the selected node changes its stored `width` and `height`.\n * A function decides per node, e.g. not while it is turned.\n */\n resizable?: NodeResize | ((node: FlowNode<Data>) => NodeResize)\n /** The `width` and `height` a new node starts with. */\n size?: { width: number; height: number }\n /**\n * `false` leaves out the card the editor draws around the component (its\n * border, background and shadow), for nodes that draw their own outline,\n * e.g. a circle. Selection then shows as a thin outline around the box.\n */\n frame?: boolean\n /**\n * How far the node is drawn turned about its centre, in degrees clockwise;\n * `0` by default. The component turns what it draws, and `draw` turns its\n * export; the editor follows: the selection outline, picking with a box,\n * what is drawn in view, the toolbar and other people's outlines. For\n * nodes with `frame: false`. A turned node has no editor resize handle.\n */\n rotation?: (node: FlowNode<Data>) => number\n /**\n * The box the node covers on screen when it draws outside its own `rect`.\n * The toolbar over the selection floats above it. By default the box\n * around `rect` turned by `rotation`.\n */\n bounds?: (node: FlowNode<Data>, rect: Rect) => Rect\n /**\n * Draws the node as SVG markup for `exportSVG` and `exportPNG`, in its\n * own coordinates. Without it the node is drawn as a card with its name.\n */\n draw?: (node: FlowNode<Data>, draw: FlowDrawContext) => string\n}\n\nexport interface NodePaletteSpec {\n label: string\n icon?: Component\n /** Types with the same group are listed together under its name. */\n group?: string\n description?: string\n /** Other words the search should find the type by. */\n keywords?: string[]\n}\n\n/** Declares a node type, with the data type checked against its component. */\nexport function defineNodeType<Data extends FlowData = FlowData>(\n spec: NodeTypeSpec<Data>,\n): NodeTypeSpec<Data> {\n if (process.env.NODE_ENV !== 'production' && spec.rotation && spec.frame !== false)\n console.warn(\n `[@dou-ui/flow] the node type \"${spec.type}\" has rotation but not frame: false: the card the editor draws around it does not turn`,\n )\n return spec\n}\n\nexport const portSide = (port: FlowPortSpec): PortSide =>\n port.side ?? (port.kind === 'input' ? 'left' : 'right')\n\nexport function portsOf(spec: NodeTypeSpec | undefined, node: FlowNode): FlowPortSpec[] {\n const ports = spec?.ports\n if (!ports) return []\n return typeof ports === 'function' ? ports(node) : ports\n}\n\nexport type NodeResize = boolean | { minWidth?: number; minHeight?: number }\n\n/** Whether a node can be resized, and its smallest size; `null` when it can't. */\nexport function resizeOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n): { minWidth?: number; minHeight?: number } | null {\n // the editor's handle grows a node along the page, not along a turned node's sides\n if (spec?.rotation?.(node)) return null\n const value = typeof spec?.resizable === 'function' ? spec.resizable(node) : spec?.resizable\n if (!value) return null\n return value === true ? {} : value\n}\n\nexport function labelOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n messages: FlowMessages,\n): string {\n if (spec?.label) return spec.label(node, messages)\n const label = node.data?.label\n return typeof label === 'string' ? label : node.id\n}\n\n/** The box `node` covers on screen, drawn at `rect`. */\nexport function nodeBounds(spec: NodeTypeSpec, node: FlowNode, rect: Rect): Rect {\n return spec.bounds?.(node, rect) ?? turnedBox(rect, spec.rotation?.(node) ?? 0)\n}\n"],"mappings":";;;AA0FA,SAAgB,eACd,MACoB;CACpB,IAAA,QAAA,IAAA,aAA6B,gBAAgB,KAAK,YAAY,KAAK,UAAU,OAC3E,QAAQ,KACN,iCAAiC,KAAK,KAAK,uFAC7C;CACF,OAAO;AACT;AAEA,IAAa,YAAY,SACvB,KAAK,SAAS,KAAK,SAAS,UAAU,SAAS;AAEjD,SAAgB,QAAQ,MAAgC,MAAgC;CACtF,MAAM,QAAQ,MAAM;CACpB,IAAI,CAAC,OAAO,OAAO,CAAC;CACpB,OAAO,OAAO,UAAU,aAAa,MAAM,IAAI,IAAI;AACrD;;AAKA,SAAgB,SACd,MACA,MACkD;CAElD,IAAI,MAAM,WAAW,IAAI,GAAG,OAAO;CACnC,MAAM,QAAQ,OAAO,MAAM,cAAc,aAAa,KAAK,UAAU,IAAI,IAAI,MAAM;CACnF,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,UAAU,OAAO,CAAC,IAAI;AAC/B;AAEA,SAAgB,QACd,MACA,MACA,UACQ;CACR,IAAI,MAAM,OAAO,OAAO,KAAK,MAAM,MAAM,QAAQ;CACjD,MAAM,QAAQ,KAAK,MAAM;CACzB,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAK;AAClD;;AAGA,SAAgB,WAAW,MAAoB,MAAgB,MAAkB;CAC/E,OAAO,KAAK,SAAS,MAAM,IAAI,KAAK,UAAU,MAAM,KAAK,WAAW,IAAI,KAAK,CAAC;AAChF"}
|
|
1
|
+
{"version":3,"file":"node-types.js","names":[],"sources":["../../src/core/node-types.ts"],"sourcesContent":["import type { FlowDrawContext } from '../export/context'\nimport type { Component } from 'vue'\nimport type { FlowMessages } from '../locale'\nimport { turnedBox } from './geometry'\nimport type { FlowData, FlowNode, FlowPortSpec, PortSide, Rect } from './model'\n\n/** What a node type's component receives. */\nexport interface NodeViewProps<Data extends FlowData = FlowData> {\n node: FlowNode<Data>\n data: Data\n ports: FlowPortSpec[]\n selected: boolean\n readonly: boolean\n /** Merges `patch` into the node's data, as one undo step. */\n updateData: (patch: Partial<Data>) => void\n}\n\nexport interface NodeTypeSpec<Data extends FlowData = FlowData> {\n type: string\n /**\n * Renders the node's content. The editor draws the frame, selection and\n * focus ring around it. Place ports with `<DouFlowPort>`; ports the\n * component does not place are spread along their sides.\n */\n component?: Component\n /** The node's ports, fixed or worked out from its data. */\n ports?: FlowPortSpec[] | ((node: FlowNode<Data>) => FlowPortSpec[])\n /** Data for a new node of this type. */\n defaults?: () => Data\n /**\n * The node's accessible name; `data.label` by default. `messages` are the\n * editor's current strings, for names that follow the locale.\n */\n label?: (node: FlowNode<Data>, messages: FlowMessages) => string\n /**\n * How the type is offered by `paletteTypes` and in the list that opens\n * when an edge is dropped on the empty canvas. Types without it aren't offered.\n */\n palette?: NodePaletteSpec\n /**\n * Nodes dropped inside become its children (`parent`) and move with it;\n * with `data.collapsed` they are hidden and their edges end on it.\n * Containers are drawn under edges and other nodes.\n */\n container?: boolean\n /**\n * A handle on the selected node changes its stored `width` and `height`.\n * A function decides per node, e.g. not while it is turned.\n */\n resizable?: NodeResize | ((node: FlowNode<Data>) => NodeResize)\n /** The `width` and `height` a new node starts with. */\n size?: { width: number; height: number }\n /**\n * `false` leaves out the card the editor draws around the component (its\n * border, background and shadow), for nodes that draw their own outline,\n * e.g. a circle. Selection then shows as a thin outline around the box.\n */\n frame?: boolean\n /**\n * How far the node is drawn turned about its centre, in degrees clockwise;\n * `0` by default. The component turns what it draws, and `draw` turns its\n * export; the editor follows: the selection outline, picking with a box,\n * what is drawn in view, the toolbar and other people's outlines. For\n * nodes with `frame: false`. A turned node has no editor resize handle.\n */\n rotation?: (node: FlowNode<Data>) => number\n /**\n * The box the node covers on screen when it draws outside its own `rect`.\n * The toolbar over the selection floats above it. By default the box\n * around `rect` turned by `rotation`.\n */\n bounds?: (node: FlowNode<Data>, rect: Rect) => Rect\n /**\n * Draws the node as SVG markup for `exportSVG` and `exportPNG`, in its\n * own coordinates. Without it the node is drawn as a card with its name.\n */\n draw?: (node: FlowNode<Data>, draw: FlowDrawContext) => string\n}\n\nexport interface NodePaletteSpec {\n label: string\n icon?: Component\n /** Types with the same group are listed together under its name. */\n group?: string\n description?: string\n /** Other words the search should find the type by. */\n keywords?: string[]\n}\n\n/** Declares a node type, with the data type checked against its component. */\nexport function defineNodeType<Data extends FlowData = FlowData>(\n spec: NodeTypeSpec<Data>,\n): NodeTypeSpec<Data> {\n if (process.env.NODE_ENV !== 'production' && spec.rotation && spec.frame !== false)\n console.warn(\n `[@dou-ui/flow] the node type \"${spec.type}\" has rotation but not frame: false: the card the editor draws around it does not turn`,\n )\n return spec\n}\n\nexport const portSide = (port: FlowPortSpec): PortSide =>\n port.side ?? (port.kind === 'input' ? 'left' : 'right')\n\nexport function portsOf(spec: NodeTypeSpec | undefined, node: FlowNode): FlowPortSpec[] {\n const ports = spec?.ports\n if (!ports) return []\n return typeof ports === 'function' ? ports(node) : ports\n}\n\nexport type NodeResize = boolean | { minWidth?: number; minHeight?: number }\n\n/** Whether a node can be resized, and its smallest size; `null` when it can't. */\nexport function resizeOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n): { minWidth?: number; minHeight?: number } | null {\n // the editor's handle grows a node along the page, not along a turned node's sides\n if (spec?.rotation?.(node)) return null\n const value = typeof spec?.resizable === 'function' ? spec.resizable(node) : spec?.resizable\n if (!value) return null\n return value === true ? {} : value\n}\n\nexport function labelOf(\n spec: NodeTypeSpec | undefined,\n node: FlowNode,\n messages: FlowMessages,\n): string {\n if (spec?.label) return spec.label(node, messages)\n const label = node.data?.label\n return typeof label === 'string' ? label : node.id\n}\n\n/** The box `node` covers on screen, drawn at `rect`. */\nexport function nodeBounds(spec: NodeTypeSpec, node: FlowNode, rect: Rect): Rect {\n return spec.bounds?.(node, rect) ?? turnedBox(rect, spec.rotation?.(node) ?? 0)\n}\n\nconst unknownWarned = new Set<string>()\n/** Warns once per page about a node type no one registered. Call only while developing. */\nexport function warnUnknownNodeType(type: string) {\n if (unknownWarned.has(type)) return\n unknownWarned.add(type)\n console.warn(\n `[@dou-ui/flow] node type \"${type}\" is not registered, so it is drawn as a default node: pass it in nodeTypes (or an extension)`,\n )\n}\n"],"mappings":";;;AA0FA,SAAgB,eACd,MACoB;CACpB,IAAA,QAAA,IAAA,aAA6B,gBAAgB,KAAK,YAAY,KAAK,UAAU,OAC3E,QAAQ,KACN,iCAAiC,KAAK,KAAK,uFAC7C;CACF,OAAO;AACT;AAEA,IAAa,YAAY,SACvB,KAAK,SAAS,KAAK,SAAS,UAAU,SAAS;AAEjD,SAAgB,QAAQ,MAAgC,MAAgC;CACtF,MAAM,QAAQ,MAAM;CACpB,IAAI,CAAC,OAAO,OAAO,CAAC;CACpB,OAAO,OAAO,UAAU,aAAa,MAAM,IAAI,IAAI;AACrD;;AAKA,SAAgB,SACd,MACA,MACkD;CAElD,IAAI,MAAM,WAAW,IAAI,GAAG,OAAO;CACnC,MAAM,QAAQ,OAAO,MAAM,cAAc,aAAa,KAAK,UAAU,IAAI,IAAI,MAAM;CACnF,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,UAAU,OAAO,CAAC,IAAI;AAC/B;AAEA,SAAgB,QACd,MACA,MACA,UACQ;CACR,IAAI,MAAM,OAAO,OAAO,KAAK,MAAM,MAAM,QAAQ;CACjD,MAAM,QAAQ,KAAK,MAAM;CACzB,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAK;AAClD;;AAGA,SAAgB,WAAW,MAAoB,MAAgB,MAAkB;CAC/E,OAAO,KAAK,SAAS,MAAM,IAAI,KAAK,UAAU,MAAM,KAAK,WAAW,IAAI,KAAK,CAAC;AAChF;AAEA,IAAM,gCAAgB,IAAI,IAAY;;AAEtC,SAAgB,oBAAoB,MAAc;CAChD,IAAI,cAAc,IAAI,IAAI,GAAG;CAC7B,cAAc,IAAI,IAAI;CACtB,QAAQ,KACN,6BAA6B,KAAK,8FACpC;AACF"}
|
package/dist/core/transaction.js
CHANGED
|
@@ -54,11 +54,12 @@ var FlowTransaction = class {
|
|
|
54
54
|
* editor fires `transactionCancel` and says why. For an extension's `transaction`
|
|
55
55
|
* hook, e.g. to keep people from changing what they may not; hooks after
|
|
56
56
|
* it don't run. A change kept out of the history (`meta.history: false`),
|
|
57
|
-
* such as undo, redo
|
|
57
|
+
* such as undo, redo, another person's or a comment's (`meta.origin`
|
|
58
|
+
* tells which), can't be cancelled and goes ahead.
|
|
58
59
|
*/
|
|
59
60
|
cancel(reason) {
|
|
60
61
|
if (this.meta.history !== false) this.stopped = { reason };
|
|
61
|
-
else warnUncancellable();
|
|
62
|
+
else warnUncancellable(this.meta.origin);
|
|
62
63
|
return this;
|
|
63
64
|
}
|
|
64
65
|
/** Whether `cancel` stopped it. */
|
|
@@ -69,15 +70,59 @@ var FlowTransaction = class {
|
|
|
69
70
|
get cancelReason() {
|
|
70
71
|
return this.stopped?.reason;
|
|
71
72
|
}
|
|
73
|
+
index = {};
|
|
74
|
+
incident;
|
|
75
|
+
was;
|
|
76
|
+
nodeIndex(id) {
|
|
77
|
+
this.index.nodes ??= new Map(this.nodes.map((node, i) => [node.id, i]));
|
|
78
|
+
return this.index.nodes.get(id) ?? -1;
|
|
79
|
+
}
|
|
80
|
+
edgeIndex(id) {
|
|
81
|
+
this.index.edges ??= new Map(this.edges.map((edge, i) => [edge.id, i]));
|
|
82
|
+
return this.index.edges.get(id) ?? -1;
|
|
83
|
+
}
|
|
84
|
+
/** The document as it was before this transaction. */
|
|
85
|
+
get original() {
|
|
86
|
+
this.was ??= {
|
|
87
|
+
nodes: new Map(this.before.nodes.map((node) => [node.id, node])),
|
|
88
|
+
edges: new Map(this.before.edges.map((edge) => [edge.id, edge]))
|
|
89
|
+
};
|
|
90
|
+
return this.was;
|
|
91
|
+
}
|
|
72
92
|
getNode(id) {
|
|
73
|
-
return this.nodes.
|
|
93
|
+
return this.nodes[this.nodeIndex(id)];
|
|
74
94
|
}
|
|
75
95
|
getEdge(id) {
|
|
76
|
-
return this.edges.
|
|
96
|
+
return this.edges[this.edgeIndex(id)];
|
|
77
97
|
}
|
|
78
98
|
/** Edges that start or end at a node. */
|
|
79
99
|
edgesOf(id) {
|
|
80
|
-
|
|
100
|
+
if (!this.incident) {
|
|
101
|
+
this.incident = /* @__PURE__ */ new Map();
|
|
102
|
+
for (const edge of this.edges) this.attach(edge);
|
|
103
|
+
}
|
|
104
|
+
return (this.incident.get(id) ?? []).flatMap((e) => this.getEdge(e) ?? []);
|
|
105
|
+
}
|
|
106
|
+
attach(edge) {
|
|
107
|
+
if (!this.incident) return;
|
|
108
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
109
|
+
const list = this.incident.get(end);
|
|
110
|
+
if (list) list.push(edge.id);
|
|
111
|
+
else this.incident.set(end, [edge.id]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
detach(edge) {
|
|
115
|
+
if (!this.incident) return;
|
|
116
|
+
for (const end of /* @__PURE__ */ new Set([edge.source.node, edge.target.node])) {
|
|
117
|
+
const list = this.incident.get(end);
|
|
118
|
+
const at = list?.indexOf(edge.id) ?? -1;
|
|
119
|
+
if (at >= 0) list.splice(at, 1);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Renumbers an index from `from` on, after an insert or removal there. */
|
|
123
|
+
reindex(map, list, from) {
|
|
124
|
+
if (!map) return;
|
|
125
|
+
for (let i = from; i < list.length; i++) map.set(list[i].id, i);
|
|
81
126
|
}
|
|
82
127
|
setMeta(meta) {
|
|
83
128
|
Object.assign(this.meta, meta);
|
|
@@ -106,19 +151,56 @@ var FlowTransaction = class {
|
|
|
106
151
|
id
|
|
107
152
|
});
|
|
108
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Changes a node. Moving it moves the points of an edge at it along when
|
|
156
|
+
* the edge's other end has moved just as far in this transaction, and
|
|
157
|
+
* drops them otherwise, as they no longer fit; so does resizing it, as
|
|
158
|
+
* its ports move.
|
|
159
|
+
*/
|
|
109
160
|
updateNode(id, patch) {
|
|
110
|
-
|
|
161
|
+
const node = this.getNode(id);
|
|
162
|
+
const changed = (key) => node !== void 0 && key in patch && patch[key] !== node[key];
|
|
163
|
+
const moved = changed("x") || changed("y");
|
|
164
|
+
const resized = changed("width") || changed("height");
|
|
165
|
+
this.step({
|
|
111
166
|
type: "updateNode",
|
|
112
167
|
id,
|
|
113
168
|
patch
|
|
114
169
|
});
|
|
170
|
+
if (moved || resized) this.carryPoints(id, resized);
|
|
171
|
+
return this;
|
|
115
172
|
}
|
|
116
173
|
moveNode(id, x, y) {
|
|
174
|
+
const node = this.getNode(id);
|
|
175
|
+
if (node && node.x === x && node.y === y) return this;
|
|
117
176
|
return this.updateNode(id, {
|
|
118
177
|
x,
|
|
119
178
|
y
|
|
120
179
|
});
|
|
121
180
|
}
|
|
181
|
+
/** Moves the points of the edges at a moved node along, or drops them. */
|
|
182
|
+
carryPoints(id, drop) {
|
|
183
|
+
const shift = this.shiftOf(id);
|
|
184
|
+
for (const edge of this.edgesOf(id)) {
|
|
185
|
+
const before = this.original.edges.get(edge.id);
|
|
186
|
+
const other = this.shiftOf(edge.source.node === id ? edge.target.node : edge.source.node);
|
|
187
|
+
const together = !drop && shift && other && other.x === shift.x && other.y === shift.y;
|
|
188
|
+
if (before?.points && together) this.updateEdge(edge.id, { points: before.points.map((p) => ({
|
|
189
|
+
x: p.x + shift.x,
|
|
190
|
+
y: p.y + shift.y
|
|
191
|
+
})) });
|
|
192
|
+
else if (edge.points) this.updateEdge(edge.id, { points: void 0 });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/** How far a node has moved in this transaction so far. */
|
|
196
|
+
shiftOf(id) {
|
|
197
|
+
const now = this.getNode(id);
|
|
198
|
+
const was = this.original.nodes.get(id);
|
|
199
|
+
return now && was ? {
|
|
200
|
+
x: now.x - was.x,
|
|
201
|
+
y: now.y - was.y
|
|
202
|
+
} : void 0;
|
|
203
|
+
}
|
|
122
204
|
/** Merges `patch` into the node's `data`. */
|
|
123
205
|
updateData(id, patch) {
|
|
124
206
|
const node = this.getNode(id);
|
|
@@ -164,15 +246,19 @@ var FlowTransaction = class {
|
|
|
164
246
|
const list = this.writable("nodes");
|
|
165
247
|
const index = clampIndex(step.index, list.length);
|
|
166
248
|
list.splice(index, 0, step.node);
|
|
249
|
+
this.reindex(this.index.nodes, list, index);
|
|
167
250
|
return {
|
|
168
251
|
type: "removeNode",
|
|
169
252
|
id: step.node.id
|
|
170
253
|
};
|
|
171
254
|
}
|
|
172
255
|
case "removeNode": {
|
|
173
|
-
const index = this.
|
|
256
|
+
const index = this.nodeIndex(step.id);
|
|
174
257
|
if (index < 0) return null;
|
|
175
|
-
const
|
|
258
|
+
const list = this.writable("nodes");
|
|
259
|
+
const [node] = list.splice(index, 1);
|
|
260
|
+
this.index.nodes?.delete(step.id);
|
|
261
|
+
this.reindex(this.index.nodes, list, index);
|
|
176
262
|
return {
|
|
177
263
|
type: "addNode",
|
|
178
264
|
node,
|
|
@@ -180,7 +266,7 @@ var FlowTransaction = class {
|
|
|
180
266
|
};
|
|
181
267
|
}
|
|
182
268
|
case "updateNode": {
|
|
183
|
-
const index = this.
|
|
269
|
+
const index = this.nodeIndex(step.id);
|
|
184
270
|
if (index < 0) return null;
|
|
185
271
|
const node = this.nodes[index];
|
|
186
272
|
if (Object.keys(step.patch).every((key) => sameValue(node[key], step.patch[key]))) return null;
|
|
@@ -195,16 +281,23 @@ var FlowTransaction = class {
|
|
|
195
281
|
case "addEdge": {
|
|
196
282
|
if (this.getEdge(step.edge.id)) return null;
|
|
197
283
|
const list = this.writable("edges");
|
|
198
|
-
|
|
284
|
+
const index = clampIndex(step.index, list.length);
|
|
285
|
+
list.splice(index, 0, step.edge);
|
|
286
|
+
this.reindex(this.index.edges, list, index);
|
|
287
|
+
this.attach(step.edge);
|
|
199
288
|
return {
|
|
200
289
|
type: "removeEdge",
|
|
201
290
|
id: step.edge.id
|
|
202
291
|
};
|
|
203
292
|
}
|
|
204
293
|
case "removeEdge": {
|
|
205
|
-
const index = this.
|
|
294
|
+
const index = this.edgeIndex(step.id);
|
|
206
295
|
if (index < 0) return null;
|
|
207
|
-
const
|
|
296
|
+
const list = this.writable("edges");
|
|
297
|
+
const [edge] = list.splice(index, 1);
|
|
298
|
+
this.index.edges?.delete(step.id);
|
|
299
|
+
this.reindex(this.index.edges, list, index);
|
|
300
|
+
this.detach(edge);
|
|
208
301
|
return {
|
|
209
302
|
type: "addEdge",
|
|
210
303
|
edge,
|
|
@@ -212,12 +305,16 @@ var FlowTransaction = class {
|
|
|
212
305
|
};
|
|
213
306
|
}
|
|
214
307
|
case "updateEdge": {
|
|
215
|
-
const index = this.
|
|
308
|
+
const index = this.edgeIndex(step.id);
|
|
216
309
|
if (index < 0) return null;
|
|
217
310
|
const edge = this.edges[index];
|
|
218
311
|
if (Object.keys(step.patch).every((key) => sameValue(edge[key], step.patch[key]))) return null;
|
|
219
312
|
const list = this.writable("edges");
|
|
220
313
|
list[index] = patched(edge, step.patch);
|
|
314
|
+
if ("source" in step.patch || "target" in step.patch) {
|
|
315
|
+
this.detach(edge);
|
|
316
|
+
this.attach(list[index]);
|
|
317
|
+
}
|
|
221
318
|
return {
|
|
222
319
|
type: "updateEdge",
|
|
223
320
|
id: step.id,
|
|
@@ -278,10 +375,10 @@ function compactFlowSteps(steps) {
|
|
|
278
375
|
}
|
|
279
376
|
var warnedUncancellable = false;
|
|
280
377
|
/** Says once that cancelling a change kept out of the history does nothing. */
|
|
281
|
-
function warnUncancellable() {
|
|
378
|
+
function warnUncancellable(origin) {
|
|
282
379
|
if (process.env.NODE_ENV === "production" || warnedUncancellable) return;
|
|
283
380
|
warnedUncancellable = true;
|
|
284
|
-
console.warn(
|
|
381
|
+
console.warn(`[@dou-ui/flow] a change kept out of the history can't be cancelled; it goes ahead (meta.origin: ${JSON.stringify(origin ?? null)}). Undo, redo, other people's changes and the marks of comments are such changes: skip them in the hook by meta.origin`);
|
|
285
382
|
}
|
|
286
383
|
export { FlowTransaction, applyFlowSteps, compactFlowSteps };
|
|
287
384
|
|