@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.19
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/LICENSE +21 -0
- package/README.md +128 -10
- package/dist/abi/canvas-live.d.ts +72 -0
- package/dist/abi/canvas-live.js +159 -0
- package/dist/abi/cart.d.ts +60 -0
- package/dist/abi/cart.js +396 -0
- package/dist/abi/convert.d.ts +360 -0
- package/dist/abi/convert.js +1330 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +213 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +264 -0
- package/dist/abi/page-data.js +746 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +46 -0
- package/dist/abi/wire.js +114 -0
- package/dist/build.d.ts +121 -0
- package/dist/build.js +401 -0
- package/dist/chunks.d.ts +23 -0
- package/dist/chunks.js +41 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +101 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +276 -0
- package/dist/headless/hooks.js +544 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +44 -0
- package/dist/headless/index.js +48 -0
- package/dist/headless/lazy-component.d.ts +72 -0
- package/dist/headless/lazy-component.js +225 -0
- package/dist/headless/lazy-section.d.ts +38 -0
- package/dist/headless/lazy-section.js +97 -0
- package/dist/headless/model.d.ts +73 -0
- package/dist/headless/model.js +115 -0
- package/dist/headless/money.d.ts +17 -0
- package/dist/headless/money.js +63 -0
- package/dist/headless/page.d.ts +45 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +187 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +198 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/tanqory-money.generated.d.ts +493 -0
- package/dist/headless/tanqory-money.generated.js +507 -0
- package/dist/headless/theme.d.ts +109 -0
- package/dist/headless/theme.js +62 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +380 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +158 -0
- package/dist/runtime/data/to-ui.d.ts +286 -0
- package/dist/runtime/data/to-ui.js +1009 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +250 -0
- package/dist/runtime/kit/cart.d.ts +222 -0
- package/dist/runtime/kit/cart.js +545 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +213 -0
- package/dist/runtime/kit/contract/content.js +119 -0
- package/dist/runtime/kit/contract/field-types.d.ts +360 -0
- package/dist/runtime/kit/contract/field-types.js +216 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +77 -0
- package/dist/runtime/kit/contract/validate.js +520 -0
- package/dist/runtime/kit/data.d.ts +732 -0
- package/dist/runtime/kit/data.js +1131 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/runtime/kit/dynamic-source.d.ts +118 -0
- package/dist/runtime/kit/dynamic-source.js +311 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/runtime/kit/storefront.d.ts +652 -0
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +155 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +199 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +243 -0
- package/dist/runtime/render/resolved.d.ts +47 -0
- package/dist/runtime/render/resolved.js +80 -0
- package/dist/runtime/render/section-actions.d.ts +67 -0
- package/dist/runtime/render/section-actions.js +205 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +96 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +118 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +18 -0
- package/dist/runtime/shell/RuntimeBridge.js +124 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/font-hooks.d.ts +7 -0
- package/dist/sdk/font-hooks.js +21 -0
- package/dist/sdk/fonts.d.ts +144 -0
- package/dist/sdk/fonts.js +241 -0
- package/dist/sdk/index.d.ts +32 -0
- package/dist/sdk/index.js +36 -0
- package/dist/sdk/motion-hooks.d.ts +23 -0
- package/dist/sdk/motion-hooks.js +46 -0
- package/dist/sdk/motion.d.ts +78 -0
- package/dist/sdk/motion.js +292 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +230 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +249 -0
- package/dist/sdk/schema.js +168 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +32 -0
- package/dist/sdk/store-lookup.js +48 -0
- package/dist/sdk/store.d.ts +164 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +45 -0
- package/dist/sdk/sx.js +117 -0
- package/dist/sdk/theme.d.ts +212 -0
- package/dist/sdk/theme.js +27 -0
- package/dist/sdk/types.d.ts +789 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +112 -0
- package/dist/server.js +589 -0
- package/dist/vite-chunks.d.ts +10 -0
- package/dist/vite-chunks.js +44 -0
- package/docs/headless-api.md +250 -0
- package/package.json +72 -26
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/data.d.ts +0 -357
- package/dist/types/dynamic-source.d.ts +0 -84
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links that come from settings (theme settings, merchant-edited copy) are
|
|
3
|
+
* rendered as `href`s on the store's own origin. React 18 still renders a
|
|
4
|
+
* `javascript:` href, so every such value is checked here at render — on the
|
|
5
|
+
* live storefront, not only in the editor.
|
|
6
|
+
*
|
|
7
|
+
* Pure; unit-tested in app/platform/safe-href.node.test.ts.
|
|
8
|
+
*/
|
|
9
|
+
const ALLOWED_SCHEMES = new Set(['http', 'https', 'mailto', 'tel']);
|
|
10
|
+
/**
|
|
11
|
+
* The value when it is safe to use as an `href`: a relative URL (`/path`,
|
|
12
|
+
* `?q`, `#frag`, `page`), protocol-relative, or http(s) / mailto / tel.
|
|
13
|
+
* Anything else — `javascript:`, `data:`, `vbscript:`, non-strings, empty — is
|
|
14
|
+
* null so the caller falls back to its default link.
|
|
15
|
+
*/
|
|
16
|
+
export function safeHref(value) {
|
|
17
|
+
if (typeof value !== 'string')
|
|
18
|
+
return null;
|
|
19
|
+
const v = value.trim();
|
|
20
|
+
if (!v)
|
|
21
|
+
return null;
|
|
22
|
+
// Browsers strip leading C0 controls/spaces and drop tabs and newlines
|
|
23
|
+
// anywhere in a URL, so "java\tscript:" navigates as javascript:. Read the
|
|
24
|
+
// scheme the way the browser will.
|
|
25
|
+
const probe = v.replace(/[\u0000-\u0020]/g, '');
|
|
26
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(probe);
|
|
27
|
+
if (scheme)
|
|
28
|
+
return ALLOWED_SCHEMES.has(scheme[1].toLowerCase()) ? v : null;
|
|
29
|
+
return v;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The account menu's extra links (theme setting `accountExtraLinks`), in either
|
|
33
|
+
* form — JSON `[{"label","href"}]` or one `Label|/path` per line / comma. Links
|
|
34
|
+
* whose href is not {@link safeHref} are dropped.
|
|
35
|
+
*/
|
|
36
|
+
export function parseAccountLinks(raw) {
|
|
37
|
+
if (typeof raw !== 'string')
|
|
38
|
+
return [];
|
|
39
|
+
const trimmed = raw.trim();
|
|
40
|
+
if (!trimmed)
|
|
41
|
+
return [];
|
|
42
|
+
let links;
|
|
43
|
+
if (trimmed.startsWith('[')) {
|
|
44
|
+
try {
|
|
45
|
+
const parsed = JSON.parse(trimmed);
|
|
46
|
+
links = Array.isArray(parsed) ? parsed : [];
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
links = trimmed
|
|
54
|
+
.split(/[,\n]/)
|
|
55
|
+
.map((entry) => entry.trim())
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.map((entry) => {
|
|
58
|
+
const [label, href] = entry.split('|').map((s) => s.trim());
|
|
59
|
+
return { label, href };
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const l of links) {
|
|
64
|
+
const href = safeHref(l?.href);
|
|
65
|
+
if (typeof l?.label === 'string' && l.label && href)
|
|
66
|
+
out.push({ label: l.label, href });
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global theme-setting helpers the runtime still needs: validating a colour or
|
|
3
|
+
* an image URL before it reaches a stylesheet or an <img>, and the header logo
|
|
4
|
+
* precedence. The theme settings themselves are resolved by Nova's provider
|
|
5
|
+
* (`ui/styles/theme.ts`), which turns each semantic choice into a token.
|
|
6
|
+
*
|
|
7
|
+
* Pure: no React, no DOM, so node's test runner loads it directly.
|
|
8
|
+
*/
|
|
9
|
+
export type ThemeSettings = Record<string, unknown>;
|
|
10
|
+
/** The slice of `shop.brand` (Settings → Brand) the logo falls back to. */
|
|
11
|
+
export interface BrandFallback {
|
|
12
|
+
logo?: {
|
|
13
|
+
url?: string | null;
|
|
14
|
+
altText?: string | null;
|
|
15
|
+
} | null;
|
|
16
|
+
}
|
|
17
|
+
type Rgb = [number, number, number];
|
|
18
|
+
/** Parse an accepted colour to RGB, or null. Accepts hex and rgb()/hsl(); opaque only. */
|
|
19
|
+
export declare function parseColor(value: unknown): Rgb | null;
|
|
20
|
+
/**
|
|
21
|
+
* A colour safe to write into a stylesheet, as canonical `#rrggbb`, or null.
|
|
22
|
+
* Hex, rgb() and hsl() are accepted when they are valid CSS and opaque; every
|
|
23
|
+
* other value — invalid numbers, translucent colours, keywords, anything that
|
|
24
|
+
* could close a declaration — is refused, so the theme default applies.
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalizeColor(value: unknown): string | null;
|
|
27
|
+
/** An image URL the header may render: absolute http(s), protocol-relative, or root-relative. */
|
|
28
|
+
export declare function normalizeImageUrl(value: unknown): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* The header logo image: the theme's own logo, else the Settings → Brand logo,
|
|
31
|
+
* else null (the header then shows the shop name as text).
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveLogo(settings: ThemeSettings, brand?: BrandFallback | null): {
|
|
34
|
+
url: string;
|
|
35
|
+
altText: string | null;
|
|
36
|
+
} | null;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global theme-setting helpers the runtime still needs: validating a colour or
|
|
3
|
+
* an image URL before it reaches a stylesheet or an <img>, and the header logo
|
|
4
|
+
* precedence. The theme settings themselves are resolved by Nova's provider
|
|
5
|
+
* (`ui/styles/theme.ts`), which turns each semantic choice into a token.
|
|
6
|
+
*
|
|
7
|
+
* Pure: no React, no DOM, so node's test runner loads it directly.
|
|
8
|
+
*/
|
|
9
|
+
const NUM = /^(?:\d+(?:\.\d+)?|\.\d+)$/;
|
|
10
|
+
const PCT = /^(?:\d+(?:\.\d+)?|\.\d+)%$/;
|
|
11
|
+
/** An opaque alpha component: `1`, `1.0`, `100%`. Anything translucent is refused. */
|
|
12
|
+
function isOpaqueAlpha(token) {
|
|
13
|
+
if (NUM.test(token))
|
|
14
|
+
return Number(token) === 1;
|
|
15
|
+
if (PCT.test(token))
|
|
16
|
+
return Number(token.slice(0, -1)) === 100;
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The arguments of `fn(…)` in either CSS syntax: legacy `a, b, c[, alpha]` or
|
|
21
|
+
* modern `a b c[ / alpha]`. Null when the list is neither (mixed separators,
|
|
22
|
+
* empty tokens, wrong arity).
|
|
23
|
+
*/
|
|
24
|
+
function colorArgs(inner) {
|
|
25
|
+
const body = inner.trim();
|
|
26
|
+
if (!body)
|
|
27
|
+
return null;
|
|
28
|
+
if (body.includes(',')) {
|
|
29
|
+
if (body.includes('/'))
|
|
30
|
+
return null;
|
|
31
|
+
const parts = body.split(',').map((t) => t.trim());
|
|
32
|
+
if (parts.some((t) => !t || /\s/.test(t)))
|
|
33
|
+
return null;
|
|
34
|
+
if (parts.length === 3)
|
|
35
|
+
return { channels: parts, alpha: null };
|
|
36
|
+
if (parts.length === 4)
|
|
37
|
+
return { channels: parts.slice(0, 3), alpha: parts[3] };
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const [main, alpha, extra] = body.split('/').map((t) => t.trim());
|
|
41
|
+
if (extra !== undefined || alpha === '')
|
|
42
|
+
return null;
|
|
43
|
+
const channels = main.split(/\s+/);
|
|
44
|
+
if (channels.length !== 3 || (alpha !== undefined && /\s/.test(alpha)))
|
|
45
|
+
return null;
|
|
46
|
+
return { channels, alpha: alpha ?? null };
|
|
47
|
+
}
|
|
48
|
+
function parseRgbFunction(inner) {
|
|
49
|
+
const args = colorArgs(inner);
|
|
50
|
+
if (!args || (args.alpha !== null && !isOpaqueAlpha(args.alpha)))
|
|
51
|
+
return null;
|
|
52
|
+
const { channels } = args;
|
|
53
|
+
// CSS requires the three channels to be all numbers or all percentages.
|
|
54
|
+
if (channels.every((t) => NUM.test(t))) {
|
|
55
|
+
const v = channels.map(Number);
|
|
56
|
+
return v.every((n) => n <= 255) ? v.map(Math.round) : null;
|
|
57
|
+
}
|
|
58
|
+
if (channels.every((t) => PCT.test(t))) {
|
|
59
|
+
const v = channels.map((t) => Number(t.slice(0, -1)));
|
|
60
|
+
return v.every((n) => n <= 100) ? v.map((n) => Math.round((n / 100) * 255)) : null;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
function parseHslFunction(inner) {
|
|
65
|
+
const args = colorArgs(inner);
|
|
66
|
+
if (!args || (args.alpha !== null && !isOpaqueAlpha(args.alpha)))
|
|
67
|
+
return null;
|
|
68
|
+
const [hueToken, sToken, lToken] = args.channels;
|
|
69
|
+
const hue = /^-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg)?$/.test(hueToken) ? Number(hueToken.replace(/deg$/, '')) : NaN;
|
|
70
|
+
if (!Number.isFinite(hue) || !PCT.test(sToken) || !PCT.test(lToken))
|
|
71
|
+
return null;
|
|
72
|
+
const sat = Number(sToken.slice(0, -1)) / 100;
|
|
73
|
+
const light = Number(lToken.slice(0, -1)) / 100;
|
|
74
|
+
if (sat > 1 || light > 1)
|
|
75
|
+
return null;
|
|
76
|
+
const h = ((hue % 360) + 360) % 360;
|
|
77
|
+
const a = sat * Math.min(light, 1 - light);
|
|
78
|
+
const f = (n) => {
|
|
79
|
+
const k = (n + h / 30) % 12;
|
|
80
|
+
return Math.round((light - a * Math.max(-1, Math.min(k - 3, 9 - k, 1))) * 255);
|
|
81
|
+
};
|
|
82
|
+
return [f(0), f(8), f(4)];
|
|
83
|
+
}
|
|
84
|
+
/** Parse an accepted colour to RGB, or null. Accepts hex and rgb()/hsl(); opaque only. */
|
|
85
|
+
export function parseColor(value) {
|
|
86
|
+
if (typeof value !== 'string')
|
|
87
|
+
return null;
|
|
88
|
+
const v = value.trim().toLowerCase();
|
|
89
|
+
const hex = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/.exec(v);
|
|
90
|
+
if (hex) {
|
|
91
|
+
let h = hex[1];
|
|
92
|
+
if (h.length <= 4)
|
|
93
|
+
h = h.split('').map((c) => c + c).join('');
|
|
94
|
+
if (h.length === 8 && h.slice(6) !== 'ff')
|
|
95
|
+
return null;
|
|
96
|
+
return [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16));
|
|
97
|
+
}
|
|
98
|
+
const fn = /^(rgba?|hsla?)\((.*)\)$/.exec(v);
|
|
99
|
+
if (!fn)
|
|
100
|
+
return null;
|
|
101
|
+
return fn[1].startsWith('rgb') ? parseRgbFunction(fn[2]) : parseHslFunction(fn[2]);
|
|
102
|
+
}
|
|
103
|
+
const toHex = (rgb) => '#' + rgb.map((c) => c.toString(16).padStart(2, '0')).join('');
|
|
104
|
+
/**
|
|
105
|
+
* A colour safe to write into a stylesheet, as canonical `#rrggbb`, or null.
|
|
106
|
+
* Hex, rgb() and hsl() are accepted when they are valid CSS and opaque; every
|
|
107
|
+
* other value — invalid numbers, translucent colours, keywords, anything that
|
|
108
|
+
* could close a declaration — is refused, so the theme default applies.
|
|
109
|
+
*/
|
|
110
|
+
export function normalizeColor(value) {
|
|
111
|
+
const rgb = parseColor(value);
|
|
112
|
+
return rgb ? toHex(rgb) : null;
|
|
113
|
+
}
|
|
114
|
+
/** An image URL the header may render: absolute http(s), protocol-relative, or root-relative. */
|
|
115
|
+
export function normalizeImageUrl(value) {
|
|
116
|
+
const raw = typeof value === 'string'
|
|
117
|
+
? value
|
|
118
|
+
: value && typeof value === 'object' && typeof value.url === 'string'
|
|
119
|
+
? value.url
|
|
120
|
+
: null;
|
|
121
|
+
if (raw === null)
|
|
122
|
+
return null;
|
|
123
|
+
const v = raw.trim();
|
|
124
|
+
if (!v || /[\s"'<>]/.test(v))
|
|
125
|
+
return null;
|
|
126
|
+
return /^https?:\/\//i.test(v) || /^\/\/[^/]/.test(v) || /^\/[^/]/.test(v) ? v : null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The header logo image: the theme's own logo, else the Settings → Brand logo,
|
|
130
|
+
* else null (the header then shows the shop name as text).
|
|
131
|
+
*/
|
|
132
|
+
export function resolveLogo(settings, brand) {
|
|
133
|
+
const themeLogo = normalizeImageUrl(settings.logo);
|
|
134
|
+
if (themeLogo)
|
|
135
|
+
return { url: themeLogo, altText: null };
|
|
136
|
+
const brandLogo = normalizeImageUrl(brand?.logo?.url);
|
|
137
|
+
if (brandLogo)
|
|
138
|
+
return { url: brandLogo, altText: brand?.logo?.altText ?? null };
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* One saved section node → the theme's component for it.
|
|
4
|
+
*
|
|
5
|
+
* node ─► section.data(node, store, { actions, context, page }) ─► props ─► component
|
|
6
|
+
*
|
|
7
|
+
* Registered as the component of every theme section (`raw`, so SectionTree
|
|
8
|
+
* hands over the node instead of pre-rendered blocks). Knows nothing about any
|
|
9
|
+
* particular section: what a section needs is the theme's `data` binding.
|
|
10
|
+
*/
|
|
11
|
+
import { useContext, useEffect, useMemo } from 'react';
|
|
12
|
+
import { resolveSettings, useThemeActions, useThemeContext } from '../../sdk/index.js';
|
|
13
|
+
import { usePageTree } from '../kit/SectionTree.js';
|
|
14
|
+
import { getSection } from '../kit/registry.js';
|
|
15
|
+
import { getThemeBundle } from '../slots.js';
|
|
16
|
+
import { useStore } from '../data/StoreProvider.js';
|
|
17
|
+
import { useListing } from './product-listing.js';
|
|
18
|
+
import { ShowStarterContext, starterSet, withoutStarter } from './starter.js';
|
|
19
|
+
import { copyPlanOf, localizeShippedCopy, shippedCopyOf } from './localized-copy.js';
|
|
20
|
+
import { useLocaleStrings } from '../kit/theme-context.js';
|
|
21
|
+
import { resolveBoundSettings, useResourceContext } from '../kit/dynamic-source.js';
|
|
22
|
+
import { BLOCK_INDEX, blockDefOf, isClientOnly, resolveNode, ResolvedSinkContext, ResolvedStateContext, withBlockIndex } from './resolved.js';
|
|
23
|
+
import { useServerRender } from '../../sdk/server-render.js';
|
|
24
|
+
/**
|
|
25
|
+
* A saved block, its own blocks kept (nested theme blocks: a block that holds blocks). Its position as saved
|
|
26
|
+
* (BLOCK_INDEX) rides along, not enumerable (no spread or equality of saved blocks sees it), so `resolvedBlockKey`
|
|
27
|
+
* finds an id-less block's resolution under the key the server filed it under.
|
|
28
|
+
*/
|
|
29
|
+
const toSavedBlock = (b) => {
|
|
30
|
+
const out = {
|
|
31
|
+
...(typeof b.id === 'string' && b.id ? { id: b.id } : {}),
|
|
32
|
+
type: b.type,
|
|
33
|
+
settings: b.settings,
|
|
34
|
+
...(b.blocks?.length ? { blocks: b.blocks.map(toSavedBlock) } : {}),
|
|
35
|
+
};
|
|
36
|
+
const at = b[BLOCK_INDEX];
|
|
37
|
+
if (typeof at === 'number')
|
|
38
|
+
Object.defineProperty(out, BLOCK_INDEX, { value: at, enumerable: false });
|
|
39
|
+
return out;
|
|
40
|
+
};
|
|
41
|
+
const toSaved = (node) => ({
|
|
42
|
+
type: node.type,
|
|
43
|
+
settings: node.settings,
|
|
44
|
+
blocks: (node.blocks ?? []).map(toSavedBlock),
|
|
45
|
+
});
|
|
46
|
+
const fieldsOf = (schema) => schema?.fields ?? {};
|
|
47
|
+
/**
|
|
48
|
+
* The resources a unit's dynamic sources read: its own product / collection setting first (a product block reads
|
|
49
|
+
* the product it shows, not the page's), else what its parent read (REF-docs
|
|
50
|
+
* `/docs/storefronts/themes/architecture/settings/dynamic-sources`, "closest" resource, fetched 2026-11-13).
|
|
51
|
+
*/
|
|
52
|
+
function closestSources(fields, settings, parent, sourceFor) {
|
|
53
|
+
if (!sourceFor || !settings)
|
|
54
|
+
return parent;
|
|
55
|
+
let out = parent;
|
|
56
|
+
for (const [k, f] of Object.entries(fields)) {
|
|
57
|
+
const v = settings[k];
|
|
58
|
+
if ((f.control !== 'product' && f.control !== 'collection') || typeof v !== 'string' || !v.trim())
|
|
59
|
+
continue;
|
|
60
|
+
const r = sourceFor(f.control, v.trim());
|
|
61
|
+
if (r)
|
|
62
|
+
out = { ...out, [f.control]: r };
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
function withBoundValues(node, pageSources, theme, sourceFor) {
|
|
67
|
+
const section = theme.sections.find((s) => s.id === node.type);
|
|
68
|
+
const sectionFields = fieldsOf(section?.schema);
|
|
69
|
+
const sources = closestSources(sectionFields, node.settings, pageSources, sourceFor);
|
|
70
|
+
const settings = resolveBoundSettings(node.settings, sources, (k) => sectionFields[k]?.control);
|
|
71
|
+
let blocksChanged = false;
|
|
72
|
+
// Nested blocks are bound too, at every depth (a block's own blocks are found among the section's block schemas).
|
|
73
|
+
const bindBlock = (b, above) => {
|
|
74
|
+
// the block's definition by the same rule resolveNode uses (an old block name finds the block it now is)
|
|
75
|
+
const def = blockDefOf(section?.blocks, b.type);
|
|
76
|
+
const blockFields = fieldsOf(def);
|
|
77
|
+
const own = closestSources(blockFields, b.settings, above, sourceFor);
|
|
78
|
+
const resolved = resolveBoundSettings(b.settings, own, (k) => blockFields[k]?.control);
|
|
79
|
+
const kids = b.blocks?.map((k) => bindBlock(k, own));
|
|
80
|
+
const kidsChanged = !!kids && kids.some((k, i) => k !== b.blocks[i]);
|
|
81
|
+
if (resolved === b.settings && !kidsChanged)
|
|
82
|
+
return b;
|
|
83
|
+
blocksChanged = true;
|
|
84
|
+
return { ...b, settings: resolved, ...(kidsChanged ? { blocks: kids } : {}) };
|
|
85
|
+
};
|
|
86
|
+
const blocks = (node.blocks ?? []).map((b) => bindBlock(b, sources));
|
|
87
|
+
if (settings === node.settings && !blocksChanged)
|
|
88
|
+
return node;
|
|
89
|
+
return { ...node, settings, ...(node.blocks ? { blocks } : {}) };
|
|
90
|
+
}
|
|
91
|
+
/** A context for a section rendered with no page shell (the editor's single-section preview). */
|
|
92
|
+
const BARE = { shop: { name: '' }, menus: {} };
|
|
93
|
+
const isBody = (n) => {
|
|
94
|
+
const area = n.area ?? getSection(n.type)?.area;
|
|
95
|
+
return area !== 'header' && area !== 'footer';
|
|
96
|
+
};
|
|
97
|
+
export function SectionRenderer({ node }) {
|
|
98
|
+
const store = useStore();
|
|
99
|
+
const actions = useThemeActions();
|
|
100
|
+
const context = useThemeContext();
|
|
101
|
+
const tree = usePageTree();
|
|
102
|
+
const showStarter = useContext(ShowStarterContext);
|
|
103
|
+
// A load this render asked for starts after it commits, even when only this section re-rendered.
|
|
104
|
+
useEffect(() => store?.resources.flush(store.bump));
|
|
105
|
+
const theme = getThemeBundle().theme;
|
|
106
|
+
// O4: a setting bound to a dynamic source reads this page's resource (StoreProvider mounts the route's resources).
|
|
107
|
+
const sources = useResourceContext();
|
|
108
|
+
// Every block carries its position as saved before anything drops one (the key of an id-less block's resolution).
|
|
109
|
+
const tagged = node ? withBlockIndex(node) : node;
|
|
110
|
+
const bound = tagged ? withBoundValues(tagged, sources, theme, store?.sourceFor) : tagged;
|
|
111
|
+
// On the live storefront, starter instructions never reach a shopper (the editor shows them).
|
|
112
|
+
const unpruned = bound && context.mode !== 'editor' && !showStarter ? withoutStarter(bound, starterSet(theme.starterText), defaultsFor(theme), copyFieldsFor(theme)) : bound;
|
|
113
|
+
// Then the theme's own shipped words in the page's language (after the starter check, which matches the English).
|
|
114
|
+
const strings = useLocaleStrings();
|
|
115
|
+
const shown = useMemo(() => (unpruned ? localizeShippedCopy(unpruned, shippedCopyOf(theme.shippedCopy?.english, theme.shippedCopy?.prefix ?? ''), strings, copyPlanOf(theme)) : unpruned), [unpruned, theme, strings]);
|
|
116
|
+
const section = shown ? theme.sections.find((s) => s.id === shown.type) : undefined;
|
|
117
|
+
const firstBody = tree?.find(isBody) ?? null;
|
|
118
|
+
// §2.2.2: the node's resolved settings. The server resolves with the schemas and records it for the page state; the
|
|
119
|
+
// browser takes the state's copy (a client-only definition has no schema to resolve with).
|
|
120
|
+
const server = useServerRender();
|
|
121
|
+
const sink = useContext(ResolvedSinkContext);
|
|
122
|
+
const stateResolved = useContext(ResolvedStateContext);
|
|
123
|
+
const clientOnly = isClientOnly(section);
|
|
124
|
+
const resolved = useMemo(() => {
|
|
125
|
+
if (!section || !shown)
|
|
126
|
+
return null;
|
|
127
|
+
// a full definition resolves itself (the editor's live edits change settings the state never saw); only a
|
|
128
|
+
// client-only definition, which has no schema, takes the state's copy
|
|
129
|
+
if (!clientOnly)
|
|
130
|
+
return resolveNode(section, shown);
|
|
131
|
+
return !server && typeof shown.id === 'string' ? (stateResolved?.[shown.id] ?? null) : null;
|
|
132
|
+
}, [section, shown, server, stateResolved, clientOnly]);
|
|
133
|
+
if (server && sink && resolved && typeof shown?.id === 'string')
|
|
134
|
+
sink.set(shown.id, resolved);
|
|
135
|
+
let props = null;
|
|
136
|
+
if (section && shown && resolved) {
|
|
137
|
+
const saved = toSaved(shown);
|
|
138
|
+
props = section.data
|
|
139
|
+
? section.data(saved, store?.ctx ?? BARE, {
|
|
140
|
+
actions,
|
|
141
|
+
context,
|
|
142
|
+
page: {
|
|
143
|
+
firstBodySection: firstBody ? toSaved(firstBody) : null,
|
|
144
|
+
isFirstBodySection: Boolean(firstBody && firstBody.id === shown.id && firstBody.type === shown.type),
|
|
145
|
+
},
|
|
146
|
+
resolved,
|
|
147
|
+
})
|
|
148
|
+
: { ...resolved.settings };
|
|
149
|
+
}
|
|
150
|
+
const listing = useListing(section?.listing && props ? props : null);
|
|
151
|
+
if (!section || !props)
|
|
152
|
+
return null;
|
|
153
|
+
const C = section.component;
|
|
154
|
+
return _jsx(C, { ...props, ...(section.listing ? listing : {}) });
|
|
155
|
+
}
|
|
156
|
+
/** Setting defaults per section / block type, built once per theme (starter text can be a default). */
|
|
157
|
+
const defaultsCache = new WeakMap();
|
|
158
|
+
function defaultsFor(theme) {
|
|
159
|
+
let map = defaultsCache.get(theme);
|
|
160
|
+
if (!map) {
|
|
161
|
+
map = new Map();
|
|
162
|
+
for (const s of theme.sections) {
|
|
163
|
+
map.set(s.id, resolveSettings(s.schema, {}).settings);
|
|
164
|
+
for (const b of s.blocks ?? []) {
|
|
165
|
+
const name = b.name;
|
|
166
|
+
if (name && !map.has(name))
|
|
167
|
+
map.set(name, resolveSettings(b, {}).settings);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
defaultsCache.set(theme, map);
|
|
171
|
+
}
|
|
172
|
+
const m = map;
|
|
173
|
+
return (type) => m.get(type);
|
|
174
|
+
}
|
|
175
|
+
/** Copy fields (text / textarea / richtext) per section / block type, built once per theme. */
|
|
176
|
+
const COPY_CONTROLS = new Set(['text', 'textarea', 'richtext']);
|
|
177
|
+
const copyCache = new WeakMap();
|
|
178
|
+
function copyFieldsFor(theme) {
|
|
179
|
+
let map = copyCache.get(theme);
|
|
180
|
+
if (!map) {
|
|
181
|
+
map = new Map();
|
|
182
|
+
const copyOf = (s) => new Set(Object.entries(s?.fields ?? {}).filter(([, f]) => COPY_CONTROLS.has(f?.control ?? '')).map(([k]) => k));
|
|
183
|
+
for (const s of theme.sections) {
|
|
184
|
+
map.set(s.id, copyOf(s.schema));
|
|
185
|
+
for (const b of s.blocks ?? []) {
|
|
186
|
+
const name = b.name;
|
|
187
|
+
if (name && !map.has(name))
|
|
188
|
+
map.set(name, copyOf(b));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
copyCache.set(theme, map);
|
|
192
|
+
}
|
|
193
|
+
const m = map;
|
|
194
|
+
return (type) => m.get(type);
|
|
195
|
+
}
|
|
196
|
+
/** Blocks render inside their section; standalone they render nothing. */
|
|
197
|
+
export function BlockPlaceholder() {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A product listing's state in the URL, so a sorted / filtered collection page can be shared,
|
|
3
|
+
* bookmarked and reloaded — and the server renders it the same way (no React here: the server's
|
|
4
|
+
* data plan and the browser's listing hook both use it).
|
|
5
|
+
*
|
|
6
|
+
* ?sort=price-asc the sort id (`featured` is the bare URL)
|
|
7
|
+
* ?filter.available=true a list facet value (its id, minus the facet id prefix)
|
|
8
|
+
* ?filter.productType=Mugs&filter.productType=Plates several values of one facet = any of them
|
|
9
|
+
* ?filter.price.gte=10&filter.price.lte=50 a price range (either end may be absent)
|
|
10
|
+
*
|
|
11
|
+
* The parameter name is the facet's own id, so it means the same thing on every store that has
|
|
12
|
+
* that facet. A value or facet this store does not have is ignored, never an error.
|
|
13
|
+
*/
|
|
14
|
+
/** The sorts the listing offers (product-listing.ts). */
|
|
15
|
+
export declare const LISTING_SORTS: readonly ["featured", "price-asc", "price-desc", "title-asc"];
|
|
16
|
+
/** A facet as the listing reads it: `input` is the JSON filter object for one value. */
|
|
17
|
+
export interface ListingFacet {
|
|
18
|
+
id: string;
|
|
19
|
+
type: string;
|
|
20
|
+
values: ReadonlyArray<{
|
|
21
|
+
id: string;
|
|
22
|
+
input: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
export type FilterInput = Record<string, unknown>;
|
|
26
|
+
export interface ListingState {
|
|
27
|
+
/** Facet id → the selected value ids. */
|
|
28
|
+
values: Record<string, string[]>;
|
|
29
|
+
price?: {
|
|
30
|
+
from?: number;
|
|
31
|
+
to?: number;
|
|
32
|
+
};
|
|
33
|
+
sort: string;
|
|
34
|
+
}
|
|
35
|
+
/** The listing part of a query string (`sort` and `filter.*`), in its original order; '' when none. */
|
|
36
|
+
export declare function listingQueryOf(search: string | URLSearchParams): string;
|
|
37
|
+
/** The selection a listing query names, resolved against this collection's facets. */
|
|
38
|
+
export declare function stateFromQuery(query: string, facets: readonly ListingFacet[]): ListingState;
|
|
39
|
+
/** The listing query for a selection (stable: facet order, then value order); '' for the default. */
|
|
40
|
+
export declare function queryOfState(state: ListingState, facets: readonly ListingFacet[]): string;
|
|
41
|
+
/** `href` with its listing params replaced by `query` (other params and the hash kept). */
|
|
42
|
+
export declare function withListingQuery(href: string, query: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* The requests a filtered selection needs. The storefront ANDs every filter it is given, so two
|
|
45
|
+
* values of one facet are asked for one request each and unioned by the caller.
|
|
46
|
+
*/
|
|
47
|
+
export declare function filterRequests(state: Pick<ListingState, 'values' | 'price'>, facets: readonly ListingFacet[], maxRequests: number): FilterInput[][];
|
|
48
|
+
/**
|
|
49
|
+
* The shared label key of a boolean metafield facet value (`filter.p.m.<ns>.<key>.true` → `filter.metafield.true`), so
|
|
50
|
+
* a theme words "Yes" / "No" in the shopper's language; null for any other value id.
|
|
51
|
+
*/
|
|
52
|
+
export declare function metafieldBooleanLabelKey(valueId: string): 'filter.metafield.true' | 'filter.metafield.false' | null;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A product listing's state in the URL, so a sorted / filtered collection page can be shared,
|
|
3
|
+
* bookmarked and reloaded — and the server renders it the same way (no React here: the server's
|
|
4
|
+
* data plan and the browser's listing hook both use it).
|
|
5
|
+
*
|
|
6
|
+
* ?sort=price-asc the sort id (`featured` is the bare URL)
|
|
7
|
+
* ?filter.available=true a list facet value (its id, minus the facet id prefix)
|
|
8
|
+
* ?filter.productType=Mugs&filter.productType=Plates several values of one facet = any of them
|
|
9
|
+
* ?filter.price.gte=10&filter.price.lte=50 a price range (either end may be absent)
|
|
10
|
+
*
|
|
11
|
+
* The parameter name is the facet's own id, so it means the same thing on every store that has
|
|
12
|
+
* that facet. A value or facet this store does not have is ignored, never an error.
|
|
13
|
+
*/
|
|
14
|
+
/** The sorts the listing offers (product-listing.ts). */
|
|
15
|
+
export const LISTING_SORTS = ['featured', 'price-asc', 'price-desc', 'title-asc'];
|
|
16
|
+
const isFilterKey = (k) => k === 'sort' || k.startsWith('filter.');
|
|
17
|
+
/** The listing part of a query string (`sort` and `filter.*`), in its original order; '' when none. */
|
|
18
|
+
export function listingQueryOf(search) {
|
|
19
|
+
const params = typeof search === 'string' ? new URLSearchParams(search.startsWith('?') ? search.slice(1) : search) : search;
|
|
20
|
+
const out = new URLSearchParams();
|
|
21
|
+
for (const [k, v] of params)
|
|
22
|
+
if (isFilterKey(k) && v !== '' && k.length <= 128 && v.length <= 256)
|
|
23
|
+
out.append(k, v);
|
|
24
|
+
return out.toString();
|
|
25
|
+
}
|
|
26
|
+
const token = (facetId, valueId) => (valueId.startsWith(`${facetId}.`) ? valueId.slice(facetId.length + 1) : valueId);
|
|
27
|
+
const num = (v) => {
|
|
28
|
+
if (v === null || v.trim() === '')
|
|
29
|
+
return undefined;
|
|
30
|
+
const n = Number(v);
|
|
31
|
+
return Number.isFinite(n) && n >= 0 ? n : undefined;
|
|
32
|
+
};
|
|
33
|
+
/** The selection a listing query names, resolved against this collection's facets. */
|
|
34
|
+
export function stateFromQuery(query, facets) {
|
|
35
|
+
const params = new URLSearchParams(query);
|
|
36
|
+
const sortParam = params.get('sort') ?? '';
|
|
37
|
+
const sort = LISTING_SORTS.includes(sortParam) ? sortParam : 'featured';
|
|
38
|
+
const values = {};
|
|
39
|
+
let price;
|
|
40
|
+
for (const f of facets) {
|
|
41
|
+
if (f.type === 'PRICE_RANGE') {
|
|
42
|
+
const from = num(params.get(`${f.id}.gte`));
|
|
43
|
+
const to = num(params.get(`${f.id}.lte`));
|
|
44
|
+
if (from !== undefined || to !== undefined)
|
|
45
|
+
price = { ...(from !== undefined ? { from } : {}), ...(to !== undefined ? { to } : {}) };
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const picked = params.getAll(f.id).flatMap((t) => f.values.filter((v) => v.id === t || token(f.id, v.id) === t).map((v) => v.id));
|
|
49
|
+
if (picked.length)
|
|
50
|
+
values[f.id] = [...new Set(picked)];
|
|
51
|
+
}
|
|
52
|
+
return { values, ...(price ? { price } : {}), sort };
|
|
53
|
+
}
|
|
54
|
+
/** The listing query for a selection (stable: facet order, then value order); '' for the default. */
|
|
55
|
+
export function queryOfState(state, facets) {
|
|
56
|
+
const out = new URLSearchParams();
|
|
57
|
+
if (state.sort && state.sort !== 'featured')
|
|
58
|
+
out.set('sort', state.sort);
|
|
59
|
+
for (const f of facets) {
|
|
60
|
+
if (f.type === 'PRICE_RANGE') {
|
|
61
|
+
if (state.price?.from != null)
|
|
62
|
+
out.set(`${f.id}.gte`, String(state.price.from));
|
|
63
|
+
if (state.price?.to != null)
|
|
64
|
+
out.set(`${f.id}.lte`, String(state.price.to));
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
for (const v of f.values)
|
|
68
|
+
if (state.values[f.id]?.includes(v.id))
|
|
69
|
+
out.append(f.id, token(f.id, v.id));
|
|
70
|
+
}
|
|
71
|
+
return out.toString();
|
|
72
|
+
}
|
|
73
|
+
/** `href` with its listing params replaced by `query` (other params and the hash kept). */
|
|
74
|
+
export function withListingQuery(href, query) {
|
|
75
|
+
const url = new URL(href);
|
|
76
|
+
for (const k of [...url.searchParams.keys()])
|
|
77
|
+
if (isFilterKey(k))
|
|
78
|
+
url.searchParams.delete(k);
|
|
79
|
+
// A new selection is a new list: the page number belonged to the old one.
|
|
80
|
+
url.searchParams.delete('page');
|
|
81
|
+
for (const [k, v] of new URLSearchParams(query))
|
|
82
|
+
url.searchParams.append(k, v);
|
|
83
|
+
return url.toString();
|
|
84
|
+
}
|
|
85
|
+
const parse = (input) => {
|
|
86
|
+
try {
|
|
87
|
+
return JSON.parse(input);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** Every pick of one value per group — the requests that together mean "any of these". */
|
|
94
|
+
function combinations(groups) {
|
|
95
|
+
return groups.reduce((acc, g) => acc.flatMap((c) => g.map((v) => [...c, v])), [[]]);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The requests a filtered selection needs. The storefront ANDs every filter it is given, so two
|
|
99
|
+
* values of one facet are asked for one request each and unioned by the caller.
|
|
100
|
+
*/
|
|
101
|
+
export function filterRequests(state, facets, maxRequests) {
|
|
102
|
+
const inputs = new Map(facets.flatMap((f) => f.values.map((v) => [v.id, parse(v.input)])));
|
|
103
|
+
const priceFacet = facets.find((f) => f.type === 'PRICE_RANGE');
|
|
104
|
+
const bounds = priceFacet ? (parse(priceFacet.values[0]?.input ?? '')?.price ?? null) : null;
|
|
105
|
+
const groups = Object.values(state.values)
|
|
106
|
+
.filter((ids) => ids.length > 0)
|
|
107
|
+
.map((ids) => ids.map((v) => inputs.get(v)).filter((v) => Boolean(v)))
|
|
108
|
+
.filter((g) => g.length > 0);
|
|
109
|
+
if (state.price)
|
|
110
|
+
groups.push([{ price: { min: state.price.from ?? bounds?.min ?? 0, max: state.price.to ?? bounds?.max ?? Number.MAX_SAFE_INTEGER } }]);
|
|
111
|
+
if (!groups.length)
|
|
112
|
+
return [];
|
|
113
|
+
return combinations(groups).slice(0, maxRequests);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The shared label key of a boolean metafield facet value (`filter.p.m.<ns>.<key>.true` → `filter.metafield.true`), so
|
|
117
|
+
* a theme words "Yes" / "No" in the shopper's language; null for any other value id.
|
|
118
|
+
*/
|
|
119
|
+
export function metafieldBooleanLabelKey(valueId) {
|
|
120
|
+
const m = /^filter\.[pv]\.m\..+\.(true|false)$/.exec(valueId);
|
|
121
|
+
return m ? (m[1] === 'true' ? 'filter.metafield.true' : 'filter.metafield.false') : null;
|
|
122
|
+
}
|