@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/dist/chunks/annotations.js +54 -4
- package/dist/chunks/annotations.js.map +1 -1
- package/dist/chunks/document-libraries.js +35 -0
- package/dist/chunks/document-libraries.js.map +1 -0
- package/dist/chunks/effects.js +115 -2
- package/dist/chunks/effects.js.map +1 -1
- package/dist/chunks/icons.js +6 -1
- package/dist/chunks/icons.js.map +1 -1
- package/dist/chunks/index.js +288 -28
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts +125 -4
- package/dist/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/annotations.ts +3 -0
- package/src/document-libraries.ts +52 -0
- package/src/icons.ts +6 -0
- package/src/images.ts +12 -0
- package/src/index.ts +6 -1
- package/src/library.ts +24 -1
- package/src/nodes.ts +28 -3
- package/src/plugin-data.ts +40 -0
- package/src/read.ts +22 -2
- package/src/schema.ts +123 -1
- package/src/text.ts +3 -1
- package/src/variables.ts +27 -3
- package/src/write.ts +342 -24
package/src/write.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit'
|
|
2
|
+
import { fromUint8Array } from 'js-base64'
|
|
3
|
+
|
|
1
4
|
import type {
|
|
2
5
|
BlendMode,
|
|
6
|
+
EnabledLibraryBinding,
|
|
3
7
|
Fill,
|
|
4
8
|
GradientTransform,
|
|
5
9
|
LayoutAlign,
|
|
@@ -15,16 +19,25 @@ import type {
|
|
|
15
19
|
import { SceneGraph as SceneGraphValue } from '@pairdraw/scene-graph'
|
|
16
20
|
import { colorToHex, colorToHex8 } from '@pairdraw/scene-graph/color'
|
|
17
21
|
import { getNodeLocalMatrix } from '@pairdraw/scene-graph/coordinate'
|
|
22
|
+
import { readDesignMemory } from '@pairdraw/scene-graph/design-memory'
|
|
18
23
|
import { stickySublayers } from '@pairdraw/scene-graph/generated'
|
|
19
24
|
import { applyGradientTransform, invertGradientTransform } from '@pairdraw/scene-graph/gradient'
|
|
25
|
+
import { imageMimeType } from '@pairdraw/scene-graph/images'
|
|
20
26
|
import { layoutSizing } from '@pairdraw/scene-graph/layout'
|
|
21
27
|
import type { Color, Vector } from '@pairdraw/scene-graph/primitives'
|
|
28
|
+
import { readStoredStatusWorkflow } from '@pairdraw/scene-graph/statuses'
|
|
29
|
+
import { readTokenSources, type TokenSource } from '@pairdraw/scene-graph/token-sources'
|
|
22
30
|
|
|
31
|
+
import { readPenImports, readSourceLibraryPublication } from './document-libraries'
|
|
23
32
|
import { networkToSVGPath } from './geometry'
|
|
24
|
-
import {
|
|
33
|
+
import { penCropTransform } from './images'
|
|
34
|
+
import { importedComponentIds } from './library'
|
|
35
|
+
import { PEN_PLUGIN_DATA_NAMESPACE, readIconOrigin } from './plugin-data'
|
|
25
36
|
import type {
|
|
26
37
|
PenDocument,
|
|
27
38
|
PenEffect,
|
|
39
|
+
PenOverride,
|
|
40
|
+
PenImagePaint,
|
|
28
41
|
PenNode,
|
|
29
42
|
PenNumber,
|
|
30
43
|
PenPaint,
|
|
@@ -59,7 +72,25 @@ type PenWriteState = {
|
|
|
59
72
|
collections: ReadonlyMap<string, VariableCollection>
|
|
60
73
|
/** Nodes the document will contain, so an instance refs only a component that is written. */
|
|
61
74
|
writtenIds: ReadonlySet<string>
|
|
75
|
+
/** Components the file resolves through its `imports`, which a ref may name without writing them. */
|
|
76
|
+
importedIds: ReadonlySet<string>
|
|
77
|
+
/** What the file cannot hold, reported once writing leaves it out. */
|
|
78
|
+
warnings: string[]
|
|
79
|
+
drops: {
|
|
80
|
+
images: number
|
|
81
|
+
effects: number
|
|
82
|
+
filters: number
|
|
83
|
+
scroll: number
|
|
84
|
+
fontFeatures: number
|
|
85
|
+
}
|
|
62
86
|
}
|
|
87
|
+
|
|
88
|
+
/** What a caller learns about content a written `.pen` file cannot hold. */
|
|
89
|
+
export interface PenWriteOptions {
|
|
90
|
+
/** Collects one sentence per kind of content left out, in place of silently losing it. */
|
|
91
|
+
warnings?: string[]
|
|
92
|
+
}
|
|
93
|
+
|
|
63
94
|
/** Snaps matrix values that are within floating-point noise of an integer. */
|
|
64
95
|
function snapCoordinate(value: number): number {
|
|
65
96
|
return Math.abs(value - Math.round(value)) < 1e-9 ? Math.round(value) : value
|
|
@@ -173,8 +204,31 @@ function gradientPaint(fill: Fill): PenPaint {
|
|
|
173
204
|
}
|
|
174
205
|
}
|
|
175
206
|
|
|
176
|
-
|
|
177
|
-
|
|
207
|
+
/**
|
|
208
|
+
* An image fill as a pen.dev paint whose image travels with the file: Pencil.dev opens a data URL
|
|
209
|
+
* and the scene graph keeps the bytes, so the fill is written whole instead of pointing at a hash
|
|
210
|
+
* no reader can resolve. A paint whose bytes are gone is left out and reported, never written as a
|
|
211
|
+
* dead URL.
|
|
212
|
+
*/
|
|
213
|
+
function imagePaint(state: PenWriteState, fill: Fill): PenPaint | null {
|
|
214
|
+
const bytes = fill.imageHash ? state.graph.images.get(fill.imageHash) : undefined
|
|
215
|
+
if (!bytes) {
|
|
216
|
+
state.drops.images++
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
if (fill.filters) state.drops.filters++
|
|
220
|
+
const paint: PenImagePaint = {
|
|
221
|
+
type: 'image',
|
|
222
|
+
...paintBase(fill),
|
|
223
|
+
url: `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
|
|
224
|
+
}
|
|
225
|
+
if (fill.imageScaleMode === 'FIT') paint.mode = 'contain'
|
|
226
|
+
if (fill.imageScaleMode === 'CROP') {
|
|
227
|
+
paint.mode = 'stretch'
|
|
228
|
+
const transform = fill.imageTransform && penCropTransform(fill.imageTransform)
|
|
229
|
+
if (transform) paint.transform = transform
|
|
230
|
+
}
|
|
231
|
+
return paint
|
|
178
232
|
}
|
|
179
233
|
|
|
180
234
|
function penPaint(
|
|
@@ -183,13 +237,13 @@ function penPaint(
|
|
|
183
237
|
field: 'fills' | 'strokes',
|
|
184
238
|
fill: Fill,
|
|
185
239
|
index: number
|
|
186
|
-
): PenPaint {
|
|
240
|
+
): PenPaint | null {
|
|
187
241
|
if (fill.type === 'SOLID') {
|
|
188
242
|
const binding = variableReference(state, node.boundVariables[`${field}/${index}/color`])
|
|
189
243
|
return { type: 'color', color: binding ?? colorValue(fill.color), ...paintBase(fill) }
|
|
190
244
|
}
|
|
191
245
|
if (fill.type.startsWith('GRADIENT_')) return gradientPaint(fill)
|
|
192
|
-
if (fill.type === 'IMAGE') return imagePaint(fill)
|
|
246
|
+
if (fill.type === 'IMAGE') return imagePaint(state, fill)
|
|
193
247
|
return { type: fill.type.toLowerCase(), ...paintBase(fill) }
|
|
194
248
|
}
|
|
195
249
|
|
|
@@ -200,7 +254,10 @@ function paintsOf(
|
|
|
200
254
|
paints: readonly Fill[]
|
|
201
255
|
): PenPaints | undefined {
|
|
202
256
|
if (paints.length === 0) return undefined
|
|
203
|
-
const result = paints
|
|
257
|
+
const result = paints
|
|
258
|
+
.map((paint, index) => penPaint(state, node, field, paint, index))
|
|
259
|
+
.filter((paint): paint is PenPaint => paint !== null)
|
|
260
|
+
if (result.length === 0) return undefined
|
|
204
261
|
return result.length === 1 ? result[0] : result
|
|
205
262
|
}
|
|
206
263
|
|
|
@@ -446,24 +503,27 @@ function penVariable(
|
|
|
446
503
|
state: PenWriteState
|
|
447
504
|
): PenVariable {
|
|
448
505
|
const modes = collection?.modes ?? []
|
|
506
|
+
const syntax = variable.codeSyntax ? { codeSyntax: variable.codeSyntax } : {}
|
|
449
507
|
if (modes.length <= 1)
|
|
450
508
|
return {
|
|
451
509
|
type: variableType(variable.type),
|
|
452
|
-
value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ''], state)
|
|
510
|
+
value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ''], state),
|
|
511
|
+
...syntax
|
|
453
512
|
}
|
|
454
513
|
return {
|
|
455
514
|
type: variableType(variable.type),
|
|
456
515
|
value: modes.map((mode, index) => ({
|
|
457
516
|
value: variableScalar(variable.valuesByMode[mode.modeId], state),
|
|
458
517
|
...(index === 0 ? {} : { theme: { [collection?.name ?? 'Mode']: mode.name } })
|
|
459
|
-
}))
|
|
518
|
+
})),
|
|
519
|
+
...syntax
|
|
460
520
|
}
|
|
461
521
|
}
|
|
462
522
|
|
|
463
523
|
function variablesOf(state: PenWriteState): Record<string, PenVariable> | undefined {
|
|
464
|
-
if (state.
|
|
524
|
+
if (state.variables.size === 0) return undefined
|
|
465
525
|
return Object.fromEntries(
|
|
466
|
-
[...state.
|
|
526
|
+
[...state.variables.values()]
|
|
467
527
|
.sort((left, right) => left.name.localeCompare(right.name))
|
|
468
528
|
.map((variable) => [
|
|
469
529
|
variable.name,
|
|
@@ -473,14 +533,25 @@ function variablesOf(state: PenWriteState): Record<string, PenVariable> | undefi
|
|
|
473
533
|
}
|
|
474
534
|
|
|
475
535
|
function themesOf(state: PenWriteState): PenDocument['themes'] {
|
|
476
|
-
if (state.
|
|
536
|
+
if (state.collections.size === 0) return undefined
|
|
477
537
|
return Object.fromEntries(
|
|
478
|
-
[...state.
|
|
538
|
+
[...state.collections.values()]
|
|
479
539
|
.sort((left, right) => left.name.localeCompare(right.name))
|
|
480
540
|
.map((collection) => [collection.name, collection.modes.map((mode) => mode.name)])
|
|
481
541
|
)
|
|
482
542
|
}
|
|
483
543
|
|
|
544
|
+
/** The selector each mode applies in, by collection and mode name; none when no mode names one. */
|
|
545
|
+
function modeConditionsOf(state: PenWriteState): PenDocument['modeConditions'] {
|
|
546
|
+
const entries = [...state.collections.values()].flatMap((collection) => {
|
|
547
|
+
const modes = collection.modes.flatMap((mode) =>
|
|
548
|
+
mode.condition ? [[mode.name, mode.condition] as const] : []
|
|
549
|
+
)
|
|
550
|
+
return modes.length > 0 ? [[collection.name, Object.fromEntries(modes)] as const] : []
|
|
551
|
+
})
|
|
552
|
+
return entries.length > 0 ? Object.fromEntries(entries) : undefined
|
|
553
|
+
}
|
|
554
|
+
|
|
484
555
|
function basicNodeFields(state: PenWriteState, node: SceneNode, result: PenNode): void {
|
|
485
556
|
if (node.name) result.name = node.name
|
|
486
557
|
const placement = penCoordinates(node)
|
|
@@ -530,6 +601,125 @@ function themeFields(state: PenWriteState, node: SceneNode): Record<string, stri
|
|
|
530
601
|
return Object.keys(theme).length > 0 ? theme : undefined
|
|
531
602
|
}
|
|
532
603
|
|
|
604
|
+
/**
|
|
605
|
+
* The pen fields an instance override writes: what it changes on a layer, not the placement and
|
|
606
|
+
* layout a reopen recalculates from the component and the boxes around it.
|
|
607
|
+
*/
|
|
608
|
+
const OVERRIDABLE_FIELDS: ReadonlySet<string> = new Set([
|
|
609
|
+
'content',
|
|
610
|
+
'fill',
|
|
611
|
+
'stroke',
|
|
612
|
+
'strokeWidth',
|
|
613
|
+
'cornerRadius',
|
|
614
|
+
'effect',
|
|
615
|
+
'opacity',
|
|
616
|
+
'enabled',
|
|
617
|
+
'fontFamily',
|
|
618
|
+
'fontSize',
|
|
619
|
+
'fontWeight',
|
|
620
|
+
'fontStyle',
|
|
621
|
+
'textAlign',
|
|
622
|
+
'textAlignVertical',
|
|
623
|
+
'lineHeight',
|
|
624
|
+
'letterSpacing',
|
|
625
|
+
'href'
|
|
626
|
+
])
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* The path a copy sits at in its component, from the instance down: the ids of the layers it
|
|
630
|
+
* copies, as pen names a `descendants` override. A copy that swapped its component copies `swapOf`
|
|
631
|
+
* instead of the component it draws.
|
|
632
|
+
*/
|
|
633
|
+
function overridePath(
|
|
634
|
+
graph: SceneGraph,
|
|
635
|
+
instanceId: string,
|
|
636
|
+
copyId: string,
|
|
637
|
+
swapOf?: string
|
|
638
|
+
): string | null {
|
|
639
|
+
const parts: string[] = []
|
|
640
|
+
let current = graph.getNode(copyId)
|
|
641
|
+
while (current && current.id !== instanceId) {
|
|
642
|
+
const id = current.id === copyId && swapOf ? swapOf : current.componentId
|
|
643
|
+
if (!id) return null
|
|
644
|
+
parts.unshift(id)
|
|
645
|
+
current = current.parentId ? graph.getNode(current.parentId) : undefined
|
|
646
|
+
}
|
|
647
|
+
return current ? parts.join('/') : null
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* The fields a swap override names an icon by: pen carries the glyph, not the component drawn.
|
|
652
|
+
*/
|
|
653
|
+
function iconOverride(graph: SceneGraph, componentId: string | null): PenOverride | undefined {
|
|
654
|
+
const origin = readIconOrigin(componentId ? graph.getNode(componentId) : undefined)
|
|
655
|
+
if (!origin) return undefined
|
|
656
|
+
const override: PenOverride = { icon: origin.icon }
|
|
657
|
+
if (origin.library) override.library = origin.library
|
|
658
|
+
if (origin.weight !== undefined) override.weight = origin.weight
|
|
659
|
+
return override
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* The overrides an instance writes, and whether one of them is a swap the file cannot name. A
|
|
664
|
+
* layer that swapped its component draws another component than the one it copies, which pen holds
|
|
665
|
+
* as a glyph for an icon; any other swap is what the instance shows, not what it links to.
|
|
666
|
+
*/
|
|
667
|
+
function instanceDescendants(
|
|
668
|
+
state: PenWriteState,
|
|
669
|
+
node: SceneNode
|
|
670
|
+
): { descendants?: Record<string, PenOverride>; unrepresentable: boolean } {
|
|
671
|
+
const overridden = [...node.instanceOverrides.descendants.keys()]
|
|
672
|
+
if (overridden.length === 0) return { unrepresentable: false }
|
|
673
|
+
// Comparing writes fields the moment a document writes them, but their drops are counted once.
|
|
674
|
+
const probe: PenWriteState = {
|
|
675
|
+
...state,
|
|
676
|
+
drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
|
|
677
|
+
}
|
|
678
|
+
const descendants: Record<string, PenOverride> = {}
|
|
679
|
+
let unrepresentable = false
|
|
680
|
+
for (const copyId of overridden) {
|
|
681
|
+
const overrides = node.instanceOverrides.descendants.get(copyId)
|
|
682
|
+
const swapped = overrides?.has('componentId') === true
|
|
683
|
+
const swapOf = swapped ? overrides.get('sourceComponentId') : undefined
|
|
684
|
+
const path = overridePath(
|
|
685
|
+
state.graph,
|
|
686
|
+
node.id,
|
|
687
|
+
copyId,
|
|
688
|
+
typeof swapOf === 'string' ? swapOf : undefined
|
|
689
|
+
)
|
|
690
|
+
const copy = state.graph.getNode(copyId)
|
|
691
|
+
if (!path || !copy) {
|
|
692
|
+
unrepresentable = true
|
|
693
|
+
continue
|
|
694
|
+
}
|
|
695
|
+
if (swapped) {
|
|
696
|
+
const icon = iconOverride(state.graph, copy.componentId)
|
|
697
|
+
if (!icon) {
|
|
698
|
+
unrepresentable = true
|
|
699
|
+
continue
|
|
700
|
+
}
|
|
701
|
+
descendants[path] = icon
|
|
702
|
+
continue
|
|
703
|
+
}
|
|
704
|
+
const master = copy.componentId ? state.graph.getNode(copy.componentId) : undefined
|
|
705
|
+
if (!master) {
|
|
706
|
+
unrepresentable = true
|
|
707
|
+
continue
|
|
708
|
+
}
|
|
709
|
+
const own = writeNode(probe, copy)
|
|
710
|
+
const base = writeNode(probe, master)
|
|
711
|
+
const override: PenOverride = {}
|
|
712
|
+
for (const field of OVERRIDABLE_FIELDS) {
|
|
713
|
+
if (!isEqual(own[field], base[field])) override[field] = own[field]
|
|
714
|
+
}
|
|
715
|
+
if (Object.keys(override).length > 0) descendants[path] = override
|
|
716
|
+
}
|
|
717
|
+
return {
|
|
718
|
+
descendants: Object.keys(descendants).length > 0 ? descendants : undefined,
|
|
719
|
+
unrepresentable
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
533
723
|
function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNode): boolean {
|
|
534
724
|
if (
|
|
535
725
|
node.type === 'TEXT' &&
|
|
@@ -539,20 +729,37 @@ function semanticNodeFields(state: PenWriteState, node: SceneNode, result: PenNo
|
|
|
539
729
|
result.width = 'fit_content'
|
|
540
730
|
if (node.type === 'STICKY') result.content = stickySublayers(state.graph, node)?.body.text ?? ''
|
|
541
731
|
penPluginFields(node, result)
|
|
542
|
-
// Shader effects have no pen.dev form
|
|
543
|
-
const effects = node.effects.filter((effect): effect is ShadowEffect =>
|
|
732
|
+
// Shader, noise, and texture effects have no pen.dev form; dropping them keeps the file loadable.
|
|
733
|
+
const effects = node.effects.filter((effect): effect is ShadowEffect => {
|
|
734
|
+
if (effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW') return true
|
|
735
|
+
if (effect.type === 'LAYER_BLUR' || effect.type === 'BACKGROUND_BLUR') return true
|
|
736
|
+
if (effect.type === 'NOISE' || effect.type === 'TEXTURE') state.drops.effects++
|
|
737
|
+
return false
|
|
738
|
+
})
|
|
544
739
|
if (effects.length > 0)
|
|
545
740
|
result.effect = effects.length === 1 ? effectOf(effects[0]) : effects.map(effectOf)
|
|
546
741
|
if (node.type === 'COMPONENT' || node.type === 'COMPONENT_SET') result.reusable = true
|
|
547
|
-
if (
|
|
742
|
+
if (
|
|
743
|
+
node.type === 'INSTANCE' &&
|
|
744
|
+
node.componentId &&
|
|
745
|
+
(state.writtenIds.has(node.componentId) || state.importedIds.has(node.componentId))
|
|
746
|
+
) {
|
|
747
|
+
const overrides = instanceDescendants(state, node)
|
|
748
|
+
// The file names a swap through the library it imports; one it cannot name is written as the
|
|
749
|
+
// layers the instance draws, which keeps what it shows, as before the library work.
|
|
750
|
+
if (overrides.unrepresentable && !state.writtenIds.has(node.componentId)) return false
|
|
548
751
|
result.type = 'ref'
|
|
549
752
|
result.ref = node.componentId
|
|
753
|
+
if (overrides.descendants) result.descendants = overrides.descendants
|
|
550
754
|
return true
|
|
551
755
|
}
|
|
552
756
|
return false
|
|
553
757
|
}
|
|
554
758
|
function writeNode(state: PenWriteState, node: SceneNode): PenNode {
|
|
759
|
+
if (node.overflowDirection !== 'NONE') state.drops.scroll++
|
|
760
|
+
if (node.fontFeatures.length > 0) state.drops.fontFeatures++
|
|
555
761
|
const result: PenNode = { id: node.id, type: nodeType(node) }
|
|
762
|
+
if (node.status !== null) result.status = node.status
|
|
556
763
|
basicNodeFields(state, node, result)
|
|
557
764
|
if (semanticNodeFields(state, node, result)) return result
|
|
558
765
|
// Written after the preserved pen fields, so a regenerated path wins over the source text.
|
|
@@ -597,32 +804,143 @@ function sortKeys(value: unknown): unknown {
|
|
|
597
804
|
return value
|
|
598
805
|
}
|
|
599
806
|
|
|
600
|
-
export function penDocumentFromGraph(
|
|
807
|
+
export function penDocumentFromGraph(
|
|
808
|
+
graph: SceneGraph,
|
|
809
|
+
options: PenWriteOptions = {}
|
|
810
|
+
): PenDocument {
|
|
811
|
+
// A deleted variable has no state `.pen` can hold, so layers bound to it write its value.
|
|
601
812
|
const variables = new Map<string, Variable>()
|
|
602
|
-
for (const variable of graph.
|
|
813
|
+
for (const variable of graph.getLiveVariables()) variables.set(variable.id, variable)
|
|
603
814
|
const collections = new Map<string, VariableCollection>()
|
|
604
|
-
for (const collection of graph.
|
|
605
|
-
|
|
606
|
-
const
|
|
607
|
-
const
|
|
815
|
+
for (const collection of graph.getLiveCollections()) collections.set(collection.id, collection)
|
|
816
|
+
// The file declares its imports, so a ref to a component one of them defines resolves on reopen.
|
|
817
|
+
const imports = readPenImports(graph)
|
|
818
|
+
const state: PenWriteState = {
|
|
819
|
+
graph,
|
|
820
|
+
variables,
|
|
821
|
+
collections,
|
|
822
|
+
writtenIds: writtenNodeIds(graph),
|
|
823
|
+
importedIds: importedComponentIds(graph, imports ?? {}),
|
|
824
|
+
warnings: options.warnings ?? [],
|
|
825
|
+
drops: { images: 0, effects: 0, filters: 0, scroll: 0, fontFeatures: 0 }
|
|
826
|
+
}
|
|
827
|
+
const pages = graph.getPages()
|
|
828
|
+
const children = pages.flatMap((page) =>
|
|
608
829
|
graph
|
|
609
830
|
.getChildren(page.id)
|
|
610
831
|
.filter((node) => !node.internalOnly)
|
|
611
832
|
.map((node) => writeNode(state, node))
|
|
612
833
|
)
|
|
613
|
-
const document: PenDocument = { version: PEN_VERSION, name:
|
|
834
|
+
const document: PenDocument = { version: PEN_VERSION, name: pages[0]?.name, children }
|
|
614
835
|
const variablesValue = variablesOf(state)
|
|
615
836
|
if (variablesValue) document.variables = variablesValue
|
|
616
837
|
const themes = themesOf(state)
|
|
617
838
|
if (themes) document.themes = themes
|
|
839
|
+
const conditions = modeConditionsOf(state)
|
|
840
|
+
if (conditions) document.modeConditions = conditions
|
|
841
|
+
const tokenSources = readTokenSources(graph).map((source) => {
|
|
842
|
+
// The reopen gives variables new ids; these are the names the file holds for them. `names`
|
|
843
|
+
// stays as the last sync recorded it, where the token each variable stands for lives.
|
|
844
|
+
const currentNames = Object.fromEntries(
|
|
845
|
+
Object.keys(source.names ?? {}).flatMap((id) => {
|
|
846
|
+
const variable = variables.get(id)
|
|
847
|
+
const collection = variable ? collections.get(variable.collectionId) : undefined
|
|
848
|
+
return variable && collection ? [[id, `${collection.name}/${variable.name}`]] : []
|
|
849
|
+
})
|
|
850
|
+
)
|
|
851
|
+
const modes = {
|
|
852
|
+
...source.modes,
|
|
853
|
+
...Object.fromEntries(
|
|
854
|
+
[...collections.values()].map((collection) => [
|
|
855
|
+
collection.name,
|
|
856
|
+
Object.fromEntries(collection.modes.map((mode) => [mode.modeId, mode.name]))
|
|
857
|
+
])
|
|
858
|
+
)
|
|
859
|
+
}
|
|
860
|
+
const refreshed: TokenSource = { ...source }
|
|
861
|
+
if (Object.keys(currentNames).length > 0) refreshed.currentNames = currentNames
|
|
862
|
+
if (Object.keys(modes).length > 0) refreshed.modes = modes
|
|
863
|
+
return refreshed
|
|
864
|
+
})
|
|
865
|
+
if (tokenSources.length > 0) document.tokenSources = tokenSources
|
|
866
|
+
const designMemory = readDesignMemory(graph)
|
|
867
|
+
if (designMemory) document.designMemory = designMemory
|
|
868
|
+
// Only a document that chose its statuses writes them: the default falls back on read.
|
|
869
|
+
const statuses = readStoredStatusWorkflow(graph)
|
|
870
|
+
if (statuses) document.statuses = statuses
|
|
871
|
+
if (imports) document.imports = imports
|
|
872
|
+
const enabledLibraries = enabledLibrariesOf(graph, state)
|
|
873
|
+
if (enabledLibraries) document.enabledLibraries = enabledLibraries
|
|
874
|
+
const publication = readSourceLibraryPublication(graph)
|
|
875
|
+
if (publication) document.sourceLibraryPublication = publication
|
|
876
|
+
if (pages.length > 1) {
|
|
877
|
+
const name = document.name ?? 'Page 1'
|
|
878
|
+
state.warnings.push(
|
|
879
|
+
`Pencil.dev files hold one page; ${pages.length} pages were merged into “${name}”.`
|
|
880
|
+
)
|
|
881
|
+
}
|
|
882
|
+
if (graph.documentColorSpace === 'display-p3') {
|
|
883
|
+
state.warnings.push(
|
|
884
|
+
'Colors are written as sRGB hex, the only form a Pencil.dev file holds; a Display P3 color outside sRGB comes out as the nearest sRGB color.'
|
|
885
|
+
)
|
|
886
|
+
}
|
|
887
|
+
reportDrops(state)
|
|
618
888
|
return document
|
|
619
889
|
}
|
|
620
890
|
|
|
891
|
+
/**
|
|
892
|
+
* The library bindings a `.pen` file records, or null when the document has none. The definitions
|
|
893
|
+
* live on the hidden page, which a Pencil.dev file cannot hold, so the file records the bindings
|
|
894
|
+
* and says that the components it was built from come back as placeholders.
|
|
895
|
+
*/
|
|
896
|
+
function enabledLibrariesOf(
|
|
897
|
+
graph: SceneGraph,
|
|
898
|
+
state: PenWriteState
|
|
899
|
+
): EnabledLibraryBinding[] | null {
|
|
900
|
+
const bindings = [...graph.enabledLibraries.values()]
|
|
901
|
+
if (bindings.length === 0) return null
|
|
902
|
+
const enabled = bindings.filter((binding) => binding.enabled).length
|
|
903
|
+
if (enabled > 0)
|
|
904
|
+
state.warnings.push(
|
|
905
|
+
`Library components came from ${enabled} enabled ${enabled === 1 ? 'library' : 'libraries'}, whose definitions Pencil.dev files do not hold; re-enable those libraries after reopening to restore them.`
|
|
906
|
+
)
|
|
907
|
+
return bindings
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** Adds one sentence for each kind of content the written file had to leave out. */
|
|
911
|
+
function reportDrops(state: PenWriteState): void {
|
|
912
|
+
const { images, effects, filters, scroll, fontFeatures } = state.drops
|
|
913
|
+
if (images > 0)
|
|
914
|
+
state.warnings.push(
|
|
915
|
+
`Image fills with missing image data were left out (${images}); Pencil.dev files cannot hold a paint without its image.`
|
|
916
|
+
)
|
|
917
|
+
if (effects > 0)
|
|
918
|
+
state.warnings.push(
|
|
919
|
+
`Noise and texture effects were left out (${effects}); Pencil.dev files have no form for them.`
|
|
920
|
+
)
|
|
921
|
+
if (filters > 0)
|
|
922
|
+
state.warnings.push(
|
|
923
|
+
`Image adjustment filters were left out (${filters}); Pencil.dev files have no form for them.`
|
|
924
|
+
)
|
|
925
|
+
if (scroll > 0)
|
|
926
|
+
state.warnings.push(
|
|
927
|
+
`Scroll overflow was left out (${scroll}); Pencil.dev files have no form for it.`
|
|
928
|
+
)
|
|
929
|
+
if (fontFeatures > 0)
|
|
930
|
+
state.warnings.push(
|
|
931
|
+
`OpenType font features were left out (${fontFeatures}); Pencil.dev files have no form for them.`
|
|
932
|
+
)
|
|
933
|
+
}
|
|
934
|
+
|
|
621
935
|
export function stringifyPenDocument(document: PenDocument): string {
|
|
622
936
|
return `${JSON.stringify(sortKeys(document), null, 2)}\n`
|
|
623
937
|
}
|
|
624
938
|
|
|
625
|
-
export function writePenDocument(
|
|
626
|
-
|
|
939
|
+
export function writePenDocument(
|
|
940
|
+
source: PenDocument | SceneGraph,
|
|
941
|
+
options: PenWriteOptions = {}
|
|
942
|
+
): string {
|
|
943
|
+
const document =
|
|
944
|
+
source instanceof SceneGraphValue ? penDocumentFromGraph(source, options) : source
|
|
627
945
|
return stringifyPenDocument(document)
|
|
628
946
|
}
|