@decocms/blocks 8.2.3 → 8.4.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/v8/builtins/lazy.d.ts +1 -1
- package/dist/v8/builtins/lazy.d.ts.map +1 -1
- package/dist/v8/builtins/lazy.js +1 -1
- package/dist/v8/builtins/lazy.js.map +1 -1
- package/dist/v8/builtins/multivariate.d.ts +12 -1
- package/dist/v8/builtins/multivariate.d.ts.map +1 -1
- package/dist/v8/builtins/multivariate.js +14 -2
- package/dist/v8/builtins/multivariate.js.map +1 -1
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +7 -3
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/index.d.ts +15 -1
- package/dist/v8/cli/schema/index.d.ts.map +1 -1
- package/dist/v8/cli/schema/index.js +96 -2
- package/dist/v8/cli/schema/index.js.map +1 -1
- package/dist/v8/cms.js +9 -1
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/draft.d.ts +2 -0
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +2 -1
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/image.d.ts +140 -0
- package/dist/v8/image.d.ts.map +1 -0
- package/dist/v8/image.js +226 -0
- package/dist/v8/image.js.map +1 -0
- package/dist/v8/preview/DraftBadge.d.ts +52 -0
- package/dist/v8/preview/DraftBadge.d.ts.map +1 -0
- package/dist/v8/preview/DraftBadge.js +167 -0
- package/dist/v8/preview/DraftBadge.js.map +1 -0
- package/dist/v8/preview/StudioBridge.d.ts +78 -0
- package/dist/v8/preview/StudioBridge.d.ts.map +1 -0
- package/dist/v8/preview/StudioBridge.js +137 -0
- package/dist/v8/preview/StudioBridge.js.map +1 -0
- package/dist/v8/preview/decoMark.d.ts +7 -0
- package/dist/v8/preview/decoMark.d.ts.map +1 -0
- package/dist/v8/preview/decoMark.js +7 -0
- package/dist/v8/preview/decoMark.js.map +1 -0
- package/dist/v8/preview/index.d.ts +14 -0
- package/dist/v8/preview/index.d.ts.map +1 -0
- package/dist/v8/preview/index.js +14 -0
- package/dist/v8/preview/index.js.map +1 -0
- package/dist/v8/remoteLoader.js +3 -3
- package/dist/v8/resolver.d.ts.map +1 -1
- package/dist/v8/resolver.js +36 -3
- package/dist/v8/resolver.js.map +1 -1
- package/dist/v8/types.d.ts +4 -3
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +11 -1
- package/src/v8/builtins/lazy.ts +1 -1
- package/src/v8/builtins/multivariate.ts +28 -11
- package/src/v8/cli/run.ts +7 -3
- package/src/v8/cli/schema/index.ts +114 -3
- package/src/v8/cms.ts +12 -1
- package/src/v8/draft.ts +2 -1
- package/src/v8/image.ts +325 -0
- package/src/v8/preview/DraftBadge.tsx +278 -0
- package/src/v8/preview/StudioBridge.ts +183 -0
- package/src/v8/preview/decoMark.ts +7 -0
- package/src/v8/preview/index.ts +18 -0
- package/src/v8/remoteLoader.ts +3 -3
- package/src/v8/resolver.ts +40 -3
- package/src/v8/types.ts +4 -3
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The "Preview mode" badge: v7's `DraftPreviewBadge`, unchanged on screen.
|
|
5
|
+
*
|
|
6
|
+
* A draft preview keeps its pointer in a cookie, so after the first click the
|
|
7
|
+
* URL no longer shows `?__draft=` and a reviewer can mistake the draft for the
|
|
8
|
+
* live site. While a draft is bound, this badge floats over the page; clicking
|
|
9
|
+
* it offers the two things a reviewer needs: leave the preview, or copy a link
|
|
10
|
+
* to this exact draft version.
|
|
11
|
+
*
|
|
12
|
+
* - Renders nothing without a `pointer`, so a binding can mount it on every
|
|
13
|
+
* page and pass the request's draft pointer (or `null`).
|
|
14
|
+
* - Hidden inside a frame: Studio's site editor shows the draft in an iframe
|
|
15
|
+
* with its own chrome. A reviewer who opens a shared link in their own tab
|
|
16
|
+
* still sees it.
|
|
17
|
+
* - Starts hidden on the server and on the first client render, and shows only
|
|
18
|
+
* after an effect confirms the page isn't framed. Rendering it and hiding it
|
|
19
|
+
* later would flash it inside Studio and break hydration (the server has no
|
|
20
|
+
* `window`).
|
|
21
|
+
* - Inline styles and an inlined mark: no dependency on the site's CSS or
|
|
22
|
+
* assets.
|
|
23
|
+
*
|
|
24
|
+
* `"use client"`: it has state and effects. In Next.js a Server Component can
|
|
25
|
+
* render it directly.
|
|
26
|
+
*/
|
|
27
|
+
import { type ReactNode, useCallback, useEffect, useRef, useState } from "react";
|
|
28
|
+
import { DRAFT_PARAM } from "../draft.ts";
|
|
29
|
+
import { DECO_MARK_DATA_URI } from "./decoMark.ts";
|
|
30
|
+
|
|
31
|
+
export interface DraftBadgeProps {
|
|
32
|
+
/**
|
|
33
|
+
* The draft pointer this page was rendered from (`cms.draftPointer`). The
|
|
34
|
+
* badge renders nothing when it's `null`, `undefined` or empty.
|
|
35
|
+
*/
|
|
36
|
+
pointer: string | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Where "Exit preview" goes. Defaults to the current URL with
|
|
39
|
+
* `?__draft=off`, which clears the draft cookie.
|
|
40
|
+
*/
|
|
41
|
+
exitHref?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The link "Share preview" copies: the current URL with `?__draft=<pointer>`,
|
|
46
|
+
* so the recipient sees this exact draft version.
|
|
47
|
+
*/
|
|
48
|
+
export function buildShareUrl(href: string, pointer: string): string {
|
|
49
|
+
const url = new URL(href);
|
|
50
|
+
url.searchParams.set(DRAFT_PARAM, pointer);
|
|
51
|
+
return url.toString();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The current URL with `?__draft=off`: ends the preview and shows the published page. */
|
|
55
|
+
export function buildExitUrl(href: string): string {
|
|
56
|
+
const url = new URL(href);
|
|
57
|
+
url.searchParams.set(DRAFT_PARAM, "off");
|
|
58
|
+
return url.toString();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether the page is in another page's frame (Studio's site editor). A
|
|
63
|
+
* reference comparison, so it never throws across origins. No `window` (the
|
|
64
|
+
* server) reads as not framed.
|
|
65
|
+
*/
|
|
66
|
+
export function isFramed(win: typeof window | undefined = globalThis.window): boolean {
|
|
67
|
+
return typeof win !== "undefined" && win.top !== win;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const DECO_GREEN = "#0b3d1e";
|
|
71
|
+
/** The badge background, Deco lime; text sits on it in dark green. */
|
|
72
|
+
const DECO_LIME = "#d0ec1a";
|
|
73
|
+
|
|
74
|
+
/** Leaving preview: an arrow back to the published site. */
|
|
75
|
+
function ExitIcon() {
|
|
76
|
+
return (
|
|
77
|
+
<svg
|
|
78
|
+
width={14}
|
|
79
|
+
height={14}
|
|
80
|
+
viewBox="0 0 24 24"
|
|
81
|
+
fill="none"
|
|
82
|
+
stroke="currentColor"
|
|
83
|
+
strokeWidth={2}
|
|
84
|
+
strokeLinecap="round"
|
|
85
|
+
strokeLinejoin="round"
|
|
86
|
+
aria-hidden="true"
|
|
87
|
+
>
|
|
88
|
+
<path d="M19 12H5" />
|
|
89
|
+
<path d="M12 19l-7-7 7-7" />
|
|
90
|
+
</svg>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Sharing the draft: the three-node share glyph. */
|
|
95
|
+
function ShareIcon() {
|
|
96
|
+
return (
|
|
97
|
+
<svg
|
|
98
|
+
width={14}
|
|
99
|
+
height={14}
|
|
100
|
+
viewBox="0 0 24 24"
|
|
101
|
+
fill="none"
|
|
102
|
+
stroke="currentColor"
|
|
103
|
+
strokeWidth={2}
|
|
104
|
+
strokeLinecap="round"
|
|
105
|
+
strokeLinejoin="round"
|
|
106
|
+
aria-hidden="true"
|
|
107
|
+
>
|
|
108
|
+
<circle cx={18} cy={5} r={3} />
|
|
109
|
+
<circle cx={6} cy={12} r={3} />
|
|
110
|
+
<circle cx={18} cy={19} r={3} />
|
|
111
|
+
<path d="M8.59 13.51l6.83 3.98" />
|
|
112
|
+
<path d="M15.41 6.51l-6.82 3.98" />
|
|
113
|
+
</svg>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function DraftBadge({ pointer, exitHref }: DraftBadgeProps): ReactNode {
|
|
118
|
+
const [open, setOpen] = useState(false);
|
|
119
|
+
const [copied, setCopied] = useState(false);
|
|
120
|
+
const [visible, setVisible] = useState(false);
|
|
121
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
122
|
+
|
|
123
|
+
// The only place isFramed() runs: after mount, so the server render and the
|
|
124
|
+
// first client render agree (both hidden).
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!isFramed()) setVisible(true);
|
|
127
|
+
}, []);
|
|
128
|
+
|
|
129
|
+
// Close the menu on an outside click or Escape.
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
if (!open) return;
|
|
132
|
+
const onPointerDown = (e: MouseEvent) => {
|
|
133
|
+
if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
|
|
134
|
+
};
|
|
135
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
136
|
+
if (e.key === "Escape") setOpen(false);
|
|
137
|
+
};
|
|
138
|
+
document.addEventListener("mousedown", onPointerDown);
|
|
139
|
+
document.addEventListener("keydown", onKeyDown);
|
|
140
|
+
return () => {
|
|
141
|
+
document.removeEventListener("mousedown", onPointerDown);
|
|
142
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
143
|
+
};
|
|
144
|
+
}, [open]);
|
|
145
|
+
|
|
146
|
+
const exitPreview = useCallback(() => {
|
|
147
|
+
window.location.href = exitHref ?? buildExitUrl(window.location.href);
|
|
148
|
+
}, [exitHref]);
|
|
149
|
+
|
|
150
|
+
const sharePreviewLink = useCallback(async () => {
|
|
151
|
+
if (!pointer) return;
|
|
152
|
+
const link = buildShareUrl(window.location.href, pointer);
|
|
153
|
+
try {
|
|
154
|
+
await navigator.clipboard.writeText(link);
|
|
155
|
+
setCopied(true);
|
|
156
|
+
window.setTimeout(() => setCopied(false), 2000);
|
|
157
|
+
} catch {
|
|
158
|
+
// No clipboard (an insecure context, a denied permission): let the
|
|
159
|
+
// reviewer copy the link by hand.
|
|
160
|
+
window.prompt("Copy preview link:", link);
|
|
161
|
+
}
|
|
162
|
+
}, [pointer]);
|
|
163
|
+
|
|
164
|
+
if (!pointer || !visible) return null;
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<div
|
|
168
|
+
ref={rootRef}
|
|
169
|
+
data-deco-preview-badge=""
|
|
170
|
+
style={{
|
|
171
|
+
position: "fixed",
|
|
172
|
+
bottom: 16,
|
|
173
|
+
left: 16,
|
|
174
|
+
zIndex: 2147483647,
|
|
175
|
+
fontFamily:
|
|
176
|
+
"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
|
|
177
|
+
fontSize: 13,
|
|
178
|
+
lineHeight: 1.4,
|
|
179
|
+
}}
|
|
180
|
+
>
|
|
181
|
+
{open ? (
|
|
182
|
+
<div
|
|
183
|
+
role="menu"
|
|
184
|
+
style={{
|
|
185
|
+
position: "absolute",
|
|
186
|
+
bottom: "calc(100% + 8px)",
|
|
187
|
+
left: 0,
|
|
188
|
+
minWidth: 200,
|
|
189
|
+
background: "#fff",
|
|
190
|
+
color: "#1a1a1a",
|
|
191
|
+
borderRadius: 12,
|
|
192
|
+
boxShadow: "0 8px 30px rgba(0,0,0,0.18), 0 0 0 1px rgba(0,0,0,0.05)",
|
|
193
|
+
overflow: "hidden",
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
<MenuItem onClick={exitPreview} label="Exit preview" icon={<ExitIcon />} />
|
|
197
|
+
<div style={{ height: 1, background: "rgba(0,0,0,0.07)" }} />
|
|
198
|
+
<MenuItem
|
|
199
|
+
onClick={sharePreviewLink}
|
|
200
|
+
label={copied ? "Copied!" : "Share preview"}
|
|
201
|
+
icon={<ShareIcon />}
|
|
202
|
+
/>
|
|
203
|
+
</div>
|
|
204
|
+
) : null}
|
|
205
|
+
|
|
206
|
+
<button
|
|
207
|
+
type="button"
|
|
208
|
+
aria-expanded={open}
|
|
209
|
+
aria-label="Preview mode"
|
|
210
|
+
onClick={() => setOpen((v) => !v)}
|
|
211
|
+
style={{
|
|
212
|
+
display: "inline-flex",
|
|
213
|
+
alignItems: "center",
|
|
214
|
+
gap: 8,
|
|
215
|
+
padding: "7px 12px 7px 8px",
|
|
216
|
+
background: DECO_LIME,
|
|
217
|
+
color: DECO_GREEN,
|
|
218
|
+
border: "none",
|
|
219
|
+
borderRadius: 9999,
|
|
220
|
+
cursor: "pointer",
|
|
221
|
+
fontSize: 13,
|
|
222
|
+
fontWeight: 600,
|
|
223
|
+
boxShadow: "0 4px 14px rgba(0,0,0,0.25)",
|
|
224
|
+
}}
|
|
225
|
+
>
|
|
226
|
+
<img
|
|
227
|
+
src={DECO_MARK_DATA_URI}
|
|
228
|
+
alt=""
|
|
229
|
+
width={18}
|
|
230
|
+
height={18}
|
|
231
|
+
style={{ display: "block", borderRadius: 4 }}
|
|
232
|
+
/>
|
|
233
|
+
<span>Preview mode</span>
|
|
234
|
+
</button>
|
|
235
|
+
</div>
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function MenuItem({
|
|
240
|
+
onClick,
|
|
241
|
+
label,
|
|
242
|
+
icon,
|
|
243
|
+
}: {
|
|
244
|
+
onClick: () => void;
|
|
245
|
+
label: string;
|
|
246
|
+
icon: ReactNode;
|
|
247
|
+
}) {
|
|
248
|
+
return (
|
|
249
|
+
<button
|
|
250
|
+
type="button"
|
|
251
|
+
role="menuitem"
|
|
252
|
+
onClick={onClick}
|
|
253
|
+
style={{
|
|
254
|
+
display: "flex",
|
|
255
|
+
alignItems: "center",
|
|
256
|
+
gap: 10,
|
|
257
|
+
width: "100%",
|
|
258
|
+
textAlign: "left",
|
|
259
|
+
padding: "10px 14px",
|
|
260
|
+
background: "transparent",
|
|
261
|
+
border: "none",
|
|
262
|
+
color: "inherit",
|
|
263
|
+
cursor: "pointer",
|
|
264
|
+
fontSize: 13,
|
|
265
|
+
whiteSpace: "nowrap",
|
|
266
|
+
}}
|
|
267
|
+
onMouseEnter={(e) => {
|
|
268
|
+
e.currentTarget.style.background = "rgba(0,0,0,0.05)";
|
|
269
|
+
}}
|
|
270
|
+
onMouseLeave={(e) => {
|
|
271
|
+
e.currentTarget.style.background = "transparent";
|
|
272
|
+
}}
|
|
273
|
+
>
|
|
274
|
+
<span style={{ display: "inline-flex", flexShrink: 0, color: DECO_GREEN }}>{icon}</span>
|
|
275
|
+
<span>{label}</span>
|
|
276
|
+
</button>
|
|
277
|
+
);
|
|
278
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Studio bridge: what lets Deco Studio's site editor drive a page it
|
|
3
|
+
* shows in an iframe (v7's `LiveControls`).
|
|
4
|
+
*
|
|
5
|
+
* When the frame loads, Studio posts `{ type: "editor::inject", args: {
|
|
6
|
+
* script } }` to it; the bridge runs that script, which draws the section
|
|
7
|
+
* overlay and handles Studio's `cms-editor::*` messages itself. The bridge also
|
|
8
|
+
* answers v7's `scrollToComponent` and `editor::rerender` (same-origin
|
|
9
|
+
* navigation only).
|
|
10
|
+
*
|
|
11
|
+
* In a frame it answers only messages from the parent window whose origin is
|
|
12
|
+
* one of `trustedOrigins`. On a page that isn't framed it answers no message;
|
|
13
|
+
* there it only listens for the "." shortcut (`editShortcut`), which opens the
|
|
14
|
+
* page in Studio's editor, as v7's `LiveControls` did. Studio signs the editor
|
|
15
|
+
* in: `/choose-editor` sends a visitor who isn't signed in to Studio's login
|
|
16
|
+
* and back.
|
|
17
|
+
* The binding passes Studio's origins in (`@decocms/blocks` holds no list of
|
|
18
|
+
* them); with none, the bridge answers nothing. It doesn't trust the page's own
|
|
19
|
+
* origin: under a test runner that proxies the site, the runner's own messages
|
|
20
|
+
* would navigate the page.
|
|
21
|
+
*
|
|
22
|
+
* An inline script, not an effect: Studio posts on the frame's `load` event,
|
|
23
|
+
* which can come before React hydrates, so the listener must exist as soon as
|
|
24
|
+
* the HTML is parsed. No hooks, so it renders from a Next.js Server Component
|
|
25
|
+
* as well as from a client tree. Its settings travel in `data-*` attributes;
|
|
26
|
+
* the script itself is static.
|
|
27
|
+
*/
|
|
28
|
+
import { createElement, type ReactNode } from "react";
|
|
29
|
+
|
|
30
|
+
export interface StudioBridgeProps {
|
|
31
|
+
/** The site's name in Deco (the "." shortcut sends it to Studio). */
|
|
32
|
+
site: string;
|
|
33
|
+
/**
|
|
34
|
+
* The origins Studio's site editor frames the page from, in the CSP source
|
|
35
|
+
* form (`https://host`, or `https://*.host` for any name under it, same
|
|
36
|
+
* scheme and port). Required: the binding owns Studio's origins and passes
|
|
37
|
+
* them here. An empty list makes the bridge answer nothing.
|
|
38
|
+
*/
|
|
39
|
+
trustedOrigins: readonly string[];
|
|
40
|
+
/**
|
|
41
|
+
* v7's editor shortcut: on a page that isn't framed, "." (or Ctrl+Shift+E)
|
|
42
|
+
* opens it in Studio, and Ctrl or Cmd+"." opens it in a new tab. On by
|
|
43
|
+
* default, as in v7; `false` turns it off.
|
|
44
|
+
*/
|
|
45
|
+
editShortcut?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The page being shown, when the layout that renders the bridge knows it:
|
|
48
|
+
* the "." shortcut sends its path template (and id), so Studio opens that
|
|
49
|
+
* page rather than guessing from the URL. A page can declare itself instead
|
|
50
|
+
* with `StudioPage` (for a layout that doesn't re-render per page, such as a
|
|
51
|
+
* Next.js root layout); a `StudioPage` on the page wins.
|
|
52
|
+
*/
|
|
53
|
+
page?: StudioPageInfo;
|
|
54
|
+
/** The CSP nonce for the inline script, when the site sends one. */
|
|
55
|
+
nonce?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The page the "." shortcut opens in Studio. */
|
|
59
|
+
export interface StudioPageInfo {
|
|
60
|
+
/** The page block's `path`, as `matchRoute` matched it (`/products/:slug/p`, `/*`). */
|
|
61
|
+
pathTemplate?: string;
|
|
62
|
+
/** The page block's id, when the site knows it. */
|
|
63
|
+
id?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The marker a page renders to tell the bridge which page it is (read on each keypress). */
|
|
67
|
+
const PAGE_MARKER = "data-deco-page";
|
|
68
|
+
|
|
69
|
+
/** Where the "." shortcut sends an editor; Studio picks the project the site belongs to. */
|
|
70
|
+
const CHOOSE_EDITOR_URL = "https://studio.decocms.com/choose-editor";
|
|
71
|
+
|
|
72
|
+
const GLOBAL = "__decoStudioBridge";
|
|
73
|
+
|
|
74
|
+
/** The browser side. Static: everything it needs comes from the script element's attributes. */
|
|
75
|
+
export const STUDIO_BRIDGE_SCRIPT = `(function(){
|
|
76
|
+
var s=document.currentScript;
|
|
77
|
+
if(!s||window.${GLOBAL})return;
|
|
78
|
+
window.${GLOBAL}=true;
|
|
79
|
+
var origins=[];
|
|
80
|
+
try{origins=JSON.parse(s.getAttribute("data-origins")||"[]")}catch(e){}
|
|
81
|
+
var site=s.getAttribute("data-site")||"";
|
|
82
|
+
function page(){
|
|
83
|
+
var all=document.querySelectorAll("[${PAGE_MARKER}]"),el=all.length?all[all.length-1]:null;
|
|
84
|
+
if(!el||!el.getAttribute("data-path-template")){el=document.querySelector("script[data-deco-studio-bridge]")||s}
|
|
85
|
+
return{t:el.getAttribute("data-path-template")||"",id:el.getAttribute("data-page-id")||""};
|
|
86
|
+
}
|
|
87
|
+
function trusted(o){
|
|
88
|
+
if(typeof o!=="string"||!o)return false;
|
|
89
|
+
var u;
|
|
90
|
+
try{u=new URL(o)}catch(e){return false}
|
|
91
|
+
if(u.origin!==o)return false;
|
|
92
|
+
for(var i=0;i<origins.length;i++){
|
|
93
|
+
var p=origins[i];
|
|
94
|
+
if(p===o)return true;
|
|
95
|
+
var m=/^([a-z][a-z0-9+.-]*:)\\/\\/\\*\\.([^\\/:]+)(?::([0-9]+))?$/.exec(p);
|
|
96
|
+
if(m&&u.protocol===m[1]&&u.port===(m[3]||"")&&u.hostname.slice(-m[2].length-1)==="."+m[2])return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if(window.self!==window.top){
|
|
101
|
+
window.addEventListener("message",function(e){
|
|
102
|
+
if(e.source!==window.parent||!trusted(e.origin))return;
|
|
103
|
+
var d=e.data;
|
|
104
|
+
if(!d||typeof d!=="object")return;
|
|
105
|
+
var a=d.args||{};
|
|
106
|
+
if(d.type==="editor::inject"){
|
|
107
|
+
if(typeof a.script==="string"){try{(0,eval)(a.script)}catch(err){console.error("[deco] Studio script failed:",err)}}
|
|
108
|
+
}else if(d.type==="scrollToComponent"){
|
|
109
|
+
var id=typeof a.id==="string"?a.id:"";
|
|
110
|
+
if(!id)return;
|
|
111
|
+
var el=null,all=document.querySelectorAll("[data-manifest-key]");
|
|
112
|
+
for(var i=0;i<all.length&&!el;i++)if(all[i].getAttribute("data-manifest-key")===id)el=all[i];
|
|
113
|
+
el=el||document.getElementById(id);
|
|
114
|
+
if(el)el.scrollIntoView({behavior:"smooth",block:"center"});
|
|
115
|
+
}else if(d.type==="editor::rerender"){
|
|
116
|
+
if(typeof a.url!=="string")return;
|
|
117
|
+
try{var t=new URL(a.url,location.origin);if(t.origin===location.origin)location.href=t.href}catch(err){}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}else if(s.hasAttribute("data-shortcut")){
|
|
121
|
+
document.addEventListener("keydown",function(e){
|
|
122
|
+
var t=e.target;
|
|
123
|
+
if(e.defaultPrevented||(t&&(t.tagName==="INPUT"||t.tagName==="TEXTAREA"||t.tagName==="SELECT"||t.isContentEditable)))return;
|
|
124
|
+
if(e.key!=="."&&!(e.ctrlKey&&e.shiftKey&&e.key==="E"))return;
|
|
125
|
+
e.preventDefault();
|
|
126
|
+
e.stopPropagation();
|
|
127
|
+
var p=page(),h=new URL("${CHOOSE_EDITOR_URL}");
|
|
128
|
+
h.searchParams.set("site",site);
|
|
129
|
+
h.searchParams.set("domain",location.origin);
|
|
130
|
+
if(p.id)h.searchParams.set("pageId",p.id);
|
|
131
|
+
h.searchParams.set("path",location.pathname+location.search);
|
|
132
|
+
h.searchParams.set("pathTemplate",p.t||"/*");
|
|
133
|
+
if((e.ctrlKey||e.metaKey)&&e.key==="."){window.open(h.toString(),"_blank");return}
|
|
134
|
+
location.href=h.toString();
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
})();`;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Lets Studio's site editor select and edit sections on the page. Render it
|
|
141
|
+
* once, in the root layout's `<body>`.
|
|
142
|
+
*/
|
|
143
|
+
export function StudioBridge({
|
|
144
|
+
site,
|
|
145
|
+
trustedOrigins,
|
|
146
|
+
editShortcut = true,
|
|
147
|
+
page,
|
|
148
|
+
nonce,
|
|
149
|
+
}: StudioBridgeProps): ReactNode {
|
|
150
|
+
return createElement("script", {
|
|
151
|
+
"data-deco-studio-bridge": "",
|
|
152
|
+
"data-site": site,
|
|
153
|
+
"data-origins": JSON.stringify(trustedOrigins),
|
|
154
|
+
"data-shortcut": editShortcut ? "" : undefined,
|
|
155
|
+
// Read on each keypress, so a client-side navigation that re-renders the
|
|
156
|
+
// bridge with the next page updates them.
|
|
157
|
+
"data-path-template": page?.pathTemplate || undefined,
|
|
158
|
+
"data-page-id": page?.id || undefined,
|
|
159
|
+
nonce,
|
|
160
|
+
// Browsers hide a nonce from the DOM after parsing, which React would
|
|
161
|
+
// report as a hydration mismatch.
|
|
162
|
+
suppressHydrationWarning: true,
|
|
163
|
+
dangerouslySetInnerHTML: { __html: STUDIO_BRIDGE_SCRIPT },
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Tells the bridge which page is on screen: render it inside the page (not a
|
|
169
|
+
* layout that stays across navigations), with the page block's `path` as
|
|
170
|
+
* `matchRoute` matched it. A `<meta>`, which React places in the document's
|
|
171
|
+
* `<head>` wherever the page renders it, so it adds nothing to the page's own
|
|
172
|
+
* markup; the "." shortcut reads the last one when the key is pressed.
|
|
173
|
+
*/
|
|
174
|
+
export function StudioPage({ pathTemplate, id }: StudioPageInfo): ReactNode {
|
|
175
|
+
if (!pathTemplate) return null;
|
|
176
|
+
return createElement("meta", {
|
|
177
|
+
name: "deco:page",
|
|
178
|
+
content: pathTemplate,
|
|
179
|
+
[PAGE_MARKER]: "",
|
|
180
|
+
"data-path-template": pathTemplate,
|
|
181
|
+
"data-page-id": id || undefined,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Deco mark the draft badge shows, inlined so the badge needs no asset
|
|
3
|
+
* from the site: a 36×36 PNG (the 18px icon at 2x). v7 shipped the 720px
|
|
4
|
+
* original (about 22KB) in every client bundle that mounted the badge.
|
|
5
|
+
*/
|
|
6
|
+
export const DECO_MARK_DATA_URI =
|
|
7
|
+
"data:image/png;base64,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";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@decocms/blocks/preview`: the two pieces of the page that serve Deco
|
|
3
|
+
* Studio and draft previews, with no framework code. `@decocms/tanstack`'s
|
|
4
|
+
* document mounts both; a Next.js site adds them to its root layout.
|
|
5
|
+
*
|
|
6
|
+
* - `DraftBadge`: the "Preview mode" badge on a page rendered from a draft,
|
|
7
|
+
* outside Studio's frame.
|
|
8
|
+
* - `StudioBridge`: lets Studio's site editor drive the page when Studio
|
|
9
|
+
* frames it, and opens the page in Studio on "." when it isn't framed.
|
|
10
|
+
* - `StudioPage`: what a page renders to tell the bridge which page it is.
|
|
11
|
+
*/
|
|
12
|
+
export { DraftBadge, type DraftBadgeProps } from "./DraftBadge.tsx";
|
|
13
|
+
export {
|
|
14
|
+
StudioBridge,
|
|
15
|
+
type StudioBridgeProps,
|
|
16
|
+
StudioPage,
|
|
17
|
+
type StudioPageInfo,
|
|
18
|
+
} from "./StudioBridge.ts";
|
package/src/v8/remoteLoader.ts
CHANGED
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
* a publish still loses to it.
|
|
26
26
|
* Pointers aren't ordered among themselves, and the fallback's content is
|
|
27
27
|
* never compared: only the two timestamps are.
|
|
28
|
-
* - With `dev: true
|
|
29
|
-
*
|
|
30
|
-
*
|
|
28
|
+
* - With `dev: true`, it never swaps, so local files win. `createCMS` passes
|
|
29
|
+
* its `dev` option, or `NODE_ENV === "development"` when the site omits it;
|
|
30
|
+
* this loader reads no environment itself.
|
|
31
31
|
* - A release larger than `MAX_SNAPSHOT_BYTES` is refused while it downloads,
|
|
32
32
|
* before it's buffered whole (a Worker isolate has 128 MB).
|
|
33
33
|
* - Without `site` it's a plain loader over the fallback.
|
package/src/v8/resolver.ts
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
* in ./builtins/legacy) is a second key for a type, so like a function it wins
|
|
8
8
|
* over a saved block of the same name; a saved block is replaced by
|
|
9
9
|
* `{ ...saved, ...arguments }` and looked up again; anything else is
|
|
10
|
-
* `UNKNOWN_BLOCK`.
|
|
11
|
-
*
|
|
10
|
+
* `UNKNOWN_BLOCK`. Two built-ins are special cases: `lazy`, whose `value` is
|
|
11
|
+
* not resolved first, and `multivariate`, whose variants' rules resolve in
|
|
12
|
+
* order and stop at the first `true` one (later rules never run, as in v7).
|
|
12
13
|
*
|
|
13
14
|
* A `Resolver` belongs to one client, so its memo (results per block function
|
|
14
15
|
* and canonical inputs) never outlives a request. Within the request the memo
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
import { RESERVED_NAMES } from "./builtins/index.ts";
|
|
20
21
|
import { isLazyBuiltin } from "./builtins/lazy.ts";
|
|
21
22
|
import { LEGACY_ALIASES, wrapLegacyVariants } from "./builtins/legacy.ts";
|
|
23
|
+
import { isMultivariateBuiltin } from "./builtins/multivariate.ts";
|
|
22
24
|
import { errors, isResolutionError } from "./errors.ts";
|
|
23
25
|
import { canonicalKey, isPlainObject, type JsonObject, own } from "./json.ts";
|
|
24
26
|
import type { BlockFunction, Blocks, Lazy, Snapshot } from "./types.ts";
|
|
@@ -240,7 +242,10 @@ export class Resolver {
|
|
|
240
242
|
): Promise<unknown> {
|
|
241
243
|
// Inputs are always fresh objects: a function may mutate its props freely.
|
|
242
244
|
const { __resolveType: _type, ...inputs } = node;
|
|
243
|
-
const resolved =
|
|
245
|
+
const resolved =
|
|
246
|
+
isMultivariateBuiltin(fn) && Array.isArray(inputs.variants)
|
|
247
|
+
? await this.#resolveFirstMatch(inputs, inputs.variants, path, chain)
|
|
248
|
+
: await this.#resolveFields(inputs, path, chain, true);
|
|
244
249
|
const start = Date.now();
|
|
245
250
|
try {
|
|
246
251
|
const value = await fn(resolved);
|
|
@@ -254,6 +259,38 @@ export class Resolver {
|
|
|
254
259
|
}
|
|
255
260
|
}
|
|
256
261
|
|
|
262
|
+
/**
|
|
263
|
+
* The built-in `multivariate`'s inputs: its other fields as usual, then the
|
|
264
|
+
* variants one at a time, each rule before the rest of its variant, up to
|
|
265
|
+
* the first rule that is `true`. Later variants are left out, so their
|
|
266
|
+
* matchers never run.
|
|
267
|
+
*/
|
|
268
|
+
async #resolveFirstMatch(
|
|
269
|
+
inputs: JsonObject,
|
|
270
|
+
variants: unknown[],
|
|
271
|
+
path: Path,
|
|
272
|
+
chain: string[],
|
|
273
|
+
): Promise<JsonObject> {
|
|
274
|
+
const { variants: _variants, ...others } = inputs;
|
|
275
|
+
const resolved = await this.#resolveFields(others, path, chain, true);
|
|
276
|
+
const kept: unknown[] = [];
|
|
277
|
+
for (const [i, variant] of variants.entries()) {
|
|
278
|
+
const at = [...path, "variants", i];
|
|
279
|
+
if (!isPlainObject(variant) || typeof variant.__resolveType === "string") {
|
|
280
|
+
const value = await this.resolve(variant, at, chain, true);
|
|
281
|
+
if (value === undefined) continue;
|
|
282
|
+
kept.push(value);
|
|
283
|
+
if (isPlainObject(value) && value.rule === true) break;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const { rule, ...rest } = variant;
|
|
287
|
+
const matched = await this.resolve(rule, [...at, "rule"], chain, true);
|
|
288
|
+
kept.push({ rule: matched, ...(await this.#resolveFields(rest, at, chain, true)) });
|
|
289
|
+
if (matched === true) break;
|
|
290
|
+
}
|
|
291
|
+
return { ...resolved, variants: kept };
|
|
292
|
+
}
|
|
293
|
+
|
|
257
294
|
#lazy(value: unknown, path: Path, chain: string[], fromContent: boolean): Lazy<unknown> {
|
|
258
295
|
let pending: Promise<unknown> | undefined;
|
|
259
296
|
return () => {
|
package/src/v8/types.ts
CHANGED
|
@@ -256,9 +256,10 @@ export interface CMSConfig {
|
|
|
256
256
|
/** ms between `update()` checks of a content source that has one; minimum 60 000. */
|
|
257
257
|
interval?: number;
|
|
258
258
|
/**
|
|
259
|
-
* Local development
|
|
260
|
-
*
|
|
261
|
-
*
|
|
259
|
+
* Local development: hosted releases are never swapped in, so local files win.
|
|
260
|
+
* Drafts still load. Default: `process.env.NODE_ENV === "development"` (read
|
|
261
|
+
* guarded, so `false` where there is no `process`: Workers, browsers). Pass it
|
|
262
|
+
* to override that, e.g. `dev: import.meta.env.DEV` or `dev: false`.
|
|
262
263
|
*/
|
|
263
264
|
dev?: boolean;
|
|
264
265
|
/** Where telemetry goes; see /next/telemetry. */
|