@flowgram.ai/document 0.1.0

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