payload-canvas 0.6.6 → 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.
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payload-canvas",
3
- "version": "0.6.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",