Skip to content

AI-native design editor. Open-source Figma alternative.

fix(figma-api): size groups and boolean operations to their contents (#775)

mrhard9090134a6ba910be
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bda115f75..0327d60d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@
### Fixed
+- Size groups and boolean operations made through the AI and MCP `group_nodes` and `boolean_*` tools to what they contain, as the editor's commands already do, instead of a default 100 × 100 box or the first operand's box (#738).
- Keep a layer where it is drawn when it moves into or out of a rotated or flipped parent, instead of shifting it and leaving it at its old angle (#737).
- Size auto-width text from `.pen` files to its content in CLI exports, instead of a 10000px placeholder that stretched hugging frames in HTML and Storybook output, and keep narrow widths a `.pen` file sets explicitly instead of widening multi-character text.
- Keep grid layouts, rotation, inner shadows, every shadow of a layer, layer and background blur, flex grow, right-to-left direction, and sections in HTML export, which previously turned grids into columns and dropped the rest.
diff --git a/packages/core/src/editor/structure.ts b/packages/core/src/editor/structure.ts
index 8e0951586..a8cf988a3 100644
--- a/packages/core/src/editor/structure.ts
+++ b/packages/core/src/editor/structure.ts
@@ -50,11 +50,11 @@ export function createStructureActions(ctx: EditorContext) {
selectedNodes: SceneNode[],
extraProps?: Partial<SceneNode>
) {
- return wrapSelectionInContainerImpl(ctx, isTopLevel, containerType, selectedNodes, extraProps)
+ return wrapSelectionInContainerImpl(ctx, containerType, selectedNodes, extraProps)
}
function wrapInAutoLayout(selectedNodes: SceneNode[]) {
- wrapInAutoLayoutImpl(ctx, isTopLevel, selectedNodes)
+ wrapInAutoLayoutImpl(ctx, selectedNodes)
}
function groupSelected(selectedNodes: SceneNode[]) {
@@ -68,7 +68,7 @@ export function createStructureActions(ctx: EditorContext) {
}
function booleanOperationSelected(selectedNodes: SceneNode[], operation: BooleanOperation) {
- return booleanOperationSelectedImpl(ctx, isTopLevel, selectedNodes, operation)
+ return booleanOperationSelectedImpl(ctx, selectedNodes, operation)
}
function ungroupSelected(selectedNode: SceneNode | undefined) {
diff --git a/packages/core/src/editor/structure/auto-layout-wrap.ts b/packages/core/src/editor/structure/auto-layout-wrap.ts
index 8c92d808a..7e994a30f 100644
--- a/packages/core/src/editor/structure/auto-layout-wrap.ts
+++ b/packages/core/src/editor/structure/auto-layout-wrap.ts
@@ -1,14 +1,10 @@
import type { LayoutMode, SceneNode } from '@open-pencil/scene-graph'
-import { computeAbsoluteBounds } from '@open-pencil/scene-graph/geometry'
+import { getAxisAlignedBoundsInParent } from '@open-pencil/scene-graph/coordinate'
import type { EditorContext } from '#core/editor/types'
import { computeLayout } from '#core/layout'
-export function wrapInAutoLayout(
- ctx: EditorContext,
- isTopLevel: (parentId: string | null) => boolean,
- selectedNodes: SceneNode[]
-) {
+export function wrapInAutoLayout(ctx: EditorContext, selectedNodes: SceneNode[]) {
if (selectedNodes.length === 0) return
const parentId = selectedNodes[0].parentId ?? ctx.state.currentPageId
@@ -20,17 +16,15 @@ export function wrapInAutoLayout(
const prevSelection = new Set(ctx.state.selectedIds)
const origPositions = selectedNodes.map((n) => ({ id: n.id, x: n.x, y: n.y, parentId }))
- const bounds = computeAbsoluteBounds(selectedNodes, (id) => ctx.graph.getAbsolutePosition(id))
-
- const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : ctx.graph.getAbsolutePosition(parentId)
+ const bounds = getAxisAlignedBoundsInParent(selectedNodes, parentId, ctx.graph)
const direction: LayoutMode =
selectedNodes.length <= 1 || bounds.height > bounds.width ? 'VERTICAL' : 'HORIZONTAL'
const frame = ctx.graph.createNode('FRAME', parentId, {
name: 'Frame',
- x: bounds.x - parentAbs.x,
- y: bounds.y - parentAbs.y,
+ x: bounds.x,
+ y: bounds.y,
width: bounds.width,
height: bounds.height,
layoutMode: direction,
diff --git a/packages/core/src/editor/structure/boolean.ts b/packages/core/src/editor/structure/boolean.ts
index c3bae096b..371ed47a4 100644
--- a/packages/core/src/editor/structure/boolean.ts
+++ b/packages/core/src/editor/structure/boolean.ts
@@ -1,6 +1,6 @@
import type { SceneNode } from '@open-pencil/scene-graph'
+import { getAxisAlignedBoundsInParent } from '@open-pencil/scene-graph/coordinate'
import { copyFills, copyStrokes } from '@open-pencil/scene-graph/copy'
-import { computeAbsoluteBounds } from '@open-pencil/scene-graph/geometry'
import { canMakeBooleanSourceNode } from '#core/canvas/boolean'
import { restoreSubtree, snapshotSubtree } from '#core/editor/clipboard/subtree-history'
@@ -12,7 +12,6 @@ export type BooleanOperation = 'UNION' | 'SUBTRACT' | 'INTERSECT' | 'EXCLUDE'
export function booleanOperationSelected(
ctx: EditorContext,
- isTopLevel: (parentId: string | null) => boolean,
selectedNodes: SceneNode[],
operation: BooleanOperation
) {
@@ -26,13 +25,12 @@ export function booleanOperationSelected(
const childSnapshots = childIds.map((id) => ({ id, subtree: snapshotSubtree(ctx.graph, id) }))
const origPositions = topLevel.map((node) => ({ id: node.id, x: node.x, y: node.y }))
const firstIndex = Math.min(...childIds.map((id) => parent.childIds.indexOf(id)))
- const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : ctx.graph.getAbsolutePosition(parentId)
- const bounds = computeAbsoluteBounds(topLevel, (id) => ctx.graph.getAbsolutePosition(id))
+ const bounds = getAxisAlignedBoundsInParent(topLevel, parentId, ctx.graph)
const booleanNode = ctx.graph.createNode('BOOLEAN_OPERATION', parentId, {
name: operationLabel(operation),
- x: bounds.x - parentAbs.x,
- y: bounds.y - parentAbs.y,
+ x: bounds.x,
+ y: bounds.y,
width: bounds.width,
height: bounds.height,
fills: copyFills(topLevel[0].fills),
diff --git a/packages/core/src/editor/structure/container-wrap.ts b/packages/core/src/editor/structure/container-wrap.ts
index 349350305..7bc9f3f65 100644
--- a/packages/core/src/editor/structure/container-wrap.ts
+++ b/packages/core/src/editor/structure/container-wrap.ts
@@ -1,11 +1,10 @@
import type { SceneNode } from '@open-pencil/scene-graph'
-import { computeAbsoluteBounds } from '@open-pencil/scene-graph/geometry'
+import { getAxisAlignedBoundsInParent } from '@open-pencil/scene-graph/coordinate'
import type { EditorContext } from '#core/editor/types'
export function wrapSelectionInContainer(
ctx: EditorContext,
- isTopLevel: (parentId: string | null) => boolean,
containerType: 'GROUP' | 'FRAME' | 'COMPONENT' | 'COMPONENT_SET',
selectedNodes: SceneNode[],
extraProps?: Partial<SceneNode>
@@ -25,16 +24,7 @@ export function wrapSelectionInContainer(
const nodeIds = selectedNodes.map((n) => n.id)
const origPositions = selectedNodes.map((n) => ({ id: n.id, x: n.x, y: n.y }))
- const {
- x: minX,
- y: minY,
- width: bw,
- height: bh
- } = computeAbsoluteBounds(selectedNodes, (id) => ctx.graph.getAbsolutePosition(id))
- const maxX = minX + bw
- const maxY = minY + bh
-
- const parentAbs = isTopLevel(parentId) ? { x: 0, y: 0 } : ctx.graph.getAbsolutePosition(parentId)
+ const bounds = getAxisAlignedBoundsInParent(selectedNodes, parentId, ctx.graph)
const firstIndex = Math.min(...nodeIds.map((id) => parent.childIds.indexOf(id)))
const padding = containerType === 'COMPONENT_SET' ? 40 : 0
@@ -46,10 +36,10 @@ export function wrapSelectionInContainer(
}
const containerNode = ctx.graph.createNode(containerType, parentId, {
name: containerNames[containerType] ?? containerType,
- x: minX - parentAbs.x - padding,
- y: minY - parentAbs.y - padding,
- width: maxX - minX + padding * 2,
- height: maxY - minY + padding * 2,
+ x: bounds.x - padding,
+ y: bounds.y - padding,
+ width: bounds.width + padding * 2,
+ height: bounds.height + padding * 2,
fills:
containerType === 'COMPONENT_SET'
? [
diff --git a/packages/core/src/figma-api/index.ts b/packages/core/src/figma-api/index.ts
index 3706dd110..1fada27cc 100644
--- a/packages/core/src/figma-api/index.ts
+++ b/packages/core/src/figma-api/index.ts
@@ -9,6 +9,7 @@ import type {
VariableType,
VariableValue
} from '@open-pencil/scene-graph'
+import { getAxisAlignedBoundsInParent } from '@open-pencil/scene-graph/coordinate'
import { copyFills, copyStrokes, copyEffects } from '@open-pencil/scene-graph/copy'
import { computeBounds } from '@open-pencil/scene-graph/geometry'
import { computeImageHash } from '@open-pencil/scene-graph/images'
@@ -200,6 +201,13 @@ export class FigmaAPI implements NodeProxyHost {
return (node as BaseNode & { [INTERNAL_ID]: string })[INTERNAL_ID]
}
+ private _rawNode(node: BaseNode | FigmaNodeProxy): CoreSceneNode {
+ const id = this._nodeId(node)
+ const raw = this.graph.getNode(id)
+ if (!raw) throw new Error(`Node ${id} not found`)
+ return raw
+ }
+
group(
nodes: ReadonlyArray<FigmaNodeProxy>,
parent: FigmaNodeProxy,
@@ -212,7 +220,12 @@ export class FigmaAPI implements NodeProxyHost {
index?: number
): FigmaGroupNode {
const parentId = this._nodeId(parent)
- const groupNode = this.graph.createNode('GROUP', parentId)
+ const members = nodes.map((node) => this._rawNode(node))
+ const groupNode = this.graph.createNode(
+ 'GROUP',
+ parentId,
+ members.length > 0 ? getAxisAlignedBoundsInParent(members, parentId, this.graph) : undefined
+ )
for (const n of nodes) {
this.graph.reparentNode(this._nodeId(n), groupNode.id)
}
@@ -376,14 +389,10 @@ export class FigmaAPI implements NodeProxyHost {
): FigmaBooleanOperationNode {
if (nodes.length < 2) throw new Error('Need at least 2 nodes for boolean operation')
const parentId = this._nodeId(parent)
- const first = this.graph.getNode(this._nodeId(nodes[0]))
- if (!first) throw new Error('Node not found')
+ const members = nodes.map((node) => this._rawNode(node))
const group = this.graph.createNode('BOOLEAN_OPERATION', parentId, {
name: `Boolean ${operation.toLowerCase()}`,
- x: first.x,
- y: first.y,
- width: first.width,
- height: first.height,
+ ...getAxisAlignedBoundsInParent(members, parentId, this.graph),
booleanOperation: operation
})
for (const node of nodes) {
diff --git a/packages/scene-graph/src/coordinate.ts b/packages/scene-graph/src/coordinate.ts
index c76d1c252..d6099da3f 100644
--- a/packages/scene-graph/src/coordinate.ts
+++ b/packages/scene-graph/src/coordinate.ts
@@ -22,27 +22,58 @@ export function getWorldMatrix(node: SceneNode, graph: Pick<SceneGraph, 'getNode
return matrix
}
-export function getAxisAlignedWorldBounds(node: SceneNode, graph: Pick<SceneGraph, 'getNode'>) {
- const matrix = getWorldMatrix(node, graph)
- const points = Matrix.mapPoints(matrix, [
- 0,
- 0,
- node.width,
- 0,
- node.width,
- node.height,
- 0,
- node.height
- ])
- const xs = [points[0], points[2], points[4], points[6]]
- const ys = [points[1], points[3], points[5], points[7]]
- const minX = Math.min(...xs)
- const maxX = Math.max(...xs)
- const minY = Math.min(...ys)
- const maxY = Math.max(...ys)
+/** Axis-aligned box around interleaved `x, y` points; empty input gives an empty box at the origin. */
+function boundsOfPoints(points: readonly number[]): Rect {
+ if (points.length === 0) return { x: 0, y: 0, width: 0, height: 0 }
+ let minX = Infinity
+ let minY = Infinity
+ let maxX = -Infinity
+ let maxY = -Infinity
+ for (let i = 0; i < points.length; i += 2) {
+ minX = Math.min(minX, points[i])
+ minY = Math.min(minY, points[i + 1])
+ maxX = Math.max(maxX, points[i])
+ maxY = Math.max(maxY, points[i + 1])
+ }
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY }
}
+function cornersOf(node: Pick<SceneNode, 'width' | 'height'>): number[] {
+ return [0, 0, node.width, 0, node.width, node.height, 0, node.height]
+}
+
+export function getAxisAlignedWorldBounds(node: SceneNode, graph: Pick<SceneGraph, 'getNode'>) {
+ return boundsOfPoints(Matrix.mapPoints(getWorldMatrix(node, graph), cornersOf(node)))
+}
+
+/** World matrix of a parent; the document root and a missing parent are the identity. */
+export function getParentWorldMatrix(
+ parent: SceneNode | undefined,
+ graph: Pick<SceneGraph, 'getNode' | 'rootId'>
+): Mat3 {
+ if (!parent || parent.id === graph.rootId) return Matrix.identity()
+ return getWorldMatrix(parent, graph)
+}
+
+/**
+ * Axis-aligned box of `nodes` in `parentId`'s own axes: where a group, frame, or boolean operation
+ * made from them sits. Each node's corners are mapped through the parent's inverse world matrix,
+ * so a rotated or flipped parent gets a box in its own axes and the nodes keep their drawn places
+ * once they move into the container.
+ */
+export function getAxisAlignedBoundsInParent(
+ nodes: readonly SceneNode[],
+ parentId: string,
+ graph: Pick<SceneGraph, 'getNode' | 'rootId'>
+): Rect {
+ const toParent =
+ Matrix.invert(getParentWorldMatrix(graph.getNode(parentId), graph)) ?? Matrix.identity()
+ const points = nodes.flatMap((node) =>
+ Matrix.mapPoints(Matrix.multiply(toParent, getWorldMatrix(node, graph)), cornersOf(node))
+ )
+ return boundsOfPoints(points)
+}
+
export function getAbsolutePosition(node: SceneNode, graph: SceneGraph): Vector {
const matrix = getWorldMatrix(node, graph)
const p = Matrix.mapPoints(matrix, [0, 0])
@@ -142,31 +173,7 @@ export function getNodeLocalMatrix(n: SceneNode, position: Vector = n) {
return matrix
}
export function getNodeWorldBounds(node: SceneNode) {
- const m = getNodeLocalMatrix(node)
-
- const points = Matrix.mapPoints(m, [0, 0, node.width, 0, node.width, node.height, 0, node.height])
-
- let minX = Infinity
- let minY = Infinity
- let maxX = -Infinity
- let maxY = -Infinity
-
- for (let i = 0; i < points.length; i += 2) {
- const x = points[i]
- const y = points[i + 1]
-
- minX = Math.min(minX, x)
- minY = Math.min(minY, y)
- maxX = Math.max(maxX, x)
- maxY = Math.max(maxY, y)
- }
-
- return {
- x: minX,
- y: minY,
- width: maxX - minX,
- height: maxY - minY
- }
+ return boundsOfPoints(Matrix.mapPoints(getNodeLocalMatrix(node), cornersOf(node)))
}
/**
diff --git a/packages/scene-graph/src/index.ts b/packages/scene-graph/src/index.ts
index 91edde7a2..1d135c278 100644
--- a/packages/scene-graph/src/index.ts
+++ b/packages/scene-graph/src/index.ts
@@ -58,7 +58,7 @@ import type { Emitter } from 'nanoevents'
import {
getAbsolutePosition,
getNodeLocalMatrix,
- getWorldMatrix,
+ getParentWorldMatrix,
isTranslationOnly,
localTransformFromWorld
} from './coordinate'
@@ -537,8 +537,7 @@ export class SceneGraph {
}
private parentWorldMatrix(parent: SceneNode | undefined): Mat3 {
- if (!parent || parent.id === this.rootId) return Matrix.identity()
- return getWorldMatrix(parent, this)
+ return getParentWorldMatrix(parent, this)
}
reorderChild(nodeId: string, parentId: string, insertIndex: number): void {
diff --git a/packages/scene-graph/tests/world-bounds.test.ts b/packages/scene-graph/tests/world-bounds.test.ts
index f96b08c8b..896558dab 100644
--- a/packages/scene-graph/tests/world-bounds.test.ts
+++ b/packages/scene-graph/tests/world-bounds.test.ts
@@ -1,7 +1,10 @@
import { describe, expect, test } from 'bun:test'
import { SceneGraph } from '@open-pencil/scene-graph'
-import { getAxisAlignedWorldBounds } from '@open-pencil/scene-graph/coordinate'
+import {
+ getAxisAlignedBoundsInParent,
+ getAxisAlignedWorldBounds
+} from '@open-pencil/scene-graph/coordinate'
describe('axis-aligned world bounds', () => {
test('includes a node own rotation', () => {
@@ -47,3 +50,82 @@ describe('axis-aligned world bounds', () => {
expect(bounds.height).toBeCloseTo(40, 10)
})
})
+
+describe('axis-aligned bounds in a parent', () => {
+ function twoRectangles(graph: SceneGraph, parentId: string) {
+ return [
+ graph.createNode('RECTANGLE', parentId, { x: 10, y: 20, width: 30, height: 30 }),
+ graph.createNode('RECTANGLE', parentId, { x: 60, y: 70, width: 20, height: 20 })
+ ]
+ }
+
+ test('spans the nodes in an unrotated parent', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const frame = graph.createNode('FRAME', page.id, { x: 100, y: 200, width: 300, height: 300 })
+ const nodes = twoRectangles(graph, frame.id)
+
+ expect(getAxisAlignedBoundsInParent(nodes, frame.id, graph)).toEqual({
+ x: 10,
+ y: 20,
+ width: 70,
+ height: 70
+ })
+ expect(getAxisAlignedBoundsInParent(nodes, page.id, graph)).toEqual({
+ x: 110,
+ y: 220,
+ width: 70,
+ height: 70
+ })
+ })
+
+ test('measures in the axes of a rotated parent', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const frame = graph.createNode('FRAME', page.id, {
+ x: 100,
+ y: 200,
+ width: 300,
+ height: 300,
+ rotation: 90
+ })
+ const nodes = twoRectangles(graph, frame.id)
+
+ const bounds = getAxisAlignedBoundsInParent(nodes, frame.id, graph)
+ expect(bounds.x).toBeCloseTo(10, 6)
+ expect(bounds.y).toBeCloseTo(20, 6)
+ expect(bounds.width).toBeCloseTo(70, 6)
+ expect(bounds.height).toBeCloseTo(70, 6)
+ })
+
+ test('accounts for a rotated node and a flipped parent', () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const frame = graph.createNode('FRAME', page.id, {
+ x: 0,
+ y: 0,
+ width: 200,
+ height: 100,
+ flipX: true
+ })
+ const node = graph.createNode('RECTANGLE', frame.id, {
+ x: 50,
+ y: 25,
+ width: 100,
+ height: 50,
+ rotation: 90
+ })
+
+ const bounds = getAxisAlignedBoundsInParent([node], frame.id, graph)
+ expect(bounds.x).toBeCloseTo(75, 6)
+ expect(bounds.y).toBeCloseTo(0, 6)
+ expect(bounds.width).toBeCloseTo(50, 6)
+ expect(bounds.height).toBeCloseTo(100, 6)
+ expect(getAxisAlignedBoundsInParent([], frame.id, graph)).toEqual({
+ x: 0,
+ y: 0,
+ width: 0,
+ height: 0
+ })
+ })
+})
diff --git a/tests/engine/editor/structure/boolean.test.ts b/tests/engine/editor/structure/boolean.test.ts
index 0d460e6ad..c8ad20e7d 100644
--- a/tests/engine/editor/structure/boolean.test.ts
+++ b/tests/engine/editor/structure/boolean.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, test } from 'bun:test'
import { TRANSPARENT } from '@open-pencil/core/constants'
import { createEditor } from '@open-pencil/core/editor'
+import { getAxisAlignedWorldBounds } from '@open-pencil/scene-graph/coordinate'
describe('booleanOperationSelected', () => {
test('wraps selected nodes in a boolean operation container', () => {
@@ -118,3 +119,68 @@ describe('booleanOperationSelected', () => {
expect(editor.state.selectedIds).toEqual(new Set([booleanId]))
})
})
+
+describe('container placement inside a rotated frame', () => {
+ function rotatedFrameWithChildren() {
+ const editor = createEditor()
+ const pageId = editor.state.currentPageId
+ const frame = editor.graph.createNode('FRAME', pageId, {
+ x: 100,
+ y: 200,
+ width: 300,
+ height: 300,
+ rotation: 90
+ })
+ const first = editor.graph.createNode('RECTANGLE', frame.id, {
+ x: 10,
+ y: 20,
+ width: 30,
+ height: 30
+ })
+ const second = editor.graph.createNode('RECTANGLE', frame.id, {
+ x: 60,
+ y: 70,
+ width: 20,
+ height: 20
+ })
+ const before = [first, second].map((node) => getAxisAlignedWorldBounds(node, editor.graph))
+ return { editor, frame, first, second, before }
+ }
+
+ function expectContainerInFrameAxes(editor: ReturnType<typeof createEditor>, id: string) {
+ const container = editor.graph.getNode(id)
+ expect(container?.x).toBeCloseTo(10, 6)
+ expect(container?.y).toBeCloseTo(20, 6)
+ expect(container?.width).toBeCloseTo(70, 6)
+ expect(container?.height).toBeCloseTo(70, 6)
+ }
+
+ test('a boolean operation sits in the frame axes and keeps its operands where drawn', () => {
+ const { editor, first, second, before } = rotatedFrameWithChildren()
+ editor.select([first.id, second.id])
+ editor.booleanOperationSelected('UNION')
+
+ const [booleanId] = [...editor.state.selectedIds]
+ expectContainerInFrameAxes(editor, booleanId)
+ const after = [first, second].map((node) => getAxisAlignedWorldBounds(node, editor.graph))
+ for (const [index, bounds] of after.entries()) {
+ expect(bounds.x).toBeCloseTo(before[index].x, 6)
+ expect(bounds.y).toBeCloseTo(before[index].y, 6)
+ }
+ })
+
+ test('a group sits in the frame axes and keeps its children where drawn', () => {
+ const { editor, first, second, before } = rotatedFrameWithChildren()
+ editor.select([first.id, second.id])
+ editor.groupSelected()
+
+ const [groupId] = [...editor.state.selectedIds]
+ expect(editor.graph.getNode(groupId)?.type).toBe('GROUP')
+ expectContainerInFrameAxes(editor, groupId)
+ const after = [first, second].map((node) => getAxisAlignedWorldBounds(node, editor.graph))
+ for (const [index, bounds] of after.entries()) {
+ expect(bounds.x).toBeCloseTo(before[index].x, 6)
+ expect(bounds.y).toBeCloseTo(before[index].y, 6)
+ }
+ })
+})
diff --git a/tests/engine/figma/api/grouping-compat.test.ts b/tests/engine/figma/api/grouping-compat.test.ts
index 7b519229f..ad534aa7e 100644
--- a/tests/engine/figma/api/grouping-compat.test.ts
+++ b/tests/engine/figma/api/grouping-compat.test.ts
@@ -17,6 +17,80 @@ describe('FigmaAPI grouping compatibility', () => {
expect(page.children.map((child) => child.id)).toEqual([before.id, group.id])
})
+ test('group spans its children', () => {
+ const api = createAPI()
+ const first = api.createRectangle()
+ first.x = 10
+ first.y = 20
+ first.resize(30, 30)
+ const second = api.createRectangle()
+ second.x = 60
+ second.y = 70
+ second.resize(20, 20)
+
+ const group = api.group([first, second], api.currentPage)
+
+ expect([group.x, group.y, group.width, group.height]).toEqual([10, 20, 70, 70])
+ expect(first.absoluteBoundingBox).toMatchObject({ x: 10, y: 20, width: 30, height: 30 })
+ expect(second.absoluteBoundingBox).toMatchObject({ x: 60, y: 70, width: 20, height: 20 })
+ })
+
+ test('group inside a frame is placed in the frame', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.x = 100
+ frame.y = 200
+ frame.resize(300, 300)
+ const first = api.createRectangle()
+ frame.appendChild(first)
+ first.x = 10
+ first.y = 20
+ first.resize(30, 30)
+ const second = api.createRectangle()
+ frame.appendChild(second)
+ second.x = 60
+ second.y = 70
+ second.resize(20, 20)
+
+ const group = api.group([first, second], frame)
+
+ expect([group.x, group.y, group.width, group.height]).toEqual([10, 20, 70, 70])
+ expect(first.absoluteBoundingBox).toMatchObject({ x: 110, y: 220 })
+ expect(second.absoluteBoundingBox).toMatchObject({ x: 160, y: 270 })
+ })
+
+ test('group and boolean operation inside a rotated frame use the frame axes', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.x = 100
+ frame.y = 200
+ frame.resize(300, 300)
+ frame.rotation = 90
+ const make = () => {
+ const first = api.createRectangle()
+ frame.appendChild(first)
+ first.x = 10
+ first.y = 20
+ first.resize(30, 30)
+ const second = api.createRectangle()
+ frame.appendChild(second)
+ second.x = 60
+ second.y = 70
+ second.resize(20, 20)
+ return [first, second]
+ }
+
+ const group = api.group(make(), frame)
+ const booleanNode = api.union(make(), frame)
+
+ for (const node of [group, booleanNode]) {
+ expect(node.x).toBeCloseTo(10, 6)
+ expect(node.y).toBeCloseTo(20, 6)
+ expect(node.width).toBeCloseTo(70, 6)
+ expect(node.height).toBeCloseTo(70, 6)
+ }
+ })
+
test('ungroup returns moved children', () => {
const api = createAPI()
const page = api.currentPage
@@ -59,4 +133,50 @@ describe('FigmaAPI grouping compatibility', () => {
expect(booleanNode.children.map((child) => child.id)).toEqual([first.id, second.id])
expect(page.children.map((child) => child.id)).toEqual([before.id, booleanNode.id])
})
+
+ test('boolean operations span all operands', () => {
+ const api = createAPI()
+ const first = api.createRectangle()
+ first.x = 10
+ first.y = 20
+ first.resize(30, 30)
+ const second = api.createRectangle()
+ second.x = 60
+ second.y = 70
+ second.resize(20, 20)
+
+ const booleanNode = api.union([first, second], api.currentPage)
+
+ expect([booleanNode.x, booleanNode.y, booleanNode.width, booleanNode.height]).toEqual([
+ 10, 20, 70, 70
+ ])
+ expect(first.absoluteBoundingBox).toMatchObject({ x: 10, y: 20, width: 30, height: 30 })
+ expect(second.absoluteBoundingBox).toMatchObject({ x: 60, y: 70, width: 20, height: 20 })
+ })
+
+ test('boolean operations inside a frame are placed in the frame', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.x = 100
+ frame.y = 200
+ frame.resize(300, 300)
+ const first = api.createRectangle()
+ frame.appendChild(first)
+ first.x = 10
+ first.y = 20
+ first.resize(30, 30)
+ const second = api.createRectangle()
+ frame.appendChild(second)
+ second.x = 60
+ second.y = 70
+ second.resize(20, 20)
+
+ const booleanNode = api.subtract([first, second], frame)
+
+ expect([booleanNode.x, booleanNode.y, booleanNode.width, booleanNode.height]).toEqual([
+ 10, 20, 70, 70
+ ])
+ expect(first.absoluteBoundingBox).toMatchObject({ x: 110, y: 220 })
+ expect(second.absoluteBoundingBox).toMatchObject({ x: 160, y: 270 })
+ })
})