From 5378d934802d51557990feeaa38075e57fb350b7 Mon Sep 17 00:00:00 2001 From: Abe Jellinek Date: Mon, 11 Sep 2023 16:27:06 -0400 Subject: [PATCH] Don't preserve whitespace from HTML in annotation text This is an imperfect approach because it might miss empty block elements that nevertheless start new paragraphs, but it works well for most webpages and typical EPUBs. We can fine-tune more later. https://forums.zotero.org/discussion/107685/z7-beta-zotero-introduces-extra-line-breaks-in-snapshot-annotations --- src/dom/epub/epub-view.ts | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/dom/epub/epub-view.ts b/src/dom/epub/epub-view.ts index 3851c492..2ef9f24c 100644 --- a/src/dom/epub/epub-view.ts +++ b/src/dom/epub/epub-view.ts @@ -16,7 +16,8 @@ import Epub, { } from "epubjs"; import { moveRangeEndsIntoTextNodes, - PersistentRange + PersistentRange, + splitRangeToTextNodes } from "../common/lib/range"; import { FragmentSelector, @@ -350,10 +351,32 @@ class EPUBView extends DOMView { if (range.collapsed) { return null; } - let text = type == 'highlight' || type == 'underline' ? range.toString().trim() : undefined; - // If this annotation type wants text, but we didn't get any, abort - if (text === '') { - return null; + let text; + if (type == 'highlight' || type == 'underline') { + text = ''; + let lastSplitRange; + for (let splitRange of splitRangeToTextNodes(range)) { + if (lastSplitRange) { + let lastSplitRangeContainer = closestElement(lastSplitRange.commonAncestorContainer); + let lastSplitRangeBlock = lastSplitRangeContainer && getContainingBlock(lastSplitRangeContainer); + let splitRangeContainer = closestElement(splitRange.commonAncestorContainer); + let splitRangeBlock = splitRangeContainer && getContainingBlock(splitRangeContainer); + if (lastSplitRangeBlock !== splitRangeBlock) { + text += '\n\n'; + } + } + text += splitRange.toString().replace(/\s+/g, ' '); + lastSplitRange = splitRange; + } + text = text.trim(); + + // If this annotation type wants text, but we didn't get any, abort + if (!text) { + return null; + } + } + else { + text = undefined; } let selector = this.toSelector(range);