cornfield 0.43.0 → 0.45.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.
@@ -9,10 +9,13 @@
9
9
  * - `shownByProposal(state, node)`: a node the open proposal touches is always drawn, whatever its layer says, so a review never hides its
10
10
  * own change.
11
11
  *
12
+ * - `touchedLayerKeys`, `reviewLayers`, `layersOf`, `layerOn`, `reviewHides` (issue #232): while a proposal is under review the map is narrowed to the
13
+ * change. The layers the proposal does not touch are off for the review; the viewer's own state (`state.activeLayers`) is never written.
14
+ *
12
15
  * The matching itself is core/layers.js (data only); this file only hands it the map's ownership line.
13
16
  */
14
17
 
15
- import { kindLetters, layerKeyOf, parentIndex, projectLayerOf } from '../core/layers.js';
18
+ import { STARTING_LAYERS, kindLetters, layerKeyOf, parentIndex, projectLayerOf } from '../core/layers.js';
16
19
  import { touchedIds } from './proposal-model.js';
17
20
 
18
21
  /** Asks the ownership line of the map the state holds, only when a layer matches by `parent` */
@@ -57,3 +60,90 @@ export function lettersOf(state, node) {
57
60
  const t = hit.map.get(node.type);
58
61
  return (t && t.get(node.kind)) || '';
59
62
  }
63
+
64
+ // ---------------------------------------------------------------------------
65
+ // The review narrows the map to the change (issue #232)
66
+ // ---------------------------------------------------------------------------
67
+
68
+ /** Layers a touched layer cannot show without (scene.js `layerShows`): Core needs Packages, Stores and ROM need Data Flow */
69
+ const NEEDS = Object.freeze({ core: ['pkg'], store: ['flow'], rom: ['flow'] });
70
+ const marked = (item) => !!item && !!item.mark && item.mark !== 'none';
71
+
72
+ /**
73
+ * The keys of the layers a proposal touches: the layer of every node it marks (added, changed, removed, or a what-if edit) and of both ends of an
74
+ * edge it marks, the Data Flow overlay when a marked edge is a flow, the Runs overlay when a touched node has a run in flight, and what a touched
75
+ * layer cannot be drawn without (`NEEDS`). The root has no layer. Pure: the map, what the layers are, the runs.
76
+ * @param {{ nodes: {id: string, mark?: string}[], edges: {from: string, to: string, kind?: string, mark?: string}[], rowOf: (node: object) => string | null, flight?: { has(id: string): boolean } | null }} map
77
+ * @returns {Set<string>}
78
+ */
79
+ export function touchedLayerKeys({ nodes, edges, rowOf, flight = null }) {
80
+ const { touched } = touchedIds(nodes, edges);
81
+ const keys = new Set();
82
+ for (const n of nodes) {
83
+ if (!touched.has(n.id)) continue;
84
+ const key = rowOf(n);
85
+ if (key) { keys.add(key); for (const need of NEEDS[key] || []) keys.add(need); }
86
+ if (flight && flight.has(n.id)) keys.add('runs');
87
+ }
88
+ for (const e of edges) if (marked(e) && e.kind === 'flow') keys.add('flow');
89
+ return keys;
90
+ }
91
+
92
+ /**
93
+ * The layers a review shows, from the viewer's own (`active`), as a new map. Untouched layers are off and touched ones on (a layer the viewer had
94
+ * off comes on for the review); then what the reviewer did by hand (`manual`) and what a playing scenario turned on (`step`). With `whole` the map
95
+ * is the viewer's as it always was, with only the hand and the scenario laid over it. `universe`: every layer key the menu knows.
96
+ * @param {{ active: Record<string, boolean>, touched: Set<string>, universe: Iterable<string>, review: { whole?: boolean, manual?: Record<string, boolean>, step?: Record<string, boolean> } }} a
97
+ * @returns {Record<string, boolean>}
98
+ */
99
+ export function reviewLayers({ active, touched, universe, review }) {
100
+ const out = { ...active };
101
+ if (!review.whole && touched.size) for (const key of universe) out[key] = touched.has(key); // a proposal that touches no layer has nothing to narrow the map to: it is left whole
102
+ Object.assign(out, review.manual || {}, review.step || {});
103
+ return out;
104
+ }
105
+
106
+ const layerKeysMemo = new WeakMap();
107
+ /** The touched layer keys of the open proposal's map: kept while the map, the runs and the layers' ownership are the same */
108
+ function touchedLayers(state) {
109
+ const nodes = state.nodes;
110
+ const edges = state.edges || [];
111
+ const flight = state.runsMap ? state.runsMap.flight : null;
112
+ const hit = layerKeysMemo.get(nodes);
113
+ if (hit && hit.edges === edges && hit.n === nodes.length && hit.e === edges.length && hit.flight === flight && hit.project === state.projectLayers) return hit;
114
+ const touched = touchedLayerKeys({ nodes, edges, rowOf: (n) => rowKeyOf(state, n), flight });
115
+ const universe = new Set([...STARTING_LAYERS.map((l) => l.key), ...(state.projectLayers || []).map((l) => l.key), ...nodes.map((n) => rowKeyOf(state, n)).filter(Boolean)]);
116
+ const made = { edges, n: nodes.length, e: edges.length, flight, project: state.projectLayers, touched, universe };
117
+ layerKeysMemo.set(nodes, made);
118
+ return made;
119
+ }
120
+
121
+ const layersMemo = new WeakMap();
122
+ /**
123
+ * The layers the map is drawn with: `state.activeLayers` itself with no review open, else the review's (`reviewLayers`). Every reader of a layer's
124
+ * state for what is drawn goes through this, so the review's hiding is decided in one place.
125
+ */
126
+ export function layersOf(state) {
127
+ const review = state.review;
128
+ if (!review || !state.proposal) return state.activeLayers;
129
+ const hit = layersMemo.get(review);
130
+ if (hit && hit.rev === review.rev && hit.version === state.sceneVersion && hit.active === state.activeLayers && hit.nodes === state.nodes) return hit.out;
131
+ const { touched, universe } = touchedLayers(state);
132
+ const out = reviewLayers({ active: state.activeLayers, touched, universe, review });
133
+ layersMemo.set(review, { rev: review.rev, version: state.sceneVersion, active: state.activeLayers, nodes: state.nodes, out });
134
+ return out;
135
+ }
136
+
137
+ /** Whether a layer is on for what is drawn (a layer with no entry is on, as the menu shows it) */
138
+ export const layerOn = (state, key) => layersOf(state)[key] !== false;
139
+
140
+ /** Whether the review hid this layer, which the viewer has on: the row says so, and the reviewer may turn it on for the review */
141
+ export function reviewHides(state, key) {
142
+ const r = state.review;
143
+ if (!r || !state.proposal || r.whole) return false;
144
+ if (state.activeLayers[key] === false || Object.hasOwn(r.manual || {}, key) || Object.hasOwn(r.step || {}, key)) return false;
145
+ return layersOf(state)[key] === false;
146
+ }
147
+
148
+ /** Whether the greying is on: a review is open, the whole map is not asked for, and the proposal involves something (a proposal that changes nothing greys nothing: scene.js, map-style.js) */
149
+ export const greying = (state) => !!state.review && !!state.proposal && !state.review.whole && touchedBy(state).size > 0;
@@ -15,24 +15,24 @@ import { themed } from './theme-model.js';
15
15
  import { onThemeChange } from './theme.js';
16
16
  import { NODE_THEMES } from '../core/schema.js';
17
17
  import { STARTING_LAYERS, clientLayers, kindLetters, orderRows, startingLayerOf } from '../core/layers.js';
18
- import { rowKeyOf, shownByProposal } from './layer-model.js';
19
- import { layerShows } from './scene.js';
20
- import { getMapStyle, groupToken, setMapSubjects } from './map-style.js';
18
+ import { layerOn, reviewHides, rowKeyOf, shownByProposal } from './layer-model.js';
19
+ import { fillOfNode, groupOf, layerShows } from './scene.js';
20
+ import { STROKE_TOKEN, cssColor, fillOf, getMapStyle, kindToken, onMapStyleChange, pageReader, setMapSubjects } from './map-style.js';
21
+ import { over, parseColor, contrast } from '../core/color.js';
22
+ import { DARK_GROUND, LIGHT_GROUND } from './theme-model.js';
21
23
  import { t } from './strings.js';
22
24
 
23
- /**
24
- * The dot of each starting row: the colour its nodes are drawn in, read from the node types' own colours (core/schema.js), so the dots and
25
- * the map cannot drift apart when the palette moves (a project layer's dot is its nodes' own, or the theme's group token)
26
- */
27
- const COLORS = {
28
- ...Object.fromEntries(Object.entries(NODE_THEMES).map(([type, { color }]) => [type, color])),
29
- flow: NODE_THEMES.store.color, cloud: NODE_THEMES.external.color, web: NODE_THEMES.external.color, house: NODE_THEMES.floor.color, runs: '#5eead4',
30
- };
25
+ /** The types a starting row's nodes have (House: floors and rooms; Cloud and the Public web: `external`), or none for a row that is not a node type */
26
+ const typesOf = (l) => (l.match && l.match.type ? [].concat(l.match.type) : []);
27
+ /** The dot of the two rows that are not a node type: Data Flow's lines and the ring of a run in flight. Every other row's built-in dot is its
28
+ * first type's colour in core/schema.js NODE_THEMES, the colour the map draws its nodes in when the theme sets none (one table, not two) */
29
+ const NOT_A_TYPE = { flow: '#fb923c', runs: '#5eead4' };
30
+ const builtIn = (l) => { const type = typesOf(l)[0]; return type && NODE_THEMES[type] ? NODE_THEMES[type].color : NOT_A_TYPE[l.key]; };
31
31
  /** The starting layers that start off: the infra layer is its own view, and the physical world's are loaded when first turned on (spec 0029) */
32
32
  const STARTS_OFF = new Set(['stack', 'machine', 'container', 'device', 'house', 'persona']);
33
33
 
34
34
  /** The starting rows (core/layers.js STARTING_LAYERS) with the colour of their dot */
35
- export const LAYER_DEFS = STARTING_LAYERS.map((l) => ({ ...l, color: COLORS[l.key] }));
35
+ export const LAYER_DEFS = STARTING_LAYERS.map((l) => ({ ...l, color: builtIn(l) }));
36
36
 
37
37
  const layerListeners = new Set();
38
38
  /** Calls `fn` after a layer goes on or off (live.js starts or stops the live layer). Returns how to stop. */
@@ -79,24 +79,56 @@ export function clickWithLegendMenu(target, act) {
79
79
 
80
80
  /** What a layer is when the viewer has not touched it: the config's `off`, else a starting layer's own default, else on */
81
81
  const defaultOf = (key) => (Object.hasOwn(state.layerDefaults, key) ? state.layerDefaults[key] : !STARTS_OFF.has(key));
82
- const isOn = (key) => state.activeLayers[key] !== false;
83
-
84
- /**
85
- * A project layer's dot: the theme's `--corn-map-group-<key>`, else the colour of the first node the layer holds (a project layer has no
86
- * colour of its own in the config: it is the theme's to give, so the contrast check sees it).
87
- */
88
- function projectColor(layer) {
89
- const set = getMapStyle().values[groupToken(layer.key)];
90
- if (set) return set;
91
- const first = state.nodes.find((n) => rowKeyOf(state, n) === layer.key);
92
- return first ? first.color : NO_COLOR;
93
- }
82
+ /** Whether a layer is on for what is drawn: the viewer's, or the review's while a proposal is under review (layer-model.js) */
83
+ const isOn = (key) => layerOn(state, key);
94
84
 
95
85
  const NO_COLOR = '#94a3b8';
96
86
  /** A colour as the dot's style may hold it: a hex or an rgb()/rgba() of numbers, else the plain grey. A node's colour is the map's data
97
87
  * (a proposal can set it), so it is checked before it reaches a style attribute, and escaped there all the same */
98
- const SWATCH_RE = /^(#[0-9a-f]{3,8}|rgba?\([0-9.,%\s/]+\))$/i;
99
- const swatch = (c) => (typeof c === 'string' && SWATCH_RE.test(c.trim()) ? c.trim() : NO_COLOR);
88
+ const NUM = '\\d{1,3}(?:\\.\\d+)?%?';
89
+ const SWATCH_RE = new RegExp(`^(?:#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|rgba?\\(\\s*${NUM}(?:\\s*,\\s*|\\s+)${NUM}(?:\\s*,\\s*|\\s+)${NUM}(?:\\s*[,/]\\s*(?:\\d*\\.)?\\d+%?)?\\s*\\))$`, 'i');
90
+ const swatch = (c) => (typeof c === 'string' && c.length <= 64 && SWATCH_RE.test(c.trim()) ? c.trim() : NO_COLOR);
91
+
92
+ /**
93
+ * The colour of a row's dot: the fill the map draws for the row's nodes, by the map's own order (map-style.js `fillOf`: the type's or layer's
94
+ * token, then the starting group's), else the row's built-in colour on this ground (`themed`). `node` is what the row's nodes are: for a
95
+ * starting row its type (`typeOfRow`: a row of two types, House, speaks for its first, floors, so `--corn-map-group-house` is its dot unless
96
+ * the theme gives floors a colour of their own) and its key as the group; for a project row its first node's type with the layer. A finer kind's token is left out: a dot is a row's, not a kind's.
97
+ * A token's value is the theme's text and a node's colour is the project's: both go through `swatch` before they reach a style.
98
+ */
99
+ function rowColor(node, built) {
100
+ const fill = fillOf(getMapStyle().values, node);
101
+ // The built-in is a node's colour (project data): normalised as the map's renderers read it (`cssColor`, the reader of the tokens), so a
102
+ // name or an `hsl()` the map draws is the dot too; anything that is not a colour is the grey
103
+ return fill ? swatch(fill) : swatch(cssColor(themed(built, state.theme)));
104
+ }
105
+ /** The type a row's dot speaks for: of a row of several types (House), the first one the map holds, else the first; a row with no type, its key */
106
+ const typeOfRow = (l) => { const types = typesOf(l); return (types.length > 1 && types.find((ty) => state.nodes.some((n) => n.type === ty))) || types[0] || l.key; };
107
+
108
+ /** The ground of the menu: its surface laid over the page, as `--corn-surface-1` and `--corn-ground` say, else the theme's own ground */
109
+ function menuSurface() {
110
+ const fallback = state.theme === 'light' ? LIGHT_GROUND : DARK_GROUND;
111
+ const read = pageReader();
112
+ if (!read) return fallback;
113
+ const ground = parseColor(read('--corn-ground'));
114
+ const surface = parseColor(read('--corn-surface-1'));
115
+ if (!ground[3] || !surface[3]) return fallback;
116
+ const [r, g, b] = over(surface, over(ground, [0, 0, 0, 1]));
117
+ return `rgb(${r}, ${g}, ${b})`;
118
+ }
119
+ /** The outline a dot or swatch gets when its fill is under 3:1 on the menu: the map's own outline colour when the theme sets one, else the CSS default (`.is-ringed`). The fill is never changed */
120
+ function ringOf(color, surface) {
121
+ if (contrast(color, surface) >= 3) return null;
122
+ const stroke = getMapStyle().values[STROKE_TOKEN];
123
+ return { stroke: stroke ? swatch(stroke) : '' };
124
+ }
125
+ /** A swatch of `color` on this menu: the fill, a soft glow on a dark ground, and the outline when it is needed. `extra` is more attributes (a kind's is `aria-hidden`) */
126
+ function dot(color, on, surface, extra = '') {
127
+ const ring = ringOf(color, surface);
128
+ const glow = on && state.theme !== 'light' ? (/^#[0-9a-f]{6}$/i.test(color) ? `0 0 8px ${color}90` : `0 0 8px color-mix(in srgb, ${color} 56%, transparent)`) : 'none';
129
+ const style = `background-color: ${color}; box-shadow: ${glow}${ring && ring.stroke ? `; --hx-dot-ring: ${ring.stroke}` : ''}`;
130
+ return `<span class="hx-dot${extra ? ' hx-kind-swatch' : ''}${ring ? ' is-ringed' : ''}" data-corn="swatch"${extra} style="${escHtml(style)}"></span>`;
131
+ }
100
132
 
101
133
  /**
102
134
  * The rows: the project layers that hold a node, then the starting ones in their usual order (`layers.order` first of all), and one more
@@ -104,6 +136,7 @@ const swatch = (c) => (typeof c === 'string' && SWATCH_RE.test(c.trim()) ? c.tri
104
136
  * `root` is always shown and has no row. The overlays are the rows with `overlay`: the menu draws them under their own heading.
105
137
  */
106
138
  function layerRows() {
139
+ const dotted = (l) => { const type = typeOfRow(l); return { ...l, color: rowColor({ type, group: l.key }, NODE_THEMES[type] ? NODE_THEMES[type].color : l.color) }; };
107
140
  const held = new Set();
108
141
  const left = new Set(); // starting layers a project layer took nodes from
109
142
  state.nodes.forEach((n) => {
@@ -113,17 +146,21 @@ function layerRows() {
113
146
  const own = startingLayerOf(n);
114
147
  if (own !== key) left.add(own);
115
148
  });
116
- const project = state.projectLayers.filter((l) => held.has(l.key)).map((l) => ({ key: l.key, label: l.label, desc: l.desc || '', project: true, color: projectColor(l) }));
149
+ const projectDot = (l) => {
150
+ const first = state.nodes.find((n) => rowKeyOf(state, n) === l.key);
151
+ return { color: rowColor(first ? { type: first.type, layer: l.key, group: groupOf(first) } : { type: l.key, layer: l.key }, first ? fillOfNode(first) : NO_COLOR) };
152
+ };
153
+ const project = state.projectLayers.filter((l) => held.has(l.key)).map((l) => ({ key: l.key, label: l.label, desc: l.desc || '', project: true, ...projectDot(l) }));
117
154
  // A starting row a project layer has taken every node from goes (spec 0009 D7, example 3); with no project layer, every starting row stays, as it always was
118
- const starting = LAYER_DEFS.filter((l) => !l.overlay && (held.has(l.key) || !left.has(l.key)));
155
+ const starting = LAYER_DEFS.filter((l) => !l.overlay && (held.has(l.key) || !left.has(l.key))).map(dotted);
119
156
  const known = new Set([...LAYER_DEFS.map((l) => l.key), ...state.projectLayers.map((l) => l.key)]);
120
157
  const extra = [];
121
158
  held.forEach((key) => {
122
159
  if (known.has(key)) return;
123
160
  known.add(key);
124
- extra.push({ key, label: key.charAt(0).toUpperCase() + key.slice(1), color: '#94a3b8', desc: `Nodes of the type ${key}` });
161
+ extra.push({ key, label: key.charAt(0).toUpperCase() + key.slice(1), color: rowColor({ type: key, group: key }, NO_COLOR), desc: `Nodes of the type ${key}` });
125
162
  });
126
- return { layers: [...orderRows(project, starting, state.layerOrder), ...extra], overlays: LAYER_DEFS.filter((l) => l.overlay) };
163
+ return { layers: [...orderRows(project, starting, state.layerOrder), ...extra], overlays: LAYER_DEFS.filter((l) => l.overlay).map(dotted) };
127
164
  }
128
165
 
129
166
  /** The chip says how many layers are on: " · 13 of 20". An overlay (Runs in flight) is not a layer: it is not counted */
@@ -198,20 +235,28 @@ function kindsByRow(rowOf) {
198
235
  if (!key) return;
199
236
  if (!rows.has(key)) rows.set(key, new Map());
200
237
  const id = `${n.type}\u0000${n.kind}`;
201
- const entry = rows.get(key).get(id) || { kind: n.kind, letters: letters.get(n.type).get(n.kind), n: 0 };
238
+ const entry = rows.get(key).get(id) || { type: n.type, kind: n.kind, letters: letters.get(n.type).get(n.kind), n: 0 };
202
239
  entry.n++;
203
240
  rows.get(key).set(id, entry);
204
241
  });
205
242
  return new Map([...rows].map(([k, m]) => [k, [...m.values()].sort((a, b) => a.kind.localeCompare(b.kind))]));
206
243
  }
207
244
 
245
+ /** The swatch of a kind: the theme's `--corn-map-kind-<type>-<kind>`, as the map draws that kind, or nothing when the token is unset (the node then has its type's colour, which is the row's dot). Decoration: the letters and the name say which kind it is */
246
+ function kindSwatch(k, surface, pad) {
247
+ const fill = getMapStyle().values[kindToken(k.type, k.kind)];
248
+ if (!fill) return pad ? '<span class="hx-dot hx-kind-swatch is-blank" aria-hidden="true"></span>' : ''; // nothing to see, but the letters of the rows stay in line
249
+ return dot(swatch(fill), true, surface, ' aria-hidden="true"');
250
+ }
251
+
208
252
  /** What a viewer sees as "kinds" for a row: the disclosure button and, when open, the key rows. A key is a key, not a toggle (spec 0009 D5) */
209
- function kindsHtml(layer, kinds) {
253
+ function kindsHtml(layer, kinds, surface) {
210
254
  if (!kinds || !kinds.length) return '';
211
255
  const open = openKinds.has(layer.key);
212
256
  const id = `layer-kinds-${layer.key}`;
213
257
  const words = `${kinds.length} ${kinds.length === 1 ? 'kind' : 'kinds'}`;
214
- const list = open ? `<ul class="hx-kind-keys" id="${escHtml(id)}">${kinds.map((k) => `<li class="hx-kind-key" data-corn="kind-key"><span class="hx-kind-letter" aria-hidden="true">${escHtml(k.letters)}</span><span class="hx-kind-name">${escHtml(k.kind)}</span><span class="hx-count" data-corn="count">${k.n}</span></li>`).join('')}</ul>` : '';
258
+ const padded = kinds.some((k) => getMapStyle().values[kindToken(k.type, k.kind)]);
259
+ const list = open ? `<ul class="hx-kind-keys" id="${escHtml(id)}">${kinds.map((k) => `<li class="hx-kind-key" data-corn="kind-key">${kindSwatch(k, surface, padded)}<span class="hx-kind-letter" aria-hidden="true">${escHtml(k.letters)}</span><span class="hx-kind-name">${escHtml(k.kind)}</span><span class="hx-count" data-corn="count">${k.n}</span></li>`).join('')}</ul>` : '';
215
260
  return `<div class="hx-kinds"><button type="button" class="hx-textbtn hx-kinds-btn" data-action="toggle-layer-kinds" data-layer="${escHtml(layer.key)}" aria-expanded="${open}" aria-controls="${escHtml(id)}" data-corn="layer-kinds" data-corn-kind="quiet">${open ? '▾' : '▸'} ${escHtml(words)}</button>${list}</div>`;
216
261
  }
217
262
 
@@ -227,8 +272,10 @@ function syncMapSubjects() {
227
272
  setMapSubjects({ kinds, layers: state.projectLayers.map((l) => l.key) });
228
273
  }
229
274
 
275
+ let drawing = false;
230
276
  export function renderLayerToggles() {
231
- syncMapSubjects();
277
+ drawing = true;
278
+ try { syncMapSubjects(); } finally { drawing = false; } // reading the tokens of a new kind may tell the style listener below: this draw already has them
232
279
  paintCount();
233
280
  const container = document.getElementById('layer-toggles-container');
234
281
  if (!container) return;
@@ -248,22 +295,25 @@ export function renderLayerToggles() {
248
295
  counts.runs = state.runsMap ? state.runsMap.flight.size : 0;
249
296
  const kinds = kindsByRow(rowOf);
250
297
  const { layers, overlays } = layerRows();
298
+ const surface = menuSurface();
251
299
 
252
300
  const row = (layer) => {
253
301
  const on = isOn(layer.key);
254
- const color = escHtml(swatch(themed(swatch(layer.color), state.theme))); // the dot is the colour the map draws on this ground
255
302
  const shown = byProposal[layer.key] ? `<span class="hx-faint hx-layer-note" data-corn="layer-note">+${byProposal[layer.key]} shown by the proposal</span>` : '';
303
+ // A row the review hid says so under its name (there is no room beside it): it can still be turned on, for the review
304
+ const hid = !shown && counts[layer.key] && reviewHides(state, layer.key) ? `<span class="hx-faint hx-layer-note" data-corn="layer-note">${escHtml(t('hiddenWhileReviewing'))}</span>` : '';
305
+ const name = `<span class="hx-toggle-name">${escHtml(layer.label)}</span>`;
256
306
  return `
257
307
  <button type="button" class="hx-toggle-row${on ? '' : ' is-off'}" data-action="toggle-layer" data-layer="${escHtml(layer.key)}" aria-pressed="${on}" title="${escHtml(layer.desc)}" data-corn="row">
258
308
  <span class="hx-toggle-label">
259
- <span class="hx-dot" data-corn="swatch" style="background-color: ${color}; box-shadow: ${on && state.theme !== 'light' ? `0 0 8px ${color}90` : 'none'}"></span>
260
- <span class="hx-toggle-name">${escHtml(layer.label)}</span>${layer.live ? '<span class="hx-live-tag" data-corn="live-tag">live</span>' : ''}
309
+ ${dot(swatch(layer.color), on, surface)}
310
+ ${hid ? `<span class="hx-toggle-text">${name}${hid}</span>` : name}${layer.live ? '<span class="hx-live-tag" data-corn="live-tag">live</span>' : ''}
261
311
  </span>
262
312
  <span class="hx-toggle-meta">${shown}
263
313
  <span class="hx-count" data-corn="count">${(UNIT[layer.key] || String)(counts[layer.key] || 0)}</span>
264
314
  <span class="hx-tick">${on ? '✓' : '—'}</span>
265
315
  </span>
266
- </button>${kindsHtml(layer, kinds.get(layer.key))}`;
316
+ </button>${kindsHtml(layer, kinds.get(layer.key), surface)}`;
267
317
  };
268
318
 
269
319
  // The focus stays where it was across the redraw: the same row or disclosure, found again by what it names
@@ -290,7 +340,7 @@ export function renderEdgeKey() {
290
340
  export function renderCloudLegend() {
291
341
  const el = document.getElementById('cloud-legend');
292
342
  if (!el) return;
293
- if (state.activeLayers.cloud === false) { el.innerHTML = ''; el.hidden = true; return; }
343
+ if (!layerOn(state, 'cloud')) { el.innerHTML = ''; el.hidden = true; return; }
294
344
  el.hidden = false;
295
345
  el.innerHTML = `
296
346
  <div class="hx-menu-title" data-corn="menu-title"><span>Cloud: when an outage bites</span></div>
@@ -315,35 +365,98 @@ function say(text) {
315
365
  }
316
366
 
317
367
  /**
318
- * Turns a layer on or off. `viewer`: the person chose it in the menu, so it is kept in their browser; a layer another part of the app
319
- * turns on (a deploy that lights a stack) is not a choice of theirs and is not kept.
368
+ * Sets a layer on or off. `viewer`: the person chose it in the menu, so it is kept in their browser; a layer another part of the app
369
+ * turns on (a deploy that lights a stack) is not a choice of theirs and is not kept. While a proposal is under review the choice is the review's:
370
+ * kept for the review only (`state.review.manual`, or `.step` when a playing scenario needs the layer: `scenario`), never in `activeLayers` or the browser.
320
371
  */
321
- export function toggleLayer(key, { viewer = false } = {}) {
322
- const on = state.activeLayers[key] === false; // a layer with no entry yet is on, as the toggles show it
323
- state.activeLayers[key] = on;
324
- if (viewer) { keep(key, on); writeChanges(); }
372
+ export function setLayer(key, on, { viewer = false, scenario = false } = {}) {
373
+ const review = state.review && state.proposal ? state.review : null;
374
+ if (review) {
375
+ (scenario ? review.step : review.manual)[key] = on;
376
+ review.rev++;
377
+ } else {
378
+ state.activeLayers[key] = on;
379
+ if (viewer) { keep(key, on); writeChanges(); }
380
+ }
325
381
  changed();
326
382
  if (viewer) {
327
383
  const { layers, overlays } = layerRows();
328
384
  const layer = [...layers, ...overlays].find((l) => l.key === key);
329
385
  const n = state.nodes.filter((x) => isNodeVisible(x)).length;
330
- say(`${layer ? layer.label : key} ${on ? 'on' : 'off'}. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
386
+ say(`${layer ? layer.label : key} ${on ? 'on' : 'off'}${review ? ' for this review' : ''}. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
331
387
  }
332
388
  }
333
389
 
334
- /** All / None: every layer on or off, never an overlay; the root stays drawn */
390
+ /** Flips a layer (what a click on its row does); see `setLayer` */
391
+ export function toggleLayer(key, opts = {}) {
392
+ setLayer(key, !layerOn(state, key), opts);
393
+ }
394
+
395
+ /** All / None: every layer on or off, never an overlay; the root stays drawn. During a review it is the review's state that changes, not the viewer's */
335
396
  export function toggleAllLayers(on, { viewer = false } = {}) {
336
397
  const { layers } = layerRows();
337
398
  const keys = new Set([...LAYER_DEFS.filter((l) => !l.overlay).map((l) => l.key), ...state.projectLayers.map((l) => l.key), ...layers.map((l) => l.key)]);
338
- keys.forEach((k) => { state.activeLayers[k] = on; if (viewer) keep(k, on); });
339
- if (viewer) writeChanges();
399
+ const review = state.review && state.proposal ? state.review : null;
400
+ if (review) { keys.forEach((k) => { review.manual[k] = on; }); review.rev++; } else {
401
+ keys.forEach((k) => { state.activeLayers[k] = on; if (viewer) keep(k, on); });
402
+ if (viewer) writeChanges();
403
+ }
340
404
  changed();
341
405
  if (viewer) {
342
406
  const n = state.nodes.filter((x) => isNodeVisible(x)).length;
343
- say(`${on ? 'All' : 'No'} layers on. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
407
+ say(`${on ? 'All' : 'No'} layers on${review ? ' for this review' : ''}. ${n} ${n === 1 ? 'node' : 'nodes'} shown.`);
344
408
  }
345
409
  }
346
410
 
411
+ // ---------------------------------------------------------------------------
412
+ // The review narrows the map to the change (issue #232)
413
+ // The hiding is the review's, never the viewer's: `state.review` holds it (layer-model.js `layersOf` reads it), `activeLayers` and the saved choices are not touched.
414
+ // ---------------------------------------------------------------------------
415
+
416
+ /** A proposal is opened: a fresh review ("Show the whole map" off, nothing turned on by hand yet), also when one was already open */
417
+ export function beginReview() {
418
+ state.review = { whole: false, manual: {}, step: {}, rev: 0 }; // the map and the menu are drawn by the caller, once the proposal is on the map (proposal.js rebuildProposalMap)
419
+ }
420
+
421
+ /** The proposal is closed: the viewer's own layers are what is drawn again, exactly as they were */
422
+ export function endReview() {
423
+ if (!state.review) return;
424
+ state.review = null;
425
+ touchScene();
426
+ renderLayerToggles();
427
+ }
428
+
429
+ /** What a screen reader hears when a review opens and hid layers: "Showing 4 of 22 layers: the ones this proposal changes." Nothing when no layer was hidden */
430
+ export function reviewLayersLine() {
431
+ if (!state.review || !state.proposal || state.review.whole) return '';
432
+ const { layers } = layerRows();
433
+ const held = new Set(state.nodes.map((n) => rowKeyOf(state, n))); // a layer that holds no node has nothing to hide
434
+ if (!layers.some((l) => held.has(l.key) && reviewHides(state, l.key))) return '';
435
+ const on = layers.filter((l) => isOn(l.key)).length;
436
+ return `Showing ${on} of ${layers.length} layers: the ones this proposal changes.`;
437
+ }
438
+
439
+ /** "Show the whole map": on brings back every layer the viewer has on and the proposal's dimming of the rest (Diff as it always was); off narrows to the change again. Said once, here */
440
+ export function setWholeMap(on) {
441
+ const review = state.review && state.proposal ? state.review : null;
442
+ if (!review || review.whole === on) return;
443
+ review.whole = on;
444
+ review.rev++;
445
+ changed();
446
+ say(on ? 'Showing the whole map.' : (reviewLayersLine() || 'Showing the change: the rest of the map is greyed.'));
447
+ }
448
+
449
+ /** A scenario ended: the layers it turned on for the review go off again (the focused view comes back). True when there were any */
450
+ export function clearStepLayers() {
451
+ const review = state.review;
452
+ if (!review || !Object.keys(review.step).length) return false;
453
+ review.step = {};
454
+ review.rev++;
455
+ touchScene();
456
+ renderLayerToggles();
457
+ return true;
458
+ }
459
+
347
460
  /** Opens or closes the kinds of one row ("5 kinds"); the focus stays on its button */
348
461
  export function toggleLayerKinds(key) {
349
462
  if (openKinds.has(key)) openKinds.delete(key); else openKinds.add(key);
@@ -352,3 +465,5 @@ export function toggleLayerKinds(key) {
352
465
 
353
466
  // The dots and the legends are drawn in the theme's colours: drawn again when it changes (theme.js)
354
467
  onThemeChange(() => renderLayerToggles());
468
+ // ... and when the map's own tokens change (a named theme, its stylesheet arriving, light and dark): the same signal the map is drawn again on
469
+ onMapStyleChange(() => { if (!drawing) renderLayerToggles(); });