@formicoidea/labre-framework-bpmn 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@formicoidea/labre-framework-bpmn",
3
+ "description": "Labre bpmn framework for @formicoidea/labre-core.",
4
+ "version": "0.23.0",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "author": "lajola",
8
+ "contributors": [
9
+ "toeverything"
10
+ ],
11
+ "license": "MPL-2.0",
12
+ "exports": {
13
+ ".": "./src/index.ts",
14
+ "./view": "./src/view.ts",
15
+ "./descriptor": "./src/descriptor.ts"
16
+ },
17
+ "files": [
18
+ "src"
19
+ ],
20
+ "dependencies": {
21
+ "@formicoidea/labre-core": "0.23.0",
22
+ "lit": "^3.2.0"
23
+ }
24
+ }
package/src/consts.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { BpmnNodeKind } from '@formicoidea/labre-core/model';
2
+
3
+ /**
4
+ * Visual constants for the BPMN basics. Style "C" (hybrid): spec-accurate
5
+ * shapes and line weights, with accent colour only on the event rings — the
6
+ * task and gateway stay neutral. All of these are just the creation-time
7
+ * defaults; every value is an editable shape property afterwards.
8
+ */
9
+
10
+ /** Accent stroke for the start event (thin green ring). */
11
+ export const EVENT_START = '#43a06b';
12
+ /** Accent stroke for the end event (thick red ring). */
13
+ export const EVENT_END = '#cf5648';
14
+ /** Neutral stroke for task / gateway (matches the EDGY base shapes). */
15
+ export const NEUTRAL_STROKE = '#262626';
16
+ /** Default fill for events / task / gateway. */
17
+ export const NODE_FILL = '#ffffff';
18
+
19
+ /** BPMN line weights: thin start ring, thick end ring, regular elsewhere. */
20
+ export const START_WIDTH = 2;
21
+ export const END_WIDTH = 4;
22
+ export const NODE_STROKE_WIDTH = 2;
23
+
24
+ /** Task corner radius (absolute px — a lightly rounded rectangle). */
25
+ export const TASK_RADIUS = 10;
26
+
27
+ /** Inner-text font for the task label. */
28
+ export const INNER_FONT_SIZE = 18;
29
+
30
+ /** Default node sizes (model units) per kind. */
31
+ export const NODE_SIZE: Record<BpmnNodeKind, { w: number; h: number }> = {
32
+ startEvent: { w: 56, h: 56 },
33
+ endEvent: { w: 56, h: 56 },
34
+ task: { w: 120, h: 72 },
35
+ gatewayExclusive: { w: 72, h: 72 },
36
+ };
37
+
38
+ /** Default inner text per kind (only the task carries a label). */
39
+ export const NODE_LABEL: Record<BpmnNodeKind, string> = {
40
+ startEvent: '',
41
+ endEvent: '',
42
+ task: 'Task',
43
+ gatewayExclusive: '',
44
+ };
45
+
46
+ /** Pool (background container) defaults. */
47
+ export const POOL_BAND_WIDTH = 28;
48
+ export const POOL_FRAME_COLOR = '#262626';
49
+ export const POOL_BAND_FILL = '#f4f4f5';
50
+ export const POOL_FRAME_WIDTH = 1.5;
51
+ export const POOL_NAME_FONT_SIZE = 15;
52
+ export const POOL_NAME_COLOR = '#262626';
53
+ export const POOL_FONT_FAMILY = 'Inter, sans-serif';
54
+
55
+ /** Sequence-flow connector preset. */
56
+ export const SEQUENCE_STROKE = '#262626';
57
+ export const SEQUENCE_WIDTH = 2;
@@ -0,0 +1,8 @@
1
+ import { BpmnViewExtension } from './view.js';
2
+
3
+ /** Host wiring for the bpmn framework. */
4
+ export const bpmnFramework = {
5
+ flag: 'bpmn',
6
+ telemetry: 'bpmn',
7
+ viewExtension: BpmnViewExtension,
8
+ } as const;
package/src/effects.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { EdgelessBpmnMenu } from './toolbar/bpmn-menu';
2
+ import { EdgelessBpmnSeniorButton } from './toolbar/bpmn-senior-button';
3
+
4
+ export function effects() {
5
+ customElements.define('edgeless-bpmn-menu', EdgelessBpmnMenu);
6
+ customElements.define('edgeless-bpmn-senior-button', EdgelessBpmnSeniorButton);
7
+ }
8
+
9
+ declare global {
10
+ interface HTMLElementTagNameMap {
11
+ 'edgeless-bpmn-menu': EdgelessBpmnMenu;
12
+ 'edgeless-bpmn-senior-button': EdgelessBpmnSeniorButton;
13
+ }
14
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ type ElementRenderer,
3
+ ElementRendererExtension,
4
+ } from '@formicoidea/labre-core/blocks/surface';
5
+ import type { BpmnPoolElementModel } from '@formicoidea/labre-core/model';
6
+
7
+ import {
8
+ POOL_BAND_FILL,
9
+ POOL_BAND_WIDTH,
10
+ POOL_FONT_FAMILY,
11
+ POOL_FRAME_COLOR,
12
+ POOL_FRAME_WIDTH,
13
+ POOL_NAME_COLOR,
14
+ POOL_NAME_FONT_SIZE,
15
+ } from './consts';
16
+
17
+ /** Trace a rounded-rectangle path (no dependency on ctx.roundRect). */
18
+ function roundedRectPath(
19
+ ctx: CanvasRenderingContext2D,
20
+ x: number,
21
+ y: number,
22
+ w: number,
23
+ h: number,
24
+ r: number
25
+ ): void {
26
+ const rr = Math.min(r, w / 2, h / 2);
27
+ ctx.beginPath();
28
+ ctx.moveTo(x + rr, y);
29
+ ctx.lineTo(x + w - rr, y);
30
+ ctx.arcTo(x + w, y, x + w, y + rr, rr);
31
+ ctx.lineTo(x + w, y + h - rr);
32
+ ctx.arcTo(x + w, y + h, x + w - rr, y + h, rr);
33
+ ctx.lineTo(x + rr, y + h);
34
+ ctx.arcTo(x, y + h, x, y + h - rr, rr);
35
+ ctx.lineTo(x, y + rr);
36
+ ctx.arcTo(x, y, x + rr, y, rr);
37
+ ctx.closePath();
38
+ }
39
+
40
+ /**
41
+ * Canvas renderer for a BPMN pool: a rounded-rect frame with a vertical name
42
+ * band on the left (the participant name is drawn rotated, as in the spec).
43
+ * Drawn directly in element space; the band width and font are fixed so they
44
+ * stay legible at any pool size. Mirrors the other framework backgrounds.
45
+ */
46
+ export const bpmnPool: ElementRenderer<BpmnPoolElementModel> = (
47
+ model,
48
+ ctx,
49
+ matrix
50
+ ) => {
51
+ const [, , w, h] = model.deserializedXYWH;
52
+ const cx = w / 2;
53
+ const cy = h / 2;
54
+ ctx.setTransform(
55
+ matrix.translateSelf(cx, cy).rotateSelf(model.rotate).translateSelf(-cx, -cy)
56
+ );
57
+
58
+ const band = Math.min(POOL_BAND_WIDTH, w);
59
+ const inset = POOL_FRAME_WIDTH / 2;
60
+
61
+ // Name band (left), filled.
62
+ ctx.fillStyle = POOL_BAND_FILL;
63
+ ctx.fillRect(0, 0, band, h);
64
+
65
+ // Frame + band divider.
66
+ ctx.strokeStyle = POOL_FRAME_COLOR;
67
+ ctx.lineWidth = POOL_FRAME_WIDTH;
68
+ ctx.lineJoin = 'round';
69
+ roundedRectPath(ctx, inset, inset, w - POOL_FRAME_WIDTH, h - POOL_FRAME_WIDTH, 6);
70
+ ctx.stroke();
71
+ ctx.beginPath();
72
+ ctx.moveTo(band, 0);
73
+ ctx.lineTo(band, h);
74
+ ctx.stroke();
75
+
76
+ // Participant name, rotated to read up the band (skip when empty / too narrow).
77
+ if (model.name && band > 12) {
78
+ ctx.save();
79
+ ctx.translate(band / 2, h / 2);
80
+ ctx.rotate(-Math.PI / 2);
81
+ ctx.fillStyle = POOL_NAME_COLOR;
82
+ ctx.font = `600 ${POOL_NAME_FONT_SIZE}px ${POOL_FONT_FAMILY}`;
83
+ ctx.textAlign = 'center';
84
+ ctx.textBaseline = 'middle';
85
+ ctx.fillText(model.name, 0, 0);
86
+ ctx.restore();
87
+ }
88
+ };
89
+
90
+ export const BpmnPoolRendererExtension = ElementRendererExtension(
91
+ 'bpmnPool',
92
+ bpmnPool
93
+ );
@@ -0,0 +1,116 @@
1
+ import { EdgelessCRUDIdentifier } from '@formicoidea/labre-core/blocks/surface';
2
+ import type { BpmnPoolElementModel } from '@formicoidea/labre-core/model';
3
+ import type { PointerEventState } from '@formicoidea/labre-core/std';
4
+ import {
5
+ GfxElementModelView,
6
+ GfxViewInteractionExtension,
7
+ } from '@formicoidea/labre-core/std/gfx';
8
+
9
+ /**
10
+ * View for a BPMN pool. A double-click edits the participant name in place
11
+ * (single field — the whole pool is the hit target). Mirrors the inline label
12
+ * editor used by the EDGY / Wardley backgrounds.
13
+ */
14
+ export class BpmnPoolView extends GfxElementModelView<BpmnPoolElementModel> {
15
+ static override type: string = 'bpmnPool';
16
+
17
+ private _nameEditor: HTMLInputElement | null = null;
18
+
19
+ override onCreated(): void {
20
+ super.onCreated();
21
+ this.on('dblclick', e => this._onDblClick(e));
22
+ }
23
+
24
+ override onDestroyed(): void {
25
+ this._closeEditor();
26
+ super.onDestroyed();
27
+ }
28
+
29
+ private _onDblClick(e: PointerEventState): void {
30
+ if (this.model.isLocked()) return;
31
+ this._openEditor(e);
32
+ }
33
+
34
+ private _openEditor(e: PointerEventState): void {
35
+ this._closeEditor();
36
+
37
+ const input = document.createElement('input');
38
+ input.value = String(this.model.name ?? '');
39
+ Object.assign(input.style, {
40
+ position: 'fixed',
41
+ left: `${e.raw.clientX}px`,
42
+ top: `${e.raw.clientY}px`,
43
+ transform: 'translate(-50%, -50%)',
44
+ zIndex: '10000',
45
+ minWidth: '140px',
46
+ padding: '3px 8px',
47
+ font: '14px Inter, sans-serif',
48
+ color: 'var(--affine-text-primary-color, #1f2328)',
49
+ background: 'var(--affine-background-overlay-panel-color, #ffffff)',
50
+ border: '1px solid var(--affine-primary-color, #1e96eb)',
51
+ borderRadius: '6px',
52
+ boxShadow: 'var(--affine-shadow-2, 0 2px 8px rgba(0,0,0,0.18))',
53
+ outline: 'none',
54
+ });
55
+ document.body.append(input);
56
+ this._nameEditor = input;
57
+
58
+ // Mark "editing" so the global edgeless key handlers (delete, escape, …)
59
+ // don't act on the pool while the user types.
60
+ this.gfx.selection.set({ elements: [this.model.id], editing: true });
61
+
62
+ input.focus();
63
+ input.select();
64
+
65
+ const commit = () => {
66
+ if (this._nameEditor !== input) return;
67
+ const value = input.value;
68
+ this._closeEditor();
69
+ this.gfx.std.store.captureSync();
70
+ this.gfx.std
71
+ .get(EdgelessCRUDIdentifier)
72
+ .updateElement(this.model.id, { name: value });
73
+ };
74
+
75
+ input.addEventListener('keydown', ev => {
76
+ ev.stopPropagation();
77
+ if (ev.key === 'Enter') {
78
+ ev.preventDefault();
79
+ commit();
80
+ } else if (ev.key === 'Escape') {
81
+ ev.preventDefault();
82
+ this._closeEditor();
83
+ }
84
+ });
85
+ input.addEventListener('blur', commit);
86
+ }
87
+
88
+ private _closeEditor(): void {
89
+ if (!this._nameEditor) return;
90
+ const input = this._nameEditor;
91
+ this._nameEditor = null;
92
+ input.remove();
93
+ if (this.isConnected) {
94
+ this.gfx.selection.set({ elements: [this.model.id], editing: false });
95
+ }
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Resize gating: the resize handles are hidden unless `model.resizeEnabled` is
101
+ * true (toggled from the toolbar). Moving / selecting stays available.
102
+ */
103
+ export const BpmnPoolInteraction = GfxViewInteractionExtension<BpmnPoolView>(
104
+ BpmnPoolView.type,
105
+ {
106
+ handleResize({ model }) {
107
+ return {
108
+ beforeResize({ set }) {
109
+ if (!model.resizeEnabled) {
110
+ set({ allowedHandlers: [] });
111
+ }
112
+ },
113
+ };
114
+ },
115
+ }
116
+ );
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,65 @@
1
+ import {
2
+ type ElementRenderer,
3
+ ElementRendererExtension,
4
+ } from '@formicoidea/labre-core/blocks/surface';
5
+ import { shape as shapeRenderer } from '@formicoidea/labre-core/gfx/shape';
6
+ import { type BpmnNodeElementModel, DefaultTheme } from '@formicoidea/labre-core/model';
7
+
8
+ /**
9
+ * Renderer for a BPMN flow-object node. The shape body (ellipse / rounded rect
10
+ * / diamond) is drawn by REUSING the native shape renderer — so stroke width,
11
+ * colors, inner text and theme behave exactly like a native shape. Only
12
+ * `gatewayExclusive` is decorated: an X drawn on top in the node's (editable)
13
+ * stroke color. Events and task are plain native shapes.
14
+ *
15
+ * Mirrors the EDGY node renderer.
16
+ */
17
+ export const bpmnNode: ElementRenderer<BpmnNodeElementModel> = (
18
+ model,
19
+ ctx,
20
+ matrix,
21
+ renderer,
22
+ rc,
23
+ bound
24
+ ) => {
25
+ const [, , w, h] = model.deserializedXYWH;
26
+ const cx = w / 2;
27
+ const cy = h / 2;
28
+
29
+ // Capture the element-local transform BEFORE the shape renderer mutates the
30
+ // matrix, so the glyph can be drawn in the same space afterwards.
31
+ const glyphMatrix = DOMMatrix.fromMatrix(matrix)
32
+ .translateSelf(cx, cy)
33
+ .rotateSelf(model.rotate)
34
+ .translateSelf(-cx, -cy);
35
+
36
+ // Native shape (fill / stroke / inner text / theme handled natively).
37
+ shapeRenderer(model, ctx, matrix, renderer, rc, bound);
38
+
39
+ if (model.kind !== 'gatewayExclusive') return;
40
+
41
+ const color = renderer.getColorValue(
42
+ model.strokeColor,
43
+ DefaultTheme.shapeStrokeColor,
44
+ true
45
+ );
46
+
47
+ // ── Exclusive-gateway X, centred and sized to the diamond ───────────
48
+ const r = Math.min(w, h) * 0.2;
49
+ ctx.setTransform(glyphMatrix);
50
+ ctx.translate(cx, cy);
51
+ ctx.strokeStyle = color;
52
+ ctx.lineWidth = Math.max(2, Math.min(w, h) * 0.06);
53
+ ctx.lineCap = 'round';
54
+ ctx.beginPath();
55
+ ctx.moveTo(-r, -r);
56
+ ctx.lineTo(r, r);
57
+ ctx.moveTo(-r, r);
58
+ ctx.lineTo(r, -r);
59
+ ctx.stroke();
60
+ };
61
+
62
+ export const BpmnNodeRendererExtension = ElementRendererExtension(
63
+ 'bpmnNode',
64
+ bpmnNode
65
+ );
@@ -0,0 +1,34 @@
1
+ import { mountShapeTextEditor } from '@formicoidea/labre-core/gfx/shape';
2
+ import {
3
+ type BpmnNodeElementModel,
4
+ ShapeElementModel,
5
+ } from '@formicoidea/labre-core/model';
6
+ import { GfxElementModelView } from '@formicoidea/labre-core/std/gfx';
7
+
8
+ /**
9
+ * View for a BPMN flow-object node. Registering it ensures `gfx.view.get(model)`
10
+ * returns a view (required so move / select / connector interactions work).
11
+ *
12
+ * BPMN nodes are native shapes, so we reuse the shape inner-text editor: a
13
+ * double-click mounts the editable text overlay (`mountShapeTextEditor`),
14
+ * exactly like a native shape (used to label the task).
15
+ *
16
+ * Mirrors {@link EdgyNodeView}.
17
+ */
18
+ export class BpmnNodeView extends GfxElementModelView<BpmnNodeElementModel> {
19
+ static override type: string = 'bpmnNode';
20
+
21
+ override onCreated(): void {
22
+ super.onCreated();
23
+ this.on('dblclick', () => {
24
+ const edgeless = this.std.view.getBlock(this.std.store.root!.id);
25
+ if (
26
+ edgeless &&
27
+ !this.model.isLocked() &&
28
+ this.model instanceof ShapeElementModel
29
+ ) {
30
+ mountShapeTextEditor(this.model, edgeless);
31
+ }
32
+ });
33
+ }
34
+ }
@@ -0,0 +1,156 @@
1
+ import {
2
+ makeTemplateSnapshot,
3
+ type SurfaceElementsJSON,
4
+ surfaceText,
5
+ type Template,
6
+ type TemplateCategory,
7
+ } from '@formicoidea/labre-core/gfx/template';
8
+ import {
9
+ ConnectorMode,
10
+ FontFamily,
11
+ PointStyle,
12
+ ShapeStyle,
13
+ StrokeStyle,
14
+ } from '@formicoidea/labre-core/model';
15
+
16
+ import {
17
+ END_WIDTH,
18
+ EVENT_END,
19
+ EVENT_START,
20
+ INNER_FONT_SIZE,
21
+ NEUTRAL_STROKE,
22
+ NODE_FILL,
23
+ NODE_SIZE,
24
+ NODE_STROKE_WIDTH,
25
+ SEQUENCE_STROKE,
26
+ SEQUENCE_WIDTH,
27
+ START_WIDTH,
28
+ TASK_RADIUS,
29
+ } from '../consts';
30
+
31
+ type NodeKind = 'startEvent' | 'endEvent' | 'task' | 'gatewayExclusive';
32
+
33
+ /** One BPMN flow-object node, as a surface-element JSON entry. */
34
+ function node(kind: NodeKind, x: number, y: number, text?: string) {
35
+ const { w, h } = NODE_SIZE[kind];
36
+ const base: Record<string, unknown> = {
37
+ type: 'bpmnNode',
38
+ kind,
39
+ filled: true,
40
+ fillColor: NODE_FILL,
41
+ shapeStyle: ShapeStyle.General,
42
+ roughness: 0,
43
+ xywh: `[${x},${y},${w},${h}]`,
44
+ };
45
+ if (kind === 'startEvent')
46
+ return { ...base, shapeType: 'ellipse', strokeColor: EVENT_START, strokeWidth: START_WIDTH };
47
+ if (kind === 'endEvent')
48
+ return { ...base, shapeType: 'ellipse', strokeColor: EVENT_END, strokeWidth: END_WIDTH };
49
+ if (kind === 'gatewayExclusive')
50
+ return { ...base, shapeType: 'diamond', strokeColor: NEUTRAL_STROKE, strokeWidth: NODE_STROKE_WIDTH };
51
+ return {
52
+ ...base,
53
+ shapeType: 'rect',
54
+ radius: TASK_RADIUS,
55
+ strokeColor: NEUTRAL_STROKE,
56
+ strokeWidth: NODE_STROKE_WIDTH,
57
+ text: surfaceText(text ?? 'Task'),
58
+ color: NEUTRAL_STROKE,
59
+ fontFamily: FontFamily.Inter,
60
+ fontSize: INNER_FONT_SIZE,
61
+ textAlign: 'center',
62
+ };
63
+ }
64
+
65
+ function pool(x: number, y: number, w: number, h: number, name = 'Pool') {
66
+ return { type: 'bpmnPool', name, xywh: `[${x},${y},${w},${h}]` };
67
+ }
68
+
69
+ /** A sequence-flow connector; ids are remapped on insert. */
70
+ function seq(source: string, target: string) {
71
+ return {
72
+ type: 'connector',
73
+ mode: ConnectorMode.Orthogonal,
74
+ stroke: SEQUENCE_STROKE,
75
+ strokeWidth: SEQUENCE_WIDTH,
76
+ strokeStyle: StrokeStyle.Solid,
77
+ frontEndpointStyle: PointStyle.None,
78
+ rearEndpointStyle: PointStyle.Triangle,
79
+ source: { id: source, position: [0.5, 0.5] },
80
+ target: { id: target, position: [0.5, 0.5] },
81
+ };
82
+ }
83
+
84
+ /** A standalone (free) sequence-flow arrow for the prefab card. */
85
+ function freeSeq(): Record<string, unknown> {
86
+ return {
87
+ type: 'connector',
88
+ mode: ConnectorMode.Orthogonal,
89
+ stroke: SEQUENCE_STROKE,
90
+ strokeWidth: SEQUENCE_WIDTH,
91
+ strokeStyle: StrokeStyle.Solid,
92
+ frontEndpointStyle: PointStyle.None,
93
+ rearEndpointStyle: PointStyle.Triangle,
94
+ source: { position: [0, 0] },
95
+ target: { position: [140, 0] },
96
+ };
97
+ }
98
+
99
+ const single = (el: Record<string, unknown>): SurfaceElementsJSON => ({ a: el });
100
+
101
+ const PREVIEW_ATTRS = 'width="100%" height="100%" viewBox="0 0 135 80" xmlns="http://www.w3.org/2000/svg"';
102
+
103
+ const previews = {
104
+ process: `<svg ${PREVIEW_ATTRS} fill="none"><circle cx="16" cy="40" r="8" stroke="#43a06b" stroke-width="2"/><rect x="34" y="31" width="26" height="18" rx="3" stroke="#262626" stroke-width="1.6"/><path d="M78 31 L88 40 L78 49 L68 40 Z" stroke="#262626" stroke-width="1.4"/><path d="M73 37 L83 43 M83 37 L73 43" stroke="#262626" stroke-width="1.2"/><circle cx="118" cy="40" r="8" stroke="#cf5648" stroke-width="3"/><path d="M24 40 H34 M60 40 H68 M88 40 H110" stroke="#262626" stroke-width="1.2"/></svg>`,
105
+ startEvent: `<svg ${PREVIEW_ATTRS} fill="none"><circle cx="67" cy="40" r="20" stroke="#43a06b" stroke-width="3"/></svg>`,
106
+ endEvent: `<svg ${PREVIEW_ATTRS} fill="none"><circle cx="67" cy="40" r="20" stroke="#cf5648" stroke-width="5"/></svg>`,
107
+ task: `<svg ${PREVIEW_ATTRS} fill="none"><rect x="34" y="24" width="66" height="32" rx="6" stroke="#262626" stroke-width="2.4"/></svg>`,
108
+ gateway: `<svg ${PREVIEW_ATTRS} fill="none"><path d="M67 16 L92 40 L67 64 L42 40 Z" stroke="#262626" stroke-width="2.4" stroke-linejoin="round"/><path d="M58 31 L76 49 M76 31 L58 49" stroke="#262626" stroke-width="2.2" stroke-linecap="round"/></svg>`,
109
+ sequence: `<svg ${PREVIEW_ATTRS} fill="none"><path d="M24 40 H96" stroke="#262626" stroke-width="2.4" stroke-linecap="round"/><path d="M94 33 L108 40 L94 47 Z" fill="#262626"/></svg>`,
110
+ pool: `<svg ${PREVIEW_ATTRS} fill="none"><rect x="14" y="20" width="107" height="40" rx="3" stroke="#262626" stroke-width="2"/><path d="M30 20 V60" stroke="#262626" stroke-width="1.8"/><rect x="14" y="20" width="16" height="40" fill="#f4f4f5"/><path d="M30 20 V60" stroke="#262626" stroke-width="1.8"/></svg>`,
111
+ };
112
+
113
+ /** The lean BPMN basics: a simple worked process + every prefab the menu makes. */
114
+ function bpmnTemplates(): Template[] {
115
+ const process: SurfaceElementsJSON = {
116
+ pool: pool(0, 0, 640, 200, 'Process'),
117
+ start: node('startEvent', 40, 72),
118
+ task1: node('task', 116, 64, 'Submit request'),
119
+ gw: node('gatewayExclusive', 272, 64),
120
+ task2: node('task', 376, 20, 'Fulfil'),
121
+ task3: node('task', 376, 124, 'Reject'),
122
+ end: node('endEvent', 556, 72),
123
+ c1: seq('start', 'task1'),
124
+ c2: seq('task1', 'gw'),
125
+ c3: seq('gw', 'task2'),
126
+ c4: seq('gw', 'task3'),
127
+ c5: seq('task2', 'end'),
128
+ c6: seq('task3', 'end'),
129
+ };
130
+
131
+ const t = (
132
+ name: string,
133
+ preview: string,
134
+ elements: SurfaceElementsJSON
135
+ ): Template => ({
136
+ name,
137
+ type: 'template',
138
+ preview,
139
+ content: makeTemplateSnapshot(elements, name),
140
+ });
141
+
142
+ return [
143
+ t('Simple process', previews.process, process),
144
+ t('Start event', previews.startEvent, single(node('startEvent', 0, 0))),
145
+ t('End event', previews.endEvent, single(node('endEvent', 0, 0))),
146
+ t('Task', previews.task, single(node('task', 0, 0))),
147
+ t('Exclusive gateway', previews.gateway, single(node('gatewayExclusive', 0, 0))),
148
+ t('Sequence flow', previews.sequence, single(freeSeq())),
149
+ t('Pool', previews.pool, single(pool(0, 0, 560, 200))),
150
+ ];
151
+ }
152
+
153
+ export const bpmnTemplateCategory: TemplateCategory = {
154
+ name: 'BPMN',
155
+ templates: bpmnTemplates(),
156
+ };
@@ -0,0 +1,224 @@
1
+ import { DefaultTool } from '@formicoidea/labre-core/blocks/surface';
2
+ import { ConnectorTool } from '@formicoidea/labre-core/gfx/connector';
3
+ import { EmptyTool } from '@formicoidea/labre-core/gfx/pointer';
4
+ import {
5
+ ConnectorMode,
6
+ FontFamily,
7
+ PointStyle,
8
+ ShapeStyle,
9
+ StrokeStyle,
10
+ } from '@formicoidea/labre-core/model';
11
+ import {
12
+ EditPropsStore,
13
+ TelemetryProvider,
14
+ } from '@formicoidea/labre-core/shared/services';
15
+ import { EdgelessToolbarToolMixin } from '@formicoidea/labre-core/widgets/edgeless-toolbar';
16
+ import { Bound } from '@formicoidea/labre-core/global/gfx';
17
+ import { css, html, LitElement } from 'lit';
18
+
19
+ import {
20
+ EVENT_END,
21
+ EVENT_START,
22
+ END_WIDTH,
23
+ INNER_FONT_SIZE,
24
+ NEUTRAL_STROKE,
25
+ NODE_FILL,
26
+ NODE_LABEL,
27
+ NODE_SIZE,
28
+ NODE_STROKE_WIDTH,
29
+ SEQUENCE_STROKE,
30
+ SEQUENCE_WIDTH,
31
+ START_WIDTH,
32
+ TASK_RADIUS,
33
+ } from '../consts';
34
+ import {
35
+ bpmnEndIcon,
36
+ bpmnGatewayIcon,
37
+ bpmnPoolIcon,
38
+ bpmnSequenceIcon,
39
+ bpmnStartIcon,
40
+ bpmnTaskIcon,
41
+ } from './icons';
42
+
43
+ type NodeKind = 'startEvent' | 'endEvent' | 'task' | 'gatewayExclusive';
44
+
45
+ /** Per-kind native shape + accent presets (style C). */
46
+ const NODE_PRESETS: Record<
47
+ NodeKind,
48
+ { shapeType: 'ellipse' | 'rect' | 'diamond'; stroke: string; width: number }
49
+ > = {
50
+ startEvent: { shapeType: 'ellipse', stroke: EVENT_START, width: START_WIDTH },
51
+ endEvent: { shapeType: 'ellipse', stroke: EVENT_END, width: END_WIDTH },
52
+ task: { shapeType: 'rect', stroke: NEUTRAL_STROKE, width: NODE_STROKE_WIDTH },
53
+ gatewayExclusive: {
54
+ shapeType: 'diamond',
55
+ stroke: NEUTRAL_STROKE,
56
+ width: NODE_STROKE_WIDTH,
57
+ },
58
+ };
59
+
60
+ /**
61
+ * The popover above the toolbar for the BPMN toolbox. Items create the flow
62
+ * objects (native shapes, so they stay editable), the pool background, and arm
63
+ * the native connector tool for sequence flows. Mirrors the EDGY menu.
64
+ */
65
+ export class EdgelessBpmnMenu extends EdgelessToolbarToolMixin(LitElement) {
66
+ static override styles = css`
67
+ :host {
68
+ position: absolute;
69
+ display: flex;
70
+ z-index: -1;
71
+ }
72
+ .menu-content {
73
+ display: flex;
74
+ align-items: center;
75
+ justify-content: center;
76
+ }
77
+ .button-group-container {
78
+ display: flex;
79
+ align-items: center;
80
+ gap: 14px;
81
+ fill: var(--affine-icon-color);
82
+ }
83
+ .button-group-container svg {
84
+ width: 24px;
85
+ height: 24px;
86
+ }
87
+ `;
88
+
89
+ override type = EmptyTool;
90
+
91
+ /** Create a flow-object node (native shape) centred on the viewport. */
92
+ private _createNode(kind: NodeKind) {
93
+ const surface = this.gfx.surface;
94
+ if (!surface) return;
95
+
96
+ const { w, h } = NODE_SIZE[kind];
97
+ const { centerX: cx, centerY: cy } = this.gfx.viewport;
98
+ const preset = NODE_PRESETS[kind];
99
+
100
+ const id = surface.addElement({
101
+ type: 'bpmnNode',
102
+ kind,
103
+ shapeType: preset.shapeType,
104
+ filled: true,
105
+ fillColor: NODE_FILL,
106
+ strokeColor: preset.stroke,
107
+ strokeWidth: preset.width,
108
+ shapeStyle: ShapeStyle.General,
109
+ roughness: 0,
110
+ radius: kind === 'task' ? TASK_RADIUS : 0,
111
+ text: NODE_LABEL[kind] || undefined,
112
+ color: NEUTRAL_STROKE,
113
+ fontFamily: FontFamily.Inter,
114
+ fontSize: INNER_FONT_SIZE,
115
+ textAlign: 'center',
116
+ xywh: new Bound(cx - w / 2, cy - h / 2, w, h).serialize(),
117
+ });
118
+ this._track('FrameworkElementAdded', `node:${kind}`);
119
+ this._finish(id);
120
+ }
121
+
122
+ /** Create a pool (background container) centred on the viewport. */
123
+ private _createPool() {
124
+ const surface = this.gfx.surface;
125
+ if (!surface) return;
126
+
127
+ const w = 560;
128
+ const h = 200;
129
+ const { centerX: cx, centerY: cy } = this.gfx.viewport;
130
+ const id = surface.addElement({
131
+ type: 'bpmnPool',
132
+ xywh: new Bound(cx - w / 2, cy - h / 2, w, h).serialize(),
133
+ });
134
+ this._track('FrameworkElementAdded', 'pool');
135
+ this._finish(id);
136
+ }
137
+
138
+ /**
139
+ * Arm the native connector tool, pre-styled for a BPMN sequence flow:
140
+ * orthogonal, solid, with a filled triangle head. The user then draws from
141
+ * one node to another (endpoints attach to centers).
142
+ */
143
+ private _activateSequenceFlow() {
144
+ this.edgeless.std.get(EditPropsStore).recordLastProps('connector', {
145
+ mode: ConnectorMode.Orthogonal,
146
+ stroke: SEQUENCE_STROKE,
147
+ strokeStyle: StrokeStyle.Solid,
148
+ strokeWidth: SEQUENCE_WIDTH,
149
+ frontEndpointStyle: PointStyle.None,
150
+ rearEndpointStyle: PointStyle.Triangle,
151
+ });
152
+ this._track('FrameworkToolPicked', 'connector:sequence');
153
+ this.gfx.tool.setTool(ConnectorTool, { mode: ConnectorMode.Orthogonal });
154
+ // Keep the palette open (native sub-menu behaviour).
155
+ }
156
+
157
+ private _finish(id: string) {
158
+ const { gfx } = this;
159
+ gfx.doc.captureSync();
160
+ gfx.tool.setTool(DefaultTool);
161
+ gfx.selection.set({ elements: [id], editing: false });
162
+ // Keep the palette open (native sub-menu behaviour).
163
+ }
164
+
165
+ private _track(
166
+ event: 'FrameworkElementAdded' | 'FrameworkToolPicked',
167
+ element: string
168
+ ) {
169
+ this.edgeless.std.getOptional(TelemetryProvider)?.track(event, {
170
+ framework: 'bpmn',
171
+ element,
172
+ page: 'whiteboard editor',
173
+ segment: 'bpmn toolbox',
174
+ module: 'bpmn menu',
175
+ });
176
+ }
177
+
178
+ override render() {
179
+ return html`
180
+ <edgeless-slide-menu>
181
+ <div class="menu-content">
182
+ <div class="button-group-container">
183
+ <edgeless-tool-icon-button
184
+ .tooltip=${'Start event'}
185
+ @click=${() => this._createNode('startEvent')}
186
+ >
187
+ ${bpmnStartIcon}
188
+ </edgeless-tool-icon-button>
189
+ <edgeless-tool-icon-button
190
+ .tooltip=${'End event'}
191
+ @click=${() => this._createNode('endEvent')}
192
+ >
193
+ ${bpmnEndIcon}
194
+ </edgeless-tool-icon-button>
195
+ <edgeless-tool-icon-button
196
+ .tooltip=${'Task'}
197
+ @click=${() => this._createNode('task')}
198
+ >
199
+ ${bpmnTaskIcon}
200
+ </edgeless-tool-icon-button>
201
+ <edgeless-tool-icon-button
202
+ .tooltip=${'Exclusive gateway'}
203
+ @click=${() => this._createNode('gatewayExclusive')}
204
+ >
205
+ ${bpmnGatewayIcon}
206
+ </edgeless-tool-icon-button>
207
+ <edgeless-tool-icon-button
208
+ .tooltip=${'Sequence flow'}
209
+ @click=${this._activateSequenceFlow}
210
+ >
211
+ ${bpmnSequenceIcon}
212
+ </edgeless-tool-icon-button>
213
+ <edgeless-tool-icon-button
214
+ .tooltip=${'Pool'}
215
+ @click=${this._createPool}
216
+ >
217
+ ${bpmnPoolIcon}
218
+ </edgeless-tool-icon-button>
219
+ </div>
220
+ </div>
221
+ </edgeless-slide-menu>
222
+ `;
223
+ }
224
+ }
@@ -0,0 +1,102 @@
1
+ import { DefaultTool } from '@formicoidea/labre-core/blocks/surface';
2
+ import { EmptyTool } from '@formicoidea/labre-core/gfx/pointer';
3
+ import { EdgelessToolbarToolMixin } from '@formicoidea/labre-core/widgets/edgeless-toolbar';
4
+ import { SignalWatcher } from '@formicoidea/labre-core/global/lit';
5
+ import { css, html, LitElement } from 'lit';
6
+
7
+ import { bpmnToolbarIcon } from './icons';
8
+
9
+ /**
10
+ * Main toolbar button (colored BPMN glyph) that opens the BPMN toolbox sub-menu
11
+ * above the toolbar. Mirrors the EDGY / Wardley senior buttons.
12
+ */
13
+ export class EdgelessBpmnSeniorButton extends EdgelessToolbarToolMixin(
14
+ SignalWatcher(LitElement)
15
+ ) {
16
+ static override styles = css`
17
+ :host,
18
+ .bpmn-button {
19
+ display: block;
20
+ width: 100%;
21
+ height: 100%;
22
+ }
23
+ :host {
24
+ position: relative;
25
+ }
26
+ .bpmn-root {
27
+ width: 100%;
28
+ height: 64px;
29
+ position: relative;
30
+ overflow: hidden;
31
+ cursor: pointer;
32
+ display: flex;
33
+ align-items: flex-end;
34
+ justify-content: center;
35
+ }
36
+ .bpmn-card {
37
+ --y: -4px;
38
+ --s: 1;
39
+ position: absolute;
40
+ bottom: 0;
41
+ width: 54px;
42
+ height: 54px;
43
+ transform: translateY(var(--y)) scale(var(--s));
44
+ translate: var(--active-x, 0) var(--active-y, 0);
45
+ rotate: var(--active-r, -2deg);
46
+ scale: var(--active-s, 1);
47
+ transition: transform 0.3s ease, translate 0.3s ease, rotate 0.3s ease,
48
+ scale 0.3s ease;
49
+ }
50
+ .bpmn-card svg {
51
+ display: block;
52
+ width: 100%;
53
+ height: 100%;
54
+ }
55
+ .bpmn-root:hover .bpmn-card {
56
+ --y: -10px;
57
+ --s: 1.07;
58
+ }
59
+ `;
60
+
61
+ override enableActiveBackground = true;
62
+
63
+ override type = EmptyTool;
64
+
65
+ private _toggleMenu() {
66
+ if (this.popper) {
67
+ this.popper.dispose();
68
+ this.popper = null;
69
+ return;
70
+ }
71
+ this.setEdgelessTool(DefaultTool);
72
+ const menu = this.createPopper('edgeless-bpmn-menu', this);
73
+ menu.element.edgeless = this.edgeless;
74
+
75
+ const el = menu.element as HTMLElement;
76
+ const wrap = el.parentElement;
77
+ if (wrap) {
78
+ wrap.style.overflow = 'visible';
79
+ wrap.style.justifyContent = 'flex-end';
80
+ }
81
+ Object.assign(el.style, {
82
+ position: 'static',
83
+ width: 'max-content',
84
+ maxWidth: 'calc(100vw - 16px)',
85
+ marginLeft: '0',
86
+ });
87
+ }
88
+
89
+ override render() {
90
+ return html`<edgeless-toolbar-button
91
+ class="bpmn-button"
92
+ .tooltip=${this.popper ? '' : 'BPMN'}
93
+ .tooltipOffset=${4}
94
+ .active=${!!this.popper}
95
+ @click=${this._toggleMenu}
96
+ >
97
+ <div class="bpmn-root">
98
+ <div class="bpmn-card">${bpmnToolbarIcon}</div>
99
+ </div>
100
+ </edgeless-toolbar-button>`;
101
+ }
102
+ }
@@ -0,0 +1,74 @@
1
+ import { EdgelessCRUDIdentifier } from '@formicoidea/labre-core/blocks/surface';
2
+ import { BpmnPoolElementModel } from '@formicoidea/labre-core/model';
3
+ import {
4
+ type ToolbarContext,
5
+ type ToolbarModuleConfig,
6
+ ToolbarModuleExtension,
7
+ } from '@formicoidea/labre-core/shared/services';
8
+ import { BlockFlavourIdentifier } from '@formicoidea/labre-core/std';
9
+ import { html, type TemplateResult } from 'lit';
10
+
11
+ const ResizeIcon = html`<svg
12
+ width="24"
13
+ height="24"
14
+ viewBox="0 0 24 24"
15
+ fill="none"
16
+ stroke="currentColor"
17
+ stroke-width="1.6"
18
+ stroke-linecap="round"
19
+ stroke-linejoin="round"
20
+ >
21
+ <path d="M9 5H5v4M15 19h4v-4" />
22
+ <path d="M5 5l6 6M19 19l-6-6" />
23
+ </svg>`;
24
+
25
+ type BpmnPoolToggleProp = 'resizeEnabled';
26
+
27
+ /**
28
+ * Build a toolbar toggle that flips a boolean flag on every selected pool:
29
+ * `active` reflects the current state, `run` flips it (with an undo checkpoint).
30
+ */
31
+ function booleanToggle(
32
+ id: string,
33
+ tooltip: string,
34
+ icon: TemplateResult,
35
+ prop: BpmnPoolToggleProp
36
+ ) {
37
+ return {
38
+ id,
39
+ tooltip,
40
+ icon,
41
+ active(ctx: ToolbarContext) {
42
+ const models = ctx.getSurfaceModelsByType(BpmnPoolElementModel);
43
+ return models.length > 0 && models.every(model => model[prop]);
44
+ },
45
+ run(ctx: ToolbarContext) {
46
+ const models = ctx.getSurfaceModelsByType(BpmnPoolElementModel);
47
+ if (!models.length) return;
48
+
49
+ const enable = !models.every(model => model[prop]);
50
+ ctx.std.store.captureSync();
51
+ const crud = ctx.std.get(EdgelessCRUDIdentifier);
52
+ for (const model of models) {
53
+ crud.updateElement(model.id, { [prop]: enable });
54
+ }
55
+ },
56
+ };
57
+ }
58
+
59
+ export const bpmnPoolToolbarConfig = {
60
+ actions: [
61
+ booleanToggle(
62
+ 'a.toggle-resize',
63
+ 'Enable / lock resizing',
64
+ ResizeIcon,
65
+ 'resizeEnabled'
66
+ ),
67
+ ],
68
+ when: ctx => ctx.getSurfaceModelsByType(BpmnPoolElementModel).length > 0,
69
+ } as const satisfies ToolbarModuleConfig;
70
+
71
+ export const bpmnPoolToolbarExtension = ToolbarModuleExtension({
72
+ id: BlockFlavourIdentifier('affine:surface:bpmnPool'),
73
+ config: bpmnPoolToolbarConfig,
74
+ });
@@ -0,0 +1,43 @@
1
+ import { svg } from 'lit';
2
+
3
+ /** Colored BPMN glyph for the main toolbar button: a pool (green name band) with
4
+ * a single activity inside. */
5
+ export const bpmnToolbarIcon = svg`<svg width="100%" height="100%" viewBox="0 0 56 56" fill="none" xmlns="http://www.w3.org/2000/svg">
6
+ <rect x="4" y="13" width="48" height="30" rx="3" fill="#ffffff" stroke="#262626" stroke-width="2.2"/>
7
+ <path d="M6 14 h5 v28 h-5 z" fill="#43a06b"/>
8
+ <line x1="11" y1="13" x2="11" y2="43" stroke="#262626" stroke-width="1.8"/>
9
+ <rect x="20" y="20" width="24" height="16" rx="3.5" fill="#ffffff" stroke="#262626" stroke-width="2.2"/>
10
+ </svg>`;
11
+
12
+ /** Start event — thin green ring. */
13
+ export const bpmnStartIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
14
+ <circle cx="12" cy="12" r="8" stroke="#43a06b" stroke-width="2"/>
15
+ </svg>`;
16
+
17
+ /** End event — thick red ring. */
18
+ export const bpmnEndIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
19
+ <circle cx="12" cy="12" r="8" stroke="#cf5648" stroke-width="3.5"/>
20
+ </svg>`;
21
+
22
+ /** Task — rounded rectangle. */
23
+ export const bpmnTaskIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
24
+ <rect x="3.5" y="6.5" width="17" height="11" rx="2.5" stroke="currentColor" stroke-width="1.6"/>
25
+ </svg>`;
26
+
27
+ /** Exclusive gateway — diamond with an X. */
28
+ export const bpmnGatewayIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
29
+ <path d="M12 3 L21 12 L12 21 L3 12 Z" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/>
30
+ <path d="M9 9 L15 15 M15 9 L9 15" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/>
31
+ </svg>`;
32
+
33
+ /** Sequence flow — solid arrow with a filled head. */
34
+ export const bpmnSequenceIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
35
+ <path d="M3 12 H17" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"/>
36
+ <path d="M15 8 L21 12 L15 16 Z" fill="currentColor"/>
37
+ </svg>`;
38
+
39
+ /** Pool — rectangle with a left name band. */
40
+ export const bpmnPoolIcon = svg`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
41
+ <rect x="3.5" y="5.5" width="17" height="13" rx="1.5" stroke="currentColor" stroke-width="1.6"/>
42
+ <path d="M8 5.5 V18.5" stroke="currentColor" stroke-width="1.6"/>
43
+ </svg>`;
@@ -0,0 +1,11 @@
1
+ import { SeniorToolExtension } from '@formicoidea/labre-core/widgets/edgeless-toolbar';
2
+ import { html } from 'lit';
3
+
4
+ export const bpmnSeniorTool = SeniorToolExtension('bpmn', ({ block }) => {
5
+ return {
6
+ name: 'BPMN',
7
+ content: html`<edgeless-bpmn-senior-button
8
+ .edgeless=${block}
9
+ ></edgeless-bpmn-senior-button>`,
10
+ };
11
+ });
package/src/view.ts ADDED
@@ -0,0 +1,37 @@
1
+ import {
2
+ type ViewExtensionContext,
3
+ ViewExtensionProvider,
4
+ } from '@formicoidea/labre-core/ext-loader';
5
+ import { extendTemplateCategory } from '@formicoidea/labre-core/gfx/template';
6
+
7
+ import { effects } from './effects';
8
+ import { bpmnTemplateCategory } from './templates';
9
+ import { BpmnPoolRendererExtension } from './element-renderer';
10
+ import { BpmnPoolInteraction, BpmnPoolView } from './element-view';
11
+ import { BpmnNodeRendererExtension } from './node/node-renderer';
12
+ import { BpmnNodeView } from './node/node-view';
13
+ import { bpmnPoolToolbarExtension } from './toolbar/config';
14
+ import { bpmnSeniorTool } from './toolbar/senior-tool';
15
+
16
+ export class BpmnViewExtension extends ViewExtensionProvider {
17
+ override name = 'affine-bpmn-gfx';
18
+
19
+ override effect(): void {
20
+ super.effect();
21
+ effects();
22
+ extendTemplateCategory(bpmnTemplateCategory);
23
+ }
24
+
25
+ override setup(context: ViewExtensionContext) {
26
+ super.setup(context);
27
+ context.register(BpmnPoolView);
28
+ context.register(BpmnPoolRendererExtension);
29
+ context.register(BpmnNodeView);
30
+ context.register(BpmnNodeRendererExtension);
31
+ if (this.isEdgeless(context.scope)) {
32
+ context.register(BpmnPoolInteraction);
33
+ context.register(bpmnSeniorTool);
34
+ context.register(bpmnPoolToolbarExtension);
35
+ }
36
+ }
37
+ }