@flowgram.ai/free-layout-core 0.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/esm/chunk-CGOMTQ3G.js +1 -0
  2. package/dist/esm/chunk-CGOMTQ3G.js.map +1 -0
  3. package/dist/esm/chunk-DDJTYHXN.js +1 -0
  4. package/dist/esm/chunk-DDJTYHXN.js.map +1 -0
  5. package/dist/esm/chunk-DE4324TR.js +7 -0
  6. package/dist/esm/chunk-DE4324TR.js.map +1 -0
  7. package/dist/esm/chunk-EUXUH3YW.js +15 -0
  8. package/dist/esm/chunk-EUXUH3YW.js.map +1 -0
  9. package/dist/esm/chunk-J5FVRRUV.js +16 -0
  10. package/dist/esm/chunk-J5FVRRUV.js.map +1 -0
  11. package/dist/esm/chunk-KNYZRMIO.js +1 -0
  12. package/dist/esm/chunk-KNYZRMIO.js.map +1 -0
  13. package/dist/esm/chunk-NU6G5HF4.js +1 -0
  14. package/dist/esm/chunk-NU6G5HF4.js.map +1 -0
  15. package/dist/esm/chunk-PT4ZVDZZ.js +20 -0
  16. package/dist/esm/chunk-PT4ZVDZZ.js.map +1 -0
  17. package/dist/esm/index.js +3256 -0
  18. package/dist/esm/index.js.map +1 -0
  19. package/dist/esm/typings/index.js +22 -0
  20. package/dist/esm/typings/index.js.map +1 -0
  21. package/dist/esm/typings/workflow-edge.js +2 -0
  22. package/dist/esm/typings/workflow-edge.js.map +1 -0
  23. package/dist/esm/typings/workflow-json.js +8 -0
  24. package/dist/esm/typings/workflow-json.js.map +1 -0
  25. package/dist/esm/typings/workflow-line.js +10 -0
  26. package/dist/esm/typings/workflow-line.js.map +1 -0
  27. package/dist/esm/typings/workflow-node.js +2 -0
  28. package/dist/esm/typings/workflow-node.js.map +1 -0
  29. package/dist/esm/typings/workflow-registry.js +2 -0
  30. package/dist/esm/typings/workflow-registry.js.map +1 -0
  31. package/dist/esm/typings/workflow-sub-canvas.js +2 -0
  32. package/dist/esm/typings/workflow-sub-canvas.js.map +1 -0
  33. package/dist/index.d.mts +360 -0
  34. package/dist/index.d.ts +360 -0
  35. package/dist/index.js +3307 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/typings/index.d.mts +17 -0
  38. package/dist/typings/index.d.ts +17 -0
  39. package/dist/typings/index.js +66 -0
  40. package/dist/typings/index.js.map +1 -0
  41. package/dist/typings/workflow-edge.d.mts +11 -0
  42. package/dist/typings/workflow-edge.d.ts +11 -0
  43. package/dist/typings/workflow-edge.js +19 -0
  44. package/dist/typings/workflow-edge.js.map +1 -0
  45. package/dist/typings/workflow-json.d.mts +8 -0
  46. package/dist/typings/workflow-json.d.ts +8 -0
  47. package/dist/typings/workflow-json.js +40 -0
  48. package/dist/typings/workflow-json.js.map +1 -0
  49. package/dist/typings/workflow-line.d.mts +8 -0
  50. package/dist/typings/workflow-line.d.ts +8 -0
  51. package/dist/typings/workflow-line.js +45 -0
  52. package/dist/typings/workflow-line.js.map +1 -0
  53. package/dist/typings/workflow-node.d.mts +8 -0
  54. package/dist/typings/workflow-node.d.ts +8 -0
  55. package/dist/typings/workflow-node.js +19 -0
  56. package/dist/typings/workflow-node.js.map +1 -0
  57. package/dist/typings/workflow-registry.d.mts +25 -0
  58. package/dist/typings/workflow-registry.d.ts +25 -0
  59. package/dist/typings/workflow-registry.js +19 -0
  60. package/dist/typings/workflow-registry.js.map +1 -0
  61. package/dist/typings/workflow-sub-canvas.d.mts +2 -0
  62. package/dist/typings/workflow-sub-canvas.d.ts +2 -0
  63. package/dist/typings/workflow-sub-canvas.js +19 -0
  64. package/dist/typings/workflow-sub-canvas.js.map +1 -0
  65. package/dist/workflow-line-entity-BJQBRDgJ.d.mts +747 -0
  66. package/dist/workflow-line-entity-CEitdjhk.d.ts +747 -0
  67. package/dist/workflow-sub-canvas-DOVla1mw.d.mts +15 -0
  68. package/dist/workflow-sub-canvas-DOVla1mw.d.ts +15 -0
  69. package/package.json +77 -0
