@dagrejs/dagre 3.0.0 → 3.1.1

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 (43) hide show
  1. package/dist/dagre.cjs +3 -0
  2. package/dist/dagre.cjs.map +7 -0
  3. package/dist/dagre.esm.js +1 -1
  4. package/dist/dagre.esm.js.map +4 -4
  5. package/dist/dagre.js +752 -278
  6. package/dist/dagre.js.map +4 -4
  7. package/dist/dagre.min.js +1 -1
  8. package/dist/dagre.min.js.map +4 -4
  9. package/dist/types/index.d.ts +2 -2
  10. package/dist/types/index.d.ts.map +1 -1
  11. package/dist/types/lib/acyclic.d.ts +2 -2
  12. package/dist/types/lib/acyclic.d.ts.map +1 -1
  13. package/dist/types/lib/add-border-segments.d.ts.map +1 -1
  14. package/dist/types/lib/coordinate-system.d.ts +22 -0
  15. package/dist/types/lib/coordinate-system.d.ts.map +1 -1
  16. package/dist/types/lib/graph-lib.d.ts +5 -2
  17. package/dist/types/lib/graph-lib.d.ts.map +1 -1
  18. package/dist/types/lib/layout.d.ts +2 -2
  19. package/dist/types/lib/layout.d.ts.map +1 -1
  20. package/dist/types/lib/nesting-graph.d.ts.map +1 -1
  21. package/dist/types/lib/order/index.d.ts +2 -8
  22. package/dist/types/lib/order/index.d.ts.map +1 -1
  23. package/dist/types/lib/order/init-order.d.ts +2 -2
  24. package/dist/types/lib/order/init-order.d.ts.map +1 -1
  25. package/dist/types/lib/order/sort-subgraph.d.ts +5 -2
  26. package/dist/types/lib/order/sort-subgraph.d.ts.map +1 -1
  27. package/dist/types/lib/order/sort.d.ts +3 -1
  28. package/dist/types/lib/order/sort.d.ts.map +1 -1
  29. package/dist/types/lib/parent-dummy-chains.d.ts.map +1 -1
  30. package/dist/types/lib/position/bk.d.ts +2 -2
  31. package/dist/types/lib/position/bk.d.ts.map +1 -1
  32. package/dist/types/lib/position/index.d.ts +1 -1
  33. package/dist/types/lib/position/index.d.ts.map +1 -1
  34. package/dist/types/lib/rank/index.d.ts.map +1 -1
  35. package/dist/types/lib/types.d.ts +27 -8
  36. package/dist/types/lib/types.d.ts.map +1 -1
  37. package/dist/types/lib/util.d.ts +3 -2
  38. package/dist/types/lib/util.d.ts.map +1 -1
  39. package/dist/types/lib/version.d.ts +1 -1
  40. package/package.json +5 -4
  41. package/dist/dagre.cjs.js +0 -3
  42. package/dist/dagre.cjs.js.map +0 -7
  43. /package/dist/{dagre.cjs.js.LEGAL.txt → dagre.cjs.LEGAL.txt} +0 -0
