@luutuankiet/clipboard2markdown 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -91
- package/bin/cli.js +32 -8
- package/bin/diagrams.js +142 -0
- package/bin/excalidraw.js +167 -0
- package/package.json +3 -2
- package/src/diagrams/browser.js +4 -0
- package/src/diagrams/edit.js +523 -0
- package/src/diagrams/index.js +8 -0
- package/src/diagrams/markdown.js +152 -0
- package/src/diagrams/scene-model.js +453 -0
- package/src/diagrams/scene-to-mermaid.js +168 -0
- package/src/diagrams/share-link.js +179 -0
- package/src/diagrams/skeleton.js +321 -0
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
// Reads an Excalidraw scene as a graph: nodes, subgraphs, edges, notes.
|
|
2
|
+
//
|
|
3
|
+
// Both the Mermaid writer and the editor go through `analyzeScene`, so the ids an
|
|
4
|
+
// agent sees in the Mermaid are exactly the ids the editor resolves.
|
|
5
|
+
//
|
|
6
|
+
// Unbound geometry is the normal case, not an edge case: canvases drawn by an LLM
|
|
7
|
+
// and then edited by hand mostly have arrows that are not bound to any shape and
|
|
8
|
+
// arrow labels that are free text. Every rule below falls back to geometry.
|
|
9
|
+
|
|
10
|
+
// All tuning constants live here.
|
|
11
|
+
export const TUNING = {
|
|
12
|
+
// An endpoint within this many px of a shape's bounds hits that shape.
|
|
13
|
+
endpointTolerance: 12,
|
|
14
|
+
// Failing a hit, snap to the nearest node within this distance.
|
|
15
|
+
endpointSnapLimit: 48,
|
|
16
|
+
// Free text within this distance of an arrow's polyline can be its label.
|
|
17
|
+
edgeLabelDistance: 36,
|
|
18
|
+
// Fewer than this share of arrows resolved -> outline instead of flowchart.
|
|
19
|
+
flowchartMinResolvedRatio: 0.75,
|
|
20
|
+
// Colour-key boxes sit at most this far from their neighbour in the key.
|
|
21
|
+
legendGap: 80,
|
|
22
|
+
// A shape counts as containing another when it encloses it within this slack.
|
|
23
|
+
containmentSlack: 2,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const NODE_TYPES = { rectangle: 'rect', ellipse: 'ellipse', diamond: 'diamond' };
|
|
27
|
+
const MERMAID_KEYWORDS = new Set([
|
|
28
|
+
'end', 'graph', 'flowchart', 'subgraph', 'direction', 'click', 'style', 'classdef',
|
|
29
|
+
'class', 'linkstyle', 'default', 'call', 'href', 'tb', 'td', 'bt', 'rl', 'lr',
|
|
30
|
+
]);
|
|
31
|
+
const READABLE_ID = /^[A-Za-z][A-Za-z0-9_]{0,23}$/;
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------- geometry ---
|
|
34
|
+
|
|
35
|
+
export function bounds(el) {
|
|
36
|
+
if (el.type === 'arrow' || el.type === 'line' || el.type === 'freedraw') {
|
|
37
|
+
var pts = absolutePoints(el);
|
|
38
|
+
var xs = pts.map(function (p) { return p[0]; });
|
|
39
|
+
var ys = pts.map(function (p) { return p[1]; });
|
|
40
|
+
var minX = Math.min.apply(null, xs), minY = Math.min.apply(null, ys);
|
|
41
|
+
return { x: minX, y: minY, w: Math.max.apply(null, xs) - minX, h: Math.max.apply(null, ys) - minY };
|
|
42
|
+
}
|
|
43
|
+
var w = el.width || 0, h = el.height || 0;
|
|
44
|
+
return { x: Math.min(el.x, el.x + w), y: Math.min(el.y, el.y + h), w: Math.abs(w), h: Math.abs(h) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function center(el) {
|
|
48
|
+
var b = bounds(el);
|
|
49
|
+
return [b.x + b.w / 2, b.y + b.h / 2];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function absolutePoints(el) {
|
|
53
|
+
var pts = el.points && el.points.length ? el.points : [[0, 0], [el.width || 0, el.height || 0]];
|
|
54
|
+
return pts.map(function (p) { return [el.x + p[0], el.y + p[1]]; });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function area(b) { return b.w * b.h; }
|
|
58
|
+
|
|
59
|
+
function containsPoint(b, p, tol) {
|
|
60
|
+
return p[0] >= b.x - tol && p[0] <= b.x + b.w + tol && p[1] >= b.y - tol && p[1] <= b.y + b.h + tol;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function containsBox(outer, inner, slack) {
|
|
64
|
+
return inner.x >= outer.x - slack && inner.y >= outer.y - slack &&
|
|
65
|
+
inner.x + inner.w <= outer.x + outer.w + slack && inner.y + inner.h <= outer.y + outer.h + slack &&
|
|
66
|
+
area(outer) > area(inner);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function distPointToBox(p, b) {
|
|
70
|
+
var dx = Math.max(b.x - p[0], 0, p[0] - (b.x + b.w));
|
|
71
|
+
var dy = Math.max(b.y - p[1], 0, p[1] - (b.y + b.h));
|
|
72
|
+
return Math.hypot(dx, dy);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function distPointToSegment(p, a, b) {
|
|
76
|
+
var vx = b[0] - a[0], vy = b[1] - a[1];
|
|
77
|
+
var len2 = vx * vx + vy * vy;
|
|
78
|
+
var t = len2 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * vx + (p[1] - a[1]) * vy) / len2)) : 0;
|
|
79
|
+
return Math.hypot(p[0] - (a[0] + t * vx), p[1] - (a[1] + t * vy));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function distPointToPolyline(p, pts) {
|
|
83
|
+
var best = Infinity;
|
|
84
|
+
for (var i = 1; i < pts.length; i++) best = Math.min(best, distPointToSegment(p, pts[i - 1], pts[i]));
|
|
85
|
+
if (pts.length === 1) best = Math.hypot(p[0] - pts[0][0], p[1] - pts[0][1]);
|
|
86
|
+
return best;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Distance from a polyline to a text box: 0 when the line passes through it.
|
|
90
|
+
function distTextToPolyline(textEl, pts) {
|
|
91
|
+
var b = bounds(textEl);
|
|
92
|
+
var c = center(textEl);
|
|
93
|
+
var best = distPointToPolyline(c, pts);
|
|
94
|
+
// Sample the polyline so a long label straddling the arrow still counts.
|
|
95
|
+
for (var i = 1; i < pts.length; i++) {
|
|
96
|
+
for (var s = 0; s <= 8; s++) {
|
|
97
|
+
var t = s / 8;
|
|
98
|
+
var q = [pts[i - 1][0] + t * (pts[i][0] - pts[i - 1][0]), pts[i - 1][1] + t * (pts[i][1] - pts[i - 1][1])];
|
|
99
|
+
best = Math.min(best, distPointToBox(q, b));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return best;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// --------------------------------------------------------------------- ids ---
|
|
106
|
+
|
|
107
|
+
function hash36(s) {
|
|
108
|
+
// FNV-1a, 32 bit, then a second round so short ids have enough entropy.
|
|
109
|
+
var h1 = 0x811c9dc5, h2 = 0x01000193;
|
|
110
|
+
for (var i = 0; i < s.length; i++) {
|
|
111
|
+
h1 = Math.imul(h1 ^ s.charCodeAt(i), 0x01000193) >>> 0;
|
|
112
|
+
h2 = Math.imul(h2 ^ s.charCodeAt(i), 0x5bd1e995) >>> 0;
|
|
113
|
+
}
|
|
114
|
+
return h1.toString(36) + h2.toString(36);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function isReadableId(id) {
|
|
118
|
+
return typeof id === 'string' && READABLE_ID.test(id) && !MERMAID_KEYWORDS.has(id.toLowerCase());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Element ids -> Mermaid ids. Readable element ids are kept; anything else gets a
|
|
122
|
+
// short id derived from a hash of the element id, so the same scene always gives
|
|
123
|
+
// the same ids. Collisions resolve in element order.
|
|
124
|
+
export function assignIds(entries) {
|
|
125
|
+
var used = new Set();
|
|
126
|
+
var out = new Map();
|
|
127
|
+
entries.forEach(function (e) {
|
|
128
|
+
if (isReadableId(e.elementId) && !used.has(e.elementId)) {
|
|
129
|
+
used.add(e.elementId);
|
|
130
|
+
out.set(e.elementId, e.elementId);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
entries.forEach(function (e) {
|
|
134
|
+
if (out.has(e.elementId)) return;
|
|
135
|
+
var h = hash36(e.elementId);
|
|
136
|
+
var id = null;
|
|
137
|
+
for (var len = 3; len <= h.length && !id; len++) {
|
|
138
|
+
var candidate = e.prefix + h.slice(0, len);
|
|
139
|
+
if (!used.has(candidate)) id = candidate;
|
|
140
|
+
}
|
|
141
|
+
for (var n = 2; !id; n++) {
|
|
142
|
+
if (!used.has(e.prefix + h + n)) id = e.prefix + h + n;
|
|
143
|
+
}
|
|
144
|
+
used.add(id);
|
|
145
|
+
out.set(e.elementId, id);
|
|
146
|
+
});
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ----------------------------------------------------------------- analyze ---
|
|
151
|
+
|
|
152
|
+
function textOf(el) {
|
|
153
|
+
return String(el.originalText != null && el.originalText !== '' ? el.originalText : el.text || '').trim();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function isEdgeElement(el) {
|
|
157
|
+
if (el.type === 'arrow') return true;
|
|
158
|
+
return el.type === 'line' && !!(el.startArrowhead || el.endArrowhead);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Fill and line style, the things a colour key distinguishes. A transparent
|
|
162
|
+
// box's fill pattern is invisible, so it does not count.
|
|
163
|
+
export function styleKey(el) {
|
|
164
|
+
var bg = el.backgroundColor || 'transparent';
|
|
165
|
+
return bg + '|' + (bg === 'transparent' ? '' : el.fillStyle || 'solid') + '|' + (el.strokeStyle || 'solid');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// A colour key: two or more labelled boxes that no arrow touches, outside every
|
|
169
|
+
// group and clear of the connected diagram, stacked close together, each in its
|
|
170
|
+
// own style, and each style worn by something in the diagram. Anything less
|
|
171
|
+
// certain stays an ordinary box.
|
|
172
|
+
// Every other box or group wearing a key style is tagged with it, including
|
|
173
|
+
// free-standing ones away from the key.
|
|
174
|
+
function findLegend(nodes, groups, edges) {
|
|
175
|
+
var none = { items: [], ids: new Set() };
|
|
176
|
+
var touched = new Set();
|
|
177
|
+
edges.forEach(function (e) { touched.add(e.from); touched.add(e.to); });
|
|
178
|
+
var candidates = [], diagram = [];
|
|
179
|
+
nodes.forEach(function (n) { (n.parent || touched.has(n.el.id) ? diagram : candidates).push(n); });
|
|
180
|
+
groups.forEach(function (g) { diagram.push(g); });
|
|
181
|
+
if (candidates.length < 2 || !diagram.length) return none;
|
|
182
|
+
|
|
183
|
+
var area = diagram.map(function (d) { return bounds(d.el); }).reduce(function (a, b) {
|
|
184
|
+
var x = Math.min(a.x, b.x), y = Math.min(a.y, b.y);
|
|
185
|
+
return { x: x, y: y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
|
|
186
|
+
});
|
|
187
|
+
var gap = function (a, b) {
|
|
188
|
+
var p = bounds(a.el), q = bounds(b.el);
|
|
189
|
+
return Math.max(p.x - (q.x + q.w), q.x - (p.x + p.w), p.y - (q.y + q.h), q.y - (p.y + p.h), 0);
|
|
190
|
+
};
|
|
191
|
+
var clustered = function (list) {
|
|
192
|
+
return list.filter(function (c) { return list.some(function (o) { return o !== c && gap(c, o) <= TUNING.legendGap; }); });
|
|
193
|
+
};
|
|
194
|
+
// Cluster first, so a stray box elsewhere that shares a key style does not
|
|
195
|
+
// knock that entry out of the key.
|
|
196
|
+
var kept = clustered(candidates.filter(function (c) {
|
|
197
|
+
var b = bounds(c.el);
|
|
198
|
+
return b.x > area.x + area.w || b.x + b.w < area.x || b.y > area.y + area.h || b.y + b.h < area.y;
|
|
199
|
+
}));
|
|
200
|
+
var styleCount = new Map();
|
|
201
|
+
kept.forEach(function (c) { styleCount.set(styleKey(c.el), (styleCount.get(styleKey(c.el)) || 0) + 1); });
|
|
202
|
+
kept = clustered(kept.filter(function (c) {
|
|
203
|
+
var k = styleKey(c.el);
|
|
204
|
+
return styleCount.get(k) === 1 && diagram.some(function (d) { return styleKey(d.el) === k; });
|
|
205
|
+
}));
|
|
206
|
+
if (kept.length < 2) return none;
|
|
207
|
+
|
|
208
|
+
var order = function (a, b) { return a.el.y - b.el.y || a.el.x - b.el.x; };
|
|
209
|
+
var others = diagram.concat(candidates.filter(function (c) { return kept.indexOf(c) < 0; }));
|
|
210
|
+
return {
|
|
211
|
+
items: kept.sort(order).map(function (c) {
|
|
212
|
+
var k = styleKey(c.el);
|
|
213
|
+
return {
|
|
214
|
+
node: c,
|
|
215
|
+
members: others.filter(function (d) { return styleKey(d.el) === k; }),
|
|
216
|
+
};
|
|
217
|
+
}),
|
|
218
|
+
ids: new Set(kept.map(function (c) { return c.el.id; })),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function analyzeScene(scene) {
|
|
223
|
+
var all = (scene && scene.elements) || [];
|
|
224
|
+
var live = all.filter(function (el) { return el && !el.isDeleted; });
|
|
225
|
+
var byId = new Map(live.map(function (el) { return [el.id, el]; }));
|
|
226
|
+
|
|
227
|
+
var shapes = live.filter(function (el) { return NODE_TYPES[el.type] || el.type === 'frame' || el.type === 'magicframe'; });
|
|
228
|
+
var edgesEls = live.filter(isEdgeElement);
|
|
229
|
+
var texts = live.filter(function (el) { return el.type === 'text' && textOf(el); });
|
|
230
|
+
var images = live.filter(function (el) { return el.type === 'image'; });
|
|
231
|
+
|
|
232
|
+
// 1. Bound text: label of its container.
|
|
233
|
+
var boundLabel = new Map(); // containerId -> text el
|
|
234
|
+
var freeTexts = [];
|
|
235
|
+
texts.forEach(function (t) {
|
|
236
|
+
var container = t.containerId && byId.get(t.containerId);
|
|
237
|
+
if (container && !boundLabel.has(container.id)) boundLabel.set(container.id, t);
|
|
238
|
+
else freeTexts.push(t);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// 2. Free text inside a shape belongs to the smallest shape containing its centre.
|
|
242
|
+
var shapeBox = new Map(shapes.map(function (s) { return [s.id, bounds(s)]; }));
|
|
243
|
+
var smallestShapeAt = function (p, filter) {
|
|
244
|
+
var best = null, bestArea = Infinity;
|
|
245
|
+
shapes.forEach(function (s) {
|
|
246
|
+
if (filter && !filter(s)) return;
|
|
247
|
+
var b = shapeBox.get(s.id);
|
|
248
|
+
if (containsPoint(b, p, 0) && area(b) < bestArea) { best = s; bestArea = area(b); }
|
|
249
|
+
});
|
|
250
|
+
return best;
|
|
251
|
+
};
|
|
252
|
+
var insideTexts = new Map(); // shapeId -> [text]
|
|
253
|
+
var outsideTexts = [];
|
|
254
|
+
freeTexts.forEach(function (t) {
|
|
255
|
+
var s = smallestShapeAt(center(t));
|
|
256
|
+
if (s) {
|
|
257
|
+
if (!insideTexts.has(s.id)) insideTexts.set(s.id, []);
|
|
258
|
+
insideTexts.get(s.id).push(t);
|
|
259
|
+
} else {
|
|
260
|
+
outsideTexts.push(t);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
var hasLabel = function (s) { return boundLabel.has(s.id) || insideTexts.has(s.id); };
|
|
265
|
+
|
|
266
|
+
// 3. Groups: frames, and shapes that enclose another labelled shape.
|
|
267
|
+
var isGroup = new Map();
|
|
268
|
+
shapes.forEach(function (s) {
|
|
269
|
+
if (s.type === 'frame' || s.type === 'magicframe') { isGroup.set(s.id, true); return; }
|
|
270
|
+
var b = shapeBox.get(s.id);
|
|
271
|
+
isGroup.set(s.id, shapes.some(function (o) {
|
|
272
|
+
return o !== s && NODE_TYPES[o.type] && hasLabel(o) && containsBox(b, shapeBox.get(o.id), TUNING.containmentSlack);
|
|
273
|
+
}));
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
var groupEls = shapes.filter(function (s) { return isGroup.get(s.id); });
|
|
277
|
+
var nodeEls = shapes.filter(function (s) { return !isGroup.get(s.id) && hasLabel(s); });
|
|
278
|
+
|
|
279
|
+
// 4. Labels. Groups take bound text, else the inside text nearest their top edge.
|
|
280
|
+
var notes = outsideTexts.slice();
|
|
281
|
+
var labelInfo = new Map(); // shapeId -> { text, els: [text elements] }
|
|
282
|
+
nodeEls.forEach(function (s) {
|
|
283
|
+
if (boundLabel.has(s.id)) {
|
|
284
|
+
labelInfo.set(s.id, { text: textOf(boundLabel.get(s.id)), els: [boundLabel.get(s.id)], bound: true });
|
|
285
|
+
(insideTexts.get(s.id) || []).forEach(function (t) { notes.push(t); });
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
var inside = (insideTexts.get(s.id) || []).slice().sort(function (a, b) { return a.y - b.y || a.x - b.x; });
|
|
289
|
+
labelInfo.set(s.id, { text: inside.map(textOf).join('\n'), els: inside, bound: false });
|
|
290
|
+
});
|
|
291
|
+
groupEls.forEach(function (g) {
|
|
292
|
+
var inside = (insideTexts.get(g.id) || []).slice();
|
|
293
|
+
if (boundLabel.has(g.id)) {
|
|
294
|
+
labelInfo.set(g.id, { text: textOf(boundLabel.get(g.id)), els: [boundLabel.get(g.id)], bound: true });
|
|
295
|
+
} else if (g.name) {
|
|
296
|
+
labelInfo.set(g.id, { text: String(g.name), els: [], bound: false, frameName: true });
|
|
297
|
+
} else if (inside.length) {
|
|
298
|
+
var top = shapeBox.get(g.id).y;
|
|
299
|
+
inside.sort(function (a, b) { return (bounds(a).y - top) - (bounds(b).y - top) || a.x - b.x; });
|
|
300
|
+
var title = inside.shift();
|
|
301
|
+
labelInfo.set(g.id, { text: textOf(title), els: [title], bound: false });
|
|
302
|
+
} else {
|
|
303
|
+
labelInfo.set(g.id, { text: '', els: [], bound: false });
|
|
304
|
+
}
|
|
305
|
+
inside.forEach(function (t) { if (labelInfo.get(g.id).els.indexOf(t) < 0) notes.push(t); });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
// 5. Ids, in element order so collisions resolve the same way every run.
|
|
309
|
+
var idMap = assignIds(live.filter(function (el) {
|
|
310
|
+
return nodeEls.indexOf(el) >= 0 || groupEls.indexOf(el) >= 0;
|
|
311
|
+
}).map(function (el) {
|
|
312
|
+
return { elementId: el.id, prefix: isGroup.get(el.id) ? 'g' : 'n' };
|
|
313
|
+
}));
|
|
314
|
+
|
|
315
|
+
// 6. Parents: frameId first, else the smallest enclosing group.
|
|
316
|
+
var parentOf = function (el) {
|
|
317
|
+
if (el.frameId && isGroup.get(el.frameId) && byId.has(el.frameId)) return el.frameId;
|
|
318
|
+
var b = shapeBox.get(el.id);
|
|
319
|
+
var best = null, bestArea = Infinity;
|
|
320
|
+
groupEls.forEach(function (g) {
|
|
321
|
+
if (g === el) return;
|
|
322
|
+
var gb = shapeBox.get(g.id);
|
|
323
|
+
if (containsBox(gb, b, TUNING.containmentSlack) && area(gb) < bestArea) { best = g.id; bestArea = area(gb); }
|
|
324
|
+
});
|
|
325
|
+
return best;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
var nodes = new Map();
|
|
329
|
+
nodeEls.forEach(function (el) {
|
|
330
|
+
nodes.set(el.id, { el: el, id: idMap.get(el.id), kind: 'node', shape: NODE_TYPES[el.type], label: labelInfo.get(el.id), parent: parentOf(el) });
|
|
331
|
+
});
|
|
332
|
+
var groups = new Map();
|
|
333
|
+
groupEls.forEach(function (el) {
|
|
334
|
+
groups.set(el.id, { el: el, id: idMap.get(el.id), kind: 'group', label: labelInfo.get(el.id), parent: parentOf(el) });
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
// 7. Edge endpoints: binding first, then geometry.
|
|
338
|
+
var nodeList = Array.from(nodes.values());
|
|
339
|
+
var groupList = Array.from(groups.values());
|
|
340
|
+
var targetOf = function (elementId) {
|
|
341
|
+
if (nodes.has(elementId)) return elementId;
|
|
342
|
+
if (groups.has(elementId)) return elementId;
|
|
343
|
+
var el = byId.get(elementId);
|
|
344
|
+
if (el && el.type === 'text' && el.containerId && (nodes.has(el.containerId) || groups.has(el.containerId))) return el.containerId;
|
|
345
|
+
return null;
|
|
346
|
+
};
|
|
347
|
+
var resolvePoint = function (p) {
|
|
348
|
+
var hit = null, hitArea = Infinity;
|
|
349
|
+
nodeList.forEach(function (n) {
|
|
350
|
+
var b = bounds(n.el);
|
|
351
|
+
if (distPointToBox(p, b) <= TUNING.endpointTolerance && area(b) < hitArea) { hit = n.el.id; hitArea = area(b); }
|
|
352
|
+
});
|
|
353
|
+
if (hit) return hit;
|
|
354
|
+
groupList.forEach(function (g) {
|
|
355
|
+
var b = bounds(g.el);
|
|
356
|
+
if (containsPoint(b, p, TUNING.endpointTolerance) && area(b) < hitArea) { hit = g.el.id; hitArea = area(b); }
|
|
357
|
+
});
|
|
358
|
+
if (hit) return hit;
|
|
359
|
+
var bestDist = TUNING.endpointSnapLimit;
|
|
360
|
+
nodeList.forEach(function (n) {
|
|
361
|
+
var d = distPointToBox(p, bounds(n.el));
|
|
362
|
+
if (d <= bestDist) { hit = n.el.id; bestDist = d; }
|
|
363
|
+
});
|
|
364
|
+
return hit;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
var rawEdges = edgesEls.map(function (el) {
|
|
368
|
+
var pts = absolutePoints(el);
|
|
369
|
+
var bindStart = el.startBinding && el.startBinding.elementId && targetOf(el.startBinding.elementId);
|
|
370
|
+
var bindEnd = el.endBinding && el.endBinding.elementId && targetOf(el.endBinding.elementId);
|
|
371
|
+
var start = bindStart || resolvePoint(pts[0]);
|
|
372
|
+
var end = bindEnd || resolvePoint(pts[pts.length - 1]);
|
|
373
|
+
if (start && start === end && !(bindStart && bindEnd)) end = null;
|
|
374
|
+
return { el: el, pts: pts, start: start, end: end };
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// 8. Edge labels: bound text, else nearest unclaimed free text. Each text is
|
|
378
|
+
// claimed once, by its nearest arrow.
|
|
379
|
+
var edgeLabel = new Map(); // arrowId -> { text, els, bound }
|
|
380
|
+
rawEdges.forEach(function (e) {
|
|
381
|
+
var t = boundLabel.get(e.el.id);
|
|
382
|
+
if (t) edgeLabel.set(e.el.id, { text: textOf(t), els: [t], bound: true });
|
|
383
|
+
});
|
|
384
|
+
var remainingNotes = [];
|
|
385
|
+
notes.forEach(function (t) {
|
|
386
|
+
var best = null, bestDist = TUNING.edgeLabelDistance;
|
|
387
|
+
rawEdges.forEach(function (e) {
|
|
388
|
+
if (edgeLabel.has(e.el.id) && edgeLabel.get(e.el.id).bound) return;
|
|
389
|
+
var d = distTextToPolyline(t, e.pts);
|
|
390
|
+
if (d <= bestDist) { best = e; bestDist = d; }
|
|
391
|
+
});
|
|
392
|
+
if (best) {
|
|
393
|
+
var cur = edgeLabel.get(best.el.id);
|
|
394
|
+
if (!cur) edgeLabel.set(best.el.id, { text: textOf(t), els: [t], bound: false, dist: bestDist });
|
|
395
|
+
else if (bestDist < cur.dist) { remainingNotes.push(cur.els[0]); edgeLabel.set(best.el.id, { text: textOf(t), els: [t], bound: false, dist: bestDist }); }
|
|
396
|
+
else remainingNotes.push(t);
|
|
397
|
+
} else {
|
|
398
|
+
remainingNotes.push(t);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
var edges = [];
|
|
403
|
+
var unresolved = [];
|
|
404
|
+
rawEdges.forEach(function (e) {
|
|
405
|
+
var el = e.el;
|
|
406
|
+
var startHead = !!el.startArrowhead;
|
|
407
|
+
// A missing endArrowhead on an arrow means Excalidraw's default head.
|
|
408
|
+
var endHead = el.type === 'arrow' && el.endArrowhead === undefined ? true : !!el.endArrowhead;
|
|
409
|
+
var from = e.start, to = e.end, kind;
|
|
410
|
+
if (startHead && endHead) kind = 'double';
|
|
411
|
+
else if (!startHead && !endHead) kind = 'open';
|
|
412
|
+
else { kind = 'arrow'; if (startHead) { from = e.end; to = e.start; } }
|
|
413
|
+
var label = edgeLabel.get(el.id) || null;
|
|
414
|
+
var edge = { el: el, from: from, to: to, kind: kind, dashed: el.strokeStyle === 'dashed' || el.strokeStyle === 'dotted', label: label };
|
|
415
|
+
if (e.start && e.end) edges.push(edge);
|
|
416
|
+
else unresolved.push(edge);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
var mermaidId = function (elementId) {
|
|
420
|
+
var n = nodes.get(elementId) || groups.get(elementId);
|
|
421
|
+
return n ? n.id : null;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
var legend = findLegend(nodes, groups, edges.concat(unresolved));
|
|
425
|
+
|
|
426
|
+
var byMermaidId = new Map();
|
|
427
|
+
nodes.forEach(function (n) { byMermaidId.set(n.id, n); });
|
|
428
|
+
groups.forEach(function (g) { byMermaidId.set(g.id, g); });
|
|
429
|
+
|
|
430
|
+
// Lifelines and member dividers betray sequence / class / ER canvases.
|
|
431
|
+
var lines = live.filter(function (el) { return el.type === 'line' && !isEdgeElement(el); });
|
|
432
|
+
var lifelines = lines.filter(function (el) { var b = bounds(el); return b.h > 120 && b.h > 4 * b.w; });
|
|
433
|
+
var dividedNodes = nodeEls.filter(function (n) {
|
|
434
|
+
var nb = bounds(n);
|
|
435
|
+
return lines.some(function (l) { var b = bounds(l); return b.h < 4 && b.w > nb.w * 0.6 && containsBox(nb, b, 2); });
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
return {
|
|
439
|
+
nodes: nodes,
|
|
440
|
+
groups: groups,
|
|
441
|
+
edges: edges,
|
|
442
|
+
unresolved: unresolved,
|
|
443
|
+
notes: remainingNotes,
|
|
444
|
+
images: images,
|
|
445
|
+
byId: byId,
|
|
446
|
+
byMermaidId: byMermaidId,
|
|
447
|
+
mermaidId: mermaidId,
|
|
448
|
+
legend: legend,
|
|
449
|
+
edgeCount: rawEdges.length,
|
|
450
|
+
signals: { lifelines: lifelines.length, dividedNodes: dividedNodes.length },
|
|
451
|
+
textOf: textOf,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// Excalidraw scene -> Mermaid reading copy.
|
|
2
|
+
//
|
|
3
|
+
// Deliberately lossy: positions, colours, fonts and arrow curves are dropped;
|
|
4
|
+
// boxes, labels, connections, line style and grouping are kept. The canvas stays
|
|
5
|
+
// the master copy.
|
|
6
|
+
|
|
7
|
+
import { analyzeScene, bounds, center, TUNING } from './scene-model.js';
|
|
8
|
+
|
|
9
|
+
export const GENERATED_MARKER = 'reading copy, edits here do not change the canvas';
|
|
10
|
+
|
|
11
|
+
export function headerLine(sourceUrl) {
|
|
12
|
+
return '%% generated from ' + (sourceUrl || 'local scene') + '; ' + GENERATED_MARKER;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function oneLine(text) {
|
|
16
|
+
return String(text).replace(/\s*\r?\n\s*/g, ' / ').trim();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function formatLabel(text) {
|
|
20
|
+
var t = String(text).replace(/\r?\n/g, '<br/>');
|
|
21
|
+
if (/^[A-Za-z0-9 _.,'?!-]+$/.test(t) && !/^\s|\s$/.test(t)) return t;
|
|
22
|
+
return '"' + t.replace(/"/g, '#quot;') + '"';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function nodeDecl(n) {
|
|
26
|
+
var label = formatLabel(n.label.text);
|
|
27
|
+
if (n.shape === 'ellipse') return n.id + '((' + label + '))';
|
|
28
|
+
if (n.shape === 'diamond') return n.id + '{' + label + '}';
|
|
29
|
+
return n.id + '[' + label + ']';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function edgeArrow(e) {
|
|
33
|
+
if (e.kind === 'double') return e.dashed ? '<-.->' : '<-->';
|
|
34
|
+
if (e.kind === 'open') return e.dashed ? '-.-' : '---';
|
|
35
|
+
return e.dashed ? '-.->' : '-->';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function edgeLine(model, e) {
|
|
39
|
+
var label = e.label && e.label.text ? '|' + formatLabel(e.label.text) + '|' : '';
|
|
40
|
+
return model.mermaidId(e.from) + ' ' + edgeArrow(e) + label + ' ' + model.mermaidId(e.to);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function flowDirection(model) {
|
|
44
|
+
var cs = Array.from(model.nodes.values()).filter(function (n) {
|
|
45
|
+
return !model.legend || !model.legend.ids.has(n.el.id);
|
|
46
|
+
}).map(function (n) { return center(n.el); });
|
|
47
|
+
if (cs.length < 2) return 'TB';
|
|
48
|
+
var xs = cs.map(function (c) { return c[0]; }), ys = cs.map(function (c) { return c[1]; });
|
|
49
|
+
var spreadX = Math.max.apply(null, xs) - Math.min.apply(null, xs);
|
|
50
|
+
var spreadY = Math.max.apply(null, ys) - Math.min.apply(null, ys);
|
|
51
|
+
return spreadX > spreadY ? 'LR' : 'TB';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function commentLines(model) {
|
|
55
|
+
var out = [];
|
|
56
|
+
model.notes.forEach(function (t) { out.push('%% note: ' + oneLine(model.textOf(t))); });
|
|
57
|
+
model.unresolved.forEach(function (e) {
|
|
58
|
+
var label = e.label && e.label.text ? ' "' + oneLine(e.label.text) + '"' : '';
|
|
59
|
+
var from = e.from ? model.mermaidId(e.from) : '?';
|
|
60
|
+
var to = e.to ? model.mermaidId(e.to) : '?';
|
|
61
|
+
out.push('%% unconnected arrow' + label + ': ' + from + ' ' + edgeArrow(e) + ' ' + to);
|
|
62
|
+
});
|
|
63
|
+
model.images.forEach(function (img) {
|
|
64
|
+
var inside = Array.from(model.groups.values()).filter(function (g) {
|
|
65
|
+
var gb = bounds(g.el), c = center(img);
|
|
66
|
+
return c[0] >= gb.x && c[0] <= gb.x + gb.w && c[1] >= gb.y && c[1] <= gb.y + gb.h;
|
|
67
|
+
}).sort(function (a, b) { return bounds(a.el).w * bounds(a.el).h - bounds(b.el).w * bounds(b.el).h; })[0];
|
|
68
|
+
out.push('%% image: embedded picture, contents not readable' + (inside ? ' (inside ' + inside.id + ')' : ''));
|
|
69
|
+
});
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The canvas's colour key, as Mermaid classes: each box in the diagram is
|
|
74
|
+
// tagged with the key entry whose style it wears, and the key boxes themselves
|
|
75
|
+
// are not drawn.
|
|
76
|
+
function legendClasses(model) {
|
|
77
|
+
var used = new Set();
|
|
78
|
+
return model.legend.items.map(function (item, i) {
|
|
79
|
+
var slug = oneLine(item.node.label.text).toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '').slice(0, 20);
|
|
80
|
+
var name = 'key_' + (slug || String(i + 1));
|
|
81
|
+
while (used.has(name)) name += '_';
|
|
82
|
+
used.add(name);
|
|
83
|
+
var bg = item.node.el.backgroundColor;
|
|
84
|
+
return {
|
|
85
|
+
name: name,
|
|
86
|
+
label: oneLine(item.node.label.text),
|
|
87
|
+
fill: bg && bg !== 'transparent' ? bg : 'none',
|
|
88
|
+
members: item.members.map(function (m) { return m.id; }),
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function legendComment(k) {
|
|
94
|
+
return '%% legend: ' + k.name + ' = ' + k.label;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function outlineReason(model) {
|
|
98
|
+
if (model.signals.lifelines >= 2) return 'looks like a sequence diagram (lifelines)';
|
|
99
|
+
if (model.nodes.size && model.signals.dividedNodes * 2 >= model.nodes.size) return 'looks like a class or ER diagram (boxes with member dividers)';
|
|
100
|
+
if (!model.nodes.size) return 'no labelled boxes found';
|
|
101
|
+
if (model.edgeCount > 0) {
|
|
102
|
+
var ratio = model.edges.length / model.edgeCount;
|
|
103
|
+
if (ratio < TUNING.flowchartMinResolvedRatio) {
|
|
104
|
+
return 'only ' + model.edges.length + ' of ' + model.edgeCount + ' arrows connect two boxes';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function sceneToMermaid(scene, options) {
|
|
111
|
+
var sourceUrl = options && options.sourceUrl;
|
|
112
|
+
var model = analyzeScene(scene);
|
|
113
|
+
var warnings = [];
|
|
114
|
+
if (model.unresolved.length) warnings.push(model.unresolved.length + ' arrow(s) could not be matched to boxes');
|
|
115
|
+
if (model.images.length) warnings.push(model.images.length + ' embedded image(s) cannot be read');
|
|
116
|
+
|
|
117
|
+
var reason = outlineReason(model);
|
|
118
|
+
var lines = [headerLine(sourceUrl)];
|
|
119
|
+
|
|
120
|
+
if (reason) {
|
|
121
|
+
lines.push('%% outline (needs agent): ' + reason);
|
|
122
|
+
lines.push('%% labels:');
|
|
123
|
+
model.nodes.forEach(function (n) { if (!model.legend.ids.has(n.el.id)) lines.push('%% ' + n.id + ': ' + oneLine(n.label.text)); });
|
|
124
|
+
model.groups.forEach(function (g) { if (g.label.text) lines.push('%% ' + g.id + ' (group): ' + oneLine(g.label.text)); });
|
|
125
|
+
if (model.edges.length) {
|
|
126
|
+
lines.push('%% connections:');
|
|
127
|
+
model.edges.forEach(function (e) {
|
|
128
|
+
lines.push('%% ' + model.mermaidId(e.from) + ' ' + edgeArrow(e) + ' ' + model.mermaidId(e.to) + (e.label && e.label.text ? ': ' + oneLine(e.label.text) : ''));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
legendClasses(model).forEach(function (k) { lines.push(legendComment(k) + ': ' + k.members.join(', ')); });
|
|
132
|
+
commentLines(model).forEach(function (l) { lines.push(l); });
|
|
133
|
+
return { text: lines.join('\n'), kind: 'outline', warnings: warnings.concat(['outline: ' + reason]) };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
lines.push('flowchart ' + flowDirection(model));
|
|
137
|
+
|
|
138
|
+
var childrenOf = function (parentId) {
|
|
139
|
+
var kids = [];
|
|
140
|
+
model.groups.forEach(function (g) { if (g.parent === parentId) kids.push(g); });
|
|
141
|
+
model.nodes.forEach(function (n) { if (n.parent === parentId && !model.legend.ids.has(n.el.id)) kids.push(n); });
|
|
142
|
+
return kids.sort(function (a, b) { return model.order.get(a.el.id) - model.order.get(b.el.id); });
|
|
143
|
+
};
|
|
144
|
+
model.order = new Map((scene.elements || []).map(function (el, i) { return [el.id, i]; }));
|
|
145
|
+
|
|
146
|
+
var emit = function (parentId, indent) {
|
|
147
|
+
childrenOf(parentId).forEach(function (item) {
|
|
148
|
+
if (item.kind === 'node') {
|
|
149
|
+
lines.push(indent + nodeDecl(item));
|
|
150
|
+
} else {
|
|
151
|
+
lines.push(indent + 'subgraph ' + item.id + (item.label.text ? ' [' + formatLabel(item.label.text) + ']' : ''));
|
|
152
|
+
emit(item.el.id, indent + ' ');
|
|
153
|
+
lines.push(indent + 'end');
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
emit(null, ' ');
|
|
158
|
+
|
|
159
|
+
model.edges.forEach(function (e) { lines.push(' ' + edgeLine(model, e)); });
|
|
160
|
+
legendClasses(model).forEach(function (k) {
|
|
161
|
+
lines.push(' ' + legendComment(k));
|
|
162
|
+
lines.push(' classDef ' + k.name + ' fill:' + k.fill);
|
|
163
|
+
if (k.members.length) lines.push(' class ' + k.members.join(',') + ' ' + k.name);
|
|
164
|
+
});
|
|
165
|
+
commentLines(model).forEach(function (l) { lines.push(' ' + l); });
|
|
166
|
+
|
|
167
|
+
return { text: lines.join('\n'), kind: 'flowchart', warnings: warnings };
|
|
168
|
+
}
|