@wastech-mdlint/viewer 0.0.0-stage → 0.4.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.
Files changed (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -2
  3. package/assets/favicon.ico +0 -0
  4. package/assets/viewer.css +4184 -0
  5. package/dist/asset-routes.d.ts +4 -0
  6. package/dist/asset-routes.js +107 -0
  7. package/dist/blame.d.ts +8 -0
  8. package/dist/blame.js +54 -0
  9. package/dist/branch-changes.d.ts +8 -0
  10. package/dist/branch-changes.js +49 -0
  11. package/dist/candidate-lint-worker.d.ts +1 -0
  12. package/dist/candidate-lint-worker.js +67 -0
  13. package/dist/candidate-lint.d.ts +39 -0
  14. package/dist/candidate-lint.js +115 -0
  15. package/dist/change-banner.d.ts +38 -0
  16. package/dist/change-banner.js +108 -0
  17. package/dist/change-diff.d.ts +23 -0
  18. package/dist/change-diff.js +140 -0
  19. package/dist/change-marks.d.ts +32 -0
  20. package/dist/change-marks.js +363 -0
  21. package/dist/changes-article.d.ts +15 -0
  22. package/dist/changes-article.js +244 -0
  23. package/dist/changes-routes.d.ts +12 -0
  24. package/dist/changes-routes.js +271 -0
  25. package/dist/changes.d.ts +113 -0
  26. package/dist/changes.js +324 -0
  27. package/dist/cli-args.d.ts +21 -0
  28. package/dist/cli-args.js +93 -0
  29. package/dist/client/banner-height.d.ts +1 -0
  30. package/dist/client/banner-height.js +33 -0
  31. package/dist/client/change-marks.d.ts +1 -0
  32. package/dist/client/change-marks.js +56 -0
  33. package/dist/client/changes.d.ts +1 -0
  34. package/dist/client/changes.js +183 -0
  35. package/dist/client/chart-option.d.ts +3 -0
  36. package/dist/client/chart-option.js +76 -0
  37. package/dist/client/charts.d.ts +1 -0
  38. package/dist/client/charts.js +54 -0
  39. package/dist/client/clipboard.d.ts +1 -0
  40. package/dist/client/clipboard.js +43 -0
  41. package/dist/client/code-copy.d.ts +1 -0
  42. package/dist/client/code-copy.js +29 -0
  43. package/dist/client/column-resize.d.ts +1 -0
  44. package/dist/client/column-resize.js +120 -0
  45. package/dist/client/config-dom.d.ts +13 -0
  46. package/dist/client/config-dom.js +47 -0
  47. package/dist/client/config-draft.d.ts +73 -0
  48. package/dist/client/config-draft.js +218 -0
  49. package/dist/client/config-editor.d.ts +3 -0
  50. package/dist/client/config-editor.js +677 -0
  51. package/dist/client/config-fields.d.ts +35 -0
  52. package/dist/client/config-fields.js +339 -0
  53. package/dist/client/config-json.d.ts +18 -0
  54. package/dist/client/config-json.js +96 -0
  55. package/dist/client/config-preview.d.ts +9 -0
  56. package/dist/client/config-preview.js +91 -0
  57. package/dist/client/config-rules.d.ts +14 -0
  58. package/dist/client/config-rules.js +202 -0
  59. package/dist/client/config-types.d.ts +91 -0
  60. package/dist/client/config-types.js +4 -0
  61. package/dist/client/copy-path.d.ts +1 -0
  62. package/dist/client/copy-path.js +32 -0
  63. package/dist/client/dashboard-requests.d.ts +11 -0
  64. package/dist/client/dashboard-requests.js +28 -0
  65. package/dist/client/dashboard.d.ts +1 -0
  66. package/dist/client/dashboard.js +119 -0
  67. package/dist/client/diagnostics.d.ts +1 -0
  68. package/dist/client/diagnostics.js +164 -0
  69. package/dist/client/diagrams.d.ts +1 -0
  70. package/dist/client/diagrams.js +81 -0
  71. package/dist/client/diff-expand.d.ts +1 -0
  72. package/dist/client/diff-expand.js +42 -0
  73. package/dist/client/display-settings-model.d.ts +74 -0
  74. package/dist/client/display-settings-model.js +229 -0
  75. package/dist/client/display-settings.d.ts +1 -0
  76. package/dist/client/display-settings.js +411 -0
  77. package/dist/client/echarts.d.ts +30 -0
  78. package/dist/client/echarts.js +14 -0
  79. package/dist/client/graph-layout.d.ts +88 -0
  80. package/dist/client/graph-layout.js +198 -0
  81. package/dist/client/graph.d.ts +2 -0
  82. package/dist/client/graph.js +566 -0
  83. package/dist/client/history.d.ts +21 -0
  84. package/dist/client/history.js +86 -0
  85. package/dist/client/live-reload-model.d.ts +28 -0
  86. package/dist/client/live-reload-model.js +52 -0
  87. package/dist/client/live-reload.d.ts +9 -0
  88. package/dist/client/live-reload.js +138 -0
  89. package/dist/client/menu.d.ts +14 -0
  90. package/dist/client/menu.js +176 -0
  91. package/dist/client/navigation-feedback.d.ts +1 -0
  92. package/dist/client/navigation-feedback.js +141 -0
  93. package/dist/client/open-config.d.ts +1 -0
  94. package/dist/client/open-config.js +48 -0
  95. package/dist/client/outline-spy.d.ts +1 -0
  96. package/dist/client/outline-spy.js +60 -0
  97. package/dist/client/prompts.d.ts +7 -0
  98. package/dist/client/prompts.js +65 -0
  99. package/dist/client/review.d.ts +1 -0
  100. package/dist/client/review.js +379 -0
  101. package/dist/client/search.d.ts +1 -0
  102. package/dist/client/search.js +199 -0
  103. package/dist/client/segmented.d.ts +13 -0
  104. package/dist/client/segmented.js +70 -0
  105. package/dist/client/source-lines.d.ts +16 -0
  106. package/dist/client/source-lines.js +47 -0
  107. package/dist/client/theme-toggle.d.ts +10 -0
  108. package/dist/client/theme-toggle.js +100 -0
  109. package/dist/client/theme.d.ts +0 -0
  110. package/dist/client/theme.js +50 -0
  111. package/dist/client/tree-marks.d.ts +31 -0
  112. package/dist/client/tree-marks.js +102 -0
  113. package/dist/client/tree-state.d.ts +0 -0
  114. package/dist/client/tree-state.js +179 -0
  115. package/dist/client/viewer.d.ts +1 -0
  116. package/dist/client/viewer.js +53 -0
  117. package/dist/config-editor.d.ts +166 -0
  118. package/dist/config-editor.js +378 -0
  119. package/dist/config-routes.d.ts +18 -0
  120. package/dist/config-routes.js +213 -0
  121. package/dist/context-view.d.ts +41 -0
  122. package/dist/context-view.js +295 -0
  123. package/dist/corpus-routes.d.ts +31 -0
  124. package/dist/corpus-routes.js +258 -0
  125. package/dist/corpus.d.ts +134 -0
  126. package/dist/corpus.js +653 -0
  127. package/dist/dashboard-article.d.ts +14 -0
  128. package/dist/dashboard-article.js +290 -0
  129. package/dist/dashboard.d.ts +94 -0
  130. package/dist/dashboard.js +468 -0
  131. package/dist/diagnostics.d.ts +23 -0
  132. package/dist/diagnostics.js +44 -0
  133. package/dist/document-routes.d.ts +39 -0
  134. package/dist/document-routes.js +396 -0
  135. package/dist/event-hub.d.ts +39 -0
  136. package/dist/event-hub.js +120 -0
  137. package/dist/findings-panel.d.ts +4 -0
  138. package/dist/findings-panel.js +74 -0
  139. package/dist/folder-page.d.ts +8 -0
  140. package/dist/folder-page.js +74 -0
  141. package/dist/graph-view.d.ts +66 -0
  142. package/dist/graph-view.js +236 -0
  143. package/dist/highlight.d.ts +24 -0
  144. package/dist/highlight.js +100 -0
  145. package/dist/http-send.d.ts +9 -0
  146. package/dist/http-send.js +53 -0
  147. package/dist/index.d.ts +2 -0
  148. package/dist/index.js +72 -0
  149. package/dist/open-file.d.ts +36 -0
  150. package/dist/open-file.js +87 -0
  151. package/dist/page-tree.d.ts +3 -0
  152. package/dist/page-tree.js +105 -0
  153. package/dist/page.d.ts +73 -0
  154. package/dist/page.js +478 -0
  155. package/dist/prompts.d.ts +25 -0
  156. package/dist/prompts.js +82 -0
  157. package/dist/render-cache.d.ts +23 -0
  158. package/dist/render-cache.js +38 -0
  159. package/dist/render-document.d.ts +60 -0
  160. package/dist/render-document.js +440 -0
  161. package/dist/request-path.d.ts +17 -0
  162. package/dist/request-path.js +51 -0
  163. package/dist/review-context.d.ts +24 -0
  164. package/dist/review-context.js +109 -0
  165. package/dist/review-queries.d.ts +26 -0
  166. package/dist/review-queries.js +69 -0
  167. package/dist/review-state.d.ts +106 -0
  168. package/dist/review-state.js +251 -0
  169. package/dist/review-store.d.ts +145 -0
  170. package/dist/review-store.js +530 -0
  171. package/dist/router.d.ts +28 -0
  172. package/dist/router.js +294 -0
  173. package/dist/search.d.ts +22 -0
  174. package/dist/search.js +164 -0
  175. package/dist/security.d.ts +95 -0
  176. package/dist/security.js +196 -0
  177. package/dist/server.d.ts +21 -0
  178. package/dist/server.js +224 -0
  179. package/dist/speculation-rules.d.ts +4 -0
  180. package/dist/speculation-rules.js +49 -0
  181. package/dist/state-dir.d.ts +6 -0
  182. package/dist/state-dir.js +31 -0
  183. package/dist/tree-marks.d.ts +49 -0
  184. package/dist/tree-marks.js +108 -0
  185. package/dist/viewer-context.d.ts +37 -0
  186. package/dist/viewer-context.js +74 -0
  187. package/dist/watch.d.ts +41 -0
  188. package/dist/watch.js +135 -0
  189. package/package.json +57 -4
@@ -0,0 +1,566 @@
1
+ import { loadECharts } from "./echarts.js";
2
+ import { forceSeries, isOwnLink, LABEL_FONT_SIZE, laidOutAtOnce, layoutFor, } from "./graph-layout.js";
3
+ import { onServerEvent } from "./live-reload.js";
4
+ import { onThemeChange } from "./theme-toggle.js";
5
+ // A name on every node of a graph this size is unreadable, so until the reader zooms in only the
6
+ // most linked-to documents are named.
7
+ const LABELED_NODES = 30;
8
+ export function initGraph() {
9
+ const canvas = document.querySelector(".graph-canvas");
10
+ if (canvas === null) {
11
+ return;
12
+ }
13
+ const form = document.querySelector(".graph-controls");
14
+ if (form !== null) {
15
+ // Picking a folder is the whole interaction, so it submits at once; the button is only for a page
16
+ // without this script.
17
+ for (const field of form.querySelectorAll("select, input[type=checkbox]")) {
18
+ field.addEventListener("change", () => {
19
+ form.requestSubmit();
20
+ });
21
+ }
22
+ form.querySelector("button")?.setAttribute("hidden", "");
23
+ }
24
+ const params = new URLSearchParams({ folder: canvas.dataset.folder ?? "" });
25
+ if (canvas.dataset.focus !== undefined) {
26
+ params.set("focus", canvas.dataset.focus);
27
+ }
28
+ if (canvas.dataset.changes === "1") {
29
+ params.set("changes", "1");
30
+ }
31
+ void draw(canvas, `/api/graph?${params.toString()}`, "page");
32
+ }
33
+ export function initLocalGraph() {
34
+ const canvas = document.querySelector(".local-graph-canvas");
35
+ const path = canvas?.dataset.path;
36
+ if (canvas === null || path === undefined) {
37
+ return;
38
+ }
39
+ // The layout is sized from the box, and a box with no size cannot hold one: the server renders it
40
+ // hidden, and on a narrow screen the whole side column is hidden by CSS. So it is drawn the first
41
+ // time it has a size — at once on a wide screen, when the window widens on a narrow one, and never
42
+ // on a phone, which then also never downloads ECharts for it.
43
+ canvas.hidden = false;
44
+ // How to zoom is not what a reader expects of a box in a side column, so it says so; without this
45
+ // script there is no graph to zoom and the line stays hidden.
46
+ const hint = canvas.parentElement?.querySelector(".local-graph-hint");
47
+ if (hint !== null && hint !== undefined) {
48
+ hint.hidden = false;
49
+ }
50
+ const sized = new ResizeObserver(() => {
51
+ if (canvas.clientWidth === 0) {
52
+ return;
53
+ }
54
+ sized.disconnect();
55
+ const params = new URLSearchParams({ path });
56
+ // Set on a changed document's page while it shows its changes.
57
+ if (canvas.dataset.changes === "1") {
58
+ params.set("changes", "1");
59
+ }
60
+ void draw(canvas, `/api/neighbors?${params.toString()}`, "local");
61
+ });
62
+ sized.observe(canvas);
63
+ }
64
+ async function draw(canvas, url, kind) {
65
+ const text = await fetchText(url);
66
+ if (text === undefined) {
67
+ return;
68
+ }
69
+ const view = JSON.parse(text);
70
+ view.nodes = paintOrder(view);
71
+ const echarts = await loadECharts();
72
+ canvas.replaceChildren();
73
+ const layout = layoutFor(view, boxOf(canvas), kind);
74
+ const chart = echarts.init(canvas);
75
+ new DrawnGraph({ canvas, chart, view, kind, layout }).start();
76
+ // Saving a document can add or drop links without changing which documents exist, and that reloads
77
+ // no page. The view is fetched again and the page reloaded only when it differs, because a reload
78
+ // restarts the layout, and most saves change no link.
79
+ onServerEvent("change", () => {
80
+ void fetchText(url).then((next) => {
81
+ if (next !== undefined && next !== text) {
82
+ location.reload();
83
+ }
84
+ });
85
+ });
86
+ }
87
+ // One drawn graph and the state its handlers share: the layout it was last given, the zoom the reader
88
+ // has reached, and how long the layout is still left to settle.
89
+ class DrawnGraph {
90
+ canvas;
91
+ chart;
92
+ view;
93
+ kind;
94
+ layout;
95
+ // The zoom the reader has reached, kept here because ECharts hands its label callback no current
96
+ // zoom: the node box it passes was measured when the labels were created and never again.
97
+ zoom;
98
+ // ECharts settles a force layout over several seconds, but checks names for overlap only on the
99
+ // first frame and on a zoom, so names that drift onto each other as documents move would stay
100
+ // drawn on top of each other. Each check renders a frame and so raises `finished` again; the
101
+ // deadline is what ends the loop once the layout has had time to settle, and it starts over
102
+ // whenever the layout is set moving again: by a resize, a theme switch or a dragged document. A
103
+ // graph laid out before it is drawn has nothing left to settle, and a change to it is laid out at
104
+ // once too, so a check or two after one is all it needs.
105
+ settleFor;
106
+ settleUntil;
107
+ lastCheck = 0;
108
+ laidOutFor = { width: 0, height: 0 };
109
+ resizeTimer;
110
+ constructor(init) {
111
+ this.canvas = init.canvas;
112
+ this.chart = init.chart;
113
+ this.view = init.view;
114
+ this.kind = init.kind;
115
+ this.layout = init.layout;
116
+ this.zoom = init.layout.zoom ?? 1;
117
+ this.settleFor = laidOutAtOnce(init.view, init.kind)
118
+ ? 2 * LABEL_CHECK_INTERVAL_MS
119
+ : LAYOUT_SETTLE_MS;
120
+ this.settleUntil = performance.now() + this.settleFor;
121
+ }
122
+ start() {
123
+ this.chart.setOption(this.option(this.layout));
124
+ this.chart.on("click", ({ dataType, dataIndex }) => {
125
+ const node = dataType === "node" ? this.view.nodes[dataIndex] : undefined;
126
+ if (node !== undefined) {
127
+ location.href = node.href;
128
+ }
129
+ });
130
+ this.watchResize();
131
+ // The reader's zoom arrives here after ECharts has already laid the labels out at the old one, so
132
+ // they are laid out once more at the new one. The factor is clamped as ECharts clamps it; a pan
133
+ // carries no factor, and the factor of one is this code's own relayout.
134
+ this.chart.on("graphroam", ({ zoom: factor }) => {
135
+ if (factor === undefined || factor === 1) {
136
+ return;
137
+ }
138
+ this.zoom = Math.min(this.layout.maxZoom, Math.max(this.layout.minZoom, this.zoom * factor));
139
+ this.relayoutLabels();
140
+ });
141
+ if (this.kind === "local") {
142
+ this.zoomWithModifier();
143
+ }
144
+ this.keepLabelsApart();
145
+ // Every theme-dependent color of the graph comes from the page's tokens, so a theme switch only
146
+ // passes them in again. ECharts' own `setTheme` would rebuild the series and lay the graph out
147
+ // from scratch, scattering every document. The new option leaves the zoom out, so the reader's
148
+ // zoom stays, and restarts the layout cool, from where each document is.
149
+ onThemeChange(() => {
150
+ this.settleUntil = performance.now() + this.settleFor;
151
+ this.chart.setOption(this.option({
152
+ ...this.layout,
153
+ zoom: undefined,
154
+ friction: SETTLED_FRICTION,
155
+ }));
156
+ });
157
+ }
158
+ option(layout) {
159
+ return graphOption(this.view, layout, readColors(), () => this.zoom);
160
+ }
161
+ // A zoom by a factor of one makes ECharts lay its labels out again and changes nothing else.
162
+ relayoutLabels() {
163
+ this.chart.dispatchAction({
164
+ type: "graphRoam",
165
+ seriesIndex: 0,
166
+ zoom: 1,
167
+ originX: 0,
168
+ originY: 0,
169
+ });
170
+ }
171
+ // The layout is sized for its box: its forces fill it and the focused document is pinned in its
172
+ // middle. When the box changes for good — the window resized, a side column dragged wider — the
173
+ // graph is laid out again for the new one, from where every document is and at the reader's zoom.
174
+ // A drag resizes the box every frame, so that waits until the size has held for a moment; the
175
+ // canvas itself follows every frame.
176
+ watchResize() {
177
+ this.laidOutFor = boxOf(this.canvas);
178
+ new ResizeObserver(() => {
179
+ this.chart.resize();
180
+ clearTimeout(this.resizeTimer);
181
+ this.resizeTimer = window.setTimeout(() => {
182
+ this.relayoutForBox();
183
+ }, RELAYOUT_DELAY_MS);
184
+ }).observe(this.canvas);
185
+ }
186
+ relayoutForBox() {
187
+ const { width, height } = boxOf(this.canvas);
188
+ if (Math.abs(width - this.laidOutFor.width) < RELAYOUT_MIN_CHANGE &&
189
+ Math.abs(height - this.laidOutFor.height) < RELAYOUT_MIN_CHANGE) {
190
+ return;
191
+ }
192
+ this.laidOutFor = { width, height };
193
+ this.layout = {
194
+ ...layoutFor(this.view, boxOf(this.canvas), this.kind),
195
+ zoom: undefined,
196
+ minZoom: this.layout.minZoom,
197
+ maxZoom: this.layout.maxZoom,
198
+ };
199
+ this.settleUntil = performance.now() + this.settleFor;
200
+ this.chart.setOption(this.option(this.layout));
201
+ }
202
+ // In the side column the plain wheel scrolls the page, as it does everywhere else in the rail: a
203
+ // graph that took it would stop the page whenever the pointer crossed the box. So the local graph
204
+ // zooms with Ctrl or ⌘ held, which is also what a trackpad pinch sends. ECharts' wheel zoom cannot
205
+ // be tied to a key, so this zooms through the same action ECharts' wheel dispatches, which applies
206
+ // the zoom limits and reaches the `graphroam` handler like any other zoom.
207
+ zoomWithModifier() {
208
+ this.canvas.addEventListener("wheel", (event) => {
209
+ if (!event.ctrlKey && !event.metaKey) {
210
+ return;
211
+ }
212
+ // Held Ctrl and a wheel otherwise zoom the whole page.
213
+ event.preventDefault();
214
+ const box = this.canvas.getBoundingClientRect();
215
+ this.chart.dispatchAction({
216
+ type: "graphRoam",
217
+ seriesIndex: 0,
218
+ zoom: wheelZoom(event),
219
+ originX: event.clientX - box.left,
220
+ originY: event.clientY - box.top,
221
+ });
222
+ }, { passive: false });
223
+ }
224
+ keepLabelsApart() {
225
+ this.chart.on("finished", () => {
226
+ const now = performance.now();
227
+ if (now > this.settleUntil ||
228
+ now - this.lastCheck < LABEL_CHECK_INTERVAL_MS) {
229
+ return;
230
+ }
231
+ this.lastCheck = now;
232
+ this.relayoutLabels();
233
+ });
234
+ this.chart.getZr().on("mouseup", () => {
235
+ this.settleUntil = performance.now() + this.settleFor;
236
+ });
237
+ }
238
+ }
239
+ // ECharts paints nodes in data order, so with a document focused its neighborhood goes last and no
240
+ // gray document is drawn over it. The click and the tooltip index into this same order.
241
+ function paintOrder(view) {
242
+ if (view.focus === null) {
243
+ return view.nodes;
244
+ }
245
+ const lit = new Set([view.focus.path, ...view.focus.neighbors]);
246
+ return [
247
+ ...view.nodes.filter((node) => !lit.has(node.path)),
248
+ ...view.nodes.filter((node) => lit.has(node.path)),
249
+ ];
250
+ }
251
+ // A notch of a mouse wheel arrives as about a hundred pixels, or as three lines in Firefox; a
252
+ // trackpad pinch as many events of a few pixels each. Both zoom by the same amount per pixel, and a
253
+ // notch by no more than the step ECharts' own wheel zoom takes on the graph page.
254
+ function wheelZoom(event) {
255
+ const pixels = event.deltaMode === WheelEvent.DOM_DELTA_LINE
256
+ ? event.deltaY * 33
257
+ : event.deltaY;
258
+ return Math.min(1.4, Math.max(1 / 1.4, Math.exp(-pixels / 300)));
259
+ }
260
+ function boxOf(canvas) {
261
+ return { width: canvas.clientWidth, height: canvas.clientHeight };
262
+ }
263
+ async function fetchText(url) {
264
+ try {
265
+ const response = await fetch(url);
266
+ return response.ok ? await response.text() : undefined;
267
+ }
268
+ catch {
269
+ // The server is restarting; the page reloads once it is back.
270
+ return undefined;
271
+ }
272
+ }
273
+ // ECharts' force layout runs about five hundred steps of sixteen milliseconds before it stops.
274
+ const LAYOUT_SETTLE_MS = 12_000;
275
+ const LABEL_CHECK_INTERVAL_MS = 250;
276
+ // A box that changes by less than this keeps its layout: a scrollbar coming and going is not worth
277
+ // setting every document moving.
278
+ const RELAYOUT_MIN_CHANGE = 8;
279
+ const RELAYOUT_DELAY_MS = 200;
280
+ // Links are thin and faint so the documents read first; with a document focused, the links that do
281
+ // not touch it fade a little further and its own are drawn in the accent color.
282
+ const LINK_OPACITY = 0.3;
283
+ const FADED_LINK_OPACITY = 0.15;
284
+ const LIT_LINK = { width: 1.2, opacity: 0.85 };
285
+ // In the local graph every link is the focused document's own or one between its neighbors, so at
286
+ // the page graph's weight the lit links alone fill the small box with solid accent color and the
287
+ // names between them stop reading. There they are hairlines, and the links between neighbors,
288
+ // which only add context, recede further.
289
+ const LOCAL_LINK_OPACITY = 0.18;
290
+ const LOCAL_LIT_LINK = { width: 0.8, opacity: 0.5 };
291
+ function readColors() {
292
+ // A token holds `light-dark(…)`, which reads back unresolved from the property itself; the color
293
+ // of an element that uses it comes back resolved for the scheme the page shows now.
294
+ const probe = document.createElement("span");
295
+ probe.hidden = true;
296
+ document.body.append(probe);
297
+ const read = (token) => {
298
+ probe.style.color = `var(${token})`;
299
+ return getComputedStyle(probe).color;
300
+ };
301
+ try {
302
+ return {
303
+ text: read("--fg"),
304
+ link: read("--fg-muted"),
305
+ muted: read("--border"),
306
+ accent: read("--accent"),
307
+ surface: read("--bg-subtle"),
308
+ added: read("--diff-added"),
309
+ removed: read("--diff-removed"),
310
+ changed: read("--warning"),
311
+ };
312
+ }
313
+ finally {
314
+ probe.remove();
315
+ }
316
+ }
317
+ // A layout restarted at ECharts' usual strength sets the documents that had not quite come to rest
318
+ // wandering again, by up to a hundred pixels on six hundred documents; restarted this cool, they stay
319
+ // within a pixel or two. The cost is that a document dragged afterwards pulls its neighbors along only
320
+ // weakly, since ECharts warms a drag up from the same starting value.
321
+ const SETTLED_FRICTION = 0.02;
322
+ // The page graph names a document once the zoom has opened the gaps between documents wide enough to
323
+ // read a name in: its label grows from nothing at the first of these on-screen spacings to full size
324
+ // at the second, so names come in gradually as the reader zooms rather than all at once.
325
+ const LABEL_SPACING_FROM = 26;
326
+ const LABEL_SPACING_TO = 40;
327
+ const CHANGE_NAMES = {
328
+ A: "Added in this branch",
329
+ M: "Modified in this branch",
330
+ D: "Deleted in this branch",
331
+ R: "Renamed in this branch",
332
+ affected: "Reached by this branch's changes",
333
+ };
334
+ // A changed document keeps its folder's fill and gets a ring in its change's color, so the folders
335
+ // still read; an affected one gets a dashed ring, being touched by the change rather than part of it.
336
+ function changeStyle(node, colors) {
337
+ switch (node.change) {
338
+ case undefined:
339
+ return undefined;
340
+ case "affected":
341
+ return {
342
+ borderColor: colors.changed,
343
+ borderWidth: 1.5,
344
+ borderType: "dashed",
345
+ };
346
+ case "A":
347
+ return { borderColor: colors.added, borderWidth: 2.5 };
348
+ case "R":
349
+ return { borderColor: colors.accent, borderWidth: 2.5 };
350
+ default:
351
+ return { borderColor: colors.changed, borderWidth: 2.5 };
352
+ }
353
+ }
354
+ function graphOption(view, layout, colors, zoomNow) {
355
+ // Sorted, so a folder keeps its color whichever document is focused.
356
+ const groups = [...new Set(view.nodes.map((node) => node.group))].sort();
357
+ const focus = view.focus;
358
+ const local = layout.kind === "local";
359
+ const context = {
360
+ view,
361
+ layout,
362
+ colors,
363
+ local,
364
+ lit: focus === null ? undefined : new Set([focus.path, ...focus.neighbors]),
365
+ laidOut: forceSeries(view, layout),
366
+ };
367
+ const linkOpacity = linkOpacityFor(local, focus !== null);
368
+ return {
369
+ // ECharts draws its labels on the canvas in a font of its own, which the stylesheet cannot reach;
370
+ // handed the page's, they follow the site font the reader chose like the rest of the page.
371
+ textStyle: { fontFamily: getComputedStyle(document.body).fontFamily },
372
+ // Text drawn on the canvas, never markup: titles and paths are repository content.
373
+ tooltip: {
374
+ renderMode: "richText",
375
+ backgroundColor: colors.surface,
376
+ borderColor: colors.muted,
377
+ textStyle: { color: colors.text },
378
+ formatter: (params) => tooltipText(view, params),
379
+ },
380
+ legend: legendOption(layout, groups, colors),
381
+ series: [
382
+ {
383
+ type: "graph",
384
+ layout: "force",
385
+ ...context.laidOut,
386
+ // The local graph pans by dragging; it zooms only by the wheel handler in `DrawnGraph`.
387
+ roam: layout.kind === "page" ? true : "move",
388
+ // Anywhere in the box, not only over the view's own rectangle, which ECharts scales with
389
+ // the zoom: a graph that opens zoomed out to fit would otherwise answer the wheel and a drag
390
+ // only in a patch around its middle, and the box's edges would ignore them.
391
+ roamTrigger: "global",
392
+ draggable: true,
393
+ ...(layout.zoom === undefined
394
+ ? {}
395
+ : {
396
+ zoom: layout.zoom,
397
+ scaleLimit: { min: layout.minZoom, max: layout.maxZoom },
398
+ }),
399
+ // Nodes grow and shrink with the zoom exactly as the gaps between them do, so a graph that
400
+ // starts zoomed out shows small documents with room around them rather than full-size ones
401
+ // on top of each other.
402
+ nodeScaleRatio: 1,
403
+ categories: groups.map((name) => ({ name })),
404
+ data: nodeData(context, groups),
405
+ links: linkData(context),
406
+ lineStyle: {
407
+ color: colors.link,
408
+ width: local ? 0.5 : 0.7,
409
+ opacity: linkOpacity,
410
+ },
411
+ label: {
412
+ position: "right",
413
+ fontSize: layout.fontSize,
414
+ color: colors.text,
415
+ formatter: ({ name }) => name?.split("/").pop() ?? "",
416
+ // In the narrow local graph a long name would run out of its box; it ends in an
417
+ // ellipsis, and the tooltip has it in full.
418
+ ...(layout.kind === "local"
419
+ ? { width: 84, overflow: "truncate" }
420
+ : {}),
421
+ },
422
+ labelLayout: labelLayoutFor(context, zoomNow),
423
+ // Hovering a document lights its links and names its neighbors, and leaves everything else as
424
+ // it was. ECharts' default fades all the rest to a tenth of its opacity, which on a graph of
425
+ // hundreds of documents flashes the whole picture dark and back at every move of the pointer;
426
+ // `blur` set to the resting look switches that fade off.
427
+ emphasis: {
428
+ focus: "adjacency",
429
+ label: { show: true, fontSize: layout.fontSize },
430
+ itemStyle: { borderColor: colors.accent, borderWidth: 1.5 },
431
+ lineStyle: {
432
+ color: colors.accent,
433
+ width: local ? 1.1 : 1.5,
434
+ opacity: 1,
435
+ },
436
+ },
437
+ blur: {
438
+ itemStyle: { opacity: 1 },
439
+ lineStyle: { opacity: linkOpacity },
440
+ label: { opacity: 1 },
441
+ },
442
+ },
443
+ ],
444
+ };
445
+ }
446
+ function linkOpacityFor(local, focused) {
447
+ if (local) {
448
+ return LOCAL_LINK_OPACITY;
449
+ }
450
+ return focused ? FADED_LINK_OPACITY : LINK_OPACITY;
451
+ }
452
+ function tooltipText(view, { dataType, dataIndex }) {
453
+ if (dataType === "node") {
454
+ const node = view.nodes[dataIndex];
455
+ return node === undefined
456
+ ? ""
457
+ : `${node.title}\n${node.path}\n${node.inDegree} in · ${node.outDegree} out${node.change === undefined ? "" : `\n${CHANGE_NAMES[node.change]}`}`;
458
+ }
459
+ const edge = view.edges[dataIndex];
460
+ return edge === undefined
461
+ ? ""
462
+ : `${edge.from}\n→ ${edge.to}${edge.change === undefined
463
+ ? ""
464
+ : `\n${edge.change === "added" ? "Linked" : "Unlinked"} in this branch`}`;
465
+ }
466
+ function legendOption(layout, groups, colors) {
467
+ if (layout.kind !== "page") {
468
+ return { show: false };
469
+ }
470
+ return {
471
+ type: "scroll",
472
+ top: 0,
473
+ data: groups,
474
+ textStyle: { color: colors.text },
475
+ inactiveColor: colors.muted,
476
+ pageTextStyle: { color: colors.link },
477
+ pageIconColor: colors.text,
478
+ pageIconInactiveColor: colors.muted,
479
+ };
480
+ }
481
+ function nodeData(context, groups) {
482
+ const { view, colors, lit, laidOut } = context;
483
+ const groupIndex = new Map(groups.map((group, index) => [group, index]));
484
+ return view.nodes.map((node, index) => {
485
+ const isFocus = node.path === view.focus?.path;
486
+ const dimmed = lit !== undefined && !lit.has(node.path);
487
+ return {
488
+ ...laidOut.data[index],
489
+ name: node.path,
490
+ category: groupIndex.get(node.group),
491
+ itemStyle: nodeItemStyle(node, { isFocus, dimmed }, colors),
492
+ label: {
493
+ show: !dimmed,
494
+ fontWeight: isFocus ? "bold" : undefined,
495
+ },
496
+ };
497
+ });
498
+ }
499
+ // Outside the focused neighborhood a document turns neutral gray rather than transparent: see-through
500
+ // color washes out to pastel on a light page and the graph's shape is lost, while gray keeps every
501
+ // document visible and lets the lit ones lead.
502
+ function nodeItemStyle(node, state, colors) {
503
+ if (state.dimmed) {
504
+ return { color: colors.muted };
505
+ }
506
+ if (state.isFocus) {
507
+ return { borderColor: colors.accent, borderWidth: 2 };
508
+ }
509
+ return changeStyle(node, colors);
510
+ }
511
+ // Arrowheads only on a focused document's own links. A filled head is far heavier than a hairline,
512
+ // and the dozens that meet at a much-linked document pile up into a dark blot; the direction of any
513
+ // link is in its tooltip.
514
+ function linkData(context) {
515
+ const { view, colors, local, laidOut } = context;
516
+ const litLink = local ? LOCAL_LIT_LINK : LIT_LINK;
517
+ return view.edges.map((edge, index) => ({
518
+ ...laidOut.links[index],
519
+ ...(edge.change === undefined
520
+ ? {}
521
+ : {
522
+ lineStyle: {
523
+ color: edge.change === "added" ? colors.added : colors.removed,
524
+ type: edge.change === "removed" ? "dashed" : "solid",
525
+ width: local ? 0.9 : 1.2,
526
+ opacity: 0.9,
527
+ },
528
+ }),
529
+ ...(isOwnLink(edge, view.focus) && edge.change === undefined
530
+ ? {
531
+ symbol: ["none", "arrow"],
532
+ symbolSize: local ? 4 : 6,
533
+ lineStyle: { color: colors.accent, ...litLink },
534
+ blur: { lineStyle: { opacity: litLink.opacity } },
535
+ }
536
+ : {}),
537
+ }));
538
+ }
539
+ // ECharts runs this for every label whenever it lays labels out, a zoom included, which is what lets
540
+ // names come in with the zoom without a new option — and a new option would restart the layout.
541
+ function labelLayoutFor(context, zoomNow) {
542
+ const { view, layout } = context;
543
+ const named = namedNodes(context);
544
+ return ({ dataType, dataIndex }) => {
545
+ const node = dataType === "node" ? view.nodes[dataIndex] : undefined;
546
+ if (node === undefined || named.has(node.path)) {
547
+ return { hideOverlap: true };
548
+ }
549
+ const onScreenSpacing = zoomNow() * layout.spacing;
550
+ const shown = Math.min(1, Math.max(0, (onScreenSpacing - LABEL_SPACING_FROM) /
551
+ (LABEL_SPACING_TO - LABEL_SPACING_FROM)));
552
+ return { hideOverlap: true, fontSize: LABEL_FONT_SIZE * shown };
553
+ };
554
+ }
555
+ // Named whatever the zoom: everything in the local graph, the neighborhood of a focused document, and
556
+ // otherwise the most linked-to documents.
557
+ function namedNodes({ view, local, lit }) {
558
+ if (local) {
559
+ return new Set(view.nodes.map((node) => node.path));
560
+ }
561
+ return (lit ??
562
+ new Set([...view.nodes]
563
+ .sort((left, right) => right.inDegree - left.inDegree)
564
+ .slice(0, LABELED_NODES)
565
+ .map((node) => node.path)));
566
+ }
@@ -0,0 +1,21 @@
1
+ export type Commit = {
2
+ short: string;
3
+ date: string;
4
+ subject: string;
5
+ author?: string;
6
+ };
7
+ export declare function initHistory(): void;
8
+ /**
9
+ * What `/api/history` answered: the commits, or the note shown in their place and whether it is an
10
+ * answer (no commit yet) rather than a failure to ask about again. A refusal — git gone, the document
11
+ * no longer in the corpus — says why, rather than reading as a document no commit has touched.
12
+ */
13
+ export declare function readHistory(ok: boolean, answer: {
14
+ commits?: Commit[];
15
+ error?: string;
16
+ }): {
17
+ commits: Commit[];
18
+ } | {
19
+ note: string;
20
+ answered: boolean;
21
+ };
@@ -0,0 +1,86 @@
1
+ // The commits that last touched a document: in the line above a changed document's text, and on
2
+ // its own above an unchanged one's. Loaded the first time the reader opens it, so no page waits for
3
+ // git to walk history; drawn with text nodes only, since a commit subject and an author's name are
4
+ // whatever someone typed.
5
+ export function initHistory() {
6
+ for (const details of document.querySelectorAll(".changes-history")) {
7
+ details.hidden = false;
8
+ let loaded = false;
9
+ details.addEventListener("toggle", () => {
10
+ if (!details.open || loaded) {
11
+ return;
12
+ }
13
+ loaded = true;
14
+ // A history that could not be read is asked for again the next time the reader opens it: a
15
+ // network hiccup or a viewer restarting is not an answer to keep.
16
+ void load(details).then((answered) => {
17
+ loaded = answered;
18
+ });
19
+ });
20
+ }
21
+ }
22
+ /**
23
+ * What `/api/history` answered: the commits, or the note shown in their place and whether it is an
24
+ * answer (no commit yet) rather than a failure to ask about again. A refusal — git gone, the document
25
+ * no longer in the corpus — says why, rather than reading as a document no commit has touched.
26
+ */
27
+ export function readHistory(ok, answer) {
28
+ if (!ok || answer.commits === undefined) {
29
+ return {
30
+ note: answer.error ?? "The history could not be loaded.",
31
+ answered: false,
32
+ };
33
+ }
34
+ return answer.commits.length === 0
35
+ ? { note: "No commit touches this document yet.", answered: true }
36
+ : { commits: answer.commits };
37
+ }
38
+ // Whether the server answered with the history, so it need not be asked again.
39
+ async function load(details) {
40
+ const list = details.querySelector(".changes-history-list");
41
+ if (list === null) {
42
+ return true;
43
+ }
44
+ const status = document.createElement("li");
45
+ status.className = "changes-history-note";
46
+ status.textContent = "Loading…";
47
+ list.replaceChildren(status);
48
+ let answer;
49
+ let ok;
50
+ try {
51
+ const response = await fetch(`/api/history?${new URLSearchParams({ path: details.dataset.path ?? "" }).toString()}`);
52
+ ok = response.ok;
53
+ answer = (await response.json());
54
+ }
55
+ catch {
56
+ status.textContent = "The history could not be loaded.";
57
+ return false;
58
+ }
59
+ const read = readHistory(ok, answer);
60
+ if ("note" in read) {
61
+ status.textContent = read.note;
62
+ return read.answered;
63
+ }
64
+ const { commits } = read;
65
+ list.replaceChildren(...commits.map((commit) => {
66
+ const item = document.createElement("li");
67
+ const hash = document.createElement("code");
68
+ hash.textContent = commit.short;
69
+ const when = document.createElement("span");
70
+ when.className = "changes-history-date";
71
+ when.textContent = commit.date;
72
+ const subject = document.createElement("span");
73
+ subject.className = "changes-history-subject";
74
+ subject.textContent = commit.subject;
75
+ item.append(hash, " ", when, " ");
76
+ if (commit.author !== undefined) {
77
+ const author = document.createElement("span");
78
+ author.className = "changes-history-author";
79
+ author.textContent = commit.author;
80
+ item.append(author, " ");
81
+ }
82
+ item.append(subject);
83
+ return item;
84
+ }));
85
+ return true;
86
+ }