@pairdraw/design-jsx 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.
@@ -1,15 +1,15 @@
1
1
  import { REKA_ELEMENTS, isTreeNode, resolveToTree } from "./behaviours/elements.js";
2
- import { BLEND_MODE, CONSTRAINT, ENUM_PROPS, LayerRefs, MASK_TYPE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_AUTO_RESIZE, TEXT_CASE, TEXT_DECORATION, TEXT_DIRECTION, applyStrokeOverrides, assignComponentProperties, assignNamedProperties, bindProperties, componentPropNames as componentPropNames$1, elementPropWarnings, exposeInstance, fillFromValue, inferComponentSetProperties, isColor, isFillValue, isVariable, parseScriptInput, readElementProps } from "./component-properties.js";
2
+ import { BLEND_MODE, CONSTRAINT, ENUM_PROPS, LayerRefs, MASK_TYPE, TEXT_ALIGN, TEXT_ALIGN_VERTICAL, TEXT_AUTO_RESIZE, TEXT_CASE, TEXT_DECORATION, TEXT_DIRECTION, applyStrokeOverrides, assignComponentProperties, assignNamedProperties, bindProperties, componentPropNames as componentPropNames$1, elementPropWarnings, enumProp, exposeInstance, fillFromValue, inferComponentSetProperties, isColor, isFillValue, isVariable, parseScriptInput, readElementProps } from "./component-properties.js";
3
3
  import { DESIGN_JSX_STYLE_KEYS, designJSXProp, renderRekaNode } from "./behaviours/render.js";
4
4
  import { DESIGN_JSX_HELPERS, TEXT_CONTENT_NAMES, applyVariableDefinitions, bindVariableProp, designJSXHelpers, isShaderFill, isTextContent, requireVariableId, resolveForLayer, textContentFields, variableModes } from "./export/index.js";
5
5
  import { resolveConnectorEnds } from "./connectors.js";
6
6
  import { elementBindings, elementNodeType, elementOwnLayer, elementProps, elementSchema } from "./elements.js";
7
7
  import { applyTextDetailOverrides, textLengthProp } from "./export/decoration.js";
8
8
  import { paintProps, scalarProps } from "./export/bindings.js";
9
- import { colorToFill, parseColor } from "@pairdraw/scene-graph/color";
9
+ import { colorSchema, colorToFill, parseColor } from "@pairdraw/scene-graph/color";
10
10
  import { TRANSPARENT } from "@pairdraw/scene-graph/constants";
11
11
  import * as v from "valibot";
12
- import { withShaderPaints } from "@pairdraw/scene-graph/shaders";
12
+ import { shaderUniformValueSchema, withShaderPaints } from "@pairdraw/scene-graph/shaders";
13
13
  import { omitBy } from "es-toolkit/object";
14
14
  import { uniq } from "es-toolkit/array";
15
15
  import { setInstanceOverride } from "@pairdraw/scene-graph/instances/overrides";
@@ -17,6 +17,8 @@ import { parsePluginVectorPath, parseSVGPathData } from "@pairdraw/scene-graph/p
17
17
  import { mergeVectorNetworks, transformVectorNetwork } from "@pairdraw/scene-graph/vector-network";
