* feat(core): read and write W3C design tokens Variables export as DTCG 2025.10 token files, one per collection mode in the shape Figma imports, with text and effect styles as typography and shadow tokens and a resolver document that combines them. OpenPencil's own token fields ride in a dev.openpencil extension, so an export reads back as it was. Import reads plain mode files, resolver documents, and Tokens Studio themes, plans what it would add, update, or skip against the document without changing it, and applies the plan as one undo step: values merge into variables by name, nothing is deleted, and an updated style updates the layers that use it. * feat: import and export design tokens from the variables dialog The stylesheet menu exports the document's variables and styles as a zip of W3C design tokens, and Import design tokens reads token files, folders, or a zip into a dialog that shows where each collection and mode goes, what would be added, updated, or skipped and why, before applying it as one undo step. * feat: import and export design tokens from tools and the CLI openpencil tokens --format dtcg --out <folder> writes the token files, design_to_tokens takes the same format, and import_design_tokens imports token files into a document for AI, MCP, and openpencil tool call. The editor and these paths share one apply step behind a target interface: the editor makes each change undoable, a headless document takes them directly. Re-importing an export changes nothing it does not need to: values that match are left alone, layers that use an updated style are changed only outside instances, where they follow their component, and collections that share a name keep their own files apart. * perf(core): index token lookups when planning an import Resolving each reference scanned every token and variable, which made importing a large design system quadratic. A resolver now also finds files picked without their folders by name. * docs: document design token import and export * test: type the design token result in the tokens command test * fix: lay out the design token import dialog as a mapping list Each collection and mode sits beside the picker for where it goes, in one bordered list, so the pickers no longer read as headings. Skipped tokens use AppCollapsible, file problems AppAlert, and the counts move to the footer. * fix: keep durations, variable metadata, and style edits through design token import A duration Figma's format writes in seconds goes back into a variable that holds milliseconds. Tokens without OpenPencil's extension leave an updated variable's unit, scopes, and code syntax as they are. A style token whose value was edited in another tool takes that edit instead of the stale exact fields its extension still carries. Aliases to a repeated collection name resolve within one collection, a collection named Styles no longer collides with the resolver's styles set, and the dialog plans again when the document changes and right before applying. The editor imports the apply step directly rather than the whole format barrel. * fix: keep effect order and explicit alias targets in design token import Edited shadows take the places of the shadows they replace, so blurs keep their positions. An alias whose Figma data names a collection that is left out or absent is missing rather than bound to the same path in another collection. The resolver reserves the Styles set name only when there is a styles file. * perf(cli): print token stylesheets without loading every page A CSS or Tailwind stylesheet reads only variables, which load with the document, so tokens skips populating and laying out every page as variables already does. On an 85 MB design kit this takes the command from about seven minutes to seconds.
112 lines
3.9 KiB
TypeScript
112 lines
3.9 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import { createEditor } from '@open-pencil/core/editor'
|
|
import { FigmaAPI } from '@open-pencil/core/figma-api'
|
|
import { SceneGraph } from '@open-pencil/scene-graph'
|
|
|
|
// Recorded with the same script in Figma desktop 126: new layers from the plugin API and from the
|
|
// drawing tools start alike.
|
|
|
|
const WHITE = [1, 1, 1]
|
|
const GREY = [0.85, 0.85, 0.85]
|
|
const BLACK = [0, 0, 0]
|
|
|
|
function solid(paints: readonly { type: string; color?: { r: number; g: number; b: number } }[]) {
|
|
return paints.map((paint) =>
|
|
paint.color
|
|
? [paint.color.r, paint.color.g, paint.color.b].map((v) => Math.round(v * 100) / 100)
|
|
: paint.type
|
|
)
|
|
}
|
|
|
|
function api() {
|
|
return new FigmaAPI(new SceneGraph())
|
|
}
|
|
|
|
describe('new layer defaults', () => {
|
|
test('frames and components are white, and frames clip their content', () => {
|
|
const figma = api()
|
|
const frame = figma.createFrame()
|
|
const component = figma.createComponent()
|
|
expect(solid(frame.fills)).toEqual([WHITE])
|
|
expect(frame.clipsContent).toBe(true)
|
|
expect(solid(component.fills)).toEqual([WHITE])
|
|
expect(component.clipsContent).toBe(false)
|
|
})
|
|
|
|
test('shapes are light grey', () => {
|
|
const figma = api()
|
|
for (const shape of [
|
|
figma.createRectangle(),
|
|
figma.createEllipse(),
|
|
figma.createPolygon(),
|
|
figma.createStar()
|
|
]) {
|
|
expect(solid(shape.fills)).toEqual([GREY])
|
|
expect(shape.strokes).toEqual([])
|
|
}
|
|
})
|
|
|
|
test('lines and vectors have a black 1 px stroke, and a line has no height', () => {
|
|
const figma = api()
|
|
const line = figma.createLine()
|
|
const vector = figma.createVector()
|
|
expect([line.width, line.height]).toEqual([100, 0])
|
|
for (const node of [line, vector]) {
|
|
expect(node.fills).toEqual([])
|
|
expect(solid(node.strokes)).toEqual([BLACK])
|
|
expect(node.strokeWeight).toBe(1)
|
|
}
|
|
})
|
|
|
|
test('a stroke a script adds takes the 1 px default weight', () => {
|
|
const rect = api().createRectangle()
|
|
expect(rect.strokeWeight).toBe(1)
|
|
// Scripts pass Figma paints, which carry no weight.
|
|
Reflect.set(rect, 'strokes', [{ type: 'SOLID', color: { r: 1, g: 0, b: 0 } }])
|
|
expect(rect.strokeWeight).toBe(1)
|
|
})
|
|
|
|
test('sections are 496 square with a faint white outline, filled for the interface theme', () => {
|
|
const figma = api()
|
|
const light = figma.createSection()
|
|
expect([light.width, light.height, light.cornerRadius]).toEqual([496, 496, 2])
|
|
expect(solid(light.fills)).toEqual([WHITE])
|
|
expect(solid(light.strokes)).toEqual([WHITE])
|
|
expect([light.strokes[0]?.opacity, light.strokeAlign]).toEqual([0.1, 'INSIDE'])
|
|
// Figma fills a section #444444 in its dark theme.
|
|
figma.theme = 'dark'
|
|
expect(solid(figma.createSection().fills)).toEqual([[0.27, 0.27, 0.27]])
|
|
})
|
|
|
|
test('each layer gets its own copy of the default paints', () => {
|
|
const editor = createEditor()
|
|
try {
|
|
const page = editor.state.currentPageId
|
|
const first = editor.graph.getNode(editor.createShape('RECTANGLE', 0, 0, 10, 10, page))
|
|
const color = first?.fills[0]?.color
|
|
if (color) color.r = 1
|
|
const second = editor.graph.getNode(editor.createShape('RECTANGLE', 0, 0, 10, 10, page))
|
|
expect(solid(second?.fills ?? [])).toEqual([GREY])
|
|
} finally {
|
|
editor.dispose()
|
|
}
|
|
})
|
|
|
|
test('the drawing tools use the same defaults', () => {
|
|
const editor = createEditor()
|
|
try {
|
|
const page = editor.state.currentPageId
|
|
const frame = editor.graph.getNode(editor.createShape('FRAME', 0, 0, 100, 100, page))
|
|
const line = editor.graph.getNode(editor.createShape('LINE', 0, 0, 100, 0, page))
|
|
expect(frame?.clipsContent).toBe(true)
|
|
expect(solid(frame?.fills ?? [])).toEqual([WHITE])
|
|
expect(line?.fills).toEqual([])
|
|
expect(solid(line?.strokes ?? [])).toEqual([BLACK])
|
|
expect(line?.strokes[0]?.weight).toBe(1)
|
|
} finally {
|
|
editor.dispose()
|
|
}
|
|
})
|
|
})
|