Skip to content

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

2.2 KB
Raw Blame
import * as v from 'valibot'

import { OpenPencilAPI } from '#core/openpencil-api'
import { nodeIdInputSchema, defineTool } from '#core/tools/schema'

export const bindVariable = defineTool({
  name: 'bind_variable',

  description:
    'Bind a variable to a node property. For fills/strokes color bindings use indexed format like "fills/0/color".',
  execution: { kind: 'sync', mutation: 'properties' },
  input: v.strictObject({
    node_id: nodeIdInputSchema,
    field: v.pipe(
      v.string(),
      v.description(
        'Property field path. For fills/strokes use indexed format: "fills/0/color", "strokes/0/color". ' +
          'For FLOAT scalars: "opacity", "width", "height", "cornerRadius", "fontSize", "letterSpacing", ' +
          '"lineHeight", "itemSpacing", "strokeWeight", "paddingLeft/Right/Top/Bottom", "counterAxisSpacing", ' +
          '"rotation", "x", "y", "minWidth", "maxWidth", "minHeight", "maxHeight", ' +
          '"topLeftRadius", "topRightRadius", "bottomLeftRadius", "bottomRightRadius", ' +
          '"borderTopWeight", "borderBottomWeight", "borderLeftWeight", "borderRightWeight", ' +
          '"gridRowGap", "gridColumnGap". ' +
          'For STRING: "fontFamily". For STRING or FLOAT: "text", a number shown as text. ' +
          'For BOOLEAN or STRING: "visible", text showing the layer unless empty or "false", "no", "off". ' +
          'For effects use indexed format: "effects/0/color" (COLOR), "effects/0/radius", ' +
          '"effects/0/spread", "effects/0/offsetX", "effects/0/offsetY" (FLOAT); a blur binds only radius.'
      )
    ),
    variable_id: v.pipe(v.string(), v.description('Variable ID'))
  }),
  execute: (figma, args) => {
    const node = figma.getNodeById(args.node_id)
    if (!node) return { error: `Node "${args.node_id}" not found` }
    const variable = figma.variables.getVariableById(args.variable_id)
    if (!variable) return { error: `Variable "${args.variable_id}" not found` }
    try {
      new OpenPencilAPI(figma).bindVariable(args.node_id, args.field, variable)
    } catch (err) {
      return { error: err instanceof Error ? err.message : String(err) }
    }
    return { node_id: args.node_id, field: args.field, variable_id: args.variable_id }
  }
})