@pairdraw/pen 0.19.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/schema.ts CHANGED
@@ -1,6 +1,19 @@
1
1
  import * as v from 'valibot'
2
2
 
3
+ import { CODE_SYNTAX_PLATFORMS } from '@pairdraw/scene-graph'
4
+ import type { EnabledLibraryBinding, SourceLibraryPublication } from '@pairdraw/scene-graph'
5
+ import { designMemorySchema, type DesignMemory } from '@pairdraw/scene-graph/design-memory'
6
+ import {
7
+ enabledLibrariesSchema,
8
+ sourceLibraryPublicationSchema
9
+ } from '@pairdraw/scene-graph/plugin-data'
3
10
  import { finiteNumberSchema } from '@pairdraw/scene-graph/primitives'
11
+ import {
12
+ nodeStatusSchema,
13
+ statusWorkflowSchema,
14
+ type StatusWorkflow
15
+ } from '@pairdraw/scene-graph/statuses'
16
+ import { tokenSourcesSchema } from '@pairdraw/scene-graph/token-sources'
4
17
 
5
18
  import { stripJSONComments } from './jsonc'
6
19
 
@@ -145,10 +158,27 @@ const paddingSchema = v.union([
145
158
  ])
146
159
  const cornerRadiusSchema = v.union([numSchema, v.array(numSchema)])
147
160
 
161
+ /**
162
+ * A layer's status, or none when the value is not an id this reader can use: a document stays
163
+ * readable because of one unreadable field.
164
+ */
165
+ const penNodeStatusSchema = v.pipe(
166
+ v.unknown(),
167
+ v.transform((value): string | undefined => {
168
+ const parsed = v.safeParse(nodeStatusSchema, value)
169
+ return parsed.success ? parsed.output : undefined
170
+ })
171
+ )
172
+
148
173
  /** Every property a node, or an override of one, can carry. */
149
174
  const nodeFields = {
150
175
  name: v.optional(strSchema),
151
176
  context: v.optional(strSchema),
177
+ /**
178
+ * The layer's status, an id in the document's status workflow, as Core keeps it on the layer.
179
+ * Pencil has no field for it, so it is an extension other Pencil clients ignore.
180
+ */
181
+ status: v.optional(penNodeStatusSchema),
152
182
  /**
153
183
  * What a layer is, for agents: pen.dev writes a `type` and any fields of its own. It is only kept
154
184
  * as plugin data, so any value is read as it is rather than rejecting the document.
@@ -282,20 +312,112 @@ const variableSchema = v.looseObject({
282
312
  theme: v.optional(v.record(strSchema, strSchema))
283
313
  })
284
314
  )
285
- ])
315
+ ]),
316
+ /**
317
+ * The code that names the token, as Figma's variables carry it. Pencil has no field for it, so
318
+ * it is an extension other Pencil clients ignore; without it a reopened document cannot match a
319
+ * renamed variable to the token its stylesheet declares.
320
+ */
321
+ codeSyntax: v.optional(v.record(v.picklist(CODE_SYNTAX_PLATFORMS), strSchema))
286
322
  })
287
323
 
288
324
  /** A font file the document bundles, which its text names by `name` as a family. */
289
325
  const fontSchema = v.looseObject({ name: strSchema, url: strSchema })
290
326
 