package/dist/dagre.js CHANGED
@@ -23,25 +23,25 @@ var dagre = (() => {
23
23
  // index.ts
24
24
  var index_exports = {};
25
25
  __export(index_exports, {
26
- Graph: () => p,
26
+ Graph: () => Graph,
27
27
  debug: () => debugOrdering,
28
28
  default: () => index_default,
29
- graphlib: () => graphlib_esm_exports,
29
+ graphlib: () => graph_lib_exports,
30
30
  layout: () => layout,
31
31
  util: () => util,
32
32
  version: () => version
33
33
  });
34
34
 
35
- // node_modules/@dagrejs/graphlib/dist/graphlib.esm.js
36
- var graphlib_esm_exports = {};
37
- __export(graphlib_esm_exports, {
38
- Graph: () => p,
39
- alg: () => v,
40
- json: () => G,
41
- version: () => H
35
+ // lib/graph-lib.ts
36
+ var graph_lib_exports = {};
37
+ __export(graph_lib_exports, {
38
+ Graph: () => Graph,
39
+ alg: () => F
42
40
  });
41
+
42
+ // node_modules/@dagrejs/graphlib/dist/graphlib.esm.js
43
43
  var V = Object.defineProperty;
44
- var F = (s, e) => {
44
+ var x = (s, e) => {
45
45
  for (var t in e) V(s, t, { get: e[t], enumerable: true });
46
46
  };
47
47
  var p = class {
@@ -95,8 +95,8 @@ var dagre = (() => {
95
95
  return this.nodes().filter((e) => Object.keys(this._out[e]).length === 0);
96
96
  }
97
97
  setNodes(e, t) {
98
- return e.forEach((n) => {
99
- t !== void 0 ? this.setNode(n, t) : this.setNode(n);
98
+ return e.forEach((r) => {
99
+ t !== void 0 ? this.setNode(r, t) : this.setNode(r);
100
100
  }), this;
101
101
  }
102
102
  setNode(e, t) {
@@ -110,9 +110,9 @@ var dagre = (() => {
110
110
  }
111
111
  removeNode(e) {
112
112
  if (e in this._nodes) {
113
- let t = (n) => this.removeEdge(this._edgeObjs[n]);
114
- delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], this.children(e).forEach((n) => {
115
- this.setParent(n);
113
+ let t = (r) => this.removeEdge(this._edgeObjs[r]);
114
+ delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], this.children(e).forEach((r) => {
115
+ this.setParent(r);
116
116
  }), delete this._children[e]), Object.keys(this._in[e]).forEach(t), delete this._in[e], delete this._preds[e], Object.keys(this._out[e]).forEach(t), delete this._out[e], delete this._sucs[e], --this._nodeCount;
117
117
  }
118
118
  return this;
@@ -122,7 +122,7 @@ var dagre = (() => {
122
122
  if (t === void 0) t = "\0";
123
123
  else {
124
124
  t += "";
125
- for (let n = t; n !== void 0; n = this.parent(n)) if (n === e) throw new Error("Setting " + t + " as parent of " + e + " would create a cycle");
125
+ for (let r = t; r !== void 0; r = this.parent(r)) if (r === e) throw new Error("Setting " + t + " as parent of " + e + " would create a cycle");
126
126
  this.setNode(t);
127
127
  }
128
128
  return this.setNode(e), this._removeFromParentsChildList(e), this._parent[e] = t, this._children[t][e] = true, this;
@@ -154,27 +154,28 @@ var dagre = (() => {
154
154
  neighbors(e) {
155
155
  let t = this.predecessors(e);
156
156
  if (t) {
157
- let n = new Set(t);
158
- for (let i of this.successors(e)) n.add(i);
159
- return Array.from(n.values());
157
+ let r = new Set(t), i = this.successors(e);
158
+ if (i) for (let n of i) r.add(n);
159
+ return Array.from(r.values());
160
160
  }
161
161
  }
162
162
  isLeaf(e) {
163
+ var r;
163
164
  let t;
164
- return this.isDirected() ? t = this.successors(e) : t = this.neighbors(e), t.length === 0;
165
+ return this.isDirected() ? t = this.successors(e) : t = this.neighbors(e), ((r = t == null ? void 0 : t.length) != null ? r : 0) === 0;
165
166
  }
166
167
  filterNodes(e) {
167
168
  let t = new this.constructor({ directed: this._isDirected, multigraph: this._isMultigraph, compound: this._isCompound });
168
- t.setGraph(this.graph()), Object.entries(this._nodes).forEach(([r, o]) => {
169
- e(r) && t.setNode(r, o);
170
- }), Object.values(this._edgeObjs).forEach((r) => {
171
- t.hasNode(r.v) && t.hasNode(r.w) && t.setEdge(r, this.edge(r));
169
+ t.setGraph(this.graph()), Object.entries(this._nodes).forEach(([n, o]) => {
170
+ e(n) && t.setNode(n, o);
171
+ }), Object.values(this._edgeObjs).forEach((n) => {
172
+ t.hasNode(n.v) && t.hasNode(n.w) && t.setEdge(n, this.edge(n));
172
173
  });
173
- let n = {}, i = (r) => {
174
- let o = this.parent(r);
175
- return !o || t.hasNode(o) ? (n[r] = o != null ? o : void 0, o != null ? o : void 0) : o in n ? n[o] : i(o);
174
+ let r = {}, i = (n) => {
175
+ let o = this.parent(n);
176
+ return !o || t.hasNode(o) ? (r[n] = o, o) : o in r ? r[o] : i(o);
176
177
  };
177
- return this._isCompound && t.nodes().forEach((r) => t.setParent(r, i(r))), t;
178
+ return this._isCompound && t.nodes().forEach((n) => t.setParent(n, i(n))), t;
178
179
  }
179
180
  setDefaultEdgeLabel(e) {
180
181
  return typeof e != "function" ? this._defaultEdgeLabelFn = () => e : this._defaultEdgeLabelFn = e, this;
@@ -186,34 +187,34 @@ var dagre = (() => {
186
187
  return Object.values(this._edgeObjs);
187
188
  }
188
189
  setPath(e, t) {
189
- return e.reduce((n, i) => (t !== void 0 ? this.setEdge(n, i, t) : this.setEdge(n, i), i)), this;
190
+ return e.reduce((r, i) => (t !== void 0 ? this.setEdge(r, i, t) : this.setEdge(r, i), i)), this;
190
191
  }
191
- setEdge(e, t, n, i) {
192
- let r, o, d, a, c = false;
193
- typeof e == "object" && e !== null && "v" in e ? (r = e.v, o = e.w, d = e.name, arguments.length === 2 && (a = t, c = true)) : (r = e, o = t, d = i, arguments.length > 2 && (a = n, c = true)), r = "" + r, o = "" + o, d !== void 0 && (d = "" + d);
194
- let h = b(this._isDirected, r, o, d);
195
- if (h in this._edgeLabels) return c && (this._edgeLabels[h] = a), this;
192
+ setEdge(e, t, r, i) {
193
+ let n, o, d, h, c = false;
194
+ typeof e == "object" && e !== null && "v" in e ? (n = e.v, o = e.w, d = e.name, arguments.length === 2 && (h = t, c = true)) : (n = e, o = t, d = i, arguments.length > 2 && (h = r, c = true)), n = "" + n, o = "" + o, d !== void 0 && (d = "" + d);
195
+ let a = _(this._isDirected, n, o, d);
196
+ if (a in this._edgeLabels) return c && (this._edgeLabels[a] = h), this;
196
197
  if (d !== void 0 && !this._isMultigraph) throw new Error("Cannot set a named edge when isMultigraph = false");
197
- this.setNode(r), this.setNode(o), this._edgeLabels[h] = c ? a : this._defaultEdgeLabelFn(r, o, d);
198
- let u = J(this._isDirected, r, o, d);
199
- return r = u.v, o = u.w, Object.freeze(u), this._edgeObjs[h] = u, k(this._preds[o], r), k(this._sucs[r], o), this._in[o][h] = u, this._out[r][h] = u, this._edgeCount++, this;
198
+ this.setNode(n), this.setNode(o), this._edgeLabels[a] = c ? h : this._defaultEdgeLabelFn(n, o, d);
199
+ let u = J(this._isDirected, n, o, d);
200
+ return n = u.v, o = u.w, Object.freeze(u), this._edgeObjs[a] = u, R(this._preds[o], n), R(this._sucs[n], o), this._in[o][a] = u, this._out[n][a] = u, this._edgeCount++, this;
200
201
  }
201
- edge(e, t, n) {
202
- let i = arguments.length === 1 ? N(this._isDirected, e) : b(this._isDirected, e, t, n);
202
+ edge(e, t, r) {
203
+ let i = arguments.length === 1 ? G(this._isDirected, e) : _(this._isDirected, e, t, r);
203
204
  return this._edgeLabels[i];
204
205
  }
205
- edgeAsObj(e, t, n) {
206
- let i = arguments.length === 1 ? this.edge(e) : this.edge(e, t, n);
207
- return typeof i != "object" ? { label: i } : i;
206
+ edgeAsObj(e, t, r) {
207
+ let i = arguments.length === 1 ? this.edge(e) : this.edge(e, t, r);
208
+ return typeof i != "object" || i === null ? { label: i } : i;
208
209
  }
209
- hasEdge(e, t, n) {
210
- return (arguments.length === 1 ? N(this._isDirected, e) : b(this._isDirected, e, t, n)) in this._edgeLabels;
210
+ hasEdge(e, t, r) {
211
+ return (arguments.length === 1 ? G(this._isDirected, e) : _(this._isDirected, e, t, r)) in this._edgeLabels;
211
212
  }
212
- removeEdge(e, t, n) {
213
- let i = arguments.length === 1 ? N(this._isDirected, e) : b(this._isDirected, e, t, n), r = this._edgeObjs[i];
214
- if (r) {
215
- let o = r.v, d = r.w;
216
- delete this._edgeLabels[i], delete this._edgeObjs[i], x(this._preds[d], o), x(this._sucs[o], d), delete this._in[d][i], delete this._out[o][i], this._edgeCount--;
213
+ removeEdge(e, t, r) {
214
+ let i = arguments.length === 1 ? G(this._isDirected, e) : _(this._isDirected, e, t, r), n = this._edgeObjs[i];
215
+ if (n) {
216
+ let o = n.v, d = n.w;
217
+ delete this._edgeLabels[i], delete this._edgeObjs[i], k(this._preds[d], o), k(this._sucs[o], d), delete this._in[d][i], delete this._out[o][i], this._edgeCount--;
217
218
  }
218
219
  return this;
219
220
  }
@@ -229,58 +230,57 @@ var dagre = (() => {
229
230
  _removeFromParentsChildList(e) {
230
231
  delete this._children[this._parent[e]][e];
231
232
  }
232
- filterEdges(e, t, n) {
233
+ filterEdges(e, t, r) {
233
234
  if (!e) return;
234
235
  let i = Object.values(e);
235
- return n ? i.filter((r) => r.v === t && r.w === n || r.v === n && r.w === t) : i;
236
+ return r ? i.filter((n) => n.v === t && n.w === r || n.v === r && n.w === t) : i;
236
237
  }
237
238
  };
238
- function k(s, e) {
239
+ function R(s, e) {
239
240
  s[e] ? s[e]++ : s[e] = 1;
240
241
  }
241
- function x(s, e) {
242
+ function k(s, e) {
242
243
  s[e] !== void 0 && !--s[e] && delete s[e];
243
244
  }
244
- function b(s, e, t, n) {
245
- let i = "" + e, r = "" + t;
246
- if (!s && i > r) {
245
+ function _(s, e, t, r) {
246
+ let i = "" + e, n = "" + t;
247
+ if (!s && i > n) {
247
248
  let o = i;
248
- i = r, r = o;
249
+ i = n, n = o;
249
250
  }
250
- return i + "" + r + "" + (n === void 0 ? "\0" : n);
251
+ return i + "" + n + "" + (r === void 0 ? "\0" : r);
251
252
  }
252
- function J(s, e, t, n) {
253
- let i = "" + e, r = "" + t;
254
- if (!s && i > r) {
253
+ function J(s, e, t, r) {
254
+ let i = "" + e, n = "" + t;
255
+ if (!s && i > n) {
255
256
  let d = i;
256
- i = r, r = d;
257
+ i = n, n = d;
257
258
  }
258
- let o = { v: i, w: r };
259
- return n && (o.name = n), o;
259
+ let o = { v: i, w: n };
260
+ return r && (o.name = r), o;
260
261
  }
261
- function N(s, e) {
262
- return b(s, e.v, e.w, e.name);
262
+ function G(s, e) {
263
+ return _(s, e.v, e.w, e.name);
263
264
  }
264
- var H = "4.0.1";
265
- var G = {};
266
- F(G, { read: () => z, write: () => U });
265
+ var v = {};
266
+ x(v, { read: () => K, write: () => U });
267
267
  function U(s) {
268
- let e = { options: { directed: s.isDirected(), multigraph: s.isMultigraph(), compound: s.isCompound() }, nodes: Y(s), edges: K(s) }, t = s.graph();
268
+ let e = { options: { directed: s.isDirected(), multigraph: s.isMultigraph(), compound: s.isCompound() }, nodes: Y(s), edges: z(s) }, t = s.graph();
269
269
  return t !== void 0 && (e.value = structuredClone(t)), e;
270
270
  }
271
271
  function Y(s) {
272
272
  return s.nodes().map((e) => {
273
- let t = s.node(e), n = s.parent(e), i = { v: e };
274
- return t !== void 0 && (i.value = t), n !== void 0 && (i.parent = n), i;
273
+ let t = s.node(e), r = s.parent(e), i = { v: e };
274
+ return t !== void 0 && (i.value = t), r !== void 0 && (i.parent = r), i;
275
275
  });
276
276
  }
277
- function K(s) {
277
+ function z(s) {
278
278
  return s.edges().map((e) => {
279
- let t = s.edge(e), n = { v: e.v, w: e.w };
280
- return e.name !== void 0 && (n.name = e.name), t !== void 0 && (n.value = t), n;
279
+ let t = s.edge(e), r = { v: e.v, w: e.w };
280
+ return e.name !== void 0 && (r.name = e.name), t !== void 0 && (r.value = t), r;
281
281
  });
282
282
  }
283
- function z(s) {
283
+ function K(s) {
284
284
  let e = new p(s.options);
285
285
  return s.value !== void 0 && e.setGraph(s.value), s.nodes.forEach((t) => {
286
286
  e.setNode(t.v, t.value), t.parent && e.setParent(t.v, t.parent);
@@ -288,23 +288,26 @@ var dagre = (() => {
288
288
  e.setEdge({ v: t.v, w: t.w, name: t.name }, t.value);
289
289
  }), e;
290
290
  }
291
- var v = {};
292
- F(v, { CycleException: () => l, bellmanFord: () => m, components: () => R, dijkstra: () => E, dijkstraAll: () => P, findCycles: () => I, floydWarshall: () => D, isAcyclic: () => O, postorder: () => T, preorder: () => A, prim: () => W, shortestPaths: () => S, tarjan: () => y, topsort: () => L });
291
+ var F = {};
292
+ x(F, { CycleException: () => l, bellmanFord: () => y, components: () => P, dijkstra: () => E, dijkstraAll: () => I, findCycles: () => D, floydWarshall: () => O, isAcyclic: () => j, postorder: () => A, preorder: () => W, prim: () => S, shortestPaths: () => M, tarjan: () => L, topsort: () => w });
293
293
  var Q = () => 1;
294
- function m(s, e, t, n) {
295
- return $(s, String(e), t || Q, n || function(i) {
296
- return s.outEdges(i);
297
- });
298
- }
299
- function $(s, e, t, n) {
300
- let i = {}, r, o = 0, d = s.nodes(), a = function(u) {
301
- let g = t(u);
302
- i[u.v].distance + g < i[u.w].distance && (i[u.w] = { distance: i[u.v].distance + g, predecessor: u.v }, r = true);
294
+ function y(s, e, t, r) {
295
+ return $(s, String(e), t || Q, r || function(i) {
296
+ var n;
297
+ return (n = s.outEdges(i)) != null ? n : [];
298
+ });
299
+ }
300
+ function $(s, e, t, r) {
301
+ let i = {}, n, o = 0, d = s.nodes(), h = function(u) {
302
+ let g = i[u.v], f = i[u.w];
303
+ if (!g || !f) return;
304
+ let m = t(u);
305
+ g.distance + m < f.distance && (i[u.w] = { distance: g.distance + m, predecessor: u.v }, n = true);
303
306
  }, c = function() {
304
307
  d.forEach(function(u) {
305
- n(u).forEach(function(g) {
306
- let f = g.v === u ? g.v : g.w, M = f === g.v ? g.w : g.v;
307
- a({ v: f, w: M });
308
+ r(u).forEach(function(g) {
309
+ let f = g.v === u ? g.v : g.w, m = f === g.v ? g.w : g.v;
310
+ h({ v: f, w: m });
308
311
  });
309
312
  });
310
313
  };
@@ -312,21 +315,22 @@ var dagre = (() => {
312
315
  let g = u === e ? 0 : Number.POSITIVE_INFINITY;
313
316
  i[u] = { distance: g, predecessor: "" };
314
317
  });
315
- let h = d.length;
316
- for (let u = 1; u < h && (r = false, o++, c(), !!r); u++) ;
317
- if (o === h - 1 && (r = false, c(), r)) throw new Error("The graph contains a negative weight cycle");
318
+ let a = d.length;
319
+ for (let u = 1; u < a && (n = false, o++, c(), !!n); u++) ;
320
+ if (o === a - 1 && (n = false, c(), n)) throw new Error("The graph contains a negative weight cycle");
318
321
  return i;
319
322
  }
320
- function R(s) {
321
- let e = {}, t = [], n;
322
- function i(r) {
323
- r in e || (e[r] = true, n.push(r), s.successors(r).forEach(i), s.predecessors(r).forEach(i));
323
+ function P(s) {
324
+ let e = {}, t = [], r;
325
+ function i(n) {
326
+ var o, d;
327
+ n in e || (e[n] = true, r.push(n), (o = s.successors(n)) == null || o.forEach(i), (d = s.predecessors(n)) == null || d.forEach(i));
324
328
  }
325
- return s.nodes().forEach(function(r) {
326
- n = [], i(r), n.length && t.push(n);
329
+ return s.nodes().forEach(function(n) {
330
+ r = [], i(n), r.length && t.push(r);
327
331
  }), t;
328
332
  }
329
- var _ = class {
333
+ var b = class {
330
334
  constructor() {
331
335
  this._arr = [];
332
336
  this._keyIndices = {};
@@ -349,205 +353,242 @@ var dagre = (() => {
349
353
  return this._arr[0].key;
350
354
  }
351
355
  add(e, t) {
352
- let n = this._keyIndices, i = String(e);
353
- if (!(i in n)) {
354
- let r = this._arr, o = r.length;
355
- return n[i] = o, r.push({ key: i, priority: t }), this._decrease(o), true;
356
+ let r = this._keyIndices, i = String(e);
357
+ if (!(i in r)) {
358
+ let n = this._arr, o = n.length;
359
+ return r[i] = o, n.push({ key: i, priority: t }), this._decrease(o), true;
356
360
  }
357
361
  return false;
358
362
  }
359
363
  removeMin() {
364
+ if (this.size() === 0) throw new Error("Queue underflow");
360
365
  this._swap(0, this._arr.length - 1);
361
366
  let e = this._arr.pop();
362
367
  return delete this._keyIndices[e.key], this._heapify(0), e.key;
363
368
  }
364
369
  decrease(e, t) {
365
- let n = this._keyIndices[e];
366
- if (n === void 0) throw new Error(`Key not found: ${e}`);
367
- let i = this._arr[n].priority;
370
+ let r = this._keyIndices[e];
371
+ if (r === void 0) throw new Error(`Key not found: ${e}`);
372
+ let i = this._arr[r].priority;
368
373
  if (t > i) throw new Error(`New priority is greater than current priority. Key: ${e} Old: ${i} New: ${t}`);
369
- this._arr[n].priority = t, this._decrease(n);
374
+ this._arr[r].priority = t, this._decrease(r);
370
375
  }
371
376
  _heapify(e) {
372
- let t = this._arr, n = 2 * e, i = n + 1, r = e;
373
- n < t.length && (r = t[n].priority < t[r].priority ? n : r, i < t.length && (r = t[i].priority < t[r].priority ? i : r), r !== e && (this._swap(e, r), this._heapify(r)));
377
+ let t = this._arr, r = 2 * e, i = r + 1, n = e;
378
+ r < t.length && (n = t[r].priority < t[n].priority ? r : n, i < t.length && (n = t[i].priority < t[n].priority ? i : n), n !== e && (this._swap(e, n), this._heapify(n)));
374
379
  }
375
380
  _decrease(e) {
376
- let t = this._arr, n = t[e].priority, i;
377
- for (; e !== 0 && (i = e >> 1, !(t[i].priority < n)); ) this._swap(e, i), e = i;
381
+ let t = this._arr, r = t[e].priority, i;
382
+ for (; e !== 0 && (i = e >> 1, !(t[i].priority < r)); ) this._swap(e, i), e = i;
378
383
  }
379
384
  _swap(e, t) {
380
- let n = this._arr, i = this._keyIndices, r = n[e], o = n[t];
381
- n[e] = o, n[t] = r, i[o.key] = e, i[r.key] = t;
385
+ let r = this._arr, i = this._keyIndices, n = r[e], o = r[t];
386
+ r[e] = o, r[t] = n, i[o.key] = e, i[n.key] = t;
382
387
  }
383
388
  };
384
389
  var q = () => 1;
385
- function E(s, e, t, n) {
386
- let i = function(r) {
387
- return s.outEdges(r);
390
+ function E(s, e, t, r) {
391
+ let i = function(n) {
392
+ var o;
393
+ return (o = s.outEdges(n)) != null ? o : [];
388
394
  };
389
- return B(s, String(e), t || q, n || i);
395
+ return B(s, String(e), t || q, r || i);
390
396
  }
391
- function B(s, e, t, n) {
392
- let i = {}, r = new _(), o, d, a = function(c) {
393
- let h = c.v !== o ? c.v : c.w, u = i[h], g = t(c), f = d.distance + g;
397
+ function B(s, e, t, r) {
398
+ let i = {}, n = new b(), o, d, h = function(c) {
399
+ let a = c.v !== o ? c.v : c.w, u = i[a];
400
+ if (!u) return;
401
+ let g = t(c), f = d.distance + g;
394
402
  if (g < 0) throw new Error("dijkstra does not allow negative edge weights. Bad edge: " + c + " Weight: " + g);
395
- f < u.distance && (u.distance = f, u.predecessor = o, r.decrease(h, f));
403
+ f < u.distance && (u.distance = f, u.predecessor = o, n.decrease(a, f));
396
404
  };
397
405
  for (s.nodes().forEach(function(c) {
398
- let h = c === e ? 0 : Number.POSITIVE_INFINITY;
399
- i[c] = { distance: h, predecessor: "" }, r.add(c, h);
400
- }); r.size() > 0 && (o = r.removeMin(), d = i[o], d.distance !== Number.POSITIVE_INFINITY); ) n(o).forEach(a);
406
+ let a = c === e ? 0 : Number.POSITIVE_INFINITY;
407
+ i[c] = { distance: a, predecessor: "" }, n.add(c, a);
408
+ }); n.size() > 0; ) {
409
+ o = n.removeMin();
410
+ let c = i[o];
411
+ if (!c || c.distance === Number.POSITIVE_INFINITY) break;
412
+ d = c, r(o).forEach(h);
413
+ }
401
414
  return i;
402
415
  }
403
- function P(s, e, t) {
404
- return s.nodes().reduce(function(n, i) {
405
- return n[i] = E(s, i, e, t), n;
416
+ function I(s, e, t) {
417
+ return s.nodes().reduce(function(r, i) {
418
+ return r[i] = E(s, i, e, t), r;
406
419
  }, {});
407
420
  }
408
- function y(s) {
409
- let e = 0, t = [], n = {}, i = [];
410
- function r(o) {
411
- let d = n[o] = { onStack: true, lowlink: e, index: e++ };
412
- if (t.push(o), s.successors(o).forEach(function(a) {
413
- a in n ? n[a].onStack && (d.lowlink = Math.min(d.lowlink, n[a].index)) : (r(a), d.lowlink = Math.min(d.lowlink, n[a].lowlink));
421
+ function L(s) {
422
+ let e = 0, t = [], r = {}, i = [];
423
+ function n(o) {
424
+ var h;
425
+ let d = r[o] = { onStack: true, lowlink: e, index: e++ };
426
+ if (t.push(o), (h = s.successors(o)) == null || h.forEach(function(c) {
427
+ if (c in r) {
428
+ let a = r[c];
429
+ a != null && a.onStack && (d.lowlink = Math.min(d.lowlink, a.index));
430
+ } else {
431
+ n(c);
432
+ let a = r[c];
433
+ a && (d.lowlink = Math.min(d.lowlink, a.lowlink));
434
+ }
414
435
  }), d.lowlink === d.index) {
415
- let a = [], c;
416
- do
417
- c = t.pop(), n[c].onStack = false, a.push(c);
418
- while (o !== c);
419
- i.push(a);
436
+ let c = [], a;
437
+ do {
438
+ a = t.pop();
439
+ let u = r[a];
440
+ u && (u.onStack = false), c.push(a);
441
+ } while (o !== a);
442
+ i.push(c);
420
443
  }
421
444
  }
422
445
  return s.nodes().forEach(function(o) {
423
- o in n || r(o);
446
+ o in r || n(o);
424
447
  }), i;
425
448
  }
426
- function I(s) {
427
- return y(s).filter(function(e) {
428
- return e.length > 1 || e.length === 1 && s.hasEdge(e[0], e[0]);
449
+ function D(s) {
450
+ return L(s).filter(function(e) {
451
+ var r;
452
+ let t = e[0];
453
+ return t ? e.length > 1 || e.length === 1 && ((r = s.outEdges(t, t)) != null ? r : []).length > 0 : false;
429
454
  });
430
455
  }
431
456
  var X = () => 1;
432
- function D(s, e, t) {
433
- return Z(s, e || X, t || function(n) {
434
- return s.outEdges(n);
457
+ function O(s, e, t) {
458
+ return Z(s, e || X, t || function(r) {
459
+ var i;
460
+ return (i = s.outEdges(r)) != null ? i : [];
435
461
  });
436
462
  }
437
463
  function Z(s, e, t) {
438
- let n = {}, i = s.nodes();
439
- return i.forEach(function(r) {
440
- n[r] = {}, n[r][r] = { distance: 0, predecessor: "" }, i.forEach(function(o) {
441
- r !== o && (n[r][o] = { distance: Number.POSITIVE_INFINITY, predecessor: "" });
442
- }), t(r).forEach(function(o) {
443
- let d = o.v === r ? o.w : o.v, a = e(o);
444
- n[r][d] = { distance: a, predecessor: r };
464
+ let r = {}, i = s.nodes();
465
+ return i.forEach(function(n) {
466
+ let o = {};
467
+ r[n] = o, o[n] = { distance: 0, predecessor: "" }, i.forEach(function(d) {
468
+ n !== d && (o[d] = { distance: Number.POSITIVE_INFINITY, predecessor: "" });
469
+ }), t(n).forEach(function(d) {
470
+ let h = d.v === n ? d.w : d.v, c = e(d);
471
+ o[h] = { distance: c, predecessor: n };
445
472
  });
446
- }), i.forEach(function(r) {
447
- let o = n[r];
448
- i.forEach(function(d) {
449
- let a = n[d];
450
- i.forEach(function(c) {
451
- let h = a[r], u = o[c], g = a[c], f = h.distance + u.distance;
452
- f < g.distance && (g.distance = f, g.predecessor = u.predecessor);
473
+ }), i.forEach(function(n) {
474
+ let o = r[n];
475
+ o && i.forEach(function(d) {
476
+ let h = r[d];
477
+ h && i.forEach(function(c) {
478
+ let a = h[n], u = o[c], g = h[c];
479
+ if (a && u && g) {
480
+ let f = a.distance + u.distance;
481
+ f < g.distance && (g.distance = f, g.predecessor = u.predecessor);
482
+ }
453
483
  });
454
484
  });
455
- }), n;
485
+ }), r;
456
486
  }
457
487
  var l = class extends Error {
458
- constructor(...e) {
459
- super(...e);
488
+ constructor(e) {
489
+ super(e), this.name = "CycleException";
460
490
  }
461
491
  };
462
- function L(s) {
463
- let e = {}, t = {}, n = [];
464
- function i(r) {
465
- if (r in t) throw new l();
466
- r in e || (t[r] = true, e[r] = true, s.predecessors(r).forEach(i), delete t[r], n.push(r));
492
+ function w(s) {
493
+ let e = {}, t = {}, r = [];
494
+ function i(n) {
495
+ var o;
496
+ if (n in t) throw new l();
497
+ n in e || (t[n] = true, e[n] = true, (o = s.predecessors(n)) == null || o.forEach(i), delete t[n], r.push(n));
467
498
  }
468
499
  if (s.sinks().forEach(i), Object.keys(e).length !== s.nodeCount()) throw new l();
469
- return n;
500
+ return r;
470
501
  }
471
- function O(s) {
502
+ function j(s) {
472
503
  try {
473
- L(s);
504
+ w(s);
474
505
  } catch (e) {
475
506
  if (e instanceof l) return false;
476
507
  throw e;
477
508
  }
478
509
  return true;
479
510
  }
480
- function j(s, e, t, n, i) {
511
+ function C(s, e, t, r, i) {
481
512
  Array.isArray(e) || (e = [e]);
482
- let r = ((d) => {
483
- var a;
484
- return (a = s.isDirected() ? s.successors(d) : s.neighbors(d)) != null ? a : [];
513
+ let n = ((d) => {
514
+ var h;
515
+ return (h = s.isDirected() ? s.successors(d) : s.neighbors(d)) != null ? h : [];
485
516
  }), o = {};
486
517
  return e.forEach(function(d) {
487
518
  if (!s.hasNode(d)) throw new Error("Graph does not have node: " + d);
488
- i = C(s, d, t === "post", o, r, n, i);
519
+ i = T(s, d, t === "post", o, n, r, i);
489
520
  }), i;
490
521
  }
491
- function C(s, e, t, n, i, r, o) {
492
- return e in n || (n[e] = true, t || (o = r(o, e)), i(e).forEach(function(d) {
493
- o = C(s, d, t, n, i, r, o);
494
- }), t && (o = r(o, e))), o;
522
+ function T(s, e, t, r, i, n, o) {
523
+ return e in r || (r[e] = true, t || (o = n(o, e)), i(e).forEach(function(d) {
524
+ o = T(s, d, t, r, i, n, o);
525
+ }), t && (o = n(o, e))), o;
495
526
  }
496
- function w(s, e, t) {
497
- return j(s, e, t, function(n, i) {
498
- return n.push(i), n;
527
+ function N(s, e, t) {
528
+ return C(s, e, t, function(r, i) {
529
+ return r.push(i), r;
499
530
  }, []);
500
531
  }
501
- function T(s, e) {
502
- return w(s, e, "post");
503
- }
504
532
  function A(s, e) {
505
- return w(s, e, "pre");
533
+ return N(s, e, "post");
506
534
  }
507
535
  function W(s, e) {
508
- let t = new p(), n = {}, i = new _(), r;
536
+ return N(s, e, "pre");
537
+ }
538
+ function S(s, e) {
539
+ var c;
540
+ let t = new p(), r = {}, i = new b(), n;
509
541
  function o(a) {
510
- let c = a.v === r ? a.w : a.v, h = i.priority(c);
511
- if (h !== void 0) {
512
- let u = e(a);
513
- u < h && (n[c] = r, i.decrease(c, u));
542
+ let u = a.v === n ? a.w : a.v, g = i.priority(u);
543
+ if (g !== void 0) {
544
+ let f = e(a);
545
+ f < g && (r[u] = n, i.decrease(u, f));
514
546
  }
515
547
  }
516
548
  if (s.nodeCount() === 0) return t;
517
549
  s.nodes().forEach(function(a) {
518
550
  i.add(a, Number.POSITIVE_INFINITY), t.setNode(a);
519
- }), i.decrease(s.nodes()[0], 0);
520
- let d = false;
551
+ });
552
+ let d = s.nodes()[0];
553
+ d !== void 0 && i.decrease(d, 0);
554
+ let h = false;
521
555
  for (; i.size() > 0; ) {
522
- if (r = i.removeMin(), r in n) t.setEdge(r, n[r]);
556
+ if (n = i.removeMin(), n in r) t.setEdge(n, r[n]);
523
557
  else {
524
- if (d) throw new Error("Input graph is not connected: " + s);
525
- d = true;
558
+ if (h) throw new Error("Input graph is not connected: " + s);
559
+ h = true;
526
560
  }
527
- s.nodeEdges(r).forEach(o);
561
+ (c = s.nodeEdges(n)) == null || c.forEach(o);
528
562
  }
529
563
  return t;
530
564
  }
531
- function S(s, e, t, n) {
532
- return ee(s, e, t, n != null ? n : ((i) => {
533
- let r = s.outEdges(i);
534
- return r != null ? r : [];
565
+ function M(s, e, t, r) {
566
+ return ee(s, e, t, r != null ? r : ((i) => {
567
+ var n;
568
+ return (n = s.outEdges(i)) != null ? n : [];
535
569
  }));
536
570
  }
537
- function ee(s, e, t, n) {
538
- if (t === void 0) return E(s, e, t, n);
539
- let i = false, r = s.nodes();
540
- for (let o = 0; o < r.length; o++) {
541
- let d = n(r[o]);
542
- for (let a = 0; a < d.length; a++) {
543
- let c = d[a], h = c.v === r[o] ? c.v : c.w, u = h === c.v ? c.w : c.v;
544
- t({ v: h, w: u }) < 0 && (i = true);
571
+ function ee(s, e, t, r) {
572
+ if (t === void 0) return E(s, e, t, r);
573
+ let i = false, n = s.nodes();
574
+ for (let o = 0; o < n.length; o++) {
575
+ let d = n[o];
576
+ if (d === void 0) continue;
577
+ let h = r(d);
578
+ for (let c = 0; c < h.length; c++) {
579
+ let a = h[c];
580
+ if (!a) continue;
581
+ let u = a.v === d ? a.v : a.w, g = u === a.v ? a.w : a.v;
582
+ t({ v: u, w: g }) < 0 && (i = true);
545
583
  }
546
- if (i) return m(s, e, t, n);
584
+ if (i) return y(s, e, t, r);
547
585
  }
548
- return E(s, e, t, n);
586
+ return E(s, e, t, r);
549
587
  }
550
588
 
589
+ // lib/graph-lib.ts
590
+ var Graph = p;
591
+
551
592
  // lib/util.ts
552
593
  function addDummyNode(graph, type, attrs, name) {
553
594
  let v2 = name;
@@ -559,7 +600,7 @@ var dagre = (() => {
559
600
  return v2;
560
601
  }
561
602
  function simplify(graph) {
562
- const simplified = new p().setGraph(graph.graph());
603
+ const simplified = new Graph().setGraph(graph.graph());
563
604
  graph.nodes().forEach((v2) => simplified.setNode(v2, graph.node(v2)));
564
605
  graph.edges().forEach((e) => {
565
606
  const simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
@@ -572,7 +613,7 @@ var dagre = (() => {
572
613
  return simplified;
573
614
  }
574
615
  function asNonCompoundGraph(graph) {
575
- const simplified = new p({ multigraph: graph.isMultigraph() }).setGraph(graph.graph());
616
+ const simplified = new Graph({ multigraph: graph.isMultigraph() }).setGraph(graph.graph());
576
617
  graph.nodes().forEach((v2) => {
577
618
  if (!graph.children(v2).length) {
578
619
  simplified.setNode(v2, graph.node(v2));
@@ -769,9 +810,34 @@ var dagre = (() => {
769
810
  }, {});
770
811
  }
771
812
  var GRAPH_NODE = "\0";
813
+ function compareByOldOrder(oldNodes, nodeA, nodeB) {
814
+ var _a, _b, _c, _d, _e, _f;
815
+ if (!(oldNodes && nodeA && nodeB && nodeA.dummy === "edge" && nodeB.dummy === "edge" && nodeA.edgeObj && nodeB.edgeObj && oldNodes[nodeA.edgeObj.v] && oldNodes[nodeB.edgeObj.v] && oldNodes[nodeA.edgeObj.w] && oldNodes[nodeB.edgeObj.w])) {
816
+ return 0;
817
+ }
818
+ let isForward = true;
819
+ if (nodeA.edgeObj.w === nodeB.edgeObj.w) {
820
+ isForward = false;
821
+ }
822
+ const targetRank = isForward ? (_b = (_a = oldNodes[nodeA.edgeObj.v]) == null ? void 0 : _a.rank) != null ? _b : NaN + 1 : (_d = (_c = oldNodes[nodeA.edgeObj.w]) == null ? void 0 : _c.rank) != null ? _d : NaN - 1;
823
+ const oldA = Object.entries(oldNodes).find((n) => {
824
+ var _a2, _b2;
825
+ return ((_a2 = n[1].edgeObj) == null ? void 0 : _a2.v) === nodeA.edgeObj.v && ((_b2 = n[1].edgeObj) == null ? void 0 : _b2.w) === nodeA.edgeObj.w && n[1].rank === targetRank;
826
+ });
827
+ const oldB = Object.entries(oldNodes).find((n) => {
828
+ var _a2, _b2;
829
+ return ((_a2 = n[1].edgeObj) == null ? void 0 : _a2.v) === nodeB.edgeObj.v && ((_b2 = n[1].edgeObj) == null ? void 0 : _b2.w) === nodeB.edgeObj.w && n[1].rank === targetRank;
830
+ });
831
+ if (!oldA || !oldB) {
832
+ return 0;
833
+ }
834
+ const oldOrderA = (_e = oldA[1].order) != null ? _e : NaN;
835
+ const oldOrderB = (_f = oldB[1].order) != null ? _f : NaN;
836
+ return isNaN(oldOrderA - oldOrderB) ? 0 : oldOrderA - oldOrderB;
837
+ }
772
838
 
773
839
  // lib/version.ts
774
- var version = "3.0.0";
840
+ var version = "3.1.1";
775
841
 
776
842
  // lib/data/list.ts
777
843
  var List = class {
@@ -883,7 +949,7 @@ var dagre = (() => {
883
949
  return results;
884
950
  }
885
951
  function buildState(graph, weightFn) {
886
- const fasGraph = new p();
952
+ const fasGraph = new Graph();
887
953
  let maxIn = 0;
888
954
  let maxOut = 0;
889
955
  graph.nodes().forEach((v2) => {
@@ -925,8 +991,8 @@ var dagre = (() => {
925
991
  }
926
992
 
927
993
  // lib/acyclic.ts
928
- function run(graph) {
929
- const fas = graph.graph().acyclicer === "greedy" ? greedyFAS(graph, weightFn(graph)) : dfsFAS(graph);
994
+ function run(graph, oldGraph) {
995
+ const fas = graph.graph().acyclicer === "greedy" ? greedyFAS(graph, weightFn(graph)) : dfsFAS(graph, oldGraph != null ? oldGraph : null);
930
996
  fas.forEach((e) => {
931
997
  const label = graph.edge(e);
932
998
  graph.removeEdge(e);
@@ -940,7 +1006,7 @@ var dagre = (() => {
940
1006
  };
941
1007
  }
942
1008
  }
943
- function dfsFAS(graph) {
1009
+ function dfsFAS(graph, oldGraph) {
944
1010
  const fas = [];
945
1011
  const stack = {};
946
1012
  const visited = {};
@@ -959,7 +1025,29 @@ var dagre = (() => {
959
1025
  });
960
1026
  delete stack[v2];
961
1027
  }
962
- graph.nodes().forEach(dfs2);
1028
+ function dfsDynamic(v2) {
1029
+ var _a;
1030
+ if (Object.hasOwn(visited, v2)) {
1031
+ return;
1032
+ }
1033
+ visited[v2] = true;
1034
+ stack[v2] = true;
1035
+ (_a = graph.outEdges(v2)) == null ? void 0 : _a.forEach((e) => {
1036
+ var _a2, _b;
1037
+ if (Object.hasOwn(stack, e.w) || ((_a2 = oldGraph.node(v2)) == null ? void 0 : _a2.rank) > ((_b = oldGraph.node(e.w)) == null ? void 0 : _b.rank) && isReachedFromStartWithoutEdge(graph, e.w, e)) {
1038
+ fas.push(e);
1039
+ } else {
1040
+ dfsDynamic(e.w);
1041
+ }
1042
+ });
1043
+ delete stack[v2];
1044
+ }
1045
+ let dfsFn = dfs2;
1046
+ if (oldGraph && typeof oldGraph.node === "function") {
1047
+ dfsFn = dfsDynamic;
1048
+ }
1049
+ graph.sources().forEach(dfsFn);
1050
+ graph.nodes().forEach(dfsFn);
963
1051
  return fas;
964
1052
  }
965
1053
  function undo(graph) {
@@ -974,6 +1062,25 @@ var dagre = (() => {
974
1062
  }
975
1063
  });
976
1064
  }
1065
+ function isReachedFromStartWithoutEdge(graph, node, ignoreEdge) {
1066
+ const localVisited = /* @__PURE__ */ new Set();
1067
+ function reverseDfs(v2) {
1068
+ var _a;
1069
+ if (graph.sources().includes(v2)) {
1070
+ return true;
1071
+ }
1072
+ localVisited.add(v2);
1073
+ for (const e of (_a = graph.inEdges(v2)) != null ? _a : []) {
1074
+ if (!(e.v === ignoreEdge.v && e.w === ignoreEdge.w) && !localVisited.has(e.v)) {
1075
+ if (reverseDfs(e.v)) {
1076
+ return true;
1077
+ }
1078
+ }
1079
+ }
1080
+ return false;
1081
+ }
1082
+ return reverseDfs(node);
1083
+ }
977
1084
 
978
1085
  // lib/normalize.ts
979
1086
  function run2(graph) {
@@ -1070,7 +1177,7 @@ var dagre = (() => {
1070
1177
  // lib/rank/feasible-tree.ts
1071
1178
  var feasible_tree_default = feasibleTree;
1072
1179
  function feasibleTree(graph) {
1073
- const tree = new p({ directed: false });
1180
+ const tree = new Graph({ directed: false });
1074
1181
  const nodes = graph.nodes();
1075
1182
  if (nodes.length === 0) {
1076
1183
  throw new Error("Graph must have at least one node");
@@ -1124,7 +1231,7 @@ var dagre = (() => {
1124
1231
  }
1125
1232
 
1126
1233
  // lib/rank/network-simplex.ts
1127
- var { preorder, postorder } = v;
1234
+ var { preorder, postorder } = F;
1128
1235
  var network_simplex_default = networkSimplex;
1129
1236
  networkSimplex.initLowLimValues = initLowLimValues;
1130
1237
  networkSimplex.initCutValues = initCutValues;
@@ -1316,7 +1423,10 @@ var dagre = (() => {
1316
1423
  var parent_dummy_chains_default = parentDummyChains;
1317
1424
  function parentDummyChains(graph) {
1318
1425
  const postorderNums = postorder2(graph);
1319
- graph.graph().dummyChains.forEach((v2) => {
1426
+ const graphData = graph.graph();
1427
+ if (!Array.isArray(graphData.dummyChains)) return;
1428
+ const dummyChains = graphData.dummyChains;
1429
+ dummyChains.forEach((v2) => {
1320
1430
  let node = graph.node(v2);
1321
1431
  const edgeObj = node.edgeObj;
1322
1432
  const pathData = findPath(graph, postorderNums, edgeObj.v, edgeObj.w);
@@ -1388,7 +1498,9 @@ var dagre = (() => {
1388
1498
  graph.graph().nestingRoot = root;
1389
1499
  graph.edges().forEach((e) => graph.edge(e).minlen *= nodeSep);
1390
1500
  const weight = sumWeights(graph) + 1;
1391
- graph.children(GRAPH_NODE).forEach((child) => dfs(graph, root, nodeSep, weight, height, depths, child));
1501
+ graph.children(GRAPH_NODE).forEach((child) => {
1502
+ dfs(graph, root, nodeSep, weight, height, depths, child);
1503
+ });
1392
1504
  graph.graph().nodeRankFactor = nodeSep;
1393
1505
  }
1394
1506
  function dfs(graph, root, nodeSep, weight, height, depths, v2) {
@@ -1466,7 +1578,7 @@ var dagre = (() => {
1466
1578
  if (children.length) {
1467
1579
  children.forEach(dfs2);
1468
1580
  }
1469
- if (Object.hasOwn(node, "minRank")) {
1581
+ if (node && Object.hasOwn(node, "minRank")) {
1470
1582
  node.borderLeft = [];
1471
1583
  node.borderRight = [];
1472
1584
  for (let rank2 = node.minRank, maxRank2 = node.maxRank + 1; rank2 < maxRank2; ++rank2) {
@@ -1548,7 +1660,7 @@ var dagre = (() => {
1548
1660
  }
1549
1661
 
1550
1662
  // lib/order/init-order.ts
1551
- function initOrder(graph) {
1663
+ function initOrder(graph, oldNodes = null) {
1552
1664
  const visited = {};
1553
1665
  const simpleNodes = graph.nodes().filter((v2) => !graph.children(v2).length);
1554
1666
  const simpleNodesRanks = simpleNodes.map((v2) => graph.node(v2).rank);
@@ -1561,11 +1673,17 @@ var dagre = (() => {
1561
1673
  layers[node.rank].push(v2);
1562
1674
  const successors = graph.successors(v2);
1563
1675
  if (successors) {
1564
- successors.forEach(dfs2);
1676
+ const sortedSuccessors = [...successors].sort((a, b2) => compareOldOrder(a, b2));
1677
+ sortedSuccessors.forEach(dfs2);
1565
1678
  }
1566
1679
  }
1567
1680
  const orderedVs = simpleNodes.sort((a, b2) => graph.node(a).rank - graph.node(b2).rank);
1568
1681
  orderedVs.forEach(dfs2);
1682
+ function compareOldOrder(a, b2) {
1683
+ const nodeA = graph.node(a);
1684
+ const nodeB = graph.node(b2);
1685
+ return compareByOldOrder(oldNodes, nodeA, nodeB);
1686
+ }
1569
1687
  return layers;
1570
1688
  }
1571
1689
 
@@ -1709,7 +1827,19 @@ var dagre = (() => {
1709
1827
  }
1710
1828
 
1711
1829
  // lib/order/sort.ts
1712
- function sort(entries, biasRight) {
1830
+ function sort(entries, reversedPairs, oldNodes, graph, biasRight) {
1831
+ let resolvedReversedPairs = {};
1832
+ let resolvedOldNodes = null;
1833
+ let resolvedGraph = null;
1834
+ let resolvedBiasRight = biasRight;
1835
+ if (typeof reversedPairs === "boolean") {
1836
+ resolvedBiasRight = reversedPairs;
1837
+ resolvedReversedPairs = {};
1838
+ } else if (reversedPairs) {
1839
+ resolvedReversedPairs = reversedPairs;
1840
+ resolvedOldNodes = oldNodes != null ? oldNodes : null;
1841
+ resolvedGraph = graph != null ? graph : null;
1842
+ }
1713
1843
  const parts = partition(entries, (entry) => {
1714
1844
  return Object.hasOwn(entry, "barycenter");
1715
1845
  });
@@ -1719,7 +1849,11 @@ var dagre = (() => {
1719
1849
  let sum = 0;
1720
1850
  let weight = 0;
1721
1851
  let vsIndex = 0;
1722
- sortable.sort(compareWithBias(!!biasRight));
1852
+ sortable.sort(compareWithOldOrder(resolvedGraph, resolvedOldNodes, !!resolvedBiasRight));
1853
+ for (const [key, value] of Object.entries(resolvedReversedPairs)) {
1854
+ const keyIndex = sortable.findIndex((entry) => entry.vs[0] === key);
1855
+ sortable.splice(keyIndex + 1, 0, value);
1856
+ }
1723
1857
  vsIndex = consumeUnsortable(vs, unsortable, vsIndex);
1724
1858
  sortable.forEach((entry) => {
1725
1859
  vsIndex += entry.vs.length;
@@ -1744,19 +1878,36 @@ var dagre = (() => {
1744
1878
  }
1745
1879
  return index;
1746
1880
  }
1747
- function compareWithBias(bias) {
1881
+ function compareWithOldOrder(graph, oldNodes, bias) {
1748
1882
  return (entryV, entryW) => {
1749
1883
  if (entryV.barycenter < entryW.barycenter) {
1750
1884
  return -1;
1751
1885
  } else if (entryV.barycenter > entryW.barycenter) {
1752
1886
  return 1;
1753
1887
  }
1888
+ if (graph && (typeof entryV.vs[0] === "string" || typeof entryW.vs[0] === "string")) {
1889
+ const nodeV = graph.node(entryV.vs[0]);
1890
+ const nodeW = graph.node(entryW.vs[0]);
1891
+ const byOldOrder = compareByOldOrder(oldNodes, nodeV, nodeW);
1892
+ if (byOldOrder !== 0) {
1893
+ return byOldOrder;
1894
+ }
1895
+ }
1754
1896
  return !bias ? entryV.i - entryW.i : entryW.i - entryV.i;
1755
1897
  };
1756
1898
  }
1757
1899
 
1758
1900
  // lib/order/sort-subgraph.ts
1759
- function sortSubgraph(graph, v2, constraintGraph, biasRight) {
1901
+ function sortSubgraph(graph, v2, constraintGraph, oldNodes, biasRight) {
1902
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1903
+ let resolvedOldNodes = null;
1904
+ let resolvedBiasRight = biasRight;
1905
+ if (typeof oldNodes === "boolean") {
1906
+ resolvedBiasRight = oldNodes;
1907
+ resolvedOldNodes = null;
1908
+ } else if (oldNodes !== void 0) {
1909
+ resolvedOldNodes = oldNodes;
1910
+ }
1760
1911
  let movable = graph.children(v2);
1761
1912
  const node = graph.node(v2);
1762
1913
  const bl = node ? node.borderLeft : void 0;
@@ -1768,7 +1919,7 @@ var dagre = (() => {
1768
1919
  const barycenters = barycenter(graph, movable);
1769
1920
  barycenters.forEach((entry) => {
1770
1921
  if (graph.children(entry.v).length) {
1771
- const subgraphResult = sortSubgraph(graph, entry.v, constraintGraph, biasRight);
1922
+ const { result: subgraphResult } = sortSubgraph(graph, entry.v, constraintGraph, resolvedOldNodes, resolvedBiasRight);
1772
1923
  subgraphs[entry.v] = subgraphResult;
1773
1924
  if (Object.hasOwn(subgraphResult, "barycenter")) {
1774
1925
  mergeBarycenters(entry, subgraphResult);
@@ -1777,7 +1928,36 @@ var dagre = (() => {
1777
1928
  });
1778
1929
  const entries = resolveConflicts(barycenters, constraintGraph);
1779
1930
  expandSubgraphs(entries, subgraphs);
1780
- const result = sort(entries, biasRight);
1931
+ const reversedPairs = {};
1932
+ let usedBias = false;
1933
+ for (let i = 0; i < entries.length; i++) {
1934
+ for (let j2 = i + 1; j2 < entries.length; j2++) {
1935
+ if (!entries[i] || !entries[j2] || !((_a = entries[i]) == null ? void 0 : _a.barycenter) || !((_b = entries[j2]) == null ? void 0 : _b.barycenter)) {
1936
+ continue;
1937
+ }
1938
+ if (((_c = entries[i]) == null ? void 0 : _c.barycenter) === entries[j2].barycenter) {
1939
+ const nameI = (_e = (_d = entries[i]) == null ? void 0 : _d.vs[0]) != null ? _e : "";
1940
+ const nameJ = (_g = (_f = entries[j2]) == null ? void 0 : _f.vs[0]) != null ? _g : "";
1941
+ const nodeI = graph.node(nameI);
1942
+ const nodeJ = graph.node(nameJ);
1943
+ if (nodeI.dummy === "edge" && nodeJ.dummy === "edge" && ((_h = nodeI.edgeObj) == null ? void 0 : _h.v) === ((_i = nodeJ.edgeObj) == null ? void 0 : _i.v) && ((_j = nodeI.edgeObj) == null ? void 0 : _j.w) === ((_k = nodeJ.edgeObj) == null ? void 0 : _k.w)) {
1944
+ if (nodeI.edgeLabel.reversed) {
1945
+ reversedPairs[nameJ] = entries[i];
1946
+ entries.splice(i, 1);
1947
+ i--;
1948
+ break;
1949
+ } else {
1950
+ reversedPairs[nameI] = entries[j2];
1951
+ entries.splice(j2, 1);
1952
+ j2--;
1953
+ }
1954
+ } else {
1955
+ usedBias = true;
1956
+ }
1957
+ }
1958
+ }
1959
+ }
1960
+ const result = sort(entries, reversedPairs, resolvedOldNodes, graph, resolvedBiasRight);
1781
1961
  if (bl && br) {
1782
1962
  result.vs = [bl, result.vs, br].flat(1);
1783
1963
  const blPredecessors = graph.predecessors(bl);
@@ -1793,6 +1973,8 @@ var dagre = (() => {
1793
1973
  result.weight += 2;
1794
1974
  }
1795
1975
  }
1976
+ Object.defineProperty(result, "result", { value: result, enumerable: false, configurable: true, writable: true });
1977
+ Object.defineProperty(result, "usedBias", { value: usedBias, enumerable: false, configurable: true, writable: true });
1796
1978
  return result;
1797
1979
  }
1798
1980
  function expandSubgraphs(entries, subgraphs) {
@@ -1821,7 +2003,7 @@ var dagre = (() => {
1821
2003
  nodesWithRank = graph.nodes();
1822
2004
  }
1823
2005
  const root = createRootNode(graph);
1824
- const result = new p({ compound: true }).setGraph({ root }).setDefaultNodeLabel((v2) => graph.node(v2));
2006
+ const result = new Graph({ compound: true }).setGraph({ root }).setDefaultNodeLabel((v2) => graph.node(v2));
1825
2007
  nodesWithRank.forEach((v2) => {
1826
2008
  const node = graph.node(v2);
1827
2009
  const parent = graph.parent(v2);
@@ -1880,7 +2062,7 @@ var dagre = (() => {
1880
2062
  }
1881
2063
 
1882
2064
  // lib/order/index.ts
1883
- function order(graph, opts = {}) {
2065
+ function order(graph, opts = {}, oldNodes = null) {
1884
2066
  if (typeof opts.customOrder === "function") {
1885
2067
  opts.customOrder(graph, order);
1886
2068
  return;
@@ -1888,7 +2070,7 @@ var dagre = (() => {
1888
2070
  const maxRank2 = maxRank(graph);
1889
2071
  const downLayerGraphs = buildLayerGraphs(graph, range(1, maxRank2 + 1), "inEdges");
1890
2072
  const upLayerGraphs = buildLayerGraphs(graph, range(maxRank2 - 1, -1, -1), "outEdges");
1891
- let layering = initOrder(graph);
2073
+ let layering = initOrder(graph, oldNodes);
1892
2074
  assignOrder(graph, layering);
1893
2075
  if (opts.disableOptimalOrderHeuristic) {
1894
2076
  return;
@@ -1897,7 +2079,7 @@ var dagre = (() => {
1897
2079
  let best;
1898
2080
  const constraints = opts.constraints || [];
1899
2081
  for (let i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
1900
- sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2, constraints);
2082
+ sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2, constraints, oldNodes);
1901
2083
  layering = buildLayerMatrix(graph);
1902
2084
  const cc = crossCount(graph, layering);
1903
2085
  if (cc < bestCC) {
@@ -1935,12 +2117,16 @@ var dagre = (() => {
1935
2117
  return buildLayerGraph(graph, rank2, relationship, nodesByRank.get(rank2) || []);
1936
2118
  });
1937
2119
  }
1938
- function sweepLayerGraphs(layerGraphs, biasRight, constraints) {
1939
- const cg = new p();
2120
+ function sweepLayerGraphs(layerGraphs, switchBias, constraints, oldNodes) {
2121
+ let biasRight = true;
2122
+ const cg = new Graph();
1940
2123
  layerGraphs.forEach(function(lg) {
1941
2124
  constraints.forEach((con) => cg.setEdge(con.left, con.right));
1942
2125
  const root = lg.graph().root;
1943
- const sorted = sortSubgraph(lg, root, cg, biasRight);
2126
+ const { result: sorted, usedBias } = sortSubgraph(lg, root, cg, oldNodes, biasRight);
2127
+ if (switchBias && usedBias) {
2128
+ biasRight = !biasRight;
2129
+ }
1944
2130
  sorted.vs.forEach((v2, i) => lg.node(v2).order = i);
1945
2131
  addSubgraphConstraints(lg, cg, sorted.vs);
1946
2132
  });
@@ -2057,7 +2243,7 @@ var dagre = (() => {
2057
2243
  const conflictsV = conflicts[v2];
2058
2244
  return conflictsV !== void 0 && Object.hasOwn(conflictsV, w2);
2059
2245
  }
2060
- function verticalAlignment(graph, layering, conflicts, neighborFn) {
2246
+ function verticalAlignment(graph, layering, conflicts, neighborFn, corePath) {
2061
2247
  const root = {};
2062
2248
  const align = {};
2063
2249
  const pos = {};
@@ -2070,9 +2256,25 @@ var dagre = (() => {
2070
2256
  });
2071
2257
  layering.forEach((layer) => {
2072
2258
  let prevIdx = -1;
2073
- layer.forEach((v2) => {
2074
- const wsRaw = neighborFn(v2);
2259
+ let alreadyAlignedIndex = -1;
2260
+ let hasShifted = false;
2261
+ let modifiedLayer = layer;
2262
+ const coreNodeIndex = layer.findIndex((v2) => {
2263
+ return (corePath == null ? void 0 : corePath.includes(v2)) || isInCorePath(v2, graph, corePath);
2264
+ });
2265
+ if (coreNodeIndex > 0) {
2266
+ modifiedLayer = [layer[coreNodeIndex], ...layer.slice(0, coreNodeIndex), ...layer.slice(coreNodeIndex + 1)];
2267
+ hasShifted = true;
2268
+ }
2269
+ modifiedLayer.forEach((v2) => {
2270
+ var _a;
2271
+ let wsRaw = neighborFn(v2);
2075
2272
  if (wsRaw && wsRaw.length) {
2273
+ if (corePath == null ? void 0 : corePath.includes(v2)) {
2274
+ wsRaw = wsRaw.filter((w2) => {
2275
+ return isInCorePath(w2, graph, corePath);
2276
+ });
2277
+ }
2076
2278
  const ws = wsRaw.sort((a, b2) => {
2077
2279
  const posA = pos[a];
2078
2280
  const posB = pos[b2];
@@ -2083,12 +2285,17 @@ var dagre = (() => {
2083
2285
  const w2 = ws[i];
2084
2286
  if (w2 === void 0) continue;
2085
2287
  const posW = pos[w2];
2086
- if (posW !== void 0 && align[v2] === v2 && prevIdx < posW && !hasConflict(conflicts, v2, w2)) {
2288
+ if (posW !== void 0 && align[v2] === v2 && prevIdx < posW && pos[w2] !== alreadyAlignedIndex && !hasConflict(conflicts, v2, w2)) {
2087
2289
  const rootW = root[w2];
2088
2290
  if (rootW !== void 0) {
2089
2291
  align[w2] = v2;
2090
2292
  align[v2] = root[v2] = rootW;
2091
2293
  prevIdx = posW;
2294
+ if (hasShifted) {
2295
+ prevIdx = -1;
2296
+ alreadyAlignedIndex = (_a = pos[w2]) != null ? _a : -1;
2297
+ hasShifted = false;
2298
+ }
2092
2299
  }
2093
2300
  }
2094
2301
  }
@@ -2164,7 +2371,7 @@ var dagre = (() => {
2164
2371
  return xs;
2165
2372
  }
2166
2373
  function buildBlockGraph(graph, layering, root, reverseSep) {
2167
- const blockGraph = new p();
2374
+ const blockGraph = new Graph();
2168
2375
  const graphLabel = graph.graph();
2169
2376
  const sepFn = sep(graphLabel.nodesep, graphLabel.edgesep, reverseSep);
2170
2377
  layering.forEach((layer) => {
@@ -2243,7 +2450,7 @@ var dagre = (() => {
2243
2450
  return (((_a = xs[1]) != null ? _a : 0) + ((_b = xs[2]) != null ? _b : 0)) / 2;
2244
2451
  });
2245
2452
  }
2246
- function positionX(graph) {
2453
+ function positionX(graph, corePath) {
2247
2454
  const layering = buildLayerMatrix(graph);
2248
2455
  const conflicts = Object.assign(
2249
2456
  findType1Conflicts(graph, layering),
@@ -2263,7 +2470,7 @@ var dagre = (() => {
2263
2470
  const result = vert === "u" ? graph.predecessors(v2) : graph.successors(v2);
2264
2471
  return result || [];
2265
2472
  };
2266
- const align = verticalAlignment(graph, adjustedLayering, conflicts, neighborFn);
2473
+ const align = verticalAlignment(graph, adjustedLayering, conflicts, neighborFn, corePath);
2267
2474
  let xs = horizontalCompaction(
2268
2475
  graph,
2269
2476
  adjustedLayering,
@@ -2324,12 +2531,21 @@ var dagre = (() => {
2324
2531
  function width(graph, v2) {
2325
2532
  return graph.node(v2).width;
2326
2533
  }
2534
+ function isInCorePath(v2, graph, corePath) {
2535
+ var _a;
2536
+ if (!corePath) return false;
2537
+ const edge = (_a = graph.node(v2)) == null ? void 0 : _a.edgeObj;
2538
+ if (!edge || graph.node(v2).edgeLabel.reversed) return false;
2539
+ const sourceIndex = corePath.indexOf(edge == null ? void 0 : edge.v);
2540
+ const targetIndex = corePath.indexOf(edge == null ? void 0 : edge.w);
2541
+ return sourceIndex !== -1 && targetIndex !== -1 && sourceIndex === (targetIndex + 1) % corePath.length || sourceIndex === (targetIndex - 1) % corePath.length;
2542
+ }
2327
2543
 
2328
2544
  // lib/position/index.ts
2329
- function position(graph) {
2545
+ function position(graph, corePath) {
2330
2546
  graph = asNonCompoundGraph(graph);
2331
2547
  positionY(graph);
2332
- Object.entries(positionX(graph)).forEach(([v2, x2]) => graph.node(v2).x = x2);
2548
+ Object.entries(positionX(graph, corePath)).forEach(([v2, x2]) => graph.node(v2).x = x2);
2333
2549
  }
2334
2550
  function positionY(graph) {
2335
2551
  const layering = buildLayerMatrix(graph);
@@ -2362,19 +2578,234 @@ var dagre = (() => {
2362
2578
  }
2363
2579
 
2364
2580
  // lib/layout.ts
2581
+ var previousLayouts = /* @__PURE__ */ new WeakMap();
2365
2582
  function layout(g, opts = {}) {
2366
- const time2 = opts.debugTiming ? time : notime;
2367
- return time2("layout", () => {
2368
- const layoutGraph = time2(" buildLayoutGraph", () => buildLayoutGraph(g));
2369
- time2(" runLayout", () => runLayout(layoutGraph, time2, opts));
2370
- time2(" updateInputGraph", () => updateInputGraph(g, layoutGraph));
2371
- return layoutGraph;
2583
+ recursiveClusterLayout(g, notime, opts);
2584
+ return g;
2585
+ }
2586
+ function topLevelChildOf(g, node, cluster) {
2587
+ let current = node;
2588
+ while (current !== void 0) {
2589
+ const parent = g.parent(current);
2590
+ if (parent === cluster) return current;
2591
+ current = parent;
2592
+ }
2593
+ return void 0;
2594
+ }
2595
+ function recursiveClusterLayout(g, time2, opts) {
2596
+ var _a;
2597
+ const clusterNodes = g.nodes().filter((v2) => g.children(v2).length);
2598
+ const clusterBounds = {};
2599
+ clusterNodes.forEach((v2) => {
2600
+ const node = g.node(v2);
2601
+ if (node && node.rankdir) {
2602
+ const subgraph = new Graph({ multigraph: true, compound: true });
2603
+ subgraph.setGraph({ rankdir: node.rankdir });
2604
+ const children = g.children(v2);
2605
+ children.forEach((childId) => {
2606
+ const childNode = { ...g.node(childId) };
2607
+ subgraph.setNode(childId, childNode);
2608
+ const parent = g.parent(childId);
2609
+ if (parent && parent !== v2 && children.includes(parent)) {
2610
+ subgraph.setParent(childId, parent);
2611
+ }
2612
+ });
2613
+ const proxiedEdges = /* @__PURE__ */ new Set();
2614
+ g.edges().forEach((e) => {
2615
+ const srcChild = topLevelChildOf(g, e.v, v2);
2616
+ const tgtChild = topLevelChildOf(g, e.w, v2);
2617
+ if (srcChild && tgtChild && srcChild !== tgtChild) {
2618
+ const key = `${srcChild}\0${tgtChild}`;
2619
+ if (!proxiedEdges.has(key)) {
2620
+ proxiedEdges.add(key);
2621
+ subgraph.setEdge(srcChild, tgtChild, { ...g.edge(e) });
2622
+ }
2623
+ }
2624
+ });
2625
+ recursiveClusterLayout(subgraph, time2, opts);
2626
+ const subLayoutG = buildLayoutGraph(subgraph);
2627
+ runLayout(subLayoutG, time2, opts, null);
2628
+ updateInputGraph(subgraph, subLayoutG);
2629
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
2630
+ subgraph.nodes().forEach((u) => {
2631
+ if (u === v2) return;
2632
+ const n = subgraph.node(u);
2633
+ if (n && typeof n.x === "number" && typeof n.y === "number" && typeof n.width === "number" && typeof n.height === "number") {
2634
+ minX = Math.min(minX, n.x - n.width / 2);
2635
+ maxX = Math.max(maxX, n.x + n.width / 2);
2636
+ minY = Math.min(minY, n.y - n.height / 2);
2637
+ maxY = Math.max(maxY, n.y + n.height / 2);
2638
+ }
2639
+ });
2640
+ if (!isFinite(minX) || !isFinite(minY) || !isFinite(maxX) || !isFinite(maxY)) {
2641
+ minX = minY = 0;
2642
+ maxX = maxY = 0;
2643
+ }
2644
+ const width2 = maxX - minX;
2645
+ const height = maxY - minY;
2646
+ clusterBounds[v2] = {
2647
+ minX,
2648
+ minY,
2649
+ maxX,
2650
+ maxY,
2651
+ width: width2,
2652
+ height,
2653
+ offsetX: minX,
2654
+ offsetY: minY
2655
+ };
2656
+ node._dagreClusterSubgraph = subgraph;
2657
+ }
2658
+ });
2659
+ const isolatedClusters = [];
2660
+ const getAllDescendants = (nodeId) => {
2661
+ const result2 = [];
2662
+ const queue = (g.children(nodeId) || []).filter((c) => c !== nodeId);
2663
+ while (queue.length > 0) {
2664
+ const curr = queue.shift();
2665
+ result2.push(curr);
2666
+ (g.children(curr) || []).filter((c) => c !== curr).forEach((c) => queue.push(c));
2667
+ }
2668
+ return result2;
2669
+ };
2670
+ const rawClusterMap = /* @__PURE__ */ new Map();
2671
+ clusterNodes.forEach((v2) => {
2672
+ const node = g.node(v2);
2673
+ if (node && node.rankdir && clusterBounds[v2]) {
2674
+ rawClusterMap.set(v2, (g.children(v2) || []).filter((c) => c !== v2));
2675
+ }
2676
+ });
2677
+ const allDirectChildrenFlat = new Set([...rawClusterMap.values()].flat());
2678
+ const clusterChildrenMap = /* @__PURE__ */ new Map();
2679
+ rawClusterMap.forEach((_2, v2) => {
2680
+ if (!allDirectChildrenFlat.has(v2)) {
2681
+ clusterChildrenMap.set(v2, getAllDescendants(v2));
2682
+ }
2683
+ });
2684
+ const allIsolatedChildren = new Set(
2685
+ [...clusterChildrenMap.values()].flat()
2686
+ );
2687
+ const topLevelNode = (id) => {
2688
+ for (const [cId, ch] of clusterChildrenMap) {
2689
+ if (ch.includes(id)) return cId;
2690
+ }
2691
+ return id;
2692
+ };
2693
+ const allSavedEdges = [];
2694
+ g.edges().forEach((e) => {
2695
+ if (allIsolatedChildren.has(e.v) || allIsolatedChildren.has(e.w)) {
2696
+ allSavedEdges.push({ edge: e, label: g.edge(e) });
2697
+ }
2698
+ });
2699
+ const savedParents = /* @__PURE__ */ new Map();
2700
+ allIsolatedChildren.forEach((id) => {
2701
+ const p2 = g.parent(id);
2702
+ savedParents.set(id, typeof p2 === "string" ? p2 : void 0);
2703
+ });
2704
+ clusterChildrenMap.forEach((children, v2) => {
2705
+ const node = g.node(v2);
2706
+ const removedNodes = [];
2707
+ children.forEach((childId) => {
2708
+ const childNode = g.node(childId);
2709
+ if (!childNode) return;
2710
+ removedNodes.push({ id: childId, node: childNode, parent: savedParents.get(childId) });
2711
+ g.removeNode(childId);
2712
+ });
2713
+ const removedEdges = allSavedEdges.filter(
2714
+ ({ edge }) => children.includes(edge.v) || children.includes(edge.w)
2715
+ );
2716
+ const bounds = clusterBounds[v2];
2717
+ if (!node) return;
2718
+ isolatedClusters.push({
2719
+ clusterId: v2,
2720
+ subgraph: node._dagreClusterSubgraph,
2721
+ bounds,
2722
+ children,
2723
+ removedNodes,
2724
+ removedEdges
2725
+ });
2726
+ node.width = bounds.width;
2727
+ node.height = bounds.height;
2728
+ });
2729
+ const proxyEdgeKeys = /* @__PURE__ */ new Set();
2730
+ allSavedEdges.forEach(({ edge, label }) => {
2731
+ const effV = topLevelNode(edge.v);
2732
+ const effW = topLevelNode(edge.w);
2733
+ if (effV !== effW && g.hasNode(effV) && g.hasNode(effW)) {
2734
+ const key = `${effV}\0${effW}`;
2735
+ if (!proxyEdgeKeys.has(key)) {
2736
+ proxyEdgeKeys.add(key);
2737
+ g.setEdge(effV, effW, { ...label, width: 0, height: 0 });
2738
+ }
2739
+ }
2740
+ });
2741
+ const layoutG = buildLayoutGraph(g);
2742
+ const result = runLayout(layoutG, time2, opts, (_a = previousLayouts.get(g)) != null ? _a : null);
2743
+ previousLayouts.set(g, result);
2744
+ updateInputGraph(g, layoutG);
2745
+ proxyEdgeKeys.forEach((key) => {
2746
+ const sep2 = key.indexOf("\0");
2747
+ const effV = key.slice(0, sep2);
2748
+ const effW = key.slice(sep2 + 1);
2749
+ if (g.hasEdge(effV, effW)) g.removeEdge(effV, effW);
2750
+ });
2751
+ isolatedClusters.forEach(({ clusterId, subgraph, bounds, removedNodes, removedEdges }) => {
2752
+ var _a2, _b;
2753
+ const clusterNode = g.node(clusterId);
2754
+ const clusterX = (_a2 = clusterNode == null ? void 0 : clusterNode.x) != null ? _a2 : 0;
2755
+ const clusterY = (_b = clusterNode == null ? void 0 : clusterNode.y) != null ? _b : 0;
2756
+ const subgraphCenterX = (bounds.minX + bounds.maxX) / 2;
2757
+ const subgraphCenterY = (bounds.minY + bounds.maxY) / 2;
2758
+ removedNodes.forEach(({ id, node, parent }) => {
2759
+ g.setNode(id, node);
2760
+ if (parent !== void 0) g.setParent(id, parent);
2761
+ });
2762
+ removedEdges.forEach(({ edge, label }) => {
2763
+ g.setEdge(edge, label);
2764
+ });
2765
+ subgraph.nodes().forEach((u) => {
2766
+ if (u === clusterId) return;
2767
+ const subNode = subgraph.node(u);
2768
+ const mainNode = g.node(u);
2769
+ if (mainNode && subNode && typeof subNode.x === "number" && typeof subNode.y === "number") {
2770
+ mainNode.x = clusterX + (subNode.x - subgraphCenterX);
2771
+ mainNode.y = clusterY + (subNode.y - subgraphCenterY);
2772
+ }
2773
+ });
2774
+ delete clusterNode._dagreClusterSubgraph;
2775
+ });
2776
+ clusterNodes.forEach((v2) => {
2777
+ var _a2, _b;
2778
+ const node = g.node(v2);
2779
+ const bounds = clusterBounds[v2];
2780
+ if (node && node.rankdir && node._dagreClusterSubgraph && bounds) {
2781
+ const subgraph = node._dagreClusterSubgraph;
2782
+ const parentX = (_a2 = node.x) != null ? _a2 : 0;
2783
+ const parentY = (_b = node.y) != null ? _b : 0;
2784
+ const subgraphCenterX = (bounds.minX + bounds.maxX) / 2;
2785
+ const subgraphCenterY = (bounds.minY + bounds.maxY) / 2;
2786
+ subgraph.nodes().forEach((u) => {
2787
+ if (u === v2) return;
2788
+ const subNode = subgraph.node(u);
2789
+ const mainNode = g.node(u);
2790
+ if (mainNode && subNode && typeof subNode.x === "number" && typeof subNode.y === "number") {
2791
+ const dx = subNode.x - subgraphCenterX;
2792
+ const dy = subNode.y - subgraphCenterY;
2793
+ mainNode.x = parentX + dx;
2794
+ mainNode.y = parentY + dy;
2795
+ }
2796
+ });
2797
+ delete node._dagreClusterSubgraph;
2798
+ }
2372
2799
  });
2373
2800
  }
2374
- function runLayout(g, time2, opts) {
2801
+ function runLayout(g, time2, opts, previous = null) {
2802
+ var _a, _b;
2803
+ const dynamic = (opts == null ? void 0 : opts.useDynamic) !== false;
2804
+ const oldGraph = dynamic ? (_a = previous == null ? void 0 : previous.graph) != null ? _a : null : null;
2805
+ const rawOldNodes = dynamic ? (_b = previous == null ? void 0 : previous.rawNodes) != null ? _b : null : null;
2375
2806
  time2(" makeSpaceForEdgeLabels", () => makeSpaceForEdgeLabels(g));
2376
2807
  time2(" removeSelfEdges", () => removeSelfEdges(g));
2377
- time2(" acyclic", () => run(g));
2808
+ time2(" acyclic", () => run(g, oldGraph));
2378
2809
  time2(" nestingGraph.run", () => run3(g));
2379
2810
  time2(" rank", () => rank_default(asNonCompoundGraph(g)));
2380
2811
  time2(" injectEdgeLabelProxies", () => injectEdgeLabelProxies(g));
@@ -2386,11 +2817,12 @@ var dagre = (() => {
2386
2817
  time2(" normalize.run", () => run2(g));
2387
2818
  time2(" parentDummyChains", () => parent_dummy_chains_default(g));
2388
2819
  time2(" addBorderSegments", () => add_border_segments_default(g));
2389
- time2(" order", () => order(g, opts));
2820
+ time2(" order", () => order(g, opts, rawOldNodes));
2390
2821
  time2(" insertSelfEdges", () => insertSelfEdges(g));
2391
2822
  time2(" adjustCoordinateSystem", () => adjust(g));
2392
- time2(" position", () => position(g));
2823
+ time2(" position", () => position(g, opts.corePath));
2393
2824
  time2(" positionSelfEdges", () => positionSelfEdges(g));
2825
+ const rawNodes = JSON.parse(JSON.stringify(g._nodes));
2394
2826
  time2(" removeBorderNodes", () => removeBorderNodes(g));
2395
2827
  time2(" normalize.undo", () => undo2(g));
2396
2828
  time2(" fixupEdgeLabelCoords", () => fixupEdgeLabelCoords(g));
@@ -2399,6 +2831,7 @@ var dagre = (() => {
2399
2831
  time2(" assignNodeIntersects", () => assignNodeIntersects(g));
2400
2832
  time2(" reversePoints", () => reversePointsForReversedEdges(g));
2401
2833
  time2(" acyclic.undo", () => undo(g));
2834
+ return { graph: g, rawNodes };
2402
2835
  }
2403
2836
  function updateInputGraph(inputGraph, layoutGraph) {
2404
2837
  inputGraph.nodes().forEach((v2) => {
@@ -2443,7 +2876,7 @@ var dagre = (() => {
2443
2876
  };
2444
2877
  var edgeAttrs = ["labelpos"];
2445
2878
  function buildLayoutGraph(inputGraph) {
2446
- const g = new p({ multigraph: true, compound: true });
2879
+ const g = new Graph({ multigraph: true, compound: true });
2447
2880
  const graph = canonicalize(inputGraph.graph());
2448
2881
  g.setGraph(Object.assign(
2449
2882
  {},
@@ -2480,9 +2913,10 @@ var dagre = (() => {
2480
2913
  const graph = g.graph();
2481
2914
  graph.ranksep /= 2;
2482
2915
  g.edges().forEach((e) => {
2916
+ var _a;
2483
2917
  const edge = g.edge(e);
2484
2918
  edge.minlen *= 2;
2485
- if (edge.labelpos.toLowerCase() !== "c") {
2919
+ if (((_a = edge.labelpos) != null ? _a : "r").toLowerCase() !== "c") {
2486
2920
  if (graph.rankdir === "TB" || graph.rankdir === "BT") {
2487
2921
  edge.width += edge.labeloffset;
2488
2922
  } else {
@@ -2574,6 +3008,7 @@ var dagre = (() => {
2574
3008
  }
2575
3009
  function assignNodeIntersects(g) {
2576
3010
  g.edges().forEach((e) => {
3011
+ if (e.v === e.w) return;
2577
3012
  const edge = g.edge(e);
2578
3013
  const nodeV = g.node(e.v);
2579
3014
  const nodeW = g.node(e.w);
@@ -2654,6 +3089,7 @@ var dagre = (() => {
2654
3089
  let orderShift = 0;
2655
3090
  layer.forEach((v2, i) => {
2656
3091
  const node = g.node(v2);
3092
+ if (typeof node.rank !== "number") node.rank = 0;
2657
3093
  node.order = i + orderShift;
2658
3094
  (node.selfEdges || []).forEach((selfEdge) => {
2659
3095
  addDummyNode(g, "selfedge", {
@@ -2662,8 +3098,19 @@ var dagre = (() => {
2662
3098
  rank: node.rank,
2663
3099
  order: i + ++orderShift,
2664
3100
  e: selfEdge.e,
2665
- label: selfEdge.label
3101
+ edgeLabel: selfEdge.label
2666
3102
  }, "_se");
3103
+ if (!Array.isArray(selfEdge.label.points) || selfEdge.label.points.length !== 7) {
3104
+ selfEdge.label.points = [
3105
+ { x: 0, y: -10 },
3106
+ { x: 0, y: -10 },
3107
+ { x: 0, y: 0 },
3108
+ { x: 0, y: 10 },
3109
+ { x: 0, y: 10 },
3110
+ { x: 0, y: 0 },
3111
+ { x: 0, y: 0 }
3112
+ ];
3113
+ }
2667
3114
  });
2668
3115
  delete node.selfEdges;
2669
3116
  });
@@ -2672,24 +3119,51 @@ var dagre = (() => {
2672
3119
  function positionSelfEdges(g) {
2673
3120
  g.nodes().forEach((v2) => {
2674
3121
  const node = g.node(v2);
3122
+ const valid = (val) => typeof val === "number" && isFinite(val);
2675
3123
  if (node.dummy === "selfedge") {
2676
3124
  const selfEdgeNode = node;
2677
3125
  const selfNode = g.node(selfEdgeNode.e.v);
2678
- const x2 = selfNode.x + selfNode.width / 2;
2679
- const y2 = selfNode.y;
2680
- const dx = node.x - x2;
2681
- const dy = selfNode.height / 2;
2682
- g.setEdge(selfEdgeNode.e, selfEdgeNode.label);
2683
- g.removeNode(v2);
2684
- selfEdgeNode.label.points = [
2685
- { x: x2 + 2 * dx / 3, y: y2 - dy },
2686
- { x: x2 + 5 * dx / 6, y: y2 - dy },
2687
- { x: x2 + dx, y: y2 },
2688
- { x: x2 + 5 * dx / 6, y: y2 + dy },
2689
- { x: x2 + 2 * dx / 3, y: y2 + dy }
3126
+ const xVal = valid(selfNode == null ? void 0 : selfNode.x) ? selfNode.x : 0;
3127
+ const yVal = valid(selfNode == null ? void 0 : selfNode.y) ? selfNode.y : 0;
3128
+ const widthVal = valid(selfNode == null ? void 0 : selfNode.width) ? selfNode.width : 0;
3129
+ const heightVal = valid(selfNode == null ? void 0 : selfNode.height) ? selfNode.height : 0;
3130
+ const nodeX = valid(node.x) ? node.x : xVal;
3131
+ const nodeY = valid(node.y) ? node.y : yVal;
3132
+ const dx = widthVal / 2;
3133
+ const dy = heightVal / 2;
3134
+ selfEdgeNode.edgeLabel.points = [
3135
+ { x: nodeX + dx, y: nodeY - dy },
3136
+ { x: nodeX + dx, y: nodeY - dy },
3137
+ { x: nodeX, y: nodeY },
3138
+ { x: nodeX - dx, y: nodeY + dy },
3139
+ { x: nodeX - dx, y: nodeY + dy },
3140
+ { x: nodeX, y: nodeY },
3141
+ { x: nodeX, y: nodeY }
2690
3142
  ];
2691
- selfEdgeNode.label.x = node.x;
2692
- selfEdgeNode.label.y = node.y;
3143
+ selfEdgeNode.edgeLabel.x = nodeX;
3144
+ selfEdgeNode.edgeLabel.y = nodeY;
3145
+ g.setEdge(selfEdgeNode.e, selfEdgeNode.edgeLabel);
3146
+ g.removeNode(v2);
3147
+ } else if (node && Array.isArray(node.selfEdges)) {
3148
+ node.selfEdges.forEach((selfEdge) => {
3149
+ if (!Array.isArray(selfEdge.label.points) || selfEdge.label.points.length !== 7) {
3150
+ const xVal = valid(node.x) ? node.x : 0;
3151
+ const yVal = valid(node.y) ? node.y : 0;
3152
+ const widthVal = valid(node.width) ? node.width : 0;
3153
+ const heightVal = valid(node.height) ? node.height : 0;
3154
+ const dx = widthVal / 2;
3155
+ const dy = heightVal / 2;
3156
+ selfEdge.label.points = [
3157
+ { x: xVal + dx, y: yVal - dy },
3158
+ { x: xVal + dx, y: yVal - dy },
3159
+ { x: xVal, y: yVal },
3160
+ { x: xVal - dx, y: yVal + dy },
3161
+ { x: xVal - dx, y: yVal + dy },
3162
+ { x: xVal, y: yVal },
3163
+ { x: xVal, y: yVal }
3164
+ ];
3165
+ }
3166
+ });
2693
3167
  }
2694
3168
  });
2695
3169
  }
@@ -2712,7 +3186,7 @@ var dagre = (() => {
2712
3186
  // lib/debug.ts
2713
3187
  function debugOrdering(graph) {
2714
3188
  const layerMatrix = buildLayerMatrix(graph);
2715
- const h = new p({ compound: true, multigraph: true }).setGraph({});
3189
+ const h = new Graph({ compound: true, multigraph: true }).setGraph({});
2716
3190
  graph.nodes().forEach((node) => {
2717
3191
  h.setNode(node, { label: node });
2718
3192
  h.setParent(node, "layer" + graph.node(node).rank);
@@ -2732,7 +3206,7 @@ var dagre = (() => {
2732
3206
  // index.ts
2733
3207
  var util = { time, notime };
2734
3208
  var dagre = {
2735
- graphlib: graphlib_esm_exports,
3209
+ graphlib: graph_lib_exports,
2736
3210
  version,
2737
3211
  layout,
2738
3212
  debug: debugOrdering,