@flowgram.ai/free-auto-layout-plugin 0.2.10 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -140,15 +140,15 @@ declare namespace dagreLib {
140
140
  export { reversePointsForReversedEdges };
141
141
  }
142
142
 
143
- declare const createFreeAutoLayoutPlugin: _flowgram_ai_core.PluginCreator<unknown>;
144
-
145
143
  declare class LayoutStore {
144
+ readonly config: LayoutConfig;
146
145
  private indexMap;
147
146
  private init;
148
147
  private store;
149
148
  private container;
149
+ constructor(config: LayoutConfig);
150
150
  get initialized(): boolean;
151
- getNode(id: string): LayoutNode | undefined;
151
+ getNode(id?: string): LayoutNode | undefined;
152
152
  getNodeByIndex(index: string): LayoutNode | undefined;
153
153
  getEdge(id: string): LayoutEdge | undefined;
154
154
  get nodes(): LayoutNode[];
@@ -223,6 +223,26 @@ interface LayoutParams {
223
223
  interface LayoutOptions {
224
224
  getFollowNode?: GetFollowNode;
225
225
  }
226
+ interface LayoutConfig {
227
+ /** Direction for rank nodes. Can be TB, BT, LR, or RL, where T = top, B = bottom, L = left, and R = right. */
228
+ rankdir: 'TB' | 'BT' | 'LR' | 'RL';
229
+ /** Alignment for rank nodes. Can be UL, UR, DL, or DR, where U = up, D = down, L = left, and R = right. */
230
+ align: 'UL' | 'UR' | 'DL' | 'DR' | undefined;
231
+ /** Number of pixels that separate nodes horizontally in the layout. */
232
+ nodesep: number;
233
+ /** Number of pixels that separate edges horizontally in the layout. */
234
+ edgesep: number;
235
+ /** Number of pixels that separate edges horizontally in the layout. */
236
+ ranksep: number;
237
+ /** Number of pixels to use as a margin around the left and right of the graph. */
238
+ marginx: number;
239
+ /** Number of pixels to use as a margin around the top and bottom of the graph. */
240
+ marginy: number;
241
+ /** If set to greedy, uses a greedy heuristic for finding a feedback arc set for a graph. A feedback arc set is a set of edges that can be removed to make a graph acyclic. */
242
+ acyclicer: 'greedy' | undefined;
243
+ /** Type of algorithm to assigns a rank to each node in the input graph. Possible values: network-simplex, tight-tree or longest-path */
244
+ ranker: 'network-simplex' | 'tight-tree' | 'longest-path';
245
+ }
226
246
  type GetFollowNode = (node: LayoutNode, context: {
227
247
  store: LayoutStore;
228
248
  /** 业务自定义参数 */
@@ -231,22 +251,32 @@ type GetFollowNode = (node: LayoutNode, context: {
231
251
  followTo?: string;
232
252
  } | undefined;
233
253
 
254
+ interface AutoLayoutOptions {
255
+ layoutConfig?: Partial<LayoutConfig>;
256
+ }
257
+
258
+ declare const createFreeAutoLayoutPlugin: _flowgram_ai_core.PluginCreator<AutoLayoutOptions>;
259
+
234
260
  declare class Layout {
235
261
  private readonly _store;
236
262
  private readonly _layout;
237
263
  private readonly _position;
238
- constructor();
264
+ constructor(config: LayoutConfig);
239
265
  init(params: LayoutParams, options?: LayoutOptions): void;
240
266
  layout(): void;
241
267
  position(): Promise<void>;
242
268
  setFollowNode(getFollowNode?: GetFollowNode): void;
243
269
  }
244
270
 
271
+ declare const DefaultLayoutConfig: LayoutConfig;
272
+
245
273
  declare class AutoLayoutService {
246
274
  private readonly document;
275
+ private layoutConfig;
276
+ init(options: AutoLayoutOptions): void;
247
277
  layout(options?: LayoutOptions): Promise<void>;
248
278
  private layoutNode;
249
279
  private getNodesAllLines;
250
280
  }
251
281
 
252
- export { AutoLayoutService, type GetFollowNode, Layout, type LayoutEdge, type LayoutNode, type LayoutOptions, LayoutStore, createFreeAutoLayoutPlugin, dagreLib };
282
+ export { AutoLayoutService, DefaultLayoutConfig, type GetFollowNode, Layout, type LayoutEdge, type LayoutNode, type LayoutOptions, LayoutStore, createFreeAutoLayoutPlugin, dagreLib };
package/dist/index.d.ts CHANGED
@@ -140,15 +140,15 @@ declare namespace dagreLib {
140
140
  export { reversePointsForReversedEdges };
141
141
  }
142
142
 
143
- declare const createFreeAutoLayoutPlugin: _flowgram_ai_core.PluginCreator<unknown>;
144
-
145
143
  declare class LayoutStore {
144
+ readonly config: LayoutConfig;
146
145
  private indexMap;
147
146
  private init;
148
147
  private store;
149
148
  private container;
149
+ constructor(config: LayoutConfig);
150
150
  get initialized(): boolean;
151
- getNode(id: string): LayoutNode | undefined;
151
+ getNode(id?: string): LayoutNode | undefined;
152
152
  getNodeByIndex(index: string): LayoutNode | undefined;
153
153
  getEdge(id: string): LayoutEdge | undefined;
154
154
  get nodes(): LayoutNode[];
@@ -223,6 +223,26 @@ interface LayoutParams {
223
223
  interface LayoutOptions {
224
224
  getFollowNode?: GetFollowNode;
225
225
  }
226
+ interface LayoutConfig {
227
+ /** Direction for rank nodes. Can be TB, BT, LR, or RL, where T = top, B = bottom, L = left, and R = right. */
228
+ rankdir: 'TB' | 'BT' | 'LR' | 'RL';
229
+ /** Alignment for rank nodes. Can be UL, UR, DL, or DR, where U = up, D = down, L = left, and R = right. */
230
+ align: 'UL' | 'UR' | 'DL' | 'DR' | undefined;
231
+ /** Number of pixels that separate nodes horizontally in the layout. */
232
+ nodesep: number;
233
+ /** Number of pixels that separate edges horizontally in the layout. */
234
+ edgesep: number;
235
+ /** Number of pixels that separate edges horizontally in the layout. */
236
+ ranksep: number;
237
+ /** Number of pixels to use as a margin around the left and right of the graph. */
238
+ marginx: number;
239
+ /** Number of pixels to use as a margin around the top and bottom of the graph. */
240
+ marginy: number;
241
+ /** If set to greedy, uses a greedy heuristic for finding a feedback arc set for a graph. A feedback arc set is a set of edges that can be removed to make a graph acyclic. */
242
+ acyclicer: 'greedy' | undefined;
243
+ /** Type of algorithm to assigns a rank to each node in the input graph. Possible values: network-simplex, tight-tree or longest-path */
244
+ ranker: 'network-simplex' | 'tight-tree' | 'longest-path';
245
+ }
226
246
  type GetFollowNode = (node: LayoutNode, context: {
227
247
  store: LayoutStore;
228
248
  /** 业务自定义参数 */
@@ -231,22 +251,32 @@ type GetFollowNode = (node: LayoutNode, context: {
231
251
  followTo?: string;
232
252
  } | undefined;
233
253
 
254
+ interface AutoLayoutOptions {
255
+ layoutConfig?: Partial<LayoutConfig>;
256
+ }
257
+
258
+ declare const createFreeAutoLayoutPlugin: _flowgram_ai_core.PluginCreator<AutoLayoutOptions>;
259
+
234
260
  declare class Layout {
235
261
  private readonly _store;
236
262
  private readonly _layout;
237
263
  private readonly _position;
238
- constructor();
264
+ constructor(config: LayoutConfig);
239
265
  init(params: LayoutParams, options?: LayoutOptions): void;
240
266
  layout(): void;
241
267
  position(): Promise<void>;
242
268
  setFollowNode(getFollowNode?: GetFollowNode): void;
243
269
  }
244
270
 
271
+ declare const DefaultLayoutConfig: LayoutConfig;
272
+
245
273
  declare class AutoLayoutService {
246
274
  private readonly document;
275
+ private layoutConfig;
276
+ init(options: AutoLayoutOptions): void;
247
277
  layout(options?: LayoutOptions): Promise<void>;
248
278
  private layoutNode;
249
279
  private getNodesAllLines;
250
280
  }
251
281
 
252
- export { AutoLayoutService, type GetFollowNode, Layout, type LayoutEdge, type LayoutNode, type LayoutOptions, LayoutStore, createFreeAutoLayoutPlugin, dagreLib };
282
+ export { AutoLayoutService, DefaultLayoutConfig, type GetFollowNode, Layout, type LayoutEdge, type LayoutNode, type LayoutOptions, LayoutStore, createFreeAutoLayoutPlugin, dagreLib };
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ var src_exports = {};
30
30
  __export(src_exports, {
31
31
  AutoLayoutService: () => AutoLayoutService,
32
32
  DagreGraph: () => import_graphlib10.Graph,
33
+ DefaultLayoutConfig: () => DefaultLayoutConfig,
33
34
  Layout: () => Layout,
34
35
  createFreeAutoLayoutPlugin: () => createFreeAutoLayoutPlugin,
35
36
  dagreLib: () => dagreLib
@@ -2114,13 +2115,17 @@ var import_free_layout_core2 = require("@flowgram.ai/free-layout-core");
2114
2115
  var import_free_layout_core = require("@flowgram.ai/free-layout-core");
2115
2116
  var import_document = require("@flowgram.ai/document");
2116
2117
  var LayoutStore = class {
2117
- constructor() {
2118
+ constructor(config) {
2119
+ this.config = config;
2118
2120
  this.init = false;
2119
2121
  }
2120
2122
  get initialized() {
2121
2123
  return this.init;
2122
2124
  }
2123
2125
  getNode(id) {
2126
+ if (!id) {
2127
+ return void 0;
2128
+ }
2124
2129
  return this.store.nodes.get(id);
2125
2130
  }
2126
2131
  getNodeByIndex(index) {
@@ -2299,8 +2304,13 @@ var LayoutStore = class {
2299
2304
  });
2300
2305
  const { outputLines } = node.getData(import_free_layout_core.WorkflowNodeLinesData);
2301
2306
  const sortedLines = outputLines.sort((a, b) => {
2307
+ const aNode = this.getNode(a.to?.id);
2308
+ const bNode = this.getNode(b.to?.id);
2302
2309
  const aPort = a.fromPort;
2303
2310
  const bPort = b.fromPort;
2311
+ if (aPort === bPort && aNode && bNode) {
2312
+ return aNode.position.y - bNode.position.y;
2313
+ }
2304
2314
  if (aPort && bPort) {
2305
2315
  return aPort.point.y - bPort.point.y;
2306
2316
  }
@@ -2371,16 +2381,6 @@ var LayoutPosition = class {
2371
2381
  // src/layout/dagre.ts
2372
2382
  var import_document2 = require("@flowgram.ai/document");
2373
2383
  var import_graphlib9 = require("@dagrejs/graphlib");
2374
-
2375
- // src/layout/constant.ts
2376
- var DagreLayoutOptions = {
2377
- rankdir: "LR",
2378
- nodesep: 100,
2379
- ranksep: 100,
2380
- ranker: "network-simplex"
2381
- };
2382
-
2383
- // src/layout/dagre.ts
2384
2384
  var DagreLayout = class {
2385
2385
  constructor(store) {
2386
2386
  this.store = store;
@@ -2429,7 +2429,7 @@ var DagreLayout = class {
2429
2429
  createGraph() {
2430
2430
  const graph = new import_graphlib9.Graph({ multigraph: true });
2431
2431
  graph.setDefaultEdgeLabel(() => ({}));
2432
- graph.setGraph(DagreLayoutOptions);
2432
+ graph.setGraph(this.store.config);
2433
2433
  return graph;
2434
2434
  }
2435
2435
  graphSetData() {
@@ -2551,8 +2551,8 @@ var DagreLayout = class {
2551
2551
 
2552
2552
  // src/layout/layout.ts
2553
2553
  var Layout = class {
2554
- constructor() {
2555
- this._store = new LayoutStore();
2554
+ constructor(config) {
2555
+ this._store = new LayoutStore(config);
2556
2556
  this._layout = new DagreLayout(this._store);
2557
2557
  this._position = new LayoutPosition(this._store);
2558
2558
  }
@@ -2589,8 +2589,30 @@ var Layout = class {
2589
2589
  }
2590
2590
  };
2591
2591
 
2592
+ // src/layout/constant.ts
2593
+ var DefaultLayoutConfig = {
2594
+ rankdir: "LR",
2595
+ align: void 0,
2596
+ nodesep: 100,
2597
+ edgesep: 10,
2598
+ ranksep: 100,
2599
+ marginx: 0,
2600
+ marginy: 0,
2601
+ acyclicer: void 0,
2602
+ ranker: "network-simplex"
2603
+ };
2604
+
2592
2605
  // src/services.ts
2593
2606
  var AutoLayoutService = class {
2607
+ constructor() {
2608
+ this.layoutConfig = DefaultLayoutConfig;
2609
+ }
2610
+ init(options) {
2611
+ this.layoutConfig = {
2612
+ ...this.layoutConfig,
2613
+ ...options.layoutConfig
2614
+ };
2615
+ }
2594
2616
  async layout(options = {}) {
2595
2617
  await this.layoutNode(this.document.root, options);
2596
2618
  }
@@ -2601,7 +2623,7 @@ var AutoLayoutService = class {
2601
2623
  }
2602
2624
  const edges = this.getNodesAllLines(nodes);
2603
2625
  await Promise.all(nodes.map(async (child) => this.layoutNode(child, options)));
2604
- const layout2 = new Layout();
2626
+ const layout2 = new Layout(this.layoutConfig);
2605
2627
  layout2.init({ nodes, edges, container: node }, options);
2606
2628
  layout2.layout();
2607
2629
  await layout2.position();
@@ -2627,7 +2649,11 @@ AutoLayoutService = __decorateClass([
2627
2649
  var createFreeAutoLayoutPlugin = (0, import_core2.definePluginCreator)({
2628
2650
  onBind: ({ bind }) => {
2629
2651
  bind(AutoLayoutService).toSelf().inSingletonScope();
2630
- }
2652
+ },
2653
+ onInit: (ctx, opts) => {
2654
+ ctx.get(AutoLayoutService).init(opts);
2655
+ },
2656
+ singleton: true
2631
2657
  });
2632
2658
 
2633
2659
  // src/index.ts
@@ -2636,6 +2662,7 @@ var import_graphlib10 = require("@dagrejs/graphlib");
2636
2662
  0 && (module.exports = {
2637
2663
  AutoLayoutService,
2638
2664
  DagreGraph,
2665
+ DefaultLayoutConfig,
2639
2666
  Layout,
2640
2667
  createFreeAutoLayoutPlugin,
2641
2668
  dagreLib