@yaks/diagram 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +222 -2
- package/_dist/ink.d.ts +30 -0
- package/_dist/ink.d.ts.map +1 -0
- package/_dist/layout.d.ts +92 -0
- package/_dist/layout.d.ts.map +1 -0
- package/_dist/mod.d.ts +20 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/mount.d.ts +55 -0
- package/_dist/mount.d.ts.map +1 -0
- package/_dist/pen.d.ts +50 -0
- package/_dist/pen.d.ts.map +1 -0
- package/_dist/shape.d.ts +44 -0
- package/_dist/shape.d.ts.map +1 -0
- package/_dist/steer.d.ts +13 -0
- package/_dist/steer.d.ts.map +1 -0
- package/_dist/styles.d.ts +25 -0
- package/_dist/styles.d.ts.map +1 -0
- package/_dist/svg.d.ts +22 -0
- package/_dist/svg.d.ts.map +1 -0
- package/_dist/tangle.d.ts +25 -0
- package/_dist/tangle.d.ts.map +1 -0
- package/_dist/view.d.ts +20 -0
- package/_dist/view.d.ts.map +1 -0
- package/deno.json +1 -0
- package/geometry.js +42 -0
- package/geometry.js.map +1 -0
- package/geometry.ts +44 -0
- package/ink.js +43 -0
- package/ink.js.map +1 -0
- package/ink.ts +61 -0
- package/layout.js +1048 -0
- package/layout.js.map +1 -0
- package/layout.ts +981 -0
- package/layout_bench.ts +27 -0
- package/mod.js +20 -0
- package/mod.js.map +1 -0
- package/mod.ts +20 -0
- package/mount.js +177 -0
- package/mount.js.map +1 -0
- package/mount.ts +205 -0
- package/package.json +18 -4
- package/pen.js +106 -0
- package/pen.js.map +1 -0
- package/pen.ts +130 -0
- package/shape.js +233 -0
- package/shape.js.map +1 -0
- package/shape.ts +204 -0
- package/steer.js +132 -0
- package/steer.js.map +1 -0
- package/steer.ts +112 -0
- package/styles.js +110 -0
- package/styles.js.map +1 -0
- package/styles.ts +110 -0
- package/svg.js +119 -0
- package/svg.js.map +1 -0
- package/svg.ts +193 -0
- package/tangle.js +151 -0
- package/tangle.js.map +1 -0
- package/tangle.ts +153 -0
- package/view.js +63 -0
- package/view.js.map +1 -0
- package/view.ts +65 -0
package/layout.js
ADDED
|
@@ -0,0 +1,1048 @@
|
|
|
1
|
+
// Where each entity of a selection sits, and the lines between them. Pure:
|
|
2
|
+
// bundles in, positions out. A drawing that has been seen keeps its places,
|
|
3
|
+
// and a newcomer goes where it overlaps nothing, crosses few lines and sits a
|
|
4
|
+
// short reach from what it names, the way a person adds to a sketch; when no
|
|
5
|
+
// such spot is left, what is in its way moves aside, as little as it must.
|
|
6
|
+
import { cross, meets } from "./geometry.js";
|
|
7
|
+
import { knots } from "./tangle.js";
|
|
8
|
+
import { APART, curve, half, on, SHAPES, tag } from "./shape.js";
|
|
9
|
+
const QUIET = [
|
|
10
|
+
'entity',
|
|
11
|
+
'created',
|
|
12
|
+
'updated'
|
|
13
|
+
];
|
|
14
|
+
// A line drawn from bookkeeping would join everything to its writer.
|
|
15
|
+
const SILENT = new Set([
|
|
16
|
+
'entity',
|
|
17
|
+
'updated',
|
|
18
|
+
'tombstone'
|
|
19
|
+
]);
|
|
20
|
+
/** `#` and the eid's first ten hex digits, the handle @yaks/id shows. */ export let handle = (eid)=>'#' + eid.replaceAll('-', '').slice(0, 10).toLowerCase();
|
|
21
|
+
/** A line of at most n characters, ending in an ellipsis when cut. */ export let cut = (text, n)=>text.length > n ? text.slice(0, n - 1).trimEnd() + '…' : text;
|
|
22
|
+
let words = (b, quiet)=>Object.keys(b).filter((k)=>!k.startsWith('$') && !quiet.includes(k));
|
|
23
|
+
let titled = (b)=>{
|
|
24
|
+
let doc = b.doc;
|
|
25
|
+
let file = b.file;
|
|
26
|
+
let text = [
|
|
27
|
+
doc?.title,
|
|
28
|
+
doc?.body,
|
|
29
|
+
file?.path
|
|
30
|
+
].find((t)=>typeof t == 'string' && t.trim());
|
|
31
|
+
return typeof text == 'string' ? cut(text.trim().split('\n')[0], 60) : '';
|
|
32
|
+
};
|
|
33
|
+
let stateOf = (b)=>{
|
|
34
|
+
for (let v of Object.values(b)){
|
|
35
|
+
let status = v?.status;
|
|
36
|
+
if (typeof status == 'string') return status;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
let isEdge = (b)=>{
|
|
40
|
+
let e = b.edge;
|
|
41
|
+
return typeof e?.from == 'string' && typeof e?.to == 'string';
|
|
42
|
+
};
|
|
43
|
+
/** The lines a selection draws: each edge entity, and every property that
|
|
44
|
+
* names another selected entity. A line to an entity outside the selection is
|
|
45
|
+
* not drawn. */ export let links = (bundles)=>{
|
|
46
|
+
let here = new Set(bundles.filter((b)=>!isEdge(b)).map((b)=>b.entity.eid));
|
|
47
|
+
let out = new Map();
|
|
48
|
+
let add = (l)=>{
|
|
49
|
+
if (l.from == l.to || !here.has(l.from) || !here.has(l.to)) return;
|
|
50
|
+
let key = `${l.from}>${l.name}>${l.to}`;
|
|
51
|
+
if (!out.has(key)) out.set(key, {
|
|
52
|
+
key,
|
|
53
|
+
...l
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
for (let b of bundles){
|
|
57
|
+
if (isEdge(b)) {
|
|
58
|
+
let { from, to } = b.edge;
|
|
59
|
+
let name = words(b, [
|
|
60
|
+
...QUIET,
|
|
61
|
+
'edge'
|
|
62
|
+
]).join(' ') || 'edge';
|
|
63
|
+
add({
|
|
64
|
+
from,
|
|
65
|
+
to,
|
|
66
|
+
name,
|
|
67
|
+
eid: b.entity.eid
|
|
68
|
+
});
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
for (let [comp, value] of Object.entries(b)){
|
|
72
|
+
if (comp.startsWith('$') || SILENT.has(comp)) continue;
|
|
73
|
+
if (!value || typeof value != 'object') continue;
|
|
74
|
+
for (let [prop, v] of Object.entries(value)){
|
|
75
|
+
if (prop == 'via') continue;
|
|
76
|
+
for (let to of Array.isArray(v) ? v : [
|
|
77
|
+
v
|
|
78
|
+
]){
|
|
79
|
+
if (typeof to == 'string') {
|
|
80
|
+
add({
|
|
81
|
+
from: b.entity.eid,
|
|
82
|
+
to,
|
|
83
|
+
name: `${comp}.${prop}`
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return [
|
|
91
|
+
...out.values()
|
|
92
|
+
];
|
|
93
|
+
};
|
|
94
|
+
// How far a shape of half size w, h reaches from its centre toward (dx, dy).
|
|
95
|
+
let out = (w, h, dx, dy)=>{
|
|
96
|
+
let l = Math.hypot(dx, dy) || 1;
|
|
97
|
+
return Math.min(w / (Math.abs(dx) / l || 1e-9), h / (Math.abs(dy) / l || 1e-9));
|
|
98
|
+
};
|
|
99
|
+
// Whether two boxes [x0, y0, x1, y1] overlap.
|
|
100
|
+
let over = (a, b)=>a[0] < b[2] && b[0] < a[2] && a[1] < b[3] && b[1] < a[3];
|
|
101
|
+
// Whether the segment a–b crosses any stretch of a route, checked stretch by
|
|
102
|
+
// stretch only when their bounds meet.
|
|
103
|
+
let crossing = ([ax, ay], [bx, by], r)=>{
|
|
104
|
+
let x0 = Math.min(ax, bx), x1 = Math.max(ax, bx);
|
|
105
|
+
let y0 = Math.min(ay, by), y1 = Math.max(ay, by);
|
|
106
|
+
let [rx0, ry0, rx1, ry1] = r.box;
|
|
107
|
+
if (x1 < rx0 || x0 > rx1 || y1 < ry0 || y0 > ry1) return false;
|
|
108
|
+
let { pts } = r;
|
|
109
|
+
for(let i = 1; i < pts.length; i++){
|
|
110
|
+
let [px, py] = pts[i - 1], [qx, qy] = pts[i];
|
|
111
|
+
if (Math.max(px, qx) < x0 || Math.min(px, qx) > x1) continue;
|
|
112
|
+
if (Math.max(py, qy) < y0 || Math.min(py, qy) > y1) continue;
|
|
113
|
+
if (cross([
|
|
114
|
+
ax,
|
|
115
|
+
ay
|
|
116
|
+
], [
|
|
117
|
+
bx,
|
|
118
|
+
by
|
|
119
|
+
], pts[i - 1], pts[i])) return true;
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
123
|
+
// The shapes already placed, filed by band so a spot is checked only against
|
|
124
|
+
// its neighbours. A band is taller than any shape, so a shape can only meet
|
|
125
|
+
// those of its own band and the two beside it.
|
|
126
|
+
const BAND = 2 * 44 + APART;
|
|
127
|
+
let bands = ()=>{
|
|
128
|
+
let rows = new Map();
|
|
129
|
+
let band = (y)=>Math.floor(y / BAND);
|
|
130
|
+
return {
|
|
131
|
+
/** Every row above this is full. */ full: 0,
|
|
132
|
+
add: (p)=>{
|
|
133
|
+
let k = band(p[1]);
|
|
134
|
+
rows.set(k, [
|
|
135
|
+
...rows.get(k) ?? [],
|
|
136
|
+
p
|
|
137
|
+
]);
|
|
138
|
+
},
|
|
139
|
+
// How much a shape of half size w, h at p would cover those placed, with
|
|
140
|
+
// the room kept between two shapes.
|
|
141
|
+
covered: ([x, y], w, h)=>{
|
|
142
|
+
let c = 0;
|
|
143
|
+
for(let k = band(y) - 1; k <= band(y) + 1; k++){
|
|
144
|
+
for (let [bx, by, bw, bh] of rows.get(k) ?? []){
|
|
145
|
+
let ox = w + bw + APART - Math.abs(x - bx);
|
|
146
|
+
let oy = h + bh + APART - Math.abs(y - by);
|
|
147
|
+
if (ox > 0 && oy > 0) c += 1e5 + ox * oy;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return c;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
// What makes a drawing hard to read costs about as much as a line twice too
|
|
155
|
+
// long: a line through a shape, two lines crossing, and two lines from the
|
|
156
|
+
// same shape at nearly the same angle, running over each other.
|
|
157
|
+
const THROUGH = 1500, CROSS = 700, OVER = 700;
|
|
158
|
+
// A line is best about LINK long between the shapes' rims, never shorter than
|
|
159
|
+
// SHORT, which leaves room for its name, and a shape is better with ROOMY
|
|
160
|
+
// around it, so a drawing spreads over its sheet.
|
|
161
|
+
const LINK = 110, SHORT = 70, ROOMY = 70;
|
|
162
|
+
// The sheet's middle pulls a little, so a drawing gathers there, and an
|
|
163
|
+
// actor keeps to its side. A spot from which a placed shape cannot be reached
|
|
164
|
+
// without crossing a line or a shape costs VIS for each, so what is placed
|
|
165
|
+
// stays open to the lines still to come.
|
|
166
|
+
const PULL = .4, VIS = 600;
|
|
167
|
+
// The room kept between a shape and the edge of the sheet, for the lines
|
|
168
|
+
// that bend around it.
|
|
169
|
+
const EDGE = 24;
|
|
170
|
+
// The cost of a newcomer at p. Overlapping a shape is never worth it, and
|
|
171
|
+
// below the sheet's bottom it costs more, so the sheet grows only when full.
|
|
172
|
+
// Its lines are reckoned straight, as most are drawn; `routed` leaves out all
|
|
173
|
+
// but their length, for a spot whose lines are drawn and paid for apart.
|
|
174
|
+
let cost = (p, s, routed = false)=>{
|
|
175
|
+
let [x, y] = p;
|
|
176
|
+
let c = s.boxes.covered(p, s.w, s.h);
|
|
177
|
+
if (c) return c;
|
|
178
|
+
if (y > s.height - s.h - 8) c += 400 + (y - s.height + s.h);
|
|
179
|
+
let mine = [
|
|
180
|
+
x - s.w - 6,
|
|
181
|
+
y - s.h - 6,
|
|
182
|
+
x + s.w + 6,
|
|
183
|
+
y + s.h + 6
|
|
184
|
+
];
|
|
185
|
+
for (let r of s.routes){
|
|
186
|
+
if (!over(r.box, mine)) continue;
|
|
187
|
+
if (r.pts.some((q, i)=>i > 0 && meets(r.pts[i - 1], q, p, s.w + 6, s.h + 6))) {
|
|
188
|
+
c += THROUGH;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
for (let n of s.reach){
|
|
192
|
+
let [nx, ny] = n.at, dx = nx - x, dy = ny - y;
|
|
193
|
+
let len = Math.hypot(dx, dy) - out(s.w, s.h, dx, dy) - out(n.w, n.h, dx, dy);
|
|
194
|
+
c += Math.abs(len - LINK) * 1.5 + Math.max(0, SHORT - len) * 20;
|
|
195
|
+
if (routed) continue;
|
|
196
|
+
for (let r of s.routes){
|
|
197
|
+
if (r.from != n.eid && r.to != n.eid) {
|
|
198
|
+
if (crossing(p, n.at, r)) c += CROSS;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
// Where a line from the same neighbour heads as it leaves it.
|
|
202
|
+
let [ox, oy] = r.from == n.eid ? r.pts[2] : r.pts[r.pts.length - 3];
|
|
203
|
+
let ux = ox - nx, uy = oy - ny;
|
|
204
|
+
let cos = (-dx * ux - dy * uy) / (Math.hypot(dx, dy) * Math.hypot(ux, uy) || 1);
|
|
205
|
+
if (cos > .96) c += OVER;
|
|
206
|
+
}
|
|
207
|
+
for (let o of s.shapes){
|
|
208
|
+
if (o.eid != n.eid && meets(p, n.at, [
|
|
209
|
+
o[0],
|
|
210
|
+
o[1]
|
|
211
|
+
], o[2] + 6, o[3] + 6)) {
|
|
212
|
+
c += THROUGH;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (let [ox, oy, ow, oh] of s.shapes){
|
|
217
|
+
let gap = Math.max(Math.abs(x - ox) - s.w - ow, Math.abs(y - oy) - s.h - oh);
|
|
218
|
+
if (gap < ROOMY) c += (ROOMY - gap) * 2;
|
|
219
|
+
}
|
|
220
|
+
return c + (s.side == null ? Math.abs(x - s.width / 2) * PULL : Math.abs(x - s.side) * 4) + Math.abs(y - s.height / 2) * PULL / 2;
|
|
221
|
+
};
|
|
222
|
+
// What a spot costs for the placed shapes it cannot reach in a straight line.
|
|
223
|
+
let blind = ([x, y], s)=>{
|
|
224
|
+
let c = 0;
|
|
225
|
+
for (let o of s.shapes){
|
|
226
|
+
if (s.reach.some((n)=>n.eid == o.eid)) continue;
|
|
227
|
+
let [ox, oy] = o;
|
|
228
|
+
let x0 = Math.min(x, ox), x1 = Math.max(x, ox);
|
|
229
|
+
let y0 = Math.min(y, oy), y1 = Math.max(y, oy);
|
|
230
|
+
let hidden = s.routes.some((r)=>r.from != o.eid && r.to != o.eid && crossing([
|
|
231
|
+
x,
|
|
232
|
+
y
|
|
233
|
+
], [
|
|
234
|
+
ox,
|
|
235
|
+
oy
|
|
236
|
+
], r)) || s.shapes.some(([qx, qy, qw, qh], i)=>s.shapes[i] != o && qx + qw + 16 > x0 && qx - qw - 16 < x1 && qy + qh + 16 > y0 && qy - qh - 16 < y1 && meets([
|
|
237
|
+
x,
|
|
238
|
+
y
|
|
239
|
+
], [
|
|
240
|
+
ox,
|
|
241
|
+
oy
|
|
242
|
+
], [
|
|
243
|
+
qx,
|
|
244
|
+
qy
|
|
245
|
+
], qw + 16, qh + 16));
|
|
246
|
+
if (hidden) {
|
|
247
|
+
c += VIS;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return c;
|
|
251
|
+
};
|
|
252
|
+
// The spots a newcomer may take, the one it takes first: the cheapest within
|
|
253
|
+
// a reach of its neighbours (or of the sheet's middle), or else the first free
|
|
254
|
+
// one, from the top down and nearest that middle across.
|
|
255
|
+
let place = (s)=>{
|
|
256
|
+
let left = s.w + EDGE, right = s.width - s.w - EDGE, top = s.h + EDGE;
|
|
257
|
+
let n = s.reach.length;
|
|
258
|
+
let [ax, ay] = n ? s.reach.reduce(([x, y], { at: [a, b] })=>[
|
|
259
|
+
x + a / n,
|
|
260
|
+
y + b / n
|
|
261
|
+
], [
|
|
262
|
+
0,
|
|
263
|
+
0
|
|
264
|
+
]) : [
|
|
265
|
+
s.width / 2,
|
|
266
|
+
s.height / 2
|
|
267
|
+
];
|
|
268
|
+
let r = LINK + 3 * s.w + 60;
|
|
269
|
+
let x0 = Math.max(left, ax - r), x1 = Math.min(right, ax + r);
|
|
270
|
+
let y0 = Math.max(top, ay - r), y1 = Math.max(y0, ay + r);
|
|
271
|
+
// Only what meets the window, or the lines to it, matters to a spot in it.
|
|
272
|
+
let xs = s.reach.map((n)=>n.at[0]), ys = s.reach.map((n)=>n.at[1]);
|
|
273
|
+
let win = [
|
|
274
|
+
Math.min(x0, ...xs) - s.w - ROOMY,
|
|
275
|
+
Math.min(y0, ...ys) - s.h - ROOMY,
|
|
276
|
+
Math.max(x1, ...xs) + s.w + ROOMY,
|
|
277
|
+
Math.max(y1, ...ys) + s.h + ROOMY
|
|
278
|
+
];
|
|
279
|
+
let near = {
|
|
280
|
+
...s,
|
|
281
|
+
routes: s.routes.filter((r)=>over(r.box, win)),
|
|
282
|
+
shapes: s.shapes.filter(([x, y, w, h])=>over([
|
|
283
|
+
x - w,
|
|
284
|
+
y - h,
|
|
285
|
+
x + w,
|
|
286
|
+
y + h
|
|
287
|
+
], win))
|
|
288
|
+
};
|
|
289
|
+
// A coarse pass over the window, coarser among more shapes; the spots it
|
|
290
|
+
// finds nearly as good as its best, weighed for what they cannot reach; and
|
|
291
|
+
// a fine pass around the best few of those.
|
|
292
|
+
let spots = [];
|
|
293
|
+
let scan = (xa, xb, ya, yb, dx, dy)=>{
|
|
294
|
+
for(let x = Math.max(x0, xa); x <= Math.min(x1, xb); x += dx){
|
|
295
|
+
for(let y = Math.max(y0, ya); y <= Math.min(y1, yb); y += dy){
|
|
296
|
+
spots.push([
|
|
297
|
+
cost([
|
|
298
|
+
x,
|
|
299
|
+
y
|
|
300
|
+
], near),
|
|
301
|
+
x,
|
|
302
|
+
y
|
|
303
|
+
]);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
let k = Math.max(1, Math.sqrt(s.shapes.length / 12));
|
|
308
|
+
let sx = 20 * k, sy = 14 * k;
|
|
309
|
+
scan(x0, x1, y0, y1, sx, sy);
|
|
310
|
+
spots.sort((a, b)=>a[0] - b[0]);
|
|
311
|
+
let close = spots.filter(([c])=>c < 1e5 && c < spots[0][0] + 4 * VIS);
|
|
312
|
+
let every = Math.ceil(close.length / 60);
|
|
313
|
+
let few = close.filter((_, i)=>i % every == 0).map(([c, x, y])=>[
|
|
314
|
+
c + blind([
|
|
315
|
+
x,
|
|
316
|
+
y
|
|
317
|
+
], near),
|
|
318
|
+
x,
|
|
319
|
+
y,
|
|
320
|
+
c
|
|
321
|
+
]).sort((a, b)=>a[0] - b[0]);
|
|
322
|
+
// The best few places apart from each other, each refined, and the one
|
|
323
|
+
// whose lines, drawn, cost least.
|
|
324
|
+
let apart = few.reduce((kept, f)=>kept.length < 4 && kept.every(([, x, y])=>Math.hypot(f[1] - x, f[2] - y) > 60) ? [
|
|
325
|
+
...kept,
|
|
326
|
+
f
|
|
327
|
+
] : kept, []);
|
|
328
|
+
let weighed = [];
|
|
329
|
+
let drawn = (x, y, s)=>{
|
|
330
|
+
let [lines, knotted] = s.lines([
|
|
331
|
+
x,
|
|
332
|
+
y
|
|
333
|
+
]);
|
|
334
|
+
let c = cost([
|
|
335
|
+
x,
|
|
336
|
+
y
|
|
337
|
+
], s, true) + blind([
|
|
338
|
+
x,
|
|
339
|
+
y
|
|
340
|
+
], s) + lines;
|
|
341
|
+
weighed.push([
|
|
342
|
+
c,
|
|
343
|
+
x,
|
|
344
|
+
y,
|
|
345
|
+
knotted
|
|
346
|
+
]);
|
|
347
|
+
};
|
|
348
|
+
for (let [, fx, fy, base] of apart){
|
|
349
|
+
spots = [
|
|
350
|
+
[
|
|
351
|
+
base,
|
|
352
|
+
fx,
|
|
353
|
+
fy
|
|
354
|
+
]
|
|
355
|
+
];
|
|
356
|
+
scan(fx - sx, fx + sx, fy - sy, fy + sy, 5 * k, 4 * k);
|
|
357
|
+
let [[, x, y]] = spots.sort((a, b)=>a[0] - b[0]);
|
|
358
|
+
drawn(x, y, near);
|
|
359
|
+
}
|
|
360
|
+
if (weighed.length) {
|
|
361
|
+
return weighed.sort((a, b)=>a[0] - b[0]).map(([, x, y, knotted])=>({
|
|
362
|
+
at: [
|
|
363
|
+
x,
|
|
364
|
+
y
|
|
365
|
+
],
|
|
366
|
+
knotted
|
|
367
|
+
}));
|
|
368
|
+
}
|
|
369
|
+
let across = [];
|
|
370
|
+
for(let x = left; x <= right; x += s.w / 2)across.push(x);
|
|
371
|
+
across.sort((a, b)=>Math.abs(a - ax) - Math.abs(b - ax));
|
|
372
|
+
// Shapes are only ever added, so a row found full stays full.
|
|
373
|
+
for(let y = Math.max(top, s.boxes.full);; y += s.h, s.boxes.full = y){
|
|
374
|
+
for (let x of across){
|
|
375
|
+
if (!s.boxes.covered([
|
|
376
|
+
x,
|
|
377
|
+
y
|
|
378
|
+
], s.w, s.h)) {
|
|
379
|
+
return [
|
|
380
|
+
{
|
|
381
|
+
at: [
|
|
382
|
+
x,
|
|
383
|
+
y
|
|
384
|
+
],
|
|
385
|
+
knotted: 0
|
|
386
|
+
}
|
|
387
|
+
];
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
// The points a line is reckoned through.
|
|
393
|
+
const STEPS = 12;
|
|
394
|
+
let traced = (from, to, c)=>{
|
|
395
|
+
let pts = Array.from({
|
|
396
|
+
length: STEPS + 1
|
|
397
|
+
}, (_, i)=>on(c, i / STEPS));
|
|
398
|
+
let xs = pts.map((p)=>p[0]), ys = pts.map((p)=>p[1]);
|
|
399
|
+
return {
|
|
400
|
+
from,
|
|
401
|
+
to,
|
|
402
|
+
pts,
|
|
403
|
+
box: [
|
|
404
|
+
Math.min(...xs),
|
|
405
|
+
Math.min(...ys),
|
|
406
|
+
Math.max(...xs),
|
|
407
|
+
Math.max(...ys)
|
|
408
|
+
]
|
|
409
|
+
};
|
|
410
|
+
};
|
|
411
|
+
// Where along a line its name may be written: the middle first, then each
|
|
412
|
+
// way from it.
|
|
413
|
+
const MIDS = [
|
|
414
|
+
.5,
|
|
415
|
+
.4,
|
|
416
|
+
.6,
|
|
417
|
+
.3,
|
|
418
|
+
.7,
|
|
419
|
+
.22,
|
|
420
|
+
.78
|
|
421
|
+
];
|
|
422
|
+
// How far aside a line may bend, at its middle: straight first, then each
|
|
423
|
+
// way, further each time.
|
|
424
|
+
const BENDS = [
|
|
425
|
+
0,
|
|
426
|
+
28,
|
|
427
|
+
-28,
|
|
428
|
+
56,
|
|
429
|
+
-56,
|
|
430
|
+
90,
|
|
431
|
+
-90,
|
|
432
|
+
130,
|
|
433
|
+
-130,
|
|
434
|
+
180,
|
|
435
|
+
-180,
|
|
436
|
+
240,
|
|
437
|
+
-240
|
|
438
|
+
];
|
|
439
|
+
// The bend a new line takes between two placed shapes: the one that runs
|
|
440
|
+
// through no other shape, crosses fewest lines and keeps clear of the lines
|
|
441
|
+
// beside it, bending no more than it must.
|
|
442
|
+
let route = (a, b, shapes, routes, [width, height])=>{
|
|
443
|
+
let chord = Math.hypot(b.x - a.x, b.y - a.y);
|
|
444
|
+
let best;
|
|
445
|
+
for (let bend of BENDS){
|
|
446
|
+
if (bend && Math.abs(bend) > Math.max(chord, 60)) continue;
|
|
447
|
+
let r = traced(a.eid, b.eid, curve(a, b, bend));
|
|
448
|
+
let c = Math.abs(bend) * 2;
|
|
449
|
+
if (best && c >= best[0]) break;
|
|
450
|
+
let [x0, y0, x1, y1] = r.box;
|
|
451
|
+
if (x0 < 16 || y0 < 16 || x1 > width - 16 || y1 > height - 16) c += THROUGH;
|
|
452
|
+
for (let o of shapes){
|
|
453
|
+
let [x, y, w, h] = o;
|
|
454
|
+
if (o.eid == a.eid || o.eid == b.eid) continue;
|
|
455
|
+
if (!over(r.box, [
|
|
456
|
+
x - w - 6,
|
|
457
|
+
y - h - 6,
|
|
458
|
+
x + w + 6,
|
|
459
|
+
y + h + 6
|
|
460
|
+
])) continue;
|
|
461
|
+
if (r.pts.some((p, i)=>i > 0 && meets(r.pts[i - 1], p, [
|
|
462
|
+
x,
|
|
463
|
+
y
|
|
464
|
+
], w + 6, h + 6))) {
|
|
465
|
+
c += THROUGH;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
for (let o of routes){
|
|
469
|
+
if (!over(r.box, o.box)) continue;
|
|
470
|
+
let shared = o.from == a.eid || o.from == b.eid || o.to == a.eid || o.to == b.eid;
|
|
471
|
+
if (!shared) {
|
|
472
|
+
if (r.pts.some((p, i)=>i > 0 && crossing(r.pts[i - 1], p, o))) {
|
|
473
|
+
c += CROSS;
|
|
474
|
+
}
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
let inner = (pts)=>pts.slice(2, -2);
|
|
478
|
+
let close = inner(r.pts).some(([x, y])=>inner(o.pts).some(([u, v])=>Math.hypot(x - u, y - v) < 12));
|
|
479
|
+
if (close) c += OVER;
|
|
480
|
+
}
|
|
481
|
+
if (!best || c < best[0]) best = [
|
|
482
|
+
c,
|
|
483
|
+
bend,
|
|
484
|
+
r
|
|
485
|
+
];
|
|
486
|
+
}
|
|
487
|
+
return [
|
|
488
|
+
best[1],
|
|
489
|
+
best[2],
|
|
490
|
+
best[0]
|
|
491
|
+
];
|
|
492
|
+
};
|
|
493
|
+
// The sheet a selection is laid out on: the one asked for, or, when the
|
|
494
|
+
// shapes and the room around them would not fit it, one as many times larger
|
|
495
|
+
// across as down, so a big graph fills a drawing of the same shape instead of
|
|
496
|
+
// running down a column of it.
|
|
497
|
+
const ROOM = 2.4;
|
|
498
|
+
let fitted = (width, height, sizes)=>{
|
|
499
|
+
let need = sizes.reduce((a, [w, h])=>a + (2 * w + APART) * (2 * h + APART), 0);
|
|
500
|
+
let grow = Math.max(1, Math.sqrt(need * ROOM / (width * height)));
|
|
501
|
+
return [
|
|
502
|
+
width * grow,
|
|
503
|
+
height * grow
|
|
504
|
+
];
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* Lay out a selection. Entities in `was` keep their places and order, and
|
|
508
|
+
* lines between them their bends; each new one takes the cheapest free spot,
|
|
509
|
+
* clear of the lines and shapes there, and each new line bends around what is
|
|
510
|
+
* in its way. When a newcomer has no spot clear of tangles, an entity in its
|
|
511
|
+
* way moves as little as it must, and says where it was in `moved`. A
|
|
512
|
+
* selection too big for the sheet gets a larger one of the same shape, and
|
|
513
|
+
* the sheet grows downward when nothing on it is free.
|
|
514
|
+
*
|
|
515
|
+
* ```ts
|
|
516
|
+
* import { layout } from '@yaks/diagram'
|
|
517
|
+
* import { equal } from '@yaks/testing'
|
|
518
|
+
*
|
|
519
|
+
* let a = { entity: { eid: '0a' }, doc: { title: 'Buy the cake' } }
|
|
520
|
+
* let b = { entity: { eid: '0b' }, comment: { target: '0a' } }
|
|
521
|
+
* let drawn = layout([a, b])
|
|
522
|
+
* equal(drawn.links.map((l) => l.name), ['comment.target'])
|
|
523
|
+
* equal(layout([a, b], { was: drawn }).nodes[0].x, drawn.nodes[0].x)
|
|
524
|
+
* ```
|
|
525
|
+
*/ export let layout = (bundles, opts = {})=>{
|
|
526
|
+
let [still, spots] = arrange(bundles, opts, new Map());
|
|
527
|
+
let tries = Math.floor(WORK / still.nodes.length ** 2);
|
|
528
|
+
if (!opts.was || !tries) return still;
|
|
529
|
+
// A newcomer that found no spot clear of tangles makes room. Its next best
|
|
530
|
+
// spots are tried, and then what is in its way is moved, a little and then
|
|
531
|
+
// further, one entity at a time. The first arrangement where nothing that
|
|
532
|
+
// joined or moved is tangled wins, or else the one that tangles least
|
|
533
|
+
// (counting only what changed, which is all that can differ), then moves
|
|
534
|
+
// least. In its way is what its tangles involve, and what names two of the
|
|
535
|
+
// entities it names, and so wants the spot it wants.
|
|
536
|
+
let fresh = new Set([
|
|
537
|
+
...still.nodes,
|
|
538
|
+
...still.links
|
|
539
|
+
].filter((x)=>x.fresh).map((x)=>'key' in x ? x.key : x.eid));
|
|
540
|
+
let tangled = knots(still, fresh);
|
|
541
|
+
if (!tangled.length) return still;
|
|
542
|
+
let ends = new Map(still.links.map((l)=>[
|
|
543
|
+
l.key,
|
|
544
|
+
[
|
|
545
|
+
l.from,
|
|
546
|
+
l.to
|
|
547
|
+
]
|
|
548
|
+
]));
|
|
549
|
+
let around = new Map();
|
|
550
|
+
for (let l of still.links){
|
|
551
|
+
for (let [a, b] of [
|
|
552
|
+
[
|
|
553
|
+
l.from,
|
|
554
|
+
l.to
|
|
555
|
+
],
|
|
556
|
+
[
|
|
557
|
+
l.to,
|
|
558
|
+
l.from
|
|
559
|
+
]
|
|
560
|
+
]){
|
|
561
|
+
around.set(a, (around.get(a) ?? new Set()).add(b));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
let involved = new Set(tangled.flatMap((k)=>k.keys.flatMap((key)=>ends.get(key) ?? [
|
|
565
|
+
key
|
|
566
|
+
])));
|
|
567
|
+
let newcomer = still.nodes.filter((n)=>fresh.has(n.eid) && involved.has(n.eid)).sort((a, b)=>a.order - b.order)[0]?.eid;
|
|
568
|
+
let rivals = [
|
|
569
|
+
...involved
|
|
570
|
+
].filter((e)=>fresh.has(e)).flatMap((e)=>{
|
|
571
|
+
let mine = around.get(e) ?? new Set();
|
|
572
|
+
return [
|
|
573
|
+
...around
|
|
574
|
+
].filter(([o, near])=>o != e && [
|
|
575
|
+
...near
|
|
576
|
+
].filter((n)=>mine.has(n)).length >= 2).map(([o])=>o);
|
|
577
|
+
});
|
|
578
|
+
let old = new Map(opts.was.nodes.map((n)=>[
|
|
579
|
+
n.eid,
|
|
580
|
+
n
|
|
581
|
+
]));
|
|
582
|
+
let lined = (e)=>around.get(e)?.size ?? 0;
|
|
583
|
+
let movable = [
|
|
584
|
+
...new Set([
|
|
585
|
+
...rivals,
|
|
586
|
+
...involved
|
|
587
|
+
])
|
|
588
|
+
].filter((e)=>old.has(e) && !fresh.has(e)).sort((a, b)=>lined(a) - lined(b)).slice(0, MOVES);
|
|
589
|
+
// An arrangement, what it tangles of what changed, how many more tangles
|
|
590
|
+
// that makes than before, and how far it moves what was drawn. What was
|
|
591
|
+
// tangled before depends only on what moved.
|
|
592
|
+
let before = new Map();
|
|
593
|
+
let score = (l)=>{
|
|
594
|
+
let moved = new Set(l.nodes.filter((n)=>n.moved).map((n)=>n.eid));
|
|
595
|
+
let changed = new Set([
|
|
596
|
+
...fresh,
|
|
597
|
+
...moved,
|
|
598
|
+
...l.links.filter((k)=>moved.has(k.from) || moved.has(k.to)).map((k)=>k.key)
|
|
599
|
+
]);
|
|
600
|
+
let left = knots(l, changed).length;
|
|
601
|
+
let key = [
|
|
602
|
+
...moved
|
|
603
|
+
].sort().join();
|
|
604
|
+
if (!before.has(key)) before.set(key, knots(still, changed).length);
|
|
605
|
+
return [
|
|
606
|
+
l,
|
|
607
|
+
left,
|
|
608
|
+
left - before.get(key),
|
|
609
|
+
l.nodes.reduce((d, n)=>d + (n.moved ? Math.hypot(n.x - n.moved.x, n.y - n.moved.y) : 0), 0)
|
|
610
|
+
];
|
|
611
|
+
};
|
|
612
|
+
// Where the newcomer is tried with each move: at every spot it could take,
|
|
613
|
+
// those whose lines route clear first, and then along the foot of the
|
|
614
|
+
// drawing, nearest across to what it names, since what is outside
|
|
615
|
+
// everything can reach round it. Every entity is pinned, so an arrangement
|
|
616
|
+
// only draws its lines again.
|
|
617
|
+
let me = still.nodes.find((n)=>n.eid == newcomer);
|
|
618
|
+
let [w, h] = me ? half(me.shape, !!me.state) : [
|
|
619
|
+
0,
|
|
620
|
+
0
|
|
621
|
+
];
|
|
622
|
+
let named = [
|
|
623
|
+
...around.get(newcomer ?? '') ?? []
|
|
624
|
+
].map((e)=>still.nodes.find((n)=>n.eid == e).x);
|
|
625
|
+
let ax = named.reduce((a, x)=>a + x / named.length, 0);
|
|
626
|
+
let across = [];
|
|
627
|
+
for(let x = w + EDGE; me && x <= still.width - w - EDGE; x += w * .7){
|
|
628
|
+
across.push(x);
|
|
629
|
+
}
|
|
630
|
+
across = across.sort((a, b)=>Math.abs(a - ax) - Math.abs(b - ax)).slice(0, FOOT);
|
|
631
|
+
let [own, ...others] = spots.get(newcomer ?? '') ?? [];
|
|
632
|
+
let tried = [
|
|
633
|
+
own,
|
|
634
|
+
...others.sort((a, b)=>a.knotted - b.knotted)
|
|
635
|
+
].filter(Boolean).map((o)=>o.at);
|
|
636
|
+
let at = (pins)=>{
|
|
637
|
+
let l = {
|
|
638
|
+
...still,
|
|
639
|
+
nodes: still.nodes.map((n)=>{
|
|
640
|
+
let p = pins.get(n.eid);
|
|
641
|
+
return p ? {
|
|
642
|
+
...n,
|
|
643
|
+
x: p[0],
|
|
644
|
+
y: p[1]
|
|
645
|
+
} : n;
|
|
646
|
+
})
|
|
647
|
+
};
|
|
648
|
+
let foot = Math.max(...l.nodes.filter((n)=>n.eid != newcomer).map((n)=>n.y + half(n.shape, !!n.state)[1] + APART));
|
|
649
|
+
let under = across.flatMap((x)=>[
|
|
650
|
+
foot - h,
|
|
651
|
+
foot,
|
|
652
|
+
foot + h
|
|
653
|
+
].map((y)=>[
|
|
654
|
+
x,
|
|
655
|
+
y
|
|
656
|
+
]));
|
|
657
|
+
return [
|
|
658
|
+
...tried,
|
|
659
|
+
...under
|
|
660
|
+
].filter((p)=>!blocked(l, newcomer, p));
|
|
661
|
+
};
|
|
662
|
+
let best = [
|
|
663
|
+
still,
|
|
664
|
+
tangled.length,
|
|
665
|
+
0,
|
|
666
|
+
0
|
|
667
|
+
];
|
|
668
|
+
// Whether an arrangement is untangled, or the tries are spent, keeping the
|
|
669
|
+
// best so far.
|
|
670
|
+
let done = (pins)=>{
|
|
671
|
+
let s = score(arrange(bundles, opts, pins)[0]);
|
|
672
|
+
if (s[2] < best[2] || s[2] == best[2] && s[3] < best[3]) best = s;
|
|
673
|
+
return !s[1] || --tries <= 0;
|
|
674
|
+
};
|
|
675
|
+
let move = (pins)=>newcomer ? at(pins).some((p)=>(pins.size || p != own?.at) && done(new Map([
|
|
676
|
+
...pins,
|
|
677
|
+
[
|
|
678
|
+
newcomer,
|
|
679
|
+
p
|
|
680
|
+
]
|
|
681
|
+
]))) : done(pins);
|
|
682
|
+
if (newcomer && move(new Map())) return best[0];
|
|
683
|
+
// Each entity is moved out of the newcomer's way first: away from the spot
|
|
684
|
+
// it took, then aside, and toward it last.
|
|
685
|
+
let ways = Array.from({
|
|
686
|
+
length: 8
|
|
687
|
+
}, (_, a)=>a * Math.PI / 4);
|
|
688
|
+
for (let r of NUDGES){
|
|
689
|
+
for (let e of movable){
|
|
690
|
+
let o = old.get(e);
|
|
691
|
+
let [fx, fy] = own?.at ?? [
|
|
692
|
+
o.x,
|
|
693
|
+
o.y
|
|
694
|
+
];
|
|
695
|
+
let away = Math.atan2(o.y - fy, o.x - fx);
|
|
696
|
+
let off = (a)=>Math.abs(Math.atan2(Math.sin(a - away), Math.cos(a - away)));
|
|
697
|
+
for (let a of [
|
|
698
|
+
...ways
|
|
699
|
+
].sort((p, q)=>off(p) - off(q))){
|
|
700
|
+
let p = [
|
|
701
|
+
o.x + r * Math.cos(a),
|
|
702
|
+
o.y + r * Math.sin(a)
|
|
703
|
+
];
|
|
704
|
+
if (!blocked(still, e, p) && move(new Map([
|
|
705
|
+
[
|
|
706
|
+
e,
|
|
707
|
+
p
|
|
708
|
+
]
|
|
709
|
+
]))) return best[0];
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return best[0];
|
|
714
|
+
};
|
|
715
|
+
// Whether the shape of `eid` at p would sit on the edge of a layout's sheet,
|
|
716
|
+
// or on another of its shapes.
|
|
717
|
+
let blocked = (l, eid, [x, y])=>{
|
|
718
|
+
let size = (n)=>half(n.shape, !!n.state);
|
|
719
|
+
let [w, h] = size(l.nodes.find((n)=>n.eid == eid));
|
|
720
|
+
return x < w + EDGE || x > l.width - w - EDGE || y < h + EDGE || l.nodes.some((n)=>{
|
|
721
|
+
let [nw, nh] = size(n);
|
|
722
|
+
return n.eid != eid && Math.abs(n.x - x) < w + nw + APART && Math.abs(n.y - y) < h + nh + APART;
|
|
723
|
+
});
|
|
724
|
+
};
|
|
725
|
+
// How far a shape already drawn is tried away from where it was, nearest
|
|
726
|
+
// first, so it moves no further than it must. Each arrangement tried lays out
|
|
727
|
+
// and checks every entity again, which costs more the more there are, so a
|
|
728
|
+
// drawing of n tries at most WORK / n² of them: 40 of ten, 10 of twenty, and
|
|
729
|
+
// none past 63.
|
|
730
|
+
const NUDGES = [
|
|
731
|
+
40,
|
|
732
|
+
80,
|
|
733
|
+
120
|
|
734
|
+
];
|
|
735
|
+
const WORK = 4000;
|
|
736
|
+
// How many spots across the foot of the drawing the newcomer is tried at.
|
|
737
|
+
const FOOT = 4;
|
|
738
|
+
// How many entities already drawn are tried in a new place, the fewest lined
|
|
739
|
+
// first.
|
|
740
|
+
const MOVES = 4;
|
|
741
|
+
// A layout with each entity of `moving` at the spot it gives: one drawn
|
|
742
|
+
// before keeps its order and stage, and the lines to it are drawn again. With
|
|
743
|
+
// it, the spots each other newcomer could take, the one it took first.
|
|
744
|
+
let arrange = (bundles, opts, moving)=>{
|
|
745
|
+
let { was, label = (b)=>handle(b.entity.eid), quiet = QUIET } = opts;
|
|
746
|
+
let { shapes = SHAPES, stage = 0 } = opts;
|
|
747
|
+
let old = new Map(was?.nodes.map((n)=>[
|
|
748
|
+
n.eid,
|
|
749
|
+
n
|
|
750
|
+
]));
|
|
751
|
+
let oldLinks = new Map(was?.links.map((l)=>[
|
|
752
|
+
l.key,
|
|
753
|
+
l
|
|
754
|
+
]));
|
|
755
|
+
let stays = (eid)=>old.has(eid) && !moving.has(eid);
|
|
756
|
+
let drawn = bundles.filter((b)=>!isEdge(b)).map((b)=>{
|
|
757
|
+
let comps = words(b, quiet);
|
|
758
|
+
let state = stateOf(b);
|
|
759
|
+
let shape = comps.map((c)=>shapes[c]).find(Boolean) ?? 'box';
|
|
760
|
+
return {
|
|
761
|
+
b,
|
|
762
|
+
comps,
|
|
763
|
+
state,
|
|
764
|
+
shape,
|
|
765
|
+
size: half(shape, !!state)
|
|
766
|
+
};
|
|
767
|
+
});
|
|
768
|
+
let [width, height] = fitted(opts.width ?? was?.width ?? 640, opts.height ?? was?.height ?? 480, drawn.map((d)=>d.size));
|
|
769
|
+
let lines = links(bundles);
|
|
770
|
+
let spot = new Map();
|
|
771
|
+
let boxes = bands();
|
|
772
|
+
// The sheet as tall as it has grown: a line below the height asked for is
|
|
773
|
+
// still on it.
|
|
774
|
+
let bottom = height;
|
|
775
|
+
let sheet = (y = 0, h = 0)=>[
|
|
776
|
+
width,
|
|
777
|
+
Math.max(bottom, y + h + APART)
|
|
778
|
+
];
|
|
779
|
+
let put = (eid, p, [w, h])=>{
|
|
780
|
+
spot.set(eid, p);
|
|
781
|
+
boxes.add([
|
|
782
|
+
...p,
|
|
783
|
+
w,
|
|
784
|
+
h
|
|
785
|
+
]);
|
|
786
|
+
bottom = Math.max(bottom, p[1] + h + APART);
|
|
787
|
+
};
|
|
788
|
+
for (let { b, size } of drawn){
|
|
789
|
+
let eid = b.entity.eid, o = old.get(eid);
|
|
790
|
+
if (o) put(eid, moving.get(eid) ?? [
|
|
791
|
+
o.x,
|
|
792
|
+
o.y
|
|
793
|
+
], size);
|
|
794
|
+
}
|
|
795
|
+
for (let { b, size } of drawn){
|
|
796
|
+
let eid = b.entity.eid;
|
|
797
|
+
if (spot.has(eid)) continue;
|
|
798
|
+
let pin = opts.at?.({
|
|
799
|
+
eid,
|
|
800
|
+
label: label(b),
|
|
801
|
+
bundle: b
|
|
802
|
+
});
|
|
803
|
+
let mine = moving.get(eid);
|
|
804
|
+
if (mine) put(eid, mine, size);
|
|
805
|
+
else if (pin) put(eid, [
|
|
806
|
+
pin[0] * width,
|
|
807
|
+
pin[1] * height
|
|
808
|
+
], size);
|
|
809
|
+
}
|
|
810
|
+
let byEid = new Map(bundles.map((b)=>[
|
|
811
|
+
b.entity.eid,
|
|
812
|
+
b
|
|
813
|
+
]));
|
|
814
|
+
let seat = new Map(drawn.map((d)=>[
|
|
815
|
+
d.b.entity.eid,
|
|
816
|
+
d
|
|
817
|
+
]));
|
|
818
|
+
let sizeOf = (eid)=>seat.get(eid).size;
|
|
819
|
+
let seated = (eid)=>{
|
|
820
|
+
let [x, y] = spot.get(eid), d = seat.get(eid);
|
|
821
|
+
return {
|
|
822
|
+
eid,
|
|
823
|
+
x,
|
|
824
|
+
y,
|
|
825
|
+
shape: d.shape,
|
|
826
|
+
state: d.state
|
|
827
|
+
};
|
|
828
|
+
};
|
|
829
|
+
let shapesPlaced = ()=>[
|
|
830
|
+
...spot
|
|
831
|
+
].map(([eid, [x, y]])=>Object.assign([
|
|
832
|
+
x,
|
|
833
|
+
y,
|
|
834
|
+
...sizeOf(eid)
|
|
835
|
+
], {
|
|
836
|
+
eid
|
|
837
|
+
}));
|
|
838
|
+
// Each line's bend once both its ends are placed, and what it is drawn
|
|
839
|
+
// through. A line both of whose ends were drawn before keeps its bend.
|
|
840
|
+
let bends = new Map();
|
|
841
|
+
let routes = [];
|
|
842
|
+
let kept = (l)=>stays(l.from) && stays(l.to) ? oldLinks.get(l.key) : undefined;
|
|
843
|
+
let draw = (l)=>{
|
|
844
|
+
let a = seated(l.from), b = seated(l.to);
|
|
845
|
+
let bend = kept(l)?.bend;
|
|
846
|
+
if (bend != null) {
|
|
847
|
+
bends.set(l.key, bend);
|
|
848
|
+
routes.push(traced(l.from, l.to, curve(a, b, bend)));
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
let [chosen, r] = route(a, b, shapesPlaced(), routes, sheet());
|
|
852
|
+
bends.set(l.key, chosen);
|
|
853
|
+
routes.push(r);
|
|
854
|
+
};
|
|
855
|
+
let ready = ()=>{
|
|
856
|
+
for (let l of lines){
|
|
857
|
+
if (!bends.has(l.key) && spot.has(l.from) && spot.has(l.to)) draw(l);
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
ready();
|
|
861
|
+
// Actors (people, personas, sessions) are what most lines run to, so each
|
|
862
|
+
// keeps to a side of the sheet, the emptier one, and the rest gather
|
|
863
|
+
// between them.
|
|
864
|
+
let actor = (shape)=>shape == 'round' || shape == 'hex';
|
|
865
|
+
let sideOf = (w)=>{
|
|
866
|
+
let xs = [
|
|
867
|
+
...spot
|
|
868
|
+
].filter(([e])=>actor(seat.get(e).shape)).map(([, [x]])=>x);
|
|
869
|
+
let lefts = xs.filter((x)=>x < width / 2).length;
|
|
870
|
+
return lefts <= xs.length - lefts ? w + EDGE : width - w - EDGE;
|
|
871
|
+
};
|
|
872
|
+
let joined = new Map();
|
|
873
|
+
for (let l of lines){
|
|
874
|
+
for (let e of [
|
|
875
|
+
l.from,
|
|
876
|
+
l.to
|
|
877
|
+
])joined.set(e, [
|
|
878
|
+
...joined.get(e) ?? [],
|
|
879
|
+
l
|
|
880
|
+
]);
|
|
881
|
+
}
|
|
882
|
+
let other = (l, eid)=>l.from == eid ? l.to : l.from;
|
|
883
|
+
let near = (eid)=>(joined.get(eid) ?? []).filter((l)=>spot.has(other(l, eid))).length;
|
|
884
|
+
// Newcomers are placed one at a time, the one with the most lines to what
|
|
885
|
+
// is already placed first (in the order given when even), so each lands
|
|
886
|
+
// beside what it names, and its lines are drawn before the next lands.
|
|
887
|
+
let waiting = drawn.filter(({ b })=>!spot.has(b.entity.eid));
|
|
888
|
+
let choices = new Map();
|
|
889
|
+
while(waiting.length){
|
|
890
|
+
let i = waiting.reduce((best, d, j)=>near(d.b.entity.eid) > near(waiting[best].b.entity.eid) ? j : best, 0);
|
|
891
|
+
let [{ b, size: [w, h] }] = waiting.splice(i, 1);
|
|
892
|
+
let eid = b.entity.eid;
|
|
893
|
+
let reach = (joined.get(eid) ?? []).flatMap((l)=>{
|
|
894
|
+
let o = other(l, eid), at = spot.get(o);
|
|
895
|
+
return at ? [
|
|
896
|
+
{
|
|
897
|
+
eid: o,
|
|
898
|
+
at,
|
|
899
|
+
w: sizeOf(o)[0],
|
|
900
|
+
h: sizeOf(o)[1]
|
|
901
|
+
}
|
|
902
|
+
] : [];
|
|
903
|
+
});
|
|
904
|
+
let shapes = shapesPlaced();
|
|
905
|
+
let d = seat.get(eid);
|
|
906
|
+
let side = actor(d.shape) ? sideOf(w) : undefined;
|
|
907
|
+
// Its lines to what is placed, drawn from p as they would be.
|
|
908
|
+
let mine = (joined.get(eid) ?? []).filter((l)=>spot.has(other(l, eid)));
|
|
909
|
+
let lines = ([x, y])=>{
|
|
910
|
+
let more = [
|
|
911
|
+
...routes
|
|
912
|
+
], knotted = 0;
|
|
913
|
+
let c = mine.reduce((c, l)=>{
|
|
914
|
+
let me = {
|
|
915
|
+
eid,
|
|
916
|
+
x,
|
|
917
|
+
y,
|
|
918
|
+
shape: d.shape,
|
|
919
|
+
state: d.state
|
|
920
|
+
};
|
|
921
|
+
let them = seated(other(l, eid));
|
|
922
|
+
let [bend, r, cost] = l.from == eid ? route(me, them, shapes, more, sheet(y, h)) : route(them, me, shapes, more, sheet(y, h));
|
|
923
|
+
more.push(r);
|
|
924
|
+
// What a line costs beyond its bend is a tangle.
|
|
925
|
+
if (cost - Math.abs(bend) * 2 >= Math.min(CROSS, OVER)) knotted++;
|
|
926
|
+
return c + cost;
|
|
927
|
+
}, 0);
|
|
928
|
+
return [
|
|
929
|
+
c,
|
|
930
|
+
knotted
|
|
931
|
+
];
|
|
932
|
+
};
|
|
933
|
+
let options = place({
|
|
934
|
+
boxes,
|
|
935
|
+
shapes,
|
|
936
|
+
routes,
|
|
937
|
+
reach,
|
|
938
|
+
width,
|
|
939
|
+
height,
|
|
940
|
+
w,
|
|
941
|
+
h,
|
|
942
|
+
side,
|
|
943
|
+
lines
|
|
944
|
+
});
|
|
945
|
+
choices.set(eid, options);
|
|
946
|
+
put(eid, options[0].at, [
|
|
947
|
+
w,
|
|
948
|
+
h
|
|
949
|
+
]);
|
|
950
|
+
ready();
|
|
951
|
+
}
|
|
952
|
+
// Where each line's name is written: kept, or the spot along it nearest
|
|
953
|
+
// its middle that covers no shape and no other name.
|
|
954
|
+
let mids = new Map();
|
|
955
|
+
let names = [];
|
|
956
|
+
let plates = shapesPlaced().map(([x, y, w, h])=>[
|
|
957
|
+
x - w - 3,
|
|
958
|
+
y - h - 3,
|
|
959
|
+
x + w + 3,
|
|
960
|
+
y + h + 3
|
|
961
|
+
]);
|
|
962
|
+
let edgeOf = (l)=>{
|
|
963
|
+
let edge = l.eid ? byEid.get(l.eid) : undefined;
|
|
964
|
+
return edge ? label(edge) : l.eid ? '' : undefined;
|
|
965
|
+
};
|
|
966
|
+
for (let pass of [
|
|
967
|
+
true,
|
|
968
|
+
false
|
|
969
|
+
]){
|
|
970
|
+
for (let l of lines){
|
|
971
|
+
let mid = kept(l)?.mid;
|
|
972
|
+
if (mid != null != pass) continue;
|
|
973
|
+
let c = curve(seated(l.from), seated(l.to), bends.get(l.key));
|
|
974
|
+
let at = (t)=>tag(c, t, l.name, edgeOf(l)).box;
|
|
975
|
+
let costOf = (t)=>{
|
|
976
|
+
let box = at(t);
|
|
977
|
+
return [
|
|
978
|
+
...plates,
|
|
979
|
+
...names
|
|
980
|
+
].filter((b)=>over(box, b)).length * 1000 + (box[0] < 0 || box[2] > width ? 500 : 0) + Math.abs(t - .5) * 100;
|
|
981
|
+
};
|
|
982
|
+
let t = mid ?? MIDS.reduce((a, b)=>costOf(b) < costOf(a) ? b : a);
|
|
983
|
+
mids.set(l.key, t);
|
|
984
|
+
names.push(at(t));
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
let next = Math.max(-1, ...(was?.nodes ?? []).map((n)=>n.order)) + 1;
|
|
988
|
+
let nodes = drawn.map(({ b, comps, state, shape })=>{
|
|
989
|
+
let eid = b.entity.eid, o = old.get(eid), [x, y] = spot.get(eid);
|
|
990
|
+
return {
|
|
991
|
+
eid,
|
|
992
|
+
label: label(b),
|
|
993
|
+
title: titled(b),
|
|
994
|
+
comps,
|
|
995
|
+
shape,
|
|
996
|
+
...state ? {
|
|
997
|
+
state
|
|
998
|
+
} : {},
|
|
999
|
+
order: o?.order ?? next++,
|
|
1000
|
+
stage: o?.stage ?? stage,
|
|
1001
|
+
x,
|
|
1002
|
+
y,
|
|
1003
|
+
fresh: !o,
|
|
1004
|
+
...o && (o.x != x || o.y != y) ? {
|
|
1005
|
+
moved: {
|
|
1006
|
+
x: o.x,
|
|
1007
|
+
y: o.y
|
|
1008
|
+
}
|
|
1009
|
+
} : {}
|
|
1010
|
+
};
|
|
1011
|
+
});
|
|
1012
|
+
let shifted = (eid)=>{
|
|
1013
|
+
let o = old.get(eid), at = spot.get(eid);
|
|
1014
|
+
return !!o && (o.x != at[0] || o.y != at[1]);
|
|
1015
|
+
};
|
|
1016
|
+
let after = Math.max(-1, ...(was?.links ?? []).map((l)=>l.order)) + 1;
|
|
1017
|
+
let drawing = {
|
|
1018
|
+
width,
|
|
1019
|
+
height: Math.max(height, ...drawn.map(({ b, size })=>spot.get(b.entity.eid)[1] + size[1] + APART)),
|
|
1020
|
+
nodes,
|
|
1021
|
+
links: lines.map((l)=>{
|
|
1022
|
+
let o = oldLinks.get(l.key);
|
|
1023
|
+
let edge = l.eid ? byEid.get(l.eid) : undefined;
|
|
1024
|
+
return {
|
|
1025
|
+
...l,
|
|
1026
|
+
...edge ? {
|
|
1027
|
+
label: label(edge)
|
|
1028
|
+
} : {},
|
|
1029
|
+
bend: bends.get(l.key),
|
|
1030
|
+
mid: mids.get(l.key),
|
|
1031
|
+
order: o?.order ?? after++,
|
|
1032
|
+
stage: o?.stage ?? stage,
|
|
1033
|
+
fresh: !o,
|
|
1034
|
+
...o && (shifted(l.from) || shifted(l.to)) ? {
|
|
1035
|
+
moved: {
|
|
1036
|
+
bend: o.bend,
|
|
1037
|
+
mid: o.mid
|
|
1038
|
+
}
|
|
1039
|
+
} : {}
|
|
1040
|
+
};
|
|
1041
|
+
})
|
|
1042
|
+
};
|
|
1043
|
+
return [
|
|
1044
|
+
drawing,
|
|
1045
|
+
choices
|
|
1046
|
+
];
|
|
1047
|
+
};
|
|
1048
|
+
//# sourceMappingURL=layout.js.map
|