Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider viewport on element distance calculation #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/BeaconLrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

import BeaconUtils from "./Utils.js";
import BeaconManager from "./BeaconManager.js";

class BeaconLrc {
constructor(config, logger) {
Expand Down Expand Up @@ -52,7 +51,7 @@ class BeaconLrc {
_getElementDistance(element) {
const rect = element.getBoundingClientRect();
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return Math.max(0, rect.top + scrollTop);
return Math.max(0, rect.top + scrollTop - BeaconUtils.getScreenHeight());
}

_skipElement(element) {
Expand Down Expand Up @@ -83,10 +82,10 @@ class BeaconLrc {
return;
}

const can_push_hash = element.parentElement && this._getElementDistance(element.parentElement) < this.config.lrc_threshold && distance > this.config.lrc_threshold;
const can_push_hash = element.parentElement && this._getElementDistance(element.parentElement) < this.config.lrc_threshold && distance >= this.config.lrc_threshold;

const color = can_push_hash ? "green" : distance === 0 ? "red" : "";
this.logger.logColoredMessage( `${'\t'.repeat(depth)}${element.tagName} (Depth: ${depth}, Distance from viewport top: ${distance}px)`, color );
this.logger.logColoredMessage( `${'\t'.repeat(depth)}${element.tagName} (Depth: ${depth}, Distance from viewport bottom: ${distance}px)`, color );

//const xpath = this._getXPath(element);
//console.log(`%c${'\t'.repeat(depth)}Xpath: ${xpath}`, style);
Expand Down
12 changes: 10 additions & 2 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
'use strict';

class BeaconUtils {
static getScreenWidth() {
return window.innerWidth || document.documentElement.clientWidth;
}

static getScreenHeight() {
return window.innerHeight || document.documentElement.clientHeight;
}

static isNotValidScreensize( is_mobile, threshold ) {
const screenWidth = window.innerWidth || document.documentElement.clientWidth;
const screenHeight = window.innerHeight || document.documentElement.clientHeight;
const screenWidth = this.getScreenWidth();
const screenHeight = this.getScreenHeight();

const isNotValidForMobile = is_mobile &&
(screenWidth > threshold.width || screenHeight > threshold.height);
Expand Down
Loading