@pairdraw/pen 0.16.2 → 0.18.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.
@@ -10,6 +10,7 @@ import { isEqual } from "es-toolkit/predicate";
10
10
  import { createComponentPropertyId, libraryDefinitionsPage } from "@pairdraw/scene-graph/components";
11
11
  import * as v from "valibot";
12
12
  import { INSTANCE_OVERRIDE_FIELDS, detachInstance, populateInstanceChildren, recordInstanceOverride, swapInstanceComponent } from "@pairdraw/scene-graph/instances";
13
+ import { normalizeEffects } from "@pairdraw/scene-graph/effects";
13
14
  import { CORNER_FIELDS } from "@pairdraw/scene-graph/fields";
14
15
  import { fillSizingFields, layoutSizing } from "@pairdraw/scene-graph/layout";
15
16
  import { stickySublayers } from "@pairdraw/scene-graph/generated";
@@ -630,7 +631,7 @@ function applyPenProperties(node, pen, build) {
630
631
  ...paint,
631
632
  align: "CENTER"
632
633
  }));
633
- node.effects = convertEffects(pen.effect, ctx, build.shaders);
634
+ node.effects = normalizeEffects(convertEffects(pen.effect, ctx, build.shaders));
634
635
  applyCornerRadius(node, pen.cornerRadius, ctx);
635
636
  applyPadding(node, pen.padding, ctx);
636
637
  if (isTextLike) applyTextProps(node, pen, ctx);
@@ -693,10 +694,11 @@ function createSceneNode(pen, parentId, build) {
693
694
  const leaveTheme = fields.theme ? build.ctx.enterTheme(fields.theme) : void 0;
694
695
  try {
695
696
  const type = instanceOf || iconInstance ? "INSTANCE" : sceneType(fields, build.ctx);
697
+ const id = build.graph.getNode(pen.id) ? build.graph.entityIdMinter()() : pen.id;
696
698
  const node = type === "STICKY" ? createAnnotation({
697
699
  ...fields,
698
- id: pen.id
699
- }, parentId, build) : build.graph.createNode(type, parentId, { id: pen.id });
700
+ id
701
+ }, parentId, build) : build.graph.createNode(type, parentId, { id });
700
702
  applyPenProperties(node, fields, build);
701
703
  if (instanceOf) {
702
704
  node.componentId = instanceOf.id;
@@ -971,7 +973,7 @@ function buildPenGraph(doc, options = {}) {
971
973
  };
972
974
  collectComponents(doc.children, build.components);
973
975
  for (const imported of imports.values()) collectComponents(imported.doc.children, build.components);
974
- const page = graph.addPage(doc.children[0]?.name ?? "Page 1");
976
+ const page = graph.addPage(doc.name ?? doc.children[0]?.name ?? "Page 1");
975
977
  for (const child of doc.children) createSceneNode(child, page.id, build);
976
978
  const libraryRoots = createLibraryRoots(doc, imports, build, createSceneNode);
977
979
  populateInstances(build);
@@ -994,6 +996,23 @@ async function readPenFile(file, options) {
994
996
  return parsePenFile(await file.text(), options);
995
997
  }
996
998
  //#endregion
999
+ //#region src/written-nodes.ts
1000
+ /**
1001
+ * Collects the nodes `writeNode` emits. Instance subtrees are skipped because a ref writes
1002
+ * no children; an instance whose component is missing here is written as a frame instead.
1003
+ */
1004
+ function writtenNodeIds(graph) {
1005
+ const ids = /* @__PURE__ */ new Set();
1006
+ const visit = (node) => {
1007
+ if (node.internalOnly) return;
1008
+ ids.add(node.id);
1009
+ if (node.type === "STICKY" || node.type === "INSTANCE") return;
1010
+ for (const child of graph.getChildren(node.id)) visit(child);
1011
+ };
1012
+ for (const page of graph.getPages()) for (const node of graph.getChildren(page.id)) visit(node);
1013
+ return ids;
1014
+ }
1015
+ //#endregion
997
1016
  //#region src/write.ts
998
1017
  const PEN_VERSION = "2.21";
999
1018
  const PEN_PLUGIN_FIELDS = {
@@ -1001,6 +1020,7 @@ const PEN_PLUGIN_FIELDS = {
1001
1020
  "pen/ref": "ref",
1002
1021
  "pen/generated": "generated",
1003
1022
  "pen/context": "context",
1023
+ "pen/metadata": "metadata",
1004
1024
  "pen/placeholder": "placeholder",
1005
1025
  "pen/slot": "slot",
1006
1026
  "pen/model": "model",
@@ -1016,8 +1036,9 @@ const PEN_PLUGIN_FIELDS = {
1016
1036
  function snapCoordinate(value) {
1017
1037
  return Math.abs(value - Math.round(value)) < 1e-9 ? Math.round(value) : value;
1018
1038
  }
1039
+ /** A non-finite number has no JSON form, so it is written as 0 to keep the file loadable. */
1019
1040
  function rounded(value) {
1020
- return Number(value.toFixed(3));
1041
+ return Number.isFinite(value) ? Number(value.toFixed(3)) : 0;
1021
1042
  }
1022
1043
  function colorValue(value) {
1023
1044
  return value.a === 1 ? colorToHex(value) : colorToHex8(value);
@@ -1413,7 +1434,7 @@ function semanticNodeFields(state, node, result) {
1413
1434
  const effects = node.effects.filter((effect) => effect.type !== "SHADER");
1414
1435
  if (effects.length > 0) result.effect = effects.length === 1 ? effectOf(effects[0]) : effects.map(effectOf);
1415
1436
  if (node.type === "COMPONENT" || node.type === "COMPONENT_SET") result.reusable = true;
1416
- if (node.type === "INSTANCE" && node.componentId) {
1437
+ if (node.type === "INSTANCE" && node.componentId && state.writtenIds.has(node.componentId)) {
1417
1438
  result.type = "ref";
1418
1439
  result.ref = node.componentId;
1419
1440
  return true;
@@ -1453,11 +1474,13 @@ function penDocumentFromGraph(graph) {
1453
1474
  const state = {
1454
1475
  graph,
1455
1476
  variables,
1456
- collections
1477
+ collections,
1478
+ writtenIds: writtenNodeIds(graph)
1457
1479
  };
1458
1480
  const children = graph.getPages().flatMap((page) => graph.getChildren(page.id).filter((node) => !node.internalOnly).map((node) => writeNode(state, node)));
1459
1481
  const document = {
1460
1482
  version: PEN_VERSION,
1483
+ name: graph.getPages()[0]?.name,
1461
1484
  children
1462
1485
  };
1463
1486
  const variablesValue = variablesOf(state);