@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/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SceneGraph } from "@pairdraw/scene-graph";
|
|
1
|
+
import { EnabledLibraryBinding, SceneGraph, SourceLibraryPublication } from "@pairdraw/scene-graph";
|
|
2
2
|
import * as v from "valibot";
|
|
3
3
|
import { Color } from "@pairdraw/scene-graph/primitives";
|
|
4
4
|
import { FontFamilyStyle } from "@pairdraw/scene-graph/font-style";
|
|
@@ -7,6 +7,11 @@ import { FontFamilyStyle } from "@pairdraw/scene-graph/font-style";
|
|
|
7
7
|
declare const nodeFields: {
|
|
8
8
|
name: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
9
9
|
context: v.OptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* The layer's status, an id in the document's status workflow, as Core keeps it on the layer.
|
|
12
|
+
* Pencil has no field for it, so it is an extension other Pencil clients ignore.
|
|
13
|
+
*/
|
|
14
|
+
status: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, string | undefined>]>, undefined>;
|
|
10
15
|
/**
|
|
11
16
|
* What a layer is, for agents: pen.dev writes a `type` and any fields of its own. It is only kept
|
|
12
17
|
* as plugin data, so any value is read as it is rather than rejecting the document.
|
|
@@ -359,6 +364,12 @@ declare const penDocumentSchema: v.LooseObjectSchema<{
|
|
|
359
364
|
readonly url: v.StringSchema<undefined>;
|
|
360
365
|
}, undefined>, undefined>, undefined>;
|
|
361
366
|
readonly themes: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.ArraySchema<v.StringSchema<undefined>, undefined>, undefined>, undefined>;
|
|
367
|
+
/**
|
|
368
|
+
* The selector each mode applies in, by collection and mode name: `.dark` for a Dark mode a
|
|
369
|
+
* stylesheet writes. Pencil has no field for it, so it is an extension other clients ignore;
|
|
370
|
+
* without it a reopened document reads the stylesheet's dark block as a new mode.
|
|
371
|
+
*/
|
|
372
|
+
readonly modeConditions: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.RecordSchema<v.StringSchema<undefined>, v.StringSchema<undefined>, undefined>, undefined>, undefined>;
|
|
362
373
|
readonly imports: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.StringSchema<undefined>, undefined>, undefined>;
|
|
363
374
|
readonly variables: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.LooseObjectSchema<{
|
|
364
375
|
readonly type: v.StringSchema<undefined>;
|
|
@@ -366,7 +377,108 @@ declare const penDocumentSchema: v.LooseObjectSchema<{
|
|
|
366
377
|
readonly value: v.UnionSchema<[v.StringSchema<undefined>, v.SchemaWithPipe<readonly [v.NumberSchema<undefined>, v.FiniteAction<number, undefined>]>, v.BooleanSchema<undefined>], undefined>;
|
|
367
378
|
readonly theme: v.OptionalSchema<v.RecordSchema<v.StringSchema<undefined>, v.StringSchema<undefined>, undefined>, undefined>;
|
|
368
379
|
}, undefined>, undefined>], undefined>;
|
|
380
|
+
/**
|
|
381
|
+
* The code that names the token, as Figma's variables carry it. Pencil has no field for it, so
|
|
382
|
+
* it is an extension other Pencil clients ignore; without it a reopened document cannot match a
|
|
383
|
+
* renamed variable to the token its stylesheet declares.
|
|
384
|
+
*/
|
|
385
|
+
readonly codeSyntax: v.OptionalSchema<v.RecordSchema<v.PicklistSchema<readonly ["WEB", "ANDROID", "iOS"], undefined>, v.StringSchema<undefined>, undefined>, undefined>;
|
|
369
386
|
}, undefined>, undefined>, undefined>;
|
|
387
|
+
/** The code the document's variables are linked to, as Core keeps it on the document node. */
|
|
388
|
+
readonly tokenSources: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, {
|
|
389
|
+
id: string;
|
|
390
|
+
kind: "css" | "folder" | "dtcg";
|
|
391
|
+
name: string;
|
|
392
|
+
path?: string | undefined;
|
|
393
|
+
syncedAt?: string | undefined;
|
|
394
|
+
snapshot: {
|
|
395
|
+
[x: string]: {
|
|
396
|
+
[x: string]: string;
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
names?: {
|
|
400
|
+
[x: string]: string;
|
|
401
|
+
} | undefined;
|
|
402
|
+
currentNames?: {
|
|
403
|
+
[x: string]: string;
|
|
404
|
+
} | undefined;
|
|
405
|
+
modes?: {
|
|
406
|
+
[x: string]: {
|
|
407
|
+
[x: string]: string;
|
|
408
|
+
};
|
|
409
|
+
} | undefined;
|
|
410
|
+
}[]>]>, undefined>;
|
|
411
|
+
/**
|
|
412
|
+
* The design memory agents follow, as Core keeps it on the document node: allowed values,
|
|
413
|
+
* decisions, and pinned examples. Pencil has no field for it, so it is an extension other
|
|
414
|
+
* Pencil clients ignore; without it a reopened document loses its rules.
|
|
415
|
+
*/
|
|
416
|
+
readonly designMemory: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, {
|
|
417
|
+
version: 1;
|
|
418
|
+
colors: {
|
|
419
|
+
fromSystem: boolean;
|
|
420
|
+
values: string[];
|
|
421
|
+
enforce: "refuse" | "warn" | "off";
|
|
422
|
+
};
|
|
423
|
+
fonts: {
|
|
424
|
+
fromSystem: boolean;
|
|
425
|
+
values: string[];
|
|
426
|
+
enforce: "refuse" | "warn" | "off";
|
|
427
|
+
};
|
|
428
|
+
fontSizes: {
|
|
429
|
+
fromSystem: boolean;
|
|
430
|
+
values: number[];
|
|
431
|
+
enforce: "refuse" | "warn" | "off";
|
|
432
|
+
};
|
|
433
|
+
spacing: {
|
|
434
|
+
fromSystem: boolean;
|
|
435
|
+
values: number[];
|
|
436
|
+
enforce: "refuse" | "warn" | "off";
|
|
437
|
+
};
|
|
438
|
+
radius: {
|
|
439
|
+
fromSystem: boolean;
|
|
440
|
+
values: number[];
|
|
441
|
+
enforce: "refuse" | "warn" | "off";
|
|
442
|
+
};
|
|
443
|
+
decisions: {
|
|
444
|
+
id: string;
|
|
445
|
+
text: string;
|
|
446
|
+
author: "person" | "agent";
|
|
447
|
+
}[];
|
|
448
|
+
examples: {
|
|
449
|
+
nodeId: string;
|
|
450
|
+
}[];
|
|
451
|
+
} | undefined>]>, undefined>;
|
|
452
|
+
/**
|
|
453
|
+
* The states the document's layers can be in, as Core keeps them on the document node. Pencil
|
|
454
|
+
* has no field for it, so it is an extension other Pencil clients ignore; without it a reopened
|
|
455
|
+
* document falls back to Figma's own two statuses.
|
|
456
|
+
*/
|
|
457
|
+
readonly statuses: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, {
|
|
458
|
+
preset?: string | undefined;
|
|
459
|
+
states: {
|
|
460
|
+
id: string;
|
|
461
|
+
name: string;
|
|
462
|
+
color: {
|
|
463
|
+
r: number;
|
|
464
|
+
g: number;
|
|
465
|
+
b: number;
|
|
466
|
+
a: number;
|
|
467
|
+
};
|
|
468
|
+
}[];
|
|
469
|
+
} | undefined>]>, undefined>;
|
|
470
|
+
/**
|
|
471
|
+
* The libraries the document switched on, as Core keeps them on the document node: the revision
|
|
472
|
+
* each library's components were inserted from. Pencil has no field for it, so it is an
|
|
473
|
+
* extension other Pencil clients ignore; without it a reopened document forgets the bindings.
|
|
474
|
+
*/
|
|
475
|
+
readonly enabledLibraries: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, EnabledLibraryBinding[] | undefined>]>, undefined>;
|
|
476
|
+
/**
|
|
477
|
+
* The library this document publishes as, as Core keeps it on the document node. Pencil has no
|
|
478
|
+
* field for it, so it is an extension other Pencil clients ignore; without it the next publish
|
|
479
|
+
* starts a new library rather than a revision of this one.
|
|
480
|
+
*/
|
|
481
|
+
readonly sourceLibraryPublication: v.OptionalSchema<v.SchemaWithPipe<readonly [v.UnknownSchema, v.TransformAction<unknown, SourceLibraryPublication | undefined>]>, undefined>;
|
|
370
482
|
readonly children: v.ArraySchema<v.GenericSchema<unknown, PenNode>, undefined>;
|
|
371
483
|
}, undefined>;
|
|
372
484
|
type PenDocument = v.InferOutput<typeof penDocumentSchema>;
|
|
@@ -493,6 +605,10 @@ interface PenReadOptions {
|
|
|
493
605
|
* component no import has reads as a placeholder.
|
|
494
606
|
*/
|
|
495
607
|
imports?: PenImports;
|
|
608
|
+
/** Called with a variable a layer references that the document does not define. */
|
|
609
|
+
onUnknownVariable?(name: string): void;
|
|
610
|
+
/** Called with a node type the reader does not know, which reads as a placeholder frame. */
|
|
611
|
+
onUnknownNodeType?(type: string): void;
|
|
496
612
|
}
|
|
497
613
|
/** A `.pen` document's JSON, validated; throws for text that is not one. */
|
|
498
614
|
declare function parsePenDocument(json: string): PenDocument;
|
|
@@ -502,9 +618,14 @@ declare function parsePenFile(json: string, options?: PenReadOptions): SceneGrap
|
|
|
502
618
|
declare function readPenFile(file: File, options?: PenReadOptions): Promise<SceneGraph>;
|
|
503
619
|
//#endregion
|
|
504
620
|
//#region src/write.d.ts
|
|
505
|
-
|
|
621
|
+
/** What a caller learns about content a written `.pen` file cannot hold. */
|
|
622
|
+
interface PenWriteOptions {
|
|
623
|
+
/** Collects one sentence per kind of content left out, in place of silently losing it. */
|
|
624
|
+
warnings?: string[];
|
|
625
|
+
}
|
|
626
|
+
declare function penDocumentFromGraph(graph: SceneGraph, options?: PenWriteOptions): PenDocument;
|
|
506
627
|
declare function stringifyPenDocument(document: PenDocument): string;
|
|
507
|
-
declare function writePenDocument(source: PenDocument | SceneGraph): string;
|
|
628
|
+
declare function writePenDocument(source: PenDocument | SceneGraph, options?: PenWriteOptions): string;
|
|
508
629
|
//#endregion
|
|
509
|
-
export { PEN_PLUGIN_DATA_NAMESPACE, type PenAssets, type PenDocument, type PenFont, type PenIconRequest, type PenIconSource, type PenImport, type PenImports, type PenNode, type PenOverride, type PenReadOptions, buildPenGraph, iconName, iconOptions, importedDocument, isDataURL, isDocumentAssetPath, parseImportedImageURL, parsePenDocument, parsePenFile, penAssets, penDocumentFromGraph, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|
|
630
|
+
export { PEN_PLUGIN_DATA_NAMESPACE, type PenAssets, type PenDocument, type PenFont, type PenIconRequest, type PenIconSource, type PenImport, type PenImports, type PenNode, type PenOverride, type PenReadOptions, type PenWriteOptions, buildPenGraph, iconName, iconOptions, importedDocument, isDataURL, isDocumentAssetPath, parseImportedImageURL, parsePenDocument, parsePenFile, penAssets, penDocumentFromGraph, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|
|
510
631
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/schema.ts","../src/assets.ts","../src/imports.ts","../src/nodes.ts","../src/library.ts","../src/plugin-data.ts","../src/read.ts","../src/write.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/schema.ts","../src/assets.ts","../src/imports.ts","../src/nodes.ts","../src/library.ts","../src/plugin-data.ts","../src/read.ts","../src/write.ts"],"mappings":";;;;;;cA6KM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA4FM,aAAa,EAAE,YAAY,EAAE,oBAAoB;;UAG5C,oBAAoB;EACnC;EACA;EACA,WAAW;EACX,cAAc,eAAe;GAC5B;;UAGc,gBAAgB;EAC/B;EACA;;;cA8CI,YAAU,EAAA;;;;cAwDV,mBAAiB,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2CX,cAAc,EAAE,mBAAmB;KACnC,UAAU,EAAE,mBAAmB;;;;iBC7Y3B,SAAS,iBAAiB;;iBAM1B,YACd,iBACA,6BACC;;UAKc;EACf;EACA,UAAU;;;;;;iBAOI,oBAAoB;;iBAMpB,UAAU;;;;;iBAWV,sBAAsB;EAAgB;EAAe;;;UASpD;;EAEf,OAAO;;;;;EAKP;;;;;EAKA;;;;;;iBAsHc,UAAU,KAAK,cAAc;;;;UCrM5B;;EAEf;;EAEA,KAAK;;;KAIK,aAAa,oBAAoB;;;;;;;iBAqH7B,iBACd,eACA,cACA,KAAK,aACL,SAAS,aACR;;;;UCtGc;;;;;;EAMf,KACE,OAAO,YACP,iBACA,cACA,cACA,OAAO,OACP,UAAU;;;;;;;;;iBC8BE,gBAAgB,KAAK,aAAa,SAAS,aAAa;;;;;;;;cCpE3D;;;;UCUI;;;;;EAKf,QAAQ,OAAO;;EAEf,SAAS,oBAAoB;;;;;EAK7B,UAAU;;EAEV,QAAQ;;;;;;EAMR,QAAQ,oBAAoB;;;;;;;EAO5B,UAAU;;EAEV,mBAAmB;;EAEnB,mBAAmB;;;iBAgDL,iBAAiB,eAAe;;iBAShC,cAAc,KAAK,aAAa,UAAS,iBAAsB;iBA8D/D,aAAa,cAAc,UAAS,iBAAsB;iBAIpD,YAAY,MAAM,MAAM,UAAU,iBAAiB,QAAQ;;;;UCvFhE;;EAEf;;iBA4sBc,qBACd,OAAO,YACP,UAAS,kBACR;iBA6Ha,qBAAqB,UAAU;iBAI/B,iBACd,QAAQ,cAAc,YACtB,UAAS"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/pen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -11,10 +11,11 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"es-toolkit": "^1.52.0",
|
|
14
|
+
"js-base64": "^3.9.3",
|
|
14
15
|
"valibot": "^1.5.0"
|
|
15
16
|
},
|
|
16
17
|
"peerDependencies": {
|
|
17
|
-
"@pairdraw/scene-graph": "^0.
|
|
18
|
+
"@pairdraw/scene-graph": "^0.21.0"
|
|
18
19
|
},
|
|
19
20
|
"exports": {
|
|
20
21
|
".": {
|
package/src/annotations.ts
CHANGED
|
@@ -30,6 +30,9 @@ const ANNOTATION_COLORS: Readonly<Record<string, Color>> = {
|
|
|
30
30
|
context: STICKY_COLORS.gray
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
/** The annotation type names, which the reader knows even though they map to stickies. */
|
|
34
|
+
export const ANNOTATION_TYPES: readonly string[] = Object.keys(ANNOTATION_COLORS)
|
|
35
|
+
|
|
33
36
|
export function isAnnotation(pen: Pick<PenNode, 'type'>): boolean {
|
|
34
37
|
return Object.hasOwn(ANNOTATION_COLORS, pen.type)
|
|
35
38
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { SceneGraph, SourceLibraryPublication } from '@pairdraw/scene-graph'
|
|
2
|
+
import {
|
|
3
|
+
PAIRDRAW_PLUGIN_DATA,
|
|
4
|
+
readPluginData,
|
|
5
|
+
withPluginData
|
|
6
|
+
} from '@pairdraw/scene-graph/plugin-data'
|
|
7
|
+
|
|
8
|
+
import type { PenDocument } from './schema'
|
|
9
|
+
|
|
10
|
+
const { penImports: penImportsField, sourceLibraryPublication: publicationField } =
|
|
11
|
+
PAIRDRAW_PLUGIN_DATA
|
|
12
|
+
|
|
13
|
+
type LibraryLinks = Pick<PenDocument, 'imports' | 'enabledLibraries' | 'sourceLibraryPublication'>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The `.pen` documents a graph imports, by the alias its file names each one with. The reader
|
|
17
|
+
* stores them beside the design memory, so a save writes the same `imports` back.
|
|
18
|
+
*/
|
|
19
|
+
export function readPenImports(graph: SceneGraph): Record<string, string> | undefined {
|
|
20
|
+
const root = graph.getNode(graph.rootId)
|
|
21
|
+
const imports = readPluginData(root?.pluginData, penImportsField)
|
|
22
|
+
return imports && Object.keys(imports).length > 0 ? imports : undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The library this document publishes as, or null when it publishes none. */
|
|
26
|
+
export function readSourceLibraryPublication(graph: SceneGraph): SourceLibraryPublication | null {
|
|
27
|
+
const root = graph.getNode(graph.rootId)
|
|
28
|
+
return readPluginData(root?.pluginData, publicationField) ?? null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Keeps the library links a `.pen` file carries on the graph it reads into: the documents it
|
|
33
|
+
* imports (their paths, which resolve their components and variables on a reopen), the libraries
|
|
34
|
+
* it switched on, and the library it publishes as.
|
|
35
|
+
*/
|
|
36
|
+
export function writeLibraryLinks(graph: SceneGraph, doc: LibraryLinks): void {
|
|
37
|
+
const root = graph.getNode(graph.rootId)
|
|
38
|
+
if (!root) return
|
|
39
|
+
let pluginData = root.pluginData
|
|
40
|
+
if (doc.imports && Object.keys(doc.imports).length > 0) {
|
|
41
|
+
pluginData = withPluginData(pluginData, penImportsField, doc.imports)
|
|
42
|
+
}
|
|
43
|
+
if (doc.sourceLibraryPublication) {
|
|
44
|
+
pluginData = withPluginData(pluginData, publicationField, doc.sourceLibraryPublication)
|
|
45
|
+
}
|
|
46
|
+
if (pluginData !== root.pluginData) graph.updateNode(root.id, { pluginData })
|
|
47
|
+
if (doc.enabledLibraries) {
|
|
48
|
+
graph.enabledLibraries = new Map(
|
|
49
|
+
doc.enabledLibraries.map((binding) => [binding.libraryId, binding])
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/icons.ts
CHANGED
|
@@ -16,6 +16,7 @@ import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
|
16
16
|
|
|
17
17
|
import { iconName, iconOptions } from './assets'
|
|
18
18
|
import type { Build } from './nodes'
|
|
19
|
+
import { writeIconOrigin } from './plugin-data'
|
|
19
20
|
import type { PenNode } from './schema'
|
|
20
21
|
import { resolveOptionalNumber, resolveString } from './variables'
|
|
21
22
|
|
|
@@ -81,6 +82,11 @@ function iconComponent(build: Build, pen: PenNode, width: number, height: number
|
|
|
81
82
|
height,
|
|
82
83
|
fills: [{ type: 'SOLID', color: { ...BLACK }, opacity: 1, visible: true }]
|
|
83
84
|
})
|
|
85
|
+
writeIconOrigin(component, {
|
|
86
|
+
library: library || undefined,
|
|
87
|
+
icon,
|
|
88
|
+
weight: resolveOptionalNumber(pen.weight, ctx)
|
|
89
|
+
})
|
|
84
90
|
drawIcon(component, pen, build)
|
|
85
91
|
build.iconComponents.set(key, component.id)
|
|
86
92
|
return component.id
|
package/src/images.ts
CHANGED
|
@@ -54,6 +54,18 @@ function invertAffine([a, b, c, d, tx, ty]: readonly number[]): Matrix | null {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* A crop's image-space window as pen.dev's affine, which maps the crop back onto the image so a
|
|
59
|
+
* reader inverts it into the same window. The tuple is `[a, b, c, d, tx, ty]`: columns first.
|
|
60
|
+
*/
|
|
61
|
+
export function penCropTransform(
|
|
62
|
+
crop: Matrix
|
|
63
|
+
): [number, number, number, number, number, number] | null {
|
|
64
|
+
const inverse = invertAffine([crop.m00, crop.m10, crop.m01, crop.m11, crop.m02, crop.m12])
|
|
65
|
+
if (!inverse) return null
|
|
66
|
+
return [inverse.m00, inverse.m10, inverse.m01, inverse.m11, inverse.m02, inverse.m12]
|
|
67
|
+
}
|
|
68
|
+
|
|
57
69
|
function compose(outer: Matrix, inner: Matrix): Matrix {
|
|
58
70
|
return {
|
|
59
71
|
m00: outer.m00 * inner.m00 + outer.m01 * inner.m10,
|
package/src/index.ts
CHANGED
|
@@ -20,4 +20,9 @@ export {
|
|
|
20
20
|
type PenReadOptions
|
|
21
21
|
} from './read'
|
|
22
22
|
export type { PenDocument, PenFont, PenNode, PenOverride } from './schema'
|
|
23
|
-
export {
|
|
23
|
+
export {
|
|
24
|
+
penDocumentFromGraph,
|
|
25
|
+
stringifyPenDocument,
|
|
26
|
+
writePenDocument,
|
|
27
|
+
type PenWriteOptions
|
|
28
|
+
} from './write'
|
package/src/library.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
1
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
2
2
|
import { libraryDefinitionsPage } from '@pairdraw/scene-graph/components'
|
|
3
3
|
|
|
4
4
|
import { withImports, type PenImports } from './imports'
|
|
@@ -151,3 +151,26 @@ export function markLibraryRoots(graph: SceneGraph, roots: readonly LibraryRoot[
|
|
|
151
151
|
root.locked = true
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The components whose `ref`s a file can resolve: the library definitions its own imports give it,
|
|
157
|
+
* matched to the alias and path each import names. Writing their ids as refs keeps the instances
|
|
158
|
+
* linked on a reopen; the definitions themselves stay out of the file.
|
|
159
|
+
*/
|
|
160
|
+
export function importedComponentIds(
|
|
161
|
+
graph: SceneGraph,
|
|
162
|
+
imports: Readonly<Record<string, string>>
|
|
163
|
+
): Set<string> {
|
|
164
|
+
const ids = new Set<string>()
|
|
165
|
+
const aliases = Object.entries(imports)
|
|
166
|
+
if (aliases.length === 0) return ids
|
|
167
|
+
const paths = new Map(aliases.map(([alias, path]) => [penLibraryId(alias), path]))
|
|
168
|
+
const visit = (node: SceneNode): void => {
|
|
169
|
+
const identity = node.librarySource?.identity
|
|
170
|
+
if (identity && paths.get(identity.libraryId) === identity.revisionId) ids.add(node.id)
|
|
171
|
+
for (const child of graph.getChildren(node.id)) visit(child)
|
|
172
|
+
}
|
|
173
|
+
for (const page of graph.getPages(true))
|
|
174
|
+
for (const node of graph.getChildren(page.id)) visit(node)
|
|
175
|
+
return ids
|
|
176
|
+
}
|
package/src/nodes.ts
CHANGED
|
@@ -12,7 +12,7 @@ import type { FontFamilyStyle } from '@pairdraw/scene-graph/font-style'
|
|
|
12
12
|
import { fillSizingFields } from '@pairdraw/scene-graph/layout'
|
|
13
13
|
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
14
14
|
|
|
15
|
-
import { applyAnnotation, createAnnotation, isAnnotation } from './annotations'
|
|
15
|
+
import { ANNOTATION_TYPES, applyAnnotation, createAnnotation, isAnnotation } from './annotations'
|
|
16
16
|
import { convertEffects } from './effects'
|
|
17
17
|
import { ellipseArc } from './geometry'
|
|
18
18
|
import { drawIcon, insideComponent, useIconComponent } from './icons'
|
|
@@ -71,6 +71,8 @@ export interface Build {
|
|
|
71
71
|
placements: Map<string, PenPlacement>
|
|
72
72
|
/** Groups in reading order. */
|
|
73
73
|
groupIds: Set<string>
|
|
74
|
+
/** Called with a node type the reader does not know. */
|
|
75
|
+
onUnknownNodeType?: (type: string) => void
|
|
74
76
|
/** The components icons in components are instances of, by glyph and size. */
|
|
75
77
|
iconComponents: Map<string, string>
|
|
76
78
|
/** The page those components are on, added with the first one. */
|
|
@@ -98,7 +100,25 @@ function isFlatLine(pen: PenNode, ctx: VarContext): boolean {
|
|
|
98
100
|
return height.sizing === 'FIXED' && height.value === 0
|
|
99
101
|
}
|
|
100
102
|
|
|
101
|
-
|
|
103
|
+
const KNOWN_PEN_TYPES = new Set([
|
|
104
|
+
'frame',
|
|
105
|
+
'line',
|
|
106
|
+
'rectangle',
|
|
107
|
+
'ellipse',
|
|
108
|
+
'polygon',
|
|
109
|
+
'group',
|
|
110
|
+
'text',
|
|
111
|
+
'icon_font',
|
|
112
|
+
'path',
|
|
113
|
+
'icon',
|
|
114
|
+
'sticky',
|
|
115
|
+
'ref',
|
|
116
|
+
// The annotation cards read as stickies; they are known types, not placeholders.
|
|
117
|
+
...ANNOTATION_TYPES
|
|
118
|
+
])
|
|
119
|
+
|
|
120
|
+
function sceneType(pen: PenNode, ctx: VarContext, warn?: (type: string) => void): NodeType {
|
|
121
|
+
if (!KNOWN_PEN_TYPES.has(pen.type)) warn?.(pen.type)
|
|
102
122
|
if (pen.type === 'frame') return pen.reusable ? 'COMPONENT' : 'FRAME'
|
|
103
123
|
if (pen.type === 'line') return isFlatLine(pen, ctx) ? 'LINE' : 'VECTOR'
|
|
104
124
|
if (isAnnotation(pen)) return 'STICKY'
|
|
@@ -206,6 +226,7 @@ const PROPERTY_FIELDS = {
|
|
|
206
226
|
justifyContent: ['primaryAxisAlign'],
|
|
207
227
|
alignItems: ['counterAxisAlign'],
|
|
208
228
|
clip: ['clipsContent'],
|
|
229
|
+
status: ['status'],
|
|
209
230
|
placeholder: ['pluginData'],
|
|
210
231
|
slot: ['pluginData'],
|
|
211
232
|
innerRadius: ['arcData'],
|
|
@@ -304,6 +325,7 @@ export function applyPenProperties(node: SceneNode, pen: PenNode, build: Build):
|
|
|
304
325
|
visible: resolveBoolean(pen.enabled, ctx),
|
|
305
326
|
opacity: resolveNumber(pen.opacity, ctx, 1),
|
|
306
327
|
clipsContent: resolveBoolean(pen.clip, ctx, false),
|
|
328
|
+
status: pen.status ?? null,
|
|
307
329
|
layoutPositioning: absolute ? 'ABSOLUTE' : 'AUTO',
|
|
308
330
|
layoutGrow: 0,
|
|
309
331
|
layoutAlignSelf: 'AUTO',
|
|
@@ -400,7 +422,10 @@ export function createSceneNode(pen: PenNode, parentId: string, build: Build): s
|
|
|
400
422
|
const iconInstance = fields.type === 'icon' && insideComponent(build.graph, parentId)
|
|
401
423
|
const leaveTheme = fields.theme ? build.ctx.enterTheme(fields.theme) : undefined
|
|
402
424
|
try {
|
|
403
|
-
const type =
|
|
425
|
+
const type =
|
|
426
|
+
instanceOf || iconInstance
|
|
427
|
+
? 'INSTANCE'
|
|
428
|
+
: sceneType(fields, build.ctx, build.onUnknownNodeType)
|
|
404
429
|
// An ID a hand-edited file repeats gets a fresh one, so the layer does not replace the first
|
|
405
430
|
// layer with it or become its own parent.
|
|
406
431
|
const id = build.graph.getNode(pen.id) ? build.graph.entityIdMinter()() : pen.id
|
package/src/plugin-data.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
1
3
|
import type { PluginDataEntry, SceneNode } from '@pairdraw/scene-graph'
|
|
2
4
|
|
|
3
5
|
import type { PenNode, PenOverride } from './schema'
|
|
@@ -51,6 +53,44 @@ export function unresolvedRef(pen: PenOverride): string | undefined {
|
|
|
51
53
|
return typeof ref === 'string' ? ref : undefined
|
|
52
54
|
}
|
|
53
55
|
|
|
56
|
+
/**
|
|
57
|
+
* The pen fields the component for an icon was drawn from, kept on it: a layer inside a component
|
|
58
|
+
* that swaps its glyph names the icon in the override, not the component, so a save needs them
|
|
59
|
+
* back to write the swap. Other pen clients ignore the entry.
|
|
60
|
+
*/
|
|
61
|
+
const ICON_ORIGIN_KEY = 'pen/iconOrigin'
|
|
62
|
+
|
|
63
|
+
const iconOriginSchema = v.object({
|
|
64
|
+
library: v.optional(v.pipe(v.string(), v.nonEmpty())),
|
|
65
|
+
icon: v.pipe(v.string(), v.nonEmpty()),
|
|
66
|
+
weight: v.optional(v.number())
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
/** The pen fields an icon component was drawn from. */
|
|
70
|
+
export type PenIconOrigin = v.InferOutput<typeof iconOriginSchema>
|
|
71
|
+
|
|
72
|
+
/** Keeps the fields an icon component was drawn from on it. */
|
|
73
|
+
export function writeIconOrigin(node: SceneNode, origin: PenIconOrigin): void {
|
|
74
|
+
const key = `${PEN_PLUGIN_DATA_NAMESPACE}/${ICON_ORIGIN_KEY}`
|
|
75
|
+
const originEntry: PluginDataEntry = {
|
|
76
|
+
pluginId: PEN_PLUGIN_DATA_NAMESPACE,
|
|
77
|
+
key,
|
|
78
|
+
value: JSON.stringify(origin)
|
|
79
|
+
}
|
|
80
|
+
node.pluginData = [...node.pluginData.filter((item) => item.key !== key), originEntry]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The fields the component `node` draws as an icon is, or undefined for any other component. */
|
|
84
|
+
export function readIconOrigin(node: SceneNode | undefined): PenIconOrigin | undefined {
|
|
85
|
+
const key = `${PEN_PLUGIN_DATA_NAMESPACE}/${ICON_ORIGIN_KEY}`
|
|
86
|
+
const stored = node?.pluginData.find(
|
|
87
|
+
(item) => item.pluginId === PEN_PLUGIN_DATA_NAMESPACE && item.key === key
|
|
88
|
+
)
|
|
89
|
+
if (!stored) return undefined
|
|
90
|
+
const parsed = v.safeParse(v.pipe(v.string(), v.parseJson(), iconOriginSchema), stored.value)
|
|
91
|
+
return parsed.success ? parsed.output : undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
54
94
|
/** As `setSharedPluginData` stores an entry, so the Figma API reads and replaces it. */
|
|
55
95
|
function entry(key: PenPluginDataKey, value: unknown): PluginDataEntry {
|
|
56
96
|
return {
|
package/src/read.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
3
|
import { SceneGraph } from '@pairdraw/scene-graph'
|
|
4
|
+
import { writeDesignMemory } from '@pairdraw/scene-graph/design-memory'
|
|
4
5
|
import type { FontFamilyStyle } from '@pairdraw/scene-graph/font-style'
|
|
6
|
+
import { writeStatusWorkflow } from '@pairdraw/scene-graph/statuses'
|
|
7
|
+
import { rekeyTokenSources, tokenSourcesPluginData } from '@pairdraw/scene-graph/token-sources'
|
|
5
8
|
|
|
9
|
+
import { writeLibraryLinks } from './document-libraries'
|
|
6
10
|
import { lineNetwork, pathNetwork } from './geometry'
|
|
7
11
|
import { removeUnusedIconComponents } from './icons'
|
|
8
12
|
import { createImageStore } from './images'
|
|
@@ -43,6 +47,10 @@ export interface PenReadOptions {
|
|
|
43
47
|
* component no import has reads as a placeholder.
|
|
44
48
|
*/
|
|
45
49
|
imports?: PenImports
|
|
50
|
+
/** Called with a variable a layer references that the document does not define. */
|
|
51
|
+
onUnknownVariable?(name: string): void
|
|
52
|
+
/** Called with a node type the reader does not know, which reads as a placeholder frame. */
|
|
53
|
+
onUnknownNodeType?(type: string): void
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
function collectComponents(nodes: PenNode[], components: Map<string, PenNode>): void {
|
|
@@ -109,7 +117,9 @@ export function buildPenGraph(doc: PenDocument, options: PenReadOptions = {}): S
|
|
|
109
117
|
const imports: PenImports = options.imports ?? new Map()
|
|
110
118
|
const build: Build = {
|
|
111
119
|
graph,
|
|
112
|
-
ctx: buildVarContext(graph, withImports(doc, imports))
|
|
120
|
+
ctx: buildVarContext(graph, withImports(doc, imports), (name) =>
|
|
121
|
+
options.onUnknownVariable?.(name)
|
|
122
|
+
),
|
|
113
123
|
images: createImageStore(graph, options.images),
|
|
114
124
|
shaders: options.shaders,
|
|
115
125
|
icons: options.icons,
|
|
@@ -121,7 +131,8 @@ export function buildPenGraph(doc: PenDocument, options: PenReadOptions = {}): S
|
|
|
121
131
|
groupIds: new Set(),
|
|
122
132
|
iconComponents: new Map(),
|
|
123
133
|
iconPageId: null,
|
|
124
|
-
replaced: new Set()
|
|
134
|
+
replaced: new Set(),
|
|
135
|
+
onUnknownNodeType: (type) => options.onUnknownNodeType?.(type)
|
|
125
136
|
}
|
|
126
137
|
collectComponents(doc.children, build.components)
|
|
127
138
|
for (const imported of imports.values())
|
|
@@ -145,6 +156,15 @@ export function buildPenGraph(doc: PenDocument, options: PenReadOptions = {}): S
|
|
|
145
156
|
if (graph.getPages(true).length === 0) {
|
|
146
157
|
graph.addPage('Page 1')
|
|
147
158
|
}
|
|
159
|
+
if (doc.tokenSources && doc.tokenSources.length > 0) {
|
|
160
|
+
graph.updateNode(graph.rootId, {
|
|
161
|
+
pluginData: tokenSourcesPluginData(graph, rekeyTokenSources(graph, doc.tokenSources))
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
// After the links, which replace the document node's plugin data: the memory is stored beside them.
|
|
165
|
+
if (doc.designMemory) writeDesignMemory(graph, doc.designMemory)
|
|
166
|
+
if (doc.statuses) writeStatusWorkflow(graph, doc.statuses)
|
|
167
|
+
writeLibraryLinks(graph, doc)
|
|
148
168
|
|
|
149
169
|
return graph
|
|
150
170
|
}
|