@popcorn245/cornfield 0.46.0 → 0.46.2

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.
Files changed (62) hide show
  1. package/explorer/capped.js +3 -2
  2. package/explorer/engine/feedback.js +49 -0
  3. package/explorer/engine/flow-ui.js +39 -0
  4. package/explorer/engine/fly.js +54 -0
  5. package/explorer/engine/hint.js +97 -0
  6. package/explorer/engine/keyboard.js +156 -0
  7. package/explorer/engine/layers.js +280 -0
  8. package/explorer/engine/layout.js +378 -0
  9. package/explorer/engine/map-style.js +547 -0
  10. package/explorer/engine/piece.js +142 -0
  11. package/explorer/engine/render.js +331 -0
  12. package/explorer/engine/search-status.js +59 -0
  13. package/explorer/engine/selection.js +42 -0
  14. package/explorer/engine/sheets.js +417 -0
  15. package/explorer/engine/strings.js +63 -0
  16. package/explorer/feedback.js +11 -45
  17. package/explorer/flow-style.js +28 -0
  18. package/explorer/flows.js +10 -51
  19. package/explorer/fly.js +11 -43
  20. package/explorer/hint.js +15 -88
  21. package/explorer/index.html +38 -75
  22. package/explorer/input.js +190 -177
  23. package/explorer/instance-state.js +172 -0
  24. package/explorer/instance.js +267 -0
  25. package/explorer/keyboard.js +14 -206
  26. package/explorer/labels.js +3 -3
  27. package/explorer/layers.js +34 -445
  28. package/explorer/layout.js +14 -372
  29. package/explorer/main.js +4 -226
  30. package/explorer/map-style.js +27 -525
  31. package/explorer/mockups-model.js +3 -3
  32. package/explorer/page-pieces.js +61 -0
  33. package/explorer/page.js +232 -0
  34. package/explorer/panels.js +29 -409
  35. package/explorer/pieces/layers.js +325 -0
  36. package/explorer/pieces/node-panel.js +536 -0
  37. package/explorer/pieces/search.js +454 -0
  38. package/explorer/proposal-model.js +1 -1
  39. package/explorer/proposal.js +1 -0
  40. package/explorer/render.js +37 -267
  41. package/explorer/renderer-canvas2d.js +4 -4
  42. package/explorer/renderer-webgl.js +21 -8
  43. package/explorer/renderer.js +11 -9
  44. package/explorer/scene.js +3 -2
  45. package/explorer/search-status.js +8 -47
  46. package/explorer/search.js +26 -324
  47. package/explorer/selection-view.js +17 -19
  48. package/explorer/sheets.js +31 -379
  49. package/explorer/state.js +38 -173
  50. package/explorer/strings.js +17 -60
  51. package/explorer/view.js +4 -5
  52. package/package.json +1 -1
  53. package/server/agent-api.js +13 -6
  54. package/server/agent-runs.js +2 -1
  55. package/server/auth.js +1 -1
  56. package/server/forge-actions.js +34 -24
  57. package/server/forge-routes.js +3 -3
  58. package/server/lander.js +3 -2
  59. package/server/proposals.js +11 -3
  60. package/server/review-api.js +13 -8
  61. package/server/server.js +5 -4
  62. package/server/throttle.js +27 -0
