AI-native design editor. Open-source Figma alternative.
fix(core): spread a pile of layers the same way every time (#1711)
Danila Poyarkov309c7046c43b
diff --git a/changes/1711/fixed.md b/changes/1711/fixed.mdnew file mode 100644index 000000000..83f0400d9--- /dev/null+++ b/changes/1711/fixed.md@@ -0,0 +1 @@+- Spread and Compact arrange a pile of layers the same way every time, rather than differently on each run.diff --git a/packages/core/src/layout/arrange/boxes.ts b/packages/core/src/layout/arrange/boxes.tsindex 15d2e7ea8..9de459966 100644--- a/packages/core/src/layout/arrange/boxes.ts+++ b/packages/core/src/layout/arrange/boxes.ts@@ -243,7 +243,43 @@ function layoutOptions(input: ArrangeInput): Record<string, string> {}}+/**+ * SPOrE's importer moves a box whose centre another box already has with an unseeded+ * `Math.random()`, and SPOrE ignores `elk.randomSeed`, so a pile would spread differently on every+ * run (ElkGraphImporter.java, eclipse-elk/elk b715fb1, lines 166-172). Doing that move here,+ * deterministically, means ELK never reaches it. Remove once elkjs seeds it+ * (https://github.com/eclipse-elk/elk/issues/1211). `NUDGE` is the first+ * move's length in pixels.+ */+const NUDGE = 0.5+/** Each further move turns by the golden angle, so the moved centres do not fall on one line. */+const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5))++/** Gives every box in each container, at every level, a centre of its own. */+function separateCentres(container: ElkNode): void {+ const repeats = new Map<string, number>()+ for (const node of container.children ?? []) {+ separateCentres(node)+ const x = (node.x ?? 0) + (node.width ?? 0) / 2+ const y = (node.y ?? 0) + (node.height ?? 0) / 2+ const key = `${x},${y}`+ const repeat = repeats.get(key) ?? 0+ repeats.set(key, repeat + 1)+ if (repeat === 0) continue+ const distance = NUDGE * Math.sqrt(repeat)+ node.x = (node.x ?? 0) + distance * Math.cos(repeat * GOLDEN_ANGLE)+ node.y = (node.y ?? 0) + distance * Math.sin(repeat * GOLDEN_ANGLE)+ }+}++/** The graph ELK lays `input` out from. */function elkGraph(input: ArrangeInput): ElkNode {+ const root = elkHierarchy(input)+ if (input.layout === 'spread' || input.layout === 'compact') separateCentres(root)+ return root+}++function elkHierarchy(input: ArrangeInput): ElkNode {const flow = input.layout === 'flow'const [outSide, inSide] = FLOW_SIDES[input.direction ?? 'TB']const options = layoutOptions(input)diff --git a/packages/core/tests/layout/arrange/layers.test.ts b/packages/core/tests/layout/arrange/layers.test.tsindex ec0f23c9d..cb0d91f20 100644--- a/packages/core/tests/layout/arrange/layers.test.ts+++ b/packages/core/tests/layout/arrange/layers.test.ts@@ -135,6 +135,24 @@ describe('arrangeLayers', () => {).rejects.toThrow('must share a parent')})+ test('spreads the same pile the same way every time', async () => {+ const spreadPile = async () => {+ const { graph, box } = pile()+ const layers = ['A', 'B', 'C', 'D', 'E'].map((name) => box(name))+ await arrangeLayers(+ graph,+ layers.map((layer) => layer.id),+ { layout: 'spread' }+ )+ return layers.map((layer) => {+ const { x, y } = node(graph, layer.id)+ return { x, y }+ })+ }+ const first = await spreadPile()+ for (let run = 0; run < 5; run++) expect(await spreadPile()).toEqual(first)+ })+test('spread unpiles layers without connectors and keeps the pile’s corner', async () => {const { graph, box } = pile()const layers = ['A', 'B', 'C', 'D'].map((name) => box(name))