@ktrysmt/beautiful-mermaid 1.5.1 → 1.5.2

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.js CHANGED
@@ -3947,6 +3947,7 @@ function createMapping(graph) {
3947
3947
  primaryRoots.push(root);
3948
3948
  }
3949
3949
  }
3950
+ const sourceStartLevel = computeSourceStartLevels(graph, primaryRoots);
3950
3951
  const rootsByTarget = /* @__PURE__ */ new Map();
3951
3952
  for (const root of primaryRoots) {
3952
3953
  const children = getChildren(graph, root);
@@ -3957,9 +3958,10 @@ function createMapping(graph) {
3957
3958
  }
3958
3959
  for (const [, roots] of rootsByTarget) {
3959
3960
  for (const node of roots) {
3960
- const requested = dir === "LR" ? { x: 0, y: highestPositionPerLevel[0] } : { x: highestPositionPerLevel[0], y: 0 };
3961
+ const startLevel = sourceStartLevel.get(node.name) ?? 0;
3962
+ const requested = dir === "LR" ? { x: startLevel, y: highestPositionPerLevel[startLevel] } : { x: highestPositionPerLevel[startLevel], y: startLevel };
3961
3963
  reserveSpotInGrid(graph, graph.nodes[node.index], requested);
3962
- highestPositionPerLevel[0] = highestPositionPerLevel[0] + 4;
3964
+ highestPositionPerLevel[startLevel] = highestPositionPerLevel[startLevel] + 4;
3963
3965
  }
3964
3966
  }
3965
3967
  if (shouldSeparate && subgraphRootNodes.length > 0) {
@@ -4108,6 +4110,62 @@ function createMapping(graph) {
4108
4110
  function getEdgesFromNode(graph, node) {
4109
4111
  return graph.edges.filter((e) => e.from.name === node.name);
4110
4112
  }
4113
+ function computeSourceStartLevels(graph, sources) {
4114
+ const childrenByName = /* @__PURE__ */ new Map();
4115
+ const parentsByName = /* @__PURE__ */ new Map();
4116
+ const inDegree = /* @__PURE__ */ new Map();
4117
+ for (const n of graph.nodes) {
4118
+ childrenByName.set(n.name, []);
4119
+ parentsByName.set(n.name, []);
4120
+ inDegree.set(n.name, 0);
4121
+ }
4122
+ for (const e of graph.edges) {
4123
+ if (e.from.name === e.to.name) continue;
4124
+ childrenByName.get(e.from.name).push(e.to);
4125
+ parentsByName.get(e.to.name).push(e.from);
4126
+ inDegree.set(e.to.name, (inDegree.get(e.to.name) ?? 0) + 1);
4127
+ }
4128
+ const queue = [];
4129
+ for (const n of graph.nodes) {
4130
+ if ((inDegree.get(n.name) ?? 0) === 0) queue.push(n);
4131
+ }
4132
+ const topoOrder = [];
4133
+ while (queue.length > 0) {
4134
+ const n = queue.shift();
4135
+ topoOrder.push(n);
4136
+ for (const c of childrenByName.get(n.name) ?? []) {
4137
+ const d = (inDegree.get(c.name) ?? 0) - 1;
4138
+ inDegree.set(c.name, d);
4139
+ if (d === 0) queue.push(c);
4140
+ }
4141
+ }
4142
+ const result = /* @__PURE__ */ new Map();
4143
+ if (topoOrder.length < graph.nodes.length) return result;
4144
+ const forwardLevel = /* @__PURE__ */ new Map();
4145
+ for (const n of topoOrder) {
4146
+ let best = 0;
4147
+ for (const p of parentsByName.get(n.name) ?? []) {
4148
+ const pl = (forwardLevel.get(p.name) ?? 0) + 1;
4149
+ if (pl > best) best = pl;
4150
+ }
4151
+ forwardLevel.set(n.name, best);
4152
+ }
4153
+ for (const src of sources) {
4154
+ const children = getChildren(graph, src).filter((c) => c.name !== src.name);
4155
+ if (children.length === 0) {
4156
+ result.set(src.name, 0);
4157
+ continue;
4158
+ }
4159
+ let minChild = Infinity;
4160
+ for (const c of children) {
4161
+ const cl = forwardLevel.get(c.name) ?? 0;
4162
+ if (cl < minChild) minChild = cl;
4163
+ }
4164
+ const logical = Number.isFinite(minChild) ? Math.max(0, minChild - 1) : 0;
4165
+ result.set(src.name, logical * 4);
4166
+ }
4167
+ return result;
4168
+ }
4111
4169
  function getChildren(graph, node) {
4112
4170
  return getEdgesFromNode(graph, node).map((e) => e.to);
4113
4171
  }