Skip to content

Commit 600a451

Browse files
Consolidate logic for styling active highlights
1 parent 9c2d97a commit 600a451

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

highlight-helper.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ function Highlighter(options = hhDefaultOptions) {
448448
changes: appearanceChanges.concat(boundsChanges),
449449
}
450450

451-
if (highlightId !== activeHighlightId || options.drawingMode !== 'mark-elements') {
451+
if (highlightId !== activeHighlightId || options.drawingMode === 'svg') {
452452
this.drawHighlights([highlightId]);
453453
}
454454

@@ -476,17 +476,6 @@ function Highlighter(options = hhDefaultOptions) {
476476
}
477477
const selection = window.getSelection();
478478
const highlightRange = highlightToActivate.rangeObj.cloneRange();
479-
480-
// Hide <mark> highlights and wrappers while the highlight is active. This prevents it from getting visually out of sync with the selection UI (mark highlights and wrappers aren't redrawn while the highlight is active, because DOM manipulation can make the selection UI unstable).
481-
for (const element of this.annotatableContainer.querySelectorAll(`[data-highlight-id="${highlightId}"]:not(g)`)) {
482-
if (element.tagName.toLowerCase() === 'mark') {
483-
element.dataset.color = '';
484-
element.dataset.style = '';
485-
} else {
486-
element.style.display = 'none';
487-
}
488-
}
489-
490479
activeHighlightId = highlightId;
491480
updateSelectionUi('appearance');
492481

@@ -525,7 +514,7 @@ function Highlighter(options = hhDefaultOptions) {
525514
}
526515
updateSelectionUi('appearance');
527516
if (deactivatedHighlight) {
528-
if (options.drawingMode === 'mark-elements') {
517+
if (options.drawingMode !== 'svg') {
529518
this.drawHighlights([deactivatedHighlight.highlightId]);
530519
}
531520
this.annotatableContainer.dispatchEvent(new CustomEvent('hh:highlightdeactivate', { detail: {
@@ -904,9 +893,10 @@ function Highlighter(options = hhDefaultOptions) {
904893
// If the selection starts in another annotatable container, let the other container handle it
905894
if (selection.anchorNode && selection.anchorNode.parentElement.closest('[data-hh-container]') && !this.annotatableContainer.contains(selection.anchorNode)) return;
906895

907-
const color = highlightsById[activeHighlightId]?.color;
896+
const highlightInfo = highlightsById[activeHighlightId] ?? null;
897+
const color = highlightInfo?.color ?? null;
898+
const style = highlightInfo?.style ?? null;
908899
const colorString = options.colors[color] ?? 'AccentColor';
909-
const style = highlightsById[activeHighlightId]?.style;
910900

911901
// Update SVG shapes for the active highlight (bring shape group to front, and duplicate it to make the highlight darker)
912902
svgActiveOverlay.innerHTML = '';
@@ -927,8 +917,12 @@ function Highlighter(options = hhDefaultOptions) {
927917
selectionStylesheet.replaceSync(`${options.containerSelector} ::selection { background-color: transparent; }`);
928918
} else if (activeHighlightId) {
929919
const styleTemplate = getStyleTemplate(style, 'css', null);
920+
// Hide the active highlight (and wrappers), and set a selection style that mimics the highlight. This avoids the need to redraw the highlight while actively editing it (especially important for <mark> highlights, because DOM manipulation around the selection can make the selection UI unstable).
930921
selectionStylesheet.replaceSync(`
931-
${options.containerSelector} ::selection { background-color: hsl(from ${colorString} h s l / 20%); --hh-color: ${colorString}; ${styleTemplate} }
922+
${options.containerSelector} ::highlight(${highlightInfo.escapedHighlightId}) { all: unset; }
923+
${options.containerSelector} mark[data-highlight-id="${activeHighlightId}"][data-style] { all: unset; }
924+
${options.containerSelector} .hh-wrapper-start[data-highlight-id="${activeHighlightId}"], .hh-wrapper-end[data-highlight-id="${activeHighlightId}"] { display: none; }
925+
${options.containerSelector} ::selection { background-color: hsl(from ${colorString} h s l / 0.25); --hh-color: ${colorString}; ${styleTemplate} }
932926
${options.containerSelector} rt::selection, ${options.containerSelector} img::selection { background-color: transparent; }
933927
`);
934928
} else {

0 commit comments

Comments
 (0)