payload-canvas 0.6.5 → 0.6.7
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/README.md +1 -0
- package/dist/classes/collection.js +2 -0
- package/dist/core/clearAgain.d.ts +6 -0
- package/dist/core/clearAgain.js +14 -0
- package/dist/react/components/RichText.js +4 -0
- package/dist/react/components/richTextUpload.d.ts +43 -0
- package/dist/react/components/richTextUpload.js +115 -0
- package/dist/theme/global.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1079,6 +1079,7 @@ export const blocks = [...defaultBlocks({ linkCollections: ['pages'] }), pricing
|
|
|
1079
1079
|
- The `field` block renders rich text with the `prose` typography (and `max-w-none`) when its own `className` has no `prose` class. Add `prose prose-lg` or another `prose` class to take control.
|
|
1080
1080
|
- `field` `path` takes a binding formatter after a `|` (see "Binding syntax"): `price|currency:EUR` gives `€1,200`, `publishedAt|date:en-GB` a date in that language, `content|readingTime` gives `5 min read`. The inspector edits it with the **Format** select under the field. `formatBindingValue(value, "currency:EUR")` in `payload-canvas/core` formats a value the same way.
|
|
1081
1081
|
- `field` and `richText` give each rich-text heading an `id` from its text, for `#id` links and the [table of contents](#table-of-contents): `headingId('Über uns & Preise')` is `uber-uns-preise`. A repeated text gets `-2`, `-3`, and a heading without letters or digits gets `section`. `richTextHeadings(value)` in `payload-canvas/core` lists the headings with the same ids (`{ id, text, level, node }`). Two rich texts on one page with the same heading text give two elements the same id; the first one wins a `#id` link.
|
|
1082
|
+
- Photos in rich text (`field`, `richText` and `renderRichText`) render as one `<img class="builder-richtext-image">` with a `srcset` of Payload's resized copies that have the photo's own shape. Cropped sizes never join, so the whole photo shows at every screen width. The original joins only when it is light (not a PNG, 600 KB or less) and no copy is 1920 px wide. URLs get `?v=<id>-<updatedAt>`, so an edited photo gets a new URL. The image has `width` and `height` from the original, `loading="lazy"`, `decoding="async"` and `sizes="auto, (min-width: 1024px) 768px, 100vw"`. The alt text is the upload node's alt, then the media alt. An upload that is only an id (not loaded) renders nothing. Other files keep Payload's default link. Round the corners from the site's CSS: `.prose { --builder-richtext-image-radius: 0.75rem; }`. The default styles use `:where()`, so any rule of the site on `.builder-richtext-image` wins.
|
|
1082
1083
|
- A list holds its items as `listItem` blocks in its `items` slot, so each item can be selected, dragged, styled and edited on the canvas. Enter at the end of an item adds the next one; Backspace at the start of an item joins it to the one before. Older layouts stored the items as a prop (`props.items: [{ text }]`). `normalizeLayout` turns them into `listItem` blocks when a layout loads, and the List component still renders the old prop until the layout is saved again. A list whose `items` prop is bound to document data keeps the old form. A `listItem` takes an optional `link` (the same link group as `button`): the item renders as `<li><a href>text</a></li>`, and the text stays editable in place. Do not set it on items of a list inside a `link` block, because a link cannot hold another link; the item's link is a prop, so the link block's type rules do not catch it.
|
|
1083
1084
|
|
|
1084
1085
|
### Where a block renders: `useCanvasMode()`
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The classes collection: one document per editable class, with drafts and versions. Server-only
|
|
2
2
|
// (the revalidation imports next/cache).
|
|
3
3
|
import { revalidatePath } from 'next/cache';
|
|
4
|
+
import { clearAgainAfterResponse } from '../core/clearAgain.js';
|
|
4
5
|
import { APIError, } from 'payload';
|
|
5
6
|
import { isTailwindUtility } from '../css/index.js';
|
|
6
7
|
import { classesConfigOf } from './config.js';
|
|
@@ -72,6 +73,7 @@ function revalidate(req) {
|
|
|
72
73
|
return;
|
|
73
74
|
try {
|
|
74
75
|
revalidatePath('/', 'layout');
|
|
76
|
+
clearAgainAfterResponse(() => revalidatePath('/', 'layout'));
|
|
75
77
|
}
|
|
76
78
|
catch (error) {
|
|
77
79
|
req.payload.logger.debug(`[websiteBuilder] Classes revalidation skipped: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `clear` again a few seconds after the response. A page render that started before the save
|
|
3
|
+
* stores what it read before the save, and Next keeps it as fresh until the next save. The second
|
|
4
|
+
* run marks that page stale too. Outside a Next request `after` throws; callers catch and log it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function clearAgainAfterResponse(clear: () => void): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { after } from 'next/server';
|
|
2
|
+
/** Long enough for a page render that started before a save to finish. */
|
|
3
|
+
const SETTLE_MS = 5000;
|
|
4
|
+
/**
|
|
5
|
+
* Runs `clear` again a few seconds after the response. A page render that started before the save
|
|
6
|
+
* stores what it read before the save, and Next keeps it as fresh until the next save. The second
|
|
7
|
+
* run marks that page stale too. Outside a Next request `after` throws; callers catch and log it.
|
|
8
|
+
*/
|
|
9
|
+
export function clearAgainAfterResponse(clear) {
|
|
10
|
+
after(async () => {
|
|
11
|
+
await new Promise((resolve) => setTimeout(resolve, SETTLE_MS));
|
|
12
|
+
clear();
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -8,6 +8,7 @@ import { richTextHeadings } from '../../core/headings.js';
|
|
|
8
8
|
import { editableText } from '../render/editable.js';
|
|
9
9
|
import { defaultResolveLink } from '../render/link.js';
|
|
10
10
|
import { PlaceholderText } from './placeholder.js';
|
|
11
|
+
import { uploadConverter } from './richTextUpload.js';
|
|
11
12
|
function isEditorState(value) {
|
|
12
13
|
return typeof value === 'object' && value !== null && typeof value.root === 'object';
|
|
13
14
|
}
|
|
@@ -29,6 +30,9 @@ export function isEmptyRichText(value) {
|
|
|
29
30
|
function converters(resolveLink) {
|
|
30
31
|
return ({ defaultConverters }) => ({
|
|
31
32
|
...defaultConverters,
|
|
33
|
+
// Photos use Payload's resized copies. Payload's default serves the original, or a <picture>
|
|
34
|
+
// that switches to a cropped size on some screen widths.
|
|
35
|
+
upload: uploadConverter(defaultConverters.upload),
|
|
32
36
|
// Internal links use the same resolver as buttons and link blocks.
|
|
33
37
|
...LinkJSXConverter({
|
|
34
38
|
internalDocToHref: ({ linkNode }) => {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/** The class on every rich text photo. Sites style it, or set `--builder-richtext-image-radius`. */
|
|
3
|
+
export declare const RICHTEXT_IMAGE_CLASS = "builder-richtext-image";
|
|
4
|
+
/**
|
|
5
|
+
* `:where()` has no specificity, so any rule of the site wins over these defaults. The radius is 0
|
|
6
|
+
* until a site sets the custom property.
|
|
7
|
+
*/
|
|
8
|
+
export declare const RICHTEXT_IMAGE_CSS = ":where(.builder-richtext-image){max-width:100%;height:auto;border-radius:var(--builder-richtext-image-radius,0)}";
|
|
9
|
+
/**
|
|
10
|
+
* A text column is rarely wider than 768 px. `auto` lets browsers that support it (lazy images
|
|
11
|
+
* only) use the width the photo really has on the page.
|
|
12
|
+
*/
|
|
13
|
+
export declare const RICHTEXT_IMAGE_SIZES = "auto, (min-width: 1024px) 768px, 100vw";
|
|
14
|
+
type Doc = Record<string, unknown>;
|
|
15
|
+
type Candidate = {
|
|
16
|
+
url: string;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Payload's resized copies with the original's shape, smallest first, then the original when it
|
|
22
|
+
* is light. Cropped sizes never join: a srcset must not switch crops between screen widths.
|
|
23
|
+
*/
|
|
24
|
+
export declare function richTextImageCandidates(doc: Doc): Candidate[];
|
|
25
|
+
/**
|
|
26
|
+
* One rich text photo as a plain `<img>`. `src` is the widest file in the srcset, so a left-out
|
|
27
|
+
* original is never downloaded, also by browsers without srcset.
|
|
28
|
+
*/
|
|
29
|
+
export declare function RichTextImage({ doc, alt }: {
|
|
30
|
+
doc: Doc;
|
|
31
|
+
alt: string;
|
|
32
|
+
}): import("react").JSX.Element;
|
|
33
|
+
type UploadArgs = {
|
|
34
|
+
node: unknown;
|
|
35
|
+
};
|
|
36
|
+
type UploadConverter = (args: UploadArgs) => ReactNode;
|
|
37
|
+
/**
|
|
38
|
+
* The rich text converter for upload nodes. Photos render with `RichTextImage`. Other files keep
|
|
39
|
+
* Payload's default output (a link to the file). An upload that is only an id renders nothing: the
|
|
40
|
+
* document was not loaded, and the renderer never fetches it.
|
|
41
|
+
*/
|
|
42
|
+
export declare function uploadConverter(fallback: unknown): UploadConverter;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/** The class on every rich text photo. Sites style it, or set `--builder-richtext-image-radius`. */
|
|
3
|
+
export const RICHTEXT_IMAGE_CLASS = 'builder-richtext-image';
|
|
4
|
+
/**
|
|
5
|
+
* `:where()` has no specificity, so any rule of the site wins over these defaults. The radius is 0
|
|
6
|
+
* until a site sets the custom property.
|
|
7
|
+
*/
|
|
8
|
+
export const RICHTEXT_IMAGE_CSS = `:where(.${RICHTEXT_IMAGE_CLASS}){max-width:100%;height:auto;border-radius:var(--builder-richtext-image-radius,0)}`;
|
|
9
|
+
/**
|
|
10
|
+
* A text column is rarely wider than 768 px. `auto` lets browsers that support it (lazy images
|
|
11
|
+
* only) use the width the photo really has on the page.
|
|
12
|
+
*/
|
|
13
|
+
export const RICHTEXT_IMAGE_SIZES = 'auto, (min-width: 1024px) 768px, 100vw';
|
|
14
|
+
/** A heavy original (a PNG, or over 600 KB) stays out of the srcset when a copy exists. */
|
|
15
|
+
const LIGHT_ORIGINAL_BYTES = 600 * 1024;
|
|
16
|
+
/** At this width a copy is large enough for any text column, so the original adds nothing. */
|
|
17
|
+
const LARGE_COPY_WIDTH = 1920;
|
|
18
|
+
const asString = (value) => (typeof value === 'string' && value ? value : undefined);
|
|
19
|
+
const asNumber = (value) => typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : undefined;
|
|
20
|
+
function isDoc(value) {
|
|
21
|
+
return typeof value === 'object' && value !== null;
|
|
22
|
+
}
|
|
23
|
+
/** True when two boxes have the same shape. A size with its own crop (a 16:9 `hero`) does not. */
|
|
24
|
+
function sameShape(a, b) {
|
|
25
|
+
return Math.abs(b.height / b.width - a.height / a.width) < 0.01;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Payload can reuse a file name after an edit. `?v=<id>-<updatedAt>` gives each version of the
|
|
29
|
+
* photo its own URL, so no browser or CDN cache shows the old photo.
|
|
30
|
+
*/
|
|
31
|
+
function versioned(doc, url) {
|
|
32
|
+
const id = doc.id;
|
|
33
|
+
const updatedAt = asString(doc.updatedAt);
|
|
34
|
+
if ((typeof id !== 'string' && typeof id !== 'number') || id === '' || !updatedAt)
|
|
35
|
+
return url;
|
|
36
|
+
return `${url}${url.includes('?') ? '&' : '?'}v=${encodeURIComponent(`${id}-${updatedAt}`)}`;
|
|
37
|
+
}
|
|
38
|
+
/** A srcset entry ends at a space or a comma, so a URL must not contain either. */
|
|
39
|
+
const srcSetURL = (url) => url.replaceAll(' ', '%20').replaceAll(',', '%2C');
|
|
40
|
+
/**
|
|
41
|
+
* The original joins the srcset only when it is light and no copy is large enough already. A PNG
|
|
42
|
+
* or a camera file can be several MB, and a retina screen would pick it for a wide column.
|
|
43
|
+
*/
|
|
44
|
+
function originalFits(doc, largestCopyWidth) {
|
|
45
|
+
if (!largestCopyWidth)
|
|
46
|
+
return true;
|
|
47
|
+
if (doc.mimeType === 'image/png')
|
|
48
|
+
return false;
|
|
49
|
+
if ((asNumber(doc.filesize) ?? 0) > LIGHT_ORIGINAL_BYTES)
|
|
50
|
+
return false;
|
|
51
|
+
return largestCopyWidth < LARGE_COPY_WIDTH;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Payload's resized copies with the original's shape, smallest first, then the original when it
|
|
55
|
+
* is light. Cropped sizes never join: a srcset must not switch crops between screen widths.
|
|
56
|
+
*/
|
|
57
|
+
export function richTextImageCandidates(doc) {
|
|
58
|
+
const url = asString(doc.url);
|
|
59
|
+
const width = asNumber(doc.width);
|
|
60
|
+
const height = asNumber(doc.height);
|
|
61
|
+
if (!url || !width || !height)
|
|
62
|
+
return [];
|
|
63
|
+
const original = { url, width, height };
|
|
64
|
+
const byWidth = new Map();
|
|
65
|
+
for (const size of Object.values(isDoc(doc.sizes) ? doc.sizes : {})) {
|
|
66
|
+
if (!isDoc(size))
|
|
67
|
+
continue;
|
|
68
|
+
const copy = { url: asString(size.url) ?? '', width: asNumber(size.width) ?? 0, height: asNumber(size.height) ?? 0 };
|
|
69
|
+
if (!copy.url || !copy.width || !copy.height)
|
|
70
|
+
continue;
|
|
71
|
+
// A copy as wide as the original adds nothing (Payload keeps a copy at full width when it
|
|
72
|
+
// cannot make it smaller).
|
|
73
|
+
if (copy.width >= width || !sameShape(original, copy))
|
|
74
|
+
continue;
|
|
75
|
+
byWidth.set(copy.width, { ...copy, url: versioned(doc, copy.url) });
|
|
76
|
+
}
|
|
77
|
+
if (originalFits(doc, Math.max(0, ...byWidth.keys())))
|
|
78
|
+
byWidth.set(width, { ...original, url: versioned(doc, url) });
|
|
79
|
+
return [...byWidth.values()].toSorted((a, b) => a.width - b.width);
|
|
80
|
+
}
|
|
81
|
+
/** True for an image upload. A doc loaded without `mimeType` counts when it has a size. */
|
|
82
|
+
function isImage(doc) {
|
|
83
|
+
const mimeType = asString(doc.mimeType);
|
|
84
|
+
if (mimeType)
|
|
85
|
+
return mimeType.startsWith('image/');
|
|
86
|
+
return asNumber(doc.width) !== undefined && asNumber(doc.height) !== undefined;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* One rich text photo as a plain `<img>`. `src` is the widest file in the srcset, so a left-out
|
|
90
|
+
* original is never downloaded, also by browsers without srcset.
|
|
91
|
+
*/
|
|
92
|
+
export function RichTextImage({ doc, alt }) {
|
|
93
|
+
const list = richTextImageCandidates(doc);
|
|
94
|
+
const widest = list.at(-1);
|
|
95
|
+
const src = widest?.url ?? versioned(doc, asString(doc.url) ?? '');
|
|
96
|
+
const srcSet = list.length > 1 ? list.map((c) => `${srcSetURL(c.url)} ${c.width}w`).join(', ') : undefined;
|
|
97
|
+
return (_jsxs(_Fragment, { children: [_jsx("style", { href: RICHTEXT_IMAGE_CLASS, precedence: "default", children: RICHTEXT_IMAGE_CSS }), _jsx("img", { alt: alt, className: RICHTEXT_IMAGE_CLASS, decoding: "async", height: asNumber(doc.height), loading: "lazy", sizes: srcSet ? RICHTEXT_IMAGE_SIZES : undefined, src: src, srcSet: srcSet, width: asNumber(doc.width) })] }));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The rich text converter for upload nodes. Photos render with `RichTextImage`. Other files keep
|
|
101
|
+
* Payload's default output (a link to the file). An upload that is only an id renders nothing: the
|
|
102
|
+
* document was not loaded, and the renderer never fetches it.
|
|
103
|
+
*/
|
|
104
|
+
export function uploadConverter(fallback) {
|
|
105
|
+
return function renderUpload(args) {
|
|
106
|
+
const node = args.node;
|
|
107
|
+
const doc = node.value;
|
|
108
|
+
if (!isDoc(doc) || !asString(doc.url))
|
|
109
|
+
return null;
|
|
110
|
+
if (!isImage(doc))
|
|
111
|
+
return typeof fallback === 'function' && asString(doc.mimeType) ? fallback(args) : null;
|
|
112
|
+
const alt = asString(node.fields?.alt) ?? asString(doc.alt) ?? '';
|
|
113
|
+
return _jsx(RichTextImage, { doc: doc, alt: alt });
|
|
114
|
+
};
|
|
115
|
+
}
|
package/dist/theme/global.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The theme global: colors, fonts, radius and spacing, with the plugin's color, font and slider
|
|
2
2
|
// fields. Server-only (the revalidation hook imports next/cache).
|
|
3
3
|
import { revalidatePath, revalidateTag } from 'next/cache';
|
|
4
|
+
import { clearAgainAfterResponse } from '../core/clearAgain.js';
|
|
4
5
|
import { deriveColors } from './colors.js';
|
|
5
6
|
import { DEFAULT_THEME_SLUG } from './config.js';
|
|
6
7
|
import { forgetThemeVariables } from './server.js';
|
|
@@ -177,8 +178,12 @@ function revalidateTheme(cacheTag) {
|
|
|
177
178
|
if (context?.disableRevalidate)
|
|
178
179
|
return doc;
|
|
179
180
|
try {
|
|
180
|
-
|
|
181
|
-
|
|
181
|
+
const clear = () => {
|
|
182
|
+
revalidateTag(cacheTag, { expire: 0 });
|
|
183
|
+
revalidatePath('/', 'layout');
|
|
184
|
+
};
|
|
185
|
+
clear();
|
|
186
|
+
clearAgainAfterResponse(clear);
|
|
182
187
|
}
|
|
183
188
|
catch (error) {
|
|
184
189
|
payload.logger.debug(`[websiteBuilder] Theme revalidation skipped: ${error instanceof Error ? error.message : String(error)}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload-canvas",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.7",
|
|
4
4
|
"description": "Payload Canvas: a visual drag-and-drop website builder for Payload CMS 3. Nested layout blocks, Tailwind styling, sections, templates, live AI editing over MCP, and the React renderer for your site.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|