@flowgram.ai/free-layout-core 0.1.0-alpha.5 → 0.1.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -247,10 +247,17 @@ var WorkflowPortEntity = class extends Entity {
247
247
  }
248
248
  /**
249
249
  * 当前点位上连接的线条
250
+ * @deprecated use `availableLines` instead
250
251
  */
251
252
  get lines() {
252
253
  return this.allLines.filter((line) => !line.isDrawing);
253
254
  }
255
+ /**
256
+ * 当前有效的线条,不包含正在画的线条和隐藏的线条(这个出现在线条重连会先把原来的线条隐藏)
257
+ */
258
+ get availableLines() {
259
+ return this.allLines.filter((line) => !line.isDrawing && !line.isHidden);
260
+ }
254
261
  /**
255
262
  * 当前点位上连接的线条(包含 isDrawing === true 的线条)
256
263
  */
@@ -475,7 +482,7 @@ var _WorkflowNodeLinesData = class _WorkflowNodeLinesData extends EntityData2 {
475
482
  constructor(entity) {
476
483
  super(entity);
477
484
  this.entity = entity;
478
- this.toDispose.push(
485
+ this.entity.preDispose.push(
479
486
  Disposable.create(() => {
480
487
  this.inputLines.slice().forEach((line) => line.dispose());
481
488
  this.outputLines.slice().forEach((line) => line.dispose());
@@ -1694,10 +1701,12 @@ var WorkflowDocument = class extends FlowDocument {
1694
1701
  * @param json
1695
1702
  */
1696
1703
  fromJSON(json, fireRender = true) {
1697
- const { flattenJSON, nodeBlocks, nodeEdges } = this.flatJSON(json);
1698
- const nestedJSON = this.nestJSON(flattenJSON, nodeBlocks, nodeEdges);
1704
+ const workflowJSON = {
1705
+ nodes: json.nodes ?? [],
1706
+ edges: json.edges ?? []
1707
+ };
1699
1708
  this.entityManager.changeEntityLocked = true;
1700
- this.renderJSON(nestedJSON);
1709
+ this.renderJSON(workflowJSON);
1701
1710
  this.entityManager.changeEntityLocked = false;
1702
1711
  this.transformer.loading = false;
1703
1712
  if (fireRender) {
@@ -2035,112 +2044,17 @@ var WorkflowDocument = class extends FlowDocument {
2035
2044
  this.disposed = true;
2036
2045
  this._onReloadEmitter.dispose();
2037
2046
  }
2038
- getEdgeID(edge) {
2039
- return WorkflowLineEntity.portInfoToLineId({
2040
- from: edge.sourceNodeID,
2041
- to: edge.targetNodeID,
2042
- fromPort: edge.sourcePortID,
2043
- toPort: edge.targetPortID
2044
- });
2045
- }
2046
- /**
2047
- * 拍平树形json结构,将结构信息提取到map
2048
- */
2049
- flatJSON(json = { nodes: [], edges: [] }) {
2050
- const nodeBlocks = /* @__PURE__ */ new Map();
2051
- const nodeEdges = /* @__PURE__ */ new Map();
2052
- const rootNodes = json.nodes ?? [];
2053
- const rootEdges = json.edges ?? [];
2054
- const flattenNodeJSONs = [...rootNodes];
2055
- const flattenEdgeJSONs = [...rootEdges];
2056
- const rootBlockIDs = rootNodes.map((node) => node.id);
2057
- const rootEdgeIDs = rootEdges.map((edge) => this.getEdgeID(edge));
2058
- nodeBlocks.set(FlowNodeBaseType.ROOT, rootBlockIDs);
2059
- nodeEdges.set(FlowNodeBaseType.ROOT, rootEdgeIDs);
2060
- rootNodes.forEach((nodeJSON) => {
2061
- const { blocks, edges } = nodeJSON;
2062
- if (blocks) {
2063
- flattenNodeJSONs.push(...blocks);
2064
- const blockIDs = [];
2065
- blocks.forEach((block) => {
2066
- blockIDs.push(block.id);
2067
- });
2068
- nodeBlocks.set(nodeJSON.id, blockIDs);
2069
- delete nodeJSON.blocks;
2070
- }
2071
- if (edges) {
2072
- flattenEdgeJSONs.push(...edges);
2073
- const edgeIDs = [];
2074
- edges.forEach((edge) => {
2075
- const edgeID = this.getEdgeID(edge);
2076
- edgeIDs.push(edgeID);
2077
- });
2078
- nodeEdges.set(nodeJSON.id, edgeIDs);
2079
- delete nodeJSON.edges;
2080
- }
2081
- });
2082
- const flattenJSON = {
2083
- nodes: flattenNodeJSONs,
2084
- edges: flattenEdgeJSONs
2085
- };
2086
- return {
2087
- flattenJSON,
2088
- nodeBlocks,
2089
- nodeEdges
2090
- };
2091
- }
2092
- /**
2093
- * 对JSON进行分层
2094
- */
2095
- nestJSON(flattenJSON, nodeBlocks, nodeEdges) {
2096
- const nestJSON = {
2097
- nodes: [],
2098
- edges: []
2099
- };
2100
- const nodeMap = /* @__PURE__ */ new Map();
2101
- const edgeMap = /* @__PURE__ */ new Map();
2102
- const rootBlockSet = new Set(nodeBlocks.get(FlowNodeBaseType.ROOT) ?? []);
2103
- const rootEdgeSet = new Set(nodeEdges.get(FlowNodeBaseType.ROOT) ?? []);
2104
- flattenJSON.nodes.forEach((nodeJSON) => {
2105
- nodeMap.set(nodeJSON.id, nodeJSON);
2106
- });
2107
- flattenJSON.edges.forEach((edgeJSON) => {
2108
- const edgeID = this.getEdgeID(edgeJSON);
2109
- edgeMap.set(edgeID, edgeJSON);
2110
- });
2111
- flattenJSON.nodes.forEach((nodeJSON) => {
2112
- if (rootBlockSet.has(nodeJSON.id)) {
2113
- nestJSON.nodes.push(nodeJSON);
2114
- }
2115
- if (nodeBlocks.has(nodeJSON.id)) {
2116
- const blockIDs = nodeBlocks.get(nodeJSON.id);
2117
- const blockJSONs = blockIDs.map((blockID) => nodeMap.get(blockID)).filter(Boolean);
2118
- nodeJSON.blocks = blockJSONs;
2119
- }
2120
- if (nodeEdges.has(nodeJSON.id)) {
2121
- const edgeIDs = nodeEdges.get(nodeJSON.id);
2122
- const edgeJSONs = edgeIDs.map((edgeID) => edgeMap.get(edgeID)).filter(Boolean);
2123
- nodeJSON.edges = edgeJSONs;
2124
- }
2125
- });
2126
- flattenJSON.edges.forEach((edgeJSON) => {
2127
- const edgeID = this.getEdgeID(edgeJSON);
2128
- if (rootEdgeSet.has(edgeID)) {
2129
- nestJSON.edges.push(edgeJSON);
2130
- }
2131
- });
2132
- return nestJSON;
2133
- }
2134
2047
  /**
2135
2048
  * 逐层创建节点和线条
2136
2049
  */
2137
2050
  renderJSON(json, options) {
2138
2051
  const { parent = this.root, isClone = false } = options ?? {};
2139
2052
  const containerID = this.getNodeSubCanvas(parent)?.canvasNode.id ?? parent.id;
2140
- json.nodes.forEach((nodeJSON) => {
2141
- this.createWorkflowNode(nodeJSON, isClone, containerID);
2142
- }), // 创建线条
2143
- json.edges.forEach((edge) => this.createWorkflowLine(edge, containerID));
2053
+ const nodes = json.nodes.map(
2054
+ (nodeJSON) => this.createWorkflowNode(nodeJSON, isClone, containerID)
2055
+ );
2056
+ const edges = json.edges.map((edge) => this.createWorkflowLine(edge, containerID)).filter(Boolean);
2057
+ return { nodes, edges };
2144
2058
  }
2145
2059
  getNodeSubCanvas(node) {
2146
2060
  if (!node) return;
@@ -2160,7 +2074,7 @@ var WorkflowDocument = class extends FlowDocument {
2160
2074
  }
2161
2075
  toLineJSON(line) {
2162
2076
  const lineJSON = line.toJSON();
2163
- if (!line.to || !line.info.to || !line.toPort) {
2077
+ if (!line.from || !line.info.from || !line.fromPort || !line.to || !line.info.to || !line.toPort) {
2164
2078
  return;
2165
2079
  }
2166
2080
  const fromSubCanvas = this.getNodeSubCanvas(line.from);
@@ -2280,7 +2194,7 @@ var WorkflowDragService = class {
2280
2194
  * 拖拽选中节点
2281
2195
  * @param triggerEvent
2282
2196
  */
2283
- startDragSelectedNodes(triggerEvent) {
2197
+ async startDragSelectedNodes(triggerEvent) {
2284
2198
  let { selectedNodes } = this.selectService;
2285
2199
  if (selectedNodes.length === 0 || this.playgroundConfig.readonly || this.playgroundConfig.disabled || this.isDragging) {
2286
2200
  return Promise.resolve(false);
@@ -2290,7 +2204,6 @@ var WorkflowDragService = class {
2290
2204
  if (sameParent && sameParent.flowNodeType !== FlowNodeBaseType2.ROOT) {
2291
2205
  selectedNodes = [sameParent];
2292
2206
  }
2293
- const { altKey } = triggerEvent;
2294
2207
  let startPosition = this.getNodesPosition(selectedNodes);
2295
2208
  let startPositions = selectedNodes.map((node) => {
2296
2209
  const transform = node.getData(TransformData9);
@@ -2304,7 +2217,6 @@ var WorkflowDragService = class {
2304
2217
  type: "onDragStart",
2305
2218
  nodes: selectedNodes,
2306
2219
  startPositions,
2307
- altKey,
2308
2220
  dragEvent,
2309
2221
  triggerEvent,
2310
2222
  dragger
@@ -2313,25 +2225,6 @@ var WorkflowDragService = class {
2313
2225
  onDrag: (dragEvent) => {
2314
2226
  if (!dragSuccess && checkDragSuccess(Date.now() - startTime, dragEvent)) {
2315
2227
  dragSuccess = true;
2316
- if (altKey) {
2317
- const tryCopyNodes = selectedNodes;
2318
- if (tryCopyNodes.length > 0) {
2319
- this.selectService.clear();
2320
- this.commandService.executeCommand("PASTE_NODES" /* PASTE_NODES */, tryCopyNodes, true).then((newNodes) => {
2321
- if (newNodes && Array.isArray(newNodes) && newNodes.length > 0) {
2322
- selectedNodes = newNodes;
2323
- startPosition = this.getNodesPosition(tryCopyNodes);
2324
- startPositions = tryCopyNodes.filter((n) => !n.getNodeMeta().copyDisable).map((node) => {
2325
- const transform = node.getData(TransformData9);
2326
- return {
2327
- x: transform.position.x,
2328
- y: transform.position.y
2329
- };
2330
- });
2331
- }
2332
- });
2333
- }
2334
- }
2335
2228
  }
2336
2229
  const offset = this.getDragPosOffset({
2337
2230
  event: dragEvent,
@@ -2362,7 +2255,6 @@ var WorkflowDragService = class {
2362
2255
  nodes: selectedNodes,
2363
2256
  startPositions,
2364
2257
  positions,
2365
- altKey,
2366
2258
  dragEvent,
2367
2259
  triggerEvent,
2368
2260
  dragger
@@ -2374,7 +2266,6 @@ var WorkflowDragService = class {
2374
2266
  type: "onDragEnd",
2375
2267
  nodes: selectedNodes,
2376
2268
  startPositions,
2377
- altKey,
2378
2269
  dragEvent,
2379
2270
  triggerEvent,
2380
2271
  dragger
@@ -2457,10 +2348,7 @@ var WorkflowDragService = class {
2457
2348
  dragNodeType: type,
2458
2349
  dropNode
2459
2350
  });
2460
- if (!allowDrop) {
2461
- return this.clearDrop();
2462
- }
2463
- const dragNode = await this.dropCard(type, e, data, dropNode);
2351
+ const dragNode = allowDrop ? await this.dropCard(type, e, data, dropNode) : void 0;
2464
2352
  this.clearDrop();
2465
2353
  if (dragNode) {
2466
2354
  domNode.remove();
@@ -2709,7 +2597,10 @@ var WorkflowDragService = class {
2709
2597
  this.setLineColor(line, this.linesManager.lineColor.drawing);
2710
2598
  if (toNode && !this.isContainer(toNode)) {
2711
2599
  const portsData = toNode.getData(WorkflowNodePortsData);
2712
- toPort = portsData.inputPorts[0];
2600
+ const { inputPorts } = portsData;
2601
+ if (inputPorts.length === 1) {
2602
+ toPort = inputPorts[0];
2603
+ }
2713
2604
  const { hasError } = this.handleDragOnNode(toNode, fromPort, line, toPort, originLine);
2714
2605
  lineErrorReset = hasError;
2715
2606
  }