@10x-media/fields 0.1.0-beta.0 → 0.1.0-beta.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/CHANGELOG.md +68 -0
- package/README.md +1 -1
- package/dist/exports/color-utils.d.ts +3 -2
- package/dist/exports/color-utils.js +2 -1
- package/dist/exports/color.d.ts +4 -2
- package/dist/exports/color.js +3 -1
- package/dist/exports/icon-adapters/radix.d.ts +2 -2
- package/dist/exports/icon-adapters/radix.js +2 -2
- package/dist/exports/icon-react.d.ts +2 -2
- package/dist/exports/icon.d.ts +6 -2
- package/dist/exports/icon.js +6 -2
- package/dist/fields/color/client/ColorField.js +8 -3
- package/dist/fields/color/client/ColorField.js.map +1 -1
- package/dist/fields/color/client/ColorPickerPanel.js +2 -1
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -1
- package/dist/fields/color/colorField.d.ts +9 -2
- package/dist/fields/color/colorField.js +34 -9
- package/dist/fields/color/colorField.js.map +1 -1
- package/dist/fields/color/engine/index.d.ts +2 -1
- package/dist/fields/color/engine/scheme.d.ts +13 -0
- package/dist/fields/color/engine/scheme.js +12 -0
- package/dist/fields/color/engine/scheme.js.map +1 -0
- package/dist/fields/color/options.d.ts +5 -4
- package/dist/fields/color/options.js.map +1 -1
- package/dist/fields/color/paths.d.ts +1 -0
- package/dist/fields/color/paths.js +32 -0
- package/dist/fields/color/paths.js.map +1 -0
- package/dist/fields/color/presetChip.js.map +1 -1
- package/dist/fields/color/presetsFromArray.d.ts +30 -0
- package/dist/fields/color/presetsFromArray.js +58 -0
- package/dist/fields/color/presetsFromArray.js.map +1 -0
- package/dist/fields/color/presetsFromDoc.js +1 -23
- package/dist/fields/color/presetsFromDoc.js.map +1 -1
- package/dist/fields/color/resolvePresets.d.ts +2 -2
- package/dist/fields/color/resolvePresets.js +38 -8
- package/dist/fields/color/resolvePresets.js.map +1 -1
- package/dist/fields/color/schemeValue.js +30 -0
- package/dist/fields/color/schemeValue.js.map +1 -0
- package/dist/fields/color/server/ColorCell.js +2 -2
- package/dist/fields/color/server/ColorCell.js.map +1 -1
- package/dist/fields/encrypted/queryRewrite.d.ts +1 -1
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -1
- package/dist/fields/icon/adapters/radix/adapter.js +7 -0
- package/dist/fields/icon/adapters/radix/adapter.js.map +1 -1
- package/dist/fields/icon/adapters/radix/client.d.ts +3 -2
- package/dist/fields/icon/adapters/radix/client.js +3 -2
- package/dist/fields/icon/adapters/radix/client.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/radix/generated/nodes.js +3625 -0
- package/dist/fields/icon/adapters/radix/generated/nodes.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -1
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -1
- package/dist/fields/icon/client/DrawerGlyph.js +34 -17
- package/dist/fields/icon/client/DrawerGlyph.js.map +1 -1
- package/dist/fields/icon/client/IconDrawerContent.js +9 -45
- package/dist/fields/icon/client/IconDrawerContent.js.map +1 -1
- package/dist/fields/icon/client/IconFieldClient.d.ts +7 -0
- package/dist/fields/icon/client/IconFieldClient.js +15 -7
- package/dist/fields/icon/client/IconFieldClient.js.map +1 -1
- package/dist/fields/icon/client/IconGridCell.js +67 -0
- package/dist/fields/icon/client/IconGridCell.js.map +1 -0
- package/dist/fields/icon/client/icon-field.css +8 -0
- package/dist/fields/icon/codegen/emit.js +5 -4
- package/dist/fields/icon/codegen/emit.js.map +1 -1
- package/dist/fields/icon/codegen/generate.js +32 -6
- package/dist/fields/icon/codegen/generate.js.map +1 -1
- package/dist/fields/icon/layers/coalesce.js +41 -0
- package/dist/fields/icon/layers/coalesce.js.map +1 -0
- package/dist/fields/icon/layers/manifestCache.d.ts +6 -0
- package/dist/fields/icon/layers/manifestCache.js +49 -0
- package/dist/fields/icon/layers/manifestCache.js.map +1 -0
- package/dist/fields/icon/layers/resolve.d.ts +20 -0
- package/dist/fields/icon/layers/resolve.js +63 -0
- package/dist/fields/icon/layers/resolve.js.map +1 -0
- package/dist/fields/icon/layers/upload.d.ts +52 -0
- package/dist/fields/icon/layers/upload.js +116 -0
- package/dist/fields/icon/layers/upload.js.map +1 -0
- package/dist/fields/icon/plugin.js +24 -0
- package/dist/fields/icon/plugin.js.map +1 -1
- package/dist/fields/icon/react/createIcon.d.ts +5 -0
- package/dist/fields/icon/react/createIcon.js +22 -4
- package/dist/fields/icon/react/createIcon.js.map +1 -1
- package/dist/fields/icon/react/createRscIcon.js +22 -1
- package/dist/fields/icon/react/createRscIcon.js.map +1 -1
- package/dist/fields/icon/react/resolveRendered.js +21 -0
- package/dist/fields/icon/react/resolveRendered.js.map +1 -0
- package/dist/fields/icon/react/types.d.ts +15 -1
- package/dist/fields/icon/rsc/IconCell.d.ts +2 -2
- package/dist/fields/icon/rsc/IconCell.js +12 -5
- package/dist/fields/icon/rsc/IconCell.js.map +1 -1
- package/dist/fields/icon/rsc/IconFieldServer.js +16 -2
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +1 -1
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -1
- package/dist/fields/icon/server/endpoint.d.ts +12 -0
- package/dist/fields/icon/server/endpoint.js +52 -0
- package/dist/fields/icon/server/endpoint.js.map +1 -0
- package/dist/fields/icon/server/manifestCache.js +9 -4
- package/dist/fields/icon/server/manifestCache.js.map +1 -1
- package/dist/fields/icon/server/resolveIconMeta.js +31 -0
- package/dist/fields/icon/server/resolveIconMeta.js.map +1 -0
- package/dist/fields/icon/server/validate.js +5 -2
- package/dist/fields/icon/server/validate.js.map +1 -1
- package/dist/fields/icon/shared/adapterComponents.d.ts +3 -2
- package/dist/fields/icon/shared/iconLabel.js +26 -0
- package/dist/fields/icon/shared/iconLabel.js.map +1 -0
- package/dist/fields/icon/{rsc → shared}/resolveStaticLabel.js +1 -1
- package/dist/fields/icon/shared/resolveStaticLabel.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +125 -8
- package/package.json +9 -7
- package/dist/fields/icon/rsc/resolveStaticLabel.js.map +0 -1
|
@@ -5,8 +5,32 @@ import { loadTablerSource } from "./sources/tabler.js";
|
|
|
5
5
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
//#region src/fields/icon/codegen/generate.ts
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* What actually constrains an icon name, which is far narrower than kebab-case:
|
|
10
|
+
*
|
|
11
|
+
* - `:` separates library from name in a stored value, so a name holding one reparses
|
|
12
|
+
* as a different library entirely
|
|
13
|
+
* - whitespace and control characters break search normalisation, which folds runs of
|
|
14
|
+
* whitespace and dashes into one separator
|
|
15
|
+
* - `'`, `\` and line terminators would escape the single-quoted key in `imports.ts`
|
|
16
|
+
*
|
|
17
|
+
* Everything else is legitimate: uppercase, digits, dots, underscores, non-latin
|
|
18
|
+
* scripts. Requiring kebab-case rejected libraries keyed by code (`HUN`, `SUI`) and
|
|
19
|
+
* forced them to hand-write an emitter, which is the same wrong assumption as deriving
|
|
20
|
+
* a display label from the name.
|
|
21
|
+
*/
|
|
22
|
+
const UNSAFE_ICON_NAME = /[\s:'\\]/;
|
|
23
|
+
/**
|
|
24
|
+
* Control characters that `\s` does not already cover. They would survive into the
|
|
25
|
+
* emitted module and the DOM, where they are invisible and unsearchable.
|
|
26
|
+
*/
|
|
27
|
+
const hasControlCharacter = (value) => {
|
|
28
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
29
|
+
const code = value.charCodeAt(index);
|
|
30
|
+
if (code < 32 || code >= 127 && code <= 159) return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
};
|
|
10
34
|
/** Codepoint order, so committed output does not depend on the generating machine's locale. */
|
|
11
35
|
const byCodepoint = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
12
36
|
const loadSource = async (options) => {
|
|
@@ -22,11 +46,13 @@ const generateIconManifest = async (options) => {
|
|
|
22
46
|
...icon,
|
|
23
47
|
categories: [...icon.categories].sort(byCodepoint)
|
|
24
48
|
})).sort((a, b) => byCodepoint(a.name, b.name));
|
|
25
|
-
const seen = /* @__PURE__ */ new
|
|
49
|
+
const seen = /* @__PURE__ */ new Map();
|
|
26
50
|
for (const icon of icons) {
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
seen.
|
|
51
|
+
if (icon.name === "" || UNSAFE_ICON_NAME.test(icon.name) || hasControlCharacter(icon.name)) throw new Error(`invalid icon name: "${icon.name}" (must be non-empty and free of whitespace, ":", "'" and "\\")`);
|
|
52
|
+
const folded = icon.name.toLowerCase();
|
|
53
|
+
const clash = seen.get(folded);
|
|
54
|
+
if (clash !== void 0) throw new Error(clash === icon.name ? `duplicate icon name: ${icon.name}` : `icon names differ only by case: "${clash}" and "${icon.name}"`);
|
|
55
|
+
seen.set(folded, icon.name);
|
|
30
56
|
}
|
|
31
57
|
const categories = [...new Set(icons.flatMap((icon) => icon.categories))].sort(byCodepoint);
|
|
32
58
|
const header = generatedHeader(options.regenCommand);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.js","names":[],"sources":["../../../../src/fields/icon/codegen/generate.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises'\nimport path from 'node:path'\nimport { emitImportsModule, emitManifestModule, emitNodesModule, generatedHeader } from './emit'\nimport { loadLucideSource } from './sources/lucide'\nimport { loadRadixSource } from './sources/radix'\nimport { loadTablerSource } from './sources/tabler'\nimport type { GenerateIconManifestOptions, LoadedIconSource } from './types'\n\n
|
|
1
|
+
{"version":3,"file":"generate.js","names":[],"sources":["../../../../src/fields/icon/codegen/generate.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises'\nimport path from 'node:path'\nimport { emitImportsModule, emitManifestModule, emitNodesModule, generatedHeader } from './emit'\nimport { loadLucideSource } from './sources/lucide'\nimport { loadRadixSource } from './sources/radix'\nimport { loadTablerSource } from './sources/tabler'\nimport type { GenerateIconManifestOptions, LoadedIconSource } from './types'\n\n/**\n * What actually constrains an icon name, which is far narrower than kebab-case:\n *\n * - `:` separates library from name in a stored value, so a name holding one reparses\n * as a different library entirely\n * - whitespace and control characters break search normalisation, which folds runs of\n * whitespace and dashes into one separator\n * - `'`, `\\` and line terminators would escape the single-quoted key in `imports.ts`\n *\n * Everything else is legitimate: uppercase, digits, dots, underscores, non-latin\n * scripts. Requiring kebab-case rejected libraries keyed by code (`HUN`, `SUI`) and\n * forced them to hand-write an emitter, which is the same wrong assumption as deriving\n * a display label from the name.\n */\nconst UNSAFE_ICON_NAME = /[\\s:'\\\\]/\n\n/**\n * Control characters that `\\s` does not already cover. They would survive into the\n * emitted module and the DOM, where they are invisible and unsearchable.\n */\nconst hasControlCharacter = (value: string): boolean => {\n\tfor (let index = 0; index < value.length; index += 1) {\n\t\tconst code = value.charCodeAt(index)\n\t\tif (code < 0x20 || (code >= 0x7f && code <= 0x9f)) return true\n\t}\n\treturn false\n}\n\n/** Codepoint order, so committed output does not depend on the generating machine's locale. */\nconst byCodepoint = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0)\n\nconst loadSource = async (options: GenerateIconManifestOptions): Promise<LoadedIconSource> => {\n\tconst { source } = options\n\tif (source === 'lucide') {\n\t\treturn loadLucideSource({ allowMissingCategories: options.allowMissingCategories })\n\t}\n\tif (source === 'radix') return loadRadixSource()\n\tif (source === 'tabler') return loadTablerSource()\n\treturn source\n}\n\nexport const generateIconManifest = async (\n\toptions: GenerateIconManifestOptions\n): Promise<{ iconCount: number; files: string[] }> => {\n\tconst { icons: rawIcons, importFor, nodes } = await loadSource(options)\n\tconst icons = rawIcons\n\t\t.map((icon) => ({ ...icon, categories: [...icon.categories].sort(byCodepoint) }))\n\t\t.sort((a, b) => byCodepoint(a.name, b.name))\n\tconst seen = new Map<string, string>()\n\tfor (const icon of icons) {\n\t\tif (icon.name === '' || UNSAFE_ICON_NAME.test(icon.name) || hasControlCharacter(icon.name)) {\n\t\t\tthrow new Error(\n\t\t\t\t`invalid icon name: \"${icon.name}\" (must be non-empty and free of whitespace, \":\", \"'\" and \"\\\\\")`\n\t\t\t)\n\t\t}\n\t\tconst folded = icon.name.toLowerCase()\n\t\tconst clash = seen.get(folded)\n\t\tif (clash !== undefined) {\n\t\t\t// Drawer search normalises case, so two names differing only by case are\n\t\t\t// indistinguishable to an editor even though they are distinct keys elsewhere.\n\t\t\tthrow new Error(\n\t\t\t\tclash === icon.name\n\t\t\t\t\t? `duplicate icon name: ${icon.name}`\n\t\t\t\t\t: `icon names differ only by case: \"${clash}\" and \"${icon.name}\"`\n\t\t\t)\n\t\t}\n\t\tseen.set(folded, icon.name)\n\t}\n\tconst categories = [...new Set(icons.flatMap((icon) => icon.categories))].sort(byCodepoint)\n\t// Emit every module before writing any, so a specifier or node the emitter\n\t// rejects cannot leave the directory holding a new manifest beside a stale\n\t// imports map or node-data.\n\tconst header = generatedHeader(options.regenCommand)\n\tconst manifestSource = emitManifestModule(icons, categories, header)\n\tconst importsSource = importFor ? emitImportsModule(icons, importFor, header) : undefined\n\tconst nodesSource = nodes ? emitNodesModule(icons, nodes, header) : undefined\n\tawait mkdir(options.outDir, { recursive: true })\n\tconst files: string[] = []\n\tconst manifestPath = path.join(options.outDir, 'manifest.ts')\n\tawait writeFile(manifestPath, manifestSource)\n\tfiles.push(manifestPath)\n\tif (importsSource !== undefined) {\n\t\tconst importsPath = path.join(options.outDir, 'imports.ts')\n\t\tawait writeFile(importsPath, importsSource)\n\t\tfiles.push(importsPath)\n\t}\n\tif (nodesSource !== undefined) {\n\t\tconst nodesPath = path.join(options.outDir, 'nodes.ts')\n\t\tawait writeFile(nodesPath, nodesSource)\n\t\tfiles.push(nodesPath)\n\t}\n\treturn { files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,KAAK,KAAK,EACjE;EAED,KAAK,IAAI,QAAQ,KAAK,IAAI;CAC3B;CACA,MAAM,aAAa,CAAC,GAAG,IAAI,IAAI,MAAM,SAAS,SAAS,KAAK,UAAU,CAAC,CAAC,EAAE,KAAK,WAAW;CAI1F,MAAM,SAAS,gBAAgB,QAAQ,YAAY;CACnD,MAAM,iBAAiB,mBAAmB,OAAO,YAAY,MAAM;CACnE,MAAM,gBAAgB,YAAY,kBAAkB,OAAO,WAAW,MAAM,IAAI,KAAA;CAChF,MAAM,cAAc,QAAQ,gBAAgB,OAAO,OAAO,MAAM,IAAI,KAAA;CACpE,MAAM,MAAM,QAAQ,QAAQ,EAAE,WAAW,KAAK,CAAC;CAC/C,MAAM,QAAkB,CAAC;CACzB,MAAM,eAAe,KAAK,KAAK,QAAQ,QAAQ,aAAa;CAC5D,MAAM,UAAU,cAAc,cAAc;CAC5C,MAAM,KAAK,YAAY;CACvB,IAAI,kBAAkB,KAAA,GAAW;EAChC,MAAM,cAAc,KAAK,KAAK,QAAQ,QAAQ,YAAY;EAC1D,MAAM,UAAU,aAAa,aAAa;EAC1C,MAAM,KAAK,WAAW;CACvB;CACA,IAAI,gBAAgB,KAAA,GAAW;EAC9B,MAAM,YAAY,KAAK,KAAK,QAAQ,QAAQ,UAAU;EACtD,MAAM,UAAU,WAAW,WAAW;EACtC,MAAM,KAAK,SAAS;CACrB;CACA,OAAO;EAAE;EAAO,WAAW,MAAM;CAAO;AACzC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/fields/icon/layers/coalesce.ts
|
|
2
|
+
const pending = /* @__PURE__ */ new Map();
|
|
3
|
+
/**
|
|
4
|
+
* Batches `resolveMetaMany` calls issued within one microtask into a single call.
|
|
5
|
+
*
|
|
6
|
+
* The window is a microtask rather than a request, because the surface with the most to
|
|
7
|
+
* gain has no request to key on: Payload renders list cells with `payload` and `i18n`
|
|
8
|
+
* only. A server-rendered list resolves its cells in the same tick, so a microtask window
|
|
9
|
+
* collapses fifty rows into one query without needing a request handle at all.
|
|
10
|
+
*/
|
|
11
|
+
const coalesceResolveMany = (args) => {
|
|
12
|
+
const { ctx, key, name, resolveMetaMany } = args;
|
|
13
|
+
let batch = pending.get(key);
|
|
14
|
+
if (!batch) {
|
|
15
|
+
let resolve = () => {};
|
|
16
|
+
let reject = () => {};
|
|
17
|
+
const result = new Promise((resolveFn, rejectFn) => {
|
|
18
|
+
resolve = resolveFn;
|
|
19
|
+
reject = rejectFn;
|
|
20
|
+
});
|
|
21
|
+
const created = {
|
|
22
|
+
names: /* @__PURE__ */ new Set(),
|
|
23
|
+
reject,
|
|
24
|
+
resolve,
|
|
25
|
+
result
|
|
26
|
+
};
|
|
27
|
+
batch = created;
|
|
28
|
+
pending.set(key, created);
|
|
29
|
+
queueMicrotask(() => {
|
|
30
|
+
if (pending.get(key) !== created) return;
|
|
31
|
+
pending.delete(key);
|
|
32
|
+
resolveMetaMany([...created.names], ctx).then(created.resolve, created.reject);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
batch.names.add(name);
|
|
36
|
+
return batch.result;
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { coalesceResolveMany };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=coalesce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coalesce.js","names":[],"sources":["../../../../src/fields/icon/layers/coalesce.ts"],"sourcesContent":["import type { IconLayerContext, IconMeta } from '../../../types'\n\nexport type ResolveMetaMany = (\n\tnames: string[],\n\tctx: IconLayerContext\n) => Promise<Map<string, IconMeta>>\n\ntype Pending = {\n\tnames: Set<string>\n\tresult: Promise<Map<string, IconMeta>>\n\tresolve: (value: Map<string, IconMeta>) => void\n\treject: (error: unknown) => void\n}\n\nconst pending = new Map<string, Pending>()\n\n/**\n * Batches `resolveMetaMany` calls issued within one microtask into a single call.\n *\n * The window is a microtask rather than a request, because the surface with the most to\n * gain has no request to key on: Payload renders list cells with `payload` and `i18n`\n * only. A server-rendered list resolves its cells in the same tick, so a microtask window\n * collapses fifty rows into one query without needing a request handle at all.\n */\nexport const coalesceResolveMany = (args: {\n\tctx: IconLayerContext\n\tkey: string\n\tname: string\n\tresolveMetaMany: ResolveMetaMany\n}): Promise<Map<string, IconMeta>> => {\n\tconst { ctx, key, name, resolveMetaMany } = args\n\tlet batch = pending.get(key)\n\tif (!batch) {\n\t\tlet resolve: (value: Map<string, IconMeta>) => void = () => {}\n\t\tlet reject: (error: unknown) => void = () => {}\n\t\tconst result = new Promise<Map<string, IconMeta>>((resolveFn, rejectFn) => {\n\t\t\tresolve = resolveFn\n\t\t\treject = rejectFn\n\t\t})\n\t\tconst created: Pending = { names: new Set(), reject, resolve, result }\n\t\tbatch = created\n\t\tpending.set(key, created)\n\t\t// Dispatch after the current microtask drain, so every synchronous caller in this\n\t\t// tick lands in the same batch. The entry is cleared first, so a lookup issued from\n\t\t// a `.then` on this batch opens a fresh one rather than joining a dispatched batch.\n\t\tqueueMicrotask(() => {\n\t\t\tif (pending.get(key) !== created) return\n\t\t\tpending.delete(key)\n\t\t\tresolveMetaMany([...created.names], ctx).then(created.resolve, created.reject)\n\t\t})\n\t}\n\tbatch.names.add(name)\n\treturn batch.result\n}\n"],"mappings":";AAcA,MAAM,0BAAU,IAAI,IAAqB;;;;;;;;;AAUzC,MAAa,uBAAuB,SAKE;CACrC,MAAM,EAAE,KAAK,KAAK,MAAM,oBAAoB;CAC5C,IAAI,QAAQ,QAAQ,IAAI,GAAG;CAC3B,IAAI,CAAC,OAAO;EACX,IAAI,gBAAwD,CAAC;EAC7D,IAAI,eAAyC,CAAC;EAC9C,MAAM,SAAS,IAAI,SAAgC,WAAW,aAAa;GAC1E,UAAU;GACV,SAAS;EACV,CAAC;EACD,MAAM,UAAmB;GAAE,uBAAO,IAAI,IAAI;GAAG;GAAQ;GAAS;EAAO;EACrE,QAAQ;EACR,QAAQ,IAAI,KAAK,OAAO;EAIxB,qBAAqB;GACpB,IAAI,QAAQ,IAAI,GAAG,MAAM,SAAS;GAClC,QAAQ,OAAO,GAAG;GAClB,gBAAgB,CAAC,GAAG,QAAQ,KAAK,GAAG,GAAG,EAAE,KAAK,QAAQ,SAAS,QAAQ,MAAM;EAC9E,CAAC;CACF;CACA,MAAM,MAAM,IAAI,IAAI;CACpB,OAAO,MAAM;AACd"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
//#region src/fields/icon/layers/manifestCache.d.ts
|
|
2
|
+
/** Drops cached listings. Without a slug it clears everything, which is what tests want. */
|
|
3
|
+
declare const invalidateLayerManifests: (slug?: string) => void;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { invalidateLayerManifests };
|
|
6
|
+
//# sourceMappingURL=manifestCache.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//#region src/fields/icon/layers/manifestCache.ts
|
|
2
|
+
const cache = /* @__PURE__ */ new Map();
|
|
3
|
+
/**
|
|
4
|
+
* Distinct per adapter, because two adapters may legitimately use the same layer id, and
|
|
5
|
+
* per the layer's own scope. Slug and id are identical across tenants by construction, so
|
|
6
|
+
* a layer whose listing varies by request must widen the key or it would serve one
|
|
7
|
+
* tenant's manifest to another.
|
|
8
|
+
*/
|
|
9
|
+
const keyFor = (slug, layer, ctx) => `${slug}::${layer.id}::${layer.cacheKey?.(ctx) ?? ""}`;
|
|
10
|
+
const isFresh = (entry, layer) => {
|
|
11
|
+
const policy = layer.cache ?? "forever";
|
|
12
|
+
if (policy === "forever") return true;
|
|
13
|
+
return Date.now() - entry.at < policy.ttl;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A layer's manifest listing, cached to its declared policy. `'forever'` is the default
|
|
17
|
+
* and reproduces how a static build artifact has always been cached; a `ttl` suits a
|
|
18
|
+
* layer whose contents change at runtime.
|
|
19
|
+
*
|
|
20
|
+
* Only the drawer *listing* comes from here. Validation goes through `resolveMeta`, which
|
|
21
|
+
* is deliberately never cached, so an icon added at runtime is valid immediately even
|
|
22
|
+
* while this listing is still stale.
|
|
23
|
+
*/
|
|
24
|
+
const loadLayerManifest = (slug, layer, ctx) => {
|
|
25
|
+
const key = keyFor(slug, layer, ctx);
|
|
26
|
+
const existing = cache.get(key);
|
|
27
|
+
if (existing && isFresh(existing, layer)) return existing.manifest;
|
|
28
|
+
const manifest = layer.loadManifest(ctx);
|
|
29
|
+
cache.set(key, {
|
|
30
|
+
at: Date.now(),
|
|
31
|
+
manifest
|
|
32
|
+
});
|
|
33
|
+
manifest.catch(() => {
|
|
34
|
+
if (cache.get(key)?.manifest === manifest) cache.delete(key);
|
|
35
|
+
});
|
|
36
|
+
return manifest;
|
|
37
|
+
};
|
|
38
|
+
/** Drops cached listings. Without a slug it clears everything, which is what tests want. */
|
|
39
|
+
const invalidateLayerManifests = (slug) => {
|
|
40
|
+
if (slug === void 0) {
|
|
41
|
+
cache.clear();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
for (const key of cache.keys()) if (key.startsWith(`${slug}::`)) cache.delete(key);
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { invalidateLayerManifests, loadLayerManifest };
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=manifestCache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manifestCache.js","names":[],"sources":["../../../../src/fields/icon/layers/manifestCache.ts"],"sourcesContent":["import type { IconLayer, IconLayerContext, IconManifest } from '../../../types'\n\ntype Entry = { at: number; manifest: Promise<IconManifest> }\n\nconst cache = new Map<string, Entry>()\n\n/**\n * Distinct per adapter, because two adapters may legitimately use the same layer id, and\n * per the layer's own scope. Slug and id are identical across tenants by construction, so\n * a layer whose listing varies by request must widen the key or it would serve one\n * tenant's manifest to another.\n */\nconst keyFor = (slug: string, layer: IconLayer, ctx: IconLayerContext): string =>\n\t`${slug}::${layer.id}::${layer.cacheKey?.(ctx) ?? ''}`\n\nconst isFresh = (entry: Entry, layer: IconLayer): boolean => {\n\tconst policy = layer.cache ?? 'forever'\n\tif (policy === 'forever') return true\n\treturn Date.now() - entry.at < policy.ttl\n}\n\n/**\n * A layer's manifest listing, cached to its declared policy. `'forever'` is the default\n * and reproduces how a static build artifact has always been cached; a `ttl` suits a\n * layer whose contents change at runtime.\n *\n * Only the drawer *listing* comes from here. Validation goes through `resolveMeta`, which\n * is deliberately never cached, so an icon added at runtime is valid immediately even\n * while this listing is still stale.\n */\nexport const loadLayerManifest = (\n\tslug: string,\n\tlayer: IconLayer,\n\tctx: IconLayerContext\n): Promise<IconManifest> => {\n\tconst key = keyFor(slug, layer, ctx)\n\tconst existing = cache.get(key)\n\tif (existing && isFresh(existing, layer)) return existing.manifest\n\tconst manifest = layer.loadManifest(ctx)\n\tcache.set(key, { at: Date.now(), manifest })\n\t// A rejected load must not poison the entry for the process lifetime; evict so the\n\t// next call retries. Same contract the adapter-level manifest cache already keeps.\n\tvoid manifest.catch(() => {\n\t\tif (cache.get(key)?.manifest === manifest) cache.delete(key)\n\t})\n\treturn manifest\n}\n\n/** Drops cached listings. Without a slug it clears everything, which is what tests want. */\nexport const invalidateLayerManifests = (slug?: string): void => {\n\tif (slug === undefined) {\n\t\tcache.clear()\n\t\treturn\n\t}\n\tfor (const key of cache.keys()) {\n\t\tif (key.startsWith(`${slug}::`)) cache.delete(key)\n\t}\n}\n"],"mappings":";AAIA,MAAM,wBAAQ,IAAI,IAAmB;;;;;;;AAQrC,MAAM,UAAU,MAAc,OAAkB,QAC/C,GAAG,KAAK,IAAI,MAAM,GAAG,IAAI,MAAM,WAAW,GAAG,KAAK;AAEnD,MAAM,WAAW,OAAc,UAA8B;CAC5D,MAAM,SAAS,MAAM,SAAS;CAC9B,IAAI,WAAW,WAAW,OAAO;CACjC,OAAO,KAAK,IAAI,IAAI,MAAM,KAAK,OAAO;AACvC;;;;;;;;;;AAWA,MAAa,qBACZ,MACA,OACA,QAC2B;CAC3B,MAAM,MAAM,OAAO,MAAM,OAAO,GAAG;CACnC,MAAM,WAAW,MAAM,IAAI,GAAG;CAC9B,IAAI,YAAY,QAAQ,UAAU,KAAK,GAAG,OAAO,SAAS;CAC1D,MAAM,WAAW,MAAM,aAAa,GAAG;CACvC,MAAM,IAAI,KAAK;EAAE,IAAI,KAAK,IAAI;EAAG;CAAS,CAAC;CAG3C,SAAc,YAAY;EACzB,IAAI,MAAM,IAAI,GAAG,GAAG,aAAa,UAAU,MAAM,OAAO,GAAG;CAC5D,CAAC;CACD,OAAO;AACR;;AAGA,MAAa,4BAA4B,SAAwB;CAChE,IAAI,SAAS,KAAA,GAAW;EACvB,MAAM,MAAM;EACZ;CACD;CACA,KAAK,MAAM,OAAO,MAAM,KAAK,GAC5B,IAAI,IAAI,WAAW,GAAG,KAAK,GAAG,GAAG,MAAM,OAAO,GAAG;AAEnD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IconAdapter, IconLayerContext, IconManifest, IconMeta } from "../../../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/fields/icon/layers/resolve.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The merged listing a drawer browses. Later layers win by name and may add names the
|
|
6
|
+
* base does not have, so a runtime layer can both override and extend a static set.
|
|
7
|
+
*
|
|
8
|
+
* An adapter with no layers returns its own manifest untouched, which is every adapter
|
|
9
|
+
* written before layers existed.
|
|
10
|
+
*/
|
|
11
|
+
declare const loadLayeredManifest: (adapter: IconAdapter, ctx: IconLayerContext) => Promise<IconManifest>;
|
|
12
|
+
/**
|
|
13
|
+
* One icon's entry, resolved newest layer first so an override wins and an older layer
|
|
14
|
+
* still answers for names the newer one lacks. Serves validation (non-null means the name
|
|
15
|
+
* exists) and label resolution from the same lookup.
|
|
16
|
+
*/
|
|
17
|
+
declare const resolveLayeredMeta: (adapter: IconAdapter, name: string, ctx: IconLayerContext) => Promise<IconMeta | null>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { loadLayeredManifest, resolveLayeredMeta };
|
|
20
|
+
//# sourceMappingURL=resolve.d.ts.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { coalesceResolveMany } from "./coalesce.js";
|
|
2
|
+
import { loadLayerManifest } from "./manifestCache.js";
|
|
3
|
+
//#region src/fields/icon/layers/resolve.ts
|
|
4
|
+
/** Codepoint order, matching the codegen, so a merged category list is stable across runs. */
|
|
5
|
+
const byCodepoint = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
6
|
+
/**
|
|
7
|
+
* The merged listing a drawer browses. Later layers win by name and may add names the
|
|
8
|
+
* base does not have, so a runtime layer can both override and extend a static set.
|
|
9
|
+
*
|
|
10
|
+
* An adapter with no layers returns its own manifest untouched, which is every adapter
|
|
11
|
+
* written before layers existed.
|
|
12
|
+
*/
|
|
13
|
+
const loadLayeredManifest = async (adapter, ctx) => {
|
|
14
|
+
const layers = adapter.layers;
|
|
15
|
+
if (!layers || layers.length === 0) return adapter.loadManifest();
|
|
16
|
+
const loaded = await Promise.all(layers.map((layer) => loadLayerManifest(adapter.slug, layer, ctx)));
|
|
17
|
+
const byName = /* @__PURE__ */ new Map();
|
|
18
|
+
const categories = /* @__PURE__ */ new Set();
|
|
19
|
+
for (const manifest of loaded) {
|
|
20
|
+
for (const icon of manifest.icons) byName.set(icon.name, icon);
|
|
21
|
+
for (const category of manifest.categories) categories.add(category);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
categories: [...categories].sort(byCodepoint),
|
|
25
|
+
icons: [...byName.values()]
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
const resolveFromLayer = async (args) => {
|
|
29
|
+
const { ctx, layer, name, slug } = args;
|
|
30
|
+
if (layer.resolveMetaMany) return (await coalesceResolveMany({
|
|
31
|
+
ctx,
|
|
32
|
+
key: `${slug}::${layer.id}`,
|
|
33
|
+
name,
|
|
34
|
+
resolveMetaMany: layer.resolveMetaMany
|
|
35
|
+
})).get(name) ?? null;
|
|
36
|
+
if (layer.resolveMeta) return layer.resolveMeta(name, ctx);
|
|
37
|
+
return (await loadLayerManifest(slug, layer, ctx)).icons.find((icon) => icon.name === name) ?? null;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* One icon's entry, resolved newest layer first so an override wins and an older layer
|
|
41
|
+
* still answers for names the newer one lacks. Serves validation (non-null means the name
|
|
42
|
+
* exists) and label resolution from the same lookup.
|
|
43
|
+
*/
|
|
44
|
+
const resolveLayeredMeta = async (adapter, name, ctx) => {
|
|
45
|
+
const layers = adapter.layers;
|
|
46
|
+
if (!layers || layers.length === 0) return null;
|
|
47
|
+
for (let index = layers.length - 1; index >= 0; index -= 1) {
|
|
48
|
+
const layer = layers[index];
|
|
49
|
+
if (!layer) continue;
|
|
50
|
+
const meta = await resolveFromLayer({
|
|
51
|
+
ctx,
|
|
52
|
+
layer,
|
|
53
|
+
name,
|
|
54
|
+
slug: adapter.slug
|
|
55
|
+
});
|
|
56
|
+
if (meta) return meta;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { loadLayeredManifest, resolveLayeredMeta };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.js","names":[],"sources":["../../../../src/fields/icon/layers/resolve.ts"],"sourcesContent":["import type {\n\tIconAdapter,\n\tIconLayer,\n\tIconLayerContext,\n\tIconManifest,\n\tIconMeta,\n} from '../../../types'\nimport { coalesceResolveMany } from './coalesce'\nimport { loadLayerManifest } from './manifestCache'\n\n/** Codepoint order, matching the codegen, so a merged category list is stable across runs. */\nconst byCodepoint = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0)\n\n/**\n * The merged listing a drawer browses. Later layers win by name and may add names the\n * base does not have, so a runtime layer can both override and extend a static set.\n *\n * An adapter with no layers returns its own manifest untouched, which is every adapter\n * written before layers existed.\n */\nexport const loadLayeredManifest = async (\n\tadapter: IconAdapter,\n\tctx: IconLayerContext\n): Promise<IconManifest> => {\n\tconst layers = adapter.layers\n\tif (!layers || layers.length === 0) return adapter.loadManifest()\n\tconst loaded = await Promise.all(\n\t\tlayers.map((layer) => loadLayerManifest(adapter.slug, layer, ctx))\n\t)\n\tconst byName = new Map<string, IconMeta>()\n\tconst categories = new Set<string>()\n\tfor (const manifest of loaded) {\n\t\tfor (const icon of manifest.icons) byName.set(icon.name, icon)\n\t\tfor (const category of manifest.categories) categories.add(category)\n\t}\n\treturn {\n\t\tcategories: [...categories].sort(byCodepoint),\n\t\ticons: [...byName.values()],\n\t}\n}\n\nconst resolveFromLayer = async (args: {\n\tctx: IconLayerContext\n\tlayer: IconLayer\n\tname: string\n\tslug: string\n}): Promise<IconMeta | null> => {\n\tconst { ctx, layer, name, slug } = args\n\tif (layer.resolveMetaMany) {\n\t\tconst batch = await coalesceResolveMany({\n\t\t\tctx,\n\t\t\tkey: `${slug}::${layer.id}`,\n\t\t\tname,\n\t\t\tresolveMetaMany: layer.resolveMetaMany,\n\t\t})\n\t\treturn batch.get(name) ?? null\n\t}\n\tif (layer.resolveMeta) return layer.resolveMeta(name, ctx)\n\tconst manifest = await loadLayerManifest(slug, layer, ctx)\n\treturn manifest.icons.find((icon) => icon.name === name) ?? null\n}\n\n/**\n * One icon's entry, resolved newest layer first so an override wins and an older layer\n * still answers for names the newer one lacks. Serves validation (non-null means the name\n * exists) and label resolution from the same lookup.\n */\nexport const resolveLayeredMeta = async (\n\tadapter: IconAdapter,\n\tname: string,\n\tctx: IconLayerContext\n): Promise<IconMeta | null> => {\n\tconst layers = adapter.layers\n\tif (!layers || layers.length === 0) return null\n\tfor (let index = layers.length - 1; index >= 0; index -= 1) {\n\t\tconst layer = layers[index]\n\t\tif (!layer) continue\n\t\tconst meta = await resolveFromLayer({ ctx, layer, name, slug: adapter.slug })\n\t\tif (meta) return meta\n\t}\n\treturn null\n}\n"],"mappings":";;;;AAWA,MAAM,eAAe,GAAW,MAAuB,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI;;;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GAAK;GAAO;GAAM,MAAM,QAAQ;EAAK,CAAC;EAC5E,IAAI,MAAM,OAAO;CAClB;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { IconLayer, IconLayerCache, IconLayerContext } from "../../../types.js";
|
|
2
|
+
import { CollectionSlug, Where } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/fields/icon/layers/upload.d.ts
|
|
5
|
+
type UploadIconLayerOptions = {
|
|
6
|
+
/** Upload collection holding the icons. */collection: CollectionSlug; /** Layer id, unique within the adapter. Defaults to the collection slug. */
|
|
7
|
+
id?: string; /** Field holding the icon name that becomes part of the stored value. Defaults to `name`. */
|
|
8
|
+
nameField?: string; /** Field holding the human-readable label. Defaults to `label`. */
|
|
9
|
+
labelField?: string; /** Field holding search tags, a string array. Defaults to `tags`. */
|
|
10
|
+
tagsField?: string; /** Field holding categories, a string array. Defaults to `categories`. */
|
|
11
|
+
categoriesField?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Extra constraint, typically tenant scoping. Receives the request so it can read the
|
|
14
|
+
* caller. Supply `cacheKey` alongside it, or one caller's listing is served to another.
|
|
15
|
+
*/
|
|
16
|
+
where?: (ctx: IconLayerContext) => Where | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Cache-key segment for a scoped layer. Required whenever `where` varies by request:
|
|
19
|
+
* the listing cache is otherwise keyed by adapter slug and layer id alone.
|
|
20
|
+
*/
|
|
21
|
+
cacheKey?: (ctx: IconLayerContext) => string; /** Listing cache policy. Defaults to a 30 second TTL, since uploads change at runtime. */
|
|
22
|
+
cache?: IconLayerCache;
|
|
23
|
+
/**
|
|
24
|
+
* How glyphs paint. `'url'` is the default and renders each icon through an `<img>`,
|
|
25
|
+
* which is inert by construction: an SVG loaded that way cannot execute script.
|
|
26
|
+
*
|
|
27
|
+
* `'svg'` inlines the markup instead, which allows `currentColor` recolouring but means
|
|
28
|
+
* rendering editor-uploaded markup inside the admin. On a multi-tenant install the
|
|
29
|
+
* uploader may be a customer rather than an operator, so that is a deliberate choice
|
|
30
|
+
* with a real blast radius. The markup is sanitised either way; `'url'` simply removes
|
|
31
|
+
* the question.
|
|
32
|
+
*/
|
|
33
|
+
render?: 'url' | 'svg'; /** importMap path resolving `(name: string) => null | string` for the `url` strategy. */
|
|
34
|
+
resolveUrl?: string; /** importMap path resolving `() => Promise<Record<string, string>>` for the `svg` strategy. */
|
|
35
|
+
loadSvgs?: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* An icon layer backed by a Payload upload collection, so editors can add icons without a
|
|
39
|
+
* deploy.
|
|
40
|
+
*
|
|
41
|
+
* This is the preset that proves the layer contract holds: if a real upload-backed library
|
|
42
|
+
* needs anything expressible only here and not through `IconLayer`, that is a gap in the
|
|
43
|
+
* contract rather than in this file.
|
|
44
|
+
*
|
|
45
|
+
* Validation never reads the listing cache. `resolveMetaMany` answers from one indexed
|
|
46
|
+
* query, so an icon uploaded a moment ago is valid immediately, on every instance, rather
|
|
47
|
+
* than after a cache expiry or a restart.
|
|
48
|
+
*/
|
|
49
|
+
declare const uploadIconLayer: (options: UploadIconLayerOptions) => IconLayer;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { UploadIconLayerOptions, uploadIconLayer };
|
|
52
|
+
//# sourceMappingURL=upload.d.ts.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
//#region src/fields/icon/layers/upload.ts
|
|
2
|
+
/**
|
|
3
|
+
* Widens one document to a bag of fields.
|
|
4
|
+
*
|
|
5
|
+
* Payload types `find` as the document union of the host app's collections, and a generated
|
|
6
|
+
* document type has no index signature, so asserting the array directly is rejected wherever
|
|
7
|
+
* types have been generated. That is the dev app and every real consumer, and it is why this
|
|
8
|
+
* only shows up outside the package's own typecheck. Taking `unknown` keeps it one legal
|
|
9
|
+
* assertion rather than a double cast.
|
|
10
|
+
*/
|
|
11
|
+
const asUploadDoc = (doc) => doc;
|
|
12
|
+
const DEFAULT_TTL = 3e4;
|
|
13
|
+
const asStrings = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
|
|
14
|
+
const toMeta = (doc, options) => {
|
|
15
|
+
const name = doc[options.nameField];
|
|
16
|
+
if (typeof name !== "string" || name === "") return null;
|
|
17
|
+
const label = doc[options.labelField];
|
|
18
|
+
return {
|
|
19
|
+
categories: asStrings(doc[options.categoriesField]),
|
|
20
|
+
name,
|
|
21
|
+
tags: asStrings(doc[options.tagsField]),
|
|
22
|
+
...typeof label === "string" && label !== "" ? { label } : {}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const findDocs = async (args) => {
|
|
26
|
+
return (await args.payload.find({
|
|
27
|
+
collection: args.collection,
|
|
28
|
+
depth: 0,
|
|
29
|
+
limit: 0,
|
|
30
|
+
pagination: false,
|
|
31
|
+
...args.ctx.req ? { req: args.ctx.req } : {},
|
|
32
|
+
...args.where ? { where: args.where } : {}
|
|
33
|
+
})).docs.map(asUploadDoc);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* An icon layer backed by a Payload upload collection, so editors can add icons without a
|
|
37
|
+
* deploy.
|
|
38
|
+
*
|
|
39
|
+
* This is the preset that proves the layer contract holds: if a real upload-backed library
|
|
40
|
+
* needs anything expressible only here and not through `IconLayer`, that is a gap in the
|
|
41
|
+
* contract rather than in this file.
|
|
42
|
+
*
|
|
43
|
+
* Validation never reads the listing cache. `resolveMetaMany` answers from one indexed
|
|
44
|
+
* query, so an icon uploaded a moment ago is valid immediately, on every instance, rather
|
|
45
|
+
* than after a cache expiry or a restart.
|
|
46
|
+
*/
|
|
47
|
+
const uploadIconLayer = (options) => {
|
|
48
|
+
const fields = {
|
|
49
|
+
categoriesField: options.categoriesField ?? "categories",
|
|
50
|
+
labelField: options.labelField ?? "label",
|
|
51
|
+
nameField: options.nameField ?? "name",
|
|
52
|
+
tagsField: options.tagsField ?? "tags"
|
|
53
|
+
};
|
|
54
|
+
const strategy = options.render ?? "url";
|
|
55
|
+
if (strategy === "url" && !options.resolveUrl) throw new Error("[fields] uploadIconLayer: the url strategy needs a resolveUrl import path");
|
|
56
|
+
if (strategy === "svg" && !options.loadSvgs) throw new Error("[fields] uploadIconLayer: the svg strategy needs a loadSvgs import path");
|
|
57
|
+
const scopedWhere = (ctx) => options.where?.(ctx);
|
|
58
|
+
return {
|
|
59
|
+
cache: options.cache ?? { ttl: DEFAULT_TTL },
|
|
60
|
+
...options.cacheKey ? { cacheKey: options.cacheKey } : {},
|
|
61
|
+
id: options.id ?? String(options.collection),
|
|
62
|
+
loadManifest: async (ctx) => {
|
|
63
|
+
const icons = (await findDocs({
|
|
64
|
+
collection: options.collection,
|
|
65
|
+
ctx,
|
|
66
|
+
payload: ctx.payload,
|
|
67
|
+
where: scopedWhere(ctx)
|
|
68
|
+
})).map((doc) => toMeta(doc, fields)).filter((icon) => icon !== null);
|
|
69
|
+
return {
|
|
70
|
+
categories: [...new Set(icons.flatMap((icon) => icon.categories))].sort(),
|
|
71
|
+
icons
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
render: strategy === "url" ? {
|
|
75
|
+
resolve: options.resolveUrl ?? "",
|
|
76
|
+
type: "url"
|
|
77
|
+
} : {
|
|
78
|
+
load: options.loadSvgs ?? "",
|
|
79
|
+
type: "svg"
|
|
80
|
+
},
|
|
81
|
+
resolveMeta: async (name, ctx) => {
|
|
82
|
+
const scoped = scopedWhere(ctx);
|
|
83
|
+
const where = scoped ? { and: [scoped, { [fields.nameField]: { equals: name } }] } : { [fields.nameField]: { equals: name } };
|
|
84
|
+
const doc = (await ctx.payload.find({
|
|
85
|
+
collection: options.collection,
|
|
86
|
+
depth: 0,
|
|
87
|
+
limit: 1,
|
|
88
|
+
...ctx.req ? { req: ctx.req } : {},
|
|
89
|
+
where
|
|
90
|
+
})).docs.map(asUploadDoc)[0];
|
|
91
|
+
return doc ? toMeta(doc, fields) : null;
|
|
92
|
+
},
|
|
93
|
+
resolveMetaMany: async (names, ctx) => {
|
|
94
|
+
const scoped = scopedWhere(ctx);
|
|
95
|
+
const where = scoped ? { and: [scoped, { [fields.nameField]: { in: names } }] } : { [fields.nameField]: { in: names } };
|
|
96
|
+
const result = await ctx.payload.find({
|
|
97
|
+
collection: options.collection,
|
|
98
|
+
depth: 0,
|
|
99
|
+
limit: 0,
|
|
100
|
+
pagination: false,
|
|
101
|
+
...ctx.req ? { req: ctx.req } : {},
|
|
102
|
+
where
|
|
103
|
+
});
|
|
104
|
+
const found = /* @__PURE__ */ new Map();
|
|
105
|
+
for (const doc of result.docs.map(asUploadDoc)) {
|
|
106
|
+
const meta = toMeta(doc, fields);
|
|
107
|
+
if (meta) found.set(meta.name, meta);
|
|
108
|
+
}
|
|
109
|
+
return found;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
//#endregion
|
|
114
|
+
export { uploadIconLayer };
|
|
115
|
+
|
|
116
|
+
//# sourceMappingURL=upload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.js","names":[],"sources":["../../../../src/fields/icon/layers/upload.ts"],"sourcesContent":["import type { CollectionSlug, Payload, Where } from 'payload'\nimport type { IconLayer, IconLayerCache, IconLayerContext, IconMeta } from '../../../types'\n\n/** Document shape the layer reads, after the configurable field names are applied. */\ntype UploadDoc = Record<string, unknown>\n\n/**\n * Widens one document to a bag of fields.\n *\n * Payload types `find` as the document union of the host app's collections, and a generated\n * document type has no index signature, so asserting the array directly is rejected wherever\n * types have been generated. That is the dev app and every real consumer, and it is why this\n * only shows up outside the package's own typecheck. Taking `unknown` keeps it one legal\n * assertion rather than a double cast.\n */\nconst asUploadDoc = (doc: unknown): UploadDoc => doc as UploadDoc\n\nexport type UploadIconLayerOptions = {\n\t/** Upload collection holding the icons. */\n\tcollection: CollectionSlug\n\t/** Layer id, unique within the adapter. Defaults to the collection slug. */\n\tid?: string\n\t/** Field holding the icon name that becomes part of the stored value. Defaults to `name`. */\n\tnameField?: string\n\t/** Field holding the human-readable label. Defaults to `label`. */\n\tlabelField?: string\n\t/** Field holding search tags, a string array. Defaults to `tags`. */\n\ttagsField?: string\n\t/** Field holding categories, a string array. Defaults to `categories`. */\n\tcategoriesField?: string\n\t/**\n\t * Extra constraint, typically tenant scoping. Receives the request so it can read the\n\t * caller. Supply `cacheKey` alongside it, or one caller's listing is served to another.\n\t */\n\twhere?: (ctx: IconLayerContext) => Where | undefined\n\t/**\n\t * Cache-key segment for a scoped layer. Required whenever `where` varies by request:\n\t * the listing cache is otherwise keyed by adapter slug and layer id alone.\n\t */\n\tcacheKey?: (ctx: IconLayerContext) => string\n\t/** Listing cache policy. Defaults to a 30 second TTL, since uploads change at runtime. */\n\tcache?: IconLayerCache\n\t/**\n\t * How glyphs paint. `'url'` is the default and renders each icon through an `<img>`,\n\t * which is inert by construction: an SVG loaded that way cannot execute script.\n\t *\n\t * `'svg'` inlines the markup instead, which allows `currentColor` recolouring but means\n\t * rendering editor-uploaded markup inside the admin. On a multi-tenant install the\n\t * uploader may be a customer rather than an operator, so that is a deliberate choice\n\t * with a real blast radius. The markup is sanitised either way; `'url'` simply removes\n\t * the question.\n\t */\n\trender?: 'url' | 'svg'\n\t/** importMap path resolving `(name: string) => null | string` for the `url` strategy. */\n\tresolveUrl?: string\n\t/** importMap path resolving `() => Promise<Record<string, string>>` for the `svg` strategy. */\n\tloadSvgs?: string\n}\n\nconst DEFAULT_TTL = 30_000\n\nconst asStrings = (value: unknown): string[] =>\n\tArray.isArray(value) ? value.filter((entry): entry is string => typeof entry === 'string') : []\n\nconst toMeta = (doc: UploadDoc, options: Required<FieldNames>): IconMeta | null => {\n\tconst name = doc[options.nameField]\n\tif (typeof name !== 'string' || name === '') return null\n\tconst label = doc[options.labelField]\n\treturn {\n\t\tcategories: asStrings(doc[options.categoriesField]),\n\t\tname,\n\t\ttags: asStrings(doc[options.tagsField]),\n\t\t...(typeof label === 'string' && label !== '' ? { label } : {}),\n\t}\n}\n\ntype FieldNames = {\n\tcategoriesField: string\n\tlabelField: string\n\tnameField: string\n\ttagsField: string\n}\n\nconst findDocs = async (args: {\n\tctx: IconLayerContext\n\tcollection: CollectionSlug\n\tpayload: Payload\n\twhere: Where | undefined\n}): Promise<UploadDoc[]> => {\n\tconst result = await args.payload.find({\n\t\tcollection: args.collection,\n\t\tdepth: 0,\n\t\tlimit: 0,\n\t\tpagination: false,\n\t\t...(args.ctx.req ? { req: args.ctx.req } : {}),\n\t\t...(args.where ? { where: args.where } : {}),\n\t})\n\treturn result.docs.map(asUploadDoc)\n}\n\n/**\n * An icon layer backed by a Payload upload collection, so editors can add icons without a\n * deploy.\n *\n * This is the preset that proves the layer contract holds: if a real upload-backed library\n * needs anything expressible only here and not through `IconLayer`, that is a gap in the\n * contract rather than in this file.\n *\n * Validation never reads the listing cache. `resolveMetaMany` answers from one indexed\n * query, so an icon uploaded a moment ago is valid immediately, on every instance, rather\n * than after a cache expiry or a restart.\n */\nexport const uploadIconLayer = (options: UploadIconLayerOptions): IconLayer => {\n\tconst fields: Required<FieldNames> = {\n\t\tcategoriesField: options.categoriesField ?? 'categories',\n\t\tlabelField: options.labelField ?? 'label',\n\t\tnameField: options.nameField ?? 'name',\n\t\ttagsField: options.tagsField ?? 'tags',\n\t}\n\tconst strategy = options.render ?? 'url'\n\tif (strategy === 'url' && !options.resolveUrl) {\n\t\tthrow new Error('[fields] uploadIconLayer: the url strategy needs a resolveUrl import path')\n\t}\n\tif (strategy === 'svg' && !options.loadSvgs) {\n\t\tthrow new Error('[fields] uploadIconLayer: the svg strategy needs a loadSvgs import path')\n\t}\n\n\tconst scopedWhere = (ctx: IconLayerContext): Where | undefined => options.where?.(ctx)\n\n\treturn {\n\t\tcache: options.cache ?? { ttl: DEFAULT_TTL },\n\t\t...(options.cacheKey ? { cacheKey: options.cacheKey } : {}),\n\t\tid: options.id ?? String(options.collection),\n\t\tloadManifest: async (ctx) => {\n\t\t\tconst docs = await findDocs({\n\t\t\t\tcollection: options.collection,\n\t\t\t\tctx,\n\t\t\t\tpayload: ctx.payload,\n\t\t\t\twhere: scopedWhere(ctx),\n\t\t\t})\n\t\t\tconst icons = docs\n\t\t\t\t.map((doc) => toMeta(doc, fields))\n\t\t\t\t.filter((icon): icon is IconMeta => icon !== null)\n\t\t\tconst categories = [...new Set(icons.flatMap((icon) => icon.categories))].sort()\n\t\t\treturn { categories, icons }\n\t\t},\n\t\trender:\n\t\t\tstrategy === 'url'\n\t\t\t\t? { resolve: options.resolveUrl ?? '', type: 'url' }\n\t\t\t\t: { load: options.loadSvgs ?? '', type: 'svg' },\n\t\tresolveMeta: async (name, ctx) => {\n\t\t\tconst scoped = scopedWhere(ctx)\n\t\t\tconst where: Where = scoped\n\t\t\t\t? { and: [scoped, { [fields.nameField]: { equals: name } }] }\n\t\t\t\t: { [fields.nameField]: { equals: name } }\n\t\t\tconst result = await ctx.payload.find({\n\t\t\t\tcollection: options.collection,\n\t\t\t\tdepth: 0,\n\t\t\t\tlimit: 1,\n\t\t\t\t...(ctx.req ? { req: ctx.req } : {}),\n\t\t\t\twhere,\n\t\t\t})\n\t\t\tconst doc = result.docs.map(asUploadDoc)[0]\n\t\t\treturn doc ? toMeta(doc, fields) : null\n\t\t},\n\t\t// One `in` query for every name looked up in the tick. A document holding eight icon\n\t\t// fields, or a fifty-row list, costs one query rather than eight or fifty.\n\t\tresolveMetaMany: async (names, ctx) => {\n\t\t\tconst scoped = scopedWhere(ctx)\n\t\t\tconst where: Where = scoped\n\t\t\t\t? { and: [scoped, { [fields.nameField]: { in: names } }] }\n\t\t\t\t: { [fields.nameField]: { in: names } }\n\t\t\tconst result = await ctx.payload.find({\n\t\t\t\tcollection: options.collection,\n\t\t\t\tdepth: 0,\n\t\t\t\tlimit: 0,\n\t\t\t\tpagination: false,\n\t\t\t\t...(ctx.req ? { req: ctx.req } : {}),\n\t\t\t\twhere,\n\t\t\t})\n\t\t\tconst found = new Map<string, IconMeta>()\n\t\t\tfor (const doc of result.docs.map(asUploadDoc)) {\n\t\t\t\tconst meta = toMeta(doc, fields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MAAM,EAAE,CAAC,EAAE,IACvD,GAAG,OAAO,YAAY,EAAE,IAAI,MAAM,EAAE;GACvC,MAAM,SAAS,MAAM,IAAI,QAAQ,KAAK;IACrC,YAAY,QAAQ;IACpB,OAAO;IACP,OAAO;IACP,YAAY;IACZ,GAAI,IAAI,MAAM,EAAE,KAAK,IAAI,IAAI,IAAI,CAAC;IAClC;GACD,CAAC;GACD,MAAM,wBAAQ,IAAI,IAAsB;GACxC,KAAK,MAAM,OAAO,OAAO,KAAK,IAAI,WAAW,GAAG;IAC/C,MAAM,OAAO,OAAO,KAAK,MAAM;IAC/B,IAAI,MAAM,MAAM,IAAI,KAAK,MAAM,IAAI;GACpC;GACA,OAAO;EACR;CACD;AACD"}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { FIELDS_REGISTRY_KEY } from "../../plugin/registry.js";
|
|
2
|
+
import { ICON_MANIFEST_PATH, createIconManifestHandler } from "./server/endpoint.js";
|
|
2
3
|
//#region src/fields/icon/plugin.ts
|
|
4
|
+
/** The importMap path a render strategy needs registered, so generate:importmap picks it up. */
|
|
5
|
+
const renderDependency = (render) => render.type === "component" ? {
|
|
6
|
+
path: render.Icon,
|
|
7
|
+
type: "component"
|
|
8
|
+
} : {
|
|
9
|
+
path: render.type === "url" ? render.resolve : render.load,
|
|
10
|
+
type: "function"
|
|
11
|
+
};
|
|
3
12
|
/**
|
|
4
13
|
* Slugs prefix every stored value (`<slug>:<icon-name>`), so a slug carrying a
|
|
5
14
|
* `:` would parse back as a different library and name. Same shape as the icon
|
|
@@ -20,6 +29,11 @@ const registerIcon = (config, icon) => {
|
|
|
20
29
|
if (!ADAPTER_SLUG.test(adapter.slug)) throw new Error(`[fields] invalid icon adapter slug: "${adapter.slug}" (expected kebab-case, e.g. "my-icons")`);
|
|
21
30
|
if (slugs.has(adapter.slug)) throw new Error(`[fields] duplicate icon adapter slug: ${adapter.slug}`);
|
|
22
31
|
slugs.add(adapter.slug);
|
|
32
|
+
const layerIds = /* @__PURE__ */ new Set();
|
|
33
|
+
for (const layer of adapter.layers ?? []) {
|
|
34
|
+
if (layerIds.has(layer.id)) throw new Error(`[fields] duplicate icon layer id "${layer.id}" in adapter "${adapter.slug}"`);
|
|
35
|
+
layerIds.add(layer.id);
|
|
36
|
+
}
|
|
23
37
|
}
|
|
24
38
|
const defaultLibrary = icon.defaultLibrary ?? firstAdapter.slug;
|
|
25
39
|
if (!slugs.has(defaultLibrary)) throw new Error(`[fields] defaultLibrary "${defaultLibrary}" is not a registered icon adapter`);
|
|
@@ -32,6 +46,15 @@ const registerIcon = (config, icon) => {
|
|
|
32
46
|
resolveAvailable: icon.resolveAvailable
|
|
33
47
|
};
|
|
34
48
|
config.custom[FIELDS_REGISTRY_KEY] = registry;
|
|
49
|
+
if (icon.adapters.some((adapter) => adapter.layers?.some((layer) => layer.render.type !== "component" || layer.resolveMeta))) config.endpoints = [...config.endpoints ?? [], {
|
|
50
|
+
handler: createIconManifestHandler({
|
|
51
|
+
adapters: icon.adapters,
|
|
52
|
+
alwaysAvailable: icon.alwaysAvailable,
|
|
53
|
+
resolveAvailable: icon.resolveAvailable
|
|
54
|
+
}),
|
|
55
|
+
method: "get",
|
|
56
|
+
path: ICON_MANIFEST_PATH
|
|
57
|
+
}];
|
|
35
58
|
config.admin ??= {};
|
|
36
59
|
config.admin.dependencies ??= {};
|
|
37
60
|
for (const adapter of icon.adapters) {
|
|
@@ -47,6 +70,7 @@ const registerIcon = (config, icon) => {
|
|
|
47
70
|
path: adapter.Nodes,
|
|
48
71
|
type: "component"
|
|
49
72
|
};
|
|
73
|
+
for (const layer of adapter.layers ?? []) config.admin.dependencies[`fields-icon-${adapter.slug}-${layer.id}`] = renderDependency(layer.render);
|
|
50
74
|
}
|
|
51
75
|
};
|
|
52
76
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","names":[],"sources":["../../../src/fields/icon/plugin.ts"],"sourcesContent":["import type { Config } from 'payload'\nimport { FIELDS_REGISTRY_KEY } from '../../plugin/registry'\nimport type { FieldsPluginRegistry, IconGlobalConfig } from '../../types'\n\n/**\n * Slugs prefix every stored value (`<slug>:<icon-name>`), so a slug carrying a\n * `:` would parse back as a different library and name. Same shape as the icon\n * names codegen accepts.\n */\nconst ADAPTER_SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/\n\n/**\n * Writes normalized icon globals to the plugin registry and forces the\n * adapters' client components into the importMap via admin.dependencies\n * (registry strings are not scanned by generate:importmap).\n */\nexport const registerIcon = (config: Config, icon: IconGlobalConfig | undefined): void => {\n\tif (!icon?.adapters) {\n\t\treturn\n\t}\n\tconst [firstAdapter] = icon.adapters\n\tif (!firstAdapter) {\n\t\treturn\n\t}\n\tconst slugs = new Set<string>()\n\tfor (const adapter of icon.adapters) {\n\t\tif (!ADAPTER_SLUG.test(adapter.slug)) {\n\t\t\tthrow new Error(\n\t\t\t\t`[fields] invalid icon adapter slug: \"${adapter.slug}\" (expected kebab-case, e.g. \"my-icons\")`\n\t\t\t)\n\t\t}\n\t\tif (slugs.has(adapter.slug)) {\n\t\t\tthrow new Error(`[fields] duplicate icon adapter slug: ${adapter.slug}`)\n\t\t}\n\t\tslugs.add(adapter.slug)\n\t}\n\tconst defaultLibrary = icon.defaultLibrary ?? firstAdapter.slug\n\tif (!slugs.has(defaultLibrary)) {\n\t\tthrow new Error(`[fields] defaultLibrary \"${defaultLibrary}\" is not a registered icon adapter`)\n\t}\n\tconfig.custom ??= {}\n\tconst registry = (config.custom[FIELDS_REGISTRY_KEY] ?? {}) as FieldsPluginRegistry\n\tregistry.icon = {\n\t\tadapters: icon.adapters,\n\t\talwaysAvailable: icon.alwaysAvailable,\n\t\tdefaultLibrary,\n\t\tresolveAvailable: icon.resolveAvailable,\n\t}\n\tconfig.custom[FIELDS_REGISTRY_KEY] = registry\n\tconfig.admin ??= {}\n\tconfig.admin.dependencies ??= {}\n\tfor (const adapter of icon.adapters) {\n\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Icon`] = {\n\t\t\tpath: adapter.Icon,\n\t\t\ttype: 'component',\n\t\t}\n\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Assets`] = {\n\t\t\tpath: adapter.Assets,\n\t\t\ttype: 'component',\n\t\t}\n\t\t// generate:importmap scans admin.dependencies, not registry strings, so the\n\t\t// optional Nodes loader must be registered here too or a regeneration drops\n\t\t// it and the drawer silently loses the bulk node-data fast path.\n\t\tif (adapter.Nodes) {\n\t\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Nodes`] = {\n\t\t\t\tpath: adapter.Nodes,\n\t\t\t\ttype: 'component',\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"plugin.js","names":[],"sources":["../../../src/fields/icon/plugin.ts"],"sourcesContent":["import type { Config } from 'payload'\nimport { FIELDS_REGISTRY_KEY } from '../../plugin/registry'\nimport type { FieldsPluginRegistry, IconGlobalConfig, IconRenderStrategy } from '../../types'\nimport { createIconManifestHandler, ICON_MANIFEST_PATH } from './server/endpoint'\n\n/** The importMap path a render strategy needs registered, so generate:importmap picks it up. */\nconst renderDependency = (\n\trender: IconRenderStrategy\n): { path: string; type: 'function' | 'component' } =>\n\trender.type === 'component'\n\t\t? { path: render.Icon, type: 'component' }\n\t\t: { path: render.type === 'url' ? render.resolve : render.load, type: 'function' }\n\n/**\n * Slugs prefix every stored value (`<slug>:<icon-name>`), so a slug carrying a\n * `:` would parse back as a different library and name. Same shape as the icon\n * names codegen accepts.\n */\nconst ADAPTER_SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/\n\n/**\n * Writes normalized icon globals to the plugin registry and forces the\n * adapters' client components into the importMap via admin.dependencies\n * (registry strings are not scanned by generate:importmap).\n */\nexport const registerIcon = (config: Config, icon: IconGlobalConfig | undefined): void => {\n\tif (!icon?.adapters) {\n\t\treturn\n\t}\n\tconst [firstAdapter] = icon.adapters\n\tif (!firstAdapter) {\n\t\treturn\n\t}\n\tconst slugs = new Set<string>()\n\tfor (const adapter of icon.adapters) {\n\t\tif (!ADAPTER_SLUG.test(adapter.slug)) {\n\t\t\tthrow new Error(\n\t\t\t\t`[fields] invalid icon adapter slug: \"${adapter.slug}\" (expected kebab-case, e.g. \"my-icons\")`\n\t\t\t)\n\t\t}\n\t\tif (slugs.has(adapter.slug)) {\n\t\t\tthrow new Error(`[fields] duplicate icon adapter slug: ${adapter.slug}`)\n\t\t}\n\t\tslugs.add(adapter.slug)\n\t\t// Layer ids key the manifest cache, so a duplicate would make two layers share\n\t\t// one cached listing and silently serve the wrong one.\n\t\tconst layerIds = new Set<string>()\n\t\tfor (const layer of adapter.layers ?? []) {\n\t\t\tif (layerIds.has(layer.id)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`[fields] duplicate icon layer id \"${layer.id}\" in adapter \"${adapter.slug}\"`\n\t\t\t\t)\n\t\t\t}\n\t\t\tlayerIds.add(layer.id)\n\t\t}\n\t}\n\tconst defaultLibrary = icon.defaultLibrary ?? firstAdapter.slug\n\tif (!slugs.has(defaultLibrary)) {\n\t\tthrow new Error(`[fields] defaultLibrary \"${defaultLibrary}\" is not a registered icon adapter`)\n\t}\n\tconfig.custom ??= {}\n\tconst registry = (config.custom[FIELDS_REGISTRY_KEY] ?? {}) as FieldsPluginRegistry\n\tregistry.icon = {\n\t\tadapters: icon.adapters,\n\t\talwaysAvailable: icon.alwaysAvailable,\n\t\tdefaultLibrary,\n\t\tresolveAvailable: icon.resolveAvailable,\n\t}\n\tconfig.custom[FIELDS_REGISTRY_KEY] = registry\n\t// The endpoint only exists for libraries whose data lives on the server. A config with\n\t// none stays byte-identical, which is every config written before layers existed.\n\tconst hasServerLayer = icon.adapters.some((adapter) =>\n\t\tadapter.layers?.some((layer) => layer.render.type !== 'component' || layer.resolveMeta)\n\t)\n\tif (hasServerLayer) {\n\t\tconfig.endpoints = [\n\t\t\t...(config.endpoints ?? []),\n\t\t\t{\n\t\t\t\thandler: createIconManifestHandler({\n\t\t\t\t\tadapters: icon.adapters,\n\t\t\t\t\talwaysAvailable: icon.alwaysAvailable,\n\t\t\t\t\tresolveAvailable: icon.resolveAvailable,\n\t\t\t\t}),\n\t\t\t\tmethod: 'get',\n\t\t\t\tpath: ICON_MANIFEST_PATH,\n\t\t\t},\n\t\t]\n\t}\n\tconfig.admin ??= {}\n\tconfig.admin.dependencies ??= {}\n\tfor (const adapter of icon.adapters) {\n\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Icon`] = {\n\t\t\tpath: adapter.Icon,\n\t\t\ttype: 'component',\n\t\t}\n\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Assets`] = {\n\t\t\tpath: adapter.Assets,\n\t\t\ttype: 'component',\n\t\t}\n\t\t// generate:importmap scans admin.dependencies, not registry strings, so the\n\t\t// optional Nodes loader must be registered here too or a regeneration drops\n\t\t// it and the drawer silently loses the bulk node-data fast path.\n\t\tif (adapter.Nodes) {\n\t\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-Nodes`] = {\n\t\t\t\tpath: adapter.Nodes,\n\t\t\t\ttype: 'component',\n\t\t\t}\n\t\t}\n\t\t// Each layer's render strategy points at its own importMap entry. Registered here\n\t\t// for the same reason Nodes is: generate:importmap scans admin.dependencies, not\n\t\t// registry strings, so a regeneration would otherwise drop them.\n\t\tfor (const layer of adapter.layers ?? []) {\n\t\t\tconfig.admin.dependencies[`fields-icon-${adapter.slug}-${layer.id}`] = renderDependency(\n\t\t\t\tlayer.render\n\t\t\t)\n\t\t}\n\t}\n}\n"],"mappings":";;;;AAMA,MAAM,oBACL,WAEA,OAAO,SAAS,cACb;CAAE,MAAM,OAAO;CAAM,MAAM;AAAY,IACvC;CAAE,MAAM,OAAO,SAAS,QAAQ,OAAO,UAAU,OAAO;CAAM,MAAM;AAAW;;;;;;AAOnF,MAAM,eAAe;;;;;;AAOrB,MAAa,gBAAgB,QAAgB,SAA6C;CACzF,IAAI,CAAC,MAAM,UACV;CAED,MAAM,CAAC,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pC,OAAO,MAAM,aAAa,eAAe,QAAQ,KAAK,UAAU;GAC/D,MAAM,QAAQ;GACd,MAAM;EACP;EACA,OAAO,MAAM,aAAa,eAAe,QAAQ,KAAK,YAAY;GACjE,MAAM,QAAQ;GACd,MAAM;EACP;EAIA,IAAI,QAAQ,OACX,OAAO,MAAM,aAAa,eAAe,QAAQ,KAAK,WAAW;GAChE,MAAM,QAAQ;GACd,MAAM;EACP;EAKD,KAAK,MAAM,SAAS,QAAQ,UAAU,CAAC,GACtC,OAAO,MAAM,aAAa,eAAe,QAAQ,KAAK,GAAG,MAAM,QAAQ,iBACtE,MAAM,MACP;CAEF;AACD"}
|
|
@@ -5,6 +5,11 @@ import React from "react";
|
|
|
5
5
|
/**
|
|
6
6
|
* Client dispatcher: per-value React.lazy components cached module-wide so a
|
|
7
7
|
* value renders one stable lazy component (per-icon chunks, loaded once).
|
|
8
|
+
*
|
|
9
|
+
* A rejected load stays cached, because React.lazy caches its own rejection.
|
|
10
|
+
* That is deliberate: retrying would re-request a failing icon on every render
|
|
11
|
+
* against an endpoint already struggling. `createRscIcon` evicts instead, because
|
|
12
|
+
* a server render has no equivalent re-render pressure.
|
|
8
13
|
*/
|
|
9
14
|
declare const createIcon: (args: {
|
|
10
15
|
adapters: IconRendererAdapter[];
|