@logicflow/layout 2.1.0-alpha.2 → 2.1.0-alpha.4

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 (47) hide show
  1. package/.turbo/turbo-build.log +7 -6
  2. package/CHANGELOG.md +15 -0
  3. package/README.md +9 -2
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/es/{dagre.d.ts → dagre/index.d.ts} +18 -20
  7. package/es/dagre/index.js +126 -0
  8. package/es/dagre/index.js.map +1 -0
  9. package/es/elkLayout/config.d.ts +26 -0
  10. package/es/elkLayout/config.js +27 -0
  11. package/es/elkLayout/config.js.map +1 -0
  12. package/es/elkLayout/index.d.ts +107 -0
  13. package/es/elkLayout/index.js +187 -0
  14. package/es/elkLayout/index.js.map +1 -0
  15. package/es/index.d.ts +1 -0
  16. package/es/index.js +1 -0
  17. package/es/index.js.map +1 -1
  18. package/es/utils/processEdge.d.ts +3 -0
  19. package/es/utils/processEdge.js +479 -0
  20. package/es/utils/processEdge.js.map +1 -0
  21. package/lib/{dagre.d.ts → dagre/index.d.ts} +18 -20
  22. package/lib/dagre/index.js +152 -0
  23. package/lib/dagre/index.js.map +1 -0
  24. package/lib/elkLayout/config.d.ts +26 -0
  25. package/lib/elkLayout/config.js +30 -0
  26. package/lib/elkLayout/config.js.map +1 -0
  27. package/lib/elkLayout/index.d.ts +107 -0
  28. package/lib/elkLayout/index.js +193 -0
  29. package/lib/elkLayout/index.js.map +1 -0
  30. package/lib/index.d.ts +1 -0
  31. package/lib/index.js +1 -0
  32. package/lib/index.js.map +1 -1
  33. package/lib/utils/processEdge.d.ts +3 -0
  34. package/lib/utils/processEdge.js +483 -0
  35. package/lib/utils/processEdge.js.map +1 -0
  36. package/package.json +3 -2
  37. package/src/dagre/index.ts +177 -0
  38. package/src/elkLayout/config.ts +26 -0
  39. package/src/elkLayout/index.ts +255 -0
  40. package/src/index.ts +2 -0
  41. package/src/utils/processEdge.ts +585 -0
  42. package/stats.html +1 -1
  43. package/es/dagre.js +0 -376
  44. package/es/dagre.js.map +0 -1
  45. package/lib/dagre.js +0 -402
  46. package/lib/dagre.js.map +0 -1
  47. package/src/dagre.ts +0 -438
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.Dagre = void 0;
38
+ var dagre_1 = __importStar(require("dagre"));
39
+ var processEdge_1 = require("../utils/processEdge");
40
+ /**
41
+ * Dagre布局类 - LogicFlow自动布局插件
42
+ * 基于dagre.js提供图的自动布局能力
43
+ */
44
+ var Dagre = /** @class */ (function () {
45
+ function Dagre() {
46
+ }
47
+ /**
48
+ * 插件初始化方法,由LogicFlow自动调用
49
+ * @param lf - LogicFlow实例
50
+ */
51
+ Dagre.prototype.render = function (lf) {
52
+ this.lf = lf;
53
+ };
54
+ /**
55
+ * 执行布局算法,重新排列图中的节点和边
56
+ * @param option - 布局配置选项
57
+ */
58
+ Dagre.prototype.layout = function (option) {
59
+ if (option === void 0) { option = {}; }
60
+ var _a = this.lf.graphModel, nodes = _a.nodes, edges = _a.edges, gridSize = _a.gridSize;
61
+ // 根据网格大小调整节点间距
62
+ var nodesep = 100;
63
+ var ranksep = 150;
64
+ if (gridSize > 20) {
65
+ nodesep = gridSize * 2;
66
+ ranksep = gridSize * 2;
67
+ }
68
+ // 合并默认配置和用户配置
69
+ this.option = __assign({
70
+ // 默认从左到右布局
71
+ rankdir: 'LR',
72
+ // 默认右下角对齐
73
+ align: 'UL',
74
+ // 紧凑树形排名算法
75
+ ranker: 'tight-tree',
76
+ // 层级间距
77
+ ranksep: ranksep,
78
+ // 同层节点间距
79
+ nodesep: nodesep,
80
+ // 图的水平边距
81
+ marginx: 120,
82
+ // 图的垂直边距
83
+ marginy: 120 }, option);
84
+ this.applyDagreLayout(nodes, edges);
85
+ };
86
+ /**
87
+ * 使用 Dagre 布局
88
+ * @param nodes - 节点数据
89
+ * @param edges - 边数据
90
+ */
91
+ Dagre.prototype.applyDagreLayout = function (nodes, edges) {
92
+ // 创建dagre图实例
93
+ var g = new dagre_1.graphlib.Graph();
94
+ // dagre布局配置
95
+ g.setGraph(this.option);
96
+ //构造dagre布局数据
97
+ g.setDefaultEdgeLabel(function () { return ({}); });
98
+ nodes.forEach(function (node) {
99
+ g.setNode(node.id, {
100
+ width: node.width || 150,
101
+ height: node.height || 50,
102
+ id: node.id,
103
+ });
104
+ });
105
+ edges.forEach(function (edge) {
106
+ g.setEdge(edge.sourceNodeId, edge.targetNodeId, {
107
+ id: edge.id,
108
+ });
109
+ });
110
+ // 开始dagre布局
111
+ try {
112
+ dagre_1.default.layout(g);
113
+ var newGraphData = this.convertLayoutDataToLf(nodes, edges, g);
114
+ this.lf.renderRawData(newGraphData);
115
+ }
116
+ catch (error) {
117
+ console.error('Dagre layout error:', error);
118
+ }
119
+ };
120
+ Dagre.prototype.convertLayoutDataToLf = function (nodes, edges, layoutData) {
121
+ // 存储新的节点和边数据
122
+ var newNodes = [];
123
+ // 更新节点位置
124
+ nodes.forEach(function (nodeModel) {
125
+ var _a;
126
+ var lfNode = nodeModel.getData();
127
+ var newNode = layoutData.node(nodeModel.id);
128
+ if (!lfNode || !newNode) {
129
+ throw new Error("\u5E03\u5C40\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230ID\u4E3A ".concat(nodeModel.id, " \u7684\u8282\u70B9"));
130
+ }
131
+ // 更新节点坐标
132
+ lfNode.x = newNode.x;
133
+ lfNode.y = newNode.y;
134
+ // 更新节点文本位置
135
+ if ((_a = lfNode === null || lfNode === void 0 ? void 0 : lfNode.text) === null || _a === void 0 ? void 0 : _a.x) {
136
+ lfNode.text.x = newNode.x;
137
+ lfNode.text.y = newNode.y;
138
+ }
139
+ newNodes.push(lfNode);
140
+ });
141
+ var newEdges = (0, processEdge_1.processEdges)(this.lf, this.option.rankdir, this.option.isDefaultAnchor, edges, newNodes);
142
+ return {
143
+ nodes: newNodes,
144
+ edges: newEdges,
145
+ };
146
+ };
147
+ /** 插件名称,用于在LogicFlow中注册 */
148
+ Dagre.pluginName = 'dagre';
149
+ return Dagre;
150
+ }());
151
+ exports.Dagre = Dagre;
152
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/dagre/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA,6CAAmD;AACnD,oDAAmD;AAoCnD;;;GAGG;AACH;IAAA;IAgIA,CAAC;IAtHC;;;OAGG;IACH,sBAAM,GAAN,UAAO,EAAa;QAClB,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AM,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAA;YAEpB,WAAW;YACX,IAAI,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,0CAAE,CAAC,EAAE,CAAC;gBACpB,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAA;gBACzB,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAA;YAC3B,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACvB,CAAC,CAAC,CAAA;QAEF,IAAM,QAAQ,GAAiB,IAAA,0BAAY,EACzC,IAAI,CAAC,EAAE,EACP,IAAI,CAAC,MAAM,CAAC,OAAO,EACnB,IAAI,CAAC,MAAM,CAAC,eAAe,EAC3B,KAAK,EACL,QAAQ,CACT,CAAA;QAED,OAAO;YACL,KAAK,EAAE,QAAQ;YACf,KAAK,EAAE,QAAQ;SAChB,CAAA;IACH,CAAC;IA9HD,2BAA2B;IACpB,gBAAU,GAAG,OAAO,CAAA;IA8H7B,YAAC;CAAA,AAhID,IAgIC;AAhIY,sBAAK"}
@@ -0,0 +1,26 @@
1
+ export declare const elkOptionMap: {
2
+ rankdir: {
3
+ LR: string;
4
+ TB: string;
5
+ BT: string;
6
+ RL: string;
7
+ default: string;
8
+ };
9
+ align: {
10
+ UL: string;
11
+ UR: string;
12
+ DL: string;
13
+ DR: string;
14
+ default: string;
15
+ };
16
+ ranker: {
17
+ 'network-simplex': string;
18
+ 'tight-tree': string;
19
+ 'longest-path': string;
20
+ default: string;
21
+ };
22
+ acyclicer: {
23
+ greedy: string;
24
+ default: string;
25
+ };
26
+ };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.elkOptionMap = void 0;
4
+ exports.elkOptionMap = {
5
+ rankdir: {
6
+ LR: 'RIGHT',
7
+ TB: 'DOWN',
8
+ BT: 'UP',
9
+ RL: 'LEFT',
10
+ default: 'RIGHT',
11
+ },
12
+ align: {
13
+ UL: 'RIGHTDOWN',
14
+ UR: 'RIGHTUP',
15
+ DL: 'LEFTDOWN',
16
+ DR: 'LEFTUP',
17
+ default: 'BALANCED',
18
+ },
19
+ ranker: {
20
+ 'network-simplex': 'NETWORK_SIMPLEX',
21
+ 'tight-tree': 'NETWORK_SIMPLEX',
22
+ 'longest-path': 'LONGEST_PATH',
23
+ default: 'NETWORK_SIMPLEX',
24
+ },
25
+ acyclicer: {
26
+ greedy: 'GREEDY',
27
+ default: 'DEPTH_FIRST',
28
+ },
29
+ };
30
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/elkLayout/config.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC1B,OAAO,EAAE;QACP,EAAE,EAAE,OAAO;QACX,EAAE,EAAE,MAAM;QACV,EAAE,EAAE,IAAI;QACR,EAAE,EAAE,MAAM;QACV,OAAO,EAAE,OAAO;KACjB;IACD,KAAK,EAAE;QACL,EAAE,EAAE,WAAW;QACf,EAAE,EAAE,SAAS;QACb,EAAE,EAAE,UAAU;QACd,EAAE,EAAE,QAAQ;QACZ,OAAO,EAAE,UAAU;KACpB;IACD,MAAM,EAAE;QACN,iBAAiB,EAAE,iBAAiB;QACpC,YAAY,EAAE,iBAAiB;QAC/B,cAAc,EAAE,cAAc;QAC9B,OAAO,EAAE,iBAAiB;KAC3B;IACD,SAAS,EAAE;QACT,MAAM,EAAE,QAAQ;QAChB,OAAO,EAAE,aAAa;KACvB;CACF,CAAA"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * @fileoverview ElkLayout布局插件 - 提供自动化图形布局功能
3
+ *
4
+ * 本插件基于elkjs实现LogicFlow的自动化布局功能,支持多种布局方向
5
+ * 可自动计算节点位置和连线路径,实现整洁的图形展示
6
+ */
7
+ import LogicFlow, { BaseNodeModel, BaseEdgeModel } from '@logicflow/core';
8
+ import { LayoutOptions, ElkNode } from 'elkjs/lib/elk-api';
9
+ /**
10
+ * ElkLayout布局配置选项接口
11
+ * @interface ElkLayoutOption
12
+ */
13
+ export interface ElkLayoutOption {
14
+ /**
15
+ * 布局方向
16
+ * 'LR' | 'TB' | 'BT' | 'RL'
17
+ */
18
+ rankdir?: 'LR' | 'TB' | 'BT' | 'RL';
19
+ /**
20
+ * 对齐方式
21
+ * 'UL' | 'UR' | 'DL' | 'DR'
22
+ */
23
+ align?: 'UL' | 'UR' | 'DL' | 'DR';
24
+ /**
25
+ * 同层节点间距
26
+ */
27
+ nodesep?: number;
28
+ /**
29
+ * 层级间距
30
+ */
31
+ ranksep?: number;
32
+ /**
33
+ * 图的水平边距
34
+ */
35
+ marginx?: number;
36
+ /**
37
+ * 图的垂直边距
38
+ */
39
+ marginy?: number;
40
+ /**
41
+ * 排版算法
42
+ * 'network-simplex' | 'tight-tree' | 'longest-path'
43
+ */
44
+ ranker?: 'network-simplex' | 'tight-tree' | 'longest-path';
45
+ /**
46
+ * 边间距
47
+ */
48
+ edgesep?: number;
49
+ /**
50
+ * 有向无环图处理算法
51
+ * 'greedy'
52
+ */
53
+ acyclicer?: 'greedy';
54
+ /**
55
+ * 是否是默认锚点
56
+ * true: 会根据布局方向自动计算边的路径点
57
+ */
58
+ isDefaultAnchor?: boolean;
59
+ /**
60
+ * ELK 原生布局属性,用于覆盖默认配置
61
+ */
62
+ elkOption?: LayoutOptions;
63
+ }
64
+ /**
65
+ * ElkLayout插件接口定义
66
+ */
67
+ export interface ElkLayoutPlugin {
68
+ /**
69
+ * 执行布局计算
70
+ * @param option - 布局配置选项
71
+ */
72
+ layout(option: ElkLayoutOption): void;
73
+ }
74
+ /**
75
+ * ElkLayout布局类 - LogicFlow自动布局插件
76
+ * 基于elkjs提供图的自动布局能力
77
+ */
78
+ export declare class ElkLayout {
79
+ /** 插件名称,用于在LogicFlow中注册 */
80
+ static pluginName: string;
81
+ /** LogicFlow实例引用 */
82
+ lf: LogicFlow;
83
+ /** 当前布局配置 */
84
+ option: ElkLayoutOption;
85
+ /**
86
+ * 插件初始化方法,由LogicFlow自动调用
87
+ * @param lf - LogicFlow实例
88
+ */
89
+ render(lf: LogicFlow): void;
90
+ /**
91
+ * 执行布局算法,重新排列图中的节点和边
92
+ * @param option - 布局配置选项
93
+ */
94
+ layout(option?: ElkLayoutOption): void;
95
+ /**
96
+ * 使用 ELK 布局
97
+ * @param nodes - 节点数据
98
+ * @param edges - 边数据
99
+ * @param elkOption - ELK 配置选项
100
+ */
101
+ applyElkLayout(nodes: BaseNodeModel[], edges: BaseEdgeModel[]): Promise<void>;
102
+ convertOptionsToElk(): LayoutOptions;
103
+ convertLayoutDataToLf(nodes: BaseNodeModel[], edges: BaseEdgeModel[], layoutData: ElkNode): {
104
+ nodes: LogicFlow.NodeConfig<LogicFlow.PropertiesType>[];
105
+ edges: LogicFlow.EdgeConfig<LogicFlow.PropertiesType>[];
106
+ };
107
+ }
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.ElkLayout = void 0;
54
+ var elk_bundled_1 = __importDefault(require("elkjs/lib/elk.bundled"));
55
+ var processEdge_1 = require("../utils/processEdge");
56
+ var config_1 = require("./config");
57
+ /**
58
+ * ElkLayout布局类 - LogicFlow自动布局插件
59
+ * 基于elkjs提供图的自动布局能力
60
+ */
61
+ var ElkLayout = /** @class */ (function () {
62
+ function ElkLayout() {
63
+ }
64
+ /**
65
+ * 插件初始化方法,由LogicFlow自动调用
66
+ * @param lf - LogicFlow实例
67
+ */
68
+ ElkLayout.prototype.render = function (lf) {
69
+ this.lf = lf;
70
+ };
71
+ /**
72
+ * 执行布局算法,重新排列图中的节点和边
73
+ * @param option - 布局配置选项
74
+ */
75
+ ElkLayout.prototype.layout = function (option) {
76
+ if (option === void 0) { option = {}; }
77
+ var _a = this.lf.graphModel, nodes = _a.nodes, edges = _a.edges, gridSize = _a.gridSize;
78
+ // 根据网格大小调整节点间距
79
+ var nodesep = 100;
80
+ var ranksep = 150;
81
+ if (gridSize > 20) {
82
+ nodesep = gridSize * 2;
83
+ ranksep = gridSize * 2;
84
+ }
85
+ // 合并默认配置和用户配置
86
+ this.option = __assign({
87
+ // 默认从左到右布局
88
+ rankdir: 'LR',
89
+ // 默认右下角对齐
90
+ align: 'UL',
91
+ // 紧凑树形排名算法
92
+ ranker: 'tight-tree',
93
+ // 层级间距
94
+ ranksep: ranksep,
95
+ // 同层节点间距
96
+ nodesep: nodesep,
97
+ // 图的水平边距
98
+ marginx: 120,
99
+ // 图的垂直边距
100
+ marginy: 120 }, option);
101
+ this.applyElkLayout(nodes, edges);
102
+ };
103
+ /**
104
+ * 使用 ELK 布局
105
+ * @param nodes - 节点数据
106
+ * @param edges - 边数据
107
+ * @param elkOption - ELK 配置选项
108
+ */
109
+ ElkLayout.prototype.applyElkLayout = function (nodes, edges) {
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ var elk, layoutOptions, elkGraph, elkLayoutGraph, newGraphData, error_1;
112
+ return __generator(this, function (_a) {
113
+ switch (_a.label) {
114
+ case 0:
115
+ elk = new elk_bundled_1.default();
116
+ layoutOptions = this.convertOptionsToElk();
117
+ elkGraph = {
118
+ id: 'root',
119
+ children: nodes.map(function (node) { return ({
120
+ id: node.id,
121
+ width: node.width || 150,
122
+ height: node.height || 50,
123
+ }); }),
124
+ edges: edges.map(function (edge) { return ({
125
+ id: edge.id,
126
+ sources: [edge.sourceNodeId],
127
+ targets: [edge.targetNodeId],
128
+ }); }),
129
+ };
130
+ _a.label = 1;
131
+ case 1:
132
+ _a.trys.push([1, 3, , 4]);
133
+ return [4 /*yield*/, elk.layout(elkGraph, { layoutOptions: layoutOptions })];
134
+ case 2:
135
+ elkLayoutGraph = _a.sent();
136
+ newGraphData = this.convertLayoutDataToLf(nodes, edges, elkLayoutGraph);
137
+ this.lf.renderRawData(newGraphData);
138
+ return [3 /*break*/, 4];
139
+ case 3:
140
+ error_1 = _a.sent();
141
+ console.error('ELK layout error:', error_1);
142
+ return [3 /*break*/, 4];
143
+ case 4: return [2 /*return*/];
144
+ }
145
+ });
146
+ });
147
+ };
148
+ ElkLayout.prototype.convertOptionsToElk = function () {
149
+ // elk布局配置
150
+ var rankdir = (this.option.rankdir ||
151
+ 'default');
152
+ var align = (this.option.align ||
153
+ 'default');
154
+ var ranker = (this.option.ranker ||
155
+ 'default');
156
+ var acyclicer = (this.option.acyclicer ||
157
+ 'default');
158
+ var layoutOptions = __assign({ 'elk.algorithm': 'layered', 'elk.direction': config_1.elkOptionMap.rankdir[rankdir] || config_1.elkOptionMap.rankdir.default, 'elk.layered.nodePlacement.bk.fixedAlignment': config_1.elkOptionMap.align[align] || config_1.elkOptionMap.align.default, 'elk.layered.layering.strategy': config_1.elkOptionMap.ranker[ranker] || config_1.elkOptionMap.ranker.default, 'elk.layered.cycleBreaking.strategy': config_1.elkOptionMap.acyclicer[acyclicer] || config_1.elkOptionMap.acyclicer.default, 'elk.padding': "[top=".concat(this.option.marginx || 20, ", left=").concat(this.option.marginy || 20, ", bottom=").concat(this.option.marginx || 20, ", right=").concat(this.option.marginy || 20, "]"), 'elk.spacing.nodeNode': "".concat(this.option.nodesep || 50), 'elk.spacing.edgeEdge': "".concat(this.option.edgesep || 10), 'elk.layered.spacing.nodeNodeBetweenLayers': "".concat(this.option.ranksep || 100), 'elk.layered.considerModelOrder.strategy': 'NODES_AND_EDGES', 'elk.layered.nodePlacement.strategy': 'BRANDES_KOEPF' }, this.option.elkOption);
159
+ return layoutOptions;
160
+ };
161
+ ElkLayout.prototype.convertLayoutDataToLf = function (nodes, edges, layoutData) {
162
+ // 存储新的节点和边数据
163
+ var newNodes = [];
164
+ // 更新节点位置
165
+ nodes.forEach(function (nodeModel) {
166
+ var _a;
167
+ var lfNode = nodeModel.getData();
168
+ var newNode = ((layoutData === null || layoutData === void 0 ? void 0 : layoutData.children) || []).find(function (n) { return n.id === nodeModel.id; });
169
+ if (!lfNode || !newNode || !newNode.x || !newNode.y) {
170
+ throw new Error("\u5E03\u5C40\u9519\u8BEF\uFF1A\u627E\u4E0D\u5230ID\u4E3A ".concat(nodeModel.id, " \u7684\u8282\u70B9"));
171
+ }
172
+ // 更新节点坐标
173
+ lfNode.x = newNode.x + nodeModel.width / 2;
174
+ lfNode.y = newNode.y + nodeModel.height / 2;
175
+ // 更新节点文本位置
176
+ if ((_a = lfNode === null || lfNode === void 0 ? void 0 : lfNode.text) === null || _a === void 0 ? void 0 : _a.x) {
177
+ lfNode.text.x = newNode.x + nodeModel.width / 2;
178
+ lfNode.text.y = newNode.y + nodeModel.height / 2;
179
+ }
180
+ newNodes.push(lfNode);
181
+ });
182
+ var newEdges = (0, processEdge_1.processEdges)(this.lf, this.option.rankdir, this.option.isDefaultAnchor, edges, newNodes);
183
+ return {
184
+ nodes: newNodes,
185
+ edges: newEdges,
186
+ };
187
+ };
188
+ /** 插件名称,用于在LogicFlow中注册 */
189
+ ElkLayout.pluginName = 'elkLayout';
190
+ return ElkLayout;
191
+ }());
192
+ exports.ElkLayout = ElkLayout;
193
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/elkLayout/index.ts"],"names":[],"mappings":";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tKD,2BAA2B;IACpB,oBAAU,GAAG,WAAW,CAAA;IAsKjC,gBAAC;CAAA,AAxKD,IAwKC;AAxKY,8BAAS"}
package/lib/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './dagre';
2
+ export * from './elkLayout';
package/lib/index.js CHANGED
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./dagre"), exports);
18
+ __exportStar(require("./elkLayout"), exports);
18
19
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AAEvB,8CAA2B"}
@@ -0,0 +1,3 @@
1
+ import LogicFlow, { BaseEdgeModel } from '@logicflow/core';
2
+ import NodeConfig = LogicFlow.NodeConfig;
3
+ export declare function processEdges(lf: LogicFlow, rankdir: string | undefined, isDefaultAnchor: boolean | undefined, edges: BaseEdgeModel[], newNodes: NodeConfig[]): LogicFlow.EdgeConfig<LogicFlow.PropertiesType>[];