@bpmn-nova/studio 0.4.0 → 0.4.1

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 (32) hide show
  1. package/README.md +1 -1
  2. package/dist/flow-tree.d.ts +4 -0
  3. package/dist/flow-tree.js +14 -0
  4. package/dist/modules/flow-tree/controller.js +364 -0
  5. package/dist/modules/flow-tree/index.d.ts +248 -0
  6. package/dist/modules/flow-tree/index.js +5 -0
  7. package/dist/modules/flow-tree/operations.js +484 -0
  8. package/dist/modules/flow-tree/sample.js +445 -0
  9. package/dist/modules/flow-tree/schema.js +588 -0
  10. package/dist/modules/flow-tree/validate.js +125 -0
  11. package/dist/modules/flow-tree-bpmn/compiler.js +547 -0
  12. package/dist/modules/flow-tree-bpmn/decompiler.js +524 -0
  13. package/dist/modules/flow-tree-bpmn/expression.js +135 -0
  14. package/dist/modules/flow-tree-bpmn/index.d.ts +53 -0
  15. package/dist/modules/flow-tree-bpmn/index.js +14 -0
  16. package/dist/modules/flow-tree-renderer/canvas.js +1003 -0
  17. package/dist/modules/flow-tree-renderer/export-svg.js +325 -0
  18. package/dist/modules/flow-tree-renderer/index.d.ts +143 -0
  19. package/dist/modules/flow-tree-renderer/index.js +3 -0
  20. package/dist/modules/flow-tree-renderer/shell.js +970 -0
  21. package/dist/modules/i18n/locales/en-US.js +399 -0
  22. package/dist/modules/i18n/locales/zh-CN.js +399 -0
  23. package/dist/modules/icons/index.js +23 -0
  24. package/dist/modules/properties-flow-tree/index.d.ts +56 -0
  25. package/dist/modules/properties-flow-tree/index.js +2 -0
  26. package/dist/modules/properties-flow-tree/panel.js +1136 -0
  27. package/dist/modules/properties-flow-tree/widgets.js +625 -0
  28. package/dist/modules/viewer/index.js +10 -2
  29. package/dist/styles.css +2368 -0
  30. package/llms-full.txt +353 -9
  31. package/llms.txt +3 -2
  32. package/package.json +6 -1
