From 478a1c2c1f4d3490ae29d26ae488654a0e2f2b88 Mon Sep 17 00:00:00 2001 From: John Wooten Date: Sun, 29 Nov 2026 16:00:16 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20Fix=20double-click=20paragraph?= =?UTF-8?q?=20copying=20in=20PDF=20viewer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Codex --- .../selection/paragraph-double-click.js | 19 +++++++++++++------ tests/paragraph-double-click.test.mjs | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/viewer/selection/paragraph-double-click.js b/src/viewer/selection/paragraph-double-click.js index 57faf0f..0b383dc 100644 --- a/src/viewer/selection/paragraph-double-click.js +++ b/src/viewer/selection/paragraph-double-click.js @@ -39,7 +39,7 @@ function visualLines(items) { return lines.sort((a, b) => a.top - b.top || a.left - b.left); } -function continuesParagraph(previous, next) { +function continuesParagraph(previous, next, following) { const height = Math.max(previous.height, next.height); const separation = next.top - previous.top; if (separation < height * 0.55 || separation > height * 1.95) return false; @@ -51,11 +51,17 @@ function continuesParagraph(previous, next) { if (Math.max(previous.height, next.height) / Math.min(previous.height, next.height) > 1.55) return false; // First-line indentation marks a new paragraph; dedenting a wrapped // second line is expected. - if (next.left - previous.left > height * 1.7) return false; + const indent = next.left - previous.left; + if (indent > height * 1.7) return false; + const previousText = previous.items.map(item => item.text).join(''); + const endsSentence = /[.!?][”"')\]]?\s*$/.test(previousText); + // A modest first-line indent can be the only visible break in justified + // text. The following line returns to the body margin on a first-line indent. + if (endsSentence && indent > height * 0.8 && following && + Math.abs(following.left - previous.left) < height * 0.25) return false; // A short sentence-ending line followed by a full line at the same margin // is often an unindented paragraph boundary. - const previousText = previous.items.map(item => item.text).join(''); - if (/[.!?][”"')\]]?\s*$/.test(previousText) && + if (endsSentence && previous.right - previous.left > height * 5 && Math.abs(next.left - previous.left) < height * 1.2 && previous.right < next.right - height * 3 && @@ -82,8 +88,9 @@ export function paragraphAtSpan(spans, clickedSpan) { Math.min(line.right, anchor.right) > Math.max(line.left, anchor.left)); const index = columnLines.indexOf(anchor); let start = index, end = index; - while (start > 0 && continuesParagraph(columnLines[start - 1], columnLines[start])) start--; - while (end < columnLines.length - 1 && continuesParagraph(columnLines[end], columnLines[end + 1])) end++; + while (start > 0 && continuesParagraph(columnLines[start - 1], columnLines[start], columnLines[start + 1])) start--; + while (end < columnLines.length - 1 && + continuesParagraph(columnLines[end], columnLines[end + 1], columnLines[end + 2])) end++; const paragraphLines = columnLines.slice(start, end + 1); return { lines: paragraphLines, spans: paragraphLines.flatMap(line => line.items.map(item => item.span)) }; } diff --git a/tests/paragraph-double-click.test.mjs b/tests/paragraph-double-click.test.mjs index a6af4a1..56a051d 100644 --- a/tests/paragraph-double-click.test.mjs +++ b/tests/paragraph-double-click.test.mjs @@ -20,6 +20,25 @@ test('selects all wrapped lines but stops at a new paragraph indent', () => { assert.deepEqual(paragraphAtSpan([a,b,c,d,e],e).spans, [d,e]); }); +test('splits tightly set paragraphs with a modest first-line indent', () => { + // Page 2 of the Bitcoin whitepaper uses a 26px indent with 18px text + // and the same 21px baseline spacing on both sides of the break. + const a = span('The first paragraph starts here', 262, 0, 685, {}, 18); + const b = span('and continues to this almost full last line.', 236, 21, 684, {}, 18); + const c = span('The second paragraph begins here', 262, 42, 686, {}, 18); + const d = span('and wraps back to the margin.', 236, 63, 360, {}, 18); + const spans = [a, b, c, d]; + assert.deepEqual(paragraphAtSpan(spans, b).spans, [a, b]); + assert.deepEqual(paragraphAtSpan(spans, c).spans, [c, d]); +}); + +test('keeps hanging indents with their opening line', () => { + const a = span('Opening line ends in a complete sentence.', 236, 0, 710, {}, 18); + const b = span('The hanging indent continues here', 262, 21, 686, {}, 18); + const c = span('and stays indented on the final line.', 262, 42, 360, {}, 18); + assert.deepEqual(paragraphAtSpan([a, b, c], b).spans, [a, b, c]); +}); + test('uses semantic tags instead of visual guesses when available', () => { const a = span('Tagged paragraph line one',20,20,175,{copyBlock:'5',copyRole:'P'}); const b = span('line two',20,35,80,{copyBlock:'5',copyRole:'P'});