@astryxdesign/cli 0.7.14-canary.gfb823d9 → 0.7.14
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 +197 -0
- package/api/component/_adapter.d.mts +15 -0
- package/api/component/_adapter.mjs +76 -0
- package/api/component/component.mjs +8 -1
- package/api/search/_named-keywords.d.mts +31 -0
- package/api/search/_named-keywords.mjs +177 -0
- package/api/search/search-index.mjs +8 -1
- package/api/search/search.d.mts +6 -1
- package/api/search/search.mjs +49 -8
- package/api/theme/_adapter.d.mts +33 -0
- package/api/theme/_adapter.mjs +352 -16
- package/api/theme/build/build.mjs +261 -10
- package/api/theme/eject/eject.mjs +13 -1
- package/api/theme/list/list.mjs +11 -2
- package/api/theme/palette/generate/color.d.mts +2 -0
- package/api/theme/palette/generate/color.mjs +1 -1
- package/api/theme/template/template.mjs +139 -6
- package/api/theme/theme.type.d.mts +15 -1
- package/api/theme/theme.type.mjs +9 -3
- package/api/theme/themeAdd.doc.mjs +1 -1
- package/api/theme/themeBuild.doc.mjs +10 -2
- package/api/theme/themeEject.doc.mjs +2 -2
- package/api/theme/themeListAvailable.doc.mjs +1 -1
- package/api/theme/themeRemove.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +8 -0
- package/api/theme/themeUse.doc.mjs +1 -1
- package/api/theme-edit/_adapter.d.mts +36 -2
- package/api/theme-edit/_adapter.mjs +340 -12
- package/api/theme-edit/_check.d.mts +28 -25
- package/api/theme-edit/_check.mjs +876 -308
- package/api/theme-edit/_color.d.mts +53 -4
- package/api/theme-edit/_color.mjs +406 -79
- package/api/theme-edit/_compiler.d.mts +14 -6
- package/api/theme-edit/_compiler.mjs +89 -59
- package/api/theme-edit/_context.d.mts +28 -8
- package/api/theme-edit/_context.mjs +142 -19
- package/api/theme-edit/_edit.d.mts +3 -1
- package/api/theme-edit/_edit.mjs +746 -76
- package/api/theme-edit/_how-commands.d.mts +91 -0
- package/api/theme-edit/_how-commands.mjs +269 -0
- package/api/theme-edit/_how-question.d.mts +57 -0
- package/api/theme-edit/_how-question.mjs +2574 -0
- package/api/theme-edit/_how.d.mts +27 -8
- package/api/theme-edit/_how.mjs +776 -175
- package/api/theme-edit/_overview.d.mts +9 -0
- package/api/theme-edit/_overview.mjs +296 -0
- package/api/theme-edit/_palette.d.mts +155 -15
- package/api/theme-edit/_palette.mjs +1446 -262
- package/api/theme-edit/_parts.mjs +61 -5
- package/api/theme-edit/_png.d.mts +18 -6
- package/api/theme-edit/_png.mjs +135 -25
- package/api/theme-edit/_preview-app.d.mts +32 -11
- package/api/theme-edit/_preview-app.mjs +1976 -285
- package/api/theme-edit/_preview.d.mts +39 -8
- package/api/theme-edit/_preview.mjs +367 -41
- package/api/theme-edit/_settings.d.mts +147 -11
- package/api/theme-edit/_settings.mjs +908 -99
- package/api/theme-edit/_tables.d.mts +4 -16
- package/api/theme-edit/_tables.mjs +51 -117
- package/api/theme-edit/theme-edit.type.d.mts +102 -66
- package/api/theme-edit/theme-edit.type.mjs +58 -40
- package/api/theme-edit/themeEditHow.doc.mjs +17 -6
- package/api/theme-edit/themeEditNew.doc.mjs +21 -5
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +18 -5
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +12 -9
- package/api/theme-edit/themeEditPreviewList.doc.mjs +11 -2
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +29 -3
- package/api/theme-edit/themeEditSet.doc.mjs +14 -8
- package/api/theme-edit/themeEditShow.doc.mjs +10 -5
- package/api/theme-edit/themeEditUndo.doc.mjs +9 -4
- package/assets/discover/official-packages.json +4 -0
- package/assets/docs/tree/icon-sets.doc.mjs +3 -3
- package/assets/docs.generated.json +1 -0
- package/assets/icons/definitions/bookmarkAdd.doc.mjs +9 -0
- package/assets/icons/definitions/documentSearch.doc.mjs +9 -0
- package/assets/icons/definitions/keyboard.doc.mjs +9 -0
- package/assets/icons/definitions/paintBrush.doc.mjs +9 -0
- package/assets/icons/definitions/wallet.doc.mjs +9 -0
- package/assets/preview-app/gitignore +3 -0
- package/assets/templates/blocks/components/ButtonGroup/ButtonGroupShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Lightbox/LightboxGallery.tsx +1 -1
- package/assets/templates/components.generated.json +1 -0
- package/assets/templates/pages/gallery-hero/page.tsx +8 -2
- package/assets/templates/pages/settings-dialog/template.doc.mjs +3 -3
- package/assets/templates/pages/side-gallery/page.tsx +1 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +2 -2
- package/assets/templates/pages/theme-showcase/template.doc.mjs +1 -1
- package/clients/cli/bin/astryx.mjs +37 -5
- package/clients/cli/commands/experimental/theme.mjs +422 -57
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +22 -9
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +103 -4
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +13 -8
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +5 -5
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +5 -5
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +3 -2
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +2 -8
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme.doc.mjs +3 -4
- package/clients/cli/commands/theme-build.doc.mjs +1 -1
- package/clients/cli/commands/theme-eject.doc.mjs +3 -3
- package/clients/cli/commands/theme-remove.doc.mjs +2 -2
- package/clients/cli/commands/theme-template.doc.mjs +11 -4
- package/clients/cli/commands/theme.mjs +145 -46
- package/clients/cli/commands.generated.json +1 -0
- package/clients/cli/index.mjs +96 -5
- package/clients/cli/lib/json-shim.mjs +29 -1
- package/foundation/config/project.d.mts +16 -2
- package/foundation/config/project.mjs +40 -6
- package/foundation/config/theme-state.d.mts +9 -0
- package/foundation/config/theme-state.mjs +20 -1
- package/foundation/discovery/template-adapter.d.mts +12 -1
- package/foundation/discovery/template-adapter.mjs +29 -4
- package/foundation/discovery/template-icons.d.mts +13 -0
- package/foundation/discovery/template-icons.mjs +76 -0
- package/foundation/discovery/template-needs.d.mts +7 -0
- package/foundation/discovery/template-needs.mjs +117 -8
- package/foundation/discovery/template-source-tree.d.mts +5 -1
- package/foundation/discovery/template-source-tree.mjs +6 -4
- package/foundation/discovery/theme-discovery.d.mts +14 -3
- package/foundation/discovery/theme-discovery.mjs +45 -12
- package/foundation/doc-compiler/import.mjs +68 -3
- package/foundation/doc-compiler/stored-docs.d.mts +27 -0
- package/foundation/doc-compiler/stored-docs.mjs +65 -0
- package/foundation/fs/fs-error.d.mts +17 -0
- package/foundation/fs/fs-error.mjs +34 -0
- package/foundation/response/error-codes.d.mts +3 -1
- package/foundation/response/error-codes.doc.mjs +13 -1
- package/foundation/response/error-codes.mjs +11 -1
- package/foundation/response/response-types.doc.mjs +6 -6
- package/package.json +6 -7
- package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +0 -73
- package/clients/cli/commands/experimental-theme-palette.doc.mjs +0 -37
|
@@ -83,14 +83,18 @@ export function catalogItems(project) {
|
|
|
83
83
|
return {items, imports};
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/** One name per process, so two commands at once never share a folder. */
|
|
87
|
+
const RUN = `${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
|
|
88
|
+
|
|
86
89
|
/**
|
|
87
90
|
* The folder generated pages render from: inside the project but outside
|
|
88
91
|
* node_modules, so the render compiles them and their stylesheet imports as
|
|
89
|
-
* the project's own.
|
|
92
|
+
* the project's own. Its name is this process's own, and it is removed
|
|
93
|
+
* after each render.
|
|
90
94
|
* @param {import('./_project.mjs').ThemeProject} project
|
|
91
95
|
*/
|
|
92
96
|
export const renderDir = project =>
|
|
93
|
-
path.join(project.cwd, 'themes',
|
|
97
|
+
path.join(project.cwd, 'themes', `.astryx-render-${RUN}`);
|
|
94
98
|
|
|
95
99
|
/**
|
|
96
100
|
* Render a generated page module and give its HTML and markup (the body,
|
|
@@ -103,7 +107,27 @@ export const renderDir = project =>
|
|
|
103
107
|
*/
|
|
104
108
|
export async function renderGenerated(project, file, source, extra = []) {
|
|
105
109
|
const dir = renderDir(project);
|
|
110
|
+
// A lookup in an app with no themes must not leave a themes/ folder.
|
|
111
|
+
const themes = path.dirname(dir);
|
|
112
|
+
// A themes/ folder that holds only other renders was made by another
|
|
113
|
+
// lookup running beside this one, so it is ours to remove too.
|
|
114
|
+
const madeThemes =
|
|
115
|
+
!fs.existsSync(themes) ||
|
|
116
|
+
(() => {
|
|
117
|
+
const names = fs.readdirSync(themes);
|
|
118
|
+
return (
|
|
119
|
+
names.length > 0 && names.every(n => n.startsWith('.astryx-render-'))
|
|
120
|
+
);
|
|
121
|
+
})();
|
|
106
122
|
fs.mkdirSync(dir, {recursive: true});
|
|
123
|
+
// Stopped by a signal, the render folder still goes.
|
|
124
|
+
const cleanup = () => fs.rmSync(dir, {recursive: true, force: true});
|
|
125
|
+
const onSignal = (/** @type {NodeJS.Signals} */ sig) => {
|
|
126
|
+
cleanup();
|
|
127
|
+
process.kill(process.pid, sig);
|
|
128
|
+
};
|
|
129
|
+
process.once('SIGINT', onSignal);
|
|
130
|
+
process.once('SIGTERM', onSignal);
|
|
107
131
|
const entry = path.join(dir, file);
|
|
108
132
|
fs.writeFileSync(entry, source);
|
|
109
133
|
for (const x of extra) {
|
|
@@ -128,7 +152,31 @@ export async function renderGenerated(project, file, source, extra = []) {
|
|
|
128
152
|
}
|
|
129
153
|
throw e;
|
|
130
154
|
} finally {
|
|
131
|
-
|
|
155
|
+
// Only this render's files go; the folder goes when nothing else is in
|
|
156
|
+
// it, so a render running beside this one keeps its own.
|
|
157
|
+
for (const f of [entry, ...extra.map(x => path.join(dir, x.name))])
|
|
158
|
+
fs.rmSync(f, {force: true});
|
|
159
|
+
removeEmpty(madeThemes ? themes : dir);
|
|
160
|
+
process.removeListener('SIGINT', onSignal);
|
|
161
|
+
process.removeListener('SIGTERM', onSignal);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @param {unknown} v @returns {v is Record<string, unknown>} */
|
|
166
|
+
const isRecord = v => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Remove `dir` and the folders under it when no file is left in them.
|
|
170
|
+
* @param {string} dir
|
|
171
|
+
*/
|
|
172
|
+
function removeEmpty(dir) {
|
|
173
|
+
if (!fs.existsSync(dir)) return;
|
|
174
|
+
for (const e of fs.readdirSync(dir, {withFileTypes: true}))
|
|
175
|
+
if (e.isDirectory()) removeEmpty(path.join(dir, e.name));
|
|
176
|
+
try {
|
|
177
|
+
fs.rmdirSync(dir);
|
|
178
|
+
} catch {
|
|
179
|
+
// Not empty: another render is using it.
|
|
132
180
|
}
|
|
133
181
|
}
|
|
134
182
|
|
|
@@ -141,9 +189,14 @@ export async function partData(project) {
|
|
|
141
189
|
const file = path.join(project.cacheDir, 'parts.json');
|
|
142
190
|
try {
|
|
143
191
|
const cached = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
192
|
+
// A file that parses but has the wrong shape (a hand edit, a cut-off
|
|
193
|
+
// write) is built again, as a file that does not parse is.
|
|
144
194
|
if (
|
|
145
195
|
cached.version === PART_DATA_VERSION &&
|
|
146
|
-
cached.coreKey === project.coreKey
|
|
196
|
+
cached.coreKey === project.coreKey &&
|
|
197
|
+
isRecord(cached.map) &&
|
|
198
|
+
Object.values(cached.map).every(isRecord) &&
|
|
199
|
+
Array.isArray(cached.pairs)
|
|
147
200
|
)
|
|
148
201
|
return cached;
|
|
149
202
|
} catch {
|
|
@@ -152,7 +205,10 @@ export async function partData(project) {
|
|
|
152
205
|
const data = await buildPartData(project);
|
|
153
206
|
try {
|
|
154
207
|
fs.mkdirSync(project.cacheDir, {recursive: true});
|
|
155
|
-
|
|
208
|
+
// Write beside it and rename, so a reader never sees half a file.
|
|
209
|
+
const part = `${file}.${RUN}`;
|
|
210
|
+
fs.writeFileSync(part, JSON.stringify(data));
|
|
211
|
+
fs.renameSync(part, file);
|
|
156
212
|
} catch {
|
|
157
213
|
// A read-only node_modules: build it again next time.
|
|
158
214
|
}
|
|
@@ -3,27 +3,32 @@
|
|
|
3
3
|
|
|
4
4
|
/** @typedef {{width: number, height: number, rgba: Uint8Array}} Picture */
|
|
5
5
|
/**
|
|
6
|
-
* Decode a PNG file's bytes. Interlaced pictures are refused
|
|
6
|
+
* Decode a PNG file's bytes. Interlaced pictures are refused, and so is
|
|
7
|
+
* any file whose header, chunks or pixel data do not match the PNG format.
|
|
7
8
|
* @param {Buffer} bytes
|
|
8
9
|
* @returns {Picture}
|
|
9
10
|
*/
|
|
10
11
|
export function decodePng(bytes: Buffer): Picture;
|
|
11
12
|
/**
|
|
12
|
-
* The exact color at each point
|
|
13
|
+
* The exact color at each point, with its alpha (0 is fully see-through,
|
|
14
|
+
* 1 fully opaque). Every point must be inside the picture.
|
|
13
15
|
* @param {Picture} pic
|
|
14
16
|
* @param {Array<{x: number, y: number}>} points
|
|
15
|
-
* @returns {Array<{at: string, color: string
|
|
17
|
+
* @returns {Array<{at: string, color: string, alpha: number}>}
|
|
16
18
|
*/
|
|
17
19
|
export function colorsAt(pic: Picture, points: Array<{
|
|
18
20
|
x: number;
|
|
19
21
|
y: number;
|
|
20
22
|
}>): Array<{
|
|
21
23
|
at: string;
|
|
22
|
-
color: string
|
|
24
|
+
color: string;
|
|
25
|
+
alpha: number;
|
|
23
26
|
}>;
|
|
24
27
|
/**
|
|
25
|
-
* The main colors of a picture: every
|
|
26
|
-
* channel, biggest share first, each with where it sits.
|
|
28
|
+
* The main colors of a picture: every pixel (on a grid past 4 million)
|
|
29
|
+
* bucketed to 5 bits per channel, biggest share first, each with where it sits. A fully
|
|
30
|
+
* see-through pixel shows no color, so it is skipped; a partly see-through
|
|
31
|
+
* one counts as it looks over white.
|
|
27
32
|
* @param {Picture} pic
|
|
28
33
|
* @param {number} count
|
|
29
34
|
* @returns {Array<{color: string, share: number, where: string}>}
|
|
@@ -33,6 +38,13 @@ export function mainColors(pic: Picture, count: number): Array<{
|
|
|
33
38
|
share: number;
|
|
34
39
|
where: string;
|
|
35
40
|
}>;
|
|
41
|
+
/**
|
|
42
|
+
* The most pixels a picture may have: about a 6K screen. A bigger header is
|
|
43
|
+
* refused before any memory is taken for it.
|
|
44
|
+
*/
|
|
45
|
+
export const MAX_PIXELS: 40000000;
|
|
46
|
+
/** A 16-bit picture takes twice the memory per pixel, so half as many. */
|
|
47
|
+
export const MAX_PIXELS_16: 20000000;
|
|
36
48
|
export type Picture = {
|
|
37
49
|
width: number;
|
|
38
50
|
height: number;
|
package/api/theme-edit/_png.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @file Read a PNG in plain Node:
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* @file Read a PNG in plain Node: check the header and size before taking
|
|
5
|
+
* memory, decompress with zlib to exactly the size the header names, undo
|
|
6
|
+
* the row filters, and give RGBA pixels. Then the main colors of a picture,
|
|
7
|
+
* or the exact color and alpha at given points.
|
|
7
8
|
* @position api/theme-edit — pure helpers for `experimental theme editor how <picture.png>`
|
|
8
9
|
*/
|
|
9
10
|
|
|
@@ -11,10 +12,39 @@ import * as zlib from 'node:zlib';
|
|
|
11
12
|
|
|
12
13
|
const SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The most pixels a picture may have: about a 6K screen. A bigger header is
|
|
17
|
+
* refused before any memory is taken for it.
|
|
18
|
+
*/
|
|
19
|
+
export const MAX_PIXELS = 40_000_000;
|
|
20
|
+
|
|
21
|
+
/** A 16-bit picture takes twice the memory per pixel, so half as many. */
|
|
22
|
+
export const MAX_PIXELS_16 = 20_000_000;
|
|
23
|
+
|
|
24
|
+
/** The most pixels the main colors read one by one; past it they stride. */
|
|
25
|
+
const SAMPLE_BUDGET = 4_000_000;
|
|
26
|
+
|
|
27
|
+
/** The bit depths each PNG color type allows. */
|
|
28
|
+
const DEPTHS = /** @type {Record<number, number[]>} */ ({
|
|
29
|
+
0: [1, 2, 4, 8, 16],
|
|
30
|
+
2: [8, 16],
|
|
31
|
+
3: [1, 2, 4, 8],
|
|
32
|
+
4: [8, 16],
|
|
33
|
+
6: [8, 16],
|
|
34
|
+
});
|
|
35
|
+
const CHANNELS = /** @type {Record<number, number>} */ ({
|
|
36
|
+
0: 1,
|
|
37
|
+
2: 3,
|
|
38
|
+
3: 1,
|
|
39
|
+
4: 2,
|
|
40
|
+
6: 4,
|
|
41
|
+
});
|
|
42
|
+
|
|
14
43
|
/** @typedef {{width: number, height: number, rgba: Uint8Array}} Picture */
|
|
15
44
|
|
|
16
45
|
/**
|
|
17
|
-
* Decode a PNG file's bytes. Interlaced pictures are refused
|
|
46
|
+
* Decode a PNG file's bytes. Interlaced pictures are refused, and so is
|
|
47
|
+
* any file whose header, chunks or pixel data do not match the PNG format.
|
|
18
48
|
* @param {Buffer} bytes
|
|
19
49
|
* @returns {Picture}
|
|
20
50
|
*/
|
|
@@ -27,6 +57,7 @@ export function decodePng(bytes) {
|
|
|
27
57
|
let depth = 0;
|
|
28
58
|
let colorType = 0;
|
|
29
59
|
let interlace = 0;
|
|
60
|
+
let header = false;
|
|
30
61
|
/** @type {Buffer | null} */
|
|
31
62
|
let palette = null;
|
|
32
63
|
/** @type {Buffer | null} */
|
|
@@ -36,31 +67,68 @@ export function decodePng(bytes) {
|
|
|
36
67
|
while (pos + 8 <= bytes.length) {
|
|
37
68
|
const length = bytes.readUInt32BE(pos);
|
|
38
69
|
const type = bytes.toString('latin1', pos + 4, pos + 8);
|
|
70
|
+
if (pos + 12 + length > bytes.length)
|
|
71
|
+
throw new Error(`the ${type} chunk is cut off`);
|
|
39
72
|
const body = bytes.subarray(pos + 8, pos + 8 + length);
|
|
40
73
|
pos += 12 + length;
|
|
41
74
|
if (type === 'IHDR') {
|
|
75
|
+
if (length !== 13) throw new Error('the PNG header is damaged');
|
|
42
76
|
width = body.readUInt32BE(0);
|
|
43
77
|
height = body.readUInt32BE(4);
|
|
44
78
|
depth = body[8];
|
|
45
79
|
colorType = body[9];
|
|
46
80
|
interlace = body[12];
|
|
81
|
+
header = true;
|
|
47
82
|
} else if (type === 'PLTE') palette = body;
|
|
48
83
|
else if (type === 'tRNS') alpha = body;
|
|
49
84
|
else if (type === 'IDAT') data.push(body);
|
|
50
85
|
else if (type === 'IEND') break;
|
|
51
86
|
}
|
|
52
|
-
if (!
|
|
87
|
+
if (!header) throw new Error('the PNG has no header');
|
|
88
|
+
if (!width || !height)
|
|
89
|
+
throw new Error(
|
|
90
|
+
`the picture is ${width}x${height} pixels, so it has no colors`,
|
|
91
|
+
);
|
|
53
92
|
if (interlace) throw new Error('interlaced PNG files are not supported');
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
93
|
+
if (!DEPTHS[colorType]?.includes(depth))
|
|
94
|
+
throw new Error(
|
|
95
|
+
`color type ${colorType} at bit depth ${depth} is not a valid PNG format`,
|
|
96
|
+
);
|
|
97
|
+
const cap = depth === 16 ? MAX_PIXELS_16 : MAX_PIXELS;
|
|
98
|
+
if (width * height > cap)
|
|
99
|
+
throw new Error(
|
|
100
|
+
`the picture is ${width}x${height}${depth === 16 ? ' at 16 bits per channel' : ''}, more than ${cap / 1e6} million pixels; crop or scale it down${depth === 16 ? ', or save it at 8 bits' : ''}`,
|
|
101
|
+
);
|
|
102
|
+
if (colorType === 3 && !palette)
|
|
103
|
+
throw new Error('the PNG uses a palette but has none');
|
|
104
|
+
if (!data.length) throw new Error('the PNG has no pixel data');
|
|
105
|
+
const channels = CHANNELS[colorType];
|
|
58
106
|
const bitsPerPixel = channels * depth;
|
|
59
107
|
const stride = Math.ceil((width * bitsPerPixel) / 8);
|
|
108
|
+
const expected = (stride + 1) * height;
|
|
109
|
+
/** @type {Buffer} */
|
|
110
|
+
let raw;
|
|
111
|
+
try {
|
|
112
|
+
raw = zlib.inflateSync(Buffer.concat(data), {maxOutputLength: expected});
|
|
113
|
+
} catch (e) {
|
|
114
|
+
// Node reports output past the limit as a RangeError.
|
|
115
|
+
throw new Error(
|
|
116
|
+
e instanceof RangeError
|
|
117
|
+
? 'the pixel data is longer than the header says'
|
|
118
|
+
: 'the pixel data is damaged',
|
|
119
|
+
{cause: e},
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (raw.length < expected)
|
|
123
|
+
throw new Error(
|
|
124
|
+
`the pixel data is cut off: ${raw.length} of ${expected} bytes`,
|
|
125
|
+
);
|
|
60
126
|
const bpp = Math.max(1, bitsPerPixel >> 3);
|
|
61
127
|
const rows = Buffer.alloc(stride * height);
|
|
62
128
|
for (let y = 0; y < height; y++) {
|
|
63
129
|
const filter = raw[y * (stride + 1)];
|
|
130
|
+
if (filter > 4)
|
|
131
|
+
throw new Error(`row ${y} has an unknown filter type ${filter}`);
|
|
64
132
|
const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
|
|
65
133
|
const out = rows.subarray(y * stride, (y + 1) * stride);
|
|
66
134
|
const prev = y > 0 ? rows.subarray((y - 1) * stride, y * stride) : null;
|
|
@@ -94,25 +162,55 @@ export function decodePng(bytes) {
|
|
|
94
162
|
const v = (byte >> (8 - depth - (bit & 7))) & max;
|
|
95
163
|
return colorType === 3 ? v : Math.round((v * 255) / max);
|
|
96
164
|
};
|
|
165
|
+
const entries = palette ? Math.floor(palette.length / 3) : 0;
|
|
166
|
+
/** A sample as stored, before scaling to 0-255. @param {number} y @param {number} i */
|
|
167
|
+
const raw16 = (y, i) => {
|
|
168
|
+
const row = y * stride;
|
|
169
|
+
if (depth === 16) return (rows[row + i * 2] << 8) | rows[row + i * 2 + 1];
|
|
170
|
+
if (depth === 8) return rows[row + i];
|
|
171
|
+
const bit = i * depth;
|
|
172
|
+
return (rows[row + (bit >> 3)] >> (8 - depth - (bit & 7))) & max;
|
|
173
|
+
};
|
|
174
|
+
// tRNS for gray and RGB pictures names one color that is fully
|
|
175
|
+
// see-through.
|
|
176
|
+
const key =
|
|
177
|
+
alpha && colorType === 0 && alpha.length >= 2
|
|
178
|
+
? [alpha.readUInt16BE(0)]
|
|
179
|
+
: alpha && colorType === 2 && alpha.length >= 6
|
|
180
|
+
? [alpha.readUInt16BE(0), alpha.readUInt16BE(2), alpha.readUInt16BE(4)]
|
|
181
|
+
: null;
|
|
97
182
|
for (let y = 0; y < height; y++) {
|
|
98
183
|
for (let x = 0; x < width; x++) {
|
|
99
184
|
const o = (y * width + x) * 4;
|
|
100
185
|
const s = x * channels;
|
|
101
186
|
if (colorType === 3) {
|
|
102
187
|
const idx = sample(y, s);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
188
|
+
if (idx >= entries)
|
|
189
|
+
throw new Error(`a pixel names palette entry ${idx} of ${entries}`);
|
|
190
|
+
const pal = /** @type {Buffer} */ (palette);
|
|
191
|
+
rgba[o] = pal[idx * 3];
|
|
192
|
+
rgba[o + 1] = pal[idx * 3 + 1];
|
|
193
|
+
rgba[o + 2] = pal[idx * 3 + 2];
|
|
106
194
|
rgba[o + 3] = alpha && idx < alpha.length ? alpha[idx] : 255;
|
|
107
195
|
} else if (colorType === 0 || colorType === 4) {
|
|
108
196
|
const g = sample(y, s);
|
|
109
197
|
rgba[o] = rgba[o + 1] = rgba[o + 2] = g;
|
|
110
|
-
rgba[o + 3] =
|
|
198
|
+
rgba[o + 3] =
|
|
199
|
+
colorType === 4
|
|
200
|
+
? sample(y, s + 1)
|
|
201
|
+
: key && raw16(y, s) === key[0]
|
|
202
|
+
? 0
|
|
203
|
+
: 255;
|
|
111
204
|
} else {
|
|
112
205
|
rgba[o] = sample(y, s);
|
|
113
206
|
rgba[o + 1] = sample(y, s + 1);
|
|
114
207
|
rgba[o + 2] = sample(y, s + 2);
|
|
115
|
-
rgba[o + 3] =
|
|
208
|
+
rgba[o + 3] =
|
|
209
|
+
colorType === 6
|
|
210
|
+
? sample(y, s + 3)
|
|
211
|
+
: key && key.every((k, n) => raw16(y, s + n) === k)
|
|
212
|
+
? 0
|
|
213
|
+
: 255;
|
|
116
214
|
}
|
|
117
215
|
}
|
|
118
216
|
}
|
|
@@ -123,26 +221,28 @@ export function decodePng(bytes) {
|
|
|
123
221
|
const hx = v => Math.round(v).toString(16).padStart(2, '0');
|
|
124
222
|
|
|
125
223
|
/**
|
|
126
|
-
* The exact color at each point
|
|
224
|
+
* The exact color at each point, with its alpha (0 is fully see-through,
|
|
225
|
+
* 1 fully opaque). Every point must be inside the picture.
|
|
127
226
|
* @param {Picture} pic
|
|
128
227
|
* @param {Array<{x: number, y: number}>} points
|
|
129
|
-
* @returns {Array<{at: string, color: string
|
|
228
|
+
* @returns {Array<{at: string, color: string, alpha: number}>}
|
|
130
229
|
*/
|
|
131
230
|
export function colorsAt(pic, points) {
|
|
132
231
|
return points.map(({x, y}) => {
|
|
133
|
-
if (x < 0 || y < 0 || x >= pic.width || y >= pic.height)
|
|
134
|
-
return {at: `${x},${y}`, color: null};
|
|
135
232
|
const i = (y * pic.width + x) * 4;
|
|
136
233
|
return {
|
|
137
234
|
at: `${x},${y}`,
|
|
138
235
|
color: `#${hx(pic.rgba[i])}${hx(pic.rgba[i + 1])}${hx(pic.rgba[i + 2])}`,
|
|
236
|
+
alpha: Number((pic.rgba[i + 3] / 255).toFixed(2)),
|
|
139
237
|
};
|
|
140
238
|
});
|
|
141
239
|
}
|
|
142
240
|
|
|
143
241
|
/**
|
|
144
|
-
* The main colors of a picture: every
|
|
145
|
-
* channel, biggest share first, each with where it sits.
|
|
242
|
+
* The main colors of a picture: every pixel (on a grid past 4 million)
|
|
243
|
+
* bucketed to 5 bits per channel, biggest share first, each with where it sits. A fully
|
|
244
|
+
* see-through pixel shows no color, so it is skipped; a partly see-through
|
|
245
|
+
* one counts as it looks over white.
|
|
146
246
|
* @param {Picture} pic
|
|
147
247
|
* @param {number} count
|
|
148
248
|
* @returns {Array<{color: string, share: number, where: string}>}
|
|
@@ -150,12 +250,22 @@ export function colorsAt(pic, points) {
|
|
|
150
250
|
export function mainColors(pic, count) {
|
|
151
251
|
/** @type {Map<number, {n: number, r: number, g: number, b: number, x: number, y: number}>} */
|
|
152
252
|
const buckets = new Map();
|
|
153
|
-
|
|
154
|
-
|
|
253
|
+
// Every pixel up to a budget, so a 1-pixel line is never skipped; a
|
|
254
|
+
// bigger picture is read on an even grid.
|
|
255
|
+
const step = Math.max(
|
|
256
|
+
1,
|
|
257
|
+
Math.ceil(Math.sqrt((pic.width * pic.height) / SAMPLE_BUDGET)),
|
|
258
|
+
);
|
|
259
|
+
for (let y = 0; y < pic.height; y += step) {
|
|
260
|
+
for (let x = 0; x < pic.width; x += step) {
|
|
155
261
|
const i = (y * pic.width + x) * 4;
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
const
|
|
262
|
+
const a = pic.rgba[i + 3] / 255;
|
|
263
|
+
if (a === 0) continue;
|
|
264
|
+
const over = (/** @type {number} */ c) =>
|
|
265
|
+
Math.round(c * a + 255 * (1 - a));
|
|
266
|
+
const r = over(pic.rgba[i]);
|
|
267
|
+
const g = over(pic.rgba[i + 1]);
|
|
268
|
+
const b = over(pic.rgba[i + 2]);
|
|
159
269
|
const k = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
|
|
160
270
|
let e = buckets.get(k);
|
|
161
271
|
if (!e) {
|
|
@@ -2,21 +2,30 @@
|
|
|
2
2
|
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Rewrite the in-app preview
|
|
6
|
-
* a font package installed after `set` loads on the next change or `show
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* Rewrite the in-app preview routes of a theme when their imports changed, so
|
|
6
|
+
* a font package installed after `set` loads on the next change or `show`,
|
|
7
|
+
* and its Colors page, so it names the shades the tokens use now.
|
|
8
|
+
* The preview in the usual place (app/ or src/app/theme-preview) and every
|
|
9
|
+
* preview added with --path for the theme are kept this way. A damaged
|
|
10
|
+
* preview, and a file changed by hand, are left as they are.
|
|
9
11
|
* @param {string} cwd
|
|
10
12
|
* @param {string} slug
|
|
11
|
-
* @returns {Promise<boolean>}
|
|
13
|
+
* @returns {Promise<{changed: boolean, stale: string[]}>} `changed`: the
|
|
14
|
+
* route or the Colors page was rewritten; `stale`: preview files changed by
|
|
15
|
+
* hand, which keep showing the theme as it was
|
|
12
16
|
*/
|
|
13
|
-
export function refreshPreviewRoute(cwd: string, slug: string): Promise<
|
|
17
|
+
export function refreshPreviewRoute(cwd: string, slug: string): Promise<{
|
|
18
|
+
changed: boolean;
|
|
19
|
+
stale: string[];
|
|
20
|
+
}>;
|
|
14
21
|
/**
|
|
15
22
|
* Add pages to the in-app theme preview, and write the preview route the
|
|
16
23
|
* first time. With a description, the build ranking picks the template
|
|
17
24
|
* pages that fit it best (`top` of them); `template` names one instead.
|
|
18
|
-
* With neither, the default pages: the sample app page
|
|
19
|
-
*
|
|
25
|
+
* With neither, the default pages: the sample app page, every component,
|
|
26
|
+
* and the theme's generated colors when it has any.
|
|
27
|
+
* Every check runs before the first write, so a refusal changes nothing;
|
|
28
|
+
* the files are staged and renamed into place together.
|
|
20
29
|
* @experimental
|
|
21
30
|
* @param {string} [description]
|
|
22
31
|
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewAddOptions} [options]
|
|
@@ -37,7 +46,8 @@ export function themeEditPreviewAdd(description?: string, options?: import("./th
|
|
|
37
46
|
export function themeEditPreviewApp(dir: string, options?: import("./theme-edit.type.mjs").ThemeEditPreviewAppOptions): Promise<import("./theme-edit.type.mjs").ThemeEditPreviewAppResponse>;
|
|
38
47
|
/**
|
|
39
48
|
* The pages of the in-app theme preview. With no preview, and with no
|
|
40
|
-
* routes folder for one, the list is empty.
|
|
49
|
+
* routes folder for one, the list is empty; a damaged theme-preview.json is
|
|
50
|
+
* refused, naming the file.
|
|
41
51
|
* @experimental
|
|
42
52
|
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewListOptions} [options]
|
|
43
53
|
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewListResponse>}
|
|
@@ -45,8 +55,9 @@ export function themeEditPreviewApp(dir: string, options?: import("./theme-edit.
|
|
|
45
55
|
export function themeEditPreviewList(options?: import("./theme-edit.type.mjs").ThemeEditPreviewListOptions): Promise<import("./theme-edit.type.mjs").ThemeEditPreviewListResponse>;
|
|
46
56
|
/**
|
|
47
57
|
* Remove one page from the in-app theme preview, or with no page the whole
|
|
48
|
-
* preview
|
|
49
|
-
* folder.
|
|
58
|
+
* preview. Only files the preview wrote are deleted, only inside the route
|
|
59
|
+
* folder, and never one changed by hand since: those are kept and named.
|
|
60
|
+
* Every check, the theme included, runs before the first delete.
|
|
50
61
|
* @experimental
|
|
51
62
|
* @param {string} [page]
|
|
52
63
|
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewListOptions} [options]
|
|
@@ -58,9 +69,19 @@ export type PreviewPage = {
|
|
|
58
69
|
label: string;
|
|
59
70
|
file: string;
|
|
60
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* `hashes`: a short hash of each file as the preview wrote it, so a file
|
|
74
|
+
* changed by hand is never replaced or deleted
|
|
75
|
+
*/
|
|
61
76
|
export type PreviewData = {
|
|
62
77
|
version: 1;
|
|
63
78
|
theme: string | null;
|
|
64
79
|
pages: PreviewPage[];
|
|
65
80
|
files: string[];
|
|
81
|
+
hashes?: Record<string, string>;
|
|
66
82
|
};
|
|
83
|
+
/**
|
|
84
|
+
* the command again, with
|
|
85
|
+
* this --path (null: none)
|
|
86
|
+
*/
|
|
87
|
+
export type Retry = (path: string | null) => string;
|