Skip to content

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

Add FigmaAPI + eval CLI command with 60 unit tests

Danila Poyarkov0b654e2d407b
diff --git a/packages/cli/src/commands/eval.ts b/packages/cli/src/commands/eval.ts
new file mode 100644
index 000000000..4207cefb6
--- /dev/null
+++ b/packages/cli/src/commands/eval.ts
@@ -0,0 +1,78 @@
+import { defineCommand } from 'citty'
+
+import { FigmaAPI } from '@open-pencil/core'
+
+import { loadDocument } from '../headless'
+import { printError } from '../format'
+
+function serializeResult(value: unknown): unknown {
+ if (value === undefined || value === null) return value
+ if (typeof value === 'object' && value !== null && 'toJSON' in value && typeof value.toJSON === 'function') {
+ return value.toJSON()
+ }
+ if (Array.isArray(value)) return value.map(serializeResult)
+ return value
+}
+
+export default defineCommand({
+ meta: { description: 'Execute JavaScript with Figma plugin API' },
+ args: {
+ file: { type: 'positional', description: '.fig file path', required: true },
+ code: { type: 'string', alias: 'c', description: 'JavaScript code to execute' },
+ stdin: { type: 'boolean', description: 'Read code from stdin' },
+ write: { type: 'boolean', alias: 'w', description: 'Write changes back to the input file' },
+ output: { type: 'string', alias: 'o', description: 'Write to a different file' },
+ json: { type: 'boolean', description: 'Output as JSON' },
+ quiet: { type: 'boolean', alias: 'q', description: 'Suppress output' },
+ },
+ async run({ args }) {
+ let code = args.code
+
+ if (args.stdin) {
+ const chunks: Buffer[] = []
+ for await (const chunk of process.stdin) chunks.push(chunk as Buffer)
+ code = Buffer.concat(chunks).toString('utf-8')
+ }
+
+ if (!code) {
+ printError('Provide code via --code or --stdin')
+ process.exit(1)
+ }
+
+ const graph = await loadDocument(args.file)
+ const figma = new FigmaAPI(graph)
+
+ const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
+ const wrappedCode = code.trim().startsWith('return')
+ ? code
+ : `return (async () => { ${code} })()`
+
+ let result: unknown
+ try {
+ const fn = new AsyncFunction('figma', wrappedCode)
+ result = await fn(figma)
+ } catch (err) {
+ printError(err instanceof Error ? err.message : String(err))
+ process.exit(1)
+ }
+
+ if (!args.quiet && result !== undefined) {
+ const serialized = serializeResult(result)
+ if (args.json || !process.stdout.isTTY) {
+ console.log(JSON.stringify(serialized, null, 2))
+ } else {
+ console.log(serialized)
+ }
+ }
+
+ if (args.write || args.output) {
+ const { exportFigFile } = await import('@open-pencil/core')
+ const outPath = args.output ?? args.file
+ const data = exportFigFile(graph)
+ await Bun.write(outPath, data)
+ if (!args.quiet) {
+ console.error(`Written to ${outPath}`)
+ }
+ }
+ },
+})
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index 5b2cfbe61..f9fe8ddef 100755
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -2,6 +2,7 @@
import { defineCommand, runMain } from 'citty'
import analyze from './commands/analyze'
+import evalCmd from './commands/eval'
import exportCmd from './commands/export'
import find from './commands/find'
import info from './commands/info'
@@ -18,6 +19,7 @@ const main = defineCommand({
},
subCommands: {
analyze,
+ eval: evalCmd,
export: exportCmd,
find,
info,
diff --git a/packages/core/src/figma-api.ts b/packages/core/src/figma-api.ts
new file mode 100644
index 000000000..aaf2910bb
--- /dev/null
+++ b/packages/core/src/figma-api.ts
@@ -0,0 +1,837 @@
+import type { Rect } from './types'
+import type {
+ SceneNode,
+ NodeType,
+ Fill,
+ Stroke,
+ Effect,
+ LayoutMode,
+} from './scene-graph'
+import { SceneGraph } from './scene-graph'
+
+const MIXED = Symbol('mixed')
+
+type FigmaFontName = { family: string; style: string }
+
+function weightToStyleName(weight: number, italic: boolean): string {
+ const names: Record<number, string> = {
+ 100: 'Thin',
+ 200: 'Extra Light',
+ 300: 'Light',
+ 400: 'Regular',
+ 500: 'Medium',
+ 600: 'Semi Bold',
+ 700: 'Bold',
+ 800: 'Extra Bold',
+ 900: 'Black',
+ }
+ const base = names[weight] ?? 'Regular'
+ return italic ? `${base} Italic` : base
+}
+
+function styleNameToWeight(style: string): { weight: number; italic: boolean } {
+ const lower = style.toLowerCase()
+ const italic = lower.includes('italic')
+ const clean = lower.replace(/italic/i, '').trim()
+ const map: Record<string, number> = {
+ thin: 100,
+ 'extra light': 200,
+ 'ultra light': 200,
+ light: 300,
+ regular: 400,
+ '': 400,
+ medium: 500,
+ 'semi bold': 600,
+ 'demi bold': 600,
+ bold: 700,
+ 'extra bold': 800,
+ 'ultra bold': 800,
+ black: 900,
+ heavy: 900,
+ }
+ return { weight: map[clean] ?? 400, italic }
+}
+
+class FigmaNodeProxy {
+ readonly _id: string
+ readonly _graph: SceneGraph
+ readonly _api: FigmaAPI
+
+ constructor(id: string, graph: SceneGraph, api: FigmaAPI) {
+ this._id = id
+ this._graph = graph
+ this._api = api
+ }
+
+ private _raw(): SceneNode {
+ const n = this._graph.getNode(this._id)
+ if (!n) throw new Error(`Node ${this._id} has been removed`)
+ return n
+ }
+
+ get id(): string {
+ return this._id
+ }
+
+ get type(): NodeType {
+ return this._raw().type
+ }
+
+ get name(): string {
+ return this._raw().name
+ }
+
+ set name(v: string) {
+ this._graph.updateNode(this._id, { name: v })
+ }
+
+ get removed(): boolean {
+ return !this._graph.getNode(this._id)
+ }
+
+ // --- Geometry ---
+
+ get x(): number {
+ return this._raw().x
+ }
+
+ set x(v: number) {
+ this._graph.updateNode(this._id, { x: v })
+ }
+
+ get y(): number {
+ return this._raw().y
+ }
+
+ set y(v: number) {
+ this._graph.updateNode(this._id, { y: v })
+ }
+
+ get width(): number {
+ return this._raw().width
+ }
+
+ get height(): number {
+ return this._raw().height
+ }
+
+ get rotation(): number {
+ return this._raw().rotation
+ }
+
+ set rotation(v: number) {
+ this._graph.updateNode(this._id, { rotation: v })
+ }
+
+ resize(width: number, height: number): void {
+ this._graph.updateNode(this._id, { width, height })
+ }
+
+ resizeWithoutConstraints(width: number, height: number): void {
+ this.resize(width, height)
+ }
+
+ get absoluteTransform(): [[number, number, number], [number, number, number]] {
+ const pos = this._graph.getAbsolutePosition(this._id)
+ return [
+ [1, 0, pos.x],
+ [0, 1, pos.y],
+ ]
+ }
+
+ get absoluteBoundingBox(): Rect {
+ return this._graph.getAbsoluteBounds(this._id)
+ }
+
+ get absoluteRenderBounds(): Rect {
+ return this.absoluteBoundingBox
+ }
+
+ // --- Visual ---
+
+ get fills(): Fill[] {
+ return this._raw().fills
+ }
+
+ set fills(v: Fill[]) {
+ this._graph.updateNode(this._id, { fills: v })
+ }
+
+ get strokes(): Stroke[] {
+ return this._raw().strokes
+ }
+
+ set strokes(v: Stroke[]) {
+ this._graph.updateNode(this._id, { strokes: v })
+ }
+
+ get effects(): Effect[] {
+ return this._raw().effects
+ }
+
+ set effects(v: Effect[]) {
+ this._graph.updateNode(this._id, { effects: v })
+ }
+
+ get opacity(): number {
+ return this._raw().opacity
+ }
+
+ set opacity(v: number) {
+ this._graph.updateNode(this._id, { opacity: v })
+ }
+
+ get visible(): boolean {
+ return this._raw().visible
+ }
+
+ set visible(v: boolean) {
+ this._graph.updateNode(this._id, { visible: v })
+ }
+
+ get locked(): boolean {
+ return this._raw().locked
+ }
+
+ set locked(v: boolean) {
+ this._graph.updateNode(this._id, { locked: v })
+ }
+
+ get blendMode(): string {
+ return this._raw().blendMode
+ }
+
+ set blendMode(v: string) {
+ this._graph.updateNode(this._id, { blendMode: v as SceneNode['blendMode'] })
+ }
+
+ get clipsContent(): boolean {
+ return this._raw().clipsContent
+ }
+
+ set clipsContent(v: boolean) {
+ this._graph.updateNode(this._id, { clipsContent: v })
+ }
+
+ // --- Corner Radius ---
+
+ get cornerRadius(): number | typeof MIXED {
+ const n = this._raw()
+ if (n.independentCorners) return MIXED
+ return n.cornerRadius
+ }
+
+ set cornerRadius(v: number | typeof MIXED) {
+ if (v === MIXED) return
+ this._graph.updateNode(this._id, {
+ cornerRadius: v as number,
+ topLeftRadius: v as number,
+ topRightRadius: v as number,
+ bottomRightRadius: v as number,
+ bottomLeftRadius: v as number,
+ independentCorners: false,
+ })
+ }
+
+ get topLeftRadius(): number {
+ return this._raw().topLeftRadius
+ }
+
+ set topLeftRadius(v: number) {
+ this._graph.updateNode(this._id, { topLeftRadius: v, independentCorners: true })
+ }
+
+ get topRightRadius(): number {
+ return this._raw().topRightRadius
+ }
+
+ set topRightRadius(v: number) {
+ this._graph.updateNode(this._id, { topRightRadius: v, independentCorners: true })
+ }
+
+ get bottomLeftRadius(): number {
+ return this._raw().bottomLeftRadius
+ }
+
+ set bottomLeftRadius(v: number) {
+ this._graph.updateNode(this._id, { bottomLeftRadius: v, independentCorners: true })
+ }
+
+ get bottomRightRadius(): number {
+ return this._raw().bottomRightRadius
+ }
+
+ set bottomRightRadius(v: number) {
+ this._graph.updateNode(this._id, { bottomRightRadius: v, independentCorners: true })
+ }
+
+ get cornerSmoothing(): number {
+ return this._raw().cornerSmoothing
+ }
+
+ set cornerSmoothing(v: number) {
+ this._graph.updateNode(this._id, { cornerSmoothing: v })
+ }
+
+ // --- Stroke details ---
+
+ get strokeWeight(): number {
+ const s = this._raw().strokes
+ return s.length > 0 ? s[0].weight : 0
+ }
+
+ set strokeWeight(v: number) {
+ const n = this._raw()
+ if (n.strokes.length > 0) {
+ const strokes = structuredClone(n.strokes)
+ strokes[0].weight = v
+ this._graph.updateNode(this._id, { strokes })
+ }
+ }
+
+ get strokeAlign(): string {
+ const s = this._raw().strokes
+ return s.length > 0 ? s[0].align : 'INSIDE'
+ }
+
+ set strokeAlign(v: string) {
+ const n = this._raw()
+ if (n.strokes.length > 0) {
+ const strokes = structuredClone(n.strokes)
+ strokes[0].align = v as Stroke['align']
+ this._graph.updateNode(this._id, { strokes })
+ }
+ }
+
+ get dashPattern(): number[] {
+ return this._raw().dashPattern
+ }
+
+ set dashPattern(v: number[]) {
+ this._graph.updateNode(this._id, { dashPattern: v })
+ }
+
+ // --- Text ---
+
+ get characters(): string {
+ return this._raw().text
+ }
+
+ set characters(v: string) {
+ this._graph.updateNode(this._id, { text: v })
+ }
+
+ get fontSize(): number {
+ return this._raw().fontSize
+ }
+
+ set fontSize(v: number) {
+ this._graph.updateNode(this._id, { fontSize: v })
+ }
+
+ get fontName(): FigmaFontName {
+ const n = this._raw()
+ return { family: n.fontFamily, style: weightToStyleName(n.fontWeight, n.italic) }
+ }
+
+ set fontName(v: FigmaFontName) {
+ const { weight, italic } = styleNameToWeight(v.style)
+ this._graph.updateNode(this._id, { fontFamily: v.family, fontWeight: weight, italic })
+ }
+
+ get fontWeight(): number {
+ return this._raw().fontWeight
+ }
+
+ set fontWeight(v: number) {
+ this._graph.updateNode(this._id, { fontWeight: v })
+ }
+
+ get textAlignHorizontal(): string {
+ return this._raw().textAlignHorizontal
+ }
+
+ set textAlignHorizontal(v: string) {
+ this._graph.updateNode(this._id, { textAlignHorizontal: v as SceneNode['textAlignHorizontal'] })
+ }
+
+ get textAlignVertical(): string {
+ return this._raw().textAlignVertical
+ }
+
+ set textAlignVertical(v: string) {
+ this._graph.updateNode(this._id, { textAlignVertical: v as SceneNode['textAlignVertical'] })
+ }
+
+ get textAutoResize(): string {
+ return this._raw().textAutoResize
+ }
+
+ set textAutoResize(v: string) {
+ this._graph.updateNode(this._id, { textAutoResize: v as SceneNode['textAutoResize'] })
+ }
+
+ get letterSpacing(): number {
+ return this._raw().letterSpacing
+ }
+
+ set letterSpacing(v: number) {
+ this._graph.updateNode(this._id, { letterSpacing: v })
+ }
+
+ get lineHeight(): number | null {
+ return this._raw().lineHeight
+ }
+
+ set lineHeight(v: number | null) {
+ this._graph.updateNode(this._id, { lineHeight: v })
+ }
+
+ get textCase(): string {
+ return this._raw().textCase
+ }
+
+ set textCase(v: string) {
+ this._graph.updateNode(this._id, { textCase: v as SceneNode['textCase'] })
+ }
+
+ get textDecoration(): string {
+ return this._raw().textDecoration
+ }
+
+ set textDecoration(v: string) {
+ this._graph.updateNode(this._id, { textDecoration: v as SceneNode['textDecoration'] })
+ }
+
+ get maxLines(): number | null {
+ return this._raw().maxLines
+ }
+
+ set maxLines(v: number | null) {
+ this._graph.updateNode(this._id, { maxLines: v })
+ }
+
+ // --- Auto-layout ---
+
+ get layoutMode(): LayoutMode {
+ return this._raw().layoutMode
+ }
+
+ set layoutMode(v: LayoutMode) {
+ this._graph.updateNode(this._id, { layoutMode: v })
+ }
+
+ get primaryAxisAlignItems(): string {
+ return this._raw().primaryAxisAlign
+ }
+
+ set primaryAxisAlignItems(v: string) {
+ this._graph.updateNode(this._id, { primaryAxisAlign: v as SceneNode['primaryAxisAlign'] })
+ }
+
+ get counterAxisAlignItems(): string {
+ return this._raw().counterAxisAlign
+ }
+
+ set counterAxisAlignItems(v: string) {
+ this._graph.updateNode(this._id, { counterAxisAlign: v as SceneNode['counterAxisAlign'] })
+ }
+
+ get itemSpacing(): number {
+ return this._raw().itemSpacing
+ }
+
+ set itemSpacing(v: number) {
+ this._graph.updateNode(this._id, { itemSpacing: v })
+ }
+
+ get counterAxisSpacing(): number {
+ return this._raw().counterAxisSpacing
+ }
+
+ set counterAxisSpacing(v: number) {
+ this._graph.updateNode(this._id, { counterAxisSpacing: v })
+ }
+
+ get paddingTop(): number {
+ return this._raw().paddingTop
+ }
+
+ set paddingTop(v: number) {
+ this._graph.updateNode(this._id, { paddingTop: v })
+ }
+
+ get paddingRight(): number {
+ return this._raw().paddingRight
+ }
+
+ set paddingRight(v: number) {
+ this._graph.updateNode(this._id, { paddingRight: v })
+ }
+
+ get paddingBottom(): number {
+ return this._raw().paddingBottom
+ }
+
+ set paddingBottom(v: number) {
+ this._graph.updateNode(this._id, { paddingBottom: v })
+ }
+
+ get paddingLeft(): number {
+ return this._raw().paddingLeft
+ }
+
+ set paddingLeft(v: number) {
+ this._graph.updateNode(this._id, { paddingLeft: v })
+ }
+
+ get layoutWrap(): string {
+ return this._raw().layoutWrap
+ }
+
+ set layoutWrap(v: string) {
+ this._graph.updateNode(this._id, { layoutWrap: v as SceneNode['layoutWrap'] })
+ }
+
+ // --- Layout child props ---
+
+ get layoutPositioning(): string {
+ return this._raw().layoutPositioning
+ }
+
+ set layoutPositioning(v: string) {
+ this._graph.updateNode(this._id, { layoutPositioning: v as SceneNode['layoutPositioning'] })
+ }
+
+ get layoutGrow(): number {
+ return this._raw().layoutGrow
+ }
+
+ set layoutGrow(v: number) {
+ this._graph.updateNode(this._id, { layoutGrow: v })
+ }
+
+ get layoutSizingHorizontal(): string {
+ const n = this._raw()
+ if (n.layoutMode !== 'NONE') return n.primaryAxisSizing
+ const parent = n.parentId ? this._graph.getNode(n.parentId) : undefined
+ if (!parent || parent.layoutMode === 'NONE') return 'FIXED'
+ return parent.layoutMode === 'HORIZONTAL' ? n.primaryAxisSizing : n.counterAxisSizing
+ }
+
+ set layoutSizingHorizontal(v: string) {
+ const n = this._raw()
+ const parent = n.parentId ? this._graph.getNode(n.parentId) : undefined
+ if (parent && parent.layoutMode === 'VERTICAL') {
+ this._graph.updateNode(this._id, { counterAxisSizing: v as SceneNode['counterAxisSizing'] })
+ } else {
+ this._graph.updateNode(this._id, { primaryAxisSizing: v as SceneNode['primaryAxisSizing'] })
+ }
+ }
+
+ get layoutSizingVertical(): string {
+ const n = this._raw()
+ if (n.layoutMode !== 'NONE') return n.counterAxisSizing
+ const parent = n.parentId ? this._graph.getNode(n.parentId) : undefined
+ if (!parent || parent.layoutMode === 'NONE') return 'FIXED'
+ return parent.layoutMode === 'VERTICAL' ? n.primaryAxisSizing : n.counterAxisSizing
+ }
+
+ set layoutSizingVertical(v: string) {
+ const n = this._raw()
+ const parent = n.parentId ? this._graph.getNode(n.parentId) : undefined
+ if (parent && parent.layoutMode === 'HORIZONTAL') {
+ this._graph.updateNode(this._id, { counterAxisSizing: v as SceneNode['counterAxisSizing'] })
+ } else {
+ this._graph.updateNode(this._id, { primaryAxisSizing: v as SceneNode['primaryAxisSizing'] })
+ }
+ }
+
+ // --- Constraints ---
+
+ get constraints(): { horizontal: string; vertical: string } {
+ const n = this._raw()
+ return { horizontal: n.horizontalConstraint, vertical: n.verticalConstraint }
+ }
+
+ set constraints(v: { horizontal: string; vertical: string }) {
+ this._graph.updateNode(this._id, {
+ horizontalConstraint: v.horizontal as SceneNode['horizontalConstraint'],
+ verticalConstraint: v.vertical as SceneNode['verticalConstraint'],
+ })
+ }
+
+ // --- Components ---
+
+ get mainComponent(): FigmaNodeProxy | null {
+ const n = this._raw()
+ if (!n.componentId) return null
+ const comp = this._graph.getNode(n.componentId)
+ if (!comp) return null
+ return this._api.wrapNode(comp.id)
+ }
+
+ createInstance(): FigmaNodeProxy {
+ const n = this._raw()
+ if (n.type !== 'COMPONENT') throw new Error('createInstance() can only be called on components')
+ const pageId = this._api.currentPageId
+ const inst = this._graph.createInstance(n.id, pageId)
+ if (!inst) throw new Error('Failed to create instance')
+ return this._api.wrapNode(inst.id)
+ }
+
+ // --- Tree ---
+
+ get parent(): FigmaNodeProxy | null {
+ const n = this._raw()
+ if (!n.parentId) return null
+ return this._api.wrapNode(n.parentId)
+ }
+
+ get children(): FigmaNodeProxy[] {
+ return this._graph
+ .getChildren(this._id)
+ .map((c) => this._api.wrapNode(c.id))
+ }
+
+ appendChild(child: FigmaNodeProxy): void {
+ this._graph.reparentNode(child._id, this._id)
+ }
+
+ insertChild(index: number, child: FigmaNodeProxy): void {
+ this._graph.reparentNode(child._id, this._id)
+ this._graph.reorderChild(child._id, this._id, index)
+ }
+
+ remove(): void {
+ this._graph.deleteNode(this._id)
+ }
+
+ findAll(callback?: (node: FigmaNodeProxy) => boolean): FigmaNodeProxy[] {
+ const results: FigmaNodeProxy[] = []
+ const walk = (id: string) => {
+ for (const child of this._graph.getChildren(id)) {
+ const proxy = this._api.wrapNode(child.id)
+ if (!callback || callback(proxy)) results.push(proxy)
+ walk(child.id)
+ }
+ }
+ walk(this._id)
+ return results
+ }
+
+ findOne(callback: (node: FigmaNodeProxy) => boolean): FigmaNodeProxy | null {
+ const walk = (id: string): FigmaNodeProxy | null => {
+ for (const child of this._graph.getChildren(id)) {
+ const proxy = this._api.wrapNode(child.id)
+ if (callback(proxy)) return proxy
+ const found = walk(child.id)
+ if (found) return found
+ }
+ return null
+ }
+ return walk(this._id)
+ }
+
+ findChild(callback: (node: FigmaNodeProxy) => boolean): FigmaNodeProxy | null {
+ for (const child of this._graph.getChildren(this._id)) {
+ const proxy = this._api.wrapNode(child.id)
+ if (callback(proxy)) return proxy
+ }
+ return null
+ }
+
+ findChildren(callback?: (node: FigmaNodeProxy) => boolean): FigmaNodeProxy[] {
+ return this._graph
+ .getChildren(this._id)
+ .map((c) => this._api.wrapNode(c.id))
+ .filter((proxy) => !callback || callback(proxy))
+ }
+
+ // --- Serialization ---
+
+ toJSON(): Record<string, unknown> {
+ const n = this._raw()
+ const obj: Record<string, unknown> = {
+ id: n.id,
+ type: n.type,
+ name: n.name,
+ x: n.x,
+ y: n.y,
+ width: n.width,
+ height: n.height,
+ }
+ if (n.fills.length > 0) obj.fills = n.fills
+ if (n.strokes.length > 0) obj.strokes = n.strokes
+ if (n.effects.length > 0) obj.effects = n.effects
+ if (n.opacity !== 1) obj.opacity = n.opacity
+ if (n.cornerRadius > 0) obj.cornerRadius = n.cornerRadius
+ if (!n.visible) obj.visible = false
+ if (n.text) obj.characters = n.text
+ if (n.layoutMode !== 'NONE') {
+ obj.layoutMode = n.layoutMode
+ obj.itemSpacing = n.itemSpacing
+ }
+ const children = this._graph.getChildren(this._id)
+ if (children.length > 0) {
+ obj.children = children.map((c) => this._api.wrapNode(c.id).toJSON())
+ }
+ return obj
+ }
+
+ toString(): string {
+ const n = this._raw()
+ return `[${n.type} "${n.name}" ${n.id}]`
+ }
+
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
+ return this.toString()
+ }
+}
+
+export class FigmaAPI {
+ readonly graph: SceneGraph
+ private _currentPageId: string
+ private _selection: FigmaNodeProxy[] = []
+ private _nodeCache = new Map<string, FigmaNodeProxy>()
+
+ readonly mixed = MIXED
+
+ constructor(graph: SceneGraph) {
+ this.graph = graph
+ const pages = graph.getPages()
+ this._currentPageId = pages[0]?.id ?? graph.rootId
+ }
+
+ get currentPageId(): string {
+ return this._currentPageId
+ }
+
+ wrapNode(id: string): FigmaNodeProxy {
+ let proxy = this._nodeCache.get(id)
+ if (!proxy) {
+ proxy = new FigmaNodeProxy(id, this.graph, this)
+ this._nodeCache.set(id, proxy)
+ }
+ return proxy
+ }
+
+ get root(): FigmaNodeProxy {
+ return this.wrapNode(this.graph.rootId)
+ }
+
+ get currentPage(): FigmaNodeProxy & { selection: FigmaNodeProxy[] } {
+ const page = this.wrapNode(this._currentPageId)
+ const self = this
+ return Object.defineProperties(page, {
+ selection: {
+ get() {
+ return self._selection
+ },
+ set(nodes: FigmaNodeProxy[]) {
+ self._selection = nodes
+ },
+ enumerable: true,
+ configurable: true,
+ },
+ }) as FigmaNodeProxy & { selection: FigmaNodeProxy[] }
+ }
+
+ set currentPage(page: FigmaNodeProxy) {
+ this._currentPageId = page._id
+ }
+
+ getNodeById(id: string): FigmaNodeProxy | null {
+ const node = this.graph.getNode(id)
+ return node ? this.wrapNode(id) : null
+ }
+
+ // --- Node Creation ---
+
+ private _createNode(type: NodeType): FigmaNodeProxy {
+ const node = this.graph.createNode(type, this._currentPageId)
+ return this.wrapNode(node.id)
+ }
+
+ createFrame(): FigmaNodeProxy {
+ return this._createNode('FRAME')
+ }
+
+ createRectangle(): FigmaNodeProxy {
+ return this._createNode('RECTANGLE')
+ }
+
+ createEllipse(): FigmaNodeProxy {
+ return this._createNode('ELLIPSE')
+ }
+
+ createText(): FigmaNodeProxy {
+ return this._createNode('TEXT')
+ }
+
+ createLine(): FigmaNodeProxy {
+ return this._createNode('LINE')
+ }
+
+ createPolygon(): FigmaNodeProxy {
+ return this._createNode('POLYGON')
+ }
+
+ createStar(): FigmaNodeProxy {
+ return this._createNode('STAR')
+ }
+
+ createVector(): FigmaNodeProxy {
+ return this._createNode('VECTOR')
+ }
+
+ createComponent(): FigmaNodeProxy {
+ return this._createNode('COMPONENT')
+ }
+
+ createSection(): FigmaNodeProxy {
+ return this._createNode('SECTION')
+ }
+
+ createPage(): FigmaNodeProxy {
+ const page = this.graph.addPage('Page')
+ return this.wrapNode(page.id)
+ }
+
+ // --- Grouping ---
+
+ group(nodes: FigmaNodeProxy[], parent: FigmaNodeProxy): FigmaNodeProxy {
+ const groupNode = this.graph.createNode('GROUP', parent._id)
+ for (const n of nodes) {
+ this.graph.reparentNode(n._id, groupNode.id)
+ }
+ return this.wrapNode(groupNode.id)
+ }
+
+ ungroup(node: FigmaNodeProxy): void {
+ const raw = this.graph.getNode(node._id)
+ if (!raw || raw.type !== 'GROUP') return
+ const parentId = raw.parentId ?? this._currentPageId
+ for (const childId of [...raw.childIds]) {
+ this.graph.reparentNode(childId, parentId)
+ }
+ this.graph.deleteNode(node._id)
+ }
+
+ // --- Stubs ---
+
+ async loadFontAsync(_fontName: FigmaFontName): Promise<void> {
+ // No-op: we don't gate text editing on font loading
+ }
+
+ notify(message: string): { cancel: () => void } {
+ if (typeof console !== 'undefined') console.log(`[figma.notify] ${message}`)
+ return { cancel() {} }
+ }
+
+ commitUndo(): void {}
+ triggerUndo(): void {}
+}
+
+export { FigmaNodeProxy }
+export type { FigmaFontName }
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 99ff35c0a..770e6e628 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -48,6 +48,7 @@ export {
type StyleRun
} from './scene-graph'
+export { FigmaAPI, FigmaNodeProxy, type FigmaFontName } from './figma-api'
export { SkiaRenderer, type RenderOverlays } from './renderer'
export { computeLayout, computeAllLayouts } from './layout'
export { getCanvasKit, type CanvasKitOptions } from './canvaskit'
diff --git a/tests/engine/figma-api.test.ts b/tests/engine/figma-api.test.ts
new file mode 100644
index 000000000..78a5d7af5
--- /dev/null
+++ b/tests/engine/figma-api.test.ts
@@ -0,0 +1,524 @@
+import { describe, expect, test } from 'bun:test'
+
+import { FigmaAPI, SceneGraph } from '@open-pencil/core'
+
+function createAPI(): FigmaAPI {
+ return new FigmaAPI(new SceneGraph())
+}
+
+describe('FigmaAPI', () => {
+ 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')
+ })
+
+ test('currentPage is first page', () => {
+ const api = createAPI()
+ expect(api.currentPage.type).toBe('CANVAS')
+ 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(api.root.children.length).toBe(2)
+ })
+
+ test('currentPage can be switched', () => {
+ const api = createAPI()
+ const page2 = api.createPage()
+ page2.name = 'Page 2'
+ api.currentPage = page2
+ expect(api.currentPage.name).toBe('Page 2')
+ })
+
+ test('getNodeById returns proxy', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ const found = api.getNodeById(frame.id)
+ expect(found).not.toBeNull()
+ expect(found!.id).toBe(frame.id)
+ })
+
+ test('getNodeById returns null for unknown id', () => {
+ const api = createAPI()
+ expect(api.getNodeById('nonexistent')).toBeNull()
+ })
+ })
+
+ describe('node creation', () => {
+ test('createFrame', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ expect(frame.type).toBe('FRAME')
+ expect(frame.parent!.id).toBe(api.currentPage.id)
+ })
+
+ test('createRectangle', () => {
+ const api = createAPI()
+ const rect = api.createRectangle()
+ expect(rect.type).toBe('RECTANGLE')
+ })
+
+ test('createEllipse', () => {
+ const api = createAPI()
+ expect(api.createEllipse().type).toBe('ELLIPSE')
+ })
+
+ test('createText', () => {
+ const api = createAPI()
+ expect(api.createText().type).toBe('TEXT')
+ })
+
+ test('createLine', () => {
+ const api = createAPI()
+ expect(api.createLine().type).toBe('LINE')
+ })
+
+ test('createPolygon', () => {
+ const api = createAPI()
+ expect(api.createPolygon().type).toBe('POLYGON')
+ })
+
+ test('createStar', () => {
+ const api = createAPI()
+ expect(api.createStar().type).toBe('STAR')
+ })
+
+ test('createVector', () => {
+ const api = createAPI()
+ expect(api.createVector().type).toBe('VECTOR')
+ })
+
+ test('createComponent', () => {
+ const api = createAPI()
+ expect(api.createComponent().type).toBe('COMPONENT')
+ })
+
+ test('createSection', () => {
+ const api = createAPI()
+ expect(api.createSection().type).toBe('SECTION')
+ })
+
+ test('created node is child of current page', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ expect(api.currentPage.children.some((c) => c.id === frame.id)).toBe(true)
+ })
+ })
+
+ describe('property access', () => {
+ test('name get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.name = 'Card'
+ expect(frame.name).toBe('Card')
+ })
+
+ test('position get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.x = 50
+ frame.y = 100
+ expect(frame.x).toBe(50)
+ expect(frame.y).toBe(100)
+ })
+
+ test('resize', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.resize(300, 200)
+ expect(frame.width).toBe(300)
+ expect(frame.height).toBe(200)
+ })
+
+ test('fills get/set', () => {
+ const api = createAPI()
+ const rect = api.createRectangle()
+ rect.fills = [{ type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true }]
+ expect(rect.fills.length).toBe(1)
+ expect(rect.fills[0].color.r).toBe(1)
+ })
+
+ test('opacity get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.opacity = 0.5
+ expect(frame.opacity).toBe(0.5)
+ })
+
+ test('visible get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.visible = false
+ expect(frame.visible).toBe(false)
+ })
+
+ test('locked get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.locked = true
+ expect(frame.locked).toBe(true)
+ })
+
+ test('rotation get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.rotation = 45
+ expect(frame.rotation).toBe(45)
+ })
+
+ test('clipsContent get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.clipsContent = true
+ expect(frame.clipsContent).toBe(true)
+ })
+ })
+
+ describe('corner radius', () => {
+ test('uniform corner radius', () => {
+ const api = createAPI()
+ const rect = api.createRectangle()
+ rect.cornerRadius = 8
+ expect(rect.cornerRadius).toBe(8)
+ expect(rect.topLeftRadius).toBe(8)
+ expect(rect.bottomRightRadius).toBe(8)
+ })
+
+ test('independent corners return mixed', () => {
+ const api = createAPI()
+ const rect = api.createRectangle()
+ rect.topLeftRadius = 4
+ rect.bottomRightRadius = 12
+ expect(rect.cornerRadius).toBe(api.mixed)
+ })
+ })
+
+ describe('text', () => {
+ test('characters maps to text content', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.characters = 'Hello World'
+ expect(text.characters).toBe('Hello World')
+ })
+
+ test('fontSize get/set', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.fontSize = 24
+ expect(text.fontSize).toBe(24)
+ })
+
+ test('fontName get/set', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.fontName = { family: 'Roboto', style: 'Bold' }
+ expect(text.fontName.family).toBe('Roboto')
+ expect(text.fontWeight).toBe(700)
+ })
+
+ test('fontName italic', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.fontName = { family: 'Inter', style: 'Bold Italic' }
+ expect(text.fontName.family).toBe('Inter')
+ expect(text.fontWeight).toBe(700)
+ expect(text.fontName.style).toBe('Bold Italic')
+ })
+
+ test('textAlignHorizontal', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.textAlignHorizontal = 'CENTER'
+ expect(text.textAlignHorizontal).toBe('CENTER')
+ })
+
+ test('textAutoResize', () => {
+ const api = createAPI()
+ const text = api.createText()
+ text.textAutoResize = 'WIDTH_AND_HEIGHT'
+ expect(text.textAutoResize).toBe('WIDTH_AND_HEIGHT')
+ })
+ })
+
+ describe('auto-layout', () => {
+ test('layoutMode', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.layoutMode = 'VERTICAL'
+ expect(frame.layoutMode).toBe('VERTICAL')
+ })
+
+ test('itemSpacing', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.layoutMode = 'VERTICAL'
+ frame.itemSpacing = 12
+ expect(frame.itemSpacing).toBe(12)
+ })
+
+ test('padding', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.paddingTop = 16
+ frame.paddingRight = 24
+ frame.paddingBottom = 16
+ frame.paddingLeft = 24
+ expect(frame.paddingTop).toBe(16)
+ expect(frame.paddingRight).toBe(24)
+ })
+
+ test('alignment', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.layoutMode = 'HORIZONTAL'
+ frame.primaryAxisAlignItems = 'SPACE_BETWEEN'
+ frame.counterAxisAlignItems = 'CENTER'
+ expect(frame.primaryAxisAlignItems).toBe('SPACE_BETWEEN')
+ expect(frame.counterAxisAlignItems).toBe('CENTER')
+ })
+
+ test('layoutWrap', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.layoutWrap = 'WRAP'
+ expect(frame.layoutWrap).toBe('WRAP')
+ })
+ })
+
+ describe('tree operations', () => {
+ test('appendChild reparents', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const child = api.createRectangle()
+ parent.appendChild(child)
+ expect(parent.children.length).toBe(1)
+ expect(parent.children[0].id).toBe(child.id)
+ expect(child.parent!.id).toBe(parent.id)
+ })
+
+ test('insertChild at index', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const a = api.createRectangle()
+ const b = api.createRectangle()
+ const c = api.createRectangle()
+ parent.appendChild(a)
+ parent.appendChild(b)
+ parent.insertChild(1, c)
+ expect(parent.children.map((c) => c.id)).toEqual([a.id, c.id, b.id])
+ })
+
+ test('remove deletes node', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ const id = frame.id
+ frame.remove()
+ expect(api.getNodeById(id)).toBeNull()
+ })
+
+ test('removed property', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ expect(frame.removed).toBe(false)
+ frame.remove()
+ expect(frame.removed).toBe(true)
+ })
+ })
+
+ describe('traversal', () => {
+ test('findAll finds deep descendants', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ parent.name = 'Container'
+ const child = api.createFrame()
+ child.name = 'Card'
+ parent.appendChild(child)
+ const text = api.createText()
+ text.characters = 'Title'
+ text.name = 'Title'
+ child.appendChild(text)
+
+ const found = parent.findAll((n) => n.type === 'TEXT')
+ expect(found.length).toBe(1)
+ expect(found[0].characters).toBe('Title')
+ })
+
+ test('findAll without callback returns all', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const a = api.createRectangle()
+ const b = api.createText()
+ parent.appendChild(a)
+ parent.appendChild(b)
+ expect(parent.findAll().length).toBe(2)
+ })
+
+ test('findOne returns first match', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const a = api.createRectangle()
+ a.name = 'First'
+ const b = api.createRectangle()
+ b.name = 'Second'
+ parent.appendChild(a)
+ parent.appendChild(b)
+ const found = parent.findOne((n) => n.type === 'RECTANGLE')
+ expect(found!.name).toBe('First')
+ })
+
+ test('findChild searches direct children only', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const child = api.createFrame()
+ const deep = api.createText()
+ deep.name = 'Deep'
+ parent.appendChild(child)
+ child.appendChild(deep)
+
+ expect(parent.findChild((n) => n.name === 'Deep')).toBeNull()
+ expect(child.findChild((n) => n.name === 'Deep')).not.toBeNull()
+ })
+
+ test('findChildren returns direct children matching', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ const a = api.createRectangle()
+ const b = api.createText()
+ const c = api.createRectangle()
+ parent.appendChild(a)
+ parent.appendChild(b)
+ parent.appendChild(c)
+ const rects = parent.findChildren((n) => n.type === 'RECTANGLE')
+ expect(rects.length).toBe(2)
+ })
+ })
+
+ describe('grouping', () => {
+ test('group creates GROUP with children', () => {
+ const api = createAPI()
+ const a = api.createRectangle()
+ const b = api.createEllipse()
+ const group = api.group([a, b], api.currentPage)
+ expect(group.type).toBe('GROUP')
+ expect(group.children.length).toBe(2)
+ })
+
+ test('ungroup dissolves group', () => {
+ const api = createAPI()
+ const a = api.createRectangle()
+ const b = api.createEllipse()
+ const group = api.group([a, b], api.currentPage)
+ const groupId = group.id
+ api.ungroup(group)
+ expect(api.getNodeById(groupId)).toBeNull()
+ expect(a.parent!.id).toBe(api.currentPage.id)
+ })
+ })
+
+ describe('selection', () => {
+ test('selection get/set', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ api.currentPage.selection = [frame]
+ expect(api.currentPage.selection.length).toBe(1)
+ expect(api.currentPage.selection[0].id).toBe(frame.id)
+ })
+
+ test('selection empty by default', () => {
+ const api = createAPI()
+ expect(api.currentPage.selection).toEqual([])
+ })
+ })
+
+ describe('components', () => {
+ test('createInstance from component', () => {
+ const api = createAPI()
+ const comp = api.createComponent()
+ comp.name = 'Button'
+ comp.resize(200, 40)
+ const instance = comp.createInstance()
+ expect(instance.type).toBe('INSTANCE')
+ expect(instance.mainComponent!.id).toBe(comp.id)
+ })
+ })
+
+ describe('serialization', () => {
+ test('toJSON returns clean object', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.name = 'Card'
+ frame.resize(300, 200)
+ frame.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1, a: 1 }, opacity: 1, visible: true }]
+ const json = frame.toJSON()
+ expect(json.name).toBe('Card')
+ expect(json.width).toBe(300)
+ expect(json.fills).toBeDefined()
+ })
+
+ test('toJSON includes children', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ const rect = api.createRectangle()
+ frame.appendChild(rect)
+ const json = frame.toJSON() as { children?: unknown[] }
+ expect(json.children).toBeDefined()
+ expect(json.children!.length).toBe(1)
+ })
+
+ test('toString returns readable string', () => {
+ const api = createAPI()
+ const frame = api.createFrame()
+ frame.name = 'Card'
+ expect(frame.toString()).toContain('FRAME')
+ expect(frame.toString()).toContain('Card')
+ })
+ })
+
+ describe('stubs', () => {
+ test('loadFontAsync resolves', async () => {
+ const api = createAPI()
+ await api.loadFontAsync({ family: 'Inter', style: 'Regular' })
+ })
+
+ test('mixed is a symbol', () => {
+ const api = createAPI()
+ expect(typeof api.mixed).toBe('symbol')
+ })
+ })
+
+ describe('absolute position', () => {
+ test('absoluteBoundingBox accounts for nesting', () => {
+ const api = createAPI()
+ const parent = api.createFrame()
+ parent.x = 100
+ parent.y = 200
+ const child = api.createRectangle()
+ parent.appendChild(child)
+ child.x = 10
+ child.y = 20
+ child.resize(50, 30)
+ const bounds = child.absoluteBoundingBox
+ expect(bounds.x).toBe(110)
+ expect(bounds.y).toBe(220)
+ expect(bounds.width).toBe(50)
+ expect(bounds.height).toBe(30)
+ })
+ })
+
+ describe('stroke details', () => {
+ test('strokeWeight and strokeAlign', () => {
+ const api = createAPI()
+ const rect = api.createRectangle()
+ rect.strokes = [{ color: { r: 0, g: 0, b: 0, a: 1 }, weight: 2, opacity: 1, visible: true, align: 'CENTER' }]
+ expect(rect.strokeWeight).toBe(2)
+ expect(rect.strokeAlign).toBe('CENTER')
+ rect.strokeWeight = 4
+ expect(rect.strokeWeight).toBe(4)
+ })
+ })
+})