@snowcone-app/ui 0.12.1 → 0.13.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.
- package/CHANGELOG.md +17 -0
- package/dist/chunk-GY6TC6M6.js +86 -0
- package/dist/chunk-GY6TC6M6.js.map +1 -0
- package/dist/index.cjs +90 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -3
- package/dist/index.d.ts +32 -3
- package/dist/index.js +61 -13
- package/dist/index.js.map +1 -1
- package/dist/personalization/bridge.cjs.map +1 -1
- package/dist/personalization/bridge.d.cts +1 -1
- package/dist/personalization/bridge.d.ts +1 -1
- package/dist/personalization/bridge.js +1 -1
- package/dist/{types-CNJT8FUd.d.cts → types-BObPMwP5.d.cts} +17 -1
- package/dist/{types-CNJT8FUd.d.ts → types-BObPMwP5.d.ts} +17 -1
- package/package.json +2 -2
- package/src/personalization/PersonalizationInputs.tsx +2 -0
- package/src/personalization/PersonalizationProvider.tsx +52 -13
- package/src/personalization/__tests__/fit.test.ts +75 -0
- package/src/personalization/index.ts +2 -1
- package/src/personalization/types.ts +16 -0
- package/src/personalization/utils.ts +68 -0
- package/dist/chunk-WDOT4BST.js +0 -50
- package/dist/chunk-WDOT4BST.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.13.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#6680](https://github.com/snowcone-app/snowcone-monorepo/pull/6680) [`608bc6c`](https://github.com/snowcone-app/snowcone-monorepo/commit/608bc6c885e21739bd1e0fed30ed711bad6d0cbe) Thanks [@kevinsproles](https://github.com/kevinsproles)! - Template text fields: `fit: "shrink"` sets the buyer's text at the element's own size and makes it smaller (never larger)
|
|
8
|
+
until one line fits the element's box width, so a long name never runs past the design's edge; `sample: true` marks the
|
|
9
|
+
element's own text as a sample that shows only until the buyer types into any field, after which an empty field draws
|
|
10
|
+
nothing. The ui personalization inputs take the field's `maxLength` and carry its label as their accessible name; the
|
|
11
|
+
fit helpers (`shrinkFontSizeToFit`, `measureTextLine`, `anyTyped`, `textValueFor`) are exported for custom inputs.
|
|
12
|
+
|
|
13
|
+
## 0.12.2
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies [[`4bbd637`](https://github.com/snowcone-app/snowcone-monorepo/commit/4bbd637cf8c446340da00983ee900f36084a0455)]:
|
|
18
|
+
- @snowcone-app/sdk@0.33.4
|
|
19
|
+
|
|
3
20
|
## 0.12.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/personalization/utils.ts
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
function normalizeHex(color) {
|
|
4
|
+
let hex = color.trim().toUpperCase();
|
|
5
|
+
if (hex.length === 4 && hex.startsWith("#")) {
|
|
6
|
+
hex = "#" + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
|
|
7
|
+
}
|
|
8
|
+
return hex;
|
|
9
|
+
}
|
|
10
|
+
function isValidImageUrl(url) {
|
|
11
|
+
if (!url || url.length < 10) return false;
|
|
12
|
+
try {
|
|
13
|
+
const parsed = new URL(url);
|
|
14
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return false;
|
|
15
|
+
if (!parsed.hostname.includes(".")) return false;
|
|
16
|
+
if (parsed.pathname.length <= 1) return false;
|
|
17
|
+
return true;
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function anyTyped(fields, values) {
|
|
23
|
+
return fields.some((f) => f.type === "text" && typeof values[f.name] === "string" && values[f.name].trim().length > 0);
|
|
24
|
+
}
|
|
25
|
+
function textValueFor(field, values, typed = false) {
|
|
26
|
+
const v = values[field.name] ?? "";
|
|
27
|
+
if (field.sample) return typed ? v : null;
|
|
28
|
+
return v;
|
|
29
|
+
}
|
|
30
|
+
function canvasFontString(style, fontSize = style.fontSize) {
|
|
31
|
+
return [style.italic ? "italic" : "", style.bold ? "bold" : "", `${fontSize}px`, style.fontFamily].filter(Boolean).join(" ");
|
|
32
|
+
}
|
|
33
|
+
var FIT_MARGIN = 0.03;
|
|
34
|
+
function shrinkFontSizeToFit(text, style, maxWidth, measure) {
|
|
35
|
+
const limit = maxWidth * (1 - FIT_MARGIN);
|
|
36
|
+
if (!text || !(limit > 0)) return style.fontSize;
|
|
37
|
+
const at = measure(text, style.fontSize);
|
|
38
|
+
if (at <= limit) return style.fontSize;
|
|
39
|
+
const spacing = (style.letterSpacing ?? 0) * text.length;
|
|
40
|
+
let size = Math.max(1, style.fontSize * Math.max(1, limit - spacing) / Math.max(1, at - spacing));
|
|
41
|
+
for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;
|
|
42
|
+
return Math.max(1, Math.floor(size * 2) / 2);
|
|
43
|
+
}
|
|
44
|
+
var measureCtx = null;
|
|
45
|
+
function measureTextLine(text, style, fontSize = style.fontSize) {
|
|
46
|
+
if (typeof document === "undefined") return 0;
|
|
47
|
+
measureCtx ??= document.createElement("canvas").getContext("2d");
|
|
48
|
+
if (!measureCtx) return 0;
|
|
49
|
+
measureCtx.font = canvasFontString(style, fontSize);
|
|
50
|
+
return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;
|
|
51
|
+
}
|
|
52
|
+
function useDebouncedValue(value, delay) {
|
|
53
|
+
const [debounced, setDebounced] = useState(value);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const timer = setTimeout(() => setDebounced(value), delay);
|
|
56
|
+
return () => clearTimeout(timer);
|
|
57
|
+
}, [value, delay]);
|
|
58
|
+
return debounced;
|
|
59
|
+
}
|
|
60
|
+
function scrollInputAboveKeyboard(input) {
|
|
61
|
+
const position = () => {
|
|
62
|
+
const rect = input.getBoundingClientRect();
|
|
63
|
+
const visibleBottom = window.visualViewport?.height || window.innerHeight * 0.5;
|
|
64
|
+
const target = visibleBottom - rect.height - 20;
|
|
65
|
+
const delta = rect.top - target;
|
|
66
|
+
if (Math.abs(delta) > 10) {
|
|
67
|
+
window.scrollBy({ top: delta, behavior: "smooth" });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
setTimeout(position, 400);
|
|
71
|
+
window.visualViewport?.addEventListener("resize", position, { once: true });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {
|
|
75
|
+
normalizeHex,
|
|
76
|
+
isValidImageUrl,
|
|
77
|
+
anyTyped,
|
|
78
|
+
textValueFor,
|
|
79
|
+
canvasFontString,
|
|
80
|
+
FIT_MARGIN,
|
|
81
|
+
shrinkFontSizeToFit,
|
|
82
|
+
measureTextLine,
|
|
83
|
+
useDebouncedValue,
|
|
84
|
+
scrollInputAboveKeyboard
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=chunk-GY6TC6M6.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/personalization/utils.ts"],"sourcesContent":["\"use client\";\n\n/**\n * Personalization Utilities\n */\n\nimport { useEffect, useState } from \"react\";\nimport type { PersonalizationField, PersonalizationTextField, PersonalizationValues } from \"./types\";\n\n/** Normalize any hex color to uppercase 6-digit form (#RGB → #RRGGBB) */\nexport function normalizeHex(color: string): string {\n let hex = color.trim().toUpperCase();\n if (hex.length === 4 && hex.startsWith(\"#\")) {\n hex = \"#\" + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];\n }\n return hex;\n}\n\n/** Check if a string looks like a complete, loadable URL */\nexport function isValidImageUrl(url: string): boolean {\n if (!url || url.length < 10) return false;\n try {\n const parsed = new URL(url);\n if (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\") return false;\n if (!parsed.hostname.includes(\".\")) return false;\n if (parsed.pathname.length <= 1) return false;\n return true;\n } catch {\n return false;\n }\n}\n\n/** Has the buyer typed into any TEXT field of the design (any non-blank text)? */\nexport function anyTyped(fields: readonly PersonalizationField[], values: PersonalizationValues): boolean {\n return fields.some((f) => f.type === \"text\" && typeof values[f.name] === \"string\" && values[f.name].trim().length > 0);\n}\n\n/**\n * What a text field's elements draw: its box's text, or null = the element's own (designed) text. A `sample` field draws\n * its sample until anything is typed in any field, then exactly its box (empty = nothing). Any other field draws its\n * box's text (\"\" until typed into, which the binder leaves alone).\n */\nexport function textValueFor(field: PersonalizationTextField, values: PersonalizationValues, typed = false): string | null {\n const v = values[field.name] ?? \"\";\n if (field.sample) return typed ? v : null;\n return v;\n}\n\n/** The font a text element draws with: the canvas's own CSS font shorthand (canvas TextMetrics buildFontString). */\nexport interface TextLineStyle {\n fontSize: number;\n fontFamily: string;\n bold?: boolean;\n italic?: boolean;\n /** Extra advance after each character, px (canvas letterSpacing). */\n letterSpacing?: number;\n}\n\n/** The canvas font shorthand for a style at a size, exactly as the canvas builds it (\"italic bold 72px Oswald\"). */\nexport function canvasFontString(style: TextLineStyle, fontSize = style.fontSize): string {\n return [style.italic ? \"italic\" : \"\", style.bold ? \"bold\" : \"\", `${fontSize}px`, style.fontFamily].filter(Boolean).join(\" \");\n}\n\n/** A share of the box kept free when fitting: measurement differs by a hair between renderers (browser, server raster). */\nexport const FIT_MARGIN = 0.03;\n\n/**\n * The font size one line of `text` is drawn at to fit `maxWidth`: `style.fontSize` when it already fits, else the\n * largest smaller size that does (text width is linear in the font size, plus a fixed letter spacing per character).\n * Never larger than the style's size; never below 1 px. `measure(text, fontSize)` is the line's width at a size.\n */\nexport function shrinkFontSizeToFit(\n text: string,\n style: TextLineStyle,\n maxWidth: number,\n measure: (text: string, fontSize: number) => number,\n): number {\n const limit = maxWidth * (1 - FIT_MARGIN);\n if (!text || !(limit > 0)) return style.fontSize;\n const at = measure(text, style.fontSize);\n if (at <= limit) return style.fontSize;\n const spacing = (style.letterSpacing ?? 0) * text.length;\n let size = Math.max(1, (style.fontSize * Math.max(1, limit - spacing)) / Math.max(1, at - spacing));\n // measurement rounding (hinting, kerning) can leave a hair over: step down until it truly fits\n for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;\n return Math.max(1, Math.floor(size * 2) / 2);\n}\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\n/** One line's width in a browser, with the canvas's own font shorthand (0 where no 2D canvas exists, e.g. on a server). */\nexport function measureTextLine(text: string, style: TextLineStyle, fontSize = style.fontSize): number {\n if (typeof document === \"undefined\") return 0;\n measureCtx ??= document.createElement(\"canvas\").getContext(\"2d\");\n if (!measureCtx) return 0;\n measureCtx.font = canvasFontString(style, fontSize);\n return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;\n}\n\n/** Debounce a string value — only emits after `delay` ms of inactivity */\nexport function useDebouncedValue(value: string, delay: number): string {\n const [debounced, setDebounced] = useState(value);\n useEffect(() => {\n const timer = setTimeout(() => setDebounced(value), delay);\n return () => clearTimeout(timer);\n }, [value, delay]);\n return debounced;\n}\n\n/**\n * Scroll an input to sit just above the iOS keyboard after it appears.\n * Uses visualViewport.height to determine the visible area above the keyboard.\n */\nexport function scrollInputAboveKeyboard(input: HTMLElement) {\n const position = () => {\n const rect = input.getBoundingClientRect();\n const visibleBottom =\n window.visualViewport?.height || window.innerHeight * 0.5;\n const target = visibleBottom - rect.height - 20;\n const delta = rect.top - target;\n if (Math.abs(delta) > 10) {\n window.scrollBy({ top: delta, behavior: \"smooth\" });\n }\n };\n setTimeout(position, 400);\n window.visualViewport?.addEventListener(\"resize\", position, { once: true });\n}\n"],"mappings":";AAMA,SAAS,WAAW,gBAAgB;AAI7B,SAAS,aAAa,OAAuB;AAClD,MAAI,MAAM,MAAM,KAAK,EAAE,YAAY;AACnC,MAAI,IAAI,WAAW,KAAK,IAAI,WAAW,GAAG,GAAG;AAC3C,UAAM,MAAM,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,CAA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-224,6 +224,7 @@ __export(src_exports, {
|
|
|
224
224
|
FACET_SECTIONS: () => FACET_SECTIONS,
|
|
225
225
|
FIRST_VIEW_MARGIN: () => FIRST_VIEW_MARGIN,
|
|
226
226
|
FIRST_VIEW_PICTURE_ASPECT: () => FIRST_VIEW_PICTURE_ASPECT,
|
|
227
|
+
FIT_MARGIN: () => FIT_MARGIN,
|
|
227
228
|
Fieldset: () => Fieldset,
|
|
228
229
|
Filters: () => Filters,
|
|
229
230
|
FiltersButton: () => FiltersButton,
|
|
@@ -345,11 +346,13 @@ __export(src_exports, {
|
|
|
345
346
|
UiRouterProvider: () => UiRouterProvider,
|
|
346
347
|
VISIBILITY_THRESHOLDS: () => VISIBILITY_THRESHOLDS,
|
|
347
348
|
ZoomOverlay: () => ZoomOverlay,
|
|
349
|
+
anyTyped: () => anyTyped,
|
|
348
350
|
badgeVariants: () => badgeVariants,
|
|
349
351
|
blockingSessionError: () => blockingSessionError,
|
|
350
352
|
brandAssets: () => brandAssets,
|
|
351
353
|
buttonVariants: () => buttonVariants,
|
|
352
354
|
canvasExportService: () => canvasExportService,
|
|
355
|
+
canvasFontString: () => canvasFontString,
|
|
353
356
|
checkComponentStyles: () => checkComponentStyles,
|
|
354
357
|
checkTailwindConfig: () => checkTailwindConfig,
|
|
355
358
|
checkThemeTokens: () => checkThemeTokens,
|
|
@@ -376,6 +379,7 @@ __export(src_exports, {
|
|
|
376
379
|
linkVariants: () => linkVariants,
|
|
377
380
|
linkedPlacementsLabel: () => linkedPlacementsLabel,
|
|
378
381
|
listProducts: () => import_sdk15.listProducts,
|
|
382
|
+
measureTextLine: () => measureTextLine,
|
|
379
383
|
normalizeHex: () => normalizeHex,
|
|
380
384
|
observe: () => observe,
|
|
381
385
|
pictureRenderState: () => pictureRenderState,
|
|
@@ -383,10 +387,12 @@ __export(src_exports, {
|
|
|
383
387
|
runConfigChecks: () => runConfigChecks,
|
|
384
388
|
scrollInputAboveKeyboard: () => scrollInputAboveKeyboard,
|
|
385
389
|
searchClient: () => searchClient,
|
|
390
|
+
shrinkFontSizeToFit: () => shrinkFontSizeToFit,
|
|
386
391
|
slideFirstViewFields: () => slideFirstViewFields,
|
|
387
392
|
slideFirstViewProps: () => slideFirstViewProps,
|
|
388
393
|
spinnerVariants: () => spinnerVariants,
|
|
389
394
|
surfaceVariants: () => surfaceVariants,
|
|
395
|
+
textValueFor: () => textValueFor,
|
|
390
396
|
useArtworkAlignment: () => useArtworkAlignment,
|
|
391
397
|
useBrand: () => useBrand,
|
|
392
398
|
useCanvasContext: () => useCanvasContext,
|
|
@@ -16973,6 +16979,36 @@ function isValidImageUrl(url) {
|
|
|
16973
16979
|
return false;
|
|
16974
16980
|
}
|
|
16975
16981
|
}
|
|
16982
|
+
function anyTyped(fields, values) {
|
|
16983
|
+
return fields.some((f) => f.type === "text" && typeof values[f.name] === "string" && values[f.name].trim().length > 0);
|
|
16984
|
+
}
|
|
16985
|
+
function textValueFor(field, values, typed = false) {
|
|
16986
|
+
const v = values[field.name] ?? "";
|
|
16987
|
+
if (field.sample) return typed ? v : null;
|
|
16988
|
+
return v;
|
|
16989
|
+
}
|
|
16990
|
+
function canvasFontString(style, fontSize = style.fontSize) {
|
|
16991
|
+
return [style.italic ? "italic" : "", style.bold ? "bold" : "", `${fontSize}px`, style.fontFamily].filter(Boolean).join(" ");
|
|
16992
|
+
}
|
|
16993
|
+
var FIT_MARGIN = 0.03;
|
|
16994
|
+
function shrinkFontSizeToFit(text, style, maxWidth, measure) {
|
|
16995
|
+
const limit = maxWidth * (1 - FIT_MARGIN);
|
|
16996
|
+
if (!text || !(limit > 0)) return style.fontSize;
|
|
16997
|
+
const at = measure(text, style.fontSize);
|
|
16998
|
+
if (at <= limit) return style.fontSize;
|
|
16999
|
+
const spacing = (style.letterSpacing ?? 0) * text.length;
|
|
17000
|
+
let size = Math.max(1, style.fontSize * Math.max(1, limit - spacing) / Math.max(1, at - spacing));
|
|
17001
|
+
for (let i = 0; i < 40 && size > 1 && measure(text, size) > limit; i++) size *= 0.98;
|
|
17002
|
+
return Math.max(1, Math.floor(size * 2) / 2);
|
|
17003
|
+
}
|
|
17004
|
+
var measureCtx = null;
|
|
17005
|
+
function measureTextLine(text, style, fontSize = style.fontSize) {
|
|
17006
|
+
if (typeof document === "undefined") return 0;
|
|
17007
|
+
measureCtx ??= document.createElement("canvas").getContext("2d");
|
|
17008
|
+
if (!measureCtx) return 0;
|
|
17009
|
+
measureCtx.font = canvasFontString(style, fontSize);
|
|
17010
|
+
return measureCtx.measureText(text).width + (style.letterSpacing ?? 0) * text.length;
|
|
17011
|
+
}
|
|
16976
17012
|
function useDebouncedValue(value, delay) {
|
|
16977
17013
|
const [debounced, setDebounced] = (0, import_react70.useState)(value);
|
|
16978
17014
|
(0, import_react70.useEffect)(() => {
|
|
@@ -16999,7 +17035,7 @@ function scrollInputAboveKeyboard(input) {
|
|
|
16999
17035
|
var import_jsx_runtime91 = require("react/jsx-runtime");
|
|
17000
17036
|
function createBridgeFromCanvas(canvasModule) {
|
|
17001
17037
|
const { useEditor, useCommands, useImageBinding } = canvasModule;
|
|
17002
|
-
function TextBinder({ name, value }) {
|
|
17038
|
+
function TextBinder({ name, value, fit, sample }) {
|
|
17003
17039
|
const { elements } = useEditor();
|
|
17004
17040
|
const { executeElementUpdate } = useCommands();
|
|
17005
17041
|
const elementsRef = import_react71.default.useRef(elements);
|
|
@@ -17007,19 +17043,52 @@ function createBridgeFromCanvas(canvasModule) {
|
|
|
17007
17043
|
const executeRef = import_react71.default.useRef(executeElementUpdate);
|
|
17008
17044
|
executeRef.current = executeElementUpdate;
|
|
17009
17045
|
const hasUserInput = import_react71.default.useRef(false);
|
|
17046
|
+
const designed = import_react71.default.useRef(/* @__PURE__ */ new Map());
|
|
17010
17047
|
import_react71.default.useEffect(() => {
|
|
17011
|
-
if (value
|
|
17012
|
-
if (value === "" && !hasUserInput.current) return;
|
|
17048
|
+
if (value === null && !hasUserInput.current) return;
|
|
17049
|
+
if (value === "" && !hasUserInput.current && !sample) return;
|
|
17013
17050
|
hasUserInput.current = true;
|
|
17014
|
-
|
|
17015
|
-
|
|
17016
|
-
|
|
17017
|
-
if (!(
|
|
17018
|
-
|
|
17019
|
-
|
|
17020
|
-
|
|
17051
|
+
let cancelled = false;
|
|
17052
|
+
const bound = elementsRef.current.filter((el) => el.name === name && typeof el.setText === "function");
|
|
17053
|
+
for (const el of bound) {
|
|
17054
|
+
if (!designed.current.has(el.id)) designed.current.set(el.id, { text: typeof el.getText === "function" ? el.getText() : el.text, fontSize: el.fontSize });
|
|
17055
|
+
}
|
|
17056
|
+
const apply = (sizes) => {
|
|
17057
|
+
if (cancelled) return;
|
|
17058
|
+
const exec = executeRef.current;
|
|
17059
|
+
for (const el of elementsRef.current) {
|
|
17060
|
+
if (el.name !== name) continue;
|
|
17061
|
+
if (!("setText" in el) || typeof el.setText !== "function") continue;
|
|
17062
|
+
const own = designed.current.get(el.id);
|
|
17063
|
+
const cloned = el.clone();
|
|
17064
|
+
cloned.setText(value ?? own?.text ?? "");
|
|
17065
|
+
const size = value === null ? own?.fontSize : sizes.get(el.id);
|
|
17066
|
+
if (size !== void 0) cloned.fontSize = size;
|
|
17067
|
+
exec(el, cloned);
|
|
17068
|
+
}
|
|
17069
|
+
};
|
|
17070
|
+
if (fit !== "shrink" || value === null) {
|
|
17071
|
+
apply(/* @__PURE__ */ new Map());
|
|
17072
|
+
return () => {
|
|
17073
|
+
cancelled = true;
|
|
17074
|
+
};
|
|
17021
17075
|
}
|
|
17022
|
-
|
|
17076
|
+
const styled = bound.map((el) => {
|
|
17077
|
+
const style = { fontSize: designed.current.get(el.id).fontSize, fontFamily: el.fontFamily, bold: el.bold, italic: el.italic, letterSpacing: el.letterSpacing };
|
|
17078
|
+
return { id: el.id, style, boxWidth: Number(el.transformData?.width) || 0 };
|
|
17079
|
+
});
|
|
17080
|
+
const fonts = typeof document !== "undefined" && document.fonts ? Promise.all(styled.map((st) => document.fonts.load(canvasFontString(st.style)).catch(() => []))) : Promise.resolve([]);
|
|
17081
|
+
void fonts.then(() => {
|
|
17082
|
+
const sizes = /* @__PURE__ */ new Map();
|
|
17083
|
+
for (const st of styled) {
|
|
17084
|
+
sizes.set(st.id, st.boxWidth > 0 ? shrinkFontSizeToFit(value, st.style, st.boxWidth, (t, size) => measureTextLine(t, st.style, size)) : st.style.fontSize);
|
|
17085
|
+
}
|
|
17086
|
+
apply(sizes);
|
|
17087
|
+
});
|
|
17088
|
+
return () => {
|
|
17089
|
+
cancelled = true;
|
|
17090
|
+
};
|
|
17091
|
+
}, [value, name, fit, sample, elements.length]);
|
|
17023
17092
|
return null;
|
|
17024
17093
|
}
|
|
17025
17094
|
function ColorBinder({ originalColor, newColor }) {
|
|
@@ -17084,9 +17153,10 @@ function createBridgeFromCanvas(canvasModule) {
|
|
|
17084
17153
|
return null;
|
|
17085
17154
|
}
|
|
17086
17155
|
return function PersonalizationBridge({ fields = [], values = {} } = {}) {
|
|
17156
|
+
const typed = anyTyped(fields, values);
|
|
17087
17157
|
return /* @__PURE__ */ (0, import_jsx_runtime91.jsx)(import_jsx_runtime91.Fragment, { children: fields.map((field) => {
|
|
17088
17158
|
if (field.type === "text") {
|
|
17089
|
-
return /* @__PURE__ */ (0, import_jsx_runtime91.jsx)(TextBinder, { name: field.name, value: values
|
|
17159
|
+
return /* @__PURE__ */ (0, import_jsx_runtime91.jsx)(TextBinder, { name: field.name, value: textValueFor(field, values, typed), fit: field.fit, sample: field.sample }, `text-${field.name}`);
|
|
17090
17160
|
}
|
|
17091
17161
|
if (field.type === "color") {
|
|
17092
17162
|
return /* @__PURE__ */ (0, import_jsx_runtime91.jsx)(ColorBinder, { originalColor: field.color, newColor: values[field.color] ?? field.color }, `color-${field.color}`);
|
|
@@ -17304,7 +17374,9 @@ function PersonalizationInputs({
|
|
|
17304
17374
|
"input",
|
|
17305
17375
|
{
|
|
17306
17376
|
type: "text",
|
|
17377
|
+
"aria-label": field.label,
|
|
17307
17378
|
placeholder: field.placeholder || field.label,
|
|
17379
|
+
maxLength: field.maxLength,
|
|
17308
17380
|
value: personValues[field.name] ?? "",
|
|
17309
17381
|
onChange: (e) => updateField(field.name, e.target.value),
|
|
17310
17382
|
onFocus: handleFocus,
|
|
@@ -17505,6 +17577,7 @@ function usePersonalizationShimmer(containerRef, safetyTimeoutMs = 1e4) {
|
|
|
17505
17577
|
FACET_SECTIONS,
|
|
17506
17578
|
FIRST_VIEW_MARGIN,
|
|
17507
17579
|
FIRST_VIEW_PICTURE_ASPECT,
|
|
17580
|
+
FIT_MARGIN,
|
|
17508
17581
|
Fieldset,
|
|
17509
17582
|
Filters,
|
|
17510
17583
|
FiltersButton,
|
|
@@ -17626,11 +17699,13 @@ function usePersonalizationShimmer(containerRef, safetyTimeoutMs = 1e4) {
|
|
|
17626
17699
|
UiRouterProvider,
|
|
17627
17700
|
VISIBILITY_THRESHOLDS,
|
|
17628
17701
|
ZoomOverlay,
|
|
17702
|
+
anyTyped,
|
|
17629
17703
|
badgeVariants,
|
|
17630
17704
|
blockingSessionError,
|
|
17631
17705
|
brandAssets,
|
|
17632
17706
|
buttonVariants,
|
|
17633
17707
|
canvasExportService,
|
|
17708
|
+
canvasFontString,
|
|
17634
17709
|
checkComponentStyles,
|
|
17635
17710
|
checkTailwindConfig,
|
|
17636
17711
|
checkThemeTokens,
|
|
@@ -17657,6 +17732,7 @@ function usePersonalizationShimmer(containerRef, safetyTimeoutMs = 1e4) {
|
|
|
17657
17732
|
linkVariants,
|
|
17658
17733
|
linkedPlacementsLabel,
|
|
17659
17734
|
listProducts,
|
|
17735
|
+
measureTextLine,
|
|
17660
17736
|
normalizeHex,
|
|
17661
17737
|
observe,
|
|
17662
17738
|
pictureRenderState,
|
|
@@ -17664,10 +17740,12 @@ function usePersonalizationShimmer(containerRef, safetyTimeoutMs = 1e4) {
|
|
|
17664
17740
|
runConfigChecks,
|
|
17665
17741
|
scrollInputAboveKeyboard,
|
|
17666
17742
|
searchClient,
|
|
17743
|
+
shrinkFontSizeToFit,
|
|
17667
17744
|
slideFirstViewFields,
|
|
17668
17745
|
slideFirstViewProps,
|
|
17669
17746
|
spinnerVariants,
|
|
17670
17747
|
surfaceVariants,
|
|
17748
|
+
textValueFor,
|
|
17671
17749
|
useArtworkAlignment,
|
|
17672
17750
|
useBrand,
|
|
17673
17751
|
useCanvasContext,
|