@pairdraw/pen 0.16.1 → 0.17.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/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,7 +574,7 @@ 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)
@@ -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
+ }