@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.
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/assets/favicon.ico +0 -0
- package/assets/viewer.css +4184 -0
- package/dist/asset-routes.d.ts +4 -0
- package/dist/asset-routes.js +107 -0
- package/dist/blame.d.ts +8 -0
- package/dist/blame.js +54 -0
- package/dist/branch-changes.d.ts +8 -0
- package/dist/branch-changes.js +49 -0
- package/dist/candidate-lint-worker.d.ts +1 -0
- package/dist/candidate-lint-worker.js +67 -0
- package/dist/candidate-lint.d.ts +39 -0
- package/dist/candidate-lint.js +115 -0
- package/dist/change-banner.d.ts +38 -0
- package/dist/change-banner.js +108 -0
- package/dist/change-diff.d.ts +23 -0
- package/dist/change-diff.js +140 -0
- package/dist/change-marks.d.ts +32 -0
- package/dist/change-marks.js +363 -0
- package/dist/changes-article.d.ts +15 -0
- package/dist/changes-article.js +244 -0
- package/dist/changes-routes.d.ts +12 -0
- package/dist/changes-routes.js +271 -0
- package/dist/changes.d.ts +113 -0
- package/dist/changes.js +324 -0
- package/dist/cli-args.d.ts +21 -0
- package/dist/cli-args.js +93 -0
- package/dist/client/banner-height.d.ts +1 -0
- package/dist/client/banner-height.js +33 -0
- package/dist/client/change-marks.d.ts +1 -0
- package/dist/client/change-marks.js +56 -0
- package/dist/client/changes.d.ts +1 -0
- package/dist/client/changes.js +183 -0
- package/dist/client/chart-option.d.ts +3 -0
- package/dist/client/chart-option.js +76 -0
- package/dist/client/charts.d.ts +1 -0
- package/dist/client/charts.js +54 -0
- package/dist/client/clipboard.d.ts +1 -0
- package/dist/client/clipboard.js +43 -0
- package/dist/client/code-copy.d.ts +1 -0
- package/dist/client/code-copy.js +29 -0
- package/dist/client/column-resize.d.ts +1 -0
- package/dist/client/column-resize.js +120 -0
- package/dist/client/config-dom.d.ts +13 -0
- package/dist/client/config-dom.js +47 -0
- package/dist/client/config-draft.d.ts +73 -0
- package/dist/client/config-draft.js +218 -0
- package/dist/client/config-editor.d.ts +3 -0
- package/dist/client/config-editor.js +677 -0
- package/dist/client/config-fields.d.ts +35 -0
- package/dist/client/config-fields.js +339 -0
- package/dist/client/config-json.d.ts +18 -0
- package/dist/client/config-json.js +96 -0
- package/dist/client/config-preview.d.ts +9 -0
- package/dist/client/config-preview.js +91 -0
- package/dist/client/config-rules.d.ts +14 -0
- package/dist/client/config-rules.js +202 -0
- package/dist/client/config-types.d.ts +91 -0
- package/dist/client/config-types.js +4 -0
- package/dist/client/copy-path.d.ts +1 -0
- package/dist/client/copy-path.js +32 -0
- package/dist/client/dashboard-requests.d.ts +11 -0
- package/dist/client/dashboard-requests.js +28 -0
- package/dist/client/dashboard.d.ts +1 -0
- package/dist/client/dashboard.js +119 -0
- package/dist/client/diagnostics.d.ts +1 -0
- package/dist/client/diagnostics.js +164 -0
- package/dist/client/diagrams.d.ts +1 -0
- package/dist/client/diagrams.js +81 -0
- package/dist/client/diff-expand.d.ts +1 -0
- package/dist/client/diff-expand.js +42 -0
- package/dist/client/display-settings-model.d.ts +74 -0
- package/dist/client/display-settings-model.js +229 -0
- package/dist/client/display-settings.d.ts +1 -0
- package/dist/client/display-settings.js +411 -0
- package/dist/client/echarts.d.ts +30 -0
- package/dist/client/echarts.js +14 -0
- package/dist/client/graph-layout.d.ts +88 -0
- package/dist/client/graph-layout.js +198 -0
- package/dist/client/graph.d.ts +2 -0
- package/dist/client/graph.js +566 -0
- package/dist/client/history.d.ts +21 -0
- package/dist/client/history.js +86 -0
- package/dist/client/live-reload-model.d.ts +28 -0
- package/dist/client/live-reload-model.js +52 -0
- package/dist/client/live-reload.d.ts +9 -0
- package/dist/client/live-reload.js +138 -0
- package/dist/client/menu.d.ts +14 -0
- package/dist/client/menu.js +176 -0
- package/dist/client/navigation-feedback.d.ts +1 -0
- package/dist/client/navigation-feedback.js +141 -0
- package/dist/client/open-config.d.ts +1 -0
- package/dist/client/open-config.js +48 -0
- package/dist/client/outline-spy.d.ts +1 -0
- package/dist/client/outline-spy.js +60 -0
- package/dist/client/prompts.d.ts +7 -0
- package/dist/client/prompts.js +65 -0
- package/dist/client/review.d.ts +1 -0
- package/dist/client/review.js +379 -0
- package/dist/client/search.d.ts +1 -0
- package/dist/client/search.js +199 -0
- package/dist/client/segmented.d.ts +13 -0
- package/dist/client/segmented.js +70 -0
- package/dist/client/source-lines.d.ts +16 -0
- package/dist/client/source-lines.js +47 -0
- package/dist/client/theme-toggle.d.ts +10 -0
- package/dist/client/theme-toggle.js +100 -0
- package/dist/client/theme.d.ts +0 -0
- package/dist/client/theme.js +50 -0
- package/dist/client/tree-marks.d.ts +31 -0
- package/dist/client/tree-marks.js +102 -0
- package/dist/client/tree-state.d.ts +0 -0
- package/dist/client/tree-state.js +179 -0
- package/dist/client/viewer.d.ts +1 -0
- package/dist/client/viewer.js +53 -0
- package/dist/config-editor.d.ts +166 -0
- package/dist/config-editor.js +378 -0
- package/dist/config-routes.d.ts +18 -0
- package/dist/config-routes.js +213 -0
- package/dist/context-view.d.ts +41 -0
- package/dist/context-view.js +295 -0
- package/dist/corpus-routes.d.ts +31 -0
- package/dist/corpus-routes.js +258 -0
- package/dist/corpus.d.ts +134 -0
- package/dist/corpus.js +653 -0
- package/dist/dashboard-article.d.ts +14 -0
- package/dist/dashboard-article.js +290 -0
- package/dist/dashboard.d.ts +94 -0
- package/dist/dashboard.js +468 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.js +44 -0
- package/dist/document-routes.d.ts +39 -0
- package/dist/document-routes.js +396 -0
- package/dist/event-hub.d.ts +39 -0
- package/dist/event-hub.js +120 -0
- package/dist/findings-panel.d.ts +4 -0
- package/dist/findings-panel.js +74 -0
- package/dist/folder-page.d.ts +8 -0
- package/dist/folder-page.js +74 -0
- package/dist/graph-view.d.ts +66 -0
- package/dist/graph-view.js +236 -0
- package/dist/highlight.d.ts +24 -0
- package/dist/highlight.js +100 -0
- package/dist/http-send.d.ts +9 -0
- package/dist/http-send.js +53 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +72 -0
- package/dist/open-file.d.ts +36 -0
- package/dist/open-file.js +87 -0
- package/dist/page-tree.d.ts +3 -0
- package/dist/page-tree.js +105 -0
- package/dist/page.d.ts +73 -0
- package/dist/page.js +478 -0
- package/dist/prompts.d.ts +25 -0
- package/dist/prompts.js +82 -0
- package/dist/render-cache.d.ts +23 -0
- package/dist/render-cache.js +38 -0
- package/dist/render-document.d.ts +60 -0
- package/dist/render-document.js +440 -0
- package/dist/request-path.d.ts +17 -0
- package/dist/request-path.js +51 -0
- package/dist/review-context.d.ts +24 -0
- package/dist/review-context.js +109 -0
- package/dist/review-queries.d.ts +26 -0
- package/dist/review-queries.js +69 -0
- package/dist/review-state.d.ts +106 -0
- package/dist/review-state.js +251 -0
- package/dist/review-store.d.ts +145 -0
- package/dist/review-store.js +530 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.js +294 -0
- package/dist/search.d.ts +22 -0
- package/dist/search.js +164 -0
- package/dist/security.d.ts +95 -0
- package/dist/security.js +196 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +224 -0
- package/dist/speculation-rules.d.ts +4 -0
- package/dist/speculation-rules.js +49 -0
- package/dist/state-dir.d.ts +6 -0
- package/dist/state-dir.js +31 -0
- package/dist/tree-marks.d.ts +49 -0
- package/dist/tree-marks.js +108 -0
- package/dist/viewer-context.d.ts +37 -0
- package/dist/viewer-context.js +74 -0
- package/dist/watch.d.ts +41 -0
- package/dist/watch.js +135 -0
- package/package.json +57 -4
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { button, element } from "./config-dom.js";
|
|
2
|
+
import { displayText } from "./config-json.js";
|
|
3
|
+
export function rulesList(host, entries) {
|
|
4
|
+
const container = element("div", "config-rule-list");
|
|
5
|
+
entries.forEach((entry, index) => {
|
|
6
|
+
container.append(ruleCard(host, entry, index));
|
|
7
|
+
});
|
|
8
|
+
container.append(ruleAdder(host));
|
|
9
|
+
return container;
|
|
10
|
+
}
|
|
11
|
+
function ruleAdder(host) {
|
|
12
|
+
const picker = rulePicker(host);
|
|
13
|
+
const add = button("Add rule");
|
|
14
|
+
add.addEventListener("click", () => {
|
|
15
|
+
const id = picker.value;
|
|
16
|
+
const entry = id === "custom"
|
|
17
|
+
? {
|
|
18
|
+
rule: "custom",
|
|
19
|
+
id: "",
|
|
20
|
+
options: { assert: { kind: firstAssertKind(host.api.schema) } },
|
|
21
|
+
}
|
|
22
|
+
: { rule: id };
|
|
23
|
+
host.addRule(entry, id === "custom" ? null : id);
|
|
24
|
+
});
|
|
25
|
+
const adder = element("div", "config-rule-add");
|
|
26
|
+
adder.append(picker, add);
|
|
27
|
+
return adder;
|
|
28
|
+
}
|
|
29
|
+
// The rules not configured yet, grouped by category, and a declarative custom rule, which can be
|
|
30
|
+
// added any number of times.
|
|
31
|
+
function rulePicker(host) {
|
|
32
|
+
const configured = host.configuredIds();
|
|
33
|
+
const picker = document.createElement("select");
|
|
34
|
+
picker.setAttribute("aria-label", "Rule to add");
|
|
35
|
+
const categories = new Map();
|
|
36
|
+
for (const rule of host.api.rules) {
|
|
37
|
+
if (!configured.has(rule.id)) {
|
|
38
|
+
categories.set(rule.category, [
|
|
39
|
+
...(categories.get(rule.category) ?? []),
|
|
40
|
+
rule,
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const [category, rules] of categories) {
|
|
45
|
+
const group = document.createElement("optgroup");
|
|
46
|
+
group.label = category;
|
|
47
|
+
for (const rule of rules) {
|
|
48
|
+
const option = new Option(`${rule.id} — ${rule.description}`, rule.id);
|
|
49
|
+
option.title = rule.description;
|
|
50
|
+
group.append(option);
|
|
51
|
+
}
|
|
52
|
+
picker.append(group);
|
|
53
|
+
}
|
|
54
|
+
const custom = document.createElement("optgroup");
|
|
55
|
+
custom.label = "Custom";
|
|
56
|
+
custom.append(new Option("A declarative custom rule", "custom"));
|
|
57
|
+
picker.append(custom);
|
|
58
|
+
return picker;
|
|
59
|
+
}
|
|
60
|
+
function ruleCard(host, entry, index) {
|
|
61
|
+
const id = host.ruleId(index);
|
|
62
|
+
const rule = {
|
|
63
|
+
host,
|
|
64
|
+
index,
|
|
65
|
+
path: ["rules", index],
|
|
66
|
+
id,
|
|
67
|
+
isCustom: entry.rule === "custom",
|
|
68
|
+
info: id === null ? undefined : host.api.rules.find((known) => known.id === id),
|
|
69
|
+
};
|
|
70
|
+
const card = document.createElement("details");
|
|
71
|
+
card.className = "config-rule";
|
|
72
|
+
card.open = host.isOpen(index);
|
|
73
|
+
const summary = document.createElement("summary");
|
|
74
|
+
const describe = ruleDescriber(rule, summary);
|
|
75
|
+
describe(entry);
|
|
76
|
+
card.append(summary);
|
|
77
|
+
const body = ruleBody(rule);
|
|
78
|
+
// The fields' own listeners update the model first; these run after them as the event bubbles.
|
|
79
|
+
const refresh = () => {
|
|
80
|
+
describe((host.current(rule.path) ?? {}));
|
|
81
|
+
};
|
|
82
|
+
body.addEventListener("input", refresh);
|
|
83
|
+
body.addEventListener("change", refresh);
|
|
84
|
+
card.append(body);
|
|
85
|
+
return card;
|
|
86
|
+
}
|
|
87
|
+
// The collapsed card has to say what the rule is set to now, not when the form was drawn: a severity
|
|
88
|
+
// changed below and still shown as "off" here reads as a change that did not take. So the summary's
|
|
89
|
+
// parts are filled by the function this returns, which the card calls again on every change.
|
|
90
|
+
function ruleDescriber(rule, summary) {
|
|
91
|
+
const name = element("code", "config-rule-id");
|
|
92
|
+
const description = element("span", "config-rule-description");
|
|
93
|
+
const severity = element("span", "config-rule-severity");
|
|
94
|
+
summary.append(name, description, severity);
|
|
95
|
+
const { id, info, isCustom } = rule;
|
|
96
|
+
return (current) => {
|
|
97
|
+
const customId = displayText(current.id);
|
|
98
|
+
const customName = customId === "" ? "custom rule" : customId;
|
|
99
|
+
name.textContent = isCustom
|
|
100
|
+
? customName
|
|
101
|
+
: (id ?? displayText(current.rule));
|
|
102
|
+
description.textContent = isCustom
|
|
103
|
+
? displayText(current.description ?? "Custom rule")
|
|
104
|
+
: (info?.description ?? "");
|
|
105
|
+
severity.textContent = displayText(current.severity ?? info?.defaultSeverity);
|
|
106
|
+
// Read by the stylesheet, which tints the pill in the severity's color.
|
|
107
|
+
severity.dataset.severity = severity.textContent;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const SEVERITY_SCHEMA = {
|
|
111
|
+
type: "string",
|
|
112
|
+
enum: ["error", "warning", "off"],
|
|
113
|
+
};
|
|
114
|
+
function ruleBody(rule) {
|
|
115
|
+
const { host, path, isCustom, info } = rule;
|
|
116
|
+
const fields = host.fields;
|
|
117
|
+
const schema = host.api.schema;
|
|
118
|
+
const branch = isCustom
|
|
119
|
+
? customBranch(schema)
|
|
120
|
+
: builtinBranch(schema, rule.id ?? "");
|
|
121
|
+
const body = element("div", "config-rule-body");
|
|
122
|
+
body.append(fields.field({
|
|
123
|
+
schema: SEVERITY_SCHEMA,
|
|
124
|
+
path: [...path, "severity"],
|
|
125
|
+
label: "severity",
|
|
126
|
+
required: false,
|
|
127
|
+
unsetLabel: `default (${info?.defaultSeverity ?? "per finding"})`,
|
|
128
|
+
}));
|
|
129
|
+
if (isCustom) {
|
|
130
|
+
body.append(...customRuleFields(fields, path, branch));
|
|
131
|
+
}
|
|
132
|
+
const optionsSchema = branch?.properties?.options;
|
|
133
|
+
if (optionsSchema !== undefined) {
|
|
134
|
+
body.append(fields.field({
|
|
135
|
+
schema: optionsSchema,
|
|
136
|
+
path: [...path, "options"],
|
|
137
|
+
label: "options",
|
|
138
|
+
required: isCustom,
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
body.append(ruleFooter(rule));
|
|
142
|
+
return body;
|
|
143
|
+
}
|
|
144
|
+
// A custom rule names itself: an id, which is required, an optional description, and its target.
|
|
145
|
+
function customRuleFields(fields, path, branch) {
|
|
146
|
+
const properties = branch?.properties ?? {};
|
|
147
|
+
return [
|
|
148
|
+
fields.field({
|
|
149
|
+
schema: { type: "string" },
|
|
150
|
+
path: [...path, "id"],
|
|
151
|
+
label: "id",
|
|
152
|
+
required: true,
|
|
153
|
+
}),
|
|
154
|
+
fields.field({
|
|
155
|
+
schema: { type: "string" },
|
|
156
|
+
path: [...path, "description"],
|
|
157
|
+
label: "description",
|
|
158
|
+
required: false,
|
|
159
|
+
}),
|
|
160
|
+
fields.field({
|
|
161
|
+
schema: properties.target ?? { type: "string" },
|
|
162
|
+
path: [...path, "target"],
|
|
163
|
+
label: "target",
|
|
164
|
+
required: false,
|
|
165
|
+
}),
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
function ruleFooter(rule) {
|
|
169
|
+
const footer = element("div", "config-rule-footer");
|
|
170
|
+
if (rule.info?.docsUrl !== undefined) {
|
|
171
|
+
const docs = document.createElement("a");
|
|
172
|
+
docs.href = rule.info.docsUrl;
|
|
173
|
+
docs.target = "_blank";
|
|
174
|
+
docs.rel = "noopener noreferrer";
|
|
175
|
+
docs.textContent = "Rule documentation";
|
|
176
|
+
footer.append(docs);
|
|
177
|
+
}
|
|
178
|
+
const remove = button("Remove rule", "config-danger");
|
|
179
|
+
remove.addEventListener("click", () => {
|
|
180
|
+
rule.host.removeRule(rule.index);
|
|
181
|
+
});
|
|
182
|
+
footer.append(remove);
|
|
183
|
+
return footer;
|
|
184
|
+
}
|
|
185
|
+
function builtinBranch(schema, id) {
|
|
186
|
+
return ruleBranches(schema).find((branch) => branch.properties?.rule?.enum?.includes(id) === true);
|
|
187
|
+
}
|
|
188
|
+
// The generic custom branch: `const: "custom"` with an id *pattern*. A project-local schema would
|
|
189
|
+
// add one branch per known custom id, but the package schema the server sends has only this one.
|
|
190
|
+
function customBranch(schema) {
|
|
191
|
+
return ruleBranches(schema).find((branch) => branch.properties?.rule?.const === "custom" &&
|
|
192
|
+
branch.properties.id?.pattern !== undefined);
|
|
193
|
+
}
|
|
194
|
+
function ruleBranches(schema) {
|
|
195
|
+
return schema.properties?.rules?.items?.oneOf ?? [];
|
|
196
|
+
}
|
|
197
|
+
// A new custom rule starts with the first assertion kind the schema lists, so its card opens with
|
|
198
|
+
// that kind's fields to fill in rather than an empty choice.
|
|
199
|
+
function firstAssertKind(schema) {
|
|
200
|
+
const assert = customBranch(schema)?.properties?.options?.properties?.assert;
|
|
201
|
+
return displayText(assert?.oneOf?.[0]?.properties?.kind?.const);
|
|
202
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export type JsonSchema = {
|
|
2
|
+
type?: string;
|
|
3
|
+
properties?: Record<string, JsonSchema>;
|
|
4
|
+
required?: string[];
|
|
5
|
+
items?: JsonSchema;
|
|
6
|
+
enum?: unknown[];
|
|
7
|
+
const?: unknown;
|
|
8
|
+
oneOf?: JsonSchema[];
|
|
9
|
+
additionalProperties?: boolean | JsonSchema;
|
|
10
|
+
description?: string;
|
|
11
|
+
default?: unknown;
|
|
12
|
+
minimum?: number;
|
|
13
|
+
exclusiveMinimum?: number;
|
|
14
|
+
pattern?: string;
|
|
15
|
+
};
|
|
16
|
+
export type RuleInfo = {
|
|
17
|
+
id: string;
|
|
18
|
+
alias: string;
|
|
19
|
+
category: string;
|
|
20
|
+
description: string;
|
|
21
|
+
defaultSeverity: string;
|
|
22
|
+
fixable: boolean;
|
|
23
|
+
docsUrl?: string;
|
|
24
|
+
};
|
|
25
|
+
export type Json = null | boolean | number | string | Json[] | {
|
|
26
|
+
[key: string]: Json;
|
|
27
|
+
};
|
|
28
|
+
export type JsonObject = Record<string, Json>;
|
|
29
|
+
export type Path = (string | number)[];
|
|
30
|
+
export type ConfigApi = {
|
|
31
|
+
file: {
|
|
32
|
+
path: string;
|
|
33
|
+
text: string;
|
|
34
|
+
hash: string;
|
|
35
|
+
} | null;
|
|
36
|
+
config: JsonObject | null;
|
|
37
|
+
ruleIds: (string | null)[];
|
|
38
|
+
errors: string[];
|
|
39
|
+
writeBlocked: string | null;
|
|
40
|
+
schema: JsonSchema;
|
|
41
|
+
rules: RuleInfo[];
|
|
42
|
+
};
|
|
43
|
+
export type Edit = {
|
|
44
|
+
op: "set" | "remove" | "insert";
|
|
45
|
+
path: Path;
|
|
46
|
+
value?: Json;
|
|
47
|
+
};
|
|
48
|
+
export type Finding = {
|
|
49
|
+
filePath: string;
|
|
50
|
+
line: number;
|
|
51
|
+
ruleId: string;
|
|
52
|
+
severity: string;
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
55
|
+
export type OkPreview = {
|
|
56
|
+
status: "ok";
|
|
57
|
+
text: string;
|
|
58
|
+
config: JsonObject;
|
|
59
|
+
ruleIds: (string | null)[];
|
|
60
|
+
documents: {
|
|
61
|
+
before: number;
|
|
62
|
+
after: number;
|
|
63
|
+
};
|
|
64
|
+
findings: {
|
|
65
|
+
before: {
|
|
66
|
+
errors: number;
|
|
67
|
+
warnings: number;
|
|
68
|
+
};
|
|
69
|
+
after: {
|
|
70
|
+
errors: number;
|
|
71
|
+
warnings: number;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
byRule: {
|
|
75
|
+
ruleId: string;
|
|
76
|
+
before: number;
|
|
77
|
+
after: number;
|
|
78
|
+
}[];
|
|
79
|
+
added: Finding[];
|
|
80
|
+
removed: Finding[];
|
|
81
|
+
addedCount: number;
|
|
82
|
+
removedCount: number;
|
|
83
|
+
};
|
|
84
|
+
export type Preview = OkPreview | {
|
|
85
|
+
status: "invalid";
|
|
86
|
+
errors: string[];
|
|
87
|
+
text?: string;
|
|
88
|
+
} | {
|
|
89
|
+
status: "conflict" | "refused" | "failed";
|
|
90
|
+
error: string;
|
|
91
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The shapes the config page exchanges with the server: the schema core generates, the rule
|
|
2
|
+
// registry's summary, the edits the form makes, and the preview the server answers with. Types only,
|
|
3
|
+
// shared by the config editor's modules so each of them reads the same contract.
|
|
4
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initCopyPath(): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { copyText } from "./clipboard.js";
|
|
2
|
+
import { attachMenu } from "./menu.js";
|
|
3
|
+
// The copy buttons beside a file's path — in the header of a document or a linked file, and beside
|
|
4
|
+
// the config file on the config page. Each offers the path relative to the repository, as links and
|
|
5
|
+
// findings name it, and the absolute path on this machine, for a terminal or an editor's "open file".
|
|
6
|
+
// The server writes both onto the button; the absolute one only when the viewer is bound to a
|
|
7
|
+
// loopback address, so it is not handed to other machines that can reach the port.
|
|
8
|
+
const ABSOLUTE_WITHHELD = "Not shown: the viewer is reachable from other machines (--host), so it does not reveal where the repository is on this one.";
|
|
9
|
+
export function initCopyPath() {
|
|
10
|
+
for (const button of document.querySelectorAll("button.copy-path")) {
|
|
11
|
+
const relative = button.dataset.path;
|
|
12
|
+
if (relative === undefined) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
// Rendered hidden by the server: without this script the button could do nothing.
|
|
16
|
+
button.hidden = false;
|
|
17
|
+
const absolute = button.dataset.absolutePath;
|
|
18
|
+
attachMenu(button, () => [
|
|
19
|
+
{
|
|
20
|
+
label: "Copy relative path",
|
|
21
|
+
detail: relative,
|
|
22
|
+
run: () => copyText(relative),
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
label: "Copy absolute path",
|
|
26
|
+
detail: absolute,
|
|
27
|
+
unavailable: absolute === undefined ? ABSOLUTE_WITHHELD : undefined,
|
|
28
|
+
run: () => copyText(absolute ?? ""),
|
|
29
|
+
},
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class DashboardRequests {
|
|
2
|
+
private latest;
|
|
3
|
+
private waiting;
|
|
4
|
+
constructor(initial: string);
|
|
5
|
+
/** A request for `address` is going out now. */
|
|
6
|
+
asked(address: string): void;
|
|
7
|
+
/** A request is waiting out its delay, and will go out with whatever address it then reads. */
|
|
8
|
+
delayed(): void;
|
|
9
|
+
/** The address a refresh should ask for, or `undefined` while a request is still waiting to go. */
|
|
10
|
+
refreshAddress(): string | undefined;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// What the dashboard asks the server for when the corpus changes under it, kept apart from the page
|
|
2
|
+
// so the choice can be tested without a browser.
|
|
3
|
+
//
|
|
4
|
+
// A live refresh must ask again for the reader's latest choice, not for the page's own address: that
|
|
5
|
+
// changes only once an answer lands, so while a newer filter is on its way it still names the one
|
|
6
|
+
// before it, and refreshing it would cancel the newer request and show the old rows under the new
|
|
7
|
+
// input. And a choice still waiting out its typing delay is left to run, since it fetches fresh rows
|
|
8
|
+
// anyway; replacing it with a refresh would drop the reader's newer filter altogether.
|
|
9
|
+
export class DashboardRequests {
|
|
10
|
+
latest;
|
|
11
|
+
waiting = false;
|
|
12
|
+
constructor(initial) {
|
|
13
|
+
this.latest = initial;
|
|
14
|
+
}
|
|
15
|
+
/** A request for `address` is going out now. */
|
|
16
|
+
asked(address) {
|
|
17
|
+
this.latest = address;
|
|
18
|
+
this.waiting = false;
|
|
19
|
+
}
|
|
20
|
+
/** A request is waiting out its delay, and will go out with whatever address it then reads. */
|
|
21
|
+
delayed() {
|
|
22
|
+
this.waiting = true;
|
|
23
|
+
}
|
|
24
|
+
/** The address a refresh should ask for, or `undefined` while a request is still waiting to go. */
|
|
25
|
+
refreshAddress() {
|
|
26
|
+
return this.waiting ? undefined : this.latest;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDashboard(): void;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { DashboardRequests } from "./dashboard-requests.js";
|
|
2
|
+
import { onServerEvent } from "./live-reload.js";
|
|
3
|
+
import { initPromptButtons } from "./prompts.js";
|
|
4
|
+
// Filters and sorts the dashboard as the reader types, without reloading. It does not filter on its
|
|
5
|
+
// own: on every change it asks the server for the same page the form would open and swaps in that
|
|
6
|
+
// page's results, so the selection and its rows are computed and laid out in one place, and the
|
|
7
|
+
// address it leaves behind opens exactly what is on screen — with or without this script.
|
|
8
|
+
// Long enough to let a number be typed without a request per digit, short enough to feel immediate.
|
|
9
|
+
const DELAY_MS = 150;
|
|
10
|
+
export function initDashboard() {
|
|
11
|
+
const form = document.querySelector("form.dashboard-filters");
|
|
12
|
+
const results = document.querySelector(".dashboard-results");
|
|
13
|
+
const summary = document.querySelector(".dashboard-summary");
|
|
14
|
+
if (form === null || results === null || summary === null) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
initPromptButtons(results);
|
|
18
|
+
// Every change applies itself, so the button the form needs without script would do nothing.
|
|
19
|
+
const apply = form.querySelector('button[type="submit"]');
|
|
20
|
+
if (apply !== null) {
|
|
21
|
+
apply.hidden = true;
|
|
22
|
+
}
|
|
23
|
+
let timer;
|
|
24
|
+
// Only the newest request may change the page: an older answer arriving late would show filters
|
|
25
|
+
// the reader has already moved past.
|
|
26
|
+
let controller;
|
|
27
|
+
const requests = new DashboardRequests(location.pathname + location.search);
|
|
28
|
+
async function show(address) {
|
|
29
|
+
requests.asked(address);
|
|
30
|
+
controller?.abort();
|
|
31
|
+
const current = new AbortController();
|
|
32
|
+
controller = current;
|
|
33
|
+
let html;
|
|
34
|
+
try {
|
|
35
|
+
html = await (await fetch(address, { signal: current.signal })).text();
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Superseded by a newer request, or the server is restarting; live reload handles the latter.
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (swapResults(html, form, results, summary)) {
|
|
42
|
+
history.replaceState(null, "", address);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const formAddress = () => dashboardAddress(form);
|
|
46
|
+
function schedule(address) {
|
|
47
|
+
window.clearTimeout(timer);
|
|
48
|
+
requests.delayed();
|
|
49
|
+
timer = window.setTimeout(() => {
|
|
50
|
+
void show(address());
|
|
51
|
+
}, DELAY_MS);
|
|
52
|
+
}
|
|
53
|
+
function showNow(address) {
|
|
54
|
+
window.clearTimeout(timer);
|
|
55
|
+
void show(address);
|
|
56
|
+
}
|
|
57
|
+
form.addEventListener("input", () => {
|
|
58
|
+
schedule(formAddress);
|
|
59
|
+
});
|
|
60
|
+
form.addEventListener("change", () => {
|
|
61
|
+
schedule(formAddress);
|
|
62
|
+
});
|
|
63
|
+
form.addEventListener("submit", (event) => {
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
showNow(formAddress());
|
|
66
|
+
});
|
|
67
|
+
results.addEventListener("click", (event) => {
|
|
68
|
+
const link = event.target.closest("a.dashboard-sort");
|
|
69
|
+
if (link === null || event.button !== 0 || event.ctrlKey || event.metaKey) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
showNow(link.getAttribute("href"));
|
|
74
|
+
});
|
|
75
|
+
// Findings arrive after a save, and an edit changes sizes; either way the rows on screen are stale.
|
|
76
|
+
// A change that adds or removes documents reloads the whole page instead, through live reload.
|
|
77
|
+
const refresh = () => {
|
|
78
|
+
const address = requests.refreshAddress();
|
|
79
|
+
if (address !== undefined) {
|
|
80
|
+
schedule(() => address);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
onServerEvent("diagnostics", refresh);
|
|
84
|
+
onServerEvent("change", refresh);
|
|
85
|
+
}
|
|
86
|
+
// Puts a dashboard page's results, summary and sort fields in place of the ones on screen; `false`
|
|
87
|
+
// when the answer is not a dashboard page.
|
|
88
|
+
function swapResults(html, form, results, summary) {
|
|
89
|
+
const next = new DOMParser().parseFromString(html, "text/html");
|
|
90
|
+
const nextResults = next.querySelector(".dashboard-results");
|
|
91
|
+
const nextSummary = next.querySelector(".dashboard-summary");
|
|
92
|
+
if (nextResults === null || nextSummary === null) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
results.replaceChildren(...nextResults.childNodes);
|
|
96
|
+
initPromptButtons(results);
|
|
97
|
+
summary.textContent = nextSummary.textContent;
|
|
98
|
+
// The sort travels in hidden fields, and the server is what decides them, as it does for a header
|
|
99
|
+
// link it renders; taking them from its answer keeps the next filter change on that sort.
|
|
100
|
+
for (const field of form.querySelectorAll('input[type="hidden"]')) {
|
|
101
|
+
field.remove();
|
|
102
|
+
}
|
|
103
|
+
for (const field of next.querySelectorAll('form.dashboard-filters input[type="hidden"]')) {
|
|
104
|
+
form.append(document.importNode(field, true));
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
// The address the form itself would open when submitted, so a filtered dashboard can be bookmarked.
|
|
109
|
+
function dashboardAddress(form) {
|
|
110
|
+
const params = new URLSearchParams();
|
|
111
|
+
for (const [key, value] of new FormData(form)) {
|
|
112
|
+
// An empty field is "not set"; leaving it out keeps the address as short as the server's own.
|
|
113
|
+
if (typeof value === "string" && value !== "") {
|
|
114
|
+
params.append(key, value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const search = params.toString();
|
|
118
|
+
return search === "" ? "/dashboard" : `/dashboard?${search}`;
|
|
119
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDiagnostics(documentPath: string | undefined): void;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { onServerEvent } from "./live-reload.js";
|
|
2
|
+
import { initPromptButtons } from "./prompts.js";
|
|
3
|
+
import { drawnByServer, findingsBadge, markElement } from "./tree-marks.js";
|
|
4
|
+
// Remembered for this address, like the theme and the column widths.
|
|
5
|
+
const TREE_FILTER_KEY = "wastech-mdlint-viewer:tree-findings-only";
|
|
6
|
+
export function initDiagnostics(documentPath) {
|
|
7
|
+
initTreeFilter();
|
|
8
|
+
// The panel's buttons as the server drew them.
|
|
9
|
+
const drawn = document.querySelector(".diagnostics");
|
|
10
|
+
if (drawn !== null) {
|
|
11
|
+
initPromptButtons(drawn);
|
|
12
|
+
}
|
|
13
|
+
const refresh = () => {
|
|
14
|
+
void refreshTree();
|
|
15
|
+
if (documentPath !== undefined) {
|
|
16
|
+
void refreshPanel(documentPath);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
if (drawnByServer("findings")) {
|
|
20
|
+
treeFilterReady = true;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
refresh();
|
|
24
|
+
}
|
|
25
|
+
onServerEvent("diagnostics", refresh);
|
|
26
|
+
}
|
|
27
|
+
async function fetchJson(url) {
|
|
28
|
+
try {
|
|
29
|
+
return (await (await fetch(url)).json());
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// The server is restarting; the next `diagnostics` event or page load tries again.
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Put the server's panel for the document in place of the one shown, keeping it folded if the
|
|
38
|
+
* reader folded it. Nothing changes while the server's first lint is still running (`204`), and the
|
|
39
|
+
* panel goes when the document has no findings left (an empty answer).
|
|
40
|
+
*/
|
|
41
|
+
async function refreshPanel(documentPath) {
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await fetch(`/api/diagnostics/panel?path=${encodeURIComponent(documentPath)}`);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (response.status !== 200) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const html = await response.text();
|
|
53
|
+
const next = new DOMParser()
|
|
54
|
+
.parseFromString(html, "text/html")
|
|
55
|
+
.querySelector("details.diagnostics");
|
|
56
|
+
const shown = document.querySelector(".diagnostics");
|
|
57
|
+
if (next === null) {
|
|
58
|
+
shown?.remove();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const placed = document.importNode(next, true);
|
|
62
|
+
if (shown === null) {
|
|
63
|
+
document.querySelector(".markdown-body")?.prepend(placed);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
placed.open = shown.open;
|
|
67
|
+
shown.replaceWith(placed);
|
|
68
|
+
}
|
|
69
|
+
initPromptButtons(placed);
|
|
70
|
+
}
|
|
71
|
+
async function refreshTree() {
|
|
72
|
+
const data = await fetchJson("/api/diagnostics");
|
|
73
|
+
if (data?.status !== "ready") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
for (const badge of document.querySelectorAll(".tree-badge")) {
|
|
77
|
+
badge.remove();
|
|
78
|
+
}
|
|
79
|
+
for (const item of document.querySelectorAll(".sidebar li[data-findings]")) {
|
|
80
|
+
item.removeAttribute("data-findings");
|
|
81
|
+
}
|
|
82
|
+
for (const link of document.querySelectorAll('.sidebar a[href^="/doc/"]')) {
|
|
83
|
+
const relPath = decodeURIComponent(link.getAttribute("href").slice("/doc/".length));
|
|
84
|
+
const counts = Object.hasOwn(data.files, relPath)
|
|
85
|
+
? data.files[relPath]
|
|
86
|
+
: undefined;
|
|
87
|
+
if (counts === undefined) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
link.append(markElement(findingsBadge(counts)));
|
|
91
|
+
// The file's row and every folder above it, so the filter below can keep the way to it open.
|
|
92
|
+
for (let item = link.closest("li"); item !== null; item = item.parentElement?.closest(".sidebar li") ?? null) {
|
|
93
|
+
item.dataset.findings = "";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
treeFilterReady = true;
|
|
97
|
+
applyTreeFilter();
|
|
98
|
+
}
|
|
99
|
+
// Set once the first findings are in: before that "no findings" only means "not checked yet", and the
|
|
100
|
+
// filter would hide the whole tree.
|
|
101
|
+
let treeFilterReady = false;
|
|
102
|
+
function treeFilterInput() {
|
|
103
|
+
return document.querySelector(".tree-filter-findings input");
|
|
104
|
+
}
|
|
105
|
+
function initTreeFilter() {
|
|
106
|
+
const label = document.querySelector(".tree-filter-findings");
|
|
107
|
+
const input = treeFilterInput();
|
|
108
|
+
if (label === null || input === null) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
// Rendered hidden by the server: without this script the switch could do nothing.
|
|
112
|
+
label.hidden = false;
|
|
113
|
+
input.checked = readStored() === "1";
|
|
114
|
+
input.addEventListener("change", () => {
|
|
115
|
+
writeStored(input.checked ? "1" : "0");
|
|
116
|
+
applyTreeFilter();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Show only the files with findings, and open the folders on the way to them. A folder this opened is
|
|
121
|
+
* marked, so turning the filter off closes it again and leaves the tree as the reader had it.
|
|
122
|
+
*/
|
|
123
|
+
function applyTreeFilter() {
|
|
124
|
+
const sidebar = document.querySelector(".sidebar");
|
|
125
|
+
const input = treeFilterInput();
|
|
126
|
+
if (sidebar === null || input === null) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const active = input.checked && treeFilterReady;
|
|
130
|
+
sidebar.classList.toggle("only-findings", active);
|
|
131
|
+
for (const details of sidebar.querySelectorAll("details")) {
|
|
132
|
+
const leadsToFindings = details.parentElement?.hasAttribute("data-findings") === true;
|
|
133
|
+
if (active && leadsToFindings && !details.open) {
|
|
134
|
+
details.open = true;
|
|
135
|
+
details.dataset.openedByFilter = "";
|
|
136
|
+
}
|
|
137
|
+
else if (!active && details.dataset.openedByFilter !== undefined) {
|
|
138
|
+
details.open = false;
|
|
139
|
+
delete details.dataset.openedByFilter;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const empty = sidebar.querySelector(".tree-filter-findings-empty");
|
|
143
|
+
if (empty !== null) {
|
|
144
|
+
empty.hidden =
|
|
145
|
+
!active || sidebar.querySelector("li[data-findings]") !== null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Storage can be unavailable (private windows, blocked site data); the filter then starts off.
|
|
149
|
+
function readStored() {
|
|
150
|
+
try {
|
|
151
|
+
return localStorage.getItem(TREE_FILTER_KEY);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function writeStored(value) {
|
|
158
|
+
try {
|
|
159
|
+
localStorage.setItem(TREE_FILTER_KEY, value);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// The choice holds for this page only.
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDiagrams(): void;
|