Skip to content

Commit

Permalink
feat: 评论区视口懒加载 (xaoxuu#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX authored May 31, 2024
1 parent e60e9f4 commit b211c82
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 95 deletions.
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ search:
comments:
service: # beaudar, utterances, giscus, twikoo, waline, artalk
comment_title: 快来参与讨论吧~
lazyload: false # true / false
# beaudar
# https://beaudar.lipk.org/
beaudar:
Expand Down
17 changes: 8 additions & 9 deletions layout/_partial/comments/artalk/script.ejs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<script>
<script type="module">
const el = document.getElementById('artalk_container');
util.viewportLazyload(el, load_artalk, <%= theme.comments.lazyload ?? false %>);
function load_artalk() {
if (!document.querySelectorAll("#artalk_container")[0]) return;
if (!el) return;
utils.css('<%- theme.comments.artalk.css %>');
utils.js('<%- theme.comments.artalk.js %>', {defer: true}).then(function () {
const el = document.getElementById("artalk_container");
var path = el.getAttribute('comment_id');
const path = el.getAttribute('comment_id');
if (!path) {
path = decodeURI(window.location.pathname);
}
var artalk = Artalk.init({
const artalk = Artalk.init({
el: '#artalk_container',
pageKey: path,
pageTitle: '<%= page.title %>',
server: '<%= theme.comments.artalk.server %>',
placeholder: '<%= theme.comments.artalk.placeholder %>',
site: "<%- config.title %>",
darkMode: '<%= theme.comments.artalk.darkMode %>'
})
});
});
}
window.addEventListener('DOMContentLoaded', (event) => {
load_artalk();
});
</script>
43 changes: 20 additions & 23 deletions layout/_partial/comments/beaudar/script.ejs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
<script>
function loadBeaudar() {
const els = document.querySelectorAll("#comments #beaudar");
if (els.length === 0) return;
els.forEach((el, i) => {
try {
el.innerHTML = '';
} catch (error) {
console.error(error);
<script type="module">
const el = document.querySelector('#comments #beaudar');
util.viewportLazyload(el, load_beaudar, <%= theme.comments.lazyload ?? false %>);
function load_beaudar() {
if (!el) return;
try {
el.innerHTML = '';
} catch (error) {
console.error(error);
}
const script = document.createElement('script');
script.src = 'https://beaudar.lipk.org/client.js';
script.async = true;
for (const key of Object.keys(el.attributes)) {
const attr = el.attributes[key];
if (['class', 'id'].includes(attr.name) === false) {
script.setAttribute(attr.name, attr.value);
}
var script = document.createElement('script');
script.src = 'https://beaudar.lipk.org/client.js';
script.async = true;
for (let key of Object.keys(el.attributes)) {
let attr = el.attributes[key];
if (['class', 'id'].includes(attr.name) === false) {
script.setAttribute(attr.name, attr.value);
}
}
el.appendChild(script);
});
}
el.appendChild(script);
}
window.addEventListener('DOMContentLoaded', (event) => {
loadBeaudar();
});
</script>
22 changes: 11 additions & 11 deletions layout/_partial/comments/giscus/script.ejs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<script>
window.addEventListener('DOMContentLoaded', (event) => {
const els = document.querySelectorAll("#comments #giscus");
if (els.length === 0) return;
els.forEach((el, i) => {
try {
<script type="module">
const el = document.querySelector('#comments #giscus');
util.viewportLazyload(el, load_discus, <%= theme.comments.lazyload ?? false %>);
function load_discus() {
if (!el) return;
try {
el.innerHTML = '';
} catch (error) {
console.error(error);
}
var script = document.createElement('script');
const script = document.createElement('script');
script.async = true;
for (let key of Object.keys(el.attributes)) {
let attr = el.attributes[key];
for (const key of Object.keys(el.attributes)) {
const attr = el.attributes[key];
if (['class', 'id'].includes(attr.name) === false) {
script.setAttribute(attr.name, attr.value);
}
}
el.appendChild(script);
});
});
}
</script>
32 changes: 15 additions & 17 deletions layout/_partial/comments/twikoo/script.ejs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<script>
function load_twikoo() {
if (!document.querySelectorAll("#twikoo_container")[0]) return;
utils.js('<%- theme.comments.twikoo.js %>', {defer: true}).then(function () {
const el = document.getElementById("twikoo_container");
var path = el.getAttribute('comment_id');
if (!path) {
path = decodeURI(window.location.pathname);
}
twikoo.init(Object.assign(<%- JSON.stringify(theme.comments.twikoo) %>, {
el: '#twikoo_container',
path: path,
}));
});
}
<script type="module">
const el = document.getElementById('artalk_container');
util.viewportLazyload(el, load_twikoo, <%= theme.comments.lazyload ?? false %>);
window.addEventListener('DOMContentLoaded', (event) => {
load_twikoo();
function load_twikoo() {
if (!el) return;
utils.js('<%- theme.comments.twikoo.js %>', {defer: true}).then(function () {
const path = el.getAttribute('comment_id');
if (!path) {
path = decodeURI(window.location.pathname);
}
twikoo.init(Object.assign(<%- JSON.stringify(theme.comments.twikoo) %>, {
el: '#twikoo_container',
path: path,
}));
});
}
</script>
43 changes: 20 additions & 23 deletions layout/_partial/comments/utterances/script.ejs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
<script>
function loadUtterances() {
const els = document.querySelectorAll("#comments #utterances");
if (els.length === 0) return;
els.forEach((el, i) => {
try {
el.innerHTML = '';
} catch (error) {
console.error(error);
<script type="module">
const el = document.querySelector("#comments #utterances");
util.viewportLazyload(el, load_utterances, <%= theme.comments.lazyload ?? false %>);
function load_utterances() {
if (!el) return;
try {
el.innerHTML = '';
} catch (error) {
console.error(error);
}
const script = document.createElement('script');
script.src = 'https://utteranc.es/client.js';
script.async = true;
for (const key of Object.keys(el.attributes)) {
const attr = el.attributes[key];
if (['class', 'id'].includes(attr.name) === false) {
script.setAttribute(attr.name, attr.value);
}
var script = document.createElement('script');
script.src = 'https://utteranc.es/client.js';
script.async = true;
for (let key of Object.keys(el.attributes)) {
let attr = el.attributes[key];
if (['class', 'id'].includes(attr.name) === false) {
script.setAttribute(attr.name, attr.value);
}
}
el.appendChild(script);
});
}
el.appendChild(script);
}
window.addEventListener('DOMContentLoaded', (event) => {
loadUtterances();
});
</script>
21 changes: 9 additions & 12 deletions layout/_partial/comments/waline/script.ejs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<script type="module">
import { init } from '<%- theme.comments.waline.js %>'
import { init } from '<%- theme.comments.waline.js %>';
function load_comment(){
if(!document.getElementById("waline_container"))return;
const el = document.getElementById('waline_container');
util.viewportLazyload(el, load_waline, <%= theme.comments.lazyload ?? false %>);
function load_waline(){
if (!el) return;
utils.css('<%- theme.comments.waline.css %>');
utils.css('<%- theme.comments.waline.meta_css %>');
const el = document.getElementById("waline_container");
var path = el.getAttribute('comment_id');
const path = el.getAttribute('comment_id');
if (!path) {
path = decodeURI(window.location.pathname);
}
Expand All @@ -18,12 +20,12 @@
path: path,
<% if(!!theme.comments.waline.imageUploader?.api){ %>
imageUploader: function(file) {
let headers = new Headers();
const headers = new Headers();
headers.set('Accept', 'application/json');
<% if(!!theme.comments.waline.imageUploader?.token) { %>
headers.set('<%= theme.comments.waline.imageUploader?.tokenName %>', '<%= theme.comments.waline.imageUploader?.token %>')
<% } %>
let formData = new FormData();
const formData = new FormData();
formData.append('<%= theme.comments.waline.imageUploader?.fileName %>', file);
return fetch('<%= theme.comments.waline.imageUploader?.api %>',{
method: 'POST',
Expand All @@ -34,10 +36,5 @@
},
<% } %>
}));
}
window.addEventListener('DOMContentLoaded', (event) => {
load_comment();
});
</script>
14 changes: 14 additions & 0 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ const util = {
scrollComment: () => {
document.getElementById('comments').scrollIntoView({behavior: "smooth"});
},

viewportLazyload: (target, func, enabled = true) => {
if (!enabled || !("IntersectionObserver" in window)) {
func();
return;
}
const observer = new IntersectionObserver((entries) => {
if (entries[0].intersectionRatio > 0) {
func();
observer.disconnect();
}
});
observer.observe(target);
}
}

const hud = {
Expand Down

0 comments on commit b211c82

Please sign in to comment.