@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,60 @@
1
+ // Marks the section being read in the rail's "On this page" outline as the page scrolls, so on a long
2
+ // document the outline also says where in it the reader is. The section is the one whose heading was
3
+ // passed last: the last heading above a line a little under the header, where a jump to a heading
4
+ // lands it. Measured on scroll, once a frame, rather than with an IntersectionObserver, which reports
5
+ // headings entering and leaving a band and so loses track when a long section has none in view.
6
+ export function initOutlineSpy() {
7
+ const links = [
8
+ ...document.querySelectorAll('.outline a[href^="#"]'),
9
+ ];
10
+ const sections = links.flatMap((link) => {
11
+ const heading = document.getElementById(decodeURIComponent(link.hash.slice(1)));
12
+ return heading === null ? [] : [{ link, heading }];
13
+ });
14
+ if (sections.length === 0) {
15
+ return;
16
+ }
17
+ let current;
18
+ const mark = (link) => {
19
+ if (link === current) {
20
+ return;
21
+ }
22
+ current?.removeAttribute("aria-current");
23
+ link?.setAttribute("aria-current", "location");
24
+ current = link;
25
+ };
26
+ const update = () => {
27
+ // The line a jump to a heading puts it on: the page's own scroll padding, under the sticky header.
28
+ const line = (parseFloat(getComputedStyle(document.documentElement).scrollPaddingTop) || 0) + 8;
29
+ const atBottom = window.innerHeight + window.scrollY >=
30
+ document.documentElement.scrollHeight - 2;
31
+ // At the very bottom the last sections may be too short ever to reach the line; the reader has
32
+ // still read to them.
33
+ if (atBottom) {
34
+ mark(sections.at(-1).link);
35
+ return;
36
+ }
37
+ let passed;
38
+ for (const { link, heading } of sections) {
39
+ if (heading.getBoundingClientRect().top > line) {
40
+ break;
41
+ }
42
+ passed = link;
43
+ }
44
+ mark(passed);
45
+ };
46
+ let scheduled = false;
47
+ const schedule = () => {
48
+ if (scheduled) {
49
+ return;
50
+ }
51
+ scheduled = true;
52
+ requestAnimationFrame(() => {
53
+ scheduled = false;
54
+ update();
55
+ });
56
+ };
57
+ window.addEventListener("scroll", schedule, { passive: true });
58
+ window.addEventListener("resize", schedule);
59
+ update();
60
+ }
@@ -0,0 +1,7 @@
1
+ /** A prompt button for `path`'s findings: all of them, or one finding's, by its rule and line. */
2
+ export declare function promptButton(path: string, finding?: {
3
+ ruleId: string;
4
+ line: number;
5
+ }): HTMLButtonElement;
6
+ /** Wire every prompt button under `root` that is not wired yet. */
7
+ export declare function initPromptButtons(root: ParentNode): void;
@@ -0,0 +1,65 @@
1
+ import { copyText } from "./clipboard.js";
2
+ import { attachMenu } from "./menu.js";
3
+ /** A prompt button for `path`'s findings: all of them, or one finding's, by its rule and line. */
4
+ export function promptButton(path, finding) {
5
+ const button = document.createElement("button");
6
+ button.type = "button";
7
+ button.className = "prompt-menu";
8
+ button.textContent = finding === undefined ? "Prompt for all" : "Prompt";
9
+ button.title =
10
+ finding === undefined
11
+ ? "Copy a prompt that answers every finding in this document"
12
+ : "Copy a prompt that answers this finding";
13
+ button.dataset.path = path;
14
+ if (finding !== undefined) {
15
+ button.dataset.rule = finding.ruleId;
16
+ // Not `data-line`: inside the article that attribute is a block's source line, and the stylesheet
17
+ // draws it in the right margin of whatever element carries it — a finding's line there showed up
18
+ // as a stray number beside the button.
19
+ button.dataset.findingLine = String(finding.line);
20
+ }
21
+ return button;
22
+ }
23
+ /** Wire every prompt button under `root` that is not wired yet. */
24
+ export function initPromptButtons(root) {
25
+ for (const button of root.querySelectorAll("button.prompt-menu:not([data-ready])")) {
26
+ const path = button.dataset.path;
27
+ if (path === undefined) {
28
+ continue;
29
+ }
30
+ button.dataset.ready = "";
31
+ // Rendered hidden by the server where it renders the button: without this script it could do
32
+ // nothing.
33
+ button.hidden = false;
34
+ const query = new URLSearchParams({ path });
35
+ if (button.dataset.rule !== undefined &&
36
+ button.dataset.findingLine !== undefined) {
37
+ query.set("rule", button.dataset.rule);
38
+ query.set("line", button.dataset.findingLine);
39
+ }
40
+ attachMenu(button, () => load(query));
41
+ }
42
+ }
43
+ async function load(query) {
44
+ let answer;
45
+ try {
46
+ answer = (await (await fetch(`/api/prompts?${query.toString()}`)).json());
47
+ }
48
+ catch {
49
+ return [];
50
+ }
51
+ if (answer.prompts === undefined) {
52
+ // Shown as the one, disabled entry, so the reader learns why there is nothing to copy.
53
+ return [
54
+ {
55
+ label: "Nothing to copy",
56
+ unavailable: answer.error ?? "The prompts could not be loaded.",
57
+ run: () => Promise.resolve(false),
58
+ },
59
+ ];
60
+ }
61
+ return answer.prompts.map((prompt) => ({
62
+ label: prompt.label,
63
+ run: () => copyText(prompt.text),
64
+ }));
65
+ }
@@ -0,0 +1 @@
1
+ export declare function initReview(documentPath: string | undefined): void;
@@ -0,0 +1,379 @@
1
+ // The reader's review marks: the "Viewed" checkbox on each card of the Changes page and above a
2
+ // changed document, the button that starts the review over, and the keys that move through what is
3
+ // left. The server renders every status, so a page reads right without this script; the script only
4
+ // sends a mark and shows the answer — never a mark it was not told was saved.
5
+ const CHIP_TEXT = {
6
+ reviewed: "Reviewed",
7
+ changed: "Changed since review",
8
+ };
9
+ export function initReview(documentPath) {
10
+ // Carried only by the pages that may send a mark, and only where marks are on.
11
+ const token = document.querySelector('meta[name="wastech-change-token"]')?.content;
12
+ const toggles = [
13
+ ...document.querySelectorAll(".review-toggle"),
14
+ ];
15
+ if (token === undefined || toggles.length === 0) {
16
+ return;
17
+ }
18
+ for (const toggle of toggles) {
19
+ toggle.hidden = false;
20
+ const input = toggle.querySelector("input");
21
+ input?.addEventListener("change", () => {
22
+ void mark(toggle, input, token);
23
+ });
24
+ }
25
+ for (const button of document.querySelectorAll(".section-mark")) {
26
+ button.hidden = false;
27
+ button.addEventListener("click", () => {
28
+ void markSection(button, token);
29
+ });
30
+ }
31
+ const reset = document.querySelector(".review-reset");
32
+ if (reset !== null) {
33
+ reset.hidden = false;
34
+ reset.addEventListener("click", () => {
35
+ void resetReview(reset, toggles, token);
36
+ });
37
+ }
38
+ document.addEventListener("keydown", (event) => {
39
+ onKey(event, toggles, documentPath);
40
+ });
41
+ }
42
+ async function mark(toggle, input, token) {
43
+ const reviewed = input.checked;
44
+ input.disabled = true;
45
+ const answer = await post(token, "/api/review/mark", {
46
+ path: toggle.dataset.path,
47
+ hash: toggle.dataset.hash,
48
+ reviewed,
49
+ worktree: toggle.dataset.worktree === "1",
50
+ });
51
+ input.disabled = false;
52
+ if (answer.status !== "saved") {
53
+ // The mark is not shown as set until the server says it was written.
54
+ input.checked = !reviewed;
55
+ say(toggle, answer.error);
56
+ return;
57
+ }
58
+ const saved = answer;
59
+ const status = saved.documents[0]?.review.status ?? "unreviewed";
60
+ input.checked = status === "reviewed";
61
+ say(toggle, "");
62
+ showStatus(toggle, status);
63
+ showSections(saved.documents[0]?.sections ?? []);
64
+ showSectionProgress(toggle, saved.documents[0]?.sections ?? []);
65
+ showCount(saved);
66
+ }
67
+ /**
68
+ * The card's "2 of 5 sections", restated from the sections the server answered with: marking a
69
+ * document as a whole marks or clears its sections with it, so the count the page was served with
70
+ * no longer holds. Shown under the same rule as the server's: only while some are viewed and some
71
+ * are not.
72
+ */
73
+ function showSectionProgress(toggle, sections) {
74
+ const card = toggle.closest(".change-card");
75
+ if (card === null) {
76
+ return;
77
+ }
78
+ card.querySelector(":scope > header .review-sections")?.remove();
79
+ const reviewed = sections.filter((section) => section.status === "reviewed").length;
80
+ if (reviewed === 0 || reviewed === sections.length) {
81
+ return;
82
+ }
83
+ const progress = document.createElement("span");
84
+ progress.className = "review-sections";
85
+ progress.title = "Sections viewed from the document's outline";
86
+ progress.textContent = `${String(reviewed)} of ${String(sections.length)} sections`;
87
+ card.querySelector(":scope > header .change-stats")?.before(progress);
88
+ }
89
+ /**
90
+ * Mark one section from the outline, and show what the server answered: the section, and the
91
+ * document, which is reviewed once every one of its sections is.
92
+ */
93
+ async function markSection(button, token) {
94
+ const reviewed = button.getAttribute("aria-pressed") !== "true";
95
+ button.disabled = true;
96
+ const answer = await post(token, "/api/review/section", {
97
+ path: button.dataset.path,
98
+ section: button.dataset.section,
99
+ hash: button.dataset.hash,
100
+ reviewed,
101
+ });
102
+ button.disabled = false;
103
+ const toggle = document.querySelector(".changes-banner .review-toggle");
104
+ if (answer.status !== "saved") {
105
+ if (toggle !== null) {
106
+ say(toggle, answer.error);
107
+ }
108
+ return;
109
+ }
110
+ const saved = answer;
111
+ if (saved.section !== undefined) {
112
+ showSections([saved.section]);
113
+ }
114
+ const status = saved.documents[0]?.review.status;
115
+ if (toggle !== null && status !== undefined) {
116
+ const input = toggle.querySelector("input");
117
+ if (input !== null) {
118
+ input.checked = status === "reviewed";
119
+ }
120
+ say(toggle, "");
121
+ showStatus(toggle, status);
122
+ }
123
+ showCount(saved);
124
+ }
125
+ const SECTION_TITLES = {
126
+ reviewed: "Viewed",
127
+ changed: "Changed since you viewed it",
128
+ unreviewed: "Not viewed yet",
129
+ };
130
+ function showSections(sections) {
131
+ for (const section of sections) {
132
+ const button = document.querySelector(`.section-mark[data-section="${CSS.escape(section.key)}"]`);
133
+ if (button === null) {
134
+ continue;
135
+ }
136
+ button.setAttribute("aria-pressed", section.status === "reviewed" ? "true" : "false");
137
+ button.title = SECTION_TITLES[section.status];
138
+ const item = button.closest("li");
139
+ if (item !== null) {
140
+ item.dataset.review = section.status;
141
+ }
142
+ }
143
+ }
144
+ function showCount(saved) {
145
+ const count = document.querySelector(".changes-review-count");
146
+ if (count !== null) {
147
+ count.textContent = `${String(saved.total - saved.left)} of ${String(saved.total)} ${saved.total === 1 ? "document" : "documents"} reviewed`;
148
+ }
149
+ }
150
+ // Marks the card or banner the checkbox belongs to, and the card's row in the list above the cards.
151
+ function showStatus(toggle, status) {
152
+ const card = toggle.closest(".change-card");
153
+ const holder = card ?? toggle.closest(".changes-banner");
154
+ if (holder === null) {
155
+ return;
156
+ }
157
+ holder.dataset.review = status;
158
+ placeChip(holder, status, toggle);
159
+ if (card === null) {
160
+ return;
161
+ }
162
+ if (status === "reviewed") {
163
+ // Folded as the server folds a reviewed card, so what is left stands out.
164
+ const diff = card.querySelector("details.change-diff");
165
+ if (diff !== null) {
166
+ diff.open = false;
167
+ }
168
+ }
169
+ const row = document
170
+ .querySelector(`.changes-files a[href="#${CSS.escape(card.id)}"]`)
171
+ ?.closest("li");
172
+ if (row !== null && row !== undefined) {
173
+ row.dataset.review = status;
174
+ placeChip(row, status, null);
175
+ }
176
+ }
177
+ function placeChip(holder, status, toggle) {
178
+ holder.querySelector(":scope .review-chip")?.remove();
179
+ if (status === "unreviewed") {
180
+ return;
181
+ }
182
+ const chip = document.createElement("span");
183
+ chip.className = `review-chip review-chip-${status}`;
184
+ chip.textContent = CHIP_TEXT[status];
185
+ // Where the server puts it: before the line counts on a card, after the text on the banner,
186
+ // at the end of a row of the list.
187
+ const stats = holder.querySelector(":scope > header .change-stats");
188
+ const text = holder.querySelector(":scope > .changes-banner-text");
189
+ if (stats !== null) {
190
+ stats.before(chip);
191
+ }
192
+ else if (text !== null) {
193
+ text.after(chip);
194
+ }
195
+ else if (toggle !== null) {
196
+ toggle.before(chip);
197
+ }
198
+ else {
199
+ holder.append(chip);
200
+ }
201
+ }
202
+ async function resetReview(button, toggles, token) {
203
+ if (!window.confirm("Forget every review mark of the documents listed here, including the versions reviewed before?")) {
204
+ return;
205
+ }
206
+ button.disabled = true;
207
+ const answer = await post(token, "/api/review/reset", {
208
+ worktree: toggles[0]?.dataset.worktree === "1",
209
+ documents: toggles.map((toggle) => ({
210
+ path: toggle.dataset.path,
211
+ hash: toggle.dataset.hash,
212
+ })),
213
+ });
214
+ button.disabled = false;
215
+ if (answer.status !== "saved") {
216
+ say(button, answer.error);
217
+ return;
218
+ }
219
+ location.reload();
220
+ }
221
+ async function post(token, url, body) {
222
+ try {
223
+ const response = await fetch(url, {
224
+ method: "POST",
225
+ headers: {
226
+ "Content-Type": "application/json",
227
+ "X-Wastech-Viewer-Token": token,
228
+ },
229
+ body: JSON.stringify(body),
230
+ });
231
+ return (await response.json());
232
+ }
233
+ catch {
234
+ const failed = {
235
+ status: "failed",
236
+ error: "The viewer did not answer; the mark was not saved.",
237
+ };
238
+ return failed;
239
+ }
240
+ }
241
+ // One line under the control that failed, read out by assistive technology; empty once it works.
242
+ function say(near, message) {
243
+ const holder = near.closest(".change-card, .changes-banner, .changes-review") ?? near.parentElement;
244
+ if (holder === null) {
245
+ return;
246
+ }
247
+ let line = holder.querySelector(":scope > .review-message");
248
+ if (line === null) {
249
+ if (message === "") {
250
+ return;
251
+ }
252
+ line = document.createElement("p");
253
+ line.className = "review-message";
254
+ line.setAttribute("role", "status");
255
+ holder.append(line);
256
+ }
257
+ line.textContent = message;
258
+ line.hidden = message === "";
259
+ }
260
+ /**
261
+ * `v` marks the card being read, or the open document; `n` and `p` go to the next and previous
262
+ * document not yet reviewed, in the order the Changes page lists them. Only bare keys, and never
263
+ * while the reader is typing: the search box and the config editor take letters as text.
264
+ */
265
+ function onKey(event, toggles, documentPath) {
266
+ const key = reviewKey(event);
267
+ if (key === undefined) {
268
+ return;
269
+ }
270
+ event.preventDefault();
271
+ const cards = [
272
+ ...document.querySelectorAll(".change-card"),
273
+ ].filter((card) => card.querySelector(".review-toggle") !== null);
274
+ if (cards.length === 0) {
275
+ onDocumentPageKey(key, toggles, documentPath);
276
+ return;
277
+ }
278
+ const current = currentCard(cards);
279
+ if (key === "v") {
280
+ current?.querySelector(".review-toggle input")?.click();
281
+ return;
282
+ }
283
+ const step = key === "n" ? 1 : -1;
284
+ if (stepToUnreviewedCard(cards, current, step)) {
285
+ return;
286
+ }
287
+ const toggle = current?.querySelector(".review-toggle");
288
+ if (toggle !== null && toggle !== undefined) {
289
+ say(toggle, step === 1
290
+ ? "Nothing left to review below this one."
291
+ : "Nothing left to review above this one.");
292
+ }
293
+ }
294
+ // The review key a press stands for, or nothing when it is not one: a modifier held means another
295
+ // shortcut, and text being typed must keep its letters.
296
+ function reviewKey(event) {
297
+ if (event.defaultPrevented ||
298
+ event.ctrlKey ||
299
+ event.metaKey ||
300
+ event.altKey ||
301
+ isTyping(event.target)) {
302
+ return undefined;
303
+ }
304
+ const key = event.key.toLowerCase();
305
+ return key === "v" || key === "n" || key === "p" ? key : undefined;
306
+ }
307
+ // A document's page: its own banner, and the list the Changes page would show.
308
+ function onDocumentPageKey(key, toggles, documentPath) {
309
+ if (key === "v") {
310
+ toggles[0]?.querySelector("input")?.click();
311
+ }
312
+ else if (documentPath !== undefined && toggles[0] !== undefined) {
313
+ void goToDocument(toggles[0], documentPath, key === "n" ? 1 : -1);
314
+ }
315
+ }
316
+ // Brings the nearest card not yet reviewed, past `current` in the direction of `step`, to the top;
317
+ // false when there is none, so the caller can say so.
318
+ function stepToUnreviewedCard(cards, current, step) {
319
+ const start = current === undefined ? -1 : cards.indexOf(current);
320
+ for (let index = start + step; index >= 0 && index < cards.length; index += step) {
321
+ const card = cards[index];
322
+ if (card.dataset.review !== "reviewed") {
323
+ card.scrollIntoView({ block: "start" });
324
+ history.replaceState(null, "", `#${card.id}`);
325
+ card
326
+ .querySelector(".review-toggle input")
327
+ ?.focus({ preventScroll: true });
328
+ return true;
329
+ }
330
+ }
331
+ return false;
332
+ }
333
+ function isTyping(target) {
334
+ if (!(target instanceof HTMLElement)) {
335
+ return false;
336
+ }
337
+ if (target instanceof HTMLInputElement) {
338
+ return target.type !== "checkbox" && target.type !== "radio";
339
+ }
340
+ return (target instanceof HTMLTextAreaElement ||
341
+ target instanceof HTMLSelectElement ||
342
+ target.isContentEditable);
343
+ }
344
+ // The card holding the focus, else the first card still in view below the header.
345
+ function currentCard(cards) {
346
+ const focused = document.activeElement?.closest(".change-card");
347
+ if (focused !== null && focused !== undefined && cards.includes(focused)) {
348
+ return focused;
349
+ }
350
+ const header = document.querySelector(".topbar")?.getBoundingClientRect();
351
+ const top = (header?.bottom ?? 0) + 8;
352
+ return cards.find((card) => card.getBoundingClientRect().bottom > top);
353
+ }
354
+ async function goToDocument(toggle, documentPath, step) {
355
+ let documents;
356
+ try {
357
+ const data = (await (await fetch("/api/changes")).json());
358
+ documents = data.documents ?? [];
359
+ }
360
+ catch {
361
+ return;
362
+ }
363
+ const start = documents.findIndex((entry) => entry.path === documentPath);
364
+ for (let index = start + step; index >= 0 && index < documents.length; index += step) {
365
+ const entry = documents[index];
366
+ if (entry.review !== null && entry.review.status !== "reviewed") {
367
+ // A deleted document has no page; its card on the Changes page is where it is reviewed. The
368
+ // page's own way of showing changes goes along, so stepping through keeps the same view.
369
+ location.href =
370
+ entry.status === "D"
371
+ ? `/changes#${entry.anchor}`
372
+ : `/doc/${entry.path.split("/").map(encodeURIComponent).join("/")}${location.search}`;
373
+ return;
374
+ }
375
+ }
376
+ say(toggle, step === 1
377
+ ? "Nothing left to review after this document."
378
+ : "Nothing left to review before this document.");
379
+ }
@@ -0,0 +1 @@
1
+ export declare function initSearch(): void;