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

FIX: Add ability for category logos to switch #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions javascripts/discourse/initializers/color-scheme-toggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ export default {
const keyValueStore = container.lookup("service:key-value-store");
const storedOverride = keyValueStore.getItem(COLOR_SCHEME_OVERRIDE_KEY);

if (Session.currentProp("darkModeAvailable") && storedOverride) {
schedule("afterRender", () => {
// delay needed for logo override
colorSchemeOverride(storedOverride);
});
}

window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
Expand All @@ -39,6 +32,14 @@ export default {
if (settings.add_color_scheme_toggle_to_header) {
withPluginApi("0.8", (api) => {
api.addToHeaderIcons("header-toggle-button");
api.onPageChange(() => {
if (Session.currentProp("darkModeAvailable") && storedOverride) {
schedule("afterRender", () => {
// delay needed for logo override
colorSchemeOverride(storedOverride);
});
}
});
});
}
},
Expand Down
27 changes: 27 additions & 0 deletions javascripts/discourse/lib/color-scheme-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export function colorSchemeOverride(type) {
}

const logoDarkSrc = document.querySelector(".title picture source");
const categoryLogosDarkSrc = document.querySelectorAll(
".category-logo picture source"
);

switch (type) {
case "dark":
Expand All @@ -20,6 +23,14 @@ export function colorSchemeOverride(type) {
logoDarkSrc.origMedia = logoDarkSrc.media;
logoDarkSrc.media = "all";
}

if (categoryLogosDarkSrc) {
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
Comment on lines +28 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
for (const logo of categoryLogosDarkSrc) {

logo.origMedia = logo.media;
logo.media = "all";
}
}
break;
case "light":
lightScheme.origMedia = lightScheme.media;
Expand All @@ -30,6 +41,14 @@ export function colorSchemeOverride(type) {
logoDarkSrc.origMedia = logoDarkSrc.media;
logoDarkSrc.media = "none";
}

if (categoryLogosDarkSrc) {
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
Comment on lines +46 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
for (const logo of categoryLogosDarkSrc) {

logo.origMedia = logo.media;
logo.media = "none";
}
}
break;
default:
if (lightScheme.origMedia) {
Expand All @@ -43,6 +62,14 @@ export function colorSchemeOverride(type) {
if (logoDarkSrc?.origMedia) {
logoDarkSrc.media = logoDarkSrc.origMedia;
}
if (categoryLogosDarkSrc) {
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
if (logo?.origMedia) {
Comment on lines +66 to +68
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (let index = 0; index < categoryLogosDarkSrc.length; index++) {
let logo = categoryLogosDarkSrc[index];
if (logo?.origMedia) {
for (const logo of categoryLogosDarkSrc) {
if (logo?.origMedia) {

logo.media = logo.origMedia;
}
}
}
break;
}
}