@samuelbradshaw/highlight-helper-js 1.0.2 → 1.0.3

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.
@@ -429,10 +429,12 @@
429
429
  let previousDrawingMode = null;
430
430
  globalThis.addEventListener('beforeprint', () => {
431
431
  previousDrawingMode = this._options.drawingMode;
432
+ if (previousDrawingMode === 'mark-elements') return;
432
433
  this.setOptions({ drawingMode: 'mark-elements' });
433
434
  this.drawHighlights();
434
435
  }, { signal: this._controller.signal });
435
436
  globalThis.addEventListener('afterprint', () => {
437
+ if (previousDrawingMode === 'mark-elements') return;
436
438
  this.setOptions({ drawingMode: previousDrawingMode });
437
439
  this.drawHighlights();
438
440
  }, { signal: this._controller.signal });
@@ -1716,6 +1718,22 @@
1716
1718
  const paragraphFontSize = Number.parseFloat(paragraphStyle.fontSize);
1717
1719
  const snapTolerance = Math.max(paragraphFontSize / 2, 4);
1718
1720
 
1721
+ const pseudoElemRects = _gatherPseudoElemRects(paragraph);
1722
+
1723
+ // Horizontal distance between a rect and a line's span so far, ignoring space taken up by pseudoelements (such as footnote markers) that contribute no text nodes. Zero or negative means the rect overlaps the span.
1724
+ const distanceToLine = (pos, rect) => {
1725
+ const gapLeft = Math.min(pos.maxRight, rect.right);
1726
+ const gapRight = Math.max(pos.minLeft, rect.left);
1727
+ let distance = gapRight - gapLeft;
1728
+ if (distance <= snapTolerance) return distance;
1729
+ for (const pseudoRect of pseudoElemRects) {
1730
+ if (pseudoRect.top >= rect.bottom || pseudoRect.bottom <= rect.top) continue;
1731
+ const overlap = Math.min(pseudoRect.right, gapRight) - Math.max(pseudoRect.left, gapLeft);
1732
+ if (overlap > 0) distance -= overlap;
1733
+ }
1734
+ return distance;
1735
+ };
1736
+
1719
1737
  const linePositions = {};
1720
1738
  const lineBottomKeys = [];
1721
1739
  const textNodeRange = document.createRange();
@@ -1731,8 +1749,7 @@
1731
1749
  for (const rect of textNodeRange.getClientRects()) {
1732
1750
  const lineBottom = lineBottomKeys.find(b => {
1733
1751
  if (Math.abs(b - rect.bottom) >= snapTolerance) return false;
1734
- const pos = linePositions[b];
1735
- return rect.left <= pos.maxRight + snapTolerance && rect.right >= pos.minLeft - snapTolerance;
1752
+ return distanceToLine(linePositions[b], rect) <= snapTolerance;
1736
1753
  }) ?? rect.bottom;
1737
1754
  if (!linePositions[lineBottom]) lineBottomKeys.push(lineBottom);
1738
1755
  const pos = linePositions[lineBottom] ??= { minLeft: rect.left, maxRight: rect.right, maxFontSize: fontSize, maxLineHeight: lineHeight };
@@ -1744,7 +1761,6 @@
1744
1761
  }
1745
1762
 
1746
1763
  // Expand line rects to include pseudoelements, and snap to the paragraph's leading edge
1747
- const pseudoElemRects = _gatherPseudoElemRects(paragraph);
1748
1764
  lineBottomKeys.sort((a, b) => a - b);
1749
1765
  const lines = lineBottomKeys.map((lineBottom) => {
1750
1766
  const pos = linePositions[lineBottom];
@@ -1784,9 +1800,9 @@
1784
1800
  paragraphLineRectsCache.set(paragraph.id, paragraphLineRects);
1785
1801
  }
1786
1802
  const { lines, snapTolerance, pseudoElemRects } = paragraphLineRects;
1787
- // If the previous paragraph is below the current paragraph (such as in multi-column layouts), reset prevParagraphBottom
1788
1803
  if (lines.length === 0) continue;
1789
- if (prevParagraphBottom > lines[0].bottom) prevParagraphBottom = Number.NEGATIVE_INFINITY;
1804
+ // The previous paragraph's bottom is only a valid boundary if it sits above this paragraph's first line — reset it if the paragraphs are side by side, or in a later column
1805
+ if (prevParagraphBottom > lines[0].top + snapTolerance) prevParagraphBottom = Number.NEGATIVE_INFINITY;
1790
1806
  const lineStates = lines.map((lineRect, i) => ({
1791
1807
  topY: i === 0 ? prevParagraphBottom : lines[i - 1].bottom,
1792
1808
  left: Infinity, right: -Infinity,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samuelbradshaw/highlight-helper-js",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "JavaScript tool that enables highlighting or underlining text in an HTML page.",
5
5
  "type": "module",
6
6
  "main": "highlight-helper.mjs",