@pairdraw/pen 0.20.0 → 0.21.1
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 +31 -1
- 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 +96 -1
- 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 +278 -24
- package/dist/chunks/index.js.map +1 -1
- package/dist/index.d.ts +118 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/package.json +3 -2
- package/src/document-libraries.ts +52 -0
- package/src/icons.ts +6 -0
- package/src/images.ts +12 -0
- package/src/index.ts +8 -1
- package/src/library.ts +24 -1
- package/src/nodes.ts +2 -0
- package/src/plugin-data.ts +40 -0
- package/src/read.ts +7 -0
- package/src/schema.ts +100 -0
- package/src/write.ts +329 -23
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.
|
|
@@ -403,6 +408,77 @@ declare const penDocumentSchema: v.LooseObjectSchema<{
|
|
|
403
408
|
};
|
|
404
409
|
} | undefined;
|
|
405
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>;
|
|
406
482
|
readonly children: v.ArraySchema<v.GenericSchema<unknown, PenNode>, undefined>;
|
|
407
483
|
}, undefined>;
|
|
408
484
|
type PenDocument = v.InferOutput<typeof penDocumentSchema>;
|
|
@@ -542,9 +618,47 @@ declare function parsePenFile(json: string, options?: PenReadOptions): SceneGrap
|
|
|
542
618
|
declare function readPenFile(file: File, options?: PenReadOptions): Promise<SceneGraph>;
|
|
543
619
|
//#endregion
|
|
544
620
|
//#region src/write.d.ts
|
|
545
|
-
|
|
621
|
+
/**
|
|
622
|
+
* What a written `.pen` file could not hold, as facts rather than a sentence: the app shows these
|
|
623
|
+
* in the reader's language, while the CLI and MCP print `penWriteWarningText` of each.
|
|
624
|
+
*/
|
|
625
|
+
type PenWriteWarning = {
|
|
626
|
+
kind: 'merged-pages';
|
|
627
|
+
pages: number;
|
|
628
|
+
name: string;
|
|
629
|
+
} | {
|
|
630
|
+
kind: 'display-p3-colors';
|
|
631
|
+
} | {
|
|
632
|
+
kind: 'library-definitions';
|
|
633
|
+
count: number;
|
|
634
|
+
} | {
|
|
635
|
+
kind: 'missing-image-fills';
|
|
636
|
+
count: number;
|
|
637
|
+
} | {
|
|
638
|
+
kind: 'dropped-effects';
|
|
639
|
+
count: number;
|
|
640
|
+
} | {
|
|
641
|
+
kind: 'dropped-filters';
|
|
642
|
+
count: number;
|
|
643
|
+
} | {
|
|
644
|
+
kind: 'dropped-scroll';
|
|
645
|
+
count: number;
|
|
646
|
+
} | {
|
|
647
|
+
kind: 'dropped-font-features';
|
|
648
|
+
count: number;
|
|
649
|
+
};
|
|
650
|
+
/** The English sentence a write warning has always been reported as. */
|
|
651
|
+
declare function penWriteWarningText(warning: PenWriteWarning): string;
|
|
652
|
+
/** What a caller learns about content a written `.pen` file cannot hold. */
|
|
653
|
+
interface PenWriteOptions {
|
|
654
|
+
/** Collects one sentence per kind of content left out, in place of silently losing it. */
|
|
655
|
+
warnings?: string[];
|
|
656
|
+
/** Collects the same facts as kinds, for a host that shows them in its own language. */
|
|
657
|
+
warningDetails?: PenWriteWarning[];
|
|
658
|
+
}
|
|
659
|
+
declare function penDocumentFromGraph(graph: SceneGraph, options?: PenWriteOptions): PenDocument;
|
|
546
660
|
declare function stringifyPenDocument(document: PenDocument): string;
|
|
547
|
-
declare function writePenDocument(source: PenDocument | SceneGraph): string;
|
|
661
|
+
declare function writePenDocument(source: PenDocument | SceneGraph, options?: PenWriteOptions): string;
|
|
548
662
|
//#endregion
|
|
549
|
-
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 };
|
|
663
|
+
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, type PenWriteWarning, buildPenGraph, iconName, iconOptions, importedDocument, isDataURL, isDocumentAssetPath, parseImportedImageURL, parsePenDocument, parsePenFile, penAssets, penDocumentFromGraph, penWriteWarningText, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|
|
550
664
|
//# 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;;;;;;;KClFrE;EACN;EAAsB;EAAe;;EACrC;;EACA;EAA6B;;EAC7B;EAA6B;;EAC7B;EAAyB;;EACzB;EAAyB;;EACzB;EAAwB;;EACxB;EAA+B;;;iBAGrB,oBAAoB,SAAS;;UAgC5B;;EAEf;;EAEA,iBAAiB;;iBA4sBH,qBACd,OAAO,YACP,UAAS,kBACR;iBAyGa,qBAAqB,UAAU;iBAI/B,iBACd,QAAQ,cAAc,YACtB,UAAS"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PEN_PLUGIN_DATA_NAMESPACE } from "./chunks/annotations.js";
|
|
2
2
|
import { iconName, iconOptions, isDataURL, isDocumentAssetPath, parseImportedImageURL, penAssets } from "./chunks/assets.js";
|
|
3
3
|
import { importedDocument } from "./chunks/imports.js";
|
|
4
|
-
import { buildPenGraph, parsePenDocument, parsePenFile, penDocumentFromGraph, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument } from "./chunks/index.js";
|
|
5
|
-
export { PEN_PLUGIN_DATA_NAMESPACE, buildPenGraph, iconName, iconOptions, importedDocument, isDataURL, isDocumentAssetPath, parseImportedImageURL, parsePenDocument, parsePenFile, penAssets, penDocumentFromGraph, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|
|
4
|
+
import { buildPenGraph, parsePenDocument, parsePenFile, penDocumentFromGraph, penWriteWarningText, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument } from "./chunks/index.js";
|
|
5
|
+
export { PEN_PLUGIN_DATA_NAMESPACE, buildPenGraph, iconName, iconOptions, importedDocument, isDataURL, isDocumentAssetPath, parseImportedImageURL, parsePenDocument, parsePenFile, penAssets, penDocumentFromGraph, penWriteWarningText, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/pen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
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.1"
|
|
18
19
|
},
|
|
19
20
|
"exports": {
|
|
20
21
|
".": {
|
|
@@ -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,11 @@ 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
|
+
penWriteWarningText,
|
|
26
|
+
stringifyPenDocument,
|
|
27
|
+
writePenDocument,
|
|
28
|
+
type PenWriteOptions,
|
|
29
|
+
type PenWriteWarning
|
|
30
|
+
} 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
|
@@ -226,6 +226,7 @@ const PROPERTY_FIELDS = {
|
|
|
226
226
|
justifyContent: ['primaryAxisAlign'],
|
|
227
227
|
alignItems: ['counterAxisAlign'],
|
|
228
228
|
clip: ['clipsContent'],
|
|
229
|
+
status: ['status'],
|
|
229
230
|
placeholder: ['pluginData'],
|
|
230
231
|
slot: ['pluginData'],
|
|
231
232
|
innerRadius: ['arcData'],
|
|
@@ -324,6 +325,7 @@ export function applyPenProperties(node: SceneNode, pen: PenNode, build: Build):
|
|
|
324
325
|
visible: resolveBoolean(pen.enabled, ctx),
|
|
325
326
|
opacity: resolveNumber(pen.opacity, ctx, 1),
|
|
326
327
|
clipsContent: resolveBoolean(pen.clip, ctx, false),
|
|
328
|
+
status: pen.status ?? null,
|
|
327
329
|
layoutPositioning: absolute ? 'ABSOLUTE' : 'AUTO',
|
|
328
330
|
layoutGrow: 0,
|
|
329
331
|
layoutAlignSelf: 'AUTO',
|
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,9 +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'
|
|
5
7
|
import { rekeyTokenSources, tokenSourcesPluginData } from '@pairdraw/scene-graph/token-sources'
|
|
6
8
|
|
|
9
|
+
import { writeLibraryLinks } from './document-libraries'
|
|
7
10
|
import { lineNetwork, pathNetwork } from './geometry'
|
|
8
11
|
import { removeUnusedIconComponents } from './icons'
|
|
9
12
|
import { createImageStore } from './images'
|
|
@@ -158,6 +161,10 @@ export function buildPenGraph(doc: PenDocument, options: PenReadOptions = {}): S
|
|
|
158
161
|
pluginData: tokenSourcesPluginData(graph, rekeyTokenSources(graph, doc.tokenSources))
|
|
159
162
|
})
|
|
160
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)
|
|
161
168
|
|
|
162
169
|
return graph
|
|
163
170
|
}
|
package/src/schema.ts
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
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'
|
|
4
10
|
import { finiteNumberSchema } from '@pairdraw/scene-graph/primitives'
|
|
11
|
+
import {
|
|
12
|
+
nodeStatusSchema,
|
|
13
|
+
statusWorkflowSchema,
|
|
14
|
+
type StatusWorkflow
|
|
15
|
+
} from '@pairdraw/scene-graph/statuses'
|
|
5
16
|
import { tokenSourcesSchema } from '@pairdraw/scene-graph/token-sources'
|
|
6
17
|
|
|
7
18
|
import { stripJSONComments } from './jsonc'
|
|
@@ -147,10 +158,27 @@ const paddingSchema = v.union([
|
|
|
147
158
|
])
|
|
148
159
|
const cornerRadiusSchema = v.union([numSchema, v.array(numSchema)])
|
|
149
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
|
+
|
|
150
173
|
/** Every property a node, or an override of one, can carry. */
|
|
151
174
|
const nodeFields = {
|
|
152
175
|
name: v.optional(strSchema),
|
|
153
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),
|
|
154
182
|
/**
|
|
155
183
|
* What a layer is, for agents: pen.dev writes a `type` and any fields of its own. It is only kept
|
|
156
184
|
* as plugin data, so any value is read as it is rather than rejecting the document.
|
|
@@ -302,6 +330,54 @@ const penTokenSourcesSchema = v.pipe(
|
|
|
302
330
|
v.transform((value) => v.parse(tokenSourcesSchema, Array.isArray(value) ? value : []))
|
|
303
331
|
)
|
|
304
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
|
+
|
|
305
381
|
const penDocumentSchema = v.looseObject({
|
|
306
382
|
version: v.optional(strSchema),
|
|
307
383
|
// The page's name. The format is single-page, so it belongs to the document.
|
|
@@ -318,6 +394,30 @@ const penDocumentSchema = v.looseObject({
|
|
|
318
394
|
variables: v.optional(v.record(strSchema, variableSchema)),
|
|
319
395
|
/** The code the document's variables are linked to, as Core keeps it on the document node. */
|
|
320
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),
|
|
321
421
|
children: v.array(nodeSchema)
|
|
322
422
|
})
|
|
323
423
|
|