@pixldocs/canvas-renderer 0.3.9 → 0.3.10

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/index.js CHANGED
@@ -8556,6 +8556,47 @@ const PreviewCanvas$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
8556
8556
  __proto__: null,
8557
8557
  PreviewCanvas
8558
8558
  }, Symbol.toStringTag, { value: "Module" }));
8559
+ function applyThemeToConfig(config, themeOverrides) {
8560
+ var _a, _b, _c;
8561
+ if (!themeOverrides || Object.keys(themeOverrides).length === 0) return config;
8562
+ const cloned = JSON.parse(JSON.stringify(config));
8563
+ if ((_a = cloned.themeConfig) == null ? void 0 : _a.variables) {
8564
+ for (const [key, value] of Object.entries(themeOverrides)) {
8565
+ if (cloned.themeConfig.variables[key]) {
8566
+ cloned.themeConfig.variables[key].value = value;
8567
+ }
8568
+ }
8569
+ }
8570
+ const varMap = /* @__PURE__ */ new Map();
8571
+ if ((_b = cloned.themeConfig) == null ? void 0 : _b.variables) {
8572
+ for (const [key, def] of Object.entries(cloned.themeConfig.variables)) {
8573
+ varMap.set(key, themeOverrides[key] ?? def.value);
8574
+ }
8575
+ }
8576
+ function walkAndApply(nodes) {
8577
+ if (!nodes) return;
8578
+ for (const node of nodes) {
8579
+ const bindings = node.themeBindings;
8580
+ if (bindings) {
8581
+ for (const [prop, varName] of Object.entries(bindings)) {
8582
+ const value = varMap.get(varName);
8583
+ if (value !== void 0) {
8584
+ node[prop] = value;
8585
+ }
8586
+ }
8587
+ }
8588
+ if (node.children) walkAndApply(node.children);
8589
+ }
8590
+ }
8591
+ for (const page of cloned.pages || []) {
8592
+ const bgBinding = (_c = page.themeBindings) == null ? void 0 : _c.backgroundColor;
8593
+ if (bgBinding && varMap.has(bgBinding) && page.settings) {
8594
+ page.settings.backgroundColor = varMap.get(bgBinding);
8595
+ }
8596
+ walkAndApply(page.children || []);
8597
+ }
8598
+ return cloned;
8599
+ }
8559
8600
  function mapFormDefFieldType(t) {
8560
8601
  if (["text", "email", "tel", "textarea", "date", "url", "number", "toggle", "color", "image"].includes(t)) return t;
8561
8602
  if (t === "currency") return "number";
@@ -10036,7 +10077,7 @@ async function resolveTemplateData(options) {
10036
10077
  return { config, templateName: template.name || "Untitled", templateId };
10037
10078
  }
10038
10079
  async function resolveFromForm(options) {
10039
- var _a, _b, _c, _d, _e;
10080
+ var _a, _b, _c, _d, _e, _f, _g;
10040
10081
  const { templateId, formSchemaId, sectionState, themeId, supabaseUrl, supabaseAnonKey } = options;
10041
10082
  const [templateRow, formSchemaRow, defaultForm] = await Promise.all([
10042
10083
  fetchRow(supabaseUrl, supabaseAnonKey, "templates", templateId),
@@ -10161,13 +10202,21 @@ async function resolveFromForm(options) {
10161
10202
  Object.keys(repeatableNestedEntryCounts).length > 0 ? repeatableNestedEntryCounts : void 0,
10162
10203
  displayFormatMap.size > 0 ? displayFormatMap : void 0
10163
10204
  );
10164
- if (themeId && themeId !== "default" && templateConfig.themeConfig) {
10205
+ if ((_d = resolvedConfig.themeConfig) == null ? void 0 : _d.variables) {
10206
+ const baseOverrides = {};
10207
+ for (const [key, def] of Object.entries(resolvedConfig.themeConfig.variables)) {
10208
+ baseOverrides[key] = def.value;
10209
+ }
10210
+ resolvedConfig = applyThemeToConfig(resolvedConfig, baseOverrides);
10211
+ }
10212
+ if (templateConfig.themeConfig) {
10165
10213
  const tc = templateConfig.themeConfig;
10166
- const variant = (_d = tc.variants) == null ? void 0 : _d.find((v) => v.id === themeId);
10167
- if (variant && tc.properties) {
10214
+ const variant = themeId && themeId !== "default" ? (_e = tc.variants) == null ? void 0 : _e.find((v) => v.id === themeId) : null;
10215
+ const shouldApplyDefaults = !variant && ((_f = tc.properties) == null ? void 0 : _f.length);
10216
+ if ((variant || shouldApplyDefaults) && tc.properties) {
10168
10217
  const themed = JSON.parse(JSON.stringify(resolvedConfig));
10169
10218
  for (const prop of tc.properties) {
10170
- const value = (_e = variant.values) == null ? void 0 : _e[prop.id];
10219
+ const value = variant ? (_g = variant.values) == null ? void 0 : _g[prop.id] : prop.defaultValue;
10171
10220
  if (value === void 0) continue;
10172
10221
  if (prop.targetProperty === "backgroundColor" && prop.elementId === "__pageBackground__") {
10173
10222
  themed.pages.forEach((p) => {
@@ -10373,20 +10422,30 @@ async function loadGoogleFontCSS(rawFontFamily) {
10373
10422
  loadingPromises.delete(fontFamily);
10374
10423
  }
10375
10424
  function collectFontsFromConfig(config) {
10425
+ var _a;
10376
10426
  const fonts = /* @__PURE__ */ new Set();
10377
10427
  fonts.add("Open Sans");
10378
10428
  function walk(nodes) {
10379
- var _a;
10429
+ var _a2;
10380
10430
  if (!nodes) return;
10381
10431
  for (const node of nodes) {
10382
- if (node.fontFamily) fonts.add(node.fontFamily);
10383
- if ((_a = node.smartProps) == null ? void 0 : _a.fontFamily) fonts.add(node.smartProps.fontFamily);
10432
+ if (node.fontFamily) fonts.add(normalizeFontFamily(node.fontFamily));
10433
+ if ((_a2 = node.smartProps) == null ? void 0 : _a2.fontFamily) fonts.add(normalizeFontFamily(node.smartProps.fontFamily));
10384
10434
  if (node.children) walk(node.children);
10385
10435
  }
10386
10436
  }
10387
10437
  for (const page of config.pages || []) {
10388
10438
  walk(page.children || []);
10389
10439
  }
10440
+ if ((_a = config.themeConfig) == null ? void 0 : _a.variables) {
10441
+ for (const def of Object.values(config.themeConfig.variables)) {
10442
+ if (def.value && typeof def.value === "string" && !def.value.startsWith("#") && !def.value.startsWith("rgb")) {
10443
+ if (def.label && /font/i.test(def.label)) {
10444
+ fonts.add(normalizeFontFamily(def.value));
10445
+ }
10446
+ }
10447
+ }
10448
+ }
10390
10449
  return fonts;
10391
10450
  }
10392
10451
  class PixldocsRenderer {
@@ -10634,47 +10693,6 @@ class PixldocsRenderer {
10634
10693
  });
10635
10694
  }
10636
10695
  }
10637
- function applyThemeToConfig(config, themeOverrides) {
10638
- var _a, _b, _c;
10639
- if (!themeOverrides || Object.keys(themeOverrides).length === 0) return config;
10640
- const cloned = JSON.parse(JSON.stringify(config));
10641
- if ((_a = cloned.themeConfig) == null ? void 0 : _a.variables) {
10642
- for (const [key, value] of Object.entries(themeOverrides)) {
10643
- if (cloned.themeConfig.variables[key]) {
10644
- cloned.themeConfig.variables[key].value = value;
10645
- }
10646
- }
10647
- }
10648
- const varMap = /* @__PURE__ */ new Map();
10649
- if ((_b = cloned.themeConfig) == null ? void 0 : _b.variables) {
10650
- for (const [key, def] of Object.entries(cloned.themeConfig.variables)) {
10651
- varMap.set(key, themeOverrides[key] ?? def.value);
10652
- }
10653
- }
10654
- function walkAndApply(nodes) {
10655
- if (!nodes) return;
10656
- for (const node of nodes) {
10657
- const bindings = node.themeBindings;
10658
- if (bindings) {
10659
- for (const [prop, varName] of Object.entries(bindings)) {
10660
- const value = varMap.get(varName);
10661
- if (value !== void 0) {
10662
- node[prop] = value;
10663
- }
10664
- }
10665
- }
10666
- if (node.children) walkAndApply(node.children);
10667
- }
10668
- }
10669
- for (const page of cloned.pages || []) {
10670
- const bgBinding = (_c = page.themeBindings) == null ? void 0 : _c.backgroundColor;
10671
- if (bgBinding && varMap.has(bgBinding) && page.settings) {
10672
- page.settings.backgroundColor = varMap.get(bgBinding);
10673
- }
10674
- walkAndApply(page.children || []);
10675
- }
10676
- return cloned;
10677
- }
10678
10696
  export {
10679
10697
  PixldocsPreview,
10680
10698
  PixldocsRenderer,