Skip to content

Commit fbfb7aa

Browse files
Support import as an ES module
1 parent 4ea679b commit fbfb7aa

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

highlight-helper.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* https://github.com/samuelbradshaw/highlight-helper-js
44
*/
55

6+
'use strict';
7+
68
function Highlighter(options = hhDefaultOptions) {
79
for (const key of Object.keys(hhDefaultOptions)) {
810
options[key] = options[key] ?? hhDefaultOptions[key];
@@ -231,6 +233,7 @@ function Highlighter(options = hhDefaultOptions) {
231233
range.endContainer.splitText(range.endOffset);
232234
const textNodeIter = document.createNodeIterator(range.commonAncestorContainer, NodeFilter.SHOW_TEXT);
233235
const relevantTextNodes = [];
236+
let node;
234237
while (node = textNodeIter.nextNode()) {
235238
if (range.intersectsNode(node) && node !== range.startContainer && node.textContent !== '' && !node.parentElement.closest('rt') && node.parentElement.closest(options.paragraphSelector)) relevantTextNodes.push(node);
236239
if (node === range.endContainer) break;
@@ -239,7 +242,7 @@ function Highlighter(options = hhDefaultOptions) {
239242
for (let tn = 0; tn < relevantTextNodes.length; tn++) {
240243
const textNode = relevantTextNodes[tn];
241244
if (textNode.parentElement.dataset.highlightId) {
242-
overlappingHighlightIds.add(textNode.parentElement.dataset.highlightId)
245+
overlappingHighlightIds.add(textNode.parentElement.dataset.highlightId);
243246
}
244247
const styledMark = document.createElement('mark');
245248
styledMark.dataset.highlightId = highlightId;
@@ -302,6 +305,7 @@ function Highlighter(options = hhDefaultOptions) {
302305
template.innerHTML = htmlString;
303306
let htmlElement = template.content.firstChild;
304307

308+
let node;
305309
const textNodeIter = document.createNodeIterator(htmlElement, NodeFilter.SHOW_TEXT);
306310
while (node = textNodeIter.nextNode()) node.parentNode.removeChild(node);
307311
range.insertNode(htmlElement);
@@ -323,8 +327,8 @@ function Highlighter(options = hhDefaultOptions) {
323327
// Create a new highlight, or update an existing highlight when it changes
324328
this.createOrUpdateHighlight = (attributes = {}, triggeredByUserAction = true) => {
325329
let highlightId = attributes.highlightId ?? activeHighlightId ?? options.highlightIdFunction();
326-
appearanceChanges = [];
327-
boundsChanges = [];
330+
const appearanceChanges = [];
331+
const boundsChanges = [];
328332

329333
let isNewHighlight, oldHighlightInfo;
330334
if (highlightsById.hasOwnProperty(highlightId)) {
@@ -541,7 +545,7 @@ function Highlighter(options = hhDefaultOptions) {
541545
// Get info for specified highlights, or all highlights on the page
542546
this.getHighlightInfo = (highlightIds = Object.keys(highlightsById), paragraphId = null) => {
543547
let filteredHighlights = []
544-
for (highlightId of highlightIds) {
548+
for (const highlightId of highlightIds) {
545549
const highlightInfo = highlightsById[highlightId];
546550
if (!paragraphId || paragraphId === highlightInfo.startParagraphId) {
547551
filteredHighlights.push(highlightInfo);
@@ -1272,6 +1276,7 @@ function Highlighter(options = hhDefaultOptions) {
12721276
// Get line positions (bottom edge of each line)
12731277
let linePositions = new Set();
12741278
let lineWalker = document.createTreeWalker(paragraph, NodeFilter.SHOW_TEXT);
1279+
let textNode;
12751280
while (textNode = lineWalker.nextNode()) {
12761281
const computedStyle = window.getComputedStyle(textNode.parentElement);
12771282
const lineHeight = parseInt((computedStyle.lineHeight === 'normal' ? computedStyle.fontSize : computedStyle.lineHeight).replace('px', ''));
@@ -1302,8 +1307,8 @@ function Highlighter(options = hhDefaultOptions) {
13021307
mergedRect.width = rect.width;
13031308
}
13041309
// Process then remove rects that apply to the current line
1305-
minLeft = Math.min(mergedRect.x, rect.x);
1306-
maxRight = Math.max(mergedRect.right, rect.right);
1310+
const minLeft = Math.min(mergedRect.x, rect.x);
1311+
const maxRight = Math.max(mergedRect.right, rect.right);
13071312
mergedRect.width = maxRight - minLeft;
13081313
mergedRect.x = minLeft;
13091314
unmergedRects.splice(r, 1); r--;
@@ -1447,3 +1452,6 @@ let hhDefaultOptions = {
14471452
}
14481453

14491454
console.info('Highlighter loaded');
1455+
1456+
// Make Highlighter available to ES module (highlight-helper.mjs)
1457+
window.Highlighter = Highlighter;

highlight-helper.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
await import('./highlight-helper.js');
2+
const Highlighter = window.Highlighter;
3+
export { Highlighter };

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "@samuelbradshaw/highlight-helper-js",
33
"version": "0.7.0",
44
"description": "JavaScript tool that enables highlighting or underlining text in an HTML page.",
5-
"main": "highlight-helper.js",
5+
"type": "module",
6+
"main": "highlight-helper.mjs",
67
"repository": {
78
"type": "git",
89
"url": "git+https://github.com/samuelbradshaw/highlight-helper-js.git"

0 commit comments

Comments
 (0)