diffdesk 0.1.0__py3-none-any.whl
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.
- diffdesk/__init__.py +3 -0
- diffdesk/agent.py +414 -0
- diffdesk/agent_guide.md +112 -0
- diffdesk/cli.py +329 -0
- diffdesk/config.py +130 -0
- diffdesk/gitio.py +391 -0
- diffdesk/index.py +635 -0
- diffdesk/mcp.py +472 -0
- diffdesk/server.py +750 -0
- diffdesk/static/app.js +402 -0
- diffdesk/static/index.html +129 -0
- diffdesk/static/js/api.js +58 -0
- diffdesk/static/js/boot.js +12 -0
- diffdesk/static/js/diffview.js +359 -0
- diffdesk/static/js/dom.js +82 -0
- diffdesk/static/js/editor-cm.js +225 -0
- diffdesk/static/js/editor-monaco.js +116 -0
- diffdesk/static/js/flow.js +227 -0
- diffdesk/static/js/hunks.js +40 -0
- diffdesk/static/js/markdown.js +102 -0
- diffdesk/static/js/settings.js +228 -0
- diffdesk/static/js/subject.js +127 -0
- diffdesk/static/js/tree.js +492 -0
- diffdesk/static/js/treemap.js +217 -0
- diffdesk/static/style.css +508 -0
- diffdesk/static/vendor/LICENSES.txt +603 -0
- diffdesk/static/vendor/codemirror.js +24 -0
- diffdesk/static/vendor/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Main-Bold.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Main-Italic.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Main-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Math-Italic.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Script-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- diffdesk/static/vendor/fonts/OFL.txt +102 -0
- diffdesk/static/vendor/katex.css +2 -0
- diffdesk/static/vendor/katex.js +258 -0
- diffdesk/static/vendor/marked.js +72 -0
- diffdesk/subject.schema.json +59 -0
- diffdesk-0.1.0.dist-info/METADATA +520 -0
- diffdesk-0.1.0.dist-info/RECORD +59 -0
- diffdesk-0.1.0.dist-info/WHEEL +4 -0
- diffdesk-0.1.0.dist-info/entry_points.txt +2 -0
- diffdesk-0.1.0.dist-info/licenses/LICENSE +21 -0
- diffdesk-0.1.0.dist-info/licenses/src/diffdesk/static/vendor/LICENSES.txt +603 -0
- diffdesk-0.1.0.dist-info/licenses/src/diffdesk/static/vendor/fonts/OFL.txt +102 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// File tabs, the diff editor (CodeMirror or Monaco), hunk mode and edit state of one subject.
|
|
2
|
+
import { api } from './api.js';
|
|
3
|
+
import { $, h, frameOnce } from './dom.js';
|
|
4
|
+
import * as settings from './settings.js';
|
|
5
|
+
import { hunkExcerpt } from './hunks.js';
|
|
6
|
+
|
|
7
|
+
const EDITORS = {
|
|
8
|
+
codemirror: () => import('./editor-cm.js'),
|
|
9
|
+
monaco: () => import('./editor-monaco.js'),
|
|
10
|
+
};
|
|
11
|
+
const LANGUAGE_BY_EXT = {
|
|
12
|
+
py: 'python', pyi: 'python', rs: 'rust', md: 'markdown', toml: 'toml', yaml: 'yaml', yml: 'yaml',
|
|
13
|
+
};
|
|
14
|
+
const REBUILD_KEYS = ['editor', 'full_diff_limit'];
|
|
15
|
+
const EDITOR_KEYS = [
|
|
16
|
+
'layout', 'word_wrap', 'ignore_whitespace', 'collapse_unchanged', 'context_lines',
|
|
17
|
+
'theme', 'code_font', 'code_font_size', 'reduced_motion',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
function languageOf(path) {
|
|
21
|
+
const ext = path.split('/').pop().split('.').pop().toLowerCase();
|
|
22
|
+
return LANGUAGE_BY_EXT[ext] || 'plaintext';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const short = (sha) => (sha ? String(sha).slice(0, 10) : 'none');
|
|
26
|
+
const lineCount = (f) => Math.max(Number(f.lines_base) || 0, Number(f.lines_head) || 0);
|
|
27
|
+
|
|
28
|
+
export class DiffView {
|
|
29
|
+
/** @param {{onDirtyChange: () => void, onSave: () => void, onFile: () => void}} hooks */
|
|
30
|
+
constructor(hooks) {
|
|
31
|
+
this.hooks = hooks;
|
|
32
|
+
this.tabs = $('file-tabs');
|
|
33
|
+
this.info = $('file-info');
|
|
34
|
+
this.host = $('editor');
|
|
35
|
+
this.subject = null;
|
|
36
|
+
this.files = [];
|
|
37
|
+
this.index = -1;
|
|
38
|
+
this.editor = null;
|
|
39
|
+
this.editorKind = null;
|
|
40
|
+
this.token = 0;
|
|
41
|
+
this.changed = frameOnce(() => {
|
|
42
|
+
this.renderTabs();
|
|
43
|
+
this.hooks.onDirtyChange();
|
|
44
|
+
});
|
|
45
|
+
this.tabs.addEventListener('click', (e) => {
|
|
46
|
+
const tab = e.target.closest('[data-i]');
|
|
47
|
+
if (tab) this.showFile(Number(tab.dataset.i));
|
|
48
|
+
});
|
|
49
|
+
this.info.addEventListener('click', (e) => {
|
|
50
|
+
const btn = e.target.closest('button[data-action]');
|
|
51
|
+
if (!btn) return;
|
|
52
|
+
if (btn.dataset.action === 'full') this.loadFullFile();
|
|
53
|
+
else if (btn.dataset.action === 'codemirror') settings.set('editor', 'codemirror');
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
editorOptions() {
|
|
58
|
+
return {
|
|
59
|
+
layout: settings.get('layout'),
|
|
60
|
+
wrap: settings.get('word_wrap'),
|
|
61
|
+
ignoreWhitespace: settings.get('ignore_whitespace'),
|
|
62
|
+
collapse: settings.get('collapse_unchanged'),
|
|
63
|
+
context: settings.get('context_lines'),
|
|
64
|
+
theme: settings.resolvedTheme(),
|
|
65
|
+
fontFamily: settings.get('code_font'),
|
|
66
|
+
fontSize: settings.get('code_font_size'),
|
|
67
|
+
reducedMotion: settings.reducedMotion(),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Show `subject`; the caller has already dealt with unsaved edits of the previous one. */
|
|
72
|
+
async open(subject, path) {
|
|
73
|
+
this.token++;
|
|
74
|
+
this.subject = subject;
|
|
75
|
+
this.files = (subject.files || []).map((f) => ({
|
|
76
|
+
...(typeof f === 'string' ? { path: f } : f),
|
|
77
|
+
mode: null, data: null, excerpt: null, context: null, saved: null, clean: null, current: null, forceFull: false,
|
|
78
|
+
}));
|
|
79
|
+
this.index = -1;
|
|
80
|
+
if (!this.files.length) {
|
|
81
|
+
this.clear();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const i = path ? this.files.findIndex((f) => f.path === path) : 0;
|
|
85
|
+
await this.showFile(Math.max(i, 0));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Update per-file flags after a refresh, keeping loaded text and unsaved edits. */
|
|
89
|
+
updateFiles(files) {
|
|
90
|
+
for (const fresh of files || []) {
|
|
91
|
+
if (typeof fresh !== 'object') continue;
|
|
92
|
+
const f = this.files.find((x) => x.path === fresh.path);
|
|
93
|
+
if (f) Object.assign(f, { lines_added: fresh.lines_added, lines_deleted: fresh.lines_deleted, edited: fresh.edited });
|
|
94
|
+
}
|
|
95
|
+
this.renderTabs();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
current() {
|
|
99
|
+
return this.files[this.index] || null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Copy the editor text into the shown file. */
|
|
103
|
+
capture() {
|
|
104
|
+
const f = this.current();
|
|
105
|
+
if (f && f.mode === 'full' && f.data && this.editor) f.current = this.editor.value();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// `clean` is the saved text as the editor returns it. Editors may normalise line endings,
|
|
109
|
+
// so comparing with the raw server text would mark untouched files as changed.
|
|
110
|
+
isDirty(f) {
|
|
111
|
+
return f.mode === 'full' && f.current != null && f.current !== f.clean;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
dirtyFiles() {
|
|
115
|
+
this.capture();
|
|
116
|
+
return this.files.filter((f) => this.isDirty(f));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
wantsHunks(f) {
|
|
120
|
+
if (f.forceFull || this.isDirty(f)) return false;
|
|
121
|
+
return lineCount(f) > settings.get('full_diff_limit');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async showFile(i) {
|
|
125
|
+
const f = this.files[i];
|
|
126
|
+
if (!f) return;
|
|
127
|
+
this.capture();
|
|
128
|
+
this.index = i;
|
|
129
|
+
const token = ++this.token;
|
|
130
|
+
const mode = this.wantsHunks(f) ? 'hunks' : 'full';
|
|
131
|
+
const context = settings.get('context_lines');
|
|
132
|
+
const ignoreWhitespace = settings.get('ignore_whitespace');
|
|
133
|
+
this.renderTabs();
|
|
134
|
+
this.info.textContent = 'Loading the diff...';
|
|
135
|
+
try {
|
|
136
|
+
if (f.mode !== mode || !f.data
|
|
137
|
+
|| (mode === 'hunks' && (f.context !== context || f.ignoreWhitespace !== ignoreWhitespace))) {
|
|
138
|
+
const s = this.subject;
|
|
139
|
+
const data = await api.diff({
|
|
140
|
+
id: s.id, pkg: s.package, path: f.path, mode, context, ignoreWhitespace, base: s.base, head: s.head,
|
|
141
|
+
});
|
|
142
|
+
if (token !== this.token) return;
|
|
143
|
+
f.mode = mode;
|
|
144
|
+
f.data = data;
|
|
145
|
+
if (mode === 'full') {
|
|
146
|
+
f.saved = data.edit ?? data.head ?? '';
|
|
147
|
+
if (f.current == null) {
|
|
148
|
+
f.current = f.saved;
|
|
149
|
+
f.clean = f.saved;
|
|
150
|
+
}
|
|
151
|
+
f.edited = data.edit != null;
|
|
152
|
+
} else {
|
|
153
|
+
f.excerpt = hunkExcerpt(data.hunks || []);
|
|
154
|
+
f.context = context;
|
|
155
|
+
f.ignoreWhitespace = ignoreWhitespace;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (f.data.binary) {
|
|
159
|
+
this.editor?.destroy();
|
|
160
|
+
this.editor = null;
|
|
161
|
+
this.editorKind = null;
|
|
162
|
+
this.host.replaceChildren(h('div', { class: 'editor-note', text: 'This is a binary file. There is no text diff.' }));
|
|
163
|
+
this.info.replaceChildren();
|
|
164
|
+
} else {
|
|
165
|
+
const editor = await this.ensureEditor();
|
|
166
|
+
if (token !== this.token) return;
|
|
167
|
+
this.showInEditor(f);
|
|
168
|
+
this.renderInfo();
|
|
169
|
+
}
|
|
170
|
+
} catch (err) {
|
|
171
|
+
if (token !== this.token) return;
|
|
172
|
+
this.showError(err);
|
|
173
|
+
}
|
|
174
|
+
this.renderTabs();
|
|
175
|
+
this.hooks.onDirtyChange();
|
|
176
|
+
this.hooks.onFile();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Show `f` in the editor; for an unchanged file, take the editor's text as the clean text. */
|
|
180
|
+
showInEditor(f) {
|
|
181
|
+
const clean = f.mode === 'full' && !this.isDirty(f);
|
|
182
|
+
this.editor.show(this.docFor(f), this.editorOptions());
|
|
183
|
+
if (clean) {
|
|
184
|
+
f.current = this.editor.value();
|
|
185
|
+
f.clean = f.current;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
docFor(f) {
|
|
190
|
+
if (f.mode === 'hunks') {
|
|
191
|
+
const x = f.excerpt;
|
|
192
|
+
return {
|
|
193
|
+
original: x.original, modified: x.modified, language: languageOf(f.path),
|
|
194
|
+
readOnly: true, numbersA: x.numbersA, numbersB: x.numbersB,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
const language = f.data.language && f.data.language !== 'plaintext' ? f.data.language : languageOf(f.path);
|
|
198
|
+
return { original: f.data.base ?? '', modified: f.current, language, readOnly: f.data.head == null };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async ensureEditor() {
|
|
202
|
+
const kind = settings.get('editor');
|
|
203
|
+
if (this.editor && this.editorKind === kind) return this.editor;
|
|
204
|
+
this.editor?.destroy();
|
|
205
|
+
this.editor = null;
|
|
206
|
+
this.editorKind = null;
|
|
207
|
+
this.host.replaceChildren(h('div', { class: 'editor-note', text: kind === 'monaco' ? 'Loading Monaco from cdn.jsdelivr.net...' : 'Loading the editor...' }));
|
|
208
|
+
const mod = await EDITORS[kind]();
|
|
209
|
+
await mod.ready?.();
|
|
210
|
+
const editor = new mod.Editor(this.host, {
|
|
211
|
+
onChange: () => this.changed(),
|
|
212
|
+
onSave: () => this.hooks.onSave(),
|
|
213
|
+
});
|
|
214
|
+
if (settings.get('editor') !== kind) {
|
|
215
|
+
editor.destroy();
|
|
216
|
+
return this.ensureEditor();
|
|
217
|
+
}
|
|
218
|
+
this.host.replaceChildren();
|
|
219
|
+
this.editor = editor;
|
|
220
|
+
this.editorKind = kind;
|
|
221
|
+
return editor;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
showError(err) {
|
|
225
|
+
this.editor?.destroy();
|
|
226
|
+
this.editor = null;
|
|
227
|
+
this.editorKind = null;
|
|
228
|
+
const monaco = settings.get('editor') === 'monaco';
|
|
229
|
+
this.host.replaceChildren(h('div', { class: 'editor-note error', text: `Cannot show the diff: ${err.message}` }));
|
|
230
|
+
this.info.replaceChildren(monaco ? h('button', { type: 'button', 'data-action': 'codemirror', text: 'Use CodeMirror' }) : '');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
clear() {
|
|
234
|
+
this.token++;
|
|
235
|
+
this.editor?.destroy();
|
|
236
|
+
this.editor = null;
|
|
237
|
+
this.editorKind = null;
|
|
238
|
+
this.host.replaceChildren();
|
|
239
|
+
this.tabs.replaceChildren();
|
|
240
|
+
this.info.replaceChildren();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** React to changed settings without losing unsaved text. */
|
|
244
|
+
applySettings(keys) {
|
|
245
|
+
const f = this.current();
|
|
246
|
+
if (!f) return;
|
|
247
|
+
if (keys.some((k) => REBUILD_KEYS.includes(k))
|
|
248
|
+
|| (f.mode === 'hunks' && (keys.includes('context_lines') || keys.includes('ignore_whitespace')))) {
|
|
249
|
+
this.showFile(this.index);
|
|
250
|
+
} else if (keys.some((k) => EDITOR_KEYS.includes(k))) {
|
|
251
|
+
this.editor?.update(this.editorOptions());
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
loadFullFile() {
|
|
256
|
+
const f = this.current();
|
|
257
|
+
if (!f) return;
|
|
258
|
+
f.forceFull = true;
|
|
259
|
+
this.showFile(this.index);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Save every changed file. Returns the saved paths. */
|
|
263
|
+
async save() {
|
|
264
|
+
const dirty = this.dirtyFiles();
|
|
265
|
+
const saved = [];
|
|
266
|
+
try {
|
|
267
|
+
for (const f of dirty) {
|
|
268
|
+
const content = f.current;
|
|
269
|
+
await api.edit({ id: this.subject.id, package: this.subject.package, path: f.path, content });
|
|
270
|
+
f.saved = content;
|
|
271
|
+
f.clean = content;
|
|
272
|
+
f.edited = true;
|
|
273
|
+
f.data.edit = content;
|
|
274
|
+
saved.push(f.path);
|
|
275
|
+
}
|
|
276
|
+
} finally {
|
|
277
|
+
this.renderTabs();
|
|
278
|
+
this.renderInfo();
|
|
279
|
+
}
|
|
280
|
+
return saved;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** What Discard edit does for the shown file: 'revert', 'remove' or `null` (nothing). */
|
|
284
|
+
discardAction() {
|
|
285
|
+
const f = this.current();
|
|
286
|
+
if (!f || f.mode !== 'full') return null;
|
|
287
|
+
if (this.isDirty(f)) return 'revert';
|
|
288
|
+
return f.edited ? 'remove' : null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Revert unsaved changes of the shown file, or remove its saved edit. */
|
|
292
|
+
async discard() {
|
|
293
|
+
this.capture();
|
|
294
|
+
const f = this.current();
|
|
295
|
+
const action = this.discardAction();
|
|
296
|
+
if (action === 'remove') {
|
|
297
|
+
await api.edit({ id: this.subject.id, package: this.subject.package, path: f.path, content: null });
|
|
298
|
+
f.edited = false;
|
|
299
|
+
f.data.edit = null;
|
|
300
|
+
f.saved = f.data.head ?? '';
|
|
301
|
+
}
|
|
302
|
+
if (action) {
|
|
303
|
+
f.current = f.saved;
|
|
304
|
+
f.clean = f.saved;
|
|
305
|
+
if (this.editor) this.showInEditor(f);
|
|
306
|
+
this.renderTabs();
|
|
307
|
+
this.renderInfo();
|
|
308
|
+
}
|
|
309
|
+
return action;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Forget unsaved changes in every file (used when leaving the subject). */
|
|
313
|
+
discardAll() {
|
|
314
|
+
for (const f of this.files) {
|
|
315
|
+
if (f.mode === 'full') f.current = f.clean;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
renderTabs() {
|
|
320
|
+
this.tabs.replaceChildren(...this.files.map((f, i) => {
|
|
321
|
+
const active = i === this.index;
|
|
322
|
+
return h('button', {
|
|
323
|
+
type: 'button', role: 'tab', class: `tab${active ? ' active' : ''}`,
|
|
324
|
+
'aria-selected': String(active), title: f.path, dataset: { i: String(i) },
|
|
325
|
+
},
|
|
326
|
+
f.path,
|
|
327
|
+
f.lines_added != null ? h('span', { class: 'add', text: ` +${f.lines_added}` }) : null,
|
|
328
|
+
f.lines_deleted != null ? h('span', { class: 'del', text: ` −${f.lines_deleted}` }) : null,
|
|
329
|
+
f.edited ? h('span', { class: 'mark', text: 'edited' }) : null,
|
|
330
|
+
this.isDirty(f) || (active && f.mode === 'full' && this.editor && f.data && this.editor.value() !== f.clean)
|
|
331
|
+
? h('span', { class: 'mark dirty', text: 'unsaved' }) : null);
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
renderInfo() {
|
|
336
|
+
const f = this.current();
|
|
337
|
+
const s = this.subject;
|
|
338
|
+
if (!f || !s || !f.data) return;
|
|
339
|
+
if (f.mode === 'hunks') {
|
|
340
|
+
const lines = lineCount(f);
|
|
341
|
+
const n = f.data.hunks?.length ?? 0;
|
|
342
|
+
this.info.replaceChildren(
|
|
343
|
+
h('span', { class: 'mode', text: 'Hunk mode' }),
|
|
344
|
+
` ${n} changed region${n === 1 ? '' : 's'}, ${f.context} context line${f.context === 1 ? '' : 's'}. `
|
|
345
|
+
+ `The file has ${lines.toLocaleString()} lines, more than the Full diff limit (${settings.get('full_diff_limit').toLocaleString()}). Read-only. `
|
|
346
|
+
+ (f.edited ? 'Your saved edit is not shown in hunk mode. ' : ''),
|
|
347
|
+
h('button', { type: 'button', 'data-action': 'full', text: 'Load full file' }),
|
|
348
|
+
);
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const right = f.data.head == null
|
|
352
|
+
? 'The file is deleted at '
|
|
353
|
+
: f.edited ? 'Right: your saved edit of ' : 'Right: ';
|
|
354
|
+
this.info.replaceChildren(
|
|
355
|
+
'Left: ', h('code', { text: short(s.base) }), ' (read-only). ', right, h('code', { text: short(s.head) }),
|
|
356
|
+
f.data.head == null ? ' (read-only).' : ' (editable).',
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export const $ = (id) => document.getElementById(id);
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create an element.
|
|
5
|
+
*
|
|
6
|
+
* `attrs` keys: `class`, `text`, `style` (object), `on<event>` (listener), `dataset` (object);
|
|
7
|
+
* other keys become attributes. `null` and `false` values are skipped.
|
|
8
|
+
*/
|
|
9
|
+
export function h(tag, attrs = {}, ...children) {
|
|
10
|
+
const node = document.createElement(tag);
|
|
11
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
12
|
+
if (value == null || value === false) continue;
|
|
13
|
+
if (key === 'class') node.className = value;
|
|
14
|
+
else if (key === 'text') node.textContent = value;
|
|
15
|
+
else if (key === 'style') Object.assign(node.style, value);
|
|
16
|
+
else if (key === 'dataset') Object.assign(node.dataset, value);
|
|
17
|
+
else if (key.startsWith('on')) node.addEventListener(key.slice(2), value);
|
|
18
|
+
else node.setAttribute(key, value === true ? '' : value);
|
|
19
|
+
}
|
|
20
|
+
for (const child of children.flat()) {
|
|
21
|
+
if (child != null && child !== false) node.append(child);
|
|
22
|
+
}
|
|
23
|
+
return node;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function escapeHtml(text) {
|
|
27
|
+
return String(text).replace(/[&<>"']/g, (c) => (
|
|
28
|
+
{ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Plain text with `code` spans, as DOM nodes. No other Markdown. */
|
|
33
|
+
export function inlineCode(text) {
|
|
34
|
+
return String(text ?? '').split(/(`[^`]+`)/).filter(Boolean).map((part) => (
|
|
35
|
+
part.length > 1 && part.startsWith('`') && part.endsWith('`')
|
|
36
|
+
? h('code', { text: part.slice(1, -1) })
|
|
37
|
+
: document.createTextNode(part)
|
|
38
|
+
));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const STATUS_LABEL = {
|
|
42
|
+
pending: 'Pending',
|
|
43
|
+
accepted: 'Accepted',
|
|
44
|
+
rejected: 'Rejected',
|
|
45
|
+
change: 'Change requested',
|
|
46
|
+
};
|
|
47
|
+
export const STATUS_ORDER = ['accepted', 'change', 'rejected', 'pending'];
|
|
48
|
+
|
|
49
|
+
/** Stacked bar of `by_status` counts. */
|
|
50
|
+
export function statusBar(byStatus, total) {
|
|
51
|
+
const bar = h('span', { class: 'bar', 'aria-hidden': 'true' });
|
|
52
|
+
if (!total) return bar;
|
|
53
|
+
for (const status of STATUS_ORDER) {
|
|
54
|
+
const n = byStatus?.[status] || 0;
|
|
55
|
+
if (n) bar.append(h('i', { class: `st-${status}`, style: { flexGrow: String(n) } }));
|
|
56
|
+
}
|
|
57
|
+
return bar;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function statusBadge(status) {
|
|
61
|
+
const s = STATUS_LABEL[status] ? status : 'pending';
|
|
62
|
+
return h('span', { class: `badge st-${s}`, text: STATUS_LABEL[s] });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `reviewed` count from a node that has `total` and `by_status`. */
|
|
66
|
+
export function reviewedOf(node) {
|
|
67
|
+
if (typeof node.reviewed === 'number') return node.reviewed;
|
|
68
|
+
return (node.total || 0) - (node.by_status?.pending || 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Schedule `fn` for the next animation frame; repeated calls in one frame run it once. */
|
|
72
|
+
export function frameOnce(fn) {
|
|
73
|
+
let pending = false;
|
|
74
|
+
return () => {
|
|
75
|
+
if (pending) return;
|
|
76
|
+
pending = true;
|
|
77
|
+
requestAnimationFrame(() => {
|
|
78
|
+
pending = false;
|
|
79
|
+
fn();
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// CodeMirror 6 merge view (bundled). Left side read-only, right side editable.
|
|
2
|
+
import * as CM from '../vendor/codemirror.js';
|
|
3
|
+
|
|
4
|
+
// The merge package's own default `scanLimit` (its `defaultDiffConfig`), kept when the diff is replaced.
|
|
5
|
+
const MERGE_DEFAULT_SCAN_LIMIT = 500;
|
|
6
|
+
|
|
7
|
+
const streamLanguages = new Map();
|
|
8
|
+
|
|
9
|
+
function languageSupport(language) {
|
|
10
|
+
switch (language) {
|
|
11
|
+
case 'python': return CM.python();
|
|
12
|
+
case 'rust': return CM.rust();
|
|
13
|
+
case 'markdown': return new CM.LanguageSupport(CM.markdownLanguage);
|
|
14
|
+
case 'toml':
|
|
15
|
+
case 'yaml':
|
|
16
|
+
if (!streamLanguages.has(language)) {
|
|
17
|
+
streamLanguages.set(language, CM.StreamLanguage.define(language === 'toml' ? CM.toml : CM.yaml));
|
|
18
|
+
}
|
|
19
|
+
return streamLanguages.get(language);
|
|
20
|
+
default: return [];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const t = CM.tags;
|
|
25
|
+
const highlight = CM.HighlightStyle.define([
|
|
26
|
+
{ tag: [t.keyword, t.controlKeyword, t.moduleKeyword, t.operatorKeyword], color: 'var(--syn-keyword)' },
|
|
27
|
+
{ tag: [t.function(t.variableName), t.function(t.propertyName), t.labelName], color: 'var(--syn-function)' },
|
|
28
|
+
{ tag: [t.definition(t.variableName), t.definition(t.propertyName), t.function(t.definition(t.variableName))], color: 'var(--syn-def)' },
|
|
29
|
+
{ tag: [t.typeName, t.className, t.namespace, t.macroName], color: 'var(--syn-type)' },
|
|
30
|
+
{ tag: [t.number, t.bool, t.null, t.atom, t.self], color: 'var(--syn-number)' },
|
|
31
|
+
{ tag: [t.string, t.special(t.string), t.regexp, t.character], color: 'var(--syn-string)' },
|
|
32
|
+
{ tag: [t.comment, t.lineComment, t.blockComment, t.docComment], color: 'var(--syn-comment)', fontStyle: 'italic' },
|
|
33
|
+
{ tag: [t.meta, t.annotation, t.processingInstruction], color: 'var(--syn-meta)' },
|
|
34
|
+
{ tag: t.propertyName, color: 'var(--syn-property)' },
|
|
35
|
+
{ tag: t.heading, color: 'var(--syn-keyword)', fontWeight: 'bold' },
|
|
36
|
+
{ tag: t.emphasis, fontStyle: 'italic' },
|
|
37
|
+
{ tag: t.strong, fontWeight: 'bold' },
|
|
38
|
+
{ tag: [t.link, t.url], color: 'var(--syn-string)', textDecoration: 'underline' },
|
|
39
|
+
{ tag: t.invalid, color: 'var(--bad)' },
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
function editorTheme(dark) {
|
|
43
|
+
return CM.EditorView.theme({
|
|
44
|
+
'&': { backgroundColor: 'var(--bg)', color: 'var(--fg)' },
|
|
45
|
+
'.cm-scroller': { fontFamily: 'var(--code-font)', fontSize: 'var(--code-font-size)', lineHeight: '1.5' },
|
|
46
|
+
'.cm-gutters': { backgroundColor: 'var(--bg-side)', color: 'var(--fg-dim)', borderRight: '1px solid var(--border)' },
|
|
47
|
+
'.cm-activeLine, .cm-activeLineGutter': { backgroundColor: 'var(--active-line)' },
|
|
48
|
+
'&.cm-focused .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection': { backgroundColor: 'var(--selection)' },
|
|
49
|
+
'.cm-cursor, .cm-dropCursor': { borderLeftColor: 'var(--fg)' },
|
|
50
|
+
'.cm-panels': { backgroundColor: 'var(--bg-side)', color: 'var(--fg)' },
|
|
51
|
+
'.cm-panels input, .cm-panels button': { font: 'inherit', color: 'inherit' },
|
|
52
|
+
'.cm-searchMatch': { backgroundColor: 'var(--search-match)', outline: '1px solid var(--border)' },
|
|
53
|
+
'.cm-selectionMatch': { backgroundColor: 'var(--selection-match)' },
|
|
54
|
+
'&.cm-merge-a .cm-changedLine, .cm-deletedChunk': { backgroundColor: 'var(--diff-del-line)' },
|
|
55
|
+
'&.cm-merge-b .cm-changedLine, .cm-inlineChangedLine': { backgroundColor: 'var(--diff-ins-line)' },
|
|
56
|
+
'&.cm-merge-a .cm-changedText, .cm-deletedChunk .cm-deletedText': { background: 'var(--diff-del-text)' },
|
|
57
|
+
'&.cm-merge-b .cm-changedText': { background: 'var(--diff-ins-text)' },
|
|
58
|
+
'&.cm-merge-a .cm-changedLineGutter, .cm-deletedLineGutter': { background: 'var(--bad)' },
|
|
59
|
+
'&.cm-merge-b .cm-changedLineGutter': { background: 'var(--ok)' },
|
|
60
|
+
'.cm-collapsedLines': { color: 'var(--fg-dim)', background: 'var(--bg-side)' },
|
|
61
|
+
}, { dark });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Remove spaces and tabs at the start and end of every line (and a trailing CR).
|
|
66
|
+
* Returns the text and `map`, where `map[i]` is the index in `text` of result character `i`
|
|
67
|
+
* and `map[result.length]` is `text.length`.
|
|
68
|
+
*/
|
|
69
|
+
function trimLines(text) {
|
|
70
|
+
const parts = [];
|
|
71
|
+
const map = new Int32Array(text.length + 1);
|
|
72
|
+
let k = 0;
|
|
73
|
+
let i = 0;
|
|
74
|
+
while (i <= text.length) {
|
|
75
|
+
let end = text.indexOf('\n', i);
|
|
76
|
+
if (end < 0) end = text.length;
|
|
77
|
+
let s = i;
|
|
78
|
+
let e = end;
|
|
79
|
+
while (s < e && (text[s] === ' ' || text[s] === '\t')) s++;
|
|
80
|
+
while (e > s && (text[e - 1] === ' ' || text[e - 1] === '\t' || text[e - 1] === '\r')) e--;
|
|
81
|
+
parts.push(text.slice(s, e));
|
|
82
|
+
for (let j = s; j < e; j++) map[k++] = j;
|
|
83
|
+
if (end < text.length) {
|
|
84
|
+
parts.push('\n');
|
|
85
|
+
map[k++] = end;
|
|
86
|
+
}
|
|
87
|
+
i = end + 1;
|
|
88
|
+
}
|
|
89
|
+
map[k] = text.length;
|
|
90
|
+
return [parts.join(''), map];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Line diff that ignores whitespace at the start and end of lines, in positions of the input. */
|
|
94
|
+
function trimmedDiff(a, b) {
|
|
95
|
+
const [ta, ma] = trimLines(a);
|
|
96
|
+
const [tb, mb] = trimLines(b);
|
|
97
|
+
return CM.diff(ta, tb, { scanLimit: MERGE_DEFAULT_SCAN_LIMIT }).map((c) => new CM.Change(
|
|
98
|
+
ma[c.fromA], c.toA > c.fromA ? ma[c.toA - 1] + 1 : ma[c.fromA],
|
|
99
|
+
mb[c.fromB], c.toB > c.fromB ? mb[c.toB - 1] + 1 : mb[c.fromB],
|
|
100
|
+
));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const REBUILD = ['layout', 'ignoreWhitespace', 'collapse', 'context'];
|
|
104
|
+
|
|
105
|
+
/** Keep CRLF when every line break of `text` is CRLF, so the text round-trips unchanged. */
|
|
106
|
+
function lineEnding(text) {
|
|
107
|
+
const crlfOnly = text.includes('\r\n') && !/(^|[^\r])\n/.test(text) && !/\r(?!\n)/.test(text);
|
|
108
|
+
return crlfOnly ? CM.EditorState.lineSeparator.of('\r\n') : [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class Editor {
|
|
112
|
+
constructor(host, hooks) {
|
|
113
|
+
this.host = host;
|
|
114
|
+
this.hooks = hooks;
|
|
115
|
+
this.doc = null;
|
|
116
|
+
this.options = null;
|
|
117
|
+
this.merge = null;
|
|
118
|
+
this.view = null;
|
|
119
|
+
this.right = null;
|
|
120
|
+
this.wrap = new CM.Compartment();
|
|
121
|
+
this.theme = new CM.Compartment();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
show(doc, options) {
|
|
125
|
+
this.destroyViews();
|
|
126
|
+
this.doc = { ...doc };
|
|
127
|
+
this.options = options;
|
|
128
|
+
this.build();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
build() {
|
|
132
|
+
const { original, modified, language, readOnly, numbersA, numbersB } = this.doc;
|
|
133
|
+
const o = this.options;
|
|
134
|
+
const lang = languageSupport(language);
|
|
135
|
+
const dark = o.theme !== 'light';
|
|
136
|
+
const common = (numbers) => [
|
|
137
|
+
CM.lineNumbers(numbers ? { formatNumber: (n) => numbers[n - 1] ?? '' } : {}),
|
|
138
|
+
CM.highlightSpecialChars(),
|
|
139
|
+
CM.drawSelection(),
|
|
140
|
+
CM.highlightActiveLine(),
|
|
141
|
+
CM.highlightActiveLineGutter(),
|
|
142
|
+
CM.syntaxHighlighting(highlight),
|
|
143
|
+
CM.bracketMatching(),
|
|
144
|
+
CM.highlightSelectionMatches(),
|
|
145
|
+
CM.search({ top: true }),
|
|
146
|
+
this.wrap.of(o.wrap ? CM.EditorView.lineWrapping : []),
|
|
147
|
+
this.theme.of(editorTheme(dark)),
|
|
148
|
+
lang,
|
|
149
|
+
];
|
|
150
|
+
const viewOnly = [CM.EditorState.readOnly.of(true), CM.keymap.of([...CM.searchKeymap, ...CM.defaultKeymap])];
|
|
151
|
+
const editable = [
|
|
152
|
+
CM.history(),
|
|
153
|
+
CM.indentOnInput(),
|
|
154
|
+
CM.Prec.high(CM.keymap.of([{ key: 'Mod-s', preventDefault: true, run: () => { this.hooks.onSave(); return true; } }])),
|
|
155
|
+
CM.keymap.of([...CM.searchKeymap, ...CM.defaultKeymap, ...CM.historyKeymap, CM.indentWithTab]),
|
|
156
|
+
CM.EditorView.updateListener.of((u) => { if (u.docChanged) this.hooks.onChange(); }),
|
|
157
|
+
];
|
|
158
|
+
const collapseUnchanged = o.collapse ? { margin: o.context } : undefined;
|
|
159
|
+
const diffConfig = o.ignoreWhitespace ? { override: trimmedDiff } : undefined;
|
|
160
|
+
if (o.layout === 'inline') {
|
|
161
|
+
this.view = new CM.EditorView({
|
|
162
|
+
parent: this.host,
|
|
163
|
+
doc: modified,
|
|
164
|
+
extensions: [
|
|
165
|
+
lineEnding(modified),
|
|
166
|
+
common(numbersB),
|
|
167
|
+
readOnly ? viewOnly : editable,
|
|
168
|
+
CM.unifiedMergeView({ original, mergeControls: false, highlightChanges: true, gutter: true, collapseUnchanged, diffConfig }),
|
|
169
|
+
],
|
|
170
|
+
});
|
|
171
|
+
this.right = this.view;
|
|
172
|
+
} else {
|
|
173
|
+
this.merge = new CM.MergeView({
|
|
174
|
+
parent: this.host,
|
|
175
|
+
a: { doc: original, extensions: [lineEnding(original), common(numbersA), viewOnly] },
|
|
176
|
+
b: { doc: modified, extensions: [lineEnding(modified), common(numbersB), readOnly ? viewOnly : editable] },
|
|
177
|
+
highlightChanges: true,
|
|
178
|
+
gutter: true,
|
|
179
|
+
collapseUnchanged,
|
|
180
|
+
diffConfig,
|
|
181
|
+
});
|
|
182
|
+
this.right = this.merge.b;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
value() {
|
|
187
|
+
// sliceDoc joins lines with the state's line separator; doc.toString always uses \n.
|
|
188
|
+
return this.right ? this.right.state.sliceDoc() : (this.doc?.modified ?? '');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
views() {
|
|
192
|
+
if (this.merge) return [this.merge.a, this.merge.b];
|
|
193
|
+
return this.view ? [this.view] : [];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
update(options) {
|
|
197
|
+
const before = this.options;
|
|
198
|
+
this.options = options;
|
|
199
|
+
if (!this.doc) return;
|
|
200
|
+
if (REBUILD.some((k) => before[k] !== options[k])) {
|
|
201
|
+
this.doc.modified = this.value();
|
|
202
|
+
this.destroyViews();
|
|
203
|
+
this.build();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const effects = [
|
|
207
|
+
this.wrap.reconfigure(options.wrap ? CM.EditorView.lineWrapping : []),
|
|
208
|
+
this.theme.reconfigure(editorTheme(options.theme !== 'light')),
|
|
209
|
+
];
|
|
210
|
+
for (const view of this.views()) view.dispatch({ effects });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
destroyViews() {
|
|
214
|
+
this.merge?.destroy();
|
|
215
|
+
this.view?.destroy();
|
|
216
|
+
this.merge = null;
|
|
217
|
+
this.view = null;
|
|
218
|
+
this.right = null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
destroy() {
|
|
222
|
+
this.destroyViews();
|
|
223
|
+
this.doc = null;
|
|
224
|
+
}
|
|
225
|
+
}
|