@@ -0,0 +1,970 @@
1
+ import { createI18n, resolveI18n } from '../i18n/index.js';
2
+ import { createDefaultIconRegistry, hydrateIcons } from '../icons/index.js';
3
+ import { ThemeController } from '../theme/index.js';
4
+ import { BpmnViewer } from '../viewer/index.js';
5
+ import { openSvgExportPreview } from '../export-svg/index.js';
6
+ import { FlowTreePropertiesPanel } from '../properties-flow-tree/index.js';
7
+ import {
8
+ compileFlowTreeToBpmn,
9
+ createFlowTreeBpmnCompiler,
10
+ decompileBpmnToFlowTree,
11
+ exportFlowTreeBpmnXml,
12
+ } from '../flow-tree-bpmn/index.js';
13
+ import { FlowTreeCanvas } from './canvas.js';
14
+
15
+ const SHELL_MODES = ['design', 'viewer', 'instance'];
16
+
17
+ function el(tag, className = '', text = undefined) {
18
+ const node = document.createElement(tag);
19
+ if (className) node.className = className;
20
+ if (text !== undefined) node.textContent = text;
21
+ return node;
22
+ }
23
+
24
+ function icon(id, className = 'nova-icon nova-icon-sm') {
25
+ const span = el('span', className);
26
+ span.dataset.icon = id;
27
+ return span;
28
+ }
29
+
30
+ function mountSlot(slot, container, context) {
31
+ if (!slot) return null;
32
+ if (slot instanceof HTMLElement) { container.appendChild(slot); return null; }
33
+ if (typeof slot === 'function') return slot(Object.defineProperties({ container }, Object.getOwnPropertyDescriptors(context)));
34
+ return null;
35
+ }
36
+
37
+ function downloadText(content, filename, mimeType = 'application/xml;charset=utf-8') {
38
+ const blob = new Blob([content], { type: mimeType });
39
+ const url = URL.createObjectURL(blob);
40
+ const link = document.createElement('a');
41
+ link.href = url;
42
+ link.download = filename;
43
+ document.body.appendChild(link);
44
+ link.click();
45
+ link.remove();
46
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
47
+ }
48
+
49
+ export class FlowTreeShell {
50
+ constructor(options = {}) {
51
+ const {
52
+ container,
53
+ controller,
54
+ mode = 'design',
55
+ allowedModes = null,
56
+ view = 'tree',
57
+ runtime = null,
58
+ runtimeAppearance = null,
59
+ runtimeAssetResolver = null,
60
+ formatRuntimeTime = null,
61
+ runtimeTimeFormat = null,
62
+ dataProviders = {},
63
+ nodePanels = {},
64
+ nodeSummaryResolver = null,
65
+ nodeCardRenderer = null,
66
+ slots = {},
67
+ regions = {},
68
+ rightWidth = 380,
69
+ rightCollapsed = false,
70
+ iconRegistry = createDefaultIconRegistry(),
71
+ theme = null,
72
+ onThemeChange = null,
73
+ onModeChange = null,
74
+ onValidation = null,
75
+ onImportError = null,
76
+ i18n = null,
77
+ locale,
78
+ messages,
79
+ } = options;
80
+
81
+ if (!container || !controller) {
82
+ throw new Error('FlowTreeShell requires container and controller.');
83
+ }
84
+
85
+ this.container = container;
86
+ this.controller = controller;
87
+ if (!controller._compiler) controller.setCompiler(createFlowTreeBpmnCompiler());
88
+
89
+ this.allowedModes = Array.isArray(allowedModes) && allowedModes.length
90
+ ? allowedModes.filter((item) => SHELL_MODES.includes(item))
91
+ : [...SHELL_MODES];
92
+ if (!this.allowedModes.length) this.allowedModes = [...SHELL_MODES];
93
+ this.mode = this.allowedModes.includes(mode) ? mode : this.allowedModes[0];
94
+ this.view = ['tree', 'bpmn', 'timeline'].includes(view) ? view : 'tree';
95
+ this.runtime = runtime;
96
+ this.runtimeAppearance = runtimeAppearance;
97
+ this.runtimeAssetResolver = runtimeAssetResolver;
98
+ this.formatRuntimeTime = formatRuntimeTime;
99
+ this.runtimeTimeFormat = runtimeTimeFormat;
100
+ this.dataProviders = dataProviders || {};
101
+ this.nodePanels = nodePanels || {};
102
+ this.nodeSummaryResolver = nodeSummaryResolver;
103
+ this.nodeCardRenderer = nodeCardRenderer;
104
+ this.slots = slots || {};
105
+ this.regions = {
106
+ header: regions.header || 'default',
107
+ right: regions.right || undefined,
108
+ footer: regions.footer || 'default',
109
+ };
110
+ this.rightWidth = Number.isFinite(Number(rightWidth)) ? Number(rightWidth) : 380;
111
+ this._rightCollapsed = Boolean(rightCollapsed);
112
+ this.iconRegistry = iconRegistry;
113
+ this.onModeChange = onModeChange;
114
+ this.onValidation = onValidation;
115
+ this.onImportError = onImportError;
116
+
117
+ if (i18n) {
118
+ this.i18n = resolveI18n(i18n);
119
+ this._ownsI18n = false;
120
+ } else if (controller.i18n) {
121
+ this.i18n = controller.i18n;
122
+ this._ownsI18n = false;
123
+ if (locale !== undefined || messages !== undefined) controller.setLocale(locale, messages);
124
+ } else {
125
+ this.i18n = createI18n({ locale, messages });
126
+ this._ownsI18n = true;
127
+ }
128
+
129
+ this.container.classList.add('nova-ft-shell');
130
+ this.container.style.setProperty('--ft-right-width', `${this.rightWidth}px`);
131
+ this.themeController = new ThemeController({ root: this.container, theme, onChange: onThemeChange });
132
+
133
+ this._cleanups = [];
134
+ this._modeListeners = new Set();
135
+ this._validationListeners = new Set();
136
+ this._panelSelectionListeners = new Set();
137
+ this._sidebarListeners = new Set();
138
+ this._canvas = null;
139
+ this._bpmnViewer = null;
140
+ this._propertiesPanel = null;
141
+ this._xmlModal = null;
142
+ this._svgExportPreview = null;
143
+ this._validationPopover = null;
144
+ this._importMenu = null;
145
+ this._exportMenu = null;
146
+ this._destroyed = false;
147
+
148
+ this.actions = Object.freeze({
149
+ undo: () => this.controller.commands.undo(),
150
+ redo: () => this.controller.commands.redo(),
151
+ validate: () => this.validate('toolbar'),
152
+ fitView: () => this.fitView(),
153
+ exportXml: (engine) => this.exportXml(engine),
154
+ importXml: (xml, engine) => this.importXml(xml, engine),
155
+ exportJson: () => this.controller.toJSON(),
156
+ importJson: (json) => this.controller.fromJSON(json),
157
+ exportSvg: (opts) => this.exportSvg(opts),
158
+ openSvgExportPreview: (opts) => this.openSvgExportPreview(opts),
159
+ openXmlPreview: () => this.openXmlPreview(),
160
+ });
161
+
162
+ this._build();
163
+
164
+ this._onKeyDown = (event) => {
165
+ if (event.key !== 'Escape') return;
166
+ if (this._xmlModal) {
167
+ event.preventDefault();
168
+ this._xmlModal.remove();
169
+ this._xmlModal = null;
170
+ return;
171
+ }
172
+ if (this._validationPopover) {
173
+ this._validationPopover.remove();
174
+ this._validationPopover = null;
175
+ }
176
+ if (this._importMenu) this._importMenu.hidden = true;
177
+ if (this._exportMenu) this._exportMenu.hidden = true;
178
+ };
179
+ document.addEventListener('keydown', this._onKeyDown);
180
+ this._cleanups.push(() => document.removeEventListener('keydown', this._onKeyDown));
181
+
182
+ this._offController = this.controller.subscribe((event) => {
183
+ if (this._destroyed) return;
184
+ if (event.type === 'documentChanged' || event.type === 'historyChanged') {
185
+ this._syncToolbarState();
186
+ this._syncBpmnViewerIfActive();
187
+ this._syncStatusBar();
188
+ }
189
+ if (event.type === 'selectionChanged' || event.type === 'documentChanged') {
190
+ this._notifyPanelSelection();
191
+ }
192
+ if (event.type === 'localeChanged') {
193
+ this._rebuildChromeText();
194
+ }
195
+ });
196
+ }
197
+
198
+ _t(key, params) {
199
+ return this.i18n.t(key, params);
200
+ }
201
+
202
+ _shellContext() {
203
+ return {
204
+ controller: this.controller,
205
+ shell: this,
206
+ i18n: this.i18n,
207
+ actions: this.actions,
208
+ getState: () => this.controller.getState(),
209
+ subscribe: (listener) => this.controller.subscribe(listener),
210
+ getMode: () => this.mode,
211
+ setMode: (nextMode) => this.setMode(nextMode),
212
+ getView: () => this.view,
213
+ setView: (nextView) => this.setView(nextView),
214
+ getPanelSelection: () => this.getPanelSelection(),
215
+ subscribePanelSelection: (listener) => this.subscribePanelSelection(listener),
216
+ };
217
+ }
218
+
219
+ _build() {
220
+ this.container.innerHTML = '';
221
+ const ctx = this._shellContext();
222
+
223
+ // Header
224
+ this._header = el('header', 'ft-shell-header');
225
+ if (this.regions.header === 'hidden') {
226
+ this._header.hidden = true;
227
+ this.container.classList.add('is-header-hidden');
228
+ } else if (this.slots.header) {
229
+ const cleanup = mountSlot(this.slots.header, this._header, ctx);
230
+ if (typeof cleanup === 'function') this._cleanups.push(cleanup);
231
+ } else {
232
+ this._buildDefaultHeader(ctx);
233
+ }
234
+
235
+ // Body
236
+ this._body = el('div', 'ft-shell-body');
237
+ this._stageCol = el('div', 'ft-shell-stage-col');
238
+ this._stageHost = el('div', 'ft-shell-stage');
239
+
240
+ // Floating viewport zoom pill
241
+ this._zoomBar = el('div', 'ft-zoom-pill');
242
+ const zoomOut = el('button', 'ft-zoom-btn');
243
+ zoomOut.type = 'button';
244
+ zoomOut.title = this._t('flowTree.shell.tools.zoomOut');
245
+ zoomOut.appendChild(icon('ui.zoomOut', 'nova-icon nova-icon-xs'));
246
+ zoomOut.addEventListener('click', () => this._canvas?.zoomOut?.());
247
+ this._zoomReadout = el('button', 'ft-zoom-readout', '100%');
248
+ this._zoomReadout.type = 'button';
249
+ this._zoomReadout.title = this._t('flowTree.shell.tools.actualSize');
250
+ this._zoomReadout.addEventListener('click', () => this._canvas?.actualSize?.());
251
+ const zoomIn = el('button', 'ft-zoom-btn');
252
+ zoomIn.type = 'button';
253
+ zoomIn.title = this._t('flowTree.shell.tools.zoomIn');
254
+ zoomIn.appendChild(icon('ui.zoomIn', 'nova-icon nova-icon-xs'));
255
+ zoomIn.addEventListener('click', () => this._canvas?.zoomIn?.());
256
+ const fitBtn = el('button', 'ft-zoom-btn');
257
+ fitBtn.type = 'button';
258
+ fitBtn.title = this._t('flowTree.shell.tools.fitView');
259
+ fitBtn.appendChild(icon('ui.fitView', 'nova-icon nova-icon-xs'));
260
+ fitBtn.addEventListener('click', () => this.fitView({ animate: true }));
261
+ this._zoomBtns = { zoomOut, zoomIn, fitBtn };
262
+ this._zoomBar.append(zoomOut, this._zoomReadout, zoomIn, fitBtn);
263
+
264
+ // Status bar
265
+ this._footer = el('footer', 'ft-shell-footer');
266
+ this._statusDot = el('span', 'ft-status-dot');
267
+ this._statusText = el('span', 'ft-status-text');
268
+ this._statusMeta = el('span', 'ft-status-meta');
269
+ this._footer.append(this._statusDot, this._statusText, this._statusMeta);
270
+ if (this.regions.footer === 'hidden') {
271
+ this._footer.hidden = true;
272
+ this._stageCol.classList.add('is-footer-hidden');
273
+ }
274
+
275
+ this._stageCol.append(this._stageHost, this._footer);
276
+
277
+ // Right sidebar + collapse handle
278
+ this._rightWrap = el('aside', 'ft-shell-right');
279
+ this._rightToggle = el('button', 'ft-right-toggle');
280
+ this._rightToggle.type = 'button';
281
+ this._rightToggle.appendChild(icon('flowTree.chevronRight', 'nova-icon nova-icon-xs'));
282
+ this._rightToggle.addEventListener('click', () => this.setSidebarCollapsed(!this._rightCollapsed));
283
+ this._rightContent = el('div', 'ft-shell-right-content');
284
+ this._rightWrap.append(this._rightToggle, this._rightContent);
285
+
286
+ if (this.slots.right) {
287
+ const cleanup = mountSlot(this.slots.right, this._rightContent, ctx);
288
+ if (typeof cleanup === 'function') this._cleanups.push(cleanup);
289
+ } else {
290
+ this._propertiesPanel = new FlowTreePropertiesPanel({
291
+ container: this._rightContent,
292
+ controller: this.controller,
293
+ dataProviders: this.dataProviders,
294
+ nodePanels: this.nodePanels,
295
+ iconRegistry: this.iconRegistry,
296
+ themeController: this.themeController,
297
+ i18n: this.i18n,
298
+ });
299
+ }
300
+
301
+ this._body.append(this._stageCol, this._rightWrap);
302
+ this.container.append(this._header, this._body);
303
+
304
+ this._mountActiveStage();
305
+ this._syncSidebarVisibility();
306
+ this._syncToolbarState();
307
+ this._syncStatusBar();
308
+ hydrateIcons(this.container, this.iconRegistry);
309
+ }
310
+
311
+ _buildDefaultHeader(ctx) {
312
+ // Left brand / headerStart
313
+ const startHost = el('div', 'ft-shell-header-start');
314
+ if (this.slots.headerStart) {
315
+ const cleanup = mountSlot(this.slots.headerStart, startHost, ctx);
316
+ if (typeof cleanup === 'function') this._cleanups.push(cleanup);
317
+ } else {
318
+ const brandMark = el('span', 'ft-brand-mark');
319
+ brandMark.appendChild(icon('flowTree.tree', 'nova-icon nova-icon-sm'));
320
+ const copy = el('div', 'ft-brand-copy');
321
+ this._brandTitle = el('strong', '', this._t('flowTree.brandTitle'));
322
+ this._brandProcessName = el('span', '', this.controller.document.name);
323
+ copy.append(this._brandTitle, this._brandProcessName);
324
+ this._engineSelect = el('select', 'ft-engine-select');
325
+ this._engineSelect.title = this._t('flowTree.shell.tools.engineLabel');
326
+ for (const opt of [
327
+ { value: 'flowable', label: 'Flowable' },
328
+ { value: 'activiti', label: 'Activiti' },
329
+ ]) {
330
+ const o = document.createElement('option');
331
+ o.value = opt.value;
332
+ o.textContent = opt.label;
333
+ if (this.controller.document.engine === opt.value) o.selected = true;
334
+ this._engineSelect.appendChild(o);
335
+ }
336
+ this._engineSelect.addEventListener('change', () => {
337
+ this.controller.commands.setEngine(this._engineSelect.value);
338
+ });
339
+ startHost.append(brandMark, copy, this._engineSelect);
340
+ }
341
+
342
+ // Center: mode switch + view switch
343
+ const center = el('div', 'ft-shell-center');
344
+ this._modeSwitch = el('nav', 'ft-segmented');
345
+ this._modeButtons = new Map();
346
+ if (this.allowedModes.length <= 1) this._modeSwitch.hidden = true;
347
+ const modeIcons = { design: 'ui.design', viewer: 'ui.preview', instance: 'ui.trace' };
348
+ for (const m of this.allowedModes) {
349
+ const btn = el('button', `ft-seg-btn${m === this.mode ? ' is-active' : ''}`);
350
+ btn.type = 'button';
351
+ btn.dataset.mode = m;
352
+ const labelEl = el('span', '', this._t(`flowTree.shell.modes.${m}`));
353
+ btn.append(icon(modeIcons[m], 'nova-icon nova-icon-xs'), labelEl);
354
+ btn.addEventListener('click', () => this.setMode(m));
355
+ this._modeButtons.set(m, { btn, labelEl });
356
+ this._modeSwitch.appendChild(btn);
357
+ }
358
+
359
+ this._viewSwitch = el('nav', 'ft-segmented is-sub');
360
+ this._viewButtons = new Map();
361
+ for (const v of ['tree', 'bpmn', 'timeline']) {
362
+ const btn = el('button', `ft-seg-btn${v === this.view ? ' is-active' : ''}`, this._t(`flowTree.shell.views.${v}`));
363
+ btn.type = 'button';
364
+ btn.dataset.view = v;
365
+ btn.addEventListener('click', () => this.setView(v));
366
+ this._viewButtons.set(v, btn);
367
+ this._viewSwitch.appendChild(btn);
368
+ }
369
+ center.append(this._modeSwitch, this._viewSwitch);
370
+
371
+ // Right tools
372
+ const tools = el('div', 'ft-shell-tools');
373
+ this._undoBtn = el('button', 'ft-tool-btn is-icon');
374
+ this._undoBtn.type = 'button';
375
+ this._undoBtn.title = this._t('flowTree.shell.tools.undo');
376
+ this._undoBtn.appendChild(icon('ui.undo', 'nova-icon nova-icon-sm'));
377
+ this._undoBtn.addEventListener('click', () => this.actions.undo());
378
+
379
+ this._redoBtn = el('button', 'ft-tool-btn is-icon');
380
+ this._redoBtn.type = 'button';
381
+ this._redoBtn.title = this._t('flowTree.shell.tools.redo');
382
+ this._redoBtn.appendChild(icon('ui.redo', 'nova-icon nova-icon-sm'));
383
+ this._redoBtn.addEventListener('click', () => this.actions.redo());
384
+
385
+ this._xmlBtn = el('button', 'ft-tool-btn');
386
+ this._xmlBtn.type = 'button';
387
+ this._xmlLabel = el('span', '', this._t('flowTree.shell.tools.previewXml'));
388
+ this._xmlBtn.append(icon('flowTree.xml', 'nova-icon nova-icon-xs'), this._xmlLabel);
389
+ this._xmlBtn.addEventListener('click', () => this.openXmlPreview());
390
+
391
+ this._designTools = [this._undoBtn, this._redoBtn];
392
+ tools.append(this._undoBtn, this._redoBtn, this._xmlBtn);
393
+
394
+ // Trailing action group (replaceable via slots.headerActions)
395
+ const actionsHost = el('div', 'ft-shell-header-actions');
396
+ if (this.slots.headerActions) {
397
+ const cleanup = mountSlot(this.slots.headerActions, actionsHost, ctx);
398
+ if (typeof cleanup === 'function') this._cleanups.push(cleanup);
399
+ } else {
400
+ // Validate button with badge
401
+ this._validateWrap = el('div', 'ft-menu-wrap');
402
+ this._validateBtn = el('button', 'ft-tool-btn');
403
+ this._validateBtn.type = 'button';
404
+ this._validateLabel = el('span', '', this._t('flowTree.shell.tools.validate'));
405
+ this._validateBadge = el('em', 'ft-tool-badge', '0');
406
+ this._validateBadge.hidden = true;
407
+ this._validateBtn.append(icon('ui.check', 'nova-icon nova-icon-xs'), this._validateLabel, this._validateBadge);
408
+ this._validateBtn.addEventListener('click', (event) => {
409
+ event.stopPropagation();
410
+ this._toggleValidationPopover();
411
+ });
412
+ this._validateWrap.appendChild(this._validateBtn);
413
+
414
+ // Import menu
415
+ this._importWrap = el('div', 'ft-menu-wrap');
416
+ this._importInput = document.createElement('input');
417
+ this._importInput.type = 'file';
418
+ this._importInput.hidden = true;
419
+ this._importInput.addEventListener('change', () => this._handleImportFile());
420
+ const importBtn = el('button', 'ft-tool-btn');
421
+ importBtn.type = 'button';
422
+ this._importLabel = el('span', '', this._t('flowTree.shell.tools.importLabel'));
423
+ importBtn.append(icon('flowTree.upload', 'nova-icon nova-icon-xs'), this._importLabel);
424
+ const importMenu = el('div', 'ft-dropdown-menu');
425
+ importMenu.hidden = true;
426
+ this._importMenu = importMenu;
427
+ this._dropdownTextBindings = [];
428
+ const addImportOption = (accept, kind, titleKey, hintKey) => {
429
+ const opt = el('button', 'ft-dropdown-option');
430
+ opt.type = 'button';
431
+ const titleEl = el('strong', '', this._t(titleKey));
432
+ const hintEl = el('small', '', this._t(hintKey));
433
+ this._dropdownTextBindings.push({ titleEl, hintEl, titleKey, hintKey });
434
+ opt.append(titleEl, hintEl);
435
+ opt.addEventListener('click', () => {
436
+ importMenu.hidden = true;
437
+ this._pendingImportKind = kind;
438
+ this._importInput.accept = accept;
439
+ this._importInput.value = '';
440
+ this._importInput.click();
441
+ });
442
+ importMenu.appendChild(opt);
443
+ };
444
+ addImportOption('.bpmn,.xml,application/xml,text/xml', 'bpmn', 'flowTree.shell.tools.importBpmn', 'flowTree.shell.tools.importBpmnHint');
445
+ addImportOption('.json,application/json', 'json', 'flowTree.shell.tools.importJson', 'flowTree.shell.tools.importJsonHint');
446
+ importBtn.addEventListener('click', (event) => {
447
+ event.stopPropagation();
448
+ exportMenu.hidden = true;
449
+ if (this._validationPopover) { this._validationPopover.remove(); this._validationPopover = null; }
450
+ importMenu.hidden = !importMenu.hidden;
451
+ });
452
+ this._importWrap.append(importBtn, importMenu, this._importInput);
453
+ this._designTools.push(this._importWrap);
454
+
455
+ // Export menu
456
+ const exportWrap = el('div', 'ft-menu-wrap');
457
+ const exportBtn = el('button', 'ft-tool-btn is-primary');
458
+ exportBtn.type = 'button';
459
+ this._exportLabel = el('span', '', this._t('flowTree.shell.tools.exportLabel'));
460
+ exportBtn.append(icon('flowTree.download', 'nova-icon nova-icon-xs'), this._exportLabel);
461
+ const exportMenu = el('div', 'ft-dropdown-menu');
462
+ exportMenu.hidden = true;
463
+ this._exportMenu = exportMenu;
464
+ const addExportOption = (titleKey, hintKey, handler) => {
465
+ const opt = el('button', 'ft-dropdown-option');
466
+ opt.type = 'button';
467
+ const titleEl = el('strong', '', this._t(titleKey));
468
+ const hintEl = el('small', '', this._t(hintKey));
469
+ this._dropdownTextBindings.push({ titleEl, hintEl, titleKey, hintKey });
470
+ opt.append(titleEl, hintEl);
471
+ opt.addEventListener('click', () => {
472
+ exportMenu.hidden = true;
473
+ handler();
474
+ });
475
+ exportMenu.appendChild(opt);
476
+ };
477
+ addExportOption('flowTree.shell.tools.exportBpmn', 'flowTree.shell.tools.exportBpmnHint', () => {
478
+ const xml = this.exportXml();
479
+ downloadText(xml, `${this.controller.document.id || 'approval-process'}.bpmn20.xml`, 'application/xml;charset=utf-8');
480
+ this._setTransientStatus(this._t('flowTree.shell.status.exportedXml'));
481
+ });
482
+ addExportOption('flowTree.shell.tools.exportJson', 'flowTree.shell.tools.exportJsonHint', () => {
483
+ const json = JSON.stringify(this.controller.toJSON(), null, 2);
484
+ downloadText(json, `${this.controller.document.id || 'approval-process'}.flowtree.json`, 'application/json;charset=utf-8');
485
+ this._setTransientStatus(this._t('flowTree.shell.status.exportedJson'));
486
+ });
487
+ addExportOption('flowTree.shell.tools.exportSvg', 'flowTree.shell.tools.exportSvgHint', () => {
488
+ this.openSvgExportPreview();
489
+ });
490
+ exportBtn.addEventListener('click', (event) => {
491
+ event.stopPropagation();
492
+ importMenu.hidden = true;
493
+ if (this._validationPopover) { this._validationPopover.remove(); this._validationPopover = null; }
494
+ exportMenu.hidden = !exportMenu.hidden;
495
+ });
496
+ exportWrap.append(exportBtn, exportMenu);
497
+
498
+ const closeMenus = (event) => {
499
+ if (!this._importWrap.contains(event.target)) importMenu.hidden = true;
500
+ if (!exportWrap.contains(event.target)) exportMenu.hidden = true;
501
+ if (this._validationPopover && !this._validateWrap.contains(event.target)) {
502
+ this._validationPopover.remove();
503
+ this._validationPopover = null;
504
+ }
505
+ };
506
+ document.addEventListener('click', closeMenus);
507
+ this._cleanups.push(() => document.removeEventListener('click', closeMenus));
508
+
509
+ actionsHost.append(this._validateWrap, this._importWrap, exportWrap);
510
+ }
511
+
512
+ tools.appendChild(actionsHost);
513
+ this._header.append(startHost, center, tools);
514
+ }
515
+
516
+ _mountActiveStage() {
517
+ this._destroyStageView();
518
+ this._stageHost.innerHTML = '';
519
+
520
+ const activeView = this.mode === 'design' ? 'tree' : this.view;
521
+ if (activeView === 'tree') {
522
+ const canvasHost = el('div', 'ft-stage-fill');
523
+ this._stageHost.append(canvasHost, this._zoomBar);
524
+ this._zoomBar.hidden = false;
525
+ this._canvas = new FlowTreeCanvas({
526
+ container: canvasHost,
527
+ controller: this.controller,
528
+ mode: this.mode,
529
+ runtime: this.mode === 'instance' ? this.runtime : null,
530
+ runtimeAppearance: this.runtimeAppearance,
531
+ runtimeAssetResolver: this.runtimeAssetResolver,
532
+ formatRuntimeTime: this.formatRuntimeTime,
533
+ runtimeTimeFormat: this.runtimeTimeFormat,
534
+ nodeSummaryResolver: this.nodeSummaryResolver,
535
+ nodeCardRenderer: this.nodeCardRenderer,
536
+ iconRegistry: this.iconRegistry,
537
+ themeController: this.themeController,
538
+ i18n: this.i18n,
539
+ onViewportChange: ({ zoom }) => {
540
+ if (this._zoomReadout) this._zoomReadout.textContent = `${Math.round(zoom * 100)}%`;
541
+ },
542
+ });
543
+ } else {
544
+ const viewerHost = el('div', 'ft-stage-fill');
545
+ this._stageHost.appendChild(viewerHost);
546
+ this._zoomBar.hidden = true;
547
+ const compiled = compileFlowTreeToBpmn(this.controller.document, {
548
+ i18n: this.i18n,
549
+ nodeRegistry: this.controller.nodeRegistry,
550
+ });
551
+ this._bpmnViewer = new BpmnViewer({
552
+ container: viewerHost,
553
+ model: compiled,
554
+ mode: this.mode === 'instance' ? 'instance' : 'viewer',
555
+ runtime: this.mode === 'instance' ? this.runtime : null,
556
+ projection: activeView === 'timeline' ? 'compact' : 'standard',
557
+ runtimeAppearance: this.runtimeAppearance,
558
+ runtimeAssetResolver: this.runtimeAssetResolver,
559
+ formatRuntimeTime: this.formatRuntimeTime,
560
+ runtimeTimeFormat: this.runtimeTimeFormat,
561
+ iconRegistry: this.iconRegistry,
562
+ themeController: this.themeController,
563
+ i18n: this.i18n,
564
+ });
565
+ if (activeView !== 'timeline') {
566
+ requestAnimationFrame(() => {
567
+ if (!this._destroyed) this._bpmnViewer?.fitView?.();
568
+ });
569
+ }
570
+ }
571
+ this._syncViewButtons();
572
+ }
573
+
574
+ _destroyStageView() {
575
+ this._canvas?.destroy();
576
+ this._canvas = null;
577
+ this._bpmnViewer?.destroy();
578
+ this._bpmnViewer = null;
579
+ }
580
+
581
+ _syncBpmnViewerIfActive() {
582
+ if (!this._bpmnViewer) return;
583
+ const compiled = compileFlowTreeToBpmn(this.controller.document, {
584
+ i18n: this.i18n,
585
+ nodeRegistry: this.controller.nodeRegistry,
586
+ });
587
+ this._bpmnViewer.setModel(compiled);
588
+ }
589
+
590
+ _syncViewButtons() {
591
+ if (!this._viewSwitch) return;
592
+ const showSwitch = this.mode !== 'design';
593
+ this._viewSwitch.hidden = !showSwitch;
594
+ if (!showSwitch) return;
595
+ for (const [v, btn] of this._viewButtons.entries()) {
596
+ btn.hidden = v === 'timeline' && this.mode !== 'instance';
597
+ btn.classList.toggle('is-active', v === this.view);
598
+ }
599
+ }
600
+
601
+ _syncSidebarVisibility() {
602
+ const showRight = this.regions.right === 'hidden'
603
+ ? false
604
+ : this.regions.right === 'default'
605
+ ? true
606
+ : this.mode === 'design';
607
+ this._rightWrap.hidden = !showRight;
608
+ this._body.classList.toggle('is-right-hidden', !showRight);
609
+ this._body.classList.toggle('is-right-collapsed', showRight && this._rightCollapsed);
610
+ this._rightWrap.classList.toggle('is-collapsed', this._rightCollapsed);
611
+ }
612
+
613
+ _syncToolbarState() {
614
+ const state = this.controller.getState();
615
+ if (this._brandProcessName) this._brandProcessName.textContent = state.document.name;
616
+ if (this._engineSelect) this._engineSelect.value = state.document.engine;
617
+ if (this._undoBtn) this._undoBtn.disabled = !state.canUndo;
618
+ if (this._redoBtn) this._redoBtn.disabled = !state.canRedo;
619
+ const readonly = this.mode !== 'design';
620
+ for (const item of this._designTools || []) item.hidden = readonly;
621
+ if (this._engineSelect) this._engineSelect.disabled = readonly;
622
+ if (this._validateBadge) {
623
+ const count = state.validationIssues.length;
624
+ this._validateBadge.hidden = count === 0;
625
+ this._validateBadge.textContent = String(count);
626
+ this._validateBadge.classList.toggle('is-error', state.validationIssues.some((i) => i.level === 'error'));
627
+ }
628
+ }
629
+
630
+ _syncStatusBar() {
631
+ if (!this._statusText) return;
632
+ const state = this.controller.getState();
633
+ const errors = state.validationIssues.filter((i) => i.level === 'error').length;
634
+ const warnings = state.validationIssues.filter((i) => i.level === 'warning').length;
635
+ this._statusDot.className = `ft-status-dot is-${errors ? 'error' : warnings ? 'warning' : 'ok'}`;
636
+ this._statusText.textContent = errors || warnings
637
+ ? this._t('flowTree.shell.status.validationIssues', { errors, warnings })
638
+ : this._t('flowTree.shell.status.validPassed');
639
+ this._statusMeta.textContent = `${this._t('flowTree.shell.status.nodeCount', { count: state.nodeCount })} · ${state.engine.toUpperCase()}`;
640
+ }
641
+
642
+ _setTransientStatus(message) {
643
+ if (!this._statusText) return;
644
+ this._statusText.textContent = message;
645
+ }
646
+
647
+ _rebuildChromeText() {
648
+ if (this._brandTitle) this._brandTitle.textContent = this._t('flowTree.brandTitle');
649
+ if (this._engineSelect) this._engineSelect.title = this._t('flowTree.shell.tools.engineLabel');
650
+ this._modeButtons?.forEach(({ labelEl }, m) => {
651
+ labelEl.textContent = this._t(`flowTree.shell.modes.${m}`);
652
+ });
653
+ this._viewButtons?.forEach((btn, v) => {
654
+ btn.textContent = this._t(`flowTree.shell.views.${v}`);
655
+ });
656
+ if (this._undoBtn) this._undoBtn.title = this._t('flowTree.shell.tools.undo');
657
+ if (this._redoBtn) this._redoBtn.title = this._t('flowTree.shell.tools.redo');
658
+ if (this._xmlLabel) this._xmlLabel.textContent = this._t('flowTree.shell.tools.previewXml');
659
+ if (this._validateLabel) this._validateLabel.textContent = this._t('flowTree.shell.tools.validate');
660
+ if (this._importLabel) this._importLabel.textContent = this._t('flowTree.shell.tools.importLabel');
661
+ if (this._exportLabel) this._exportLabel.textContent = this._t('flowTree.shell.tools.exportLabel');
662
+ this._dropdownTextBindings?.forEach(({ titleEl, hintEl, titleKey, hintKey }) => {
663
+ titleEl.textContent = this._t(titleKey);
664
+ hintEl.textContent = this._t(hintKey);
665
+ });
666
+ if (this._zoomBtns) {
667
+ this._zoomBtns.zoomOut.title = this._t('flowTree.shell.tools.zoomOut');
668
+ this._zoomBtns.zoomIn.title = this._t('flowTree.shell.tools.zoomIn');
669
+ this._zoomBtns.fitBtn.title = this._t('flowTree.shell.tools.fitView');
670
+ }
671
+ if (this._zoomReadout) this._zoomReadout.title = this._t('flowTree.shell.tools.actualSize');
672
+ this._syncStatusBar();
673
+ }
674
+
675
+ _toggleValidationPopover() {
676
+ if (this._validationPopover) {
677
+ this._validationPopover.remove();
678
+ this._validationPopover = null;
679
+ return;
680
+ }
681
+ const result = this.validate('toolbar');
682
+ const pop = el('div', 'ft-validation-popover');
683
+ pop.appendChild(el('div', 'ft-validation-head', this._t('flowTree.shell.validationPanel.title')));
684
+ const list = el('div', 'ft-validation-list');
685
+ if (!result.issues.length) {
686
+ list.appendChild(el('div', 'ft-validation-empty', this._t('flowTree.shell.validationPanel.empty')));
687
+ } else {
688
+ for (const issue of result.issues) {
689
+ const row = el('button', `ft-validation-row is-${issue.level}`);
690
+ row.type = 'button';
691
+ row.append(
692
+ icon('flowTree.warning', 'nova-icon nova-icon-xs'),
693
+ el('span', '', issue.message),
694
+ ...(issue.nodeId ? [el('em', '', this._t('flowTree.shell.validationPanel.locate'))] : []),
695
+ );
696
+ row.addEventListener('click', () => {
697
+ if (issue.nodeId) {
698
+ this.controller.commands.select({ kind: 'node', id: issue.nodeId });
699
+ this.revealNode(issue.nodeId);
700
+ }
701
+ pop.remove();
702
+ this._validationPopover = null;
703
+ });
704
+ list.appendChild(row);
705
+ }
706
+ }
707
+ pop.appendChild(list);
708
+ this._validateWrap.appendChild(pop);
709
+ hydrateIcons(pop, this.iconRegistry);
710
+ this._validationPopover = pop;
711
+ }
712
+
713
+ async _handleImportFile() {
714
+ const file = this._importInput?.files?.[0];
715
+ if (!file) return;
716
+ try {
717
+ const content = await file.text();
718
+ if (this._pendingImportKind === 'json') {
719
+ this.controller.fromJSON(content);
720
+ } else {
721
+ const res = this.importXml(content);
722
+ if (!res.compatible) {
723
+ const err = new Error(this._t('flowTree.shell.status.importIncompatible'));
724
+ err.diagnostics = res.diagnostics;
725
+ this.onImportError?.(err, content);
726
+ this._setTransientStatus(err.message);
727
+ return;
728
+ }
729
+ }
730
+ this._setTransientStatus(this._t('flowTree.shell.status.importedSuccess'));
731
+ requestAnimationFrame(() => this.fitView());
732
+ } catch (error) {
733
+ this.onImportError?.(error, '');
734
+ this._setTransientStatus(error?.message || String(error));
735
+ }
736
+ }
737
+
738
+ openXmlPreview() {
739
+ this._xmlModal?.remove();
740
+ const xml = this.exportXml();
741
+ const backdrop = el('div', 'ft-modal-backdrop');
742
+ const modal = el('section', 'ft-xml-modal');
743
+ modal.setAttribute('role', 'dialog');
744
+ modal.setAttribute('aria-label', this._t('flowTree.shell.status.xmlPreviewTitle'));
745
+
746
+ const head = el('header', 'ft-xml-modal-head');
747
+ head.append(
748
+ el('strong', '', `${this._t('flowTree.shell.status.xmlPreviewTitle')} (${this.controller.document.engine.toUpperCase()})`),
749
+ (() => {
750
+ const btns = el('div', 'ft-xml-modal-actions');
751
+ const copyBtn = el('button', 'ft-tool-btn', this._t('flowTree.shell.status.copyXml'));
752
+ copyBtn.type = 'button';
753
+ copyBtn.addEventListener('click', async () => {
754
+ try {
755
+ await navigator.clipboard?.writeText(xml);
756
+ copyBtn.textContent = this._t('flowTree.shell.status.copiedXml');
757
+ } catch {
758
+ // Clipboard unavailable
759
+ }
760
+ });
761
+ const dlBtn = el('button', 'ft-tool-btn is-primary', this._t('flowTree.shell.status.downloadXml'));
762
+ dlBtn.type = 'button';
763
+ dlBtn.addEventListener('click', () => {
764
+ downloadText(xml, `${this.controller.document.id || 'approval-process'}.bpmn20.xml`);
765
+ });
766
+ const closeBtn = el('button', 'ft-icon-btn');
767
+ closeBtn.type = 'button';
768
+ closeBtn.appendChild(icon('ui.close', 'nova-icon nova-icon-sm'));
769
+ closeBtn.addEventListener('click', () => { backdrop.remove(); this._xmlModal = null; });
770
+ btns.append(copyBtn, dlBtn, closeBtn);
771
+ return btns;
772
+ })(),
773
+ );
774
+
775
+ const pre = el('pre', 'ft-xml-pre');
776
+ pre.textContent = xml;
777
+ modal.append(head, pre);
778
+ backdrop.appendChild(modal);
779
+ backdrop.addEventListener('click', (event) => {
780
+ if (event.target === backdrop) { backdrop.remove(); this._xmlModal = null; }
781
+ });
782
+ this.container.appendChild(backdrop);
783
+ hydrateIcons(backdrop, this.iconRegistry);
784
+ this._xmlModal = backdrop;
785
+ }
786
+
787
+ getMode() { return this.mode; }
788
+ setMode(nextMode) {
789
+ if (!this.allowedModes.includes(nextMode) || nextMode === this.mode) return false;
790
+ this.mode = nextMode;
791
+ if (this.mode === 'design') this.view = 'tree';
792
+ else if (this.mode === 'viewer' && this.view === 'timeline') this.view = 'tree';
793
+ this._modeButtons?.forEach(({ btn }, m) => btn.classList.toggle('is-active', m === this.mode));
794
+ this._syncSidebarVisibility();
795
+ this._syncToolbarState();
796
+ this._mountActiveStage();
797
+ this.onModeChange?.({ mode: this.mode, view: this.view });
798
+ this._modeListeners.forEach((fn) => fn({ mode: this.mode, view: this.view }));
799
+ return true;
800
+ }
801
+
802
+ getView() { return this.view; }
803
+ setView(nextView) {
804
+ if (!['tree', 'bpmn', 'timeline'].includes(nextView)) return false;
805
+ if (nextView === 'timeline' && this.mode !== 'instance') return false;
806
+ if (this.mode === 'design' && nextView !== 'tree') return false;
807
+ if (this.view === nextView) return true;
808
+ this.view = nextView;
809
+ this._mountActiveStage();
810
+ return true;
811
+ }
812
+
813
+ setRuntime(runtime) {
814
+ this.runtime = runtime;
815
+ if (this.mode === 'instance') {
816
+ if (this._canvas) this._canvas.setRuntime(runtime);
817
+ if (this._bpmnViewer) this._bpmnViewer.setRuntime(runtime);
818
+ }
819
+ }
820
+
821
+ setDataProviders(providers) {
822
+ this.dataProviders = providers || {};
823
+ this._propertiesPanel?.setDataProviders(this.dataProviders);
824
+ }
825
+
826
+ setSidebarCollapsed(collapsed) {
827
+ this._rightCollapsed = Boolean(collapsed);
828
+ this._syncSidebarVisibility();
829
+ this._sidebarListeners.forEach((fn) => fn(this.getSidebarState()));
830
+ return this._rightCollapsed;
831
+ }
832
+
833
+ getSidebarState() {
834
+ return Object.freeze({ rightCollapsed: this._rightCollapsed });
835
+ }
836
+
837
+ subscribeSidebarChange(listener) {
838
+ if (typeof listener !== 'function') return () => {};
839
+ this._sidebarListeners.add(listener);
840
+ return () => this._sidebarListeners.delete(listener);
841
+ }
842
+
843
+ getPanelSelection() {
844
+ const state = this.controller.getState();
845
+ return Object.freeze({
846
+ selection: state.selection,
847
+ selectedNode: state.selectedNode,
848
+ });
849
+ }
850
+
851
+ subscribePanelSelection(listener) {
852
+ if (typeof listener !== 'function') return () => {};
853
+ this._panelSelectionListeners.add(listener);
854
+ return () => this._panelSelectionListeners.delete(listener);
855
+ }
856
+
857
+ _notifyPanelSelection() {
858
+ const snapshot = this.getPanelSelection();
859
+ this._panelSelectionListeners.forEach((fn) => fn(snapshot));
860
+ }
861
+
862
+ subscribeMode(listener) {
863
+ if (typeof listener !== 'function') return () => {};
864
+ this._modeListeners.add(listener);
865
+ return () => this._modeListeners.delete(listener);
866
+ }
867
+
868
+ subscribeValidation(listener) {
869
+ if (typeof listener !== 'function') return () => {};
870
+ this._validationListeners.add(listener);
871
+ return () => this._validationListeners.delete(listener);
872
+ }
873
+
874
+ validate(source = 'api') {
875
+ const issues = this.controller.validate();
876
+ const errorCount = issues.filter((item) => item.level === 'error').length;
877
+ const warningCount = issues.filter((item) => item.level === 'warning').length;
878
+ const result = Object.freeze({
879
+ source,
880
+ issues,
881
+ errorCount,
882
+ warningCount,
883
+ valid: errorCount === 0,
884
+ });
885
+ this._syncToolbarState();
886
+ this._syncStatusBar();
887
+ this.onValidation?.(result);
888
+ this._validationListeners.forEach((fn) => fn(result));
889
+ return result;
890
+ }
891
+
892
+ exportXml(engine = this.controller.document.engine, options = {}) {
893
+ return exportFlowTreeBpmnXml(this.controller.document, engine, {
894
+ i18n: this.i18n,
895
+ nodeRegistry: this.controller.nodeRegistry,
896
+ ...options,
897
+ });
898
+ }
899
+
900
+ importXml(xml, engine = null, options = {}) {
901
+ const result = decompileBpmnToFlowTree(xml, engine, {
902
+ i18n: this.i18n,
903
+ nodeRegistry: this.controller.nodeRegistry,
904
+ ...options,
905
+ });
906
+ if (result.compatible && result.document) {
907
+ this.controller.setDocument(result.document);
908
+ }
909
+ return result;
910
+ }
911
+
912
+ exportSvg(options = {}) {
913
+ if (this._canvas) return this._canvas.exportSvg(options);
914
+ if (this._bpmnViewer) return this._bpmnViewer.exportSvg(options);
915
+ return null;
916
+ }
917
+
918
+ openSvgExportPreview(options = {}) {
919
+ if (this._svgExportPreview && !this._svgExportPreview.closed) {
920
+ this._svgExportPreview.focus();
921
+ return this._svgExportPreview;
922
+ }
923
+ if (!this._canvas && !this._bpmnViewer) return null;
924
+ const viewLabel = this._t(`flowTree.shell.views.${this.mode === 'design' ? 'tree' : this.view}`);
925
+ const filename = options.filename || `${this.controller.document.name || this.controller.document.id || 'approval-flow'}-${viewLabel}`;
926
+ this._svgExportPreview = openSvgExportPreview({
927
+ container: this.container,
928
+ title: options.previewTitle || this._t('exportSvg.preview.defaultTitle'),
929
+ initialTheme: options.theme || 'current',
930
+ i18n: this.i18n,
931
+ createArtifact: (previewOptions) => this.exportSvg({ ...options, ...previewOptions, filename }),
932
+ onClose: () => { this._svgExportPreview = null; },
933
+ onDownload: (artifact) => {
934
+ this._setTransientStatus(this._t('shell.status.exportedSvg', { filename: artifact.filename }));
935
+ options.onDownload?.(artifact);
936
+ },
937
+ });
938
+ return this._svgExportPreview;
939
+ }
940
+
941
+ fitView(options = {}) {
942
+ this._canvas?.fitView?.(28, options);
943
+ this._bpmnViewer?.fitView?.();
944
+ }
945
+
946
+ revealNode(nodeId) {
947
+ return this._canvas?.revealNode?.(nodeId) ?? false;
948
+ }
949
+
950
+ setTheme(theme) { return this.themeController.setTheme(theme); }
951
+ setLocale(locale, messages) {
952
+ if (!this._ownsI18n && this.controller?.setLocale) return this.controller.setLocale(locale, messages);
953
+ return this.i18n.setLocale(locale, messages);
954
+ }
955
+ getLocale() { return this.i18n.getState(); }
956
+
957
+ destroy() {
958
+ this._destroyed = true;
959
+ this._xmlModal?.remove();
960
+ this._svgExportPreview?.close?.({ immediate: true });
961
+ this._validationPopover?.remove();
962
+ this._destroyStageView();
963
+ this._propertiesPanel?.destroy();
964
+ this._offController?.();
965
+ this._cleanups.splice(0).forEach((fn) => fn?.());
966
+ this.themeController.destroy();
967
+ this.container.innerHTML = '';
968
+ this.container.classList.remove('nova-ft-shell', 'is-header-hidden');
969
+ }
970
+ }