-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
173 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
addons/website/static/src/interactions/text_highlights.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Interaction } from "@website/core/interaction"; | ||
import { registry } from "@web/core/registry"; | ||
import { | ||
applyTextHighlight, | ||
removeTextHighlight, | ||
switchTextHighlight, | ||
} from "@website/js/text_processing"; | ||
|
||
class TextHighlight extends Interaction { | ||
|
||
static selector = '#wrapwrap' | ||
|
||
setup() { | ||
this.observerLock = new Map(); | ||
this.resizeObserver = new window.ResizeObserver(entries => { | ||
if (this.isDestroyed) { | ||
return; | ||
} | ||
window.requestAnimationFrame(() => { | ||
const textHighlightEls = new Set(); | ||
entries.forEach(entry => { | ||
const target = entry.target; | ||
if (this.observerLock.get(target)) { | ||
return this.observerLock.set(target, false); | ||
} | ||
const topTextEl = target.closest(".o_text_highlight"); | ||
for (const el of topTextEl ? [topTextEl] | ||
: target.querySelectorAll(":scope .o_text_highlight") | ||
) { | ||
textHighlightEls.add(el); | ||
} | ||
}); | ||
textHighlightEls.forEach(textHighlightEl => { | ||
for (const textHighlightItemEl of this.getTextHighlightItems(textHighlightEl)) { | ||
this.resizeObserver.unobserve(textHighlightItemEl); | ||
} | ||
switchTextHighlight(textHighlightEl); | ||
}); | ||
}); | ||
}); | ||
this.el.addEventListener("text_highlight_added", this.onTextHighlightAdded.bind(this)); | ||
this.el.addEventListener("text_highlight_remove", this.onTextHighlightRemoved.bind(this)); | ||
for (const textEl of this.el.querySelectorAll(".o_text_highlight")) { | ||
applyTextHighlight(textEl); | ||
} | ||
} | ||
|
||
destroy() { | ||
this.el.removeEventListener("text_highlight_added"); | ||
this.el.removeEventListener("text_highlight_remove"); | ||
for (const textHighlightEl of this.el.querySelectorAll(".o_text_highlight")) { | ||
removeTextHighlight(textHighlightEl); | ||
} | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} el | ||
*/ | ||
closestToObserve(el) { | ||
if (el === this.el || !el) { | ||
return null; | ||
} | ||
if (window.getComputedStyle(el).display !== "inline") { | ||
return el; | ||
} | ||
return this.closestToObserve(el.parentElement); | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} el | ||
*/ | ||
getTextHighlightItems(el = this.el) { | ||
return el.querySelectorAll(":scope .o_text_highlight_item"); | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} topTextEl | ||
*/ | ||
getObservedEls(topTextEl) { | ||
const closestToObserve = this.closestToObserve(topTextEl); | ||
return [ | ||
...(closestToObserve ? [closestToObserve] : []), | ||
...this.getTextHighlightItems(topTextEl), | ||
] | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} topTextEl | ||
* be observed. | ||
*/ | ||
observeTextHighlightResize(topTextEl) { | ||
// The `ResizeObserver` cannot detect the width change on highlight | ||
// units (`.o_text_highlight_item`) as long as the width of the entire | ||
// `.o_text_highlight` element remains the same, so we need to observe | ||
// each one of them and do the adjustment only once for the whole text. | ||
for (const highlightItemEl of this.getObservedEls(topTextEl)) { | ||
this.resizeObserver.observe(highlightItemEl); | ||
} | ||
} | ||
|
||
/** | ||
* @param {HTMLElement} topTextEl | ||
*/ | ||
lockTextHighlightObserver(topTextEl) { | ||
for (const targetEl of this.getObservedEls(topTextEl)) { | ||
this.observerLock.set(targetEl, true); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Event} ev | ||
*/ | ||
onTextHighlightAdded(ev) { | ||
this.lockTextHighlightObserver(ev.target); | ||
this.observeTextHighlightResize(ev.target); | ||
} | ||
|
||
/** | ||
* @param {Event} ev | ||
*/ | ||
onTextHighlightRemoved(ev) { | ||
for (const highlightItemEl of this.getTextHighlightItems(ev.target)) { | ||
this.observerLock.delete(highlightItemEl); | ||
} | ||
} | ||
} | ||
|
||
registry.category("website.active_elements").add("website.text_highlight", TextHighlight); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters