@popcorn245/cornfield 0.51.2 → 0.51.4

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.
@@ -104,14 +104,34 @@ export function createMap(inst, opts = {}) {
104
104
  // -------------------------------------------------------------------------
105
105
  function resize() {
106
106
  const { canvas } = state;
107
+ const before = `${state.width}x${state.height}`;
107
108
  state.width = canvas.parentElement.clientWidth;
108
109
  state.height = canvas.parentElement.clientHeight;
109
110
  getRenderer().resize(state.width, state.height, inst.dpr());
110
111
  const labels = getLabelLayer();
111
112
  if (labels) labels.resize(state.width, state.height, inst.dpr());
113
+ if (before !== `${state.width}x${state.height}`) fitToNewBox();
112
114
  render();
113
115
  }
114
116
 
117
+ /**
118
+ * The box the map's fit reads is not always final when the map is first fitted: the page's own box is laid out a moment after the topology arrives
119
+ * (an embedded pane sized after the page began loading), a component's stage is `display: none` or not yet sized. So the engine keeps what the last
120
+ * fit asked for (`memo.lastFit`: its arguments and the camera it set), and when the box changes while the camera is still exactly there, nobody has
121
+ * panned, zoomed or flown since, so the fit is made again for the box. Once anyone has moved the camera, a new box keeps their view.
122
+ * A fit asked for while the map has no box (a parent `display: none`, a box of no size) waits for one (`memo.waiting`): fitted to nothing,
123
+ * the camera would be lost.
124
+ */
125
+ const memo = inst.slot('fit', () => ({ lastFit: null, waiting: null })); // on the instance: a page that made two maps over one state (page.js, then the map piece) has one memory
126
+
127
+ function fitToNewBox() {
128
+ if (!state.width || !state.height) return;
129
+ if (memo.waiting) { const { padding, only } = memo.waiting; fitGraphToViewport(padding === null ? fitPadding() : padding, only); return; }
130
+ const { camera } = state;
131
+ const last = memo.lastFit;
132
+ if (last && camera.x === last.x && camera.y === last.y && camera.zoom === last.zoom) fitGraphToViewport(last.auto ? fitPadding() : last.padding, last.only);
133
+ }
134
+
115
135
  /**
116
136
  * Draws again when the map's box changes size (in place of a listener on the window): a ResizeObserver on the canvas's parent. The first
117
137
  * report, which every observer makes on `observe`, is skipped when the size is already the one the map has.
@@ -179,7 +199,12 @@ export function createMap(inst, opts = {}) {
179
199
 
180
200
  /** Zoom and pan so every visible node (and its label) is on screen; `only` (a Set of ids) narrows it to those, when any are visible */
181
201
  function fitGraphToViewport(padding = 40, only = null) {
182
- const { width, camera } = state;
202
+ const { width, height, camera } = state;
203
+ if (!width || !height) { // no box yet: the fit is made when there is one (resize())
204
+ if (state.nodes.length) memo.waiting = { padding: padding === fitPadding() ? null : padding, only };
205
+ return;
206
+ }
207
+ memo.waiting = null;
183
208
  const t = fitTarget(padding, only);
184
209
  if (!t) return;
185
210
  const { minX, maxX, minY, maxY, mapHeight } = t;
@@ -188,6 +213,7 @@ export function createMap(inst, opts = {}) {
188
213
  camera.zoom = targetZoom;
189
214
  camera.x = width / 2 - ((minX + maxX) / 2) * targetZoom;
190
215
  camera.y = mapHeight / 2 - ((minY + maxY) / 2) * targetZoom;
216
+ memo.lastFit = { padding, auto: padding === fitPadding(), only, x: camera.x, y: camera.y, zoom: camera.zoom };
191
217
  render();
192
218
  }
193
219
 
@@ -279,6 +305,12 @@ export function createMap(inst, opts = {}) {
279
305
  for (const type of ACTIVITY) inst.listen(inst.root, type, onInput, { passive: true, capture: true });
280
306
  }
281
307
  inst.onShown(() => scheduler.shown());
308
+ // A page that loaded hidden (a background tab, a pane not yet shown) has had no frames, and a ResizeObserver reports nothing while it is hidden:
309
+ // the box is read again when it is shown (resize() fits the map again if nobody has moved the camera)
310
+ inst.onShown(() => {
311
+ const box = state.canvas && state.canvas.parentElement;
312
+ if (!ended && box && box.clientWidth && box.clientHeight && (box.clientWidth !== state.width || box.clientHeight !== state.height)) resize();
313
+ });
282
314
 
283
315
  /** Ends the map: no frame is waiting, the renderer's GPU context and the label layer are released, nothing is called again */
284
316
  function destroy() {
@@ -249,13 +249,8 @@ function mountOn(root, inst, options, privateScope) {
249
249
  /** Where nodes sit for the current view (engine/topology.js) */
250
250
  const applyLayout = () => layoutMap(inst);
251
251
 
252
- /** A fit asked for while the map has no box (a parent `display: none`, a box of no size) waits for one: fitted to nothing, the camera would be lost */
253
- let fitWhenSized = false;
254
- const fit = () => {
255
- if (!state.nodes.length) return;
256
- fitWhenSized = !state.width || !state.height;
257
- if (!fitWhenSized) map.fitGraphToViewport(map.fitPadding());
258
- };
252
+ /** The whole map in view. With no box yet the engine waits for one (engine/render.js), and fits again if the box changes before anyone moves the camera */
253
+ const fit = () => { if (state.nodes.length) map.fitGraphToViewport(map.fitPadding()); };
259
254
  const nodeById = (id) => state.nodes.find((n) => n.id === id) || null;
260
255
 
261
256
  // -- selection: one path for the page and a component ---------------------------------------------------------------------------------
@@ -302,7 +297,6 @@ function mountOn(root, inst, options, privateScope) {
302
297
 
303
298
  // -- the first frame ---------------------------------------------------------------------------------------------------------------------
304
299
  scope.own(map.onPaint(() => {
305
- if (fitWhenSized && state.width && state.height) fit();
306
300
  if (ready || !state.nodes.length) return;
307
301
  ready = true;
308
302
  if (options.onReady) options.onReady();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@popcorn245/cornfield",
3
- "version": "0.51.2",
3
+ "version": "0.51.4",
4
4
  "description": "A self-hostable dev dashboard with the topology map at its centre: proposals on the map, scenarios, outages and what-if edits, for any repo.",
5
5
  "license": "MIT",
6
6
  "author": "popcorn245 (https://github.com/popcorn245)",
@@ -24,7 +24,7 @@ const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
24
24
  * An href or src the page may keep: http(s), mailto, or a relative or anchor link. Checked also as a browser reads
25
25
  * it, which drops every tab and newline and any control or space at the start, so `java\tscript:` is `javascript:`.
26
26
  */
27
- const safeUrl = (u) => {
27
+ export const safeUrl = (u) => {
28
28
  if (typeof u !== 'string') return null;
29
29
  const read = u.replace(/[\t\n\r]/g, '').replace(/^[\u0000- ]+/, '');
30
30
  return (WEB_URL.test(u) || !SCHEME.test(u.trim())) && (WEB_URL.test(read) || !SCHEME.test(read)) ? u : null;
@@ -17,7 +17,7 @@ import path from 'node:path';
17
17
  import { escapeHtml as esc } from '../../core/text.js';
18
18
  import { deriveTopology } from '../../core/derive.js';
19
19
  import { readTopology } from '../../core/validate.js';
20
- import { renderMarkdown } from './markdown.js';
20
+ import { renderMarkdown, safeUrl } from './markdown.js';
21
21
  import docsDeriver, { resolveLink } from './index.js';
22
22
 
23
23
  /** The file a built site holds, so a rebuild knows the folder is its own */
@@ -33,10 +33,24 @@ export function sourceUrlOf(config) {
33
33
  return f.type === 'forgejo' ? `${origin}/${f.repo}/src/branch/main` : `${origin}/${f.repo}/blob/HEAD`;
34
34
  }
35
35
 
36
- /** A link from one site file to another, relative */
36
+ /**
37
+ * A link from one site file to another, relative. A file name is data: a `%`, `#`, `?`, `\` (a browser reads it as `/`),
38
+ * space or control character is written as `%XX`, so the link names the file and nothing else; and a first segment
39
+ * that holds a `:` gets a leading `./`, or a browser reads it as a scheme (`javascript:x.html`).
40
+ */
37
41
  const relHref = (fromOutput, toOutput) => {
38
- const rel = path.posix.relative(path.posix.dirname(fromOutput), toOutput);
39
- return rel || path.posix.basename(toOutput);
42
+ const rel = path.posix.relative(path.posix.dirname(fromOutput), toOutput) || path.posix.basename(toOutput);
43
+ const href = rel.replace(/[%#?\\\u0000-\u0020\u007f]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')}`);
44
+ return href.split('/')[0].includes(':') ? `./${href}` : href;
45
+ };
46
+
47
+ /**
48
+ * A config's URL the site may link to: absolute http or https with a host (`https://`; not `https:x`, which a page
49
+ * on https reads as a path of its own), and one a browser reads as such (see `safeUrl`), or null
50
+ */
51
+ const webUrl = (u) => {
52
+ const url = safeUrl(u);
53
+ return url && /^https?:\/\//i.test(url) ? url : null;
40
54
  };
41
55
 
42
56
  /**
@@ -48,6 +62,7 @@ const relHref = (fromOutput, toOutput) => {
48
62
  * @returns {Record<string, string | { copy: string }>}
49
63
  */
50
64
  export function siteFiles({ title, pages, nodes, edges, read, exists, isDir, sourceUrl = null }) {
65
+ sourceUrl = webUrl(sourceUrl);
51
66
  const files = {};
52
67
  const byId = new Map(pages.map((p) => [p.id, p]));
53
68
  const byFile = new Map(pages.map((p) => [p.file, p]));
@@ -162,7 +177,7 @@ function mapBody({ pages, nodes, out, back }) {
162
177
  const words = `<text class="${cls}" x="${f(tx)}" y="${f(y)}" text-anchor="${right ? 'start' : 'end'}" dominant-baseline="middle" transform="rotate(${f(deg)} ${f(x)} ${f(y)})">${esc(short)}</text>`;
163
178
  return href ? `<a href="${esc(href)}"><title>${esc(text)}</title>${dot}${words}</a>` : `<g><title>${esc(text)}</title>${dot}${words}</g>`;
164
179
  };
165
- const docLabels = pages.map((p) => label(p.id, p.title, p.output, 'doc'));
180
+ const docLabels = pages.map((p) => label(p.id, p.title, relHref('map.html', p.output), 'doc'));
166
181
  const codeLabels = codeIds.map((id) => label(id, nodes.get(id).file || nodes.get(id).label, null, 'code'));
167
182
  const pad = 230;
168
183
  const svg = `<svg class="map" viewBox="${-pad} ${-pad / 2} ${size + 2 * pad} ${size + pad}" role="img" aria-label="The docs and their links">
@@ -173,7 +188,7 @@ ${codeLabels.join('\n')}
173
188
  const rows = pages.map((p) => {
174
189
  const to = out.get(p.id).map((id) => byId.get(id)?.title || nodes.get(id)?.file).filter(Boolean);
175
190
  const from = back.get(p.id).map((id) => byId.get(id).title);
176
- return `<tr><td><a href="${esc(p.output)}">${esc(p.title)}</a><br><code>${esc(p.file)}</code></td><td>${to.length ? esc(to.join(', ')) : '—'}</td><td>${from.length ? esc(from.join(', ')) : '—'}</td></tr>`;
191
+ return `<tr><td><a href="${esc(relHref('map.html', p.output))}">${esc(p.title)}</a><br><code>${esc(p.file)}</code></td><td>${to.length ? esc(to.join(', ')) : '—'}</td><td>${from.length ? esc(from.join(', ')) : '—'}</td></tr>`;
177
192
  });
178
193
  const edgeCount = pages.reduce((s, p) => s + out.get(p.id).length, 0);
179
194
  return `<article class="doc wide">