@searchspring/snap-controller
Advanced tools
Comparing version 0.44.3 to 0.45.0
@@ -487,8 +487,6 @@ "use strict"; | ||
return new Promise(function (resolve) { return __awaiter(_this, void 0, void 0, function () { | ||
var offset, maxCheckTime, checkTime, maxScrolls, maxCheckCount, scrollBackCount, checkCount, scrolledElem, checkAndScroll; | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
var maxCheckTime, checkTime, maxScrolls, maxCheckCount, scrollBackCount, checkCount, scrolledElem, checkAndScroll; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
offset = ((_a = element === null || element === void 0 ? void 0 : element.domRect) === null || _a === void 0 ? void 0 : _a.top) || 0; | ||
maxCheckTime = 500; | ||
@@ -502,29 +500,34 @@ checkTime = 50; | ||
checkAndScroll = function () { | ||
var _a; | ||
var offset = ((_a = element === null || element === void 0 ? void 0 : element.domRect) === null || _a === void 0 ? void 0 : _a.top) || 0; | ||
var elem = document.querySelector(element === null || element === void 0 ? void 0 : element.selector); | ||
if (elem) { | ||
scrollBackCount++; | ||
// for case where the element clicked on has no height | ||
while (elem && !elem.getBoundingClientRect().height) { | ||
elem = elem.parentElement; | ||
// original offset no longer applies since using different element | ||
offset = 0; | ||
} | ||
else { | ||
checkCount++; | ||
} | ||
if (elem) { | ||
var y = elem.getBoundingClientRect().y; | ||
scrollBackCount++; | ||
// if the offset is off, we need to scroll into position (can be caused by lazy loaded images) | ||
if (y > offset + 1 || y < offset - 1) { | ||
elem.scrollIntoView(); | ||
// after scrolling into view, use top value with offset (for when element at bottom) | ||
var top_1 = elem.getBoundingClientRect().top; | ||
window.scrollBy(0, -(offset - top_1)); | ||
window.scrollBy(0, y - offset); | ||
} | ||
else { | ||
// don't need to scroll - it is right where we want it | ||
scrolledElem = elem; | ||
return true; | ||
} | ||
} | ||
return false; | ||
else { | ||
checkCount++; | ||
} | ||
return true; | ||
}; | ||
_b.label = 1; | ||
_a.label = 1; | ||
case 1: | ||
if (!(checkAndScroll() || (scrollBackCount <= maxScrolls && checkCount <= maxCheckCount))) return [3 /*break*/, 3]; | ||
if (!(checkAndScroll() && scrollBackCount <= maxScrolls && checkCount <= maxCheckCount)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, checkTime); })]; | ||
case 2: | ||
_b.sent(); | ||
_a.sent(); | ||
return [3 /*break*/, 1]; | ||
@@ -536,3 +539,3 @@ case 3: | ||
else { | ||
controller.log.debug('could not locate element with selector: ', element === null || element === void 0 ? void 0 : element.selector); | ||
controller.log.debug('attempted to scroll back to element with selector: ', element === null || element === void 0 ? void 0 : element.selector); | ||
} | ||
@@ -539,0 +542,0 @@ resolve(); |
@@ -337,3 +337,2 @@ import deepmerge from 'deepmerge'; | ||
return new Promise(async (resolve) => { | ||
const offset = element?.domRect?.top || 0; | ||
const maxCheckTime = 500; | ||
@@ -347,24 +346,28 @@ const checkTime = 50; | ||
const checkAndScroll = () => { | ||
const elem = document.querySelector(element?.selector); | ||
if (elem) { | ||
scrollBackCount++; | ||
let offset = element?.domRect?.top || 0; | ||
let elem = document.querySelector(element?.selector); | ||
// for case where the element clicked on has no height | ||
while (elem && !elem.getBoundingClientRect().height) { | ||
elem = elem.parentElement; | ||
// original offset no longer applies since using different element | ||
offset = 0; | ||
} | ||
else { | ||
checkCount++; | ||
} | ||
if (elem) { | ||
const { y } = elem.getBoundingClientRect(); | ||
scrollBackCount++; | ||
// if the offset is off, we need to scroll into position (can be caused by lazy loaded images) | ||
if (y > offset + 1 || y < offset - 1) { | ||
elem.scrollIntoView(); | ||
// after scrolling into view, use top value with offset (for when element at bottom) | ||
const { top } = elem.getBoundingClientRect(); | ||
window.scrollBy(0, -(offset - top)); | ||
window.scrollBy(0, y - offset); | ||
} | ||
else { | ||
// don't need to scroll - it is right where we want it | ||
scrolledElem = elem; | ||
return true; | ||
} | ||
} | ||
return false; | ||
else { | ||
checkCount++; | ||
} | ||
return true; | ||
}; | ||
while (checkAndScroll() || (scrollBackCount <= maxScrolls && checkCount <= maxCheckCount)) { | ||
while (checkAndScroll() && scrollBackCount <= maxScrolls && checkCount <= maxCheckCount) { | ||
await new Promise((resolve) => setTimeout(resolve, checkTime)); | ||
@@ -376,3 +379,3 @@ } | ||
else { | ||
controller.log.debug('could not locate element with selector: ', element?.selector); | ||
controller.log.debug('attempted to scroll back to element with selector: ', element?.selector); | ||
} | ||
@@ -379,0 +382,0 @@ resolve(); |
{ | ||
"name": "@searchspring/snap-controller", | ||
"version": "0.44.3", | ||
"version": "0.45.0", | ||
"description": "Snap Controllers", | ||
@@ -23,3 +23,3 @@ "main": "dist/cjs/index.js", | ||
"dependencies": { | ||
"@searchspring/snap-toolbox": "^0.44.3", | ||
"@searchspring/snap-toolbox": "^0.45.0", | ||
"css.escape": "1.5.1", | ||
@@ -29,9 +29,9 @@ "deepmerge": "4.3.1" | ||
"devDependencies": { | ||
"@searchspring/snap-client": "^0.44.3", | ||
"@searchspring/snap-event-manager": "^0.44.3", | ||
"@searchspring/snap-logger": "^0.44.3", | ||
"@searchspring/snap-profiler": "^0.44.3", | ||
"@searchspring/snap-store-mobx": "^0.44.3", | ||
"@searchspring/snap-tracker": "^0.44.3", | ||
"@searchspring/snap-url-manager": "^0.44.3" | ||
"@searchspring/snap-client": "^0.45.0", | ||
"@searchspring/snap-event-manager": "^0.45.0", | ||
"@searchspring/snap-logger": "^0.45.0", | ||
"@searchspring/snap-profiler": "^0.45.0", | ||
"@searchspring/snap-store-mobx": "^0.45.0", | ||
"@searchspring/snap-tracker": "^0.45.0", | ||
"@searchspring/snap-url-manager": "^0.45.0" | ||
}, | ||
@@ -42,3 +42,3 @@ "sideEffects": false, | ||
], | ||
"gitHead": "37ec16f63f5ba375d4cfbbddd32b8dbbd38deaa6" | ||
"gitHead": "e17a2adcdce980dfbd0ed7ad6e19228b1f5a2871" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
298750
5268
+ Added@searchspring/snap-toolbox@0.45.1(transitive)
- Removed@searchspring/snap-toolbox@0.44.3(transitive)