A platform for community discussion. Free, open, simple.
FIX: apply color palette change when preview is unavailable, warn for dark mode (#33827)
Kris6223cbc4c6de
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.jsindex fdb0126cecb..8779dd8fb66 100644--- a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js+++ b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js@@ -17,6 +17,7 @@ export default class AdminCustomizeColorsController extends Controller {@service toasts;@service session;@service site;+ @service siteSettings;@tracked defaultTheme = null;@tracked filterValue = "";@@ -32,7 +33,13 @@ export default class AdminCustomizeColorsController extends Controller {_sortedOnce = false;get canPreviewColorScheme() {- return currentThemeId() === this.defaultTheme?.id;+ const usingDefaultTheme = currentThemeId() === this.defaultTheme?.id;+ const usingDefaultLightScheme =+ this._initialUserColorSchemeId === this._initialDefaultThemeColorSchemeId;++ return (+ usingDefaultTheme && usingDefaultLightScheme && !this.isUsingDarkMode+ );}get allBaseColorSchemes() {@@ -57,6 +64,15 @@ export default class AdminCustomizeColorsController extends Controller {return changedColors || changedTheme;}+ get isUsingDarkMode() {+ // check if user has dark mode available and is using it+ return (+ this.session.darkModeAvailable &&+ this.session.userDarkSchemeId !== -1 &&+ window.matchMedia("(prefers-color-scheme: dark)").matches+ );+ }+get filteredColorSchemes() {// only sort initially, this avoids position jumps when state changes on interactionif (!this._sortedOnce && this.model?.length > 0) {@@ -96,7 +112,7 @@ export default class AdminCustomizeColorsController extends Controller {if (lightBaseScheme) {const builtInDefault = {...lightBaseScheme,- id: 0,+ id: null,name: i18n("admin.customize.theme.default_light_scheme"),description: i18n("admin.customize.theme.default_light_scheme"),is_builtin_default: true,@@ -195,10 +211,19 @@ export default class AdminCustomizeColorsController extends Controller {@actionasync setAsDefaultThemePalette(scheme) {try {- if (this.canPreviewColorScheme) {- this.defaultTheme = await setDefaultColorScheme(scheme, this.store);+ let previewMode;+ if (scheme.is_builtin_default) {+ previewMode = "reload";+ } else if (this.canPreviewColorScheme) {+ previewMode = "live";+ } else {+ previewMode = "none";}+ this.defaultTheme = await setDefaultColorScheme(scheme, this.store, {+ previewMode,+ });+if (!this.canPreviewColorScheme) {const schemeName = scheme.description || scheme.name;const themeName = this.defaultTheme.name;diff --git a/app/assets/javascripts/admin/addon/lib/color-scheme-manager.js b/app/assets/javascripts/admin/addon/lib/color-scheme-manager.jsindex 2575e7a7681..2920282fb46 100644--- a/app/assets/javascripts/admin/addon/lib/color-scheme-manager.js+++ b/app/assets/javascripts/admin/addon/lib/color-scheme-manager.js@@ -1,23 +1,5 @@import { ajax } from "discourse/lib/ajax";-/**- * determine if a scheme is the built-in default- * @param {Object} scheme- * @returns {boolean} true if built-in default scheme- */-function isBuiltInDefault(scheme) {- return !scheme || scheme?.is_builtin_default;-}--/**- * get the scheme ID for API calls- * @param {Object} scheme- * @returns {number|null}- */-function getSchemeId(scheme) {- return isBuiltInDefault(scheme) ? null : scheme?.id;-}-/*** apply color scheme by updating stylesheet links*@@ -36,8 +18,7 @@ export async function applyColorScheme(scheme, options = {}) {await scheme.save({ forceSave: true });}- const id = getSchemeId(scheme);- const isBuiltIn = isBuiltInDefault(scheme);+ const id = scheme?.id;let existingTags = [];if (id) {@@ -101,7 +82,7 @@ export async function applyColorScheme(scheme, options = {}) {if (replace && id) {lightTag.setAttribute("data-scheme-id", id);- } else if (replace && isBuiltIn) {+ } else if (replace && !id) {lightTag.removeAttribute("data-scheme-id");}}@@ -111,7 +92,7 @@ export async function applyColorScheme(scheme, options = {}) {if (replace && id) {darkTag.setAttribute("data-scheme-id", id);- } else if (replace && isBuiltIn) {+ } else if (replace && !id) {darkTag.removeAttribute("data-scheme-id");}}@@ -129,19 +110,34 @@ export async function applyColorScheme(scheme, options = {}) {** @param {Object} scheme - color scheme to set as default* @param {Object} options- * @param {boolean} options.reload - reload the page after setting? (default: false)+ * @param {string} options.previewMode - preview mode: "live", "none", or "reload" (default: auto-detect)* @returns {Promise}*/export async function setDefaultColorScheme(scheme, store, options = {}) {- const { reload = false } = options;+ const { previewMode = "live" } = options;try {- // can't live preview if isBuiltInDefault- const isBuiltIn = isBuiltInDefault(scheme);- const shouldReload = reload || isBuiltIn;+ // Determine preview behavior+ let shouldPreview = false;+ let shouldReload = false;++ switch (previewMode) {+ case "live":+ shouldPreview = true;+ shouldReload = false;+ break;+ case "none":+ shouldPreview = false;+ shouldReload = false;+ break;+ case "reload":+ shouldPreview = false;+ shouldReload = true;+ break;+ }- if (!shouldReload) {+ if (shouldPreview) {await applyColorScheme(scheme, { replace: true });}@@ -152,7 +148,7 @@ export async function setDefaultColorScheme(scheme, store, options = {}) {throw new Error("Could not find default theme");}- const schemeId = getSchemeId(scheme);+ const schemeId = scheme?.id || null;defaultTheme.set("color_scheme_id", schemeId);await defaultTheme.saveChanges("color_scheme_id");diff --git a/app/assets/javascripts/admin/addon/templates/customize-colors.gjs b/app/assets/javascripts/admin/addon/templates/customize-colors.gjsindex b86e1e42544..f5cbf86b1d5 100644--- a/app/assets/javascripts/admin/addon/templates/customize-colors.gjs+++ b/app/assets/javascripts/admin/addon/templates/customize-colors.gjs@@ -8,6 +8,7 @@ import DPageSubheader from "discourse/components/d-page-subheader";import DSelect from "discourse/components/d-select";import FilterInput from "discourse/components/filter-input";import PluginOutlet from "discourse/components/plugin-outlet";+import getUrl from "discourse/helpers/get-url";import lazyHash from "discourse/helpers/lazy-hash";import { i18n } from "discourse-i18n";import ColorPaletteListItem from "admin/components/color-palette-list-item";@@ -76,12 +77,28 @@ export default RouteTemplate({{htmlSafe(i18n"admin.customize.colors.preference_warning"- link="/my/preferences/interface"+ link=(getUrl "/my/preferences/interface"))}}</div>{{/if}}+ {{#unless @controller.changedThemePreferences}}+ {{! only show one alert at a time, changedThemePreferences takes precedence }}+ {{#if @controller.isUsingDarkMode}}+ <div class="alert alert-info">+ {{htmlSafe+ (i18n+ "admin.customize.colors.dark_mode_warning"+ link=(getUrl+ "/admin/site_settings/category/all_results?filter=default dark mode"+ )+ )+ }}+ </div>+ {{/if}}+ {{/unless}}+<ul class="color-palette__list">{{#each @controller.filteredColorSchemes as |scheme|}}<ColorPaletteListItemdiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 7703e83bb25..15bd736d12b 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -6988,6 +6988,7 @@ en:title: "Select base color palette"description: "Base palette:"preference_warning: "You've changed <a href='%{link}'>your personal theme settings</a>, so palette changes may not be visible to you. Visitors that haven't changed their defaults will still see the changes you make here."+ dark_mode_warning: "You're currently using dark mode. When setting an active color palette the colors will be applied to the light mode of the theme. If you want to change the default dark mode colors, please edit the <a href='%{link}'>dark mode site setting</a>."title: "Colors"edit: "Edit"set_default: "Set as active on %{theme}"