@rvagg/marginalia 0.1.1 → 0.1.2
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/dist/anchor.test.js +81 -0
- package/dist/anchor.test.js.map +1 -0
- package/package.json +1 -1
- package/ui/anchor.d.ts +12 -0
- package/ui/anchor.js +80 -0
- package/ui/app.js +123 -72
- package/ui/diff2html-theme.css +2 -0
- package/ui/index.html +1 -1
- package/ui/style.css +2 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import { createAnchor, locateAnchor } from '../ui/anchor.js';
|
|
4
|
+
// One side of a file's diff: each hunk is a start line and its texts.
|
|
5
|
+
function side(...hunks) {
|
|
6
|
+
const lines = new Map();
|
|
7
|
+
for (const [start, texts] of hunks) {
|
|
8
|
+
texts.forEach((text, i) => lines.set(start + i, text));
|
|
9
|
+
}
|
|
10
|
+
return lines;
|
|
11
|
+
}
|
|
12
|
+
const FILE = ['a', 'b', 'c', 'target', 'd', 'e', 'f'];
|
|
13
|
+
test('createAnchor captures up to three neighbours each side, nearest first', () => {
|
|
14
|
+
assert.deepEqual(createAnchor(side([1, FILE]), 4), {
|
|
15
|
+
line: 4, text: 'target', before: ['c', 'b', 'a'], after: ['d', 'e', 'f']
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
test('createAnchor stops context at a gap between hunks', () => {
|
|
19
|
+
const anchor = createAnchor(side([1, ['a', 'b']], [10, ['target', 'x']]), 10);
|
|
20
|
+
assert.deepEqual(anchor?.before, []);
|
|
21
|
+
assert.deepEqual(anchor?.after, ['x']);
|
|
22
|
+
});
|
|
23
|
+
test('createAnchor returns null for a line the diff does not show', () => {
|
|
24
|
+
assert.equal(createAnchor(side([1, FILE]), 20), null);
|
|
25
|
+
});
|
|
26
|
+
test('locateAnchor follows lines added or removed above', () => {
|
|
27
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
28
|
+
assert.deepEqual(locateAnchor(anchor, side([1, ['new1', 'new2', ...FILE]])), { line: 6, offset: 2, fuzz: 0 });
|
|
29
|
+
assert.deepEqual(locateAnchor(anchor, side([1, FILE.slice(1)])), { line: 3, offset: -1, fuzz: 0 });
|
|
30
|
+
});
|
|
31
|
+
test('locateAnchor uses context to choose between duplicate lines', () => {
|
|
32
|
+
const original = side([1, ['if (x) {', 'one()', '}', 'two()', '}', 'end']]);
|
|
33
|
+
const anchor = createAnchor(original, 5);
|
|
34
|
+
// Two lines added above move the old `}` at line 3 to line 5.
|
|
35
|
+
const shifted = side([1, ['x', 'y', 'if (x) {', 'one()', '}', 'two()', '}', 'end']]);
|
|
36
|
+
assert.deepEqual(locateAnchor(anchor, shifted), { line: 7, offset: 2, fuzz: 0 });
|
|
37
|
+
});
|
|
38
|
+
test('locateAnchor applies fuzz when outer context changed', () => {
|
|
39
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
40
|
+
const outerChanged = side([1, ['A', 'b', 'c', 'target', 'd', 'e', 'F']]);
|
|
41
|
+
assert.deepEqual(locateAnchor(anchor, outerChanged), { line: 4, offset: 0, fuzz: 1 });
|
|
42
|
+
const twoChanged = side([1, ['A', 'B', 'c', 'target', 'd', 'E', 'F']]);
|
|
43
|
+
assert.deepEqual(locateAnchor(anchor, twoChanged), { line: 4, offset: 0, fuzz: 2 });
|
|
44
|
+
});
|
|
45
|
+
test('locateAnchor keeps a line when a line is inserted beside it', () => {
|
|
46
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
47
|
+
const insertedAbove = side([1, ['a', 'b', 'c', 'new', 'target', 'd', 'e', 'f']]);
|
|
48
|
+
assert.deepEqual(locateAnchor(anchor, insertedAbove), { line: 5, offset: 1, fuzz: 3 });
|
|
49
|
+
});
|
|
50
|
+
test('locateAnchor keeps a line when a line is inserted below it', () => {
|
|
51
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
52
|
+
const insertedBelow = side([1, ['a', 'b', 'c', 'target', 'new', 'd', 'e', 'f']]);
|
|
53
|
+
assert.deepEqual(locateAnchor(anchor, insertedBelow), { line: 4, offset: 0, fuzz: 3 });
|
|
54
|
+
});
|
|
55
|
+
test('locateAnchor keeps a line at a hunk edge when its one neighbour changes', () => {
|
|
56
|
+
const anchor = createAnchor(side([10, ['target', 'd', 'e', 'f']]), 10);
|
|
57
|
+
assert.deepEqual(locateAnchor(anchor, side([10, ['target', 'D', 'e', 'f']])), { line: 10, offset: 0, fuzz: 3 });
|
|
58
|
+
// Away from the hunk edge, a changed neighbour on each side detaches it.
|
|
59
|
+
assert.equal(locateAnchor(anchor, side([8, ['x', 'y', 'target', 'D', 'e', 'f']])), null);
|
|
60
|
+
});
|
|
61
|
+
test('locateAnchor fails when the line or both nearest neighbours changed', () => {
|
|
62
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
63
|
+
assert.equal(locateAnchor(anchor, side([1, ['a', 'b', 'c', 'changed', 'd', 'e', 'f']])), null);
|
|
64
|
+
assert.equal(locateAnchor(anchor, side([1, ['a', 'b', 'C', 'target', 'D', 'e', 'f']])), null);
|
|
65
|
+
});
|
|
66
|
+
test('locateAnchor accepts a match at a hunk edge with unseen context', () => {
|
|
67
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
68
|
+
// The hunk now starts or ends at the target; lines beyond it are outside the diff.
|
|
69
|
+
assert.deepEqual(locateAnchor(anchor, side([4, FILE.slice(3)])), { line: 4, offset: 0, fuzz: 0 });
|
|
70
|
+
assert.deepEqual(locateAnchor(anchor, side([1, FILE.slice(0, 4)])), { line: 4, offset: 0, fuzz: 0 });
|
|
71
|
+
});
|
|
72
|
+
test('locateAnchor breaks a distance tie toward the earlier line', () => {
|
|
73
|
+
const anchor = createAnchor(side([5, ['target']]), 5);
|
|
74
|
+
assert.deepEqual(locateAnchor(anchor, side([3, ['target']], [7, ['target']])), { line: 3, offset: -2, fuzz: 0 });
|
|
75
|
+
});
|
|
76
|
+
test('locateAnchor prefers less fuzz over a nearer match', () => {
|
|
77
|
+
const anchor = createAnchor(side([1, FILE]), 4);
|
|
78
|
+
const lines = side([1, ['A', 'b', 'c', 'target', 'd', 'e', 'f']], [20, FILE]);
|
|
79
|
+
assert.deepEqual(locateAnchor(anchor, lines), { line: 23, offset: 19, fuzz: 0 });
|
|
80
|
+
});
|
|
81
|
+
//# sourceMappingURL=anchor.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anchor.test.js","sourceRoot":"","sources":["../src/anchor.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAA;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE5D,sEAAsE;AACtE,SAAS,IAAI,CAAC,GAAG,KAAgC;IAC/C,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA;IACvC,KAAK,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,KAAK,EAAE,CAAC;QACnC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,CAAA;IACxD,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,IAAI,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;AAErD,IAAI,CAAC,uEAAuE,EAAE,GAAG,EAAE;IACjF,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;QACjD,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,GAAG,EA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CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;AAClH,CAAC,CAAC,CAAA;AAEF,IAAI,CAAC,oDAAoD,EAAE,GAAG,EAAE;IAC9D,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC,CAAE,CAAA;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC,CAAA;IAC7E,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAA;AAClF,CAAC,CAAC,CAAA"}
|
package/package.json
CHANGED
package/ui/anchor.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface Anchor {
|
|
2
|
+
line: number
|
|
3
|
+
text: string
|
|
4
|
+
/** Up to three contiguous lines above, nearest first. */
|
|
5
|
+
before: string[]
|
|
6
|
+
/** Up to three contiguous lines below, nearest first. */
|
|
7
|
+
after: string[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function createAnchor(lines: Map<number, string>, line: number): Anchor | null
|
|
11
|
+
|
|
12
|
+
export function locateAnchor(anchor: Anchor, lines: Map<number, string>): { line: number, offset: number, fuzz: number } | null
|
package/ui/anchor.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Comment anchors that follow their line across diff re-renders. A comment
|
|
2
|
+
// is placed the way `patch` places a hunk: the line plus its context.
|
|
3
|
+
|
|
4
|
+
const CONTEXT = 3
|
|
5
|
+
const MAX_FUZZ = 2
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Capture the anchor for `line` on one side of a file's diff.
|
|
9
|
+
* @param {Map<number, string>} lines line number to text for one side of
|
|
10
|
+
* one file; a missing number marks a gap between hunks
|
|
11
|
+
* @param {number} line
|
|
12
|
+
* @returns {Anchor | null} null when `line` isn't shown
|
|
13
|
+
*/
|
|
14
|
+
export function createAnchor(lines, line) {
|
|
15
|
+
const text = lines.get(line)
|
|
16
|
+
if (text === undefined) return null
|
|
17
|
+
return { line, text, before: neighbours(lines, line, -1), after: neighbours(lines, line, 1) }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Find an anchor's line in a re-rendered diff. As in `git apply`, the line
|
|
22
|
+
* and its context must match exactly, and the match nearest the old line
|
|
23
|
+
* wins. Failing that, as in GNU patch, fuzz 1 then 2 ignore that many
|
|
24
|
+
* outermost context lines on each side, always keeping the nearest one.
|
|
25
|
+
* Context the diff doesn't show isn't compared: a hunk shows only part of
|
|
26
|
+
* a file, so a line at a hunk's edge has fewer visible neighbours.
|
|
27
|
+
* Last, beyond patch, fuzz 3 accepts the line beside either of its nearest
|
|
28
|
+
* neighbours, so a line inserted directly above or below doesn't detach it.
|
|
29
|
+
* A side with no context at a hunk's edge counts as a neighbour match when
|
|
30
|
+
* the candidate is at a hunk's edge too.
|
|
31
|
+
* An exact match can win over the true line at fuzz 3 when unseen context
|
|
32
|
+
* lets a duplicate at a distant hunk's edge pass; this needs the line and
|
|
33
|
+
* its visible context duplicated, which is rare enough to accept.
|
|
34
|
+
* @param {Anchor} anchor
|
|
35
|
+
* @param {Map<number, string>} lines as for createAnchor
|
|
36
|
+
* @returns {{ line: number, offset: number, fuzz: number } | null} null when
|
|
37
|
+
* no placement matches, so the line was changed or left the diff
|
|
38
|
+
*/
|
|
39
|
+
export function locateAnchor(anchor, lines) {
|
|
40
|
+
const candidates = [...lines.keys()]
|
|
41
|
+
.filter((n) => lines.get(n) === anchor.text)
|
|
42
|
+
.sort((a, b) => Math.abs(a - anchor.line) - Math.abs(b - anchor.line) || a - b)
|
|
43
|
+
|
|
44
|
+
for (let fuzz = 0; fuzz <= MAX_FUZZ; fuzz++) {
|
|
45
|
+
const before = trim(anchor.before, fuzz)
|
|
46
|
+
const after = trim(anchor.after, fuzz)
|
|
47
|
+
const line = candidates.find((n) => matches(lines, n, before, -1) && matches(lines, n, after, 1))
|
|
48
|
+
if (line !== undefined) return { line, offset: line - anchor.line, fuzz }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const line = candidates.find((n) =>
|
|
52
|
+
nearestMatches(lines, n - 1, anchor.before) || nearestMatches(lines, n + 1, anchor.after))
|
|
53
|
+
if (line !== undefined) return { line, offset: line - anchor.line, fuzz: MAX_FUZZ + 1 }
|
|
54
|
+
return null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function neighbours(lines, line, step) {
|
|
58
|
+
const found = []
|
|
59
|
+
for (let n = line + step; found.length < CONTEXT && lines.has(n); n += step) {
|
|
60
|
+
found.push(lines.get(n))
|
|
61
|
+
}
|
|
62
|
+
return found
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function nearestMatches(lines, line, context) {
|
|
66
|
+
return context.length > 0 ? lines.get(line) === context[0] : !lines.has(line)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function trim(context, fuzz) {
|
|
70
|
+
return context.slice(0, Math.max(Math.min(1, context.length), context.length - fuzz))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function matches(lines, line, context, step) {
|
|
74
|
+
for (let i = 0; i < context.length; i++) {
|
|
75
|
+
const text = lines.get(line + step * (i + 1))
|
|
76
|
+
if (text === undefined) return true
|
|
77
|
+
if (text !== context[i]) return false
|
|
78
|
+
}
|
|
79
|
+
return true
|
|
80
|
+
}
|
package/ui/app.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createAnchor, locateAnchor } from './anchor.js'
|
|
2
|
+
|
|
1
3
|
(function () {
|
|
2
4
|
'use strict'
|
|
3
5
|
|
|
@@ -233,7 +235,8 @@
|
|
|
233
235
|
break
|
|
234
236
|
case 'comment_ack':
|
|
235
237
|
if (msg.file) {
|
|
236
|
-
|
|
238
|
+
const target = msg.anchor ? { side: msg.side, anchor: msg.anchor } : null
|
|
239
|
+
confirmInlineComment(msg.thread_id, msg.file, target, msg.text)
|
|
237
240
|
} else {
|
|
238
241
|
confirmGeneralComment(msg.thread_id, msg.text)
|
|
239
242
|
}
|
|
@@ -263,6 +266,8 @@
|
|
|
263
266
|
loadingState.classList.add('hidden')
|
|
264
267
|
|
|
265
268
|
if (!diffText || diffText.trim() === '') {
|
|
269
|
+
heldDrafts = saveOpenInputs().map((draft) => ({ ...draft, focused: false }))
|
|
270
|
+
diffIndex = new Map()
|
|
266
271
|
diffContainer.innerHTML = ''
|
|
267
272
|
diffContainer.appendChild(emptyState)
|
|
268
273
|
emptyState.classList.remove('hidden')
|
|
@@ -305,6 +310,7 @@
|
|
|
305
310
|
|
|
306
311
|
diff2htmlUi.draw()
|
|
307
312
|
diff2htmlUi.highlightCode()
|
|
313
|
+
indexDiff()
|
|
308
314
|
|
|
309
315
|
updateFilenav(diffText)
|
|
310
316
|
injectViewedToggles()
|
|
@@ -314,6 +320,35 @@
|
|
|
314
320
|
diffContainer.scrollTop = scrollTop
|
|
315
321
|
}
|
|
316
322
|
|
|
323
|
+
// file -> { wrapper, left, right }; each side holds line -> text and line -> row.
|
|
324
|
+
let diffIndex = new Map()
|
|
325
|
+
|
|
326
|
+
function indexDiff() {
|
|
327
|
+
diffIndex = new Map()
|
|
328
|
+
for (const wrapper of diffContainer.querySelectorAll('.d2h-file-wrapper')) {
|
|
329
|
+
const file = wrapper.querySelector('.d2h-file-name')?.textContent.trim()
|
|
330
|
+
if (!file) continue
|
|
331
|
+
const entry = {
|
|
332
|
+
wrapper,
|
|
333
|
+
left: { texts: new Map(), rows: new Map() },
|
|
334
|
+
right: { texts: new Map(), rows: new Map() }
|
|
335
|
+
}
|
|
336
|
+
for (const row of wrapper.querySelectorAll('tr')) {
|
|
337
|
+
const numEl = row.querySelector('.d2h-code-linenumber')
|
|
338
|
+
if (!numEl) continue
|
|
339
|
+
const text = row.querySelector('.d2h-code-line-ctn')?.textContent ?? ''
|
|
340
|
+
const oldLine = parseInt(numEl.querySelector('.line-num1')?.textContent ?? '', 10)
|
|
341
|
+
const newLine = parseInt(numEl.querySelector('.line-num2')?.textContent ?? '', 10)
|
|
342
|
+
for (const [side, line] of [['left', oldLine], ['right', newLine]]) {
|
|
343
|
+
if (isNaN(line)) continue
|
|
344
|
+
entry[side].texts.set(line, text)
|
|
345
|
+
entry[side].rows.set(line, row)
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
diffIndex.set(file, entry)
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
317
352
|
// --- File header controls ---
|
|
318
353
|
|
|
319
354
|
function injectViewedToggles() {
|
|
@@ -385,48 +420,48 @@
|
|
|
385
420
|
|
|
386
421
|
// --- Comment input persistence across re-renders ---
|
|
387
422
|
|
|
423
|
+
// Comment input rows -> { file, target, textarea }; target is null on a file comment.
|
|
424
|
+
const commentInputs = new WeakMap()
|
|
425
|
+
// Drafts whose file left the diff, restored when it returns.
|
|
426
|
+
let heldDrafts = []
|
|
427
|
+
|
|
388
428
|
function saveOpenInputs() {
|
|
389
|
-
const inputs =
|
|
429
|
+
const inputs = heldDrafts
|
|
430
|
+
heldDrafts = []
|
|
390
431
|
diffContainer.querySelectorAll('.comment-input-row').forEach((row) => {
|
|
391
|
-
const
|
|
392
|
-
if (!
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
432
|
+
const input = commentInputs.get(row)
|
|
433
|
+
if (!input) return
|
|
434
|
+
const { textarea } = input
|
|
435
|
+
inputs.push({
|
|
436
|
+
file: input.file,
|
|
437
|
+
target: input.target,
|
|
438
|
+
text: textarea.value,
|
|
439
|
+
focused: document.activeElement === textarea,
|
|
440
|
+
selection: [textarea.selectionStart, textarea.selectionEnd]
|
|
441
|
+
})
|
|
400
442
|
})
|
|
401
443
|
return inputs
|
|
402
444
|
}
|
|
403
445
|
|
|
404
446
|
function restoreOpenInputs(inputs) {
|
|
405
|
-
for (const
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
diffBody.insertBefore(inputRow.el, firstRow)
|
|
417
|
-
inputRow.el.querySelector('textarea').value = text
|
|
418
|
-
break
|
|
419
|
-
}
|
|
420
|
-
} else {
|
|
421
|
-
// Line-level: find the row and re-open
|
|
422
|
-
const row = findDiffRow(file, line, 'right') || findDiffRow(file, line, 'left')
|
|
423
|
-
if (!row) continue
|
|
424
|
-
const inputRow = createCommentInputRow(file, line)
|
|
425
|
-
row.parentNode.insertBefore(inputRow.el, row.nextSibling)
|
|
447
|
+
for (const draft of inputs) {
|
|
448
|
+
const { file, target, text, focused, selection } = draft
|
|
449
|
+
const row = target && placeTarget(file, target)
|
|
450
|
+
const top = row ? null : fileTopRow(file)
|
|
451
|
+
if (!row && !top) {
|
|
452
|
+
heldDrafts.push({ ...draft, focused: false })
|
|
453
|
+
continue
|
|
454
|
+
}
|
|
455
|
+
const inputRow = createCommentInputRow(file, target)
|
|
456
|
+
if (row) {
|
|
457
|
+
row.after(inputRow.el)
|
|
426
458
|
row.classList.add('mg-commenting')
|
|
427
459
|
inputRow.onCleanup = () => row.classList.remove('mg-commenting')
|
|
428
|
-
|
|
460
|
+
} else {
|
|
461
|
+
top.before(inputRow.el)
|
|
462
|
+
if (target) inputRow.markChanged()
|
|
429
463
|
}
|
|
464
|
+
inputRow.restore(text, focused, selection)
|
|
430
465
|
}
|
|
431
466
|
}
|
|
432
467
|
|
|
@@ -441,23 +476,26 @@
|
|
|
441
476
|
function openCommentInput(afterRow, file, line, side) {
|
|
442
477
|
const existingInput = afterRow.nextElementSibling
|
|
443
478
|
if (existingInput?.classList.contains('comment-input-row')) return
|
|
479
|
+
const lines = diffIndex.get(file)?.[side]
|
|
480
|
+
const anchor = lines && createAnchor(lines.texts, line)
|
|
481
|
+
if (!anchor) return
|
|
444
482
|
|
|
445
|
-
const inputRow = createCommentInputRow(file,
|
|
483
|
+
const inputRow = createCommentInputRow(file, { side, anchor })
|
|
446
484
|
afterRow.parentNode.insertBefore(inputRow.el, afterRow.nextSibling)
|
|
447
485
|
afterRow.classList.add('mg-commenting')
|
|
448
486
|
inputRow.onCleanup = () => afterRow.classList.remove('mg-commenting')
|
|
449
487
|
inputRow.focus()
|
|
450
488
|
}
|
|
451
489
|
|
|
452
|
-
function createCommentInputRow(file,
|
|
453
|
-
const isFileLevel =
|
|
490
|
+
function createCommentInputRow(file, target) {
|
|
491
|
+
const isFileLevel = !target
|
|
454
492
|
const tr = document.createElement('tr')
|
|
455
493
|
tr.className = 'comment-input-row'
|
|
456
494
|
const cell = document.createElement('td')
|
|
457
495
|
cell.colSpan = 3
|
|
458
496
|
cell.innerHTML = `
|
|
459
497
|
<div class="comment-input-box">
|
|
460
|
-
<div class="comment-input-context">${escapeHtml(file)}${isFileLevel ? '' : ':' + line}</div>
|
|
498
|
+
<div class="comment-input-context">${escapeHtml(file)}${isFileLevel ? '' : ':' + target.anchor.line}</div>
|
|
461
499
|
<textarea class="mg-textarea" rows="3" placeholder="${isFileLevel ? 'Comment on this file...' : 'Leave a comment...'}"></textarea>
|
|
462
500
|
<div class="comment-input-actions">
|
|
463
501
|
<button class="small" data-action="submit">Comment</button>
|
|
@@ -468,7 +506,11 @@
|
|
|
468
506
|
tr.appendChild(cell)
|
|
469
507
|
|
|
470
508
|
const textarea = cell.querySelector('textarea')
|
|
509
|
+
commentInputs.set(tr, { file, target, textarea })
|
|
471
510
|
let onCleanup = null
|
|
511
|
+
// Set when the commented line changed or left the diff; its line number
|
|
512
|
+
// no longer names that code, so the comment goes to the file instead.
|
|
513
|
+
let changed = false
|
|
472
514
|
|
|
473
515
|
function cleanup() {
|
|
474
516
|
tr.remove()
|
|
@@ -478,7 +520,14 @@
|
|
|
478
520
|
function submit() {
|
|
479
521
|
const text = textarea.value.trim()
|
|
480
522
|
if (!text) return
|
|
481
|
-
|
|
523
|
+
if (isFileLevel) {
|
|
524
|
+
send({ type: 'comment', file, line: 0, side: 'right', text })
|
|
525
|
+
} else if (changed) {
|
|
526
|
+
const quoted = `On former line ${target.anchor.line}:\n\n\`\`\`\n${target.anchor.text}\n\`\`\`\n\n${text}`
|
|
527
|
+
send({ type: 'comment', file, line: 0, side: 'right', text: quoted })
|
|
528
|
+
} else {
|
|
529
|
+
send({ type: 'comment', file, line: target.anchor.line, side: target.side, text, anchor: target.anchor })
|
|
530
|
+
}
|
|
482
531
|
cleanup()
|
|
483
532
|
}
|
|
484
533
|
|
|
@@ -493,6 +542,19 @@
|
|
|
493
542
|
return {
|
|
494
543
|
el: tr,
|
|
495
544
|
focus: () => textarea.focus(),
|
|
545
|
+
markChanged() {
|
|
546
|
+
changed = true
|
|
547
|
+
const note = document.createElement('span')
|
|
548
|
+
note.className = 'comment-outdated'
|
|
549
|
+
note.textContent = 'line changed or left the diff; sends as a file comment'
|
|
550
|
+
cell.querySelector('.comment-input-context').append(' ', note)
|
|
551
|
+
},
|
|
552
|
+
restore(text, focused, [start, end]) {
|
|
553
|
+
textarea.value = text
|
|
554
|
+
if (!focused) return
|
|
555
|
+
textarea.focus()
|
|
556
|
+
textarea.setSelectionRange(start, end)
|
|
557
|
+
},
|
|
496
558
|
set onCleanup(fn) { onCleanup = fn }
|
|
497
559
|
}
|
|
498
560
|
}
|
|
@@ -544,13 +606,13 @@
|
|
|
544
606
|
|
|
545
607
|
// --- Comment threads ---
|
|
546
608
|
|
|
547
|
-
function confirmInlineComment(threadId, file,
|
|
548
|
-
threads[threadId] = { file,
|
|
609
|
+
function confirmInlineComment(threadId, file, target, text) {
|
|
610
|
+
threads[threadId] = { file, target, messages: [{ from: 'user', text }], el: null }
|
|
549
611
|
injectThread(threadId)
|
|
550
612
|
}
|
|
551
613
|
|
|
552
614
|
function confirmGeneralComment(threadId, text) {
|
|
553
|
-
threads[threadId] = { file: null,
|
|
615
|
+
threads[threadId] = { file: null, target: null, messages: [{ from: 'user', text }], el: null }
|
|
554
616
|
addChatMessage('you', text)
|
|
555
617
|
}
|
|
556
618
|
|
|
@@ -573,22 +635,12 @@
|
|
|
573
635
|
const thread = threads[threadId]
|
|
574
636
|
if (!thread || !thread.file) return
|
|
575
637
|
|
|
576
|
-
//
|
|
577
|
-
|
|
578
|
-
let
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
// File-level comment: insert before the first row in the file's tbody
|
|
583
|
-
const wrappers = diffContainer.querySelectorAll('.d2h-file-wrapper')
|
|
584
|
-
for (const wrapper of wrappers) {
|
|
585
|
-
const nameEl = wrapper.querySelector('.d2h-file-name')
|
|
586
|
-
if (nameEl?.textContent?.trim() !== thread.file) continue
|
|
587
|
-
anchor = wrapper.querySelector('.d2h-diff-tbody tr')
|
|
588
|
-
insertBefore = true
|
|
589
|
-
break
|
|
590
|
-
}
|
|
591
|
-
}
|
|
638
|
+
// A line comment follows its line; a file comment, or one whose line
|
|
639
|
+
// changed, sits at the top of the file's diff.
|
|
640
|
+
let anchor = thread.target && placeTarget(thread.file, thread.target)
|
|
641
|
+
thread.outdated = Boolean(thread.target) && !anchor
|
|
642
|
+
const insertBefore = !anchor
|
|
643
|
+
if (!anchor) anchor = fileTopRow(thread.file)
|
|
592
644
|
if (!anchor) return
|
|
593
645
|
|
|
594
646
|
if (!thread.el) {
|
|
@@ -640,6 +692,7 @@
|
|
|
640
692
|
|
|
641
693
|
cell.innerHTML = '<div class="comment-thread">' +
|
|
642
694
|
'<button class="comment-dismiss" title="Dismiss">×</button>' +
|
|
695
|
+
(thread.outdated ? `<div class="comment-outdated">Outdated: line ${thread.target.anchor.line} changed or left the diff</div>` : '') +
|
|
643
696
|
thread.messages.map(renderMessageContent).join('') +
|
|
644
697
|
'<div class="thread-reply-box">' +
|
|
645
698
|
'<textarea rows="2" class="mg-textarea thread-reply-input" placeholder="Reply..."></textarea>' +
|
|
@@ -676,21 +729,19 @@
|
|
|
676
729
|
}
|
|
677
730
|
}
|
|
678
731
|
|
|
679
|
-
function
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
return null
|
|
732
|
+
function fileTopRow(file) {
|
|
733
|
+
return diffIndex.get(file)?.wrapper.querySelector('.d2h-diff-tbody tr') ?? null
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Find a line target in the current diff and refresh its anchor there.
|
|
737
|
+
// Returns the row, or null when the line changed or left the diff.
|
|
738
|
+
function placeTarget(file, target) {
|
|
739
|
+
const lines = diffIndex.get(file)?.[target.side]
|
|
740
|
+
if (!lines) return null
|
|
741
|
+
const found = locateAnchor(target.anchor, lines.texts)
|
|
742
|
+
if (!found) return null
|
|
743
|
+
target.anchor = createAnchor(lines.texts, found.line)
|
|
744
|
+
return lines.rows.get(found.line)
|
|
694
745
|
}
|
|
695
746
|
|
|
696
747
|
// --- Permission relay ---
|
package/ui/diff2html-theme.css
CHANGED
|
@@ -43,7 +43,9 @@
|
|
|
43
43
|
|
|
44
44
|
/* --- Line numbers: static table-cell instead of absolute --- */
|
|
45
45
|
|
|
46
|
+
/* diff2html's rtl gutter would put the new number first: GitHub puts old first. */
|
|
46
47
|
.d2h-code-linenumber {
|
|
48
|
+
direction: ltr !important;
|
|
47
49
|
position: static !important;
|
|
48
50
|
display: table-cell !important;
|
|
49
51
|
width: 1% !important;
|
package/ui/index.html
CHANGED
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
<script src="https://cdn.jsdelivr.net/npm/diff2html@3/bundles/js/diff2html.min.js"></script>
|
|
58
58
|
<script src="https://cdn.jsdelivr.net/npm/diff2html@3/bundles/js/diff2html-ui.min.js"></script>
|
|
59
59
|
<script src="https://cdn.jsdelivr.net/npm/marked@17/lib/marked.umd.js"></script>
|
|
60
|
-
<script src="/app.js"></script>
|
|
60
|
+
<script type="module" src="/app.js"></script>
|
|
61
61
|
|
|
62
62
|
</body>
|
|
63
63
|
</html>
|
package/ui/style.css
CHANGED
|
@@ -335,6 +335,8 @@ body { display: flex; flex-direction: column; }
|
|
|
335
335
|
}
|
|
336
336
|
|
|
337
337
|
.comment-input-context { font-size: 12px; color: var(--mg-text-muted); margin-bottom: 6px; font-family: var(--mg-mono); }
|
|
338
|
+
.comment-outdated { font-size: 12px; color: var(--mg-permission-text); font-family: inherit; }
|
|
339
|
+
.comment-thread > .comment-outdated { margin-bottom: 6px; }
|
|
338
340
|
.mg-textarea {
|
|
339
341
|
width: 100%;
|
|
340
342
|
background: var(--mg-bg); color: var(--mg-text);
|