Skip to content

Commit

Permalink
Only add child element below the fold threshold and parent elements o…
Browse files Browse the repository at this point in the history
…nly below the fold
  • Loading branch information
jeawhanlee committed Aug 23, 2024
1 parent 502aaac commit 7dff5df
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/BeaconLrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BeaconLrc {
_getElementDistance(element) {
const rect = element.getBoundingClientRect();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return rect.top + scrollTop - (window.innerHeight || document.documentElement.clientHeight);
return Math.max(0, rect.top + scrollTop - (window.innerHeight || document.documentElement.clientHeight));
}

_skipElement(element) {
Expand All @@ -78,11 +78,12 @@ class BeaconLrc {
if (this._shouldSkipElement(element, this.config.exclusions || [])) {
return;
}
if ( 'No hash detected' !== hash ) {
this.lazyRenderElements.push( hash );

if ( 'No hash detected' === hash ) {
return;
}

const style = distance > 1800 ? 'color: green;' : distance === 0 ? 'color: red;' : '';
const style = (depth === 2 && distance >= 1800) || (element.parentElement && this._getElementDistance(element.parentElement) === 0 && distance >= 1800) ? "color: green;" : distance === 0 ? "color: red;" : "";
console.log(`%c${'\t'.repeat(depth)}${element.tagName} (Depth: ${depth}, Distance from viewport top: ${distance}px)`, style);

const xpath = this._getXPath(element);
Expand All @@ -91,6 +92,19 @@ class BeaconLrc {
console.log(`%c${'\t'.repeat(depth)}Location hash: ${hash}`, style);

console.log(`%c${'\t'.repeat(depth)}Dimensions Client Height: ${element.clientHeight}`, style);

// Check if the element is a parent at depth 2 with distance >= 1800
if (depth === 2 && distance >= 1800) {
this.lazyRenderElements.push(hash);
console.log(`Parent element at depth 2 with distance >= 1800 pushed with hash: ${hash}`);
return;
}

// Check parent element with distance of 0 but children is within threshold.
if (element.parentElement && this._getElementDistance(element.parentElement) === 0 && distance >= 1800) {
this.lazyRenderElements.push(hash); // Push the child's hash
console.log(`Child element pushed with hash: ${hash}`);
}
});
}

Expand Down

0 comments on commit 7dff5df

Please sign in to comment.