Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/resources/formats/html/giscus/giscus.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,25 @@
<%- giscus.loading ? `data-loading=${giscus.loading}` : '' %>
async>
</script>
<script type="application/javascript">
const giscusIframeObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
mutation.addedNodes.forEach(function (addedNode) {
if (addedNode.matches && addedNode.matches('div.giscus')) {
const giscusIframe = addedNode.querySelector('iframe.giscus-frame');
if(giscusIframe) {
giscusIframe.addEventListener("load", function() {
window.setTimeout(() => {
toggleGiscusIfUsed(hasAlternateSentinel(), authorPrefersDark);
}, 100);
});
giscusIframeObserver.disconnect();
}
}
});
});
});
giscusIframeObserver.observe(document.body, { childList: true, subtree: true });
</script>
<input type="hidden" id="giscus-base-theme" value="<%- giscus.baseTheme %>">
<input type="hidden" id="giscus-alt-theme" value="<%- giscus.altTheme %>">
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

window.document.body.appendChild(a);
}
window.setColorSchemeToggle(window.hasAlternateSentinel())
setColorSchemeToggle(hasAlternateSentinel())

<% } %>

Expand Down
15 changes: 8 additions & 7 deletions src/resources/formats/html/templates/quarto-html-before-body.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
}

window.setColorSchemeToggle = (alternate) => {
const setColorSchemeToggle = (alternate) => {
const toggles = window.document.querySelectorAll('.quarto-color-scheme-toggle');
for (let i=0; i < toggles.length; i++) {
const toggle = toggles[i];
Expand Down Expand Up @@ -56,7 +56,7 @@
manageTransitions('#quarto-margin-sidebar .nav-link', true);

// Switch the toggles
window.setColorSchemeToggle(alternate)
setColorSchemeToggle(alternate)

// Hack to workaround the fact that safari doesn't
// properly recolor the scrollbar when toggling (#1455)
Expand Down Expand Up @@ -103,7 +103,7 @@
return window.location.protocol === 'file:';
}

window.hasAlternateSentinel = () => {
const hasAlternateSentinel = () => {
let styleSentinel = getColorSchemeSentinel();
if (styleSentinel !== null) {
return styleSentinel === "alternate";
Expand Down Expand Up @@ -136,7 +136,7 @@

let newTheme = '';

if(darkModeDefault) {
if(authorPrefersDark) {
newTheme = isAlternate ? baseTheme : alternateTheme;
} else {
newTheme = isAlternate ? alternateTheme : baseTheme;
Expand Down Expand Up @@ -164,11 +164,12 @@
}
};

const authorPrefersDark = <%= darkModeDefault %>;
<% if (respectUserColorScheme) { %>
const queryPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
const darkModeDefault = queryPrefersDark.matches;
<% } else { %>
const darkModeDefault = <%= darkModeDefault %>;
const darkModeDefault = authorPrefersDark;
<% } %>

<% if (!darkModeDefault) { %>
Expand All @@ -181,7 +182,7 @@
// Dark / light mode switch
window.quartoToggleColorScheme = () => {
// Read the current dark / light value
let toAlternate = !window.hasAlternateSentinel();
let toAlternate = !hasAlternateSentinel();
toggleColorMode(toAlternate);
setStyleSentinel(toAlternate);
toggleGiscusIfUsed(toAlternate, darkModeDefault);
Expand All @@ -199,7 +200,7 @@
<% } %>

// Switch to dark mode if need be
if (window.hasAlternateSentinel()) {
if (hasAlternateSentinel()) {
toggleColorMode(true);
} else {
toggleColorMode(false);
Expand Down
Loading