@pairdraw/design-jsx 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.
@@ -1,6 +1,6 @@
1
1
  import { isTreeNode } from "../behaviours/elements.js";
2
2
  import { BLEND_MODE, CONSTRAINT, MASK_TYPE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_CASE, TEXT_DECORATION, defineMode, defineVars, designMode, designVar, isMode, isVariable, parseScriptInput, property, ref } from "../component-properties.js";
3
- import { backgroundBlur, dropShadow, foregroundBlur, innerShadow, layerBlur } from "../effects.js";
3
+ import { backgroundBlur, dropShadow, foregroundBlur, innerShadow, layerBlur, noise, texture } from "../effects.js";
4
4
  import { Reference, connectorExportScope, connectorLabel, connectorProps, connectorRefDeclarations, helperCall, layersUnder, plainValue, propAttribute, valueSyntax } from "../connectors.js";
5
5
  import { DESIGN_JSX_ELEMENTS, collectCornerRadii, collectEffectProps, collectFillProps, collectPadding, collectStrokeProps, elementTag, emitPadding, formatColor, formatTracks, getNodeContext, shapeWithTextLabelText, shapeWithTextProps, stickyBody, stickyProps } from "../elements.js";
6
6
  import { AUTO, collectDecorationProps, textLengthJSX } from "./decoration.js";
@@ -161,6 +161,23 @@ const BLUR_OPTIONS = {
161
161
  radius: true,
162
162
  visible: true
163
163
  };
