@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,183 @@
1
+ import { onServerEvent } from "./live-reload.js";
2
+ import { changeLetter, changesCount, drawnByServer, markElement, reviewDot, } from "./tree-marks.js";
3
+ // Remembered for this address, like the findings switch.
4
+ const FILTER_KEY = "wastech-mdlint-viewer:tree-changed-only";
5
+ const UNREVIEWED_KEY = "wastech-mdlint-viewer:tree-unreviewed-only";
6
+ export function initChanges() {
7
+ // Drawn by the server only in a git work tree; without it there is nothing to review.
8
+ const section = document.querySelector('.section-nav a[href="/changes"]');
9
+ if (section === null) {
10
+ return;
11
+ }
12
+ initFilter();
13
+ const refresh = () => {
14
+ void load(section);
15
+ };
16
+ if (drawnByServer("changes")) {
17
+ filterReady = true;
18
+ }
19
+ else {
20
+ refresh();
21
+ }
22
+ onServerEvent("change", refresh);
23
+ // A mark saved on any page changes what is left everywhere, and a commit, a checkout or a fetch
24
+ // can change what changed at all.
25
+ onServerEvent("review", refresh);
26
+ onServerEvent("git", refresh);
27
+ }
28
+ async function load(section) {
29
+ let data;
30
+ try {
31
+ data = (await (await fetch("/api/changes")).json());
32
+ }
33
+ catch {
34
+ // The server is restarting; the next change event or page load tries again.
35
+ return;
36
+ }
37
+ const changed = data.status === "ready"
38
+ ? new Map(data.documents.map((entry) => [entry.path, entry]))
39
+ : new Map();
40
+ markSection(section, data);
41
+ markTree(changed);
42
+ filterReady = true;
43
+ applyFilter();
44
+ }
45
+ function markSection(section, data) {
46
+ section.querySelector(".segment-count")?.remove();
47
+ if (data.status !== "ready") {
48
+ return;
49
+ }
50
+ const review = data.review?.state === "ready" ? data.review : null;
51
+ const count = changesCount(data.documents.length, review === null ? null : { left: review.left, total: review.total });
52
+ if (count !== undefined) {
53
+ section.append(markElement(count));
54
+ }
55
+ }
56
+ function markTree(changed) {
57
+ for (const mark of document.querySelectorAll(".tree-change, .tree-review-dot")) {
58
+ mark.remove();
59
+ }
60
+ for (const item of document.querySelectorAll(".sidebar li[data-changed], .sidebar li[data-unreviewed]")) {
61
+ item.removeAttribute("data-changed");
62
+ item.removeAttribute("data-unreviewed");
63
+ }
64
+ for (const link of document.querySelectorAll('.sidebar a[href^="/doc/"]')) {
65
+ const relPath = decodeURIComponent(link.getAttribute("href").slice("/doc/".length));
66
+ const entry = changed.get(relPath);
67
+ if (entry !== undefined) {
68
+ markFile(link, entry);
69
+ }
70
+ }
71
+ }
72
+ // The letter and the dot on one file's row, and the flags its row and every folder above it carry
73
+ // for the switches.
74
+ function markFile(link, entry) {
75
+ const mark = markElement(changeLetter(entry));
76
+ // Before a findings count, so the two read in the same place on every row that has both.
77
+ const badge = link.querySelector(".tree-badge");
78
+ if (badge === null) {
79
+ link.append(mark);
80
+ }
81
+ else {
82
+ badge.before(mark);
83
+ }
84
+ const dot = reviewDot(entry.review?.status ?? null);
85
+ if (dot !== undefined) {
86
+ mark.after(markElement(dot));
87
+ }
88
+ for (let item = link.closest("li"); item !== null; item = item.parentElement?.closest(".sidebar li") ?? null) {
89
+ item.dataset.changed = "";
90
+ if (dot !== undefined) {
91
+ item.dataset.unreviewed = "";
92
+ }
93
+ }
94
+ }
95
+ // Set once the first answer is in: before that "not changed" only means "not asked yet", and the
96
+ // filter would hide the whole tree.
97
+ let filterReady = false;
98
+ function filterInput() {
99
+ return document.querySelector(".tree-filter-changes input");
100
+ }
101
+ // Drawn only where review marks are on.
102
+ function unreviewedInput() {
103
+ return document.querySelector(".tree-filter-unreviewed input");
104
+ }
105
+ function initFilter() {
106
+ const switches = [
107
+ [filterInput(), FILTER_KEY],
108
+ [unreviewedInput(), UNREVIEWED_KEY],
109
+ ];
110
+ for (const [input, key] of switches) {
111
+ const label = input?.closest(".tree-filter");
112
+ if (input === null || label === null || label === undefined) {
113
+ continue;
114
+ }
115
+ label.hidden = false;
116
+ input.checked = readStored(key) === "1";
117
+ input.addEventListener("change", () => {
118
+ writeStored(key, input.checked ? "1" : "0");
119
+ applyFilter();
120
+ });
121
+ }
122
+ }
123
+ /**
124
+ * Show only the changed files — or only those left to review, which are among them — and open the
125
+ * folders on the way to them. A folder this opened is marked as the changes filters' own — apart
126
+ * from the findings filter's — so turning them off closes exactly what they opened, whatever the
127
+ * other switch is doing.
128
+ */
129
+ function applyFilter() {
130
+ const sidebar = document.querySelector(".sidebar");
131
+ const input = filterInput();
132
+ if (sidebar === null || input === null) {
133
+ return;
134
+ }
135
+ const unreviewed = unreviewedInput()?.checked === true && filterReady;
136
+ const active = (input.checked || unreviewed) && filterReady;
137
+ const attribute = unreviewed ? "data-unreviewed" : "data-changed";
138
+ sidebar.classList.toggle("only-changed", active && !unreviewed);
139
+ sidebar.classList.toggle("only-unreviewed", unreviewed);
140
+ syncFilterFolders(sidebar, active, attribute);
141
+ const empty = sidebar.querySelector(".tree-filter-changes-empty");
142
+ if (empty !== null) {
143
+ empty.textContent = unreviewed
144
+ ? "Nothing left to review."
145
+ : "No document changed.";
146
+ empty.hidden =
147
+ !active || sidebar.querySelector(`li[${attribute}]`) !== null;
148
+ }
149
+ }
150
+ /**
151
+ * Open the folders leading to a marked file while a filter is on, and close again only those the
152
+ * filter itself opened once it is off, so a folder the reader opened by hand stays as they left it.
153
+ */
154
+ function syncFilterFolders(sidebar, active, attribute) {
155
+ for (const details of sidebar.querySelectorAll("details")) {
156
+ const leadsToChange = details.parentElement?.hasAttribute(attribute) === true;
157
+ if (active && leadsToChange && !details.open) {
158
+ details.open = true;
159
+ details.dataset.openedByChangesFilter = "";
160
+ }
161
+ else if (!active && details.dataset.openedByChangesFilter !== undefined) {
162
+ details.open = false;
163
+ delete details.dataset.openedByChangesFilter;
164
+ }
165
+ }
166
+ }
167
+ // Storage can be unavailable (private windows, blocked site data); the filter then starts off.
168
+ function readStored(key) {
169
+ try {
170
+ return localStorage.getItem(key);
171
+ }
172
+ catch {
173
+ return null;
174
+ }
175
+ }
176
+ function writeStored(key, value) {
177
+ try {
178
+ localStorage.setItem(key, value);
179
+ }
180
+ catch {
181
+ // The choice holds for this page only.
182
+ }
183
+ }
@@ -0,0 +1,3 @@
1
+ export type ChartOption = Record<string, unknown>;
2
+ /** A parsed chart option made safe to hand to ECharts, or an error saying why it cannot be one. */
3
+ export declare function safeChartOption(value: unknown): ChartOption;
@@ -0,0 +1,76 @@
1
+ // A chart in a document is drawn from an option its author wrote as JSON, so the option is as
2
+ // untrusted as the Markdown around it. JSON already rules out code: it holds no functions, and the
3
+ // page parses it with `JSON.parse` and never evaluates it. What plain data can still do is reach the
4
+ // few places where ECharts writes markup into the page or opens a URL, and this module closes each:
5
+ //
6
+ // - Tooltips are HTML by default, and ECharts sets the formatter template as `innerHTML`. It escapes
7
+ // the values it fills into `{a}`/`{b}`/`{c}`, but not the template, so `<img onerror=…>` written
8
+ // there would become an element. Every tooltip is switched to `richText`, which draws it on the
9
+ // canvas as text. A tooltip config can sit at the top level, in `baseOption`, a timeline step, a
10
+ // media query, a series or a single data item, and the component reads `renderMode` from whichever
11
+ // wins the merge, so every object under a `tooltip` key is switched, wherever it sits.
12
+ // - The toolbox's data view writes its `title` and `lang` strings into the page as HTML. They are
13
+ // dropped, so it falls back to ECharts' own labels.
14
+ // - `link` and `sublink` (on a title, a treemap or a sunburst node) are opened with `window.open`
15
+ // when clicked. Only an http(s) URL is kept: a `javascript:` URL would run in the viewer's origin
16
+ // if the content security policy ever let it, and no other scheme belongs in a chart.
17
+ //
18
+ // This module has no DOM or ECharts dependency, so the tests run it in Node.
19
+ // Tooltip templates in the ECharts examples break lines with `<br/>`, which richText would print
20
+ // literally; it breaks lines on `\n`.
21
+ const LINE_BREAK = /<br\s*\/?>/gi;
22
+ /** A parsed chart option made safe to hand to ECharts, or an error saying why it cannot be one. */
23
+ export function safeChartOption(value) {
24
+ if (!isRecord(value)) {
25
+ throw new Error("the option must be a JSON object");
26
+ }
27
+ visit(value, undefined);
28
+ // The dark theme paints a background of its own; transparent keeps the chart on the page's colors
29
+ // in both themes, unless the author chose one.
30
+ value.backgroundColor ??= "transparent";
31
+ return value;
32
+ }
33
+ // Array items take their parent's key, so `tooltip: [{…}]` and `series: [{…}]` are seen as
34
+ // tooltips and series.
35
+ function visit(value, key) {
36
+ if (Array.isArray(value)) {
37
+ for (const item of value) {
38
+ visit(item, key);
39
+ }
40
+ return;
41
+ }
42
+ if (!isRecord(value)) {
43
+ return;
44
+ }
45
+ if (key === "tooltip") {
46
+ value.renderMode = "richText";
47
+ if (typeof value.formatter === "string") {
48
+ value.formatter = value.formatter.replace(LINE_BREAK, "\n");
49
+ }
50
+ }
51
+ else if (key === "dataView") {
52
+ delete value.title;
53
+ delete value.lang;
54
+ }
55
+ if (typeof value.link === "string" && !isWebUrl(value.link)) {
56
+ delete value.link;
57
+ }
58
+ if (typeof value.sublink === "string" && !isWebUrl(value.sublink)) {
59
+ delete value.sublink;
60
+ }
61
+ for (const [childKey, child] of Object.entries(value)) {
62
+ visit(child, childKey);
63
+ }
64
+ }
65
+ function isWebUrl(url) {
66
+ try {
67
+ const { protocol } = new URL(url);
68
+ return protocol === "http:" || protocol === "https:";
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
74
+ function isRecord(value) {
75
+ return typeof value === "object" && value !== null && !Array.isArray(value);
76
+ }
@@ -0,0 +1 @@
1
+ export declare function initCharts(): void;
@@ -0,0 +1,54 @@
1
+ import { safeChartOption } from "./chart-option.js";
2
+ import { echartsTheme, loadECharts } from "./echarts.js";
3
+ import { effectiveTheme, onThemeChange } from "./theme-toggle.js";
4
+ // The `echarts` blocks of a document, drawn from the JSON the server leaves in each
5
+ // `.echarts-chart`. A block the server already found invalid carries `data-state="invalid"` and its
6
+ // reason, and is left alone. The source stays in the page after drawing (hidden by CSS), so a chart
7
+ // that fails here still shows what its author wrote.
8
+ export function initCharts() {
9
+ const blocks = document.querySelectorAll(".echarts-chart:not([data-state])");
10
+ if (blocks.length === 0) {
11
+ return;
12
+ }
13
+ const charts = [];
14
+ for (const block of blocks) {
15
+ void draw(block).then((chart) => chart !== undefined && charts.push(chart));
16
+ }
17
+ // ECharts writes its colors into the canvas, so CSS cannot recolor a chart; the theme is switched
18
+ // in place, which keeps the chart's state (a zoomed axis, a hidden series) as the reader left it.
19
+ onThemeChange((theme) => {
20
+ for (const chart of charts) {
21
+ chart.setTheme(echartsTheme(theme));
22
+ }
23
+ });
24
+ }
25
+ async function draw(block) {
26
+ const source = block.querySelector(".echarts-source")?.textContent ?? "";
27
+ let canvas;
28
+ try {
29
+ const option = safeChartOption(JSON.parse(source));
30
+ const echarts = await loadECharts();
31
+ canvas = document.createElement("div");
32
+ canvas.className = "echarts-canvas";
33
+ block.append(canvas);
34
+ const chart = echarts.init(canvas, echartsTheme(effectiveTheme()));
35
+ chart.setOption(option);
36
+ // Also what draws a chart inside a closed <details>: it starts at zero size and gets its real
37
+ // one when the section opens.
38
+ new ResizeObserver(() => {
39
+ chart.resize();
40
+ }).observe(canvas);
41
+ block.dataset.state = "rendered";
42
+ return chart;
43
+ }
44
+ catch (error) {
45
+ // The source stays on the page; the message says why it is not a picture.
46
+ canvas?.remove();
47
+ block.dataset.state = "failed";
48
+ const message = document.createElement("p");
49
+ message.className = "echarts-error";
50
+ message.textContent = `Chart could not be drawn: ${error instanceof Error ? error.message : String(error)}`;
51
+ block.prepend(message);
52
+ return undefined;
53
+ }
54
+ }
@@ -0,0 +1 @@
1
+ export declare function copyText(text: string): Promise<boolean>;
@@ -0,0 +1,43 @@
1
+ // Copying text for the page's copy buttons. The asynchronous Clipboard API is available only in a
2
+ // secure context, which a viewer bound to a LAN address with `--host` is not (plain http on a
3
+ // non-loopback address), so a selected, off-screen textarea and the older copy command stand in
4
+ // there. Either way the caller learns whether the text actually reached the clipboard, so a button
5
+ // never claims "Copied" when it was not.
6
+ export async function copyText(text) {
7
+ // The asynchronous clipboard exists exactly in a secure context, so that is the one check needed.
8
+ if (window.isSecureContext) {
9
+ try {
10
+ await navigator.clipboard.writeText(text);
11
+ return true;
12
+ }
13
+ catch {
14
+ // A denied permission or an unfocused document; the fallback may still work.
15
+ }
16
+ }
17
+ return copyThroughSelection(text);
18
+ }
19
+ function copyThroughSelection(text) {
20
+ const previous = document.activeElement;
21
+ const area = document.createElement("textarea");
22
+ area.value = text;
23
+ area.readOnly = true;
24
+ // Off screen and out of the layout, so selecting it scrolls nothing and moves nothing.
25
+ area.style.position = "fixed";
26
+ area.style.top = "0";
27
+ area.style.left = "-9999px";
28
+ document.body.append(area);
29
+ area.select();
30
+ let copied = false;
31
+ try {
32
+ // eslint-disable-next-line @typescript-eslint/no-deprecated -- the one way to copy an insecure context has: navigator.clipboard does not exist there
33
+ copied = document.execCommand("copy");
34
+ }
35
+ catch {
36
+ copied = false;
37
+ }
38
+ area.remove();
39
+ if (previous instanceof HTMLElement) {
40
+ previous.focus();
41
+ }
42
+ return copied;
43
+ }
@@ -0,0 +1 @@
1
+ export declare function initCodeCopy(): void;
@@ -0,0 +1,29 @@
1
+ import { copyText } from "./clipboard.js";
2
+ // The "Copy" button in the corner of every highlighted code block: in a document, in a linked source
3
+ // file and in the config page's current file. It copies the block's code as it stands in the file —
4
+ // the text of the code element, which holds the lines and the line breaks between them, and none of
5
+ // the line numbers, which the stylesheet draws as generated content.
6
+ // Long enough to read "Copied", short enough that the button is ready for the next block.
7
+ const DONE_MS = 1500;
8
+ export function initCodeCopy() {
9
+ for (const button of document.querySelectorAll("button.code-copy")) {
10
+ const code = button.parentElement?.querySelector("pre code");
11
+ if (code === null || code === undefined) {
12
+ continue;
13
+ }
14
+ // Rendered hidden by the server: without this script the button could do nothing.
15
+ button.hidden = false;
16
+ let timer;
17
+ button.addEventListener("click", () => {
18
+ void copyText(code.textContent).then((copied) => {
19
+ button.textContent = copied ? "Copied" : "Copy failed";
20
+ button.classList.add("is-done");
21
+ window.clearTimeout(timer);
22
+ timer = window.setTimeout(() => {
23
+ button.textContent = "Copy";
24
+ button.classList.remove("is-done");
25
+ }, DONE_MS);
26
+ });
27
+ });
28
+ }
29
+ }
@@ -0,0 +1 @@
1
+ export declare function initColumnResize(): void;
@@ -0,0 +1,120 @@
1
+ // The handles on the inner edges of the two side columns: the document tree on the left and the rail
2
+ // of links, outline and backlinks on the right. Dragging a handle, or the arrow keys while it has
3
+ // focus, moves that edge; a double click puts the column's default width back. Each width lives in
4
+ // one custom property on <html> that the layout grid reads, and is remembered for this address:
5
+ // `theme.ts` restores it before the first paint, so a widened column does not jump back on every
6
+ // page.
7
+ const COLUMNS = {
8
+ sidebar: {
9
+ variable: "--sidebar-width",
10
+ storageKey: "wastech-mdlint-viewer:sidebar-width",
11
+ edge: "end",
12
+ },
13
+ rail: {
14
+ variable: "--rail-width",
15
+ storageKey: "wastech-mdlint-viewer:rail-width",
16
+ edge: "start",
17
+ },
18
+ };
19
+ const MIN_WIDTH = 200;
20
+ const MAX_WIDTH = 640;
21
+ const KEY_STEP = 16;
22
+ // However wide the reader makes both columns, the article between them keeps this much room.
23
+ const MIN_ARTICLE_WIDTH = 360;
24
+ export function initColumnResize() {
25
+ const handles = [
26
+ ...document.querySelectorAll(".column-resizer"),
27
+ ];
28
+ for (const handle of handles) {
29
+ const column = COLUMNS[handle.dataset.column ?? ""];
30
+ const frame = handle.parentElement;
31
+ if (column !== undefined && frame !== null) {
32
+ attach(handle, frame, column, handles);
33
+ }
34
+ }
35
+ }
36
+ // Measured on every call rather than once: the window and the other column can change width
37
+ // between two drags.
38
+ function widestAllowed(handle, handles) {
39
+ const others = handles
40
+ .filter((other) => other !== handle)
41
+ .reduce((sum, other) => sum + (other.parentElement?.getBoundingClientRect().width ?? 0), 0);
42
+ return Math.max(MIN_WIDTH, Math.min(MAX_WIDTH, window.innerWidth - others - MIN_ARTICLE_WIDTH));
43
+ }
44
+ function attach(handle, frame, column, handles) {
45
+ // Rendered hidden by the server: without this script the handle could do nothing.
46
+ handle.hidden = false;
47
+ const root = document.documentElement;
48
+ const outward = column.edge === "end" ? 1 : -1;
49
+ const width = () => Math.round(frame.getBoundingClientRect().width);
50
+ const maxWidth = () => widestAllowed(handle, handles);
51
+ // A separator that can be focused is a control, and assistive technology reads its value.
52
+ const describe = (current) => {
53
+ handle.setAttribute("aria-valuemin", String(MIN_WIDTH));
54
+ handle.setAttribute("aria-valuemax", String(maxWidth()));
55
+ handle.setAttribute("aria-valuenow", String(current));
56
+ };
57
+ const apply = (next) => {
58
+ const clamped = Math.round(Math.min(maxWidth(), Math.max(MIN_WIDTH, next)));
59
+ root.style.setProperty(column.variable, `${clamped}px`);
60
+ describe(clamped);
61
+ };
62
+ const remember = () => {
63
+ try {
64
+ localStorage.setItem(column.storageKey, String(width()));
65
+ }
66
+ catch {
67
+ // Not remembered across pages, but applied to this one.
68
+ }
69
+ };
70
+ describe(width());
71
+ handle.addEventListener("pointerdown", (event) => {
72
+ if (event.button !== 0) {
73
+ return;
74
+ }
75
+ // Keeps the drag from selecting the column's text.
76
+ event.preventDefault();
77
+ // Captured, so the drag follows the pointer past the handle's few pixels and over the article.
78
+ handle.setPointerCapture(event.pointerId);
79
+ const startX = event.clientX;
80
+ const startWidth = width();
81
+ document.body.classList.add("is-resizing-column");
82
+ // Keeps this handle's line and grip shown while the pointer runs ahead of it, and the other
83
+ // handle's hidden.
84
+ handle.classList.add("is-dragging");
85
+ const move = (moved) => {
86
+ apply(startWidth + outward * (moved.clientX - startX));
87
+ };
88
+ const end = () => {
89
+ handle.removeEventListener("pointermove", move);
90
+ handle.removeEventListener("pointerup", end);
91
+ handle.removeEventListener("pointercancel", end);
92
+ document.body.classList.remove("is-resizing-column");
93
+ handle.classList.remove("is-dragging");
94
+ remember();
95
+ };
96
+ handle.addEventListener("pointermove", move);
97
+ handle.addEventListener("pointerup", end);
98
+ handle.addEventListener("pointercancel", end);
99
+ });
100
+ // The arrow keys move the edge the way they point, as a drag would.
101
+ handle.addEventListener("keydown", (event) => {
102
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") {
103
+ return;
104
+ }
105
+ event.preventDefault();
106
+ const step = event.key === "ArrowRight" ? KEY_STEP : -KEY_STEP;
107
+ apply(width() + outward * step);
108
+ remember();
109
+ });
110
+ handle.addEventListener("dblclick", () => {
111
+ root.style.removeProperty(column.variable);
112
+ describe(width());
113
+ try {
114
+ localStorage.removeItem(column.storageKey);
115
+ }
116
+ catch {
117
+ // Nothing stored to forget.
118
+ }
119
+ });
120
+ }
@@ -0,0 +1,13 @@
1
+ export declare function element<K extends keyof HTMLElementTagNameMap>(tag: K, className?: string): HTMLElementTagNameMap[K];
2
+ export declare function button(text: string, className?: string): HTMLButtonElement;
3
+ export declare function paragraph(text: string, className?: string): HTMLParagraphElement;
4
+ export declare function hint(text: string): HTMLElement;
5
+ export declare function section(title: string, description: string): HTMLElement;
6
+ export declare function labelText(label: string, required: boolean): string;
7
+ export type FieldName = {
8
+ label: string;
9
+ required: boolean;
10
+ description?: string | undefined;
11
+ wrapperTag?: "label" | "div";
12
+ };
13
+ export declare function labelled(control: HTMLElement, name: FieldName): HTMLElement;
@@ -0,0 +1,47 @@
1
+ // Small element builders the config editor's modules share, so every part of the page names its
2
+ // classes and wraps its fields the same way.
3
+ export function element(tag, className) {
4
+ const node = document.createElement(tag);
5
+ if (className !== undefined) {
6
+ node.className = className;
7
+ }
8
+ return node;
9
+ }
10
+ export function button(text, className) {
11
+ const node = element("button", className);
12
+ node.type = "button";
13
+ node.textContent = text;
14
+ return node;
15
+ }
16
+ export function paragraph(text, className) {
17
+ const node = element("p", className);
18
+ node.textContent = text;
19
+ return node;
20
+ }
21
+ export function hint(text) {
22
+ return paragraph(text, "config-hint");
23
+ }
24
+ export function section(title, description) {
25
+ const node = element("section", "config-section");
26
+ const heading = element("h2");
27
+ heading.textContent = title;
28
+ node.append(heading, hint(description));
29
+ return node;
30
+ }
31
+ export function labelText(label, required) {
32
+ return required ? `${label} *` : label;
33
+ }
34
+ // A field's name beside its control. The wrapper is a <label> for a single input, so a click on the
35
+ // name focuses it; a segmented control is a group of buttons, and a label around it would pass a
36
+ // click on the name to its first button, choosing that value, so it is wrapped in a <div> instead
37
+ // and named by its own `aria-label`.
38
+ export function labelled(control, name) {
39
+ const wrapper = element(name.wrapperTag ?? "label", "config-field");
40
+ const text = element("span", "config-field-name");
41
+ text.textContent = labelText(name.label, name.required);
42
+ wrapper.append(text, control);
43
+ if (name.description !== undefined) {
44
+ wrapper.append(hint(name.description));
45
+ }
46
+ return wrapper;
47
+ }
@@ -0,0 +1,73 @@
1
+ import type { Edit } from "./config-types.js";
2
+ export type Draft = {
3
+ base: string;
4
+ text: string;
5
+ edits: Edit[];
6
+ mode: "form" | "text";
7
+ textEdited: boolean;
8
+ full?: string;
9
+ saving?: {
10
+ text: string;
11
+ edited: boolean;
12
+ edits?: Edit[];
13
+ };
14
+ };
15
+ /** The config file a draft is checked against: its text and that text's hash. */
16
+ export type FileVersion = {
17
+ text: string;
18
+ hash: string;
19
+ };
20
+ /**
21
+ * A stored value as a draft, or `undefined` when it is not one. Storage is written by this page
22
+ * alone, under keys only it uses, but also by older versions of the viewer and by hand; a value that
23
+ * does not have a draft's shape would break the editor rather than restore anything.
24
+ */
25
+ export declare function parseDraft(value: unknown): Draft | undefined;
26
+ /**
27
+ * The changed file's whole text, for copying or downloading: the text itself when no form edit is
28
+ * left to apply to it, the server's last answer when there is, or `undefined` when there is none.
29
+ */
30
+ export declare function draftText(draft: Draft): string | undefined;
31
+ /**
32
+ * What is left of a draft once its save has written `saved`: nothing, when the reader changed
33
+ * nothing while the save was on its way, or the change they made meanwhile, now based on the saved
34
+ * file.
35
+ *
36
+ * A change made in the form stays one: the saved text, with the edits made after the save began.
37
+ * It has to, because a draft whose text is taken for the reader's own replaces the file when it is
38
+ * later set aside and applied — and the text it held was the one from before the save, so applying
39
+ * it would put back what another program changed since. A text the reader wrote, or a draft whose
40
+ * edits cannot be split at the save, keeps its text and edits as they are: applied to the text they
41
+ * were made to, they give the saved text plus the later change.
42
+ */
43
+ export declare function afterSave(draft: Draft, saved: FileVersion): Draft | undefined;
44
+ /**
45
+ * What to do with the stored drafts once the page has read the config file:
46
+ *
47
+ * - `restore`: the draft to put back into the editor, made to this version of the file.
48
+ * - `setAside`: a draft made to another version, to offer back to the reader.
49
+ *
50
+ * A draft whose save wrote the file the page now reads is not stale: the reload that save started
51
+ * reached the page before the save's answer did, and the draft becomes whatever the reader changed
52
+ * after saving. Any other draft for another version is set aside, replacing an older one set aside
53
+ * before — that one the reader was already shown, and went on to make a new change instead.
54
+ */
55
+ export declare function planDrafts(stored: Draft | undefined, setAside: Draft | undefined, file: FileVersion): {
56
+ restore: Draft | undefined;
57
+ setAside: Draft | undefined;
58
+ };
59
+ /**
60
+ * A set-aside draft, made to apply to the current file: a change made in the form replays its edits
61
+ * onto the current text, as they name values by their place in the config, not by line; a text the
62
+ * reader wrote replaces the current one, and the preview then shows what that does.
63
+ */
64
+ export declare function rebaseDraft(draft: Draft, file: FileVersion): Draft;
65
+ /**
66
+ * Keeps the draft, or forgets it when there is nothing unsaved; `false` when storage refused, for a
67
+ * caller about to reload the page and count on finding it there.
68
+ */
69
+ export declare function storeDraft(draft: Draft | undefined): boolean;
70
+ export declare function clearDraft(): void;
71
+ export declare function readDraft(): Draft | undefined;
72
+ export declare function storeSetAside(draft: Draft | undefined): void;
73
+ export declare function readSetAside(): Draft | undefined;