feat(ipub): make ipub-background containerized into ipub-body

This commit is contained in:
Guz
2025-10-23 15:00:32 -03:00
parent 3bede11393
commit b46ed80a00

View File

@@ -28,49 +28,84 @@ class IPUBBackground extends IPUBElement {
/** /**
* @private * @private
*/ */
static #observer = (() => { static #instancesOnScreen = {
/** @type {Map<string, IPUBBackground>} */ /** @type {Map<IPUBBody, Set<IPUBBackground>>} */
const instancesOnScreen = new Map(); map: new Map(),
/**
document.addEventListener("scroll", () => { * @param {IPUBBackground} background
for (const [_, instance] of instancesOnScreen) { */
const perc = getPercentageInView( add(background) {
instance.querySelector("img") || instance, const body = getAncestor(background, IPUBBody.elementName);
if (!body) {
console.error(
`IPUBBackground: ${background.id} does not have a valid ipub-body ancestor`,
); );
instance.fade(perc); return;
} }
});
return new IntersectionObserver((entries) => let set = this.map.get(body);
entries.forEach((e) => { if (!set) {
let instance = e.target.parentElement; set = new Set();
body.addEventListener("scroll", () => {
for (const instance of set.values()) {
const perc = getPercentageInView(
instance.querySelector("img") || instance,
);
instance.fade(perc);
}
});
}
set.add(background);
this.map.set(body, set);
},
/**
* @param {IPUBBackground} background
*/
remove(background) {
const body = getAncestor(background, IPUBBody.elementName);
if (!body) {
console.error(
`IPUBBackground: ${background.id} does not have a valid ipub-body ancestor`,
);
return;
}
if ( const set = this.map.get(body);
instance.tagName.toLowerCase() !== if (!set) {
IPUBBackground.elementName.toLowerCase() return;
) { }
instance = instance.parentElement;
}
if ( set.delete(background);
instance.tagName.toLowerCase() !==
IPUBBackground.elementName.toLowerCase()
) {
console.error(
"IPUBBackground: malformed <ipub-background> element",
e.target,
);
return;
}
if (e.intersectionRatio > 0 && instance.id) { if (set.size === 0) {
instancesOnScreen.set(instance.id, instance); this.map.delete(body);
} else if (instance.id) { }
instancesOnScreen.delete(instance.id); },
} };
}), static addToScreen() {}
);
})(); /**
* @private
*/
static #observer = new IntersectionObserver((entries) => {
for (const { intersectionRatio, target: image } of entries) {
const instance = getAncestor(image, IPUBBackground.elementName);
if (!instance) {
console.error(
"IPUBBackground: malformed <ipub-background> element",
image,
);
return;
}
if (intersectionRatio > 0 && instance.id) {
IPUBBackground.#instancesOnScreen.add(instance);
} else if (instance.id) {
IPUBBackground.#instancesOnScreen.remove(instance);
}
}
});
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
@@ -126,8 +161,6 @@ class IPUBBackground extends IPUBElement {
* @returns {void | Promise<void>} * @returns {void | Promise<void>}
*/ */
fade(perc) { fade(perc) {
console.debug(`IPUBBackground: ${this.id} is ${perc} on screen`);
if (!this.style.getPropertyValue("--ipub-fade")) { if (!this.style.getPropertyValue("--ipub-fade")) {
this.style.setProperty("--ipub-fade", `${perc}%`); this.style.setProperty("--ipub-fade", `${perc}%`);
return; return;