@flowgram.ai/document 0.1.0-alpha.10

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