@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/chunks/index.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import { UNRESOLVED_REF, applyAnnotation, applyCornerRadius, applyPadding, applyPenPluginData, bindIfVar, buildVarContext, createAnnotation, isAnnotation, isVarRef, mapAlignItems, mapJustifyContent, mapLayoutMode, parseSize, resolveBoolean, resolveNumber, resolveString, unresolvedRef } from "./annotations.js";
|
|
1
|
+
import { ANNOTATION_TYPES, UNRESOLVED_REF, applyAnnotation, applyCornerRadius, applyPadding, applyPenPluginData, bindIfVar, buildVarContext, createAnnotation, isAnnotation, isVarRef, mapAlignItems, mapJustifyContent, mapLayoutMode, parseSize, readIconOrigin, resolveBoolean, resolveNumber, resolveString, unresolvedRef } from "./annotations.js";
|
|
2
2
|
import { withImports } from "./imports.js";
|
|
3
|
+
import { readPenImports, readSourceLibraryPublication, writeLibraryLinks } from "./document-libraries.js";
|
|
3
4
|
import { ellipseArc, lineNetwork, networkToSVGPath, pathNetwork } from "./geometry.js";
|
|
4
5
|
import { drawIcon, insideComponent, removeUnusedIconComponents, useIconComponent } from "./icons.js";
|
|
5
|
-
import { applyStroke, convertEffects, convertPaints, createImageStore, penFileSchema, strokeFields } from "./effects.js";
|
|
6
|
+
import { applyStroke, convertEffects, convertPaints, createImageStore, penCropTransform, penFileSchema, strokeFields } from "./effects.js";
|
|
6
7
|
import { SceneGraph, generateId } from "@pairdraw/scene-graph";
|
|
7
8
|
import { colorToHex, colorToHex8 } from "@pairdraw/scene-graph/color";
|
|
8
9
|
import { pick } from "es-toolkit/object";
|
|
9
10
|
import { isEqual } from "es-toolkit/predicate";
|
|
10
11
|
import { createComponentPropertyId, libraryDefinitionsPage } from "@pairdraw/scene-graph/components";
|
|
11
12
|
import * as v from "valibot";
|
|
13
|
+
import { readDesignMemory, writeDesignMemory } from "@pairdraw/scene-graph/design-memory";
|
|
14
|
+
import { readStoredStatusWorkflow, writeStatusWorkflow } from "@pairdraw/scene-graph/statuses";
|
|
15
|
+
import { readTokenSources, rekeyTokenSources, tokenSourcesPluginData } from "@pairdraw/scene-graph/token-sources";
|
|
12
16
|
import { INSTANCE_OVERRIDE_FIELDS, detachInstance, populateInstanceChildren, recordInstanceOverride, swapInstanceComponent } from "@pairdraw/scene-graph/instances";
|
|
17
|
+
import { imageMimeType } from "@pairdraw/scene-graph/images";
|
|
13
18
|
import { normalizeEffects } from "@pairdraw/scene-graph/effects";
|
|
14
19
|
import { CORNER_FIELDS } from "@pairdraw/scene-graph/fields";
|
|
15
20
|
import { fillSizingFields, layoutSizing } from "@pairdraw/scene-graph/layout";
|
|
@@ -19,8 +24,10 @@ import { getNodeLocalMatrix, localTransformFromWorld } from "@pairdraw/scene-gra
|
|
|
19
24
|
import { fitEnclosingGroups } from "@pairdraw/scene-graph/group-bounds";
|
|
20
25
|
import Matrix from "@pairdraw/scene-graph/matrix";
|
|
21
26
|
import { canCreateSlot, claimSlotContent, createSlotProperty, slotOwner, slotScope, updateSlotProperty } from "@pairdraw/scene-graph/slots";
|
|
27
|
+
import { isEqual as isEqual$1, omit } from "es-toolkit";
|
|
22
28
|
import { parseFontStyle } from "@pairdraw/scene-graph/font-style";
|
|
23
29
|
import { percentLength, pixelLength } from "@pairdraw/scene-graph/text";
|
|
30
|
+
import { fromUint8Array } from "js-base64";
|
|
24
31
|
//#region src/library.ts
|
|
25
32
|
/** The component IDs a layer and its overrides name: its `ref`s and the slots' suggestions. */
|
|
26
33
|
function namedComponents(node, found) {
|
|
@@ -147,6 +154,24 @@ function markLibraryRoots(graph, roots) {
|
|
|
147
154
|
root.locked = true;
|
|
148
155
|
}
|
|
149
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* The components whose `ref`s a file can resolve: the library definitions its own imports give it,
|
|
159
|
+
* matched to the alias and path each import names. Writing their ids as refs keeps the instances
|
|
160
|
+
* linked on a reopen; the definitions themselves stay out of the file.
|
|
161
|
+
*/
|
|
162
|
+
function importedComponentIds(graph, imports) {
|
|
163
|
+
const ids = /* @__PURE__ */ new Set();
|
|
164
|
+
const aliases = Object.entries(imports);
|
|
165
|
+
if (aliases.length === 0) return ids;
|
|
166
|
+
const paths = new Map(aliases.map(([alias, path]) => [penLibraryId(alias), path]));
|
|
167
|
+
const visit = (node) => {
|
|
168
|
+
const identity = node.librarySource?.identity;
|
|
169
|
+
if (identity && paths.get(identity.libraryId) === identity.revisionId) ids.add(node.id);
|
|
170
|
+
for (const child of graph.getChildren(node.id)) visit(child);
|
|
171
|
+
};
|
|
172
|
+
for (const page of graph.getPages(true)) for (const node of graph.getChildren(page.id)) visit(node);
|
|
173
|
+
return ids;
|
|
174
|
+
}
|
|
150
175
|
//#endregion
|
|
151
176
|
//#region src/placeholders.ts
|
|
152
177
|
/**
|
|
@@ -385,7 +410,7 @@ function applyTextProps(node, pen, ctx, fonts) {
|
|
|
385
410
|
fontWeight: weight,
|
|
386
411
|
italic
|
|
387
412
|
});
|
|
388
|
-
|
|
413
|
+
node.boundVariables = omit(node.boundVariables, ["fontFamily"]);
|
|
389
414
|
}
|
|
390
415
|
node.textAlignHorizontal = TEXT_ALIGN[pen.textAlign ?? "left"] ?? "LEFT";
|
|
391
416
|
node.textAlignVertical = TEXT_ALIGN_VERTICAL[pen.textAlignVertical ?? "top"] ?? "TOP";
|
|
@@ -428,7 +453,23 @@ function isFlatLine(pen, ctx) {
|
|
|
428
453
|
const height = parseSize(pen.height, 100, ctx);
|
|
429
454
|
return height.sizing === "FIXED" && height.value === 0;
|
|
430
455
|
}
|
|
431
|
-
|
|
456
|
+
const KNOWN_PEN_TYPES = /* @__PURE__ */ new Set([
|
|
457
|
+
"frame",
|
|
458
|
+
"line",
|
|
459
|
+
"rectangle",
|
|
460
|
+
"ellipse",
|
|
461
|
+
"polygon",
|
|
462
|
+
"group",
|
|
463
|
+
"text",
|
|
464
|
+
"icon_font",
|
|
465
|
+
"path",
|
|
466
|
+
"icon",
|
|
467
|
+
"sticky",
|
|
468
|
+
"ref",
|
|
469
|
+
...ANNOTATION_TYPES
|
|
470
|
+
]);
|
|
471
|
+
function sceneType(pen, ctx, warn) {
|
|
472
|
+
if (!KNOWN_PEN_TYPES.has(pen.type)) warn?.(pen.type);
|
|
432
473
|
if (pen.type === "frame") return pen.reusable ? "COMPONENT" : "FRAME";
|
|
433
474
|
if (pen.type === "line") return isFlatLine(pen, ctx) ? "LINE" : "VECTOR";
|
|
434
475
|
if (isAnnotation(pen)) return "STICKY";
|
|
@@ -532,6 +573,7 @@ const PROPERTY_FIELDS = {
|
|
|
532
573
|
justifyContent: ["primaryAxisAlign"],
|
|
533
574
|
alignItems: ["counterAxisAlign"],
|
|
534
575
|
clip: ["clipsContent"],
|
|
576
|
+
status: ["status"],
|
|
535
577
|
placeholder: ["pluginData"],
|
|
536
578
|
slot: ["pluginData"],
|
|
537
579
|
innerRadius: ["arcData"],
|
|
@@ -622,6 +664,7 @@ function applyPenProperties(node, pen, build) {
|
|
|
622
664
|
visible: resolveBoolean(pen.enabled, ctx),
|
|
623
665
|
opacity: resolveNumber(pen.opacity, ctx, 1),
|
|
624
666
|
clipsContent: resolveBoolean(pen.clip, ctx, false),
|
|
667
|
+
status: pen.status ?? null,
|
|
625
668
|
layoutPositioning: absolute ? "ABSOLUTE" : "AUTO",
|
|
626
669
|
layoutGrow: 0,
|
|
627
670
|
layoutAlignSelf: "AUTO",
|
|
@@ -706,7 +749,7 @@ function createSceneNode(pen, parentId, build) {
|
|
|
706
749
|
const iconInstance = fields.type === "icon" && insideComponent(build.graph, parentId);
|
|
707
750
|
const leaveTheme = fields.theme ? build.ctx.enterTheme(fields.theme) : void 0;
|
|
708
751
|
try {
|
|
709
|
-
const type = instanceOf || iconInstance ? "INSTANCE" : sceneType(fields, build.ctx);
|
|
752
|
+
const type = instanceOf || iconInstance ? "INSTANCE" : sceneType(fields, build.ctx, build.onUnknownNodeType);
|
|
710
753
|
const id = build.graph.getNode(pen.id) ? build.graph.entityIdMinter()() : pen.id;
|
|
711
754
|
const node = type === "STICKY" ? createAnnotation({
|
|
712
755
|
...fields,
|
|
@@ -979,7 +1022,7 @@ function buildPenGraph(doc, options = {}) {
|
|
|
979
1022
|
const imports = options.imports ?? /* @__PURE__ */ new Map();
|
|
980
1023
|
const build = {
|
|
981
1024
|
graph,
|
|
982
|
-
ctx: buildVarContext(graph, withImports(doc, imports)),
|
|
1025
|
+
ctx: buildVarContext(graph, withImports(doc, imports), (name) => options.onUnknownVariable?.(name)),
|
|
983
1026
|
images: createImageStore(graph, options.images),
|
|
984
1027
|
shaders: options.shaders,
|
|
985
1028
|
icons: options.icons,
|
|
@@ -991,7 +1034,8 @@ function buildPenGraph(doc, options = {}) {
|
|
|
991
1034
|
groupIds: /* @__PURE__ */ new Set(),
|
|
992
1035
|
iconComponents: /* @__PURE__ */ new Map(),
|
|
993
1036
|
iconPageId: null,
|
|
994
|
-
replaced: /* @__PURE__ */ new Set()
|
|
1037
|
+
replaced: /* @__PURE__ */ new Set(),
|
|
1038
|
+
onUnknownNodeType: (type) => options.onUnknownNodeType?.(type)
|
|
995
1039
|
};
|
|
996
1040
|
collectComponents(doc.children, build.components);
|
|
997
1041
|
for (const imported of imports.values()) collectComponents(imported.doc.children, build.components);
|
|
@@ -1009,6 +1053,10 @@ function buildPenGraph(doc, options = {}) {
|
|
|
1009
1053
|
removeUnusedIconComponents(build);
|
|
1010
1054
|
markLibraryRoots(graph, libraryRoots);
|
|
1011
1055
|
if (graph.getPages(true).length === 0) graph.addPage("Page 1");
|
|
1056
|
+
if (doc.tokenSources && doc.tokenSources.length > 0) graph.updateNode(graph.rootId, { pluginData: tokenSourcesPluginData(graph, rekeyTokenSources(graph, doc.tokenSources)) });
|
|
1057
|
+
if (doc.designMemory) writeDesignMemory(graph, doc.designMemory);
|
|
1058
|
+
if (doc.statuses) writeStatusWorkflow(graph, doc.statuses);
|
|
1059
|
+
writeLibraryLinks(graph, doc);
|
|
1012
1060
|
return graph;
|
|
1013
1061
|
}
|
|
1014
1062
|
function parsePenFile(json, options = {}) {
|
|
@@ -1195,12 +1243,31 @@ function gradientPaint(fill) {
|
|
|
1195
1243
|
}))
|
|
1196
1244
|
};
|
|
1197
1245
|
}
|
|
1198
|
-
|
|
1199
|
-
|
|
1246
|
+
/**
|
|
1247
|
+
* An image fill as a pen.dev paint whose image travels with the file: Pencil.dev opens a data URL
|
|
1248
|
+
* and the scene graph keeps the bytes, so the fill is written whole instead of pointing at a hash
|
|
1249
|
+
* no reader can resolve. A paint whose bytes are gone is left out and reported, never written as a
|
|
1250
|
+
* dead URL.
|
|
1251
|
+
*/
|
|
1252
|
+
function imagePaint(state, fill) {
|
|
1253
|
+
const bytes = fill.imageHash ? state.graph.images.get(fill.imageHash) : void 0;
|
|
1254
|
+
if (!bytes) {
|
|
1255
|
+
state.drops.images++;
|
|
1256
|
+
return null;
|
|
1257
|
+
}
|
|
1258
|
+
if (fill.filters) state.drops.filters++;
|
|
1259
|
+
const paint = {
|
|
1200
1260
|
type: "image",
|
|
1201
1261
|
...paintBase(fill),
|
|
1202
|
-
url:
|
|
1262
|
+
url: `data:${imageMimeType(bytes)};base64,${fromUint8Array(bytes)}`
|
|
1203
1263
|
};
|
|
1264
|
+
if (fill.imageScaleMode === "FIT") paint.mode = "contain";
|
|
1265
|
+
if (fill.imageScaleMode === "CROP") {
|
|
1266
|
+
paint.mode = "stretch";
|
|
1267
|
+
const transform = fill.imageTransform && penCropTransform(fill.imageTransform);
|
|
1268
|
+
if (transform) paint.transform = transform;
|
|
1269
|
+
}
|
|
1270
|
+
return paint;
|
|
1204
1271
|
}
|
|
1205
1272
|
function penPaint(state, node, field, fill, index) {
|
|
1206
1273
|
if (fill.type === "SOLID") return {
|
|
@@ -1209,7 +1276,7 @@ function penPaint(state, node, field, fill, index) {
|
|
|
1209
1276
|
...paintBase(fill)
|
|
1210
1277
|
};
|
|
1211
1278
|
if (fill.type.startsWith("GRADIENT_")) return gradientPaint(fill);
|
|
1212
|
-
if (fill.type === "IMAGE") return imagePaint(fill);
|
|
1279
|
+
if (fill.type === "IMAGE") return imagePaint(state, fill);
|
|
1213
1280
|
return {
|
|
1214
1281
|
type: fill.type.toLowerCase(),
|
|
1215
1282
|
...paintBase(fill)
|
|
@@ -1217,7 +1284,8 @@ function penPaint(state, node, field, fill, index) {
|
|
|
1217
1284
|
}
|
|
1218
1285
|
function paintsOf(state, node, field, paints) {
|
|
1219
1286
|
if (paints.length === 0) return void 0;
|
|
1220
|
-
const result = paints.map((paint, index) => penPaint(state, node, field, paint, index));
|
|
1287
|
+
const result = paints.map((paint, index) => penPaint(state, node, field, paint, index)).filter((paint) => paint !== null);
|
|
1288
|
+
if (result.length === 0) return void 0;
|
|
1221
1289
|
return result.length === 1 ? result[0] : result;
|
|
1222
1290
|
}
|
|
1223
1291
|
function strokesOf(state, node) {
|
|
@@ -1413,25 +1481,36 @@ function variableScalar(value, state) {
|
|
|
1413
1481
|
}
|
|
1414
1482
|
function penVariable(variable, collection, state) {
|
|
1415
1483
|
const modes = collection?.modes ?? [];
|
|
1484
|
+
const syntax = variable.codeSyntax ? { codeSyntax: variable.codeSyntax } : {};
|
|
1416
1485
|
if (modes.length <= 1) return {
|
|
1417
1486
|
type: variableType(variable.type),
|
|
1418
|
-
value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ""], state)
|
|
1487
|
+
value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ""], state),
|
|
1488
|
+
...syntax
|
|
1419
1489
|
};
|
|
1420
1490
|
return {
|
|
1421
1491
|
type: variableType(variable.type),
|
|
1422
1492
|
value: modes.map((mode, index) => ({
|
|
1423
1493
|
value: variableScalar(variable.valuesByMode[mode.modeId], state),
|
|
1424
1494
|
...index === 0 ? {} : { theme: { [collection?.name ?? "Mode"]: mode.name } }
|
|
1425
|
-
}))
|
|
1495
|
+
})),
|
|
1496
|
+
...syntax
|
|
1426
1497
|
};
|
|
1427
1498
|
}
|
|
1428
1499
|
function variablesOf(state) {
|
|
1429
|
-
if (state.
|
|
1430
|
-
return Object.fromEntries([...state.
|
|
1500
|
+
if (state.variables.size === 0) return void 0;
|
|
1501
|
+
return Object.fromEntries([...state.variables.values()].sort((left, right) => left.name.localeCompare(right.name)).map((variable) => [variable.name, penVariable(variable, state.collections.get(variable.collectionId), state)]));
|
|
1431
1502
|
}
|
|
1432
1503
|
function themesOf(state) {
|
|
1433
|
-
if (state.
|
|
1434
|
-
return Object.fromEntries([...state.
|
|
1504
|
+
if (state.collections.size === 0) return void 0;
|
|
1505
|
+
return Object.fromEntries([...state.collections.values()].sort((left, right) => left.name.localeCompare(right.name)).map((collection) => [collection.name, collection.modes.map((mode) => mode.name)]));
|
|
1506
|
+
}
|
|
1507
|
+
/** The selector each mode applies in, by collection and mode name; none when no mode names one. */
|
|
1508
|
+
function modeConditionsOf(state) {
|
|
1509
|
+
const entries = [...state.collections.values()].flatMap((collection) => {
|
|
1510
|
+
const modes = collection.modes.flatMap((mode) => mode.condition ? [[mode.name, mode.condition]] : []);
|
|
1511
|
+
return modes.length > 0 ? [[collection.name, Object.fromEntries(modes)]] : [];
|
|
1512
|
+
});
|
|
1513
|
+
return entries.length > 0 ? Object.fromEntries(entries) : void 0;
|
|
1435
1514
|
}
|
|
1436
1515
|
function basicNodeFields(state, node, result) {
|
|
1437
1516
|
if (node.name) result.name = node.name;
|
|
@@ -1464,25 +1543,141 @@ function themeFields(state, node) {
|
|
|
1464
1543
|
}
|
|
1465
1544
|
return Object.keys(theme).length > 0 ? theme : void 0;
|
|
1466
1545
|
}
|
|
1546
|
+
/**
|
|
1547
|
+
* The pen fields an instance override writes: what it changes on a layer, not the placement and
|
|
1548
|
+
* layout a reopen recalculates from the component and the boxes around it.
|
|
1549
|
+
*/
|
|
1550
|
+
const OVERRIDABLE_FIELDS = /* @__PURE__ */ new Set([
|
|
1551
|
+
"content",
|
|
1552
|
+
"fill",
|
|
1553
|
+
"stroke",
|
|
1554
|
+
"strokeWidth",
|
|
1555
|
+
"cornerRadius",
|
|
1556
|
+
"effect",
|
|
1557
|
+
"opacity",
|
|
1558
|
+
"enabled",
|
|
1559
|
+
"fontFamily",
|
|
1560
|
+
"fontSize",
|
|
1561
|
+
"fontWeight",
|
|
1562
|
+
"fontStyle",
|
|
1563
|
+
"textAlign",
|
|
1564
|
+
"textAlignVertical",
|
|
1565
|
+
"lineHeight",
|
|
1566
|
+
"letterSpacing",
|
|
1567
|
+
"href"
|
|
1568
|
+
]);
|
|
1569
|
+
/**
|
|
1570
|
+
* The path a copy sits at in its component, from the instance down: the ids of the layers it
|
|
1571
|
+
* copies, as pen names a `descendants` override. A copy that swapped its component copies `swapOf`
|
|
1572
|
+
* instead of the component it draws.
|
|
1573
|
+
*/
|
|
1574
|
+
function overridePath(graph, instanceId, copyId, swapOf) {
|
|
1575
|
+
const parts = [];
|
|
1576
|
+
let current = graph.getNode(copyId);
|
|
1577
|
+
while (current && current.id !== instanceId) {
|
|
1578
|
+
const id = current.id === copyId && swapOf ? swapOf : current.componentId;
|
|
1579
|
+
if (!id) return null;
|
|
1580
|
+
parts.unshift(id);
|
|
1581
|
+
current = current.parentId ? graph.getNode(current.parentId) : void 0;
|
|
1582
|
+
}
|
|
1583
|
+
return current ? parts.join("/") : null;
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* The fields a swap override names an icon by: pen carries the glyph, not the component drawn.
|
|
1587
|
+
*/
|
|
1588
|
+
function iconOverride(graph, componentId) {
|
|
1589
|
+
const origin = readIconOrigin(componentId ? graph.getNode(componentId) : void 0);
|
|
1590
|
+
if (!origin) return void 0;
|
|
1591
|
+
const override = { icon: origin.icon };
|
|
1592
|
+
if (origin.library) override.library = origin.library;
|
|
1593
|
+
if (origin.weight !== void 0) override.weight = origin.weight;
|
|
1594
|
+
return override;
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* The overrides an instance writes, and whether one of them is a swap the file cannot name. A
|
|
1598
|
+
* layer that swapped its component draws another component than the one it copies, which pen holds
|
|
1599
|
+
* as a glyph for an icon; any other swap is what the instance shows, not what it links to.
|
|
1600
|
+
*/
|
|
1601
|
+
function instanceDescendants(state, node) {
|
|
1602
|
+
const overridden = [...node.instanceOverrides.descendants.keys()];
|
|
1603
|
+
if (overridden.length === 0) return { unrepresentable: false };
|
|
1604
|
+
const probe = {
|
|
1605
|
+
...state,
|
|
1606
|
+
drops: {
|
|
1607
|
+
images: 0,
|
|
1608
|
+
effects: 0,
|
|
1609
|
+
filters: 0,
|
|
1610
|
+
scroll: 0,
|
|
1611
|
+
fontFeatures: 0
|
|
1612
|
+
}
|
|
1613
|
+
};
|
|
1614
|
+
const descendants = {};
|
|
1615
|
+
let unrepresentable = false;
|
|
1616
|
+
for (const copyId of overridden) {
|
|
1617
|
+
const overrides = node.instanceOverrides.descendants.get(copyId);
|
|
1618
|
+
const swapped = overrides?.has("componentId") === true;
|
|
1619
|
+
const swapOf = swapped ? overrides.get("sourceComponentId") : void 0;
|
|
1620
|
+
const path = overridePath(state.graph, node.id, copyId, typeof swapOf === "string" ? swapOf : void 0);
|
|
1621
|
+
const copy = state.graph.getNode(copyId);
|
|
1622
|
+
if (!path || !copy) {
|
|
1623
|
+
unrepresentable = true;
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
if (swapped) {
|
|
1627
|
+
const icon = iconOverride(state.graph, copy.componentId);
|
|
1628
|
+
if (!icon) {
|
|
1629
|
+
unrepresentable = true;
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
descendants[path] = icon;
|
|
1633
|
+
continue;
|
|
1634
|
+
}
|
|
1635
|
+
const master = copy.componentId ? state.graph.getNode(copy.componentId) : void 0;
|
|
1636
|
+
if (!master) {
|
|
1637
|
+
unrepresentable = true;
|
|
1638
|
+
continue;
|
|
1639
|
+
}
|
|
1640
|
+
const own = writeNode(probe, copy);
|
|
1641
|
+
const base = writeNode(probe, master);
|
|
1642
|
+
const override = {};
|
|
1643
|
+
for (const field of OVERRIDABLE_FIELDS) if (!isEqual$1(own[field], base[field])) override[field] = own[field];
|
|
1644
|
+
if (Object.keys(override).length > 0) descendants[path] = override;
|
|
1645
|
+
}
|
|
1646
|
+
return {
|
|
1647
|
+
descendants: Object.keys(descendants).length > 0 ? descendants : void 0,
|
|
1648
|
+
unrepresentable
|
|
1649
|
+
};
|
|
1650
|
+
}
|
|
1467
1651
|
function semanticNodeFields(state, node, result) {
|
|
1468
1652
|
if (node.type === "TEXT" && node.textAutoResize === "WIDTH_AND_HEIGHT" && layoutSizing(state.graph, node, "HORIZONTAL") === "HUG") result.width = "fit_content";
|
|
1469
1653
|
if (node.type === "STICKY") result.content = stickySublayers(state.graph, node)?.body.text ?? "";
|
|
1470
1654
|
penPluginFields(node, result);
|
|
1471
|
-
const effects = node.effects.filter((effect) =>
|
|
1655
|
+
const effects = node.effects.filter((effect) => {
|
|
1656
|
+
if (effect.type === "DROP_SHADOW" || effect.type === "INNER_SHADOW") return true;
|
|
1657
|
+
if (effect.type === "LAYER_BLUR" || effect.type === "BACKGROUND_BLUR") return true;
|
|
1658
|
+
if (effect.type === "NOISE" || effect.type === "TEXTURE") state.drops.effects++;
|
|
1659
|
+
return false;
|
|
1660
|
+
});
|
|
1472
1661
|
if (effects.length > 0) result.effect = effects.length === 1 ? effectOf(effects[0]) : effects.map(effectOf);
|
|
1473
1662
|
if (node.type === "COMPONENT" || node.type === "COMPONENT_SET") result.reusable = true;
|
|
1474
|
-
if (node.type === "INSTANCE" && node.componentId && state.writtenIds.has(node.componentId)) {
|
|
1663
|
+
if (node.type === "INSTANCE" && node.componentId && (state.writtenIds.has(node.componentId) || state.importedIds.has(node.componentId))) {
|
|
1664
|
+
const overrides = instanceDescendants(state, node);
|
|
1665
|
+
if (overrides.unrepresentable && !state.writtenIds.has(node.componentId)) return false;
|
|
1475
1666
|
result.type = "ref";
|
|
1476
1667
|
result.ref = node.componentId;
|
|
1668
|
+
if (overrides.descendants) result.descendants = overrides.descendants;
|
|
1477
1669
|
return true;
|
|
1478
1670
|
}
|
|
1479
1671
|
return false;
|
|
1480
1672
|
}
|
|
1481
1673
|
function writeNode(state, node) {
|
|
1674
|
+
if (node.overflowDirection !== "NONE") state.drops.scroll++;
|
|
1675
|
+
if (node.fontFeatures.length > 0) state.drops.fontFeatures++;
|
|
1482
1676
|
const result = {
|
|
1483
1677
|
id: node.id,
|
|
1484
1678
|
type: nodeType(node)
|
|
1485
1679
|
};
|
|
1680
|
+
if (node.status !== null) result.status = node.status;
|
|
1486
1681
|
basicNodeFields(state, node, result);
|
|
1487
1682
|
if (semanticNodeFields(state, node, result)) return result;
|
|
1488
1683
|
if (node.type === "VECTOR" && node.vectorNetwork && node.vectorNetwork.segments.length > 0) {
|
|
@@ -1516,34 +1711,99 @@ function sortKeys(value) {
|
|
|
1516
1711
|
}
|
|
1517
1712
|
return value;
|
|
1518
1713
|
}
|
|
1519
|
-
function penDocumentFromGraph(graph) {
|
|
1714
|
+
function penDocumentFromGraph(graph, options = {}) {
|
|
1520
1715
|
const variables = /* @__PURE__ */ new Map();
|
|
1521
|
-
for (const variable of graph.
|
|
1716
|
+
for (const variable of graph.getLiveVariables()) variables.set(variable.id, variable);
|
|
1522
1717
|
const collections = /* @__PURE__ */ new Map();
|
|
1523
|
-
for (const collection of graph.
|
|
1718
|
+
for (const collection of graph.getLiveCollections()) collections.set(collection.id, collection);
|
|
1719
|
+
const imports = readPenImports(graph);
|
|
1524
1720
|
const state = {
|
|
1525
1721
|
graph,
|
|
1526
1722
|
variables,
|
|
1527
1723
|
collections,
|
|
1528
|
-
writtenIds: writtenNodeIds(graph)
|
|
1724
|
+
writtenIds: writtenNodeIds(graph),
|
|
1725
|
+
importedIds: importedComponentIds(graph, imports ?? {}),
|
|
1726
|
+
warnings: options.warnings ?? [],
|
|
1727
|
+
drops: {
|
|
1728
|
+
images: 0,
|
|
1729
|
+
effects: 0,
|
|
1730
|
+
filters: 0,
|
|
1731
|
+
scroll: 0,
|
|
1732
|
+
fontFeatures: 0
|
|
1733
|
+
}
|
|
1529
1734
|
};
|
|
1530
|
-
const
|
|
1735
|
+
const pages = graph.getPages();
|
|
1736
|
+
const children = pages.flatMap((page) => graph.getChildren(page.id).filter((node) => !node.internalOnly).map((node) => writeNode(state, node)));
|
|
1531
1737
|
const document = {
|
|
1532
1738
|
version: PEN_VERSION,
|
|
1533
|
-
name:
|
|
1739
|
+
name: pages[0]?.name,
|
|
1534
1740
|
children
|
|
1535
1741
|
};
|
|
1536
1742
|
const variablesValue = variablesOf(state);
|
|
1537
1743
|
if (variablesValue) document.variables = variablesValue;
|
|
1538
1744
|
const themes = themesOf(state);
|
|
1539
1745
|
if (themes) document.themes = themes;
|
|
1746
|
+
const conditions = modeConditionsOf(state);
|
|
1747
|
+
if (conditions) document.modeConditions = conditions;
|
|
1748
|
+
const tokenSources = readTokenSources(graph).map((source) => {
|
|
1749
|
+
const currentNames = Object.fromEntries(Object.keys(source.names ?? {}).flatMap((id) => {
|
|
1750
|
+
const variable = variables.get(id);
|
|
1751
|
+
const collection = variable ? collections.get(variable.collectionId) : void 0;
|
|
1752
|
+
return variable && collection ? [[id, `${collection.name}/${variable.name}`]] : [];
|
|
1753
|
+
}));
|
|
1754
|
+
const modes = {
|
|
1755
|
+
...source.modes,
|
|
1756
|
+
...Object.fromEntries([...collections.values()].map((collection) => [collection.name, Object.fromEntries(collection.modes.map((mode) => [mode.modeId, mode.name]))]))
|
|
1757
|
+
};
|
|
1758
|
+
const refreshed = { ...source };
|
|
1759
|
+
if (Object.keys(currentNames).length > 0) refreshed.currentNames = currentNames;
|
|
1760
|
+
if (Object.keys(modes).length > 0) refreshed.modes = modes;
|
|
1761
|
+
return refreshed;
|
|
1762
|
+
});
|
|
1763
|
+
if (tokenSources.length > 0) document.tokenSources = tokenSources;
|
|
1764
|
+
const designMemory = readDesignMemory(graph);
|
|
1765
|
+
if (designMemory) document.designMemory = designMemory;
|
|
1766
|
+
const statuses = readStoredStatusWorkflow(graph);
|
|
1767
|
+
if (statuses) document.statuses = statuses;
|
|
1768
|
+
if (imports) document.imports = imports;
|
|
1769
|
+
const enabledLibraries = enabledLibrariesOf(graph, state);
|
|
1770
|
+
if (enabledLibraries) document.enabledLibraries = enabledLibraries;
|
|
1771
|
+
const publication = readSourceLibraryPublication(graph);
|
|
1772
|
+
if (publication) document.sourceLibraryPublication = publication;
|
|
1773
|
+
if (pages.length > 1) {
|
|
1774
|
+
const name = document.name ?? "Page 1";
|
|
1775
|
+
state.warnings.push(`Pencil.dev files hold one page; ${pages.length} pages were merged into “${name}”.`);
|
|
1776
|
+
}
|
|
1777
|
+
if (graph.documentColorSpace === "display-p3") state.warnings.push("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.");
|
|
1778
|
+
reportDrops(state);
|
|
1540
1779
|
return document;
|
|
1541
1780
|
}
|
|
1781
|
+
/**
|
|
1782
|
+
* The library bindings a `.pen` file records, or null when the document has none. The definitions
|
|
1783
|
+
* live on the hidden page, which a Pencil.dev file cannot hold, so the file records the bindings
|
|
1784
|
+
* and says that the components it was built from come back as placeholders.
|
|
1785
|
+
*/
|
|
1786
|
+
function enabledLibrariesOf(graph, state) {
|
|
1787
|
+
const bindings = [...graph.enabledLibraries.values()];
|
|
1788
|
+
if (bindings.length === 0) return null;
|
|
1789
|
+
const enabled = bindings.filter((binding) => binding.enabled).length;
|
|
1790
|
+
if (enabled > 0) state.warnings.push(`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.`);
|
|
1791
|
+
return bindings;
|
|
1792
|
+
}
|
|
1793
|
+
/** Adds one sentence for each kind of content the written file had to leave out. */
|
|
1794
|
+
function reportDrops(state) {
|
|
1795
|
+
const { images, effects, filters, scroll, fontFeatures } = state.drops;
|
|
1796
|
+
if (images > 0) state.warnings.push(`Image fills with missing image data were left out (${images}); Pencil.dev files cannot hold a paint without its image.`);
|
|
1797
|
+
if (effects > 0) state.warnings.push(`Noise and texture effects were left out (${effects}); Pencil.dev files have no form for them.`);
|
|
1798
|
+
if (filters > 0) state.warnings.push(`Image adjustment filters were left out (${filters}); Pencil.dev files have no form for them.`);
|
|
1799
|
+
if (scroll > 0) state.warnings.push(`Scroll overflow was left out (${scroll}); Pencil.dev files have no form for it.`);
|
|
1800
|
+
if (fontFeatures > 0) state.warnings.push(`OpenType font features were left out (${fontFeatures}); Pencil.dev files have no form for them.`);
|
|
1801
|
+
}
|
|
1542
1802
|
function stringifyPenDocument(document) {
|
|
1543
1803
|
return `${JSON.stringify(sortKeys(document), null, 2)}\n`;
|
|
1544
1804
|
}
|
|
1545
|
-
function writePenDocument(source) {
|
|
1546
|
-
return stringifyPenDocument(source instanceof SceneGraph ? penDocumentFromGraph(source) : source);
|
|
1805
|
+
function writePenDocument(source, options = {}) {
|
|
1806
|
+
return stringifyPenDocument(source instanceof SceneGraph ? penDocumentFromGraph(source, options) : source);
|
|
1547
1807
|
}
|
|
1548
1808
|
//#endregion
|
|
1549
1809
|
export { buildPenGraph, parsePenDocument, parsePenFile, penDocumentFromGraph, readPenFile, stringifyPenDocument, withUsedImports, writePenDocument };
|