@quitecustard/payload 0.2.0 → 0.2.1
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,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.1",
|
|
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",
|