@meltemi/diagrams-editor 1.9.0 → 1.10.17

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 (154) hide show
  1. package/dist/add-icons.d.ts +44 -0
  2. package/dist/add-icons.d.ts.map +1 -0
  3. package/dist/add-icons.js +178 -0
  4. package/dist/add-icons.js.map +1 -0
  5. package/dist/arrange.d.ts +54 -1
  6. package/dist/arrange.d.ts.map +1 -1
  7. package/dist/arrange.js +209 -8
  8. package/dist/arrange.js.map +1 -1
  9. package/dist/asset-files.d.ts +46 -0
  10. package/dist/asset-files.d.ts.map +1 -0
  11. package/dist/asset-files.js +79 -0
  12. package/dist/asset-files.js.map +1 -0
  13. package/dist/c4.d.ts +91 -1
  14. package/dist/c4.d.ts.map +1 -1
  15. package/dist/c4.js +358 -9
  16. package/dist/c4.js.map +1 -1
  17. package/dist/canvas/index.d.ts +1 -0
  18. package/dist/canvas/index.d.ts.map +1 -1
  19. package/dist/canvas/index.js +1 -0
  20. package/dist/canvas/index.js.map +1 -1
  21. package/dist/canvas/keyboard.d.ts +23 -2
  22. package/dist/canvas/keyboard.d.ts.map +1 -1
  23. package/dist/canvas/keyboard.js +23 -0
  24. package/dist/canvas/keyboard.js.map +1 -1
  25. package/dist/canvas/palette.d.ts +48 -6
  26. package/dist/canvas/palette.d.ts.map +1 -1
  27. package/dist/canvas/palette.js +55 -9
  28. package/dist/canvas/palette.js.map +1 -1
  29. package/dist/canvas/pens.d.ts +10 -0
  30. package/dist/canvas/pens.d.ts.map +1 -0
  31. package/dist/canvas/pens.js +29 -0
  32. package/dist/canvas/pens.js.map +1 -0
  33. package/dist/canvas/protocol.d.ts +580 -21
  34. package/dist/canvas/protocol.d.ts.map +1 -1
  35. package/dist/canvas/protocol.js +140 -3
  36. package/dist/canvas/protocol.js.map +1 -1
  37. package/dist/canvas/routes.d.ts +17 -0
  38. package/dist/canvas/routes.d.ts.map +1 -1
  39. package/dist/canvas/routes.js +37 -0
  40. package/dist/canvas/routes.js.map +1 -1
  41. package/dist/canvas/turning.d.ts +7 -0
  42. package/dist/canvas/turning.d.ts.map +1 -1
  43. package/dist/canvas/turning.js +17 -3
  44. package/dist/canvas/turning.js.map +1 -1
  45. package/dist/catalog.d.ts +5 -0
  46. package/dist/catalog.d.ts.map +1 -1
  47. package/dist/catalog.js +1 -1
  48. package/dist/catalog.js.map +1 -1
  49. package/dist/clipboard.d.ts.map +1 -1
  50. package/dist/clipboard.js +98 -9
  51. package/dist/clipboard.js.map +1 -1
  52. package/dist/effects.d.ts +5 -0
  53. package/dist/effects.d.ts.map +1 -1
  54. package/dist/effects.js +31 -22
  55. package/dist/effects.js.map +1 -1
  56. package/dist/exports.d.ts +25 -11
  57. package/dist/exports.d.ts.map +1 -1
  58. package/dist/exports.js +65 -28
  59. package/dist/exports.js.map +1 -1
  60. package/dist/fonts.d.ts +63 -0
  61. package/dist/fonts.d.ts.map +1 -0
  62. package/dist/fonts.js +175 -0
  63. package/dist/fonts.js.map +1 -0
  64. package/dist/icons.d.ts +2 -0
  65. package/dist/icons.d.ts.map +1 -1
  66. package/dist/icons.js +39 -6
  67. package/dist/icons.js.map +1 -1
  68. package/dist/index.d.ts +6 -3
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +4 -1
  71. package/dist/index.js.map +1 -1
  72. package/dist/inspector.d.ts.map +1 -1
  73. package/dist/inspector.js +545 -141
  74. package/dist/inspector.js.map +1 -1
  75. package/dist/layouts.d.ts +3 -2
  76. package/dist/layouts.d.ts.map +1 -1
  77. package/dist/layouts.js +4 -2
  78. package/dist/layouts.js.map +1 -1
  79. package/dist/messages/en.d.ts +106 -20
  80. package/dist/messages/en.d.ts.map +1 -1
  81. package/dist/messages/en.js +110 -26
  82. package/dist/messages/en.js.map +1 -1
  83. package/dist/messages/es.d.ts.map +1 -1
  84. package/dist/messages/es.js +80 -43
  85. package/dist/messages/es.js.map +1 -1
  86. package/dist/messages/index.d.ts +76 -17
  87. package/dist/messages/index.d.ts.map +1 -1
  88. package/dist/messages/keys.d.ts.map +1 -1
  89. package/dist/messages/keys.js +26 -0
  90. package/dist/messages/keys.js.map +1 -1
  91. package/dist/messages/services.d.ts +130 -1
  92. package/dist/messages/services.d.ts.map +1 -1
  93. package/dist/messages/services.js +150 -18
  94. package/dist/messages/services.js.map +1 -1
  95. package/dist/move-file.d.ts +32 -0
  96. package/dist/move-file.d.ts.map +1 -0
  97. package/dist/move-file.js +58 -0
  98. package/dist/move-file.js.map +1 -0
  99. package/dist/node.d.ts +1 -1
  100. package/dist/node.d.ts.map +1 -1
  101. package/dist/node.js.map +1 -1
  102. package/dist/projects.d.ts +65 -1
  103. package/dist/projects.d.ts.map +1 -1
  104. package/dist/projects.js +349 -1
  105. package/dist/projects.js.map +1 -1
  106. package/dist/render.d.ts +5 -0
  107. package/dist/render.d.ts.map +1 -1
  108. package/dist/render.js +4 -2
  109. package/dist/render.js.map +1 -1
  110. package/dist/sequence.d.ts +37 -8
  111. package/dist/sequence.d.ts.map +1 -1
  112. package/dist/sequence.js +409 -38
  113. package/dist/sequence.js.map +1 -1
  114. package/dist/services/actions.d.ts.map +1 -1
  115. package/dist/services/actions.js +65 -1
  116. package/dist/services/actions.js.map +1 -1
  117. package/dist/services/completion.d.ts.map +1 -1
  118. package/dist/services/completion.js +146 -16
  119. package/dist/services/completion.js.map +1 -1
  120. package/dist/services/docs.d.ts +5 -0
  121. package/dist/services/docs.d.ts.map +1 -1
  122. package/dist/services/docs.js +7 -0
  123. package/dist/services/docs.js.map +1 -1
  124. package/dist/services/hover.d.ts +5 -0
  125. package/dist/services/hover.d.ts.map +1 -1
  126. package/dist/services/hover.js +154 -21
  127. package/dist/services/hover.js.map +1 -1
  128. package/dist/services/navigation.d.ts +1 -1
  129. package/dist/services/navigation.d.ts.map +1 -1
  130. package/dist/services/navigation.js +8 -4
  131. package/dist/services/navigation.js.map +1 -1
  132. package/dist/services/stories.d.ts.map +1 -1
  133. package/dist/services/stories.js +31 -3
  134. package/dist/services/stories.js.map +1 -1
  135. package/dist/session.d.ts +121 -13
  136. package/dist/session.d.ts.map +1 -1
  137. package/dist/session.js +1349 -80
  138. package/dist/session.js.map +1 -1
  139. package/dist/syntax.d.ts.map +1 -1
  140. package/dist/syntax.js +3 -0
  141. package/dist/syntax.js.map +1 -1
  142. package/dist/targets.d.ts +25 -4
  143. package/dist/targets.d.ts.map +1 -1
  144. package/dist/targets.js +103 -7
  145. package/dist/targets.js.map +1 -1
  146. package/dist/text-places.d.ts +15 -3
  147. package/dist/text-places.d.ts.map +1 -1
  148. package/dist/text-places.js +187 -12
  149. package/dist/text-places.js.map +1 -1
  150. package/dist/themes.d.ts +0 -2
  151. package/dist/themes.d.ts.map +1 -1
  152. package/dist/themes.js +27 -13
  153. package/dist/themes.js.map +1 -1
  154. package/package.json +16 -16
package/dist/session.js CHANGED
@@ -1,29 +1,38 @@
1
1
  import { geometrySvg, searchLibraries, } from "@meltemi/diagrams-assets";
2
- import { CREATABLE_DIAGRAMS, defaultLabel, newDiagramId, planEdit, } from "@meltemi/diagrams-edit";
2
+ import { CREATABLE_DIAGRAMS, activationPair, defaultLabel, newDiagramId, planEdit, } from "@meltemi/diagrams-edit";
3
3
  import { applyTextEdits, isIdentifierText } from "@meltemi/diagrams-language";
4
- import { layoutDiagram } from "@meltemi/diagrams-layout";
4
+ import { DEFAULT_PEN_WIDTH, StyleResolver, layoutDiagram, } from "@meltemi/diagrams-layout";
5
5
  import { MAIN_LAYER, compile, } from "@meltemi/diagrams-model";
6
6
  import { diagramView } from "./analysis.js";
7
- import { arrangeAttempt, containerOperation, convertOperation, stackedAfterMove, interiorOf, materializeOperation, steadyGroups, } from "./arrange.js";
7
+ import { alignable, arrangeAttempt, containerOperation, convertOperation, keepsPoints, penOperations, placingList, stackedAfterMove, interiorOf, materializeOperation, steadyGroups, steadySize, strokeParent, unlistedOperations, } from "./arrange.js";
8
8
  import { alignBoxes, distributeBoxes } from "./canvas/arrange.js";
9
9
  import { center } from "./canvas/geometry.js";
10
+ import { scalePenPoints } from "./canvas/pens.js";
10
11
  import { carriedChildren } from "./canvas/selection.js";
11
12
  import { isCanvasMessage, } from "./canvas/protocol.js";
12
13
  import { DEFAULT_GRID, snapValue, writtenCenter } from "./canvas/snapping.js";
13
14
  import { copyFragment, pasteOperations } from "./clipboard.js";
14
15
  import { iconGroups, takesIcon, writtenIcon } from "./icons.js";
15
- import { fieldOperations, inspect, namedRelation } from "./inspector.js";
16
- import { freshRelationId } from "./targets.js";
16
+ import { fieldOperations, inspect, namedRelation, themeSwatches } from "./inspector.js";
17
+ import { freshModelRelationId, freshRelationId, nameable } from "./targets.js";
17
18
  // The editor's catalog; `messages` names the host messages in this file.
18
19
  import { messages as wording } from "./messages/index.js";
19
- import { baseName, themeLocation, themePanel, themeTokenOperation } from "./themes.js";
20
+ import { baseName, effectiveTheme, themeLocation, themePanel, themeTokenOperation, } from "./themes.js";
21
+ import { drawnText, fontOptions, stepFontSize } from "./fonts.js";
20
22
  import { renderDiagram, renderLayout } from "./render.js";
21
23
  import { selectionAt } from "./services/navigation.js";
22
- import { eventInfos, sequenceAction, sequenceMove } from "./sequence.js";
23
- import { entityChoices, modelIsLocal } from "./c4.js";
24
+ import { activationOf, allEvents, eventInfos, movedEvent, sequenceAction, sequenceMove, } from "./sequence.js";
25
+ import { allowedKinds, copiedEntities, copiedNotes, entityChoices, entityParent, freshEntityPath, modelIsLocal, pasteEntities, } from "./c4.js";
26
+ import { addIcons, checkIconFiles, checkIconPrefix } from "./add-icons.js";
24
27
  import { storyChoices, storyPlayback } from "./story.js";
25
28
  const NONE = { messages: [] };
26
29
  /** Default sizes of groups created from the palette, on the 14 px grid. */
30
+ /** A note of a C4 view: a declaration of the view, not an entity of the model. */
31
+ function isViewNote(element) {
32
+ return element.kind === "note" && element.entity === null;
33
+ }
34
+ /** What a new note measures before its text is typed: "Note" at the note size. */
35
+ const NEW_NOTE_SIZE = { width: 36, height: 22 };
27
36
  const NEW_GROUP_SIZE = { width: 280, height: 168 };
