@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,677 @@
|
|
|
1
|
+
// The config page's editor. It builds a form from the config schema core generates — the one an
|
|
2
|
+
// editor validates the file against — and turns every change into an edit of the file's text, which
|
|
3
|
+
// the server applies with core's `editConfigText` (so comments survive) and checks with core's
|
|
4
|
+
// `parseConfiguration`. Nothing here decides whether a config is valid: the preview shows core's
|
|
5
|
+
// answer, and a save writes the exact text that was previewed.
|
|
6
|
+
import { copyText } from "./clipboard.js";
|
|
7
|
+
import { button, element, paragraph, section } from "./config-dom.js";
|
|
8
|
+
import { afterSave, clearDraft, draftText, planDrafts, readDraft, readSetAside, rebaseDraft, storeDraft, storeSetAside, } from "./config-draft.js";
|
|
9
|
+
import { FieldRenderer } from "./config-fields.js";
|
|
10
|
+
import { clone, getAt, insertAt, removeAt, replayEdits, samePath, setAt, } from "./config-json.js";
|
|
11
|
+
import { counts, invalidPreview, okPreview } from "./config-preview.js";
|
|
12
|
+
import { rulesList } from "./config-rules.js";
|
|
13
|
+
/** The id of the data the editor is built from, as the server names it. */
|
|
14
|
+
export const CONFIG_DATA_ID = "viewer.config-data";
|
|
15
|
+
// Long enough for a burst of typing to become one preview request.
|
|
16
|
+
const PREVIEW_DELAY_MS = 400;
|
|
17
|
+
export function initConfigEditor() {
|
|
18
|
+
const root = document.querySelector(".config-editor");
|
|
19
|
+
const token = document.querySelector('meta[name="wastech-change-token"]')?.content;
|
|
20
|
+
// The page carries what the editor is built from (`CONFIG_DATA_ID` in the server's
|
|
21
|
+
// `config-editor.ts`, the same data `/api/config` answers), so the form is built as the script
|
|
22
|
+
// runs, before the page is first drawn, rather than once a request comes back — which pushed the
|
|
23
|
+
// file below it down the page a frame or two after it was drawn.
|
|
24
|
+
const data = document.getElementById(CONFIG_DATA_ID)?.textContent ?? "";
|
|
25
|
+
if (root === null || token === undefined || data === "") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const api = JSON.parse(data);
|
|
29
|
+
if (api.file !== null) {
|
|
30
|
+
new ConfigEditor(root, api, api.file, token).start();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
class ConfigEditor {
|
|
34
|
+
root;
|
|
35
|
+
api;
|
|
36
|
+
file;
|
|
37
|
+
token;
|
|
38
|
+
mode;
|
|
39
|
+
// The text the edits apply to: the file's text, until the reader changes the text itself.
|
|
40
|
+
text;
|
|
41
|
+
edits = [];
|
|
42
|
+
// The config as edited so far, for the form to show; `null` while the text does not load.
|
|
43
|
+
model;
|
|
44
|
+
ruleIds;
|
|
45
|
+
preview;
|
|
46
|
+
previewTimer;
|
|
47
|
+
previewController;
|
|
48
|
+
previewRun = Promise.resolve();
|
|
49
|
+
openRule;
|
|
50
|
+
// Set while a restored draft's form edits wait to be folded into its changed text. Only the server
|
|
51
|
+
// can apply edits to text, so until its preview does, the textarea holds the text without them and
|
|
52
|
+
// stays read-only: typing into it starts over from that text and would drop the edits.
|
|
53
|
+
foldingDraft = false;
|
|
54
|
+
// The whole changed file as the last preview built it, while no change has been made since: what
|
|
55
|
+
// the draft keeps so a change with form edits can still be copied once it can no longer be
|
|
56
|
+
// previewed against the file.
|
|
57
|
+
previewedText;
|
|
58
|
+
// Set while a save is on its way: the text sent, and whether the reader has changed anything since.
|
|
59
|
+
saving;
|
|
60
|
+
// A draft made to an earlier version of the file, offered back above the editor.
|
|
61
|
+
setAside;
|
|
62
|
+
notice = element("div", "config-notice");
|
|
63
|
+
formView = element("div", "config-form-view");
|
|
64
|
+
textView = element("div", "config-text-view");
|
|
65
|
+
textarea = document.createElement("textarea");
|
|
66
|
+
modeButtons = {
|
|
67
|
+
form: button("Form", "config-mode segment"),
|
|
68
|
+
text: button("Text", "config-mode segment"),
|
|
69
|
+
};
|
|
70
|
+
status = element("p", "config-status");
|
|
71
|
+
previewBody = element("div", "config-preview-body");
|
|
72
|
+
actions = element("div", "config-actions");
|
|
73
|
+
// Pinned to the bottom of the window while there is a change: the form is long, and Save at the
|
|
74
|
+
// end of it, below the preview, is a button a reader who changed one severity never scrolls to.
|
|
75
|
+
bar = element("div", "config-bar");
|
|
76
|
+
barText = element("p", "config-bar-text");
|
|
77
|
+
// The form's inputs read the model and hand every change back as an edit.
|
|
78
|
+
fields = new FieldRenderer({
|
|
79
|
+
get: (path) => getAt(this.model, path),
|
|
80
|
+
set: (path, value) => {
|
|
81
|
+
this.set(path, value);
|
|
82
|
+
},
|
|
83
|
+
remove: (path) => {
|
|
84
|
+
this.remove(path);
|
|
85
|
+
},
|
|
86
|
+
insert: (path, value) => {
|
|
87
|
+
this.insert(path, value);
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
constructor(root, api,
|
|
91
|
+
// Replaced by the saved version once a save lands, so a change made while it was on its way is
|
|
92
|
+
// previewed, saved and kept as a draft against the file now on disk.
|
|
93
|
+
file, token) {
|
|
94
|
+
this.root = root;
|
|
95
|
+
this.api = api;
|
|
96
|
+
this.file = file;
|
|
97
|
+
this.token = token;
|
|
98
|
+
this.text = file.text;
|
|
99
|
+
this.model = api.config === null ? null : clone(api.config);
|
|
100
|
+
this.ruleIds = [...api.ruleIds];
|
|
101
|
+
// A file that does not load cannot be shown as a form; its text, with core's reason, can.
|
|
102
|
+
this.mode = this.model === null ? "text" : "form";
|
|
103
|
+
}
|
|
104
|
+
start() {
|
|
105
|
+
this.restoreDrafts();
|
|
106
|
+
this.root.hidden = false;
|
|
107
|
+
this.textarea.className = "config-textarea";
|
|
108
|
+
this.textarea.spellcheck = false;
|
|
109
|
+
this.textarea.setAttribute("aria-label", "Config file text");
|
|
110
|
+
this.textarea.readOnly = this.foldingDraft;
|
|
111
|
+
this.textarea.addEventListener("input", () => {
|
|
112
|
+
this.text = this.textarea.value;
|
|
113
|
+
this.edits = [];
|
|
114
|
+
this.model = null;
|
|
115
|
+
this.changed();
|
|
116
|
+
});
|
|
117
|
+
this.textView.append(this.textarea);
|
|
118
|
+
const modes = element("div", "config-modes segmented");
|
|
119
|
+
modes.setAttribute("role", "group");
|
|
120
|
+
modes.setAttribute("aria-label", "Edit as");
|
|
121
|
+
modes.append(this.modeButtons.form, this.modeButtons.text);
|
|
122
|
+
this.modeButtons.form.addEventListener("click", () => void this.switchTo("form"));
|
|
123
|
+
this.modeButtons.text.addEventListener("click", () => void this.switchTo("text"));
|
|
124
|
+
const toolbar = element("div", "config-toolbar");
|
|
125
|
+
toolbar.append(modes, this.status);
|
|
126
|
+
if (this.foldingDraft) {
|
|
127
|
+
this.status.textContent = "Restoring your unsaved change…";
|
|
128
|
+
}
|
|
129
|
+
const previewSection = element("section", "config-preview");
|
|
130
|
+
previewSection.id = "config-preview";
|
|
131
|
+
previewSection.setAttribute("aria-label", "Preview");
|
|
132
|
+
const heading = element("h2");
|
|
133
|
+
heading.textContent = "Preview";
|
|
134
|
+
previewSection.append(heading, this.previewBody);
|
|
135
|
+
this.bar.setAttribute("role", "region");
|
|
136
|
+
this.bar.setAttribute("aria-label", "Unsaved change");
|
|
137
|
+
this.barText.setAttribute("aria-live", "polite");
|
|
138
|
+
this.bar.hidden = true;
|
|
139
|
+
this.bar.append(this.barText, this.actions);
|
|
140
|
+
this.notice.setAttribute("role", "region");
|
|
141
|
+
this.notice.setAttribute("aria-label", "Earlier unsaved change");
|
|
142
|
+
this.renderNotice();
|
|
143
|
+
this.root.replaceChildren(this.notice, toolbar, this.formView, this.textView, this.bar, previewSection);
|
|
144
|
+
this.render();
|
|
145
|
+
if (this.hasChanges()) {
|
|
146
|
+
this.schedulePreview(0);
|
|
147
|
+
}
|
|
148
|
+
else if (this.api.errors.length > 0) {
|
|
149
|
+
this.preview = { status: "invalid", errors: this.api.errors };
|
|
150
|
+
this.renderPreview();
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this.renderPreview();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
hasChanges() {
|
|
157
|
+
return this.text !== this.file.text || this.edits.length > 0;
|
|
158
|
+
}
|
|
159
|
+
render() {
|
|
160
|
+
this.modeButtons.form.setAttribute("aria-pressed", String(this.mode === "form"));
|
|
161
|
+
this.modeButtons.text.setAttribute("aria-pressed", String(this.mode === "text"));
|
|
162
|
+
this.formView.hidden = this.mode !== "form";
|
|
163
|
+
this.textView.hidden = this.mode !== "text";
|
|
164
|
+
if (this.mode === "text") {
|
|
165
|
+
this.textarea.value = this.text;
|
|
166
|
+
this.formView.replaceChildren();
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this.renderForm();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async switchTo(mode) {
|
|
173
|
+
if (mode === this.mode) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (this.hasChanges()) {
|
|
177
|
+
// The other view needs this one's result: the text the edits produce, or the config the text
|
|
178
|
+
// loads as. Both come from the server, so wait for the preview that is on its way.
|
|
179
|
+
window.clearTimeout(this.previewTimer);
|
|
180
|
+
this.previewRun = this.runPreview();
|
|
181
|
+
await this.previewRun;
|
|
182
|
+
}
|
|
183
|
+
const preview = this.preview;
|
|
184
|
+
if (mode === "text") {
|
|
185
|
+
if (this.edits.length > 0) {
|
|
186
|
+
if (preview?.status !== "ok" &&
|
|
187
|
+
!(preview?.status === "invalid" &&
|
|
188
|
+
preview.text !== undefined &&
|
|
189
|
+
preview.text !== "")) {
|
|
190
|
+
this.status.textContent =
|
|
191
|
+
"These changes cannot be turned into text yet; see the preview below.";
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.text = preview.text;
|
|
195
|
+
this.edits = [];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else if (this.model === null) {
|
|
199
|
+
if (preview?.status !== "ok") {
|
|
200
|
+
this.status.textContent =
|
|
201
|
+
"The form can show this config once it loads; fix what the preview names first.";
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
this.model = clone(preview.config);
|
|
205
|
+
this.ruleIds = [...preview.ruleIds];
|
|
206
|
+
}
|
|
207
|
+
this.status.textContent = "";
|
|
208
|
+
this.mode = mode;
|
|
209
|
+
this.render();
|
|
210
|
+
this.saveDraft();
|
|
211
|
+
}
|
|
212
|
+
// --- The form ------------------------------------------------------------------------------
|
|
213
|
+
renderForm() {
|
|
214
|
+
const model = this.model;
|
|
215
|
+
const schema = this.api.schema.properties ?? {};
|
|
216
|
+
const files = section("Files", "Which Markdown files are documents.");
|
|
217
|
+
for (const key of ["include", "exclude", "respectGitignore"]) {
|
|
218
|
+
files.append(this.topField(schema[key] ?? {}, key));
|
|
219
|
+
}
|
|
220
|
+
const rules = section("Rules", "The checks this config runs, in the order it lists them.");
|
|
221
|
+
rules.append(rulesList(this.ruleListHost(), Array.isArray(model.rules) ? model.rules : []));
|
|
222
|
+
const settings = section("Settings", "How root-relative links resolve and which ids link documents.");
|
|
223
|
+
settings.append(this.topField(schema.settings ?? {}, "settings"));
|
|
224
|
+
const compile = section("Compile", "What wastech-mdlint compile writes.");
|
|
225
|
+
compile.append(this.topField(schema.compile ?? {}, "compile"));
|
|
226
|
+
this.formView.replaceChildren(files, rules, settings, compile);
|
|
227
|
+
}
|
|
228
|
+
topField(schema, key) {
|
|
229
|
+
return this.fields.field({
|
|
230
|
+
schema,
|
|
231
|
+
path: [key],
|
|
232
|
+
label: key,
|
|
233
|
+
required: false,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
// The rule cards' view of the editor. The rule ids and the open card live here, because a switch
|
|
237
|
+
// between the views or a restored draft replaces them, and the cards are drawn again after either.
|
|
238
|
+
ruleListHost() {
|
|
239
|
+
return {
|
|
240
|
+
api: this.api,
|
|
241
|
+
fields: this.fields,
|
|
242
|
+
ruleId: (index) => this.ruleIds[index] ?? null,
|
|
243
|
+
configuredIds: () => new Set(this.ruleIds.filter((id) => id !== null)),
|
|
244
|
+
isOpen: (index) => this.openRule === index,
|
|
245
|
+
current: (path) => getAt(this.model, path),
|
|
246
|
+
addRule: (entry, id) => {
|
|
247
|
+
this.addRule(entry, id);
|
|
248
|
+
},
|
|
249
|
+
removeRule: (index) => {
|
|
250
|
+
this.removeRule(index);
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
// A new rule goes at the end of the list, or starts the list, and its card opens.
|
|
255
|
+
addRule(entry, id) {
|
|
256
|
+
const rules = this.model?.rules;
|
|
257
|
+
const length = Array.isArray(rules) ? rules.length : 0;
|
|
258
|
+
if (Array.isArray(rules)) {
|
|
259
|
+
this.insert(["rules", length], entry);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
this.set(["rules"], [entry]);
|
|
263
|
+
}
|
|
264
|
+
this.ruleIds.push(id);
|
|
265
|
+
this.openRule = length;
|
|
266
|
+
this.renderForm();
|
|
267
|
+
}
|
|
268
|
+
removeRule(index) {
|
|
269
|
+
this.remove(["rules", index]);
|
|
270
|
+
this.ruleIds.splice(index, 1);
|
|
271
|
+
this.openRule = undefined;
|
|
272
|
+
this.renderForm();
|
|
273
|
+
}
|
|
274
|
+
// --- Edits ---------------------------------------------------------------------------------
|
|
275
|
+
set(path, value) {
|
|
276
|
+
this.model = setAt(this.model ?? {}, path, clone(value));
|
|
277
|
+
const last = this.edits.at(-1);
|
|
278
|
+
// Typing into one field is one change, not one per keystroke — but never into an edit a save on
|
|
279
|
+
// its way already carries: the change made after it has to stay a change of its own.
|
|
280
|
+
const saved = this.saving?.edits?.length ?? 0;
|
|
281
|
+
if (last?.op === "set" &&
|
|
282
|
+
samePath(last.path, path) &&
|
|
283
|
+
this.edits.length > saved) {
|
|
284
|
+
last.value = clone(value);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
this.edits.push({ op: "set", path: [...path], value: clone(value) });
|
|
288
|
+
}
|
|
289
|
+
this.changed();
|
|
290
|
+
}
|
|
291
|
+
remove(path) {
|
|
292
|
+
if (getAt(this.model, path) === undefined) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
removeAt(this.model, path);
|
|
296
|
+
this.edits.push({ op: "remove", path: [...path] });
|
|
297
|
+
this.changed();
|
|
298
|
+
}
|
|
299
|
+
insert(path, value) {
|
|
300
|
+
insertAt(this.model, path, clone(value));
|
|
301
|
+
this.edits.push({ op: "insert", path: [...path], value: clone(value) });
|
|
302
|
+
this.changed();
|
|
303
|
+
}
|
|
304
|
+
changed() {
|
|
305
|
+
this.previewedText = undefined;
|
|
306
|
+
if (this.saving !== undefined) {
|
|
307
|
+
this.saving.edited = true;
|
|
308
|
+
}
|
|
309
|
+
this.saveDraft();
|
|
310
|
+
this.schedulePreview(PREVIEW_DELAY_MS);
|
|
311
|
+
}
|
|
312
|
+
// --- Preview and save ----------------------------------------------------------------------
|
|
313
|
+
schedulePreview(delay) {
|
|
314
|
+
window.clearTimeout(this.previewTimer);
|
|
315
|
+
// A preview still on its way describes the config before this change. Let through, it would draw
|
|
316
|
+
// that older result — and a Save button carrying its text — until the new one arrives.
|
|
317
|
+
this.previewController?.abort();
|
|
318
|
+
this.previewBody.replaceChildren(paragraph("Checking the change…"));
|
|
319
|
+
this.actions.replaceChildren();
|
|
320
|
+
this.showBar("Checking the change…");
|
|
321
|
+
this.previewTimer = window.setTimeout(() => {
|
|
322
|
+
this.previewRun = this.runPreview();
|
|
323
|
+
}, delay);
|
|
324
|
+
}
|
|
325
|
+
async runPreview() {
|
|
326
|
+
this.previewController?.abort();
|
|
327
|
+
if (!this.hasChanges()) {
|
|
328
|
+
this.preview = undefined;
|
|
329
|
+
this.renderPreview();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const controller = new AbortController();
|
|
333
|
+
this.previewController = controller;
|
|
334
|
+
const body = { base: this.file.hash };
|
|
335
|
+
if (this.text !== this.file.text) {
|
|
336
|
+
body.text = this.text;
|
|
337
|
+
}
|
|
338
|
+
if (this.edits.length > 0) {
|
|
339
|
+
body.edits = this.edits;
|
|
340
|
+
}
|
|
341
|
+
let preview;
|
|
342
|
+
try {
|
|
343
|
+
const response = await this.post("/api/config/preview", body, controller.signal);
|
|
344
|
+
preview = (await response.json());
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
if (error.name === "AbortError") {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
preview = {
|
|
351
|
+
status: "failed",
|
|
352
|
+
error: "The viewer did not answer; it may be restarting.",
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
// A newer change has started its own preview since; this answer is about an older config.
|
|
356
|
+
if (controller !== this.previewController) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
this.preview = preview;
|
|
360
|
+
if (this.foldingDraft) {
|
|
361
|
+
this.foldDraft();
|
|
362
|
+
}
|
|
363
|
+
// Any change since the request aborted it, so this answer is about the change as it stands.
|
|
364
|
+
if ((preview.status === "ok" || preview.status === "invalid") &&
|
|
365
|
+
preview.text !== undefined) {
|
|
366
|
+
this.previewedText = preview.text;
|
|
367
|
+
this.saveDraft();
|
|
368
|
+
}
|
|
369
|
+
this.renderPreview();
|
|
370
|
+
}
|
|
371
|
+
// Once the server has applied a restored draft's form edits to its text, the text it answers with
|
|
372
|
+
// replaces both, so the textarea shows the whole change and can be edited again. Edits it could not
|
|
373
|
+
// apply stay, with the preview saying why.
|
|
374
|
+
foldDraft() {
|
|
375
|
+
const preview = this.preview;
|
|
376
|
+
const text = preview?.status === "ok" || preview?.status === "invalid"
|
|
377
|
+
? preview.text
|
|
378
|
+
: undefined;
|
|
379
|
+
if (text !== undefined) {
|
|
380
|
+
this.text = text;
|
|
381
|
+
this.edits = [];
|
|
382
|
+
if (this.mode === "text") {
|
|
383
|
+
this.textarea.value = text;
|
|
384
|
+
}
|
|
385
|
+
this.saveDraft();
|
|
386
|
+
}
|
|
387
|
+
this.foldingDraft = false;
|
|
388
|
+
this.textarea.readOnly = false;
|
|
389
|
+
this.status.textContent = "";
|
|
390
|
+
}
|
|
391
|
+
post(url, body, signal) {
|
|
392
|
+
return fetch(url, {
|
|
393
|
+
method: "POST",
|
|
394
|
+
headers: {
|
|
395
|
+
"Content-Type": "application/json",
|
|
396
|
+
"X-Wastech-Viewer-Token": this.token,
|
|
397
|
+
},
|
|
398
|
+
body: JSON.stringify(body),
|
|
399
|
+
signal,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
renderPreview() {
|
|
403
|
+
const preview = this.preview;
|
|
404
|
+
this.actions.replaceChildren();
|
|
405
|
+
if (preview === undefined || !this.hasChanges()) {
|
|
406
|
+
this.previewBody.replaceChildren(paragraph("No changes yet. Change a value and this shows what the linter would report with it."));
|
|
407
|
+
this.showBar(undefined);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (preview.status === "invalid") {
|
|
411
|
+
this.previewBody.replaceChildren(...invalidPreview(preview.errors));
|
|
412
|
+
// The draft outlives a reload, so a change that does not load must be one the reader can drop.
|
|
413
|
+
this.actions.append(this.discardButton());
|
|
414
|
+
this.showBar("The linter would not load this config.", true);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (preview.status !== "ok") {
|
|
418
|
+
this.previewBody.replaceChildren(paragraph(preview.error, "config-invalid"));
|
|
419
|
+
this.showBar(preview.error);
|
|
420
|
+
if (preview.status === "conflict") {
|
|
421
|
+
// The file changed on disk under the change. Reloading keeps the draft: the reloaded page
|
|
422
|
+
// finds it made to an earlier version and offers it back, to apply to the new file or copy.
|
|
423
|
+
// The change can be copied from here too, as text, whenever there is one.
|
|
424
|
+
const text = this.currentText();
|
|
425
|
+
if (text !== undefined) {
|
|
426
|
+
this.actions.append(this.copyButton(text), this.downloadButton(text));
|
|
427
|
+
}
|
|
428
|
+
const reload = button("Reload the page");
|
|
429
|
+
reload.addEventListener("click", () => {
|
|
430
|
+
location.reload();
|
|
431
|
+
});
|
|
432
|
+
this.actions.append(reload);
|
|
433
|
+
}
|
|
434
|
+
this.actions.append(this.discardButton());
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
this.previewBody.replaceChildren(...okPreview(preview, this.file.path));
|
|
438
|
+
this.showBar(`With this change: ${counts(preview.findings.after)} — ${preview.addedCount} new, ${preview.removedCount} gone.`, true);
|
|
439
|
+
this.renderActions(preview.text);
|
|
440
|
+
}
|
|
441
|
+
// `undefined` hides the bar: with nothing changed there is nothing to save or drop.
|
|
442
|
+
showBar(text, linkToPreview = false) {
|
|
443
|
+
this.bar.hidden = text === undefined;
|
|
444
|
+
if (text === undefined) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const content = [text];
|
|
448
|
+
if (linkToPreview) {
|
|
449
|
+
const link = document.createElement("a");
|
|
450
|
+
link.href = "#config-preview";
|
|
451
|
+
link.textContent = "See the preview";
|
|
452
|
+
content.push(" ", link);
|
|
453
|
+
}
|
|
454
|
+
this.barText.replaceChildren(...content);
|
|
455
|
+
}
|
|
456
|
+
renderActions(text) {
|
|
457
|
+
this.actions.append(this.copyButton(text), this.downloadButton(text));
|
|
458
|
+
if (this.api.writeBlocked === null) {
|
|
459
|
+
const save = button("Save", "config-primary");
|
|
460
|
+
save.addEventListener("click", () => void this.save(text, save));
|
|
461
|
+
this.actions.append(save);
|
|
462
|
+
}
|
|
463
|
+
this.actions.append(this.discardButton());
|
|
464
|
+
if (this.api.writeBlocked !== null) {
|
|
465
|
+
this.actions.append(paragraph(this.api.writeBlocked, "config-save-off"));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
copyButton(text, label = "Copy") {
|
|
469
|
+
const copy = button(label);
|
|
470
|
+
copy.addEventListener("click", () => {
|
|
471
|
+
void copyText(text).then((copied) => (copy.textContent = copied ? "Copied" : "Copy failed"));
|
|
472
|
+
});
|
|
473
|
+
return copy;
|
|
474
|
+
}
|
|
475
|
+
downloadButton(text, label = "Download") {
|
|
476
|
+
const download = button(label);
|
|
477
|
+
download.addEventListener("click", () => {
|
|
478
|
+
const url = URL.createObjectURL(new Blob([text], { type: "application/json" }));
|
|
479
|
+
const link = document.createElement("a");
|
|
480
|
+
link.href = url;
|
|
481
|
+
link.download =
|
|
482
|
+
this.file.path.split("/").pop() ?? "wastech-mdlint.config.json";
|
|
483
|
+
link.click();
|
|
484
|
+
URL.revokeObjectURL(url);
|
|
485
|
+
});
|
|
486
|
+
return download;
|
|
487
|
+
}
|
|
488
|
+
// Starts over from the file on disk: the draft is dropped and the page loaded again.
|
|
489
|
+
discardButton(label = "Discard changes") {
|
|
490
|
+
const discard = button(label);
|
|
491
|
+
discard.addEventListener("click", () => {
|
|
492
|
+
clearDraft();
|
|
493
|
+
location.reload();
|
|
494
|
+
});
|
|
495
|
+
return discard;
|
|
496
|
+
}
|
|
497
|
+
async save(text, control) {
|
|
498
|
+
control.disabled = true;
|
|
499
|
+
// Kept in the draft too: the reload the server starts once the file is written can come before
|
|
500
|
+
// this request's answer, and the reloaded page has only the draft to tell that it was saved.
|
|
501
|
+
// Only a form change counts its edits: they are what a later change is told apart from.
|
|
502
|
+
this.saving = {
|
|
503
|
+
text,
|
|
504
|
+
edited: false,
|
|
505
|
+
...(this.text === this.file.text
|
|
506
|
+
? { edits: this.edits.map((edit) => clone(edit)) }
|
|
507
|
+
: {}),
|
|
508
|
+
};
|
|
509
|
+
this.saveDraft();
|
|
510
|
+
let result;
|
|
511
|
+
try {
|
|
512
|
+
result = (await (await this.post("/api/config/save", { base: this.file.hash, text })).json());
|
|
513
|
+
}
|
|
514
|
+
catch {
|
|
515
|
+
result = {
|
|
516
|
+
status: "failed",
|
|
517
|
+
error: "The viewer did not answer; nothing is known to be saved.",
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
if (result.status === "saved" && result.hash !== undefined) {
|
|
521
|
+
this.saved({ text, hash: result.hash });
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
this.saving = undefined;
|
|
525
|
+
this.saveDraft();
|
|
526
|
+
control.disabled = false;
|
|
527
|
+
this.preview =
|
|
528
|
+
result.status === "invalid"
|
|
529
|
+
? { status: "invalid", errors: result.errors ?? [] }
|
|
530
|
+
: {
|
|
531
|
+
status: result.status === "conflict" ? "conflict" : "failed",
|
|
532
|
+
error: result.error ?? "The save failed.",
|
|
533
|
+
};
|
|
534
|
+
this.renderPreview();
|
|
535
|
+
}
|
|
536
|
+
// The file on disk is now `saved`. A change the reader made while the save was on its way is not
|
|
537
|
+
// part of it, so it stays — as the draft and in the editor — now made to the saved file; only a
|
|
538
|
+
// draft that is exactly what was saved is dropped.
|
|
539
|
+
saved(saved) {
|
|
540
|
+
const remaining = afterSave(this.currentDraft(), saved);
|
|
541
|
+
this.saving = undefined;
|
|
542
|
+
this.file = { ...this.file, ...saved };
|
|
543
|
+
if (remaining === undefined) {
|
|
544
|
+
this.text = saved.text;
|
|
545
|
+
this.edits = [];
|
|
546
|
+
this.previewedText = undefined;
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
// What the draft keeps is what the editor holds, so the next draft it stores is that one.
|
|
550
|
+
this.text = remaining.text;
|
|
551
|
+
this.edits = remaining.edits;
|
|
552
|
+
}
|
|
553
|
+
this.saveDraft();
|
|
554
|
+
// The server reloads the corpus for the new config and tells every page, this one included,
|
|
555
|
+
// to reload; until then the reader knows the write happened.
|
|
556
|
+
const message = `Saved to ${this.file.path}. The viewer is reloading the corpus with it.`;
|
|
557
|
+
if (remaining === undefined) {
|
|
558
|
+
// A change made and undone while saving left a preview on its way; it has nothing to add.
|
|
559
|
+
window.clearTimeout(this.previewTimer);
|
|
560
|
+
this.previewController?.abort();
|
|
561
|
+
this.previewBody.replaceChildren(paragraph(message, "config-saved"));
|
|
562
|
+
this.actions.replaceChildren();
|
|
563
|
+
this.showBar(message);
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
this.status.textContent = `${message} The change you made since is kept.`;
|
|
567
|
+
this.schedulePreview(0);
|
|
568
|
+
}
|
|
569
|
+
// --- Draft ---------------------------------------------------------------------------------
|
|
570
|
+
currentDraft() {
|
|
571
|
+
return {
|
|
572
|
+
base: this.file.hash,
|
|
573
|
+
text: this.text,
|
|
574
|
+
edits: this.edits,
|
|
575
|
+
mode: this.mode,
|
|
576
|
+
textEdited: this.text !== this.file.text,
|
|
577
|
+
full: this.edits.length === 0 ? undefined : this.previewedText,
|
|
578
|
+
saving: this.saving,
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
// The changed file as text, when it can be had without the server.
|
|
582
|
+
currentText() {
|
|
583
|
+
return draftText(this.currentDraft());
|
|
584
|
+
}
|
|
585
|
+
saveDraft() {
|
|
586
|
+
storeDraft(this.hasChanges() ? this.currentDraft() : undefined);
|
|
587
|
+
}
|
|
588
|
+
// Restores the draft made to this version of the file, and sets aside one made to another.
|
|
589
|
+
restoreDrafts() {
|
|
590
|
+
const plan = planDrafts(readDraft(), readSetAside(), this.file);
|
|
591
|
+
storeDraft(plan.restore);
|
|
592
|
+
storeSetAside(plan.setAside);
|
|
593
|
+
this.setAside = plan.setAside;
|
|
594
|
+
if (plan.restore !== undefined) {
|
|
595
|
+
this.restoreDraft(plan.restore);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
restoreDraft(draft) {
|
|
599
|
+
this.previewedText = draft.full;
|
|
600
|
+
this.text = draft.text;
|
|
601
|
+
this.edits = draft.edits;
|
|
602
|
+
if (this.text !== this.file.text) {
|
|
603
|
+
// The form's model would have to be rebuilt from text, which only the server can parse. Edits
|
|
604
|
+
// the form made on top of that text are kept and folded into it by the first preview.
|
|
605
|
+
this.model = null;
|
|
606
|
+
this.mode = "text";
|
|
607
|
+
this.foldingDraft = this.edits.length > 0;
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
this.model = replayEdits(this.model, this.edits);
|
|
611
|
+
this.ruleIds = (Array.isArray(this.model?.rules) ? this.model.rules : []).map((entry) => this.resolveRuleId(entry));
|
|
612
|
+
// Edits are restored into the form, whichever view they were made in: turning them into text is
|
|
613
|
+
// the server's job, and the form can switch to text once the preview has done it.
|
|
614
|
+
this.mode = this.model === null ? "text" : "form";
|
|
615
|
+
}
|
|
616
|
+
// A draft made to an earlier version of the file is never applied or dropped without the reader:
|
|
617
|
+
// the notice says the file changed under it and offers it back until they apply or discard it.
|
|
618
|
+
renderNotice() {
|
|
619
|
+
const draft = this.setAside;
|
|
620
|
+
this.notice.hidden = draft === undefined;
|
|
621
|
+
if (draft === undefined) {
|
|
622
|
+
this.notice.replaceChildren();
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
const text = draftText(draft);
|
|
626
|
+
const actions = element("div", "config-actions");
|
|
627
|
+
const apply = button("Apply it to the current file", "config-primary");
|
|
628
|
+
apply.addEventListener("click", () => {
|
|
629
|
+
this.applySetAside(draft);
|
|
630
|
+
});
|
|
631
|
+
actions.append(apply);
|
|
632
|
+
if (text !== undefined) {
|
|
633
|
+
actions.append(this.copyButton(text), this.downloadButton(text));
|
|
634
|
+
}
|
|
635
|
+
const discard = button("Discard it");
|
|
636
|
+
discard.addEventListener("click", () => {
|
|
637
|
+
storeSetAside(undefined);
|
|
638
|
+
this.setAside = undefined;
|
|
639
|
+
this.renderNotice();
|
|
640
|
+
});
|
|
641
|
+
actions.append(discard);
|
|
642
|
+
const then = text === undefined
|
|
643
|
+
? "Apply it to see it against the current file; it can be copied from there."
|
|
644
|
+
: "Apply it to see it against the current file, or copy it as it was.";
|
|
645
|
+
this.notice.replaceChildren(paragraph(`You have an unsaved change made to an earlier version of ${this.file.path}, which has changed on disk since, so it was not restored. ${then}`, "config-notice-text"), actions);
|
|
646
|
+
}
|
|
647
|
+
// Becomes the current draft, made to apply to the file now on disk, and the page is loaded again to
|
|
648
|
+
// restore it as any draft is restored. It would replace a change the reader has made here since,
|
|
649
|
+
// so that is asked first.
|
|
650
|
+
applySetAside(draft) {
|
|
651
|
+
if (this.hasChanges() &&
|
|
652
|
+
!window.confirm("Replace the change you have made on this page with the earlier one?")) {
|
|
653
|
+
return;
|
|
654
|
+
}
|
|
655
|
+
// Without storage the reload would restore nothing, and the earlier change would be gone with
|
|
656
|
+
// the set-aside copy; it stays offered, and can still be copied.
|
|
657
|
+
if (!storeDraft(rebaseDraft(draft, this.file))) {
|
|
658
|
+
this.status.textContent =
|
|
659
|
+
"The browser would not keep the change across a reload, so it cannot be applied here.";
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
storeSetAside(undefined);
|
|
663
|
+
location.reload();
|
|
664
|
+
}
|
|
665
|
+
// An entry as written may name a rule by its alias or in another case; the form shows it by id.
|
|
666
|
+
resolveRuleId(entry) {
|
|
667
|
+
const name = typeof entry === "object" && entry !== null && !Array.isArray(entry)
|
|
668
|
+
? entry.rule
|
|
669
|
+
: undefined;
|
|
670
|
+
if (typeof name !== "string" || name === "custom") {
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
const rule = this.api.rules.find((candidate) => candidate.id.toUpperCase() === name.toUpperCase() ||
|
|
674
|
+
candidate.alias === name.toLowerCase());
|
|
675
|
+
return rule?.id ?? null;
|
|
676
|
+
}
|
|
677
|
+
}
|