@pairdraw/pen 0.19.2 → 0.20.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.
@@ -1,4 +1,4 @@
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, resolveBoolean, resolveNumber, resolveString, unresolvedRef } from "./annotations.js";
2
2
  import { withImports } from "./imports.js";
3
3
  import { ellipseArc, lineNetwork, networkToSVGPath, pathNetwork } from "./geometry.js";
4
4
  import { drawIcon, insideComponent, removeUnusedIconComponents, useIconComponent } from "./icons.js";
@@ -9,6 +9,7 @@ import { pick } from "es-toolkit/object";
9
9
  import { isEqual } from "es-toolkit/predicate";
10
10
  import { createComponentPropertyId, libraryDefinitionsPage } from "@pairdraw/scene-graph/components";
11
11
  import * as v from "valibot";
12
+ import { readTokenSources, rekeyTokenSources, tokenSourcesPluginData } from "@pairdraw/scene-graph/token-sources";
12
13
  import { INSTANCE_OVERRIDE_FIELDS, detachInstance, populateInstanceChildren, recordInstanceOverride, swapInstanceComponent } from "@pairdraw/scene-graph/instances";
13
14
  import { normalizeEffects } from "@pairdraw/scene-graph/effects";
14
15
  import { CORNER_FIELDS } from "@pairdraw/scene-graph/fields";
@@ -19,6 +20,7 @@ import { getNodeLocalMatrix, localTransformFromWorld } from "@pairdraw/scene-gra
19
20
  import { fitEnclosingGroups } from "@pairdraw/scene-graph/group-bounds";
20
21
  import Matrix from "@pairdraw/scene-graph/matrix";
21
22
  import { canCreateSlot, claimSlotContent, createSlotProperty, slotOwner, slotScope, updateSlotProperty } from "@pairdraw/scene-graph/slots";
23
+ import { omit } from "es-toolkit";
22
24
  import { parseFontStyle } from "@pairdraw/scene-graph/font-style";
23
25
  import { percentLength, pixelLength } from "@pairdraw/scene-graph/text";
24
26
  //#region src/library.ts
@@ -385,7 +387,7 @@ function applyTextProps(node, pen, ctx, fonts) {
385
387
  fontWeight: weight,
386
388
  italic
387
389
  });
388
- delete node.boundVariables.fontFamily;
390
+ node.boundVariables = omit(node.boundVariables, ["fontFamily"]);
389
391
  }
390
392
  node.textAlignHorizontal = TEXT_ALIGN[pen.textAlign ?? "left"] ?? "LEFT";
391
393
  node.textAlignVertical = TEXT_ALIGN_VERTICAL[pen.textAlignVertical ?? "top"] ?? "TOP";
@@ -428,7 +430,23 @@ function isFlatLine(pen, ctx) {
428
430
  const height = parseSize(pen.height, 100, ctx);
429
431
  return height.sizing === "FIXED" && height.value === 0;
430
432
  }