18
18
  import { parseCSSGridTracks, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
19
19
  import { autoLayoutSizingFields, fillSizingFields, parseAutoLayoutDirection, parseCounterAxisAlign, parsePrimaryAxisAlign } from "@pairdraw/scene-graph/layout";
20
+ import { normalizeEffects } from "@pairdraw/scene-graph/effects";
21
+ import { finiteNumberSchema } from "@pairdraw/scene-graph/primitives";
20
22
  import { omit } from "es-toolkit";
21
23
  import { createDefaultNode } from "@pairdraw/scene-graph/node-defaults";
22
24
  import { parse } from "acorn";
@@ -47,14 +49,84 @@ function createElement(type, props, ...children) {
47
49
  }
48
50
  //#endregion
49
51
  //#region src/overrides/effects.ts
50
- function isEffect(value) {
51
- return value !== null && typeof value === "object" && "type" in value && "radius" in value && "visible" in value;
52
+ const offsetSchema = v.object({
53
+ x: v.optional(finiteNumberSchema),
54
+ y: v.optional(finiteNumberSchema)
55
+ });
56
+ const effectFields = {
57
+ radius: finiteNumberSchema,
58
+ offset: v.optional(offsetSchema),
59
+ spread: v.optional(finiteNumberSchema, 0),
60
+ visible: v.optional(v.boolean(), true),
61
+ blendMode: v.optional(enumProp(BLEND_MODE)),
62
+ showShadowBehindNode: v.optional(v.boolean())
63
+ };
64
+ function shadowEffectSchema(type) {
65
+ return v.object({
66
+ type: v.literal(type),
67
+ color: colorSchema,
68
+ ...effectFields
69
+ });
70
+ }
71
+ function blurEffectSchema(type) {
72
+ return v.object({
73
+ type: v.literal(type),
74
+ color: v.optional(colorSchema),
75
+ ...effectFields
76
+ });
77
+ }
78
+ const shaderEffectSchema = v.object({
79
+ type: v.literal("SHADER"),
80
+ source: v.pipe(v.string(), v.minLength(1)),
81
+ uniforms: v.optional(v.record(v.string(), shaderUniformValueSchema)),
82
+ uniformBindings: v.optional(v.record(v.string(), v.string())),
83
+ visible: v.optional(v.boolean(), true),
84
+ blendMode: v.optional(enumProp(BLEND_MODE))
85
+ });
86
+ const effectSchema = v.variant("type", [
87
+ shadowEffectSchema("DROP_SHADOW"),
88
+ shadowEffectSchema("INNER_SHADOW"),
89
+ blurEffectSchema("LAYER_BLUR"),
90
+ blurEffectSchema("BACKGROUND_BLUR"),
91
+ blurEffectSchema("FOREGROUND_BLUR"),
92
+ shaderEffectSchema
93
+ ]);
94
+ const effectsSchema = v.array(effectSchema);
95
+ /** Drop an omitted offset so `normalizeEffects` can apply the shadow or blur default. */
96
+ function forNormalize(effect) {
97
+ if (effect.type === "SHADER") {
98
+ const shader = {
99
+ type: "SHADER",
100
+ source: effect.source,
101
+ visible: effect.visible
102
+ };
103
+ if (effect.uniforms !== void 0) shader.uniforms = effect.uniforms;
104
+ if (effect.uniformBindings !== void 0) shader.uniformBindings = effect.uniformBindings;
105
+ if (effect.blendMode !== void 0) shader.blendMode = effect.blendMode;
106
+ return shader;
107
+ }
108
+ const shadow = {
109
+ type: effect.type,
110
+ color: effect.color ?? { ...TRANSPARENT },
111
+ radius: effect.radius,
112
+ spread: effect.spread,
113
+ visible: effect.visible
114
+ };
115
+ if (effect.offset !== void 0) {
116
+ const offset = {};
117
+ if (effect.offset.x !== void 0) offset.x = effect.offset.x;
118
+ if (effect.offset.y !== void 0) offset.y = effect.offset.y;
119
+ shadow.offset = offset;
120
+ }
121
+ if (effect.blendMode !== void 0) shadow.blendMode = effect.blendMode;
122
+ if (effect.showShadowBehindNode !== void 0) shadow.showShadowBehindNode = effect.showShadowBehindNode;
123
+ return shadow;
52
124
  }
53
125
  /** `effects` takes structured effects; `shadow` appends its shadows and `blur` a layer blur. */
54
126
  function applyEffectOverrides(props, o) {
55
- if (Array.isArray(props.effects)) {
56
- const effects = props.effects.filter(isEffect).map((effect) => structuredClone(effect));
57
- if (effects.length > 0) o.effects = effects;
127
+ if (Array.isArray(props.effects) && props.effects.length > 0) {
128
+ const effects = parseScriptInput("effects", effectsSchema, props.effects).map(forNormalize);
129
+ o.effects = normalizeEffects(effects);
58
130
  }
59
131
  const shadows = typeof props.shadow === "string" ? parseCSSShadows(props.shadow) : [];
60
132
  if (shadows.length > 0) o.effects = [...o.effects ?? [], ...shadows];
@@ -962,8 +1034,79 @@ function transformDesignJSXProgram(source) {
962
1034
  const REKA_ALIASES = Object.entries(REKA_ELEMENTS).map(([namespace, parts]) => `const ${namespace} = ${JSON.stringify(Object.fromEntries(Object.keys(parts).map((part) => [part, `${namespace}.${part}`])))}`).join("\n");
963
1035
  /** `const solid = __helpers.solid`, …: every helper in the scope of the code. */
964
1036
  const HELPER_BINDINGS = DESIGN_JSX_HELPERS.map(({ name }) => `const ${name} = __helpers.${name}`).join("\n");
1037
+ /** End (exclusive) of the string that opens at `start`; JSX attribute strings have no escapes. */
1038
+ function stringEnd(source, start, escapes) {
1039
+ const quote = source[start];
1040
+ let i = start + 1;
1041
+ while (i < source.length && source[i] !== quote) i += escapes && source[i] === "\\" ? 2 : 1;
1042
+ return Math.min(i + 1, source.length);
1043
+ }
1044
+ /** End of the string or JS comment at `i`, copied as written, or -1 when none starts there. */
1045
+ function verbatimEnd(source, i, frame) {
1046
+ const char = source.charAt(i);
1047
+ if (frame === "open" || frame === "close") return /["']/.test(char) ? stringEnd(source, i, false) : -1;
1048
+ if (frame !== "code") return -1;
1049
+ if (/["'`]/.test(char)) return stringEnd(source, i, true);
1050
+ if (source.startsWith("//", i)) {
1051
+ const end = source.indexOf("\n", i);
1052
+ return end === -1 ? source.length : end;
1053
+ }
1054
+ if (source.startsWith("/*", i)) {
1055
+ const end = source.indexOf("*/", i + 2);
1056
+ return end === -1 ? source.length : end + 2;
1057
+ }
1058
+ return -1;
1059
+ }
1060
+ /** Whether the `<` just copied to `before` opens a tag rather than comparing, as in `a<b`. */
1061
+ function startsTag(source, next, frame, before) {
1062
+ const following = source.charAt(next);
1063
+ if (frame === "text") return /[A-Za-z_$/>]/.test(following);
1064
+ if (!/[A-Za-z_$>]/.test(following)) return false;
1065
+ const previous = before.slice(0, -1).trimEnd();
1066
+ return !/[\w$)\].]$/.test(previous) || /(?:^|[^\w$])return$/.test(previous);
1067
+ }
1068
+ /** Enter or leave a frame after copying the character just before `next`. */
1069
+ function updateFrames(stack, source, next, out) {
1070
+ const char = source.charAt(next - 1);
1071
+ const frame = stack.at(-1) ?? "code";
1072
+ const inTag = frame === "open" || frame === "close";
1073
+ if (char === "{") stack.push("code");
1074
+ else if (char === "}" && frame === "code" && stack.length > 1) stack.pop();
1075
+ else if (char === "<" && !inTag && startsTag(source, next, frame, out)) stack.push(source.charAt(next) === "/" ? "close" : "open");
1076
+ else if (char === ">" && frame === "open") {
1077
+ if (source.charAt(next - 2) === "/") stack.pop();
1078
+ else stack[stack.length - 1] = "text";
1079
+ } else if (char === ">" && frame === "close") {
1080
+ stack.pop();
1081
+ if (stack.at(-1) === "text") stack.pop();
1082
+ }
1083
+ }
1084
+ /**
1085
+ * Remove `<!-- … -->` written in JSX text or between statements. Markup inside an attribute
1086
+ * string or a JS string literal is part of the value and stays.
1087
+ */
965
1088
  function stripHTMLComments(jsxString) {
966
- return jsxString.replace(/<!--[\s\S]*?-->/g, "");
1089
+ const stack = ["code"];
1090
+ let out = "";
1091
+ let i = 0;
1092
+ while (i < jsxString.length) {
1093
+ const frame = stack.at(-1) ?? "code";
1094
+ if (frame !== "open" && frame !== "close" && jsxString.startsWith("<!--", i)) {
1095
+ const end = jsxString.indexOf("-->", i + 4);
1096
+ i = end === -1 ? jsxString.length : end + 3;
1097
+ continue;
1098
+ }
1099
+ const end = verbatimEnd(jsxString, i, frame);
1100
+ if (end !== -1) {
1101
+ out += jsxString.slice(i, end);
1102
+ i = end;
1103
+ continue;
1104
+ }
1105
+ out += jsxString.charAt(i);
1106
+ i++;
1107
+ updateFrames(stack, jsxString, i, out);
1108
+ }
1109
+ return out;
967
1110
  }
968
1111
  function unsupportedPropWarnings(graph, tree) {
969
1112
  const warnings = [];