feat: page-by-page compare for multi-page docs + move mode dropdown right
- PDF and DjVu extractors separate pages with \f; compare() splits each document
into pages and renders a per-page diff. The existing toolbar page nav (< 1 / N >)
drives it through the diff controller's new setPage, so multi-page PDFs and DjVu
compare page-by-page. Single-page formats (docx/doc/rtf/odf/txt/...) are
unchanged — they render as a continuous flow with no page breaks to split on.
- Move the compare view-mode dropdown from the middle to the right, just left of
the language selector.
- Add a 2-page sample PDF pair (samples/manual-v{1,2}.pdf) for testing.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+2
-3
@@ -82,15 +82,14 @@
|
||||
<path d="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div id="toolbarRight">
|
||||
<select id="compareModeSelect" class="compare-only" title="Compare view" data-i18n-title="compare.badge">
|
||||
<option value="side-by-side" data-i18n="compare.modeSideBySide">Side by side</option>
|
||||
<option value="unified" data-i18n="compare.modeUnified">Unified</option>
|
||||
<option value="inline" data-i18n="compare.modeInline">Inline</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div id="toolbarRight">
|
||||
<select id="langSelect" title="Language" data-i18n-title="tip.language">
|
||||
<option value="en">English</option>
|
||||
<option value="pl">Polski</option>
|
||||
|
||||
+2
-3
@@ -82,15 +82,14 @@
|
||||
<path d="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div id="toolbarRight">
|
||||
<select id="compareModeSelect" class="compare-only" title="Compare view" data-i18n-title="compare.badge">
|
||||
<option value="side-by-side" data-i18n="compare.modeSideBySide">Side by side</option>
|
||||
<option value="unified" data-i18n="compare.modeUnified">Unified</option>
|
||||
<option value="inline" data-i18n="compare.modeInline">Inline</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div id="toolbarRight">
|
||||
<select id="langSelect" title="Language" data-i18n-title="tip.language">
|
||||
<option value="en">English</option>
|
||||
<option value="pl">Polski</option>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
%PDF-1.4
|
||||
1 0 obj
|
||||
<< /Type /Catalog /Pages 2 0 R >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 7 0 R >> >> /Contents 5 0 R >>
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 7 0 R >> >> /Contents 6 0 R >>
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Length 174 >>
|
||||
stream
|
||||
BT
|
||||
/F1 14 Tf
|
||||
16 TL
|
||||
72 720 Td
|
||||
(Project Aurora Manual) Tj
|
||||
T* (Version 2.3.0) Tj
|
||||
T* (Released June 1, 2026.) Tj
|
||||
T* () Tj
|
||||
T* (Chapter 1: Setup) Tj
|
||||
T* (Install the package.) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
6 0 obj
|
||||
<< /Length 130 >>
|
||||
stream
|
||||
BT
|
||||
/F1 14 Tf
|
||||
16 TL
|
||||
72 720 Td
|
||||
(Chapter 2: Usage) Tj
|
||||
T* (Run the start command.) Tj
|
||||
T* () Tj
|
||||
T* (Known issue: export is slow.) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
7 0 obj
|
||||
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
|
||||
endobj
|
||||
xref
|
||||
0 8
|
||||
0000000000 65535 f
|
||||
0000000009 00000 n
|
||||
0000000058 00000 n
|
||||
0000000121 00000 n
|
||||
0000000247 00000 n
|
||||
0000000373 00000 n
|
||||
0000000598 00000 n
|
||||
0000000779 00000 n
|
||||
trailer
|
||||
<< /Size 8 /Root 1 0 R >>
|
||||
startxref
|
||||
849
|
||||
%%EOF
|
||||
@@ -0,0 +1,61 @@
|
||||
%PDF-1.4
|
||||
1 0 obj
|
||||
<< /Type /Catalog /Pages 2 0 R >>
|
||||
endobj
|
||||
2 0 obj
|
||||
<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>
|
||||
endobj
|
||||
3 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 7 0 R >> >> /Contents 5 0 R >>
|
||||
endobj
|
||||
4 0 obj
|
||||
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 7 0 R >> >> /Contents 6 0 R >>
|
||||
endobj
|
||||
5 0 obj
|
||||
<< /Length 189 >>
|
||||
stream
|
||||
BT
|
||||
/F1 14 Tf
|
||||
16 TL
|
||||
72 720 Td
|
||||
(Project Aurora Manual) Tj
|
||||
T* (Version 2.4.0) Tj
|
||||
T* (Released June 24, 2026.) Tj
|
||||
T* () Tj
|
||||
T* (Chapter 1: Setup) Tj
|
||||
T* (Install the package and the embed.) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
6 0 obj
|
||||
<< /Length 129 >>
|
||||
stream
|
||||
BT
|
||||
/F1 14 Tf
|
||||
16 TL
|
||||
72 720 Td
|
||||
(Chapter 2: Usage) Tj
|
||||
T* (Run the start command.) Tj
|
||||
T* () Tj
|
||||
T* (Added page-by-page compare.) Tj
|
||||
ET
|
||||
endstream
|
||||
endobj
|
||||
7 0 obj
|
||||
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
|
||||
endobj
|
||||
xref
|
||||
0 8
|
||||
0000000000 65535 f
|
||||
0000000009 00000 n
|
||||
0000000058 00000 n
|
||||
0000000121 00000 n
|
||||
0000000247 00000 n
|
||||
0000000373 00000 n
|
||||
0000000613 00000 n
|
||||
0000000793 00000 n
|
||||
trailer
|
||||
<< /Size 8 /Root 1 0 R >>
|
||||
startxref
|
||||
863
|
||||
%%EOF
|
||||
+11
-3
@@ -166,8 +166,13 @@ const RENDERERS = { 'side-by-side': renderSideBySide, unified: renderUnified, in
|
||||
|
||||
/* ── Public entry ──────────────────────────────────────────────────────────── */
|
||||
|
||||
export function renderCompare(target, { leftText, rightText, blame = {}, mode = 'side-by-side' } = {}) {
|
||||
const rows = buildRows(leftText, rightText)
|
||||
export function renderCompare(target, { pages = [], blame = {}, mode = 'side-by-side' } = {}) {
|
||||
// `pages`: one { leftText, rightText } per document page. Most formats are a
|
||||
// single page; PDF/DjVu split per page so the diff can be navigated page by page.
|
||||
if (!pages.length) pages = [{ leftText: '', rightText: '' }]
|
||||
const rowsCache = new Map()
|
||||
const rowsFor = i => { if (!rowsCache.has(i)) rowsCache.set(i, buildRows(pages[i].leftText, pages[i].rightText)); return rowsCache.get(i) }
|
||||
let pageIdx = 0
|
||||
target.innerHTML = ''
|
||||
target.classList.add('diff-view')
|
||||
|
||||
@@ -248,14 +253,17 @@ export function renderCompare(target, { leftText, rightText, blame = {}, mode =
|
||||
[...bodyWrap.children].forEach(c => { if (c !== tip) c.remove() })
|
||||
pins.splice(0).forEach(p => p.remove()) // re-render invalidates anchors
|
||||
hideTip()
|
||||
RENDERERS[current](bodyWrap, rows)
|
||||
RENDERERS[current](bodyWrap, rowsFor(pageIdx))
|
||||
}
|
||||
function setMode(m) { if (RENDERERS[m]) { current = m; render() } }
|
||||
function setPage(i) { if (i >= 0 && i < pages.length && i !== pageIdx) { pageIdx = i; render() } }
|
||||
|
||||
render()
|
||||
|
||||
return {
|
||||
setMode,
|
||||
setPage,
|
||||
pageCount: pages.length,
|
||||
destroy() {
|
||||
pins.splice(0).forEach(p => p.remove())
|
||||
tip.remove()
|
||||
|
||||
+16
-5
@@ -319,10 +319,16 @@ export class DocumentViewer {
|
||||
container.appendChild(page)
|
||||
document.getElementById('mainContainer').classList.add('is-compare') // full-bleed diff + flat toolbar
|
||||
|
||||
// \f from the extractors separates document pages → compare page-by-page.
|
||||
const aPages = (a.text ?? '').split('\f')
|
||||
const bPages = (b.text ?? '').split('\f')
|
||||
const pageCount = Math.max(aPages.length, bPages.length)
|
||||
const pages = []
|
||||
for (let i = 0; i < pageCount; i++) pages.push({ leftText: aPages[i] ?? '', rightText: bPages[i] ?? '' })
|
||||
|
||||
const { renderCompare } = await import('./diff-view.js')
|
||||
this._compareCtl = renderCompare(page, {
|
||||
leftText: a.text, rightText: b.text,
|
||||
blame: opts.blame || {}, mode: opts.mode || 'side-by-side',
|
||||
pages, blame: opts.blame || {}, mode: opts.mode || 'side-by-side',
|
||||
})
|
||||
|
||||
// drive the view mode from the top-toolbar dropdown (shown via .is-compare)
|
||||
@@ -332,7 +338,7 @@ export class DocumentViewer {
|
||||
modeSel.onchange = () => this._compareCtl?.setMode(modeSel.value)
|
||||
}
|
||||
|
||||
this.numPages = 1; this.currentPage = 1
|
||||
this.numPages = pageCount; this.currentPage = 1
|
||||
this.updatePageInfo()
|
||||
const badge = document.getElementById('formatBadge')
|
||||
badge.textContent = t('compare.badge')
|
||||
@@ -365,10 +371,15 @@ export class DocumentViewer {
|
||||
}
|
||||
|
||||
goToPage(page) {
|
||||
if (!this.activeRenderer) return
|
||||
if (!this.activeRenderer && !this._compareCtl) return
|
||||
page = Math.max(1, Math.min(this.numPages, page))
|
||||
this.currentPage = page
|
||||
this.activeRenderer.scrollToPage(page)
|
||||
if (this._compareCtl) {
|
||||
this._compareCtl.setPage(page - 1) // compare pages are 0-based
|
||||
document.getElementById('viewerContainer').scrollTop = 0 // show the top of the new page
|
||||
} else {
|
||||
this.activeRenderer.scrollToPage(page)
|
||||
}
|
||||
this.updatePageInfo()
|
||||
}
|
||||
|
||||
|
||||
@@ -46,13 +46,16 @@ export class DjVuRenderer extends BaseRenderer {
|
||||
|
||||
// Plain text of the DjVu for the compare view — the worker returns word zones
|
||||
// ({xmin,ymin,xmax,ymax,str}) per page; group them into lines by baseline.
|
||||
// Returns the document text with a form-feed (\f) between pages, so the
|
||||
// compare view can diff page-by-page.
|
||||
async extractText(buffer) {
|
||||
this._spawnWorker()
|
||||
try {
|
||||
const meta = await this._call('open', { buffer })
|
||||
const lines = []
|
||||
const pages = []
|
||||
for (let i = 0; i < (meta.pageCount || 1); i++) {
|
||||
const { words } = await this._call('text', { index: i })
|
||||
const lines = []
|
||||
let line = [], lineY = null
|
||||
for (const w of (words || [])) {
|
||||
const h = (w.ymax - w.ymin) || 10
|
||||
@@ -60,8 +63,9 @@ export class DjVuRenderer extends BaseRenderer {
|
||||
line.push(w.str); lineY = w.ymin
|
||||
}
|
||||
if (line.length) lines.push(line.join(' '))
|
||||
pages.push(lines.join('\n'))
|
||||
}
|
||||
return lines.join('\n')
|
||||
return pages.join('\f')
|
||||
} finally {
|
||||
this._worker?.terminate()
|
||||
this._worker = null
|
||||
|
||||
@@ -174,12 +174,15 @@ export class PDFRenderer extends BaseRenderer {
|
||||
// Plain-text of the PDF for the compare view — pulled from pdf.js's text
|
||||
// layer (no canvas render). Lines are split on pdf.js's hasEOL flag and on a
|
||||
// baseline (y) change, so reflowed runs land on the right line.
|
||||
// Returns the document text with a form-feed (\f) between pages, so the
|
||||
// compare view can diff page-by-page.
|
||||
async extractText(buffer) {
|
||||
const doc = await pdfjsLib.getDocument({ data: new Uint8Array(buffer) }).promise
|
||||
const lines = []
|
||||
const pages = []
|
||||
try {
|
||||
for (let p = 1; p <= doc.numPages; p++) {
|
||||
const { items } = await (await doc.getPage(p)).getTextContent()
|
||||
const lines = []
|
||||
let line = '', lastY = null
|
||||
for (const it of items) {
|
||||
const y = it.transform?.[5]
|
||||
@@ -189,11 +192,12 @@ export class PDFRenderer extends BaseRenderer {
|
||||
if (it.hasEOL) { lines.push(line); line = ''; lastY = null }
|
||||
}
|
||||
if (line) lines.push(line)
|
||||
pages.push(lines.join('\n'))
|
||||
}
|
||||
} finally {
|
||||
doc.destroy()
|
||||
}
|
||||
return lines.join('\n')
|
||||
return pages.join('\f')
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
||||
Reference in New Issue
Block a user