@pairdraw/pen 0.16.2 → 0.18.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,15 +1,19 @@
1
1
  import * as v from 'valibot'
2
2
 
3
+ import { finiteNumberSchema } from '@pairdraw/scene-graph/primitives'
4
+
3
5
  import { stripJSONComments } from './jsonc'
4
6
 
5
7
  /**
6
8
  * The `.pen` document as pen.dev writes it (schema 2.21), plus the older forms still found in
7
9
  * files: the object `stroke` of 2.8 and `icon_font` nodes. Properties typed `…OrVariable` hold a
8
10
  * `$name` reference in place of the value. Enumerated values stay strings so a value this reader
9
- * does not know falls back to the default instead of rejecting the document.
11
+ * does not know falls back to the default instead of rejecting the document. Numbers must be
12
+ * finite: JSON reads a literal too large for a double, such as `1e999`, as Infinity and writes it
13
+ * back as `null`, so a document holding one would save as a file no reader opens.
10
14
  */
11
15
 
12
- const numSchema = v.union([v.number(), v.string()])
16
+ const numSchema = v.union([finiteNumberSchema, v.string()])
13
17
  const boolSchema = v.union([v.boolean(), v.string()])
14
18
  const strSchema = v.string()
15
19
  const sidesSchema = v.looseObject({
@@ -35,7 +39,9 @@ const gradientPaintSchema = v.looseObject({
35
39
  ...paintBase,
36
40
  gradientType: v.optional(strSchema),
37
41
  opacity: v.optional(numSchema),
38
- center: v.optional(v.looseObject({ x: v.optional(v.number()), y: v.optional(v.number()) })),
42
+ center: v.optional(
43
+ v.looseObject({ x: v.optional(finiteNumberSchema), y: v.optional(finiteNumberSchema) })
44
+ ),
39
45
  size: v.optional(
40
46
  v.looseObject({
41
47
  width: v.optional(numSchema),
@@ -53,7 +59,14 @@ const imagePaintSchema = v.looseObject({
53
59
  url: v.optional(strSchema),
54
60
  mode: v.optional(strSchema),
55
61
  transform: v.optional(
56
- v.tuple([v.number(), v.number(), v.number(), v.number(), v.number(), v.number()])
62
+ v.tuple([
63
+ finiteNumberSchema,
64
+ finiteNumberSchema,
65
+ finiteNumberSchema,
66
+ finiteNumberSchema,
67
+ finiteNumberSchema,
68
+ finiteNumberSchema
69
+ ])
57
70
  )
58
71
  })
59
72
 
@@ -139,8 +152,8 @@ const nodeFields = {
139
152
  * as plugin data, so any value is read as it is rather than rejecting the document.
140
153
  */
141
154
  metadata: v.optional(v.unknown()),
142
- x: v.optional(v.number()),
143
- y: v.optional(v.number()),
155
+ x: v.optional(finiteNumberSchema),
156
+ y: v.optional(finiteNumberSchema),
144
157
  width: v.optional(sizeSchema),
145
158
  height: v.optional(sizeSchema),
146
159
  reusable: v.optional(v.boolean()),
@@ -177,7 +190,9 @@ const nodeFields = {
177
190
  polygonCount: v.optional(numSchema),
178
191
  geometry: v.optional(strSchema),
179
192
  fillRule: v.optional(strSchema),
180
- viewBox: v.optional(v.tuple([v.number(), v.number(), v.number(), v.number()])),
193
+ viewBox: v.optional(
194
+ v.tuple([finiteNumberSchema, finiteNumberSchema, finiteNumberSchema, finiteNumberSchema])
195
+ ),
181
196
 
182
197
  content: v.optional(strSchema),
183
198
  textGrowth: v.optional(strSchema),
@@ -253,7 +268,7 @@ const nodeSchema: v.GenericSchema<unknown, PenNode> = v.looseObject({
253
268
  descendants: v.optional(v.record(strSchema, overrideSchema))
254
269
  })
255
270
 
256
- const variableScalarSchema = v.union([v.string(), v.number(), v.boolean()])
271
+ const variableScalarSchema = v.union([v.string(), finiteNumberSchema, v.boolean()])
257
272
 
258
273
  const variableSchema = v.looseObject({
259
274
  type: strSchema,
@@ -270,6 +285,8 @@ const variableSchema = v.looseObject({
270
285
 
271
286
  const penDocumentSchema = v.looseObject({
272
287
  version: v.optional(strSchema),
288
+ // The page's name. The format is single-page, so it belongs to the document.
289
+ name: v.optional(strSchema),
273
290
  themes: v.optional(v.record(strSchema, v.array(strSchema))),
274
291
  imports: v.optional(v.record(strSchema, strSchema)),
275
292
  variables: v.optional(v.record(strSchema, variableSchema)),
package/src/variables.ts CHANGED
@@ -79,7 +79,11 @@ export function resolveBoolean(
79
79
  ): boolean {
80
80
  if (value === undefined) return fallback
81
81
  if (typeof value === 'boolean') return value
82
- return isVarRef(value) ? ctx.resolveBoolean(value) : value !== 'false'
82
+ if (isVarRef(value)) return ctx.resolveBoolean(value)
83
+ // Text other than true or false, such as `no` or `0`, reads as the default, as a missing
84
+ // value does.
85
+ const text = value.toLowerCase()
86
+ return text === 'true' || text === 'false' ? text === 'true' : fallback
83
87
  }
84
88
 
85
89
  /** A string, or the value of the string variable it references. */
@@ -178,7 +182,10 @@ function sceneValue(
178
182
  return aliasId ? { aliasId } : emptyValue(type)
179
183
  }
180
184
  if (type === 'COLOR') return typeof raw === 'string' ? parseColor(raw) : emptyValue(type)
181
- if (type === 'FLOAT') return typeof raw === 'number' ? raw : Number(raw) || 0
185
+ if (type === 'FLOAT') {
186
+ const value = Number(raw)
187
+ return Number.isFinite(value) ? value : 0
188
+ }
182
189
  if (type === 'BOOLEAN') return typeof raw === 'boolean' ? raw : raw === 'true'
183
190
  return String(raw)
184
191
  }
package/src/write.ts CHANGED
@@ -32,12 +32,14 @@ import type {
32
32
  PenVariable,
33
33
  PenVariableScalar
34
34
  } from './schema'
35
+ import { writtenNodeIds } from './written-nodes'
35
36
  const PEN_VERSION = '2.21'
36
37
  const PEN_PLUGIN_FIELDS: Readonly<Partial<Record<string, string>>> = {
37
38
  'pen/type': 'type',
38
39
  'pen/ref': 'ref',
39
40
  'pen/generated': 'generated',
40
41
  'pen/context': 'context',
42
+ 'pen/metadata': 'metadata',
41
43
  'pen/placeholder': 'placeholder',
42
44
  'pen/slot': 'slot',
43
45
  'pen/model': 'model',
@@ -54,13 +56,16 @@ type PenWriteState = {
54
56
  graph: SceneGraph
55
57
  variables: ReadonlyMap<string, Variable>
56
58
  collections: ReadonlyMap<string, VariableCollection>
59
+ /** Nodes the document will contain, so an instance refs only a component that is written. */
60
+ writtenIds: ReadonlySet<string>
57
61
  }
58
62
  /** Snaps matrix values that are within floating-point noise of an integer. */
59
63
  function snapCoordinate(value: number): number {
60
64
  return Math.abs(value - Math.round(value)) < 1e-9 ? Math.round(value) : value
61
65
  }
66
+ /** A non-finite number has no JSON form, so it is written as 0 to keep the file loadable. */
62
67
  function rounded(value: number): number {
63
- return Number(value.toFixed(3))
68
+ return Number.isFinite(value) ? Number(value.toFixed(3)) : 0
64
69
  }
65
70
  function colorValue(value: Color): string {
66
71
  return value.a === 1 ? colorToHex(value) : colorToHex8(value)
@@ -521,7 +526,7 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
521
526
  if (effects.length > 0)
522
527
  result.effect = effects.length === 1 ? effectOf(effects[0]) : effects.map(effectOf)
523
528
  if (node.type === 'COMPONENT' || node.type === 'COMPONENT_SET') result.reusable = true
524
- if (node.type === 'INSTANCE' && node.componentId) {
529
+ if (node.type === 'INSTANCE' && node.componentId && state.writtenIds.has(node.componentId)) {
525
530
  result.type = 'ref'
526
531
  result.ref = node.componentId
527
532
  return true
@@ -569,14 +574,14 @@ export function penDocumentFromGraph(graph: SceneGraph): PenDocument {
569
574
  const collections = new Map<string, VariableCollection>()
570
575
  for (const collection of graph.variableCollections.values())
571
576
  collections.set(collection.id, collection)
572
- const state: PenWriteState = { graph, variables, collections }
577
+ const state: PenWriteState = { graph, variables, collections, writtenIds: writtenNodeIds(graph) }
573
578
  const children = graph.getPages().flatMap((page) =>
574
579
  graph
575
580
  .getChildren(page.id)
576
581
  .filter((node) => !node.internalOnly)
577
582
  .map((node) => writeNode(state, node))
578
583
  )
579
- const document: PenDocument = { version: PEN_VERSION, children }
584
+ const document: PenDocument = { version: PEN_VERSION, name: graph.getPages()[0]?.name, children }
580
585
  const variablesValue = variablesOf(state)
581
586
  if (variablesValue) document.variables = variablesValue
582
587
  const themes = themesOf(state)
@@ -0,0 +1,17 @@
1
+ import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
2
+
3
+ /**
4
+ * Collects the nodes `writeNode` emits. Instance subtrees are skipped because a ref writes
5
+ * no children; an instance whose component is missing here is written as a frame instead.
6
+ */
7
+ export function writtenNodeIds(graph: SceneGraph): Set<string> {
8
+ const ids = new Set<string>()
9
+ const visit = (node: SceneNode): void => {
10
+ if (node.internalOnly) return
11
+ ids.add(node.id)
12
+ if (node.type === 'STICKY' || node.type === 'INSTANCE') return
13
+ for (const child of graph.getChildren(node.id)) visit(child)
14
+ }
15
+ for (const page of graph.getPages()) for (const node of graph.getChildren(page.id)) visit(node)
16
+ return ids
17
+ }