-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: optimize comment plugin load logic
- Loading branch information
Showing
15 changed files
with
371 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div class="configuration-items-error-tip border-box"> | ||
<i class="fa-solid fa-circle-exclamation"></i> | ||
<%- __('comment.config_error', comment_plugin) %> | ||
</div> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<div class="comment-plugin-fail border-box"> | ||
<span class="fail-tip"><%- __("comment.fail_tip") %></span> | ||
<button class="reload keep-button"><%- __("comment.reload") %></button> | ||
</div> | ||
<div class="comment-plugin-loading flex-center border-box"> | ||
<i class="loading-icon fa-solid fa-spinner fa-spin"></i> | ||
<span class="load-tip"><%- __("comment.loading") %></span> | ||
</div> | ||
<script data-pjax> | ||
window.KeepCommentPlugin = {} | ||
window.KeepCommentPlugin.hideLoading = () => { | ||
const cplDom = document.querySelector('.comments-container .comment-plugin-loading') | ||
cplDom.style.display = 'none' | ||
} | ||
window.KeepCommentPlugin.loadFailHandle = () => { | ||
window.KeepCommentPlugin.hideLoading() | ||
const cpfDom = document.querySelector('.comments-container .comment-plugin-fail') | ||
cpfDom.style.display = 'flex' | ||
cpfDom.querySelector('.reload').addEventListener('click', () => { | ||
window.location.reload() | ||
}) | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<% | ||
const { enable: pjax_enable } = theme?.pjax || {} | ||
const { | ||
repo: giscus_repo, | ||
repo_id: giscus_repo_id, | ||
category: giscus_category, | ||
category_id: giscus_category_id, | ||
reactions_enabled: giscus_reactions_enabled, | ||
} = theme?.comment?.giscus || {} | ||
const giscus_language = config?.language || 'zh-CN' | ||
%> | ||
<% if (giscus_repo && giscus_repo_id && giscus_category && giscus_category_id) { %> | ||
<div class="giscus-comments-container"> | ||
<div class="giscus" id="giscus"></div> | ||
<script <%= pjax_enable === true ? 'data-pjax' : '' %> | ||
onerror="window.KeepCommentPlugin.loadFailHandle()" | ||
> | ||
if (!window.KeepCommentPlugin?.getGiscusTheme) { | ||
window.KeepCommentPlugin.getGiscusTheme = () => { | ||
return document.body.classList.contains('dark-mode') ? 'dark_dimmed' : 'light_tritanopia' | ||
} | ||
} | ||
if (!window.KeepCommentPlugin?.changeGiscusTheme) { | ||
window.KeepCommentPlugin.changeGiscusTheme = () => { | ||
const iframe = document.querySelector('iframe.giscus-frame') | ||
iframe && iframe.contentWindow.postMessage({ | ||
giscus: { | ||
setConfig: { | ||
theme: window.KeepCommentPlugin.getGiscusTheme() | ||
} | ||
} | ||
}, 'https://giscus.app') | ||
} | ||
} | ||
if (!window.KeepCommentPlugin?.initGiscus) { | ||
window.KeepCommentPlugin.initGiscus = () => { | ||
const script = document.createElement('script') | ||
script.async = true | ||
script.src = 'https://giscus.app/client.js' | ||
script.setAttribute('data-repo', '<%= giscus_repo %>') | ||
script.setAttribute('data-repo-id', '<%= giscus_repo_id %>') | ||
script.setAttribute('data-category', '<%= giscus_category %>') | ||
script.setAttribute('data-category-id', '<%= giscus_category_id %>') | ||
script.setAttribute('data-reactions-enabled', '<%= giscus_reactions_enabled === true ? '1' : '0' %>') | ||
script.setAttribute('data-lang', '<%= giscus_language %>') | ||
script.setAttribute('data-mapping', 'pathname') | ||
script.setAttribute('data-strict', '0') | ||
script.setAttribute('data-emit-metadata', '0') | ||
script.setAttribute('data-input-position', 'top') | ||
script.setAttribute('crossorigin', 'anonymous') | ||
script.setAttribute('loading', 'lazy') | ||
script.setAttribute('data-theme', window.KeepCommentPlugin.getGiscusTheme()) | ||
document.querySelector('.giscus-comments-container').appendChild(script) | ||
script.onerror = () => { | ||
window.KeepCommentPlugin.loadFailHandle() | ||
} | ||
script.onload = () => { | ||
window.KeepCommentPlugin.hideLoading() | ||
} | ||
const toggleThemeBtn = document.querySelector('.tool-dark-light-toggle') | ||
toggleThemeBtn && toggleThemeBtn.addEventListener('click', () => { | ||
window.KeepCommentPlugin.changeGiscusTheme() | ||
}) | ||
} | ||
} | ||
if ('<%= pjax_enable %>' === "true") { | ||
setTimeout(() => { | ||
window.KeepCommentPlugin.initGiscus() | ||
}, 1000) | ||
} else { | ||
window.addEventListener("DOMContentLoaded", window.KeepCommentPlugin.initGiscus) | ||
} | ||
</script> | ||
</div> | ||
<% } else { %> | ||
<%- partial('../config-error', { comment_plugin: 'Giscus' }) %> | ||
<% } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.