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