@@ -0,0 +1,232 @@
1
+ /**
2
+ * @module explorer/page
3
+ *
4
+ * cornfield's own page: the shell that mounts the engine in the document (spec 0011 D1, D2). explorer/main.js (which index.html loads) only
5
+ * calls `startPage()`. It makes exactly one instance (instance.js) with the document as its root, starts the map on it, and wires the header,
6
+ * the Layers menu and the side panel to the modules that do the work (some are on the instance already, some still reach it through
7
+ * state.js), loads topology.json, then the sources the repo's config names (plugins.js).
8
+ *
9
+ * This is the one module that may read `location`, write `history` and listen on `document`: the address bar, the click on any
10
+ * `data-action` and the deep links belong to the page, not to a piece. (test/explorer/instance-tripwires.test.js exempts it.)
11
+ *
12
+ * instance.js the instance (state, root, base) engine/layout.js where nodes sit (orbit, lanes, freeform)
13
+ * engine/render.js the camera and the loop flows.js flow styling, legend, the data-flow preset
14
+ * scene.js what is drawn (contract) renderer.js how it is drawn: renderer-webgl.js or renderer-canvas2d.js
15
+ * pieces/ the node panel, the Layers menu and the search: pieces mounted where index.html has their places (page-pieces.js)
16
+ * view.js loading and view switching input.js mouse and touch
17
+ * proposal.js the proposal overlay (picker, Before / Diff / After); proposal-model.js its pure half
18
+ * whatif.js what-if edits on an open proposal (add, remove, draw, steps, undo); whatif-model.js its pure half
19
+ * review.js approve a proposal or send it back with a note and the edits; review-model.js its pure half
20
+ * deep-link.js ?run=, ?issue= and ?proposal=…&start=1 land on a run, an issue and a Start dialog
21
+ * scenario.js the player's pure core scenario-ui.js its bar, keys and camera
22
+ * bench.js /?bench=1 stopwatch and /?fixture=5k (the generated topology, from the server)
23
+ * keyboard.js the map by keyboard and the screen reader's announcer; keyboard-model.js its pure half
24
+ * live.js the live layer: the chip, polling, laying a live source's answer over the map; live-model.js its pure half
25
+ * review-tokens.js review tokens for the review API, when somebody is signed in: make one (shown once), list, revoke; review-tokens-model.js its pure half
26
+ * forge-panel.js the issues and PRs panel, when the config names a forge: a PR lights the nodes it changes; forge-panel-model.js its pure half
27
+ * theme.js light or dark: the system setting and the header toggle; theme-model.js the colours (theme-boot.js before the first paint)
28
+ */
29
+
30
+ import { state, startPageInstance } from './state.js';
31
+ import { createMap } from './engine/render.js';
32
+ import { createRenderer, rendererPreference } from './renderer.js';
33
+ import { clearFreeformPositions, layoutOrbit, loadFreeformPositions } from './engine/layout.js';
34
+ import { measureCovered } from './sheets.js';
35
+ import { stopFlight } from './fly.js';
36
+ import { setProjectLayers } from './layers.js';
37
+ import { closeDrawer } from './panels.js';
38
+ import { selectNode, clearSelection } from './engine/selection.js';
39
+ import { ensurePagePieces } from './page-pieces.js';
40
+ import { initScenarioUi, maybeStartDemo, playOutage } from './scenario-ui.js';
41
+ import { initSheets } from './sheets.js';
42
+ import { initReviewColumn } from './review-column.js';
43
+ import { loadTopology, setView, syncViewUi } from './view.js';
44
+ import { bindCanvasInput } from './input.js';
45
+ import { initProposals } from './proposal.js';
46
+ import { followDeepLink } from './deep-link.js';
47
+ import { initWhatIf } from './whatif.js';
48
+ import { initReview } from './review.js';
49
+ import { initMockups } from './mockups.js';
50
+ import { benchMeta, measureParams, readoutLines, resultLine, runBench, showReadout } from './bench.js';
51
+ import { initLive } from './live.js';
52
+ import { initHouse } from './house.js';
53
+ import { houseOffered } from './house-model.js';
54
+ import { initChecks } from './check.js';
55
+ import { initKeyboard } from './keyboard.js';
56
+ import { initForgePanel } from './forge-panel.js';
57
+ import { initReviewTokens } from './review-tokens.js';
58
+ import { loadClientConfig, loadSources, applyBranding } from './plugins.js';
59
+ import { initTheme, setThemeChoice } from './theme.js';
60
+ import { initAccountMenu } from './account-menu.js';
61
+ import { watchHeaderFit } from './header-fit.js';
62
+ import { watchHint } from './hint.js';
63
+ import { watchFocusRoom } from './focus-room.js';
64
+ import { mapStyleOf } from './engine/map-style.js';
65
+
66
+ /** The instance and its map: made by `startPage`, the one of this document */
67
+ let inst;
68
+ let map;
69
+
70
+ const $ = (id) => inst.el(id);
71
+ const render = () => map.render();
72
+ const fitGraphToViewport = (...a) => map.fitGraphToViewport(...a);
73
+ const fitPadding = () => map.fitPadding();
74
+
75
+ // ---------------------------------------------------------------------------
76
+ // Freeform mode: orbit clustering (locked) or dragging nodes anywhere
77
+ // ---------------------------------------------------------------------------
78
+ function updateFreeformUI() {
79
+ const on = state.isFreeform;
80
+ $('freeform-toggle-btn').setAttribute('aria-checked', String(on));
81
+ $('reset-positions-btn').hidden = !on;
82
+ syncViewUi();
83
+ state.canvas.style.cursor = on ? 'default' : 'grab';
84
+ }
85
+
86
+ function toggleFreeformMode() {
87
+ setView('orbit'); // positions are only draggable on the full map
88
+ state.isFreeform = !state.isFreeform;
89
+ layoutOrbit(inst);
90
+ updateFreeformUI();
91
+ render();
92
+ }
93
+
94
+ function resetFreeformPositions() {
95
+ clearFreeformPositions(inst);
96
+ layoutOrbit(inst);
97
+ updateFreeformUI();
98
+ render();
99
+ }
100
+
101
+ // ---------------------------------------------------------------------------
102
+ // One click handler for everything with a data-action
103
+ // ---------------------------------------------------------------------------
104
+ const ACTIONS = {
105
+ 'reset-view': () => fitGraphToViewport(fitPadding()),
106
+ 'fit': () => fitGraphToViewport(fitPadding()),
107
+ 'toggle-freeform': () => toggleFreeformMode(),
108
+ 'reset-positions': () => resetFreeformPositions(),
109
+ 'preset-map': () => {
110
+ closeDrawer();
111
+ setView('orbit');
112
+ },
113
+ 'preset-dataflow': () => {
114
+ closeDrawer();
115
+ setView(state.view === 'dataflow' ? 'orbit' : 'dataflow');
116
+ },
117
+ 'play-outage': (el) => playOutage(el.dataset.id), // the cloud panel's Play button (spec 0031 section 8)
118
+ 'retry': () => loadTopology(),
119
+ 'set-theme': (el) => setThemeChoice(el.dataset.choice),
120
+ };
121
+
122
+ // The click on a control the page owns (the header's View switch, Fit, Retry, the theme and the Freeform layout). The pieces' controls are their own:
123
+ // the node panel's, the Layers menu's and the search's (explorer/pieces/), which also close the Layers menu on a click outside it
124
+ document.addEventListener('click', (e) => {
125
+ const target = e.target.closest('[data-action]');
126
+ if (target) {
127
+ const action = ACTIONS[target.dataset.action];
128
+ if (action) action(target);
129
+ }
130
+ });
131
+
132
+ // ---------------------------------------------------------------------------
133
+ // /?bench=1: the scripted pan-and-zoom of bench.js on the map as loaded
134
+ // ---------------------------------------------------------------------------
135
+ async function runPageBench(frames, zoomFactor, sync) {
136
+ fitGraphToViewport(fitPadding());
137
+ const { camera, width, height } = state;
138
+ const base = { zoom: camera.zoom, cx: (width / 2 - camera.x) / camera.zoom, cy: (height / 2 - camera.y) / camera.zoom, width, height, zoomFactor };
139
+ const result = await runBench({
140
+ frames,
141
+ base,
142
+ setCamera: (c) => { camera.x = c.x; camera.y = c.y; camera.zoom = c.zoom; },
143
+ // renderNow draws this instant (and the labels with it), so a measured frame is a whole one
144
+ draw: () => {
145
+ map.renderNow();
146
+ // ?sync=1: wait for the GPU, so the time in draw is the whole frame's and not just the hand-over
147
+ if (sync) { const gl = state.canvas.getContext('webgl2'); if (gl) gl.finish(); }
148
+ },
149
+ // The page's own frames (a packet loop, a pending draw) would add draws the script does not count:
150
+ // hold them while the run lasts, then let go, so the run draws what a viewer sees
151
+ before: () => map.holdFrames(true),
152
+ after: () => { map.holdFrames(false); fitGraphToViewport(fitPadding()); map.ensureFlowLoop(); },
153
+ });
154
+ const meta = benchMeta({
155
+ nodes: state.nodes.length, edges: state.edges.length, view: state.view, renderer: map.getRenderer().kind,
156
+ zoomFactor, width, height, dpr: window.devicePixelRatio, search: window.location.search,
157
+ });
158
+ const line = resultLine(result, meta);
159
+ console.log(line);
160
+ window.__bench = { result, meta, line };
161
+ showReadout(document, readoutLines(result, meta));
162
+ }
163
+
164
+ // ---------------------------------------------------------------------------
165
+ // Start
166
+ // ---------------------------------------------------------------------------
167
+ /**
168
+ * Starts cornfield's page: the one instance of this document, its map, and everything the page shows. Called once, by main.js.
169
+ * @returns {ReturnType<typeof startPageInstance>} the page's instance
170
+ */
171
+ export function startPage() {
172
+ inst = startPageInstance({ root: document }); // the one instance of this document, the document its root; state.js hands the modules that are not yet on an instance its state
173
+ map = createMap(inst, { measureCovered }); // the camera, the scene and the frame loop of this instance (engine/render.js)
174
+ const mapStyle = mapStyleOf(inst);
175
+ initTheme(); // light or dark, before the first scene is built (theme.js)
176
+ mapStyle.watch(); // the map reads --corn-map-* from the theme, and again when it changes (engine/map-style.js)
177
+ mapStyle.onChange(() => render());
178
+ state.canvas = $('graph-canvas');
179
+ state.labelCanvas = $('label-canvas');
180
+ // A 2d context that only measures text (layout.js labelWidth): the map canvas may be WebGL, and a
181
+ // canvas that gave a WebGL context can never give a 2d one
182
+ state.ctx = document.createElement('canvas').getContext('2d');
183
+ // WebGL2, or Canvas 2D when it is missing or the address says ?renderer=canvas2d (the address is read here, not by the renderer)
184
+ map.setRenderer(createRenderer(state.canvas, { prefer: rendererPreference(window.location.search), mapStyle: mapStyle.get }));
185
+ loadFreeformPositions(inst);
186
+ ensurePagePieces(); // the node panel, the Layers menu and the search, mounted where index.html has their places (page-pieces.js)
187
+ // What a tap on the map asks of the page: a node's panel, the normal view, the end of a flight to a match
188
+ inst.on('select', (node) => selectNode(inst, node, { source: 'tap' }));
189
+ inst.on('background', () => clearSelection(inst, { source: 'tap' }));
190
+ inst.on('grab', () => stopFlight());
191
+ bindCanvasInput(inst);
192
+ initKeyboard(); // the map by keyboard (the panel's, the Layers menu's and the search's keys are their pieces') (engine/keyboard.js)
193
+ initAccountMenu(); // the one menu at the right of the header (account-menu.js)
194
+ initSheets();
195
+ initScenarioUi();
196
+ initReviewColumn(); // the review column: the changes, and the personas with their scenarios (review-column.js)
197
+ map.watchSize(); // the map follows its box (a ResizeObserver on the stage), in place of the window's resize event
198
+
199
+ map.resize();
200
+ syncViewUi();
201
+ // /?proposal=<slug> opens a proposal over the map, /?view=dataflow the data-flow preset: both have a link
202
+ const configReady = loadClientConfig().then((config) => { applyBranding(document, config); watchHeaderFit(document); watchHint(document); watchFocusRoom(document); mapStyle.applyTheme(config.theme); setProjectLayers(config.layers); syncViewUi(); return config; }); // the brand, then the theme's icons for the map; the hint again, in the repo's words
203
+ loadTopology().then(async (ok) => {
204
+ if (!ok) return;
205
+ // The sources the repo's cornfield.config.js names (plugins.js): a live layer, a house, devices, a checker; each optional
206
+ const config = await configReady;
207
+ state.forge = config.forge || null;
208
+ const { liveSource, floorRenderer, checker, houseSource } = await loadSources(config);
209
+ const forgeOn = initForgePanel(config); // issues and PRs: only when the config names a forge (forge-panel.js)
210
+ initReviewTokens(config); // review tokens for the review API: only for somebody signed in (review-tokens.js)
211
+ initWhatIf(); // before a proposal can open, so its edits come with it
212
+ initReview(); // after it: the review controls sit under the edit controls on the bar
213
+ initMockups(); // a proposal's screens open from the node they name
214
+ const proposalOpened = await initProposals();
215
+ // The live layer (live.js): loads only once a live layer goes on in the Layers menu; with no source it stays off
216
+ initLive({ source: liveSource, onSelectionGone: closeDrawer });
217
+ // The House view (house.js): a source's floor renderer draws the plan under the map (plain room boxes without one); /?view=house opens it
218
+ initHouse({ floor: floorRenderer, offered: houseOffered(houseSource, state.nodes) }); // no house source and no house in the map: no House button
219
+ initChecks({ checker }); // scenarios ask the source's checker, when there is one (check.js)
220
+ if (new URLSearchParams(window.location.search).get("view") === "dataflow") setView("dataflow");
221
+ // /?run=, /?issue= and /?proposal=…&start=1 land on a run, an issue's Start button and a proposal's Start dialog (deep-link.js)
222
+ followDeepLink({ proposalOpened, forgeOn }).catch((err) => console.error('Following the link failed', err));
223
+ // /?scenario-demo=1 plays a small scenario over real nodes, to look at the player
224
+ maybeStartDemo();
225
+ // /?bench=1 runs the stopwatch (bench.js) once the map is up
226
+ const { benchFrames, benchZoom, benchSync } = measureParams(window.location.search);
227
+ if (benchFrames) {
228
+ try { await runPageBench(benchFrames, benchZoom, benchSync); } catch (err) { console.error("The bench failed", err); }
229
+ }
230
+ });
231
+ return inst;
232
+ }