@aphexcms/plugin-phosphor-icons 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +51 -2
- package/dist/Icon.svelte +34 -0
- package/dist/Icon.svelte.d.ts +9 -0
- package/dist/Icon.svelte.d.ts.map +1 -0
- package/dist/IconInput.svelte +279 -0
- package/dist/IconInput.svelte.d.ts +5 -0
- package/dist/IconInput.svelte.d.ts.map +1 -0
- package/dist/app.d.ts +24 -0
- package/dist/constants.d.ts +10 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +17 -0
- package/dist/data/bold.d.ts +3 -0
- package/dist/data/bold.d.ts.map +1 -0
- package/dist/data/bold.js +4 -0
- package/dist/data/catalog.d.ts +3 -0
- package/dist/data/catalog.d.ts.map +1 -0
- package/dist/data/catalog.js +4 -0
- package/dist/data/duotone.d.ts +3 -0
- package/dist/data/duotone.d.ts.map +1 -0
- package/dist/data/duotone.js +4 -0
- package/dist/data/fill.d.ts +3 -0
- package/dist/data/fill.d.ts.map +1 -0
- package/dist/data/fill.js +4 -0
- package/dist/data/light.d.ts +3 -0
- package/dist/data/light.d.ts.map +1 -0
- package/dist/data/light.js +4 -0
- package/dist/data/regular.d.ts +3 -0
- package/dist/data/regular.d.ts.map +1 -0
- package/dist/data/regular.js +4 -0
- package/dist/data/thin.d.ts +3 -0
- package/dist/data/thin.d.ts.map +1 -0
- package/dist/data/thin.js +4 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +65 -0
- package/dist/lookup.d.ts +33 -0
- package/dist/lookup.d.ts.map +1 -0
- package/dist/lookup.js +88 -0
- package/dist/schema.d.ts +44 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +130 -0
- package/dist/value.d.ts +22 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +14 -0
- package/package.json +72 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ICON_INPUT, ICON_TYPE, ICON_WEIGHTS } from './constants.js';
|
|
2
|
+
export type { IconWeight } from './constants.js';
|
|
3
|
+
export type { IconField, IconValue } from './schema.js';
|
|
4
|
+
export { default as Icon } from './Icon.svelte';
|
|
5
|
+
export declare function phosphorIconsPlugin(): import("@aphexcms/cms-core").CMSPlugin;
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AA6BA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACrE,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,MAAM,eAAe,CAAC;AAOhD,wBAAgB,mBAAmB,2CAgClC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phosphor icons for AphexCMS: an `icon` field with a searchable picker of
|
|
3
|
+
* Phosphor's 1,512 icons in six weights, and a component that draws the stored value.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* // aphex.config.ts
|
|
7
|
+
* import { phosphorIconsPlugin } from '@aphexcms/plugin-phosphor-icons';
|
|
8
|
+
* plugins: [phosphorIconsPlugin()]
|
|
9
|
+
*
|
|
10
|
+
* // a schema
|
|
11
|
+
* { name: 'icon', type: 'icon', title: 'Icon' }
|
|
12
|
+
*
|
|
13
|
+
* // the site
|
|
14
|
+
* import Icon from '@aphexcms/plugin-phosphor-icons/icon';
|
|
15
|
+
* <Icon value={item.icon} class="size-6" />
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The drawing is saved with the choice, so the site loads no icon set. The admin
|
|
19
|
+
* loads one weight's drawings when the picker opens. Saving fills a drawing in from
|
|
20
|
+
* the name, so the site's AI writes `{ name: 'coffee' }` and `search_icons` finds names.
|
|
21
|
+
*/
|
|
22
|
+
import { definePlugin } from '@aphexcms/cms-core';
|
|
23
|
+
import { z } from 'zod';
|
|
24
|
+
import IconInput from './IconInput.svelte';
|
|
25
|
+
import { ICON_INPUT, ICON_WEIGHTS } from './constants.js';
|
|
26
|
+
import { searchIcons } from './lookup.js';
|
|
27
|
+
import { expandIconTypes } from './schema.js';
|
|
28
|
+
export { ICON_INPUT, ICON_TYPE, ICON_WEIGHTS } from './constants.js';
|
|
29
|
+
export { default as Icon } from './Icon.svelte';
|
|
30
|
+
const searchInput = z.object({
|
|
31
|
+
query: z.string().min(1).describe('What the icon should show, like "phone" or "gluten free".'),
|
|
32
|
+
limit: z.number().int().min(1).max(60).optional()
|
|
33
|
+
});
|
|
34
|
+
export function phosphorIconsPlugin() {
|
|
35
|
+
const parts = [
|
|
36
|
+
{ implements: 'aphex/field/component', input: ICON_INPUT, component: IconInput },
|
|
37
|
+
{ implements: 'aphex/schema/transform', transform: expandIconTypes },
|
|
38
|
+
{
|
|
39
|
+
implements: 'aphex/agent/instructions',
|
|
40
|
+
id: 'phosphor-icons',
|
|
41
|
+
title: 'Icons',
|
|
42
|
+
text: [
|
|
43
|
+
'An icon field holds a Phosphor icon by name. Write only its name and, if you like, a weight: { "name": "coffee", "weight": "regular" }. Saving fills in the drawing.',
|
|
44
|
+
`Weights: ${ICON_WEIGHTS.join(', ')}. Keep one weight across a page.`,
|
|
45
|
+
'Find names with search_icons (by meaning: "haircut", "delivery", "open hours"). Never guess a name; one Phosphor lacks is kept but draws nothing.'
|
|
46
|
+
].join('\n')
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
implements: 'aphex/agent/tool',
|
|
50
|
+
definition: {
|
|
51
|
+
name: 'search_icons',
|
|
52
|
+
description: 'Find Phosphor icon names for an icon field, by name or meaning ("cafe" finds coffee). Best matches first.',
|
|
53
|
+
mutates: false,
|
|
54
|
+
requiredCapabilities: ['document.read'],
|
|
55
|
+
execution: 'server',
|
|
56
|
+
inputSchema: searchInput
|
|
57
|
+
},
|
|
58
|
+
execute: async (input) => {
|
|
59
|
+
const { query, limit } = searchInput.parse(input);
|
|
60
|
+
return { success: true, data: await searchIcons(query, limit ?? 20) };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
return definePlugin({ name: '@aphexcms/plugin-phosphor-icons', version: '0.1.0', parts });
|
|
65
|
+
}
|
package/dist/lookup.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Icon data, loaded on demand: one module per weight (about 200 KB compressed) and
|
|
3
|
+
* a catalog of names and search words. Nothing here loads until it's asked for, so
|
|
4
|
+
* importing the plugin costs a site nothing.
|
|
5
|
+
*/
|
|
6
|
+
import { type IconWeight } from './constants.js';
|
|
7
|
+
import type { IconValue } from './value.js';
|
|
8
|
+
type Paths = Record<string, string>;
|
|
9
|
+
type Duotone = Record<string, [string, string]>;
|
|
10
|
+
/** The drawing for a name in a weight, or null when Phosphor has no such icon. */
|
|
11
|
+
export declare function iconPaths(name: string, weight?: IconWeight): Promise<{
|
|
12
|
+
path: string;
|
|
13
|
+
shade?: string;
|
|
14
|
+
} | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Every icon's drawing in one weight, for the picker's grid. Two-tone entries are
|
|
17
|
+
* `[path, shade]`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function allIcons(weight: IconWeight): Promise<Paths | Duotone>;
|
|
20
|
+
/**
|
|
21
|
+
* A stored or written value with its drawing filled in from its name and weight,
|
|
22
|
+
* or null when the name isn't a Phosphor icon. The drawing always follows the
|
|
23
|
+
* name, so a value can't claim one icon and draw another.
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveIcon(value: IconValue): Promise<IconValue | null>;
|
|
26
|
+
/**
|
|
27
|
+
* Icons matching a search, best first: a name that is the query, then names that
|
|
28
|
+
* start with it, then names containing it, then icons whose search words do
|
|
29
|
+
* (`cafe` finds `coffee`). Every word of the query has to match.
|
|
30
|
+
*/
|
|
31
|
+
export declare function searchIcons(query: string, limit?: number): Promise<string[]>;
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=lookup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lookup.d.ts","sourceRoot":"","sources":["../src/lib/lookup.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,KAAK,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACpC,KAAK,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAwBhD,kFAAkF;AAClF,wBAAsB,SAAS,CAC9B,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,UAAsB,GAC5B,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAKlD;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,CAErE;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAQ7E;AAID;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAoB9E"}
|
package/dist/lookup.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Icon data, loaded on demand: one module per weight (about 200 KB compressed) and
|
|
3
|
+
* a catalog of names and search words. Nothing here loads until it's asked for, so
|
|
4
|
+
* importing the plugin costs a site nothing.
|
|
5
|
+
*/
|
|
6
|
+
import { isIconWeight } from './constants.js';
|
|
7
|
+
const loaders = {
|
|
8
|
+
regular: () => import('./data/regular.js'),
|
|
9
|
+
bold: () => import('./data/bold.js'),
|
|
10
|
+
fill: () => import('./data/fill.js'),
|
|
11
|
+
light: () => import('./data/light.js'),
|
|
12
|
+
thin: () => import('./data/thin.js')
|
|
13
|
+
};
|
|
14
|
+
const cache = new Map();
|
|
15
|
+
function weightData(weight) {
|
|
16
|
+
let data = cache.get(weight);
|
|
17
|
+
if (!data) {
|
|
18
|
+
data =
|
|
19
|
+
weight === 'duotone'
|
|
20
|
+
? import('./data/duotone.js').then((module) => module.default)
|
|
21
|
+
: loaders[weight]().then((module) => module.default);
|
|
22
|
+
cache.set(weight, data);
|
|
23
|
+
}
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
/** The drawing for a name in a weight, or null when Phosphor has no such icon. */
|
|
27
|
+
export async function iconPaths(name, weight = 'regular') {
|
|
28
|
+
const data = await weightData(weight);
|
|
29
|
+
const entry = data[name];
|
|
30
|
+
if (entry === undefined)
|
|
31
|
+
return null;
|
|
32
|
+
return typeof entry === 'string' ? { path: entry } : { path: entry[0], shade: entry[1] };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Every icon's drawing in one weight, for the picker's grid. Two-tone entries are
|
|
36
|
+
* `[path, shade]`.
|
|
37
|
+
*/
|
|
38
|
+
export function allIcons(weight) {
|
|
39
|
+
return weightData(weight);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A stored or written value with its drawing filled in from its name and weight,
|
|
43
|
+
* or null when the name isn't a Phosphor icon. The drawing always follows the
|
|
44
|
+
* name, so a value can't claim one icon and draw another.
|
|
45
|
+
*/
|
|
46
|
+
export async function resolveIcon(value) {
|
|
47
|
+
const name = value.name.trim().toLowerCase();
|
|
48
|
+
const weight = isIconWeight(value.weight) ? value.weight : 'regular';
|
|
49
|
+
const drawing = await iconPaths(name, weight);
|
|
50
|
+
if (!drawing)
|
|
51
|
+
return null;
|
|
52
|
+
return drawing.shade
|
|
53
|
+
? { name, weight, path: drawing.path, shade: drawing.shade }
|
|
54
|
+
: { name, weight, path: drawing.path };
|
|
55
|
+
}
|
|
56
|
+
let catalog;
|
|
57
|
+
/**
|
|
58
|
+
* Icons matching a search, best first: a name that is the query, then names that
|
|
59
|
+
* start with it, then names containing it, then icons whose search words do
|
|
60
|
+
* (`cafe` finds `coffee`). Every word of the query has to match.
|
|
61
|
+
*/
|
|
62
|
+
export async function searchIcons(query, limit = 60) {
|
|
63
|
+
catalog ??= import('./data/catalog.js').then((module) => module.default);
|
|
64
|
+
const list = await catalog;
|
|
65
|
+
const words = query.toLowerCase().trim().split(/\s+/).filter(Boolean);
|
|
66
|
+
if (!words.length)
|
|
67
|
+
return list.slice(0, limit).map(([name]) => name);
|
|
68
|
+
const joined = words.join('-');
|
|
69
|
+
const scored = [];
|
|
70
|
+
for (const [name, tags] of list) {
|
|
71
|
+
let score;
|
|
72
|
+
if (name === joined)
|
|
73
|
+
score = 0;
|
|
74
|
+
else if (name.startsWith(joined))
|
|
75
|
+
score = 1;
|
|
76
|
+
else if (name.includes(joined))
|
|
77
|
+
score = 2;
|
|
78
|
+
else if (words.every((word) => name.includes(word) || tags.includes(word)))
|
|
79
|
+
score = 3;
|
|
80
|
+
else
|
|
81
|
+
continue;
|
|
82
|
+
scored.push([name, score]);
|
|
83
|
+
}
|
|
84
|
+
return scored
|
|
85
|
+
.sort((a, b) => a[1] - b[1] || a[0].length - b[0].length || a[0].localeCompare(b[0]))
|
|
86
|
+
.slice(0, limit)
|
|
87
|
+
.map(([name]) => name);
|
|
88
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema surface for the icon field.
|
|
3
|
+
*
|
|
4
|
+
* `{ type: 'icon', name: 'icon' }`, or `icon({ name: 'icon' })`, is an `object`
|
|
5
|
+
* under the hood storing `{ name, weight, path, shade }` (see `IconValue`), so a site
|
|
6
|
+
* draws the icon from the document alone, with no icon set in its bundle.
|
|
7
|
+
*
|
|
8
|
+
* Saving fills the drawing in from the name: the transform gives every document
|
|
9
|
+
* type holding an icon field a `beforeValidate` hook that looks it up. So an AI or
|
|
10
|
+
* an import can write `{ name: 'coffee' }` and get the same value the picker saves.
|
|
11
|
+
*/
|
|
12
|
+
import type { BaseField, DocumentHook, ObjectField, SchemaType } from '@aphexcms/cms-core';
|
|
13
|
+
import { type IconWeight } from './constants.js';
|
|
14
|
+
export type { IconValue } from './value.js';
|
|
15
|
+
export type { IconWeight } from './constants.js';
|
|
16
|
+
/** The authored shape of a `{ type: 'icon' }` field. */
|
|
17
|
+
export interface IconField extends BaseField {
|
|
18
|
+
type: 'icon';
|
|
19
|
+
/** The weight a new pick starts in. Default: `'regular'`. */
|
|
20
|
+
weight?: IconWeight;
|
|
21
|
+
/** Weights the editor may choose from. Default: all six. */
|
|
22
|
+
weights?: readonly IconWeight[];
|
|
23
|
+
}
|
|
24
|
+
declare module '@aphexcms/cms-core' {
|
|
25
|
+
interface FieldTypeMap {
|
|
26
|
+
icon: IconField;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export interface IconFieldConfig extends Partial<Omit<BaseField, 'type' | 'name' | 'input' | 'inputOptions'>> {
|
|
30
|
+
name: string;
|
|
31
|
+
weight?: IconWeight;
|
|
32
|
+
weights?: readonly IconWeight[];
|
|
33
|
+
}
|
|
34
|
+
/** Build an icon `object` field, edited by the Phosphor picker. */
|
|
35
|
+
export declare function icon(config: IconFieldConfig): ObjectField;
|
|
36
|
+
type Types = Map<string, SchemaType>;
|
|
37
|
+
/** The hook a document type holding icons gets: fills each icon's drawing in from its name. */
|
|
38
|
+
export declare function fillIconsHook(schema: SchemaType, types: Types): DocumentHook;
|
|
39
|
+
/**
|
|
40
|
+
* Schema-transform: desugar `{ type: 'icon' }` into the icon `object`, then give every
|
|
41
|
+
* document type that holds one, anywhere in its fields, the hook that fills drawings in.
|
|
42
|
+
*/
|
|
43
|
+
export declare function expandIconTypes(schemas: SchemaType[]): SchemaType[];
|
|
44
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/lib/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EACX,SAAS,EACT,YAAY,EAEZ,WAAW,EACX,UAAU,EAEV,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAKN,KAAK,UAAU,EACf,MAAM,gBAAgB,CAAC;AAIxB,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAC5C,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,wDAAwD;AACxD,MAAM,WAAW,SAAU,SAAQ,SAAS;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,4DAA4D;IAC5D,OAAO,CAAC,EAAE,SAAS,UAAU,EAAE,CAAC;CAChC;AAED,OAAO,QAAQ,oBAAoB,CAAC;IACnC,UAAU,YAAY;QACrB,IAAI,EAAE,SAAS,CAAC;KAChB;CACD;AAED,MAAM,WAAW,eAAgB,SAAQ,OAAO,CAC/C,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,cAAc,CAAC,CAC3D;IACA,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,OAAO,CAAC,EAAE,SAAS,UAAU,EAAE,CAAC;CAChC;AAED,mEAAmE;AACnE,wBAAgB,IAAI,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW,CAuBzD;AAUD,KAAK,KAAK,GAAG,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAmErC,+FAA+F;AAC/F,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,GAAG,YAAY,CAI5E;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,CAmBnE"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { desugarFieldType } from '@aphexcms/cms-core';
|
|
2
|
+
import { ICON_INPUT, ICON_TYPE, ICON_WEIGHTS, WEIGHT_TITLES } from './constants.js';
|
|
3
|
+
import { resolveIcon } from './lookup.js';
|
|
4
|
+
import { isIconValue } from './value.js';
|
|
5
|
+
/** Build an icon `object` field, edited by the Phosphor picker. */
|
|
6
|
+
export function icon(config) {
|
|
7
|
+
const { name, title, weight, weights, ...rest } = config;
|
|
8
|
+
return {
|
|
9
|
+
...rest,
|
|
10
|
+
name,
|
|
11
|
+
type: 'object',
|
|
12
|
+
title: title ?? 'Icon',
|
|
13
|
+
input: ICON_INPUT,
|
|
14
|
+
inputOptions: { weight: weight ?? 'regular', ...(weights ? { weights: [...weights] } : {}) },
|
|
15
|
+
// Path data must never carry preview markers, and nothing in it is text to click.
|
|
16
|
+
stega: false,
|
|
17
|
+
fields: [
|
|
18
|
+
{ name: 'name', type: 'string', title: 'Name' },
|
|
19
|
+
{
|
|
20
|
+
name: 'weight',
|
|
21
|
+
type: 'string',
|
|
22
|
+
title: 'Weight',
|
|
23
|
+
list: ICON_WEIGHTS.map((value) => ({ title: WEIGHT_TITLES[value], value }))
|
|
24
|
+
},
|
|
25
|
+
{ name: 'path', type: 'string', title: 'Drawing' },
|
|
26
|
+
{ name: 'shade', type: 'string', title: 'Shade' }
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function isIconField(field) {
|
|
31
|
+
return field.type === 'object' && field.input === ICON_INPUT;
|
|
32
|
+
}
|
|
33
|
+
function isRecord(value) {
|
|
34
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
35
|
+
}
|
|
36
|
+
/** The fields an array member holds: its own, or those of the object type it names. */
|
|
37
|
+
function memberFields(member, types) {
|
|
38
|
+
return member.fields ?? types.get(member.type)?.fields;
|
|
39
|
+
}
|
|
40
|
+
function holdsIcons(fields, types, seen) {
|
|
41
|
+
if (!fields)
|
|
42
|
+
return false;
|
|
43
|
+
return fields.some((field) => {
|
|
44
|
+
if (isIconField(field))
|
|
45
|
+
return true;
|
|
46
|
+
if (field.type === 'object')
|
|
47
|
+
return holdsIcons(field.fields, types, seen);
|
|
48
|
+
if (field.type === 'array') {
|
|
49
|
+
return field.of.some((member) => {
|
|
50
|
+
if (isIconField(member))
|
|
51
|
+
return true;
|
|
52
|
+
// A named object type can contain itself; look at each one once.
|
|
53
|
+
if (!member.fields) {
|
|
54
|
+
if (seen.has(member.type))
|
|
55
|
+
return false;
|
|
56
|
+
seen.add(member.type);
|
|
57
|
+
}
|
|
58
|
+
return holdsIcons(memberFields(member, types), types, seen);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function fillValue(value) {
|
|
65
|
+
if (!isIconValue(value))
|
|
66
|
+
return value;
|
|
67
|
+
// An unknown name is kept as written: nothing is lost, and the site draws nothing.
|
|
68
|
+
return (await resolveIcon(value)) ?? value;
|
|
69
|
+
}
|
|
70
|
+
async function fillFields(fields, data, types) {
|
|
71
|
+
const out = { ...data };
|
|
72
|
+
for (const field of fields) {
|
|
73
|
+
const value = data[field.name];
|
|
74
|
+
if (value === undefined || value === null)
|
|
75
|
+
continue;
|
|
76
|
+
if (isIconField(field))
|
|
77
|
+
out[field.name] = await fillValue(value);
|
|
78
|
+
else if (field.type === 'object' && isRecord(value))
|
|
79
|
+
out[field.name] = await fillFields(field.fields, value, types);
|
|
80
|
+
else if (field.type === 'array' && Array.isArray(value)) {
|
|
81
|
+
out[field.name] = await Promise.all(value.map(async (item) => {
|
|
82
|
+
if (!isRecord(item))
|
|
83
|
+
return item;
|
|
84
|
+
// One member, or the one this item's `_type` names.
|
|
85
|
+
const member = field.of.length === 1
|
|
86
|
+
? field.of[0]
|
|
87
|
+
: field.of.find((entry) => (entry.name ?? entry.type) === item._type);
|
|
88
|
+
if (!member)
|
|
89
|
+
return item;
|
|
90
|
+
if (isIconField(member))
|
|
91
|
+
return fillValue(item);
|
|
92
|
+
const nested = memberFields(member, types);
|
|
93
|
+
return nested ? fillFields(nested, item, types) : item;
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
const ourHooks = new WeakSet();
|
|
100
|
+
/** The hook a document type holding icons gets: fills each icon's drawing in from its name. */
|
|
101
|
+
export function fillIconsHook(schema, types) {
|
|
102
|
+
const hook = ({ data }) => fillFields(schema.fields, data, types);
|
|
103
|
+
ourHooks.add(hook);
|
|
104
|
+
return hook;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Schema-transform: desugar `{ type: 'icon' }` into the icon `object`, then give every
|
|
108
|
+
* document type that holds one, anywhere in its fields, the hook that fills drawings in.
|
|
109
|
+
*/
|
|
110
|
+
export function expandIconTypes(schemas) {
|
|
111
|
+
const expanded = desugarFieldType(schemas, {
|
|
112
|
+
type: ICON_TYPE,
|
|
113
|
+
sugarKeys: ['weight', 'weights'],
|
|
114
|
+
build: (field) => {
|
|
115
|
+
const authored = field.type === ICON_TYPE ? field : {};
|
|
116
|
+
return icon({ name: field.name, weight: authored.weight, weights: authored.weights });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
const types = new Map(expanded.map((schema) => [schema.name, schema]));
|
|
120
|
+
return expanded.map((schema) => {
|
|
121
|
+
if (schema.type !== 'document' || !holdsIcons(schema.fields, types, new Set()))
|
|
122
|
+
return schema;
|
|
123
|
+
// Run twice over the same list (a schema reload), a type keeps one hook.
|
|
124
|
+
const others = (schema.hooks?.beforeValidate ?? []).filter((hook) => !ourHooks.has(hook));
|
|
125
|
+
return {
|
|
126
|
+
...schema,
|
|
127
|
+
hooks: { ...schema.hooks, beforeValidate: [...others, fillIconsHook(schema, types)] }
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
}
|
package/dist/value.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IconWeight } from './constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* What an icon field stores. `path` and `shade` are the drawing itself, saved with
|
|
4
|
+
* the choice so a site draws it with no icon set loaded: the name alone is enough to
|
|
5
|
+
* write one (an AI, an import), and saving fills the drawing in.
|
|
6
|
+
*/
|
|
7
|
+
export interface IconValue {
|
|
8
|
+
/** Phosphor's name, like `coffee` or `map-pin`. */
|
|
9
|
+
name: string;
|
|
10
|
+
weight?: IconWeight;
|
|
11
|
+
/** The icon's path data (`d`), in a 256 × 256 box. */
|
|
12
|
+
path?: string;
|
|
13
|
+
/** Two-tone only: the 20% layer behind it. */
|
|
14
|
+
shade?: string;
|
|
15
|
+
}
|
|
16
|
+
/** An icon value with something to draw. */
|
|
17
|
+
export declare function isDrawableIcon(value: unknown): value is IconValue & {
|
|
18
|
+
path: string;
|
|
19
|
+
};
|
|
20
|
+
/** Any value naming an icon, drawn or not. */
|
|
21
|
+
export declare function isIconValue(value: unknown): value is IconValue;
|
|
22
|
+
//# sourceMappingURL=value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value.d.ts","sourceRoot":"","sources":["../src/lib/value.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACzB,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8CAA8C;IAC9C,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAMD,4CAA4C;AAC5C,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAOpF;AAED,8CAA8C;AAC9C,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D"}
|
package/dist/value.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function isRecord(value) {
|
|
2
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
/** An icon value with something to draw. */
|
|
5
|
+
export function isDrawableIcon(value) {
|
|
6
|
+
return (isRecord(value) &&
|
|
7
|
+
typeof value.name === 'string' &&
|
|
8
|
+
typeof value.path === 'string' &&
|
|
9
|
+
value.path !== '');
|
|
10
|
+
}
|
|
11
|
+
/** Any value naming an icon, drawn or not. */
|
|
12
|
+
export function isIconValue(value) {
|
|
13
|
+
return isRecord(value) && typeof value.name === 'string' && value.name !== '';
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aphexcms/plugin-phosphor-icons",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Phosphor icons for AphexCMS: an icon field with a searchable picker of 1,512 icons in six weights, and a component that draws the stored icon with no icon set loaded",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/IcelandicIcecream/aphex.git",
|
|
12
|
+
"directory": "plugins/plugin-phosphor-icons"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"svelte": "./dist/index.js",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"svelte": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./schema": {
|
|
25
|
+
"types": "./dist/schema.d.ts",
|
|
26
|
+
"default": "./dist/schema.js"
|
|
27
|
+
},
|
|
28
|
+
"./icon": {
|
|
29
|
+
"types": "./dist/Icon.svelte.d.ts",
|
|
30
|
+
"svelte": "./dist/Icon.svelte",
|
|
31
|
+
"default": "./dist/Icon.svelte"
|
|
32
|
+
},
|
|
33
|
+
"./lookup": {
|
|
34
|
+
"types": "./dist/lookup.d.ts",
|
|
35
|
+
"default": "./dist/lookup.js"
|
|
36
|
+
},
|
|
37
|
+
"./value": {
|
|
38
|
+
"types": "./dist/value.d.ts",
|
|
39
|
+
"default": "./dist/value.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist"
|
|
44
|
+
],
|
|
45
|
+
"keywords": [
|
|
46
|
+
"aphex",
|
|
47
|
+
"aphexcms",
|
|
48
|
+
"cms",
|
|
49
|
+
"plugin",
|
|
50
|
+
"icons",
|
|
51
|
+
"phosphor"
|
|
52
|
+
],
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@sveltejs/package": "^2.5.6",
|
|
55
|
+
"svelte": "^5.0.0",
|
|
56
|
+
"svelte-check": "^4.0.0",
|
|
57
|
+
"typescript": "^5.9.3",
|
|
58
|
+
"vitest": "^4.1.10"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"@aphexcms/cms-core": ">=12.0.0-0",
|
|
62
|
+
"@lucide/svelte": "^0.554.0",
|
|
63
|
+
"svelte": "^5.0.0",
|
|
64
|
+
"zod": "^4.0.0",
|
|
65
|
+
"@aphexcms/ui": "^0.8.7"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"dev": "tsc --watch --preserveWatchOutput",
|
|
69
|
+
"build": "svelte-package",
|
|
70
|
+
"check": "svelte-check --tsconfig ./tsconfig.json",
|
|
71
|
+
"dev:local": "node ../../scripts/swap-package-paths.js ./package.json src lib",
|
|
72
|
+
"test": "vitest run",
|
|
73
|
+
"generate": "node scripts/generate.mjs"
|
|
74
|
+
}
|
|
6
75
|
}
|