@flowgram.ai/document 0.1.0-alpha.2

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/dist/index.js ADDED
@@ -0,0 +1,3186 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __decorateClass = (decorators, target, key, kind) => {
20
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
21
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
22
+ if (decorator = decorators[i])
23
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
24
+ if (kind && result) __defProp(target, key, result);
25
+ return result;
26
+ };
27
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ ConstantKeys: () => ConstantKeys,
33
+ DEFAULT_FLOW_NODE_META: () => DEFAULT_FLOW_NODE_META,
34
+ DEFAULT_SIZE: () => DEFAULT_SIZE,
35
+ DEFAULT_SPACING: () => DEFAULT_SPACING,
36
+ DRAGGING_TYPE: () => DRAGGING_TYPE,
37
+ DefaultSpacingKey: () => DefaultSpacingKey,
38
+ FLOW_DEFAULT_HIDDEN_TYPES: () => FLOW_DEFAULT_HIDDEN_TYPES,
39
+ FlowDocument: () => FlowDocument,
40
+ FlowDocumentConfig: () => FlowDocumentConfig,
41
+ FlowDocumentConfigDefaultData: () => FlowDocumentConfigDefaultData,
42
+ FlowDocumentConfigEnum: () => FlowDocumentConfigEnum,
43
+ FlowDocumentContainerModule: () => FlowDocumentContainerModule,
44
+ FlowDocumentContribution: () => FlowDocumentContribution,
45
+ FlowDocumentOptions: () => FlowDocumentOptions,
46
+ FlowDocumentOptionsDefault: () => FlowDocumentOptionsDefault,
47
+ FlowDocumentProvider: () => FlowDocumentProvider,
48
+ FlowDocumentTransformerEntity: () => FlowDocumentTransformerEntity,
49
+ FlowDragService: () => FlowDragService,
50
+ FlowGroupController: () => FlowGroupController,
51
+ FlowGroupService: () => FlowGroupService,
52
+ FlowLayout: () => FlowLayout,
53
+ FlowLayoutContribution: () => FlowLayoutContribution,
54
+ FlowLayoutDefault: () => FlowLayoutDefault,
55
+ FlowNodeBaseType: () => FlowNodeBaseType,
56
+ FlowNodeEntity: () => FlowNodeEntity,
57
+ FlowNodeRegistry: () => FlowNodeRegistry,
58
+ FlowNodeRenderData: () => FlowNodeRenderData,
59
+ FlowNodeSplitType: () => FlowNodeSplitType,
60
+ FlowNodeTransformData: () => FlowNodeTransformData,
61
+ FlowNodeTransitionData: () => FlowNodeTransitionData,
62
+ FlowOperationBaseService: () => FlowOperationBaseService,
63
+ FlowOperationBaseServiceImpl: () => FlowOperationBaseServiceImpl,
64
+ FlowRendererStateEntity: () => FlowRendererStateEntity,
65
+ FlowTransitionLabelEnum: () => FlowTransitionLabelEnum,
66
+ FlowTransitionLineEnum: () => FlowTransitionLineEnum,
67
+ FlowVirtualTree: () => FlowVirtualTree,
68
+ LABEL_SIDE_TYPE: () => LABEL_SIDE_TYPE,
69
+ OperationType: () => OperationType,
70
+ drawLineToBottom: () => drawLineToBottom,
71
+ drawLineToNext: () => drawLineToNext,
72
+ getDefaultSpacing: () => getDefaultSpacing
73
+ });
74
+ module.exports = __toCommonJS(src_exports);
75
+
76
+ // src/typings/flow.ts
77
+ var FlowNodeBaseType = /* @__PURE__ */ ((FlowNodeBaseType2) => {
78
+ FlowNodeBaseType2["START"] = "start";
79
+ FlowNodeBaseType2["DEFAULT"] = "default";
80
+ FlowNodeBaseType2["ROOT"] = "root";
81
+ FlowNodeBaseType2["EMPTY"] = "empty";
82
+ FlowNodeBaseType2["INLINE_BLOCKS"] = "inlineBlocks";
83
+ FlowNodeBaseType2["BLOCK_ICON"] = "blockIcon";
84
+ FlowNodeBaseType2["BLOCK"] = "block";
85
+ FlowNodeBaseType2["BLOCK_ORDER_ICON"] = "blockOrderIcon";
86
+ FlowNodeBaseType2["GROUP"] = "group";
87
+ FlowNodeBaseType2["END"] = "end";
88
+ FlowNodeBaseType2["CONDITION"] = "condition";
89
+ FlowNodeBaseType2["SUB_CANVAS"] = "subCanvas";
90
+ return FlowNodeBaseType2;
91
+ })(FlowNodeBaseType || {});
92
+ var FlowNodeSplitType = /* @__PURE__ */ ((FlowNodeSplitType2) => {
93
+ FlowNodeSplitType2["DYNAMIC_SPLIT"] = "dynamicSplit";
94
+ FlowNodeSplitType2["STATIC_SPLIT"] = "staticSplit";
95
+ return FlowNodeSplitType2;
96
+ })(FlowNodeSplitType || {});
97
+ var FlowDocumentConfigEnum = /* @__PURE__ */ ((FlowDocumentConfigEnum2) => {
98
+ FlowDocumentConfigEnum2["END_NODES_REFINE_BRANCH"] = "END_NODES_REFINE_BRANCH";
99
+ return FlowDocumentConfigEnum2;
100
+ })(FlowDocumentConfigEnum || {});
101
+ var FLOW_DEFAULT_HIDDEN_TYPES = [
102
+ "root" /* ROOT */,
103
+ "inlineBlocks" /* INLINE_BLOCKS */,
104
+ "block" /* BLOCK */
105
+ ];
106
+
107
+ // src/typings/flow-layout.ts
108
+ var FlowLayout = Symbol("FlowLayout");
109
+ var FlowLayoutContribution = Symbol("FlowLayoutContribution");
110
+ var FlowLayoutDefault = /* @__PURE__ */ ((FlowLayoutDefault2) => {
111
+ FlowLayoutDefault2["VERTICAL_FIXED_LAYOUT"] = "vertical-fixed-layout";
112
+ FlowLayoutDefault2["HORIZONTAL_FIXED_LAYOUT"] = "horizontal-fixed-layout";
113
+ return FlowLayoutDefault2;
114
+ })(FlowLayoutDefault || {});
115
+ ((FlowLayoutDefault2) => {
116
+ function isVertical(layout) {
117
+ return layout.name === "vertical-fixed-layout" /* VERTICAL_FIXED_LAYOUT */;
118
+ }
119
+ FlowLayoutDefault2.isVertical = isVertical;
120
+ })(FlowLayoutDefault || (FlowLayoutDefault = {}));
121
+
122
+ // src/typings/flow-transition.ts
123
+ var FlowTransitionLineEnum = /* @__PURE__ */ ((FlowTransitionLineEnum2) => {
124
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["STRAIGHT_LINE"] = 0] = "STRAIGHT_LINE";
125
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["DIVERGE_LINE"] = 1] = "DIVERGE_LINE";
126
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["MERGE_LINE"] = 2] = "MERGE_LINE";
127
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["ROUNDED_LINE"] = 3] = "ROUNDED_LINE";
128
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["CUSTOM_LINE"] = 4] = "CUSTOM_LINE";
129
+ FlowTransitionLineEnum2[FlowTransitionLineEnum2["DRAGGING_LINE"] = 5] = "DRAGGING_LINE";
130
+ return FlowTransitionLineEnum2;
131
+ })(FlowTransitionLineEnum || {});
132
+ var FlowTransitionLabelEnum = /* @__PURE__ */ ((FlowTransitionLabelEnum2) => {
133
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["ADDER_LABEL"] = 0] = "ADDER_LABEL";
134
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["TEXT_LABEL"] = 1] = "TEXT_LABEL";
135
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["COLLAPSE_LABEL"] = 2] = "COLLAPSE_LABEL";
136
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["COLLAPSE_ADDER_LABEL"] = 3] = "COLLAPSE_ADDER_LABEL";
137
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["CUSTOM_LABEL"] = 4] = "CUSTOM_LABEL";
138
+ FlowTransitionLabelEnum2[FlowTransitionLabelEnum2["BRANCH_DRAGGING_LABEL"] = 5] = "BRANCH_DRAGGING_LABEL";
139
+ return FlowTransitionLabelEnum2;
140
+ })(FlowTransitionLabelEnum || {});
141
+
142
+ // src/typings/flow-node-register.ts
143
+ var DefaultSpacingKey = {
144
+ /**
145
+ * 普通节点间距。垂直 / 水平
146
+ */
147
+ NODE_SPACING: "SPACING",
148
+ /**
149
+ * 圆弧线条 x radius
150
+ */
151
+ ROUNDED_LINE_X_RADIUS: "ROUNDED_LINE_X_RADIUS",
152
+ /**
153
+ * 圆弧线条 y radius
154
+ */
155
+ ROUNDED_LINE_Y_RADIUS: "ROUNDED_LINE_Y_RADIUS",
156
+ /**
157
+ * dynamicSplit block list 下部留白间距,因为有两个拐弯,所以翻一倍
158
+ */
159
+ INLINE_BLOCKS_PADDING_BOTTOM: "INLINE_BLOCKS_PADDING_BOTTOM",
160
+ /**
161
+ * 复合节点距离上个节点的距离
162
+ * 条件分支菱形下边和分支的距离
163
+ */
164
+ COLLAPSED_SPACING: "COLLAPSED_SPACING",
165
+ /**
166
+ * width of hover area
167
+ */
168
+ HOVER_AREA_WIDTH: "HOVER_AREA_WIDTH"
169
+ };
170
+ var DEFAULT_SPACING = {
171
+ NULL: 0,
172
+ [DefaultSpacingKey.NODE_SPACING]: 32,
173
+ // 普通节点间距。垂直 / 水平
174
+ MARGIN_RIGHT: 20,
175
+ // 普通节点右边间距
176
+ INLINE_BLOCK_PADDING_BOTTOM: 16,
177
+ // block 底部留白
178
+ INLINE_BLOCKS_PADDING_TOP: 30,
179
+ // block list 上部留白间距
180
+ [DefaultSpacingKey.INLINE_BLOCKS_PADDING_BOTTOM]: 40,
181
+ // block lit 下部留白间距,因为有两个拐弯,所以翻一倍
182
+ MIN_INLINE_BLOCK_SPACING: 200,
183
+ // 分支间最小边距 (垂直布局)
184
+ MIN_INLINE_BLOCK_SPACING_HORIZONTAL: 80,
185
+ // 分支间最小边距 (水平布局)
186
+ [DefaultSpacingKey.COLLAPSED_SPACING]: 12,
187
+ // 复合节点距离上个节点的距离
188
+ [DefaultSpacingKey.ROUNDED_LINE_X_RADIUS]: 16,
189
+ // 圆弧线条 x radius
190
+ [DefaultSpacingKey.ROUNDED_LINE_Y_RADIUS]: 20,
191
+ // 圆弧线条 y radius
192
+ [DefaultSpacingKey.HOVER_AREA_WIDTH]: 20
193
+ // width of hover area
194
+ };
195
+ var DRAGGING_TYPE = /* @__PURE__ */ ((DRAGGING_TYPE2) => {
196
+ DRAGGING_TYPE2["NODE"] = "node";
197
+ DRAGGING_TYPE2["BRANCH"] = "branch";
198
+ return DRAGGING_TYPE2;
199
+ })(DRAGGING_TYPE || {});
200
+ var LABEL_SIDE_TYPE = /* @__PURE__ */ ((LABEL_SIDE_TYPE2) => {
201
+ LABEL_SIDE_TYPE2["PRE_BRANCH"] = "pre_branch";
202
+ LABEL_SIDE_TYPE2["NORMAL_BRANCH"] = "normal_branch";
203
+ return LABEL_SIDE_TYPE2;
204
+ })(LABEL_SIDE_TYPE || {});
205
+ var DEFAULT_SIZE = {
206
+ width: 280,
207
+ height: 60
208
+ };
209
+ var DEFAULT_FLOW_NODE_META = (nodeType, document) => {
210
+ const hidden = FLOW_DEFAULT_HIDDEN_TYPES.includes(nodeType);
211
+ return {
212
+ isStart: nodeType === "start",
213
+ hidden,
214
+ defaultExpanded: document.options.allNodesDefaultExpanded,
215
+ expandedSize: { width: 520, height: 300 },
216
+ // 展开后的大小
217
+ size: DEFAULT_SIZE,
218
+ origin: document.layout.getDefaultNodeOrigin(),
219
+ isInlineBlocks: nodeType === "inlineBlocks" /* INLINE_BLOCKS */,
220
+ // miniSize: { width: 200, height: 40 },
221
+ spacing: DEFAULT_SPACING.SPACING,
222
+ inlineSpacingPre: 0,
223
+ inlineSpacingAfter: 0,
224
+ expandable: true,
225
+ draggable: true,
226
+ selectable: true,
227
+ renderKey: "",
228
+ minInlineBlockSpacing: () => {
229
+ const isVertical = FlowLayoutDefault.isVertical(document.layout);
230
+ return isVertical ? DEFAULT_SPACING.MIN_INLINE_BLOCK_SPACING : DEFAULT_SPACING.MIN_INLINE_BLOCK_SPACING_HORIZONTAL;
231
+ }
232
+ };
233
+ };
234
+ var FlowNodeRegistry;
235
+ ((FlowNodeRegistry4) => {
236
+ function merge(registry1, registry2, finalType) {
237
+ return {
238
+ ...registry1,
239
+ ...registry2,
240
+ meta: { ...registry1.meta, ...registry2.meta },
241
+ extend: void 0,
242
+ type: finalType
243
+ };
244
+ }
245
+ FlowNodeRegistry4.merge = merge;
246
+ function extend(registry, extendRegistries) {
247
+ if (!extendRegistries.length) return registry;
248
+ return extendRegistries.reduce((res, ext) => merge(res, ext, registry.type), registry);
249
+ }
250
+ FlowNodeRegistry4.extend = extend;
251
+ })(FlowNodeRegistry || (FlowNodeRegistry = {}));
252
+
253
+ // src/typings/flow-operation.ts
254
+ var OperationType = /* @__PURE__ */ ((OperationType2) => {
255
+ OperationType2["addFromNode"] = "addFromNode";
256
+ OperationType2["deleteFromNode"] = "deleteFromNode";
257
+ OperationType2["addBlock"] = "addBlock";
258
+ OperationType2["deleteBlock"] = "deleteBlock";
259
+ OperationType2["createGroup"] = "createGroup";
260
+ OperationType2["ungroup"] = "ungroup";
261
+ OperationType2["moveNodes"] = "moveNodes";
262
+ OperationType2["moveBlock"] = "moveBlock";
263
+ OperationType2["moveChildNodes"] = "moveChildNodes";
264
+ OperationType2["addNodes"] = "addNodes";
265
+ OperationType2["deleteNodes"] = "deleteNodes";
266
+ OperationType2["changeNode"] = "changeNode";
267
+ OperationType2["addChildNode"] = "addChildNode";
268
+ OperationType2["deleteChildNode"] = "deleteChildNode";
269
+ OperationType2["addNode"] = "addNode";
270
+ OperationType2["deleteNode"] = "deleteNode";
271
+ return OperationType2;
272
+ })(OperationType || {});
273
+ var FlowOperationBaseService = Symbol("FlowOperationBaseService");
274
+
275
+ // src/entities/flow-node-entity.ts
276
+ var import_core4 = require("@flowgram.ai/core");
277
+
278
+ // src/datas/flow-node-transform-data.ts
279
+ var import_utils2 = require("@flowgram.ai/utils");
280
+ var import_core2 = require("@flowgram.ai/core");
281
+
282
+ // src/datas/flow-node-render-data.ts
283
+ var import_utils = require("@flowgram.ai/utils");
284
+ var import_core = require("@flowgram.ai/core");
285
+ var _FlowNodeRenderData = class _FlowNodeRenderData extends import_core.EntityData {
286
+ constructor(entity) {
287
+ super(entity);
288
+ this.onExtInfoChangeEmitter = new import_utils.Emitter();
289
+ this.onExtInfoChange = this.onExtInfoChangeEmitter.event;
290
+ this.toDispose.push(
291
+ import_utils.Disposable.create(() => {
292
+ if (this._node) this._node.remove();
293
+ })
294
+ );
295
+ }
296
+ get key() {
297
+ return this.entity.id;
298
+ }
299
+ getDefaultData() {
300
+ const { addable, expandable, defaultExpanded } = this.entity.getNodeMeta();
301
+ return {
302
+ addable,
303
+ expandable,
304
+ expanded: defaultExpanded || false,
305
+ activated: false,
306
+ hovered: false,
307
+ dragging: false
308
+ };
309
+ }
310
+ updateExtInfo(info) {
311
+ if (import_utils.Compare.isChanged(this.data.extInfo, info)) {
312
+ const oldInfo = this.data.extInfo;
313
+ this.update({
314
+ extInfo: info
315
+ });
316
+ this.onExtInfoChangeEmitter.fire({ oldInfo, newInfo: info });
317
+ }
318
+ }
319
+ getExtInfo() {
320
+ return this.data.extInfo;
321
+ }
322
+ get addable() {
323
+ return this.data.addable;
324
+ }
325
+ get expandable() {
326
+ return this.data.expandable;
327
+ }
328
+ get draggable() {
329
+ const { draggable } = this.entity.getNodeMeta();
330
+ if (typeof draggable === "function") {
331
+ return draggable(this.entity);
332
+ }
333
+ return draggable;
334
+ }
335
+ get expanded() {
336
+ return this.data.expanded;
337
+ }
338
+ set expanded(expanded) {
339
+ if (this.expandable && this.data.expanded !== expanded) {
340
+ this.data.expanded = expanded;
341
+ this.fireChange();
342
+ }
343
+ }
344
+ toggleExpand() {
345
+ this.expanded = !this.expanded;
346
+ }
347
+ toggleMouseEnter(silent = false) {
348
+ this.entity.document.renderState.setNodeHovered(this.entity);
349
+ if (silent) return;
350
+ const transform = this.entity.getData(FlowNodeTransformData);
351
+ if (transform.renderState.hidden) {
352
+ return;
353
+ }
354
+ if (this.mouseLeaveTimeout) {
355
+ clearTimeout(this.mouseLeaveTimeout);
356
+ this.mouseLeaveTimeout = void 0;
357
+ }
358
+ transform.renderState.hovered = true;
359
+ if (this.entity.isFirst && this.entity.parent?.id !== "root") {
360
+ transform.parent.renderState.activated = true;
361
+ } else {
362
+ transform.renderState.activated = true;
363
+ }
364
+ }
365
+ toggleMouseLeave(silent = false) {
366
+ this.entity.document.renderState.setNodeHovered(void 0);
367
+ if (silent) return;
368
+ const transform = this.entity.getData(FlowNodeTransformData);
369
+ this.mouseLeaveTimeout = setTimeout(() => {
370
+ transform.renderState.hovered = false;
371
+ if (this.entity.isFirst && this.entity.parent?.id !== "root") {
372
+ transform.parent.renderState.activated = false;
373
+ }
374
+ transform.renderState.activated = false;
375
+ }, 200);
376
+ }
377
+ get hidden() {
378
+ return this.entity.hidden;
379
+ }
380
+ set hovered(hovered) {
381
+ this.data.hovered = hovered;
382
+ this.fireChange();
383
+ }
384
+ get hovered() {
385
+ return this.data.hovered;
386
+ }
387
+ get dragging() {
388
+ return this.data.dragging;
389
+ }
390
+ set dragging(dragging) {
391
+ if (this.data.dragging !== dragging) {
392
+ this.data.dragging = dragging;
393
+ this.fireChange();
394
+ }
395
+ }
396
+ set activated(activated) {
397
+ if (this.entity.flowNodeType === "blockIcon" /* BLOCK_ICON */ && this.entity.parent) {
398
+ this.entity.parent.getData(_FlowNodeRenderData).activated = activated;
399
+ return;
400
+ }
401
+ if (this.data.activated !== activated) {
402
+ this.data.activated = activated;
403
+ this.fireChange();
404
+ }
405
+ }
406
+ get activated() {
407
+ const { entity } = this;
408
+ if (entity.parent && entity.parent.getData(_FlowNodeRenderData).activated) {
409
+ return true;
410
+ }
411
+ return this.data.activated;
412
+ }
413
+ get lineActivated() {
414
+ const { activated } = this;
415
+ if (!activated) return false;
416
+ return Boolean(
417
+ this.entity.parent?.getData(_FlowNodeRenderData)?.activated || this.entity.isInlineBlock || this.entity.next?.getData(_FlowNodeRenderData).activated
418
+ );
419
+ }
420
+ get node() {
421
+ if (this._node) return this._node;
422
+ this._node = import_utils.domUtils.createDivWithClass("gedit-flow-activity-node");
423
+ this._node.dataset.testid = "sdk.workflow.canvas.node";
424
+ this._node.dataset.nodeId = this.entity.id;
425
+ return this._node;
426
+ }
427
+ dispose() {
428
+ super.dispose();
429
+ this.onExtInfoChangeEmitter.dispose();
430
+ }
431
+ };
432
+ _FlowNodeRenderData.type = "FlowNodeRenderData";
433
+ var FlowNodeRenderData = _FlowNodeRenderData;
434
+
435
+ // src/datas/flow-node-transform-data.ts
436
+ var _FlowNodeTransformData = class _FlowNodeTransformData extends import_core2.EntityData {
437
+ constructor(entity) {
438
+ super(entity);
439
+ this.localDirty = true;
440
+ const { origin } = this.entity.getNodeMeta();
441
+ this.transform = this.entity.addData(import_core2.TransformData);
442
+ this.transform.changeLocked = true;
443
+ this.transform.update({ origin: { ...origin } });
444
+ this.transform.changeLocked = false;
445
+ this.renderState = this.entity.addData(FlowNodeRenderData);
446
+ this.bindChange(this.transform);
447
+ this.toDispose.push(
448
+ import_utils2.Disposable.create(() => {
449
+ const { next, parent } = this;
450
+ if (next) next.localDirty = true;
451
+ if (parent) parent.localDirty = true;
452
+ })
453
+ );
454
+ }
455
+ get origin() {
456
+ return this.transform.origin;
457
+ }
458
+ get key() {
459
+ return this.entity.id;
460
+ }
461
+ getDefaultData() {
462
+ const { size, defaultExpanded, expandedSize, hidden } = this.entity.getNodeMeta();
463
+ const defaultSize = defaultExpanded ? expandedSize : size;
464
+ return {
465
+ size: !hidden ? { ...defaultSize } : { width: 0, height: 0 }
466
+ };
467
+ }
468
+ /**
469
+ * 获取节点是否展开
470
+ */
471
+ get collapsed() {
472
+ return this.entity.collapsed;
473
+ }
474
+ set collapsed(collapsed) {
475
+ this.entity.collapsed = collapsed;
476
+ this.localDirty = true;
477
+ if (this.firstChild) this.firstChild.localDirty = true;
478
+ this.fireChange();
479
+ }
480
+ /**
481
+ * 获取节点的大小
482
+ */
483
+ get size() {
484
+ return this.entity.memoGlobal("size", () => {
485
+ if (this.isContainer) return this.transform.localSize;
486
+ return this.data.size;
487
+ });
488
+ }
489
+ get position() {
490
+ const { position } = this.transform;
491
+ return {
492
+ x: position.x,
493
+ y: position.y
494
+ };
495
+ }
496
+ set size(size) {
497
+ const { width, height } = this.data.size;
498
+ if (this.isContainer) return;
499
+ if (size.width !== width || size.height !== height) {
500
+ this._data.size = { ...size };
501
+ this.localDirty = true;
502
+ this.fireChange();
503
+ }
504
+ }
505
+ get inputPoint() {
506
+ return this.entity.memoGlobal("inputPoint", () => {
507
+ const { getInputPoint } = this.entity.getNodeRegistry();
508
+ return getInputPoint ? getInputPoint(this, this.entity.document.layout) : this.defaultInputPoint;
509
+ });
510
+ }
511
+ get defaultInputPoint() {
512
+ return this.entity.memoGlobal(
513
+ "defaultInputPoint",
514
+ () => this.entity.document.layout.getDefaultInputPoint(this.entity)
515
+ );
516
+ }
517
+ get defaultOutputPoint() {
518
+ return this.entity.memoGlobal(
519
+ "defaultOutputPoint",
520
+ () => this.entity.document.layout.getDefaultOutputPoint(this.entity)
521
+ );
522
+ }
523
+ get outputPoint() {
524
+ return this.entity.memoGlobal("outputPoint", () => {
525
+ const { getOutputPoint } = this.entity.getNodeRegistry();
526
+ return getOutputPoint ? getOutputPoint(this, this.entity.document.layout) : this.defaultOutputPoint;
527
+ });
528
+ }
529
+ /**
530
+ * 原点的最左偏移
531
+ */
532
+ get originDeltaX() {
533
+ return this.entity.memoLocal("originDeltaX", () => {
534
+ const { children } = this;
535
+ const { getOriginDeltaX } = this.entity.getNodeRegistry();
536
+ if (getOriginDeltaX) return getOriginDeltaX(this, this.entity.document.layout);
537
+ if (children.length === 0) {
538
+ return -this.size.width * this.origin.x;
539
+ }
540
+ if (children.length === 1) return children[0].originDeltaX;
541
+ if (this.entity.isInlineBlocks && children.length > 1) {
542
+ return children[0].originDeltaX + this.transform.position.x;
543
+ }
544
+ return children.reduce((res, child) => {
545
+ const deltaX = child.originDeltaX;
546
+ return res === void 0 || deltaX < res ? deltaX : res;
547
+ }, void 0);
548
+ });
549
+ }
550
+ /**
551
+ * 原点 y 轴偏移
552
+ */
553
+ get originDeltaY() {
554
+ return this.entity.memoLocal("originDeltaY", () => {
555
+ const { children } = this;
556
+ const { getOriginDeltaY } = this.entity.getNodeRegistry();
557
+ if (getOriginDeltaY) return getOriginDeltaY(this, this.entity.document.layout);
558
+ if (children.length === 0) {
559
+ return -this.size.height * this.origin.y;
560
+ }
561
+ if (children.length === 1) return children[0].originDeltaY;
562
+ if (this.entity.isInlineBlocks && children.length > 1) {
563
+ return children[0].originDeltaY + this.transform.position.y;
564
+ }
565
+ return children.reduce((res, child) => {
566
+ const deltaY = child.originDeltaY;
567
+ return res === void 0 || deltaY < res ? deltaY : res;
568
+ }, void 0);
569
+ });
570
+ }
571
+ /**
572
+ * 绝对坐标 bbox, 不包含自身的 spacing(marginBottom), 但是包含 inlineSpacing 和 子节点的 spacing
573
+ */
574
+ get bounds() {
575
+ return this.entity.memoGlobal("bounds", () => {
576
+ const { transform } = this;
577
+ if (this.isContainer) {
578
+ const childrenRects = transform.children.map(
579
+ (c) => c.entity.getData(_FlowNodeTransformData).boundsWithPadding
580
+ );
581
+ return import_utils2.Rectangle.enlarge(childrenRects).withPadding(this.padding);
582
+ }
583
+ return transform.bounds;
584
+ });
585
+ }
586
+ get boundsWithPadding() {
587
+ return this.entity.memoGlobal("boundsWithPadding", () => {
588
+ const { transform } = this;
589
+ if (this.isContainer) {
590
+ const childrenRects = transform.children.map(
591
+ (c) => c.entity.getData(_FlowNodeTransformData).boundsWithPadding
592
+ );
593
+ return import_utils2.Rectangle.enlarge(childrenRects).withPadding(this.padding);
594
+ }
595
+ return transform.bounds.clone().withPadding(this.padding);
596
+ });
597
+ }
598
+ get isContainer() {
599
+ return this.transform.isContainer;
600
+ }
601
+ /**
602
+ * 相对坐标 bbox, 这里的 localBounds 会加入 padding 一起算
603
+ */
604
+ get localBounds() {
605
+ return this.entity.memoLocal("localBounds", () => {
606
+ const { transform } = this;
607
+ if (this.isContainer) {
608
+ const childrenRects = transform.children.map(
609
+ (c) => c.entity.getData(_FlowNodeTransformData).localBounds
610
+ );
611
+ const childrenBounds = import_utils2.Rectangle.enlarge(childrenRects).withPadding(this.padding);
612
+ return import_core2.Bounds.applyMatrix(childrenBounds, transform.localTransform);
613
+ }
614
+ return transform.localBounds.clone().withPadding(this.padding);
615
+ });
616
+ }
617
+ get padding() {
618
+ return this.entity.document.layout.getPadding(this.entity);
619
+ }
620
+ setParentTransform(transform) {
621
+ if (this.transform.parent !== transform?.transform) {
622
+ this.localDirty = true;
623
+ }
624
+ this.transform.setParent(transform?.transform);
625
+ }
626
+ get spacing() {
627
+ const { spacing } = this.entity.getNodeMeta();
628
+ return typeof spacing === "function" ? spacing(this) : spacing;
629
+ }
630
+ get inlineSpacingPre() {
631
+ const { inlineSpacingPre } = this.entity.getNodeMeta();
632
+ return typeof inlineSpacingPre === "function" ? inlineSpacingPre(this) : inlineSpacingPre;
633
+ }
634
+ get inlineSpacingAfter() {
635
+ const { inlineSpacingAfter } = this.entity.getNodeMeta();
636
+ return typeof inlineSpacingAfter === "function" ? inlineSpacingAfter(this) : inlineSpacingAfter;
637
+ }
638
+ get minInlineBlockSpacing() {
639
+ const { minInlineBlockSpacing } = this.entity.getNodeMeta();
640
+ return typeof minInlineBlockSpacing === "function" ? minInlineBlockSpacing(this) : minInlineBlockSpacing;
641
+ }
642
+ get children() {
643
+ return this.entity.children.map(
644
+ (child) => child.getData(_FlowNodeTransformData)
645
+ );
646
+ }
647
+ /**
648
+ * 上一个节点的 transform 数据
649
+ */
650
+ get pre() {
651
+ return this.entity.pre?.getData(_FlowNodeTransformData);
652
+ }
653
+ get originParent() {
654
+ return this.entity.originParent?.getData(_FlowNodeTransformData);
655
+ }
656
+ get isFirst() {
657
+ return this.entity.isFirst;
658
+ }
659
+ get isLast() {
660
+ return this.entity.isLast;
661
+ }
662
+ get lastChild() {
663
+ return this.entity.lastChild?.getData(_FlowNodeTransformData);
664
+ }
665
+ get firstChild() {
666
+ return this.entity.firstChild?.getData(_FlowNodeTransformData);
667
+ }
668
+ /**
669
+ * 下一个节点的 transform 数据
670
+ */
671
+ get next() {
672
+ return this.entity.next?.getData(_FlowNodeTransformData);
673
+ }
674
+ /**
675
+ * parent 节点的 transform 数据
676
+ */
677
+ get parent() {
678
+ return this.entity.parent?.getData(_FlowNodeTransformData);
679
+ }
680
+ };
681
+ _FlowNodeTransformData.type = "FlowNodeTransformData";
682
+ var FlowNodeTransformData = _FlowNodeTransformData;
683
+
684
+ // src/datas/flow-node-transition-data.ts
685
+ var import_utils3 = require("@flowgram.ai/utils");
686
+ var import_core3 = require("@flowgram.ai/core");
687
+ var drawLineToNext = (transition) => {
688
+ const { transform } = transition;
689
+ const currentOutput = transform.outputPoint;
690
+ if (transform.next) {
691
+ return [
692
+ {
693
+ type: 0 /* STRAIGHT_LINE */,
694
+ from: currentOutput,
695
+ to: transform.next.inputPoint
696
+ }
697
+ ];
698
+ }
699
+ return [];
700
+ };
701
+ var drawLineToBottom = (transition) => {
702
+ const { transform } = transition;
703
+ const currentOutput = transform.outputPoint;
704
+ const parentOutput = transform.parent?.outputPoint;
705
+ if (!transform.next && parentOutput && !new import_utils3.Point().copyFrom(currentOutput).equals(parentOutput) && !transition.isNodeEnd) {
706
+ return [
707
+ {
708
+ type: 0 /* STRAIGHT_LINE */,
709
+ from: currentOutput,
710
+ to: parentOutput
711
+ }
712
+ ];
713
+ }
714
+ return [];
715
+ };
716
+ var FlowNodeTransitionData = class extends import_core3.EntityData {
717
+ getDefaultData() {
718
+ return {};
719
+ }
720
+ formatLines(lines) {
721
+ if (this.entity.document.options?.formatNodeLines) {
722
+ return this.entity.document.options?.formatNodeLines?.(this.entity, lines);
723
+ }
724
+ return lines;
725
+ }
726
+ formatLabels(labels) {
727
+ if (this.entity.document.options.formatNodeLabels) {
728
+ return this.entity.document.options?.formatNodeLabels?.(this.entity, labels);
729
+ }
730
+ return labels;
731
+ }
732
+ get lines() {
733
+ return this.entity.memoGlobal("lines", () => {
734
+ const { getChildLines } = this.entity.parent?.getNodeRegistry() || {};
735
+ if (getChildLines) {
736
+ return this.formatLines(getChildLines(this, this.entity.document.layout));
737
+ }
738
+ const { getLines } = this.entity.getNodeRegistry();
739
+ if (getLines) {
740
+ return this.formatLines(getLines(this, this.entity.document.layout));
741
+ }
742
+ if (this.transform.entity.isInlineBlock) {
743
+ return [];
744
+ }
745
+ return this.formatLines([...drawLineToNext(this), ...drawLineToBottom(this)]);
746
+ });
747
+ }
748
+ get labels() {
749
+ return this.entity.memoGlobal("labels", () => {
750
+ const { getChildLabels } = this.entity.parent?.getNodeRegistry() || {};
751
+ if (getChildLabels) {
752
+ return this.formatLabels(getChildLabels(this, this.entity.document.layout));
753
+ }
754
+ const { getLabels } = this.entity.getNodeRegistry();
755
+ if (getLabels) {
756
+ return this.formatLabels(getLabels(this, this.entity.document.layout));
757
+ }
758
+ if (this.transform.entity.isInlineBlock) {
759
+ return [];
760
+ }
761
+ const currentOutput = this.transform.outputPoint;
762
+ if (this.transform.next) {
763
+ return this.formatLabels([
764
+ {
765
+ offset: import_utils3.Point.getMiddlePoint(currentOutput, this.transform.next.inputPoint),
766
+ type: 0 /* ADDER_LABEL */
767
+ }
768
+ ]);
769
+ }
770
+ const parentOutput = this.transform.parent?.outputPoint;
771
+ if (parentOutput && !new import_utils3.Point().copyFrom(currentOutput).equals(parentOutput) && !this.isNodeEnd) {
772
+ return this.formatLabels([
773
+ {
774
+ offset: parentOutput,
775
+ type: 0 /* ADDER_LABEL */
776
+ }
777
+ ]);
778
+ }
779
+ return [];
780
+ });
781
+ }
782
+ constructor(entity) {
783
+ super(entity);
784
+ this.transform = this.entity.addData(FlowNodeTransformData);
785
+ this.renderData = this.entity.addData(FlowNodeRenderData);
786
+ this.bindChange(this.transform);
787
+ this.bindChange(this.renderData);
788
+ }
789
+ get collapsed() {
790
+ return this.entity.collapsed;
791
+ }
792
+ get isNodeEnd() {
793
+ return this.entity.isNodeEnd;
794
+ }
795
+ };
796
+ FlowNodeTransitionData.type = "FlowNodeTransitionData";
797
+
798
+ // src/entities/flow-node-entity.ts
799
+ var FlowNodeEntity = class extends import_core4.Entity {
800
+ constructor(conf) {
801
+ super(conf);
802
+ this._memoLocalCache = /* @__PURE__ */ new Map();
803
+ this._memoGlobalCache = /* @__PURE__ */ new Map();
804
+ this.flowNodeType = "unknown";
805
+ // 流程类型
806
+ /**
807
+ * 是否隐藏
808
+ */
809
+ this._hidden = false;
810
+ this.index = -1;
811
+ this.document = conf.document;
812
+ this.flowNodeType = conf.flowNodeType;
813
+ this.originParent = conf.originParent;
814
+ this.metaFromJSON = conf.meta;
815
+ this.onDispose(() => {
816
+ this.document.originTree.getChildren(this).slice().forEach((child) => {
817
+ child.dispose();
818
+ });
819
+ this.document.originTree.remove(this, false);
820
+ this.originParent = void 0;
821
+ });
822
+ }
823
+ initData(initConf) {
824
+ if (initConf.originParent !== this.originParent) {
825
+ this.originParent = initConf.originParent;
826
+ this._registerCache = void 0;
827
+ }
828
+ if (initConf.parent) {
829
+ initConf.parent.addChild(this, initConf.index);
830
+ }
831
+ if (initConf.meta !== this.metaFromJSON) {
832
+ this._metaCache = void 0;
833
+ this.metaFromJSON = initConf.meta;
834
+ }
835
+ this._hidden = !!(this.getNodeMeta().hidden || initConf.hidden);
836
+ }
837
+ get isStart() {
838
+ return this.getNodeMeta().isStart;
839
+ }
840
+ get isFirst() {
841
+ return !this.pre;
842
+ }
843
+ get isLast() {
844
+ return !this.next;
845
+ }
846
+ /**
847
+ * 子节点采用水平布局
848
+ */
849
+ get isInlineBlocks() {
850
+ const originIsInlineBlocks = this.getNodeMeta().isInlineBlocks;
851
+ return typeof originIsInlineBlocks === "function" ? originIsInlineBlocks(this) : originIsInlineBlocks;
852
+ }
853
+ /**
854
+ * 水平节点
855
+ */
856
+ get isInlineBlock() {
857
+ const parent = this.document.renderTree.getParent(this);
858
+ return !!(parent && parent.isInlineBlocks);
859
+ }
860
+ /**
861
+ * 节点结束标记
862
+ * - 当前节点是结束节点
863
+ * - 当前节点最后一个节点包含结束标记
864
+ * - 当前节点为 inlineBlock,每一个 block 包含结束标记
865
+ *
866
+ * 由子元素确定,因此使用 memoLocal
867
+ */
868
+ get isNodeEnd() {
869
+ return this.memoLocal("isNodeEnd", () => {
870
+ if (this.getNodeMeta().isNodeEnd) {
871
+ return true;
872
+ }
873
+ if (this.isInlineBlocks && this.collapsedChildren.length) {
874
+ return this.collapsedChildren.every((child) => child.isNodeEnd);
875
+ }
876
+ if (this.lastCollapsedChild) {
877
+ return this.lastCollapsedChild.isNodeEnd;
878
+ }
879
+ return false;
880
+ });
881
+ }
882
+ /**
883
+ * 添加 子节点
884
+ *
885
+ * @param child 插入节点
886
+ */
887
+ addChild(child, index) {
888
+ if (child.parent === this) return;
889
+ this.document.originTree.addChild(this, child, index);
890
+ }
891
+ get hasChild() {
892
+ return this.children.length > 0;
893
+ }
894
+ get pre() {
895
+ return this.document.renderTree.getPre(this);
896
+ }
897
+ get next() {
898
+ return this.document.renderTree.getNext(this);
899
+ }
900
+ get parent() {
901
+ return this.document.renderTree.getParent(this);
902
+ }
903
+ getNodeRegistry() {
904
+ if (this._registerCache) return this._registerCache;
905
+ this._registerCache = this.document.getNodeRegistry(this.flowNodeType, this.originParent);
906
+ return this._registerCache;
907
+ }
908
+ /**
909
+ * @deprecated
910
+ * use getNodeRegistry instead
911
+ */
912
+ getNodeRegister() {
913
+ return this.getNodeRegistry();
914
+ }
915
+ getNodeMeta() {
916
+ if (this._metaCache) return this._metaCache;
917
+ if (this.metaFromJSON) {
918
+ this._metaCache = {
919
+ ...this.getNodeRegistry().meta,
920
+ ...this.metaFromJSON
921
+ };
922
+ } else {
923
+ this._metaCache = this.getNodeRegistry().meta;
924
+ }
925
+ return this._metaCache;
926
+ }
927
+ /**
928
+ * 获取所有子节点,包含 child 及其所有兄弟节点
929
+ */
930
+ get allChildren() {
931
+ const children = [];
932
+ for (const child of this.children) {
933
+ children.push(child);
934
+ children.push(...child.allChildren);
935
+ }
936
+ return children;
937
+ }
938
+ /**
939
+ * 获取所有收起的子节点,包含 child 及其所有兄弟节点
940
+ */
941
+ get allCollapsedChildren() {
942
+ const children = [];
943
+ for (const child of this.collapsedChildren) {
944
+ children.push(child);
945
+ children.push(...child.allCollapsedChildren);
946
+ }
947
+ return children;
948
+ }
949
+ /**
950
+ *
951
+ * Get child blocks
952
+ *
953
+ * use `blocks` instead
954
+ * @deprecated
955
+ */
956
+ get collapsedChildren() {
957
+ return this.document.renderTree.getCollapsedChildren(this);
958
+ }
959
+ /**
960
+ * Get child blocks
961
+ */
962
+ get blocks() {
963
+ return this.collapsedChildren;
964
+ }
965
+ /**
966
+ * Get last block
967
+ */
968
+ get lastBlock() {
969
+ return this.lastCollapsedChild;
970
+ }
971
+ /**
972
+ * use `lastBlock` instead
973
+ */
974
+ get lastCollapsedChild() {
975
+ const { collapsedChildren } = this;
976
+ return collapsedChildren[collapsedChildren.length - 1];
977
+ }
978
+ /**
979
+ * 获取子节点,如果子节点收起来,则会返回 空数组
980
+ */
981
+ get children() {
982
+ return this.document.renderTree.getChildren(this);
983
+ }
984
+ get lastChild() {
985
+ const { children } = this;
986
+ return children[children.length - 1];
987
+ }
988
+ get firstChild() {
989
+ return this.children[0];
990
+ }
991
+ memoLocal(key, fn) {
992
+ if (this._memoLocalCache.has(key)) {
993
+ return this._memoLocalCache.get(key);
994
+ }
995
+ const data = fn();
996
+ this._memoLocalCache.set(key, data);
997
+ return data;
998
+ }
999
+ memoGlobal(key, fn) {
1000
+ if (this._memoGlobalCache.has(key)) {
1001
+ return this._memoGlobalCache.get(key);
1002
+ }
1003
+ const data = fn();
1004
+ this._memoGlobalCache.set(key, data);
1005
+ return data;
1006
+ }
1007
+ clearMemoGlobal() {
1008
+ this._memoGlobalCache.clear();
1009
+ }
1010
+ clearMemoLocal() {
1011
+ this._memoLocalCache.clear();
1012
+ }
1013
+ get childrenLength() {
1014
+ return this.children.length;
1015
+ }
1016
+ get collapsed() {
1017
+ if (this.document.renderTree.isCollapsed(this)) return true;
1018
+ return !!this.parent?.collapsed;
1019
+ }
1020
+ set collapsed(collapsed) {
1021
+ this.document.renderTree.setCollapsed(this, collapsed);
1022
+ this.clearMemoGlobal();
1023
+ this.clearMemoLocal();
1024
+ }
1025
+ get hidden() {
1026
+ return this._hidden;
1027
+ }
1028
+ // 展开该节点
1029
+ openInsideCollapsed() {
1030
+ this.document.renderTree.openNodeInsideCollapsed(this);
1031
+ }
1032
+ /**
1033
+ * 可以重载
1034
+ */
1035
+ getJSONData() {
1036
+ return this.getExtInfo();
1037
+ }
1038
+ /**
1039
+ * 生成 JSON
1040
+ * @param newId
1041
+ */
1042
+ toJSON() {
1043
+ if (this.document.options.toNodeJSON) {
1044
+ return this.document.options.toNodeJSON(this);
1045
+ }
1046
+ const nodesMap = {};
1047
+ let startNodeJSON;
1048
+ this.document.traverse((node) => {
1049
+ const isSystemNode = node.id.startsWith("$");
1050
+ if (isSystemNode) return;
1051
+ const nodeJSONData = this.getJSONData();
1052
+ const nodeJSON = {
1053
+ id: node.id,
1054
+ type: node.flowNodeType
1055
+ };
1056
+ if (nodeJSONData !== void 0) {
1057
+ nodeJSON.data = nodeJSONData;
1058
+ }
1059
+ if (!startNodeJSON) startNodeJSON = nodeJSON;
1060
+ let { parent } = node;
1061
+ if (parent && parent.id.startsWith("$")) {
1062
+ parent = parent.originParent;
1063
+ }
1064
+ const parentJSON = parent ? nodesMap[parent.id] : void 0;
1065
+ if (parentJSON) {
1066
+ if (!parentJSON.blocks) {
1067
+ parentJSON.blocks = [];
1068
+ }
1069
+ parentJSON.blocks.push(nodeJSON);
1070
+ }
1071
+ nodesMap[node.id] = nodeJSON;
1072
+ }, this);
1073
+ return startNodeJSON;
1074
+ }
1075
+ get isVertical() {
1076
+ return this.document.layout.name === "vertical-fixed-layout" /* VERTICAL_FIXED_LAYOUT */;
1077
+ }
1078
+ /**
1079
+ * 修改节点扩展信息
1080
+ * @param info
1081
+ */
1082
+ updateExtInfo(extInfo) {
1083
+ this.getData(FlowNodeRenderData).updateExtInfo(extInfo);
1084
+ }
1085
+ /**
1086
+ * 获取节点扩展信息
1087
+ */
1088
+ getExtInfo() {
1089
+ return this.getData(FlowNodeRenderData).getExtInfo();
1090
+ }
1091
+ get onExtInfoChange() {
1092
+ return this.renderData.onExtInfoChange;
1093
+ }
1094
+ /**
1095
+ * 获取渲染数据
1096
+ */
1097
+ get renderData() {
1098
+ return this.getData(FlowNodeRenderData);
1099
+ }
1100
+ /**
1101
+ * 获取位置大小数据
1102
+ */
1103
+ get transform() {
1104
+ return this.getData(FlowNodeTransformData);
1105
+ }
1106
+ /**
1107
+ * 获取节点的位置及大小矩形
1108
+ */
1109
+ get bounds() {
1110
+ return this.transform.bounds;
1111
+ }
1112
+ };
1113
+ FlowNodeEntity.type = "FlowNodeEntity";
1114
+ ((FlowNodeEntity2) => {
1115
+ function is(obj) {
1116
+ return obj instanceof FlowNodeEntity2;
1117
+ }
1118
+ FlowNodeEntity2.is = is;
1119
+ })(FlowNodeEntity || (FlowNodeEntity = {}));
1120
+
1121
+ // src/entities/flow-document-transformer-entity.ts
1122
+ var import_utils4 = require("@flowgram.ai/utils");
1123
+ var import_core5 = require("@flowgram.ai/core");
1124
+ var FlowDocumentTransformerEntity = class extends import_core5.ConfigEntity {
1125
+ constructor(conf) {
1126
+ super(conf);
1127
+ this.onRefreshEmitter = new import_utils4.Emitter();
1128
+ this.lastTransformVersion = -1;
1129
+ this.lastTreeVersion = -1;
1130
+ this.onRefresh = this.onRefreshEmitter.event;
1131
+ this.document = conf.document;
1132
+ this.toDispose.push(
1133
+ this.document.originTree.onTreeChange(() => {
1134
+ this.config.treeVersion += 1;
1135
+ this.fireChange();
1136
+ })
1137
+ );
1138
+ this.toDispose.push(this.onRefreshEmitter);
1139
+ }
1140
+ getDefaultConfig() {
1141
+ return {
1142
+ loading: true,
1143
+ treeVersion: 0
1144
+ };
1145
+ }
1146
+ get loading() {
1147
+ return this.config.loading;
1148
+ }
1149
+ set loading(loading) {
1150
+ if (this.config.loading !== loading) {
1151
+ this.config.loading = loading;
1152
+ this.fireChange();
1153
+ }
1154
+ }
1155
+ /**
1156
+ * 更新矩阵结构 (这个只有在树结构变化时候才会触发,如:添加节点、删除节点、改变位置节点)
1157
+ */
1158
+ updateTransformsTree() {
1159
+ this.document.renderTree.traverse((node, depth, index) => {
1160
+ const transform = node.getData(FlowNodeTransformData);
1161
+ if (transform.collapsed) {
1162
+ transform.transform.clearChildren();
1163
+ }
1164
+ if (node.parent) {
1165
+ transform.setParentTransform(node.parent.getData(FlowNodeTransformData));
1166
+ }
1167
+ node.index = index;
1168
+ });
1169
+ }
1170
+ clear() {
1171
+ this.lastTreeVersion = -1;
1172
+ this.lastTransformVersion = -1;
1173
+ }
1174
+ isTreeDirty() {
1175
+ const transformVersion = this.entityManager.getEntityDataVersion(FlowNodeTransformData);
1176
+ const isTreeVersionChanged = this.lastTreeVersion !== this.config.treeVersion;
1177
+ const isTransformVersionChanged = this.lastTransformVersion !== transformVersion;
1178
+ return isTreeVersionChanged || isTransformVersionChanged;
1179
+ }
1180
+ /**
1181
+ * 刷新节点的相对偏移
1182
+ */
1183
+ refresh() {
1184
+ const transformVersion = this.entityManager.getEntityDataVersion(FlowNodeTransformData);
1185
+ const isTreeVersionChanged = this.lastTreeVersion !== this.config.treeVersion;
1186
+ const isTransformVersionChanged = this.lastTransformVersion !== transformVersion;
1187
+ this.entityManager.changeEntityLocked = true;
1188
+ if (isTreeVersionChanged) {
1189
+ this.document.renderTree.updateRenderStruct();
1190
+ this.updateTransformsTree();
1191
+ this.lastTreeVersion = this.config.treeVersion;
1192
+ }
1193
+ if (isTreeVersionChanged || isTransformVersionChanged) {
1194
+ this.document.layout.update();
1195
+ this.lastTransformVersion = this.entityManager.getEntityDataVersion(FlowNodeTransformData);
1196
+ this.lastTreeVersion = this.config.treeVersion;
1197
+ this.onRefreshEmitter.fire();
1198
+ }
1199
+ this.entityManager.changeEntityLocked = false;
1200
+ }
1201
+ };
1202
+ FlowDocumentTransformerEntity.type = "FlowDocumentTransformerEntity";
1203
+
1204
+ // src/entities/flow-renderer-state-entity.ts
1205
+ var import_lodash = require("lodash");
1206
+ var import_core6 = require("@flowgram.ai/core");
1207
+ var FlowRendererStateEntity = class extends import_core6.ConfigEntity {
1208
+ getDefaultConfig() {
1209
+ return {};
1210
+ }
1211
+ constructor(conf) {
1212
+ super(conf);
1213
+ }
1214
+ getNodeHovered() {
1215
+ return this.config.nodeHoveredId ? this.entityManager.getEntityById(this.config.nodeHoveredId) : void 0;
1216
+ }
1217
+ setNodeHovered(node) {
1218
+ this.updateConfig({
1219
+ nodeHoveredId: node?.id
1220
+ });
1221
+ }
1222
+ getDragLabelSide() {
1223
+ return this.config.dragLabelSide;
1224
+ }
1225
+ setDragLabelSide(dragLabelSide) {
1226
+ this.updateConfig({
1227
+ dragLabelSide
1228
+ });
1229
+ }
1230
+ getNodeDroppingId() {
1231
+ return this.config.nodeDroppingId;
1232
+ }
1233
+ setNodeDroppingId(nodeDroppingId) {
1234
+ this.updateConfig({
1235
+ nodeDroppingId
1236
+ });
1237
+ }
1238
+ getDragStartEntity() {
1239
+ const { nodeDragStartId } = this.config;
1240
+ return this.entityManager.getEntityById(nodeDragStartId);
1241
+ }
1242
+ setDragStartEntity(node) {
1243
+ this.updateConfig({
1244
+ nodeDragStartId: node?.id
1245
+ });
1246
+ }
1247
+ // 拖拽多个节点时
1248
+ getDragEntities() {
1249
+ const { nodeDragIds } = this.config;
1250
+ return (nodeDragIds || []).map((_id) => this.entityManager.getEntityById(_id));
1251
+ }
1252
+ // 设置拖拽的节点
1253
+ setDragEntities(nodes) {
1254
+ this.updateConfig({
1255
+ nodeDragIds: nodes.map((_node) => _node.id),
1256
+ nodeDragIdsWithChildren: nodes.map((_node) => [_node.id, ..._node.allCollapsedChildren.map((_n) => _n.id)]).flat()
1257
+ });
1258
+ }
1259
+ onNodeHoveredChange(fn, debounceTime = 100) {
1260
+ return this.onConfigChanged((0, import_lodash.debounce)(() => fn(this.getNodeHovered()), debounceTime));
1261
+ }
1262
+ };
1263
+ FlowRendererStateEntity.type = "FlowRendererStateEntity";
1264
+
1265
+ // src/flow-document.ts
1266
+ var import_lodash2 = require("lodash");
1267
+ var import_inversify2 = require("inversify");
1268
+ var import_utils7 = require("@flowgram.ai/utils");
1269
+ var import_core7 = require("@flowgram.ai/core");
1270
+
1271
+ // src/flow-virtual-tree.ts
1272
+ var import_utils5 = require("@flowgram.ai/utils");
1273
+ var FlowVirtualTree = class _FlowVirtualTree {
1274
+ constructor(root) {
1275
+ this.root = root;
1276
+ this.onTreeChangeEmitter = new import_utils5.Emitter();
1277
+ /**
1278
+ * tree 结构变化时候触发
1279
+ */
1280
+ this.onTreeChange = this.onTreeChangeEmitter.event;
1281
+ this.map = /* @__PURE__ */ new Map();
1282
+ }
1283
+ dispose() {
1284
+ this.map.clear();
1285
+ this.onTreeChangeEmitter.dispose();
1286
+ }
1287
+ getInfo(node) {
1288
+ let res = this.map.get(node);
1289
+ if (!res) {
1290
+ res = { children: [] };
1291
+ this.map.set(node, res);
1292
+ }
1293
+ return res;
1294
+ }
1295
+ clear() {
1296
+ this.map.clear();
1297
+ }
1298
+ cloneMap() {
1299
+ const newMap = /* @__PURE__ */ new Map();
1300
+ for (const [key, value] of this.map) {
1301
+ newMap.set(key, {
1302
+ ...value,
1303
+ children: value.children.slice()
1304
+ });
1305
+ }
1306
+ return newMap;
1307
+ }
1308
+ clone() {
1309
+ const newTree = new _FlowVirtualTree(this.root);
1310
+ newTree.map = this.cloneMap();
1311
+ return newTree;
1312
+ }
1313
+ remove(node, withChildren = true) {
1314
+ this.removeParent(node);
1315
+ if (withChildren) {
1316
+ this._removeChildren(node);
1317
+ }
1318
+ this.map.delete(node);
1319
+ this.fireTreeChange();
1320
+ }
1321
+ addChild(parent, child, index) {
1322
+ const parentInfo = this.getInfo(parent);
1323
+ const childInfo = this.getInfo(child);
1324
+ if (childInfo.parent) {
1325
+ if (childInfo.parent === parent) return child;
1326
+ if (childInfo.parent !== parent) {
1327
+ this.removeParent(child);
1328
+ }
1329
+ }
1330
+ const len = parentInfo.children.length;
1331
+ const idx = typeof index === "undefined" ? len - 1 : index - 1;
1332
+ const lastChild = parentInfo.children[idx];
1333
+ const nextChild = parentInfo.children[idx + 1];
1334
+ if (lastChild) this.getInfo(lastChild).next = child;
1335
+ if (nextChild) this.getInfo(nextChild).pre = child;
1336
+ childInfo.pre = lastChild;
1337
+ childInfo.next = nextChild;
1338
+ parentInfo.children.splice(idx + 1, 0, child);
1339
+ childInfo.parent = parent;
1340
+ this.fireTreeChange();
1341
+ return child;
1342
+ }
1343
+ moveChilds(parent, childs, index) {
1344
+ const parentInfo = this.getInfo(parent);
1345
+ const len = parentInfo.children.length;
1346
+ let childIndex = index ?? len;
1347
+ childs.forEach((child) => {
1348
+ const childInfo = this.getInfo(child);
1349
+ if (childInfo.parent) {
1350
+ this.removeParent(child);
1351
+ }
1352
+ });
1353
+ childs.forEach((child) => {
1354
+ const childInfo = this.getInfo(child);
1355
+ let lastChild = parentInfo.children[childIndex - 1];
1356
+ let nextChild = parentInfo.children[childIndex];
1357
+ if (lastChild) this.getInfo(lastChild).next = child;
1358
+ if (nextChild) this.getInfo(nextChild).pre = child;
1359
+ childInfo.pre = lastChild;
1360
+ childInfo.next = nextChild;
1361
+ parentInfo.children.splice(childIndex, 0, child);
1362
+ childInfo.parent = parent;
1363
+ childIndex++;
1364
+ });
1365
+ this.fireTreeChange();
1366
+ return childs;
1367
+ }
1368
+ getById(id) {
1369
+ for (const node of this.map.keys()) {
1370
+ if (node.id === id) return node;
1371
+ }
1372
+ }
1373
+ /**
1374
+ * 插入节点到后边
1375
+ * @param before
1376
+ * @param after
1377
+ */
1378
+ insertAfter(before, after) {
1379
+ const beforeInfo = this.getInfo(before);
1380
+ const afterInfo = this.getInfo(after);
1381
+ this.removeParent(after);
1382
+ if (beforeInfo.parent) {
1383
+ const parentInfo = this.getInfo(beforeInfo.parent);
1384
+ parentInfo.children.splice(parentInfo.children.indexOf(before) + 1, 0, after);
1385
+ const { next } = beforeInfo;
1386
+ if (next) {
1387
+ this.getInfo(next).pre = after;
1388
+ }
1389
+ afterInfo.next = next;
1390
+ beforeInfo.next = after;
1391
+ afterInfo.pre = before;
1392
+ afterInfo.parent = beforeInfo.parent;
1393
+ }
1394
+ this.fireTreeChange();
1395
+ }
1396
+ removeParent(node) {
1397
+ const info = this.getInfo(node);
1398
+ if (!info.parent) return;
1399
+ const parentInfo = this.getInfo(info.parent);
1400
+ const index = parentInfo.children.indexOf(node);
1401
+ parentInfo.children.splice(index, 1);
1402
+ const { pre, next } = info;
1403
+ if (pre) this.getInfo(pre).next = next;
1404
+ if (next) this.getInfo(next).pre = pre;
1405
+ this.fireTreeChange();
1406
+ }
1407
+ _removeChildren(node) {
1408
+ const children = this.getChildren(node);
1409
+ if (children.length > 0) {
1410
+ children.forEach((child) => {
1411
+ this._removeChildren(child);
1412
+ this.map.delete(child);
1413
+ });
1414
+ }
1415
+ }
1416
+ getParent(node) {
1417
+ return this.getInfo(node).parent;
1418
+ }
1419
+ getPre(node) {
1420
+ return this.getInfo(node).pre;
1421
+ }
1422
+ getNext(node) {
1423
+ return this.getInfo(node).next;
1424
+ }
1425
+ getChildren(node) {
1426
+ return this.getInfo(node).children;
1427
+ }
1428
+ traverse(fn, node = this.root, depth = 0, index = 0) {
1429
+ const breaked = fn(node, depth, index);
1430
+ if (breaked) return true;
1431
+ const info = this.getInfo(node);
1432
+ const shouldBreak = info.children.find((child, i) => this.traverse(fn, child, depth + 1, i));
1433
+ if (shouldBreak) return true;
1434
+ }
1435
+ /**
1436
+ * 通知文档树结构更新
1437
+ */
1438
+ fireTreeChange() {
1439
+ this.onTreeChangeEmitter.fire();
1440
+ }
1441
+ get size() {
1442
+ return this.map.size;
1443
+ }
1444
+ toString() {
1445
+ const ret = [];
1446
+ this.traverse((node, depth) => {
1447
+ if (depth === 0) {
1448
+ ret.push(node.id);
1449
+ } else {
1450
+ ret.push(`|${new Array(depth).fill("--").join("")} ${node.id}`);
1451
+ }
1452
+ });
1453
+ return `${ret.join("\n")}`;
1454
+ }
1455
+ };
1456
+
1457
+ // src/flow-render-tree.ts
1458
+ var FlowRenderTree = class extends FlowVirtualTree {
1459
+ constructor(root, originTree, document) {
1460
+ super(root);
1461
+ this.root = root;
1462
+ /**
1463
+ * 折叠的节点
1464
+ * @protected
1465
+ */
1466
+ this.nodesCollapsed = /* @__PURE__ */ new Set();
1467
+ this.originTree = originTree;
1468
+ this.onTreeChange = this.originTree.onTreeChange;
1469
+ this.document = document;
1470
+ }
1471
+ isCollapsed(node) {
1472
+ return this.nodesCollapsed.has(node);
1473
+ }
1474
+ get collapsedNodeList() {
1475
+ return Array.from(this.nodesCollapsed);
1476
+ }
1477
+ /**
1478
+ * 折叠元素
1479
+ * @param node
1480
+ * @param collapsed
1481
+ */
1482
+ setCollapsed(node, collapsed) {
1483
+ if (collapsed) {
1484
+ this.nodesCollapsed.add(node);
1485
+ } else {
1486
+ this.nodesCollapsed.delete(node);
1487
+ }
1488
+ this.originTree.fireTreeChange();
1489
+ }
1490
+ /**
1491
+ *
1492
+ */
1493
+ openNodeInsideCollapsed(node) {
1494
+ let curr = this.originTree.getInfo(node)?.parent;
1495
+ while (curr) {
1496
+ if (this.nodesCollapsed.has(curr)) {
1497
+ this.nodesCollapsed.delete(curr);
1498
+ }
1499
+ const { parent } = this.originTree.getInfo(curr) || {};
1500
+ curr = parent;
1501
+ }
1502
+ this.originTree.fireTreeChange();
1503
+ }
1504
+ /**
1505
+ * 更新结束节点等位置信息,分支里如果全是结束节点则要做相应的偏移
1506
+ */
1507
+ updateRenderStruct() {
1508
+ this.map = this.originTree.cloneMap();
1509
+ if (this.document.config.get("END_NODES_REFINE_BRANCH" /* END_NODES_REFINE_BRANCH */)) {
1510
+ this.refineBranch(this.root);
1511
+ }
1512
+ this.hideCollapsed();
1513
+ }
1514
+ /**
1515
+ * 隐藏收起节点
1516
+ */
1517
+ hideCollapsed() {
1518
+ this.nodesCollapsed.forEach((collapsedNode) => {
1519
+ const collapsedNodeInfo = this.getInfo(collapsedNode);
1520
+ if (!collapsedNodeInfo) {
1521
+ this.nodesCollapsed.delete(collapsedNode);
1522
+ return;
1523
+ }
1524
+ const iconChild = collapsedNodeInfo.children.find(
1525
+ (_child) => _child.flowNodeType === "blockIcon" /* BLOCK_ICON */ || _child.flowNodeType === "blockOrderIcon" /* BLOCK_ORDER_ICON */
1526
+ );
1527
+ if (iconChild) {
1528
+ const iconInfo = this.getInfo(iconChild);
1529
+ iconInfo.next = void 0;
1530
+ iconInfo.pre = void 0;
1531
+ collapsedNodeInfo.children = [iconChild];
1532
+ return;
1533
+ }
1534
+ collapsedNodeInfo.children = [];
1535
+ });
1536
+ }
1537
+ // 节点是否为结束节点
1538
+ isNodeEnd(node) {
1539
+ if (node.getNodeMeta().isNodeEnd) {
1540
+ return true;
1541
+ }
1542
+ const { children } = this.getInfo(node);
1543
+ if (children.length > 0 && node.isInlineBlocks) {
1544
+ return children.every((child) => this.isNodeEnd(child));
1545
+ }
1546
+ if (node.isInlineBlock) {
1547
+ return this.isNodeEnd(children[children.length - 1]);
1548
+ }
1549
+ return false;
1550
+ }
1551
+ /**
1552
+ * 优化精简分支线
1553
+ * - 结束节点拉直分支线
1554
+ */
1555
+ refineBranch(block) {
1556
+ let curr = this.getInfo(block).children[0];
1557
+ while (curr) {
1558
+ if (curr.flowNodeType === "dynamicSplit" /* DYNAMIC_SPLIT */ || curr.flowNodeType === "staticSplit" /* STATIC_SPLIT */) {
1559
+ const { next, children: branchChildren } = this.getInfo(curr);
1560
+ const { children } = this.getInfo(branchChildren[1]);
1561
+ const passBlocks = (children || []).filter((child) => !this.isNodeEnd(child));
1562
+ const shouldDragAllNextNodes = passBlocks.length === 1;
1563
+ if (shouldDragAllNextNodes && next) {
1564
+ this.dragNextNodesToBlock(passBlocks[0], next);
1565
+ }
1566
+ children?.forEach((child) => {
1567
+ this.refineBranch(child);
1568
+ });
1569
+ if (shouldDragAllNextNodes) {
1570
+ break;
1571
+ }
1572
+ }
1573
+ curr = curr.next;
1574
+ }
1575
+ }
1576
+ // 结束节点拽分支,将后续节点拽到对应分支内
1577
+ dragNextNodesToBlock(toBlock, next) {
1578
+ const toBlockInfo = this.getInfo(toBlock);
1579
+ const nextInfo = this.getInfo(next);
1580
+ const toBlockLastChild = toBlockInfo.children[toBlock.children.length - 1];
1581
+ if (nextInfo.parent) {
1582
+ const nextParentInfo = this.getInfo(nextInfo.parent);
1583
+ if (nextInfo.pre) {
1584
+ this.getInfo(nextInfo.pre).next = void 0;
1585
+ }
1586
+ if (toBlockLastChild) {
1587
+ const lastChildInfo = this.getInfo(toBlockLastChild);
1588
+ lastChildInfo.next = next;
1589
+ nextInfo.pre = toBlockLastChild;
1590
+ }
1591
+ const nextNodeIndex = nextParentInfo.children.indexOf(next);
1592
+ const allNextNodes = nextParentInfo.children.slice(nextNodeIndex);
1593
+ nextParentInfo.children = nextParentInfo.children.slice(0, nextNodeIndex);
1594
+ for (const node of allNextNodes) {
1595
+ const nodeInfo = this.getInfo(node);
1596
+ toBlockInfo.children.push(node);
1597
+ nodeInfo.parent = toBlock;
1598
+ }
1599
+ }
1600
+ }
1601
+ getInfo(node) {
1602
+ const info = this.map.get(node) || this.originTree.getInfo(node);
1603
+ return info;
1604
+ }
1605
+ // 或者originTree节点的信息
1606
+ getOriginInfo(node) {
1607
+ return this.originTree.getInfo(node);
1608
+ }
1609
+ // 获取收起的隐藏节点
1610
+ getCollapsedChildren(node) {
1611
+ return this.getOriginInfo(node).children || [];
1612
+ }
1613
+ remove() {
1614
+ throw new Error("Render Tree cannot use remove node");
1615
+ }
1616
+ addChild() {
1617
+ throw new Error("Render tree cannot use add child");
1618
+ }
1619
+ insertAfter() {
1620
+ throw new Error("Render tree cannot use insert after");
1621
+ }
1622
+ removeParent() {
1623
+ throw new Error("Render tree cannot use remove parent");
1624
+ }
1625
+ };
1626
+
1627
+ // src/flow-document-options.ts
1628
+ var FlowDocumentOptions = Symbol("FlowDocumentOptions");
1629
+ var FlowDocumentOptionsDefault = {
1630
+ allNodesDefaultExpanded: false
1631
+ };
1632
+ var ConstantKeys = {
1633
+ ...DefaultSpacingKey,
1634
+ /**
1635
+ * loop 底部留白
1636
+ */
1637
+ INLINE_SPACING_BOTTOM: "INLINE_SPACING_BOTTOM",
1638
+ /**
1639
+ * inlineBlocks 的 inlineTop
1640
+ * loop 循环线条上边距
1641
+ */
1642
+ INLINE_BLOCKS_INLINE_SPACING_TOP: "INLINE_BLOCKS_INLINE_SPACING_TOP",
1643
+ /**
1644
+ * inlineBlocks 的 inlineBottom
1645
+ * loop 循环线条的下边距
1646
+ *
1647
+ */
1648
+ INLINE_BLOCKS_INLINE_SPACING_BOTTOM: "INLINE_BLOCKS_INLINE_SPACING_BOTTOM",
1649
+ /***
1650
+ * 线条、label 默认颜色
1651
+ */
1652
+ BASE_COLOR: "BASE_COLOR",
1653
+ /***
1654
+ * 线条、label 激活后的颜色
1655
+ */
1656
+ BASE_ACTIVATED_COLOR: "BASE_ACTIVATED_COLOR"
1657
+ };
1658
+
1659
+ // src/flow-document-contribution.ts
1660
+ var FlowDocumentContribution = Symbol("FlowDocumentContribution");
1661
+
1662
+ // src/flow-document-config.ts
1663
+ var import_inversify = require("inversify");
1664
+ var import_utils6 = require("@flowgram.ai/utils");
1665
+ var FlowDocumentConfigDefaultData = Symbol("FlowDocumentConfigDefaultData");
1666
+ var FlowDocumentConfig = class {
1667
+ constructor(_data = {}) {
1668
+ this._data = _data;
1669
+ this.onDataChangeEmitter = new import_utils6.Emitter();
1670
+ this.onChange = this.onDataChangeEmitter.event;
1671
+ }
1672
+ get(key) {
1673
+ return this._data[key];
1674
+ }
1675
+ set(key, value) {
1676
+ if (this.get(key) !== value) {
1677
+ this._data[key] = value;
1678
+ this.onDataChangeEmitter.fire(key);
1679
+ }
1680
+ }
1681
+ registerConfigs(config) {
1682
+ Object.keys(config).forEach((key) => {
1683
+ this.set(key, config[key]);
1684
+ });
1685
+ }
1686
+ };
1687
+ FlowDocumentConfig = __decorateClass([
1688
+ (0, import_inversify.injectable)(),
1689
+ __decorateParam(0, (0, import_inversify.inject)(FlowDocumentConfigDefaultData)),
1690
+ __decorateParam(0, (0, import_inversify.optional)())
1691
+ ], FlowDocumentConfig);
1692
+
1693
+ // src/flow-document.ts
1694
+ var FlowDocumentProvider = Symbol("FlowDocumentProvider");
1695
+ var FlowDocument = class {
1696
+ constructor() {
1697
+ this.contributions = [];
1698
+ this.registers = /* @__PURE__ */ new Map();
1699
+ this.nodeRegistryCache = /* @__PURE__ */ new Map();
1700
+ this.nodeDataRegistries = [];
1701
+ this.layouts = [];
1702
+ this.currentLayoutKey = "";
1703
+ this.onNodeUpdateEmitter = new import_utils7.Emitter();
1704
+ this.onNodeCreateEmitter = new import_utils7.Emitter();
1705
+ this.onNodeDisposeEmitter = new import_utils7.Emitter();
1706
+ this.onLayoutChangeEmitter = new import_utils7.Emitter();
1707
+ this.onNodeUpdate = this.onNodeUpdateEmitter.event;
1708
+ this.onNodeCreate = this.onNodeCreateEmitter.event;
1709
+ this.onNodeDispose = this.onNodeDisposeEmitter.event;
1710
+ this.onLayoutChange = this.onLayoutChangeEmitter.event;
1711
+ }
1712
+ init() {
1713
+ if (!this.options) this.options = FlowDocumentOptionsDefault;
1714
+ this.currentLayoutKey = this.options.defaultLayout || "vertical-fixed-layout" /* VERTICAL_FIXED_LAYOUT */;
1715
+ this.contributions.forEach((contrib) => contrib.registerDocument?.(this));
1716
+ this.root = this.addNode({ id: "root", type: "root" /* ROOT */ });
1717
+ this.originTree = new FlowVirtualTree(this.root);
1718
+ this.transformer = this.entityManager.createEntity(
1719
+ FlowDocumentTransformerEntity,
1720
+ { document: this }
1721
+ );
1722
+ this.renderState = this.entityManager.createEntity(FlowRendererStateEntity);
1723
+ this.renderTree = new FlowRenderTree(this.root, this.originTree, this);
1724
+ this.layout.reload?.();
1725
+ }
1726
+ /**
1727
+ * 从数据初始化 O(n)
1728
+ * @param json
1729
+ */
1730
+ /**
1731
+ * 加载数据,可以被重载
1732
+ * @param json 文档数据更新
1733
+ * @param fireRender 是否要触发渲染,默认 true
1734
+ */
1735
+ fromJSON(json, fireRender = true) {
1736
+ this.originTree.clear();
1737
+ this.renderTree.clear();
1738
+ this.entityManager.changeEntityLocked = true;
1739
+ const oldNodes = this.entityManager.getEntities(FlowNodeEntity);
1740
+ const newNodes = [this.root];
1741
+ this.addBlocksAsChildren(this.root, json.nodes || [], newNodes);
1742
+ oldNodes.forEach((node) => {
1743
+ if (!newNodes.includes(node)) {
1744
+ node.dispose();
1745
+ }
1746
+ });
1747
+ this.entityManager.changeEntityLocked = false;
1748
+ this.transformer.loading = false;
1749
+ if (fireRender) this.fireRender();
1750
+ }
1751
+ get layout() {
1752
+ const layout = this.layouts.find((layout2) => layout2.name == this.currentLayoutKey);
1753
+ if (!layout) {
1754
+ throw new Error(`Unknown flow layout: ${this.currentLayoutKey}`);
1755
+ }
1756
+ return layout;
1757
+ }
1758
+ async load() {
1759
+ await Promise.all(this.contributions.map((c) => c.loadDocument?.(this)));
1760
+ }
1761
+ get loading() {
1762
+ return this.transformer.loading;
1763
+ }
1764
+ /**
1765
+ * 触发 render
1766
+ */
1767
+ fireRender() {
1768
+ if (this.transformer.isTreeDirty()) {
1769
+ this.entityManager.fireEntityChanged(FlowNodeEntity.type);
1770
+ this.entityManager.fireEntityChanged(FlowDocumentTransformerEntity.type);
1771
+ }
1772
+ }
1773
+ /**
1774
+ * 从指定节点的下一个节点新增
1775
+ * @param fromNode
1776
+ * @param json
1777
+ */
1778
+ addFromNode(fromNode, json) {
1779
+ const node = typeof fromNode === "string" ? this.getNode(fromNode) : fromNode;
1780
+ this.entityManager.changeEntityLocked = true;
1781
+ const { parent } = node;
1782
+ const result = this.addNode({
1783
+ ...json,
1784
+ parent
1785
+ // originParent,
1786
+ });
1787
+ this.originTree.insertAfter(node, result);
1788
+ this.entityManager.changeEntityLocked = false;
1789
+ this.entityManager.fireEntityChanged(FlowNodeEntity.type);
1790
+ return result;
1791
+ }
1792
+ removeNode(node) {
1793
+ if (typeof node === "string") {
1794
+ this.getNode(node)?.dispose();
1795
+ } else {
1796
+ node.dispose();
1797
+ }
1798
+ }
1799
+ /**
1800
+ * 添加节点,如果节点已经存在则不会重复创建
1801
+ * @param data
1802
+ * @param addedNodes
1803
+ */
1804
+ addNode(data, addedNodes, ignoreCreateEvent) {
1805
+ const { id, type = "block", originParent, parent, meta, hidden, index } = data;
1806
+ let node = this.getNode(id);
1807
+ let isNew = false;
1808
+ const register = this.getNodeRegistry(type, data.originParent);
1809
+ if (node && node.flowNodeType !== data.type) {
1810
+ node.dispose();
1811
+ node = void 0;
1812
+ }
1813
+ if (!node) {
1814
+ const { dataRegistries } = register;
1815
+ node = this.entityManager.createEntity(FlowNodeEntity, {
1816
+ id,
1817
+ document: this,
1818
+ flowNodeType: type,
1819
+ originParent,
1820
+ meta
1821
+ });
1822
+ const datas = dataRegistries ? this.nodeDataRegistries.concat(...dataRegistries) : this.nodeDataRegistries;
1823
+ node.addInitializeData(datas);
1824
+ node.onDispose(() => this.onNodeDisposeEmitter.fire({ node }));
1825
+ if (this.options.fromNodeJSON) {
1826
+ this.options.fromNodeJSON(node, data);
1827
+ }
1828
+ isNew = true;
1829
+ }
1830
+ node.initData({
1831
+ originParent,
1832
+ parent,
1833
+ meta,
1834
+ hidden,
1835
+ index
1836
+ });
1837
+ if (node.isStart) {
1838
+ this.root.addChild(node);
1839
+ }
1840
+ this.onNodeUpdateEmitter.fire({ node, data });
1841
+ addedNodes?.push(node);
1842
+ if (register.onCreate) {
1843
+ const extendNodes = register.onCreate(node, data);
1844
+ if (extendNodes && addedNodes) {
1845
+ addedNodes.push(...extendNodes);
1846
+ }
1847
+ } else if (data.blocks && data.blocks.length > 0) {
1848
+ if (!data.blocks[0].type) {
1849
+ this.addInlineBlocks(node, data.blocks, addedNodes);
1850
+ } else {
1851
+ this.addBlocksAsChildren(node, data.blocks, addedNodes);
1852
+ }
1853
+ }
1854
+ if (isNew && !ignoreCreateEvent) {
1855
+ this.onNodeCreateEmitter.fire({
1856
+ node,
1857
+ data
1858
+ });
1859
+ }
1860
+ return node;
1861
+ }
1862
+ addBlocksAsChildren(parent, blocks, addedNodes) {
1863
+ for (const block of blocks) {
1864
+ this.addNode(
1865
+ {
1866
+ ...block,
1867
+ parent
1868
+ },
1869
+ addedNodes
1870
+ );
1871
+ }
1872
+ }
1873
+ /**
1874
+ * block 格式:
1875
+ * node: (最原始的 id)
1876
+ * blockIcon
1877
+ * inlineBlocks
1878
+ * block
1879
+ * blockOrderIcon
1880
+ * block
1881
+ * blockOrderIcon
1882
+ * @param node
1883
+ * @param blocks
1884
+ * @param addedNodes
1885
+ */
1886
+ addInlineBlocks(node, blocks, addedNodes = []) {
1887
+ const blockIconNode = this.addNode({
1888
+ id: `$blockIcon$${node.id}`,
1889
+ type: "blockIcon" /* BLOCK_ICON */,
1890
+ originParent: node,
1891
+ parent: node
1892
+ });
1893
+ addedNodes.push(blockIconNode);
1894
+ if (blocks.length > 0) {
1895
+ const inlineBlocksNode = this.addNode({
1896
+ id: `$inlineBlocks$${node.id}`,
1897
+ type: "inlineBlocks" /* INLINE_BLOCKS */,
1898
+ originParent: node,
1899
+ parent: node
1900
+ });
1901
+ addedNodes.push(inlineBlocksNode);
1902
+ blocks.forEach((blockData) => {
1903
+ this.addBlock(node, blockData, addedNodes);
1904
+ });
1905
+ }
1906
+ return addedNodes;
1907
+ }
1908
+ /**
1909
+ * 添加单个 block
1910
+ * @param target
1911
+ * @param blockData
1912
+ * @param addedNodes
1913
+ * @param parent 默认去找 $inlineBlocks$
1914
+ */
1915
+ addBlock(target, blockData, addedNodes, parent, index) {
1916
+ const node = typeof target === "string" ? this.getNode(target) : target;
1917
+ const { onBlockChildCreate } = node.getNodeRegistry();
1918
+ if (onBlockChildCreate) {
1919
+ return onBlockChildCreate(node, blockData, addedNodes);
1920
+ }
1921
+ parent = parent || this.getNode(`$inlineBlocks$${node.id}`);
1922
+ const block = this.addNode({
1923
+ ...(0, import_lodash2.omit)(blockData, "blocks"),
1924
+ type: blockData.type || "block" /* BLOCK */,
1925
+ originParent: node,
1926
+ parent,
1927
+ index
1928
+ });
1929
+ if (blockData.meta?.defaultCollapsed) {
1930
+ block.collapsed = true;
1931
+ }
1932
+ const blockOrderIcon = this.addNode({
1933
+ id: `$blockOrderIcon$${blockData.id}`,
1934
+ type: "blockOrderIcon" /* BLOCK_ORDER_ICON */,
1935
+ originParent: node,
1936
+ meta: blockData.meta,
1937
+ data: blockData.data,
1938
+ parent: block
1939
+ });
1940
+ addedNodes?.push(block, blockOrderIcon);
1941
+ if (blockData.blocks) {
1942
+ this.addBlocksAsChildren(block, blockData.blocks, addedNodes);
1943
+ }
1944
+ return block;
1945
+ }
1946
+ /**
1947
+ * 根据 id 获取节点
1948
+ * @param id
1949
+ */
1950
+ getNode(id) {
1951
+ if (!id) return void 0;
1952
+ return this.entityManager.getEntityById(id);
1953
+ }
1954
+ /**
1955
+ * 注册节点
1956
+ * @param registries
1957
+ */
1958
+ registerFlowNodes(...registries) {
1959
+ registries.forEach((newRegistry) => {
1960
+ if (!newRegistry) {
1961
+ throw new Error("[FlowDocument] registerFlowNodes parameters get undefined registry.");
1962
+ }
1963
+ const preRegistry = this.registers.get(newRegistry.type);
1964
+ this.registers.set(newRegistry.type, {
1965
+ ...preRegistry,
1966
+ ...newRegistry,
1967
+ meta: {
1968
+ ...preRegistry?.meta,
1969
+ ...newRegistry?.meta
1970
+ }
1971
+ });
1972
+ });
1973
+ }
1974
+ /**
1975
+ * 导出数据,可以重载
1976
+ */
1977
+ toJSON() {
1978
+ return {
1979
+ nodes: this.root.toJSON().blocks
1980
+ };
1981
+ }
1982
+ /**
1983
+ * @deprecated
1984
+ * use `getNodeRegistry` instead
1985
+ */
1986
+ getNodeRegister(type, originParent) {
1987
+ return this.getNodeRegistry(type, originParent);
1988
+ }
1989
+ getNodeRegistry(type, originParent) {
1990
+ const typeKey = `${type}_${originParent?.flowNodeType || ""}`;
1991
+ if (this.nodeRegistryCache.has(typeKey)) {
1992
+ return this.nodeRegistryCache.get(typeKey);
1993
+ }
1994
+ const customDefaultRegistry = this.options.getNodeDefaultRegistry?.(type);
1995
+ let register = this.registers.get(type) || { type };
1996
+ const extendRegisters = [];
1997
+ if (register.extend && this.registers.has(register.extend)) {
1998
+ register = FlowNodeRegistry.merge(
1999
+ this.getNodeRegistry(register.extend),
2000
+ register,
2001
+ register.type
2002
+ );
2003
+ }
2004
+ if (originParent) {
2005
+ const extendRegister = this.getNodeRegistry(
2006
+ originParent.flowNodeType
2007
+ ).extendChildRegistries?.find((r) => r.type === type);
2008
+ if (extendRegister) {
2009
+ if (extendRegister.extend && this.registers.has(extendRegister.extend)) {
2010
+ extendRegisters.push(this.registers.get(extendRegister.extend));
2011
+ }
2012
+ extendRegisters.push(extendRegister);
2013
+ }
2014
+ }
2015
+ register = FlowNodeRegistry.extend(register, extendRegisters);
2016
+ const defaultNodeMeta = DEFAULT_FLOW_NODE_META(type, this);
2017
+ defaultNodeMeta.spacing = this.options?.constants?.[ConstantKeys.NODE_SPACING] || defaultNodeMeta.spacing;
2018
+ const res = {
2019
+ ...customDefaultRegistry,
2020
+ ...register,
2021
+ meta: {
2022
+ ...defaultNodeMeta,
2023
+ ...customDefaultRegistry?.meta,
2024
+ ...register.meta
2025
+ }
2026
+ };
2027
+ this.nodeRegistryCache.set(typeKey, res);
2028
+ return res;
2029
+ }
2030
+ /**
2031
+ * 节点注入数据
2032
+ * @param nodeDatas
2033
+ */
2034
+ registerNodeDatas(...nodeDatas) {
2035
+ this.nodeDataRegistries.push(...nodeDatas);
2036
+ }
2037
+ /**
2038
+ * traverse all nodes, O(n)
2039
+ * R
2040
+ * |
2041
+ * +---1
2042
+ * | |
2043
+ * | +---1.1
2044
+ * | |
2045
+ * | +---1.2
2046
+ * | |
2047
+ * | +---1.3
2048
+ * | | |
2049
+ * | | +---1.3.1
2050
+ * | | |
2051
+ * | | +---1.3.2
2052
+ * | |
2053
+ * | +---1.4
2054
+ * |
2055
+ * +---2
2056
+ * |
2057
+ * +---2.1
2058
+ *
2059
+ * sort: [1, 1.1, 1.2, 1.3, 1.3.1, 1.3.2, 1.4, 2, 2.1]
2060
+ * @param fn
2061
+ * @param node
2062
+ * @param depth
2063
+ * @return isBreak
2064
+ */
2065
+ traverse(fn, node = this.root, depth = 0) {
2066
+ return this.originTree.traverse(fn, node, depth);
2067
+ }
2068
+ get size() {
2069
+ return this.getAllNodes().length;
2070
+ }
2071
+ hasNode(nodeId) {
2072
+ return !!this.entityManager.getEntityById(nodeId);
2073
+ }
2074
+ getAllNodes() {
2075
+ return this.entityManager.getEntities(FlowNodeEntity);
2076
+ }
2077
+ toString() {
2078
+ return this.originTree.toString();
2079
+ }
2080
+ /**
2081
+ * 返回需要渲染的数据
2082
+ */
2083
+ getRenderDatas(dataRegistry, containHiddenNodes = true) {
2084
+ const result = [];
2085
+ this.renderTree.traverse((node) => {
2086
+ if (!containHiddenNodes && node.hidden) return;
2087
+ result.push(node.getData(dataRegistry));
2088
+ });
2089
+ return result;
2090
+ }
2091
+ /**
2092
+ * 移动节点
2093
+ * @param param0
2094
+ * @returns
2095
+ */
2096
+ moveNodes({
2097
+ dropNodeId,
2098
+ sortNodeIds,
2099
+ inside = false
2100
+ }) {
2101
+ const dropEntity = this.getNode(dropNodeId);
2102
+ if (!dropEntity) {
2103
+ return;
2104
+ }
2105
+ const sortNodes = sortNodeIds.map((id) => this.getNode(id));
2106
+ this.entityManager.changeEntityLocked = true;
2107
+ for (const node of sortNodes.reverse()) {
2108
+ if (inside) {
2109
+ this.originTree.addChild(dropEntity, node, 0);
2110
+ } else {
2111
+ this.originTree.insertAfter(dropEntity, node);
2112
+ }
2113
+ }
2114
+ this.entityManager.changeEntityLocked = false;
2115
+ this.fireRender();
2116
+ }
2117
+ /**
2118
+ * 移动子节点
2119
+ * @param param0
2120
+ * @returns
2121
+ */
2122
+ moveChildNodes({
2123
+ toParentId,
2124
+ toIndex,
2125
+ nodeIds
2126
+ }) {
2127
+ if (nodeIds.length === 0) {
2128
+ return;
2129
+ }
2130
+ const toParent = this.getNode(toParentId);
2131
+ if (!toParent) {
2132
+ return;
2133
+ }
2134
+ this.entityManager.changeEntityLocked = true;
2135
+ this.originTree.moveChilds(
2136
+ toParent,
2137
+ nodeIds.map((nodeId) => this.getNode(nodeId)),
2138
+ toIndex
2139
+ );
2140
+ this.entityManager.changeEntityLocked = false;
2141
+ this.fireRender();
2142
+ }
2143
+ /**
2144
+ * 注册布局
2145
+ * @param layout
2146
+ */
2147
+ registerLayout(layout) {
2148
+ this.layouts.push(layout);
2149
+ }
2150
+ /**
2151
+ * 更新布局
2152
+ * @param layoutKey
2153
+ */
2154
+ setLayout(layoutKey) {
2155
+ if (this.currentLayoutKey === layoutKey) return;
2156
+ const layout = this.layouts.find((layout2) => layout2.name === layoutKey);
2157
+ if (!layout) return;
2158
+ this.currentLayoutKey = layoutKey;
2159
+ this.transformer.clear();
2160
+ layout.reload?.();
2161
+ this.fireRender();
2162
+ this.onLayoutChangeEmitter.fire(this.layout);
2163
+ }
2164
+ /**
2165
+ * 切换垂直或水平布局
2166
+ */
2167
+ toggleFixedLayout() {
2168
+ this.setLayout(
2169
+ this.layout.name === "horizontal-fixed-layout" /* HORIZONTAL_FIXED_LAYOUT */ ? "vertical-fixed-layout" /* VERTICAL_FIXED_LAYOUT */ : "horizontal-fixed-layout" /* HORIZONTAL_FIXED_LAYOUT */
2170
+ );
2171
+ }
2172
+ dispose() {
2173
+ this.registers.clear();
2174
+ this.nodeRegistryCache.clear();
2175
+ this.originTree.dispose();
2176
+ this.renderTree.dispose();
2177
+ this.onNodeUpdateEmitter.dispose();
2178
+ this.onNodeCreateEmitter.dispose();
2179
+ this.onNodeDisposeEmitter.dispose();
2180
+ this.onLayoutChangeEmitter.dispose();
2181
+ }
2182
+ };
2183
+ __decorateClass([
2184
+ (0, import_inversify2.inject)(import_core7.EntityManager)
2185
+ ], FlowDocument.prototype, "entityManager", 2);
2186
+ __decorateClass([
2187
+ (0, import_inversify2.inject)(FlowDocumentConfig)
2188
+ ], FlowDocument.prototype, "config", 2);
2189
+ __decorateClass([
2190
+ (0, import_inversify2.inject)(FlowDocumentOptions),
2191
+ (0, import_inversify2.optional)()
2192
+ ], FlowDocument.prototype, "options", 2);
2193
+ __decorateClass([
2194
+ (0, import_inversify2.multiInject)(FlowDocumentContribution),
2195
+ (0, import_inversify2.optional)()
2196
+ ], FlowDocument.prototype, "contributions", 2);
2197
+ __decorateClass([
2198
+ (0, import_inversify2.postConstruct)()
2199
+ ], FlowDocument.prototype, "init", 1);
2200
+ FlowDocument = __decorateClass([
2201
+ (0, import_inversify2.injectable)()
2202
+ ], FlowDocument);
2203
+
2204
+ // src/flow-document-container-module.ts
2205
+ var import_inversify8 = require("inversify");
2206
+
2207
+ // src/services/flow-drag-service.ts
2208
+ var import_inversify5 = require("inversify");
2209
+ var import_core10 = require("@flowgram.ai/core");
2210
+ var import_utils9 = require("@flowgram.ai/utils");
2211
+
2212
+ // src/services/flow-operation-base-service.ts
2213
+ var import_inversify3 = require("inversify");
2214
+ var import_utils8 = require("@flowgram.ai/utils");
2215
+ var import_core8 = require("@flowgram.ai/core");
2216
+ var FlowOperationBaseServiceImpl = class {
2217
+ constructor() {
2218
+ this.onNodeAddEmitter = new import_utils8.Emitter();
2219
+ this.onNodeAdd = this.onNodeAddEmitter.event;
2220
+ this.toDispose = new import_utils8.DisposableCollection();
2221
+ }
2222
+ init() {
2223
+ this.toDispose.push(this.onNodeAddEmitter);
2224
+ }
2225
+ addNode(nodeJSON, config = {}) {
2226
+ const { parent, index, hidden } = config;
2227
+ let parentEntity;
2228
+ if (parent) {
2229
+ parentEntity = this.toNodeEntity(parent);
2230
+ }
2231
+ let register;
2232
+ if (parentEntity) {
2233
+ register = parentEntity.getNodeRegistry();
2234
+ }
2235
+ const addJSON = {
2236
+ ...nodeJSON,
2237
+ type: nodeJSON.type || "block" /* BLOCK */
2238
+ };
2239
+ const addNodeData = {
2240
+ ...addJSON,
2241
+ parent: parentEntity,
2242
+ index,
2243
+ hidden
2244
+ };
2245
+ let added;
2246
+ if (parentEntity && register?.addChild) {
2247
+ added = register.addChild(parentEntity, addJSON, {
2248
+ index,
2249
+ hidden
2250
+ });
2251
+ } else {
2252
+ added = this.document.addNode(addNodeData);
2253
+ }
2254
+ this.onNodeAddEmitter.fire({
2255
+ node: added,
2256
+ data: addNodeData
2257
+ });
2258
+ return added;
2259
+ }
2260
+ addFromNode(fromNode, nodeJSON) {
2261
+ return this.document.addFromNode(fromNode, nodeJSON);
2262
+ }
2263
+ deleteNode(node) {
2264
+ this.document.removeNode(node);
2265
+ }
2266
+ deleteNodes(nodes) {
2267
+ (nodes || []).forEach((node) => {
2268
+ this.deleteNode(node);
2269
+ });
2270
+ }
2271
+ addBlock(target, blockJSON, config = {}) {
2272
+ const { parent, index } = config;
2273
+ return this.document.addBlock(target, blockJSON, void 0, parent, index);
2274
+ }
2275
+ moveNode(node, config = {}) {
2276
+ const { parent: newParent, index } = config;
2277
+ const entity = this.toNodeEntity(node);
2278
+ const parent = entity?.parent;
2279
+ if (!parent) {
2280
+ return;
2281
+ }
2282
+ const newParentEntity = newParent ? this.toNodeEntity(newParent) : parent;
2283
+ if (!newParentEntity) {
2284
+ console.warn("no new parent found", newParent);
2285
+ return;
2286
+ }
2287
+ let toIndex = typeof index === "undefined" ? parent.children.length : index;
2288
+ return this.doMoveNode(entity, newParentEntity, toIndex);
2289
+ }
2290
+ /**
2291
+ * 拖拽节点
2292
+ * @param param0
2293
+ * @returns
2294
+ */
2295
+ dragNodes({ dropNode, nodes }) {
2296
+ if (nodes.length === 0) {
2297
+ return;
2298
+ }
2299
+ const startNode = nodes[0];
2300
+ const fromParent = startNode.parent;
2301
+ const toParent = dropNode.parent;
2302
+ if (!fromParent || !toParent) {
2303
+ return;
2304
+ }
2305
+ const fromIndex = fromParent.children.findIndex((child) => child === startNode);
2306
+ const dropIndex = toParent.children.findIndex((child) => child === dropNode);
2307
+ let toIndex = dropIndex + 1;
2308
+ if (fromParent === toParent && fromIndex < dropIndex) {
2309
+ toIndex = toIndex - nodes.length;
2310
+ }
2311
+ const value = {
2312
+ nodeIds: nodes.map((node) => node.id),
2313
+ fromParentId: fromParent.id,
2314
+ toParentId: toParent.id,
2315
+ fromIndex,
2316
+ toIndex
2317
+ };
2318
+ return this.apply({
2319
+ type: "moveChildNodes" /* moveChildNodes */,
2320
+ value
2321
+ });
2322
+ }
2323
+ /**
2324
+ * 执行操作
2325
+ * @param operation 可序列化的操作
2326
+ * @returns 操作返回
2327
+ */
2328
+ apply(operation) {
2329
+ const document = this.document;
2330
+ switch (operation.type) {
2331
+ case "addFromNode" /* addFromNode */:
2332
+ return document.addFromNode(operation.value.fromId, operation.value.data);
2333
+ case "deleteFromNode" /* deleteFromNode */:
2334
+ return document.getNode(operation.value?.data?.id)?.dispose();
2335
+ case "addBlock" /* addBlock */: {
2336
+ let parent;
2337
+ if (operation.value.parentId) {
2338
+ parent = document.getNode(operation.value.parentId);
2339
+ }
2340
+ return document.addBlock(
2341
+ operation.value.targetId,
2342
+ operation.value.blockData,
2343
+ void 0,
2344
+ parent,
2345
+ operation.value.index
2346
+ );
2347
+ }
2348
+ case "deleteBlock" /* deleteBlock */: {
2349
+ const entity = document.getNode(operation.value?.blockData.id);
2350
+ return entity?.dispose();
2351
+ }
2352
+ case "createGroup" /* createGroup */: {
2353
+ const groupNode = document.addFromNode(operation.value.targetId, {
2354
+ id: operation.value.groupId,
2355
+ type: "group" /* GROUP */
2356
+ });
2357
+ document.moveNodes({
2358
+ dropNodeId: operation.value.groupId,
2359
+ sortNodeIds: operation.value.nodeIds,
2360
+ inside: true
2361
+ });
2362
+ return groupNode;
2363
+ }
2364
+ case "ungroup" /* ungroup */: {
2365
+ document.moveNodes({
2366
+ dropNodeId: operation.value.groupId,
2367
+ sortNodeIds: operation.value.nodeIds
2368
+ });
2369
+ return document.getNode(operation.value.groupId)?.dispose();
2370
+ }
2371
+ case "moveNodes" /* moveNodes */: {
2372
+ return document.moveNodes({
2373
+ dropNodeId: operation.value.toId,
2374
+ sortNodeIds: operation.value.nodeIds
2375
+ });
2376
+ }
2377
+ case "moveBlock" /* moveBlock */: {
2378
+ return document.moveChildNodes({
2379
+ ...operation.value,
2380
+ nodeIds: [operation.value.nodeId]
2381
+ });
2382
+ }
2383
+ case "addNodes" /* addNodes */: {
2384
+ let fromId = operation.value.fromId;
2385
+ (operation.value.nodes || []).forEach((node) => {
2386
+ const added = document.addFromNode(fromId, node);
2387
+ fromId = added.id;
2388
+ });
2389
+ break;
2390
+ }
2391
+ case "deleteNodes" /* deleteNodes */: {
2392
+ (operation.value.nodes || []).forEach((node) => {
2393
+ const entity = document.getNode(node.id);
2394
+ entity?.dispose();
2395
+ });
2396
+ break;
2397
+ }
2398
+ case "addChildNode" /* addChildNode */: {
2399
+ return document.addNode({
2400
+ ...operation.value.data,
2401
+ parent: operation.value.parentId ? document.getNode(operation.value.parentId) : void 0,
2402
+ originParent: operation.value.originParentId ? document.getNode(operation.value.originParentId) : void 0,
2403
+ index: operation.value.index,
2404
+ hidden: operation.value.hidden
2405
+ });
2406
+ }
2407
+ case "deleteChildNode" /* deleteChildNode */:
2408
+ return document.getNode(operation.value.data.id)?.dispose();
2409
+ case "moveChildNodes" /* moveChildNodes */:
2410
+ return document.moveChildNodes(operation.value);
2411
+ default:
2412
+ throw new Error(`unknown operation type`);
2413
+ }
2414
+ }
2415
+ /**
2416
+ * 事务执行
2417
+ * @param transaction
2418
+ */
2419
+ transact(transaction) {
2420
+ transaction();
2421
+ }
2422
+ dispose() {
2423
+ this.toDispose.dispose();
2424
+ }
2425
+ toId(node) {
2426
+ return typeof node === "string" ? node : node.id;
2427
+ }
2428
+ toNodeEntity(node) {
2429
+ return typeof node === "string" ? this.document.getNode(node) : node;
2430
+ }
2431
+ getNodeIndex(node) {
2432
+ const entity = this.toNodeEntity(node);
2433
+ const parent = entity?.parent;
2434
+ if (!parent) {
2435
+ return -1;
2436
+ }
2437
+ return parent.children.findIndex((child) => child === entity);
2438
+ }
2439
+ doMoveNode(node, newParent, index) {
2440
+ return this.document.moveChildNodes({
2441
+ nodeIds: [this.toId(node)],
2442
+ toParentId: this.toId(newParent),
2443
+ toIndex: index
2444
+ });
2445
+ }
2446
+ };
2447
+ __decorateClass([
2448
+ (0, import_inversify3.inject)(import_core8.EntityManager)
2449
+ ], FlowOperationBaseServiceImpl.prototype, "entityManager", 2);
2450
+ __decorateClass([
2451
+ (0, import_inversify3.inject)(FlowDocument)
2452
+ ], FlowOperationBaseServiceImpl.prototype, "document", 2);
2453
+ __decorateClass([
2454
+ (0, import_inversify3.postConstruct)()
2455
+ ], FlowOperationBaseServiceImpl.prototype, "init", 1);
2456
+ FlowOperationBaseServiceImpl = __decorateClass([
2457
+ (0, import_inversify3.injectable)()
2458
+ ], FlowOperationBaseServiceImpl);
2459
+
2460
+ // src/services/flow-group-service.ts
2461
+ var import_nanoid = require("nanoid");
2462
+ var import_inversify4 = require("inversify");
2463
+ var import_core9 = require("@flowgram.ai/core");
2464
+ var FlowGroupService = class {
2465
+ /** 创建分组节点 */
2466
+ createGroup(nodes) {
2467
+ if (!nodes || !Array.isArray(nodes) || nodes.length === 0) {
2468
+ return;
2469
+ }
2470
+ if (!FlowGroupController.validate(nodes)) {
2471
+ return;
2472
+ }
2473
+ const sortedNodes = nodes.sort((a, b) => a.index - b.index);
2474
+ const fromNode = sortedNodes[0];
2475
+ const groupId = `group_${(0, import_nanoid.nanoid)(5)}`;
2476
+ this.operationService.apply({
2477
+ type: "createGroup" /* createGroup */,
2478
+ value: {
2479
+ targetId: fromNode.id,
2480
+ groupId,
2481
+ nodeIds: nodes.map((node) => node.id)
2482
+ }
2483
+ });
2484
+ const groupNode = this.entityManager.getEntityById(groupId);
2485
+ if (!groupNode) {
2486
+ return;
2487
+ }
2488
+ const group = this.groupController(groupNode);
2489
+ if (!group) {
2490
+ return;
2491
+ }
2492
+ group.expand();
2493
+ return groupNode;
2494
+ }
2495
+ /** 删除分组 */
2496
+ deleteGroup(groupNode) {
2497
+ const json = groupNode.toJSON();
2498
+ if (!groupNode.pre || !json) {
2499
+ return;
2500
+ }
2501
+ this.operationService.apply({
2502
+ type: "deleteNodes" /* deleteNodes */,
2503
+ value: {
2504
+ fromId: groupNode.pre.id,
2505
+ nodes: [json]
2506
+ }
2507
+ });
2508
+ }
2509
+ /** 取消分组 */
2510
+ ungroup(groupNode) {
2511
+ const group = this.groupController(groupNode);
2512
+ if (!group) {
2513
+ return;
2514
+ }
2515
+ const nodes = group.nodes;
2516
+ if (!groupNode.pre) {
2517
+ return;
2518
+ }
2519
+ group.collapse();
2520
+ this.operationService.apply({
2521
+ type: "ungroup" /* ungroup */,
2522
+ value: {
2523
+ groupId: groupNode.id,
2524
+ targetId: groupNode.pre.id,
2525
+ nodeIds: nodes.map((node) => node.id)
2526
+ }
2527
+ });
2528
+ }
2529
+ /** 返回所有分组节点 */
2530
+ getAllGroups() {
2531
+ const allNodes = this.entityManager.getEntities(FlowNodeEntity);
2532
+ const groupNodes = allNodes.filter((node) => node.flowNodeType === "group" /* GROUP */);
2533
+ return groupNodes.map((node) => this.groupController(node)).filter(Boolean);
2534
+ }
2535
+ /** 获取分组控制器*/
2536
+ groupController(group) {
2537
+ return FlowGroupController.create(group);
2538
+ }
2539
+ static validate(nodes) {
2540
+ return FlowGroupController.validate(nodes);
2541
+ }
2542
+ };
2543
+ __decorateClass([
2544
+ (0, import_inversify4.inject)(import_core9.EntityManager)
2545
+ ], FlowGroupService.prototype, "entityManager", 2);
2546
+ __decorateClass([
2547
+ (0, import_inversify4.inject)(FlowOperationBaseService)
2548
+ ], FlowGroupService.prototype, "operationService", 2);
2549
+ FlowGroupService = __decorateClass([
2550
+ (0, import_inversify4.injectable)()
2551
+ ], FlowGroupService);
2552
+ var FlowGroupController = class _FlowGroupController {
2553
+ constructor(groupNode) {
2554
+ this.groupNode = groupNode;
2555
+ }
2556
+ get nodes() {
2557
+ return this.groupNode.collapsedChildren || [];
2558
+ }
2559
+ get collapsed() {
2560
+ const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2561
+ return groupTransformData.collapsed;
2562
+ }
2563
+ collapse() {
2564
+ this.collapsed = true;
2565
+ }
2566
+ expand() {
2567
+ this.collapsed = false;
2568
+ }
2569
+ /** 获取分组外围的最大边框 */
2570
+ get bounds() {
2571
+ const groupNodeBounds = this.groupNode.getData(FlowNodeTransformData).bounds;
2572
+ return groupNodeBounds;
2573
+ }
2574
+ /** 是否是开始节点 */
2575
+ isStartNode(node) {
2576
+ if (!node) {
2577
+ return false;
2578
+ }
2579
+ const nodes = this.nodes;
2580
+ if (!nodes[0]) {
2581
+ return false;
2582
+ }
2583
+ return node.id === nodes[0].id;
2584
+ }
2585
+ /** 是否是结束节点 */
2586
+ isEndNode(node) {
2587
+ if (!node) {
2588
+ return false;
2589
+ }
2590
+ const nodes = this.nodes;
2591
+ if (!nodes[nodes.length - 1]) {
2592
+ return false;
2593
+ }
2594
+ return node.id === nodes[nodes.length - 1].id;
2595
+ }
2596
+ set note(note) {
2597
+ this.groupNode.getNodeMeta().note = note;
2598
+ }
2599
+ get note() {
2600
+ return this.groupNode.getNodeMeta().note || "";
2601
+ }
2602
+ set noteHeight(height) {
2603
+ this.groupNode.getNodeMeta().noteHeight = height;
2604
+ }
2605
+ get noteHeight() {
2606
+ return this.groupNode.getNodeMeta().noteHeight || 0;
2607
+ }
2608
+ get positionConfig() {
2609
+ return this.groupNode.getNodeMeta().positionConfig;
2610
+ }
2611
+ set collapsed(collapsed) {
2612
+ const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2613
+ groupTransformData.collapsed = collapsed;
2614
+ groupTransformData.localDirty = true;
2615
+ if (groupTransformData.parent) groupTransformData.parent.localDirty = true;
2616
+ if (groupTransformData.parent?.firstChild)
2617
+ groupTransformData.parent.firstChild.localDirty = true;
2618
+ }
2619
+ set hovered(hovered) {
2620
+ const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2621
+ if (hovered) {
2622
+ groupRenderData.toggleMouseEnter();
2623
+ } else {
2624
+ groupRenderData.toggleMouseLeave();
2625
+ }
2626
+ if (groupRenderData.hovered === hovered) {
2627
+ return;
2628
+ }
2629
+ groupRenderData.hovered = hovered;
2630
+ }
2631
+ get hovered() {
2632
+ const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2633
+ return groupRenderData.hovered;
2634
+ }
2635
+ static create(groupNode) {
2636
+ if (!groupNode) {
2637
+ return;
2638
+ }
2639
+ if (!_FlowGroupController.isGroupNode(groupNode)) {
2640
+ return;
2641
+ }
2642
+ return new _FlowGroupController(groupNode);
2643
+ }
2644
+ /** 判断节点能否组成分组 */
2645
+ static validate(nodes) {
2646
+ if (!nodes || !Array.isArray(nodes) || nodes.length === 0) {
2647
+ return false;
2648
+ }
2649
+ const isGroupRelatedNode = nodes.some((node) => _FlowGroupController.isGroupNode(node));
2650
+ if (isGroupRelatedNode) return false;
2651
+ const hasGroup = nodes.some((node) => node && this.isNodeInGroup(node));
2652
+ if (hasGroup) return false;
2653
+ const parent = nodes[0].parent;
2654
+ const isSameParent = nodes.every((node) => node.parent === parent);
2655
+ if (!isSameParent) return false;
2656
+ const indexes = nodes.map((node) => node.index).sort((a, b) => a - b);
2657
+ const isIndexContinuous = indexes.every((index, i, arr) => {
2658
+ if (i === 0) {
2659
+ return true;
2660
+ }
2661
+ return index === arr[i - 1] + 1;
2662
+ });
2663
+ if (!isIndexContinuous) return false;
2664
+ const parents = this.findNodeParents(nodes[0]);
2665
+ const parentsInGroup = parents.some((parent2) => this.isNodeInGroup(parent2));
2666
+ if (parentsInGroup) return false;
2667
+ return true;
2668
+ }
2669
+ /** 获取节点分组控制 */
2670
+ static getNodeGroupController(node) {
2671
+ if (!node) {
2672
+ return;
2673
+ }
2674
+ if (!this.isNodeInGroup(node)) {
2675
+ return;
2676
+ }
2677
+ const groupNode = node?.parent;
2678
+ return _FlowGroupController.create(groupNode);
2679
+ }
2680
+ /** 向上递归查找分组递归控制 */
2681
+ static getNodeRecursionGroupController(node) {
2682
+ if (!node) {
2683
+ return;
2684
+ }
2685
+ const group = this.getNodeGroupController(node);
2686
+ if (group) {
2687
+ return group;
2688
+ }
2689
+ if (node.parent) {
2690
+ return this.getNodeRecursionGroupController(node.parent);
2691
+ }
2692
+ return;
2693
+ }
2694
+ /** 是否分组节点 */
2695
+ static isGroupNode(group) {
2696
+ return group.flowNodeType === "group" /* GROUP */;
2697
+ }
2698
+ /** 找到节点所有上级 */
2699
+ static findNodeParents(node) {
2700
+ const parents = [];
2701
+ let parent = node.parent;
2702
+ while (parent) {
2703
+ parents.push(parent);
2704
+ parent = parent.parent;
2705
+ }
2706
+ return parents;
2707
+ }
2708
+ /** 节点是否处于分组中 */
2709
+ static isNodeInGroup(node) {
2710
+ if (node?.parent?.flowNodeType === "group" /* GROUP */) {
2711
+ return true;
2712
+ }
2713
+ return false;
2714
+ }
2715
+ };
2716
+
2717
+ // src/services/flow-drag-service.ts
2718
+ var FlowDragService = class {
2719
+ constructor() {
2720
+ this.onDropEmitter = new import_utils9.Emitter();
2721
+ this.onDrop = this.onDropEmitter.event;
2722
+ }
2723
+ get renderState() {
2724
+ return this.document.renderState;
2725
+ }
2726
+ // 拖拽所有节点中的首个节点
2727
+ get dragStartNode() {
2728
+ return this.renderState.getDragStartEntity();
2729
+ }
2730
+ // 拖拽的所有节点
2731
+ get dragNodes() {
2732
+ return this.renderState.getDragEntities();
2733
+ }
2734
+ // 放置的区域
2735
+ get dropNodeId() {
2736
+ return this.renderState.getNodeDroppingId();
2737
+ }
2738
+ // 是否在拖拽分支
2739
+ get isDragBranch() {
2740
+ return this.dragStartNode?.isInlineBlock;
2741
+ }
2742
+ // 拖拽的所有节点及其自节点
2743
+ get nodeDragIdsWithChildren() {
2744
+ return this.renderState.config.nodeDragIdsWithChildren || [];
2745
+ }
2746
+ get dragging() {
2747
+ const renderData = this.dragStartNode?.getData(FlowNodeRenderData);
2748
+ return !!renderData?.dragging;
2749
+ }
2750
+ get labelSide() {
2751
+ return this.renderState.config.dragLabelSide;
2752
+ }
2753
+ /**
2754
+ * 放置到目标分支
2755
+ */
2756
+ dropBranch() {
2757
+ this.dropNode();
2758
+ }
2759
+ /**
2760
+ * 移动到目标节点
2761
+ */
2762
+ dropNode() {
2763
+ const dropEntity = this.document.getNode(this.dropNodeId);
2764
+ if (!dropEntity) {
2765
+ return;
2766
+ }
2767
+ const sortNodes = [];
2768
+ let curr = this.dragStartNode;
2769
+ while (curr && this.dragNodes.includes(curr)) {
2770
+ sortNodes.push(curr);
2771
+ curr = curr.next;
2772
+ }
2773
+ this.operationService.dragNodes({
2774
+ dropNode: dropEntity,
2775
+ nodes: sortNodes
2776
+ });
2777
+ if (sortNodes.length > 0) {
2778
+ this.onDropEmitter.fire({
2779
+ dropNode: dropEntity,
2780
+ dragNodes: sortNodes
2781
+ });
2782
+ }
2783
+ }
2784
+ /**
2785
+ * 拖拽是否可以释放在该节点后面
2786
+ */
2787
+ isDroppableNode(node) {
2788
+ if (!this.dragging || this.isDragBranch) {
2789
+ return false;
2790
+ }
2791
+ if (this.nodeDragIdsWithChildren.includes(node.id) || node.next && this.nodeDragIdsWithChildren.includes(node.next.id)) {
2792
+ return false;
2793
+ }
2794
+ if (node.isInlineBlocks || node.isInlineBlock) {
2795
+ return false;
2796
+ }
2797
+ const hasGroupNode = this.dragNodes.some((node2) => node2.flowNodeType === "group" /* GROUP */);
2798
+ if (hasGroupNode) {
2799
+ const group = FlowGroupController.getNodeRecursionGroupController(node);
2800
+ if (group) {
2801
+ return false;
2802
+ }
2803
+ }
2804
+ return true;
2805
+ }
2806
+ /**
2807
+ * 拖拽分支是否可以释放在该分支
2808
+ * @param node 拖拽的分支节点
2809
+ * @param side 分支的前面还是后面
2810
+ */
2811
+ isDroppableBranch(node, side = "normal_branch" /* NORMAL_BRANCH */) {
2812
+ if (this.isDragBranch) {
2813
+ if (
2814
+ // 拖拽到分支
2815
+ !node.isInlineBlock || // 只能在同一分支条件下
2816
+ node.parent !== this.dragStartNode.parent || // 自己不能拖拽给自己
2817
+ node === this.dragStartNode
2818
+ ) {
2819
+ return false;
2820
+ }
2821
+ if (side === "normal_branch" /* NORMAL_BRANCH */ && node.next !== this.dragStartNode) {
2822
+ return true;
2823
+ }
2824
+ if (side === "pre_branch" /* PRE_BRANCH */ && node.pre !== this.dragStartNode) {
2825
+ return true;
2826
+ }
2827
+ }
2828
+ return false;
2829
+ }
2830
+ };
2831
+ __decorateClass([
2832
+ (0, import_inversify5.inject)(FlowDocument)
2833
+ ], FlowDragService.prototype, "document", 2);
2834
+ __decorateClass([
2835
+ (0, import_inversify5.inject)(FlowOperationBaseService)
2836
+ ], FlowDragService.prototype, "operationService", 2);
2837
+ __decorateClass([
2838
+ (0, import_inversify5.inject)(import_core10.EntityManager)
2839
+ ], FlowDragService.prototype, "entityManager", 2);
2840
+ FlowDragService = __decorateClass([
2841
+ (0, import_inversify5.injectable)()
2842
+ ], FlowDragService);
2843
+
2844
+ // src/layout/vertical-fixed-layout.ts
2845
+ var import_inversify6 = require("inversify");
2846
+ var DEFAULT_SCROLL = -36;
2847
+ function isStructDataEqual(struct1, struct2) {
2848
+ return struct1.childrenLength === struct2.childrenLength && struct1.index === struct2.index;
2849
+ }
2850
+ var VerticalFixedLayout = class {
2851
+ constructor() {
2852
+ this.name = "vertical-fixed-layout" /* VERTICAL_FIXED_LAYOUT */;
2853
+ this.structDataMap = /* @__PURE__ */ new WeakMap();
2854
+ }
2855
+ get document() {
2856
+ return this.documentProvider();
2857
+ }
2858
+ reload() {
2859
+ this.structDataMap = /* @__PURE__ */ new WeakMap();
2860
+ }
2861
+ /**
2862
+ * 更新布局
2863
+ */
2864
+ update() {
2865
+ this.updateLocalTransform(this.document.root);
2866
+ }
2867
+ /**
2868
+ * 更新节点的偏移
2869
+ * @param node
2870
+ * @param forceChange
2871
+ */
2872
+ updateLocalTransform(node, forceChange = false) {
2873
+ const { children, parent, isInlineBlock } = node;
2874
+ const transform = node.getData(FlowNodeTransformData);
2875
+ const { getDelta, getOrigin } = node.getNodeRegistry();
2876
+ const lastStructData = this.structDataMap.get(node) || {
2877
+ childrenLength: 0,
2878
+ index: -1
2879
+ };
2880
+ node.clearMemoGlobal();
2881
+ let localDirty = transform.localDirty || forceChange;
2882
+ const newStructData = {
2883
+ index: node.index,
2884
+ childrenLength: node.children.length
2885
+ };
2886
+ if (!isStructDataEqual(lastStructData, newStructData)) {
2887
+ localDirty = true;
2888
+ this.structDataMap.set(node, newStructData);
2889
+ }
2890
+ let siblingDirty = false;
2891
+ if (children.length > 0) {
2892
+ for (const child of children) {
2893
+ const childDirty = this.updateLocalTransform(child, siblingDirty);
2894
+ if (childDirty) {
2895
+ siblingDirty = true;
2896
+ localDirty = true;
2897
+ }
2898
+ }
2899
+ }
2900
+ if (!localDirty) return false;
2901
+ node.clearMemoLocal();
2902
+ transform.transform.update({
2903
+ origin: getOrigin ? getOrigin(transform, this) : this.getDefaultNodeOrigin()
2904
+ });
2905
+ const preTransform = transform.pre;
2906
+ const delta = getDelta?.(transform, this) || { x: 0, y: 0 };
2907
+ const inlineSpacingPre = isInlineBlock && transform.parent?.inlineSpacingPre ? transform.parent?.inlineSpacingPre : 0;
2908
+ const fromParentDelta = parent?.getNodeRegistry().getChildDelta?.(transform, this) || {
2909
+ x: 0,
2910
+ y: 0
2911
+ };
2912
+ delta.x += fromParentDelta.x;
2913
+ delta.y += fromParentDelta.y;
2914
+ const position = { x: delta.x, y: delta.y };
2915
+ if (isInlineBlock) {
2916
+ position.y += inlineSpacingPre;
2917
+ } else {
2918
+ position.y += preTransform?.localBounds.bottom || 0;
2919
+ position.y += preTransform?.spacing || 0;
2920
+ }
2921
+ transform.transform.update({
2922
+ size: transform.data.size,
2923
+ position
2924
+ });
2925
+ this.onAfterUpdateLocalTransform(transform);
2926
+ transform.localDirty = false;
2927
+ return true;
2928
+ }
2929
+ onAfterUpdateLocalTransform(transform) {
2930
+ const { onAfterUpdateLocalTransform } = transform.entity.getNodeRegistry();
2931
+ onAfterUpdateLocalTransform?.(transform, this);
2932
+ this.contribs?.forEach((_contrib) => {
2933
+ _contrib?.onAfterUpdateLocalTransform?.(transform, this);
2934
+ });
2935
+ }
2936
+ getNodeTransform(node) {
2937
+ return node.getData(FlowNodeTransformData);
2938
+ }
2939
+ getPadding(node) {
2940
+ const { inlineSpacingPre, inlineSpacingAfter, padding } = node.getNodeMeta();
2941
+ const transform = this.getNodeTransform(node);
2942
+ if (padding) {
2943
+ return typeof padding === "function" ? padding(transform) : padding;
2944
+ }
2945
+ const paddingPre = typeof inlineSpacingPre === "function" ? inlineSpacingPre(transform) : inlineSpacingPre;
2946
+ const paddingAfter = typeof inlineSpacingAfter === "function" ? inlineSpacingAfter(transform) : inlineSpacingAfter;
2947
+ return {
2948
+ left: 0,
2949
+ top: paddingPre,
2950
+ right: 0,
2951
+ bottom: paddingAfter
2952
+ };
2953
+ }
2954
+ getInitScroll(contentSize) {
2955
+ return {
2956
+ scrollX: -contentSize.width / 2,
2957
+ scrollY: DEFAULT_SCROLL
2958
+ };
2959
+ }
2960
+ getDefaultInputPoint(node) {
2961
+ return this.getNodeTransform(node).bounds.topCenter;
2962
+ }
2963
+ getDefaultOutputPoint(node) {
2964
+ return this.getNodeTransform(node).bounds.bottomCenter;
2965
+ }
2966
+ getDefaultNodeOrigin() {
2967
+ return { x: 0.5, y: 0 };
2968
+ }
2969
+ };
2970
+ __decorateClass([
2971
+ (0, import_inversify6.inject)(FlowDocumentProvider)
2972
+ ], VerticalFixedLayout.prototype, "documentProvider", 2);
2973
+ __decorateClass([
2974
+ (0, import_inversify6.multiInject)(FlowLayoutContribution),
2975
+ (0, import_inversify6.optional)()
2976
+ ], VerticalFixedLayout.prototype, "contribs", 2);
2977
+ VerticalFixedLayout = __decorateClass([
2978
+ (0, import_inversify6.injectable)()
2979
+ ], VerticalFixedLayout);
2980
+
2981
+ // src/layout/horizontal-fixed-layout.ts
2982
+ var import_inversify7 = require("inversify");
2983
+ var DEFAULT_SCROLL2 = -36;
2984
+ function isStructDataEqual2(struct1, struct2) {
2985
+ return struct1.childrenLength === struct2.childrenLength && struct1.index === struct2.index;
2986
+ }
2987
+ var HorizontalFixedLayout = class {
2988
+ constructor() {
2989
+ this.name = "horizontal-fixed-layout" /* HORIZONTAL_FIXED_LAYOUT */;
2990
+ this.structDataMap = /* @__PURE__ */ new WeakMap();
2991
+ }
2992
+ get document() {
2993
+ return this.documentProvider();
2994
+ }
2995
+ reload() {
2996
+ this.structDataMap = /* @__PURE__ */ new WeakMap();
2997
+ }
2998
+ /**
2999
+ * 更新布局
3000
+ */
3001
+ update() {
3002
+ this.updateLocalTransform(this.document.root);
3003
+ }
3004
+ /**
3005
+ * 更新节点的偏移
3006
+ * @param node
3007
+ * @param forceChange
3008
+ */
3009
+ updateLocalTransform(node, forceChange = false) {
3010
+ const { children, parent, isInlineBlock } = node;
3011
+ const transform = node.getData(FlowNodeTransformData);
3012
+ const { getDelta, getOrigin } = node.getNodeRegistry();
3013
+ const lastStructData = this.structDataMap.get(node) || {
3014
+ childrenLength: 0,
3015
+ index: -1
3016
+ };
3017
+ node.clearMemoGlobal();
3018
+ let localDirty = transform.localDirty || forceChange;
3019
+ const newStructData = {
3020
+ index: node.index,
3021
+ childrenLength: node.children.length
3022
+ };
3023
+ if (!isStructDataEqual2(lastStructData, newStructData)) {
3024
+ localDirty = true;
3025
+ this.structDataMap.set(node, newStructData);
3026
+ }
3027
+ let siblingDirty = false;
3028
+ if (children.length > 0) {
3029
+ for (const child of children) {
3030
+ const childDirty = this.updateLocalTransform(child, siblingDirty);
3031
+ if (childDirty) {
3032
+ siblingDirty = true;
3033
+ localDirty = true;
3034
+ }
3035
+ }
3036
+ }
3037
+ if (!localDirty) return false;
3038
+ node.clearMemoLocal();
3039
+ transform.transform.update({
3040
+ origin: getOrigin ? getOrigin(transform, this) : this.getDefaultNodeOrigin()
3041
+ });
3042
+ const preTransform = transform.pre;
3043
+ const delta = getDelta?.(transform, this) || { x: 0, y: 0 };
3044
+ const inlineSpacingPre = isInlineBlock && transform.parent?.inlineSpacingPre ? transform.parent?.inlineSpacingPre : 0;
3045
+ const fromParentDelta = parent?.getNodeRegistry().getChildDelta?.(transform, this) || {
3046
+ x: 0,
3047
+ y: 0
3048
+ };
3049
+ delta.x += fromParentDelta.x;
3050
+ delta.y += fromParentDelta.y;
3051
+ const position = { x: delta.x, y: delta.y };
3052
+ if (isInlineBlock) {
3053
+ position.x += inlineSpacingPre;
3054
+ } else {
3055
+ position.x += preTransform?.localBounds.right || 0;
3056
+ position.x += preTransform?.spacing || 0;
3057
+ }
3058
+ transform.transform.update({
3059
+ size: transform.data.size,
3060
+ position
3061
+ });
3062
+ this.onAfterUpdateLocalTransform(transform);
3063
+ transform.localDirty = false;
3064
+ return true;
3065
+ }
3066
+ onAfterUpdateLocalTransform(transform) {
3067
+ const { onAfterUpdateLocalTransform } = transform.entity.getNodeRegistry();
3068
+ onAfterUpdateLocalTransform?.(transform, this);
3069
+ this.contribs?.forEach((_contrib) => {
3070
+ _contrib?.onAfterUpdateLocalTransform?.(transform, this);
3071
+ });
3072
+ }
3073
+ getNodeTransform(node) {
3074
+ return node.getData(FlowNodeTransformData);
3075
+ }
3076
+ getPadding(node) {
3077
+ const { inlineSpacingPre, inlineSpacingAfter, padding } = node.getNodeMeta();
3078
+ const transform = this.getNodeTransform(node);
3079
+ if (padding) {
3080
+ return typeof padding === "function" ? padding(transform) : padding;
3081
+ }
3082
+ const paddingPre = typeof inlineSpacingPre === "function" ? inlineSpacingPre(transform) : inlineSpacingPre;
3083
+ const paddingAfter = typeof inlineSpacingAfter === "function" ? inlineSpacingAfter(transform) : inlineSpacingAfter;
3084
+ return {
3085
+ left: paddingPre,
3086
+ top: 0,
3087
+ right: paddingAfter,
3088
+ bottom: 0
3089
+ };
3090
+ }
3091
+ getInitScroll(contentSize) {
3092
+ return {
3093
+ scrollX: DEFAULT_SCROLL2,
3094
+ scrollY: -contentSize.height / 2
3095
+ };
3096
+ }
3097
+ getDefaultInputPoint(node) {
3098
+ return this.getNodeTransform(node).bounds.leftCenter;
3099
+ }
3100
+ getDefaultOutputPoint(node) {
3101
+ return this.getNodeTransform(node).bounds.rightCenter;
3102
+ }
3103
+ getDefaultNodeOrigin() {
3104
+ return { x: 0, y: 0.5 };
3105
+ }
3106
+ };
3107
+ __decorateClass([
3108
+ (0, import_inversify7.inject)(FlowDocumentProvider)
3109
+ ], HorizontalFixedLayout.prototype, "documentProvider", 2);
3110
+ __decorateClass([
3111
+ (0, import_inversify7.multiInject)(FlowLayoutContribution),
3112
+ (0, import_inversify7.optional)()
3113
+ ], HorizontalFixedLayout.prototype, "contribs", 2);
3114
+ HorizontalFixedLayout = __decorateClass([
3115
+ (0, import_inversify7.injectable)()
3116
+ ], HorizontalFixedLayout);
3117
+
3118
+ // src/flow-document-container-module.ts
3119
+ var FlowDocumentContainerModule = new import_inversify8.ContainerModule((bind) => {
3120
+ bind(FlowDocument).toSelf().inSingletonScope();
3121
+ bind(FlowDocumentProvider).toDynamicValue((ctx) => () => ctx.container.get(FlowDocument)).inSingletonScope();
3122
+ bind(FlowDocumentConfig).toSelf().inSingletonScope();
3123
+ bind(VerticalFixedLayout).toSelf().inSingletonScope();
3124
+ bind(HorizontalFixedLayout).toSelf().inSingletonScope();
3125
+ bind(FlowDragService).toSelf().inSingletonScope();
3126
+ bind(FlowOperationBaseService).to(FlowOperationBaseServiceImpl).inSingletonScope();
3127
+ bind(FlowGroupService).toSelf().inSingletonScope();
3128
+ bind(FlowDocumentContribution).toDynamicValue((ctx) => ({
3129
+ registerDocument: (document) => {
3130
+ document.registerLayout(ctx.container.get(VerticalFixedLayout));
3131
+ document.registerLayout(ctx.container.get(HorizontalFixedLayout));
3132
+ }
3133
+ }));
3134
+ });
3135
+
3136
+ // src/utils/get-default-spacing.ts
3137
+ var getDefaultSpacing = (node, key, defaultSpacing) => {
3138
+ const flowDocumentOptions = node.getService(FlowDocumentOptions);
3139
+ const spacing = flowDocumentOptions?.constants?.[key] || defaultSpacing || DEFAULT_SPACING[key];
3140
+ return spacing;
3141
+ };
3142
+ // Annotate the CommonJS export names for ESM import in node:
3143
+ 0 && (module.exports = {
3144
+ ConstantKeys,
3145
+ DEFAULT_FLOW_NODE_META,
3146
+ DEFAULT_SIZE,
3147
+ DEFAULT_SPACING,
3148
+ DRAGGING_TYPE,
3149
+ DefaultSpacingKey,
3150
+ FLOW_DEFAULT_HIDDEN_TYPES,
3151
+ FlowDocument,
3152
+ FlowDocumentConfig,
3153
+ FlowDocumentConfigDefaultData,
3154
+ FlowDocumentConfigEnum,
3155
+ FlowDocumentContainerModule,
3156
+ FlowDocumentContribution,
3157
+ FlowDocumentOptions,
3158
+ FlowDocumentOptionsDefault,
3159
+ FlowDocumentProvider,
3160
+ FlowDocumentTransformerEntity,
3161
+ FlowDragService,
3162
+ FlowGroupController,
3163
+ FlowGroupService,
3164
+ FlowLayout,
3165
+ FlowLayoutContribution,
3166
+ FlowLayoutDefault,
3167
+ FlowNodeBaseType,
3168
+ FlowNodeEntity,
3169
+ FlowNodeRegistry,
3170
+ FlowNodeRenderData,
3171
+ FlowNodeSplitType,
3172
+ FlowNodeTransformData,
3173
+ FlowNodeTransitionData,
3174
+ FlowOperationBaseService,
3175
+ FlowOperationBaseServiceImpl,
3176
+ FlowRendererStateEntity,
3177
+ FlowTransitionLabelEnum,
3178
+ FlowTransitionLineEnum,
3179
+ FlowVirtualTree,
3180
+ LABEL_SIDE_TYPE,
3181
+ OperationType,
3182
+ drawLineToBottom,
3183
+ drawLineToNext,
3184
+ getDefaultSpacing
3185
+ });
3186
+ //# sourceMappingURL=index.js.map