@samuelbradshaw/highlight-helper-js 0.7.2 → 0.7.4

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.
Files changed (2) hide show
  1. package/highlight-helper.js +18 -16
  2. package/package.json +1 -1
@@ -453,11 +453,12 @@ function Highlighter(options = hhDefaultOptions) {
453
453
  changes: appearanceChanges.concat(boundsChanges),
454
454
  }
455
455
 
456
- if (highlightId !== activeHighlightId) this.drawHighlights([highlightId]);
457
- if (highlightId === activeHighlightId && appearanceChanges.length > 0) {
458
- updateSelectionUi('appearance');
459
- } else if (triggeredByUserAction && highlightId !== activeHighlightId) {
456
+ if (triggeredByUserAction && !activeHighlightId) {
460
457
  this.activateHighlight(highlightId);
458
+ } else if (activeHighlightId && highlightId === activeHighlightId && appearanceChanges.length > 0) {
459
+ updateSelectionUi('appearance');
460
+ } else {
461
+ this.drawHighlights([highlightId]);
461
462
  }
462
463
 
463
464
  if (isNewHighlight) {
@@ -480,6 +481,7 @@ function Highlighter(options = hhDefaultOptions) {
480
481
  const highlightRange = highlightToActivate.rangeObj.cloneRange();
481
482
  activeHighlightId = highlightId;
482
483
  updateSelectionUi('appearance');
484
+ updateSelectionUi('bounds');
483
485
 
484
486
  // Update the selection range if needed
485
487
  // In Android Chrome, sometimes selection handles don't show when a selection is updated programmatically, so it's best to only update the selection if needed.
@@ -702,7 +704,7 @@ function Highlighter(options = hhDefaultOptions) {
702
704
 
703
705
  // Long press in annotatable container (triggered by setTimeout() in pointerdown event)
704
706
  const respondToLongPress = (event) => {
705
- respondToPointerUp(event, isLongPress = true);
707
+ respondToPointerUp(event, true);
706
708
  tapResult = null;
707
709
  }
708
710
 
@@ -914,8 +916,10 @@ function Highlighter(options = hhDefaultOptions) {
914
916
 
915
917
  // Bring active highlight to the front
916
918
  const svgHighlight = svgBackground.querySelector(`g[data-highlight-id="${activeHighlightId}"]`);
917
- svgBackground.appendChild(svgHighlight);
918
- svgBackground.appendChild(svgActiveOverlay);
919
+ if (svgHighlight) {
920
+ svgBackground.appendChild(svgHighlight);
921
+ svgBackground.appendChild(svgActiveOverlay);
922
+ }
919
923
  }
920
924
 
921
925
  if (changeType === 'appearance') {
@@ -1010,7 +1014,7 @@ function Highlighter(options = hhDefaultOptions) {
1010
1014
 
1011
1015
  // Prevent the range from starting or ending in an element that doesn't match the paragraph selector
1012
1016
  if (this.annotatableContainer.contains(range.commonAncestorContainer) && (!startNode.parentElement.closest(options.paragraphSelector) || !endNode.parentElement.closest(options.paragraphSelector))) {
1013
- annotatableParagraphsInRange = Array.from(this.annotatableParagraphs).filter((paragraph) => {
1017
+ let annotatableParagraphsInRange = Array.from(this.annotatableParagraphs).filter((paragraph) => {
1014
1018
  const relativeStartPosition = startNode.compareDocumentPosition(paragraph);
1015
1019
  const relativeEndPosition = endNode.compareDocumentPosition(paragraph);
1016
1020
  return (relativeStartPosition & Node.DOCUMENT_POSITION_FOLLOWING || relativeStartPosition & Node.DOCUMENT_POSITION_CONTAINS) && (relativeEndPosition & Node.DOCUMENT_POSITION_PRECEDING || relativeEndPosition & Node.DOCUMENT_POSITION_CONTAINS);
@@ -1263,6 +1267,7 @@ function Highlighter(options = hhDefaultOptions) {
1263
1267
  const elementRect = element.getBoundingClientRect();
1264
1268
  if (Math.round(elementRect.width) === Math.round(rect.width) && Math.round(elementRect.height) === Math.round(rect.height) && Math.round(elementRect.top) === Math.round(rect.top) && Math.round(elementRect.left) === Math.round(rect.left)) {
1265
1269
  unmergedRects.splice(ur, 1);
1270
+ break;
1266
1271
  }
1267
1272
  }
1268
1273
  }
@@ -1271,7 +1276,6 @@ function Highlighter(options = hhDefaultOptions) {
1271
1276
  for (const paragraph of paragraphs) {
1272
1277
  const paragraphRect = paragraph.getBoundingClientRect();
1273
1278
  const computedParagraphStyle = window.getComputedStyle(paragraph);
1274
- const paragraphLineHeight = parseInt((computedParagraphStyle.lineHeight === 'normal' ? computedParagraphStyle.fontSize : computedParagraphStyle.lineHeight).replace('px', ''));
1275
1279
  const paragraphTopPadding = parseInt(computedParagraphStyle.getPropertyValue('padding-top').replace('px', ''));
1276
1280
 
1277
1281
  // Get line positions (bottom edge of each line)
@@ -1279,13 +1283,11 @@ function Highlighter(options = hhDefaultOptions) {
1279
1283
  let lineWalker = document.createTreeWalker(paragraph, NodeFilter.SHOW_TEXT);
1280
1284
  let textNode;
1281
1285
  while (textNode = lineWalker.nextNode()) {
1282
- const computedStyle = window.getComputedStyle(textNode.parentElement);
1283
- const lineHeight = parseInt((computedStyle.lineHeight === 'normal' ? computedStyle.fontSize : computedStyle.lineHeight).replace('px', ''));
1284
- if (lineHeight >= paragraphLineHeight) {
1285
- const referenceRange = document.createRange();
1286
- referenceRange.selectNode(textNode);
1287
- for (const rangeRect of referenceRange.getClientRects()) linePositions.add(rangeRect.bottom);
1288
- }
1286
+ // Skip text nodes in elements that are higher or lower than surrounding text
1287
+ if (textNode.parentElement.closest('sup, sub, rt')) continue;
1288
+ const referenceRange = document.createRange();
1289
+ referenceRange.selectNode(textNode);
1290
+ for (const rangeRect of referenceRange.getClientRects()) linePositions.add(rangeRect.bottom);
1289
1291
  }
1290
1292
  linePositions = Array.from(linePositions);
1291
1293
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samuelbradshaw/highlight-helper-js",
3
- "version": "0.7.2",
3
+ "version": "0.7.4",
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",