diff --git a/README.md b/README.md index 9b427417..6aab3a24 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ npm test `npm run build:docs` builds no kit, and needs `npm run build:components` first, as [the docs site](documents/DOCS-SITE.md#commands) says. The kit's build builds it each time: it compiles it, packs it into `_packages/` as `noldova-ui.components.angular-.tgz`, at the version `version.json` holds, and installs the archive at the root without saving it. It installs the npm packages first when `node_modules` is missing, and the browser for the Angular tests. -It writes into the package `glyphs.json`, the glyphs each exported part can draw, which it reads from the kit's source as the [Part glyphs](#part-glyphs) check does, and which [KIT.md](documents/KIT.md#icons-a-site-subsets) describes; it compares the packed kit's `glyphs.json` with the file it wrote, byte for byte. +It writes into the package `glyphs.json`, the glyphs each exported part can draw and each exported model holds, which it reads from the kit's source as the [Part glyphs](#part-glyphs) check does, and which [KIT.md](documents/KIT.md#icons-a-site-subsets) describes; it compares the packed kit's `glyphs.json` with the file it wrote, byte for byte. Each archive's `package.json` records the commit it was built from as `gitHead` when the clone has no changed tracked files and no untracked files that Git doesn't ignore, and leaves it out otherwise. The kit's build also refuses an entry other than the testing entry that reaches `src/testing/` or Vitest. `npm run docs:build -w src/noldova.ui.components.angular.docs` builds the docs site into `_build/docs` from the kit's source, `npm run docs:test -w src/noldova.ui.components.angular.docs` runs its tests, and `npm run docs:api -w src/noldova.ui.components.angular.docs` writes only its API reference; all three need `npm run build:components` first, as [the docs site](documents/DOCS-SITE.md) says. @@ -141,8 +141,9 @@ It reads one line at a time: it refuses an assertion that a line break splits fr The part glyphs check, `src/noldova.ui.components.angular/scripts/checks/part-glyphs-check.ts`, reads the glyphs each part the kit's entries export can draw, as the build writes them to the package's `glyphs.json`, with TypeScript's type checker over every source file under `src/noldova.ui.components.angular/src` but the specs. A glyph is a string literal typed `IconName`, or, in a constant written `as const`, an icon name under a property whose name holds glyph or icon; an icon written between the symbol font's span tags in a template is one too, and so is an icon name a template gives an input, or model, that a part's selector places and types `IconName` or a list of them: the attribute's value, such as `icon="forum"`, or each quoted literal of a bound expression and each one between backticks with no interpolation, such as `[icon]="open() ? 'close' : 'menu'"` or ``[icon]="`forum`"``. A part's list holds the glyphs its own file gives, those of the files it imports, not only for types, that its code uses or its template names, the icons its template draws as written, and the same of every part it imports, which it shows. -The check fails on each glyph of the source that no part's list holds, naming its file and line, since a part could draw it and a site's subset of the font would leave it out. -It first reads a small source of its own, with a part that shows another, a resources class with a used and an unused glyph, a template's glyph, icons handed to an input as written, and a constant's, and fails when that reading differs from the one it expects. +Each other class the entries export, a model such as `LinkReferenceAction`, gets a list of the glyphs of its files its code uses, such as the glyph it defaults to, when it has any, under `models`, since a part draws what a model holds. +The check fails on each glyph of the source that no part's or model's list holds, naming its file and line, since a part could draw it and a site's subset of the font would leave it out. +It first reads a small source of its own, with a part that shows another, a resources class with a used and an unused glyph, a template's glyph, icons handed to an input as written, a constant's, a model that defaults to a glyph and one that holds none, and fails when that reading differs from the one it expects. A glyph a part takes as an input or a model field, such as `IconButtonComponent.icon`, is the site's own and in no list. ### Kit bundles diff --git a/documents/KIT.md b/documents/KIT.md index 14318d74..47dc2cd3 100644 --- a/documents/KIT.md +++ b/documents/KIT.md @@ -284,13 +284,13 @@ A compact row, as tall as an icon button, takes `--nol-icon-button`, 1.375rem, f `kit.css` loads the whole Material Symbols font, about 5.4 MB, so a site that draws a few dozen glyphs, such as a website or a documentation site, serves a subset of it instead, holding only the glyphs its pages and parts draw. The kit's build writes the glyphs its parts draw, from the kit's source, to the package's `glyphs.json`, exported as `@noldova/ui.components.angular/glyphs.json`, as the README's [Part glyphs](../README.md#part-glyphs) check reads them; nobody keeps it by hand. -It is `{ "parts": { "": ["", …], … } }`: for each part the package's entries export, by its class name, such as `SiteHeaderComponent`, the sorted names of the glyphs it can draw, those of the kit's parts it shows included. +It is `{ "parts": { "": ["", …], … }, "models": { … } }`: for each part the package's entries export, by its class name, such as `SiteHeaderComponent`, the sorted names of the glyphs it can draw, those of the kit's parts it shows included, and the same for each other class they export that holds glyphs, a model such as `LinkReferenceAction`, whose list holds the glyph it defaults to, `open_in_new`. A glyph the site passes to a part, such as an icon button's `icon` or a `SiteAction`'s, is in no list. The icon chooser's and the gallery's lists hold every name of the set, since they can draw any of them. A site gets its subset from the entry `@noldova/ui.components.angular/symbols`, a Node.js tool that loads in no browser: - Its `MaterialSymbols` takes where the site keeps its subset as a `MaterialSymbolsSite`, whose defaults a site overrides in a class of its own, and fails with a `MaterialSymbolsException`, a `UiComponentsException`. - It reads the union of the `glyphs.json` lists of the kit's parts the site's code imports, the icons its templates draw between the symbol class's span tags or give a part as `icon="…"`, the glyphs its code keeps in a field whose name ends in glyph or starts with glyph and a capital, such as `glyphShield`, as an `IconName` or a string, the same icons and glyphs of the other folders of code its pages show, its `moreCode`, and the names the site gives it for icons its code builds; a refresh, which the site runs as `npm run symbols`, downloads that subset from Google Fonts with its license, keeps it in the site's fonts folder named by its SHA-256, points the site's `@font-face` at it and records the address the site serves it at, its SHA-256 and its source in the site's license notice. + It reads the union of the `glyphs.json` lists of the kit's parts and models the site's code imports, the icons its templates draw between the symbol class's span tags or give a part as `icon="…"`, the glyphs its code keeps in a field whose name ends in glyph or starts with glyph and a capital, such as `glyphShield`, as an `IconName` or a string, the same icons and glyphs of the other folders of code its pages show, its `moreCode`, with the lists of the kit's models they import by any path, and the names the site gives it for icons its code builds; a refresh, which the site runs as `npm run symbols`, downloads that subset from Google Fonts with its license, keeps it in the site's fonts folder named by its SHA-256, points the site's `@font-face` at it and records the address the site serves it at, its SHA-256 and its source in the site's license notice. - Its check, `npm run symbols -- --check` in the site's checks, fails when the committed font, its record, its stylesheet or the glyphs the site draws disagree. - Its `drawnGlyphs` and `loadedFonts` page expressions and `findPageProblemsAsync` let the site's check of its built pages fail on a page that draws a glyph the subset lacks, or that loads another copy of the font, such as the whole font `kit.css` brings. diff --git a/src/noldova.ui.components.angular.docs/scripts/docs/docs-symbols.ts b/src/noldova.ui.components.angular.docs/scripts/docs/docs-symbols.ts index 26f19b38..7e90d72a 100644 --- a/src/noldova.ui.components.angular.docs/scripts/docs/docs-symbols.ts +++ b/src/noldova.ui.components.angular.docs/scripts/docs/docs-symbols.ts @@ -20,9 +20,9 @@ class DocsSymbolsSite extends MaterialSymbolsSite { public override readonly moreCode: readonly string[] = ["../noldova.ui.components.angular/src/app/components/gallery"]; public override readonly kitGlyphs: string; public override readonly wholeFontParts: readonly string[] = ["IconChooserComponent"]; - // The glyphs no reading finds: those the examples give a part's model by position, such as a tree node's or a quick input item's icon, the - // gallery's option icons, and a link reference action's default, which belongs to a model and so to no part's list. - public override readonly names: readonly string[] = ["book", "bookmark", "groups", "open_in_new", "person"]; + // The glyphs no reading finds: those the examples give a part's model by position, such as a tree node's or a quick input item's icon, and + // the gallery's option icons. + public override readonly names: readonly string[] = ["book", "bookmark", "groups", "person"]; public constructor(root: string, kitGlyphs: string) { super(root); @@ -40,7 +40,7 @@ export class DocsSymbols { public static async createAsync(root: string): Promise { const glyphs = path.join(root, DocsSymbols.GLYPHS_FILE); await mkdir(path.dirname(glyphs), { recursive: true }); - await writeFile(glyphs, PartGlyphs.render((await KitGlyphs.readAsync(root)).parts)); + await writeFile(glyphs, PartGlyphs.render(await KitGlyphs.readAsync(root))); const site = path.join(root, DocsSymbols.SITE_FOLDER); return new MaterialSymbols(new DocsSymbolsSite(site, path.relative(site, glyphs).split(path.sep).join(path.posix.sep))); } diff --git a/src/noldova.ui.components.angular/scripts/build.ts b/src/noldova.ui.components.angular/scripts/build.ts index 7e97a7b5..640d68fc 100644 --- a/src/noldova.ui.components.angular/scripts/build.ts +++ b/src/noldova.ui.components.angular/scripts/build.ts @@ -180,7 +180,7 @@ export class Build extends Script { const { KitGlyphs } = await import("./glyphs/kit-glyphs.ts"); const { PartGlyphs } = await import("./glyphs/part-glyphs.ts"); const file = path.join(output, KitGlyphs.FILE_NAME); - await this.writeFileAsync(file, PartGlyphs.render((await KitGlyphs.readAsync(Build.ROOT)).parts)); + await this.writeFileAsync(file, PartGlyphs.render(await KitGlyphs.readAsync(Build.ROOT))); return new PackageFile(file, KitGlyphs.FILE_NAME); } diff --git a/src/noldova.ui.components.angular/scripts/checks/part-glyphs-check.ts b/src/noldova.ui.components.angular/scripts/checks/part-glyphs-check.ts index 9ad32385..56beba54 100644 --- a/src/noldova.ui.components.angular/scripts/checks/part-glyphs-check.ts +++ b/src/noldova.ui.components.angular/scripts/checks/part-glyphs-check.ts @@ -18,8 +18,9 @@ import { KitGlyphs } from "../glyphs/kit-glyphs.ts"; import { PartGlyphs } from "../glyphs/part-glyphs.ts"; import type { PartGlyphsReading } from "../glyphs/part-glyphs-reading.ts"; -// Reads the glyphs each exported part can draw, as the build writes them to the package's glyphs.json, first from a small source of its own, then -// from the kit's, and fails when a glyph of the kit's source is in no part's list: a part would draw it and a site's subset of the font leave it out. +// Reads the glyphs each exported part can draw, and each exported model holds, as the build writes them to the package's glyphs.json, first from a +// small source of its own, then from the kit's, and fails when a glyph of the kit's source is in no list: a part would draw it and a site's subset +// of the font leave it out. export class PartGlyphsCheck implements ICheck { private static readonly OWN_FOLDER: string = "_build/part-glyphs-check"; private static readonly OWN_NAMES: readonly string[] = ["add", "close", "delete", "edit", "forum", "home", "menu", "refresh", "search", "share", "star", "sync", @@ -94,8 +95,26 @@ export class PartGlyphsCheck implements ICheck { "}", "" ].join("\n")], + ["action.ts", [ + "import { HeaderResources } from \"./header.resources.js\";", + "import type { IconName } from \"./icon-name.js\";", + "export class Action {", + " public readonly glyph: IconName;", + " public constructor(glyph: IconName = HeaderResources.searchGlyph) {", + " this.glyph = glyph;", + " }", + "}", + "" + ].join("\n")], + ["plain.ts", "export class Plain {\n public readonly label: string = \"add\";\n}\n"], ["unused.ts", "import type { IconName } from \"./icon-name.js\";\nexport const zoomGlyph: IconName = \"zoom_in\";\n"], - [PartGlyphsCheck.OWN_ENTRY, "export { HeaderComponent } from \"./header.component.js\";\nexport { PanelComponent } from \"./panel.component.js\";\n"] + [PartGlyphsCheck.OWN_ENTRY, [ + "export { Action } from \"./action.js\";", + "export { HeaderComponent } from \"./header.component.js\";", + "export { PanelComponent } from \"./panel.component.js\";", + "export { Plain } from \"./plain.js\";", + "" + ].join("\n")] ]); private static readonly OWN_LIST: string = [ "{", @@ -114,11 +133,16 @@ export class PartGlyphsCheck implements ICheck { " \"add\",", " \"star\"", " ]", + " },", + " \"models\": {", + " \"Action\": [", + " \"search\"", + " ]", " }", "}", "" ].join("\n"); - private static readonly OWN_ORPHANS: string = "header.resources.ts:4: search,unused.ts:2: zoom_in"; + private static readonly OWN_ORPHANS: string = "unused.ts:2: zoom_in"; private readonly root: string; @@ -130,18 +154,19 @@ export class PartGlyphsCheck implements ICheck { public async runAsync(output: Writable): Promise { const own = this.readOwn(); - const isOwnRight = PartGlyphs.render(own.parts) === PartGlyphsCheck.OWN_LIST && own.orphans.join(",") === PartGlyphsCheck.OWN_ORPHANS; + const isOwnRight = PartGlyphs.render(own) === PartGlyphsCheck.OWN_LIST && own.orphans.join(",") === PartGlyphsCheck.OWN_ORPHANS; if (!isOwnRight) - output.write(`The check's own test read ${JSON.stringify(PartGlyphs.render(own.parts))} with the glyphs in no list ${JSON.stringify(own.orphans)} ` + + output.write(`The check's own test read ${JSON.stringify(PartGlyphs.render(own))} with the glyphs in no list ${JSON.stringify(own.orphans)} ` + `from a small source, not ${JSON.stringify(PartGlyphsCheck.OWN_LIST)} with ${PartGlyphsCheck.OWN_ORPHANS}.\n`); const kit = await KitGlyphs.readAsync(this.root); for (const orphan of kit.orphans) - output.write(`${orphan} is a glyph no exported part's list in ${KitGlyphs.FILE_NAME} holds; ` + + output.write(`${orphan} is a glyph no exported part's or model's list in ${KitGlyphs.FILE_NAME} holds; ` + "name it in a resources class's field that the part that draws it uses, or in its template.\n"); if (kit.parts.size === 0) output.write(`The kit's entries export no part, so ${KitGlyphs.FILE_NAME} would list none.\n`); else - output.write(`${KitGlyphs.FILE_NAME} lists the glyphs of ${kit.parts.size} parts, ${new Set([...kit.parts.values()].flat()).size} glyphs in all.\n`); + output.write(`${KitGlyphs.FILE_NAME} lists the glyphs of ${kit.parts.size} parts and ${kit.models.size} models, ` + + `${new Set([...kit.parts.values(), ...kit.models.values()].flat()).size} glyphs in all.\n`); return isOwnRight && kit.orphans.length === 0 && kit.parts.size > 0; } diff --git a/src/noldova.ui.components.angular/scripts/glyphs/exported-part.ts b/src/noldova.ui.components.angular/scripts/glyphs/exported-class.ts similarity index 52% rename from src/noldova.ui.components.angular/scripts/glyphs/exported-part.ts rename to src/noldova.ui.components.angular/scripts/glyphs/exported-class.ts index ff21235e..aca9a2d0 100644 --- a/src/noldova.ui.components.angular/scripts/glyphs/exported-part.ts +++ b/src/noldova.ui.components.angular/scripts/glyphs/exported-class.ts @@ -8,13 +8,15 @@ import type ts from "typescript"; -// A part an entry of the kit exports: its exported name and the file that declares it. -export class ExportedPart { +// A class an entry of the kit exports: its exported name, the file that declares it, and whether it is a part, a component, or a model. +export class ExportedClass { public readonly name: string; public readonly source: ts.SourceFile; + public readonly isPart: boolean; - public constructor(name: string, source: ts.SourceFile) { + public constructor(name: string, source: ts.SourceFile, isPart: boolean) { this.name = name; this.source = source; + this.isPart = isPart; } } diff --git a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-file.ts b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-file.ts index ee66ef95..334198e0 100644 --- a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-file.ts +++ b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-file.ts @@ -6,11 +6,14 @@ * LICENSE file in the root directory of this source tree. */ -// The package's glyphs.json: for each part the kit exports, by its class name, the sorted glyphs it can draw. +// The package's glyphs.json: for each part the kit exports, by its class name, the sorted glyphs it can draw, and the same for each model it +// exports that holds glyphs. export class PartGlyphsFile { public readonly parts: Readonly>; + public readonly models: Readonly>; - public constructor(parts: Readonly>) { + public constructor(parts: Readonly>, models: Readonly>) { this.parts = parts; + this.models = models; } } diff --git a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-reading.ts b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-reading.ts index f64a2c00..1f79917f 100644 --- a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-reading.ts +++ b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs-reading.ts @@ -6,13 +6,16 @@ * LICENSE file in the root directory of this source tree. */ -// What PartGlyphs read: each exported part's glyphs, and each glyph of the source that no part's list holds, by its place. +// What PartGlyphs read: each exported part's glyphs, each exported model's that has any, and each glyph of the source that no list holds, by its +// place. export class PartGlyphsReading { public readonly parts: ReadonlyMap; + public readonly models: ReadonlyMap; public readonly orphans: readonly string[]; - public constructor(parts: ReadonlyMap, orphans: readonly string[]) { + public constructor(parts: ReadonlyMap, models: ReadonlyMap, orphans: readonly string[]) { this.parts = parts; + this.models = models; this.orphans = orphans; } } diff --git a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs.ts b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs.ts index c44589ff..7ef6d06f 100644 --- a/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs.ts +++ b/src/noldova.ui.components.angular/scripts/glyphs/part-glyphs.ts @@ -12,7 +12,7 @@ import ts from "typescript"; import "@noldova/context.core"; -import { ExportedPart } from "./exported-part.ts"; +import { ExportedClass } from "./exported-class.ts"; import { GlyphPlace } from "./glyph-place.ts"; import { IconInput } from "./icon-input.ts"; import { PartGlyphsFile } from "./part-glyphs-file.ts"; @@ -23,6 +23,8 @@ import { PartTemplate } from "./part-template.ts"; // its code uses or its template names, the names its template draws as written, and the same of the kit's parts it shows. A glyph is a string // literal typed `IconName`, or, in a constant written `as const`, an icon name under a property whose name holds glyph or icon, or an icon name a // template gives an input of a part that the input's type, `IconName` or a list of them, says is an icon. Icons a site passes in are the site's own. +// Each other class the package's entries export, a model such as a link reference action, has a list of the same glyphs of its files when it has +// any, such as the glyph it defaults to, since a part draws what the model holds. export class PartGlyphs { private static readonly ICON_TYPE: string = "IconName"; private static readonly COMPONENT_DECORATOR: string = "Component"; @@ -74,12 +76,11 @@ export class PartGlyphs { this.readText = readText; } - public static render(parts: ReadonlyMap): string { - const sorted = new Map([...parts.keys()].sort().map(t => [t, parts.get(t) ?? []])); - return `${JSON.stringify(new PartGlyphsFile(Object.fromEntries(sorted)), null, 2)}\n`; + public static render(reading: PartGlyphsReading): string { + return `${JSON.stringify(new PartGlyphsFile(PartGlyphs.sorted(reading.parts), PartGlyphs.sorted(reading.models)), null, 2)}\n`; } - // Reads every part the entries export, and every glyph of the source files under the folder that no part's list took. + // Reads every part and model the entries export, and every glyph of the source files under the folder that no list took. public read(entries: readonly string[], folder: string): PartGlyphsReading { const sources = this.program.getSourceFiles().filter(t => PartGlyphs.isUnder(t.fileName, folder)); const iconType = this.findIconType(sources); @@ -89,12 +90,22 @@ export class PartGlyphs { for (const source of sources) this.collectGlyphs(source, iconType, folder); const parts = new Map(); + const models = new Map(); for (const entry of entries) - for (const part of this.exportedParts(entry)) - parts.set(part.name, this.glyphsOf(part.source, folder)); + for (const exported of this.exportedClasses(entry)) { + const glyphs = this.glyphsOf(exported.source, folder); + if (exported.isPart) + parts.set(exported.name, glyphs); + else if (glyphs.length > 0) + models.set(exported.name, glyphs); + } const orphans = this.places.filter(t => t.isDrawn ? !this.usedTemplates.has(t.file) : Object.isNull(t.owner) || !this.usedOwners.has(t.owner)) .map(t => `${t.place}: ${t.name}`); - return new PartGlyphsReading(parts, [...new Set(orphans)].sort()); + return new PartGlyphsReading(parts, models, [...new Set(orphans)].sort()); + } + + private static sorted(lists: ReadonlyMap): Readonly> { + return Object.fromEntries([...lists.keys()].sort().map(t => [t, lists.get(t) ?? []])); } private static isUnder(file: string, folder: string): boolean { @@ -241,19 +252,19 @@ export class PartGlyphs { return !Object.isNull(owner) && PartGlyphs.GLYPH_NAME.test(owner.text) && PartGlyphs.isConstant(node); } - private exportedParts(entry: string): readonly ExportedPart[] { + private exportedClasses(entry: string): readonly ExportedClass[] { const source = this.program.getSourceFile(entry); const module = Object.isUndefined(source) ? undefined : this.checker.getSymbolAtLocation(source); if (Object.isUndefined(module)) return []; - const parts: ExportedPart[] = []; + const classes: ExportedClass[] = []; for (const exported of this.checker.getExportsOfModule(module)) { const symbol = (exported.flags & ts.SymbolFlags.Alias) === 0 ? exported : this.checker.getAliasedSymbol(exported); const declaration = symbol.declarations?.[0]; - if (!Object.isUndefined(declaration) && ts.isClassDeclaration(declaration) && !Object.isNull(PartGlyphs.componentOf(declaration))) - parts.push(new ExportedPart(exported.name, declaration.getSourceFile())); + if (!Object.isUndefined(declaration) && ts.isClassDeclaration(declaration)) + classes.push(new ExportedClass(exported.name, declaration.getSourceFile(), !Object.isNull(PartGlyphs.componentOf(declaration)))); } - return parts; + return classes; } private readTemplate(source: ts.SourceFile): PartTemplate | null { diff --git a/src/noldova.ui.components.angular/src/api/symbols.d.ts b/src/noldova.ui.components.angular/src/api/symbols.d.ts index 889f37c9..b9c7e137 100644 --- a/src/noldova.ui.components.angular/src/api/symbols.d.ts +++ b/src/noldova.ui.components.angular/src/api/symbols.d.ts @@ -31,8 +31,9 @@ export interface IMaterialSymbolsSite { /** * Other folders of code the site's pages show, such as the kit's own * source a documentation site renders, read as `code` is for the icons - * their templates draw or give and the glyphs their code keeps, but not for - * the kit's parts they import. + * their templates draw or give and the glyphs their code keeps, and for the + * kit's models they import by any path, but not for the kit's parts they + * import. */ readonly moreCode: readonly string[]; @@ -60,7 +61,8 @@ export interface IMaterialSymbolsSite { readonly fontsAddress: string; /** - * The kit's `glyphs.json`, the glyphs each part can draw. + * The kit's `glyphs.json`, the glyphs each part can draw and each model + * holds. */ readonly kitGlyphs: string; @@ -185,8 +187,8 @@ export declare class MaterialSymbolsException extends UiComponentsException { * whole font, about 5.4 MB, that `styles/kit.css` loads: a Node.js tool, * which loads in no browser. * - * The subset holds the union of the `glyphs.json` lists of the kit's parts the - * site's code imports, with the icons its templates draw or give a part's + * The subset holds the union of the `glyphs.json` lists of the kit's parts and + * models the site's code imports, with the icons its templates draw or give a part's * `icon`, the glyphs its code keeps in a field whose name ends in glyph or * starts with glyph and a capital, such as `glyphShield`, and the site's own * `names`. A refresh asks Google Fonts for that subset, keeps diff --git a/src/noldova.ui.components.angular/src/symbols/material-symbols.resources.ts b/src/noldova.ui.components.angular/src/symbols/material-symbols.resources.ts index 5e538a01..715e3a0e 100644 --- a/src/noldova.ui.components.angular/src/symbols/material-symbols.resources.ts +++ b/src/noldova.ui.components.angular/src/symbols/material-symbols.resources.ts @@ -18,6 +18,7 @@ export class MaterialSymbolsResources { public static readonly woff2Signature: string = "wOF2"; public static readonly licenseTitle: string = "Apache License"; public static readonly partsField: string = "parts"; + public static readonly modelsField: string = "models"; public static readonly iconNamesParameter: string = "icon_names"; public static readonly htmlExtension: string = ".html"; public static readonly codeExtension: string = ".ts"; @@ -74,6 +75,9 @@ export class MaterialSymbolsResources { public static formatNoParts(kitGlyphs: string): string { return `${kitGlyphs} lists no parts.`; } + public static formatModelsNotObject(kitGlyphs: string): string { + return `${kitGlyphs} lists its models as something other than an object.`; + } public static formatListNotList(kitGlyphs: string, part: string): string { return `${kitGlyphs} lists ${part}'s glyphs as something other than a list.`; } diff --git a/src/noldova.ui.components.angular/src/symbols/material-symbols.ts b/src/noldova.ui.components.angular/src/symbols/material-symbols.ts index a9e8bf03..04e76672 100644 --- a/src/noldova.ui.components.angular/src/symbols/material-symbols.ts +++ b/src/noldova.ui.components.angular/src/symbols/material-symbols.ts @@ -38,6 +38,8 @@ export class MaterialSymbols { private static readonly KEPT_GLYPH: RegExp = /(?:[gG]lyph|\bglyph[A-Z]\w*)(?:: (?:IconName|string) =|:) "([a-z0-9_]+)"/gu; // The names the site's code imports from any of the kit's entry points. private static readonly KIT_IMPORT: RegExp = /import (?:type )?\{([^}]+)\} from "@noldova\/ui\.components\.angular(?:\/[a-z-]+)*";/gu; + // Any named import, such as the kit's own source's relative imports of its models. + private static readonly ANY_IMPORT: RegExp = /import (?:type )?\{([^}]+)\} from "[^"]+";/gu; private static readonly FONT_NAME: RegExp = /^material-symbols-([a-f0-9]{8})\.woff2$/u; private static readonly ANY_FONT_NAME: RegExp = /^material-symbols(?:-[a-f0-9]{8})?\.woff2$/u; private static readonly STYLES_FONT: RegExp = /material-symbols(?:-[a-f0-9]{8})?\.woff2/gu; @@ -173,34 +175,48 @@ export class MaterialSymbols { } } - // The glyphs the kit's parts the site's code imports can draw: the union of their lists in the kit's glyphs.json. A name the - // site imports that is no part, such as a model or a type, has no list there, and a part the site shows only in the whole font - // adds nothing. + // The glyphs the kit's parts and models the site's code imports can draw: the union of their lists in the kit's glyphs.json, with + // those of the models the other folders of code import by any path. A name the site imports that is neither, such as a type, has + // no list there, a part the site shows only in the whole font adds nothing, and a glyphs.json from before models has none. private async kitIconNamesAsync(): Promise> { const kitGlyphs = this.site.kitGlyphs; - const parts = MaterialSymbols.field(await this.kitGlyphListAsync(), MaterialSymbolsResources.partsField); + const file = await this.kitGlyphListAsync(); + const parts = MaterialSymbols.field(file, MaterialSymbolsResources.partsField); if (!Object.isObject(parts)) throw new MaterialSymbolsException(MaterialSymbolsResources.formatNoParts(kitGlyphs)); - const lists = new Map(Object.entries(parts)); + const models = MaterialSymbols.field(file, MaterialSymbolsResources.modelsField) ?? {}; + if (!Object.isObject(models)) + throw new MaterialSymbolsException(MaterialSymbolsResources.formatModelsNotObject(kitGlyphs)); + const partLists = new Map(Object.entries(parts)); + const modelLists = new Map(Object.entries(models)); const imported = new Set(); - await this.collectKitImportsAsync(this.at(this.site.code), imported); + await this.collectImportsAsync(this.at(this.site.code), MaterialSymbols.KIT_IMPORT, imported); + const modelImports = new Set(imported); + for (const folder of this.site.moreCode) + await this.collectImportsAsync(this.at(folder), MaterialSymbols.ANY_IMPORT, modelImports); const names = new Set(); - for (const part of imported) { - if (!lists.has(part) || this.site.wholeFontParts.includes(part)) - continue; - const list = lists.get(part); - if (!Array.isArray(list)) - throw new MaterialSymbolsException(MaterialSymbolsResources.formatListNotList(kitGlyphs, part)); - const glyphs: readonly unknown[] = list; - for (const glyph of glyphs) { - if (!Object.isString(glyph)) - throw new MaterialSymbolsException(MaterialSymbolsResources.formatListNotNames(kitGlyphs, part)); - names.add(glyph); - } - } + for (const part of imported) + if (!this.site.wholeFontParts.includes(part)) + this.addList(partLists, part, names); + for (const model of modelImports) + this.addList(modelLists, model, names); return names; } + private addList(lists: ReadonlyMap, name: string, names: Set): void { + const list = lists.get(name); + if (Object.isUndefined(list)) + return; + if (!Array.isArray(list)) + throw new MaterialSymbolsException(MaterialSymbolsResources.formatListNotList(this.site.kitGlyphs, name)); + const glyphs: readonly unknown[] = list; + for (const glyph of glyphs) { + if (!Object.isString(glyph)) + throw new MaterialSymbolsException(MaterialSymbolsResources.formatListNotNames(this.site.kitGlyphs, name)); + names.add(glyph); + } + } + // The kit's glyphs.json, which kits from 0.0.37 on publish. private async kitGlyphListAsync(): Promise { let text: string; @@ -218,13 +234,13 @@ export class MaterialSymbols { } } - private async collectKitImportsAsync(directory: string, imported: Set): Promise { + private async collectImportsAsync(directory: string, pattern: RegExp, imported: Set): Promise { for (const entry of await readdir(directory, { withFileTypes: true })) { const filename = path.join(directory, entry.name); if (entry.isDirectory()) - await this.collectKitImportsAsync(filename, imported); + await this.collectImportsAsync(filename, pattern, imported); else if (filename.endsWith(MaterialSymbolsResources.codeExtension)) - for (const list of MaterialSymbols.groupsOf(await readFile(filename, MaterialSymbolsResources.encoding), MaterialSymbols.KIT_IMPORT)) + for (const list of MaterialSymbols.groupsOf(await readFile(filename, MaterialSymbolsResources.encoding), pattern)) for (const name of list.split(",")) imported.add(name.trim()); } diff --git a/src/noldova.ui.components.angular/tests/node/material-symbols.test.ts b/src/noldova.ui.components.angular/tests/node/material-symbols.test.ts index 9610e8e6..adab5c88 100644 --- a/src/noldova.ui.components.angular/tests/node/material-symbols.test.ts +++ b/src/noldova.ui.components.angular/tests/node/material-symbols.test.ts @@ -91,8 +91,8 @@ class Site { await this.writeAsync(`node_modules/@noldova/ui.components.angular/${relative}`, content); } - public async writeKitGlyphsAsync(parts: Readonly>): Promise { - await this.writeKitAsync("glyphs.json", JSON.stringify({ parts })); + public async writeKitGlyphsAsync(parts: Readonly>, models: Readonly> = {}): Promise { + await this.writeKitAsync("glyphs.json", JSON.stringify({ parts, models })); } public static notice(sha: string, name: string, address: string): string { @@ -253,6 +253,29 @@ describe("MaterialSymbols.checkAsync", () => { assert.equal(await current().refusalAsync(), ""); }); + it("reads the glyphs.json list of a kit model the site's code imports, and none of a model it doesn't", async () => { + const parts = { SiteHeaderComponent: ["close", "dark_mode", "desktop_windows", "download", "light_mode", "menu", "search"] }; + await current().writeKitGlyphsAsync(parts, { LinkReferenceAction: ["open_in_new"] }); + assert.equal(await current().refusalAsync(), ""); + await current().writeKitGlyphsAsync(parts, { SiteLink: ["open_in_new"] }); + assert.equal(await current().refusalAsync(), changed); + }); + + it("reads the lists of the kit models the other folders of code import by any path, and none of the parts they import", async () => { + const parts = { SiteCanvasComponent: ["zoom_in"], SiteHeaderComponent: ["close", "dark_mode", "desktop_windows", "download", "light_mode", "menu", "search"] }; + await current().writeKitGlyphsAsync(parts, { LinkReferenceAction: ["open_in_new"] }); + await current().writeAsync("shared/canvas.ts", "import { SiteCanvasComponent } from \"../site/site-canvas.component.js\";\n"); + assert.equal(await current().refusalAsync(new MoreCodeSite(current().root)), ""); + await current().writeAsync("shared/references.ts", "import { LinkReferenceAction } from \"../models/link-reference-action.js\";\n"); + assert.equal(await current().refusalAsync(new MoreCodeSite(current().root)), changed); + assert.equal(await current().refusalAsync(), ""); + }); + + it("reads the glyphs.json of a kit from before models, which lists none", async () => { + await current().writeKitAsync("glyphs.json", JSON.stringify({ parts: { SiteHeaderComponent: ["close", "dark_mode", "desktop_windows", "download", "light_mode", "menu", "search"] } })); + assert.equal(await current().refusalAsync(), ""); + }); + it("reads a glyph the site's code keeps as a string by name", async () => { await current().writeAsync("src/resources.ts", "public static readonly menuGlyph: string = \"toc\";\n"); assert.equal(await current().refusalAsync(), changed); @@ -339,6 +362,16 @@ describe("MaterialSymbols.checkAsync", () => { await assert.rejects(current().symbols().checkAsync(), { name: "MaterialSymbolsException", message: `${kitGlyphs} lists SiteHeaderComponent's glyphs as something other than a list.` }); }); + it("stops when the kit's glyphs.json lists its models as anything but an object", async () => { + await current().writeKitAsync("glyphs.json", JSON.stringify({ parts: { SiteHeaderComponent: [] }, models: 7 })); + await assert.rejects(current().symbols().checkAsync(), { name: "MaterialSymbolsException", message: `${kitGlyphs} lists its models as something other than an object.` }); + }); + + it("stops when the kit's glyphs.json lists a model's glyphs as anything but a list", async () => { + await current().writeKitGlyphsAsync({ SiteHeaderComponent: [] }, { SiteLink: "open_in_new" }); + await assert.rejects(current().symbols().checkAsync(), { name: "MaterialSymbolsException", message: `${kitGlyphs} lists SiteLink's glyphs as something other than a list.` }); + }); + it("stops when the kit's glyphs.json isn't JSON", async () => { await current().writeKitAsync("glyphs.json", "{ \"parts\": "); await assert.rejects(current().symbols().checkAsync(), { name: "MaterialSymbolsException", message: `${kitGlyphs} isn't JSON.` });