@unofficialbox/box-open-elements 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +15 -13
  2. package/dist/components/actions/button.js +3 -3
  3. package/dist/components/actions/segmented-control.d.ts +1 -0
  4. package/dist/components/actions/segmented-control.js +20 -6
  5. package/dist/components/feedback/alert.js +1 -1
  6. package/dist/components/feedback/progress-bar.js +2 -2
  7. package/dist/components/forms/checkbox.js +4 -2
  8. package/dist/components/forms/code-editor.d.ts +61 -0
  9. package/dist/components/forms/code-editor.js +382 -0
  10. package/dist/components/forms/number-input.js +4 -1
  11. package/dist/core/form-associated.d.ts +1 -0
  12. package/dist/core/form-associated.js +3 -0
  13. package/dist/entries/code-editor.d.ts +1 -0
  14. package/dist/entries/code-editor.js +1 -0
  15. package/dist/foundations/icons/box-iconography.generated.d.ts +1 -472
  16. package/dist/foundations/icons/box-iconography.generated.js +2 -472
  17. package/dist/foundations/icons/glyphs/constants.d.ts +472 -0
  18. package/dist/foundations/icons/glyphs/constants.js +473 -0
  19. package/dist/foundations/icons/glyphs/index.d.ts +1 -1
  20. package/dist/foundations/icons/glyphs/index.js +1 -1
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +1 -0
  23. package/dist/patterns/flow-builder/flow-builder.d.ts +2 -0
  24. package/dist/patterns/flow-builder/flow-builder.js +14 -8
  25. package/dist/patterns/flow-builder/model.d.ts +2 -0
  26. package/dist/patterns/flow-builder/model.js +3 -3
  27. package/dist/patterns/flow-builder/primitives.d.ts +1 -0
  28. package/dist/patterns/flow-builder/primitives.js +4 -3
  29. package/dist/patterns/insights/metric-card.js +2 -2
  30. package/dist/patterns/process-modeler/index.d.ts +2 -0
  31. package/dist/patterns/process-modeler/index.js +2 -0
  32. package/dist/patterns/process-modeler/model.d.ts +95 -0
  33. package/dist/patterns/process-modeler/model.js +113 -0
  34. package/dist/patterns/process-modeler/process-modeler.d.ts +82 -0
  35. package/dist/patterns/process-modeler/process-modeler.js +861 -0
  36. package/package.json +18 -1
