@quitecustard/payload 0.2.0 → 0.2.2
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
CHANGED
|
@@ -34,6 +34,8 @@ shows on hover — this page is the map, not the reference.
|
|
|
34
34
|
- `published-where` — the published clause, stated once so a bypassing read and the access rule
|
|
35
35
|
cannot drift apart
|
|
36
36
|
- `collection-versions` — `draftVersions(maxPerDoc?)`: the autosave + schedulePublish shape
|
|
37
|
+
- `lexical-plain-text` — `lexicalPlainText` / `lexicalNodeText`: a rich text value as plain text,
|
|
38
|
+
one block per line, for search indexes and excerpts
|
|
37
39
|
|
|
38
40
|
### `access/`
|
|
39
41
|
|
|
@@ -89,6 +91,9 @@ Postgres only, and the only folder needing the `@payloadcms/drizzle` + `drizzle-
|
|
|
89
91
|
- `after-response` — defer non-critical work past the response, falling back to inline outside a request
|
|
90
92
|
- `run-queued-jobs` — drains the Payload job queue, which nothing else does
|
|
91
93
|
- `unique-slug`, `unique-username` — collision-free values, each candidate checked exactly
|
|
94
|
+
- `accent-colour-hook` — `accentColourHook({field, source, extract})`: an afterChange hook that
|
|
95
|
+
keeps a colour field in step with the document's picture, deferred past the response. The decode
|
|
96
|
+
is the caller's `extract`.
|
|
92
97
|
|
|
93
98
|
`src/internal/` holds implementation detail and is not importable: `"./internal/*": null` sits ahead
|
|
94
99
|
of the wildcard in `exports`.
|
|
@@ -163,7 +168,8 @@ replacing it, so registering both on one table is safe.
|
|
|
163
168
|
directly. What they can't run on is SQLite or Mongo: the raw SQL uses `pg_constraint`,
|
|
164
169
|
`timestamptz` and data-modifying CTEs. No module outside `db/` imports drizzle at all, so the
|
|
165
170
|
other five folders work on any adapter.
|
|
166
|
-
- `fields/seo-tab` assumes `@payloadcms/plugin-seo`, and `operations/after-response` assumes `next
|
|
167
|
-
Those
|
|
171
|
+
- `fields/seo-tab` assumes `@payloadcms/plugin-seo`, and `operations/after-response` assumes `next`,
|
|
172
|
+
as does `operations/accent-colour-hook`, which defers through it. Those are the only modules
|
|
173
|
+
touching either peer.
|
|
168
174
|
|
|
169
175
|
MIT licensed. Source: [custard-kit](https://github.com/QuiteCustard/custard-kit).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plain text of one serialised Lexical node and everything beneath it.
|
|
3
|
+
*
|
|
4
|
+
* Blocks come out one per line, a table row as its cells joined by `" | "`, a list item with a
|
|
5
|
+
* leading `"- "`, and a line break as `"\n"`. Formatting, link targets and block or upload nodes
|
|
6
|
+
* (which carry no `children`) contribute nothing. Reads the node structurally, so it takes whatever
|
|
7
|
+
* a rich text field hands back and returns `''` for anything that is not a node.
|
|
8
|
+
*/
|
|
9
|
+
export declare const lexicalNodeText: (node: unknown) => string;
|
|
10
|
+
/**
|
|
11
|
+
* The plain text of a rich text field's value (the editor state, `{ root }`), for search indexes,
|
|
12
|
+
* excerpts and anywhere else markup cannot go. An empty or missing value reads as `''`.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* lexicalPlainText(post.content) // "First paragraph\n- a list item\nName | Value"
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* See `lexicalNodeText` for how each node type is written out.
|
|
19
|
+
*/
|
|
20
|
+
export declare const lexicalPlainText: (value: unknown) => string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isRecord } from '@quitecustard/core/utils/is-record';
|
|
2
|
+
const childrenOf = (node) => Array.isArray(node.children) ? node.children : [];
|
|
3
|
+
// Containers whose children are blocks of their own. Every other node (paragraph, heading, link,
|
|
4
|
+
// table cell) holds inline content and reads as one run.
|
|
5
|
+
const blockContainers = new Set(['root', 'list', 'table']);
|
|
6
|
+
const joinFilled = (parts, separator) => parts
|
|
7
|
+
.map((part) => part.trim())
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
.join(separator);
|
|
10
|
+
/**
|
|
11
|
+
* The plain text of one serialised Lexical node and everything beneath it.
|
|
12
|
+
*
|
|
13
|
+
* Blocks come out one per line, a table row as its cells joined by `" | "`, a list item with a
|
|
14
|
+
* leading `"- "`, and a line break as `"\n"`. Formatting, link targets and block or upload nodes
|
|
15
|
+
* (which carry no `children`) contribute nothing. Reads the node structurally, so it takes whatever
|
|
16
|
+
* a rich text field hands back and returns `''` for anything that is not a node.
|
|
17
|
+
*/
|
|
18
|
+
export const lexicalNodeText = (node) => {
|
|
19
|
+
if (!isRecord(node))
|
|
20
|
+
return '';
|
|
21
|
+
if (node.type === 'linebreak')
|
|
22
|
+
return '\n';
|
|
23
|
+
if (typeof node.text === 'string')
|
|
24
|
+
return node.text;
|
|
25
|
+
const parts = childrenOf(node).map(lexicalNodeText);
|
|
26
|
+
if (node.type === 'tablerow')
|
|
27
|
+
return joinFilled(parts, ' | ');
|
|
28
|
+
if (node.type === 'listitem')
|
|
29
|
+
return `- ${parts.join('').trim()}`;
|
|
30
|
+
if (typeof node.type === 'string' && blockContainers.has(node.type))
|
|
31
|
+
return joinFilled(parts, '\n');
|
|
32
|
+
return parts.join('');
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The plain text of a rich text field's value (the editor state, `{ root }`), for search indexes,
|
|
36
|
+
* excerpts and anywhere else markup cannot go. An empty or missing value reads as `''`.
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* lexicalPlainText(post.content) // "First paragraph\n- a list item\nName | Value"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* See `lexicalNodeText` for how each node type is written out.
|
|
43
|
+
*/
|
|
44
|
+
export const lexicalPlainText = (value) => isRecord(value) ? lexicalNodeText(value.root) : '';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CollectionAfterChangeHook } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Keeps a colour field on a document in step with the document's picture - a crest, a hero image, a
|
|
4
|
+
* banner - by reading the image once after the save and writing the colour back.
|
|
5
|
+
*
|
|
6
|
+
* Two things are deliberately NOT in here, because they are what made the original unportable:
|
|
7
|
+
*
|
|
8
|
+
* - **Decoding.** The caller passes `extract`, a function from a media URL to a colour string. The
|
|
9
|
+
* decode wants `sharp` or a canvas and usually a cache around it, and none of that belongs in a
|
|
10
|
+
* helpers package. `@quitecustard/core/media/accent-colour` holds the pixel maths if you want it.
|
|
11
|
+
* - **Which field holds the picture.** `source` answers that per collection, because a team reads
|
|
12
|
+
* its logo while a page reads a hero block's image or a video's poster.
|
|
13
|
+
*
|
|
14
|
+
* The work is deferred with {@link runAfterResponse}: fetching and decoding an image is a network
|
|
15
|
+
* round-trip plus a CPU-bound decode, and an editor's save should not wait for it.
|
|
16
|
+
*/
|
|
17
|
+
export type AccentColourSource = {
|
|
18
|
+
/** Media collection the image ref points into. */
|
|
19
|
+
collection: string;
|
|
20
|
+
/** The image on the saved doc: an id, a populated doc, or null. */
|
|
21
|
+
ref: unknown;
|
|
22
|
+
/** The same image on the previous doc, used to skip a save that did not change the picture. */
|
|
23
|
+
previousRef?: unknown;
|
|
24
|
+
};
|
|
25
|
+
export type AccentColourHookOptions = {
|
|
26
|
+
/** Document field the colour is written to. */
|
|
27
|
+
field: string;
|
|
28
|
+
/** Picks the picture to read, or null when this document has none. */
|
|
29
|
+
source: (args: {
|
|
30
|
+
doc: Record<string, unknown>;
|
|
31
|
+
previousDoc?: Record<string, unknown>;
|
|
32
|
+
collection: string;
|
|
33
|
+
}) => AccentColourSource | null;
|
|
34
|
+
/** The app's decode: a media URL in, a colour string out, null when it cannot be read. */
|
|
35
|
+
extract: (url: string) => Promise<string | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Context flag that suppresses the hook. The write-back sets it so the hook does not re-enter on
|
|
38
|
+
* its own update, and callers can set it to silence the hook on a bulk import.
|
|
39
|
+
*/
|
|
40
|
+
suppressContextKey?: string;
|
|
41
|
+
};
|
|
42
|
+
export declare const accentColourHook: (options: AccentColourHookOptions) => CollectionAfterChangeHook;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { relId } from '../documents/resolve-ref.js';
|
|
2
|
+
import { runAfterResponse } from './after-response.js';
|
|
3
|
+
/** A url already on a populated relationship saves a lookup in the deferred work. */
|
|
4
|
+
const inlineUrl = (ref) => {
|
|
5
|
+
if (!ref || typeof ref !== 'object')
|
|
6
|
+
return null;
|
|
7
|
+
const url = ref.url;
|
|
8
|
+
return typeof url === 'string' && url.length > 0 ? url : null;
|
|
9
|
+
};
|
|
10
|
+
export const accentColourHook = (options) => {
|
|
11
|
+
const { field, source, extract, suppressContextKey = 'disableHook' } = options;
|
|
12
|
+
return async ({ doc, previousDoc, collection, req }) => {
|
|
13
|
+
const { payload, context } = req;
|
|
14
|
+
if (context[suppressContextKey])
|
|
15
|
+
return doc;
|
|
16
|
+
const picked = source({ doc, previousDoc, collection: collection.slug });
|
|
17
|
+
if (!picked)
|
|
18
|
+
return doc;
|
|
19
|
+
const imageId = relId(picked.ref);
|
|
20
|
+
const previousImageId = relId(picked.previousRef);
|
|
21
|
+
// Nothing to do without a picture, and nothing to do when the picture is unchanged and a
|
|
22
|
+
// colour is already stored. Both checks are cheap and stay on the save's critical path.
|
|
23
|
+
if (imageId === null)
|
|
24
|
+
return doc;
|
|
25
|
+
if (imageId === previousImageId && doc[field])
|
|
26
|
+
return doc;
|
|
27
|
+
const tag = '[' + field + '] ' + collection.slug + '#' + String(doc.id);
|
|
28
|
+
await deferredWrite({
|
|
29
|
+
payload,
|
|
30
|
+
picked,
|
|
31
|
+
imageId,
|
|
32
|
+
tag,
|
|
33
|
+
field,
|
|
34
|
+
extract,
|
|
35
|
+
doc,
|
|
36
|
+
collection,
|
|
37
|
+
suppressContextKey,
|
|
38
|
+
});
|
|
39
|
+
return doc;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
/** The post-commit half: resolve a url, extract a colour, write it back. */
|
|
43
|
+
const deferredWrite = async (args) => {
|
|
44
|
+
const { payload, picked, imageId, tag, field, extract, doc, collection, suppressContextKey } = args;
|
|
45
|
+
const known = inlineUrl(picked.ref);
|
|
46
|
+
const collectionSlug = collection.slug;
|
|
47
|
+
const docId = doc.id;
|
|
48
|
+
// Runs post-commit in its own transaction, so it must not reuse `req`, and it reads the
|
|
49
|
+
// just-committed document.
|
|
50
|
+
await runAfterResponse(payload, async () => {
|
|
51
|
+
let url = known;
|
|
52
|
+
if (!url) {
|
|
53
|
+
const image = await payload.findByID({
|
|
54
|
+
collection: picked.collection,
|
|
55
|
+
id: imageId,
|
|
56
|
+
depth: 0,
|
|
57
|
+
});
|
|
58
|
+
const resolved = image?.url;
|
|
59
|
+
url = typeof resolved === 'string' ? resolved : null;
|
|
60
|
+
}
|
|
61
|
+
if (!url) {
|
|
62
|
+
payload.logger.warn(tag + ' no url for ' + picked.collection + '#' + String(imageId));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const colour = await extract(url);
|
|
66
|
+
if (!colour) {
|
|
67
|
+
payload.logger.warn(tag + ' nothing extracted from ' + url);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
await payload.update({
|
|
71
|
+
collection: collectionSlug,
|
|
72
|
+
id: docId,
|
|
73
|
+
data: { [field]: colour },
|
|
74
|
+
// Suppressed so this write does not re-enter the hook. Revalidation is left ENABLED on
|
|
75
|
+
// purpose: the triggering save revalidated before this colour existed, so this write has to
|
|
76
|
+
// purge again for it to show.
|
|
77
|
+
context: { [suppressContextKey]: true },
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quitecustard/payload",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Payload v3 helpers: relation narrowing, transactions, autosave-vs-unpublish detection, sync locks, unique slugs and usernames",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@quitecustard/core": "^0.2.
|
|
41
|
+
"@quitecustard/core": "^0.2.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@payloadcms/drizzle": "^3.0.0",
|