28
37
  /**
29
38
  * Kinds created as empty shapes, `box box1 ""` or `node1[""]`, the text added later in place;
@@ -41,6 +50,18 @@ const GEOMETRY = new Set([
41
50
  "paste",
42
51
  "duplicate",
43
52
  ]);
53
+ /**
54
+ * Actions that can make a list grow or shrink: a card created, moved, pasted, duplicated,
55
+ * removed or resized, whose outermost placed list keeps its top-left corner (`listCorners`).
56
+ */
57
+ const LIST_RESIZING = new Set([
58
+ "create",
59
+ "move",
60
+ "remove",
61
+ "paste",
62
+ "duplicate",
63
+ "resize",
64
+ ]);
44
65
  /**
45
66
  * The state of one canvas on one document: the analysed text, the diagram shown and the
46
67
  * selection. It turns canvas messages into edit plans and host messages, and never touches the
@@ -53,10 +74,26 @@ export class CanvasSession {
53
74
  rendered = new Map();
54
75
  pendingSelection;
55
76
  pendingEdit;
77
+ /** With `pendingEdit`, the index of the tag of that owner whose pill the editor opens on. */
78
+ pendingTag;
79
+ /** A status line to show after the next render and the selection it brings. */
80
+ pendingStatus;
81
+ /** The participant whose gap a head drag just wrote, and the amount: said with the render. */
82
+ pendingGap;
56
83
  /** The diagram to show once it exists, after a conversion. */
57
84
  pendingDiagram;
58
- /** True while the canvas shows the diagram's effects running. */
59
- playing = false;
85
+ /**
86
+ * The event whose statement the last planned edit moves, by its key before the edit, with the
87
+ * spans of the edited text that the edit writes and the revision it was planned on: the next
88
+ * analysis finds it there (`movedEvent`) and the selection follows it to its key, since counted
89
+ * keys (`activate#2`) follow the order of the statements.
90
+ */
91
+ pendingMove;
92
+ /**
93
+ * True while the canvas shows the diagram's effects running: from the start, so a diagram with
94
+ * effects opens animated; the canvas pauses it from the choice it remembers per diagram.
95
+ */
96
+ playing = true;
60
97
  /** The scenario of a sequence diagram previewed read-only, with its diagram. */
61
98
  scenario;
62
99
  /** The story the canvas plays, read-only, in story mode. */
@@ -186,8 +223,29 @@ export class CanvasSession {
186
223
  });
187
224
  }
188
225
  if (render && this.pendingEdit) {
189
- messages.push({ type: "select", ids: [this.pendingEdit] });
226
+ const tag = this.pendingTag;
227
+ messages.push({
228
+ type: "select",
229
+ ids: [this.pendingEdit],
230
+ ...(tag !== undefined ? { tag } : {}),
231
+ });
190
232
  this.pendingEdit = undefined;
233
+ this.pendingTag = undefined;
234
+ }
235
+ // A head drag says the gap it wrote, and what the layout drew when the floor won.
236
+ if (render && this.pendingGap !== undefined) {
237
+ const { participant, amount } = this.pendingGap;
238
+ const floor = view.layout?.diagnostics.find((d) => d.code === "layout.gap-floor" && d.data?.gap?.participant === participant)?.data?.gap;
239
+ this.pendingStatus = floor
240
+ ? words.gapFloor(participant, floor.written, floor.applied)
241
+ : words.gapSet(participant, amount);
242
+ }
243
+ this.pendingGap = undefined;
244
+ // After the selection, whose own announcement it would otherwise lose to.
245
+ if (this.pendingStatus !== undefined) {
246
+ if (render)
247
+ messages.push({ type: "status", message: this.pendingStatus });
248
+ this.pendingStatus = undefined;
191
249
  }
192
250
  const model = this.inspect(view.id);
193
251
  if (model)
@@ -264,7 +322,10 @@ export class CanvasSession {
264
322
  return NONE;
265
323
  const operation = themeTokenOperation(location, token, value);
266
324
  if (location.file !== null) {
267
- return { messages: [], project: { kind: "themeFile", file: location.file, operation } };
325
+ return {
326
+ messages: [],
327
+ project: { kind: "files", steps: [{ file: location.file, operation }] },
328
+ };
268
329
  }
269
330
  return this.planned(view, "setField", [operation]);
270
331
  }
