A platform for community discussion. Free, open, simple.
FEATURE: Improve color palette sorting order in admin (#34282)
Krzysztof Kotlareka2343f247731
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.jsindex 98faa86ac8b..b763a664e7d 100644--- a/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js+++ b/app/assets/javascripts/admin/addon/controllers/admin-customize-colors.js@@ -125,31 +125,36 @@ export default class AdminCustomizeColorsController extends Controller {name: i18n("admin.customize.theme.default_light_scheme"),description: i18n("admin.customize.theme.default_light_scheme"),is_builtin_default: true,+ user_selectable: false,+ theme_id: -1,};schemes.unshift(builtInDefault);}- schemes.sort((a, b) => {- const defaultLightId = this.defaultTheme?.color_scheme_id;- const defaultDarkId = this.defaultTheme?.dark_color_scheme_id;-- const isDefaultA = a.is_builtin_default- ? defaultLightId === null || defaultDarkId === null- : a.id === defaultLightId || a.id === defaultDarkId;+ const defaultThemeId = this.defaultTheme?.id;- const isDefaultB = b.is_builtin_default- ? defaultLightId === null || defaultDarkId === null- : b.id === defaultLightId || b.id === defaultDarkId;+ schemes.sort((a, b) => {+ // 1. Sort by user selectable first+ if (a.user_selectable !== b.user_selectable) {+ return a.user_selectable ? -1 : 1;+ }- if (isDefaultA !== isDefaultB) {- return isDefaultA ? -1 : 1;+ // 2. Sort custom schemes (no theme) before themed schemes+ const aIsCustom = !a.theme_id && !a.is_builtin_default;+ const bIsCustom = !b.theme_id && !b.is_builtin_default;+ if (aIsCustom !== bIsCustom) {+ return aIsCustom ? -1 : 1;}- if (a.user_selectable !== b.user_selectable) {- return a.user_selectable ? -1 : 1;+ // 3. Prioritize schemes from the current default theme+ const aIsFromDefaultTheme = a.theme_id === defaultThemeId;+ const bIsFromDefaultTheme = b.theme_id === defaultThemeId;+ if (aIsFromDefaultTheme !== bIsFromDefaultTheme) {+ return aIsFromDefaultTheme ? -1 : 1;}- return (a.name || "").localeCompare(b.name || "");+ // 4. Finally, sort alphabetically by name+ return (a.originals.name || "").localeCompare(b.originals.name || "");});this._initialSortedSchemes = schemes;diff --git a/spec/system/admin_color_palettes_features_spec.rb b/spec/system/admin_color_palettes_features_spec.rbindex 21b7857098f..29438c24a51 100644--- a/spec/system/admin_color_palettes_features_spec.rb+++ b/spec/system/admin_color_palettes_features_spec.rb@@ -13,11 +13,60 @@ describe "Admin Color Palettes Features", type: :system dolet(:toasts) { PageObjects::Components::Toasts.new }- before do- sign_in(admin)- # filter requires 8 palettes to show- 6.times { |i| Fabricate(:color_scheme, name: "Extra Palette #{i}") }+ let(:foundation_scheme_a) do+ Fabricate(:color_scheme, name: "Foundation scheme a", theme: Theme.foundation_theme)end+ let(:foundation_scheme_b) do+ Fabricate(:color_scheme, name: "Foundation scheme b", theme: Theme.foundation_theme)+ end+ let(:horizon_scheme_a) do+ Fabricate(:color_scheme, name: "Horizon scheme a", theme: Theme.horizon_theme)+ end+ let(:horizon_scheme_b) do+ Fabricate(:color_scheme, name: "Horizon scheme b", theme: Theme.horizon_theme)+ end+ let(:custom_scheme_a) { Fabricate(:color_scheme, name: "Custom scheme a") }+ let(:custom_scheme_b) { Fabricate(:color_scheme, name: "Custom scheme b") }+ let(:selectable_foundation_scheme_a) do+ Fabricate(+ :color_scheme,+ name: "Selectable foundation scheme a",+ theme: Theme.foundation_theme,+ user_selectable: true,+ )+ end+ let(:selectable_foundation_scheme_b) do+ Fabricate(+ :color_scheme,+ name: "Selectable foundation scheme b",+ theme: Theme.foundation_theme,+ user_selectable: true,+ )+ end+ let(:selectable_horizon_theme_scheme_a) do+ Fabricate(+ :color_scheme,+ name: "Selectable horizon scheme a",+ theme: Theme.horizon_theme,+ user_selectable: true,+ )+ end+ let(:selectable_horizon_theme_scheme_b) do+ Fabricate(+ :color_scheme,+ name: "Selectable horizon scheme b",+ theme: Theme.horizon_theme,+ user_selectable: true,+ )+ end+ let(:selectable_custom_scheme_a) do+ Fabricate(:color_scheme, name: "Selectable custom scheme a", user_selectable: true)+ end+ let(:selectable_custom_scheme_b) do+ Fabricate(:color_scheme, name: "Selectable custom scheme b", user_selectable: true)+ end++ before { sign_in(admin) }describe "filtering" doit "shows filters when there are more than 8 color schemes" do@@ -144,4 +193,67 @@ describe "Admin Color Palettes Features", type: :system doexpect(palette_item[:style]).to include("--tertiary--preview:")endend++ describe "sort" do+ before do+ ColorScheme.delete_all+ foundation_scheme_a+ foundation_scheme_b+ horizon_scheme_a+ horizon_scheme_b+ custom_scheme_a+ custom_scheme_b+ selectable_foundation_scheme_a+ selectable_foundation_scheme_b+ selectable_horizon_theme_scheme_a+ selectable_horizon_theme_scheme_b+ selectable_custom_scheme_a+ selectable_custom_scheme_b+ end+ it "sorts schemes in order: selectable, custom, current default theme, alphabetical for horizon theme" do+ SiteSetting.default_theme_id = Theme.horizon_theme.id+ visit("/admin/customize/colors")+ color_schemes = page.all(".color-palette__details h3").map(&:text)+ expect(color_schemes).to eq(+ [+ "Selectable custom scheme a",+ "Selectable custom scheme b",+ "Selectable horizon scheme a",+ "Selectable horizon scheme b",+ "Selectable foundation scheme a",+ "Selectable foundation scheme b",+ "Custom scheme a",+ "Custom scheme b",+ "Horizon scheme a",+ "Horizon scheme b",+ "Foundation scheme a",+ "Foundation scheme b",+ "Light (default)",+ ],+ )+ end++ it "sorts schemes in order: selectable, custom, current default theme, alphabetical for foundation theme" do+ SiteSetting.default_theme_id = Theme.foundation_theme.id+ visit("/admin/customize/colors")+ color_schemes = page.all(".color-palette__details h3").map(&:text)+ expect(color_schemes).to eq(+ [+ "Selectable custom scheme a",+ "Selectable custom scheme b",+ "Selectable foundation scheme a",+ "Selectable foundation scheme b",+ "Selectable horizon scheme a",+ "Selectable horizon scheme b",+ "Custom scheme a",+ "Custom scheme b",+ "Foundation scheme a",+ "Foundation scheme b",+ "Light (default)",+ "Horizon scheme a",+ "Horizon scheme b",+ ],+ )+ end+ endend