cornfield 0.26.0 → 0.27.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/core/config.js CHANGED
@@ -26,7 +26,7 @@
26
26
  * hookPoll: { specs: '5m' }, // how often cornfield looks for ready specs and thread activity on its own, for the spec.ready and thread.* hooks; false turns it off (docs/config.md#hook-poll)
27
27
  * threads: { label: 'idea' }, // an idea is an issue with this label, its thread the issue's comments; false turns threads off (docs/threads.md)
28
28
  * links: [], // [{ label, href }] shown in the explorer's header
29
- * theme: {}, // { css, fonts, fontHosts, names, default, contrast }: a repo's own look, CSS only (docs/config.md#theme)
29
+ * theme: {}, // { css, fonts, fontHosts, names, default, icons, pixelated, contrast }: a repo's own look, CSS only (docs/config.md#theme)
30
30
  * brand: {}, // { logo, wordmark, favicon, appName, color, background }: a repo's own logo and name, images from the repo (docs/config.md#brand)
31
31
  * proxy: {}, // { '/api/': 'http://127.0.0.1:3245' }: same-origin paths for a live source
32
32
  * importMap: {}, // bare imports a repo-local browser module needs: { name: '/repo/path.js' }
@@ -168,7 +168,7 @@ export const DEFAULTS = Object.freeze({
168
168
  hookPoll: { specs: '5m' },
169
169
  threads: { label: 'idea' },
170
170
  links: [],
171
- theme: { css: null, fonts: [], fontHosts: [], names: {}, default: null, contrast: 'error' },
171
+ theme: { css: null, fonts: [], fontHosts: [], names: {}, default: null, icons: {}, pixelated: false, contrast: 'error' },
172
172
  brand: { logo: null, wordmark: null, favicon: null, appName: null, color: null, background: null },
173
173
  proxy: {},
174
174
  importMap: {},
package/core/contrast.js CHANGED
@@ -379,6 +379,7 @@ export const PAINTED_TEXT = Object.freeze([
379
379
  ['--corn-text-2', tint('--corn-text-3', 10), 'a check'],
380
380
  ['--corn-text-2', tint('--corn-text-2', 12), 'a web badge'],
381
381
  ['--corn-text-2', '--corn-line-soft', 'a text button, hovered'],
382
+ ['--corn-map-collision', GROUND, 'a collision tag on the map'],
382
383
  ]);
383
384
  /**
384
385
  * The parts of the interface a person needs to see to use it (WCAG 1.4.11), at 3:1: `[fg, bg, what]`. The focus indicators, and the
package/core/theme.js CHANGED
@@ -11,6 +11,8 @@
11
11
  * fontHosts: [], // the hosts of the font FILES, when a fonts host is not a known pair
12
12
  * names: { day: './tools/cornfield/day.css' }, // named themes: data-theme-name on <html>, a picker in the header
13
13
  * default: 'day', // the name used until a viewer picks one
14
+ * icons: { app: './tools/cornfield/icons/app.svg' }, // an icon per node kind, drawn inside the node on the map (svg or png)
15
+ * pixelated: true, // small icons are drawn crisp (nearest-neighbour), not smoothed
14
16
  * contrast: 'error', // 'warn' turns a failing contrast pair in `validate` into a warning
15
17
  * }
16
18
  *
@@ -19,8 +21,13 @@
19
21
 
20
22
  import fs from 'node:fs';
21
23
  import path from 'node:path';
24
+ import { NODE_THEMES } from './schema.js';
22
25
 
23
- export const THEME_KEYS = Object.freeze(['css', 'fonts', 'fontHosts', 'names', 'default', 'contrast']);
26
+ export const THEME_KEYS = Object.freeze(['css', 'fonts', 'fontHosts', 'names', 'default', 'icons', 'pixelated', 'contrast']);
27
+ /** The node kinds an icon may be set for: every node type of the map */
28
+ export const ICON_KINDS = Object.freeze(Object.keys(NODE_THEMES));
29
+ /** The file types of a node icon */
30
+ export const ICON_EXTENSIONS = Object.freeze(['.svg', '.png']);
24
31
  /** The largest a theme file may be, in bytes */
25
32
  export const MAX_THEME_BYTES = 256 * 1024;
26
33
  export const MAX_THEME_NAMES = 12;
@@ -60,7 +67,7 @@ export function fontUrl(v) {
60
67
  */
61
68
  export function themeProblems(t) {
62
69
  if (t === undefined) return [];
63
- if (!isObject(t)) return ['"theme" is { css?, fonts?, fontHosts?, names?, default?, contrast? }.'];
70
+ if (!isObject(t)) return ['"theme" is { css?, fonts?, fontHosts?, names?, default?, icons?, pixelated?, contrast? }.'];
64
71
  const out = [];
65
72
  for (const k of Object.keys(t)) if (!THEME_KEYS.includes(k)) out.push(`theme.${k}: the keys are ${THEME_KEYS.join(', ')}.`);
66
73
  if (t.css !== undefined && t.css !== null && !cssPathInRepo(t.css)) out.push('theme.css is one stylesheet (a .css file) inside the repo, written relative to it, like "./tools/cornfield/theme.css".');
@@ -100,6 +107,48 @@ export function themeProblems(t) {
100
107
  if (!isText(t.default)) out.push('theme.default is the name of one of theme.names.');
101
108
  else if (!names.includes(t.default)) out.push(`theme.default "${t.default}" is not one of theme.names (${names.join(', ') || 'none are listed'}).`);
102
109
  }
110
+ if (t.pixelated !== undefined && typeof t.pixelated !== 'boolean') out.push('theme.pixelated is true or false.');
111
+ if (t.icons !== undefined) {
112
+ if (!isObject(t.icons)) out.push('theme.icons is { "<node kind>": "./path.svg" }.');
113
+ else {
114
+ for (const [kind, rel] of Object.entries(t.icons)) {
115
+ if (!ICON_KINDS.includes(kind)) out.push(`theme.icons "${kind}": not a node kind (${ICON_KINDS.join(', ')}).`);
116
+ const found = themeIconAsset(t, rel);
117
+ if ('problem' in found) out.push(`theme.icons.${kind} ${found.problem}`);
118
+ }
119
+ }
120
+ }
121
+ return out;
122
+ }
123
+
124
+ /**
125
+ * Where a node icon of the config is served, by its repo path: `{ asset }` (the path after THEME_ASSET_PREFIX) or `{ problem }`.
126
+ * An icon is an .svg or .png file inside the repo, and inside the theme folder (themeAssetDir): it is served by the route that serves the
127
+ * images beside a theme, nothing else. No disk is read here (core/validate.js checks the file).
128
+ * @param {object} theme
129
+ * @param {unknown} rel the icon's path as the config wrote it
130
+ * @returns {{ asset: string } | { problem: string }}
131
+ */
132
+ export function themeIconAsset(theme, rel) {
133
+ if (!isText(rel) || path.isAbsolute(rel) || /^[a-z]:/i.test(rel) || rel.includes('\0') || rel.includes('\\') || rel.split('/').includes('..')) return { problem: 'is an .svg or .png file inside the repo, written relative to it, like "./tools/cornfield/icons/app.svg".' };
134
+ if (!ICON_EXTENSIONS.includes(path.posix.extname(rel).toLowerCase())) return { problem: `(${rel}) is not an .svg or .png file.` };
135
+ const dir = themeAssetDir(theme);
136
+ if (!dir) return { problem: `(${rel}) cannot be served: icons are served beside the theme files, so the theme needs a stylesheet (theme.css or theme.names) in a folder, not at the top of the repo.` };
137
+ const norm = path.posix.normalize(rel);
138
+ if (!norm.startsWith(`${dir}/`)) return { problem: `(${rel}) is outside the theme folder ${dir}, the only folder whose images are served; put the icon under it.` };
139
+ const asset = themeAssetPath(norm.slice(dir.length + 1));
140
+ return asset ? { asset } : { problem: `(${rel}) has a name that cannot be served (letters, digits, ".", "_" and "-" only, no segment starting with a dot).` };
141
+ }
142
+
143
+ /** The icons the browser is told: `{ kind: '/cornfield/theme/assets/<path>' }`, those the config names validly */
144
+ export function iconUrls(theme) {
145
+ const out = {};
146
+ if (!theme || !isObject(theme.icons)) return out;
147
+ for (const [kind, rel] of Object.entries(theme.icons)) {
148
+ if (!ICON_KINDS.includes(kind)) continue;
149
+ const found = themeIconAsset(theme, rel);
150
+ if (!('problem' in found)) out[kind] = THEME_ASSET_PREFIX + found.asset;
151
+ }
103
152
  return out;
104
153
  }
105
154
 
@@ -303,11 +352,14 @@ export function themeUrlTarget(ref, servedAt) {
303
352
  return asset ? { asset } : { problem: why };
304
353
  }
305
354
 
306
- /** The theme as the browser is told it: names and the default, never a path on this machine */
355
+ /** The theme as the browser is told it: names and the default, the icons' served paths and the pixelated flag, never a path on this machine */
307
356
  export function clientTheme(theme) {
308
357
  if (!theme) return null;
309
358
  const names = Object.keys(theme.names || {});
310
- return names.length ? { names, default: theme.default || null } : null;
359
+ const icons = iconUrls(theme);
360
+ const pixelated = theme.pixelated === true;
361
+ if (!names.length && !Object.keys(icons).length && !pixelated) return null;
362
+ return { names, default: names.length ? theme.default || null : null, ...(Object.keys(icons).length ? { icons } : {}), ...(pixelated ? { pixelated } : {}) };
311
363
  }
312
364
 
313
365
  /**
@@ -337,3 +389,32 @@ export function themeAssetFile(root, theme, rel) {
337
389
  if (stats.size > MAX_THEME_ASSET_BYTES) return { problem: `${dir}/${rel} is ${Math.ceil(stats.size / 1024)} KB; an asset beside a theme is at most ${MAX_THEME_ASSET_BYTES / 1024 / 1024} MB.` };
338
390
  return { file: real, size: stats.size, type };
339
391
  }
392
+
393
+ /**
394
+ * What is wrong with the icon files of the config (`theme.icons`): each one exists as a plain file under the theme folder (a link out of it is
395
+ * refused), is at most MAX_THEME_ASSET_BYTES, and is what its extension says (an .svg is text with an <svg> element and no script, a .png starts
396
+ * with the PNG signature). The server serves an icon only if themeAssetFile finds it, so this is the same rule.
397
+ * @param {{ root: string, theme?: object }} config
398
+ * @returns {string[]}
399
+ */
400
+ export function themeIconFileProblems(config) {
401
+ const out = [];
402
+ const icons = config.theme && isObject(config.theme.icons) ? config.theme.icons : {};
403
+ for (const [kind, rel] of Object.entries(icons)) {
404
+ const where = `theme.icons.${kind} (${rel})`;
405
+ const target = themeIconAsset(config.theme, rel);
406
+ if ('problem' in target) { out.push(`${where} ${target.problem}`); continue; }
407
+ const found = themeAssetFile(config.root, config.theme, target.asset);
408
+ if ('problem' in found) { out.push(`${where} ${found.problem}`); continue; }
409
+ let head;
410
+ try { head = fs.readFileSync(found.file); } catch { out.push(`${where} cannot be read.`); continue; }
411
+ if (path.posix.extname(target.asset).toLowerCase() === '.png') {
412
+ if (!head.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) out.push(`${where} is not a PNG image.`);
413
+ } else {
414
+ const text = head.toString('utf8');
415
+ if (!/<svg[\s>]/i.test(text)) out.push(`${where} is not an SVG image (no <svg> element).`);
416
+ else if (/<\s*script\b|\son[a-z]+\s*=|javascript\s*:/i.test(text)) out.push(`${where} holds script, which an icon may not (an icon is an image only).`);
417
+ }
418
+ }
419
+ return out;
420
+ }
package/core/validate.js CHANGED
@@ -25,7 +25,7 @@ import { missingPaths, planSync } from './sync.js';
25
25
  import { repoPaths, repoReader } from './repo.js';
26
26
  import { checkThemeContrast } from './contrast.js';
27
27
  import { BRAND_IMAGE_KEYS, brandAssetFile } from './brand.js';
28
- import { MAX_THEME_BYTES, cssProblems, cssUrls, themeAssetFile, themeFileUrl, themeFiles, themeUrlTarget } from './theme.js';
28
+ import { MAX_THEME_BYTES, cssProblems, cssUrls, themeAssetFile, themeFileUrl, themeFiles, themeIconFileProblems, themeUrlTarget } from './theme.js';
29
29
 
30
30
  /** How a page is produced today, and whether it is baked to a file */
31
31
  export const RENDER_NOW = ['build', 'request', 'build+request', 'rom'];
@@ -36,7 +36,8 @@ const ONLY_REMOVES_HINT = '(A proposal that adds nothing cannot show it has been
36
36
  /**
37
37
  * The config's theme files (docs/config.md#theme): each one exists, is a plain file inside the repo (a link out of it is refused),
38
38
  * is at most MAX_THEME_BYTES, is CSS that keeps out of script (core/theme.js `cssProblems`), and every url() in it is a data: image
39
- * or an image or font the server serves beside the theme (core/theme.js `themeUrlTarget`, `themeAssetFile`).
39
+ * or an image or font the server serves beside the theme (core/theme.js `themeUrlTarget`, `themeAssetFile`), and every node icon
40
+ * (`theme.icons`) is an .svg or .png file there (`themeIconFileProblems`).
40
41
  * @param {{ root: string, theme?: object }} config
41
42
  * @returns {string[]}
42
43
  */
@@ -65,6 +66,7 @@ export function validateThemeFiles(config) {
65
66
  }
66
67
  }
67
68
  }
69
+ errors.push(...themeIconFileProblems(config)); // theme.icons: each file is there, served, and an image
68
70
  return errors;
69
71
  }
