@flowgram.ai/document 0.1.0-alpha.7 → 0.1.0-alpha.9

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
@@ -22,11 +22,17 @@ var FlowNodeBaseType = /* @__PURE__ */ ((FlowNodeBaseType2) => {
22
22
  FlowNodeBaseType2["BLOCK_ORDER_ICON"] = "blockOrderIcon";
23
23
  FlowNodeBaseType2["GROUP"] = "group";
24
24
  FlowNodeBaseType2["END"] = "end";
25
+ FlowNodeBaseType2["BREAK"] = "break";
25
26
  FlowNodeBaseType2["CONDITION"] = "condition";
26
27
  FlowNodeBaseType2["SUB_CANVAS"] = "subCanvas";
28
+ FlowNodeBaseType2["MULTI_INPUTS"] = "multiInputs";
29
+ FlowNodeBaseType2["MULTI_OUTPUTS"] = "multiOutputs";
30
+ FlowNodeBaseType2["INPUT"] = "input";
31
+ FlowNodeBaseType2["OUTPUT"] = "output";
27
32
  return FlowNodeBaseType2;
28
33
  })(FlowNodeBaseType || {});
29
34
  var FlowNodeSplitType = /* @__PURE__ */ ((FlowNodeSplitType2) => {
35
+ FlowNodeSplitType2["SIMPLE_SPLIT"] = "simpleSplit";
30
36
  FlowNodeSplitType2["DYNAMIC_SPLIT"] = "dynamicSplit";
31
37
  FlowNodeSplitType2["STATIC_SPLIT"] = "staticSplit";
32
38
  return FlowNodeSplitType2;
@@ -170,13 +176,37 @@ var DEFAULT_FLOW_NODE_META = (nodeType, document) => {
170
176
  };
171
177
  var FlowNodeRegistry;
172
178
  ((FlowNodeRegistry4) => {
179
+ function mergeChildRegistries(r1 = [], r2 = []) {
180
+ if (r1.length === 0 || r2.length === 0) {
181
+ return [...r1, ...r2];
182
+ }
183
+ const r1Filter = r1.map((r1Current) => {
184
+ const r2Current = r2.find((n) => n.type === r1Current.type);
185
+ if (r2Current) {
186
+ return merge(r1Current, r2Current, r1Current.type);
187
+ }
188
+ return r1Current;
189
+ });
190
+ const r2Filter = r2.filter((n) => !r1.some((r) => r.type === n.type));
191
+ return [...r1Filter, ...r2Filter];
192
+ }
193
+ FlowNodeRegistry4.mergeChildRegistries = mergeChildRegistries;
173
194
  function merge(registry1, registry2, finalType) {
195
+ const extendKeys = registry1.__extends__ ? registry1.__extends__.slice() : [];
196
+ if (registry1.type !== registry2.type) {
197
+ extendKeys.unshift(registry1.type);
198
+ }
174
199
  return {
175
200
  ...registry1,
176
201
  ...registry2,
202
+ extendChildRegistries: mergeChildRegistries(
203
+ registry1.extendChildRegistries,
204
+ registry2.extendChildRegistries
205
+ ),
177
206
  meta: { ...registry1.meta, ...registry2.meta },
178
207
  extend: void 0,
179
- type: finalType
208
+ type: finalType,
209
+ __extends__: extendKeys
180
210
  };
181
211
  }
182
212
  FlowNodeRegistry4.merge = merge;
@@ -1389,13 +1419,15 @@ var FlowVirtualTree = class _FlowVirtualTree {
1389
1419
  get size() {
1390
1420
  return this.map.size;
1391
1421
  }
1392
- toString() {
1422
+ toString(showType) {
1393
1423
  const ret = [];
1394
1424
  this.traverse((node, depth) => {
1395
1425
  if (depth === 0) {
1396
1426
  ret.push(node.id);
1397
1427
  } else {
1398
- ret.push(`|${new Array(depth).fill("--").join("")} ${node.id}`);
1428
+ ret.push(
1429
+ `|${new Array(depth).fill("--").join("")} ${showType ? `${node.flowNodeType}(${node.id})` : node.id}`
1430
+ );
1399
1431
  }
1400
1432
  });
1401
1433
  return `${ret.join("\n")}`;
@@ -1661,6 +1693,13 @@ var FlowDocument = class {
1661
1693
  this.onNodeCreate = this.onNodeCreateEmitter.event;
1662
1694
  this.onNodeDispose = this.onNodeDisposeEmitter.event;
1663
1695
  this.onLayoutChange = this.onLayoutChangeEmitter.event;
1696
+ this._disposed = false;
1697
+ }
1698
+ /**
1699
+ *
1700
+ */
1701
+ get disposed() {
1702
+ return this._disposed;
1664
1703
  }
1665
1704
  init() {
1666
1705
  if (!this.options) this.options = FlowDocumentOptionsDefault;
@@ -1686,6 +1725,7 @@ var FlowDocument = class {
1686
1725
  * @param fireRender 是否要触发渲染,默认 true
1687
1726
  */
1688
1727
  fromJSON(json, fireRender = true) {
1728
+ if (this._disposed) return;
1689
1729
  this.originTree.clear();
1690
1730
  this.renderTree.clear();
1691
1731
  this.entityManager.changeEntityLocked = true;
@@ -1754,7 +1794,7 @@ var FlowDocument = class {
1754
1794
  * @param data
1755
1795
  * @param addedNodes
1756
1796
  */
1757
- addNode(data, addedNodes, ignoreCreateEvent) {
1797
+ addNode(data, addedNodes) {
1758
1798
  const { id, type = "block", originParent, parent, meta, hidden, index } = data;
1759
1799
  let node = this.getNode(id);
1760
1800
  let isNew = false;
@@ -1775,10 +1815,10 @@ var FlowDocument = class {
1775
1815
  const datas = dataRegistries ? this.nodeDataRegistries.concat(...dataRegistries) : this.nodeDataRegistries;
1776
1816
  node.addInitializeData(datas);
1777
1817
  node.onDispose(() => this.onNodeDisposeEmitter.fire({ node }));
1778
- if (this.options.fromNodeJSON) {
1779
- this.options.fromNodeJSON(node, data);
1780
- }
1818
+ this.options.fromNodeJSON?.(node, data, true);
1781
1819
  isNew = true;
1820
+ } else {
1821
+ this.options.fromNodeJSON?.(node, data, false);
1782
1822
  }
1783
1823
  node.initData({
1784
1824
  originParent,
@@ -1790,7 +1830,6 @@ var FlowDocument = class {
1790
1830
  if (node.isStart) {
1791
1831
  this.root.addChild(node);
1792
1832
  }
1793
- this.onNodeUpdateEmitter.fire({ node, data });
1794
1833
  addedNodes?.push(node);
1795
1834
  if (register.onCreate) {
1796
1835
  const extendNodes = register.onCreate(node, data);
@@ -1804,11 +1843,14 @@ var FlowDocument = class {
1804
1843
  this.addBlocksAsChildren(node, data.blocks, addedNodes);
1805
1844
  }
1806
1845
  }
1807
- if (isNew && !ignoreCreateEvent) {
1846
+ if (isNew) {
1808
1847
  this.onNodeCreateEmitter.fire({
1809
1848
  node,
1810
- data
1849
+ data,
1850
+ json: data
1811
1851
  });
1852
+ } else {
1853
+ this.onNodeUpdateEmitter.fire({ node, data, json: data });
1812
1854
  }
1813
1855
  return node;
1814
1856
  }
@@ -1844,18 +1886,16 @@ var FlowDocument = class {
1844
1886
  parent: node
1845
1887
  });
1846
1888
  addedNodes.push(blockIconNode);
1847
- if (blocks.length > 0) {
1848
- const inlineBlocksNode = this.addNode({
1849
- id: `$inlineBlocks$${node.id}`,
1850
- type: "inlineBlocks" /* INLINE_BLOCKS */,
1851
- originParent: node,
1852
- parent: node
1853
- });
1854
- addedNodes.push(inlineBlocksNode);
1855
- blocks.forEach((blockData) => {
1856
- this.addBlock(node, blockData, addedNodes);
1857
- });
1858
- }
1889
+ const inlineBlocksNode = this.addNode({
1890
+ id: `$inlineBlocks$${node.id}`,
1891
+ type: "inlineBlocks" /* INLINE_BLOCKS */,
1892
+ originParent: node,
1893
+ parent: node
1894
+ });
1895
+ addedNodes.push(inlineBlocksNode);
1896
+ blocks.forEach((blockData) => {
1897
+ this.addBlock(node, blockData, addedNodes);
1898
+ });
1859
1899
  return addedNodes;
1860
1900
  }
1861
1901
  /**
@@ -1920,10 +1960,22 @@ var FlowDocument = class {
1920
1960
  meta: {
1921
1961
  ...preRegistry?.meta,
1922
1962
  ...newRegistry?.meta
1923
- }
1963
+ },
1964
+ extendChildRegistries: FlowNodeRegistry.mergeChildRegistries(
1965
+ preRegistry?.extendChildRegistries,
1966
+ newRegistry?.extendChildRegistries
1967
+ )
1924
1968
  });
1925
1969
  });
1926
1970
  }
1971
+ /**
1972
+ * Check node extend
1973
+ * @param currentType
1974
+ * @param parentType
1975
+ */
1976
+ isExtend(currentType, parentType) {
1977
+ return (this.getNodeRegistry(currentType).__extends__ || []).includes(parentType);
1978
+ }
1927
1979
  /**
1928
1980
  * 导出数据,可以重载
1929
1981
  */
@@ -1947,6 +1999,7 @@ var FlowDocument = class {
1947
1999
  const customDefaultRegistry = this.options.getNodeDefaultRegistry?.(type);
1948
2000
  let register = this.registers.get(type) || { type };
1949
2001
  const extendRegisters = [];
2002
+ const extendKey = register.extend;
1950
2003
  if (register.extend && this.registers.has(register.extend)) {
1951
2004
  register = FlowNodeRegistry.merge(
1952
2005
  this.getNodeRegistry(register.extend),
@@ -1977,6 +2030,9 @@ var FlowDocument = class {
1977
2030
  ...register.meta
1978
2031
  }
1979
2032
  };
2033
+ if (extendKey) {
2034
+ res.extend = extendKey;
2035
+ }
1980
2036
  this.nodeRegistryCache.set(typeKey, res);
1981
2037
  return res;
1982
2038
  }
@@ -2027,8 +2083,8 @@ var FlowDocument = class {
2027
2083
  getAllNodes() {
2028
2084
  return this.entityManager.getEntities(FlowNodeEntity);
2029
2085
  }
2030
- toString() {
2031
- return this.originTree.toString();
2086
+ toString(showType) {
2087
+ return this.originTree.toString(showType);
2032
2088
  }
2033
2089
  /**
2034
2090
  * 返回需要渲染的数据
@@ -2123,6 +2179,7 @@ var FlowDocument = class {
2123
2179
  );
2124
2180
  }
2125
2181
  dispose() {
2182
+ if (this._disposed) return;
2126
2183
  this.registers.clear();
2127
2184
  this.nodeRegistryCache.clear();
2128
2185
  this.originTree.dispose();
@@ -2131,6 +2188,7 @@ var FlowDocument = class {
2131
2188
  this.onNodeCreateEmitter.dispose();
2132
2189
  this.onNodeDisposeEmitter.dispose();
2133
2190
  this.onLayoutChangeEmitter.dispose();
2191
+ this._disposed = true;
2134
2192
  }
2135
2193
  };
2136
2194
  __decorateClass([
@@ -2158,135 +2216,427 @@ FlowDocument = __decorateClass([
2158
2216
  import { ContainerModule } from "inversify";
2159
2217
 
2160
2218
  // src/services/flow-drag-service.ts
2161
- import { inject as inject5, injectable as injectable5 } from "inversify";
2162
- import { EntityManager as EntityManager4 } from "@flowgram.ai/core";
2163
- import { Emitter as Emitter7 } from "@flowgram.ai/utils";
2164
-
2165
- // src/services/flow-operation-base-service.ts
2166
- import { inject as inject3, injectable as injectable3, postConstruct as postConstruct2 } from "inversify";
2167
- import { DisposableCollection, Emitter as Emitter6 } from "@flowgram.ai/utils";
2219
+ import { inject as inject3, injectable as injectable3 } from "inversify";
2220
+ import { Emitter as Emitter6 } from "@flowgram.ai/utils";
2168
2221
  import { EntityManager as EntityManager2 } from "@flowgram.ai/core";
2169
- var FlowOperationBaseServiceImpl = class {
2170
- constructor() {
2171
- this.onNodeAddEmitter = new Emitter6();
2172
- this.onNodeAdd = this.onNodeAddEmitter.event;
2173
- this.toDispose = new DisposableCollection();
2174
- this.onNodeMoveEmitter = new Emitter6();
2175
- this.onNodeMove = this.onNodeMoveEmitter.event;
2222
+
2223
+ // src/services/flow-group-service/flow-group-controller.ts
2224
+ var FlowGroupController = class _FlowGroupController {
2225
+ constructor(groupNode) {
2226
+ this.groupNode = groupNode;
2176
2227
  }
2177
- init() {
2178
- this.toDispose.pushAll([this.onNodeAddEmitter, this.onNodeMoveEmitter]);
2228
+ get nodes() {
2229
+ return this.groupNode.collapsedChildren || [];
2179
2230
  }
2180
- addNode(nodeJSON, config = {}) {
2181
- const { parent, index, hidden } = config;
2182
- let parentEntity;
2183
- if (parent) {
2184
- parentEntity = this.toNodeEntity(parent);
2231
+ get collapsed() {
2232
+ const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2233
+ return groupTransformData.collapsed;
2234
+ }
2235
+ collapse() {
2236
+ this.collapsed = true;
2237
+ }
2238
+ expand() {
2239
+ this.collapsed = false;
2240
+ }
2241
+ /** 获取分组外围的最大边框 */
2242
+ get bounds() {
2243
+ const groupNodeBounds = this.groupNode.getData(FlowNodeTransformData).bounds;
2244
+ return groupNodeBounds;
2245
+ }
2246
+ /** 是否是开始节点 */
2247
+ isStartNode(node) {
2248
+ if (!node) {
2249
+ return false;
2185
2250
  }
2186
- let register;
2187
- if (parentEntity) {
2188
- register = parentEntity.getNodeRegistry();
2251
+ const nodes = this.nodes;
2252
+ if (!nodes[0]) {
2253
+ return false;
2189
2254
  }
2190
- const addJSON = {
2191
- ...nodeJSON,
2192
- type: nodeJSON.type || "block" /* BLOCK */
2193
- };
2194
- const addNodeData = {
2195
- ...addJSON,
2196
- parent: parentEntity,
2197
- index,
2198
- hidden
2199
- };
2200
- let added;
2201
- if (parentEntity && register?.addChild) {
2202
- added = register.addChild(parentEntity, addJSON, {
2203
- index,
2204
- hidden
2205
- });
2206
- } else {
2207
- added = this.document.addNode(addNodeData);
2255
+ return node.id === nodes[0].id;
2256
+ }
2257
+ /** 是否是结束节点 */
2258
+ isEndNode(node) {
2259
+ if (!node) {
2260
+ return false;
2208
2261
  }
2209
- this.onNodeAddEmitter.fire({
2210
- node: added,
2211
- data: addNodeData
2212
- });
2213
- return added;
2262
+ const nodes = this.nodes;
2263
+ if (!nodes[nodes.length - 1]) {
2264
+ return false;
2265
+ }
2266
+ return node.id === nodes[nodes.length - 1].id;
2214
2267
  }
2215
- addFromNode(fromNode, nodeJSON) {
2216
- return this.document.addFromNode(fromNode, nodeJSON);
2268
+ set note(note) {
2269
+ this.groupNode.getNodeMeta().note = note;
2217
2270
  }
2218
- deleteNode(node) {
2219
- this.document.removeNode(node);
2271
+ get note() {
2272
+ return this.groupNode.getNodeMeta().note || "";
2220
2273
  }
2221
- deleteNodes(nodes) {
2222
- (nodes || []).forEach((node) => {
2223
- this.deleteNode(node);
2224
- });
2274
+ set noteHeight(height) {
2275
+ this.groupNode.getNodeMeta().noteHeight = height;
2225
2276
  }
2226
- addBlock(target, blockJSON, config = {}) {
2227
- const { parent, index } = config;
2228
- return this.document.addBlock(target, blockJSON, void 0, parent, index);
2277
+ get noteHeight() {
2278
+ return this.groupNode.getNodeMeta().noteHeight || 0;
2229
2279
  }
2230
- moveNode(node, config = {}) {
2231
- const { parent: newParent, index } = config;
2232
- const entity = this.toNodeEntity(node);
2233
- const parent = entity?.parent;
2234
- if (!parent) {
2235
- return;
2280
+ get positionConfig() {
2281
+ return this.groupNode.getNodeMeta().positionConfig;
2282
+ }
2283
+ set collapsed(collapsed) {
2284
+ const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2285
+ groupTransformData.collapsed = collapsed;
2286
+ groupTransformData.localDirty = true;
2287
+ if (groupTransformData.parent) groupTransformData.parent.localDirty = true;
2288
+ if (groupTransformData.parent?.firstChild)
2289
+ groupTransformData.parent.firstChild.localDirty = true;
2290
+ }
2291
+ set hovered(hovered) {
2292
+ const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2293
+ if (hovered) {
2294
+ groupRenderData.toggleMouseEnter();
2295
+ } else {
2296
+ groupRenderData.toggleMouseLeave();
2236
2297
  }
2237
- const newParentEntity = newParent ? this.toNodeEntity(newParent) : parent;
2238
- if (!newParentEntity) {
2239
- console.warn("no new parent found", newParent);
2298
+ if (groupRenderData.hovered === hovered) {
2240
2299
  return;
2241
2300
  }
2242
- let toIndex = typeof index === "undefined" ? newParentEntity.collapsedChildren.length : index;
2243
- return this.doMoveNode(entity, newParentEntity, toIndex);
2301
+ groupRenderData.hovered = hovered;
2244
2302
  }
2245
- /**
2246
- * 拖拽节点
2247
- * @param param0
2248
- * @returns
2249
- */
2250
- dragNodes({ dropNode, nodes }) {
2251
- if (nodes.length === 0) {
2303
+ get hovered() {
2304
+ const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2305
+ return groupRenderData.hovered;
2306
+ }
2307
+ static create(groupNode) {
2308
+ if (!groupNode) {
2252
2309
  return;
2253
2310
  }
2254
- const startNode = nodes[0];
2255
- const fromParent = startNode.parent;
2256
- const toParent = dropNode.parent;
2257
- if (!fromParent || !toParent) {
2311
+ if (!FlowGroupUtils.isGroupNode(groupNode)) {
2258
2312
  return;
2259
2313
  }
2260
- const fromIndex = fromParent.children.findIndex((child) => child === startNode);
2261
- const dropIndex = toParent.children.findIndex((child) => child === dropNode);
2262
- let toIndex = dropIndex + 1;
2263
- if (fromParent === toParent && fromIndex < dropIndex) {
2264
- toIndex = toIndex - nodes.length;
2314
+ return new _FlowGroupController(groupNode);
2315
+ }
2316
+ };
2317
+
2318
+ // src/services/flow-group-service/flow-group-utils.ts
2319
+ var FlowGroupUtils;
2320
+ ((FlowGroupUtils2) => {
2321
+ const findNodeParents = (node) => {
2322
+ const parents = [];
2323
+ let parent = node.parent;
2324
+ while (parent) {
2325
+ parents.push(parent);
2326
+ parent = parent.parent;
2265
2327
  }
2266
- const value = {
2267
- nodeIds: nodes.map((node) => node.id),
2268
- fromParentId: fromParent.id,
2269
- toParentId: toParent.id,
2270
- fromIndex,
2271
- toIndex
2272
- };
2273
- return this.apply({
2274
- type: "moveChildNodes" /* moveChildNodes */,
2275
- value
2328
+ return parents;
2329
+ };
2330
+ const isNodeInGroup = (node) => {
2331
+ if (node?.parent?.flowNodeType === "group" /* GROUP */) {
2332
+ return true;
2333
+ }
2334
+ return false;
2335
+ };
2336
+ FlowGroupUtils2.validate = (nodes) => {
2337
+ if (!nodes || !Array.isArray(nodes) || nodes.length === 0) {
2338
+ return false;
2339
+ }
2340
+ const isGroupRelatedNode = nodes.some((node) => (0, FlowGroupUtils2.isGroupNode)(node));
2341
+ if (isGroupRelatedNode) return false;
2342
+ const hasGroup = nodes.some((node) => node && isNodeInGroup(node));
2343
+ if (hasGroup) return false;
2344
+ const parent = nodes[0].parent;
2345
+ const isSameParent = nodes.every((node) => node.parent === parent);
2346
+ if (!isSameParent) return false;
2347
+ const indexes = nodes.map((node) => node.index).sort((a, b) => a - b);
2348
+ const isIndexContinuous = indexes.every((index, i, arr) => {
2349
+ if (i === 0) {
2350
+ return true;
2351
+ }
2352
+ return index === arr[i - 1] + 1;
2276
2353
  });
2354
+ if (!isIndexContinuous) return false;
2355
+ const parents = findNodeParents(nodes[0]);
2356
+ const parentsInGroup = parents.some((parent2) => isNodeInGroup(parent2));
2357
+ if (parentsInGroup) return false;
2358
+ return true;
2359
+ };
2360
+ FlowGroupUtils2.getNodeGroupController = (node) => {
2361
+ if (!node) {
2362
+ return;
2363
+ }
2364
+ if (!isNodeInGroup(node)) {
2365
+ return;
2366
+ }
2367
+ const groupNode = node?.parent;
2368
+ return FlowGroupController.create(groupNode);
2369
+ };
2370
+ FlowGroupUtils2.getNodeRecursionGroupController = (node) => {
2371
+ if (!node) {
2372
+ return;
2373
+ }
2374
+ const group = (0, FlowGroupUtils2.getNodeGroupController)(node);
2375
+ if (group) {
2376
+ return group;
2377
+ }
2378
+ if (node.parent) {
2379
+ return (0, FlowGroupUtils2.getNodeRecursionGroupController)(node.parent);
2380
+ }
2381
+ return;
2382
+ };
2383
+ FlowGroupUtils2.isGroupNode = (group) => group.flowNodeType === "group" /* GROUP */;
2384
+ })(FlowGroupUtils || (FlowGroupUtils = {}));
2385
+
2386
+ // src/services/flow-drag-service.ts
2387
+ var FlowDragService = class {
2388
+ constructor() {
2389
+ this.onDropEmitter = new Emitter6();
2390
+ this.onDrop = this.onDropEmitter.event;
2277
2391
  }
2278
- /**
2279
- * 执行操作
2280
- * @param operation 可序列化的操作
2281
- * @returns 操作返回
2282
- */
2283
- apply(operation) {
2284
- const document = this.document;
2285
- switch (operation.type) {
2286
- case "addFromNode" /* addFromNode */:
2287
- return document.addFromNode(operation.value.fromId, operation.value.data);
2288
- case "deleteFromNode" /* deleteFromNode */:
2289
- return document.getNode(operation.value?.data?.id)?.dispose();
2392
+ get renderState() {
2393
+ return this.document.renderState;
2394
+ }
2395
+ // 拖拽所有节点中的首个节点
2396
+ get dragStartNode() {
2397
+ return this.renderState.getDragStartEntity();
2398
+ }
2399
+ // 拖拽的所有节点
2400
+ get dragNodes() {
2401
+ return this.renderState.getDragEntities();
2402
+ }
2403
+ // 放置的区域
2404
+ get dropNodeId() {
2405
+ return this.renderState.getNodeDroppingId();
2406
+ }
2407
+ // 是否在拖拽分支
2408
+ get isDragBranch() {
2409
+ return this.dragStartNode?.isInlineBlock;
2410
+ }
2411
+ // 拖拽的所有节点及其自节点
2412
+ get nodeDragIdsWithChildren() {
2413
+ return this.renderState.config.nodeDragIdsWithChildren || [];
2414
+ }
2415
+ get dragging() {
2416
+ const renderData = this.dragStartNode?.getData(FlowNodeRenderData);
2417
+ return !!renderData?.dragging;
2418
+ }
2419
+ get labelSide() {
2420
+ return this.renderState.config.dragLabelSide;
2421
+ }
2422
+ /**
2423
+ * 放置到目标分支
2424
+ */
2425
+ dropBranch() {
2426
+ this.dropNode();
2427
+ }
2428
+ /**
2429
+ * 移动到目标节点
2430
+ */
2431
+ dropNode() {
2432
+ const dropEntity = this.document.getNode(this.dropNodeId);
2433
+ if (!dropEntity) {
2434
+ return;
2435
+ }
2436
+ const sortNodes = [];
2437
+ let curr = this.dragStartNode;
2438
+ while (curr && this.dragNodes.includes(curr)) {
2439
+ sortNodes.push(curr);
2440
+ curr = curr.next;
2441
+ }
2442
+ this.operationService.dragNodes({
2443
+ dropNode: dropEntity,
2444
+ nodes: sortNodes
2445
+ });
2446
+ if (sortNodes.length > 0) {
2447
+ this.onDropEmitter.fire({
2448
+ dropNode: dropEntity,
2449
+ dragNodes: sortNodes
2450
+ });
2451
+ }
2452
+ }
2453
+ /**
2454
+ * 拖拽是否可以释放在该节点后面
2455
+ */
2456
+ isDroppableNode(node) {
2457
+ if (!this.dragging || this.isDragBranch) {
2458
+ return false;
2459
+ }
2460
+ if (this.nodeDragIdsWithChildren.includes(node.id) || node.next && this.nodeDragIdsWithChildren.includes(node.next.id)) {
2461
+ return false;
2462
+ }
2463
+ if (node.isInlineBlocks || node.isInlineBlock) {
2464
+ return false;
2465
+ }
2466
+ const hasGroupNode = this.dragNodes.some(
2467
+ (node2) => node2.flowNodeType === "group" /* GROUP */
2468
+ );
2469
+ if (hasGroupNode) {
2470
+ const group = FlowGroupUtils.getNodeRecursionGroupController(node);
2471
+ if (group) {
2472
+ return false;
2473
+ }
2474
+ }
2475
+ return true;
2476
+ }
2477
+ /**
2478
+ * 拖拽分支是否可以释放在该分支
2479
+ * @param node 拖拽的分支节点
2480
+ * @param side 分支的前面还是后面
2481
+ */
2482
+ isDroppableBranch(node, side = "normal_branch" /* NORMAL_BRANCH */) {
2483
+ if (this.isDragBranch) {
2484
+ if (
2485
+ // 拖拽到分支
2486
+ !node.isInlineBlock || // 只能在同一分支条件下
2487
+ node.parent !== this.dragStartNode.parent || // 自己不能拖拽给自己
2488
+ node === this.dragStartNode
2489
+ ) {
2490
+ return false;
2491
+ }
2492
+ if (side === "normal_branch" /* NORMAL_BRANCH */ && node.next !== this.dragStartNode) {
2493
+ return true;
2494
+ }
2495
+ if (side === "pre_branch" /* PRE_BRANCH */ && node.pre !== this.dragStartNode) {
2496
+ return true;
2497
+ }
2498
+ }
2499
+ return false;
2500
+ }
2501
+ };
2502
+ __decorateClass([
2503
+ inject3(FlowDocument)
2504
+ ], FlowDragService.prototype, "document", 2);
2505
+ __decorateClass([
2506
+ inject3(FlowOperationBaseService)
2507
+ ], FlowDragService.prototype, "operationService", 2);
2508
+ __decorateClass([
2509
+ inject3(EntityManager2)
2510
+ ], FlowDragService.prototype, "entityManager", 2);
2511
+ FlowDragService = __decorateClass([
2512
+ injectable3()
2513
+ ], FlowDragService);
2514
+
2515
+ // src/services/flow-operation-base-service.ts
2516
+ import { inject as inject4, injectable as injectable4, postConstruct as postConstruct2 } from "inversify";
2517
+ import { DisposableCollection, Emitter as Emitter7 } from "@flowgram.ai/utils";
2518
+ import { EntityManager as EntityManager3 } from "@flowgram.ai/core";
2519
+ var FlowOperationBaseServiceImpl = class {
2520
+ constructor() {
2521
+ this.onNodeAddEmitter = new Emitter7();
2522
+ this.onNodeAdd = this.onNodeAddEmitter.event;
2523
+ this.toDispose = new DisposableCollection();
2524
+ this.onNodeMoveEmitter = new Emitter7();
2525
+ this.onNodeMove = this.onNodeMoveEmitter.event;
2526
+ }
2527
+ init() {
2528
+ this.toDispose.pushAll([this.onNodeAddEmitter, this.onNodeMoveEmitter]);
2529
+ }
2530
+ addNode(nodeJSON, config = {}) {
2531
+ const { parent, index, hidden } = config;
2532
+ let parentEntity;
2533
+ if (parent) {
2534
+ parentEntity = this.toNodeEntity(parent);
2535
+ }
2536
+ let register;
2537
+ if (parentEntity) {
2538
+ register = parentEntity.getNodeRegistry();
2539
+ }
2540
+ const addJSON = {
2541
+ ...nodeJSON,
2542
+ type: nodeJSON.type || "block" /* BLOCK */
2543
+ };
2544
+ const addNodeData = {
2545
+ ...addJSON,
2546
+ parent: parentEntity,
2547
+ index,
2548
+ hidden
2549
+ };
2550
+ let added;
2551
+ if (parentEntity && register?.addChild) {
2552
+ added = register.addChild(parentEntity, addJSON, {
2553
+ index,
2554
+ hidden
2555
+ });
2556
+ } else {
2557
+ added = this.document.addNode(addNodeData);
2558
+ }
2559
+ this.onNodeAddEmitter.fire({
2560
+ node: added,
2561
+ data: addNodeData
2562
+ });
2563
+ return added;
2564
+ }
2565
+ addFromNode(fromNode, nodeJSON) {
2566
+ return this.document.addFromNode(fromNode, nodeJSON);
2567
+ }
2568
+ deleteNode(node) {
2569
+ this.document.removeNode(node);
2570
+ }
2571
+ deleteNodes(nodes) {
2572
+ (nodes || []).forEach((node) => {
2573
+ this.deleteNode(node);
2574
+ });
2575
+ }
2576
+ addBlock(target, blockJSON, config = {}) {
2577
+ const { parent, index } = config;
2578
+ return this.document.addBlock(target, blockJSON, void 0, parent, index);
2579
+ }
2580
+ moveNode(node, config = {}) {
2581
+ const { parent: newParent, index } = config;
2582
+ const entity = this.toNodeEntity(node);
2583
+ const parent = entity?.parent;
2584
+ if (!parent) {
2585
+ return;
2586
+ }
2587
+ const newParentEntity = newParent ? this.toNodeEntity(newParent) : parent;
2588
+ if (!newParentEntity) {
2589
+ console.warn("no new parent found", newParent);
2590
+ return;
2591
+ }
2592
+ let toIndex = typeof index === "undefined" ? newParentEntity.collapsedChildren.length : index;
2593
+ return this.doMoveNode(entity, newParentEntity, toIndex);
2594
+ }
2595
+ /**
2596
+ * 拖拽节点
2597
+ * @param param0
2598
+ * @returns
2599
+ */
2600
+ dragNodes({ dropNode, nodes }) {
2601
+ if (nodes.length === 0) {
2602
+ return;
2603
+ }
2604
+ const startNode = nodes[0];
2605
+ const fromParent = startNode.parent;
2606
+ const toParent = dropNode.parent;
2607
+ if (!fromParent || !toParent) {
2608
+ return;
2609
+ }
2610
+ const fromIndex = fromParent.children.findIndex((child) => child === startNode);
2611
+ const dropIndex = toParent.children.findIndex((child) => child === dropNode);
2612
+ let toIndex = dropIndex + 1;
2613
+ if (fromParent === toParent && fromIndex < dropIndex) {
2614
+ toIndex = toIndex - nodes.length;
2615
+ }
2616
+ const value = {
2617
+ nodeIds: nodes.map((node) => node.id),
2618
+ fromParentId: fromParent.id,
2619
+ toParentId: toParent.id,
2620
+ fromIndex,
2621
+ toIndex
2622
+ };
2623
+ return this.apply({
2624
+ type: "moveChildNodes" /* moveChildNodes */,
2625
+ value
2626
+ });
2627
+ }
2628
+ /**
2629
+ * 执行操作
2630
+ * @param operation 可序列化的操作
2631
+ * @returns 操作返回
2632
+ */
2633
+ apply(operation) {
2634
+ const document = this.document;
2635
+ switch (operation.type) {
2636
+ case "addFromNode" /* addFromNode */:
2637
+ return document.addFromNode(operation.value.fromId, operation.value.data);
2638
+ case "deleteFromNode" /* deleteFromNode */:
2639
+ return document.getNode(operation.value?.data?.id)?.dispose();
2290
2640
  case "addBlock" /* addBlock */: {
2291
2641
  let parent;
2292
2642
  if (operation.value.parentId) {
@@ -2411,29 +2761,29 @@ var FlowOperationBaseServiceImpl = class {
2411
2761
  }
2412
2762
  };
2413
2763
  __decorateClass([
2414
- inject3(EntityManager2)
2764
+ inject4(EntityManager3)
2415
2765
  ], FlowOperationBaseServiceImpl.prototype, "entityManager", 2);
2416
2766
  __decorateClass([
2417
- inject3(FlowDocument)
2767
+ inject4(FlowDocument)
2418
2768
  ], FlowOperationBaseServiceImpl.prototype, "document", 2);
2419
2769
  __decorateClass([
2420
2770
  postConstruct2()
2421
2771
  ], FlowOperationBaseServiceImpl.prototype, "init", 1);
2422
2772
  FlowOperationBaseServiceImpl = __decorateClass([
2423
- injectable3()
2773
+ injectable4()
2424
2774
  ], FlowOperationBaseServiceImpl);
2425
2775
 
2426
- // src/services/flow-group-service.ts
2776
+ // src/services/flow-group-service/flow-group-service.ts
2427
2777
  import { nanoid } from "nanoid";
2428
- import { inject as inject4, injectable as injectable4 } from "inversify";
2429
- import { EntityManager as EntityManager3 } from "@flowgram.ai/core";
2778
+ import { inject as inject5, injectable as injectable5 } from "inversify";
2779
+ import { EntityManager as EntityManager4 } from "@flowgram.ai/core";
2430
2780
  var FlowGroupService = class {
2431
2781
  /** 创建分组节点 */
2432
2782
  createGroup(nodes) {
2433
2783
  if (!nodes || !Array.isArray(nodes) || nodes.length === 0) {
2434
2784
  return;
2435
2785
  }
2436
- if (!FlowGroupController.validate(nodes)) {
2786
+ if (!FlowGroupUtils.validate(nodes)) {
2437
2787
  return;
2438
2788
  }
2439
2789
  const sortedNodes = nodes.sort((a, b) => a.index - b.index);
@@ -2503,309 +2853,18 @@ var FlowGroupService = class {
2503
2853
  return FlowGroupController.create(group);
2504
2854
  }
2505
2855
  static validate(nodes) {
2506
- return FlowGroupController.validate(nodes);
2856
+ return FlowGroupUtils.validate(nodes);
2507
2857
  }
2508
2858
  };
2509
2859
  __decorateClass([
2510
- inject4(EntityManager3)
2860
+ inject5(EntityManager4)
2511
2861
  ], FlowGroupService.prototype, "entityManager", 2);
2512
2862
  __decorateClass([
2513
- inject4(FlowOperationBaseService)
2863
+ inject5(FlowOperationBaseService)
2514
2864
  ], FlowGroupService.prototype, "operationService", 2);
2515
2865
  FlowGroupService = __decorateClass([
2516
- injectable4()
2866
+ injectable5()
2517
2867
  ], FlowGroupService);
2518
- var FlowGroupController = class _FlowGroupController {
2519
- constructor(groupNode) {
2520
- this.groupNode = groupNode;
2521
- }
2522
- get nodes() {
2523
- return this.groupNode.collapsedChildren || [];
2524
- }
2525
- get collapsed() {
2526
- const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2527
- return groupTransformData.collapsed;
2528
- }
2529
- collapse() {
2530
- this.collapsed = true;
2531
- }
2532
- expand() {
2533
- this.collapsed = false;
2534
- }
2535
- /** 获取分组外围的最大边框 */
2536
- get bounds() {
2537
- const groupNodeBounds = this.groupNode.getData(FlowNodeTransformData).bounds;
2538
- return groupNodeBounds;
2539
- }
2540
- /** 是否是开始节点 */
2541
- isStartNode(node) {
2542
- if (!node) {
2543
- return false;
2544
- }
2545
- const nodes = this.nodes;
2546
- if (!nodes[0]) {
2547
- return false;
2548
- }
2549
- return node.id === nodes[0].id;
2550
- }
2551
- /** 是否是结束节点 */
2552
- isEndNode(node) {
2553
- if (!node) {
2554
- return false;
2555
- }
2556
- const nodes = this.nodes;
2557
- if (!nodes[nodes.length - 1]) {
2558
- return false;
2559
- }
2560
- return node.id === nodes[nodes.length - 1].id;
2561
- }
2562
- set note(note) {
2563
- this.groupNode.getNodeMeta().note = note;
2564
- }
2565
- get note() {
2566
- return this.groupNode.getNodeMeta().note || "";
2567
- }
2568
- set noteHeight(height) {
2569
- this.groupNode.getNodeMeta().noteHeight = height;
2570
- }
2571
- get noteHeight() {
2572
- return this.groupNode.getNodeMeta().noteHeight || 0;
2573
- }
2574
- get positionConfig() {
2575
- return this.groupNode.getNodeMeta().positionConfig;
2576
- }
2577
- set collapsed(collapsed) {
2578
- const groupTransformData = this.groupNode.getData(FlowNodeTransformData);
2579
- groupTransformData.collapsed = collapsed;
2580
- groupTransformData.localDirty = true;
2581
- if (groupTransformData.parent) groupTransformData.parent.localDirty = true;
2582
- if (groupTransformData.parent?.firstChild)
2583
- groupTransformData.parent.firstChild.localDirty = true;
2584
- }
2585
- set hovered(hovered) {
2586
- const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2587
- if (hovered) {
2588
- groupRenderData.toggleMouseEnter();
2589
- } else {
2590
- groupRenderData.toggleMouseLeave();
2591
- }
2592
- if (groupRenderData.hovered === hovered) {
2593
- return;
2594
- }
2595
- groupRenderData.hovered = hovered;
2596
- }
2597
- get hovered() {
2598
- const groupRenderData = this.groupNode.getData(FlowNodeRenderData);
2599
- return groupRenderData.hovered;
2600
- }
2601
- static create(groupNode) {
2602
- if (!groupNode) {
2603
- return;
2604
- }
2605
- if (!_FlowGroupController.isGroupNode(groupNode)) {
2606
- return;
2607
- }
2608
- return new _FlowGroupController(groupNode);
2609
- }
2610
- /** 判断节点能否组成分组 */
2611
- static validate(nodes) {
2612
- if (!nodes || !Array.isArray(nodes) || nodes.length === 0) {
2613
- return false;
2614
- }
2615
- const isGroupRelatedNode = nodes.some((node) => _FlowGroupController.isGroupNode(node));
2616
- if (isGroupRelatedNode) return false;
2617
- const hasGroup = nodes.some((node) => node && this.isNodeInGroup(node));
2618
- if (hasGroup) return false;
2619
- const parent = nodes[0].parent;
2620
- const isSameParent = nodes.every((node) => node.parent === parent);
2621
- if (!isSameParent) return false;
2622
- const indexes = nodes.map((node) => node.index).sort((a, b) => a - b);
2623
- const isIndexContinuous = indexes.every((index, i, arr) => {
2624
- if (i === 0) {
2625
- return true;
2626
- }
2627
- return index === arr[i - 1] + 1;
2628
- });
2629
- if (!isIndexContinuous) return false;
2630
- const parents = this.findNodeParents(nodes[0]);
2631
- const parentsInGroup = parents.some((parent2) => this.isNodeInGroup(parent2));
2632
- if (parentsInGroup) return false;
2633
- return true;
2634
- }
2635
- /** 获取节点分组控制 */
2636
- static getNodeGroupController(node) {
2637
- if (!node) {
2638
- return;
2639
- }
2640
- if (!this.isNodeInGroup(node)) {
2641
- return;
2642
- }
2643
- const groupNode = node?.parent;
2644
- return _FlowGroupController.create(groupNode);
2645
- }
2646
- /** 向上递归查找分组递归控制 */
2647
- static getNodeRecursionGroupController(node) {
2648
- if (!node) {
2649
- return;
2650
- }
2651
- const group = this.getNodeGroupController(node);
2652
- if (group) {
2653
- return group;
2654
- }
2655
- if (node.parent) {
2656
- return this.getNodeRecursionGroupController(node.parent);
2657
- }
2658
- return;
2659
- }
2660
- /** 是否分组节点 */
2661
- static isGroupNode(group) {
2662
- return group.flowNodeType === "group" /* GROUP */;
2663
- }
2664
- /** 找到节点所有上级 */
2665
- static findNodeParents(node) {
2666
- const parents = [];
2667
- let parent = node.parent;
2668
- while (parent) {
2669
- parents.push(parent);
2670
- parent = parent.parent;
2671
- }
2672
- return parents;
2673
- }
2674
- /** 节点是否处于分组中 */
2675
- static isNodeInGroup(node) {
2676
- if (node?.parent?.flowNodeType === "group" /* GROUP */) {
2677
- return true;
2678
- }
2679
- return false;
2680
- }
2681
- };
2682
-
2683
- // src/services/flow-drag-service.ts
2684
- var FlowDragService = class {
2685
- constructor() {
2686
- this.onDropEmitter = new Emitter7();
2687
- this.onDrop = this.onDropEmitter.event;
2688
- }
2689
- get renderState() {
2690
- return this.document.renderState;
2691
- }
2692
- // 拖拽所有节点中的首个节点
2693
- get dragStartNode() {
2694
- return this.renderState.getDragStartEntity();
2695
- }
2696
- // 拖拽的所有节点
2697
- get dragNodes() {
2698
- return this.renderState.getDragEntities();
2699
- }
2700
- // 放置的区域
2701
- get dropNodeId() {
2702
- return this.renderState.getNodeDroppingId();
2703
- }
2704
- // 是否在拖拽分支
2705
- get isDragBranch() {
2706
- return this.dragStartNode?.isInlineBlock;
2707
- }
2708
- // 拖拽的所有节点及其自节点
2709
- get nodeDragIdsWithChildren() {
2710
- return this.renderState.config.nodeDragIdsWithChildren || [];
2711
- }
2712
- get dragging() {
2713
- const renderData = this.dragStartNode?.getData(FlowNodeRenderData);
2714
- return !!renderData?.dragging;
2715
- }
2716
- get labelSide() {
2717
- return this.renderState.config.dragLabelSide;
2718
- }
2719
- /**
2720
- * 放置到目标分支
2721
- */
2722
- dropBranch() {
2723
- this.dropNode();
2724
- }
2725
- /**
2726
- * 移动到目标节点
2727
- */
2728
- dropNode() {
2729
- const dropEntity = this.document.getNode(this.dropNodeId);
2730
- if (!dropEntity) {
2731
- return;
2732
- }
2733
- const sortNodes = [];
2734
- let curr = this.dragStartNode;
2735
- while (curr && this.dragNodes.includes(curr)) {
2736
- sortNodes.push(curr);
2737
- curr = curr.next;
2738
- }
2739
- this.operationService.dragNodes({
2740
- dropNode: dropEntity,
2741
- nodes: sortNodes
2742
- });
2743
- if (sortNodes.length > 0) {
2744
- this.onDropEmitter.fire({
2745
- dropNode: dropEntity,
2746
- dragNodes: sortNodes
2747
- });
2748
- }
2749
- }
2750
- /**
2751
- * 拖拽是否可以释放在该节点后面
2752
- */
2753
- isDroppableNode(node) {
2754
- if (!this.dragging || this.isDragBranch) {
2755
- return false;
2756
- }
2757
- if (this.nodeDragIdsWithChildren.includes(node.id) || node.next && this.nodeDragIdsWithChildren.includes(node.next.id)) {
2758
- return false;
2759
- }
2760
- if (node.isInlineBlocks || node.isInlineBlock) {
2761
- return false;
2762
- }
2763
- const hasGroupNode = this.dragNodes.some((node2) => node2.flowNodeType === "group" /* GROUP */);
2764
- if (hasGroupNode) {
2765
- const group = FlowGroupController.getNodeRecursionGroupController(node);
2766
- if (group) {
2767
- return false;
2768
- }
2769
- }
2770
- return true;
2771
- }
2772
- /**
2773
- * 拖拽分支是否可以释放在该分支
2774
- * @param node 拖拽的分支节点
2775
- * @param side 分支的前面还是后面
2776
- */
2777
- isDroppableBranch(node, side = "normal_branch" /* NORMAL_BRANCH */) {
2778
- if (this.isDragBranch) {
2779
- if (
2780
- // 拖拽到分支
2781
- !node.isInlineBlock || // 只能在同一分支条件下
2782
- node.parent !== this.dragStartNode.parent || // 自己不能拖拽给自己
2783
- node === this.dragStartNode
2784
- ) {
2785
- return false;
2786
- }
2787
- if (side === "normal_branch" /* NORMAL_BRANCH */ && node.next !== this.dragStartNode) {
2788
- return true;
2789
- }
2790
- if (side === "pre_branch" /* PRE_BRANCH */ && node.pre !== this.dragStartNode) {
2791
- return true;
2792
- }
2793
- }
2794
- return false;
2795
- }
2796
- };
2797
- __decorateClass([
2798
- inject5(FlowDocument)
2799
- ], FlowDragService.prototype, "document", 2);
2800
- __decorateClass([
2801
- inject5(FlowOperationBaseService)
2802
- ], FlowDragService.prototype, "operationService", 2);
2803
- __decorateClass([
2804
- inject5(EntityManager4)
2805
- ], FlowDragService.prototype, "entityManager", 2);
2806
- FlowDragService = __decorateClass([
2807
- injectable5()
2808
- ], FlowDragService);
2809
2868
 
2810
2869
  // src/layout/vertical-fixed-layout.ts
2811
2870
  import { injectable as injectable6, inject as inject6, multiInject as multiInject2, optional as optional3 } from "inversify";