mirror of
https://github.com/shoelace-style/webawesome.git
synced 2026-01-12 04:09:12 +00:00
Fix remember scroll (#148)
* add data-turbo-track * add data-turbo-track * add debugging * add debugging * remove nopreview * move top level * move top level * move top level * remove data-turbo-track * add comment to base.njk * prettier
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import 'https://cdn.jsdelivr.net/npm/@hotwired/turbo@7.3.0/+esm';
|
||||
|
||||
if (!window.___turboScrollPositions___) {
|
||||
window.___turboScrollPositions___ = {};
|
||||
}
|
||||
@@ -7,11 +5,11 @@ if (!window.___turboScrollPositions___) {
|
||||
const positions = window.___turboScrollPositions___;
|
||||
|
||||
function saveScrollPosition() {
|
||||
document.querySelectorAll('[data-remember-scroll]').forEach(el => {
|
||||
if (el.id) {
|
||||
positions[el.id] = {
|
||||
top: el.scrollTop,
|
||||
left: el.scrollLeft
|
||||
document.querySelectorAll('[data-remember-scroll]').forEach(element => {
|
||||
if (element.id) {
|
||||
positions[element.id] = {
|
||||
top: element.scrollTop,
|
||||
left: element.scrollLeft
|
||||
};
|
||||
} else {
|
||||
console.warn(`Can't save scroll position for elements without an id.`, el);
|
||||
@@ -20,13 +18,28 @@ function saveScrollPosition() {
|
||||
}
|
||||
|
||||
function restoreScrollPosition(event) {
|
||||
const el = event.detail?.newBody || document;
|
||||
if (event.detail && event.detail.newBody) {
|
||||
event.detail.newBody.querySelectorAll('[data-remember-scroll]').forEach(element => {
|
||||
if (!positions[element.id]) {
|
||||
return;
|
||||
}
|
||||
|
||||
el.querySelectorAll('[data-remember-scroll]').forEach(el => {
|
||||
if (positions[el.id]) {
|
||||
el.scrollTop = positions[el.id].top;
|
||||
el.scrollLeft = positions[el.id].left;
|
||||
const { top, left } = positions[element.id];
|
||||
|
||||
element.scrollTop = top;
|
||||
element.scrollLeft = left;
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll('[data-remember-scroll]').forEach(element => {
|
||||
if (!positions[element.id]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { top, left } = positions[element.id];
|
||||
|
||||
element.scrollTop = top;
|
||||
element.scrollLeft = left;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user