164
+ const NOISE_OPTIONS = {
165
+ noiseType: true,
166
+ noiseSize: true,
167
+ density: true,
168
+ color: true,
169
+ secondaryColor: true,
170
+ opacity: true,
171
+ seed: true,
172
+ visible: true
173
+ };
174
+ const TEXTURE_OPTIONS = {
175
+ noiseSize: true,
176
+ radius: true,
177
+ clipToShape: true,
178
+ seed: true,
179
+ visible: true
180
+ };
164
181
  const SOLID_OPTIONS = {
165
182
  opacity: true,
166
183
  visible: true,
@@ -212,6 +229,8 @@ function designJSXHelpers(warnings) {
212
229
  layerBlur: checked(warnings, "layerBlur", layerBlur, BLUR_OPTIONS, 0),
213
230
  backgroundBlur: checked(warnings, "backgroundBlur", backgroundBlur, BLUR_OPTIONS, 0),
214
231
  foregroundBlur: checked(warnings, "foregroundBlur", foregroundBlur, BLUR_OPTIONS, 0),
232
+ noise: checked(warnings, "noise", noise, NOISE_OPTIONS, 0),
233
+ texture: checked(warnings, "texture", texture, TEXTURE_OPTIONS, 0),
215
234
  defineVars,
216
235
  defineMode,
217
236
  designVar,
@@ -332,7 +351,7 @@ function resolveVariableId(graph, variable) {
332
351
  return collection && variableNamed(graph, collection, variable.name)?.id;
333
352
  }
334
353
  if (graph.variables.has(variable.name)) return variable.name;
335
- const matches = [...graph.variables.values()].filter((item) => item.name === variable.name);
354
+ const matches = graph.getLiveVariables().filter((item) => item.name === variable.name);
336
355
  if (matches.length > 1) {
337
356
  const [first] = matches;
338
357
  const collection = graph.variableCollections.get(first.collectionId)?.name ?? "";
@@ -826,7 +845,7 @@ function definitionExport(graph, rootIds) {
826
845
  if (typeof raw === "number" || typeof raw === "boolean") return raw;
827
846
  if (typeof raw !== "object") return void 0;
828
847
  if ("aliasId" in raw) return variable(raw.aliasId);
829
- return "r" in raw ? formatColor(raw, raw.a) : void 0;
848
+ return "r" in raw ? formatColor(raw, raw.a, graph.documentColorSpace) : void 0;
830
849
  };
831
850
  const values = (variables, modeId, base) => Object.fromEntries(variables.flatMap((item) => {
832
851
  const written = value(item.valuesByMode[modeId]);
@@ -935,9 +954,9 @@ function collectCornerRadiiProps(node, props) {
935
954
  if (bl > 0) props.push(["roundedBL", bl]);
936
955
  }
937
956
  }
938
- function collectAppearanceProps(node, props) {
939
- collectFillProps(node, props);
940
- collectStrokeProps(node, props);
957
+ function collectAppearanceProps(node, props, profile) {
958
+ collectFillProps(node, props, profile);
959
+ collectStrokeProps(node, props, profile);
941
960
  collectCornerRadiiProps(node, props);
942
961
  if (node.cornerSmoothing > 0) props.push(["cornerSmoothing", node.cornerSmoothing]);
943
962
  if (node.opacity < 1) props.push(["opacity", Math.round(node.opacity * 100) / 100]);
@@ -946,7 +965,7 @@ function collectAppearanceProps(node, props) {
946
965
  if (node.flipY) props.push(["flipY", true]);
947
966
  if (node.blendMode !== "PASS_THROUGH" && node.blendMode !== "NORMAL") props.push(["blendMode", BLEND_MODE.print(node.blendMode)]);
948
967
  if (node.clipsContent) props.push(["overflow", "hidden"]);
949
- collectEffectProps(node, props);
968
+ collectEffectProps(node, props, profile);
950
969
  }
951
970
  function collectPositionProps(node, ctx, props) {
952
971
  if (ctx.parentIsAutoLayout || ctx.parentIsGrid) {
@@ -1027,7 +1046,7 @@ function fontWeightValue(weight) {
1027
1046
  if (weight === 500) return "medium";
1028
1047
  return weight;
1029
1048
  }
1030
- function collectTextNodeProps(node, props) {
1049
+ function collectTextNodeProps(node, props, profile) {
1031
1050
  const direction = resolveNodeTextDirection(node);
1032
1051
  if (node.fontSize !== 14) props.push(["size", node.fontSize]);
1033
1052
  if (node.fontFamily && node.fontFamily !== DEFAULT_FONT_FAMILY) props.push(["font", node.fontFamily]);
@@ -1039,7 +1058,7 @@ function collectTextNodeProps(node, props) {
1039
1058
  if (node.lineHeight != null) props.push(["lineHeight", textLengthJSX(node.lineHeight)]);
1040
1059
  if (node.letterSpacing.value !== 0) props.push(["letterSpacing", textLengthJSX(node.letterSpacing)]);
1041
1060
  if (node.textDecoration !== "NONE") props.push(["textDecoration", TEXT_DECORATION.print(node.textDecoration)]);
1042
- collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props);
1061
+ collectDecorationProps(ownTextFields(node, DECORATION_FIELDS), node, props, profile);
1043
1062
  collectTextFontSettingProps(node, props);
1044
1063
  if (node.textCase !== "ORIGINAL") props.push(["textCase", TEXT_CASE.print(node.textCase)]);
1045
1064
  if (node.maxLines != null) props.push(["maxLines", node.maxLines]);
@@ -1079,7 +1098,7 @@ function collectShapeNodeProps(node, props) {
1079
1098
  if (node.starInnerRadius !== .382) props.push(["innerRadius", node.starInnerRadius]);
1080
1099
  }
1081
1100
  if (node.type === "POLYGON" && node.pointCount !== 3) props.push(["points", node.pointCount]);
1082
- if (node.type === "VECTOR" && node.vectorNetwork) {
1101
+ if ((node.type === "VECTOR" || node.type === "BOOLEAN_OPERATION") && node.vectorNetwork) {
1083
1102
  const paths = vectorNetworkPaths(node.vectorNetwork);
1084
1103
  if (paths.length > 0) props.push(["paths", paths.map((path) => ({ ...path }))]);
1085
1104
  }
@@ -1097,9 +1116,9 @@ function collectProps(node, graph, variables = variableReferences(graph)) {
1097
1116
  if (ctx.parentIsGrid) collectGridPositionProps(node, props);
1098
1117
  if (ctx.isFlex) collectFlexAlignmentProps(node, props);
1099
1118
  if (ctx.isAutoLayout) collectAutoLayoutPaddingProps(node, props);
1100
- collectAppearanceProps(node, props);
1119
+ collectAppearanceProps(node, props, graph.documentColorSpace);
1101
1120
  if (node.type === "TEXT") {
1102
- collectTextNodeProps(node, props);
1121
+ collectTextNodeProps(node, props, graph.documentColorSpace);
1103
1122
  collectTextAutoResizeProp(node, ctx, props);
1104
1123
  }
1105
1124
  collectShapeNodeProps(node, props);
@@ -1125,7 +1144,7 @@ function characterStyles(node) {
1125
1144
  }
1126
1145
  return styles.map((style) => omitBy(style, (value, key) => value === void 0 || key !== "hyperlink" && isEqual(value, node[key])));
1127
1146
  }
1128
- function spanProps(style, node) {
1147
+ function spanProps(style, node, profile) {
1129
1148
  const props = [];
1130
1149
  if (style.fontSize !== void 0) props.push(["size", style.fontSize]);
1131
1150
  if (style.fontFamily !== void 0) props.push(["font", style.fontFamily]);
@@ -1134,25 +1153,25 @@ function spanProps(style, node) {
1134
1153
  if (style.fills) collectFillProps({
1135
1154
  ...node,
1136
1155
  fills: style.fills
1137
- }, props);
1156
+ }, props, profile);
1138
1157
  if (style.lineHeight !== void 0) props.push(["lineHeight", style.lineHeight ? textLengthJSX(style.lineHeight) : AUTO]);
1139
1158
  if (style.letterSpacing !== void 0) props.push(["letterSpacing", textLengthJSX(style.letterSpacing)]);
1140
1159
  if (style.textDecoration !== void 0) props.push(["textDecoration", TEXT_DECORATION.print(style.textDecoration)]);
1141
- collectDecorationProps(style, node, props);
1160
+ collectDecorationProps(style, node, props, profile);
1142
1161
  collectFontSettingProps(style, props);
1143
1162
  if (style.textCase !== void 0) props.push(["textCase", TEXT_CASE.print(style.textCase)]);
1144
1163
  if (style.hyperlink) props.push(["href", style.hyperlink.value]);
1145
1164
  return props;
1146
1165
  }
1147
1166
  /** `text[start, end)` as text and a `<Span>` for every stretch styled apart from the text. */
1148
- function inlineContent(node, styles, start, end) {
1167
+ function inlineContent(node, styles, start, end, profile) {
1149
1168
  const content = [];
1150
1169
  let from = start;
1151
1170
  for (let index = start + 1; index <= end; index++) {
1152
1171
  if (index < end && isEqual(styles[index], styles[from])) continue;
1153
1172
  const text = jsx.inlineText(node.text.slice(from, index));
1154
1173
  const style = styles[from];
1155
- content.push(isEmptyObject(style) ? text : jsx.element("Span", spanProps(style, node).map(propAttribute), [text], 0, true));
1174
+ content.push(isEmptyObject(style) ? text : jsx.element("Span", spanProps(style, node, profile).map(propAttribute), [text], 0, true));
1156
1175
  from = index;
1157
1176
  }
1158
1177
  return content;
@@ -1187,7 +1206,7 @@ function paragraphProps(style) {
1187
1206
  * `<Span>`s for characters styled apart, and as `<Paragraph>`s when a paragraph is a list item
1188
1207
  * or has spacing other than the text's.
1189
1208
  */
1190
- function textChildren(node, depth) {
1209
+ function textChildren(node, depth, profile = "srgb") {
1191
1210
  const styles = characterStyles(node);
1192
1211
  const ranges = textParagraphRanges(node.text);
1193
1212
  const paragraphs = ranges.map((_, index) => ownParagraphStyle(node, index));
@@ -1197,12 +1216,12 @@ function textChildren(node, depth) {
1197
1216
  inline: true
1198
1217
  };
1199
1218
  return {
1200
- children: inlineContent(node, styles, 0, node.text.length),
1219
+ children: inlineContent(node, styles, 0, node.text.length, profile),
1201
1220
  inline: true
1202
1221
  };
1203
1222
  }
1204
1223
  return {
1205
- children: ranges.map(({ start, end }, index) => jsx.element("Paragraph", paragraphProps(paragraphs[index]).map(propAttribute), inlineContent(node, styles, start, end), depth + 1, true)),
1224
+ children: ranges.map(({ start, end }, index) => jsx.element("Paragraph", paragraphProps(paragraphs[index]).map(propAttribute), inlineContent(node, styles, start, end, profile), depth + 1, true)),
1206
1225
  inline: false
1207
1226
  };
1208
1227
  }
@@ -1294,10 +1313,11 @@ function nodeToJSX(node, graph, depth, scope, variables, layerIds, outer = NO_RE
1294
1313
  if (ownProps?.some(([name]) => name === "of")) return [jsx.element(tag, withRef(ownProps).map(propAttribute), [], depth)];
1295
1314
  const attributes = withRef([...collectProps(node, graph, variables), ...ownProps ?? []]).map(propAttribute);
1296
1315
  if (node.type === "TEXT") {
1297
- const { children: content, inline } = textChildren(node, depth);
1316
+ const { children: content, inline } = textChildren(node, depth, graph.documentColorSpace);
1298
1317
  for (let count = elementCount(content); count > 0; count--) layerIds?.push(null);
1299
1318
  return [jsx.element(tag, attributes, content, depth, inline)];
1300
1319
  }
1320
+ if (node.type === "BOOLEAN_OPERATION") return [jsx.element(tag, attributes, [], depth)];
1301
1321
  return [jsx.element(tag, attributes, children(depth + 1), depth)];
1302
1322
  }
1303
1323
  /** Printed values break lines only between tokens, never inside a string, so joining is lossless. */
@@ -1336,7 +1356,7 @@ function designJSXElement(nodeId, graph) {
1336
1356
  const hasContent = node.type === "TEXT" || own !== void 0;
1337
1357
  const attributes = Object.fromEntries(printed.filter(({ name }) => !hasContent || name !== "text").map(({ name, source }) => [name, source]));
1338
1358
  if (node.type === "TEXT") {
1339
- const { children, inline } = textChildren(node, 0);
1359
+ const { children, inline } = textChildren(node, 0, graph.documentColorSpace);
1340
1360
  return {
1341
1361
  tag,
1342
1362
  attributes,