Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] giscus theme is different from FixIt theme when initialize #453

Closed
wants to merge 1 commit into from

Conversation

Bendancom
Copy link

giscus 初始化时因未修改其中 <script> 元素中的 data-theme 属性,导致其仍为默认的 preferred_color_scheme,会被自动推导至 dark 主题。
the attribute:data-theme in the <script> element isn't modified when the giscus initialize,which leads to the content of data-theme is also in default which is preferred_color_scheme and it automatically infer to dark theme.

Copy link

vercel bot commented Jun 22, 2024

@Bendancom is attempting to deploy a commit to the FixIt Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Author

@Bendancom Bendancom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a js script line to modify the data-theme when it is initialize

@@ -324,7 +324,9 @@
crossorigin="anonymous"
async
defer
></script>
>
document.querySelector("#giscus>script").setAttribute("data-theme",this.isDark ? "dark" : "light");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

似乎这段内联 js 不会被执行,因为它会有限执行 src 的 js 文件。
还有 this.isDark 是没有定义的变量,尽管 theme.js 里有定义,但是要等到 theme.js 加载执行完的实例才能拿到, this.fixit.isDark

最后就是,其实 theme.js 之前有做处理(第一次收到消息时

FixIt/assets/js/theme.js

Lines 853 to 868 in b998371

if (this.config.comment.giscus) {
const giscusConfig = this.config.comment.giscus;
this._giscusOnSwitchTheme = this._giscusOnSwitchTheme || (() => {
const message = { setConfig: { theme: this.isDark ? giscusConfig.darkTheme : giscusConfig.lightTheme }};
document.querySelector('.giscus-frame')?.contentWindow.postMessage({ giscus: message }, giscusConfig.origin);
});
this.switchThemeEventSet.add(this._giscusOnSwitchTheme);
this.giscus2parentMsg = window.addEventListener('message', (event) => {
const $script = document.querySelector('#giscus>script');
if ($script){
this._giscusOnSwitchTheme();
$script.parentElement.removeChild($script);
}
}, { once: true });
return;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有看到过,但是我这不加载,在第一次加载时该消息发送失败。
然后 hugo template 是直接替换,但 theme 应该是一个状态,可以先开一篇 light 主题文章,切换为 dark 主题后退回主页再开一篇文章。
this.isDark 确实是没定义,但实际测下来问题确实是解决了的,也不知为何。
如下是问题的体现。
屏幕截图 2024-06-22 174546

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我再测试一下

@Lruihao
Copy link
Member

Lruihao commented Jun 23, 2024

暂时无法复现,网络不太好的时候是无法切换的。

@Lruihao Lruihao closed this Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants