@wix/himalaya-cli 0.1647.0 → 0.1649.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/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/dev-server/src/builder.d.ts +3 -1
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/schema.d.ts +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/core/ts/src/validator.d.ts +6 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/_types/stdlib/wix-sdk/media/src/rendition.d.ts +6 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/core.mjs +1 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/screens.mjs +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/sdui.mjs +1 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-events.mjs +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-media.mjs +22 -4
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-rich-content.mjs +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-stores-collections.mjs +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-stores.mjs +4 -0
- package/dist/authoring-runtime/node_modules/@wix/himalaya/dist/wix-video.mjs +4 -0
- package/dist/browser-plane/assets.json +5 -5
- package/dist/browser-plane/preview-deferred/{155f61519885c85d61a39fa595197071a3ca07680da56055ccb1dcff2e709cd1.bin → 4ef4d326496b4d7809746936207067b4c921f5db9878ecc7030aa3a9e4d2aaeb.bin} +1 -1
- package/dist/browser-plane/sw.js +5 -5
- package/dist/cli.mjs +4098 -3351
- package/dist/preview-assets.generated.bin +0 -0
- package/guides/component-reference.json +1 -1
- package/guides/component-reference.md +2 -0
- package/guides/designing-himi-screens.md +4 -0
- package/guides/himi-skill.md +6 -0
- package/guides/image-preview.md +92 -0
- package/guides/index.json +7 -0
- package/guides/release-notes.md +6 -0
- package/guides/sdk-reference.json +9 -2
- package/package.json +2 -2
- package/template-icons/wholesale-store.png +0 -0
- package/templates/beauty-salon-owner/dev/index.ts +7 -0
- package/templates/community-club-owner/dev/index.ts +7 -0
- package/templates/events-meetups-owner/dev/index.ts +7 -0
- package/templates/gym-fitness-owner/dev/index.ts +7 -0
- package/templates/nonprofit-cause-owner/dev/index.ts +7 -0
- package/templates/owner-lite/dev/index.ts +7 -0
- package/templates/podcast-creator-owner/dev/index.ts +7 -0
- package/templates/portfolio-creative-owner/dev/index.ts +7 -0
- package/templates/restaurant-owner/dev/index.ts +7 -0
- package/templates/retail-store-owner/dev/index.ts +7 -0
- package/templates/wholesale-store/README.md +53 -0
- package/templates/wholesale-store/config.ts +247 -0
- package/templates/wholesale-store/dev/index.ts +12194 -0
- package/templates/wholesale-store/himalaya.content.json +43 -0
- package/templates/wholesale-store/himalaya.template.json +14 -0
- package/templates/wholesale-store/icon.png +0 -0
- package/templates/wholesale-store/platforms.json +1 -0
- package/templates/wholesale-store/tier5-src/_shared/app-locale.ts +61 -0
- package/templates/wholesale-store/tier5-src/_shared/branded-lite-hosted.ts +279 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-copy.ts +40 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-history.ts +51 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-message-model.ts +177 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-model.ts +56 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-presenter.ts +576 -0
- package/templates/wholesale-store/tier5-src/_shared/business-chat-transport.ts +148 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-contract.ts +228 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-query-legacy.ts +194 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-query.ts +252 -0
- package/templates/wholesale-store/tier5-src/_shared/collection-service.ts +423 -0
- package/templates/wholesale-store/tier5-src/_shared/detail-presence.ts +51 -0
- package/templates/wholesale-store/tier5-src/_shared/empty-flags.ts +33 -0
- package/templates/wholesale-store/tier5-src/_shared/engine-intl.ts +508 -0
- package/templates/wholesale-store/tier5-src/_shared/exclusive-writes.ts +66 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-account.ts +164 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-copy.ts +89 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-store-categories.ts +108 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-store-collection.ts +22 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-store-saved.ts +57 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-store.ts +7 -0
- package/templates/wholesale-store/tier5-src/_shared/himi-text.ts +24 -0
- package/templates/wholesale-store/tier5-src/_shared/hosted-checkout.ts +70 -0
- package/templates/wholesale-store/tier5-src/_shared/iap-presentation.ts +207 -0
- package/templates/wholesale-store/tier5-src/_shared/iap-worker.ts +125 -0
- package/templates/wholesale-store/tier5-src/_shared/inbox-rooms.ts +68 -0
- package/templates/wholesale-store/tier5-src/_shared/live-session.ts +172 -0
- package/templates/wholesale-store/tier5-src/_shared/local-preferences.ts +38 -0
- package/templates/wholesale-store/tier5-src/_shared/member-access.ts +52 -0
- package/templates/wholesale-store/tier5-src/_shared/member-account-routes.ts +52 -0
- package/templates/wholesale-store/tier5-src/_shared/member-orders.ts +8 -0
- package/templates/wholesale-store/tier5-src/_shared/member-screen.ts +99 -0
- package/templates/wholesale-store/tier5-src/_shared/online-actions.ts +76 -0
- package/templates/wholesale-store/tier5-src/_shared/owner-editor-value.ts +164 -0
- package/templates/wholesale-store/tier5-src/_shared/owner-load-error.ts +348 -0
- package/templates/wholesale-store/tier5-src/_shared/payment-setup.ts +6 -0
- package/templates/wholesale-store/tier5-src/_shared/read-service.ts +255 -0
- package/templates/wholesale-store/tier5-src/_shared/restaurant-ordering.ts +201 -0
- package/templates/wholesale-store/tier5-src/_shared/rich-body.ts +78 -0
- package/templates/wholesale-store/tier5-src/_shared/rich-content-link.ts +34 -0
- package/templates/wholesale-store/tier5-src/_shared/sign-in-sheet.ts +18 -0
- package/templates/wholesale-store/tier5-src/_shared/store-order-presentation.ts +56 -0
- package/templates/wholesale-store/tier5-src/_shared/store-product-info.ts +50 -0
- package/templates/wholesale-store/tier5-src/business-chat/index.ts +724 -0
- package/templates/wholesale-store/tier5-src/himi-branded-lite-cart/index.ts +602 -0
- package/templates/wholesale-store/tier5-src/himi-branded-lite-order/index.ts +472 -0
- package/templates/wholesale-store/tier5-src/himi-branded-lite-orders/index.ts +327 -0
- package/templates/wholesale-store/tier5-src/himi-store-account/index.ts +998 -0
- package/templates/wholesale-store/tier5-src/himi-store-product/index.ts +889 -0
- package/templates/wholesale-store/tier5-src/himi-store-saved/index.ts +68 -0
- package/templates/wholesale-store/tier5-src/himi-store-shop/index.ts +150 -0
- package/templates/wholesale-store/tier5-src/himi-wholesale-home/index.ts +507 -0
- package/templates/wholesale-store/tier5-src/wix-spaces-collection-detail/index.ts +195 -0
- package/templates/wholesale-store/tokens.json +340 -0
- package/templates/yoga-studio-owner/dev/index.ts +7 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Can this host reach the member account routes My Addresses and My Wallet need?
|
|
2
|
+
//
|
|
3
|
+
// Both are served OFF www.wixapis.com (the address book on ecom.wix.com, saved cards on
|
|
4
|
+
// the member's own site), so only a native runtime whose WixApiCapability pins them can
|
|
5
|
+
// call them: HimalayaCore 1.4.0 and later. The web bridges refuse absolute URLs, and a
|
|
6
|
+
// hosted app (Spaces) signs its calls with a capability pinned to www.wixapis.com. Saved
|
|
7
|
+
// cards also need the shell to have bound its own site (app_info `memberSiteBound`): a
|
|
8
|
+
// multi-app host such as Himi Player binds none, so its wallet route has no host. The
|
|
9
|
+
// Account screen hides a link whose route is unreachable, and the screen says so instead
|
|
10
|
+
// of failing a load.
|
|
11
|
+
import { get as getAppInfo } from "@himalaya/app-info";
|
|
12
|
+
import { isHostedWorker } from "./hosted-checkout.js";
|
|
13
|
+
|
|
14
|
+
/** The HimalayaCore version whose WixApiCapability pins these routes. Checked here, at run
|
|
15
|
+
* time: the screens declare no `minBinaryVersion`, because a release takes its highest
|
|
16
|
+
* screen floor as its own and would then refuse every older binary. */
|
|
17
|
+
export const MEMBER_ACCOUNT_ROUTES_MIN_BINARY = "1.4.0";
|
|
18
|
+
export const MEMBER_MEDIA_FIELD_MIN_BINARY = "1.5.0";
|
|
19
|
+
export const MEMBER_PASSWORD_ACTIONS_MIN_BINARY = "1.5.0";
|
|
20
|
+
|
|
21
|
+
/** `version` (a HimalayaCore binary version, pre-release suffix ignored) is `floor` or later. */
|
|
22
|
+
export function atLeast(version: string, floor: string): boolean {
|
|
23
|
+
const parts = (v: string) => v.split("-")[0].split(".").map((p) => Number.parseInt(p, 10) || 0);
|
|
24
|
+
const a = parts(version), b = parts(floor);
|
|
25
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
26
|
+
const x = a[i] ?? 0, y = b[i] ?? 0;
|
|
27
|
+
if (x !== y) return x > y;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MemberAccountRoutes {
|
|
33
|
+
/** The address book on ecom.wix.com (My Addresses). */
|
|
34
|
+
addresses: boolean;
|
|
35
|
+
/** Saved cards on the member's own site (My Wallet). */
|
|
36
|
+
wallet: boolean;
|
|
37
|
+
/** Native MediaField image picking and presigned PUT support. */
|
|
38
|
+
mediaField: boolean;
|
|
39
|
+
/** Password credential actions introduced in HimalayaCore 1.5.0. */
|
|
40
|
+
passwordCredentials: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function memberAccountRoutes(): Promise<MemberAccountRoutes> {
|
|
44
|
+
if (isHostedWorker()) return { addresses: false, wallet: false, mediaField: false, passwordCredentials: false };
|
|
45
|
+
const info = await getAppInfo().catch(() => undefined);
|
|
46
|
+
if (!info) return { addresses: false, wallet: false, mediaField: false, passwordCredentials: false };
|
|
47
|
+
const native = info.platform === "ios" || info.platform === "android" || info.platform === "macos";
|
|
48
|
+
const addresses = native && atLeast(info.binaryVersion ?? "", MEMBER_ACCOUNT_ROUTES_MIN_BINARY);
|
|
49
|
+
const mediaField = native && atLeast(info.binaryVersion ?? "", MEMBER_MEDIA_FIELD_MIN_BINARY);
|
|
50
|
+
const passwordCredentials = native && atLeast(info.binaryVersion ?? "", MEMBER_PASSWORD_ACTIONS_MIN_BINARY);
|
|
51
|
+
return { addresses, wallet: addresses && info.memberSiteBound === true, mediaField, passwordCredentials };
|
|
52
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { MemberAccess as OrderMember } from "./member-access.js";
|
|
2
|
+
export { memberAccess as orderMember, assertMemberAccess as assertOrderMember, memberAccessChanged as orderAccessChanged,
|
|
3
|
+
memberAccessDenied as orderAccessDenied, type MemberAccess as OrderMember } from "./member-access.js";
|
|
4
|
+
export function ownedOrderCache(row: unknown, member: OrderMember): row is Record<string, unknown> & { id: string } {
|
|
5
|
+
if (!member.email || !member.scope || !row || typeof row !== "object") return false;
|
|
6
|
+
const value = row as Record<string, unknown>;
|
|
7
|
+
return typeof value.id === "string" && !!value.id && value.memberOrderScope === member.scope;
|
|
8
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// "Open a member-only screen" (the business chat): a member goes there; a guest gets the app's
|
|
2
|
+
// sign-in first, never the screen's own guest prompt, and the screen opens once signed in
|
|
3
|
+
// (himi-store-account resumes `returnScreenId`). Salon & Spa offers it from Home and from the
|
|
4
|
+
// shared booking screens (a treatment that cannot be booked online, a Choose a Time with no times),
|
|
5
|
+
// so each of those workers runs the same rule.
|
|
6
|
+
//
|
|
7
|
+
// The tap's params:
|
|
8
|
+
// screenId the member screen
|
|
9
|
+
// signInScreenId the app's sign-in screen; without it the tap opens `screenId` as is
|
|
10
|
+
// signInPresentation "sheet" | "full_screen": the sign-in opens as that modal, and once signed
|
|
11
|
+
// in it closes and opens the screen on `returnTab`. Pushed (no presentation),
|
|
12
|
+
// the sign-in pushes the screen itself.
|
|
13
|
+
// returnTab the tab the screen opens on after a modal sign-in
|
|
14
|
+
//
|
|
15
|
+
// A modal sign-in also carries a host-held continuation (`authContinuation`, begun here for
|
|
16
|
+
// `memberScreenTarget(screenId)`). Where the account change of the sign-in replaces the sign-in
|
|
17
|
+
// worker before it can resume (Android), the replacement consumes it to open the screen, once.
|
|
18
|
+
// A route alone never opens a member screen; old hosts without the contract keep the plain sheet.
|
|
19
|
+
import { dispatch } from "@himalaya/dispatch";
|
|
20
|
+
import type { NavDirective } from "@himalaya/state";
|
|
21
|
+
import { signedInEmail } from "./himi-account.js";
|
|
22
|
+
|
|
23
|
+
/** How long a continuation call may take before the worker goes on without it. Just above the web
|
|
24
|
+
* host's own 4 s identity-read deadline (identityReader.ts), which it runs before answering: a host
|
|
25
|
+
* that answers is never cut off, and one that never answers cannot hold the sheet or the form. */
|
|
26
|
+
export const CONTINUATION_BUDGET_MS = 4_500;
|
|
27
|
+
/** The whole tap (session read, then begin) waits at most this long. On the web host each call runs
|
|
28
|
+
* its own identity read, so a hung host would otherwise cost two full deadlines (~8 s) before the
|
|
29
|
+
* sheet. The session read may use the host's whole deadline; begin then gets what is left, but at
|
|
30
|
+
* least an ordinary read's 1.5 s, so a slow host (2.5 s a read) still hands out the token. */
|
|
31
|
+
const TAP_BUDGET_MS = CONTINUATION_BUDGET_MS + 1_500;
|
|
32
|
+
const MIN_BEGIN_MS = 1_500;
|
|
33
|
+
|
|
34
|
+
/** The continuation target for a member screen, shared with himi-store-account. */
|
|
35
|
+
export const memberScreenTarget = (screenId: string): string => `member_screen:${screenId}`;
|
|
36
|
+
|
|
37
|
+
/** A host-held continuation for `screenId`, or "" (an old host, or a member already). */
|
|
38
|
+
async function beginContinuation(screenId: string): Promise<string> {
|
|
39
|
+
try {
|
|
40
|
+
const value = await dispatch("himi.wix.authContinuation", { operation: "begin", target: memberScreenTarget(screenId) }) as
|
|
41
|
+
{ supported?: unknown; token?: unknown; payload?: { supported?: unknown; token?: unknown } } | null;
|
|
42
|
+
const fields = value?.payload ?? value;
|
|
43
|
+
return fields?.supported === true && typeof fields.token === "string" ? fields.token : "";
|
|
44
|
+
} catch {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const strField = (arg: unknown, key: string): string => {
|
|
50
|
+
const value = (arg as Record<string, unknown> | undefined)?.[key];
|
|
51
|
+
return typeof value === "string" ? value : "";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** `read`, or `fallback` once `ms` pass, where the engine has a timer. A bare JSContext has no
|
|
55
|
+
* timer; there it waits, as every read does. `read` must not reject. */
|
|
56
|
+
export function within<T>(read: Promise<T>, ms: number, fallback: T): Promise<T> {
|
|
57
|
+
const host = globalThis as { setTimeout?: (fn: () => void, ms: number) => unknown; clearTimeout?: (timer: unknown) => void };
|
|
58
|
+
if (typeof host.setTimeout !== "function") return read;
|
|
59
|
+
return new Promise<T>((resolve) => {
|
|
60
|
+
const timer = host.setTimeout!(() => resolve(fallback), ms);
|
|
61
|
+
void read.then((value) => { host.clearTimeout?.(timer); resolve(value); });
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Where a tap goes, given the session's email ("" for a guest). null: no screen named. */
|
|
66
|
+
export function memberScreenNav(arg: unknown, email: string): NavDirective | null {
|
|
67
|
+
const screenId = strField(arg, "screenId"), signInScreenId = strField(arg, "signInScreenId");
|
|
68
|
+
if (!screenId) return null;
|
|
69
|
+
if (!signInScreenId || email) return { screenId };
|
|
70
|
+
const args: Record<string, string> = { mode: "signin", returnScreenId: screenId };
|
|
71
|
+
const presentation = strField(arg, "signInPresentation");
|
|
72
|
+
if (presentation !== "sheet" && presentation !== "full_screen") return { screenId: signInScreenId, args };
|
|
73
|
+
if (strField(arg, "returnTab")) args.returnTab = strField(arg, "returnTab");
|
|
74
|
+
return { screenId: signInScreenId, presentation, args: { ...args, modal: "1" } };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One worker's opener. The session is asked at the tap, never read from the last load; a read
|
|
78
|
+
* that never answers counts as a guest, so the tap opens the sign-in rather than nothing. Only the
|
|
79
|
+
* newest tap navigates (no second sheet, no sheet over the screen): an older one answers null. */
|
|
80
|
+
// The session read has the continuation calls' budget: on the web host they all wait on the same
|
|
81
|
+
// identity read, so a shorter cutoff here would take a slow member for a guest and flash the sheet.
|
|
82
|
+
export function memberScreenOpener(readEmail: () => Promise<string> = () => within(signedInEmail(), CONTINUATION_BUDGET_MS, "")): (arg: unknown) => Promise<NavDirective | null> {
|
|
83
|
+
let taps = 0;
|
|
84
|
+
return async (arg) => {
|
|
85
|
+
if (!strField(arg, "screenId")) return null;
|
|
86
|
+
const mine = ++taps;
|
|
87
|
+
const tapped = Date.now();
|
|
88
|
+
const email = strField(arg, "signInScreenId") ? await readEmail() : "";
|
|
89
|
+
if (mine !== taps) return null;
|
|
90
|
+
const nav = memberScreenNav(arg, email);
|
|
91
|
+
if (!nav || !("presentation" in nav) || !nav.args?.modal) return nav;
|
|
92
|
+
// Bounded by what the tap has left (TAP_BUDGET_MS): a host that never answers still opens the
|
|
93
|
+
// plain sheet.
|
|
94
|
+
const left = Math.min(CONTINUATION_BUDGET_MS, Math.max(MIN_BEGIN_MS, TAP_BUDGET_MS - (Date.now() - tapped)));
|
|
95
|
+
const token = await within(beginContinuation(strField(arg, "screenId")), left, "");
|
|
96
|
+
if (mine !== taps) return null;
|
|
97
|
+
return token ? { ...nav, args: { ...nav.args, authContinuation: token } } : nav;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { connectivity, defineService as register, type ServiceDefinition } from '@himalaya/state';
|
|
2
|
+
|
|
3
|
+
// Template generation enables this behavior only in the selected two starter families.
|
|
4
|
+
export const offlineWritesEnabled = false;
|
|
5
|
+
|
|
6
|
+
export const offlineWriteMessage = 'You’re offline. Connect to save this change, then submit it again.';
|
|
7
|
+
|
|
8
|
+
/** The guard runs before the reducer: entered values survive and no optimistic
|
|
9
|
+
* changes, success state, or queued writes can occur. It never submits on reconnect. */
|
|
10
|
+
export function offlineWriteState<S extends object>(state: S): S {
|
|
11
|
+
const next: Record<string, unknown> = { ...state, error: offlineWriteMessage, message: offlineWriteMessage };
|
|
12
|
+
for (const key of ['saving', 'isSaving', 'submitting', 'isSubmitting', 'adding', 'added', 'bookingInFlight']) {
|
|
13
|
+
if (key in next) next[key] = false;
|
|
14
|
+
}
|
|
15
|
+
for (const key of ['savedMessage', 'successMessage']) {
|
|
16
|
+
if (key in next) next[key] = '';
|
|
17
|
+
}
|
|
18
|
+
// A saved/favourited item is business state, not a submission-success flag.
|
|
19
|
+
return next as S;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface OfflineWriteOptions<S> {
|
|
23
|
+
derive?: (state: S) => S;
|
|
24
|
+
/** Exempt only non-mutating branches of a registered action. */
|
|
25
|
+
requiresConnectivity?: (action: string, state: S, argument?: unknown) => boolean;
|
|
26
|
+
messageFields?: readonly string[] | ((action: string) => readonly string[]);
|
|
27
|
+
draftFields?: readonly string[] | Readonly<Record<string, string>>;
|
|
28
|
+
secretFields?: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function withOnlineWrites<S extends object>(registerService: typeof register, names: readonly string[], options: OfflineWriteOptions<S> = {}): typeof register {
|
|
32
|
+
if (!offlineWritesEnabled) return registerService;
|
|
33
|
+
return ((name: string, definition: ServiceDefinition<Record<string, unknown>>) => {
|
|
34
|
+
let currentState: (() => Record<string, unknown>) | undefined;
|
|
35
|
+
const handle = registerService(name, {
|
|
36
|
+
...definition,
|
|
37
|
+
notHydrated: [...new Set([...(definition.notHydrated ?? []), ...(options.secretFields ?? [])])],
|
|
38
|
+
actions: Object.fromEntries(Object.entries(definition.actions ?? {}).map(([action, reduce]) => [action,
|
|
39
|
+
(state: Record<string, unknown>, ...args: never[]) => {
|
|
40
|
+
if (!names.includes(action)) return reduce(state, ...args);
|
|
41
|
+
if (options.requiresConnectivity?.(action, state as S, args[0]) === false) return reduce(state, ...args);
|
|
42
|
+
const fields = typeof options.messageFields === 'function' ? options.messageFields(action) : options.messageFields ?? ['message'];
|
|
43
|
+
if (connectivity().online) {
|
|
44
|
+
let cleared = false;
|
|
45
|
+
for (const field of ['error', 'message', ...fields]) {
|
|
46
|
+
if (state[field] === offlineWriteMessage) { state[field] = ''; cleared = true; }
|
|
47
|
+
}
|
|
48
|
+
// Keep the runtime's state object: reducers may mutate it and return undefined.
|
|
49
|
+
const result = reduce(state, ...args);
|
|
50
|
+
if (!cleared) return result;
|
|
51
|
+
// Publish cleared feedback even for a void reducer. An async completion must
|
|
52
|
+
// keep any newer state published by another action while it was awaiting.
|
|
53
|
+
const finish = (next: Record<string, unknown> | undefined) => next === undefined ? currentState?.() ?? state : next;
|
|
54
|
+
return result && typeof (result as Promise<unknown>).then === 'function'
|
|
55
|
+
? Promise.resolve(result).then(finish)
|
|
56
|
+
: finish(result as Record<string, unknown> | undefined);
|
|
57
|
+
}
|
|
58
|
+
const draft = { ...state };
|
|
59
|
+
const argument = args[0] as Record<string, unknown> | undefined;
|
|
60
|
+
const draftFields = options.draftFields ?? [];
|
|
61
|
+
const entries = Array.isArray(draftFields) ? draftFields.map(key => [key, key]) : Object.entries(draftFields);
|
|
62
|
+
for (const [key, source] of entries) {
|
|
63
|
+
if (argument && key in state && typeof argument[source] === typeof state[key]) draft[key] = argument[source];
|
|
64
|
+
}
|
|
65
|
+
for (const key of options.secretFields ?? []) draft[key] = '';
|
|
66
|
+
const feedback = Object.fromEntries(fields.map(key => [key, offlineWriteMessage]));
|
|
67
|
+
let next = { ...offlineWriteState(draft), ...feedback };
|
|
68
|
+
if (options.derive) next = options.derive(next as S) as Record<string, unknown>;
|
|
69
|
+
return { ...next, ...feedback };
|
|
70
|
+
},
|
|
71
|
+
])),
|
|
72
|
+
});
|
|
73
|
+
if (typeof handle.getState === 'function') currentState = () => handle.getState();
|
|
74
|
+
return handle;
|
|
75
|
+
}) as typeof register;
|
|
76
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// [rico] #3371 — how an owner form reads a RichContentEditor out of `submit(s, values)`.
|
|
2
|
+
//
|
|
3
|
+
// The editor writes its Ricos document into the Form's value bag as a JSON STRING under its `name`,
|
|
4
|
+
// on every change and again on blur. The value is ABSENT when the editor never became ready (the
|
|
5
|
+
// engine page is not bundled, failed, or timed out): the stored body must then stay exactly as it is
|
|
6
|
+
// (user decision, 2026-09-26 — save everything but the body). A document is written exactly as the
|
|
7
|
+
// editor produced it: never re-encoded (encodeRichContent is the VIEWER's; it rewrites media ids and
|
|
8
|
+
// drops nodes the owner never touched).
|
|
9
|
+
// Inside the platform source this import is a relative path, so node test runs resolve it; an app's
|
|
10
|
+
// copy imports "@himalaya/wix-rich-content".
|
|
11
|
+
import { parseRicosJson, isPlainRicos, richContentOverLimits, type RicosDocument } from "@himalaya/wix-rich-content";
|
|
12
|
+
|
|
13
|
+
export type EditorValue =
|
|
14
|
+
| { kind: "absent" } // leave the stored body untouched
|
|
15
|
+
| { kind: "unchanged" } // byte-identical to the seed: nothing to write
|
|
16
|
+
| { kind: "doc"; doc: RicosDocument; json: string } // write `doc`; `json` is the editor's own string
|
|
17
|
+
| { kind: "too_large" } // past the rich-content limits: save the rest, say why
|
|
18
|
+
| { kind: "invalid" }; // not a Ricos document: refuse, write nothing
|
|
19
|
+
|
|
20
|
+
const TEXT_ONLY = new Set(["PARAGRAPH", "TEXT", "HEADING", "BULLETED_LIST", "ORDERED_LIST", "LIST_ITEM", "BLOCKQUOTE", "CODE_BLOCK"]);
|
|
21
|
+
/** No visible text and no non-text node (an image, a divider, an embed…): an empty body. */
|
|
22
|
+
export function isEmptyDocument(doc: RicosDocument): boolean {
|
|
23
|
+
const empty = (nodes: RicosDocument["nodes"] | undefined): boolean => (nodes ?? []).every((n) =>
|
|
24
|
+
TEXT_ONLY.has(String(n?.type)) && !String(n.textData?.text ?? "").trim() && empty(n.nodes));
|
|
25
|
+
return empty(doc.nodes);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A Ricos JSON string checked as every viewer would: the limits first, then the shape. */
|
|
29
|
+
function checkedDocument(raw: string): EditorValue {
|
|
30
|
+
// Every viewer renders a body past the limits as its fallback, so it is never written. One parse
|
|
31
|
+
// feeds both checks; a string past the byte limit is refused before it is parsed at all.
|
|
32
|
+
if (richContentOverLimits(raw, null) === "too_large") return { kind: "too_large" };
|
|
33
|
+
const doc = parseRicosJson(raw);
|
|
34
|
+
if (!doc) return { kind: "invalid" };
|
|
35
|
+
return richContentOverLimits(raw, doc) ? { kind: "too_large" } : { kind: "doc", doc, json: raw };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `raw` is `values[name]`; `seededJson` is the string the form published as the editor's seed. */
|
|
39
|
+
export function editorValue(raw: unknown, seededJson: string): EditorValue {
|
|
40
|
+
if (typeof raw !== "string" || raw === "") return { kind: "absent" };
|
|
41
|
+
if (seededJson !== "" && raw === seededJson) return { kind: "unchanged" };
|
|
42
|
+
const checked = checkedDocument(raw);
|
|
43
|
+
// An empty editor over no body (a record that had none) writes nothing, rather than an empty document.
|
|
44
|
+
if (checked.kind === "doc" && seededJson === "" && isEmptyDocument(checked.doc)) return { kind: "unchanged" };
|
|
45
|
+
return checked;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── The per-field editor machinery every owner form shares (post body, event / product description,
|
|
49
|
+
// FAQ answer). Each form keeps `<key>Json` (the seed), `<key>Pending` and `<key>Dirty` in its state.
|
|
50
|
+
|
|
51
|
+
/** `<key>Pending`: until this run's load lands, the descriptor hides the editor — it seeds ONCE, so a
|
|
52
|
+
* provisional editor the owner typed into would keep that text over the real document.
|
|
53
|
+
* `<key>Dirty`: the editor's on_change fired since the field was loaded or saved. Once ready the bag
|
|
54
|
+
* ALWAYS holds a document — the engine's own re-serialization of the seed, not byte-equal to it — so
|
|
55
|
+
* only this says the owner edited the field. Every arm fires a pending on_change before it releases a
|
|
56
|
+
* Form submit, so an edit made inside the debounce window is reported before the submit reads it. */
|
|
57
|
+
export interface EditorGate { pending: boolean; dirty: boolean }
|
|
58
|
+
|
|
59
|
+
/** What a live submit does with `values[name]`: only an edit the editor reported counts. */
|
|
60
|
+
export function editorSubmitValue(raw: unknown, seededJson: string, gate: EditorGate): EditorValue {
|
|
61
|
+
return gate.pending || !gate.dirty ? { kind: "absent" } : editorValue(raw, seededJson);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A queued write's document. A replay runs in a FRESH worker with no editor, so the string is checked
|
|
65
|
+
* again (shape and limits); "absent" when the item carries none. Never "unchanged": it was an edit. */
|
|
66
|
+
export function queuedDocument(raw: unknown): EditorValue {
|
|
67
|
+
return typeof raw === "string" && raw !== "" ? checkedDocument(raw) : { kind: "absent" };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The editor's on_change: the field is now edited, and a confirmation of an earlier save is stale.
|
|
71
|
+
* No timing window: an arm fires the pending on_change BEFORE it releases a submit, so any on_change
|
|
72
|
+
* that arrives after a save carries an edit that save did not write. */
|
|
73
|
+
export function editorChanged<S extends { status: string; savedMessage: string }>(s: S): Pick<S, "status" | "savedMessage"> {
|
|
74
|
+
return { status: s.status === "saved" ? "idle" : s.status, savedMessage: "" };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The offline cache's verdict on the stored body (`<key>Protected`): rich unless KNOWN plain. From the
|
|
78
|
+
* document when the form knows it; otherwise plain only when the record was loaded as plain. A bundle
|
|
79
|
+
* rolled back past the editor reads this flag to decide whether its plain field may overwrite the body. */
|
|
80
|
+
export function cachedRichVerdict(doc: RicosDocument | null, knownPlain: boolean): "true" | "false" {
|
|
81
|
+
if (doc) return isPlainRicos(doc) ? "false" : "true";
|
|
82
|
+
return knownPlain ? "false" : "true";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The keys of one editor field that describe THIS run, never the last one: the seed (`<key>Json`),
|
|
86
|
+
* the load gate (`<key>Pending`), the edit flag (`<key>Dirty`) and its counter (`<key>Edits`). A form
|
|
87
|
+
* lists them in `defineService(name, { notHydrated })`, so a cold boot takes their DECLARED values: the
|
|
88
|
+
* editor seeds once, and a persisted seed would be painted as its content and saved back over a newer
|
|
89
|
+
* record. */
|
|
90
|
+
export function editorRunKeys<K extends string>(key: K): [`${K}Json`, `${K}Pending`, `${K}Dirty`, `${K}Edits`] {
|
|
91
|
+
return [`${key}Json`, `${key}Pending`, `${key}Dirty`, `${key}Edits`];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Does the form already hold `target` with its editor run in progress? A `load` for it then keeps the form as
|
|
95
|
+
* it is: on the way back to a warm worker (its refresh and onAppear) a re-read would race an edit in flight and
|
|
96
|
+
* reset the reported edit, and the editor, which seeds once, would show another document than the state holds.
|
|
97
|
+
* An edit form holds its record once the editor loaded; a create form holds a draft once the owner edited. */
|
|
98
|
+
export function holdsLoadedForm(state: object, target: { id: string; mode: string }, key: string): boolean {
|
|
99
|
+
const s = state as Record<string, unknown>;
|
|
100
|
+
if (s.status === 'error' || s.error || s.mode !== target.mode || (s.id ?? '') !== target.id) return false;
|
|
101
|
+
return target.mode === 'create' ? s[`${key}Dirty`] === true || Number(s[`${key}Edits`] ?? 0) > 0 : s[`${key}Pending`] === false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The same keys on a runtime that ignores `notHydrated` (a binary from before #3644, still served new
|
|
105
|
+
* bundles over the air): reset them in place, before any action runs. A no-op where the runtime
|
|
106
|
+
* honours the option (`himalaya.hydrationOptOut`). */
|
|
107
|
+
export function resetHydratedEditorOnOlderRuntime(svc: unknown, key: string, pendingAtBoot: boolean, extra: Record<string, unknown> = {}): void {
|
|
108
|
+
if ((globalThis as { himalaya?: { hydrationOptOut?: unknown } }).himalaya?.hydrationOptOut === true) return;
|
|
109
|
+
const booted = (svc as { getState?: () => Record<string, unknown> | undefined }).getState?.();
|
|
110
|
+
if (!booted || typeof booted !== "object") return;
|
|
111
|
+
if (booted[`${key}Json`]) booted[`${key}Json`] = "";
|
|
112
|
+
booted[`${key}Pending`] = pendingAtBoot;
|
|
113
|
+
booted[`${key}Dirty`] = false;
|
|
114
|
+
booted[`${key}Edits`] = 0;
|
|
115
|
+
Object.assign(booted, extra);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** What an awaited save leaves the field as, read from the LIVE state when it lands. An on_change that
|
|
119
|
+
* arrived while the save was in flight (`<key>Edits` moved) is an edit the save did not write: the
|
|
120
|
+
* field stays dirty and the save's confirmation is stale. Otherwise a save that wrote the field
|
|
121
|
+
* re-bases it as saved. `live` is the service's state now (none, or one that never moved: the start state). */
|
|
122
|
+
export function editorAfterSave(key: string, start: Record<string, unknown>, live: Record<string, unknown> | undefined, wrote: boolean):
|
|
123
|
+
{ dirty: boolean; edits: number; editedDuringSave: boolean } {
|
|
124
|
+
const atStart = Number(start[`${key}Edits`] ?? 0);
|
|
125
|
+
const now = Number(live?.[`${key}Edits`] ?? atStart);
|
|
126
|
+
// Only a counter that MOVED ON counts; one behind the start is a host that does not apply results.
|
|
127
|
+
const editedDuringSave = now > atStart;
|
|
128
|
+
return editedDuringSave
|
|
129
|
+
? { dirty: true, edits: now, editedDuringSave }
|
|
130
|
+
: { dirty: wrote ? false : Boolean(start[`${key}Dirty`]), edits: atStart, editedDuringSave };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The live state of a service handle, when the runtime gives one. */
|
|
134
|
+
export function liveState(svc: unknown): Record<string, unknown> | undefined {
|
|
135
|
+
const st = (svc as { getState?: () => unknown } | undefined)?.getState?.();
|
|
136
|
+
return st && typeof st === "object" ? st as Record<string, unknown> : undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** An awaited action's result, laid over the LIVE state: only the keys the action CHANGED (against the
|
|
140
|
+
* state it started from) are written, so anything the owner did while it awaited — an edit, a field in
|
|
141
|
+
* the details sheet, closing the sheet — survives a save that lands after it, success or failure. A
|
|
142
|
+
* key both changed goes to the action. Where the host gives no live state, the result as it is. */
|
|
143
|
+
/** An awaited load's result, unless the form moved to another record while it read. On a warm return the host's bare
|
|
144
|
+
* refresh and the screen's onAppear run together: after a save the refresh fetches the record just saved while the
|
|
145
|
+
* onAppear of a create screen opens a blank form, and the refresh landing last turned the new form into that record's
|
|
146
|
+
* edit. Nothing lands then (`undefined`, the runtime's no-op), nor from a load that changes nothing: started with the
|
|
147
|
+
* same state as the onAppear, it would put back the record that one replaced. A load for the record the form still
|
|
148
|
+
* holds lands as it is; so does any load where the host gives no live state. */
|
|
149
|
+
export function landsOnLive<S extends object>(svc: unknown, start: S, result: S): S | undefined {
|
|
150
|
+
if (result === start) return undefined;
|
|
151
|
+
const now = liveState(svc);
|
|
152
|
+
if (!now || now === start) return result;
|
|
153
|
+
const s0 = start as Record<string, unknown>;
|
|
154
|
+
return now.id !== s0.id || now.mode !== s0.mode ? undefined : result;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function settleOverLive<S extends object>(svc: unknown, start: S, result: S): S {
|
|
158
|
+
const now = liveState(svc) as S | undefined;
|
|
159
|
+
if (!now || now === start || now === result) return result;
|
|
160
|
+
const out = { ...now } as Record<string, unknown>;
|
|
161
|
+
const r = result as Record<string, unknown>, s0 = start as Record<string, unknown>;
|
|
162
|
+
for (const k of Object.keys(r)) if (!Object.is(r[k], s0[k])) out[k] = r[k];
|
|
163
|
+
return out as S;
|
|
164
|
+
}
|