* fix(figma-api): lay out pending edits before geometry reads Scripts read x, y, width, height, transforms, and bounds as they were before the script ran until the tool finished and laid out its changes. Figma lays out on read, so a hugging parent reports its new size right after a child is added. The graph now records the scope of edits outside layout application, and the geometry getters lay that scope out first through the same runner tools use after a call. One recorder exists per graph; a new FigmaAPI starts it afresh because the editor lays out its own edits. * fix(layout): size each axis on its own, as Figma does Fill is stored on the child, as layoutGrow along the parent's primary axis and STRETCH across it, with a grid laid out like a row; primaryAxisSizing and counterAxisSizing only fix or hug. A shared layoutSizing helper in scene-graph reads and writes per-axis sizing, and layout, the Figma API, the properties panel, design JSX, DOM/CSS export, and .pen import use it. This fixes grid children filling both axes when set to fill one, auto-layout children that stretch but kept a fixed size, the Figma API writing Fill to a frame's own sizing (which .fig export dropped), and JSX and HTML exports losing grid and cross-axis fill. Behavior was checked against live Figma, and imported layouts were compared with the geometry stored in material3.fig and nuxtui.fig. * fix(layout): keep pending edits until laid out and opt out of inherited stretch A new FigmaAPI cleared the edits recorded on its graph, so after a script failed before its tool laid out its changes, the next script read stale geometry; edits now stay recorded until a read lays them out, and return to the record if that layout throws. Setting a child to Fixed or Hug across a parent that stretches every child left it filling; it now opts out with MIN, as frame presets do.
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import { FigmaAPI } from '@open-pencil/core/figma-api'
|
|
import { SceneGraph } from '@open-pencil/scene-graph'
|
|
|
|
function createAPI(): FigmaAPI {
|
|
return new FigmaAPI(new SceneGraph())
|
|
}
|
|
|
|
function hugRow(api: ReturnType<typeof createAPI>) {
|
|
const row = api.createFrame()
|
|
row.layoutMode = 'HORIZONTAL'
|
|
row.primaryAxisSizingMode = 'AUTO'
|
|
row.counterAxisSizingMode = 'AUTO'
|
|
row.itemSpacing = 10
|
|
return row
|
|
}
|
|
|
|
describe('geometry reads after edits', () => {
|
|
test('a hugging parent reports its new size as soon as a child is added or resized', () => {
|
|
const api = createAPI()
|
|
const row = hugRow(api)
|
|
const first = api.createRectangle()
|
|
first.resize(40, 20)
|
|
row.appendChild(first)
|
|
expect([row.width, row.height]).toEqual([40, 20])
|
|
|
|
const second = api.createRectangle()
|
|
second.resize(60, 30)
|
|
row.appendChild(second)
|
|
expect([row.width, row.height]).toEqual([110, 30])
|
|
expect(second.x).toBe(50)
|
|
|
|
first.resize(80, 20)
|
|
expect(second.x).toBe(90)
|
|
expect(second.absoluteBoundingBox.x).toBe(row.absoluteBoundingBox.x + 90)
|
|
expect(row.width).toBe(150)
|
|
})
|
|
|
|
test('removing a child and changing spacing reflow the rest before the next read', () => {
|
|
const api = createAPI()
|
|
const row = hugRow(api)
|
|
const children = [40, 50, 60].map((width) => {
|
|
const child = api.createRectangle()
|
|
child.resize(width, 20)
|
|
row.appendChild(child)
|
|
return child
|
|
})
|
|
children[0].remove()
|
|
expect(children[1].x).toBe(0)
|
|
row.itemSpacing = 0
|
|
expect(children[2].x).toBe(50)
|
|
expect(row.width).toBe(110)
|
|
})
|
|
|
|
test('edits stay recorded when another API opens the same graph before a read', () => {
|
|
const api = createAPI()
|
|
const row = hugRow(api)
|
|
const child = api.createRectangle()
|
|
child.resize(40, 20)
|
|
row.appendChild(child)
|
|
expect(row.width).toBe(40)
|
|
|
|
// A script resizes the child and fails before its tool lays the edit out.
|
|
child.resize(90, 20)
|
|
const next = new FigmaAPI(api.graph)
|
|
expect(next.getNodeById(row.id)?.width).toBe(90)
|
|
})
|
|
|
|
test('reading again without edits does no layout work', () => {
|
|
const api = createAPI()
|
|
const row = hugRow(api)
|
|
const child = api.createRectangle()
|
|
row.appendChild(child)
|
|
expect(row.width).toBe(child.width)
|
|
|
|
let updates = 0
|
|
const unbind = api.graph.onNodeEvents({ updated: () => updates++ })
|
|
void row.width
|
|
void child.x
|
|
void row.absoluteTransform
|
|
unbind()
|
|
expect(updates).toBe(0)
|
|
})
|
|
})
|