refractor code

This commit is contained in:
FarseaSH 2023-04-04 22:24:43 +08:00
parent d64d41e7e6
commit e55815f3d2

View file

@ -53,59 +53,59 @@
{{ if .Site.Params.infiniteScroll }} {{ if .Site.Params.infiniteScroll }}
<script> <script>
var throttle_pause = false; var throttle_pause = false;
var fetching = false; var fetching = false;
var page_number = {{ $.Paginator.PageNumber }}; var page_number = {{ $.Paginator.PageNumber }};
const total_pages = {{ $.Paginator.TotalPages }}; const total_pages = {{ $.Paginator.TotalPages }};
const throttle = (func, timeout) => { const throttle = (func, timeout) => {
if (throttle_pause || fetching) return; if (throttle_pause || fetching) return;
throttle_pause = true; throttle_pause = true;
setTimeout(() => { setTimeout(() => {
func(); func();
throttle_pause = false; throttle_pause = false;
}, timeout); }, timeout);
}; };
const loading_wrapper = document.querySelector(".loading-wrapper"); const loading_wrapper = document.querySelector(".loading-wrapper");
document.addEventListener("scroll", () => { document.addEventListener("scroll", () => {
throttle(() => { throttle(() => {
const last_moment = document.querySelector(".page > .container .moment-row:last-child"); const last_moment = document.querySelector(".page > .container .moment-row:last-child");
const page_end = window.innerHeight + window.pageYOffset + 100 + last_moment.offsetHeight >= document.body.offsetHeight; const page_end = window.innerHeight + window.pageYOffset + 100 + last_moment.offsetHeight >= document.body.offsetHeight;
if (page_end && page_number < total_pages) { if (page_end && page_number < total_pages) {
fetching = true; fetching = true;
loading_wrapper.style.display = "flex"; loading_wrapper.style.display = "flex";
fetch(`/${++page_number}`).then((response) => { fetch(`/${++page_number}`).then((response) => {
return response.text(); return response.text();
}).then((html_string) => { }).then((html_string) => {
const parser = new DOMParser(); const parser = new DOMParser();
const next_page = parser.parseFromString(html_string, "text/html"); const next_page = parser.parseFromString(html_string, "text/html");
{{ if .Site.Params.heti }} {{ if .Site.Params.heti }}
const heti = new Heti(next_page); const heti = new Heti(next_page);
heti.autoSpacing(); heti.autoSpacing();
{{ end }} {{ end }}
const rows = next_page.querySelectorAll(".moment-row"); const rows = next_page.querySelectorAll(".moment-row");
const container = document.querySelector(".page > .container"); const container = document.querySelector(".page > .container");
rows.forEach((r) => { rows.forEach((r) => {
container.appendChild(r); container.appendChild(r);
});
}).catch((err) => {
console.log(err);
}).then(() => {
fetching = false;
loading_wrapper.style.display = "none";
}); });
}).catch((err) => { }
console.log(err); }, 250);
}).then(() => { })
fetching = false;
loading_wrapper.style.display = "none";
});
}
}, 250);
})
</script> </script>
{{ end }} {{ end }}
{{ if .Site.Params.heti }} {{ if .Site.Params.heti }}
<script> <script>
const heti = new Heti(document); const heti = new Heti(document);
heti.autoSpacing(); heti.autoSpacing();
</script> </script>
{{ end }} {{ end }}