AI-native design editor. Open-source Figma alternative.
fix(core)!: read pages and the document root with Figma's node types (#1735)
Danila Poyarkovcc1a2f1729a8
diff --git a/changes/1735/breaking.md b/changes/1735/breaking.mdnew file mode 100644index 000000000..7e0209dc8--- /dev/null+++ b/changes/1735/breaking.md@@ -0,0 +1 @@+- Plugin API nodes read Figma's node types: a page's `type` is `PAGE` instead of `CANVAS`, and `figma.root.type` is `DOCUMENT` instead of `FRAME`, in `node.type` and `toJSON()`. Scripts that compared against `CANVAS` should compare against `PAGE`.diff --git a/packages/core/src/figma-api/accessors/basic.ts b/packages/core/src/figma-api/accessors/basic.tsindex f4f4addb3..736000c97 100644--- a/packages/core/src/figma-api/accessors/basic.ts+++ b/packages/core/src/figma-api/accessors/basic.ts@@ -27,6 +27,7 @@ import {type NodeProxyInternals,type ProxyThis} from '#core/figma-api/accessor-utils'+import type { FigmaNodeType } from '#core/figma-api/members'import { flushPendingLayout } from '#core/figma-api/pending-layout'import type { FigmaNodeProxy, NodeProxyHost } from '#core/figma-api/proxy'import { computeAbsoluteRenderBounds } from '#core/figma-api/render-bounds'@@ -92,6 +93,13 @@ function setTransform(fitGroupsAround(scene, node.parentId, hostFitOptions(target, internals))}+/** A node's type as Figma's plugin API names it (`FigmaNodeType`). */+export function figmaNodeType(graph: SceneGraph, node: SceneNode): FigmaNodeType {+ if (node.id === graph.rootId) return 'DOCUMENT'+ if (slotPropertyId(node)) return 'SLOT'+ return node.type === 'CANVAS' ? 'PAGE' : node.type+}+export function installBasicNodeProxyAccessors(prototype: object,internals: NodeProxyInternals@@ -103,9 +111,8 @@ export function installBasicNodeProxyAccessors(}},type: {- get(this: ProxyThis): SceneNode['type'] | 'SLOT' {- const node = raw(this, internals)- return slotPropertyId(node) ? 'SLOT' : node.type+ get(this: ProxyThis): FigmaNodeType {+ return figmaNodeType(graph(this, internals), raw(this, internals))}},name: {diff --git a/packages/core/src/figma-api/members.ts b/packages/core/src/figma-api/members.tsindex 1ad1dcd54..ecebb10d6 100644--- a/packages/core/src/figma-api/members.ts+++ b/packages/core/src/figma-api/members.ts@@ -45,10 +45,16 @@ type StyleIdSetter = MinimalFillsMixin['setFillStyleIdAsync']* at runtime, on the prototype or, for members Figma has on some node types alone, on each* proxy; this class only declares their types.*/+/**+ * A node's type as Figma's plugin API names it: the document root reads as `'DOCUMENT'`, a page+ * as `'PAGE'` (the scene graph's `CANVAS`), and a slot frame as `'SLOT'`, as Figma's+ * `DocumentNode`, `PageNode` and `SlotNode` do.+ */+export type FigmaNodeType = Exclude<NodeType, 'CANVAS'> | 'DOCUMENT' | 'PAGE' | 'SLOT'+export abstract class FigmaNodeMembers {declare readonly id: string- /** A slot frame reads as `'SLOT'`, as Figma's `SlotNode` does. */- declare readonly type: NodeType | 'SLOT'+ declare readonly type: FigmaNodeTypedeclare name: stringdeclare readonly removed: booleandeclare x: numberdiff --git a/packages/core/src/figma-api/serialization.ts b/packages/core/src/figma-api/serialization.tsindex 3f8349677..00a6d3d4a 100644--- a/packages/core/src/figma-api/serialization.ts+++ b/packages/core/src/figma-api/serialization.ts@@ -1,5 +1,6 @@import type { SceneGraph, SceneNode } from '@open-pencil/scene-graph'+import { figmaNodeType } from './accessors/basic'import { pluginChildren } from './accessors/sticky'import { figmaFills, figmaStrokes } from './accessors/visual'import type { NodeProxyHost } from './proxy'@@ -60,7 +61,7 @@ export function nodeProxyToJSON(const obj: Record<string, unknown> = {id: n.id,- type: n.type,+ type: figmaNodeType(graph, n),name: n.name,x: n.x,y: n.y,diff --git a/packages/core/tests/figma-api/pages.test.ts b/packages/core/tests/figma-api/pages.test.tsindex 9e66be8de..010f49bfa 100644--- a/packages/core/tests/figma-api/pages.test.ts+++ b/packages/core/tests/figma-api/pages.test.ts@@ -7,19 +7,29 @@ describe('document & pages', () => {test('root has pages as children', () => {const api = createAPI()expect(api.root.children.length).toBeGreaterThan(0)- expect(api.root.children[0].type).toBe('CANVAS')+ expect(api.root.children[0].type).toBe('PAGE')+ })++ test("the root, pages and their layers read Figma's node types", () => {+ const api = createAPI()+ const rectangle = api.createRectangle()+ expect(api.root.type).toBe('DOCUMENT')+ expect(rectangle.parent?.type).toBe('PAGE')+ expect(rectangle.parent?.parent?.type).toBe('DOCUMENT')+ expect(api.currentPage.toJSON().type).toBe('PAGE')+ expect(api.root.toJSON(0).type).toBe('DOCUMENT')})test('currentPage is first page', () => {const api = createAPI()- expect(api.currentPage.type).toBe('CANVAS')+ expect(api.currentPage.type).toBe('PAGE')expect(api.currentPage.name).toBe('Page 1')})test('createPage adds a new page', () => {const api = createAPI()const page = api.createPage()- expect(page.type).toBe('CANVAS')+ expect(page.type).toBe('PAGE')expect(api.root.children.length).toBe(2)})diff --git a/packages/core/tests/figma-api/slots.test.ts b/packages/core/tests/figma-api/slots.test.tsindex f146cd242..77a24dd4d 100644--- a/packages/core/tests/figma-api/slots.test.ts+++ b/packages/core/tests/figma-api/slots.test.ts@@ -18,10 +18,12 @@ function setup() {describe('slots', () => {test('createSlot appends a 100×100 frame named Slot bound to a new SLOT property', () => {- const { component } = setup()+ const { api, component } = setup()const slot = component.createSlot()expect(slot.type).toBe('SLOT')+ // Figma's SlotNode typing has no toJSON; the proxy behind it does.+ expect(api.getNodeById(slot.id)?.toJSON().type).toBe('SLOT')expect(slot.name).toBe('Slot')expect([slot.width, slot.height]).toEqual([100, 100])expect(slot.parent?.id).toBe(component.id)diff --git a/packages/core/tests/helpers/figma-api.ts b/packages/core/tests/helpers/figma-api.tsindex 5cbdbea0b..8674e2eb2 100644--- a/packages/core/tests/helpers/figma-api.ts+++ b/packages/core/tests/helpers/figma-api.ts@@ -41,14 +41,17 @@ export function asComponentNode(node: FigmaNodeProxy): FigmaNodeProxy & Componenreturn node as FigmaNodeProxy & ComponentNode}+function isPageNode(node: FigmaNodeProxy): node is FigmaNodeProxy & PageNode {+ return node.type === 'PAGE'+}+/*** Names `api.currentPage` as a page. The boolean-operation entry points only take Figma's node* types, while the current-page proxy is typed by its selection alone.*/-export function asPageNode(- page: FigmaNodeProxy & { selection: FigmaNodeProxy[] }-): FigmaNodeProxy & PageNode {- return page as FigmaNodeProxy & PageNode+export function asPageNode(page: FigmaNodeProxy): FigmaNodeProxy & PageNode {+ if (!isPageNode(page)) throw new Error(`Expected a page, got ${page.type}`)+ return page}/**diff --git a/packages/core/tests/tools/analyze/eval.test.ts b/packages/core/tests/tools/analyze/eval.test.tsindex ebb413965..f70dff7eb 100644--- a/packages/core/tests/tools/analyze/eval.test.ts+++ b/packages/core/tests/tools/analyze/eval.test.ts@@ -17,7 +17,7 @@ test('returns what a script returns as the JSON it prints as', async () => {)expect(result).toMatchObject({ width: null, list: [1, null], byId: {} })- expect(result).toHaveProperty('page.type', 'CANVAS')+ expect(result).toHaveProperty('page.type', 'PAGE')})test('a script result makes a tool message the AI SDK accepts in the next prompt', async () => {diff --git a/tests/engine/tools/stock-photo/apply.test.ts b/tests/engine/tools/stock-photo/apply.test.tsindex 452f6e9ba..1833e851a 100644--- a/tests/engine/tools/stock-photo/apply.test.ts+++ b/tests/engine/tools/stock-photo/apply.test.ts@@ -212,7 +212,9 @@ describe('applyPhoto', () => {query: 'should not run'})- expect(result.error).toContain(`(${type}) is not a suitable stock photo target`)+ // A page reads as Figma's PAGE, the type the error names.+ const named = type === 'CANVAS' ? 'PAGE' : type+ expect(result.error).toContain(`(${named}) is not a suitable stock photo target`)expect(calls).toHaveLength(0)expect(node.fills).toEqual(originalFills)expect(graph.images.size).toBe(0)diff --git a/tests/helpers/plugin-api.ts b/tests/helpers/plugin-api.tsindex 671a8b564..d28d56626 100644--- a/tests/helpers/plugin-api.ts+++ b/tests/helpers/plugin-api.ts@@ -12,11 +12,14 @@ export function createAPI(): FigmaAPI {return new FigmaAPI(new SceneGraph())}+function isPageNode(node: FigmaNodeProxy): node is FigmaNodeProxy & PageNode {+ return node.type === 'PAGE'+}+/** Names `api.currentPage` as a page, which the boolean-operation entry points require. */-export function asPageNode(- page: FigmaNodeProxy & { selection: FigmaNodeProxy[] }-): FigmaNodeProxy & PageNode {- return page as FigmaNodeProxy & PageNode+export function asPageNode(page: FigmaNodeProxy): FigmaNodeProxy & PageNode {+ if (!isPageNode(page)) throw new Error(`Expected a page, got ${page.type}`)+ return page}/** A solid paint both the scene graph and the plugin API take: it carries no transform. */