@thecms/next 0.0.0-stage → 0.20.1-dev.122
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/chunk-NLO5OID2.js +175 -0
- package/dist/client.d.ts +133 -0
- package/dist/client.js +178 -0
- package/dist/kit.d.ts +87 -0
- package/dist/kit.js +150 -0
- package/dist/server.d.ts +313 -0
- package/dist/server.js +1177 -0
- package/package.json +52 -4
- package/README.md +0 -3
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// src/blocks.ts
|
|
2
|
+
import { cache, createElement, Fragment } from "react";
|
|
3
|
+
import { CmsEditorBridge, CmsPageLive } from "./client.js";
|
|
4
|
+
function editorMark(block) {
|
|
5
|
+
return block._editor;
|
|
6
|
+
}
|
|
7
|
+
function markForEditor(page, site) {
|
|
8
|
+
const of = { view: page.id, entity: page.entity, record: page.record, locale: page._locale.resolved };
|
|
9
|
+
const walk = (blocks, root) => {
|
|
10
|
+
for (const block of blocks) {
|
|
11
|
+
block._editor = root ? { ...of, root, site, ...page._live ? { live: page._live } : {} } : { ...of, root };
|
|
12
|
+
for (const slot of Object.values(block.slots ?? {})) walk(slot, false);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
walk(page.body, true);
|
|
16
|
+
}
|
|
17
|
+
var registry = null;
|
|
18
|
+
var perRequest = null;
|
|
19
|
+
function registerComponents(publicKeys, components) {
|
|
20
|
+
const keys = new Set(typeof publicKeys === "string" ? [publicKeys] : publicKeys);
|
|
21
|
+
if (registry && [...keys].some((k) => !registry.publicKeys.has(k))) {
|
|
22
|
+
throw new Error("theCMS.: CmsBlocks draws the sites a server declares \u2014 createNextCms was already given components for other public keys than these");
|
|
23
|
+
}
|
|
24
|
+
registry = { publicKeys: keys, components };
|
|
25
|
+
}
|
|
26
|
+
function isComponent(value) {
|
|
27
|
+
return typeof value === "function" || typeof value === "object" && value !== null && "$$typeof" in value;
|
|
28
|
+
}
|
|
29
|
+
function componentOf(components, block) {
|
|
30
|
+
const entry = components[block.set];
|
|
31
|
+
if (!entry) return void 0;
|
|
32
|
+
if (isComponent(entry)) return entry;
|
|
33
|
+
return block.shape != null ? entry[block.shape] : void 0;
|
|
34
|
+
}
|
|
35
|
+
var warned = /* @__PURE__ */ new Set();
|
|
36
|
+
function warnMissing(block) {
|
|
37
|
+
const what = `${block.set}/${block.shape ?? ""}`;
|
|
38
|
+
if (warned.has(what)) return;
|
|
39
|
+
warned.add(what);
|
|
40
|
+
console.warn(`theCMS.: no component for the set "${block.set}"${block.shape ? `, shape "${block.shape}"` : ""} \u2014 add it to createNextCms({ components })`);
|
|
41
|
+
}
|
|
42
|
+
function wrapped(block, drawn, problem = {}) {
|
|
43
|
+
return createElement("cms-block", {
|
|
44
|
+
key: block.uid,
|
|
45
|
+
"data-cms-block": "",
|
|
46
|
+
"data-uid": block.uid,
|
|
47
|
+
"data-in": block.in,
|
|
48
|
+
"data-set": block.set,
|
|
49
|
+
...block.shape ? { "data-shape": block.shape } : {},
|
|
50
|
+
...problem.missing ? { "data-missing": "" } : {},
|
|
51
|
+
...problem.error ? { "data-error": problem.error } : {},
|
|
52
|
+
style: { display: "contents" }
|
|
53
|
+
}, drawn);
|
|
54
|
+
}
|
|
55
|
+
function drawBlock(components, block) {
|
|
56
|
+
const inEditor = editorMark(block) !== void 0;
|
|
57
|
+
if (block._error || block._geoBlocked) {
|
|
58
|
+
return inEditor ? wrapped(block, null, { error: block._geoBlocked ? "geoBlocked" : block._error }) : null;
|
|
59
|
+
}
|
|
60
|
+
const Component = componentOf(components, block);
|
|
61
|
+
if (!Component) {
|
|
62
|
+
warnMissing(block);
|
|
63
|
+
return inEditor ? wrapped(block, null, { missing: true }) : null;
|
|
64
|
+
}
|
|
65
|
+
const drawn = createElement(Component, { key: block.uid, block });
|
|
66
|
+
return inEditor ? wrapped(block, drawn) : drawn;
|
|
67
|
+
}
|
|
68
|
+
var drawing = () => (perRequest ??= cache(() => ({ context: null })))();
|
|
69
|
+
function pick(tree, key) {
|
|
70
|
+
return key.split(".").reduce((v, k) => v && typeof v === "object" ? v[k] : void 0, tree);
|
|
71
|
+
}
|
|
72
|
+
function translated(value, locales) {
|
|
73
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || "url" in value) return false;
|
|
74
|
+
const keys = Object.keys(value);
|
|
75
|
+
return keys.length > 0 && keys.every((k) => locales.includes(k));
|
|
76
|
+
}
|
|
77
|
+
function textsOf(site, locale) {
|
|
78
|
+
const locales = site.locales ?? [];
|
|
79
|
+
return {
|
|
80
|
+
t: (key) => {
|
|
81
|
+
const v = pick(site.messages?.[locale], key) ?? pick(site.messages?.[site.defaultLocale], key);
|
|
82
|
+
return typeof v === "string" ? v : key;
|
|
83
|
+
},
|
|
84
|
+
g: (key) => {
|
|
85
|
+
const v = site.globals?.[key];
|
|
86
|
+
return translated(v, locales) ? v[locale] ?? v[site.defaultLocale] : v;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function setCmsContext(page, site, path) {
|
|
91
|
+
const locale = page._locale.resolved;
|
|
92
|
+
const firstOwn = (blocks) => {
|
|
93
|
+
for (const b of blocks) {
|
|
94
|
+
if (b.in === "view") return b;
|
|
95
|
+
for (const slot of Object.values(b.slots ?? {})) {
|
|
96
|
+
const found = firstOwn(slot);
|
|
97
|
+
if (found) return found;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return void 0;
|
|
101
|
+
};
|
|
102
|
+
const first = firstOwn(page.body)?.uid;
|
|
103
|
+
const context = {
|
|
104
|
+
page,
|
|
105
|
+
locale,
|
|
106
|
+
path,
|
|
107
|
+
alternates: (page._route?.alternates ?? []).filter((a) => a.locale !== "x-default"),
|
|
108
|
+
site,
|
|
109
|
+
...textsOf(site, locale),
|
|
110
|
+
isFirst: (block) => !!first && block.uid === first
|
|
111
|
+
};
|
|
112
|
+
drawing().context = context;
|
|
113
|
+
return context;
|
|
114
|
+
}
|
|
115
|
+
function cmsContext() {
|
|
116
|
+
const context = drawing().context;
|
|
117
|
+
if (!context) throw new Error("theCMS.: cmsContext() has no page \u2014 call it while drawing a page given by cms.page() (server components only)");
|
|
118
|
+
return context;
|
|
119
|
+
}
|
|
120
|
+
function drawList(blocks) {
|
|
121
|
+
if (!registry) throw new Error("theCMS.: CmsBlocks needs the site's components \u2014 createNextCms({ publicKey, components })");
|
|
122
|
+
const components = registry.components;
|
|
123
|
+
return blocks.map((block) => drawBlock(components, block));
|
|
124
|
+
}
|
|
125
|
+
function bridgeOf(blocks) {
|
|
126
|
+
const root = blocks.map(editorMark).find((m) => !!m?.root && !!m.site);
|
|
127
|
+
return root ? createElement(CmsEditorBridge, {
|
|
128
|
+
key: "thecms:editor",
|
|
129
|
+
mark: { view: root.view, entity: root.entity, record: root.record, locale: root.locale, site: root.site, live: root.live }
|
|
130
|
+
}) : null;
|
|
131
|
+
}
|
|
132
|
+
function liveOf(page, visitor) {
|
|
133
|
+
if (!registry || page._preview || !page._live && !page._visit) return null;
|
|
134
|
+
const publicKey = page._cms?.publicKey ?? (registry.publicKeys.size === 1 ? [...registry.publicKeys][0] : void 0);
|
|
135
|
+
if (!publicKey) return null;
|
|
136
|
+
const mark = {
|
|
137
|
+
site: { publicKey },
|
|
138
|
+
...page._live ? { live: page._live } : {},
|
|
139
|
+
...page._visit ? { visit: page._visit } : {},
|
|
140
|
+
...visitor ? { visitor } : {}
|
|
141
|
+
};
|
|
142
|
+
return createElement(CmsPageLive, { key: "thecms:live", mark });
|
|
143
|
+
}
|
|
144
|
+
function CmsBlocks(props) {
|
|
145
|
+
if (!props.page) {
|
|
146
|
+
const list = props.blocks ?? [];
|
|
147
|
+
return createElement(Fragment, null, ...drawList(list), bridgeOf(list));
|
|
148
|
+
}
|
|
149
|
+
const body = props.page.body;
|
|
150
|
+
const bridge = bridgeOf(body) ?? liveOf(props.page, props.visitor);
|
|
151
|
+
const first = body.findIndex((b) => b.in === "view");
|
|
152
|
+
if (!props.main || first < 0) return createElement(Fragment, null, ...drawList(body), bridge);
|
|
153
|
+
let last = first;
|
|
154
|
+
for (let i = body.length - 1; i > first; i--) if (body[i].in === "view") {
|
|
155
|
+
last = i;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
return createElement(
|
|
159
|
+
Fragment,
|
|
160
|
+
null,
|
|
161
|
+
...drawList(body.slice(0, first)),
|
|
162
|
+
createElement("main", { key: "thecms:main", id: "main" }, ...drawList(body.slice(first, last + 1))),
|
|
163
|
+
...drawList(body.slice(last + 1)),
|
|
164
|
+
bridge
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export {
|
|
169
|
+
markForEditor,
|
|
170
|
+
registerComponents,
|
|
171
|
+
textsOf,
|
|
172
|
+
setCmsContext,
|
|
173
|
+
cmsContext,
|
|
174
|
+
CmsBlocks
|
|
175
|
+
};
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { EditorPage, Sdk } from '@thecms/sdk';
|
|
3
|
+
|
|
4
|
+
/** What the browser needs to talk to the CMS (D186, SITE-KEY D255): carried by every page, never a secret. */
|
|
5
|
+
interface CmsClientConfig {
|
|
6
|
+
/** the application's public key: the browser's SDK finds the site from it */
|
|
7
|
+
publicKey: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* KIT (D383): the messages of the pages the CMS does not give (not found, an
|
|
11
|
+
* error, a page blocked in the visitor's country), by fixed key —
|
|
12
|
+
* `notFound.title`, `notFound.body`, `notFound.home`, `error.title`,
|
|
13
|
+
* `error.body`, `error.retry`, `geoBlocked.title`, `geoBlocked.body`.
|
|
14
|
+
*/
|
|
15
|
+
declare const SITE_TEXT_KEYS: {
|
|
16
|
+
readonly notFound: readonly ["title", "body", "home"];
|
|
17
|
+
readonly error: readonly ["title", "body", "retry"];
|
|
18
|
+
readonly geoBlocked: readonly ["title", "body"];
|
|
19
|
+
};
|
|
20
|
+
type Group<G extends keyof typeof SITE_TEXT_KEYS> = Record<(typeof SITE_TEXT_KEYS)[G][number], string>;
|
|
21
|
+
/** What `CmsSiteTexts` hands to the client components (`useSiteTexts()`): plain data. */
|
|
22
|
+
interface SiteTexts {
|
|
23
|
+
/** the language of the address */
|
|
24
|
+
locale: string;
|
|
25
|
+
/** the Site name, null when not set */
|
|
26
|
+
name: string | null;
|
|
27
|
+
/** the home of the address's language first, then the other languages' */
|
|
28
|
+
homes: {
|
|
29
|
+
locale: string;
|
|
30
|
+
path: string;
|
|
31
|
+
}[];
|
|
32
|
+
notFound: Group<"notFound">;
|
|
33
|
+
error: Group<"error">;
|
|
34
|
+
geoBlocked: Group<"geoBlocked">;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Anything `cms.page()` (or the SDK) answered: `_cms` from `@thecms/next`, `_live` / `_visit` from the API. */
|
|
38
|
+
interface CmsData {
|
|
39
|
+
_cms?: CmsClientConfig;
|
|
40
|
+
/** PREVIEW: drafts shown through a preview session */
|
|
41
|
+
_preview?: true;
|
|
42
|
+
_live?: {
|
|
43
|
+
tags: string[];
|
|
44
|
+
url: string;
|
|
45
|
+
channel?: "preview";
|
|
46
|
+
listen?: string;
|
|
47
|
+
};
|
|
48
|
+
_visit?: string;
|
|
49
|
+
}
|
|
50
|
+
type Fetch = (input: string, init?: RequestInit) => Promise<Response>;
|
|
51
|
+
/** Tests: a fetch for every SDK this module makes, and timings. */
|
|
52
|
+
declare const tuning: {
|
|
53
|
+
fetch?: Fetch;
|
|
54
|
+
jitterMs?: number;
|
|
55
|
+
pollMs?: number;
|
|
56
|
+
};
|
|
57
|
+
declare function tuneForTests(t: typeof tuning): void;
|
|
58
|
+
/** Does the site have its CMS route? One `GET /api/cms/ping` per browser session (remembered in sessionStorage). */
|
|
59
|
+
declare function siteRoute(): Promise<boolean>;
|
|
60
|
+
/** One SDK per site and way (through the site or straight), shared by every hook of the page. */
|
|
61
|
+
declare function clientSdk(config: CmsClientConfig, throughSite: boolean): Sdk;
|
|
62
|
+
/** Follow `data` and call `onFresh` with its new version whenever it changes (what `useLive` does). Returns stop. */
|
|
63
|
+
declare function followLive<T extends CmsData>(data: T, onFresh: (fresh: T) => void): () => void;
|
|
64
|
+
/** Record one view of `page` from the browser (what `useVisit` does). */
|
|
65
|
+
declare function sendVisit(page: CmsData, opts?: {
|
|
66
|
+
visitor?: string | null;
|
|
67
|
+
}): void;
|
|
68
|
+
/**
|
|
69
|
+
* SITE-EDITOR (D284, D286) — what a preview page needs to talk with the editor
|
|
70
|
+
* that shows it: the page, the site's public key and its listening stream.
|
|
71
|
+
* `CmsBlocks` mounts `CmsEditorBridge` with it on a page with the drafts.
|
|
72
|
+
*/
|
|
73
|
+
interface EditorBridgeMark extends EditorPage {
|
|
74
|
+
site: CmsClientConfig;
|
|
75
|
+
live?: CmsData["_live"];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* SITE-EDITOR (D276–D278) — inside the editor's frame, on a page with the
|
|
79
|
+
* drafts: the conversation with the editor (`editorBridge`: greeting, blocks,
|
|
80
|
+
* links asked of the editor, picks, focus) and the page drawn again by the
|
|
81
|
+
* server whenever a draft it shows is saved (`router.refresh()`). Mounted by
|
|
82
|
+
* `CmsBlocks`; the site never mounts it. Draws nothing.
|
|
83
|
+
*/
|
|
84
|
+
declare function CmsEditorBridge({ mark }: {
|
|
85
|
+
mark: EditorBridgeMark;
|
|
86
|
+
}): null;
|
|
87
|
+
/** SITE-EDITOR E6 (D311): what the root `CmsBlocks` gives the browser of a public page — never the page itself. */
|
|
88
|
+
interface PageLiveMark {
|
|
89
|
+
site: CmsClientConfig;
|
|
90
|
+
live?: CmsData["_live"];
|
|
91
|
+
visit?: string;
|
|
92
|
+
/** the anonymous visitor's id, only with their consent (D160) */
|
|
93
|
+
visitor?: string | null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* SITE-EDITOR E6 (D311) — on a public page, mounted by the root `CmsBlocks`:
|
|
97
|
+
* the page drawn again by the server whenever the CMS changes what it shows
|
|
98
|
+
* (`router.refresh()`), and the view recorded from the browser. It receives
|
|
99
|
+
* only the page's `_live` and `_visit` (the content never goes back to the
|
|
100
|
+
* browser). Draws nothing; the site never mounts it.
|
|
101
|
+
*/
|
|
102
|
+
declare function CmsPageLive({ mark }: {
|
|
103
|
+
mark: PageLiveMark;
|
|
104
|
+
}): null;
|
|
105
|
+
/**
|
|
106
|
+
* The same data, renewed when it changes on the CMS (a page, a list, the
|
|
107
|
+
* site's answer `@thecms/next` gave). New data from the server (a navigation,
|
|
108
|
+
* a refresh) is returned AS IS, the same object: a different object means the
|
|
109
|
+
* CMS changed it (`useLive(page) !== page`). On a page with the drafts it does
|
|
110
|
+
* nothing: `CmsBlocks` talks with the editor and redraws the page (SITE-EDITOR D286).
|
|
111
|
+
*/
|
|
112
|
+
declare function useLive<T extends CmsData>(data: T): T;
|
|
113
|
+
/**
|
|
114
|
+
* Record the view of this page from the browser (D187), once per page: the
|
|
115
|
+
* visitor's id only when the site has their consent (else the view only counts).
|
|
116
|
+
*/
|
|
117
|
+
declare function useVisit(page: CmsData, opts?: {
|
|
118
|
+
visitor?: string | null;
|
|
119
|
+
}): void;
|
|
120
|
+
/** Set by `CmsSiteTexts` (server, in the layout): the site's texts for the client components below. */
|
|
121
|
+
declare function CmsSiteTextsProvider({ texts, children }: {
|
|
122
|
+
texts: SiteTexts;
|
|
123
|
+
children?: ReactNode;
|
|
124
|
+
}): ReactNode;
|
|
125
|
+
/**
|
|
126
|
+
* KIT (D383) — in `error.tsx`, `not-found.tsx` and any client component under
|
|
127
|
+
* the layout's `CmsSiteTexts`: the texts of the pages the CMS does not give
|
|
128
|
+
* (`notFound`, `error`, `geoBlocked`), the address's language and the homes.
|
|
129
|
+
* Outside it, every text is its key (as a missing message).
|
|
130
|
+
*/
|
|
131
|
+
declare function useSiteTexts(): SiteTexts;
|
|
132
|
+
|
|
133
|
+
export { type CmsClientConfig, type CmsData, CmsEditorBridge, CmsPageLive, CmsSiteTextsProvider, type EditorBridgeMark, type PageLiveMark, type SiteTexts, clientSdk, followLive, sendVisit, siteRoute, tuneForTests, useLive, useSiteTexts, useVisit };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/client.ts
|
|
4
|
+
import { createContext, createElement, useContext, useEffect, useState } from "react";
|
|
5
|
+
import { useRouter } from "next/navigation";
|
|
6
|
+
import { createSdk, editorBridge, markFrameRequests } from "@thecms/sdk";
|
|
7
|
+
|
|
8
|
+
// src/config.ts
|
|
9
|
+
var SITE_ROUTE = "/api/cms";
|
|
10
|
+
var SITE_TEXT_KEYS = {
|
|
11
|
+
notFound: ["title", "body", "home"],
|
|
12
|
+
error: ["title", "body", "retry"],
|
|
13
|
+
geoBlocked: ["title", "body"]
|
|
14
|
+
};
|
|
15
|
+
function siteTextsOf(t, of) {
|
|
16
|
+
const group = (g) => Object.fromEntries(SITE_TEXT_KEYS[g].map((k) => [k, t(`${g}.${k}`)]));
|
|
17
|
+
return { ...of, notFound: group("notFound"), error: group("error"), geoBlocked: group("geoBlocked") };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/client.ts
|
|
21
|
+
var tuning = {};
|
|
22
|
+
function tuneForTests(t) {
|
|
23
|
+
Object.assign(tuning, t);
|
|
24
|
+
sdks.clear();
|
|
25
|
+
routeCheck = null;
|
|
26
|
+
}
|
|
27
|
+
var ROUTE_KEY = "thecms:site-route";
|
|
28
|
+
var routeCheck = null;
|
|
29
|
+
function siteRoute() {
|
|
30
|
+
if (routeCheck) return routeCheck;
|
|
31
|
+
routeCheck = (async () => {
|
|
32
|
+
try {
|
|
33
|
+
const known = globalThis.sessionStorage?.getItem(ROUTE_KEY);
|
|
34
|
+
if (known === "1" || known === "0") return known === "1";
|
|
35
|
+
} catch {
|
|
36
|
+
}
|
|
37
|
+
let ok = false;
|
|
38
|
+
try {
|
|
39
|
+
const res = await (tuning.fetch ?? fetch)(`${SITE_ROUTE}/ping`, { cache: "no-store" });
|
|
40
|
+
ok = res.ok;
|
|
41
|
+
} catch {
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
globalThis.sessionStorage?.setItem(ROUTE_KEY, ok ? "1" : "0");
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
return ok;
|
|
48
|
+
})();
|
|
49
|
+
return routeCheck;
|
|
50
|
+
}
|
|
51
|
+
var sdks = /* @__PURE__ */ new Map();
|
|
52
|
+
var STREAM = /\/public\/(preview\/)?live$/;
|
|
53
|
+
function throughSiteFetch(base) {
|
|
54
|
+
return (input, init) => {
|
|
55
|
+
const u = new URL(input);
|
|
56
|
+
return STREAM.test(u.pathname) ? base(input, init) : base(`${SITE_ROUTE}${u.pathname}${u.search}`, init);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function clientSdk(config, throughSite) {
|
|
60
|
+
const key = `${config.publicKey}|${throughSite}`;
|
|
61
|
+
let sdk = sdks.get(key);
|
|
62
|
+
if (!sdk) {
|
|
63
|
+
const base = tuning.fetch ?? ((input, init) => fetch(input, init));
|
|
64
|
+
sdk = createSdk({
|
|
65
|
+
publicKey: config.publicKey,
|
|
66
|
+
fetch: throughSite ? throughSiteFetch(base) : base,
|
|
67
|
+
live: { ...tuning.jitterMs !== void 0 ? { jitterMs: tuning.jitterMs } : {}, ...tuning.pollMs !== void 0 ? { pollMs: tuning.pollMs } : {} }
|
|
68
|
+
});
|
|
69
|
+
sdks.set(key, sdk);
|
|
70
|
+
}
|
|
71
|
+
return sdk;
|
|
72
|
+
}
|
|
73
|
+
function followLive(data, onFresh) {
|
|
74
|
+
if (!data?._cms || !data._live) return () => {
|
|
75
|
+
};
|
|
76
|
+
let stop = () => {
|
|
77
|
+
};
|
|
78
|
+
let stopped = false;
|
|
79
|
+
void siteRoute().then((throughSite) => {
|
|
80
|
+
if (stopped) return;
|
|
81
|
+
stop = clientSdk(data._cms, throughSite).live.watch(data, (fresh) => {
|
|
82
|
+
if (fresh) onFresh({ ...fresh, ..."kind" in data ? { kind: data.kind } : {}, _cms: data._cms });
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return () => {
|
|
86
|
+
stopped = true;
|
|
87
|
+
stop();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function sendVisit(page, opts = {}) {
|
|
91
|
+
if (!page?._cms || !page._visit) return;
|
|
92
|
+
void siteRoute().then((throughSite) => {
|
|
93
|
+
clientSdk(page._cms, throughSite).visits.track(page, opts.visitor ? { visitor: opts.visitor } : {});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
var bridge = null;
|
|
97
|
+
function CmsEditorBridge({ mark }) {
|
|
98
|
+
markFrameRequests();
|
|
99
|
+
const router = useRouter();
|
|
100
|
+
const key = `${mark.view}|${mark.record ?? ""}|${mark.locale}`;
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (bridge && bridge.key !== key) {
|
|
103
|
+
bridge.stop();
|
|
104
|
+
bridge = null;
|
|
105
|
+
}
|
|
106
|
+
if (!bridge) {
|
|
107
|
+
const page = { view: mark.view, entity: mark.entity, record: mark.record, locale: mark.locale };
|
|
108
|
+
const talk = editorBridge({ page });
|
|
109
|
+
const follow = followLive({ _cms: mark.site, _live: mark.live, _preview: true }, () => router.refresh());
|
|
110
|
+
bridge = { key, count: 0, stop: () => {
|
|
111
|
+
talk();
|
|
112
|
+
follow();
|
|
113
|
+
} };
|
|
114
|
+
}
|
|
115
|
+
const mine = bridge;
|
|
116
|
+
mine.count++;
|
|
117
|
+
return () => {
|
|
118
|
+
mine.count--;
|
|
119
|
+
if (mine.count === 0 && bridge === mine) {
|
|
120
|
+
mine.stop();
|
|
121
|
+
bridge = null;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}, [key]);
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
function CmsPageLive({ mark }) {
|
|
128
|
+
const router = useRouter();
|
|
129
|
+
const url = mark.live?.url;
|
|
130
|
+
useEffect(
|
|
131
|
+
() => followLive({ _cms: mark.site, _live: mark.live }, () => router.refresh()),
|
|
132
|
+
// the same page keeps its stream across redraws
|
|
133
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
134
|
+
[url]
|
|
135
|
+
);
|
|
136
|
+
const visitor = mark.visitor ?? null;
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (mark.visit) sendVisit({ _cms: mark.site, _visit: mark.visit }, { visitor });
|
|
139
|
+
}, [mark.visit, visitor]);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
function useLive(data) {
|
|
143
|
+
const [state, setState] = useState({ source: data, value: data });
|
|
144
|
+
let current = state.value;
|
|
145
|
+
if (state.source !== data) {
|
|
146
|
+
setState({ source: data, value: data });
|
|
147
|
+
current = data;
|
|
148
|
+
}
|
|
149
|
+
useEffect(() => data?._preview ? void 0 : followLive(data, (fresh) => setState({ source: data, value: fresh })), [data]);
|
|
150
|
+
return current;
|
|
151
|
+
}
|
|
152
|
+
function useVisit(page, opts = {}) {
|
|
153
|
+
const key = page?._visit;
|
|
154
|
+
const visitor = opts.visitor ?? null;
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (key) sendVisit(page, { visitor });
|
|
157
|
+
}, [key, visitor]);
|
|
158
|
+
}
|
|
159
|
+
var SiteTextsContext = createContext(null);
|
|
160
|
+
function CmsSiteTextsProvider({ texts, children }) {
|
|
161
|
+
return createElement(SiteTextsContext.Provider, { value: texts }, children);
|
|
162
|
+
}
|
|
163
|
+
function useSiteTexts() {
|
|
164
|
+
return useContext(SiteTextsContext) ?? siteTextsOf((key) => key, { locale: "", name: null, homes: [] });
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
CmsEditorBridge,
|
|
168
|
+
CmsPageLive,
|
|
169
|
+
CmsSiteTextsProvider,
|
|
170
|
+
clientSdk,
|
|
171
|
+
followLive,
|
|
172
|
+
sendVisit,
|
|
173
|
+
siteRoute,
|
|
174
|
+
tuneForTests,
|
|
175
|
+
useLive,
|
|
176
|
+
useSiteTexts,
|
|
177
|
+
useVisit
|
|
178
|
+
};
|
package/dist/kit.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ResolvedBodyItem, EntityRecord } from '@thecms/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The class of a block's root: `blk`, `first` when it is the page's first
|
|
5
|
+
* own block (the one carrying the page's H1), then the block's own class.
|
|
6
|
+
*/
|
|
7
|
+
declare function blk(block: ResolvedBodyItem, own?: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The block's place among the page's own blocks (`in: "view"`) of the same
|
|
10
|
+
* set and shape, in page order, containers included: 0 for the first, -1 for
|
|
11
|
+
* a block that is not one of the page's own.
|
|
12
|
+
*/
|
|
13
|
+
declare function orderOf(block: ResolvedBodyItem): number;
|
|
14
|
+
/**
|
|
15
|
+
* How an image of the block loads: the first image (`index` 0) of the page's
|
|
16
|
+
* first block at once — it is the first thing on screen —, every other one
|
|
17
|
+
* when it comes near the screen.
|
|
18
|
+
*/
|
|
19
|
+
declare function loadingOf(block: ResolvedBodyItem, index?: number): "lazy" | undefined;
|
|
20
|
+
|
|
21
|
+
/** An image ready for `<img>` (also what a server component hands to a client one). */
|
|
22
|
+
interface Img {
|
|
23
|
+
src: string;
|
|
24
|
+
srcSet?: string;
|
|
25
|
+
alt: string;
|
|
26
|
+
width?: number;
|
|
27
|
+
height?: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A file the CMS gave (a record's field, a block's value, a global), ready for
|
|
31
|
+
* `<img>`: absolute addresses, its `srcset`, the alternative text in the
|
|
32
|
+
* page's language, its size. `undefined` when the value is not a file.
|
|
33
|
+
*/
|
|
34
|
+
declare function image(asset: unknown): Img | undefined;
|
|
35
|
+
|
|
36
|
+
/** Where a link field leads: the path of a page of the site, or the outside address as written. */
|
|
37
|
+
declare function hrefOf(link: unknown): string | undefined;
|
|
38
|
+
/** The page of a record (its `_urls`): the one of the given view, else the first. */
|
|
39
|
+
declare function urlOf(record: EntityRecord, view?: string): string | undefined;
|
|
40
|
+
/** The home of a language: `/` for the site's default language, `/<language>` for the others. */
|
|
41
|
+
declare function homeOf(locale: string): string;
|
|
42
|
+
|
|
43
|
+
/** The text as markup, safe to put in the page (`dangerouslySetInnerHTML`). */
|
|
44
|
+
declare function markdown(text: string | null | undefined): string;
|
|
45
|
+
/** The text's paragraphs, each as markup: to show some now and the rest on request. */
|
|
46
|
+
declare function paragraphs(text: string | null | undefined): string[];
|
|
47
|
+
/** The text's first paragraph as written (no markup): a summary drawn from the full text. */
|
|
48
|
+
declare function firstParagraph(text: string | null | undefined): string;
|
|
49
|
+
|
|
50
|
+
/** The parts of a date a design shows. */
|
|
51
|
+
interface DateParts {
|
|
52
|
+
/** the day, two digits (`05`) */
|
|
53
|
+
day: string;
|
|
54
|
+
/** the short month (`ott`, `Oct`) */
|
|
55
|
+
mon: string;
|
|
56
|
+
/** the month (`ottobre`, `October`) */
|
|
57
|
+
month: string;
|
|
58
|
+
/** month and year (`ottobre 2026`) */
|
|
59
|
+
monthYear: string;
|
|
60
|
+
/** the year (`2026`) */
|
|
61
|
+
year: string;
|
|
62
|
+
/** the whole date (`5 ottobre 2026`) */
|
|
63
|
+
full: string;
|
|
64
|
+
/** the instant, ISO 8601 (for `<time dateTime>`) */
|
|
65
|
+
iso: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* KIT (D380) — the parts of a date in a language's format (`it`, `en-GB`) and
|
|
69
|
+
* a time zone (`Europe/Rome`), both chosen by the site; as `Intl` writes them
|
|
70
|
+
* in that language: capitals and punctuation are the design's (CSS). Pure.
|
|
71
|
+
*/
|
|
72
|
+
declare function dateParts(iso: string, format: string, timeZone: string): DateParts;
|
|
73
|
+
|
|
74
|
+
/** A message by key, the key itself when there is none (`t` of `cmsContext()`). */
|
|
75
|
+
type Translate = (key: string) => string;
|
|
76
|
+
/** A filter's label (`filters.category.label`, else the CMS's). */
|
|
77
|
+
declare function filterLabel(t: Translate, filter: {
|
|
78
|
+
field: string;
|
|
79
|
+
label: string;
|
|
80
|
+
}): string;
|
|
81
|
+
/** An option's label (`filters.category.news`, else the CMS's, else its value); also for a record's value of that field. */
|
|
82
|
+
declare function optionLabel(t: Translate, field: string, option: {
|
|
83
|
+
value: string;
|
|
84
|
+
label?: string;
|
|
85
|
+
}): string;
|
|
86
|
+
|
|
87
|
+
export { type DateParts, type Img, type Translate, blk, dateParts, filterLabel, firstParagraph, homeOf, hrefOf, image, loadingOf, markdown, optionLabel, orderOf, paragraphs, urlOf };
|