70
72
 
@@ -804,10 +804,10 @@ a.hx-forge-row:hover, button.hx-forge-row:hover { background: var(--corn-surface
804
804
  .hx-run-overlay.is-static .hx-run-ring { animation: none; transform: none; opacity: 1; border-width: calc(var(--corn-border-strong) + var(--corn-border)); }
805
805
  @media (prefers-reduced-motion: reduce) { .hx-run-ring { animation: none !important; transform: none; opacity: 1; border-width: calc(var(--corn-border-strong) + var(--corn-border)); } }
806
806
  /* Collision: dashed, a sign and words; the same ring whether or not colour is seen */
807
- .hx-run-mark--collision .hx-run-ring { border: calc(var(--corn-border-strong) + var(--corn-border)) dashed var(--corn-warn); animation: none; }
808
- .hx-run-tag { position: absolute; left: 0; top: calc(var(--r) + 4px); transform: translateX(-50%); white-space: nowrap; padding: calc(var(--corn-space-1) / 2) var(--corn-space-2); border-radius: var(--corn-radius-pill); border: var(--corn-border) solid var(--corn-warn); background: color-mix(in srgb, var(--corn-ground) 80%, transparent); color: var(--corn-warn); font-size: var(--corn-text-xs); font-weight: 700; }
807
+ .hx-run-mark--collision .hx-run-ring { border: calc(var(--corn-border-strong) + var(--corn-border)) dashed var(--corn-map-collision); animation: none; }
808
+ .hx-run-tag { position: absolute; left: 0; top: calc(var(--r) + 4px); transform: translateX(-50%); white-space: nowrap; padding: calc(var(--corn-space-1) / 2) var(--corn-space-2); border-radius: var(--corn-radius-pill); border: var(--corn-border) solid var(--corn-map-collision); background: color-mix(in srgb, var(--corn-ground) 80%, transparent); color: var(--corn-map-collision); font-size: var(--corn-text-xs); font-weight: 700; }
809
809
  .hx-run-swatch { display: inline-block; width: 14px; height: 14px; flex: none; box-sizing: border-box; border-radius: 50%; border: var(--corn-border-strong) solid var(--corn-added); }
810
- .hx-run-swatch--collision { border: var(--corn-border-strong) dashed var(--corn-warn); }
810
+ .hx-run-swatch--collision { border: var(--corn-border-strong) dashed var(--corn-map-collision); }
811
811
 
812
812
  /* The Runs view (forge-panel.js, runs-view.js): each run, its limits beside what was reported, its timeline; the compare puts profiles side by side */
813
813
  .hx-runs-list { display: flex; flex-direction: column; gap: var(--corn-space-3); margin: 0; padding: 0; list-style: none; }
@@ -835,3 +835,6 @@ a.hx-forge-row:hover, button.hx-forge-row:hover { background: var(--corn-surface
835
835
  .hx-stage:has(.hx-forge .hx-run-compare):has(.drawer-open) { --hx-forge-w: min(400px, calc(100% - 24px)); }
836
836
  .hx-stage:has(.drawer-open) .hx-run-columns { grid-template-columns: 1fr; }
837
837
  }
838
+
839
+ /* theme.pixelated (docs/config.md#theme): small pixel-art images are sampled without smoothing, in the page as on the map (map-style.js) */
840
+ :root[data-pixelated] img { image-rendering: pixelated; }
@@ -36,6 +36,7 @@
36
36
  */
37
37
 
38
38
  import { EDGE_LABEL_SIZE, FONT_EDGE, FONT_NODE, labelAt, looksFor } from './scene.js';
39
+ import { getMapStyle, styledScene } from './map-style.js';
39
40
 
40
41
  /** At most this many node labels at a time (lane text and selection edge labels are extra) */
41
42
  export const MAX_LABELS = 400;
@@ -68,7 +69,7 @@ const TIER_REST = 3;
68
69
  export function chooseLabels(scene, camera, viewport, opts = {}) {
69
70
  const { max = MAX_LABELS, margin = CULL_MARGIN, measure = null } = opts;
70
71
  const out = [];
71
- const outline = looksFor(scene.theme).labelOutline; // the ground's own colour, under the text
72
+ const outline = scene.labelOutline || looksFor(scene.theme).labelOutline; // the ground's own colour (or the theme's --corn-map-label-halo), under the text
72
73
 
73
74
  // Always there: the data-flow lanes' titles, notes and arrows ...
74
75
  for (const lane of scene.decor.lanes) {
@@ -168,13 +169,16 @@ function coverWithNodes(ctx, nodes) {
168
169
  /**
169
170
  * A transparent 2D canvas that draws the chosen labels over the map
170
171
  * @param {HTMLCanvasElement} canvas
172
+ * @param {{mapStyle?: () => import('./map-style.js').MapStyle}} [opts] `mapStyle`: where the theme's map tokens come from (default: the page's, map-style.js `getMapStyle`);
173
+ * a node's label colour is the theme's `--corn-map-label`, and the outline under every label its `--corn-map-label-halo`, when it sets them
171
174
  * @returns {{draw: (scene: import('./scene.js').Scene, camera: import('./scene.js').Camera) => boolean, resize: (width: number, height: number, dpr: number) => void, destroy: () => void}}
172
175
  * `draw` returns whether it painted: false when the camera, the scene and the size were unchanged
173
176
  */
174
- export function createLabelLayer(canvas) {
177
+ export function createLabelLayer(canvas, opts = {}) {
178
+ const styleOf = opts.mapStyle || getMapStyle;
175
179
  const ctx = canvas.getContext('2d');
176
180
  let width = 0, height = 0, dpr = 1;
177
- let lastScene = null, lastVersion, lastX, lastY, lastZoom;
181
+ let lastScene = null, lastVersion, lastX, lastY, lastZoom, lastStyle;
178
182
  let dirty = true;
179
183
  let destroyed = false;
180
184
 
@@ -202,11 +206,14 @@ export function createLabelLayer(canvas) {
202
206
  dirty = true;
203
207
  },
204
208
 
205
- draw(scene, camera) {
209
+ draw(drawn, camera) {
206
210
  if (destroyed) return false;
207
- if (!dirty && scene === lastScene && scene.version === lastVersion && camera.x === lastX && camera.y === lastY && camera.zoom === lastZoom) return false;
208
- lastScene = scene; lastVersion = scene.version; lastX = camera.x; lastY = camera.y; lastZoom = camera.zoom;
211
+ const style = styleOf();
212
+ const styleKey = style.plain ? 0 : style.version;
213
+ if (!dirty && drawn === lastScene && drawn.version === lastVersion && camera.x === lastX && camera.y === lastY && camera.zoom === lastZoom && styleKey === lastStyle) return false;
214
+ lastScene = drawn; lastVersion = drawn.version; lastX = camera.x; lastY = camera.y; lastZoom = camera.zoom; lastStyle = styleKey;
209
215
  dirty = false;
216
+ const scene = styledScene(drawn, style); // the label colours of the theme (map-style.js)
210
217
 
211
218
  ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
212
219
  ctx.clearRect(0, 0, width, height);
package/explorer/main.js CHANGED
@@ -47,6 +47,7 @@ import { initForgePanel } from './forge-panel.js';
47
47
  import { initReviewTokens } from './review-tokens.js';
48
48
  import { loadClientConfig, loadSources, applyBranding } from './plugins.js';
49
49
  import { cycleTheme, initTheme } from './theme.js';
50
+ import { applyMapTheme, onMapStyleChange, watchMapStyle } from './map-style.js';
50
51
 
51
52
  const $ = (id) => document.getElementById(id);
52
53
 
@@ -186,6 +187,8 @@ async function runPageBench(frames, zoomFactor, sync) {
186
187
  // Start
187
188
  // ---------------------------------------------------------------------------
188
189
  initTheme(); // light or dark, before the first scene is built (theme.js)
190
+ watchMapStyle(document); // the map reads --corn-map-* from the theme, and again when it changes (map-style.js)
191
+ onMapStyleChange(() => render());
189
192
  state.canvas = $('graph-canvas');
190
193
  state.labelCanvas = $('label-canvas');
191
194
  // A 2d context that only measures text (layout.js labelWidth): the map canvas may be WebGL, and a
@@ -204,7 +207,7 @@ $('mobile-node-search').addEventListener('input', (e) => handleSearch(e.target.v
204
207
  resize();
205
208
  syncViewUi();
206
209
  // /?proposal=<slug> opens a proposal over the map, /?view=dataflow the data-flow preset: both have a link
207
- const configReady = loadClientConfig().then((config) => { applyBranding(document, config); return config; });
210
+ const configReady = loadClientConfig().then((config) => { applyBranding(document, config); applyMapTheme(document, config.theme); return config; }); // the brand, then the theme's icons for the map
208
211
  loadTopology().then(async (ok) => {
209
212
  if (!ok) return;
210
213
  // The sources the repo's cornfield.config.js names (plugins.js): a live layer, a house, devices, a checker; each optional