@byscott-io/pagelet 0.0.0-stage → 0.1.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/CHANGELOG.md +30 -0
- package/LICENSE +21 -0
- package/README.md +140 -2
- package/dist/editor.cjs +59 -0
- package/dist/editor.cjs.map +1 -0
- package/dist/editor.css +3214 -0
- package/dist/editor.js +57 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.cjs +599 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +582 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
+
import { migrate, Render } from '@puckeditor/core/rsc';
|
|
3
|
+
|
|
4
|
+
const DOCUMENT_VERSION = 1;
|
|
5
|
+
function emptyData() {
|
|
6
|
+
return { root: { props: {} }, content: [] };
|
|
7
|
+
}
|
|
8
|
+
function createDocument(data = emptyData()) {
|
|
9
|
+
return { pagelet: DOCUMENT_VERSION, data };
|
|
10
|
+
}
|
|
11
|
+
function upgradeDocument(document, palette) {
|
|
12
|
+
if (document == null) return createDocument();
|
|
13
|
+
if (typeof document !== "object") {
|
|
14
|
+
throw new TypeError("pagelet: a page document must be an object");
|
|
15
|
+
}
|
|
16
|
+
let doc = document;
|
|
17
|
+
if (!("pagelet" in doc)) {
|
|
18
|
+
if (!("content" in doc) && !("root" in doc)) {
|
|
19
|
+
throw new TypeError('pagelet: not a page document (no "pagelet" version and no Puck data)');
|
|
20
|
+
}
|
|
21
|
+
doc = createDocument(doc);
|
|
22
|
+
}
|
|
23
|
+
if (doc.pagelet > DOCUMENT_VERSION) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`pagelet: this page was saved by a newer pagelet (format ${doc.pagelet}); this version reads up to format ${DOCUMENT_VERSION}. Upgrade @byscott-io/pagelet.`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
const data = { ...emptyData(), ...doc.data };
|
|
29
|
+
return createDocument(palette && needsPuckMigration(data) ? migrate(data, palette.config) : data);
|
|
30
|
+
}
|
|
31
|
+
function needsPuckMigration(data) {
|
|
32
|
+
const zones = data.zones ?? {};
|
|
33
|
+
if (Object.values(zones).some((content) => Array.isArray(content) && content.length > 0)) return true;
|
|
34
|
+
const root = data.root ?? {};
|
|
35
|
+
return Object.keys(root).some((key) => key !== "props" && key !== "readOnly");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function PageletPage({ document, palette, theme }) {
|
|
39
|
+
if (!palette) throw new Error("pagelet: <PageletPage> needs a palette (see createPalette)");
|
|
40
|
+
const { data } = upgradeDocument(document, palette);
|
|
41
|
+
return /* @__PURE__ */ jsx(Render, { config: palette.config, data, metadata: { pageletTheme: theme } });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const SAFE_SCHEMES = ["http:", "https:", "mailto:", "tel:"];
|
|
45
|
+
function safeUrl(value) {
|
|
46
|
+
if (typeof value !== "string") return void 0;
|
|
47
|
+
const url = value.trim();
|
|
48
|
+
if (url === "") return void 0;
|
|
49
|
+
if (url.startsWith("/") && !url.startsWith("//")) return url;
|
|
50
|
+
if (url.startsWith("#") || url.startsWith("?")) return url;
|
|
51
|
+
const normalized = url.replace(/[\u0000- ]/g, "").toLowerCase();
|
|
52
|
+
const scheme = normalized.match(/^([a-z][a-z0-9+.-]*):/);
|
|
53
|
+
if (!scheme) return void 0;
|
|
54
|
+
return SAFE_SCHEMES.includes(`${scheme[1]}:`) ? url : void 0;
|
|
55
|
+
}
|
|
56
|
+
function safeImageUrl(value) {
|
|
57
|
+
const url = safeUrl(value);
|
|
58
|
+
if (!url) return void 0;
|
|
59
|
+
return /^(mailto|tel):/i.test(url) ? void 0 : url;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const defaultTheme = {
|
|
63
|
+
colorText: "#1f2933",
|
|
64
|
+
colorBackground: "#ffffff",
|
|
65
|
+
colorMuted: "#f3f4f6",
|
|
66
|
+
colorAccent: "#2457a6",
|
|
67
|
+
colorAccentText: "#ffffff",
|
|
68
|
+
fontBody: 'system-ui, -apple-system, "Segoe UI", Roboto, sans-serif',
|
|
69
|
+
fontHeading: "inherit",
|
|
70
|
+
radius: "8px",
|
|
71
|
+
maxWidth: "1100px",
|
|
72
|
+
space: "24px"
|
|
73
|
+
};
|
|
74
|
+
const TOKEN_NAMES = Object.keys(defaultTheme);
|
|
75
|
+
function cssVariableName(token) {
|
|
76
|
+
return `--pagelet-${token.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`;
|
|
77
|
+
}
|
|
78
|
+
function themeToStyle(theme = {}) {
|
|
79
|
+
const merged = { ...defaultTheme, ...theme };
|
|
80
|
+
const style = {};
|
|
81
|
+
for (const token of TOKEN_NAMES) {
|
|
82
|
+
style[cssVariableName(token)] = String(merged[token]);
|
|
83
|
+
}
|
|
84
|
+
style.color = "var(--pagelet-color-text)";
|
|
85
|
+
style.background = "var(--pagelet-color-background)";
|
|
86
|
+
style.fontFamily = "var(--pagelet-font-body)";
|
|
87
|
+
return style;
|
|
88
|
+
}
|
|
89
|
+
function tokenVar(token) {
|
|
90
|
+
return `var(${cssVariableName(token)})`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const alignOptions = [
|
|
94
|
+
{ label: "Left", value: "left" },
|
|
95
|
+
{ label: "Center", value: "center" }
|
|
96
|
+
];
|
|
97
|
+
function paragraphs(text) {
|
|
98
|
+
return String(text ?? "").split(/\n\s*\n/).map((paragraph) => paragraph.trim()).filter(Boolean);
|
|
99
|
+
}
|
|
100
|
+
function Paragraphs({ text, style }) {
|
|
101
|
+
return paragraphs(text).map((paragraph, index) => /* @__PURE__ */ jsx("p", { style: { margin: "0 0 1em", whiteSpace: "pre-line", ...style }, children: paragraph }, index));
|
|
102
|
+
}
|
|
103
|
+
function ButtonLink({ label, href, variant = "primary" }) {
|
|
104
|
+
const url = safeUrl(href);
|
|
105
|
+
if (!label) return null;
|
|
106
|
+
const primary = variant !== "outline";
|
|
107
|
+
return /* @__PURE__ */ jsx(
|
|
108
|
+
"a",
|
|
109
|
+
{
|
|
110
|
+
href: url,
|
|
111
|
+
style: {
|
|
112
|
+
display: "inline-block",
|
|
113
|
+
padding: "10px 20px",
|
|
114
|
+
borderRadius: tokenVar("radius"),
|
|
115
|
+
border: `2px solid ${tokenVar("colorAccent")}`,
|
|
116
|
+
background: primary ? tokenVar("colorAccent") : "transparent",
|
|
117
|
+
color: primary ? tokenVar("colorAccentText") : tokenVar("colorAccent"),
|
|
118
|
+
fontWeight: 600,
|
|
119
|
+
textDecoration: "none"
|
|
120
|
+
},
|
|
121
|
+
children: label
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const buttonFields = {
|
|
126
|
+
buttonLabel: { type: "text", label: "Button label" },
|
|
127
|
+
buttonHref: { type: "text", label: "Button link" }
|
|
128
|
+
};
|
|
129
|
+
const Heading = {
|
|
130
|
+
label: "Heading",
|
|
131
|
+
category: "content",
|
|
132
|
+
fields: {
|
|
133
|
+
text: { type: "text" },
|
|
134
|
+
level: {
|
|
135
|
+
type: "select",
|
|
136
|
+
options: [
|
|
137
|
+
{ label: "Large (H1)", value: "h1" },
|
|
138
|
+
{ label: "Medium (H2)", value: "h2" },
|
|
139
|
+
{ label: "Small (H3)", value: "h3" }
|
|
140
|
+
]
|
|
141
|
+
},
|
|
142
|
+
align: { type: "radio", options: alignOptions }
|
|
143
|
+
},
|
|
144
|
+
defaultProps: { text: "Heading", level: "h2", align: "left" },
|
|
145
|
+
render: ({ text, level, align }) => {
|
|
146
|
+
const Tag = ["h1", "h2", "h3"].includes(level) ? level : "h2";
|
|
147
|
+
return /* @__PURE__ */ jsx(Tag, { style: { fontFamily: tokenVar("fontHeading"), textAlign: align, margin: "0 0 0.5em", textWrap: "balance" }, children: text });
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const Text = {
|
|
151
|
+
label: "Text",
|
|
152
|
+
category: "content",
|
|
153
|
+
fields: {
|
|
154
|
+
text: { type: "textarea" },
|
|
155
|
+
align: { type: "radio", options: alignOptions }
|
|
156
|
+
},
|
|
157
|
+
defaultProps: { text: "Write something here.", align: "left" },
|
|
158
|
+
render: ({ text, align }) => /* @__PURE__ */ jsx("div", { style: { textAlign: align, maxWidth: "68ch", marginInline: align === "center" ? "auto" : void 0 }, children: /* @__PURE__ */ jsx(Paragraphs, { text }) })
|
|
159
|
+
};
|
|
160
|
+
const Button = {
|
|
161
|
+
label: "Button",
|
|
162
|
+
category: "content",
|
|
163
|
+
fields: {
|
|
164
|
+
label: { type: "text" },
|
|
165
|
+
href: { type: "text", label: "Link" },
|
|
166
|
+
variant: {
|
|
167
|
+
type: "radio",
|
|
168
|
+
options: [
|
|
169
|
+
{ label: "Filled", value: "primary" },
|
|
170
|
+
{ label: "Outline", value: "outline" }
|
|
171
|
+
]
|
|
172
|
+
},
|
|
173
|
+
align: { type: "radio", options: alignOptions }
|
|
174
|
+
},
|
|
175
|
+
defaultProps: { label: "Learn more", href: "#", variant: "primary", align: "left" },
|
|
176
|
+
render: ({ label, href, variant, align }) => /* @__PURE__ */ jsx("div", { style: { textAlign: align, margin: "0 0 1em" }, children: /* @__PURE__ */ jsx(ButtonLink, { label, href, variant }) })
|
|
177
|
+
};
|
|
178
|
+
const Hero = {
|
|
179
|
+
label: "Hero",
|
|
180
|
+
category: "content",
|
|
181
|
+
fields: {
|
|
182
|
+
heading: { type: "text" },
|
|
183
|
+
subheading: { type: "textarea" },
|
|
184
|
+
imageUrl: { type: "text", label: "Background image URL" },
|
|
185
|
+
...buttonFields
|
|
186
|
+
},
|
|
187
|
+
defaultProps: { heading: "Welcome", subheading: "", imageUrl: "", buttonLabel: "", buttonHref: "" },
|
|
188
|
+
render: ({ heading, subheading, imageUrl, buttonLabel, buttonHref }) => {
|
|
189
|
+
const image = safeImageUrl(imageUrl);
|
|
190
|
+
return /* @__PURE__ */ jsx(
|
|
191
|
+
"header",
|
|
192
|
+
{
|
|
193
|
+
style: {
|
|
194
|
+
padding: "96px 16px",
|
|
195
|
+
textAlign: "center",
|
|
196
|
+
color: image ? "#ffffff" : tokenVar("colorAccentText"),
|
|
197
|
+
background: image ? `linear-gradient(rgba(0,0,0,0.45), rgba(0,0,0,0.45)), url("${encodeURI(image)}") center / cover` : tokenVar("colorAccent")
|
|
198
|
+
},
|
|
199
|
+
children: /* @__PURE__ */ jsxs("div", { style: { maxWidth: "760px", margin: "0 auto" }, children: [
|
|
200
|
+
/* @__PURE__ */ jsx("h1", { style: { fontFamily: tokenVar("fontHeading"), fontSize: "clamp(2rem, 5vw, 3.25rem)", margin: "0 0 0.4em", textWrap: "balance" }, children: heading }),
|
|
201
|
+
/* @__PURE__ */ jsx(Paragraphs, { text: subheading, style: { fontSize: "1.15rem" } }),
|
|
202
|
+
/* @__PURE__ */ jsx(ButtonLink, { label: buttonLabel, href: buttonHref, variant: "outline" })
|
|
203
|
+
] })
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const CallToAction = {
|
|
209
|
+
label: "Call to action",
|
|
210
|
+
category: "content",
|
|
211
|
+
fields: {
|
|
212
|
+
heading: { type: "text" },
|
|
213
|
+
text: { type: "textarea" },
|
|
214
|
+
...buttonFields
|
|
215
|
+
},
|
|
216
|
+
defaultProps: { heading: "Join us", text: "", buttonLabel: "Get in touch", buttonHref: "#" },
|
|
217
|
+
render: ({ heading, text, buttonLabel, buttonHref }) => /* @__PURE__ */ jsxs("div", { style: { background: tokenVar("colorMuted"), borderRadius: tokenVar("radius"), padding: "40px 24px", textAlign: "center" }, children: [
|
|
218
|
+
/* @__PURE__ */ jsx("h2", { style: { fontFamily: tokenVar("fontHeading"), margin: "0 0 0.5em" }, children: heading }),
|
|
219
|
+
/* @__PURE__ */ jsx(Paragraphs, { text }),
|
|
220
|
+
/* @__PURE__ */ jsx(ButtonLink, { label: buttonLabel, href: buttonHref })
|
|
221
|
+
] })
|
|
222
|
+
};
|
|
223
|
+
const Cards = {
|
|
224
|
+
label: "Cards",
|
|
225
|
+
category: "content",
|
|
226
|
+
fields: {
|
|
227
|
+
items: {
|
|
228
|
+
type: "array",
|
|
229
|
+
getItemSummary: (item) => item.title || "Card",
|
|
230
|
+
arrayFields: {
|
|
231
|
+
title: { type: "text" },
|
|
232
|
+
text: { type: "textarea" },
|
|
233
|
+
imageUrl: { type: "text", label: "Image URL" },
|
|
234
|
+
href: { type: "text", label: "Link (optional)" }
|
|
235
|
+
},
|
|
236
|
+
defaultItemProps: { title: "Card title", text: "", imageUrl: "", href: "" }
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
defaultProps: { items: [{ title: "Card title", text: "A short description.", imageUrl: "", href: "" }] },
|
|
240
|
+
render: ({ items = [] }) => /* @__PURE__ */ jsx("div", { style: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(min(100%, 240px), 1fr))", gap: tokenVar("space") }, children: items.map((item, index) => {
|
|
241
|
+
const image = safeImageUrl(item.imageUrl);
|
|
242
|
+
const href = safeUrl(item.href);
|
|
243
|
+
const body = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
244
|
+
image && /* @__PURE__ */ jsx("img", { src: image, alt: "", style: { width: "100%", aspectRatio: "16 / 9", objectFit: "cover", display: "block" } }),
|
|
245
|
+
/* @__PURE__ */ jsxs("div", { style: { padding: "16px" }, children: [
|
|
246
|
+
/* @__PURE__ */ jsx("h3", { style: { fontFamily: tokenVar("fontHeading"), margin: "0 0 0.4em" }, children: item.title }),
|
|
247
|
+
/* @__PURE__ */ jsx(Paragraphs, { text: item.text })
|
|
248
|
+
] })
|
|
249
|
+
] });
|
|
250
|
+
const cardStyle = { display: "block", background: tokenVar("colorMuted"), borderRadius: tokenVar("radius"), overflow: "hidden", color: "inherit", textDecoration: "none" };
|
|
251
|
+
return href ? /* @__PURE__ */ jsx("a", { href, style: cardStyle, children: body }, index) : /* @__PURE__ */ jsx("div", { style: cardStyle, children: body }, index);
|
|
252
|
+
}) })
|
|
253
|
+
};
|
|
254
|
+
const Faq = {
|
|
255
|
+
label: "FAQ",
|
|
256
|
+
category: "content",
|
|
257
|
+
fields: {
|
|
258
|
+
items: {
|
|
259
|
+
type: "array",
|
|
260
|
+
getItemSummary: (item) => item.question || "Question",
|
|
261
|
+
arrayFields: { question: { type: "text" }, answer: { type: "textarea" } },
|
|
262
|
+
defaultItemProps: { question: "Question?", answer: "" }
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
defaultProps: { items: [{ question: "Question?", answer: "Answer." }] },
|
|
266
|
+
render: ({ items = [] }) => /* @__PURE__ */ jsx("div", { children: items.map((item, index) => /* @__PURE__ */ jsxs("details", { style: { borderBottom: `1px solid ${tokenVar("colorMuted")}`, padding: "12px 0" }, children: [
|
|
267
|
+
/* @__PURE__ */ jsx("summary", { style: { fontWeight: 600, cursor: "pointer" }, children: item.question }),
|
|
268
|
+
/* @__PURE__ */ jsx("div", { style: { paddingTop: "8px" }, children: /* @__PURE__ */ jsx(Paragraphs, { text: item.answer }) })
|
|
269
|
+
] }, index)) })
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const SPACING = { none: "0", small: "16px", medium: "40px", large: "80px" };
|
|
273
|
+
const spacingOptions = Object.keys(SPACING).map((value) => ({ label: value, value }));
|
|
274
|
+
const BACKGROUNDS = {
|
|
275
|
+
none: "transparent",
|
|
276
|
+
muted: tokenVar("colorMuted"),
|
|
277
|
+
accent: tokenVar("colorAccent")
|
|
278
|
+
};
|
|
279
|
+
const Section = {
|
|
280
|
+
label: "Section",
|
|
281
|
+
category: "layout",
|
|
282
|
+
fields: {
|
|
283
|
+
background: {
|
|
284
|
+
type: "select",
|
|
285
|
+
options: [
|
|
286
|
+
{ label: "None", value: "none" },
|
|
287
|
+
{ label: "Muted", value: "muted" },
|
|
288
|
+
{ label: "Accent", value: "accent" }
|
|
289
|
+
]
|
|
290
|
+
},
|
|
291
|
+
padding: { type: "select", options: spacingOptions },
|
|
292
|
+
content: { type: "slot" }
|
|
293
|
+
},
|
|
294
|
+
defaultProps: { background: "none", padding: "medium" },
|
|
295
|
+
render: ({ background, padding, content: Content }) => /* @__PURE__ */ jsx(
|
|
296
|
+
"section",
|
|
297
|
+
{
|
|
298
|
+
style: {
|
|
299
|
+
background: BACKGROUNDS[background] ?? BACKGROUNDS.none,
|
|
300
|
+
color: background === "accent" ? tokenVar("colorAccentText") : void 0,
|
|
301
|
+
paddingBlock: SPACING[padding] ?? SPACING.medium
|
|
302
|
+
},
|
|
303
|
+
children: /* @__PURE__ */ jsx("div", { style: { maxWidth: tokenVar("maxWidth"), margin: "0 auto", paddingInline: "16px" }, children: /* @__PURE__ */ jsx(Content, {}) })
|
|
304
|
+
}
|
|
305
|
+
)
|
|
306
|
+
};
|
|
307
|
+
const Columns = {
|
|
308
|
+
label: "Columns",
|
|
309
|
+
category: "layout",
|
|
310
|
+
fields: {
|
|
311
|
+
count: {
|
|
312
|
+
type: "radio",
|
|
313
|
+
options: [
|
|
314
|
+
{ label: "2", value: 2 },
|
|
315
|
+
{ label: "3", value: 3 }
|
|
316
|
+
]
|
|
317
|
+
},
|
|
318
|
+
first: { type: "slot" },
|
|
319
|
+
second: { type: "slot" },
|
|
320
|
+
third: { type: "slot" }
|
|
321
|
+
},
|
|
322
|
+
defaultProps: { count: 2 },
|
|
323
|
+
render: ({ count, first: First, second: Second, third: Third }) => {
|
|
324
|
+
const columns = count === 3 ? [First, Second, Third] : [First, Second];
|
|
325
|
+
return /* @__PURE__ */ jsx(
|
|
326
|
+
"div",
|
|
327
|
+
{
|
|
328
|
+
style: {
|
|
329
|
+
display: "grid",
|
|
330
|
+
gridTemplateColumns: `repeat(auto-fit, minmax(min(100%, ${count === 3 ? 240 : 320}px), 1fr))`,
|
|
331
|
+
gap: tokenVar("space")
|
|
332
|
+
},
|
|
333
|
+
children: columns.map((Column, index) => /* @__PURE__ */ jsx("div", { style: { minWidth: 0 }, children: /* @__PURE__ */ jsx(Column, {}) }, index))
|
|
334
|
+
}
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
const Spacer = {
|
|
339
|
+
label: "Spacer",
|
|
340
|
+
category: "layout",
|
|
341
|
+
fields: { size: { type: "select", options: spacingOptions.slice(1) } },
|
|
342
|
+
defaultProps: { size: "medium" },
|
|
343
|
+
render: ({ size }) => /* @__PURE__ */ jsx("div", { "aria-hidden": "true", style: { height: SPACING[size] ?? SPACING.medium } })
|
|
344
|
+
};
|
|
345
|
+
const Divider = {
|
|
346
|
+
label: "Divider",
|
|
347
|
+
category: "layout",
|
|
348
|
+
fields: {},
|
|
349
|
+
render: () => /* @__PURE__ */ jsx("hr", { style: { border: 0, borderTop: `1px solid ${tokenVar("colorMuted")}`, margin: `${tokenVar("space")} 0` } })
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const Image = {
|
|
353
|
+
label: "Image",
|
|
354
|
+
category: "media",
|
|
355
|
+
fields: {
|
|
356
|
+
url: { type: "text", label: "Image URL" },
|
|
357
|
+
alt: { type: "text", label: "Description (for screen readers)" },
|
|
358
|
+
caption: { type: "text" }
|
|
359
|
+
},
|
|
360
|
+
defaultProps: { url: "", alt: "", caption: "" },
|
|
361
|
+
render: ({ url, alt, caption }) => {
|
|
362
|
+
const src = safeImageUrl(url);
|
|
363
|
+
if (!src) return null;
|
|
364
|
+
return /* @__PURE__ */ jsxs("figure", { style: { margin: "0 0 1em" }, children: [
|
|
365
|
+
/* @__PURE__ */ jsx("img", { src, alt: alt ?? "", style: { maxWidth: "100%", height: "auto", display: "block", borderRadius: tokenVar("radius") } }),
|
|
366
|
+
caption && /* @__PURE__ */ jsx("figcaption", { style: { fontSize: "0.9em", opacity: 0.75, marginTop: "6px" }, children: caption })
|
|
367
|
+
] });
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const ImageText = {
|
|
371
|
+
label: "Image with text",
|
|
372
|
+
category: "media",
|
|
373
|
+
fields: {
|
|
374
|
+
url: { type: "text", label: "Image URL" },
|
|
375
|
+
alt: { type: "text", label: "Image description" },
|
|
376
|
+
heading: { type: "text" },
|
|
377
|
+
text: { type: "textarea" },
|
|
378
|
+
imageSide: {
|
|
379
|
+
type: "radio",
|
|
380
|
+
options: [
|
|
381
|
+
{ label: "Left", value: "left" },
|
|
382
|
+
{ label: "Right", value: "right" }
|
|
383
|
+
]
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
defaultProps: { url: "", alt: "", heading: "Heading", text: "", imageSide: "left" },
|
|
387
|
+
render: ({ url, alt, heading, text, imageSide }) => {
|
|
388
|
+
const src = safeImageUrl(url);
|
|
389
|
+
const image = src ? /* @__PURE__ */ jsx("img", { src, alt: alt ?? "", style: { width: "100%", height: "auto", display: "block", borderRadius: tokenVar("radius"), order: imageSide === "right" ? 2 : 0 } }) : null;
|
|
390
|
+
const words = /* @__PURE__ */ jsxs("div", { style: { minWidth: 0, order: 1 }, children: [
|
|
391
|
+
/* @__PURE__ */ jsx("h2", { style: { fontFamily: tokenVar("fontHeading"), margin: "0 0 0.5em" }, children: heading }),
|
|
392
|
+
String(text ?? "").split(/\n\s*\n/).filter((paragraph) => paragraph.trim()).map((paragraph, index) => /* @__PURE__ */ jsx("p", { style: { margin: "0 0 1em" }, children: paragraph.trim() }, index))
|
|
393
|
+
] });
|
|
394
|
+
return /* @__PURE__ */ jsxs("div", { style: { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(min(100%, 300px), 1fr))", gap: tokenVar("space"), alignItems: "center" }, children: [
|
|
395
|
+
image,
|
|
396
|
+
words
|
|
397
|
+
] });
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
function embedUrl(value) {
|
|
401
|
+
let url;
|
|
402
|
+
try {
|
|
403
|
+
url = new URL(String(value ?? "").trim());
|
|
404
|
+
} catch {
|
|
405
|
+
return void 0;
|
|
406
|
+
}
|
|
407
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") return void 0;
|
|
408
|
+
const host = url.hostname.replace(/^www\./, "").replace(/^m\./, "");
|
|
409
|
+
let id;
|
|
410
|
+
if (host === "youtube.com") {
|
|
411
|
+
id = url.pathname === "/watch" ? url.searchParams.get("v") : url.pathname.match(/^\/(?:embed|shorts|live)\/([^/]+)/)?.[1];
|
|
412
|
+
return id && /^[\w-]{6,}$/.test(id) ? `https://www.youtube-nocookie.com/embed/${id}` : void 0;
|
|
413
|
+
}
|
|
414
|
+
if (host === "youtu.be") {
|
|
415
|
+
id = url.pathname.slice(1);
|
|
416
|
+
return /^[\w-]{6,}$/.test(id) ? `https://www.youtube-nocookie.com/embed/${id}` : void 0;
|
|
417
|
+
}
|
|
418
|
+
if (host === "vimeo.com") {
|
|
419
|
+
id = url.pathname.match(/^\/(\d+)/)?.[1];
|
|
420
|
+
return id ? `https://player.vimeo.com/video/${id}` : void 0;
|
|
421
|
+
}
|
|
422
|
+
return void 0;
|
|
423
|
+
}
|
|
424
|
+
const VideoEmbed = {
|
|
425
|
+
label: "Video (YouTube or Vimeo)",
|
|
426
|
+
category: "media",
|
|
427
|
+
fields: {
|
|
428
|
+
url: { type: "text", label: "Video link" },
|
|
429
|
+
title: { type: "text", label: "Title (for screen readers)" }
|
|
430
|
+
},
|
|
431
|
+
defaultProps: { url: "", title: "Video" },
|
|
432
|
+
render: ({ url, title }) => {
|
|
433
|
+
const src = embedUrl(url);
|
|
434
|
+
if (!src) return null;
|
|
435
|
+
return /* @__PURE__ */ jsx("div", { style: { position: "relative", aspectRatio: "16 / 9", maxWidth: "100%", margin: "0 0 1em" }, children: /* @__PURE__ */ jsx(
|
|
436
|
+
"iframe",
|
|
437
|
+
{
|
|
438
|
+
src,
|
|
439
|
+
title: title || "Video",
|
|
440
|
+
loading: "lazy",
|
|
441
|
+
allow: "accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen",
|
|
442
|
+
allowFullScreen: true,
|
|
443
|
+
style: { position: "absolute", inset: 0, width: "100%", height: "100%", border: 0, borderRadius: tokenVar("radius") }
|
|
444
|
+
}
|
|
445
|
+
) });
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
const builtInBlocks = {
|
|
450
|
+
Section,
|
|
451
|
+
Columns,
|
|
452
|
+
Spacer,
|
|
453
|
+
Divider,
|
|
454
|
+
Hero,
|
|
455
|
+
Heading,
|
|
456
|
+
Text,
|
|
457
|
+
Button,
|
|
458
|
+
CallToAction,
|
|
459
|
+
Cards,
|
|
460
|
+
Faq,
|
|
461
|
+
Image,
|
|
462
|
+
ImageText,
|
|
463
|
+
VideoEmbed
|
|
464
|
+
};
|
|
465
|
+
const builtInCategories = {
|
|
466
|
+
layout: { title: "Layout" },
|
|
467
|
+
content: { title: "Content" },
|
|
468
|
+
media: { title: "Media" }
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
function ThemeRoot({ theme, children }) {
|
|
472
|
+
return /* @__PURE__ */ jsx("div", { className: "pagelet", style: themeToStyle(theme), children });
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function createPalette({ blocks = {}, hide = [], categories = {}, root = {} } = {}) {
|
|
476
|
+
const hidden = new Set(hide);
|
|
477
|
+
for (const name of hidden) {
|
|
478
|
+
if (!(name in builtInBlocks) && !(name in blocks)) {
|
|
479
|
+
throw new Error(`pagelet: cannot hide unknown block "${name}"`);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const all = { ...builtInBlocks, ...blocks };
|
|
483
|
+
const components = {};
|
|
484
|
+
const membership = {};
|
|
485
|
+
for (const [name, block] of Object.entries(all)) {
|
|
486
|
+
if (hidden.has(name)) continue;
|
|
487
|
+
if (typeof block?.render !== "function") {
|
|
488
|
+
throw new Error(`pagelet: block "${name}" needs a render function`);
|
|
489
|
+
}
|
|
490
|
+
const { category = "other", ...component } = block;
|
|
491
|
+
components[name] = component;
|
|
492
|
+
(membership[category] ?? (membership[category] = [])).push(name);
|
|
493
|
+
}
|
|
494
|
+
const categoryInfo = { ...builtInCategories, ...categories };
|
|
495
|
+
const puckCategories = {};
|
|
496
|
+
const order = [.../* @__PURE__ */ new Set([...Object.keys(categories), ...Object.keys(builtInCategories), ...Object.keys(membership)])];
|
|
497
|
+
for (const category of order) {
|
|
498
|
+
if (!membership[category]) continue;
|
|
499
|
+
puckCategories[category] = { title: categoryInfo[category]?.title ?? category, components: membership[category] };
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
blocks: Object.keys(components),
|
|
503
|
+
config: {
|
|
504
|
+
components,
|
|
505
|
+
categories: puckCategories,
|
|
506
|
+
root: {
|
|
507
|
+
...root,
|
|
508
|
+
// The page's root render applies the theme. It is the root of the
|
|
509
|
+
// editor's preview iframe as well as of a rendered page, so the theme
|
|
510
|
+
// reaches both. Pages receive it through Puck's metadata.
|
|
511
|
+
render: ({ children, puck }) => /* @__PURE__ */ jsx(ThemeRoot, { theme: puck?.metadata?.pageletTheme, children })
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
class PageletApiError extends Error {
|
|
518
|
+
constructor(message, { status, errors } = {}) {
|
|
519
|
+
super(message);
|
|
520
|
+
this.name = "PageletApiError";
|
|
521
|
+
this.status = status;
|
|
522
|
+
this.errors = errors ?? {};
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
function csrfFromPage() {
|
|
526
|
+
if (typeof document === "undefined") return void 0;
|
|
527
|
+
return document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") ?? void 0;
|
|
528
|
+
}
|
|
529
|
+
function createPageletClient({ baseUrl = "/pagelet", csrfToken, headers = {}, fetch: fetchImpl } = {}) {
|
|
530
|
+
const doFetch = fetchImpl ?? globalThis.fetch;
|
|
531
|
+
if (typeof doFetch !== "function") throw new Error("pagelet: no fetch available; pass one to createPageletClient");
|
|
532
|
+
let root = String(baseUrl);
|
|
533
|
+
while (root.endsWith("/")) root = root.slice(0, -1);
|
|
534
|
+
async function request(method, path, body) {
|
|
535
|
+
const token = typeof csrfToken === "function" ? csrfToken() : csrfToken ?? csrfFromPage();
|
|
536
|
+
const response = await doFetch(`${root}${path}`, {
|
|
537
|
+
method,
|
|
538
|
+
credentials: "same-origin",
|
|
539
|
+
headers: {
|
|
540
|
+
Accept: "application/json",
|
|
541
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" },
|
|
542
|
+
...token && method !== "GET" ? { "X-CSRF-Token": token } : {},
|
|
543
|
+
...headers
|
|
544
|
+
},
|
|
545
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
546
|
+
});
|
|
547
|
+
if (response.status === 204) return null;
|
|
548
|
+
const payload = await response.json().catch(() => ({}));
|
|
549
|
+
if (response.ok) return payload;
|
|
550
|
+
const messages = Object.values(payload.errors ?? {}).flat();
|
|
551
|
+
const detail = messages.length ? messages.join("; ") : payload.error ?? response.statusText;
|
|
552
|
+
throw new PageletApiError(`pagelet: ${method} ${path} failed (${response.status}): ${detail}`, {
|
|
553
|
+
status: response.status,
|
|
554
|
+
errors: payload.errors
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
const pagePath = (id) => `/pages/${encodeURIComponent(id)}`;
|
|
558
|
+
return {
|
|
559
|
+
listPages: () => request("GET", "/pages"),
|
|
560
|
+
getPage: (id) => request("GET", pagePath(id)),
|
|
561
|
+
createPage: ({ path, title }) => request("POST", "/pages", { page: { path, title } }),
|
|
562
|
+
updatePage: (id, { path, title }) => request("PATCH", pagePath(id), { page: { path, title } }),
|
|
563
|
+
saveDraft: (id, document2) => request("PATCH", pagePath(id), { page: { draft_document: document2 } }),
|
|
564
|
+
deletePage: (id) => request("DELETE", pagePath(id)),
|
|
565
|
+
publish: (id, { publishedBy } = {}) => request("POST", `${pagePath(id)}/publish`, publishedBy ? { published_by: publishedBy } : {}),
|
|
566
|
+
listVersions: (id) => request("GET", `${pagePath(id)}/versions`),
|
|
567
|
+
restoreVersion: (id, number) => request("POST", `${pagePath(id)}/versions/${encodeURIComponent(number)}/restore`, {}),
|
|
568
|
+
// Public: the published document at a path, or null when there is none.
|
|
569
|
+
getPublished: async (path) => {
|
|
570
|
+
try {
|
|
571
|
+
const { document: document2 } = await request("GET", `/published?path=${encodeURIComponent(path)}`);
|
|
572
|
+
return document2;
|
|
573
|
+
} catch (error) {
|
|
574
|
+
if (error instanceof PageletApiError && error.status === 404) return null;
|
|
575
|
+
throw error;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
export { DOCUMENT_VERSION, PageletApiError, PageletPage, builtInBlocks, builtInCategories, createDocument, createPageletClient, createPalette, defaultTheme, embedUrl, emptyData, safeImageUrl, safeUrl, themeToStyle, tokenVar, upgradeDocument };
|
|
582
|
+
//# sourceMappingURL=index.js.map
|