package/dist/index.js ADDED
@@ -0,0 +1,3307 @@
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
+
28
+ // src/index.ts
29
+ var src_exports = {};
30
+ __export(src_exports, {
31
+ EditorCursorState: () => EditorCursorState,
32
+ InteractiveType: () => InteractiveType,
33
+ LINE_HOVER_DISTANCE: () => LINE_HOVER_DISTANCE,
34
+ LineColors: () => LineColors,
35
+ LineType: () => LineType,
36
+ POINT_RADIUS: () => POINT_RADIUS,
37
+ PORT_SIZE: () => PORT_SIZE,
38
+ URLParams: () => URLParams,
39
+ WORKFLOW_LINE_ENTITY: () => WORKFLOW_LINE_ENTITY,
40
+ WorkflowCommands: () => WorkflowCommands,
41
+ WorkflowContentChangeType: () => WorkflowContentChangeType,
42
+ WorkflowDocument: () => WorkflowDocument,
43
+ WorkflowDocumentContainerModule: () => WorkflowDocumentContainerModule,
44
+ WorkflowDocumentOptions: () => WorkflowDocumentOptions,
45
+ WorkflowDocumentOptionsDefault: () => WorkflowDocumentOptionsDefault,
46
+ WorkflowDocumentProvider: () => WorkflowDocumentProvider,
47
+ WorkflowDragService: () => WorkflowDragService,
48
+ WorkflowHoverService: () => WorkflowHoverService,
49
+ WorkflowLineEntity: () => WorkflowLineEntity,
50
+ WorkflowLineRenderData: () => WorkflowLineRenderData,
51
+ WorkflowLinesManager: () => WorkflowLinesManager,
52
+ WorkflowNodeEntity: () => WorkflowNodeEntity,
53
+ WorkflowNodeLinesData: () => WorkflowNodeLinesData,
54
+ WorkflowNodePortsData: () => WorkflowNodePortsData,
55
+ WorkflowPortEntity: () => WorkflowPortEntity,
56
+ WorkflowResetLayoutService: () => WorkflowResetLayoutService,
57
+ WorkflowSelectService: () => WorkflowSelectService,
58
+ WorkflowSimpleLineContribution: () => WorkflowSimpleLineContribution,
59
+ bindConfigEntity: () => import_core3.bindConfigEntity,
60
+ compose: () => import_utils.compose,
61
+ composeAsync: () => import_utils.composeAsync,
62
+ delay: () => import_utils4.delay,
63
+ domReactToBounds: () => domReactToBounds,
64
+ fitView: () => fitView,
65
+ getAntiOverlapPosition: () => getAntiOverlapPosition,
66
+ getPortEntityId: () => getPortEntityId,
67
+ nanoid: () => nanoid,
68
+ useConfigEntity: () => import_core24.useConfigEntity,
69
+ useCurrentDomNode: () => useCurrentDomNode,
70
+ useCurrentEntity: () => useCurrentEntity,
71
+ useEntities: () => import_core24.useEntities,
72
+ useEntityDataFromContext: () => import_core24.useEntityDataFromContext,
73
+ useEntityFromContext: () => import_core24.useEntityFromContext,
74
+ useListenEvents: () => import_core24.useListenEvents,
75
+ useNodeRender: () => useNodeRender,
76
+ usePlayground: () => import_core24.usePlayground,
77
+ usePlaygroundContainer: () => import_core24.usePlaygroundContainer,
78
+ usePlaygroundContext: () => import_core24.usePlaygroundContext,
79
+ usePlaygroundLatest: () => import_core24.usePlaygroundLatest,
80
+ usePlaygroundReadonlyState: () => usePlaygroundReadonlyState,
81
+ useRefresh: () => import_core24.useRefresh,
82
+ useService: () => import_core24.useService,
83
+ useWorkflowDocument: () => useWorkflowDocument
84
+ });
85
+ module.exports = __toCommonJS(src_exports);
86
+
87
+ // src/workflow-commands.ts
88
+ var WorkflowCommands = /* @__PURE__ */ ((WorkflowCommands2) => {
89
+ WorkflowCommands2["DELETE_NODES"] = "DELETE_NODES";
90
+ WorkflowCommands2["COPY_NODES"] = "COPY_NODES";
91
+ WorkflowCommands2["PASTE_NODES"] = "PASTE_NODES";
92
+ WorkflowCommands2["ZOOM_IN"] = "ZOOM_IN";
93
+ WorkflowCommands2["ZOOM_OUT"] = "ZOOM_OUT";
94
+ WorkflowCommands2["UNDO"] = "UNDO";
95
+ WorkflowCommands2["REDO"] = "REDO";
96
+ return WorkflowCommands2;
97
+ })(WorkflowCommands || {});
98
+
99
+ // src/hooks/index.ts
100
+ var import_core24 = require("@flowgram.ai/core");
101
+
102
+ // src/hooks/use-node-render.tsx
103
+ var import_react2 = require("react");
104
+ var import_reactive = require("@flowgram.ai/reactive");
105
+ var import_node = require("@flowgram.ai/node");
106
+ var import_document10 = require("@flowgram.ai/document");
107
+ var import_core20 = require("@flowgram.ai/core");
108
+
109
+ // src/service/workflow-select-service.ts
110
+ var import_inversify = require("inversify");
111
+ var import_core9 = require("@flowgram.ai/core");
112
+ var import_utils9 = require("@flowgram.ai/utils");
113
+
114
+ // src/utils/index.ts
115
+ var import_core3 = require("@flowgram.ai/core");
116
+ var import_utils4 = require("@flowgram.ai/utils");
117
+
118
+ // src/utils/nanoid.ts
119
+ var import_nanoid = require("nanoid");
120
+ function nanoid(n) {
121
+ return (0, import_nanoid.nanoid)(n);
122
+ }
123
+
124
+ // src/utils/compose.ts
125
+ var import_utils = require("@flowgram.ai/utils");
126
+
127
+ // src/utils/fit-view.ts
128
+ var import_core = require("@flowgram.ai/core");
129
+ var import_utils2 = require("@flowgram.ai/utils");
130
+ var fitView = (doc, playgroundConfig, easing = true) => {
131
+ const bounds = import_utils2.Rectangle.enlarge(
132
+ doc.getAllNodes().map((node) => node.getData(import_core.TransformData).bounds)
133
+ );
134
+ return playgroundConfig.fitView(bounds, easing, 30);
135
+ };
136
+
137
+ // src/utils/get-anti-overlap-position.ts
138
+ var import_core2 = require("@flowgram.ai/core");
139
+ function getAntiOverlapPosition(doc, position, containerNode) {
140
+ let { x, y } = position;
141
+ const nodes = containerNode ? containerNode.collapsedChildren : doc.getAllNodes();
142
+ const positions = nodes.map((n) => {
143
+ const transform = n.getData(import_core2.TransformData);
144
+ return { x: transform.position.x, y: transform.position.y };
145
+ }).sort((a, b) => a.y - b.y);
146
+ const minDistance = 3;
147
+ for (const pos of positions) {
148
+ const { x: posX, y: posY } = pos;
149
+ if (y - posY < -minDistance) {
150
+ break;
151
+ }
152
+ const deltaX = Math.abs(x - posX);
153
+ const deltaY = Math.abs(y - posY);
154
+ if (deltaX <= minDistance && deltaY <= minDistance) {
155
+ x += 30;
156
+ y += 30;
157
+ }
158
+ }
159
+ return { x, y };
160
+ }
161
+
162
+ // src/utils/statics.ts
163
+ var import_utils3 = require("@flowgram.ai/utils");
164
+ var getPortEntityId = (node, portType, portID = "") => `port_${portType}_${node.id}_${portID}`;
165
+ var WORKFLOW_LINE_ENTITY = "WorkflowLineEntity";
166
+ function domReactToBounds(react) {
167
+ return new import_utils3.Rectangle(react.x, react.y, react.width, react.height);
168
+ }
169
+
170
+ // src/entities/workflow-node-entity.ts
171
+ var import_document = require("@flowgram.ai/document");
172
+ var WorkflowNodeEntity = import_document.FlowNodeEntity;
173
+
174
+ // src/entities/workflow-line-entity.ts
175
+ var import_lodash_es2 = require("lodash-es");
176
+ var import_utils8 = require("@flowgram.ai/utils");
177
+ var import_core8 = require("@flowgram.ai/core");
178
+
179
+ // src/entity-datas/workflow-node-ports-data.ts
180
+ var import_lodash_es = require("lodash-es");
181
+ var import_document2 = require("@flowgram.ai/document");
182
+ var import_core5 = require("@flowgram.ai/core");
183
+
184
+ // src/entities/workflow-port-entity.ts
185
+ var import_utils5 = require("@flowgram.ai/utils");
186
+ var import_core4 = require("@flowgram.ai/core");
187
+ var PORT_SIZE = 24;
188
+ var WorkflowPortEntity = class extends import_core4.Entity {
189
+ // relativePosition
190
+ constructor(opts) {
191
+ super(opts);
192
+ this.portID = "";
193
+ this._disabled = false;
194
+ this._hasError = false;
195
+ this._onErrorChangedEmitter = new import_utils5.Emitter();
196
+ this.onErrorChanged = this._onErrorChangedEmitter.event;
197
+ this.portID = opts.portID || "";
198
+ this.portType = opts.type;
199
+ this._disabled = opts.disabled ?? false;
200
+ this.node = opts.node;
201
+ this.updateTargetElement(opts.targetElement);
202
+ this.toDispose.push(this.node.getData(import_core4.TransformData).onDataChange(() => this.fireChange()));
203
+ this.toDispose.push(this.node.onDispose(this.dispose.bind(this)));
204
+ }
205
+ static getPortEntityId(node, portType, portID = "") {
206
+ return getPortEntityId(node, portType, portID);
207
+ }
208
+ // 获取连线是否为错误态
209
+ get hasError() {
210
+ return this._hasError;
211
+ }
212
+ // 设置连线的错误态,外部应使用 validate 进行更新
213
+ set hasError(hasError) {
214
+ this._hasError = hasError;
215
+ this._onErrorChangedEmitter.fire();
216
+ }
217
+ validate() {
218
+ const anyLineHasError = this.allLines.some((line) => {
219
+ if (line.disposed || line.isHidden) {
220
+ return false;
221
+ }
222
+ line.validateSelf();
223
+ return line.hasError;
224
+ });
225
+ const isPortHasError = this.node.document.isErrorPort(this);
226
+ this.hasError = anyLineHasError || isPortHasError;
227
+ }
228
+ isErrorPort() {
229
+ return this.node.document.isErrorPort(this);
230
+ }
231
+ get point() {
232
+ const { targetElement } = this;
233
+ const { bounds } = this.node.getData(import_core4.TransformData);
234
+ if (targetElement) {
235
+ const pos = domReactToBounds(targetElement.getBoundingClientRect()).center;
236
+ return this.entityManager.getEntity(import_core4.PlaygroundConfigEntity).getPosFromMouseEvent({
237
+ clientX: pos.x,
238
+ clientY: pos.y
239
+ });
240
+ }
241
+ if (this.portType === "input") {
242
+ return bounds.leftCenter;
243
+ }
244
+ return bounds.rightCenter;
245
+ }
246
+ /**
247
+ * 点的区域
248
+ */
249
+ get bounds() {
250
+ const { point } = this;
251
+ const halfSize = PORT_SIZE / 2;
252
+ return new import_utils5.Rectangle(point.x - halfSize, point.y - halfSize, PORT_SIZE, PORT_SIZE);
253
+ }
254
+ isHovered(x, y) {
255
+ return this.bounds.contains(x, y);
256
+ }
257
+ /**
258
+ * 相对节点左上角的位置
259
+ */
260
+ get relativePosition() {
261
+ const { point } = this;
262
+ const { bounds } = this.node.getData(import_core4.TransformData);
263
+ return {
264
+ x: point.x - bounds.x,
265
+ y: point.y - bounds.y
266
+ };
267
+ }
268
+ updateTargetElement(el) {
269
+ if (el !== this.targetElement) {
270
+ this.targetElement = el;
271
+ this.fireChange();
272
+ }
273
+ }
274
+ /**
275
+ * 是否被禁用
276
+ */
277
+ get disabled() {
278
+ const document2 = this.node.document;
279
+ if (typeof document2.options.isDisabledPort === "function") {
280
+ return document2.options.isDisabledPort(this);
281
+ }
282
+ if (this._disabled) {
283
+ return true;
284
+ }
285
+ const meta = this.node.getNodeMeta();
286
+ if (this.portType === "input") {
287
+ return !!meta.inputDisable;
288
+ }
289
+ return !!meta.outputDisable;
290
+ }
291
+ /**
292
+ * 当前点位上连接的线条
293
+ */
294
+ get lines() {
295
+ return this.allLines.filter((line) => !line.isDrawing);
296
+ }
297
+ /**
298
+ * 当前点位上连接的线条(包含 isDrawing === true 的线条)
299
+ */
300
+ get allLines() {
301
+ const lines = [];
302
+ const allLines = this.entityManager.getEntities({
303
+ type: WORKFLOW_LINE_ENTITY
304
+ });
305
+ allLines.forEach((line) => {
306
+ if (line.toPort === this || line.fromPort === this) {
307
+ lines.push(line);
308
+ }
309
+ });
310
+ return lines;
311
+ }
312
+ dispose() {
313
+ this.lines.forEach((l) => l.dispose());
314
+ super.dispose();
315
+ }
316
+ };
317
+ WorkflowPortEntity.type = "WorkflowPortEntity";
318
+
319
+ // src/entity-datas/workflow-node-ports-data.ts
320
+ var WorkflowNodePortsData = class extends import_core5.EntityData {
321
+ constructor(entity) {
322
+ super(entity);
323
+ /** 静态的 ports 数据 */
324
+ this._staticPorts = [];
325
+ /** 存储 port 实体的 id,用于判断 port 是否存在 */
326
+ this._portIDSet = /* @__PURE__ */ new Set();
327
+ this.entity = entity;
328
+ const meta = entity.getNodeMeta();
329
+ const defaultPorts = meta.useDynamicPort ? [] : [{ type: "input" }, { type: "output" }];
330
+ this._staticPorts = meta.defaultPorts?.slice() || defaultPorts;
331
+ this.updatePorts(this._staticPorts);
332
+ if (meta.useDynamicPort) {
333
+ this.toDispose.push(
334
+ // 只需要监听节点的大小,因为算的是相对位置
335
+ entity.getData(import_core5.SizeData).onDataChange(() => {
336
+ if (entity.getData(import_core5.SizeData).width && entity.getData(import_core5.SizeData).height) {
337
+ this.updateDynamicPorts();
338
+ }
339
+ })
340
+ );
341
+ }
342
+ this.onDispose(() => {
343
+ this.allPorts.forEach((port) => port.dispose());
344
+ });
345
+ }
346
+ getDefaultData() {
347
+ return {};
348
+ }
349
+ /**
350
+ * 更新静态的 ports 数据
351
+ */
352
+ updateStaticPorts(ports) {
353
+ const meta = this.entity.getNodeMeta();
354
+ this._staticPorts = ports;
355
+ if (meta.useDynamicPort) {
356
+ this.updateDynamicPorts();
357
+ } else {
358
+ this.updatePorts(this._staticPorts);
359
+ }
360
+ }
361
+ /**
362
+ * 动态计算点位,通过 dom 的 data-port-key
363
+ */
364
+ updateDynamicPorts() {
365
+ const domNode = this.entity.getData(import_document2.FlowNodeRenderData).node;
366
+ const elements = domNode.querySelectorAll("[data-port-id]");
367
+ const staticPorts = this._staticPorts;
368
+ const dynamicPorts = [];
369
+ if (elements.length > 0) {
370
+ dynamicPorts.push(
371
+ ...Array.from(elements).map((element) => ({
372
+ portID: element.getAttribute("data-port-id"),
373
+ type: element.getAttribute("data-port-type"),
374
+ targetElement: element
375
+ }))
376
+ );
377
+ }
378
+ this.updatePorts(staticPorts.concat(dynamicPorts));
379
+ }
380
+ /**
381
+ * 根据 key 获取 port 实体
382
+ */
383
+ getPortEntityByKey(portType, portKey) {
384
+ const entity = this.getOrCreatePortEntity({
385
+ type: portType,
386
+ portID: portKey
387
+ });
388
+ return entity;
389
+ }
390
+ /**
391
+ * 更新 ports 数据
392
+ */
393
+ updatePorts(ports) {
394
+ if (!(0, import_lodash_es.isEqual)(this._prePorts, ports)) {
395
+ const portKeys = ports.map((port) => this.getPortId(port.type, port.portID));
396
+ this._portIDSet.forEach((portId) => {
397
+ if (!portKeys.includes(portId)) {
398
+ this.getPortEntity(portId)?.dispose();
399
+ }
400
+ });
401
+ ports.forEach((port) => this.updatePortEntity(port));
402
+ this._prePorts = ports;
403
+ this.fireChange();
404
+ }
405
+ this.allPorts.forEach((port) => {
406
+ port.allLines.forEach((line) => {
407
+ line.validate();
408
+ });
409
+ port.validate();
410
+ });
411
+ }
412
+ /**
413
+ * 获取所有 port entities
414
+ */
415
+ get allPorts() {
416
+ return Array.from(this._portIDSet).map((portId) => this.getPortEntity(portId)).filter(Boolean);
417
+ }
418
+ /**
419
+ * 获取输入点位
420
+ */
421
+ get inputPorts() {
422
+ return this.allPorts.filter((port) => port.portType === "input");
423
+ }
424
+ /**
425
+ * 获取输出点位
426
+ */
427
+ get outputPorts() {
428
+ return this.allPorts.filter((port) => port.portType === "output");
429
+ }
430
+ /**
431
+ * 获取输入点位置
432
+ */
433
+ get inputPoints() {
434
+ return this.inputPorts.map((port) => port.point);
435
+ }
436
+ /**
437
+ * 获取输出点位置
438
+ */
439
+ get outputPoints() {
440
+ return this.inputPorts.map((port) => port.point);
441
+ }
442
+ /**
443
+ * 根据 key 获取 输入点位置
444
+ */
445
+ getInputPoint(key) {
446
+ return this.getPortEntityByKey("input", key).point;
447
+ }
448
+ /**
449
+ * 根据 key 获取输出点位置
450
+ */
451
+ getOutputPoint(key) {
452
+ return this.getPortEntityByKey("output", key).point;
453
+ }
454
+ /**
455
+ * 获取 port 实体
456
+ */
457
+ getPortEntity(portId) {
458
+ if (!this._portIDSet.has(portId)) {
459
+ return void 0;
460
+ }
461
+ return this.entity.entityManager.getEntityById(portId);
462
+ }
463
+ /**
464
+ * 拼接 port 实体的 id
465
+ */
466
+ getPortId(portType, portKey = "") {
467
+ return getPortEntityId(this.entity, portType, portKey);
468
+ }
469
+ /**
470
+ * 创建 port 实体
471
+ */
472
+ createPortEntity(portInfo) {
473
+ const id = this.getPortId(portInfo.type, portInfo.portID);
474
+ let portEntity = this.entity.entityManager.getEntityById(id);
475
+ if (!portEntity) {
476
+ portEntity = this.entity.entityManager.createEntity(WorkflowPortEntity, {
477
+ id,
478
+ node: this.entity,
479
+ ...portInfo
480
+ });
481
+ }
482
+ portEntity.onDispose(() => {
483
+ this._portIDSet.delete(id);
484
+ });
485
+ this._portIDSet.add(id);
486
+ return portEntity;
487
+ }
488
+ /**
489
+ * 获取或创建 port 实体
490
+ */
491
+ getOrCreatePortEntity(portInfo) {
492
+ const id = this.getPortId(portInfo.type, portInfo.portID);
493
+ return this.getPortEntity(id) ?? this.createPortEntity(portInfo);
494
+ }
495
+ /**
496
+ * 更新 port 实体
497
+ */
498
+ updatePortEntity(portInfo) {
499
+ const portEntity = this.getOrCreatePortEntity(portInfo);
500
+ if (portInfo.targetElement) {
501
+ portEntity.updateTargetElement(portInfo.targetElement);
502
+ }
503
+ return portEntity;
504
+ }
505
+ };
506
+ WorkflowNodePortsData.type = "WorkflowNodePortsData";
507
+
508
+ // src/entity-datas/workflow-node-lines-data.ts
509
+ var import_utils6 = require("@flowgram.ai/utils");
510
+ var import_core6 = require("@flowgram.ai/core");
511
+ var _WorkflowNodeLinesData = class _WorkflowNodeLinesData extends import_core6.EntityData {
512
+ getDefaultData() {
513
+ return {
514
+ inputLines: [],
515
+ outputLines: []
516
+ };
517
+ }
518
+ constructor(entity) {
519
+ super(entity);
520
+ this.entity = entity;
521
+ this.toDispose.push(
522
+ import_utils6.Disposable.create(() => {
523
+ this.inputLines.slice().forEach((line) => line.dispose());
524
+ this.outputLines.slice().forEach((line) => line.dispose());
525
+ })
526
+ );
527
+ }
528
+ /**
529
+ * 输入线条
530
+ */
531
+ get inputLines() {
532
+ return this.data.inputLines;
533
+ }
534
+ /**
535
+ * 输出线条
536
+ */
537
+ get outputLines() {
538
+ return this.data.outputLines;
539
+ }
540
+ /**
541
+ * 输入节点
542
+ */
543
+ get inputNodes() {
544
+ return this.inputLines.map((l) => l.from).filter(Boolean);
545
+ }
546
+ /**
547
+ * 所有输入节点
548
+ */
549
+ get allInputNodes() {
550
+ const nodeSet = /* @__PURE__ */ new Set();
551
+ const handleNode = (node) => {
552
+ if (nodeSet.has(node)) {
553
+ return;
554
+ }
555
+ nodeSet.add(node);
556
+ const { inputNodes } = node.getData(_WorkflowNodeLinesData);
557
+ if (!inputNodes || !inputNodes.length) {
558
+ return;
559
+ }
560
+ inputNodes.forEach((inputNode) => {
561
+ if (inputNode?.parent === node || node?.parent === inputNode) {
562
+ return;
563
+ }
564
+ handleNode(inputNode);
565
+ });
566
+ };
567
+ handleNode(this.entity);
568
+ nodeSet.delete(this.entity);
569
+ return Array.from(nodeSet);
570
+ }
571
+ /**
572
+ * 输出节点
573
+ */
574
+ get outputNodes() {
575
+ return this.outputLines.map((l) => l.to).filter(Boolean);
576
+ }
577
+ /**
578
+ * 输入输出节点
579
+ */
580
+ get allOutputNodes() {
581
+ const nodeSet = /* @__PURE__ */ new Set();
582
+ const handleNode = (node) => {
583
+ if (nodeSet.has(node)) {
584
+ return;
585
+ }
586
+ nodeSet.add(node);
587
+ const { outputNodes } = node.getData(_WorkflowNodeLinesData);
588
+ if (!outputNodes || !outputNodes.length) {
589
+ return;
590
+ }
591
+ outputNodes.forEach((outputNode) => {
592
+ if (outputNode?.parent === node || node?.parent === outputNode) {
593
+ return;
594
+ }
595
+ handleNode(outputNode);
596
+ });
597
+ };
598
+ handleNode(this.entity);
599
+ nodeSet.delete(this.entity);
600
+ return Array.from(nodeSet);
601
+ }
602
+ addLine(line) {
603
+ if (line.from === this.entity) {
604
+ this.outputLines.push(line);
605
+ } else {
606
+ this.inputLines.push(line);
607
+ }
608
+ this.fireChange();
609
+ }
610
+ removeLine(line) {
611
+ const { inputLines, outputLines } = this;
612
+ const inputIndex = inputLines.indexOf(line);
613
+ const outputIndex = outputLines.indexOf(line);
614
+ if (inputIndex !== -1) {
615
+ inputLines.splice(inputIndex, 1);
616
+ this.fireChange();
617
+ }
618
+ if (outputIndex !== -1) {
619
+ outputLines.splice(outputIndex, 1);
620
+ this.fireChange();
621
+ }
622
+ }
623
+ };
624
+ _WorkflowNodeLinesData.type = "WorkflowNodeLinesData";
625
+ var WorkflowNodeLinesData = _WorkflowNodeLinesData;
626
+
627
+ // src/entity-datas/workflow-line-render-data.ts
628
+ var import_utils7 = require("@flowgram.ai/utils");
629
+ var import_core7 = require("@flowgram.ai/core");
630
+ var WorkflowLineRenderData = class extends import_core7.EntityData {
631
+ constructor(entity) {
632
+ super(entity);
633
+ this.syncContributions();
634
+ }
635
+ getDefaultData() {
636
+ return {
637
+ version: "",
638
+ contributions: /* @__PURE__ */ new Map(),
639
+ position: {
640
+ from: { x: 0, y: 0 },
641
+ to: { x: 0, y: 0 }
642
+ }
643
+ };
644
+ }
645
+ get renderVersion() {
646
+ return this.data.version;
647
+ }
648
+ get position() {
649
+ return this.data.position;
650
+ }
651
+ get path() {
652
+ return this.currentLine?.path ?? "";
653
+ }
654
+ calcDistance(pos) {
655
+ return this.currentLine?.calcDistance(pos) ?? Number.MAX_SAFE_INTEGER;
656
+ }
657
+ get bounds() {
658
+ return this.currentLine?.bounds ?? new import_utils7.Rectangle();
659
+ }
660
+ /**
661
+ * 更新数据
662
+ * WARNING: 这个方法,必须在 requestAnimationFrame / useLayoutEffect 中调用,否则会引起浏览器强制重排
663
+ */
664
+ update() {
665
+ this.syncContributions();
666
+ const oldVersion = this.data.version;
667
+ this.updatePosition();
668
+ const newVersion = this.data.version;
669
+ if (oldVersion === newVersion) {
670
+ return;
671
+ }
672
+ this.data.version = newVersion;
673
+ this.currentLine?.update({
674
+ fromPos: this.data.position.from,
675
+ toPos: this.data.position.to
676
+ });
677
+ }
678
+ get lineType() {
679
+ return this.entity.renderType ?? this.entity.linesManager.lineType;
680
+ }
681
+ /**
682
+ * 更新版本
683
+ * WARNING: 这个方法,必须在 requestAnimationFrame / useLayoutEffect 中调用,否则会引起浏览器强制重排
684
+ */
685
+ updatePosition() {
686
+ this.data.position.from = this.entity.from.getData(WorkflowNodePortsData).getOutputPoint(this.entity.info.fromPort);
687
+ this.data.position.to = this.entity.info.drawingTo ?? this.entity.to?.getData(WorkflowNodePortsData)?.getInputPoint(this.entity.info.toPort) ?? {
688
+ x: this.data.position.from.x,
689
+ y: this.data.position.from.y
690
+ };
691
+ this.data.version = [
692
+ this.lineType,
693
+ this.data.position.from.x,
694
+ this.data.position.from.y,
695
+ this.data.position.to.x,
696
+ this.data.position.to.y
697
+ ].join("-");
698
+ }
699
+ get currentLine() {
700
+ return this.data.contributions.get(this.lineType);
701
+ }
702
+ syncContributions() {
703
+ if (this.entity.linesManager.contributionFactories.length === this.data.contributions.size) {
704
+ return;
705
+ }
706
+ this.entity.linesManager.contributionFactories.forEach((factory) => {
707
+ this.registerContribution(factory);
708
+ });
709
+ }
710
+ registerContribution(contributionFactory) {
711
+ if (this.data.contributions.has(contributionFactory.type)) {
712
+ return;
713
+ }
714
+ const contribution = new contributionFactory(this.entity);
715
+ this.data.contributions.set(contributionFactory.type, contribution);
716
+ }
717
+ };
718
+ WorkflowLineRenderData.type = "WorkflowLineRenderData";
719
+
720
+ // src/entities/workflow-line-entity.ts
721
+ var LINE_HOVER_DISTANCE = 8;
722
+ var POINT_RADIUS = 10;
723
+ var _WorkflowLineEntity = class _WorkflowLineEntity extends import_core8.Entity {
724
+ constructor(opts) {
725
+ super(opts);
726
+ this._processing = false;
727
+ this._hasError = false;
728
+ /**
729
+ * 线条数据
730
+ */
731
+ this.info = {
732
+ from: ""
733
+ };
734
+ this.document = opts.document;
735
+ this.linesManager = opts.linesManager;
736
+ this.initInfo({
737
+ from: opts.from,
738
+ to: opts.to,
739
+ drawingTo: opts.drawingTo,
740
+ fromPort: opts.fromPort,
741
+ toPort: opts.toPort
742
+ });
743
+ if (opts.drawingTo) {
744
+ this.isDrawing = true;
745
+ }
746
+ }
747
+ /**
748
+ * 转成线条 id
749
+ * @param info
750
+ */
751
+ static portInfoToLineId(info) {
752
+ const { from, to, fromPort, toPort } = info;
753
+ return `${from}_${fromPort || ""}-${to || ""}_${toPort || ""}`;
754
+ }
755
+ /**
756
+ * 获取线条的前置节点
757
+ */
758
+ get from() {
759
+ return this._from;
760
+ }
761
+ /**
762
+ * 获取线条的后置节点
763
+ */
764
+ get to() {
765
+ return this._to;
766
+ }
767
+ get isHidden() {
768
+ return this.highlightColor === this.linesManager.lineColor.hidden;
769
+ }
770
+ get inContainer() {
771
+ const nodeInContainer = (node) => !!node?.parent && node.parent.flowNodeType !== "root";
772
+ return nodeInContainer(this.from) || nodeInContainer(this.to);
773
+ }
774
+ /**
775
+ * 获取是否 testrun processing
776
+ */
777
+ get processing() {
778
+ return this._processing;
779
+ }
780
+ /**
781
+ * 设置 testrun processing 状态
782
+ */
783
+ set processing(status) {
784
+ if (this._processing !== status) {
785
+ this._processing = status;
786
+ this.fireChange();
787
+ }
788
+ }
789
+ // 获取连线是否为错误态
790
+ get hasError() {
791
+ return this._hasError;
792
+ }
793
+ // 设置连线的错误态
794
+ set hasError(hasError) {
795
+ if (this._hasError !== hasError) {
796
+ this._hasError = hasError;
797
+ this.fireChange();
798
+ }
799
+ if (this._node) {
800
+ this._node.dataset.hasError = this.hasError ? "true" : "false";
801
+ }
802
+ }
803
+ /**
804
+ * 设置线条的后置节点
805
+ */
806
+ setToPort(toPort) {
807
+ if (!this.isDrawing) {
808
+ throw new Error("[setToPort] only support drawing line.");
809
+ }
810
+ if (this.toPort === toPort) {
811
+ return;
812
+ }
813
+ if (toPort && toPort.portType === "input" && this.linesManager.canAddLine(this.fromPort, toPort, true)) {
814
+ const { node, portID } = toPort;
815
+ this._to = node;
816
+ this.info.drawingTo = void 0;
817
+ this.info.isDefaultLine = false;
818
+ this.info.to = node.id;
819
+ this.info.toPort = portID;
820
+ } else {
821
+ this._to = void 0;
822
+ this.info.to = void 0;
823
+ this.info.toPort = "";
824
+ }
825
+ this.fireChange();
826
+ }
827
+ /**
828
+ * 设置线条画线时的目标位置
829
+ */
830
+ set drawingTo(pos) {
831
+ const oldDrawingTo = this.info.drawingTo;
832
+ if (!pos) {
833
+ this.info.drawingTo = void 0;
834
+ this.fireChange();
835
+ return;
836
+ }
837
+ if (!oldDrawingTo || pos.x !== oldDrawingTo.x || pos.y !== oldDrawingTo.y) {
838
+ this.info.to = void 0;
839
+ this.info.isDefaultLine = false;
840
+ this.info.drawingTo = pos;
841
+ this.fireChange();
842
+ }
843
+ }
844
+ /**
845
+ * 获取线条正在画线的位置
846
+ */
847
+ get drawingTo() {
848
+ return this.info.drawingTo;
849
+ }
850
+ get highlightColor() {
851
+ return this.info.highlightColor || "";
852
+ }
853
+ set highlightColor(color) {
854
+ if (this.info.highlightColor !== color) {
855
+ this.info.highlightColor = color;
856
+ this.fireChange();
857
+ }
858
+ }
859
+ /**
860
+ * 获取线条的边框位置大小
861
+ */
862
+ get bounds() {
863
+ return this.getData(WorkflowLineRenderData).bounds;
864
+ }
865
+ /**
866
+ * 获取点和线最接近的距离
867
+ */
868
+ getHoverDist(pos) {
869
+ return this.getData(WorkflowLineRenderData).calcDistance(pos);
870
+ }
871
+ get fromPort() {
872
+ return this.from.getData(WorkflowNodePortsData).getPortEntityByKey("output", this.info.fromPort);
873
+ }
874
+ get toPort() {
875
+ if (!this.to) {
876
+ return void 0;
877
+ }
878
+ return this.to.getData(WorkflowNodePortsData).getPortEntityByKey("input", this.info.toPort);
879
+ }
880
+ /**
881
+ * 获取线条真实的输入输出节点坐标
882
+ */
883
+ get position() {
884
+ return this.getData(WorkflowLineRenderData).position;
885
+ }
886
+ /** 是否反转箭头 */
887
+ get reverse() {
888
+ return this.linesManager.isReverseLine(this);
889
+ }
890
+ /** 是否隐藏箭头 */
891
+ get hideArrow() {
892
+ return this.linesManager.isHideArrowLine(this);
893
+ }
894
+ /** 是否流动 */
895
+ get flowing() {
896
+ return this.linesManager.isFlowingLine(this);
897
+ }
898
+ /** 是否禁用 */
899
+ get disabled() {
900
+ return this.linesManager.isDisabledLine(this);
901
+ }
902
+ /** 是否竖向 */
903
+ get vertical() {
904
+ return this.linesManager.isVerticalLine(this);
905
+ }
906
+ /** 获取线条渲染器类型 */
907
+ get renderType() {
908
+ return this.linesManager.setLineRenderType(this);
909
+ }
910
+ /** 获取线条样式 */
911
+ get className() {
912
+ return this.linesManager.setLineClassName(this) ?? "";
913
+ }
914
+ get color() {
915
+ return this.linesManager.getLineColor(this);
916
+ }
917
+ /**
918
+ * 初始化线条
919
+ * @param info 线条信息
920
+ */
921
+ initInfo(info) {
922
+ if (!(0, import_lodash_es2.isEqual)(info, this.info)) {
923
+ this.info = info;
924
+ this._from = this.document.getNode(info.from);
925
+ this._to = info.to ? this.document.getNode(info.to) : void 0;
926
+ this.fireChange();
927
+ }
928
+ }
929
+ // 校验连线是否为错误态
930
+ validate() {
931
+ const { fromPort, toPort } = this;
932
+ this.validateSelf();
933
+ fromPort?.validate();
934
+ toPort?.validate();
935
+ }
936
+ validateSelf() {
937
+ const { fromPort, toPort } = this;
938
+ if (fromPort) {
939
+ this.hasError = this.linesManager.isErrorLine(fromPort, toPort);
940
+ }
941
+ }
942
+ is(line) {
943
+ if (line instanceof _WorkflowLineEntity) {
944
+ return this === line;
945
+ }
946
+ return _WorkflowLineEntity.portInfoToLineId(line) === this.id;
947
+ }
948
+ canRemove(newLineInfo) {
949
+ return this.linesManager.canRemove(this, newLineInfo);
950
+ }
951
+ get node() {
952
+ if (this._node) return this._node;
953
+ this._node = import_utils8.domUtils.createDivWithClass("gedit-flow-activity-line");
954
+ this._node.dataset.testid = "sdk.workflow.canvas.line";
955
+ this._node.dataset.lineId = this.id;
956
+ this._node.dataset.fromNodeId = this.from.id;
957
+ this._node.dataset.fromPortId = this.fromPort?.id ?? "";
958
+ this._node.dataset.toNodeId = this.to?.id ?? "";
959
+ this._node.dataset.toPortId = this.toPort?.id ?? "";
960
+ this._node.dataset.hasError = this.hasError ? "true" : "false";
961
+ return this._node;
962
+ }
963
+ toJSON() {
964
+ const json = {
965
+ sourceNodeID: this.info.from,
966
+ targetNodeID: this.info.to,
967
+ sourcePortID: this.info.fromPort,
968
+ targetPortID: this.info.toPort
969
+ };
970
+ if (!json.sourcePortID) {
971
+ delete json.sourcePortID;
972
+ }
973
+ if (!json.targetPortID) {
974
+ delete json.targetPortID;
975
+ }
976
+ return json;
977
+ }
978
+ /** 触发线条渲染 */
979
+ fireRender() {
980
+ this.fireChange();
981
+ }
982
+ };
983
+ _WorkflowLineEntity.type = WORKFLOW_LINE_ENTITY;
984
+ var WorkflowLineEntity = _WorkflowLineEntity;
985
+
986
+ // src/service/workflow-select-service.ts
987
+ var WorkflowSelectService = class {
988
+ get onSelectionChanged() {
989
+ return this.selectionService.onSelectionChanged;
990
+ }
991
+ get selection() {
992
+ return this.selectionService.selection;
993
+ }
994
+ set selection(entities) {
995
+ this.selectionService.selection = entities;
996
+ }
997
+ /**
998
+ * 当前激活的节点只能有一个
999
+ */
1000
+ get activatedNode() {
1001
+ const { selectedNodes } = this;
1002
+ if (selectedNodes.length !== 1) {
1003
+ return void 0;
1004
+ }
1005
+ return selectedNodes[0];
1006
+ }
1007
+ isSelected(id) {
1008
+ return this.selectionService.selection.some((s) => s.id === id);
1009
+ }
1010
+ isActivated(id) {
1011
+ return this.activatedNode?.id === id;
1012
+ }
1013
+ /**
1014
+ * 选中的节点
1015
+ */
1016
+ get selectedNodes() {
1017
+ return this.selectionService.selection.filter(
1018
+ (n) => n instanceof WorkflowNodeEntity
1019
+ );
1020
+ }
1021
+ /**
1022
+ * 选中
1023
+ * @param node
1024
+ */
1025
+ selectNode(node) {
1026
+ this.selectionService.selection = [node];
1027
+ }
1028
+ toggleSelect(node) {
1029
+ if (this.selectionService.selection.includes(node)) {
1030
+ this.selectionService.selection = this.selectionService.selection.filter((n) => n !== node);
1031
+ } else {
1032
+ this.selectionService.selection = this.selectionService.selection.concat(node);
1033
+ }
1034
+ }
1035
+ select(node) {
1036
+ this.selectionService.selection = [node];
1037
+ }
1038
+ clear() {
1039
+ this.selectionService.selection = [];
1040
+ }
1041
+ /**
1042
+ * 选中并滚动到节点
1043
+ * @param node
1044
+ */
1045
+ async selectNodeAndScrollToView(node, fitView2) {
1046
+ this.selectNodeAndFocus(node);
1047
+ const DELAY_TIME = 30;
1048
+ await (0, import_utils4.delay)(DELAY_TIME);
1049
+ const scrollConfig = {
1050
+ entities: [node]
1051
+ };
1052
+ if (fitView2) {
1053
+ const bounds = import_utils9.Rectangle.enlarge([node.getData(import_core9.TransformData).bounds]).pad(
1054
+ 30,
1055
+ 30
1056
+ );
1057
+ const viewport = this.playground.config.getViewport(false);
1058
+ const zoom = import_utils9.SizeSchema.fixSize(bounds, viewport);
1059
+ scrollConfig.zoom = zoom;
1060
+ scrollConfig.scrollToCenter = true;
1061
+ scrollConfig.easing = true;
1062
+ }
1063
+ return this.playground.config.scrollToView(scrollConfig);
1064
+ }
1065
+ selectNodeAndFocus(node) {
1066
+ this.select(node);
1067
+ this.playground.node.focus();
1068
+ }
1069
+ };
1070
+ __decorateClass([
1071
+ (0, import_inversify.inject)(import_core9.SelectionService)
1072
+ ], WorkflowSelectService.prototype, "selectionService", 2);
1073
+ __decorateClass([
1074
+ (0, import_inversify.inject)(import_core9.Playground)
1075
+ ], WorkflowSelectService.prototype, "playground", 2);
1076
+ WorkflowSelectService = __decorateClass([
1077
+ (0, import_inversify.injectable)()
1078
+ ], WorkflowSelectService);
1079
+
1080
+ // src/service/workflow-hover-service.ts
1081
+ var import_inversify2 = require("inversify");
1082
+ var import_core10 = require("@flowgram.ai/core");
1083
+ var import_utils11 = require("@flowgram.ai/utils");
1084
+ var WorkflowHoverService = class {
1085
+ constructor() {
1086
+ this.onHoveredChangeEmitter = new import_utils11.Emitter();
1087
+ this.onHoveredChange = this.onHoveredChangeEmitter.event;
1088
+ // 当前鼠标 hover 位置
1089
+ this.hoveredPos = { x: 0, y: 0 };
1090
+ /**
1091
+ * 当前 hovered 的 节点或者线条或者点
1092
+ * 1: nodeId / lineId (节点 / 线条)
1093
+ * 2: nodeId:portKey (节点连接点)
1094
+ */
1095
+ this.hoveredKey = "";
1096
+ }
1097
+ /**
1098
+ * 更新 hover 的内容
1099
+ * @param hoveredKey hovered key
1100
+ */
1101
+ updateHoveredKey(hoveredKey) {
1102
+ if (this.hoveredKey !== hoveredKey) {
1103
+ this.hoveredKey = hoveredKey;
1104
+ this.onHoveredChangeEmitter.fire(hoveredKey);
1105
+ }
1106
+ }
1107
+ /**
1108
+ * 清空 hover 内容
1109
+ */
1110
+ clearHovered() {
1111
+ this.updateHoveredKey("");
1112
+ }
1113
+ /**
1114
+ * 判断是否 hover
1115
+ * @param nodeId hoveredKey
1116
+ * @returns 是否 hover
1117
+ */
1118
+ isHovered(nodeId) {
1119
+ return nodeId === this.hoveredKey;
1120
+ }
1121
+ isSomeHovered() {
1122
+ return !!this.hoveredKey;
1123
+ }
1124
+ /**
1125
+ * 获取被 hover 的节点或线条
1126
+ */
1127
+ get hoveredNode() {
1128
+ return this.entityManager.getEntityById(this.hoveredKey);
1129
+ }
1130
+ };
1131
+ __decorateClass([
1132
+ (0, import_inversify2.inject)(import_core10.EntityManager)
1133
+ ], WorkflowHoverService.prototype, "entityManager", 2);
1134
+ WorkflowHoverService = __decorateClass([
1135
+ (0, import_inversify2.injectable)()
1136
+ ], WorkflowHoverService);
1137
+
1138
+ // src/service/workflow-drag-service.ts
1139
+ var import_nanoid3 = require("nanoid");
1140
+ var import_inversify6 = require("inversify");
1141
+ var import_utils16 = require("@flowgram.ai/utils");
1142
+ var import_document7 = require("@flowgram.ai/document");
1143
+ var import_document8 = require("@flowgram.ai/document");
1144
+ var import_core15 = require("@flowgram.ai/core");
1145
+
1146
+ // src/workflow-lines-manager.ts
1147
+ var import_lodash_es3 = require("lodash-es");
1148
+ var import_inversify3 = require("inversify");
1149
+ var import_utils12 = require("@flowgram.ai/utils");
1150
+ var import_document4 = require("@flowgram.ai/document");
1151
+ var import_core12 = require("@flowgram.ai/core");
1152
+
1153
+ // src/workflow-document-option.ts
1154
+ var import_form_core2 = require("@flowgram.ai/form-core");
1155
+ var import_document3 = require("@flowgram.ai/document");
1156
+ var import_core11 = require("@flowgram.ai/core");
1157
+
1158
+ // src/utils/flow-node-form-data.ts
1159
+ var import_form_core = require("@flowgram.ai/form-core");
1160
+
1161
+ // src/typings/workflow-json.ts
1162
+ var WorkflowContentChangeType = /* @__PURE__ */ ((WorkflowContentChangeType2) => {
1163
+ WorkflowContentChangeType2["ADD_NODE"] = "ADD_NODE";
1164
+ WorkflowContentChangeType2["DELETE_NODE"] = "DELETE_NODE";
1165
+ WorkflowContentChangeType2["MOVE_NODE"] = "MOVE_NODE";
1166
+ WorkflowContentChangeType2["NODE_DATA_CHANGE"] = "NODE_DATA_CHANGE";
1167
+ WorkflowContentChangeType2["ADD_LINE"] = "ADD_LINE";
1168
+ WorkflowContentChangeType2["DELETE_LINE"] = "DELETE_LINE";
1169
+ WorkflowContentChangeType2["META_CHANGE"] = "META_CHANGE";
1170
+ return WorkflowContentChangeType2;
1171
+ })(WorkflowContentChangeType || {});
1172
+
1173
+ // src/typings/workflow-line.ts
1174
+ var LineType = /* @__PURE__ */ ((LineType2) => {
1175
+ LineType2[LineType2["BEZIER"] = 0] = "BEZIER";
1176
+ LineType2[LineType2["LINE_CHART"] = 1] = "LINE_CHART";
1177
+ return LineType2;
1178
+ })(LineType || {});
1179
+ var LineColors = /* @__PURE__ */ ((LineColors2) => {
1180
+ LineColors2["HIDDEN"] = "transparent";
1181
+ LineColors2["DEFUALT"] = "#4d53e8";
1182
+ LineColors2["DRAWING"] = "#5DD6E3";
1183
+ LineColors2["HOVER"] = "#37d0ff";
1184
+ LineColors2["ERROR"] = "red";
1185
+ return LineColors2;
1186
+ })(LineColors || {});
1187
+
1188
+ // src/typings/index.ts
1189
+ var URLParams = Symbol("");
1190
+
1191
+ // src/utils/flow-node-form-data.ts
1192
+ function getFlowNodeFormData(node) {
1193
+ return node.getData(import_form_core.FlowNodeFormData);
1194
+ }
1195
+ function toFormJSON(node) {
1196
+ const formData = node.getData(import_form_core.FlowNodeFormData);
1197
+ if (!formData || !node.getNodeRegistry().formMeta) return void 0;
1198
+ return formData.toJSON();
1199
+ }
1200
+ function initFormDataFromJSON(node, json) {
1201
+ const formData = node.getData(import_form_core.FlowNodeFormData);
1202
+ const registry = node.getNodeRegistry();
1203
+ const { formMeta } = registry;
1204
+ if (formData && formMeta) {
1205
+ formData.createForm(formMeta, json.data);
1206
+ formData.onDataChange(() => {
1207
+ node.document.fireContentChange({
1208
+ type: "NODE_DATA_CHANGE" /* NODE_DATA_CHANGE */,
1209
+ toJSON: () => formData.toJSON(),
1210
+ entity: node
1211
+ });
1212
+ });
1213
+ }
1214
+ }
1215
+
1216
+ // src/workflow-document-option.ts
1217
+ var WorkflowDocumentOptions = Symbol("WorkflowDocumentOptions");
1218
+ var WorkflowDocumentOptionsDefault = {
1219
+ cursors: {
1220
+ grab: 'url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMjEiIHZpZXdCb3g9IjAgMCAyMCAyMSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0xMC40ODczIDIuNjIzNzhDOS45MDczMSAyLjYyMzc4IDkuNDM3MTMgMy4wOTM5NiA5LjQzNzEzIDMuNjczOTZWNS4xNDM3NkM5LjM5NDI4IDQuNDAyNzQgOC43Nzk3OCAzLjgxNTA0IDguMDI4MDIgMy44MTUwNEM3LjI0ODQ4IDMuODE1MDQgNi42MTY1MyA0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MMTIuNzcwMyA1LjQ3OTI2QzEyLjY1ODggNS40NzkyNiAxMi41NTA3IDUuNDk0MDggMTIuNDQ3OSA1LjUyMTg2WiIgZmlsbD0iIzFEMUMyMyIvPgo8L3N2Zz4="), auto'
1222
+ },
1223
+ fromNodeJSON(node, json) {
1224
+ initFormDataFromJSON(node, json);
1225
+ return;
1226
+ },
1227
+ toNodeJSON(node) {
1228
+ const nodeError = node.getData(import_form_core2.FlowNodeErrorData)?.getError();
1229
+ if (nodeError) {
1230
+ throw nodeError;
1231
+ }
1232
+ const transform = node.getData(import_core11.TransformData);
1233
+ let formJSON = toFormJSON(node);
1234
+ const metaData = {};
1235
+ const nodeMeta = node.getNodeMeta();
1236
+ const subCanvas = nodeMeta.subCanvas?.(node);
1237
+ if (subCanvas?.isCanvas === false) {
1238
+ const canvasNodeTransform = subCanvas.canvasNode.getData(import_document3.FlowNodeTransformData);
1239
+ const { x, y } = canvasNodeTransform.transform.position;
1240
+ metaData.canvasPosition = { x, y };
1241
+ }
1242
+ const json = {
1243
+ id: node.id,
1244
+ type: node.flowNodeType,
1245
+ meta: {
1246
+ position: { x: transform.position.x, y: transform.position.y },
1247
+ ...metaData
1248
+ },
1249
+ data: formJSON
1250
+ };
1251
+ return json;
1252
+ }
1253
+ };
1254
+
1255
+ // src/workflow-lines-manager.ts
1256
+ var WorkflowLinesManager = class {
1257
+ constructor() {
1258
+ this.toDispose = new import_utils12.DisposableCollection();
1259
+ // 线条类型
1260
+ this._lineType = 0 /* BEZIER */;
1261
+ this.onAvailableLinesChangeEmitter = new import_utils12.Emitter();
1262
+ this.onForceUpdateEmitter = new import_utils12.Emitter();
1263
+ /**
1264
+ * 有效的线条被添加或者删除时候触发,未连上的线条不算
1265
+ */
1266
+ this.onAvailableLinesChange = this.onAvailableLinesChangeEmitter.event;
1267
+ /**
1268
+ * 强制渲染 lines
1269
+ */
1270
+ this.onForceUpdate = this.onForceUpdateEmitter.event;
1271
+ this.contributionFactories = [];
1272
+ /**
1273
+ * 是否在调整线条
1274
+ */
1275
+ this.isDrawing = false;
1276
+ }
1277
+ init(doc) {
1278
+ this.document = doc;
1279
+ }
1280
+ forceUpdate() {
1281
+ this.onForceUpdateEmitter.fire();
1282
+ }
1283
+ get lineType() {
1284
+ return this._lineType;
1285
+ }
1286
+ get lineColor() {
1287
+ const color = {
1288
+ default: "#4d53e8" /* DEFUALT */,
1289
+ error: "red" /* ERROR */,
1290
+ hidden: "transparent" /* HIDDEN */,
1291
+ drawing: "#5DD6E3" /* DRAWING */,
1292
+ hovered: "#37d0ff" /* HOVER */,
1293
+ selected: "#37d0ff" /* HOVER */
1294
+ };
1295
+ if (this.options.lineColor) {
1296
+ Object.assign(color, this.options.lineColor);
1297
+ }
1298
+ return color;
1299
+ }
1300
+ switchLineType(newType) {
1301
+ if (newType === void 0) {
1302
+ if (this._lineType === 0 /* BEZIER */) {
1303
+ newType = 1 /* LINE_CHART */;
1304
+ } else {
1305
+ newType = 0 /* BEZIER */;
1306
+ }
1307
+ }
1308
+ if (newType !== this._lineType) {
1309
+ this._lineType = newType;
1310
+ this.getAllLines().forEach((line) => {
1311
+ line.getData(WorkflowLineRenderData).update();
1312
+ });
1313
+ window.requestAnimationFrame(() => {
1314
+ this.entityManager.fireEntityChanged(WorkflowLineEntity.type);
1315
+ });
1316
+ }
1317
+ return this._lineType;
1318
+ }
1319
+ getAllLines() {
1320
+ return this.entityManager.getEntities(WorkflowLineEntity);
1321
+ }
1322
+ hasLine(portInfo) {
1323
+ return !!this.entityManager.getEntityById(
1324
+ WorkflowLineEntity.portInfoToLineId(portInfo)
1325
+ );
1326
+ }
1327
+ getLine(portInfo) {
1328
+ return this.entityManager.getEntityById(
1329
+ WorkflowLineEntity.portInfoToLineId(portInfo)
1330
+ );
1331
+ }
1332
+ replaceLine(oldPortInfo, newPortInfo) {
1333
+ const oldLine = this.getLine(oldPortInfo);
1334
+ if (oldLine) {
1335
+ oldLine.dispose();
1336
+ }
1337
+ return this.createLine(newPortInfo);
1338
+ }
1339
+ createLine(options) {
1340
+ const { from, to, drawingTo, fromPort, toPort } = options;
1341
+ const available = Boolean(from && to);
1342
+ const key = options.key || WorkflowLineEntity.portInfoToLineId(options);
1343
+ let line = this.entityManager.getEntityById(key);
1344
+ if (line) {
1345
+ line.highlightColor = "";
1346
+ line.validate();
1347
+ return line;
1348
+ }
1349
+ const fromNode = this.entityManager.getEntityById(from)?.getData(WorkflowNodeLinesData);
1350
+ const toNode = to ? this.entityManager.getEntityById(to).getData(WorkflowNodeLinesData) : void 0;
1351
+ if (!fromNode) {
1352
+ return;
1353
+ }
1354
+ this.isDrawing = Boolean(drawingTo);
1355
+ line = this.entityManager.createEntity(WorkflowLineEntity, {
1356
+ id: key,
1357
+ document: this.document,
1358
+ linesManager: this,
1359
+ from,
1360
+ fromPort,
1361
+ toPort,
1362
+ to,
1363
+ drawingTo
1364
+ });
1365
+ this.registerData(line);
1366
+ fromNode.addLine(line);
1367
+ toNode?.addLine(line);
1368
+ line.onDispose(() => {
1369
+ if (drawingTo) {
1370
+ this.isDrawing = false;
1371
+ }
1372
+ fromNode.removeLine(line);
1373
+ toNode?.removeLine(line);
1374
+ line.validate();
1375
+ });
1376
+ line.toDispose.push(
1377
+ import_utils12.Disposable.create(() => {
1378
+ if (available) {
1379
+ this.onAvailableLinesChangeEmitter.fire({
1380
+ type: "DELETE_LINE" /* DELETE_LINE */,
1381
+ toJSON: () => line.toJSON(),
1382
+ entity: line
1383
+ });
1384
+ }
1385
+ })
1386
+ );
1387
+ if (available) {
1388
+ this.onAvailableLinesChangeEmitter.fire({
1389
+ type: "ADD_LINE" /* ADD_LINE */,
1390
+ toJSON: () => line.toJSON(),
1391
+ entity: line
1392
+ });
1393
+ }
1394
+ line.validate();
1395
+ return line;
1396
+ }
1397
+ /**
1398
+ * 获取线条中距离鼠标位置最近的线条和距离
1399
+ * @param mousePos 鼠标位置
1400
+ * @param minDistance 最小检测距离
1401
+ * @returns 距离鼠标位置最近的线条 以及距离
1402
+ */
1403
+ getCloseInLineFromMousePos(mousePos, minDistance = LINE_HOVER_DISTANCE) {
1404
+ let targetLine, targetLineDist;
1405
+ this.getAllLines().forEach((line) => {
1406
+ const dist = line.getHoverDist(mousePos);
1407
+ if (dist <= minDistance && (!targetLineDist || targetLineDist >= dist)) {
1408
+ targetLineDist = dist;
1409
+ targetLine = line;
1410
+ }
1411
+ });
1412
+ return targetLine;
1413
+ }
1414
+ dispose() {
1415
+ this.toDispose.dispose();
1416
+ }
1417
+ get disposed() {
1418
+ return this.toDispose.disposed;
1419
+ }
1420
+ isErrorLine(fromPort, toPort) {
1421
+ if (this.options.isErrorLine) {
1422
+ return this.options.isErrorLine(fromPort, toPort, this);
1423
+ }
1424
+ return false;
1425
+ }
1426
+ isReverseLine(line) {
1427
+ if (this.options.isReverseLine) {
1428
+ return this.options.isReverseLine(line);
1429
+ }
1430
+ return false;
1431
+ }
1432
+ isHideArrowLine(line) {
1433
+ if (this.options.isHideArrowLine) {
1434
+ return this.options.isHideArrowLine(line);
1435
+ }
1436
+ return false;
1437
+ }
1438
+ isFlowingLine(line) {
1439
+ if (this.options.isFlowingLine) {
1440
+ return this.options.isFlowingLine(line);
1441
+ }
1442
+ return false;
1443
+ }
1444
+ isDisabledLine(line) {
1445
+ if (this.options.isDisabledLine) {
1446
+ return this.options.isDisabledLine(line);
1447
+ }
1448
+ return false;
1449
+ }
1450
+ isVerticalLine(line) {
1451
+ if (this.options.isVerticalLine) {
1452
+ return this.options.isVerticalLine(line);
1453
+ }
1454
+ return false;
1455
+ }
1456
+ setLineRenderType(line) {
1457
+ if (this.options.setLineRenderType) {
1458
+ return this.options.setLineRenderType(line);
1459
+ }
1460
+ return void 0;
1461
+ }
1462
+ setLineClassName(line) {
1463
+ if (this.options.setLineClassName) {
1464
+ return this.options.setLineClassName(line);
1465
+ }
1466
+ return void 0;
1467
+ }
1468
+ getLineColor(line) {
1469
+ if (line.isHidden) {
1470
+ return this.lineColor.hidden;
1471
+ }
1472
+ if (line.hasError) {
1473
+ return this.lineColor.error;
1474
+ }
1475
+ if (line.highlightColor) {
1476
+ return line.highlightColor;
1477
+ }
1478
+ if (line.drawingTo) {
1479
+ return this.lineColor.drawing;
1480
+ }
1481
+ if (this.hoverService.isHovered(line.id)) {
1482
+ return this.lineColor.hovered;
1483
+ }
1484
+ if (this.selectService.isSelected(line.id)) {
1485
+ return this.lineColor.selected;
1486
+ }
1487
+ return this.lineColor.default;
1488
+ }
1489
+ canAddLine(fromPort, toPort, silent) {
1490
+ if (fromPort === toPort || fromPort.node === toPort.node || fromPort.portType !== "output" || toPort.portType !== "input" || toPort.disabled) {
1491
+ return false;
1492
+ }
1493
+ if (this.options.canAddLine) {
1494
+ return this.options.canAddLine(fromPort, toPort, this, silent);
1495
+ }
1496
+ return fromPort.node !== toPort.node;
1497
+ }
1498
+ toJSON() {
1499
+ return this.getAllLines().filter((l) => !l.isDrawing).map((l) => l.toJSON());
1500
+ }
1501
+ getPortById(portId) {
1502
+ return this.entityManager.getEntityById(portId);
1503
+ }
1504
+ canRemove(line, newLineInfo, silent) {
1505
+ if (this.options && this.options.canDeleteLine && !this.options.canDeleteLine(line, newLineInfo, silent)) {
1506
+ return false;
1507
+ }
1508
+ return true;
1509
+ }
1510
+ canReset(fromPort, oldToPort, newToPort) {
1511
+ if (this.options && this.options.canResetLine && !this.options.canResetLine(fromPort, oldToPort, newToPort, this)) {
1512
+ return false;
1513
+ }
1514
+ return true;
1515
+ }
1516
+ /**
1517
+ * 根据鼠标位置找到 port
1518
+ * @param pos
1519
+ */
1520
+ getPortFromMousePos(pos) {
1521
+ const allPorts = this.entityManager.getEntities(WorkflowPortEntity).filter((port) => port.node.flowNodeType !== "root");
1522
+ const targetPort = allPorts.find((port) => port.isHovered(pos.x, pos.y));
1523
+ if (targetPort) {
1524
+ const targetNode = this.document.getAllNodes().slice().reverse().filter((node) => targetPort.node?.parent?.id !== node.id).find((node) => node.getData(import_core12.TransformData).contains(pos.x, pos.y));
1525
+ if (targetNode && targetNode !== targetPort.node) {
1526
+ return;
1527
+ }
1528
+ }
1529
+ return targetPort;
1530
+ }
1531
+ /**
1532
+ * 根据鼠标位置找到 node
1533
+ * @param pos - 鼠标位置
1534
+ */
1535
+ getNodeFromMousePos(pos) {
1536
+ const allNodes = this.document.getAllNodes();
1537
+ const containNodes = [];
1538
+ const { selection } = this.selectService;
1539
+ const zoom = this.entityManager.getEntity(import_core12.PlaygroundConfigEntity)?.config?.zoom || 1;
1540
+ allNodes.forEach((node) => {
1541
+ const { bounds } = node.getData(import_document4.FlowNodeTransformData);
1542
+ if (bounds.clone().pad(4 / zoom).contains(pos.x, pos.y)) {
1543
+ containNodes.push(node);
1544
+ }
1545
+ });
1546
+ if (selection?.length) {
1547
+ const filteredNodes = containNodes.filter(
1548
+ (node) => selection.some((_node) => node.id === _node.id)
1549
+ );
1550
+ if (filteredNodes?.length) {
1551
+ return (0, import_lodash_es3.last)(filteredNodes);
1552
+ }
1553
+ }
1554
+ return (0, import_lodash_es3.last)(containNodes);
1555
+ }
1556
+ registerContribution(factory) {
1557
+ this.contributionFactories.push(factory);
1558
+ return this;
1559
+ }
1560
+ registerData(line) {
1561
+ line.addData(WorkflowLineRenderData);
1562
+ }
1563
+ };
1564
+ __decorateClass([
1565
+ (0, import_inversify3.inject)(WorkflowHoverService)
1566
+ ], WorkflowLinesManager.prototype, "hoverService", 2);
1567
+ __decorateClass([
1568
+ (0, import_inversify3.inject)(WorkflowSelectService)
1569
+ ], WorkflowLinesManager.prototype, "selectService", 2);
1570
+ __decorateClass([
1571
+ (0, import_inversify3.inject)(import_core12.EntityManager)
1572
+ ], WorkflowLinesManager.prototype, "entityManager", 2);
1573
+ __decorateClass([
1574
+ (0, import_inversify3.inject)(WorkflowDocumentOptions)
1575
+ ], WorkflowLinesManager.prototype, "options", 2);
1576
+ WorkflowLinesManager = __decorateClass([
1577
+ (0, import_inversify3.injectable)()
1578
+ ], WorkflowLinesManager);
1579
+
1580
+ // src/workflow-document.ts
1581
+ var import_nanoid2 = require("nanoid");
1582
+ var import_inversify5 = require("inversify");
1583
+ var import_utils14 = require("@flowgram.ai/utils");
1584
+ var import_form_core3 = require("@flowgram.ai/form-core");
1585
+ var import_document6 = require("@flowgram.ai/document");
1586
+ var import_core14 = require("@flowgram.ai/core");
1587
+
1588
+ // src/layout/free-layout.ts
1589
+ var import_inversify4 = require("inversify");
1590
+ var import_document5 = require("@flowgram.ai/document");
1591
+ var import_core13 = require("@flowgram.ai/core");
1592
+ var import_utils13 = require("@flowgram.ai/utils");
1593
+ var FREE_LAYOUT_KEY = "free-layout";
1594
+ var FreeLayout = class {
1595
+ constructor() {
1596
+ this.name = FREE_LAYOUT_KEY;
1597
+ }
1598
+ get document() {
1599
+ return this.documentProvider();
1600
+ }
1601
+ /**
1602
+ * 更新布局
1603
+ */
1604
+ update() {
1605
+ if (this.document.root.getData(import_document5.FlowNodeTransformData)?.localDirty) {
1606
+ this.document.root.clearMemoGlobal();
1607
+ }
1608
+ }
1609
+ syncTransform(node) {
1610
+ const transform = node.getData(import_document5.FlowNodeTransformData);
1611
+ if (!transform.localDirty) {
1612
+ return;
1613
+ }
1614
+ node.clearMemoGlobal();
1615
+ node.clearMemoLocal();
1616
+ transform.transform.update({
1617
+ size: transform.data.size
1618
+ });
1619
+ if (!node.parent) {
1620
+ return;
1621
+ }
1622
+ node.parent.clearMemoGlobal();
1623
+ node.parent.clearMemoLocal();
1624
+ const parentTransform = node.parent.getData(import_document5.FlowNodeTransformData);
1625
+ parentTransform.transform.fireChange();
1626
+ }
1627
+ /**
1628
+ * 获取节点的 padding 数据
1629
+ * @param node
1630
+ */
1631
+ getPadding(node) {
1632
+ const { padding } = node.getNodeMeta();
1633
+ const transform = node.getData(import_document5.FlowNodeTransformData);
1634
+ if (padding) {
1635
+ return typeof padding === "function" ? padding(transform) : padding;
1636
+ }
1637
+ return import_utils13.PaddingSchema.empty();
1638
+ }
1639
+ /**
1640
+ * 默认滚动到 fitview 区域
1641
+ * @param contentSize
1642
+ */
1643
+ getInitScroll(contentSize) {
1644
+ const bounds = import_utils13.Rectangle.enlarge(
1645
+ this.document.getAllNodes().map((node) => node.getData(import_core13.TransformData).bounds)
1646
+ ).pad(30, 30);
1647
+ const viewport = this.playgroundConfig.getViewport(false);
1648
+ const zoom = import_utils13.SizeSchema.fixSize(bounds, viewport);
1649
+ return {
1650
+ scrollX: (bounds.x + bounds.width / 2) * zoom - this.playgroundConfig.config.width / 2,
1651
+ scrollY: (bounds.y + bounds.height / 2) * zoom - this.playgroundConfig.config.height / 2
1652
+ };
1653
+ }
1654
+ /**
1655
+ * 获取默认输入点
1656
+ */
1657
+ getDefaultInputPoint(node) {
1658
+ return node.getData(import_core13.TransformData).bounds.leftCenter;
1659
+ }
1660
+ /**
1661
+ * 获取默认输出点
1662
+ */
1663
+ getDefaultOutputPoint(node) {
1664
+ return node.getData(import_core13.TransformData).bounds.rightCenter;
1665
+ }
1666
+ /**
1667
+ * 水平中心点
1668
+ */
1669
+ getDefaultNodeOrigin() {
1670
+ return { x: 0.5, y: 0 };
1671
+ }
1672
+ };
1673
+ __decorateClass([
1674
+ (0, import_inversify4.inject)(import_core13.PlaygroundConfigEntity)
1675
+ ], FreeLayout.prototype, "playgroundConfig", 2);
1676
+ __decorateClass([
1677
+ (0, import_inversify4.inject)(import_document5.FlowDocumentProvider)
1678
+ ], FreeLayout.prototype, "documentProvider", 2);
1679
+ FreeLayout = __decorateClass([
1680
+ (0, import_inversify4.injectable)()
1681
+ ], FreeLayout);
1682
+
1683
+ // src/workflow-document.ts
1684
+ var nanoid2 = (0, import_nanoid2.customAlphabet)("1234567890", 5);
1685
+ var WorkflowDocumentProvider = Symbol("WorkflowDocumentProvider");
1686
+ var WorkflowDocument = class extends import_document6.FlowDocument {
1687
+ constructor() {
1688
+ super(...arguments);
1689
+ this._onContentChangeEmitter = new import_utils14.Emitter();
1690
+ this.onLoadedEmitter = new import_utils14.Emitter();
1691
+ this.onContentChange = this._onContentChangeEmitter.event;
1692
+ this._onReloadEmitter = new import_utils14.Emitter();
1693
+ this.onReload = this._onReloadEmitter.event;
1694
+ this.disposed = false;
1695
+ /**
1696
+ * 数据加载完成
1697
+ */
1698
+ this.onLoaded = this.onLoadedEmitter.event;
1699
+ this._loading = false;
1700
+ this.options = {};
1701
+ }
1702
+ get loading() {
1703
+ return this._loading;
1704
+ }
1705
+ async fitView(easing) {
1706
+ return fitView(this, this.playgroundConfig, easing).then(() => {
1707
+ this.linesManager.forceUpdate();
1708
+ });
1709
+ }
1710
+ init() {
1711
+ super.init();
1712
+ this.currentLayoutKey = this.options.defaultLayout || FREE_LAYOUT_KEY;
1713
+ this.linesManager.init(this);
1714
+ this.playgroundConfig.getCursors = () => this.options.cursors;
1715
+ this.linesManager.onAvailableLinesChange((e) => this.fireContentChange(e));
1716
+ this.playgroundConfig.onReadonlyOrDisabledChange(({ readonly }) => {
1717
+ if (this.nodeEngineContext) {
1718
+ this.nodeEngineContext.readonly = readonly;
1719
+ }
1720
+ });
1721
+ }
1722
+ async load() {
1723
+ this._loading = true;
1724
+ await super.load();
1725
+ this._loading = false;
1726
+ this.onLoadedEmitter.fire();
1727
+ }
1728
+ async reload(json, delayTime = 0) {
1729
+ this._loading = true;
1730
+ this.clear();
1731
+ this.fromJSON(json);
1732
+ await (0, import_utils4.delay)(delayTime);
1733
+ this._loading = false;
1734
+ this._onReloadEmitter.fire(this);
1735
+ }
1736
+ /**
1737
+ * 从数据加载
1738
+ * @param json
1739
+ */
1740
+ fromJSON(json, fireRender = true) {
1741
+ const { flattenJSON, nodeBlocks, nodeEdges } = this.flatJSON(json);
1742
+ const nestedJSON = this.nestJSON(flattenJSON, nodeBlocks, nodeEdges);
1743
+ this.entityManager.changeEntityLocked = true;
1744
+ this.renderJSON(nestedJSON);
1745
+ this.entityManager.changeEntityLocked = false;
1746
+ this.transformer.loading = false;
1747
+ if (fireRender) {
1748
+ this.fireRender();
1749
+ }
1750
+ }
1751
+ /**
1752
+ * 清空画布
1753
+ */
1754
+ clear() {
1755
+ this.getAllNodes().map((node) => node.dispose());
1756
+ this.linesManager.getAllLines().map((line) => line.dispose());
1757
+ this.getAllPorts().map((port) => port.dispose());
1758
+ this.selectServices.clear();
1759
+ }
1760
+ /**
1761
+ * 创建流程节点
1762
+ * @param json
1763
+ */
1764
+ createWorkflowNode(json, isClone = false, parentId) {
1765
+ const isExistedNode = this.getNode(json.id);
1766
+ const parent = this.getNode(parentId ?? this.root.id) ?? this.root;
1767
+ const node = this.addNode(
1768
+ {
1769
+ ...json,
1770
+ parent
1771
+ },
1772
+ void 0,
1773
+ true
1774
+ );
1775
+ const registry = node.getNodeRegistry();
1776
+ const { formMeta } = registry;
1777
+ const meta = node.getNodeMeta();
1778
+ const formData = getFlowNodeFormData(node);
1779
+ const transform = node.getData(import_document6.FlowNodeTransformData);
1780
+ const freeLayout = this.layout;
1781
+ transform.onDataChange(() => {
1782
+ freeLayout.syncTransform(node);
1783
+ });
1784
+ let { position } = meta;
1785
+ if (!position) {
1786
+ position = this.getNodeDefaultPosition(json.type);
1787
+ }
1788
+ node.getData(import_core14.TransformData).update({
1789
+ position
1790
+ });
1791
+ if (formMeta && formData && !formData.formModel.initialized) {
1792
+ formData.createForm(formMeta, json.data);
1793
+ formData.onDataChange(() => {
1794
+ this.fireContentChange({
1795
+ type: "NODE_DATA_CHANGE" /* NODE_DATA_CHANGE */,
1796
+ toJSON: () => formData.toJSON(),
1797
+ entity: node
1798
+ });
1799
+ });
1800
+ }
1801
+ const positionData = node.getData(import_core14.PositionData);
1802
+ positionData.onDataChange(() => {
1803
+ this.fireContentChange({
1804
+ type: "MOVE_NODE" /* MOVE_NODE */,
1805
+ toJSON: () => positionData.toJSON(),
1806
+ entity: node
1807
+ });
1808
+ });
1809
+ const subCanvas = this.getNodeSubCanvas(node);
1810
+ if (!isExistedNode && !subCanvas?.isCanvas) {
1811
+ this.fireContentChange({
1812
+ type: "ADD_NODE" /* ADD_NODE */,
1813
+ entity: node,
1814
+ toJSON: () => this.toNodeJSON(node)
1815
+ });
1816
+ node.toDispose.push(
1817
+ import_utils14.Disposable.create(() => {
1818
+ this.fireContentChange({
1819
+ type: "DELETE_NODE" /* DELETE_NODE */,
1820
+ entity: node,
1821
+ toJSON: () => this.toNodeJSON(node)
1822
+ });
1823
+ })
1824
+ );
1825
+ node.toDispose.push(
1826
+ import_utils14.Disposable.create(() => {
1827
+ if (!node.parent || node.parent.flowNodeType === import_document6.FlowNodeBaseType.ROOT) {
1828
+ return;
1829
+ }
1830
+ const parentTransform = node.parent.getData(import_document6.FlowNodeTransformData);
1831
+ setTimeout(() => {
1832
+ parentTransform.fireChange();
1833
+ }, 0);
1834
+ })
1835
+ );
1836
+ }
1837
+ if (json.blocks) {
1838
+ this.renderJSON(
1839
+ { nodes: json.blocks, edges: json.edges ?? [] },
1840
+ {
1841
+ parent: node,
1842
+ isClone
1843
+ }
1844
+ );
1845
+ }
1846
+ if (subCanvas) {
1847
+ const canvasTransform = subCanvas.canvasNode.getData(import_core14.TransformData);
1848
+ canvasTransform.update({
1849
+ position: subCanvas.parentNode.getNodeMeta()?.canvasPosition
1850
+ });
1851
+ subCanvas.parentNode.onDispose(() => {
1852
+ subCanvas.canvasNode.dispose();
1853
+ });
1854
+ subCanvas.canvasNode.onDispose(() => {
1855
+ subCanvas.parentNode.dispose();
1856
+ });
1857
+ }
1858
+ this.onNodeCreateEmitter.fire({
1859
+ node,
1860
+ data: json
1861
+ });
1862
+ return node;
1863
+ }
1864
+ /**
1865
+ * 获取默认的 x y 坐标, 默认为当前画布可视区域中心
1866
+ * @param type
1867
+ * @protected
1868
+ */
1869
+ getNodeDefaultPosition(type) {
1870
+ const { size } = this.getNodeRegistry(type).meta || {};
1871
+ let position = this.playgroundConfig.getViewport(true).center;
1872
+ if (size) {
1873
+ position = {
1874
+ x: position.x,
1875
+ y: position.y - size.height / 2
1876
+ };
1877
+ }
1878
+ return getAntiOverlapPosition(this, position);
1879
+ }
1880
+ /**
1881
+ * 通过类型创建节点, 如果没有提供position 则直接放在画布中间
1882
+ * @param type
1883
+ */
1884
+ createWorkflowNodeByType(type, position, json = {}, parentID) {
1885
+ let id = json.id;
1886
+ if (id === void 0) {
1887
+ do {
1888
+ id = `1${nanoid2()}`;
1889
+ } while (this.entityManager.getEntityById(id));
1890
+ } else {
1891
+ if (this.entityManager.getEntityById(id)) {
1892
+ throw new Error(`[WorkflowDocument.createWorkflowNodeByType] Node Id "${id}" duplicated.`);
1893
+ }
1894
+ }
1895
+ return this.createWorkflowNode(
1896
+ {
1897
+ ...json,
1898
+ id,
1899
+ type,
1900
+ meta: { position, ...json?.meta },
1901
+ // TODO title 和 meta 要从注册数据去拿
1902
+ data: json?.data,
1903
+ blocks: json?.blocks,
1904
+ edges: json?.edges
1905
+ },
1906
+ false,
1907
+ parentID
1908
+ );
1909
+ }
1910
+ getAllNodes() {
1911
+ return this.entityManager.getEntities(WorkflowNodeEntity).filter((n) => n.id !== import_document6.FlowNodeBaseType.ROOT);
1912
+ }
1913
+ getAllPorts() {
1914
+ return this.entityManager.getEntities(WorkflowPortEntity).filter((p) => p.node.id !== import_document6.FlowNodeBaseType.ROOT);
1915
+ }
1916
+ /**
1917
+ * 获取画布中的非游离节点
1918
+ * 1. 开始节点
1919
+ * 2. 从开始节点出发能走到的节点
1920
+ * 3. 结束节点
1921
+ * 4. 默认所有子画布内节点为游离节点
1922
+ */
1923
+ getAssociatedNodes() {
1924
+ const allNode = this.getAllNodes();
1925
+ const allLines = this.linesManager.getAllLines().filter((line) => line.from && line.to).map((line) => ({
1926
+ from: line.from.id,
1927
+ to: line.to.id
1928
+ }));
1929
+ const startNodeId = allNode.find((node) => node.isStart).id;
1930
+ const endNodeId = allNode.find((node) => node.isNodeEnd).id;
1931
+ const nodeInSubCanvas = allNode.filter((node) => node.parent?.flowNodeType === import_document6.FlowNodeBaseType.SUB_CANVAS).map((node) => node.id);
1932
+ const associatedCache = /* @__PURE__ */ new Set([endNodeId, ...nodeInSubCanvas]);
1933
+ const bfs = (nodeId) => {
1934
+ if (associatedCache.has(nodeId)) {
1935
+ return;
1936
+ }
1937
+ associatedCache.add(nodeId);
1938
+ const nextNodes = allLines.reduce((ids, { from, to }) => {
1939
+ if (from === nodeId && !associatedCache.has(to)) {
1940
+ ids.push(to);
1941
+ }
1942
+ return ids;
1943
+ }, []);
1944
+ nextNodes.forEach(bfs);
1945
+ };
1946
+ bfs(startNodeId);
1947
+ const associatedNodes = allNode.filter((node) => associatedCache.has(node.id));
1948
+ return associatedNodes;
1949
+ }
1950
+ /**
1951
+ * 触发渲染
1952
+ */
1953
+ fireRender() {
1954
+ this.entityManager.fireEntityChanged(WorkflowNodeEntity.type);
1955
+ this.entityManager.fireEntityChanged(WorkflowLineEntity.type);
1956
+ this.entityManager.fireEntityChanged(WorkflowPortEntity.type);
1957
+ }
1958
+ fireContentChange(event) {
1959
+ if (this._loading || this.disposed || this.entityManager.changeEntityLocked) {
1960
+ return;
1961
+ }
1962
+ this._onContentChangeEmitter.fire(event);
1963
+ }
1964
+ toNodeJSON(node) {
1965
+ const subCanvas = this.getNodeSubCanvas(node);
1966
+ if (subCanvas?.isCanvas === true) {
1967
+ return this.toNodeJSON(subCanvas.parentNode);
1968
+ }
1969
+ const json = this.toNodeJSONFromOptions(node);
1970
+ const children = this.getNodeChildren(node);
1971
+ const blocks = children.map((child) => this.toNodeJSON(child));
1972
+ const linesMap = /* @__PURE__ */ new Map();
1973
+ children.forEach((child) => {
1974
+ const childLinesData = child.getData(WorkflowNodeLinesData);
1975
+ [...childLinesData.inputLines, ...childLinesData.outputLines].filter(Boolean).forEach((line) => {
1976
+ const lineJSON = this.toLineJSON(line);
1977
+ if (!lineJSON || linesMap.has(line.id)) {
1978
+ return;
1979
+ }
1980
+ linesMap.set(line.id, lineJSON);
1981
+ });
1982
+ });
1983
+ const edges = Array.from(linesMap.values());
1984
+ if (blocks.length > 0) json.blocks = blocks;
1985
+ if (edges.length > 0) json.edges = edges;
1986
+ return json;
1987
+ }
1988
+ /**
1989
+ * 节点转换为JSON, 没有format的过程
1990
+ * @param node
1991
+ * @returns
1992
+ */
1993
+ toNodeJSONFromOptions(node) {
1994
+ if (this.options.toNodeJSON) {
1995
+ return this.options.toNodeJSON(node);
1996
+ }
1997
+ return WorkflowDocumentOptionsDefault.toNodeJSON(node);
1998
+ }
1999
+ copyNode(node, newNodeId, format, position) {
2000
+ let json = this.toNodeJSON(node);
2001
+ if (format) {
2002
+ json = format(json);
2003
+ }
2004
+ position = position || {
2005
+ x: json.meta.position.x + 30,
2006
+ y: json.meta.position.y + 30
2007
+ };
2008
+ return this.createWorkflowNode(
2009
+ {
2010
+ id: newNodeId || `1${nanoid2()}`,
2011
+ type: node.flowNodeType,
2012
+ meta: {
2013
+ ...json.meta,
2014
+ position
2015
+ },
2016
+ data: json.data,
2017
+ blocks: json.blocks,
2018
+ edges: json.edges
2019
+ },
2020
+ true,
2021
+ node.parent?.id
2022
+ );
2023
+ }
2024
+ copyNodeFromJSON(flowNodeType, nodeJSON, newNodeId, position, parentId) {
2025
+ position = position || {
2026
+ x: nodeJSON.meta.position.x + 30,
2027
+ y: nodeJSON.meta.position.y + 30
2028
+ };
2029
+ return this.createWorkflowNode(
2030
+ {
2031
+ id: newNodeId || `1${nanoid2()}`,
2032
+ type: flowNodeType,
2033
+ meta: {
2034
+ ...nodeJSON.meta,
2035
+ position
2036
+ },
2037
+ data: nodeJSON.data,
2038
+ blocks: nodeJSON.blocks,
2039
+ edges: nodeJSON.edges
2040
+ },
2041
+ true,
2042
+ parentId
2043
+ );
2044
+ }
2045
+ canRemove(node, silent) {
2046
+ const meta = node.getNodeMeta();
2047
+ if (meta.deleteDisable) {
2048
+ return false;
2049
+ }
2050
+ if (this.options.canDeleteNode && !this.options.canDeleteNode(node, silent)) {
2051
+ return false;
2052
+ }
2053
+ return true;
2054
+ }
2055
+ /**
2056
+ * 判断端口是否为错误态
2057
+ */
2058
+ isErrorPort(port) {
2059
+ if (typeof this.options.isErrorPort === "function") {
2060
+ return this.options.isErrorPort(port);
2061
+ }
2062
+ return false;
2063
+ }
2064
+ /**
2065
+ * 导出数据
2066
+ */
2067
+ toJSON() {
2068
+ const rootJSON = this.toNodeJSON(this.root);
2069
+ return {
2070
+ nodes: rootJSON.blocks ?? [],
2071
+ edges: rootJSON.edges ?? []
2072
+ };
2073
+ }
2074
+ dispose() {
2075
+ if (this.disposed) {
2076
+ return;
2077
+ }
2078
+ super.dispose();
2079
+ this.disposed = true;
2080
+ this._onReloadEmitter.dispose();
2081
+ }
2082
+ getEdgeID(edge) {
2083
+ return WorkflowLineEntity.portInfoToLineId({
2084
+ from: edge.sourceNodeID,
2085
+ to: edge.targetNodeID,
2086
+ fromPort: edge.sourcePortID,
2087
+ toPort: edge.targetPortID
2088
+ });
2089
+ }
2090
+ /**
2091
+ * 拍平树形json结构,将结构信息提取到map
2092
+ */
2093
+ flatJSON(json = { nodes: [], edges: [] }) {
2094
+ const nodeBlocks = /* @__PURE__ */ new Map();
2095
+ const nodeEdges = /* @__PURE__ */ new Map();
2096
+ const rootNodes = json.nodes ?? [];
2097
+ const rootEdges = json.edges ?? [];
2098
+ const flattenNodeJSONs = [...rootNodes];
2099
+ const flattenEdgeJSONs = [...rootEdges];
2100
+ const rootBlockIDs = rootNodes.map((node) => node.id);
2101
+ const rootEdgeIDs = rootEdges.map((edge) => this.getEdgeID(edge));
2102
+ nodeBlocks.set(import_document6.FlowNodeBaseType.ROOT, rootBlockIDs);
2103
+ nodeEdges.set(import_document6.FlowNodeBaseType.ROOT, rootEdgeIDs);
2104
+ rootNodes.forEach((nodeJSON) => {
2105
+ const { blocks, edges } = nodeJSON;
2106
+ if (blocks) {
2107
+ flattenNodeJSONs.push(...blocks);
2108
+ const blockIDs = [];
2109
+ blocks.forEach((block) => {
2110
+ blockIDs.push(block.id);
2111
+ });
2112
+ nodeBlocks.set(nodeJSON.id, blockIDs);
2113
+ delete nodeJSON.blocks;
2114
+ }
2115
+ if (edges) {
2116
+ flattenEdgeJSONs.push(...edges);
2117
+ const edgeIDs = [];
2118
+ edges.forEach((edge) => {
2119
+ const edgeID = this.getEdgeID(edge);
2120
+ edgeIDs.push(edgeID);
2121
+ });
2122
+ nodeEdges.set(nodeJSON.id, edgeIDs);
2123
+ delete nodeJSON.edges;
2124
+ }
2125
+ });
2126
+ const flattenJSON = {
2127
+ nodes: flattenNodeJSONs,
2128
+ edges: flattenEdgeJSONs
2129
+ };
2130
+ return {
2131
+ flattenJSON,
2132
+ nodeBlocks,
2133
+ nodeEdges
2134
+ };
2135
+ }
2136
+ /**
2137
+ * 对JSON进行分层
2138
+ */
2139
+ nestJSON(flattenJSON, nodeBlocks, nodeEdges) {
2140
+ const nestJSON = {
2141
+ nodes: [],
2142
+ edges: []
2143
+ };
2144
+ const nodeMap = /* @__PURE__ */ new Map();
2145
+ const edgeMap = /* @__PURE__ */ new Map();
2146
+ const rootBlockSet = new Set(nodeBlocks.get(import_document6.FlowNodeBaseType.ROOT) ?? []);
2147
+ const rootEdgeSet = new Set(nodeEdges.get(import_document6.FlowNodeBaseType.ROOT) ?? []);
2148
+ flattenJSON.nodes.forEach((nodeJSON) => {
2149
+ nodeMap.set(nodeJSON.id, nodeJSON);
2150
+ });
2151
+ flattenJSON.edges.forEach((edgeJSON) => {
2152
+ const edgeID = this.getEdgeID(edgeJSON);
2153
+ edgeMap.set(edgeID, edgeJSON);
2154
+ });
2155
+ flattenJSON.nodes.forEach((nodeJSON) => {
2156
+ if (rootBlockSet.has(nodeJSON.id)) {
2157
+ nestJSON.nodes.push(nodeJSON);
2158
+ }
2159
+ if (nodeBlocks.has(nodeJSON.id)) {
2160
+ const blockIDs = nodeBlocks.get(nodeJSON.id);
2161
+ const blockJSONs = blockIDs.map((blockID) => nodeMap.get(blockID)).filter(Boolean);
2162
+ nodeJSON.blocks = blockJSONs;
2163
+ }
2164
+ if (nodeEdges.has(nodeJSON.id)) {
2165
+ const edgeIDs = nodeEdges.get(nodeJSON.id);
2166
+ const edgeJSONs = edgeIDs.map((edgeID) => edgeMap.get(edgeID)).filter(Boolean);
2167
+ nodeJSON.edges = edgeJSONs;
2168
+ }
2169
+ });
2170
+ flattenJSON.edges.forEach((edgeJSON) => {
2171
+ const edgeID = this.getEdgeID(edgeJSON);
2172
+ if (rootEdgeSet.has(edgeID)) {
2173
+ nestJSON.edges.push(edgeJSON);
2174
+ }
2175
+ });
2176
+ return nestJSON;
2177
+ }
2178
+ /**
2179
+ * 逐层创建节点和线条
2180
+ */
2181
+ renderJSON(json, options) {
2182
+ const { parent = this.root, isClone = false } = options ?? {};
2183
+ const containerID = this.getNodeSubCanvas(parent)?.canvasNode.id ?? parent.id;
2184
+ json.nodes.forEach((nodeJSON) => {
2185
+ this.createWorkflowNode(nodeJSON, isClone, containerID);
2186
+ }), // 创建线条
2187
+ json.edges.forEach((edge) => this.createWorkflowLine(edge, containerID));
2188
+ }
2189
+ getNodeSubCanvas(node) {
2190
+ if (!node) return;
2191
+ const nodeMeta = node.getNodeMeta();
2192
+ const subCanvas = nodeMeta.subCanvas?.(node);
2193
+ return subCanvas;
2194
+ }
2195
+ getNodeChildren(node) {
2196
+ if (!node) return [];
2197
+ const subCanvas = this.getNodeSubCanvas(node);
2198
+ const childrenWithCanvas = subCanvas ? subCanvas.canvasNode.collapsedChildren : node.collapsedChildren;
2199
+ const children = childrenWithCanvas.filter((child) => {
2200
+ const childMeta = child.getNodeMeta();
2201
+ return !childMeta.subCanvas?.(node)?.isCanvas;
2202
+ }).filter(Boolean);
2203
+ return children;
2204
+ }
2205
+ toLineJSON(line) {
2206
+ const lineJSON = line.toJSON();
2207
+ if (!line.to || !line.info.to || !line.toPort) {
2208
+ return;
2209
+ }
2210
+ const fromSubCanvas = this.getNodeSubCanvas(line.from);
2211
+ const toSubCanvas = this.getNodeSubCanvas(line.to);
2212
+ if (fromSubCanvas && !fromSubCanvas.isCanvas && toSubCanvas && toSubCanvas.isCanvas) {
2213
+ return;
2214
+ }
2215
+ if (line.from === line.to.parent && fromSubCanvas) {
2216
+ return {
2217
+ ...lineJSON,
2218
+ sourceNodeID: fromSubCanvas.parentNode.id
2219
+ };
2220
+ }
2221
+ if (line.to === line.from.parent && toSubCanvas) {
2222
+ return {
2223
+ ...lineJSON,
2224
+ targetNodeID: toSubCanvas.parentNode.id
2225
+ };
2226
+ }
2227
+ return lineJSON;
2228
+ }
2229
+ createWorkflowLine(json, parentId) {
2230
+ const fromNode = this.getNode(json.sourceNodeID);
2231
+ const toNode = this.getNode(json.targetNodeID);
2232
+ if (!fromNode || !toNode) {
2233
+ return;
2234
+ }
2235
+ const lineInfo = {
2236
+ from: json.sourceNodeID,
2237
+ fromPort: json.sourcePortID,
2238
+ to: json.targetNodeID,
2239
+ toPort: json.targetPortID
2240
+ };
2241
+ if (!parentId) {
2242
+ return this.linesManager.createLine(lineInfo);
2243
+ }
2244
+ const canvasNode = this.getNode(parentId);
2245
+ if (!canvasNode) {
2246
+ return this.linesManager.createLine(lineInfo);
2247
+ }
2248
+ const parentSubCanvas = this.getNodeSubCanvas(canvasNode);
2249
+ if (!parentSubCanvas) {
2250
+ return this.linesManager.createLine(lineInfo);
2251
+ }
2252
+ if (lineInfo.from === parentSubCanvas.parentNode.id) {
2253
+ return this.linesManager.createLine({
2254
+ ...lineInfo,
2255
+ from: parentSubCanvas.canvasNode.id
2256
+ });
2257
+ }
2258
+ if (lineInfo.to === parentSubCanvas.parentNode.id) {
2259
+ return this.linesManager.createLine({
2260
+ ...lineInfo,
2261
+ to: parentSubCanvas.canvasNode.id
2262
+ });
2263
+ }
2264
+ return this.linesManager.createLine(lineInfo);
2265
+ }
2266
+ };
2267
+ __decorateClass([
2268
+ (0, import_inversify5.inject)(WorkflowLinesManager)
2269
+ ], WorkflowDocument.prototype, "linesManager", 2);
2270
+ __decorateClass([
2271
+ (0, import_inversify5.inject)(import_core14.PlaygroundConfigEntity)
2272
+ ], WorkflowDocument.prototype, "playgroundConfig", 2);
2273
+ __decorateClass([
2274
+ (0, import_core14.injectPlaygroundContext)()
2275
+ ], WorkflowDocument.prototype, "playgroundContext", 2);
2276
+ __decorateClass([
2277
+ (0, import_inversify5.inject)(WorkflowDocumentOptions)
2278
+ ], WorkflowDocument.prototype, "options", 2);
2279
+ __decorateClass([
2280
+ (0, import_inversify5.inject)(import_form_core3.NodeEngineContext),
2281
+ (0, import_inversify5.optional)()
2282
+ ], WorkflowDocument.prototype, "nodeEngineContext", 2);
2283
+ __decorateClass([
2284
+ (0, import_inversify5.inject)(WorkflowSelectService)
2285
+ ], WorkflowDocument.prototype, "selectServices", 2);
2286
+ __decorateClass([
2287
+ (0, import_inversify5.postConstruct)()
2288
+ ], WorkflowDocument.prototype, "init", 1);
2289
+ WorkflowDocument = __decorateClass([
2290
+ (0, import_inversify5.injectable)()
2291
+ ], WorkflowDocument);
2292
+
2293
+ // src/service/workflow-drag-service.ts
2294
+ var DRAG_TIMEOUT = 100;
2295
+ var DRAG_MIN_DELTA = 5;
2296
+ function checkDragSuccess(time, e, originLine) {
2297
+ if (!originLine || time > DRAG_TIMEOUT || Math.abs(e.endPos.x - e.startPos.x) >= DRAG_MIN_DELTA || Math.abs(e.endPos.y - e.startPos.y) >= DRAG_MIN_DELTA) {
2298
+ return true;
2299
+ }
2300
+ return false;
2301
+ }
2302
+ var WorkflowDragService = class {
2303
+ constructor() {
2304
+ this._onDragLineEventEmitter = new import_utils16.Emitter();
2305
+ this.onDragLineEventChange = this._onDragLineEventEmitter.event;
2306
+ this.isDragging = false;
2307
+ this._nodesDragEmitter = new import_utils16.Emitter();
2308
+ this.onNodesDrag = this._nodesDragEmitter.event;
2309
+ this._toDispose = new import_utils16.DisposableCollection();
2310
+ this._droppableTransforms = [];
2311
+ this.posAdjusters = /* @__PURE__ */ new Set();
2312
+ this._onDragLineEndCallbacks = /* @__PURE__ */ new Map();
2313
+ }
2314
+ init() {
2315
+ this._toDispose.pushAll([this._onDragLineEventEmitter, this._nodesDragEmitter]);
2316
+ if (this.options.onDragLineEnd) {
2317
+ this._toDispose.push(this.onDragLineEnd(this.options.onDragLineEnd));
2318
+ }
2319
+ }
2320
+ dispose() {
2321
+ this._toDispose.dispose();
2322
+ }
2323
+ /**
2324
+ * 拖拽选中节点
2325
+ * @param event
2326
+ */
2327
+ startDragSelectedNodes(event) {
2328
+ let { selectedNodes } = this.selectService;
2329
+ if (selectedNodes.length === 0 || this.playgroundConfig.readonly || this.playgroundConfig.disabled) {
2330
+ return Promise.resolve(false);
2331
+ }
2332
+ const sameParent = this.childrenOfContainer(selectedNodes);
2333
+ if (sameParent && sameParent.flowNodeType !== import_document8.FlowNodeBaseType.ROOT) {
2334
+ selectedNodes = [sameParent];
2335
+ }
2336
+ const { altKey } = event;
2337
+ let startPosition = this.getNodesPosition(selectedNodes);
2338
+ let startPositions = selectedNodes.map((node) => {
2339
+ const transform = node.getData(import_core15.TransformData);
2340
+ return { x: transform.position.x, y: transform.position.y };
2341
+ });
2342
+ let dragSuccess = false;
2343
+ const startTime = Date.now();
2344
+ const dragger = new import_core15.PlaygroundDrag({
2345
+ onDragStart: () => {
2346
+ this.isDragging = true;
2347
+ },
2348
+ onDrag: (e) => {
2349
+ if (!dragSuccess && checkDragSuccess(Date.now() - startTime, e)) {
2350
+ dragSuccess = true;
2351
+ if (altKey) {
2352
+ const tryCopyNodes = selectedNodes;
2353
+ if (tryCopyNodes.length > 0) {
2354
+ this.selectService.clear();
2355
+ this.commandService.executeCommand("PASTE_NODES" /* PASTE_NODES */, tryCopyNodes, true).then((newNodes) => {
2356
+ if (newNodes && Array.isArray(newNodes) && newNodes.length > 0) {
2357
+ selectedNodes = newNodes;
2358
+ startPosition = this.getNodesPosition(tryCopyNodes);
2359
+ startPositions = tryCopyNodes.filter((n) => !n.getNodeMeta().copyDisable).map((node) => {
2360
+ const transform = node.getData(import_core15.TransformData);
2361
+ return {
2362
+ x: transform.position.x,
2363
+ y: transform.position.y
2364
+ };
2365
+ });
2366
+ }
2367
+ });
2368
+ }
2369
+ }
2370
+ }
2371
+ const offset = this.getDragPosOffset({
2372
+ event: e,
2373
+ selectedNodes,
2374
+ startPosition
2375
+ });
2376
+ selectedNodes.forEach((node, index) => {
2377
+ const transform = node.getData(import_core15.TransformData);
2378
+ const nodeStartPosition = startPositions[index];
2379
+ const newPosition = {
2380
+ x: nodeStartPosition.x + offset.x,
2381
+ y: nodeStartPosition.y + offset.y
2382
+ };
2383
+ if (node.collapsedChildren?.length > 0) {
2384
+ node.collapsedChildren.forEach((childNode) => {
2385
+ const childNodeTransformData = childNode.getData(import_document7.FlowNodeTransformData);
2386
+ childNodeTransformData.fireChange();
2387
+ });
2388
+ }
2389
+ transform.update({
2390
+ position: newPosition
2391
+ });
2392
+ });
2393
+ },
2394
+ onDragEnd: () => {
2395
+ this.isDragging = false;
2396
+ this._nodesDragEmitter.fire({
2397
+ type: "onDragEnd",
2398
+ nodes: selectedNodes,
2399
+ startPositions,
2400
+ altKey
2401
+ });
2402
+ }
2403
+ });
2404
+ return dragger.start(event.clientX, event.clientY, this.playgroundConfig).then(() => dragSuccess);
2405
+ }
2406
+ /**
2407
+ * 通过拖入卡片添加
2408
+ * @param type
2409
+ * @param event
2410
+ * @param data 节点数据
2411
+ */
2412
+ async dropCard(type, event, data, parent) {
2413
+ const mousePos = this.playgroundConfig.getPosFromMouseEvent(event);
2414
+ if (!this.playgroundConfig.getViewport().contains(mousePos.x, mousePos.y)) {
2415
+ return;
2416
+ }
2417
+ const position = this.adjustSubNodePosition(type, parent, mousePos);
2418
+ const node = await this.document.createWorkflowNodeByType(
2419
+ type,
2420
+ position,
2421
+ data,
2422
+ parent?.id
2423
+ );
2424
+ return node;
2425
+ }
2426
+ /**
2427
+ * 拖拽卡片到画布
2428
+ * 返回创建结果
2429
+ * @param type
2430
+ * @param event
2431
+ */
2432
+ async startDragCard(type, event, data, cloneNode) {
2433
+ let domNode;
2434
+ let startPos = { x: 0, y: 0 };
2435
+ const deferred = new import_utils16.PromiseDeferred();
2436
+ const dragger = new import_core15.PlaygroundDrag({
2437
+ onDragStart: (e) => {
2438
+ const targetNode = event.currentTarget;
2439
+ domNode = cloneNode ? cloneNode(e) : targetNode.cloneNode(true);
2440
+ const bounds = targetNode.getBoundingClientRect();
2441
+ startPos = { x: bounds.left, y: bounds.top };
2442
+ import_utils16.domUtils.setStyle(domNode, {
2443
+ zIndex: 1e3,
2444
+ position: "absolute",
2445
+ left: startPos.x,
2446
+ top: startPos.y,
2447
+ boxShadow: "0 6px 8px 0 rgba(28, 31, 35, .2)"
2448
+ });
2449
+ document.body.appendChild(domNode);
2450
+ this.updateDroppableTransforms();
2451
+ },
2452
+ onDrag: (e) => {
2453
+ const deltaX = e.endPos.x - e.startPos.x;
2454
+ const deltaY = e.endPos.y - e.startPos.y;
2455
+ const left = startPos.x + deltaX;
2456
+ const right = startPos.y + deltaY;
2457
+ domNode.style.left = `${left}px`;
2458
+ domNode.style.top = `${right}px`;
2459
+ const { x, y } = this.playgroundConfig.getPosFromMouseEvent(e);
2460
+ const draggingRect = new import_utils16.Rectangle(x, y, 170, 90);
2461
+ const collisionTransform = this._droppableTransforms.find((transform) => {
2462
+ const { bounds, entity } = transform;
2463
+ const padding = this.document.layout.getPadding(entity);
2464
+ const transformRect = new import_utils16.Rectangle(
2465
+ bounds.x + padding.left + padding.right,
2466
+ bounds.y,
2467
+ bounds.width,
2468
+ bounds.height
2469
+ );
2470
+ return import_utils16.Rectangle.intersects(draggingRect, transformRect);
2471
+ });
2472
+ this.updateDropNode(collisionTransform?.entity);
2473
+ },
2474
+ onDragEnd: async (e) => {
2475
+ const dropNode = this._dropNode;
2476
+ const dragNode = await this.dropCard(type, e, data, dropNode);
2477
+ this.clearDrop();
2478
+ if (dragNode) {
2479
+ domNode.remove();
2480
+ deferred.resolve(dragNode);
2481
+ } else {
2482
+ domNode.style.transition = "all ease .2s";
2483
+ domNode.style.left = `${startPos.x}px`;
2484
+ domNode.style.top = `${startPos.y}px`;
2485
+ const TIMEOUT = 200;
2486
+ await (0, import_utils16.delay)(TIMEOUT);
2487
+ domNode.remove();
2488
+ deferred.resolve();
2489
+ }
2490
+ }
2491
+ });
2492
+ await dragger.start(event.clientX, event.clientY);
2493
+ return deferred.promise;
2494
+ }
2495
+ /**
2496
+ * 如果存在容器节点,且传入鼠标坐标,需要用容器的坐标减去传入的鼠标坐标
2497
+ */
2498
+ adjustSubNodePosition(subNodeType, containerNode, mousePos, resetEmptyPos = true) {
2499
+ if (!mousePos) {
2500
+ return { x: 0, y: 0 };
2501
+ }
2502
+ if (!subNodeType || !containerNode || containerNode.flowNodeType === import_document8.FlowNodeBaseType.ROOT) {
2503
+ return mousePos;
2504
+ }
2505
+ const isParentEmpty = !containerNode.children || containerNode.children.length === 0;
2506
+ const parentPadding = this.document.layout.getPadding(containerNode);
2507
+ const parentTransform = containerNode.getData(import_core15.TransformData);
2508
+ if (isParentEmpty && resetEmptyPos) {
2509
+ return {
2510
+ x: 0,
2511
+ y: parentPadding.top
2512
+ };
2513
+ } else {
2514
+ return {
2515
+ x: mousePos.x - parentTransform.position.x,
2516
+ y: mousePos.y - parentTransform.position.y
2517
+ };
2518
+ }
2519
+ }
2520
+ /**
2521
+ * 注册位置调整
2522
+ */
2523
+ registerPosAdjuster(adjuster) {
2524
+ this.posAdjusters.add(adjuster);
2525
+ return {
2526
+ dispose: () => this.posAdjusters.delete(adjuster)
2527
+ };
2528
+ }
2529
+ /**
2530
+ * 获取拖拽偏移
2531
+ */
2532
+ getDragPosOffset(params) {
2533
+ const { event, selectedNodes, startPosition } = params;
2534
+ const { finalScale } = this.playgroundConfig;
2535
+ const mouseOffset = {
2536
+ x: (event.endPos.x - event.startPos.x) / finalScale,
2537
+ y: (event.endPos.y - event.startPos.y) / finalScale
2538
+ };
2539
+ const wholePosition = {
2540
+ x: startPosition.x + mouseOffset.x,
2541
+ y: startPosition.y + mouseOffset.y
2542
+ };
2543
+ const adjustedOffsets = Array.from(this.posAdjusters.values()).map(
2544
+ (adjuster) => adjuster({
2545
+ selectedNodes,
2546
+ position: wholePosition
2547
+ })
2548
+ );
2549
+ const offset = adjustedOffsets.reduce(
2550
+ (offset2, adjustOffset) => ({
2551
+ x: offset2.x + adjustOffset.x,
2552
+ y: offset2.y + adjustOffset.y
2553
+ }),
2554
+ mouseOffset
2555
+ );
2556
+ return offset;
2557
+ }
2558
+ updateDroppableTransforms() {
2559
+ this._droppableTransforms = this.document.getRenderDatas(import_document7.FlowNodeTransformData, false).filter((transform) => {
2560
+ const { entity } = transform;
2561
+ if (entity.originParent) {
2562
+ return this.nodeSelectable(entity) && this.nodeSelectable(entity.originParent);
2563
+ }
2564
+ return this.nodeSelectable(entity);
2565
+ }).filter((transform) => {
2566
+ const { entity } = transform;
2567
+ return entity.flowNodeType === import_document8.FlowNodeBaseType.SUB_CANVAS;
2568
+ });
2569
+ }
2570
+ /**
2571
+ * 获取节点整体位置
2572
+ */
2573
+ getNodesPosition(nodes) {
2574
+ const selectedBounds = import_utils16.Rectangle.enlarge(
2575
+ nodes.map((n) => n.getData(import_document7.FlowNodeTransformData).bounds)
2576
+ );
2577
+ const position = {
2578
+ x: selectedBounds.x,
2579
+ y: selectedBounds.y
2580
+ };
2581
+ return position;
2582
+ }
2583
+ nodeSelectable(node) {
2584
+ const selectable = node.getNodeMeta().selectable;
2585
+ if (typeof selectable === "function") {
2586
+ return selectable(node);
2587
+ } else {
2588
+ return selectable;
2589
+ }
2590
+ }
2591
+ updateDropNode(node) {
2592
+ if (this._dropNode) {
2593
+ if (this._dropNode.id === node?.id) {
2594
+ return;
2595
+ }
2596
+ this.selectService.clear();
2597
+ }
2598
+ if (node) {
2599
+ this.selectService.selectNode(node);
2600
+ }
2601
+ this._dropNode = node;
2602
+ }
2603
+ clearDrop() {
2604
+ if (this._dropNode) {
2605
+ this.selectService.clear();
2606
+ }
2607
+ this._dropNode = void 0;
2608
+ this._droppableTransforms = [];
2609
+ }
2610
+ setLineColor(line, color) {
2611
+ line.highlightColor = color;
2612
+ this.hoverService.clearHovered();
2613
+ }
2614
+ handleDragOnNode(toNode, fromPort, line, toPort, originLine) {
2615
+ if (toPort && (originLine?.toPort === toPort || toPort.portType === "input" && this.linesManager.canAddLine(fromPort, toPort, true))) {
2616
+ this.hoverService.updateHoveredKey(toPort.id);
2617
+ line.setToPort(toPort);
2618
+ this._onDragLineEventEmitter.fire({
2619
+ type: "onDrag",
2620
+ onDragNodeId: toNode.id
2621
+ });
2622
+ return {
2623
+ hasError: false
2624
+ };
2625
+ } else if (toNode.flowNodeType === import_document8.FlowNodeBaseType.SUB_CANVAS) {
2626
+ return {
2627
+ hasError: false
2628
+ };
2629
+ } else {
2630
+ this.setLineColor(line, this.linesManager.lineColor.error);
2631
+ return {
2632
+ hasError: true
2633
+ };
2634
+ }
2635
+ }
2636
+ childrenOfContainer(nodes) {
2637
+ if (nodes.length === 0) {
2638
+ return;
2639
+ }
2640
+ const sourceContainer = nodes[0]?.parent;
2641
+ if (!sourceContainer || sourceContainer.collapsedChildren.length !== nodes.length) {
2642
+ return;
2643
+ }
2644
+ const valid = nodes.every((node) => node?.parent === sourceContainer);
2645
+ if (!valid) {
2646
+ return;
2647
+ }
2648
+ return sourceContainer;
2649
+ }
2650
+ /**
2651
+ * 绘制线条
2652
+ * @param opts
2653
+ * @param event
2654
+ */
2655
+ async startDrawingLine(fromPort, event, originLine) {
2656
+ const isFromInActivePort = !originLine && fromPort.isErrorPort() && fromPort.disabled;
2657
+ if (originLine?.disabled || isFromInActivePort || this.playgroundConfig.readonly || this.playgroundConfig.disabled) {
2658
+ return { dragSuccess: false, newLine: void 0 };
2659
+ }
2660
+ const config = this.playgroundConfig;
2661
+ const deferred = new import_utils16.PromiseDeferred();
2662
+ const preCursor = config.cursor;
2663
+ let line, toPort, toNode, lineErrorReset = false;
2664
+ const startTime = Date.now();
2665
+ let dragSuccess = false;
2666
+ const dragger = new import_core15.PlaygroundDrag({
2667
+ onDrag: (e) => {
2668
+ if (!line && checkDragSuccess(Date.now() - startTime, e, originLine)) {
2669
+ if (originLine) {
2670
+ originLine.highlightColor = this.linesManager.lineColor.hidden;
2671
+ }
2672
+ dragSuccess = true;
2673
+ line = this.linesManager.createLine({
2674
+ from: fromPort.node.id,
2675
+ fromPort: fromPort.portID,
2676
+ drawingTo: config.getPosFromMouseEvent(event)
2677
+ });
2678
+ if (!line) {
2679
+ return;
2680
+ }
2681
+ config.updateCursor("grab");
2682
+ line.highlightColor = this.linesManager.lineColor.drawing;
2683
+ this.hoverService.updateHoveredKey("");
2684
+ }
2685
+ if (!line) {
2686
+ return;
2687
+ }
2688
+ lineErrorReset = false;
2689
+ const dragPos = config.getPosFromMouseEvent(e);
2690
+ toNode = this.linesManager.getNodeFromMousePos(dragPos);
2691
+ toPort = this.linesManager.getPortFromMousePos(dragPos);
2692
+ if (!toPort) {
2693
+ line.setToPort(void 0);
2694
+ } else if (!this.linesManager.canAddLine(fromPort, toPort, true)) {
2695
+ line.highlightColor = this.linesManager.lineColor.error;
2696
+ lineErrorReset = true;
2697
+ line.setToPort(void 0);
2698
+ } else {
2699
+ line.setToPort(toPort);
2700
+ }
2701
+ this._onDragLineEventEmitter.fire({
2702
+ type: "onDrag"
2703
+ });
2704
+ this.setLineColor(line, this.linesManager.lineColor.drawing);
2705
+ if (toNode && toNode.flowNodeType !== import_document8.FlowNodeBaseType.SUB_CANVAS) {
2706
+ const portsData = toNode.getData(WorkflowNodePortsData);
2707
+ toPort = portsData.inputPorts[0];
2708
+ const { hasError } = this.handleDragOnNode(toNode, fromPort, line, toPort, originLine);
2709
+ lineErrorReset = hasError;
2710
+ }
2711
+ if (line.toPort) {
2712
+ line.drawingTo = { x: line.toPort.point.x, y: line.toPort.point.y };
2713
+ } else {
2714
+ line.drawingTo = { x: dragPos.x, y: dragPos.y };
2715
+ }
2716
+ originLine?.validate();
2717
+ line.validate();
2718
+ },
2719
+ // eslint-disable-next-line complexity
2720
+ onDragEnd: async (e) => {
2721
+ const dragPos = config.getPosFromMouseEvent(e);
2722
+ const onDragLineEndCallbacks = Array.from(this._onDragLineEndCallbacks.values());
2723
+ config.updateCursor(preCursor);
2724
+ await Promise.all(
2725
+ onDragLineEndCallbacks.map(
2726
+ (callback) => callback({
2727
+ fromPort,
2728
+ toPort,
2729
+ mousePos: dragPos,
2730
+ line,
2731
+ originLine,
2732
+ event: e
2733
+ })
2734
+ )
2735
+ );
2736
+ line?.dispose();
2737
+ this._onDragLineEventEmitter.fire({
2738
+ type: "onDragEnd"
2739
+ });
2740
+ if (originLine) {
2741
+ originLine.highlightColor = "";
2742
+ }
2743
+ const end = () => {
2744
+ originLine?.validate();
2745
+ deferred.resolve({ dragSuccess });
2746
+ };
2747
+ if (dragSuccess) {
2748
+ if (originLine && originLine.toPort === toPort) {
2749
+ return end();
2750
+ }
2751
+ if (toPort && toPort.portType !== "input") {
2752
+ return end();
2753
+ }
2754
+ const newLineInfo = toPort ? {
2755
+ from: fromPort.node.id,
2756
+ fromPort: fromPort.portID,
2757
+ to: toPort.node.id,
2758
+ toPort: toPort.portID
2759
+ } : void 0;
2760
+ const isReset = originLine && toPort;
2761
+ if (isReset && !this.linesManager.canReset(
2762
+ originLine.fromPort,
2763
+ originLine.toPort,
2764
+ toPort
2765
+ )) {
2766
+ return end();
2767
+ }
2768
+ if (originLine && (!this.linesManager.canRemove(originLine, newLineInfo, false) || lineErrorReset)) {
2769
+ return end();
2770
+ } else {
2771
+ originLine?.dispose();
2772
+ }
2773
+ if (!toPort || !this.linesManager.canAddLine(fromPort, toPort, false)) {
2774
+ return end();
2775
+ }
2776
+ const newLine = this.linesManager.createLine(newLineInfo);
2777
+ if (!newLine) {
2778
+ end();
2779
+ }
2780
+ deferred.resolve({
2781
+ dragSuccess,
2782
+ newLine
2783
+ });
2784
+ } else {
2785
+ end();
2786
+ }
2787
+ }
2788
+ });
2789
+ await dragger.start(event.clientX, event.clientY, config);
2790
+ return deferred.promise;
2791
+ }
2792
+ /**
2793
+ * 重新连接线条
2794
+ * @param line
2795
+ * @param e
2796
+ */
2797
+ async resetLine(line, e) {
2798
+ const { fromPort } = line;
2799
+ const { dragSuccess } = await this.startDrawingLine(fromPort, e, line);
2800
+ if (!dragSuccess) {
2801
+ this.selectService.select(line);
2802
+ }
2803
+ }
2804
+ /** 线条拖拽结束 */
2805
+ onDragLineEnd(callback) {
2806
+ const id = (0, import_nanoid3.nanoid)();
2807
+ this._onDragLineEndCallbacks.set(id, callback);
2808
+ return {
2809
+ dispose: () => {
2810
+ this._onDragLineEndCallbacks.delete(id);
2811
+ }
2812
+ };
2813
+ }
2814
+ };
2815
+ __decorateClass([
2816
+ (0, import_inversify6.inject)(import_core15.PlaygroundConfigEntity)
2817
+ ], WorkflowDragService.prototype, "playgroundConfig", 2);
2818
+ __decorateClass([
2819
+ (0, import_inversify6.inject)(WorkflowHoverService)
2820
+ ], WorkflowDragService.prototype, "hoverService", 2);
2821
+ __decorateClass([
2822
+ (0, import_inversify6.inject)(WorkflowDocument)
2823
+ ], WorkflowDragService.prototype, "document", 2);
2824
+ __decorateClass([
2825
+ (0, import_inversify6.inject)(WorkflowLinesManager)
2826
+ ], WorkflowDragService.prototype, "linesManager", 2);
2827
+ __decorateClass([
2828
+ (0, import_inversify6.inject)(import_core15.CommandService)
2829
+ ], WorkflowDragService.prototype, "commandService", 2);
2830
+ __decorateClass([
2831
+ (0, import_inversify6.inject)(WorkflowSelectService)
2832
+ ], WorkflowDragService.prototype, "selectService", 2);
2833
+ __decorateClass([
2834
+ (0, import_inversify6.inject)(import_document7.FlowOperationBaseService)
2835
+ ], WorkflowDragService.prototype, "operationService", 2);
2836
+ __decorateClass([
2837
+ (0, import_inversify6.inject)(WorkflowDocumentOptions)
2838
+ ], WorkflowDragService.prototype, "options", 2);
2839
+ __decorateClass([
2840
+ (0, import_inversify6.postConstruct)()
2841
+ ], WorkflowDragService.prototype, "init", 1);
2842
+ WorkflowDragService = __decorateClass([
2843
+ (0, import_inversify6.injectable)()
2844
+ ], WorkflowDragService);
2845
+
2846
+ // src/service/workflow-reset-layout-service.ts
2847
+ var import_inversify7 = require("inversify");
2848
+ var import_core17 = require("@flowgram.ai/core");
2849
+ var import_core18 = require("@flowgram.ai/core");
2850
+ var import_utils17 = require("@flowgram.ai/utils");
2851
+
2852
+ // src/utils/layout-to-positions.ts
2853
+ var import_document9 = require("@flowgram.ai/document");
2854
+ var import_core16 = require("@flowgram.ai/core");
2855
+ var layoutToPositions = async (nodes, nodePositionMap) => {
2856
+ const newNodePositionMap = {};
2857
+ nodes.forEach((node) => {
2858
+ const transform = node.getData(import_core16.TransformData);
2859
+ const nodeTransform = node.getData(import_document9.FlowNodeTransformData);
2860
+ newNodePositionMap[node.id] = {
2861
+ x: transform.position.x,
2862
+ y: transform.position.y + nodeTransform.bounds.height / 2
2863
+ };
2864
+ });
2865
+ return new Promise((resolve) => {
2866
+ (0, import_core16.startTween)({
2867
+ from: { d: 0 },
2868
+ to: { d: 100 },
2869
+ duration: 300,
2870
+ onUpdate: (v) => {
2871
+ nodes.forEach((node) => {
2872
+ const transform = node.getData(import_core16.TransformData);
2873
+ const deltaX = (nodePositionMap[node.id].x - transform.position.x) * v.d / 100;
2874
+ const deltaY = (nodePositionMap[node.id].y - transform.bounds.height / 2 - transform.position.y) * v.d / 100;
2875
+ if (node.collapsedChildren?.length > 0) {
2876
+ node.collapsedChildren.forEach((childNode) => {
2877
+ const childNodeTransformData = childNode.getData(import_document9.FlowNodeTransformData);
2878
+ childNodeTransformData.fireChange();
2879
+ });
2880
+ }
2881
+ transform.update({
2882
+ position: {
2883
+ x: transform.position.x + deltaX,
2884
+ y: transform.position.y + deltaY
2885
+ }
2886
+ });
2887
+ });
2888
+ },
2889
+ onComplete: () => {
2890
+ resolve(newNodePositionMap);
2891
+ }
2892
+ });
2893
+ });
2894
+ };
2895
+
2896
+ // src/service/workflow-reset-layout-service.ts
2897
+ var WorkflowResetLayoutService = class {
2898
+ constructor() {
2899
+ this._resetLayoutEmitter = new import_utils17.Emitter();
2900
+ /**
2901
+ * reset layout事件
2902
+ */
2903
+ this.onResetLayout = this._resetLayoutEmitter.event;
2904
+ this._toDispose = new import_utils17.DisposableCollection();
2905
+ }
2906
+ init() {
2907
+ this._toDispose.push(this._resetLayoutEmitter);
2908
+ }
2909
+ /**
2910
+ * 触发重置布局
2911
+ * @param nodeIds 节点id
2912
+ * @param positionMap 新布局数据
2913
+ * @param oldPositionMap 老布局数据
2914
+ */
2915
+ fireResetLayout(nodeIds, positionMap, oldPositionMap) {
2916
+ this._resetLayoutEmitter.fire({
2917
+ nodeIds,
2918
+ positionMap,
2919
+ oldPositionMap
2920
+ });
2921
+ }
2922
+ /**
2923
+ * 根据数据重新布局
2924
+ * @param positionMap
2925
+ * @returns
2926
+ */
2927
+ async layoutToPositions(nodeIds, positionMap) {
2928
+ const nodes = nodeIds.map((id) => this._entityManager.getEntityById(id)).filter(Boolean);
2929
+ const positions = await layoutToPositions(nodes, positionMap);
2930
+ fitView(this._document, this._config, true);
2931
+ return positions;
2932
+ }
2933
+ /**
2934
+ * 销毁
2935
+ */
2936
+ dispose() {
2937
+ this._toDispose.dispose();
2938
+ }
2939
+ };
2940
+ __decorateClass([
2941
+ (0, import_inversify7.inject)(import_core17.PlaygroundConfigEntity)
2942
+ ], WorkflowResetLayoutService.prototype, "_config", 2);
2943
+ __decorateClass([
2944
+ (0, import_inversify7.inject)(WorkflowDocument)
2945
+ ], WorkflowResetLayoutService.prototype, "_document", 2);
2946
+ __decorateClass([
2947
+ (0, import_inversify7.inject)(import_core18.EntityManager)
2948
+ ], WorkflowResetLayoutService.prototype, "_entityManager", 2);
2949
+ __decorateClass([
2950
+ (0, import_inversify7.postConstruct)()
2951
+ ], WorkflowResetLayoutService.prototype, "init", 1);
2952
+ WorkflowResetLayoutService = __decorateClass([
2953
+ (0, import_inversify7.injectable)()
2954
+ ], WorkflowResetLayoutService);
2955
+
2956
+ // src/hooks/use-playground-readonly-state.ts
2957
+ var import_react = require("react");
2958
+ var import_core19 = require("@flowgram.ai/core");
2959
+ function usePlaygroundReadonlyState(listenChange) {
2960
+ const playground = (0, import_core19.usePlayground)();
2961
+ const refresh = (0, import_core19.useRefresh)();
2962
+ (0, import_react.useEffect)(() => {
2963
+ let dispose = void 0;
2964
+ if (listenChange) {
2965
+ dispose = playground.config.onReadonlyOrDisabledChange(() => refresh());
2966
+ }
2967
+ return () => dispose?.dispose();
2968
+ }, [listenChange]);
2969
+ return playground.config.readonly;
2970
+ }
2971
+
2972
+ // src/hooks/use-node-render.tsx
2973
+ function checkTargetDraggable(el) {
2974
+ return el && el.tagName !== "INPUT" && el.tagName !== "TEXTAREA" && !el.closest(".flow-canvas-not-draggable");
2975
+ }
2976
+ function useNodeRender(nodeFromProps) {
2977
+ const node = nodeFromProps || (0, import_react2.useContext)(import_core20.PlaygroundEntityContext);
2978
+ const renderData = node.getData(import_document10.FlowNodeRenderData);
2979
+ const portsData = node.getData(WorkflowNodePortsData);
2980
+ const readonly = usePlaygroundReadonlyState();
2981
+ const dragService = (0, import_core20.useService)(WorkflowDragService);
2982
+ const selectionService = (0, import_core20.useService)(WorkflowSelectService);
2983
+ const isDragging = (0, import_react2.useRef)(false);
2984
+ const nodeRef = (0, import_react2.useRef)(null);
2985
+ const [linkingNodeId, setLinkingNodeId] = (0, import_react2.useState)("");
2986
+ (0, import_react2.useEffect)(() => {
2987
+ const disposable = dragService.onDragLineEventChange(({ type, onDragNodeId }) => {
2988
+ if (type === "onDrag") {
2989
+ setLinkingNodeId(onDragNodeId || "");
2990
+ } else {
2991
+ setLinkingNodeId("");
2992
+ }
2993
+ });
2994
+ return () => {
2995
+ disposable.dispose();
2996
+ };
2997
+ }, []);
2998
+ const startDrag = (0, import_react2.useCallback)(
2999
+ (e) => {
3000
+ e.preventDefault();
3001
+ if (!selectionService.isSelected(node.id)) {
3002
+ selectNode(e);
3003
+ }
3004
+ if (!checkTargetDraggable(e.target) || !checkTargetDraggable(document.activeElement)) {
3005
+ return;
3006
+ }
3007
+ isDragging.current = true;
3008
+ dragService.startDragSelectedNodes(e).finally(
3009
+ () => setTimeout(() => {
3010
+ isDragging.current = false;
3011
+ })
3012
+ );
3013
+ },
3014
+ [dragService, node]
3015
+ );
3016
+ const selectNode = (0, import_react2.useCallback)(
3017
+ (e) => {
3018
+ if (isDragging.current) {
3019
+ return;
3020
+ }
3021
+ if (e.metaKey || e.shiftKey || e.ctrlKey) {
3022
+ selectionService.toggleSelect(node);
3023
+ } else {
3024
+ selectionService.selectNode(node);
3025
+ }
3026
+ if (e.target) {
3027
+ e.target.focus();
3028
+ }
3029
+ },
3030
+ [node]
3031
+ );
3032
+ const deleteNode = (0, import_react2.useCallback)(() => node.dispose(), [node]);
3033
+ (0, import_core20.useListenEvents)(portsData.onDataChange);
3034
+ const isFirefox = navigator?.userAgent?.includes?.("Firefox");
3035
+ const onFocus = (0, import_react2.useCallback)(() => {
3036
+ if (isFirefox) {
3037
+ nodeRef.current?.setAttribute("draggable", "false");
3038
+ }
3039
+ }, []);
3040
+ const onBlur = (0, import_react2.useCallback)(() => {
3041
+ if (isFirefox) {
3042
+ nodeRef.current?.setAttribute("draggable", "true");
3043
+ }
3044
+ }, []);
3045
+ const getExtInfo = (0, import_react2.useCallback)(() => node.getExtInfo(), [node]);
3046
+ const updateExtInfo = (0, import_react2.useCallback)(
3047
+ (data) => {
3048
+ node.updateExtInfo(data);
3049
+ },
3050
+ [node]
3051
+ );
3052
+ const form = (0, import_react2.useMemo)(() => (0, import_node.getNodeForm)(node), [node]);
3053
+ const formState = (0, import_reactive.useObserve)(form?.state);
3054
+ const toggleExpand = (0, import_react2.useCallback)(() => {
3055
+ renderData.toggleExpand();
3056
+ }, [renderData]);
3057
+ return {
3058
+ node,
3059
+ selected: selectionService.isSelected(node.id),
3060
+ activated: selectionService.isActivated(node.id),
3061
+ expanded: renderData.expanded,
3062
+ startDrag,
3063
+ ports: portsData.allPorts,
3064
+ deleteNode,
3065
+ selectNode,
3066
+ readonly,
3067
+ linkingNodeId,
3068
+ nodeRef,
3069
+ onFocus,
3070
+ onBlur,
3071
+ getExtInfo,
3072
+ updateExtInfo,
3073
+ toggleExpand,
3074
+ get form() {
3075
+ if (!form) return void 0;
3076
+ return {
3077
+ ...form,
3078
+ get values() {
3079
+ return form.values;
3080
+ },
3081
+ get state() {
3082
+ return formState;
3083
+ }
3084
+ };
3085
+ }
3086
+ };
3087
+ }
3088
+
3089
+ // src/hooks/use-current-dom-node.ts
3090
+ var import_document11 = require("@flowgram.ai/document");
3091
+ var import_core21 = require("@flowgram.ai/core");
3092
+ function useCurrentDomNode() {
3093
+ const entity = (0, import_core21.useEntityFromContext)();
3094
+ const renderData = entity.getData(import_document11.FlowNodeRenderData);
3095
+ return renderData.node;
3096
+ }
3097
+
3098
+ // src/hooks/use-current-entity.ts
3099
+ var import_core22 = require("@flowgram.ai/core");
3100
+ function useCurrentEntity() {
3101
+ return (0, import_core22.useEntityFromContext)();
3102
+ }
3103
+
3104
+ // src/hooks/use-workflow-document.ts
3105
+ var import_core23 = require("@flowgram.ai/core");
3106
+ function useWorkflowDocument() {
3107
+ return (0, import_core23.useService)(WorkflowDocument);
3108
+ }
3109
+
3110
+ // src/constants.ts
3111
+ var EditorCursorState = /* @__PURE__ */ ((EditorCursorState2) => {
3112
+ EditorCursorState2["GRAB"] = "GRAB";
3113
+ EditorCursorState2["SELECT"] = "SELECT";
3114
+ return EditorCursorState2;
3115
+ })(EditorCursorState || {});
3116
+ var InteractiveType = /* @__PURE__ */ ((InteractiveType2) => {
3117
+ InteractiveType2["MOUSE"] = "MOUSE";
3118
+ InteractiveType2["PAD"] = "PAD";
3119
+ return InteractiveType2;
3120
+ })(InteractiveType || {});
3121
+
3122
+ // src/workflow-document-container-module.ts
3123
+ var import_inversify9 = require("inversify");
3124
+ var import_document13 = require("@flowgram.ai/document");
3125
+ var import_utils19 = require("@flowgram.ai/utils");
3126
+
3127
+ // src/workflow-document-contribution.ts
3128
+ var import_inversify8 = require("inversify");
3129
+ var import_document12 = require("@flowgram.ai/document");
3130
+ var WorkflowDocumentContribution = class {
3131
+ registerDocument(document2) {
3132
+ document2.registerNodeDatas(
3133
+ import_document12.FlowNodeTransformData,
3134
+ import_document12.FlowNodeRenderData,
3135
+ WorkflowNodePortsData,
3136
+ WorkflowNodeLinesData
3137
+ );
3138
+ document2.registerLayout(this.freeLayout);
3139
+ }
3140
+ };
3141
+ __decorateClass([
3142
+ (0, import_inversify8.inject)(FreeLayout)
3143
+ ], WorkflowDocumentContribution.prototype, "freeLayout", 2);
3144
+ WorkflowDocumentContribution = __decorateClass([
3145
+ (0, import_inversify8.injectable)()
3146
+ ], WorkflowDocumentContribution);
3147
+
3148
+ // src/utils/get-url-params.ts
3149
+ function getUrlParams() {
3150
+ return location.search.replace(/^\?/, "").split("&").reduce((res, key) => {
3151
+ const [k, v] = key.split("=");
3152
+ res[k] = v;
3153
+ return res;
3154
+ }, {});
3155
+ }
3156
+
3157
+ // src/workflow-document-container-module.ts
3158
+ var WorkflowDocumentContainerModule = new import_inversify9.ContainerModule(
3159
+ (bind, unbind, isBound, rebind) => {
3160
+ bind(WorkflowDocument).toSelf().inSingletonScope();
3161
+ bind(WorkflowLinesManager).toSelf().inSingletonScope();
3162
+ bind(FreeLayout).toSelf().inSingletonScope();
3163
+ bind(WorkflowDragService).toSelf().inSingletonScope();
3164
+ bind(WorkflowSelectService).toSelf().inSingletonScope();
3165
+ bind(WorkflowHoverService).toSelf().inSingletonScope();
3166
+ bind(WorkflowResetLayoutService).toSelf().inSingletonScope();
3167
+ bind(URLParams).toDynamicValue(() => getUrlParams()).inSingletonScope();
3168
+ (0, import_utils19.bindContributions)(bind, WorkflowDocumentContribution, [import_document13.FlowDocumentContribution]);
3169
+ bind(WorkflowDocumentOptions).toConstantValue({
3170
+ ...WorkflowDocumentOptionsDefault
3171
+ });
3172
+ rebind(import_document13.FlowDocument).toService(WorkflowDocument);
3173
+ bind(WorkflowDocumentProvider).toDynamicValue((ctx) => () => ctx.container.get(WorkflowDocument)).inSingletonScope();
3174
+ }
3175
+ );
3176
+
3177
+ // src/utils/simple-line.ts
3178
+ var import_utils20 = require("@flowgram.ai/utils");
3179
+ var LINE_PADDING = 12;
3180
+ var WorkflowSimpleLineContribution = class {
3181
+ constructor(entity) {
3182
+ this.entity = entity;
3183
+ }
3184
+ get path() {
3185
+ return this.data?.path ?? "";
3186
+ }
3187
+ calcDistance(pos) {
3188
+ if (!this.data) {
3189
+ return Number.MAX_SAFE_INTEGER;
3190
+ }
3191
+ const [start, end] = this.data.points;
3192
+ return import_utils20.Point.getDistance(pos, this.projectPointOnLine(pos, start, end));
3193
+ }
3194
+ get bounds() {
3195
+ if (!this.data) {
3196
+ return new import_utils20.Rectangle();
3197
+ }
3198
+ return this.data.bbox;
3199
+ }
3200
+ update(params) {
3201
+ const { fromPos, toPos } = params;
3202
+ const { vertical } = this.entity;
3203
+ const sourceOffset = {
3204
+ x: vertical ? 0 : POINT_RADIUS,
3205
+ y: vertical ? POINT_RADIUS : 0
3206
+ };
3207
+ const targetOffset = {
3208
+ x: vertical ? 0 : -POINT_RADIUS,
3209
+ y: vertical ? -POINT_RADIUS : 0
3210
+ };
3211
+ const points = [
3212
+ {
3213
+ x: fromPos.x + sourceOffset.x,
3214
+ y: fromPos.y + sourceOffset.y
3215
+ },
3216
+ {
3217
+ x: toPos.x + targetOffset.x,
3218
+ y: toPos.y + targetOffset.y
3219
+ }
3220
+ ];
3221
+ const bbox = import_utils20.Rectangle.createRectangleWithTwoPoints(points[0], points[1]);
3222
+ const adjustedPoints = points.map((p) => ({
3223
+ x: p.x - bbox.x + LINE_PADDING,
3224
+ y: p.y - bbox.y + LINE_PADDING
3225
+ }));
3226
+ const path = `M ${adjustedPoints[0].x} ${adjustedPoints[0].y} L ${adjustedPoints[1].x} ${adjustedPoints[1].y}`;
3227
+ this.data = {
3228
+ points,
3229
+ path,
3230
+ bbox
3231
+ };
3232
+ }
3233
+ projectPointOnLine(point, lineStart, lineEnd) {
3234
+ const dx = lineEnd.x - lineStart.x;
3235
+ const dy = lineEnd.y - lineStart.y;
3236
+ if (dx === 0) {
3237
+ return { x: lineStart.x, y: point.y };
3238
+ }
3239
+ if (dy === 0) {
3240
+ return { x: point.x, y: lineStart.y };
3241
+ }
3242
+ const t = ((point.x - lineStart.x) * dx + (point.y - lineStart.y) * dy) / (dx * dx + dy * dy);
3243
+ const clampedT = Math.max(0, Math.min(1, t));
3244
+ return {
3245
+ x: lineStart.x + clampedT * dx,
3246
+ y: lineStart.y + clampedT * dy
3247
+ };
3248
+ }
3249
+ };
3250
+ WorkflowSimpleLineContribution.type = "WorkflowSimpleLineContribution";
3251
+ // Annotate the CommonJS export names for ESM import in node:
3252
+ 0 && (module.exports = {
3253
+ EditorCursorState,
3254
+ InteractiveType,
3255
+ LINE_HOVER_DISTANCE,
3256
+ LineColors,
3257
+ LineType,
3258
+ POINT_RADIUS,
3259
+ PORT_SIZE,
3260
+ URLParams,
3261
+ WORKFLOW_LINE_ENTITY,
3262
+ WorkflowCommands,
3263
+ WorkflowContentChangeType,
3264
+ WorkflowDocument,
3265
+ WorkflowDocumentContainerModule,
3266
+ WorkflowDocumentOptions,
3267
+ WorkflowDocumentOptionsDefault,
3268
+ WorkflowDocumentProvider,
3269
+ WorkflowDragService,
3270
+ WorkflowHoverService,
3271
+ WorkflowLineEntity,
3272
+ WorkflowLineRenderData,
3273
+ WorkflowLinesManager,
3274
+ WorkflowNodeEntity,
3275
+ WorkflowNodeLinesData,
3276
+ WorkflowNodePortsData,
3277
+ WorkflowPortEntity,
3278
+ WorkflowResetLayoutService,
3279
+ WorkflowSelectService,
3280
+ WorkflowSimpleLineContribution,
3281
+ bindConfigEntity,
3282
+ compose,
3283
+ composeAsync,
3284
+ delay,
3285
+ domReactToBounds,
3286
+ fitView,
3287
+ getAntiOverlapPosition,
3288
+ getPortEntityId,
3289
+ nanoid,
3290
+ useConfigEntity,
3291
+ useCurrentDomNode,
3292
+ useCurrentEntity,
3293
+ useEntities,
3294
+ useEntityDataFromContext,
3295
+ useEntityFromContext,
3296
+ useListenEvents,
3297
+ useNodeRender,
3298
+ usePlayground,
3299
+ usePlaygroundContainer,
3300
+ usePlaygroundContext,
3301
+ usePlaygroundLatest,
3302
+ usePlaygroundReadonlyState,
3303
+ useRefresh,
3304
+ useService,
3305
+ useWorkflowDocument
3306
+ });
3307
+ //# sourceMappingURL=index.js.map