fix(plugin/heti): make autoSpacing work (#77)

We modified heti.autoSpacing() to remove DOMContentLoaded
(DOM has been loaded in deferred block.)
This commit is contained in:
pan93412 2022-05-15 11:44:54 +08:00 committed by GitHub
parent 6b385f3224
commit 8ab2eb9f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -62,15 +62,6 @@ function og_args() {
<meta name="theme-color" content="#f8f8f8">
<title><%- generate_title() %></title>
<% if (theme.heti) { %>
<link rel="stylesheet" href="//unpkg.com/heti/umd/heti.min.css">
<script src="//unpkg.com/heti/umd/heti-addon.min.js"></script>
<script defer>
const heti = new Heti('.heti');
heti.autoSpacing(); // 自动进行中西文混排美化和标点挤压
</script>
<% } %>
<% if (theme.open_graph && theme.open_graph.enable) { %>
<%- open_graph(og_args()) %>
<% } %>

View File

@ -301,8 +301,22 @@ if (stellar.plugins.fancybox) {
if (stellar.plugins.heti) {
stellar.loadCSS(stellar.plugins.heti.css);
stellar.loadScript(stellar.plugins.heti.js, { defer: true }).then(function () {
const heti = new Heti('.heti p');
heti.autoSpacing();
const heti = new Heti('.heti');
// Copied from heti.autoSpacing() without DOMContentLoaded.
// https://github.com/sivan/heti/blob/eadee6a3b748b3b7924a9e7d5b395d4bce479c9a/js/heti-addon.js
//
// We managed to minimize the code modification to ensure .autoSpacing()
// is synced with upstream; therefore, we use `.bind()` to emulate the
// behavior of .autoSpacing() so we can even modify almost no code.
void (function () {
const $$rootList = document.querySelectorAll(this.rootSelector)
for (let $$root of $$rootList) {
this.spacingElement($$root)
}
}).bind(heti)();
stellar.plugins.heti.enable = false;
});
}