@@ -0,0 +1,861 @@
1
+ import { BaseElement } from "../../core/index.js";
2
+ import { announce } from "../../foundations/a11y/index.js";
3
+ import { boeFocusVisibleStyles } from "../../foundations/tokens/interaction.js";
4
+ import { completeLayout, ProcessHistory, validateProjection, } from "./model.js";
5
+ const svgElement = (tag) => document.createElementNS("http://www.w3.org/2000/svg", tag);
6
+ const emit = (element, name, detail) => element.dispatchEvent(new CustomEvent(name, {
7
+ detail,
8
+ bubbles: true,
9
+ composed: true,
10
+ cancelable: true,
11
+ }));
12
+ /** Projects host documents without owning workflow schemas or persistence. */
13
+ export class ProcessModeler extends BaseElement {
14
+ static tagName = "box-process-modeler";
15
+ static get observedAttributes() {
16
+ return ["locked", "snap-to-grid"];
17
+ }
18
+ documentValue;
19
+ modelValue = {
20
+ project: (document) => document,
21
+ };
22
+ projection = { boxes: [], lines: [] };
23
+ layoutValue = { boxes: {} };
24
+ catalogValue = [];
25
+ selectedId = null;
26
+ connecting = null;
27
+ checks = [];
28
+ renderer;
29
+ cleanupInspector;
30
+ inspected;
31
+ inspectedId;
32
+ historyValue = new ProcessHistory();
33
+ viewport = { x: 0, y: 0, zoom: 1 };
34
+ pointers = new Map();
35
+ drag;
36
+ pinchDistance = 0;
37
+ gestureZoom = 1;
38
+ get document() {
39
+ return this.documentValue;
40
+ }
41
+ get history() {
42
+ return this.historyValue;
43
+ }
44
+ set history(value) {
45
+ this.historyValue = value;
46
+ this.refresh();
47
+ }
48
+ set document(value) {
49
+ this.documentValue = value;
50
+ this.refresh();
51
+ }
52
+ get model() {
53
+ return this.modelValue;
54
+ }
55
+ set model(value) {
56
+ this.modelValue = value;
57
+ this.refresh();
58
+ }
59
+ get layout() {
60
+ return structuredClone(this.layoutValue);
61
+ }
62
+ set layout(value) {
63
+ if (JSON.stringify(this.layoutValue) === JSON.stringify(value))
64
+ return;
65
+ this.layoutValue = structuredClone(value);
66
+ if (!this.history.replaying)
67
+ this.history.clear();
68
+ this.refresh();
69
+ }
70
+ get catalog() {
71
+ return this.catalogValue;
72
+ }
73
+ set catalog(value) {
74
+ this.catalogValue = value;
75
+ this.refresh();
76
+ }
77
+ get locked() {
78
+ return this.hasAttribute("locked");
79
+ }
80
+ set locked(value) {
81
+ this.toggleAttribute("locked", value);
82
+ }
83
+ get snapToGrid() {
84
+ return this.hasAttribute("snap-to-grid");
85
+ }
86
+ set snapToGrid(value) {
87
+ this.toggleAttribute("snap-to-grid", value);
88
+ }
89
+ get selected() {
90
+ return (this.projection.boxes.find((box) => box.id === this.selectedId) ?? null);
91
+ }
92
+ get renderInspector() {
93
+ return this.renderer;
94
+ }
95
+ set renderInspector(value) {
96
+ this.cleanupInspector?.();
97
+ this.cleanupInspector = undefined;
98
+ this.inspected = undefined;
99
+ this.renderer = value;
100
+ this.refresh();
101
+ }
102
+ disconnectedCallback() {
103
+ this.cleanupInspector?.();
104
+ this.cleanupInspector = undefined;
105
+ this.inspected = undefined;
106
+ this.pointers.clear();
107
+ this.drag = undefined;
108
+ }
109
+ refresh() {
110
+ if (this.isRendered)
111
+ this.update();
112
+ }
113
+ select(id) {
114
+ if (!this.isRendered && this.documentValue !== undefined) {
115
+ this.projection = this.model.project(this.documentValue);
116
+ validateProjection(this.projection);
117
+ }
118
+ if (id !== null && !this.projection.boxes.some((box) => box.id === id))
119
+ return;
120
+ this.selectedId = id;
121
+ this.renderSelection();
122
+ if (this.isRendered)
123
+ this.updateToolbar();
124
+ emit(this, "selection-changed", { box: this.selected });
125
+ }
126
+ setValidation(checks) {
127
+ this.checks = checks;
128
+ this.refresh();
129
+ }
130
+ setValidationAtPath(message, path) {
131
+ this.setValidation(message ? [{ message, path }] : []);
132
+ }
133
+ requestEdit(edit) {
134
+ if (this.locked)
135
+ return;
136
+ const before = this.layout;
137
+ const previousIds = new Set(this.projection.boxes.map((box) => box.id));
138
+ let accepted = false;
139
+ const request = {
140
+ ...edit,
141
+ accept: (command) => {
142
+ if (accepted)
143
+ return;
144
+ accepted = true;
145
+ this.refresh();
146
+ if (edit.position &&
147
+ Number.isFinite(edit.position.x) &&
148
+ Number.isFinite(edit.position.y)) {
149
+ const added = this.projection.boxes.find((box) => !previousIds.has(box.id));
150
+ if (added)
151
+ this.layoutValue = this.translatedLayout(added.id, edit.position.x, edit.position.y);
152
+ }
153
+ const after = this.layout;
154
+ const restore = (layout) => {
155
+ this.layoutValue = structuredClone(layout);
156
+ this.refresh();
157
+ emit(this, "layout-changed", { layout: this.layout });
158
+ };
159
+ this.history.record({
160
+ undo: () => {
161
+ command.undo();
162
+ restore(before);
163
+ },
164
+ redo: () => {
165
+ command.redo();
166
+ restore(after);
167
+ },
168
+ });
169
+ this.refresh();
170
+ if (JSON.stringify(before) !== JSON.stringify(after))
171
+ emit(this, "layout-changed", { layout: this.layout });
172
+ announce("Process updated", "polite", this.ownerDocument);
173
+ },
174
+ };
175
+ emit(this, "process-edit-request", request);
176
+ }
177
+ undo() {
178
+ if (this.locked)
179
+ return;
180
+ this.history.undo();
181
+ this.refresh();
182
+ }
183
+ redo() {
184
+ if (this.locked)
185
+ return;
186
+ this.history.redo();
187
+ this.refresh();
188
+ }
189
+ commitLayout(next) {
190
+ const before = this.layout;
191
+ const after = structuredClone(next);
192
+ if (JSON.stringify(before) === JSON.stringify(after) || this.locked)
193
+ return;
194
+ const apply = (layout) => {
195
+ this.layoutValue = structuredClone(layout);
196
+ this.refresh();
197
+ emit(this, "layout-changed", { layout: this.layout });
198
+ };
199
+ apply(after);
200
+ this.history.record({
201
+ undo: () => apply(before),
202
+ redo: () => apply(after),
203
+ });
204
+ this.updateToolbar();
205
+ }
206
+ move(id, x, y) {
207
+ if (!Number.isFinite(x) ||
208
+ !Number.isFinite(y) ||
209
+ !this.layoutValue.boxes[id] ||
210
+ this.locked)
211
+ return;
212
+ const next = this.translatedLayout(id, x, y);
213
+ if (emit(this, "move-request", { boxId: id, position: next.boxes[id] }))
214
+ this.commitLayout(next);
215
+ }
216
+ translatedLayout(id, x, y) {
217
+ const snap = (n) => (this.snapToGrid ? Math.round(n / 20) * 20 : n);
218
+ const next = this.layout;
219
+ next.boxes[id] = { ...next.boxes[id], x: snap(x), y: snap(y) };
220
+ const dx = next.boxes[id].x - this.layoutValue.boxes[id].x;
221
+ const dy = next.boxes[id].y - this.layoutValue.boxes[id].y;
222
+ const descendants = new Set([id]);
223
+ for (let changed = true; changed;) {
224
+ changed = false;
225
+ for (const box of this.projection.boxes) {
226
+ if (box.parentId &&
227
+ descendants.has(box.parentId) &&
228
+ !descendants.has(box.id)) {
229
+ descendants.add(box.id);
230
+ changed = true;
231
+ next.boxes[box.id] = {
232
+ ...next.boxes[box.id],
233
+ x: next.boxes[box.id].x + dx,
234
+ y: next.boxes[box.id].y + dy,
235
+ };
236
+ }
237
+ }
238
+ }
239
+ return next;
240
+ }
241
+ tidy() {
242
+ if (this.locked)
243
+ return;
244
+ this.commitLayout(completeLayout(this.projection, { ...this.layout, boxes: {} }));
245
+ this.fit();
246
+ }
247
+ resize(id, width, height) {
248
+ if (!this.layoutValue.boxes[id] ||
249
+ !Number.isFinite(width) ||
250
+ !Number.isFinite(height))
251
+ return;
252
+ const next = this.layout;
253
+ next.boxes[id] = {
254
+ ...next.boxes[id],
255
+ width: Math.max(120, width),
256
+ height: Math.max(64, height),
257
+ };
258
+ this.commitLayout(next);
259
+ }
260
+ routeLine(id, points) {
261
+ if (!this.projection.lines.some((line) => line.id === id) ||
262
+ points.some((point) => !Number.isFinite(point.x) || !Number.isFinite(point.y)))
263
+ return;
264
+ this.commitLayout({
265
+ ...this.layout,
266
+ lines: {
267
+ ...this.layoutValue.lines,
268
+ [id]: points.map((point) => ({ ...point })),
269
+ },
270
+ });
271
+ }
272
+ setNote(note, id = note?.id) {
273
+ if (!id || (note && (!Number.isFinite(note.x) || !Number.isFinite(note.y))))
274
+ return;
275
+ this.commitLayout({
276
+ ...this.layout,
277
+ notes: [
278
+ ...(this.layoutValue.notes ?? []).filter((item) => item.id !== id),
279
+ ...(note ? [{ ...note, id }] : []),
280
+ ],
281
+ });
282
+ }
283
+ setSection(section, id = section?.id) {
284
+ if (!id ||
285
+ (section &&
286
+ [section.x, section.y, section.width, section.height].some((value) => !Number.isFinite(value))))
287
+ return;
288
+ this.commitLayout({
289
+ ...this.layout,
290
+ sections: [
291
+ ...(this.layoutValue.sections ?? []).filter((item) => item.id !== id),
292
+ ...(section ? [{ ...section, id }] : []),
293
+ ],
294
+ });
295
+ }
296
+ zoomBy(factor) {
297
+ if (!Number.isFinite(factor) || factor <= 0)
298
+ return;
299
+ const viewport = this.shadowRoot.querySelector("[part=canvas]");
300
+ const centerX = viewport.clientWidth / 2;
301
+ const centerY = viewport.clientHeight / 2;
302
+ const previous = this.viewport.zoom;
303
+ const next = Math.max(0.25, Math.min(3, previous * factor));
304
+ this.viewport.x = centerX - ((centerX - this.viewport.x) / previous) * next;
305
+ this.viewport.y = centerY - ((centerY - this.viewport.y) / previous) * next;
306
+ this.viewport.zoom = next;
307
+ this.paintViewport();
308
+ }
309
+ fit() {
310
+ const bounds = this.bounds();
311
+ const canvas = this.shadowRoot.querySelector("[part=canvas]");
312
+ this.viewport.zoom = Math.max(0.25, Math.min(1.5, ((canvas.clientWidth || 700) - 40) / bounds.width, ((canvas.clientHeight || 480) - 40) / bounds.height));
313
+ this.viewport.x = 20 - bounds.x * this.viewport.zoom;
314
+ this.viewport.y = 20 - bounds.y * this.viewport.zoom;
315
+ this.paintViewport();
316
+ }
317
+ bounds() {
318
+ const positions = this.projection.boxes.map((box) => this.layoutValue.boxes[box.id]);
319
+ const x = Math.min(0, ...positions.map((pos) => pos.x));
320
+ const y = Math.min(0, ...positions.map((pos) => pos.y));
321
+ return {
322
+ x,
323
+ y,
324
+ width: Math.max(100, ...positions.map((pos) => pos.x + (pos.width ?? 220))) -
325
+ x,
326
+ height: Math.max(100, ...positions.map((pos) => pos.y + (pos.height ?? 96))) -
327
+ y,
328
+ };
329
+ }
330
+ renderTemplate() {
331
+ this.shadowRoot.innerHTML = `<style>
332
+ :host{display:block;font:inherit;color:var(--boe-token-text-text,#222);min-width:0}:host([hidden]){display:none!important}
333
+ *{box-sizing:border-box}button,input{font:inherit;color:inherit}button{background:var(--boe-token-surface-surface,#fff);border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:8px;padding:8px;min-height:36px;cursor:pointer}button:disabled{opacity:.5;cursor:default}
334
+ ${boeFocusVisibleStyles(":is(button,input,[part=canvas],[part=minimap])")}
335
+ [part=toolbar]{display:flex;flex-wrap:wrap;gap:8px;margin-bottom:12px}
336
+ [part=layout]{display:grid;grid-template-columns:minmax(0,1fr) minmax(220px,280px);gap:16px}
337
+ [part=canvas]{position:relative;overflow:hidden;height:var(--boe-process-height,560px);background:var(--boe-token-surface-surface-secondary,#fbfbfb);border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:12px;touch-action:none}
338
+ [part=world]{position:absolute;inset:0;transform-origin:0 0}
339
+ [part=lines]{position:absolute;inset:0;width:1px;height:1px;overflow:visible;pointer-events:none}
340
+ [part=line]{stroke:var(--boe-token-text-text-secondary,#666);stroke-width:2;fill:none}
341
+ [part=box],[part=frame]{position:absolute;padding:12px;display:flex;flex-direction:column;gap:6px;text-align:start;background:var(--boe-token-surface-surface,#fff);border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:8px;touch-action:none}
342
+ [part=box] strong,[part=frame] strong{overflow-wrap:anywhere}[part=box] small{color:var(--boe-token-text-text-secondary,#666)}
343
+ [part=frame]{background:color-mix(in srgb,var(--boe-token-surface-surface-brand,#0061d5) 4%,var(--boe-token-surface-surface,#fff));justify-content:flex-start}
344
+ [aria-pressed=true]{border-color:var(--boe-token-surface-surface-brand,#0061d5)}[data-invalid=true]{border-color:var(--boe-token-text-status-text-error,#b92340);border-width:2px}
345
+ [part=connection]{position:absolute;display:flex;gap:4px;align-items:center;background:var(--boe-token-surface-surface,#fff);border-radius:8px;padding:4px;font-size:12px;white-space:nowrap}
346
+ [part=connection] button{font-size:12px;min-height:32px;padding:4px}
347
+ [part=inspector]{min-width:0;padding:12px;border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:12px;background:var(--boe-token-surface-surface,#fff)}
348
+ [part=palette]{display:grid;gap:8px}[part=palette] input{width:100%;min-width:0;padding:8px;background:var(--boe-token-surface-surface,#fff);border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:8px}
349
+ [part=choices]{display:grid;gap:6px;max-height:300px;overflow:auto}[part=group]{font-weight:600;margin:8px 0 0}
350
+ [part=checks]{display:grid;gap:6px;margin-top:12px}[part=checks] button{text-align:start;color:var(--boe-token-text-status-text-error,#b92340)}
351
+ [part=minimap]{position:absolute;right:12px;bottom:12px;width:150px;height:90px;background:var(--boe-token-surface-surface,#fff);border:1px solid var(--boe-token-stroke-stroke,#ddd);border-radius:8px;touch-action:none}
352
+ [part=minimap] rect{fill:var(--boe-token-surface-surface-brand,#0061d5)}[part=minimap] [data-viewport]{fill:none;stroke:var(--boe-token-text-text,#222);stroke-width:3}
353
+ [part=icon]{width:20px;height:20px;color:var(--boe-token-surface-surface-brand,#0061d5)}[part=icon] svg{width:20px;height:20px}
354
+ [part=note],[part=section]{position:absolute;border:1px dashed var(--boe-token-stroke-stroke,#ddd);background:var(--boe-token-surface-surface,#fff);padding:8px;pointer-events:none}
355
+ [part=help]{font-size:13px;color:var(--boe-token-text-text-secondary,#666)}
356
+ @media(max-width:800px){[part=layout]{grid-template-columns:minmax(0,1fr)}[part=canvas]{height:420px}}
357
+ @media(prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important;scroll-behavior:auto!important}}
358
+ </style><div part="toolbar" role="toolbar" aria-label="Diagram controls"><button data-command="zoom-out">Zoom out</button><button data-command="reset">100%</button><button data-command="zoom-in">Zoom in</button><button data-command="fit">Fit the whole process</button><button data-command="tidy">Tidy up</button><button data-command="undo">Undo</button><button data-command="redo">Redo</button><button data-command="snap" aria-pressed="false">Snap to grid</button><button data-command="lock" aria-pressed="false">Lock diagram</button></div><div part="layout"><div part="canvas" tabindex="0" role="region" aria-label="Process diagram" aria-describedby="process-help"><div part="world"></div><svg part="minimap" role="img" aria-label="Diagram overview. Click to jump"></svg></div><div part="inspector" role="region" aria-label="Process details"><div part="palette"><label>Find a building block<input type="search" part="search"></label><div part="choices"></div></div><div part="editor"></div><div part="checks" role="region" aria-label="Checks"></div></div></div><p part="help" id="process-help">Select a box. Arrow keys move it; C then another box connects it; Delete removes it. Drag the background to pan. Use zoom controls or pinch to zoom. Escape cancels connecting.</p><div part="status" role="status" aria-live="polite"></div>`;
359
+ }
360
+ setupListeners() {
361
+ const toolbar = this.shadowRoot.querySelector("[part=toolbar]");
362
+ for (const [command, label] of [
363
+ ["connect", "Connect selected"],
364
+ ["delete", "Delete selected"],
365
+ ]) {
366
+ const button = document.createElement("button");
367
+ button.type = "button";
368
+ button.dataset.command = command;
369
+ button.textContent = label;
370
+ toolbar.append(button);
371
+ }
372
+ this.shadowRoot.querySelector("[part=toolbar]").addEventListener("click", (event) => {
373
+ const command = event.target.closest("button")?.dataset.command;
374
+ if (command === "zoom-in")
375
+ this.zoomBy(1.2);
376
+ else if (command === "zoom-out")
377
+ this.zoomBy(1 / 1.2);
378
+ else if (command === "reset")
379
+ this.zoomBy(1 / this.viewport.zoom);
380
+ else if (command === "fit")
381
+ this.fit();
382
+ else if (command === "tidy")
383
+ this.tidy();
384
+ else if (command === "undo")
385
+ this.undo();
386
+ else if (command === "redo")
387
+ this.redo();
388
+ else if (command === "snap")
389
+ this.snapToGrid = !this.snapToGrid;
390
+ else if (command === "lock")
391
+ this.locked = !this.locked;
392
+ else if (command === "connect" && this.selected && !this.locked) {
393
+ this.connecting = this.selected.id;
394
+ this.setStatus(`Select the next box to connect from ${this.selected.title}`);
395
+ }
396
+ else if (command === "delete" && this.selected)
397
+ this.requestEdit({ type: "delete", boxId: this.selected.id });
398
+ });
399
+ this.shadowRoot.querySelector("[part=search]").addEventListener("input", () => this.renderPalette());
400
+ const canvas = this.shadowRoot.querySelector("[part=canvas]");
401
+ canvas.addEventListener("keydown", (event) => this.onKey(event));
402
+ canvas.addEventListener("wheel", (event) => {
403
+ event.preventDefault();
404
+ if (event.ctrlKey || event.metaKey)
405
+ this.zoomBy(Math.exp(-event.deltaY * 0.01));
406
+ else {
407
+ this.viewport.x -= event.deltaX;
408
+ this.viewport.y -= event.deltaY;
409
+ this.paintViewport();
410
+ }
411
+ }, { passive: false });
412
+ canvas.addEventListener("pointerdown", (event) => this.pointerDown(event));
413
+ canvas.addEventListener("pointermove", (event) => this.pointerMove(event));
414
+ canvas.addEventListener("pointerup", (event) => this.pointerEnd(event, true));
415
+ canvas.addEventListener("pointercancel", (event) => this.pointerEnd(event, false));
416
+ canvas.addEventListener("gesturestart", (event) => {
417
+ event.preventDefault();
418
+ this.gestureZoom = this.viewport.zoom;
419
+ });
420
+ canvas.addEventListener("gesturechange", (event) => {
421
+ event.preventDefault();
422
+ const scale = event.scale;
423
+ if (scale > 0)
424
+ this.zoomBy((this.gestureZoom * scale) / this.viewport.zoom);
425
+ });
426
+ const minimap = this.shadowRoot.querySelector("[part=minimap]");
427
+ minimap.setAttribute("tabindex", "0");
428
+ minimap.setAttribute("role", "button");
429
+ minimap.setAttribute("aria-label", "Diagram overview. Arrow keys pan; Enter fits the whole process");
430
+ minimap.addEventListener("keydown", (event) => {
431
+ const key = event;
432
+ key.stopPropagation();
433
+ if (key.key === "Enter" || key.key === " ") {
434
+ key.preventDefault();
435
+ this.fit();
436
+ return;
437
+ }
438
+ const direction = {
439
+ ArrowLeft: [100, 0],
440
+ ArrowRight: [-100, 0],
441
+ ArrowUp: [0, 100],
442
+ ArrowDown: [0, -100],
443
+ }[key.key];
444
+ if (direction) {
445
+ key.preventDefault();
446
+ this.viewport.x += direction[0];
447
+ this.viewport.y += direction[1];
448
+ this.paintViewport();
449
+ }
450
+ });
451
+ minimap.addEventListener("pointerdown", (event) => {
452
+ event.stopPropagation();
453
+ const rect = minimap.getBoundingClientRect();
454
+ if (!rect.width || !rect.height)
455
+ return;
456
+ const bounds = this.bounds();
457
+ this.viewport.x =
458
+ canvas.clientWidth / 2 -
459
+ (bounds.x + ((event.clientX - rect.left) / rect.width) * bounds.width) *
460
+ this.viewport.zoom;
461
+ this.viewport.y =
462
+ canvas.clientHeight / 2 -
463
+ (bounds.y +
464
+ ((event.clientY - rect.top) / rect.height) * bounds.height) *
465
+ this.viewport.zoom;
466
+ this.paintViewport();
467
+ });
468
+ }
469
+ onKey(event) {
470
+ if (event.target.closest("input,textarea,select,[contenteditable]"))
471
+ return;
472
+ if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "z") {
473
+ event.preventDefault();
474
+ if (event.shiftKey)
475
+ this.redo();
476
+ else
477
+ this.undo();
478
+ return;
479
+ }
480
+ if (event.key === "Escape") {
481
+ this.connecting = null;
482
+ this.setStatus("Connecting cancelled");
483
+ return;
484
+ }
485
+ const box = this.selected;
486
+ if (!box || this.locked)
487
+ return;
488
+ if (event.key.toLowerCase() === "c") {
489
+ event.preventDefault();
490
+ this.connecting = box.id;
491
+ this.setStatus(`Select the next box to connect from ${box.title}`);
492
+ }
493
+ if (event.key === "Delete" || event.key === "Backspace") {
494
+ event.preventDefault();
495
+ this.requestEdit({ type: "delete", boxId: box.id });
496
+ }
497
+ const direction = {
498
+ ArrowLeft: [-1, 0],
499
+ ArrowRight: [1, 0],
500
+ ArrowUp: [0, -1],
501
+ ArrowDown: [0, 1],
502
+ }[event.key];
503
+ if (direction) {
504
+ event.preventDefault();
505
+ const pos = this.layoutValue.boxes[box.id];
506
+ const distance = event.shiftKey ? 100 : this.snapToGrid ? 20 : 10;
507
+ this.move(box.id, pos.x + direction[0] * distance, pos.y + direction[1] * distance);
508
+ this.focusBox(box.id);
509
+ }
510
+ }
511
+ pointerDown(event) {
512
+ if (event.button !== 0 ||
513
+ event.target.closest("[part=connection]"))
514
+ return;
515
+ this.pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
516
+ if (this.pointers.size === 2) {
517
+ this.drag = undefined;
518
+ this.pinchDistance = this.distance();
519
+ this.refresh();
520
+ return;
521
+ }
522
+ const target = event.target.closest("[data-box-id]");
523
+ const id = target?.dataset.boxId;
524
+ const canvas = this.shadowRoot.querySelector("[part=canvas]");
525
+ (target ?? canvas).setPointerCapture?.(event.pointerId);
526
+ this.drag = {
527
+ id: this.locked ? undefined : id,
528
+ x: event.clientX,
529
+ y: event.clientY,
530
+ position: id ? { ...this.layoutValue.boxes[id] } : undefined,
531
+ panX: this.viewport.x,
532
+ panY: this.viewport.y,
533
+ };
534
+ }
535
+ distance() {
536
+ const [a, b] = [...this.pointers.values()];
537
+ return a && b ? Math.hypot(a.x - b.x, a.y - b.y) : 0;
538
+ }
539
+ pointerMove(event) {
540
+ if (!this.pointers.has(event.pointerId))
541
+ return;
542
+ this.pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
543
+ if (this.pointers.size === 2) {
544
+ const distance = this.distance();
545
+ if (this.pinchDistance)
546
+ this.zoomBy(distance / this.pinchDistance);
547
+ this.pinchDistance = distance;
548
+ return;
549
+ }
550
+ if (!this.drag)
551
+ return;
552
+ const dx = event.clientX - this.drag.x;
553
+ const dy = event.clientY - this.drag.y;
554
+ if (this.drag.id && this.drag.position) {
555
+ const element = Array.from(this.shadowRoot.querySelectorAll("[data-box-id]")).find((box) => box.dataset.boxId === this.drag.id);
556
+ if (element) {
557
+ element.style.left = `${this.drag.position.x + dx / this.viewport.zoom}px`;
558
+ element.style.top = `${this.drag.position.y + dy / this.viewport.zoom}px`;
559
+ }
560
+ }
561
+ else {
562
+ this.viewport.x = this.drag.panX + dx;
563
+ this.viewport.y = this.drag.panY + dy;
564
+ this.paintViewport();
565
+ }
566
+ }
567
+ pointerEnd(event, commit) {
568
+ this.pointers.delete(event.pointerId);
569
+ const drag = this.drag;
570
+ this.drag = undefined;
571
+ this.pinchDistance = 0;
572
+ if (drag?.id && drag.position) {
573
+ const dx = event.clientX - drag.x;
574
+ const dy = event.clientY - drag.y;
575
+ if (commit && Math.hypot(dx, dy) > 4)
576
+ this.move(drag.id, drag.position.x + dx / this.viewport.zoom, drag.position.y + dy / this.viewport.zoom);
577
+ else if (!commit)
578
+ this.refresh();
579
+ }
580
+ }
581
+ update() {
582
+ if (this.documentValue !== undefined) {
583
+ this.projection = this.model.project(this.documentValue);
584
+ validateProjection(this.projection);
585
+ }
586
+ else
587
+ this.projection = { boxes: [], lines: [] };
588
+ this.layoutValue = completeLayout(this.projection, this.layoutValue);
589
+ if (this.selectedId && !this.selected)
590
+ this.selectedId = null;
591
+ const focused = this.shadowRoot.activeElement
592
+ ?.dataset.boxId;
593
+ this.renderWorld();
594
+ this.renderPalette();
595
+ this.renderChecks();
596
+ this.renderSelection();
597
+ this.paintViewport();
598
+ this.updateToolbar();
599
+ if (focused)
600
+ this.focusBox(focused);
601
+ }
602
+ renderWorld() {
603
+ const world = this.shadowRoot.querySelector("[part=world]");
604
+ world.replaceChildren();
605
+ for (const section of this.layoutValue.sections ?? []) {
606
+ const element = document.createElement("div");
607
+ element.setAttribute("part", "section");
608
+ element.textContent = section.title;
609
+ this.place(element, section);
610
+ world.append(element);
611
+ }
612
+ for (const box of [...this.projection.boxes].sort((a, b) => Number(Boolean(b.frame)) - Number(Boolean(a.frame)))) {
613
+ const element = document.createElement("button");
614
+ element.type = "button";
615
+ element.setAttribute("part", box.frame ? "frame" : "box");
616
+ element.dataset.boxId = box.id;
617
+ element.setAttribute("aria-label", [box.title, box.description, box.frame ? "Frame" : ""]
618
+ .filter(Boolean)
619
+ .join(". "));
620
+ element.setAttribute("aria-pressed", String(box.id === this.selectedId));
621
+ this.place(element, this.layoutValue.boxes[box.id]);
622
+ const kind = this.catalog.find((kind) => kind.kind === box.kind);
623
+ if (kind?.icon) {
624
+ const icon = document.createElement("span");
625
+ icon.setAttribute("part", "icon");
626
+ icon.setAttribute("aria-hidden", "true");
627
+ icon.append(kind.icon());
628
+ element.append(icon);
629
+ }
630
+ const title = document.createElement("strong");
631
+ title.textContent = box.title;
632
+ element.append(title);
633
+ if (box.description) {
634
+ const description = document.createElement("small");
635
+ description.textContent = box.description;
636
+ element.append(description);
637
+ }
638
+ element.addEventListener("click", () => {
639
+ if (this.connecting && this.connecting !== box.id) {
640
+ this.requestEdit({
641
+ type: "connect",
642
+ from: this.connecting,
643
+ to: box.id,
644
+ });
645
+ this.connecting = null;
646
+ }
647
+ this.select(box.id);
648
+ });
649
+ world.append(element);
650
+ }
651
+ const lines = svgElement("svg");
652
+ lines.setAttribute("part", "lines");
653
+ lines.setAttribute("aria-hidden", "true");
654
+ world.append(lines);
655
+ for (const line of this.projection.lines) {
656
+ const from = this.layoutValue.boxes[line.from];
657
+ const to = this.layoutValue.boxes[line.to];
658
+ const x1 = from.x + (from.width ?? 220) / 2;
659
+ const y1 = from.y + (from.height ?? 96);
660
+ const x2 = to.x + (to.width ?? 220) / 2;
661
+ const y2 = to.y;
662
+ const path = svgElement("polyline");
663
+ path.setAttribute("part", "line");
664
+ path.setAttribute("points", [
665
+ { x: x1, y: y1 },
666
+ ...(this.layoutValue.lines?.[line.id] ?? line.points ?? []),
667
+ { x: x2, y: y2 },
668
+ ]
669
+ .map((point) => `${point.x},${point.y}`)
670
+ .join(" "));
671
+ if (line.dashed)
672
+ path.setAttribute("stroke-dasharray", "6 4");
673
+ lines.append(path);
674
+ const label = document.createElement("div");
675
+ label.setAttribute("part", "connection");
676
+ label.style.left = `${(x1 + x2) / 2}px`;
677
+ label.style.top = `${(y1 + y2) / 2}px`;
678
+ const names = `${this.projection.boxes.find((box) => box.id === line.from).title} to ${this.projection.boxes.find((box) => box.id === line.to).title}${line.label ? `, ${line.label}` : ""}`;
679
+ const text = document.createElement("span");
680
+ text.textContent = line.label ?? "Connection";
681
+ label.append(text);
682
+ for (const [type, title] of [
683
+ ["insert", "Insert a step here"],
684
+ ["disconnect", "Remove connection"],
685
+ ]) {
686
+ const button = document.createElement("button");
687
+ button.type = "button";
688
+ button.textContent = type === "insert" ? "+" : "Remove";
689
+ button.setAttribute("aria-label", `${title}: ${names}`);
690
+ button.disabled = this.locked;
691
+ button.addEventListener("click", () => this.requestEdit({
692
+ type,
693
+ lineId: line.id,
694
+ from: line.from,
695
+ to: line.to,
696
+ }));
697
+ label.append(button);
698
+ }
699
+ world.append(label);
700
+ }
701
+ for (const note of this.layoutValue.notes ?? []) {
702
+ const element = document.createElement("div");
703
+ element.setAttribute("part", "note");
704
+ element.textContent = note.text;
705
+ this.place(element, { ...note, width: 180, height: 80 });
706
+ world.append(element);
707
+ }
708
+ }
709
+ place(element, position) {
710
+ Object.assign(element.style, {
711
+ left: `${position.x}px`,
712
+ top: `${position.y}px`,
713
+ width: `${position.width ?? 220}px`,
714
+ height: `${position.height ?? 96}px`,
715
+ });
716
+ }
717
+ renderPalette() {
718
+ const choices = this.shadowRoot.querySelector("[part=choices]");
719
+ const query = this.shadowRoot.querySelector("[part=search]").value.toLowerCase();
720
+ choices.replaceChildren();
721
+ let group = "";
722
+ for (const kind of this.catalog.filter((kind) => `${kind.label} ${kind.description} ${kind.group ?? ""}`
723
+ .toLowerCase()
724
+ .includes(query))) {
725
+ if (kind.group && kind.group !== group) {
726
+ group = kind.group;
727
+ const heading = document.createElement("p");
728
+ heading.setAttribute("part", "group");
729
+ heading.textContent = group;
730
+ choices.append(heading);
731
+ }
732
+ const button = document.createElement("button");
733
+ button.type = "button";
734
+ button.textContent = kind.label;
735
+ button.disabled = this.locked;
736
+ button.draggable = !this.locked;
737
+ button.addEventListener("click", () => this.requestEdit({ type: "add", kind, position: { x: 40, y: 40 } }));
738
+ button.addEventListener("dragstart", (event) => event.dataTransfer?.setData("application/boe-process-kind", kind.kind));
739
+ choices.append(button);
740
+ }
741
+ const canvas = this.shadowRoot.querySelector("[part=canvas]");
742
+ canvas.ondragover = (event) => {
743
+ if (!this.locked)
744
+ event.preventDefault();
745
+ };
746
+ canvas.ondrop = (event) => {
747
+ event.preventDefault();
748
+ const kind = this.catalog.find((kind) => kind.kind ===
749
+ event.dataTransfer?.getData("application/boe-process-kind"));
750
+ if (!kind)
751
+ return;
752
+ const rect = canvas.getBoundingClientRect();
753
+ this.requestEdit({
754
+ type: "add",
755
+ kind,
756
+ position: {
757
+ x: (event.clientX - rect.left - this.viewport.x) / this.viewport.zoom,
758
+ y: (event.clientY - rect.top - this.viewport.y) / this.viewport.zoom,
759
+ },
760
+ });
761
+ };
762
+ }
763
+ renderChecks() {
764
+ const checks = this.shadowRoot.querySelector("[part=checks]");
765
+ checks.replaceChildren();
766
+ this.checks.forEach((check) => {
767
+ const button = document.createElement("button");
768
+ button.type = "button";
769
+ button.textContent = check.message;
770
+ const box = this.projection.boxes.find((box) => box.id === check.boxId ||
771
+ (check.path &&
772
+ JSON.stringify(box.path) === JSON.stringify(check.path)));
773
+ button.addEventListener("click", () => {
774
+ if (box) {
775
+ this.select(box.id);
776
+ this.focusBox(box.id);
777
+ }
778
+ this.setStatus(check.message);
779
+ });
780
+ checks.append(button);
781
+ });
782
+ }
783
+ renderSelection() {
784
+ if (!this.isRendered)
785
+ return;
786
+ this.shadowRoot.querySelectorAll("[data-box-id]").forEach((element) => {
787
+ element.setAttribute("aria-pressed", String(element.dataset.boxId === this.selectedId));
788
+ const box = this.projection.boxes.find((box) => box.id === element.dataset.boxId);
789
+ element.dataset.invalid = String(this.checks.some((check) => check.boxId === box.id ||
790
+ (check.path &&
791
+ JSON.stringify(check.path) === JSON.stringify(box.path))));
792
+ });
793
+ const editor = this.shadowRoot.querySelector("[part=editor]");
794
+ const selected = this.selected;
795
+ if (this.inspected === selected?.node &&
796
+ this.inspectedId === selected?.id &&
797
+ editor.childNodes.length) {
798
+ editor.querySelector("h3").textContent = selected?.title ?? "";
799
+ return;
800
+ }
801
+ this.cleanupInspector?.();
802
+ this.cleanupInspector = undefined;
803
+ editor.replaceChildren();
804
+ this.inspected = selected?.node;
805
+ this.inspectedId = selected?.id;
806
+ if (selected) {
807
+ const heading = document.createElement("h3");
808
+ heading.textContent = selected.title;
809
+ editor.append(heading);
810
+ this.cleanupInspector =
811
+ this.renderer?.(selected.node, editor) || undefined;
812
+ }
813
+ }
814
+ updateToolbar() {
815
+ const button = (command) => this.shadowRoot.querySelector(`[data-command=${command}]`);
816
+ button("undo").disabled = !this.history.canUndo || this.locked;
817
+ button("redo").disabled = !this.history.canRedo || this.locked;
818
+ button("tidy").disabled = this.locked;
819
+ button("connect").disabled = !this.selected || this.locked;
820
+ button("delete").disabled = !this.selected || this.locked;
821
+ button("lock").setAttribute("aria-pressed", String(this.locked));
822
+ button("snap").setAttribute("aria-pressed", String(this.snapToGrid));
823
+ }
824
+ paintViewport() {
825
+ this.shadowRoot.querySelector("[part=world]").style.transform =
826
+ `translate(${this.viewport.x}px,${this.viewport.y}px) scale(${this.viewport.zoom})`;
827
+ this.shadowRoot.querySelector("[data-command=reset]").textContent =
828
+ `${Math.round(this.viewport.zoom * 100)}%`;
829
+ const minimap = this.shadowRoot.querySelector("[part=minimap]");
830
+ const bounds = this.bounds();
831
+ minimap.replaceChildren();
832
+ minimap.setAttribute("viewBox", `${bounds.x} ${bounds.y} ${bounds.width} ${bounds.height}`);
833
+ minimap.setAttribute("preserveAspectRatio", "none");
834
+ this.projection.boxes.forEach((box) => {
835
+ const pos = this.layoutValue.boxes[box.id];
836
+ const rect = svgElement("rect");
837
+ rect.setAttribute("x", String(pos.x));
838
+ rect.setAttribute("y", String(pos.y));
839
+ rect.setAttribute("width", String(pos.width ?? 220));
840
+ rect.setAttribute("height", String(pos.height ?? 96));
841
+ minimap.append(rect);
842
+ });
843
+ const canvas = this.shadowRoot.querySelector("[part=canvas]");
844
+ const viewport = svgElement("rect");
845
+ viewport.dataset.viewport = "true";
846
+ viewport.setAttribute("x", String(-this.viewport.x / this.viewport.zoom));
847
+ viewport.setAttribute("y", String(-this.viewport.y / this.viewport.zoom));
848
+ viewport.setAttribute("width", String(canvas.clientWidth / this.viewport.zoom));
849
+ viewport.setAttribute("height", String(canvas.clientHeight / this.viewport.zoom));
850
+ minimap.append(viewport);
851
+ }
852
+ focusBox(id) {
853
+ Array.from(this.shadowRoot.querySelectorAll("[data-box-id]"))
854
+ .find((box) => box.dataset.boxId === id)
855
+ ?.focus();
856
+ }
857
+ setStatus(message) {
858
+ this.shadowRoot.querySelector("[part=status]").textContent = message;
859
+ }
860
+ }
861
+ ProcessModeler.register();