431
- function sceneType(pen, ctx) {
433
+ const KNOWN_PEN_TYPES = /* @__PURE__ */ new Set([
434
+ "frame",
435
+ "line",
436
+ "rectangle",
437
+ "ellipse",
438
+ "polygon",
439
+ "group",
440
+ "text",
441
+ "icon_font",
442
+ "path",
443
+ "icon",
444
+ "sticky",
445
+ "ref",
446
+ ...ANNOTATION_TYPES
447
+ ]);
448
+ function sceneType(pen, ctx, warn) {
449
+ if (!KNOWN_PEN_TYPES.has(pen.type)) warn?.(pen.type);
432
450
  if (pen.type === "frame") return pen.reusable ? "COMPONENT" : "FRAME";
433
451
  if (pen.type === "line") return isFlatLine(pen, ctx) ? "LINE" : "VECTOR";
434
452
  if (isAnnotation(pen)) return "STICKY";
@@ -706,7 +724,7 @@ function createSceneNode(pen, parentId, build) {
706
724
  const iconInstance = fields.type === "icon" && insideComponent(build.graph, parentId);
707
725
  const leaveTheme = fields.theme ? build.ctx.enterTheme(fields.theme) : void 0;
708
726
  try {
709
- const type = instanceOf || iconInstance ? "INSTANCE" : sceneType(fields, build.ctx);
727
+ const type = instanceOf || iconInstance ? "INSTANCE" : sceneType(fields, build.ctx, build.onUnknownNodeType);
710
728
  const id = build.graph.getNode(pen.id) ? build.graph.entityIdMinter()() : pen.id;
711
729
  const node = type === "STICKY" ? createAnnotation({
712
730
  ...fields,
@@ -979,7 +997,7 @@ function buildPenGraph(doc, options = {}) {
979
997
  const imports = options.imports ?? /* @__PURE__ */ new Map();
980
998
  const build = {
981
999
  graph,
982
- ctx: buildVarContext(graph, withImports(doc, imports)),
1000
+ ctx: buildVarContext(graph, withImports(doc, imports), (name) => options.onUnknownVariable?.(name)),
983
1001
  images: createImageStore(graph, options.images),
984
1002
  shaders: options.shaders,
985
1003
  icons: options.icons,
@@ -991,7 +1009,8 @@ function buildPenGraph(doc, options = {}) {
991
1009
  groupIds: /* @__PURE__ */ new Set(),
992
1010
  iconComponents: /* @__PURE__ */ new Map(),
993
1011
  iconPageId: null,
994
- replaced: /* @__PURE__ */ new Set()
1012
+ replaced: /* @__PURE__ */ new Set(),
1013
+ onUnknownNodeType: (type) => options.onUnknownNodeType?.(type)
995
1014
  };
996
1015
  collectComponents(doc.children, build.components);
997
1016
  for (const imported of imports.values()) collectComponents(imported.doc.children, build.components);
@@ -1009,6 +1028,7 @@ function buildPenGraph(doc, options = {}) {
1009
1028
  removeUnusedIconComponents(build);
1010
1029
  markLibraryRoots(graph, libraryRoots);
1011
1030
  if (graph.getPages(true).length === 0) graph.addPage("Page 1");
1031
+ if (doc.tokenSources && doc.tokenSources.length > 0) graph.updateNode(graph.rootId, { pluginData: tokenSourcesPluginData(graph, rekeyTokenSources(graph, doc.tokenSources)) });
1012
1032
  return graph;
1013
1033
  }
1014
1034
  function parsePenFile(json, options = {}) {
@@ -1413,16 +1433,19 @@ function variableScalar(value, state) {
1413
1433
  }
1414
1434
  function penVariable(variable, collection, state) {
1415
1435
  const modes = collection?.modes ?? [];
1436
+ const syntax = variable.codeSyntax ? { codeSyntax: variable.codeSyntax } : {};
1416
1437
  if (modes.length <= 1) return {
1417
1438
  type: variableType(variable.type),
1418
- value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ""], state)
1439
+ value: variableScalar(variable.valuesByMode[modes[0]?.modeId ?? ""], state),
1440
+ ...syntax
1419
1441
  };
1420
1442
  return {
1421
1443
  type: variableType(variable.type),
1422
1444
  value: modes.map((mode, index) => ({
1423
1445
  value: variableScalar(variable.valuesByMode[mode.modeId], state),
1424
1446
  ...index === 0 ? {} : { theme: { [collection?.name ?? "Mode"]: mode.name } }
1425
- }))
1447
+ })),
1448
+ ...syntax
1426
1449
  };
1427
1450
  }
1428
1451
  function variablesOf(state) {
@@ -1433,6 +1456,14 @@ function themesOf(state) {
1433
1456
  if (state.graph.variableCollections.size === 0) return void 0;
1434
1457
  return Object.fromEntries([...state.graph.variableCollections.values()].sort((left, right) => left.name.localeCompare(right.name)).map((collection) => [collection.name, collection.modes.map((mode) => mode.name)]));
1435
1458
  }
1459
+ /** The selector each mode applies in, by collection and mode name; none when no mode names one. */
1460
+ function modeConditionsOf(state) {
1461
+ const entries = [...state.graph.variableCollections.values()].flatMap((collection) => {
1462
+ const modes = collection.modes.flatMap((mode) => mode.condition ? [[mode.name, mode.condition]] : []);
1463
+ return modes.length > 0 ? [[collection.name, Object.fromEntries(modes)]] : [];
1464
+ });
1465
+ return entries.length > 0 ? Object.fromEntries(entries) : void 0;
1466
+ }
1436
1467
  function basicNodeFields(state, node, result) {
1437
1468
  if (node.name) result.name = node.name;
1438
1469
  const placement = penCoordinates(node);
@@ -1537,6 +1568,24 @@ function penDocumentFromGraph(graph) {
1537
1568
  if (variablesValue) document.variables = variablesValue;
1538
1569
  const themes = themesOf(state);
1539
1570
  if (themes) document.themes = themes;
1571
+ const conditions = modeConditionsOf(state);
1572
+ if (conditions) document.modeConditions = conditions;
1573
+ const tokenSources = readTokenSources(graph).map((source) => {
1574
+ const currentNames = Object.fromEntries(Object.keys(source.names ?? {}).flatMap((id) => {
1575
+ const variable = variables.get(id);
1576
+ const collection = variable ? collections.get(variable.collectionId) : void 0;
1577
+ return variable && collection ? [[id, `${collection.name}/${variable.name}`]] : [];
1578
+ }));
1579
+ const modes = {
1580
+ ...source.modes,
1581
+ ...Object.fromEntries([...collections.values()].map((collection) => [collection.name, Object.fromEntries(collection.modes.map((mode) => [mode.modeId, mode.name]))]))
1582
+ };
1583
+ const refreshed = { ...source };
1584
+ if (Object.keys(currentNames).length > 0) refreshed.currentNames = currentNames;
1585
+ if (Object.keys(modes).length > 0) refreshed.modes = modes;
1586
+ return refreshed;
1587
+ });
1588
+ if (tokenSources.length > 0) document.tokenSources = tokenSources;
1540
1589
  return document;
1541
1590
  }
1542
1591
  function stringifyPenDocument(document) {