@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.5
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 +98 -10
- package/dist/abi/canvas-live.d.ts +49 -0
- package/dist/abi/canvas-live.js +88 -0
- package/dist/abi/cart.d.ts +48 -0
- package/dist/abi/cart.js +321 -0
- package/dist/abi/convert.d.ts +192 -0
- package/dist/abi/convert.js +670 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +198 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/page-data.d.ts +209 -0
- package/dist/abi/page-data.js +583 -0
- package/dist/abi/page.d.ts +23 -0
- package/dist/abi/page.js +46 -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 +22 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +113 -0
- package/dist/build.js +294 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -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 +273 -0
- package/dist/headless/hooks.js +533 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +36 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -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 +186 -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 +186 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +56 -0
- package/dist/runtime/data/StoreProvider.js +308 -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 +154 -0
- package/dist/runtime/data/to-ui.d.ts +258 -0
- package/dist/runtime/data/to-ui.js +820 -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 +233 -0
- package/dist/runtime/kit/cart.d.ts +186 -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 +164 -0
- package/dist/runtime/kit/contract/content.js +83 -0
- package/dist/runtime/kit/contract/field-types.d.ts +290 -0
- package/dist/runtime/kit/contract/field-types.js +129 -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 +67 -0
- package/dist/runtime/kit/contract/validate.js +388 -0
- package/dist/runtime/kit/data.d.ts +636 -0
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
- package/dist/runtime/kit/dynamic-source.js +143 -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 +566 -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 +142 -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 +111 -0
- package/dist/runtime/render/listing-query.d.ts +47 -0
- package/dist/runtime/render/listing-query.js +114 -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 +240 -0
- package/dist/runtime/render/section-actions.d.ts +48 -0
- package/dist/runtime/render/section-actions.js +170 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +87 -0
- package/dist/runtime/seo/head.js +224 -0
- package/dist/runtime/seo/structured-data.d.ts +22 -0
- package/dist/runtime/seo/structured-data.js +115 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +85 -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/index.d.ts +24 -0
- package/dist/sdk/index.js +24 -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 +135 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +197 -0
- package/dist/sdk/schema.js +121 -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 +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +115 -0
- package/dist/sdk/store.js +1 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +152 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +470 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +65 -0
- package/dist/server.js +370 -0
- package/docs/headless-api.md +220 -0
- package/package.json +77 -23
- 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/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,85 @@
|
|
|
1
|
+
export interface RewardsWay {
|
|
2
|
+
id: string;
|
|
3
|
+
kind: string;
|
|
4
|
+
name: string;
|
|
5
|
+
/** Fixed points for the action; null for an order, which earns at `program.earnRates`. */
|
|
6
|
+
points: number | null;
|
|
7
|
+
completed: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface RewardsReward {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
kind: string;
|
|
13
|
+
pointsCost: number;
|
|
14
|
+
/** The member can turn points into this code now. */
|
|
15
|
+
affordable: boolean;
|
|
16
|
+
pointsNeeded: number;
|
|
17
|
+
tierLocked: boolean;
|
|
18
|
+
limitReached: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface RewardsCode {
|
|
21
|
+
code: string;
|
|
22
|
+
rewardName: string;
|
|
23
|
+
kind: string;
|
|
24
|
+
expiresAt: string | null;
|
|
25
|
+
}
|
|
26
|
+
export interface RewardsTier {
|
|
27
|
+
basis: 'POINTS' | 'SPEND' | string;
|
|
28
|
+
currency: string | null;
|
|
29
|
+
current: {
|
|
30
|
+
name: string;
|
|
31
|
+
earnMultiplier: number;
|
|
32
|
+
} | null;
|
|
33
|
+
next: {
|
|
34
|
+
name: string;
|
|
35
|
+
threshold: number;
|
|
36
|
+
} | null;
|
|
37
|
+
progress: {
|
|
38
|
+
qualifying: number;
|
|
39
|
+
remaining: number | null;
|
|
40
|
+
} | null;
|
|
41
|
+
}
|
|
42
|
+
export interface RewardsState {
|
|
43
|
+
enabled: true;
|
|
44
|
+
signedIn: boolean;
|
|
45
|
+
/** Signed in and in the program (a signed-in shopper may still have to join). */
|
|
46
|
+
member?: boolean;
|
|
47
|
+
/** `earnRates`: points per one unit of a currency, by ISO code. */
|
|
48
|
+
program: {
|
|
49
|
+
name: string;
|
|
50
|
+
earnRates: Readonly<Record<string, number>>;
|
|
51
|
+
};
|
|
52
|
+
balance?: {
|
|
53
|
+
available: number;
|
|
54
|
+
pending: number;
|
|
55
|
+
lifetimeEarned: number;
|
|
56
|
+
};
|
|
57
|
+
tier?: RewardsTier | null;
|
|
58
|
+
rewards?: readonly RewardsReward[];
|
|
59
|
+
codes?: readonly RewardsCode[];
|
|
60
|
+
ways: readonly RewardsWay[];
|
|
61
|
+
}
|
|
62
|
+
export type RedeemResult = {
|
|
63
|
+
ok: true;
|
|
64
|
+
code: string;
|
|
65
|
+
} | {
|
|
66
|
+
ok: false;
|
|
67
|
+
error: 'insufficient' | 'failed';
|
|
68
|
+
};
|
|
69
|
+
export interface RewardsValue {
|
|
70
|
+
/** null until known, and for a store with loyalty off. */
|
|
71
|
+
state: RewardsState | null;
|
|
72
|
+
/** Turns points into a code; the state is refreshed on success. Never rejects. */
|
|
73
|
+
redeem(rewardId: string): Promise<RedeemResult>;
|
|
74
|
+
/** Joins the program (a signed-in shopper who is not a member yet). Never rejects. */
|
|
75
|
+
join(): Promise<boolean>;
|
|
76
|
+
}
|
|
77
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
78
|
+
export declare const RewardsContext: import("react").Context<RewardsValue>;
|
|
79
|
+
/** The loyalty program, the shopper's balance and the redeem / join actions. */
|
|
80
|
+
export declare function useRewards(): RewardsValue;
|
|
81
|
+
/**
|
|
82
|
+
* Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
|
|
83
|
+
* rounded down. null when the program does not earn in that currency (the hint is then not shown).
|
|
84
|
+
*/
|
|
85
|
+
export declare function earnPoints(amount: number, currencyCode: string, state: RewardsState | null): number | null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The store's loyalty program as the shopper sees it (army #7749): what the rewards launcher and
|
|
3
|
+
* the "Earn N points" hints render from, and the two things a shopper can ask of it.
|
|
4
|
+
*
|
|
5
|
+
* The theme runtime provides it after the page has loaded in the browser (lib/abi/rewards.tsx);
|
|
6
|
+
* on the server, in the editor and for a store with loyalty off it is `null`, so nothing renders.
|
|
7
|
+
*/
|
|
8
|
+
import { createContext, useContext } from 'react';
|
|
9
|
+
const OFF = { state: null, redeem: async () => ({ ok: false, error: 'failed' }), join: async () => false };
|
|
10
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
11
|
+
export const RewardsContext = createContext(OFF);
|
|
12
|
+
/** The loyalty program, the shopper's balance and the redeem / join actions. */
|
|
13
|
+
export function useRewards() {
|
|
14
|
+
return useContext(RewardsContext);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
|
|
18
|
+
* rounded down. null when the program does not earn in that currency (the hint is then not shown).
|
|
19
|
+
*/
|
|
20
|
+
export function earnPoints(amount, currencyCode, state) {
|
|
21
|
+
if (!state)
|
|
22
|
+
return null;
|
|
23
|
+
const rate = Number(state.program.earnRates[currencyCode]);
|
|
24
|
+
if (!(rate > 0) || !(amount > 0))
|
|
25
|
+
return null;
|
|
26
|
+
const multiplier = Number(state.tier?.current?.earnMultiplier) > 0 ? Number(state.tier.current.earnMultiplier) : 1;
|
|
27
|
+
// Rounded to 6 places first so 0.1 x 3 style float noise never drops a whole point.
|
|
28
|
+
return Math.floor(Math.round(amount * rate * multiplier * 1e6) / 1e6);
|
|
29
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Cart, Link, Shop, Product } from './types.js';
|
|
2
|
+
export type ThemeMode = 'storefront' | 'editor';
|
|
3
|
+
export type OverlayName = 'cart' | 'search' | 'account';
|
|
4
|
+
export interface ThemeToast {
|
|
5
|
+
id: string;
|
|
6
|
+
message: string;
|
|
7
|
+
tone: 'info' | 'error';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Why the store refused a line's last quantity change, when it said: `cart_limit_exceeded` is the
|
|
11
|
+
* merchant's "Maximum quantity per product"; `maxQuantity` is that limit when known.
|
|
12
|
+
*/
|
|
13
|
+
export interface CartLineRefusal {
|
|
14
|
+
code: 'cart_limit_exceeded';
|
|
15
|
+
maxQuantity?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface ThemeContextValue {
|
|
18
|
+
/** `editor` inside the Studio canvas: empty sections draw their setup frame. */
|
|
19
|
+
mode: ThemeMode;
|
|
20
|
+
/** BCP 47 language of the page. */
|
|
21
|
+
locale: string;
|
|
22
|
+
/** The store's theme settings, including the editor's unsaved live values. */
|
|
23
|
+
settings: Readonly<Record<string, unknown>>;
|
|
24
|
+
/** The shopper's cart (null until known). */
|
|
25
|
+
cart: Cart | null;
|
|
26
|
+
/** Where Checkout goes. */
|
|
27
|
+
checkoutUrl: string;
|
|
28
|
+
/** Which page-level overlay is open. */
|
|
29
|
+
overlay: OverlayName | null;
|
|
30
|
+
/** The cart drawer was opened by an add, not by the shopper. */
|
|
31
|
+
justAddedToCart: boolean;
|
|
32
|
+
/** Lines with a request in flight / whose last request failed. */
|
|
33
|
+
busyLines: readonly string[];
|
|
34
|
+
failedLines: readonly string[];
|
|
35
|
+
/** Failed lines the store said why for (a subset of `failedLines`), by line id. */
|
|
36
|
+
refusedLines: Readonly<Record<string, CartLineRefusal>>;
|
|
37
|
+
toasts: readonly ThemeToast[];
|
|
38
|
+
/** The cookie banner should be shown (the platform decided consent is needed and the shopper has not answered). */
|
|
39
|
+
consentRequired: boolean;
|
|
40
|
+
/** The store's privacy policy page, when it is published; null otherwise (or not known yet). */
|
|
41
|
+
privacyPolicyUrl: string | null;
|
|
42
|
+
/** The shop as sections see it (name, cookie banner, taxes, free-shipping threshold); absent outside a store. */
|
|
43
|
+
shop?: Shop;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* How applying a discount code went (`ThemeActions.cart.applyDiscountCode`). Kept equal to the runtime's
|
|
47
|
+
* `DiscountCodeResult` (runtime/kit/cart.tsx).
|
|
48
|
+
*/
|
|
49
|
+
export type DiscountCodeResult = 'applied' | 'not_applicable' | 'failed';
|
|
50
|
+
export interface ConsentDecision {
|
|
51
|
+
analytics: boolean;
|
|
52
|
+
marketing: boolean;
|
|
53
|
+
}
|
|
54
|
+
export interface ThemeActions {
|
|
55
|
+
cart: {
|
|
56
|
+
/** Resolves true when the line is in the cart; never rejects. Opens the cart on success. */
|
|
57
|
+
add(variantId: string, quantity: number): Promise<boolean>;
|
|
58
|
+
setQuantity(lineId: string, quantity: number): Promise<void>;
|
|
59
|
+
remove(lineId: string): Promise<void>;
|
|
60
|
+
setNote(note: string): Promise<void>;
|
|
61
|
+
open(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Put a discount code on the cart; resolves how it went, never rejects. The store holds one code, so it replaces
|
|
64
|
+
* the code there. `applied`: the store applies it (the cart's totals already show it); `not_applicable`: the store
|
|
65
|
+
* does not apply it to this cart (unknown, expired, a minimum not met, does not combine) — it was taken off again
|
|
66
|
+
* and the earlier code put back; `failed`: the request did not go through.
|
|
67
|
+
* Optional: absent on hosts without cart discount codes — a theme must check before offering a code field.
|
|
68
|
+
*/
|
|
69
|
+
applyDiscountCode?(code: string): Promise<DiscountCodeResult>;
|
|
70
|
+
/** Take a discount code off the cart. Resolves true once it is gone; never rejects. Present with `applyDiscountCode`. */
|
|
71
|
+
removeDiscountCode?(code: string): Promise<boolean>;
|
|
72
|
+
};
|
|
73
|
+
search: {
|
|
74
|
+
open(): void;
|
|
75
|
+
/** Go to the results page for a query. */
|
|
76
|
+
go(query: string): void;
|
|
77
|
+
/** Product suggestions for a query (predictive search). */
|
|
78
|
+
suggest(query: string): Promise<SearchSuggestions>;
|
|
79
|
+
};
|
|
80
|
+
localization: {
|
|
81
|
+
/** Switch market (prices and currency follow). */
|
|
82
|
+
changeCountry(countryCode: string): void;
|
|
83
|
+
/**
|
|
84
|
+
* Apply a Region & language choice: `?country=` when the country changed, `?locale=` when the
|
|
85
|
+
* language changed (`current` is what the page shows now). Optional for older hosts.
|
|
86
|
+
*/
|
|
87
|
+
changeLocale?(next: {
|
|
88
|
+
countryCode?: string;
|
|
89
|
+
languageCode?: string;
|
|
90
|
+
}, current?: {
|
|
91
|
+
countryCode?: string;
|
|
92
|
+
languageCode?: string;
|
|
93
|
+
}): void;
|
|
94
|
+
};
|
|
95
|
+
consent: {
|
|
96
|
+
decide(choice: ConsentDecision): void;
|
|
97
|
+
/** Show the cookie banner again, so the shopper can change an earlier choice. */
|
|
98
|
+
open(): void;
|
|
99
|
+
};
|
|
100
|
+
overlays: {
|
|
101
|
+
close(): void;
|
|
102
|
+
};
|
|
103
|
+
toasts: {
|
|
104
|
+
show(message: string, tone?: ThemeToast['tone']): void;
|
|
105
|
+
dismiss(id: string): void;
|
|
106
|
+
};
|
|
107
|
+
/** Go to a storefront URL (soft navigation when on). */
|
|
108
|
+
navigate(href: string): void;
|
|
109
|
+
}
|
|
110
|
+
export interface SearchSuggestions {
|
|
111
|
+
products: Product[];
|
|
112
|
+
collections: Link[];
|
|
113
|
+
articles: Link[];
|
|
114
|
+
/** All matches (the results page count). */
|
|
115
|
+
total: number;
|
|
116
|
+
}
|
|
117
|
+
export interface ThemeRuntimeValue {
|
|
118
|
+
context: ThemeContextValue;
|
|
119
|
+
actions: ThemeActions;
|
|
120
|
+
}
|
|
121
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
122
|
+
export declare const ThemeRuntimeContext: import("react").Context<ThemeRuntimeValue>;
|
|
123
|
+
/** Page state the theme can render from (cart, open overlay, mode, locale, settings). */
|
|
124
|
+
export declare function useThemeContext(): ThemeContextValue;
|
|
125
|
+
/** The platform actions a theme can trigger (cart, search, localization, consent, toasts). */
|
|
126
|
+
export declare function useThemeActions(): ThemeActions;
|
|
127
|
+
/**
|
|
128
|
+
* Predictive search for a query, debounced. Results are tagged with the query
|
|
129
|
+
* they answered: a slow answer for "shirt" never renders under "hat", and
|
|
130
|
+
* `loading` stays true until the CURRENT query has one.
|
|
131
|
+
*/
|
|
132
|
+
export declare function useSearchSuggestions(query: string, debounceMs?: number): {
|
|
133
|
+
results: SearchSuggestions | null;
|
|
134
|
+
loading: boolean;
|
|
135
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a theme can read from and ask of the platform while it renders.
|
|
3
|
+
*
|
|
4
|
+
* The theme runtime provides these; a theme only calls the hooks. Nothing here
|
|
5
|
+
* exposes how it is done — no API clients, no cart engine, no session.
|
|
6
|
+
*/
|
|
7
|
+
import { createContext, useContext, useEffect, useRef, useState } from 'react';
|
|
8
|
+
const NOOP_ACTIONS = {
|
|
9
|
+
cart: { add: async () => false, setQuantity: async () => { }, remove: async () => { }, setNote: async () => { }, open: () => { } },
|
|
10
|
+
search: { open: () => { }, go: () => { }, suggest: async () => ({ products: [], collections: [], articles: [], total: 0 }) },
|
|
11
|
+
localization: { changeCountry: () => { } },
|
|
12
|
+
consent: { decide: () => { }, open: () => { } },
|
|
13
|
+
overlays: { close: () => { } },
|
|
14
|
+
toasts: { show: () => { }, dismiss: () => { } },
|
|
15
|
+
navigate: () => { },
|
|
16
|
+
};
|
|
17
|
+
const DETACHED = {
|
|
18
|
+
context: {
|
|
19
|
+
mode: 'storefront',
|
|
20
|
+
locale: 'en',
|
|
21
|
+
settings: {},
|
|
22
|
+
cart: null,
|
|
23
|
+
checkoutUrl: '/checkout',
|
|
24
|
+
overlay: null,
|
|
25
|
+
justAddedToCart: false,
|
|
26
|
+
busyLines: [],
|
|
27
|
+
failedLines: [],
|
|
28
|
+
refusedLines: {},
|
|
29
|
+
toasts: [],
|
|
30
|
+
consentRequired: false,
|
|
31
|
+
privacyPolicyUrl: null,
|
|
32
|
+
},
|
|
33
|
+
actions: NOOP_ACTIONS,
|
|
34
|
+
};
|
|
35
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
36
|
+
export const ThemeRuntimeContext = createContext(DETACHED);
|
|
37
|
+
/** Page state the theme can render from (cart, open overlay, mode, locale, settings). */
|
|
38
|
+
export function useThemeContext() {
|
|
39
|
+
return useContext(ThemeRuntimeContext).context;
|
|
40
|
+
}
|
|
41
|
+
/** The platform actions a theme can trigger (cart, search, localization, consent, toasts). */
|
|
42
|
+
export function useThemeActions() {
|
|
43
|
+
return useContext(ThemeRuntimeContext).actions;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Predictive search for a query, debounced. Results are tagged with the query
|
|
47
|
+
* they answered: a slow answer for "shirt" never renders under "hat", and
|
|
48
|
+
* `loading` stays true until the CURRENT query has one.
|
|
49
|
+
*/
|
|
50
|
+
export function useSearchSuggestions(query, debounceMs = 250) {
|
|
51
|
+
const { search } = useThemeActions();
|
|
52
|
+
const [answer, setAnswer] = useState(null);
|
|
53
|
+
const timer = useRef(null);
|
|
54
|
+
const term = query.trim();
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (timer.current)
|
|
57
|
+
clearTimeout(timer.current);
|
|
58
|
+
if (!term)
|
|
59
|
+
return;
|
|
60
|
+
timer.current = setTimeout(() => {
|
|
61
|
+
search
|
|
62
|
+
.suggest(term)
|
|
63
|
+
.then((results) => setAnswer({ term, results }))
|
|
64
|
+
.catch(() => setAnswer({ term, results: { products: [], collections: [], articles: [], total: 0 } }));
|
|
65
|
+
}, debounceMs);
|
|
66
|
+
return () => {
|
|
67
|
+
if (timer.current)
|
|
68
|
+
clearTimeout(timer.current);
|
|
69
|
+
};
|
|
70
|
+
}, [term, search, debounceMs]);
|
|
71
|
+
const fresh = answer && answer.term === term ? answer.results : null;
|
|
72
|
+
return { results: term ? fresh : null, loading: Boolean(term) && !fresh };
|
|
73
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings schema DSL — the ONE place a Nova section or block declares its
|
|
3
|
+
* persisted settings in code. Everything else is derived from it:
|
|
4
|
+
*
|
|
5
|
+
* schema ──► TypeScript prop types (SettingsOf<typeof schema>)
|
|
6
|
+
* ──► the adapter (resolveSettings: defaults, legacy, coercion)
|
|
7
|
+
* ──► Storybook controls (stories/argTypes.ts)
|
|
8
|
+
* ──► contract drift validation (validate.ts, against generated/*.json)
|
|
9
|
+
*
|
|
10
|
+
* Option IDs here are the PERSISTED IDs (what a store's saved JSON holds), never
|
|
11
|
+
* display labels. Labels come from the approved contract at display time.
|
|
12
|
+
*/
|
|
13
|
+
/** The editor control vocabulary used by the approved contracts. */
|
|
14
|
+
export type Control = 'select' | 'boolean' | 'text' | 'textarea' | 'richtext' | 'url' | 'range' | 'number' | 'color' | 'image' | 'video' | 'menu' | 'collection' | 'product' | 'page' | 'blog' | 'article';
|
|
15
|
+
/**
|
|
16
|
+
* How a persisted setting reaches the component:
|
|
17
|
+
* prop — passed through as the typed component prop of the same name
|
|
18
|
+
* adapter — resolved by the adapter layer into data (a menu handle becomes a
|
|
19
|
+
* Menu object, an image id an Image). The component takes the data.
|
|
20
|
+
*/
|
|
21
|
+
export type Binding = 'prop' | 'adapter';
|
|
22
|
+
interface FieldBase<C extends Control, V, B extends Binding = 'prop'> {
|
|
23
|
+
readonly control: C;
|
|
24
|
+
readonly default: V;
|
|
25
|
+
readonly binding: B;
|
|
26
|
+
/** LEGACY-HIDDEN: saved values keep rendering; not a control for new stores. */
|
|
27
|
+
readonly legacyHidden?: boolean;
|
|
28
|
+
/** The semantic this setting controls — two visible fields must never share one. */
|
|
29
|
+
readonly semantic?: string;
|
|
30
|
+
/** Why a field that is not in the approved contract exists. Absent = it must be in the contract. */
|
|
31
|
+
readonly extension?: string;
|
|
32
|
+
/** The editor's label for the control (a theme's manifest build reads it; the key, humanised, when absent). */
|
|
33
|
+
readonly label?: string;
|
|
34
|
+
/** Helper text under the control. */
|
|
35
|
+
readonly info?: string;
|
|
36
|
+
/** Placeholder text in the control. */
|
|
37
|
+
readonly placeholder?: string;
|
|
38
|
+
/** The editor panel group the control is filed under. */
|
|
39
|
+
readonly group?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface SelectField<O extends string = string> extends FieldBase<'select', O> {
|
|
42
|
+
readonly options: readonly O[];
|
|
43
|
+
/** The editor's label for each option id (the id itself when absent). */
|
|
44
|
+
readonly optionLabels?: Readonly<Partial<Record<O, string>>>;
|
|
45
|
+
/** Saved values that are no longer options → the option they render as. */
|
|
46
|
+
readonly legacy?: Readonly<Record<string, O>>;
|
|
47
|
+
}
|
|
48
|
+
export interface BooleanField extends FieldBase<'boolean', boolean> {
|
|
49
|
+
}
|
|
50
|
+
export interface TextField<C extends 'text' | 'textarea' | 'richtext' | 'url' | 'color' = 'text'> extends FieldBase<C, string> {
|
|
51
|
+
}
|
|
52
|
+
export interface RangeField extends FieldBase<'range' | 'number', number> {
|
|
53
|
+
readonly min: number;
|
|
54
|
+
readonly max: number;
|
|
55
|
+
readonly step: number;
|
|
56
|
+
/** Values the design snaps to (e.g. spacer 8·12·16·24…). */
|
|
57
|
+
readonly snap?: readonly number[];
|
|
58
|
+
/** The unit the editor shows beside a range ("px", "%"). */
|
|
59
|
+
readonly unit?: string;
|
|
60
|
+
}
|
|
61
|
+
export type ResourceControl = 'image' | 'video' | 'menu' | 'collection' | 'product' | 'page' | 'blog' | 'article';
|
|
62
|
+
export interface ResourceField<C extends ResourceControl = ResourceControl> extends FieldBase<C, string, 'adapter'> {
|
|
63
|
+
}
|
|
64
|
+
export type Field = SelectField | BooleanField | TextField<'text' | 'textarea' | 'richtext' | 'url' | 'color'> | RangeField | ResourceField;
|
|
65
|
+
type Opts = {
|
|
66
|
+
legacyHidden?: boolean;
|
|
67
|
+
semantic?: string;
|
|
68
|
+
extension?: string;
|
|
69
|
+
label?: string;
|
|
70
|
+
info?: string;
|
|
71
|
+
placeholder?: string;
|
|
72
|
+
group?: string;
|
|
73
|
+
};
|
|
74
|
+
declare function select<const O extends string>(options: readonly O[], def: NoInfer<O>): SelectField<O>;
|
|
75
|
+
declare function select<const O extends string, const X extends Opts & {
|
|
76
|
+
legacy?: Record<string, NoInfer<O>>;
|
|
77
|
+
optionLabels?: Readonly<Partial<Record<NoInfer<O>, string>>>;
|
|
78
|
+
}>(options: readonly O[], def: NoInfer<O>, opts: X): SelectField<O> & X;
|
|
79
|
+
declare function resource<const C extends ResourceControl>(control: C, def?: string): ResourceField<C>;
|
|
80
|
+
declare function resource<const C extends ResourceControl, const X extends Opts>(control: C, def: string, opts: X): ResourceField<C> & X;
|
|
81
|
+
export declare const field: {
|
|
82
|
+
select: typeof select;
|
|
83
|
+
boolean: {
|
|
84
|
+
(def?: boolean | undefined): BooleanField;
|
|
85
|
+
<const X extends Opts>(def: boolean, opts: X): BooleanField & X;
|
|
86
|
+
};
|
|
87
|
+
text: {
|
|
88
|
+
(def?: string | undefined): TextField<"text">;
|
|
89
|
+
<const X extends Opts>(def: string, opts: X): TextField<"text"> & X;
|
|
90
|
+
};
|
|
91
|
+
textarea: {
|
|
92
|
+
(def?: string | undefined): TextField<"textarea">;
|
|
93
|
+
<const X extends Opts>(def: string, opts: X): TextField<"textarea"> & X;
|
|
94
|
+
};
|
|
95
|
+
richtext: {
|
|
96
|
+
(def?: string | undefined): TextField<"richtext">;
|
|
97
|
+
<const X extends Opts>(def: string, opts: X): TextField<"richtext"> & X;
|
|
98
|
+
};
|
|
99
|
+
url: {
|
|
100
|
+
(def?: string | undefined): TextField<"url">;
|
|
101
|
+
<const X extends Opts>(def: string, opts: X): TextField<"url"> & X;
|
|
102
|
+
};
|
|
103
|
+
color: {
|
|
104
|
+
(def?: string | undefined): TextField<"color">;
|
|
105
|
+
<const X extends Opts>(def: string, opts: X): TextField<"color"> & X;
|
|
106
|
+
};
|
|
107
|
+
range(def: number, min: number, max: number, step: number, opts?: Opts & {
|
|
108
|
+
snap?: readonly number[];
|
|
109
|
+
unit?: string;
|
|
110
|
+
}): RangeField;
|
|
111
|
+
/** A number input (contract control "number") — same resolution as range. */
|
|
112
|
+
number(def: number, min: number, max: number, step: number, opts?: Opts): RangeField;
|
|
113
|
+
/** A data picker: the persisted value is a handle/id; the adapter resolves it. */
|
|
114
|
+
resource: typeof resource;
|
|
115
|
+
};
|
|
116
|
+
export type Fields = Readonly<Record<string, Field>>;
|
|
117
|
+
/** Where a schema came from: section or block, and which design family owns it. */
|
|
118
|
+
export interface SchemaMeta {
|
|
119
|
+
/** global = a theme setting group (Nova 13 popup), not placed on a page. */
|
|
120
|
+
readonly kind: 'section' | 'block' | 'global';
|
|
121
|
+
readonly name: string;
|
|
122
|
+
/** Design family, e.g. '12A'. The validator reads the contract from here. */
|
|
123
|
+
readonly family: string;
|
|
124
|
+
/** Blocks a section accepts (sections only). */
|
|
125
|
+
readonly blocks?: readonly string[];
|
|
126
|
+
/** Blocks still accepted for saved stores but hidden from the picker. */
|
|
127
|
+
readonly legacyBlocks?: readonly string[];
|
|
128
|
+
/** Sections a block may be placed in (blocks only). */
|
|
129
|
+
readonly parents?: readonly string[];
|
|
130
|
+
/**
|
|
131
|
+
* Earlier persisted names of this block (blocks only). Saved content may still
|
|
132
|
+
* hold a node of that type: the section keeps accepting it (`legacyBlocks`),
|
|
133
|
+
* the theme renders it as this block, and the contract validator reads this
|
|
134
|
+
* block's contract under the old name. New content is written with `name`.
|
|
135
|
+
*/
|
|
136
|
+
readonly legacyNames?: readonly string[];
|
|
137
|
+
/** How the editor names the block in "Add block" (blocks). */
|
|
138
|
+
readonly title?: string;
|
|
139
|
+
/** One line for the editor's picker. */
|
|
140
|
+
readonly description?: string;
|
|
141
|
+
readonly icon?: string;
|
|
142
|
+
/** Route context the block needs (`product`, `collection`, …): it is offered only where the page has it. */
|
|
143
|
+
readonly requiresContext?: readonly string[];
|
|
144
|
+
}
|
|
145
|
+
export interface Schema<F extends Fields = Fields> extends SchemaMeta {
|
|
146
|
+
readonly fields: F;
|
|
147
|
+
}
|
|
148
|
+
export declare function defineSchema<const F extends Fields>(meta: SchemaMeta, fields: F): Schema<F>;
|
|
149
|
+
type ValueOf<F> = F extends SelectField<infer O> ? O : F extends BooleanField ? boolean : F extends RangeField ? number : string;
|
|
150
|
+
/**
|
|
151
|
+
* The resolved settings of a schema: every visible field required (the adapter
|
|
152
|
+
* fills defaults), every LEGACY-HIDDEN field optional.
|
|
153
|
+
*/
|
|
154
|
+
export type SettingsOf<S extends Schema> = {
|
|
155
|
+
-readonly [K in keyof S['fields'] as S['fields'][K] extends {
|
|
156
|
+
binding: 'adapter';
|
|
157
|
+
} ? never : S['fields'][K] extends {
|
|
158
|
+
legacyHidden: true;
|
|
159
|
+
} ? never : K]: ValueOf<S['fields'][K]>;
|
|
160
|
+
} & {
|
|
161
|
+
-readonly [K in keyof S['fields'] as S['fields'][K] extends {
|
|
162
|
+
binding: 'adapter';
|
|
163
|
+
} ? never : S['fields'][K] extends {
|
|
164
|
+
legacyHidden: true;
|
|
165
|
+
} ? K : never]?: ValueOf<S['fields'][K]>;
|
|
166
|
+
};
|
|
167
|
+
/** The raw adapter-bound (handle) values of a schema. */
|
|
168
|
+
export type HandlesOf<S extends Schema> = {
|
|
169
|
+
-readonly [K in keyof S['fields'] as S['fields'][K] extends {
|
|
170
|
+
binding: 'adapter';
|
|
171
|
+
} ? K : never]: string;
|
|
172
|
+
};
|
|
173
|
+
export interface SettingIssue {
|
|
174
|
+
readonly key: string;
|
|
175
|
+
readonly code: 'unknown-option' | 'legacy-value' | 'coerced' | 'unknown-setting' | 'out-of-range';
|
|
176
|
+
readonly value: unknown;
|
|
177
|
+
readonly resolved?: unknown;
|
|
178
|
+
}
|
|
179
|
+
export interface Resolved<S extends Schema> {
|
|
180
|
+
readonly settings: SettingsOf<S>;
|
|
181
|
+
readonly handles: HandlesOf<S>;
|
|
182
|
+
/** Everything the adapter had to correct — surfaced in dev and asserted in tests. */
|
|
183
|
+
readonly issues: readonly SettingIssue[];
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The adapter core: saved JSON settings → typed, complete settings.
|
|
187
|
+
*
|
|
188
|
+
* - missing → the field default
|
|
189
|
+
* - a legacy option → the option it renders as (issue: legacy-value)
|
|
190
|
+
* - an unknown option → the default (issue: unknown-option), never a crash
|
|
191
|
+
* - "true"/"false"/"32" strings → boolean / number (issue: coerced)
|
|
192
|
+
* - LEGACY-HIDDEN fields keep their saved value (that is the point of them)
|
|
193
|
+
*/
|
|
194
|
+
export declare function resolveSettings<S extends Schema>(schema: S, raw: Readonly<Record<string, unknown>> | undefined): Resolved<S>;
|
|
195
|
+
/** Defaults of a schema — what a brand-new section renders with. */
|
|
196
|
+
export declare function defaultsOf<S extends Schema>(schema: S): SettingsOf<S>;
|
|
197
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings schema DSL — the ONE place a Nova section or block declares its
|
|
3
|
+
* persisted settings in code. Everything else is derived from it:
|
|
4
|
+
*
|
|
5
|
+
* schema ──► TypeScript prop types (SettingsOf<typeof schema>)
|
|
6
|
+
* ──► the adapter (resolveSettings: defaults, legacy, coercion)
|
|
7
|
+
* ──► Storybook controls (stories/argTypes.ts)
|
|
8
|
+
* ──► contract drift validation (validate.ts, against generated/*.json)
|
|
9
|
+
*
|
|
10
|
+
* Option IDs here are the PERSISTED IDs (what a store's saved JSON holds), never
|
|
11
|
+
* display labels. Labels come from the approved contract at display time.
|
|
12
|
+
*/
|
|
13
|
+
function select(options, def, opts) {
|
|
14
|
+
return { control: 'select', options, default: def, binding: 'prop', ...opts };
|
|
15
|
+
}
|
|
16
|
+
function simple(control) {
|
|
17
|
+
function make(def, opts) {
|
|
18
|
+
return { control, default: def ?? (control === 'boolean' ? false : ''), binding: 'prop', ...opts };
|
|
19
|
+
}
|
|
20
|
+
return make;
|
|
21
|
+
}
|
|
22
|
+
function resource(control, def = '', opts) {
|
|
23
|
+
return { control, default: def, binding: 'adapter', ...opts };
|
|
24
|
+
}
|
|
25
|
+
export const field = {
|
|
26
|
+
select,
|
|
27
|
+
boolean: simple('boolean'),
|
|
28
|
+
text: simple('text'),
|
|
29
|
+
textarea: simple('textarea'),
|
|
30
|
+
richtext: simple('richtext'),
|
|
31
|
+
url: simple('url'),
|
|
32
|
+
color: simple('color'),
|
|
33
|
+
range(def, min, max, step, opts = {}) {
|
|
34
|
+
return { control: 'range', default: def, min, max, step, binding: 'prop', ...opts };
|
|
35
|
+
},
|
|
36
|
+
/** A number input (contract control "number") — same resolution as range. */
|
|
37
|
+
number(def, min, max, step, opts = {}) {
|
|
38
|
+
return { control: 'number', default: def, min, max, step, binding: 'prop', ...opts };
|
|
39
|
+
},
|
|
40
|
+
/** A data picker: the persisted value is a handle/id; the adapter resolves it. */
|
|
41
|
+
resource,
|
|
42
|
+
};
|
|
43
|
+
export function defineSchema(meta, fields) {
|
|
44
|
+
return { ...meta, fields };
|
|
45
|
+
}
|
|
46
|
+
function snapTo(value, snap) {
|
|
47
|
+
return snap.reduce((best, s) => (Math.abs(s - value) < Math.abs(best - value) ? s : best), snap[0]);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The adapter core: saved JSON settings → typed, complete settings.
|
|
51
|
+
*
|
|
52
|
+
* - missing → the field default
|
|
53
|
+
* - a legacy option → the option it renders as (issue: legacy-value)
|
|
54
|
+
* - an unknown option → the default (issue: unknown-option), never a crash
|
|
55
|
+
* - "true"/"false"/"32" strings → boolean / number (issue: coerced)
|
|
56
|
+
* - LEGACY-HIDDEN fields keep their saved value (that is the point of them)
|
|
57
|
+
*/
|
|
58
|
+
export function resolveSettings(schema, raw) {
|
|
59
|
+
const input = raw ?? {};
|
|
60
|
+
const settings = {};
|
|
61
|
+
const handles = {};
|
|
62
|
+
const issues = [];
|
|
63
|
+
for (const [key, f] of Object.entries(schema.fields)) {
|
|
64
|
+
const has = Object.prototype.hasOwnProperty.call(input, key) && input[key] !== undefined && input[key] !== null;
|
|
65
|
+
let value = has ? input[key] : f.default;
|
|
66
|
+
if (f.control === 'select') {
|
|
67
|
+
const opts = f.options;
|
|
68
|
+
if (typeof value === 'number')
|
|
69
|
+
value = String(value);
|
|
70
|
+
if (!opts.includes(value)) {
|
|
71
|
+
const legacy = f.legacy?.[value];
|
|
72
|
+
if (legacy !== undefined) {
|
|
73
|
+
issues.push({ key, code: 'legacy-value', value, resolved: legacy });
|
|
74
|
+
value = legacy;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
if (has)
|
|
78
|
+
issues.push({ key, code: 'unknown-option', value, resolved: f.default });
|
|
79
|
+
value = f.default;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (f.control === 'boolean') {
|
|
84
|
+
if (typeof value === 'string') {
|
|
85
|
+
issues.push({ key, code: 'coerced', value, resolved: value === 'true' });
|
|
86
|
+
value = value === 'true';
|
|
87
|
+
}
|
|
88
|
+
else
|
|
89
|
+
value = Boolean(value);
|
|
90
|
+
}
|
|
91
|
+
else if (f.control === 'range' || f.control === 'number') {
|
|
92
|
+
let n = typeof value === 'number' ? value : Number(value);
|
|
93
|
+
if (typeof value === 'string')
|
|
94
|
+
issues.push({ key, code: 'coerced', value, resolved: n });
|
|
95
|
+
if (!Number.isFinite(n))
|
|
96
|
+
n = f.default;
|
|
97
|
+
const clamped = Math.min(f.max, Math.max(f.min, n));
|
|
98
|
+
if (clamped !== n)
|
|
99
|
+
issues.push({ key, code: 'out-of-range', value: n, resolved: clamped });
|
|
100
|
+
value = f.snap ? snapTo(clamped, f.snap) : clamped;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
value = typeof value === 'string' ? value : String(value ?? '');
|
|
104
|
+
}
|
|
105
|
+
if (f.legacyHidden && !has)
|
|
106
|
+
continue; // a legacy field that was never saved stays absent
|
|
107
|
+
if (f.binding === 'adapter')
|
|
108
|
+
handles[key] = value;
|
|
109
|
+
else
|
|
110
|
+
settings[key] = value;
|
|
111
|
+
}
|
|
112
|
+
for (const key of Object.keys(input)) {
|
|
113
|
+
if (!(key in schema.fields))
|
|
114
|
+
issues.push({ key, code: 'unknown-setting', value: input[key] });
|
|
115
|
+
}
|
|
116
|
+
return { settings: settings, handles: handles, issues };
|
|
117
|
+
}
|
|
118
|
+
/** Defaults of a schema — what a brand-new section renders with. */
|
|
119
|
+
export function defaultsOf(schema) {
|
|
120
|
+
return resolveSettings(schema, {}).settings;
|
|
121
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Set by the platform's server `render()` around the page; never in the browser. */
|
|
2
|
+
export declare const ServerRenderProvider: import("react").Provider<boolean>;
|
|
3
|
+
/**
|
|
4
|
+
* True while the platform renders this page on the server; false in the browser — the hydrating first render
|
|
5
|
+
* included. For markup the platform adds to the delivered page after the server render (the gateway's form
|
|
6
|
+
* fields): the browser renders it so its tree matches the page it received, the server never does.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useServerRender(): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
const ServerRenderContext = createContext(false);
|
|
3
|
+
/** Set by the platform's server `render()` around the page; never in the browser. */
|
|
4
|
+
export const ServerRenderProvider = ServerRenderContext.Provider;
|
|
5
|
+
/**
|
|
6
|
+
* True while the platform renders this page on the server; false in the browser — the hydrating first render
|
|
7
|
+
* included. For markup the platform adds to the delivered page after the server render (the gateway's form
|
|
8
|
+
* fields): the browser renders it so its tree matches the page it received, the server never does.
|
|
9
|
+
*/
|
|
10
|
+
export function useServerRender() {
|
|
11
|
+
return useContext(ServerRenderContext) || typeof document === 'undefined';
|
|
12
|
+
}
|