327
+ /** A document whose links are a list this reader can use; any other value is no link at all. */
328
+ const penTokenSourcesSchema = v.pipe(
329
+ v.unknown(),
330
+ v.transform((value) => v.parse(tokenSourcesSchema, Array.isArray(value) ? value : []))
331
+ )
332
+
333
+ /**
334
+ * The memory a file holds, or none when an older client wrote something this reader cannot use:
335
+ * a document stays readable because of one unreadable field.
336
+ */
337
+ const penDesignMemorySchema = v.pipe(
338
+ v.unknown(),
339
+ v.transform((value): DesignMemory | undefined => {
340
+ const parsed = v.safeParse(designMemorySchema, value)
341
+ return parsed.success ? parsed.output : undefined
342
+ })
343
+ )
344
+
345
+ /**
346
+ * The status workflow the file holds, or none when an older client wrote something this reader
347
+ * cannot use: a document stays readable because of one unreadable field.
348
+ */
349
+ const penStatusWorkflowSchema = v.pipe(
350
+ v.unknown(),
351
+ v.transform((value): StatusWorkflow | undefined => {
352
+ const parsed = v.safeParse(statusWorkflowSchema, value)
353
+ return parsed.success ? parsed.output : undefined
354
+ })
355
+ )
356
+
357
+ /**
358
+ * The libraries the file switched on, or none when an older client wrote something this reader
359
+ * cannot use: a document stays readable because of one unreadable field.
360
+ */
361
+ const penEnabledLibrariesSchema = v.pipe(
362
+ v.unknown(),
363
+ v.transform((value): EnabledLibraryBinding[] | undefined => {
364
+ const parsed = v.safeParse(enabledLibrariesSchema, value)
365
+ return parsed.success && parsed.output.length > 0 ? parsed.output : undefined
366
+ })
367
+ )
368
+
369
+ /**
370
+ * The library the file publishes as, or none when an older client wrote something this reader
371
+ * cannot use: a document stays readable because of one unreadable field.
372
+ */
373
+ const penSourceLibraryPublicationSchema = v.pipe(
374
+ v.unknown(),
375
+ v.transform((value): SourceLibraryPublication | undefined => {
376
+ const parsed = v.safeParse(sourceLibraryPublicationSchema, value)
377
+ return parsed.success ? parsed.output : undefined
378
+ })
379
+ )
380
+
291
381
  const penDocumentSchema = v.looseObject({
292
382
  version: v.optional(strSchema),
293
383
  // The page's name. The format is single-page, so it belongs to the document.
294
384
  name: v.optional(strSchema),
295
385
  fonts: v.optional(v.array(fontSchema)),
296
386
  themes: v.optional(v.record(strSchema, v.array(strSchema))),
387
+ /**
388
+ * The selector each mode applies in, by collection and mode name: `.dark` for a Dark mode a
389
+ * stylesheet writes. Pencil has no field for it, so it is an extension other clients ignore;
390
+ * without it a reopened document reads the stylesheet's dark block as a new mode.
391
+ */
392
+ modeConditions: v.optional(v.record(strSchema, v.record(strSchema, strSchema))),
297
393
  imports: v.optional(v.record(strSchema, strSchema)),
298
394
  variables: v.optional(v.record(strSchema, variableSchema)),
395
+ /** The code the document's variables are linked to, as Core keeps it on the document node. */
396
+ tokenSources: v.optional(penTokenSourcesSchema),
397
+ /**
398
+ * The design memory agents follow, as Core keeps it on the document node: allowed values,
399
+ * decisions, and pinned examples. Pencil has no field for it, so it is an extension other
400
+ * Pencil clients ignore; without it a reopened document loses its rules.
401
+ */
402
+ designMemory: v.optional(penDesignMemorySchema),
403
+ /**
404
+ * The states the document's layers can be in, as Core keeps them on the document node. Pencil
405
+ * has no field for it, so it is an extension other Pencil clients ignore; without it a reopened
406
+ * document falls back to Figma's own two statuses.
407
+ */
408
+ statuses: v.optional(penStatusWorkflowSchema),
409
+ /**
410
+ * The libraries the document switched on, as Core keeps them on the document node: the revision
411
+ * each library's components were inserted from. Pencil has no field for it, so it is an
412
+ * extension other Pencil clients ignore; without it a reopened document forgets the bindings.
413
+ */
414
+ enabledLibraries: v.optional(penEnabledLibrariesSchema),
415
+ /**
416
+ * The library this document publishes as, as Core keeps it on the document node. Pencil has no
417
+ * field for it, so it is an extension other Pencil clients ignore; without it the next publish
418
+ * starts a new library rather than a revision of this one.
419
+ */
420
+ sourceLibraryPublication: v.optional(penSourceLibraryPublicationSchema),
299
421
  children: v.array(nodeSchema)
300
422
  })
301
423
 
package/src/text.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { omit } from 'es-toolkit'
2
+
1
3
  import type { SceneNode, StyleRun, TextAlignVertical, TextAutoResize } from '@pairdraw/scene-graph'
2
4
  import { parseFontStyle, type FontFamilyStyle } from '@pairdraw/scene-graph/font-style'
3
5
  import { percentLength, pixelLength } from '@pairdraw/scene-graph/text'
@@ -86,7 +88,7 @@ export function applyTextProps(
86
88
  })
