diff --git a/e2e/plain.spec.ts b/e2e/plain.spec.ts index c8e05ff..3a38789 100644 --- a/e2e/plain.spec.ts +++ b/e2e/plain.spec.ts @@ -63,6 +63,18 @@ test('a document beyond the browser scroll-height limit reaches, finds and edits }) throw new Error(`${e}\n${JSON.stringify(state)}`) }) + // Find selects what it found once the scroll has come to rest, a few frames after the paragraph is in view. On a + // busy machine that came after the caret was put behind the word, and the "!" replaced the word (or went by the + // caret of before): the selection is awaited + await v.page.waitForFunction(() => getSelection()?.toString() === 'END_MARKER', null, { timeout: 5000 }).catch(async (e) => { + const state = await v.page.evaluate(() => ({ + selection: getSelection()?.toString(), + focus: document.activeElement?.outerHTML.slice(0, 120), + matches: document.querySelector('.de-find-count')?.textContent, + top: document.querySelector('.de-scroll')?.scrollTop, + })) + throw new Error(`${e}\n${JSON.stringify(state)}`) + }) await input.press('Escape') await v.selectText('END_MARKER', 'end') await v.page.keyboard.type('!') diff --git a/src/react/docx/track.ts b/src/react/docx/track.ts index 05c2139..87f78c4 100644 --- a/src/react/docx/track.ts +++ b/src/react/docx/track.ts @@ -96,17 +96,43 @@ export function trackEdit(before: Paragraph[], next: Paragraph[], pa0: string, s const inserted = N.slice(p, N.length - s) if (!removed.some(isText) && !inserted.some(isText)) return { next, sel } + // A change right next to one of the same kind by the same author, made since the document was opened, goes on with + // it, under its mark: letters typed one after another are one insertion of when it began, as in Word. (Every edit + // comes with a mark of its own time, and runs join only under the same mark: a word typed across the end of a second + // was two insertions.) A change read from the file has its w:id: it was made before, and what is typed now is a + // change of its own, of now, also when the author's name is the same (two people who never set one are both "Me"). + const own = (t: Tok | undefined, k: TrackMark['k']): TrackMark | undefined => { + if (!t || !isText(t) || t.obj || !t.tc || t.tc.k !== k || t.tc.author !== mark.author || t.tc.mv || t.tc.id !== undefined) return undefined + // (a deletion of text someone inserted carries that insertion: it belongs to that text, not to the change) + const { ins: theirs, ...plain } = t.tc + return theirs ? plain : t.tc + } + const left = p > 0 ? B[p - 1] : undefined + const right = s > 0 ? B[B.length - s] : undefined + // what stays of the removed text: deleted marks. The author's own insertion taken out again is gone for good; // someone else's becomes a deletion, so the reviewer still sees it. Objects go for real, unless already deleted. - const kept: Extract[] = [] - for (const t of removed) { - if (!isText(t)) continue - if (t.tc?.k === 'del') kept.push(t) - else if (t.obj) continue - else if (t.tc?.k === 'ins' && t.tc.author === mark.author) continue - else kept.push({ ...t, tc: { ...mark, k: 'del', ...(t.tc?.k === 'ins' ? { ins: t.tc } : {}) } }) - } - const ins: TrackMark = { ...mark, k: 'ins' } + const stays = removed.filter((t): t is Extract => isText(t) && (t.tc?.k === 'del' || (!t.obj && !(t.tc?.k === 'ins' && t.tc.author === mark.author)))) + const struck = (t: Tok | undefined) => !!t && isText(t) && t.tc?.k !== 'del' + const text = (t: Tok) => isText(t) && !t.obj + // In the paragraph, the text before the edit is followed by what stays (deletions made before, and the text struck + // out now), then by the new text, then by the text after the edit. A change goes on with the one beside it only when + // all of it touches that one: the text struck out is of one piece (no deletion made before in it), the new text is + // text only (a paragraph mark or an object splits it), and nothing else is between them. + let first = -1 + let last = -1 + stays.forEach((t, i) => { + if (!struck(t)) return + if (first < 0) first = i + last = i + }) + const piece = first >= 0 && stays.slice(first, last + 1).every(struck) + const del: TrackMark = + (piece ? own(first > 0 ? stays[first - 1] : left, 'del') ?? own(last < stays.length - 1 ? stays[last + 1] : inserted.length ? undefined : right, 'del') : undefined) ?? + { ...mark, k: 'del' } + const kept = stays.map((t) => (struck(t) ? { ...t, tc: t.tc?.k === 'ins' ? { ...del, ins: t.tc } : del } : t)) + const ins: TrackMark = + (inserted.length && inserted.every(text) ? own(stays.length ? stays[stays.length - 1] : left, 'ins') ?? own(right, 'ins') : undefined) ?? { ...mark, k: 'ins' } // the paragraphs of `next`, the kept text put back where the removal was, the new text marked const paras: Extract[][] = [[]] diff --git a/test/convert-service.test.ts b/test/convert-service.test.ts index b9d4dbb..3a18922 100644 --- a/test/convert-service.test.ts +++ b/test/convert-service.test.ts @@ -230,8 +230,9 @@ describe('office-convert', () => { const dir = mkdtempSync(`${tmpdir()}/office-convert-`) afterAll(() => rmSync(dir, { recursive: true, force: true })) const CLI = `${import.meta.dir}/../packages/convert/src/cli.ts` + // (spawnSync holds the thread: a test's own time can't stop it, the process is stopped instead) const run = (...args: string[]) => { - const p = Bun.spawnSync(['bun', CLI, ...args], { cwd: dir }) + const p = Bun.spawnSync(['bun', CLI, ...args], { cwd: dir, timeout: 60_000 }) return { code: p.exitCode, err: p.stderr.toString(), out: p.stdout } } @@ -264,11 +265,12 @@ describe('office-convert', () => { expect(Buffer.compare(readFileSync(`${dir}/src.doc`), S('sample.doc'))).toBe(0) }) + // (the big file takes the engine 4 to 6 seconds, a test has 5 by default: the time is this test's own) test('to stdout, whole', () => { writeFileSync(`${dir}/big.csv`, bigCsv()) const r = run('big.csv', '-o', '-', '--timeout', '0') expect(r.code).toBe(0) expect(r.out.length).toBeGreaterThan(1_000_000) expect(r.out.subarray(0, 2).toString('latin1')).toBe('PK') - }) + }, 60_000) }) diff --git a/test/track.test.ts b/test/track.test.ts index 5cf547d..b37cb0b 100644 --- a/test/track.test.ts +++ b/test/track.test.ts @@ -80,6 +80,90 @@ describe('tracking an edit', () => { const r = trackEdit(b, n, '0', caret(1, '1'), caret(1), ME) expect(r.next.map(show)).toEqual(['a[+X]', '[+Y]b']) }) + + // (every edit is stamped with its own time: these come a second after one another) + const at = (s: number): TrackMark => ({ ...ME, date: `2026-11-08T10:00:0${s}Z` }) + const dates = (p: Paragraph) => p.runs.flatMap((r) => (r.t === 'text' && r.tc ? [r.tc.date] : [])) + + test('what I type or strike out next to a change of mine goes on with it: one change, whatever the clock says', () => { + const type = (p: Paragraph, o: number, text: string, mark: TrackMark) => + trackEdit([p], [para(insertText(p.runs as RRun[], o, text) as Inline[])], '0', caret(o + text.length), caret(o), mark).next[0] + const strike = (p: Paragraph, a: number, mark: TrackMark) => trackEdit([p], [para(deleteRange(p.runs as RRun[], a, a + 1) as Inline[])], '0', caret(a), caret(a + 1), mark).next[0] + + // "m", then "s" after it, then "r" before them: one insertion, of when it began + let p = type(para([t('tea')]), 3, 'm', at(0)) + p = type(p, 4, 's', at(1)) + expect([show(p), dates(p)]).toEqual(['tea[+ms]', [at(0).date]]) + p = type(p, 3, 'r', at(2)) + expect([show(p), dates(p)]).toEqual(['tea[+rms]', [at(0).date]]) + // Backspace, and again: one deletion + let d = strike(para([t('Hello')]), 4, at(0)) + d = strike(d, 3, at(1)) + expect([show(d), dates(d)]).toEqual(['Hel[-lo]', [at(0).date]]) + // typed next to a deletion of mine: an insertion of its own (a change of another kind is not gone on with), and + // what is typed after it goes on with that insertion + let both = type(strike(para([t('team')]), 3, at(0)), 3, 'x', at(1)) + expect([show(both), dates(both)]).toEqual(['tea[+x][-m]', [at(1).date, at(0).date]]) + both = type(both, 4, 'y', at(2)) + expect([show(both), dates(both)]).toEqual(['tea[+xy][-m]', [at(1).date, at(0).date]]) + + // someone else's change next to mine is theirs: mine begins a change of its own + const theirs = para([t('ab'), t('X', 'ins'), t('cd')]) + const mine = type(theirs, 3, 'Y', at(0)) + expect(mine.runs.filter((r) => r.t === 'text' && r.tc).map((r) => (r as { tc: TrackMark }).tc.author)).toEqual(['Lan', 'Vy']) + // …and so does text I take out of what someone inserted: it keeps their insertion with it + const lan = para([t('ab'), t('XY', 'ins'), t('cd')]) + const cut = strike(strike(lan, 3, at(0)), 2, at(1)) + expect(show(cut)).toBe('ab[-XY]cd') + expect(cut.runs.filter((r) => r.t === 'text' && r.tc).every((r) => (r as { tc: TrackMark }).tc.ins?.author === 'Lan')).toBe(true) + }) + + test('a change goes on with one of mine only where they touch', () => { + const mine = (text: string, k: TrackMark['k'], s: number) => ({ ...t(text), tc: { ...at(s), k } }) as Inline + const range = (a: number, f: number) => ({ a: { pa: '0', o: a }, f: { pa: '0', o: f } }) + const b = para([t('ab'), mine('X', 'del', 0), t('e')]) + + // Enter over a selection right before a deletion of mine: the new deletion stays in the first paragraph, mine + // starts the next one + const enter = trackEdit([b], [para([t('a')]), para([mine('X', 'del', 0), t('e')], 2)], '0', caret(0, '1'), range(1, 2), at(1)).next + expect([enter.map(show), enter.map(dates)]).toEqual([['a[-b]', '[-X]e'], [[at(1).date], [at(0).date]]]) + // a picture pasted over it: the picture is between them + const img: Inline = { t: 'img', target: 'media/image1.png', w: 10, h: 10 } + const pic = trackEdit([b], [para([t('a'), img, mine('X', 'del', 0), t('e')])], '0', caret(2), range(1, 2), at(1)).next[0] + expect([show(pic), dates(pic)]).toEqual(['a[-b][-X]e', [at(1).date, at(0).date]]) + + // a paste that begins with a paragraph mark, right after an insertion of mine: the pasted text is in the next paragraph + const typed = para([t('tea'), mine('m', 'ins', 0), t('x')]) + const paste = trackEdit([typed], [para([t('tea'), mine('m', 'ins', 0)]), para([t('foox')], 2)], '0', caret(3, '1'), caret(4), at(1)).next + expect([paste.map(show), paste.map(dates)]).toEqual([['tea[+m]', '[+foo]x'], [[at(0).date], [at(1).date]]]) + // …or that holds one, or a picture: only part of it would touch mine, it is a change of its own + const two = trackEdit([typed], [para([t('tea'), mine('m', 'ins', 0), t('foo')]), para([t('barx')], 2)], '0', caret(3, '1'), caret(4), at(1)).next + expect([two.map(show), two.map(dates)]).toEqual([['tea[+m][+foo]', '[+bar]x'], [[at(0).date, at(1).date], [at(1).date]]]) + const pictured = trackEdit([typed], [para([t('tea'), mine('m', 'ins', 0), t('foo'), img, t('barx')])], '0', caret(8), caret(4), at(1)).next[0] + expect([show(pictured), dates(pictured)]).toEqual(['tea[+m][+foo][+bar]x', [at(0).date, at(1).date, at(1).date]]) + + // a selection over a deletion of mine and the letter after it, struck out: the letter goes on with my deletion + const struck = para([t('ab'), mine('X', 'del', 0), t('cd')]) + const over2 = trackEdit([struck], [para(deleteRange(struck.runs as RRun[], 2, 4) as Inline[])], '0', caret(2), range(2, 4), at(1)).next[0] + expect([show(over2), dates(over2)]).toEqual(['ab[-Xc]d', [at(0).date]]) + + // typing over a selection right before an insertion of mine: the deletion comes first, the new text touches mine + const ins = para([t('ab'), mine('XY', 'ins', 0)]) + const over = trackEdit([ins], [para(insertText(deleteRange(ins.runs as RRun[], 1, 2), 1, 'Z') as Inline[])], '0', caret(2), range(1, 2), at(1)).next[0] + expect([show(over), dates(over)]).toEqual(['a[-b][+ZXY]', [at(1).date, at(0).date]]) + }) + + test('a change read from the file is not gone on with, even with my name on it: it was made before', () => { + const old: TrackMark = { k: 'ins', id: '12', author: 'Vy', date: '2026-10-01T09:00:00Z' } + const file = para([t('tea'), { ...t('m'), tc: old } as Inline]) + const typed = trackEdit([file], [para(insertText(file.runs as RRun[], 4, 's') as Inline[])], '0', caret(5), caret(4), at(0)).next[0] + expect([show(typed), dates(typed)]).toEqual(['tea[+m][+s]', [old.date, at(0).date]]) + expect(typed.runs.map((r) => r.tc?.id)).toEqual([undefined, '12', undefined]) + + const gone = para([t('Hell'), { ...t('o'), tc: { ...old, k: 'del' } } as Inline]) + const struck = trackEdit([gone], [para(deleteRange(gone.runs as RRun[], 3, 4) as Inline[])], '0', caret(3), caret(4), at(0)).next[0] + expect([show(struck), dates(struck)]).toEqual(['Hel[-l][-o]', [at(0).date, old.date]]) + }) }) describe('accept / reject', () => {