@@ -292,6 +353,17 @@ export class CanvasSession {
292
353
  return undefined;
293
354
  const diagram = view.diagram;
294
355
  const geometry = rendered.geometry;
356
+ /** The drawn tags of a participant or a message, with their pills; none when it has none. */
357
+ const tagsOf = (owner, tags) => {
358
+ const boxes = geometry.tags.get(owner) ?? [];
359
+ const places = geometry.texts.tags.get(owner) ?? [];
360
+ const drawn = tags.flatMap((tag, i) => {
361
+ const box = boxes[i];
362
+ const place = places[i];
363
+ return box ? [{ text: tag.text, box, ...(place ? { place } : {}) }] : [];
364
+ });
365
+ return drawn.length > 0 ? { tags: drawn } : {};
366
+ };
295
367
  const elements = [];
296
368
  for (const element of diagram.elements) {
297
369
  const box = geometry.boxes.get(element.id);
@@ -303,6 +375,26 @@ export class CanvasSession {
303
375
  const labelBox = geometry.elementLabels.get(element.id);
304
376
  const rotation = geometry.rotations.get(element.id);
305
377
  const radius = geometry.radii.get(element.id);
378
+ // A free line: its written points (ends and bends) and the polyline it is drawn with.
379
+ const connector = element.kind === "line" ? geometry.connectors.get(element.id) : undefined;
380
+ const line = connector && {
381
+ points: [connector.points[0], ...connector.via, connector.points.at(-1)],
382
+ drawn: connector.line,
383
+ via: connector.via,
384
+ };
385
+ // A pen stroke: where its points are drawn, and its bounds.
386
+ const stroke = element.kind === "pen" ? view.layout?.pens?.get(element.id) : undefined;
387
+ const pen = stroke && { points: stroke.points, bounds: stroke.bounds, width: stroke.width };
388
+ // A list group: how it arranges its children, defaults filled in.
389
+ const arranged = element.kind === "group" ? view.layout?.lists?.get(element.id) : undefined;
390
+ const list = arranged && {
391
+ layout: arranged.layout,
392
+ spacing: arranged.spacing,
393
+ align: arranged.align,
394
+ children: arranged.children,
395
+ };
396
+ // A group its list places has no `place` and resizes all the same: it writes its size.
397
+ const listed = placingList(diagram, element) !== undefined;
306
398
  elements.push({
307
399
  id: element.id,
308
400
  kind: element.kind,
@@ -310,7 +402,9 @@ export class CanvasSession {
310
402
  parent: element.groupPath[element.groupPath.length - 1] ?? null,
311
403
  box,
312
404
  placed,
313
- sized: element.kind === "group" ? placed && (sized || diagram.type === "free") : true,
405
+ sized: element.kind === "group"
406
+ ? (placed || listed) && (sized || diagram.type === "free")
407
+ : true,
314
408
  layer: element.layer,
315
409
  shape: geometry.shapes.get(element.id) ?? "rounded",
316
410
  ...(rotation !== undefined ? { rotation } : {}),
@@ -320,6 +414,10 @@ export class CanvasSession {
320
414
  takesIcon: takesIcon(diagram, element),
321
415
  icon: writtenIcon(diagram, element.id, "element"),
322
416
  ...(labelBox ? { labelBox } : {}),
417
+ ...(line ? { line } : {}),
418
+ ...(pen ? { pen } : {}),
419
+ ...(list ? { list } : {}),
420
+ ...tagsOf(element.id, element.tags),
323
421
  });
324
422
  }
325
423
  const relations = [];
@@ -328,6 +426,7 @@ export class CanvasSession {
328
426
  if (!connector)
329
427
  continue;
330
428
  const text = geometry.texts.relations.get(relation.key);
429
+ const activation = diagram.type === "sequence" ? activationOf(diagram, relation) : undefined;
331
430
  relations.push({
332
431
  key: relation.key,
333
432
  named: relation.id !== null,
@@ -343,6 +442,9 @@ export class CanvasSession {
343
442
  labelBox: geometry.labels.get(relation.key) ?? null,
344
443
  layer: relation.layer,
345
444
  ...(text ? { text } : {}),
445
+ ...(relation.returns !== null ? { returns: relation.returns } : {}),
446
+ ...tagsOf(relation.key, relation.tags),
447
+ ...(activation ? { activation } : {}),
346
448
  });
347
449
  }
348
450
  const events = (diagram.type === "sequence" && !scenario ? eventInfos(diagram, view.layout) : []).map((event) => {
@@ -354,6 +456,19 @@ export class CanvasSession {
354
456
  ...relations.map((r) => r.key),
355
457
  ...events.map((e) => e.key),
356
458
  ]);
459
+ // An event the last edit moved keeps its place in the selection under the key it has now.
460
+ const move = this.pendingMove;
461
+ if (move && move.revision !== analysis.revision) {
462
+ this.pendingMove = undefined;
463
+ const now = move.diagram === diagram.id ? movedEvent(diagram, move.kind, move.written) : null;
464
+ if (now && now.key !== move.key) {
465
+ const follow = (ids) => ids.map((id) => (id === move.key ? now.key : id));
466
+ if (this.pendingSelection)
467
+ this.pendingSelection = follow(this.pendingSelection);
468
+ else
469
+ this.selection = follow(this.selection);
470
+ }
471
+ }
357
472
  if (this.pendingSelection) {
358
473
  this.selection = this.pendingSelection.filter((id) => valid.has(id));
359
474
  this.pendingSelection = undefined;
@@ -367,6 +482,14 @@ export class CanvasSession {
367
482
  diagram: view.id,
368
483
  profile: diagram.type,
369
484
  ...(diagram.definition ? { tools: diagram.definition.tools } : {}),
485
+ ...(diagram.c4
486
+ ? {
487
+ tools: allowedKinds(diagram.c4.level).map((kind) => ({
488
+ kind,
489
+ label: wording().c4.tools[kind],
490
+ })),
491
+ }
492
+ : {}),
370
493
  layers: diagram.properties.layers,
371
494
  mode: view.mode,
372
495
  svg: rendered.svg,
@@ -380,6 +503,7 @@ export class CanvasSession {
380
503
  scenario: scenario ?? null,
381
504
  effects,
382
505
  playing,
506
+ ...(this.inProject() ? { project: true } : {}),
383
507
  entities: diagram.type === "c4" ? entityChoices(diagram) : [],
384
508
  selection: this.selection,
385
509
  ...(view.layout?.theme
@@ -387,7 +511,10 @@ export class CanvasSession {
387
511
  colors: {
388
512
  selection: view.layout.theme.selection ?? view.layout.theme.accent,
389
513
  guide: view.layout.theme.guide ?? view.layout.theme.accent,
514
+ accent: view.layout.theme.accent,
390
515
  },
516
+ // The pen menu's colours, and what a stroke in progress resolves its token with.
517
+ swatches: themeSwatches(view.layout.theme),
391
518
  }
392
519
  : {}),
393
520
  };
@@ -448,6 +575,93 @@ export class CanvasSession {
448
575
  return [];
449
576
  return iconGroups(analysis, this.libraries(), query);
450
577
  }
578
+ /**
579
+ * Adds icons to the project (`addIcons`): the files of the assets and the statements that give
580
+ * the first icon to the targets, as one request of steps over files the host applies in one
581
+ * undo step; outside a project every file is refused.
582
+ */
583
+ addIcons(message) {
584
+ const words = wording().addIcons;
585
+ if (!this.inProject()) {
586
+ const reason = words.needsProject;
587
+ return {
588
+ messages: [
589
+ {
590
+ type: "iconsAdded",
591
+ token: message.token,
592
+ added: [],
593
+ refused: message.files.map((f) => ({ file: f.file, reason })),
594
+ },
595
+ { type: "rejected", code: "edit.unsafe-target", reason },
596
+ ],
597
+ };
598
+ }
599
+ const result = addIcons(this.analysis?.icons, this.view()?.diagram, message);
600
+ const answer = {
601
+ type: "iconsAdded",
602
+ token: message.token,
603
+ added: result.added,
604
+ refused: result.refused,
605
+ };
606
+ if (result.added.length === 0)
607
+ return { messages: [answer] };
608
+ return {
609
+ messages: [
610
+ answer,
611
+ { type: "status", message: words.added(result.added.map((a) => a.reference)) },
612
+ ],
613
+ project: { kind: "files", steps: result.steps, assets: result.assets },
614
+ };
615
+ }
616
+ /**
617
+ * The text size keys: each selected item that draws text gets the common size one step up or
618
+ * down from the size it draws (its style's, its rules' and classes', or the theme's for its
619
+ * kind), in one edit. Items at the end of the list are left as they are.
620
+ */
621
+ stepTextSize(view, ids, step) {
622
+ const words = wording().session;
623
+ const diagram = view.diagram;
624
+ const theme = effectiveTheme(this.analysis, view);
625
+ const resolver = new StyleResolver(theme, diagram);
626
+ const operations = [];
627
+ const sizes = [];
628
+ let texts = 0;
629
+ for (const id of ids) {
630
+ const kind = diagram.elementsById.has(id)
631
+ ? "element"
632
+ : diagram.relationsByKey.has(id)
633
+ ? "relation"
634
+ : diagram.eventsByKey.has(id)
635
+ ? "event"
636
+ : undefined;
637
+ if (!kind)
638
+ continue;
639
+ const drawn = drawnText(theme, diagram, { id, kind }, resolver);
640
+ if (!drawn)
641
+ continue;
642
+ texts++;
643
+ const next = stepFontSize(drawn.size, step);
644
+ if (next === undefined)
645
+ continue;
646
+ const result = fieldOperations(this.analysis, view.diagram.id, [id], "style.fontSize", next);
647
+ if (!result.ok)
648
+ continue;
649
+ operations.push(...result.operations);
650
+ sizes.push(next);
651
+ }
652
+ if (texts === 0)
653
+ return { messages: [{ type: "status", message: words.noTextSize }] };
654
+ if (operations.length === 0)
655
+ return { messages: [{ type: "status", message: words.textSizeEnd }] };
656
+ this.pendingSelection = [...this.selection];
657
+ const planned = this.planned(view, "setField", operations);
658
+ const said = sizes.every((size) => size === sizes[0])
659
+ ? words.textSize(sizes[0])
660
+ : words.textSizes;
661
+ return planned.plan
662
+ ? { ...planned, messages: [...planned.messages, { type: "status", message: said }] }
663
+ : planned;
664
+ }
451
665
  /** Highlights the element or relation of the statement at a text offset. */
452
666
  selectAt(offset) {
453
667
  if (!this.analysis)
@@ -503,6 +717,25 @@ export class CanvasSession {
503
717
  { type: "icons", token: message.token, groups: this.searchIcons(message.query) },
504
718
  ],
505
719
  };
720
+ case "listFonts":
721
+ return {
722
+ messages: [
723
+ { type: "fonts", token: message.token, families: fontOptions(analysis.fonts) },
724
+ ],
725
+ };
726
+ case "checkIcons":
727
+ return {
728
+ messages: [
729
+ {
730
+ type: "iconsChecked",
731
+ token: message.token,
732
+ prefix: checkIconPrefix(analysis.icons, message.prefix),
733
+ files: checkIconFiles(analysis.icons, message.prefix, message.files),
734
+ },
735
+ ],
736
+ };
737
+ case "addIcons":
738
+ return this.addIcons(message);
506
739
  case "editTheme":
507
740
  this.themeEditing = message.open;
508
741
  this.awaitedTheme = undefined;
@@ -594,6 +827,10 @@ export class CanvasSession {
594
827
  };
595
828
  }
596
829
  return this.act(message.action);
830
+ case "presencePointer":
831
+ case "presenceLaser":
832
+ // The host's to share; the document has nothing to do with it.
833
+ return NONE;
597
834
  }
598
835
  }
599
836
  /** Pastes clipboard text at a point, or two grid steps off when no point is given. */
@@ -602,12 +839,32 @@ export class CanvasSession {
602
839
  if (!view)
603
840
  return NONE;
604
841
  const where = at ? { at: this.snap(at) } : { shift: { x: 2 * this.grid, y: 2 * this.grid } };
842
+ if (view.diagram.type === "c4")
843
+ return this.pasteEntities(view, text, where);
605
844
  const pasted = pasteOperations(this.analysis, view.id, text, where);
606
845
  if (!pasted.ok)
607
846
  return { messages: [{ type: "status", message: pasted.reason }] };
608
847
  this.pendingSelection = [...pasted.ids];
609
848
  return this.planned(view, "paste", pasted.operations);
610
849
  }
850
+ /**
851
+ * Pastes entities copied from a C4 view into this one: copies declared in its model, included
852
+ * and placed; anything else on the clipboard says why it does not paste here.
853
+ */
854
+ pasteEntities(view, text, where) {
855
+ const compilation = compile(text);
856
+ const copied = copiedEntities(compilation.document.models, compilation.document.diagrams);
857
+ const notes = copiedNotes(compilation.document.diagrams);
858
+ if (!copied || (copied.length === 0 && notes.length === 0)) {
859
+ return { messages: [{ type: "status", message: wording().reasons.notFragment }] };
860
+ }
861
+ const placed = view.mode !== "auto" || view.layout?.boxes !== undefined;
862
+ const pasted = pasteEntities(view.diagram, placed, copied, where, notes);
863
+ if (!pasted.ok)
864
+ return { messages: [{ type: "status", message: pasted.reason }] };
865
+ this.pendingSelection = [...pasted.ids];
866
+ return this.planned(view, "paste", pasted.operations);
867
+ }
611
868
  /** The scenario the canvas previews, if any. */
612
869
  get previewed() {
613
870
  const view = this.view();
@@ -726,6 +983,17 @@ export class CanvasSession {
726
983
  * One plan for the operations, after the arrangement of an `auto` diagram when needed; undefined
727
984
  * without operations. It changes nothing in the session: previews plan with it too.
728
985
  */
986
+ /**
987
+ * After an edit names the relation `key` as `id` first, the selection holds `id` where it held
988
+ * the key, so the canvas and the inspector stay on the relation; a refusal drops it.
989
+ */
990
+ followNaming(key, id, operations) {
991
+ if (operations.length === 0)
992
+ return;
993
+ this.pendingSelection = this.selection.includes(key)
994
+ ? this.selection.map((s) => (s === key ? id : s))
995
+ : [id];
996
+ }
729
997
  plan(view, kind, operations) {
730
998
  if (operations.length === 0)
731
999
  return undefined;
@@ -738,14 +1006,27 @@ export class CanvasSession {
738
1006
  all.push(...operations);
739
1007
  return planEdit(this.analysis.compilation, all.length === 1 ? all[0] : { kind: "batch", operations: all }, { revision: this.analysis.revision });
740
1008
  }
741
- /** The plan of `plan` for the host to apply; a refusal drops what waited for the edit. */
742
- planned(view, kind, operations) {
743
- const result = this.plan(view, kind, operations);
1009
+ /**
1010
+ * The plan of `plan` for the host to apply; a refusal drops what waited for the edit. When the
1011
+ * operations write the model of a C4 view that another file holds, they go to the host as
1012
+ * steps over files instead, with `retry` for the dependents dialog.
1013
+ */
1014
+ planned(view, kind, operations, retry) {
1015
+ const steps = this.modelSteps(view, kind, operations, retry);
1016
+ if (steps)
1017
+ return steps;
1018
+ const result = this.plan(view, kind, [
1019
+ ...operations,
1020
+ ...this.listCorners(view, kind, operations),
1021
+ ]);
744
1022
  if (!result)
745
1023
  return NONE;
746
1024
  if (!result.ok) {
747
1025
  this.pendingSelection = undefined;
748
1026
  this.pendingEdit = undefined;
1027
+ this.pendingTag = undefined;
1028
+ this.pendingMove = undefined;
1029
+ this.pendingStatus = undefined;
749
1030
  this.pendingDiagram = undefined;
750
1031
  return {
751
1032
  messages: [
@@ -755,10 +1036,181 @@ export class CanvasSession {
755
1036
  }
756
1037
  return { messages: [], plan: result.plan };
757
1038
  }
1039
+ /**
1040
+ * The `setPosition` operations that keep the top-left corner of every list that `operations`
1041
+ * make grow or shrink where it is drawn, so that a column grows downward, a row to the right,
1042
+ * and the cards already there stay: each outermost list with a `place` (a list whose own parent
1043
+ * is not a list) whose drawn size the operations change gets its place moved by half the
1044
+ * change, in its parent's interior. The groups without `size` around it, which grow about their
1045
+ * centre with it, keep their corner the same way, so that the interior it is placed in stays.
1046
+ * The lists inside it need nothing: it places them. Nothing for an `auto` diagram, whose engine
1047
+ * places everything, and nothing for a list or group the operations move or size themselves.
1048
+ * The planned text is laid out once, and once more for each level of groups around a list.
1049
+ */
1050
+ listCorners(view, kind, operations) {
1051
+ const diagram = view.diagram;
1052
+ const before = view.layout?.boxes;
1053
+ const origins = view.layout?.origins;
1054
+ if (!LIST_RESIZING.has(kind) ||
1055
+ diagram.type !== "free" ||
1056
+ view.mode === "auto" ||
1057
+ !before ||
1058
+ operations.length === 0) {
1059
+ return [];
1060
+ }
1061
+ const parentOf = (e) => e.groupPath[e.groupPath.length - 1] ?? null;
1062
+ const placeOf = (d, id) => d.placement.find((p) => p.statement === "place" && p.target === id);
1063
+ const sized = (d, id) => d.placement.some((p) => p.statement === "size" && p.target === id);
1064
+ // What the operations themselves place, size or move keeps what they write.
1065
+ const touched = new Set(operations.flatMap((op) => "element" in op && typeof op.element === "string" ? [op.element] : []));
1066
+ const outermost = (d, id) => {
1067
+ const element = d.elementsById.get(id);
1068
+ const parent = element ? parentOf(element) : null;
1069
+ return Boolean(element?.list) && (parent === null || !d.elementsById.get(parent)?.list);
1070
+ };
1071
+ if (!diagram.elements.some((e) => outermost(diagram, e.id) && placeOf(diagram, e.id))) {
1072
+ return [];
1073
+ }
1074
+ const analysis = this.analysis;
1075
+ const corners = new Map();
1076
+ const operationsWith = () => [
1077
+ ...operations,
1078
+ ...[...corners].map(([element, at]) => ({
1079
+ kind: "setPosition",
1080
+ diagram: view.id,
1081
+ element,
1082
+ x: at.x,
1083
+ y: at.y,
1084
+ })),
1085
+ ];
1086
+ const laidOut = () => {
1087
+ const result = this.plan(view, kind, operationsWith());
1088
+ if (!result?.ok)
1089
+ return undefined;
1090
+ const compilation = compile(applyTextEdits(analysis.compilation.text, result.plan.edits), analysis.compilation.options);
1091
+ const copy = compilation.document.diagrams.find((d) => d.id === view.id);
1092
+ if (!copy)
1093
+ return undefined;
1094
+ const boxes = layoutDiagram(compilation, copy, analysis.layoutOptions).boxes;
1095
+ return boxes ? { diagram: copy, boxes } : undefined;
1096
+ };
1097
+ let after = laidOut();
1098
+ if (!after)
1099
+ return [];
1100
+ const next = after.diagram;
1101
+ // Whether a group keeps the place and the group it had: the operations leave it where it is.
1102
+ const kept = (id) => {
1103
+ const was = diagram.elementsById.get(id);
1104
+ const now = next.elementsById.get(id);
1105
+ const old = placeOf(diagram, id);
1106
+ const place = placeOf(next, id);
1107
+ return (was !== undefined &&
1108
+ now !== undefined &&
1109
+ !touched.has(id) &&
1110
+ parentOf(was) === parentOf(now) &&
1111
+ old !== undefined &&
1112
+ place !== undefined &&
1113
+ old.x === place.x &&
1114
+ old.y === place.y &&
1115
+ before.has(id));
1116
+ };
1117
+ // The lists, and the groups without size around each up to the first that is not one.
1118
+ const levels = new Map();
1119
+ for (const element of next.elements) {
1120
+ if (!outermost(next, element.id) || !kept(element.id))
1121
+ continue;
1122
+ let id = element.id;
1123
+ while (id !== null) {
1124
+ const group = next.elementsById.get(id);
1125
+ const depth = group.groupPath.length;
1126
+ levels.set(depth, (levels.get(depth) ?? new Set()).add(id));
1127
+ const parent = parentOf(group);
1128
+ const outer = parent === null ? undefined : next.elementsById.get(parent);
1129
+ id =
1130
+ outer && outer.list === null && !sized(next, outer.id) && kept(outer.id)
1131
+ ? outer.id
1132
+ : null;
1133
+ }
1134
+ }
1135
+ const depths = [...levels.keys()].sort((a, b) => b - a);
1136
+ for (const [i, depth] of depths.entries()) {
1137
+ if (i > 0 && corners.size > 0) {
1138
+ after = laidOut();
1139
+ if (!after)
1140
+ return [];
1141
+ }
1142
+ for (const id of levels.get(depth)) {
1143
+ const old = before.get(id);
1144
+ const box = after.boxes.get(id);
1145
+ if (!box ||
1146
+ (Math.abs(box.width - old.width) < 1e-6 && Math.abs(box.height - old.height) < 1e-6)) {
1147
+ continue;
1148
+ }
1149
+ // Its corner where it is drawn, in its parent's interior, which the outer levels keep.
1150
+ const origin = origins?.get(id) ?? { x: 0, y: 0 };
1151
+ corners.set(id, {
1152
+ x: old.x + box.width / 2 - origin.x,
1153
+ y: old.y + box.height / 2 - origin.y,
1154
+ });
1155
+ }
1156
+ }
1157
+ return operationsWith().slice(operations.length);
1158
+ }
1159
+ /**
1160
+ * The operations as steps over files when a C4 view's model is in another file of the project
1161
+ * and they write it: the model's first, then the view's, which name what the model's wrote;
1162
+ * the host plans and applies them together. Undefined when the plan is the document's alone.
1163
+ */
1164
+ modelSteps(view, kind, operations, retry) {
1165
+ const diagram = view.diagram;
1166
+ const file = diagram.type === "c4" ? (diagram.c4?.resolved?.file ?? null) : null;
1167
+ if (file === null || !operations.some((op) => "model" in op))
1168
+ return undefined;
1169
+ const own = [];
1170
+ if (GEOMETRY.has(kind)) {
1171
+ const materialize = materializeOperation(view);
1172
+ if (materialize)
1173
+ own.push(materialize);
1174
+ }
1175
+ own.push(...operations.filter((op) => !("model" in op)));
1176
+ const steps = [
1177
+ ...operations.filter((op) => "model" in op).map((operation) => ({ file, operation })),
1178
+ ...own.map((operation) => ({ file: null, operation })),
1179
+ ];
1180
+ const asks = operations.some((op) => op.kind === "removeModelRelation" && !op.cascade);
1181
+ return {
1182
+ messages: [],
1183
+ project: { kind: "files", steps, ...(asks && retry ? { retry } : {}) },
1184
+ };
1185
+ }
1186
+ /**
1187
+ * Where a new note's centre goes: where the tool was dropped, or, dropped on an element it is
1188
+ * attached to, at the element's east edge plus the theme's `groupPadding`, aligned to its top,
1189
+ * as a note of the size the text "Note" gives it, snapped to the grid.
1190
+ */
1191
+ notePlace(view, action) {
1192
+ const target = action.attachedTo !== undefined ? view.layout?.boxes?.get(action.attachedTo) : undefined;
1193
+ if (!target)
1194
+ return this.snap(action.at);
1195
+ const padding = view.layout?.theme?.groupPadding ?? 16;
1196
+ return this.snap({
1197
+ x: target.x + target.width + padding + NEW_NOTE_SIZE.width / 2,
1198
+ y: target.y + NEW_NOTE_SIZE.height / 2,
1199
+ });
1200
+ }
1201
+ /** A view whose model is not resolved cannot write it. */
1202
+ unresolved() {
1203
+ return {
1204
+ messages: [
1205
+ { type: "rejected", code: "edit.unsafe-target", reason: wording().session.fixFirst },
1206
+ ],
1207
+ };
1208
+ }
758
1209
  /**
759
1210
  * The actions of a C4 view that differ from flow diagrams: entities are added from the model
760
- * and removed by excluding them; labels are local; ids rename the model's entities. Returns
761
- * undefined for the actions the flow code handles.
1211
+ * and removed by excluding them; relations are the model's; labels are local or the model's;
1212
+ * ids rename the model's entities and relations. Returns undefined for the actions the flow
1213
+ * code handles.
762
1214
  */
763
1215
  c4Action(view, action) {
764
1216
  const diagram = view.diagram;
@@ -769,6 +1221,101 @@ export class CanvasSession {
769
1221
  }
770
1222
  const model = diagram.c4?.resolved?.definition;
771
1223
  switch (action.kind) {
1224
+ case "create": {
1225
+ // A note, the one declaration a view holds: written in the view, never in the model.
1226
+ if (action.elementKind === "note") {
1227
+ if (!diagram.c4?.resolved)
1228
+ return this.unresolved();
1229
+ const id = freeElementId(this.analysis, diagram, "note");
1230
+ const placed = view.mode !== "auto" || view.layout?.boxes !== undefined;
1231
+ const at = this.notePlace(view, action);
1232
+ this.pendingSelection = [id];
1233
+ this.pendingEdit = id;
1234
+ if (action.attachedTo !== undefined) {
1235
+ this.pendingStatus = wording().session.noteAttached(action.attachedTo);
1236
+ }
1237
+ return this.planned(view, "create", [
1238
+ {
1239
+ kind: "createElement",
1240
+ diagram: d,
1241
+ elementKind: "note",
1242
+ id,
1243
+ label: "Note",
1244
+ ...(placed ? { at } : {}),
1245
+ ...(action.attachedTo !== undefined ? { attachedTo: action.attachedTo } : {}),
1246
+ },
1247
+ ]);
1248
+ }
1249
+ if (action.attachedTo !== undefined) {
1250
+ return {
1251
+ messages: [
1252
+ {
1253
+ type: "rejected",
1254
+ code: "edit.invalid-value",
1255
+ reason: wording().session.attachNotesOnly(action.elementKind),
1256
+ },
1257
+ ],
1258
+ };
1259
+ }
1260
+ // An entity of a kind the level draws, declared in the model where the level puts it,
1261
+ // included in the view (nothing when the view draws it by default) and placed.
1262
+ if (!model)
1263
+ return this.unresolved();
1264
+ const kind = action.elementKind;
1265
+ const kinds = allowedKinds(diagram.c4.level);
1266
+ if (!kinds.includes(kind)) {
1267
+ return {
1268
+ messages: [
1269
+ {
1270
+ type: "rejected",
1271
+ code: "edit.invalid-value",
1272
+ reason: wording().c4.kindNotDrawn(action.elementKind, kinds),
1273
+ },
1274
+ ],
1275
+ };
1276
+ }
1277
+ const parent = entityParent(diagram, kind);
1278
+ const path = freshEntityPath(model, parent, kind);
1279
+ const ops = [
1280
+ {
1281
+ kind: "createEntity",
1282
+ model: model.id,
1283
+ entityKind: kind,
1284
+ ...(parent !== null ? { parent } : {}),
1285
+ id: path.slice(path.lastIndexOf(".") + 1),
1286
+ label: "",
1287
+ },
1288
+ { kind: "includeEntity", diagram: d, entity: path },
1289
+ ];
1290
+ // Where it was dropped: an `auto` view writes its arrangement first, as for any creation.
1291
+ if (view.mode !== "auto" || view.layout?.boxes) {
1292
+ const at = this.snap(action.at);
1293
+ ops.push({ kind: "setPosition", diagram: d, element: path, x: at.x, y: at.y });
1294
+ }
1295
+ this.pendingSelection = [path];
1296
+ this.pendingEdit = path;
1297
+ return this.planned(view, "create", ops);
1298
+ }
1299
+ case "duplicate": {
1300
+ // Copies of the selected entities, declared beside them in the model and placed off them.
1301
+ const fragment = copyFragment(this.analysis, d, action.ids);
1302
+ const read = fragment ? compile(fragment) : undefined;
1303
+ const copied = read
1304
+ ? copiedEntities(read.document.models, read.document.diagrams)
1305
+ : undefined;
1306
+ const notes = read ? copiedNotes(read.document.diagrams) : [];
1307
+ if (!copied || (copied.length === 0 && notes.length === 0))
1308
+ return { messages: [] };
1309
+ const placed = view.mode !== "auto" || view.layout?.boxes !== undefined;
1310
+ const pasted = pasteEntities(diagram, placed, copied, { shift: { x: 2 * this.grid, y: 2 * this.grid } }, notes);
1311
+ if (!pasted.ok) {
1312
+ return {
1313
+ messages: [{ type: "rejected", code: "edit.invalid-value", reason: pasted.reason }],
1314
+ };
1315
+ }
1316
+ this.pendingSelection = [...pasted.ids];
1317
+ return this.planned(view, "duplicate", pasted.operations);
1318
+ }
772
1319
  case "includeEntity": {
773
1320
  const ops = [{ kind: "includeEntity", diagram: d, entity: action.entity }];
774
1321
  if (action.at && view.mode !== "auto") {
@@ -778,33 +1325,186 @@ export class CanvasSession {
778
1325
  this.pendingSelection = [action.entity];
779
1326
  return this.planned(view, "includeEntity", ops);
780
1327
  }
1328
+ case "connect": {
1329
+ if (!model)
1330
+ return this.unresolved();
1331
+ // A relation pinned to points needs a route, and a route needs a named relation.
1332
+ const pinned = action.pinned;
1333
+ const id = pinned ? freshModelRelationId(diagram, action) : undefined;
1334
+ const ops = [
1335
+ {
1336
+ kind: "createModelRelation",
1337
+ model: model.id,
1338
+ from: action.from,
1339
+ to: action.to,
1340
+ ...(id ? { id } : {}),
1341
+ },
1342
+ ];
1343
+ if (pinned && id) {
1344
+ ops.push({
1345
+ kind: "setRoute",
1346
+ diagram: d,
1347
+ relation: id,
1348
+ via: [],
1349
+ ...(pinned.fromPort !== undefined ? { from: pinned.fromPort } : {}),
1350
+ ...(pinned.fromPosition !== undefined ? { fromPosition: pinned.fromPosition } : {}),
1351
+ ...(pinned.toPort !== undefined ? { to: pinned.toPort } : {}),
1352
+ ...(pinned.toPosition !== undefined ? { toPosition: pinned.toPosition } : {}),
1353
+ });
1354
+ }
1355
+ // The new relation comes after the others of its pair in the model, so its key is the next.
1356
+ const pair = model.relations.filter((r) => r.from === action.from && r.to === action.to);
1357
+ this.pendingSelection = [id ?? `${action.from}->${action.to}#${pair.length + 1}`];
1358
+ return this.planned(view, "connect", ops);
1359
+ }
1360
+ case "setEnd": {
1361
+ const relation = diagram.relationsByKey.get(action.relation);
1362
+ if (!relation)
1363
+ return { messages: [] };
1364
+ if (!model)
1365
+ return this.unresolved();
1366
+ const combined = diagram.c4?.combined.includes(relation.key) ?? false;
1367
+ const source = combined ? undefined : model.relationsByKey.get(relation.key);
1368
+ const current = action.end === "from" ? relation.from : relation.to;
1369
+ const end = (id, port) => ({
1370
+ kind: "setRoute",
1371
+ diagram: d,
1372
+ relation: id,
1373
+ via: currentVia(diagram, id),
1374
+ ...(action.end === "from"
1375
+ ? {
1376
+ from: port,
1377
+ ...(action.position !== undefined ? { fromPosition: action.position } : {}),
1378
+ }
1379
+ : {
1380
+ to: port,
1381
+ ...(action.position !== undefined ? { toPosition: action.position } : {}),
1382
+ }),
1383
+ });
1384
+ if (current !== action.element) {
1385
+ // Another entity: the model relation's end moves there.
1386
+ if (!source) {
1387
+ return {
1388
+ messages: [
1389
+ {
1390
+ type: "rejected",
1391
+ code: "edit.invalid-value",
1392
+ reason: wording().c4.reconnectCombination(relation.key, relation.sources),
1393
+ },
1394
+ ],
1395
+ };
1396
+ }
1397
+ const route = relation.id
1398
+ ? diagram.placement.find((p) => p.statement === "route" && p.target === relation.id)
1399
+ : undefined;
1400
+ const endpoint = route ? (action.end === "from" ? route.from : route.to) : undefined;
1401
+ // A dropped port, else the compass port of the old end carried over; a named port of
1402
+ // the old entity does not carry over.
1403
+ const port = action.port !== null
1404
+ ? action.port
1405
+ : endpoint?.explicitPort
1406
+ ? endpoint.named
1407
+ ? null
1408
+ : endpoint.port
1409
+ : undefined;
1410
+ const from = action.end === "from" ? action.element : source.from;
1411
+ const to = action.end === "to" ? action.element : source.to;
1412
+ const ops = [];
1413
+ let id = relation.id;
1414
+ if (port !== undefined && id === null) {
1415
+ // The route needs a name: the relation is named after its new ends.
1416
+ id = freshModelRelationId(diagram, { from, to });
1417
+ ops.push({
1418
+ kind: "setModelRelationProperty",
1419
+ model: model.id,
1420
+ relation: source.key,
1421
+ property: "id",
1422
+ value: id,
1423
+ });
1424
+ }
1425
+ // Once named, the relation goes by its id. The move rewrites the relation's routes in
1426
+ // the views of the model's file, with the dropped port in this one's; a route still to
1427
+ // write (none here yet), or one in another file than the model's, is set after it.
1428
+ ops.push({
1429
+ kind: "reconnectModelRelation",
1430
+ model: model.id,
1431
+ relation: id ?? source.key,
1432
+ end: action.end,
1433
+ entity: action.element,
1434
+ ...(id !== null && port !== undefined
1435
+ ? {
1436
+ view: d,
1437
+ port,
1438
+ ...(action.position !== undefined ? { position: action.position } : {}),
1439
+ }
1440
+ : {}),
1441
+ });
1442
+ const elsewhere = (diagram.c4?.resolved?.file ?? null) !== null;
1443
+ if (port !== undefined && id !== null && (!route || elsewhere))
1444
+ ops.push(end(id, port));
1445
+ // The relation keeps its place in the model's text, so its key counts the relations of
1446
+ // its new pair written before it.
1447
+ const before = model.relations.filter((r) => r.from === from && r.to === to && r.node.start < source.node.start).length;
1448
+ this.pendingSelection = [id ?? `${from}->${to}#${before + 1}`];
1449
+ return this.planned(view, "setEnd", ops);
1450
+ }
1451
+ // The same entity: the end pinned to a port or a point of its border, or handed back to
1452
+ // the layout, which changes nothing for a relation without a route.
1453
+ if (action.port === null && relation.id === null)
1454
+ return { messages: [] };
1455
+ if (!nameable(diagram, relation)) {
1456
+ return {
1457
+ messages: [
1458
+ {
1459
+ type: "rejected",
1460
+ code: "edit.invalid-value",
1461
+ reason: wording().session.fixFirst,
1462
+ },
1463
+ ],
1464
+ };
1465
+ }
1466
+ const { id, operations } = namedRelation(diagram, relation);
1467
+ this.followNaming(relation.key, id, operations);
1468
+ return this.planned(view, "setEnd", [...operations, end(id, action.port)]);
1469
+ }
781
1470
  case "remove": {
782
1471
  const ops = [];
783
1472
  const elements = action.ids.filter((id) => diagram.elementsById.get(id)?.kind !== undefined);
784
1473
  for (const id of elements) {
785
- if (diagram.elementsById.get(id).kind === "group")
1474
+ const element = diagram.elementsById.get(id);
1475
+ if (element.kind === "group")
786
1476
  continue;
787
- ops.push({ kind: "excludeEntity", diagram: d, entity: id, cascade: action.cascade });
1477
+ // A note is the view's own declaration; an entity is left out of the view.
1478
+ ops.push(isViewNote(element)
1479
+ ? { kind: "removeElement", diagram: d, element: id, cascade: action.cascade }
1480
+ : { kind: "excludeEntity", diagram: d, entity: id, cascade: action.cascade });
788
1481
  }
789
1482
  for (const key of action.ids) {
790
1483
  if (diagram.c4?.combined.includes(key))
791
1484
  ops.push({ kind: "removeRelation", diagram: d, relation: key });
792
1485
  else if (diagram.relationsByKey.has(key)) {
793
- return {
794
- messages: [
795
- {
796
- type: "rejected",
797
- code: "edit.invalid-value",
798
- reason: wording().session.modelRelation(key),
799
- },
800
- ],
801
- };
1486
+ // A drawn model relation goes from the model, with what names it in its views.
1487
+ const source = model?.relationsByKey.get(key);
1488
+ if (!model || !source)
1489
+ return this.unresolved();
1490
+ ops.push({
1491
+ kind: "removeModelRelation",
1492
+ model: model.id,
1493
+ relation: source.key,
1494
+ cascade: action.cascade,
1495
+ view: d,
1496
+ });
802
1497
  }
803
1498
  }
804
1499
  if (!action.cascade) {
805
1500
  const dependents = [];
806
1501
  for (const operation of ops) {
807
- if (operation.kind !== "excludeEntity")
1502
+ if (operation.kind !== "excludeEntity" &&
1503
+ operation.kind !== "removeElement" &&
1504
+ operation.kind !== "removeModelRelation")
1505
+ continue;
1506
+ // A model in another file is asked through the host's steps.
1507
+ if (operation.kind === "removeModelRelation" && !modelIsLocal(diagram))
808
1508
  continue;
809
1509
  const probe = planEdit(this.analysis.compilation, operation);
810
1510
  if (!probe.ok && probe.rejection.code === "edit.has-dependents") {
@@ -827,27 +1527,85 @@ export class CanvasSession {
827
1527
  }
828
1528
  }
829
1529
  this.pendingSelection = [];
830
- return this.planned(view, "remove", ops);
1530
+ return this.planned(view, "remove", ops, { ...action, cascade: true });
831
1531
  }
832
1532
  case "setLabel": {
833
1533
  const label = action.label === "" ? null : action.label;
834
- const ops = diagram.c4?.combined.includes(action.id)
835
- ? [{ kind: "setRelationLabel", diagram: d, relation: action.id, label }]
836
- : [{ kind: "setLocalLabel", diagram: d, target: action.id, label }];
837
- return this.planned(view, "setLabel", ops);
838
- }
839
- case "renameId": {
840
- if (!model || !modelIsLocal(diagram)) {
1534
+ if (diagram.c4?.combined.includes(action.id)) {
1535
+ return this.planned(view, "setLabel", [
1536
+ { kind: "setRelationLabel", diagram: d, relation: action.id, label },
1537
+ ]);
1538
+ }
1539
+ const relation = diagram.relationsByKey.get(action.id);
1540
+ const note = diagram.elementsById.get(action.id);
1541
+ if (note && isViewNote(note)) {
1542
+ return this.planned(view, "setLabel", [
1543
+ { kind: "setElementLabel", diagram: d, element: action.id, label: action.label },
1544
+ ]);
1545
+ }
1546
+ if (!relation) {
1547
+ // A label typed on an entity the model left empty (one just created) is the model's;
1548
+ // otherwise it is this view's local label.
1549
+ const entity = model?.entitiesByPath.get(action.id);
1550
+ if (entity && entity.label === "" && label !== null) {
1551
+ return this.planned(view, "setLabel", [
1552
+ {
1553
+ kind: "setEntityProperty",
1554
+ model: model.id,
1555
+ entity: entity.path,
1556
+ property: "label",
1557
+ value: label,
1558
+ },
1559
+ ]);
1560
+ }
1561
+ return this.planned(view, "setLabel", [
1562
+ { kind: "setLocalLabel", diagram: d, target: action.id, label },
1563
+ ]);
1564
+ }
1565
+ // A label typed on an arrow fills the model relation's when it has none; otherwise it is
1566
+ // this view's local label, which names the relation first.
1567
+ const source = model?.relationsByKey.get(relation.key);
1568
+ if (source && source.label === null && label !== null) {
1569
+ return this.planned(view, "setLabel", [
1570
+ {
1571
+ kind: "setModelRelationProperty",
1572
+ model: model.id,
1573
+ relation: source.key,
1574
+ property: "label",
1575
+ value: label,
1576
+ },
1577
+ ]);
1578
+ }
1579
+ if (!nameable(diagram, relation)) {
841
1580
  return {
842
1581
  messages: [
843
1582
  {
844
1583
  type: "rejected",
845
- code: "edit.unsafe-target",
846
- reason: wording().session.modelElsewhere,
1584
+ code: "edit.invalid-value",
1585
+ reason: wording().session.fixFirst,
847
1586
  },
848
1587
  ],
849
1588
  };
850
1589
  }
1590
+ if (relation.id === null && label === null)
1591
+ return { messages: [] };
1592
+ const { id, operations } = namedRelation(diagram, relation);
1593
+ this.followNaming(relation.key, id, operations);
1594
+ return this.planned(view, "setLabel", [
1595
+ ...operations,
1596
+ { kind: "setLocalLabel", diagram: d, target: id, label },
1597
+ ]);
1598
+ }
1599
+ case "renameId": {
1600
+ const note = diagram.elementsById.get(action.id);
1601
+ if (note && isViewNote(note)) {
1602
+ this.pendingSelection = this.selection.map((s) => (s === action.id ? action.newId : s));
1603
+ return this.planned(view, "renameId", [
1604
+ { kind: "renameElement", diagram: d, element: action.id, newId: action.newId },
1605
+ ]);
1606
+ }
1607
+ if (!model)
1608
+ return this.unresolved();
851
1609
  const entity = model.entitiesByPath.get(action.id);
852
1610
  const relation = model.relationsByKey.get(action.id);
853
1611
  const ops = entity
@@ -878,6 +1636,8 @@ export class CanvasSession {
878
1636
  }
879
1637
  }
880
1638
  act(action) {
1639
+ // Only the action that adds a tag opens the editor on a pill.
1640
+ this.pendingTag = undefined;
881
1641
  const story = this.storyAction(action);
882
1642
  if (story)
883
1643
  return story;
@@ -918,6 +1678,8 @@ export class CanvasSession {
918
1678
  return box ? center(box) : undefined;
919
1679
  };
920
1680
  const ops = [];
1681
+ /** An event whose statement the operations move, which the selection follows. */
1682
+ let follow;
921
1683
  if (diagram.type === "c4") {
922
1684
  const result = this.c4Action(view, action);
923
1685
  if (result)
@@ -926,11 +1688,23 @@ export class CanvasSession {
926
1688
  switch (action.kind) {
927
1689
  case "includeEntity":
928
1690
  break;
1691
+ case "stepTextSize":
1692
+ return this.stepTextSize(view, action.ids, action.step);
929
1693
  case "moveEvent":
930
1694
  case "toggleActivation":
1695
+ case "nestInCall":
1696
+ case "unnestCall":
1697
+ case "addReturn":
931
1698
  case "insertEvent":
932
- case "wrap": {
933
- if (diagram.type !== "sequence")
1699
+ case "wrap":
1700
+ case "moveActivation":
1701
+ case "addTag":
1702
+ case "setTag":
1703
+ case "removeTag": {
1704
+ // Tags are planned on any diagram, so that the edit says why only sequence diagrams
1705
+ // take them.
1706
+ const tagging = action.kind === "addTag" || action.kind === "setTag" || action.kind === "removeTag";
1707
+ if (diagram.type !== "sequence" && !tagging)
934
1708
  break;
935
1709
  const result = sequenceAction(view, action);
936
1710
  if (!result.ok) {
@@ -940,18 +1714,37 @@ export class CanvasSession {
940
1714
  }
941
1715
  if (result.select)
942
1716
  this.pendingSelection = result.select;
1717
+ if (result.follow)
1718
+ follow = diagram.eventsByKey.get(result.follow);
1719
+ if (result.editTag) {
1720
+ // The editor opens on the new pill once it is drawn.
1721
+ this.pendingEdit = result.editTag.owner;
1722
+ this.pendingTag = result.editTag.index;
1723
+ }
943
1724
  ops.push(...result.operations);
944
1725
  break;
945
1726
  }
946
1727
  case "move": {
947
1728
  if (diagram.type === "sequence") {
948
- ops.push(...sequenceMove(view, action.ids, action.delta));
1729
+ const moved = sequenceMove(view, action.ids, action.delta ?? { x: 0, y: 0 });
1730
+ ops.push(...moved);
949
1731
  this.pendingSelection = [...this.selection];
1732
+ const gap = moved.find((op) => op.kind === "setSpacing" && op.targetKind === "participant");
1733
+ if (gap?.kind === "setSpacing") {
1734
+ this.pendingGap = { participant: gap.target, amount: gap.amount ?? 0 };
1735
+ }
950
1736
  break;
951
1737
  }
1738
+ const refused = this.moveRefusal(view, action);
1739
+ if (refused) {
1740
+ return { messages: [{ type: "rejected", code: "edit.invalid-value", reason: refused }] };
1741
+ }
952
1742
  ops.push(...this.moveOperations(view, action));
953
1743
  // A box dropped on a box stays a drawing on top of it: say how to make it a container.
954
- const stack = stackedAfterMove(view, action.ids, action.delta);
1744
+ // Into a list it lands where the list puts it, on nothing.
1745
+ const stack = action.delta && !this.intoList(view, action)
1746
+ ? stackedAfterMove(view, action.ids, action.delta)
1747
+ : undefined;
955
1748
  if (stack) {
956
1749
  const planned = this.planned(view, action.kind, ops);
957
1750
  return planned.plan
@@ -970,10 +1763,28 @@ export class CanvasSession {
970
1763
  ops.push(...this.resizeOperations(view, action));
971
1764
  break;
972
1765
  case "moveLabel": {
1766
+ // A free line's pill moves as a relation's does, by the line's id.
1767
+ const line = diagram.elementsById.get(action.relation);
1768
+ if (line?.kind === "line") {
1769
+ const entry = diagram.placement.find((p) => p.statement === "label" && p.target === line.id);
1770
+ const offset = entry?.offset ?? { x: 0, y: 0 };
1771
+ ops.push({
1772
+ kind: "setLabelAnchor",
1773
+ diagram: d,
1774
+ relation: line.id,
1775
+ at: entry?.at ?? 50,
1776
+ offset: {
1777
+ x: Math.round(offset.x + action.delta.x),
1778
+ y: Math.round(offset.y + action.delta.y),
1779
+ },
1780
+ });
1781
+ break;
1782
+ }
973
1783
  const relation = diagram.relationsByKey.get(action.relation);
974
1784
  if (!relation)
975
1785
  break;
976
1786
  const { id, operations } = namedRelation(diagram, relation);
1787
+ this.followNaming(relation.key, id, operations);
977
1788
  const entry = relation.id
978
1789
  ? diagram.placement.find((p) => p.statement === "label" && p.target === relation.id)
979
1790
  : undefined;
@@ -990,11 +1801,96 @@ export class CanvasSession {
990
1801
  });
991
1802
  break;
992
1803
  }
1804
+ case "createLine": {
1805
+ // In the group under the press, its points in that group's interior; the armed kind's
1806
+ // dashes and heads as styles, with the effective theme's head.
1807
+ const id = freeElementId(this.analysis, diagram, "line");
1808
+ const origin = interiorOf(view, action.parent ?? null);
1809
+ const at = (p) => {
1810
+ const snapped = this.snap(p);
1811
+ return { x: snapped.x - origin.x, y: snapped.y - origin.y };
1812
+ };
1813
+ const head = view.layout?.theme?.arrowhead ?? "triangle";
1814
+ const styles = [
1815
+ ...(action.line === "dashed" ? [{ property: "line", value: "dashed" }] : []),
1816
+ ...(action.heads === "both" ? [{ property: "arrowtail", value: head }] : []),
1817
+ ...(action.heads !== "none" ? [{ property: "arrowhead", value: head }] : []),
1818
+ ];
1819
+ ops.push({
1820
+ kind: "createLine",
1821
+ diagram: d,
1822
+ id,
1823
+ from: at(action.from),
1824
+ to: at(action.to),
1825
+ ...(action.parent ? { parent: action.parent } : {}),
1826
+ ...(styles.length > 0 ? { styles } : {}),
1827
+ });
1828
+ this.pendingSelection = [id];
1829
+ break;
1830
+ }
1831
+ case "createPen": {
1832
+ // In the group under the first point, or the nearest one around it, that holds the whole
1833
+ // stroke and is no list (a list would grow with it and move its children), else at the
1834
+ // top level; its points in that group's interior as drawn, not snapped, and a group
1835
+ // without size kept as drawn, so that its interior stays where the stroke was drawn.
1836
+ // Nothing is selected and no editor opens: the next stroke starts at once.
1837
+ const style = action.style && Object.keys(action.style).length > 0 ? action.style : undefined;
1838
+ const parent = strokeParent(view, action.parent ?? null, action.points, style?.width ?? DEFAULT_PEN_WIDTH);
1839
+ const origin = interiorOf(view, parent);
1840
+ ops.push({
1841
+ kind: "createPen",
1842
+ diagram: d,
1843
+ points: action.points.map((p) => ({ x: p.x - origin.x, y: p.y - origin.y })),
1844
+ ...(parent !== null ? { parent } : {}),
1845
+ ...(style ? { style } : {}),
1846
+ }, ...steadySize(view, parent));
1847
+ break;
1848
+ }
1849
+ case "setPenPoints": {
1850
+ // In its group's interior, kept as drawn; out of a list. The edit refuses an element that
1851
+ // is not a pen.
1852
+ const element = diagram.elementsById.get(action.element);
1853
+ if (!element)
1854
+ break;
1855
+ if (element.kind !== "pen") {
1856
+ const origin = interiorOf(view, element.groupPath[element.groupPath.length - 1] ?? null);
1857
+ ops.push({
1858
+ kind: "setPenPoints",
1859
+ diagram: d,
1860
+ element: element.id,
1861
+ points: action.points.map((p) => ({ x: p.x - origin.x, y: p.y - origin.y })),
1862
+ });
1863
+ break;
1864
+ }
1865
+ const drawn = penOperations(view, element, action.points);
1866
+ ops.push(...drawn.operations, ...steadySize(view, drawn.parent));
1867
+ break;
1868
+ }
1869
+ case "setLinePoints": {
1870
+ const element = diagram.elementsById.get(action.element);
1871
+ if (element?.kind !== "line")
1872
+ break;
1873
+ const origin = interiorOf(view, element.groupPath[element.groupPath.length - 1] ?? null);
1874
+ const at = (p) => ({
1875
+ x: Math.round(p.x - origin.x),
1876
+ y: Math.round(p.y - origin.y),
1877
+ });
1878
+ ops.push({
1879
+ kind: "setLinePoints",
1880
+ diagram: d,
1881
+ element: element.id,
1882
+ from: at(action.from),
1883
+ to: at(action.to),
1884
+ via: action.via.map(at),
1885
+ });
1886
+ break;
1887
+ }
993
1888
  case "setVia": {
994
1889
  const relation = diagram.relationsByKey.get(action.relation);
995
1890
  if (!relation)
996
1891
  break;
997
1892
  const { id, operations } = namedRelation(diagram, relation);
1893
+ this.followNaming(relation.key, id, operations);
998
1894
  ops.push(...operations, {
999
1895
  kind: "setRoute",
1000
1896
  diagram: d,
@@ -1013,6 +1909,7 @@ export class CanvasSession {
1013
1909
  if (action.port === null && relation.id === null)
1014
1910
  break;
1015
1911
  const { id, operations } = namedRelation(diagram, relation);
1912
+ this.followNaming(relation.key, id, operations);
1016
1913
  ops.push(...operations, {
1017
1914
  kind: "setRoute",
1018
1915
  diagram: d,
@@ -1069,26 +1966,73 @@ export class CanvasSession {
1069
1966
  this.pendingEdit = id;
1070
1967
  break;
1071
1968
  }
1072
- const snapped = this.snap(action.at);
1969
+ // A position among a list's children, as a position among the declarations of its block.
1970
+ let index;
1971
+ if (action.index !== undefined) {
1972
+ const children = this.listChildren(view, action.parent);
1973
+ if (!children) {
1974
+ return {
1975
+ messages: [
1976
+ {
1977
+ type: "rejected",
1978
+ code: "edit.invalid-value",
1979
+ reason: words.session.notList(action.parent ?? ""),
1980
+ },
1981
+ ],
1982
+ };
1983
+ }
1984
+ // Before the child there, else right after the last one: the strokes and lines declared
1985
+ // after the children stay painted above the new one.
1986
+ const declared = this.declarations(view, action.parent);
1987
+ const anchor = children[action.index];
1988
+ const last = children[children.length - 1];
1989
+ if (anchor !== undefined)
1990
+ index = declared.indexOf(anchor);
1991
+ else if (last !== undefined)
1992
+ index = declared.indexOf(last) + 1;
1993
+ }
1994
+ // A note dropped on an element goes beside it; anything else where it was dropped.
1995
+ const snapped = kind === "note" ? this.notePlace(view, action) : this.snap(action.at);
1073
1996
  // Groups too go inside the group under the point, so groups nest.
1074
1997
  const origin = interiorOf(view, action.parent ?? null);
1075
- // A library element keeps its label; a shape from the palette, the keys or the grid is empty.
1076
- const label = action.label ?? (EMPTY_KINDS.has(kind) ? "" : undefined);
1998
+ // A library element keeps its label; a shape from the palette, the keys or the grid is
1999
+ // empty; a column or a row is named so in the interface language; a text or a sticky gets
2000
+ // its default text from the edit.
2001
+ const label = action.label ??
2002
+ (action.layout !== undefined
2003
+ ? words.written[action.layout]
2004
+ : EMPTY_KINDS.has(kind)
2005
+ ? ""
2006
+ : undefined);
1077
2007
  ops.push({
1078
2008
  kind: "createElement",
1079
2009
  diagram: d,
1080
2010
  elementKind: kind,
1081
2011
  id,
2012
+ // A list places its children: the edit writes no place for one.
1082
2013
  at: { x: snapped.x - origin.x, y: snapped.y - origin.y },
1083
2014
  ...(kind === "group" ? { size: NEW_GROUP_SIZE } : {}),
1084
2015
  ...(action.parent ? { parent: action.parent } : {}),
2016
+ ...(index !== undefined ? { index } : {}),
1085
2017
  ...(action.source !== undefined ? { source: action.source } : {}),
1086
2018
  ...(label !== undefined ? { label } : {}),
1087
2019
  ...(action.icon !== undefined ? { icon: action.icon } : {}),
1088
2020
  ...(action.shape !== undefined ? { shape: action.shape } : {}),
2021
+ ...(action.fill !== undefined ? { fill: action.fill } : {}),
2022
+ ...(action.layout !== undefined ? { layout: action.layout } : {}),
2023
+ ...(action.attachedTo !== undefined ? { attachedTo: action.attachedTo } : {}),
1089
2024
  });
1090
2025
  this.pendingSelection = [id];
1091
2026
  this.pendingEdit = id;
2027
+ // The selection is announced too; the status line ends on the attachment.
2028
+ if (action.attachedTo !== undefined) {
2029
+ this.pendingStatus = words.session.noteAttached(action.attachedTo);
2030
+ }
2031
+ break;
2032
+ }
2033
+ case "attachNote": {
2034
+ ops.push({ kind: "attachNote", diagram: d, note: action.note, element: action.element });
2035
+ this.pendingSelection = [action.note];
1092
2036
  break;
1093
2037
  }
1094
2038
  case "connect": {
@@ -1101,7 +2045,7 @@ export class CanvasSession {
1101
2045
  from: action.from,
1102
2046
  to: action.to,
1103
2047
  line: action.line,
1104
- ...(action.twoWay ? { twoWay: true } : {}),
2048
+ ...(action.heads ? { heads: action.heads } : {}),
1105
2049
  ...(pinned
1106
2050
  ? {
1107
2051
  id,
@@ -1122,22 +2066,60 @@ export class CanvasSession {
1122
2066
  const gone = new Set(elements);
1123
2067
  for (const id of elements)
1124
2068
  ops.push({ kind: "removeElement", diagram: d, element: id, cascade: action.cascade });
2069
+ // Messages and other events go unless a selected fragment or call around them goes with
2070
+ // its block; an activation goes with its other end, and so does one with an end inside a
2071
+ // fragment or a call that goes, so that no bar is left closing nothing. Both ends are read
2072
+ // here, on the text as it is, and each is removed alone: a removal planned before it in
2073
+ // the batch cannot pair it with another bar's end. The last statement goes first, so that
2074
+ // the keys of the ones before it, which count statements in order, stay what they are.
2075
+ const removed = new Set(action.ids);
2076
+ const inRemoved = (e) => e.path.some((p) => removed.has("fragment" in p ? p.fragment : p.message));
2077
+ const removals = new Map();
2078
+ /** An event removed by its exact key; an end of an activation without its other end. */
2079
+ const removeExactly = (event) => {
2080
+ const activation = event.kind === "activate" || event.kind === "deactivate";
2081
+ removals.set(event.key, {
2082
+ start: event.node.start,
2083
+ op: {
2084
+ kind: "removeEvent",
2085
+ diagram: d,
2086
+ event: event.key,
2087
+ ...(activation ? { alone: true } : {}),
2088
+ },
2089
+ });
2090
+ };
1125
2091
  for (const key of action.ids) {
1126
2092
  const relation = diagram.relationsByKey.get(key);
1127
- if (!relation || gone.has(relation.from) || gone.has(relation.to))
2093
+ if (relation) {
2094
+ const message = diagram.eventsByKey.get(key);
2095
+ if (gone.has(relation.from) || gone.has(relation.to))
2096
+ continue;
2097
+ if (message && inRemoved(message))
2098
+ continue;
2099
+ removals.set(key, {
2100
+ start: relation.range.start.offset,
2101
+ op: { kind: "removeRelation", diagram: d, relation: key },
2102
+ });
1128
2103
  continue;
1129
- ops.push({ kind: "removeRelation", diagram: d, relation: key });
2104
+ }
2105
+ const found = diagram.eventsByKey.get(key);
2106
+ if (!found || found.kind === "message")
2107
+ continue;
2108
+ for (const event of [found, activationPair(diagram, found)]) {
2109
+ if (event && !inRemoved(event))
2110
+ removeExactly(event);
2111
+ }
1130
2112
  }
1131
- // Events other than messages, unless a selected fragment around them goes too; the last
1132
- // first, so that a `deactivate` goes before its `activate`.
1133
- const removed = new Set(action.ids);
1134
- const events = action.ids
1135
- .map((key) => diagram.eventsByKey.get(key))
1136
- .filter((e) => e !== undefined && e.kind !== "message")
1137
- .filter((e) => !e.path.some((p) => removed.has(p.fragment)))
1138
- .sort((a, b) => b.node.start - a.node.start);
1139
- for (const event of events)
1140
- ops.push({ kind: "removeEvent", diagram: d, event: event.key });
2113
+ // The other end, outside, of an activation with an end in a fragment or call that goes.
2114
+ for (const event of allEvents(diagram.events)) {
2115
+ if (!inRemoved(event))
2116
+ continue;
2117
+ const pair = activationPair(diagram, event);
2118
+ if (pair && !inRemoved(pair))
2119
+ removeExactly(pair);
2120
+ }
2121
+ for (const { op } of [...removals.values()].sort((a, b) => b.start - a.start))
2122
+ ops.push(op);
1141
2123
  if (!action.cascade) {
1142
2124
  // Ask before removing what depends on the elements.
1143
2125
  const dependents = [];
@@ -1202,6 +2184,7 @@ export class CanvasSession {
1202
2184
  if (!relation)
1203
2185
  break;
1204
2186
  const { id, operations } = namedRelation(diagram, relation);
2187
+ this.followNaming(relation.key, id, operations);
1205
2188
  ops.push(...operations, {
1206
2189
  kind: "setRelationLabel",
1207
2190
  diagram: d,
@@ -1242,6 +2225,24 @@ export class CanvasSession {
1242
2225
  // The label is what the person would have typed: in the interface language.
1243
2226
  const label = defaultLabel("group");
1244
2227
  ops.push({ kind: "groupElements", diagram: d, elements: ids, id, label });
2228
+ // Cards of a list go into a list of the same layout where the first of them was, which
2229
+ // keeps them arranged: a plain group would need a place for each.
2230
+ // (The edit refuses elements of different groups.)
2231
+ const path = diagram.elementsById.get(ids[0]).groupPath;
2232
+ const owner = path[path.length - 1];
2233
+ const layout = diagram.type === "free" && owner !== undefined
2234
+ ? diagram.elementsById.get(owner)?.list?.layout
2235
+ : undefined;
2236
+ if (layout !== undefined) {
2237
+ ops.push({
2238
+ kind: "setElementProperty",
2239
+ diagram: d,
2240
+ target: id,
2241
+ targetKind: "element",
2242
+ property: "layout",
2243
+ value: layout,
2244
+ });
2245
+ }
1245
2246
  this.pendingSelection = [id];
1246
2247
  break;
1247
2248
  }
@@ -1266,6 +2267,13 @@ export class CanvasSession {
1266
2267
  }
1267
2268
  case "ungroup": {
1268
2269
  ops.push({ kind: "ungroup", diagram: d, group: action.id });
2270
+ // A list's children had no place: they get one where they are drawn, in the interior of
2271
+ // the group they go up to, which is kept as drawn; another list takes them in order.
2272
+ const group = diagram.elementsById.get(action.id);
2273
+ if (diagram.type === "free" && group?.list) {
2274
+ const parent = group.groupPath[group.groupPath.length - 1] ?? null;
2275
+ ops.push(...unlistedOperations(view, group.id, parent), ...steadySize(view, parent));
2276
+ }
1269
2277
  this.pendingSelection = diagram.elements
1270
2278
  .filter((e) => e.groupPath[e.groupPath.length - 1] === action.id)
1271
2279
  .map((e) => e.id);
@@ -1277,7 +2285,9 @@ export class CanvasSession {
1277
2285
  for (const id of action.ids) {
1278
2286
  const element = diagram.elementsById.get(id);
1279
2287
  const box = boxes.get(id);
1280
- if (element && element.kind !== "group" && box)
2288
+ // Lines and pens have no edges to align: they keep their points; a list places its
2289
+ // children.
2290
+ if (element && alignable(diagram, element) && box)
1281
2291
  selected.set(id, box);
1282
2292
  }
1283
2293
  const centres = action.kind === "align"
@@ -1338,7 +2348,11 @@ export class CanvasSession {
1338
2348
  this.pendingSelection = [...result.select];
1339
2349
  // Moving into a group of a free diagram writes the place in the group's interior, and a
1340
2350
  // group left empty gets its place and size.
1341
- const geometric = ["x", "y", "w", "h"].includes(action.field) ||
2351
+ // A rotation turns a box in place: an `auto` diagram keeps its arrangement first.
2352
+ // A list that stops placing its children writes their places, as a move does.
2353
+ const geometric = ["x", "y", "w", "h", "style.rotation"].includes(action.field) ||
2354
+ (action.field === "layout" &&
2355
+ result.operations.some((op) => op.kind === "setPosition")) ||
1342
2356
  (action.field === "inside" &&
1343
2357
  (diagram.type === "free" ||
1344
2358
  result.operations.some((op) => op.kind === "setPosition" || op.kind === "setSize")));
@@ -1351,6 +2365,20 @@ export class CanvasSession {
1351
2365
  }
1352
2366
  : planned;
1353
2367
  }
2368
+ case "setFields": {
2369
+ // The fields of one control written together: one plan, one undo step.
2370
+ const operations = [];
2371
+ for (const { field, value } of action.fields) {
2372
+ const result = fieldOperations(this.analysis, d, action.ids, field, value);
2373
+ if (!result.ok) {
2374
+ return {
2375
+ messages: [{ type: "rejected", code: "edit.invalid-value", reason: result.reason }],
2376
+ };
2377
+ }
2378
+ operations.push(...result.operations);
2379
+ }
2380
+ return this.planned(view, "setField", operations);
2381
+ }
1354
2382
  case "setIcon": {
1355
2383
  const targets = action.ids.filter((id) => {
1356
2384
  const element = diagram.elementsById.get(id);
@@ -1440,7 +2468,25 @@ export class CanvasSession {
1440
2468
  break;
1441
2469
  }
1442
2470
  }
1443
- return this.planned(view, action.kind, ops);
2471
+ const planned = this.planned(view, action.kind, ops);
2472
+ if (action.kind === "moveActivation" && planned.plan?.edits.length === 0) {
2473
+ // A bar's end dropped where it is writes nothing: the drawing as it is answers, so a canvas
2474
+ // that dropped before its preview came back waits for no edit.
2475
+ this.pendingSelection = undefined;
2476
+ const shown = this.rendered.get(view.id);
2477
+ return { messages: shown?.type === "render" ? [shown] : [] };
2478
+ }
2479
+ if (follow && planned.plan && planned.plan.edits.length > 0) {
2480
+ this.pendingMove = {
2481
+ diagram: d,
2482
+ key: follow.key,
2483
+ kind: follow.kind,
2484
+ // The inverse edits span, in the edited text, what the edit wrote.
2485
+ written: planned.plan.inverse,
2486
+ revision: this.analysis.revision,
2487
+ };
2488
+ }
2489
+ return planned;
1444
2490
  }
1445
2491
  /** Writes a new empty diagram of `type`, shown once the host has applied the edit. */
1446
2492
  createDiagram(type) {
@@ -1457,9 +2503,110 @@ export class CanvasSession {
1457
2503
  this.pendingDiagram = id;
1458
2504
  return { messages: [], plan: result.plan };
1459
2505
  }
2506
+ /**
2507
+ * Why a move is refused before it is planned, or undefined: an `index` into a group that is no
2508
+ * list, or a child of a list moved by a delta that leaves it in its list (a nudge, an Alt-drag,
2509
+ * a drop back into its own list without a position), which would write a `place` the list
2510
+ * ignores.
2511
+ */
2512
+ moveRefusal(view, action) {
2513
+ const words = wording().session;
2514
+ const diagram = view.diagram;
2515
+ if (action.index !== undefined && !this.intoList(view, action)) {
2516
+ return words.notList(action.parent ?? "");
2517
+ }
2518
+ if (action.index !== undefined)
2519
+ return undefined;
2520
+ const moved = new Set(action.ids);
2521
+ for (const id of action.ids) {
2522
+ const element = diagram.elementsById.get(id);
2523
+ if (!element || element.groupPath.some((g) => moved.has(g)))
2524
+ continue;
2525
+ const list = placingList(diagram, element);
2526
+ if (list === undefined)
2527
+ continue;
2528
+ // Dropped into another group or onto the top level, it leaves its list with a place.
2529
+ if (action.parent !== undefined && action.parent !== list)
2530
+ continue;
2531
+ return words.listPlaces(id, list);
2532
+ }
2533
+ return undefined;
2534
+ }
2535
+ /** Whether a move drops its elements into a list group, which places them. */
2536
+ intoList(view, action) {
2537
+ return this.listChildren(view, action.parent) !== undefined;
2538
+ }
2539
+ /**
2540
+ * The children a list group places, in their order (`ElementInfo.list`), or undefined when
2541
+ * `group` is not a list group of the free diagram.
2542
+ */
2543
+ listChildren(view, group) {
2544
+ if (group === undefined || group === null || view.diagram.type !== "free")
2545
+ return undefined;
2546
+ if (!view.diagram.elementsById.get(group)?.list)
2547
+ return undefined;
2548
+ return view.layout?.lists?.get(group)?.children ?? [];
2549
+ }
2550
+ /**
2551
+ * The ids declared in a group's block in source order, as `moveElement` and `createElement`
2552
+ * count positions: every declaration, its lines and pens included.
2553
+ */
2554
+ declarations(view, group) {
2555
+ return view.diagram.elements
2556
+ .filter((e) => e.node && e.groupPath[e.groupPath.length - 1] === group)
2557
+ .sort((a, b) => a.node.start - b.node.start)
2558
+ .map((e) => e.id);
2559
+ }
2560
+ /**
2561
+ * The `moveElement` operations that put `ids` into the list group `list`, one after another in
2562
+ * that order, before the child at `index` among its children once the moved ones are taken out,
2563
+ * or after the last of them without one (so that the strokes and lines declared after the
2564
+ * children stay painted above them). Each position counts the declarations of the list's block
2565
+ * as the text has them after the moves before it. A drop that leaves the list's order as it is
2566
+ * writes nothing.
2567
+ */
2568
+ listMoves(view, list, ids, index) {
2569
+ const diagram = view.diagram;
2570
+ const moving = new Set(ids);
2571
+ const children = this.listChildren(view, list) ?? [];
2572
+ const remaining = children.filter((id) => !moving.has(id));
2573
+ const anchor = index === undefined ? undefined : remaining[index];
2574
+ const order = anchor === undefined
2575
+ ? [...remaining, ...ids]
2576
+ : [...remaining.slice(0, index), ...ids, ...remaining.slice(index)];
2577
+ const kept = order.length === children.length &&
2578
+ order.every((id, i) => children[i] === id) &&
2579
+ !ids.some((id) => isPlaced(diagram, id));
2580
+ if (kept)
2581
+ return [];
2582
+ const declared = this.declarations(view, list);
2583
+ // Before the anchor, else after the last child placed so far.
2584
+ let after = anchor === undefined ? remaining[remaining.length - 1] : undefined;
2585
+ const position = () => anchor !== undefined
2586
+ ? declared.indexOf(anchor)
2587
+ : after !== undefined
2588
+ ? declared.indexOf(after) + 1
2589
+ : declared.length;
2590
+ const ops = [];
2591
+ for (const id of ids) {
2592
+ const own = declared.indexOf(id);
2593
+ const at = position();
2594
+ const stays = own >= 0 && (at === own || at === own + 1);
2595
+ if (!stays || isPlaced(diagram, id)) {
2596
+ ops.push({ kind: "moveElement", diagram: view.id, element: id, parent: list, index: at });
2597
+ }
2598
+ if (own >= 0)
2599
+ declared.splice(own, 1);
2600
+ declared.splice(position(), 0, id);
2601
+ if (anchor === undefined)
2602
+ after = id;
2603
+ }
2604
+ return ops;
2605
+ }
1460
2606
  /**
1461
2607
  * The operations of a move of elements of a flow or free diagram or a C4 view, as the drop
1462
- * writes them: new places, and the declarations of those that land in another group.
2608
+ * writes them: new places, and the declarations of those that land in another group. Into a
2609
+ * list group they are declared at the position it gives them, with no place.
1463
2610
  */
1464
2611
  moveOperations(view, action) {
1465
2612
  const diagram = view.diagram;
@@ -1471,26 +2618,47 @@ export class CanvasSession {
1471
2618
  return box ? center(box) : undefined;
1472
2619
  };
1473
2620
  const moved = new Set(action.ids.filter((id) => diagram.elementsById.has(id)));
2621
+ const delta = action.delta;
1474
2622
  const landed = (id) => {
1475
2623
  const c = centreOf(id);
1476
- return c ? { x: c.x + action.delta.x, y: c.y + action.delta.y } : undefined;
2624
+ return c && delta ? { x: c.x + delta.x, y: c.y + delta.y } : undefined;
2625
+ };
2626
+ const parentOf = (id) => {
2627
+ const path = diagram.elementsById.get(id)?.groupPath ?? [];
2628
+ return path[path.length - 1] ?? null;
1477
2629
  };
1478
2630
  // The outermost moved elements whose parent differs go into the target group, placed where
1479
2631
  // they land in its interior; what a moved group holds stays in it, and so do all the
1480
2632
  // children of a group without place, which moves with them.
1481
2633
  const reparented = new Set();
1482
- if (action.parent !== undefined) {
2634
+ if (action.parent !== undefined && this.intoList(view, action)) {
2635
+ // A list places what it takes, in the order given: those already in it are reordered.
2636
+ const target = action.parent;
2637
+ const entering = [...moved].filter((id) => {
2638
+ const element = diagram.elementsById.get(id);
2639
+ return !keepsPoints(element) && !element.groupPath.some((g) => moved.has(g));
2640
+ });
2641
+ ops.push(...this.listMoves(view, target, entering, action.index));
2642
+ for (const id of entering)
2643
+ reparented.add(id);
2644
+ ops.push(...steadyGroups(view, entering.filter((id) => parentOf(id) !== target), target));
2645
+ }
2646
+ else if (action.parent !== undefined) {
1483
2647
  const target = action.parent;
1484
2648
  const origin = interiorOf(view, target);
1485
2649
  const carried = carriedChildren(diagram.elements.map((e) => ({
1486
2650
  id: e.id,
1487
2651
  kind: e.kind,
1488
2652
  parent: e.groupPath[e.groupPath.length - 1] ?? null,
1489
- placed: isPlaced(diagram, e.id),
2653
+ // A list does not follow its children: it places them.
2654
+ placed: isPlaced(diagram, e.id) || e.list !== null,
1490
2655
  })), [...moved]);
1491
2656
  for (const id of moved) {
1492
2657
  const element = diagram.elementsById.get(id);
1493
2658
  const parent = element.groupPath[element.groupPath.length - 1] ?? null;
2659
+ // A free line or a pen stays in its group: its points are written there.
2660
+ if (keepsPoints(element))
2661
+ continue;
1494
2662
  if (parent === target || carried.has(id) || element.groupPath.some((g) => moved.has(g)))
1495
2663
  continue;
1496
2664
  reparented.add(id);
@@ -1511,10 +2679,52 @@ export class CanvasSession {
1511
2679
  ops.push(...steadyGroups(view, [...reparented], target));
1512
2680
  }
1513
2681
  if (diagram.type === "free") {
2682
+ // The groups strokes end in, kept as drawn once.
2683
+ const steady = new Set();
1514
2684
  // Children are placed inside their group, so a moved group carries them.
1515
2685
  for (const id of moved) {
1516
2686
  const element = diagram.elementsById.get(id);
1517
- if (reparented.has(id) || element.groupPath.some((g) => moved.has(g)))
2687
+ if (!delta || reparented.has(id) || element.groupPath.some((g) => moved.has(g)))
2688
+ continue;
2689
+ // A free line moves every point by the same amount, in its own coordinates.
2690
+ if (element.kind === "line" && element.line) {
2691
+ const by = (p) => ({
2692
+ x: Math.round(p.x + delta.x),
2693
+ y: Math.round(p.y + delta.y),
2694
+ });
2695
+ ops.push({
2696
+ kind: "setLinePoints",
2697
+ diagram: d,
2698
+ element: id,
2699
+ from: by(element.line.from),
2700
+ to: by(element.line.to),
2701
+ via: element.line.via.map(by),
2702
+ });
2703
+ continue;
2704
+ }
2705
+ // A pen moves every point too, as dragged: the grid does not apply to a stroke. Its group
2706
+ // stays as drawn, and out of a list it lands where it is dropped.
2707
+ if (element.kind === "pen" && element.pen) {
2708
+ const stroke = view.layout?.pens?.get(id);
2709
+ if (!stroke) {
2710
+ ops.push({
2711
+ kind: "setPenPoints",
2712
+ diagram: d,
2713
+ element: id,
2714
+ points: element.pen.points.map((p) => ({ x: p.x + delta.x, y: p.y + delta.y })),
2715
+ });
2716
+ continue;
2717
+ }
2718
+ const drawn = penOperations(view, element, stroke.points.map((p) => ({ x: p.x + delta.x, y: p.y + delta.y })));
2719
+ ops.push(...drawn.operations);
2720
+ if (!steady.has(drawn.parent)) {
2721
+ steady.add(drawn.parent);
2722
+ ops.push(...steadySize(view, drawn.parent));
2723
+ }
2724
+ continue;
2725
+ }
2726
+ // A list places its children (`moveRefusal` turns such a move down).
2727
+ if (placingList(diagram, element) !== undefined)
1518
2728
  continue;
1519
2729
  const to = landed(id);
1520
2730
  if (!to)
@@ -1524,6 +2734,8 @@ export class CanvasSession {
1524
2734
  }
1525
2735
  return ops;
1526
2736
  }
2737
+ if (!delta)
2738
+ return ops;
1527
2739
  const ids = new Set();
1528
2740
  for (const id of action.ids) {
1529
2741
  const element = diagram.elementsById.get(id);
@@ -1549,13 +2761,16 @@ export class CanvasSession {
1549
2761
  kind: "setPosition",
1550
2762
  diagram: d,
1551
2763
  element: id,
1552
- x: Math.round(c.x + action.delta.x),
1553
- y: Math.round(c.y + action.delta.y),
2764
+ x: Math.round(c.x + delta.x),
2765
+ y: Math.round(c.y + delta.y),
1554
2766
  });
1555
2767
  }
1556
2768
  return ops;
1557
2769
  }
1558
- /** The operations of a resize of an element of a flow or free diagram or a C4 view. */
2770
+ /**
2771
+ * The operations of a resize of an element of a flow or free diagram or a C4 view: its size and
2772
+ * its place, its size alone in a list, or a pen's points scaled to the box.
2773
+ */
1559
2774
  resizeOperations(view, action) {
1560
2775
  const diagram = view.diagram;
1561
2776
  const d = view.id;
@@ -1570,6 +2785,19 @@ export class CanvasSession {
1570
2785
  const c = writtenCenter(action.box);
1571
2786
  return [size, { kind: "setPosition", diagram: d, element: action.id, x: c.x, y: c.y }];
1572
2787
  }
2788
+ const element = diagram.elementsById.get(action.id);
2789
+ // A pen scales its points from its bounds to the box, its width kept: one `setPenPoints`, its
2790
+ // group kept as drawn, out of a list first.
2791
+ const stroke = element?.kind === "pen" ? view.layout?.pens?.get(action.id) : undefined;
2792
+ if (element && stroke) {
2793
+ const points = scalePenPoints(stroke.points, stroke.bounds, action.box, stroke.width);
2794
+ const drawn = penOperations(view, element, points);
2795
+ return [...drawn.operations, ...steadySize(view, drawn.parent)];
2796
+ }
2797
+ // A child of a list keeps the place its list gives it, and its own children theirs: a list
2798
+ // itself writes the size as its least size.
2799
+ if (element && placingList(diagram, element) !== undefined)
2800
+ return [size];
1573
2801
  const at = this.written(view, action.id, writtenCenter(action.box));
1574
2802
  const ops = [
1575
2803
  size,
@@ -1604,7 +2832,8 @@ export class CanvasSession {
1604
2832
  * the document (its project's imports) and the shown diagram laid out with the document's layout
1605
2833
  * options and drawn as the stage draws it. Nothing is written, sent or kept: the session's
1606
2834
  * state stays as it was. The drawing is null when the drop would be refused, the copy has more
1607
- * errors than the text, or the diagram is a sequence diagram.
2835
+ * errors than the text, or the diagram is a sequence diagram, whose only preview is the end of
2836
+ * an activation bar (`moveActivation`): refused, it carries the reason the drop would get.
1608
2837
  */
1609
2838
  preview(token, action) {
1610
2839
  const none = { messages: [{ type: "preview", token, svg: null }] };
@@ -1613,14 +2842,31 @@ export class CanvasSession {
1613
2842
  if (!view?.layout?.scene)
1614
2843
  return none;
1615
2844
  const diagram = view.diagram;
1616
- // Sequence diagrams move their own events live; only they preview scenarios.
1617
- if (diagram.type === "sequence")
2845
+ // Sequence diagrams move their own events live; only they preview scenarios. The end of an
2846
+ // activation bar is the one gesture they preview, as the session writes it, with the reason
2847
+ // when the order rules refuse it.
2848
+ if ((diagram.type === "sequence") !== (action.kind === "moveActivation"))
1618
2849
  return none;
1619
2850
  if (diagram.type === "c4" && refusal(wording().c4Refused, action, diagram))
1620
2851
  return none;
2852
+ if (action.kind === "moveActivation" && this.scenarioOf(view))
2853
+ return none;
2854
+ const refused = (reason) => ({
2855
+ messages: [{ type: "preview", token, svg: null, reason }],
2856
+ });
1621
2857
  let operations;
1622
- if (action.kind === "move")
2858
+ if (action.kind === "moveActivation") {
2859
+ const planned = sequenceAction(view, action);
2860
+ if (!planned.ok)
2861
+ return refused(planned.reason);
2862
+ operations = [...planned.operations];
2863
+ }
2864
+ else if (action.kind === "move") {
2865
+ // A move the drop would refuse has no drawing; a drop into a list shows it making room.
2866
+ if (this.moveRefusal(view, action))
2867
+ return none;
1623
2868
  operations = this.moveOperations(view, action);
2869
+ }
1624
2870
  else if (action.kind === "resize")
1625
2871
  operations = this.resizeOperations(view, action);
1626
2872
  else {
@@ -1630,9 +2876,25 @@ export class CanvasSession {
1630
2876
  return none;
1631
2877
  operations = [...field.operations];
1632
2878
  }
1633
- const result = this.plan(view, action.kind, operations);
1634
- if (!result?.ok)
1635
- return none;
2879
+ // A rotation previews as its release writes it: after the arrangement of an `auto` diagram.
2880
+ // A list that grows or shrinks keeps its corner, as on the drop.
2881
+ const kind = action.kind === "setField" ? "move" : action.kind;
2882
+ if (operations.length === 0 && action.kind === "moveActivation") {
2883
+ // A bar's end left where it is: the drawing as it is.
2884
+ const shown = this.rendered.get(view.id);
2885
+ if (shown?.type !== "render")
2886
+ return none;
2887
+ const { svg, width, height, offset } = shown;
2888
+ return { messages: [{ type: "preview", token, svg, width, height, offset }] };
2889
+ }
2890
+ const result = this.plan(view, kind, [
2891
+ ...operations,
2892
+ ...this.listCorners(view, kind, operations),
2893
+ ]);
2894
+ if (!result?.ok) {
2895
+ // The order rule the move breaks, which the canvas says while the handle is there.
2896
+ return result && action.kind === "moveActivation" ? refused(result.rejection.reason) : none;
2897
+ }
1636
2898
  const compilation = compile(applyTextEdits(analysis.compilation.text, result.plan.edits), analysis.compilation.options);
1637
2899
  const copy = compilation.document.diagrams.find((d) => d.id === view.id);
1638
2900
  if (!copy || copy.hasErrors || errorCount(compilation) > errorCount(analysis.compilation)) {
@@ -1645,7 +2907,13 @@ export class CanvasSession {
1645
2907
  if (!drawn)
1646
2908
  return none;
1647
2909
  const { width, height, offset } = drawn.geometry;
1648
- return { messages: [{ type: "preview", token, svg: drawn.svg, width, height, offset }] };
2910
+ // The turned element's box in the drawing, which the canvas turns it and its outline about.
2911
+ const box = action.kind === "setField" ? layout.boxes?.get(action.ids[0]) : undefined;
2912
+ return {
2913
+ messages: [
2914
+ { type: "preview", token, svg: drawn.svg, width, height, offset, ...(box ? { box } : {}) },
2915
+ ],
2916
+ };
1649
2917
  }
1650
2918
  }
1651
2919
  /** How many errors a compilation reports. */
@@ -1669,7 +2937,8 @@ function refusal(refused, action, diagram) {
1669
2937
  }
1670
2938
  if (action.kind === "create" && action.shape !== undefined)
1671
2939
  keys.push("create.shape");
1672
- if (action.kind === "connect" && action.twoWay)
2940
+ // A view's relations go one way with a head: no two-way arrow, no line.
2941
+ if (action.kind === "connect" && action.heads)
1673
2942
  keys.push("connect.twoWay");
1674
2943
  const key = keys.find((k) => Object.hasOwn(refused, k));
1675
2944
  return key === undefined ? undefined : refused[key];