87
89
  // The bound name is the pen-only one no font is registered under, so a later reconcile
88
90
  // would restore a family nothing draws with. The bundled face owns the family now.
89
- delete node.boundVariables.fontFamily
91
+ node.boundVariables = omit(node.boundVariables, ['fontFamily'])
90
92
  }
91
93
  node.textAlignHorizontal = TEXT_ALIGN[pen.textAlign ?? 'left'] ?? 'LEFT'
92
94
  node.textAlignVertical = TEXT_ALIGN_VERTICAL[pen.textAlignVertical ?? 'top'] ?? 'TOP'
package/src/variables.ts CHANGED
@@ -50,7 +50,7 @@ function varName(ref: string): string {
50
50
  export function bindIfVar(node: SceneNode, field: string, val: unknown, ctx: VarContext): void {
51
51
  if (!isVarRef(val)) return
52
52
  const variable = ctx.byName.get(varName(val))
53
- if (variable) node.boundVariables[field] = variable.id
53
+ if (variable) node.boundVariables = { ...node.boundVariables, [field]: variable.id }
54
54
  }
55
55
 
56
56
  /** A number, or the value of the number variable it references. */
@@ -230,6 +230,23 @@ interface ThemeCollection {
230
230
  modeByTheme: Map<string, string>
231
231
  }
232
232
 
233
+ /** The selector each mode applies in, which Pencil itself does not record. */
234
+ function applyModeConditions(
235
+ graph: SceneGraph,
236
+ doc: PenDocument,
237
+ collections: readonly ThemeCollection[]
238
+ ) {
239
+ for (const created of collections) {
240
+ const collection = graph.variableCollections.get(created.id)
241
+ const conditions = collection && doc.modeConditions?.[collection.name]
242
+ if (!collection || !conditions) continue
243
+ for (const mode of collection.modes) {
244
+ const condition = conditions[mode.name]
245
+ if (condition) mode.condition = condition
246
+ }
247
+ }
248
+ }
249
+
233
250
  function addThemeCollection(graph: SceneGraph, axes: Axis[]): ThemeCollection {
234
251
  const themes = combinations(axes)
235
252
  const modes: VariableCollectionMode[] = themes.map((theme) => ({
@@ -250,13 +267,18 @@ function addThemeCollection(graph: SceneGraph, axes: Axis[]): ThemeCollection {
250
267
  return { id, axes, themes, modes, modeByTheme }
251
268
  }
252
269
 
253
- export function buildVarContext(graph: SceneGraph, doc: PenDocument): VarContext {
270
+ export function buildVarContext(
271
+ graph: SceneGraph,
272
+ doc: PenDocument,
273
+ onUnknownVariable?: (name: string) => void
274
+ ): VarContext {
254
275
  const axes = themeAxes(doc)
255
276
  const definitions = Object.entries(doc.variables ?? {})
256
277
  const collections = axisGroups(
257
278
  axes,
258
279
  definitions.map(([, definition]) => definition)
259
280
  ).map((group) => addThemeCollection(graph, group))
281
+ applyModeConditions(graph, doc, collections)
260
282
  const collectionOfAxis = new Map(
261
283
  collections.flatMap((collection) => collection.axes.map(([axis]) => [axis, collection]))
262
284
  )
@@ -289,7 +311,8 @@ export function buildVarContext(graph: SceneGraph, doc: PenDocument): VarContext
289
311
  ])
290
312
  ),
291
313
  description: '',
292
- hiddenFromPublishing: false
314
+ hiddenFromPublishing: false,
315
+ ...(definition.codeSyntax ? { codeSyntax: definition.codeSyntax } : {})
293
316
  }
294
317
  graph.addVariable(variable)
295
318
  byName.set(name, variable)
@@ -303,6 +326,7 @@ export function buildVarContext(graph: SceneGraph, doc: PenDocument): VarContext
303
326
  /** A variable's value in the current theme, following aliases. */
304
327
  function resolve(ref: string): VariableValue | undefined {
305
328
  let variable = byName.get(varName(ref))
329
+ if (!variable) onUnknownVariable?.(varName(ref))
306
330
  for (let hops = 0; variable && hops <= byName.size; hops++) {
307
331
  const collection = collectionById.get(variable.collectionId)
308
332
  const value = collection ? variable.valuesByMode[modeIn(collection)] : undefined