A platform for community discussion. Free, open, simple.
FIX: restore category text color field (#32915)
David Battersby69120255649f
diff --git a/app/assets/javascripts/discourse/app/components/edit-category-general.gjs b/app/assets/javascripts/discourse/app/components/edit-category-general.gjsindex 7a0f892a6e6..8a9df98665b 100644--- a/app/assets/javascripts/discourse/app/components/edit-category-general.gjs+++ b/app/assets/javascripts/discourse/app/components/edit-category-general.gjs@@ -9,7 +9,10 @@ import PluginOutlet from "discourse/components/plugin-outlet";import categoryBadge from "discourse/helpers/category-badge";import { categoryBadgeHTML } from "discourse/helpers/category-link";import lazyHash from "discourse/helpers/lazy-hash";-import { CATEGORY_STYLE_TYPES } from "discourse/lib/constants";+import {+ CATEGORY_STYLE_TYPES,+ CATEGORY_TEXT_COLORS,+} from "discourse/lib/constants";import getURL from "discourse/lib/get-url";import Category from "discourse/models/category";import { i18n } from "discourse-i18n";@@ -80,10 +83,10 @@ export default class EditCategoryGeneral extends Component {const category = this.args.category;const previewCategory = Category.create({+ id: category.id,name: transientData.name || i18n("category.untitled"),color: transientData.color,- id: category.id,- text_color: category.text_color,+ text_color: transientData.text_color,parent_category_id: parseInt(category.get("parent_category_id"), 10),read_restricted: category.get("read_restricted"),});@@ -123,7 +126,37 @@ export default class EditCategoryGeneral extends Component {@actionupdateColor(field, newColor) {- field.set(newColor.replace("#", ""));+ const color = newColor.replace("#", "");++ if (field.name === "color") {+ const whiteDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[0]);+ const blackDiff = this.colorDifference(color, CATEGORY_TEXT_COLORS[1]);+ const colorIndex = whiteDiff > blackDiff ? 0 : 1;++ this.args.form.setProperties({+ color,+ text_color: CATEGORY_TEXT_COLORS[colorIndex],+ });+ } else {+ field.set(color);+ }+ }++ @action+ colorDifference(color1, color2) {+ const r1 = parseInt(color1.substr(0, 2), 16);+ const g1 = parseInt(color1.substr(2, 2), 16);+ const b1 = parseInt(color1.substr(4, 2), 16);++ const r2 = parseInt(color2.substr(0, 2), 16);+ const g2 = parseInt(color2.substr(2, 2), 16);+ const b2 = parseInt(color2.substr(4, 2), 16);++ const rDiff = Math.max(r1, r2) - Math.min(r1, r2);+ const gDiff = Math.max(g1, g2) - Math.min(g1, g2);+ const bDiff = Math.max(b1, b2) - Math.min(b1, b2);++ return rDiff + gDiff + bDiff;}get categoryDescription() {@@ -304,6 +337,31 @@ export default class EditCategoryGeneral extends Component {</div></field.Custom></@form.Field>++ <@form.Field+ @name="text_color"+ @title={{i18n "category.foreground_color"}}+ @format="full"+ as |field|+ >+ <field.Custom>+ <div class="category-color-editor">+ <div class="colorpicker-wrapper edit-text-color">+ <ColorInput+ @hexValue={{readonly field.value}}+ @ariaLabelledby="foreground-color-label"+ @onChangeColor={{fn this.updateColor field}}+ />+ <ColorPicker+ @colors={{CATEGORY_TEXT_COLORS}}+ @value={{readonly field.value}}+ @ariaLabel={{i18n "category.predefined_colors"}}+ @onSelectColor={{fn this.updateColor field}}+ />+ </div>+ </div>+ </field.Custom>+ </@form.Field></@form.Section></div></template>diff --git a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.jsindex f271231efb9..c2b4b4f7c00 100644--- a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js+++ b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js@@ -122,7 +122,6 @@ export default class EditCategoryTabsController extends Controller {}this.model.setProperties(transientData);- this.setTextColor(this.model.color);this.set("saving", true);@@ -188,15 +187,4 @@ export default class EditCategoryTabsController extends Controller {goBack() {DiscourseURL.routeTo(this.model.url);}-- @action- setTextColor(backgroundColor) {- const r = parseInt(backgroundColor.substr(0, 2), 16);- const g = parseInt(backgroundColor.substr(2, 2), 16);- const b = parseInt(backgroundColor.substr(4, 2), 16);- const brightness = (r * 299 + g * 587 + b * 114) / 1000;- const color = brightness >= 128 ? this.textColors[0] : this.textColors[1];-- this.model.set("text_color", color);- }}diff --git a/app/assets/javascripts/discourse/app/lib/constants.js b/app/assets/javascripts/discourse/app/lib/constants.jsindex b444eff80a9..454056fc3c7 100644--- a/app/assets/javascripts/discourse/app/lib/constants.js+++ b/app/assets/javascripts/discourse/app/lib/constants.js@@ -24,6 +24,8 @@ export const SIDEBAR_SECTION = {export const CATEGORY_STYLE_TYPES = { square: 0, icon: 1, emoji: 2 };+export const CATEGORY_TEXT_COLORS = ["FFFFFF", "000000"];+export const AUTO_GROUPS = {everyone: {id: 0,diff --git a/app/assets/javascripts/discourse/tests/acceptance/category-new-test.js b/app/assets/javascripts/discourse/tests/acceptance/category-new-test.jsindex 4c61185448a..baae01ff229 100644--- a/app/assets/javascripts/discourse/tests/acceptance/category-new-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/category-new-test.js@@ -1,6 +1,7 @@import { click, currentURL, fillIn, visit } from "@ember/test-helpers";import { test } from "qunit";import sinon from "sinon";+import { CATEGORY_TEXT_COLORS } from "discourse/lib/constants";import { cloneJSON } from "discourse/lib/object";import DiscourseURL from "discourse/lib/url";import { fixturesByUrl } from "discourse/tests/helpers/create-pretender";@@ -135,37 +136,16 @@ acceptance("Category text color", function (needs) {test("Category text color is set based on contrast", async function (assert) {await visit("/new-category");- let previewTextColor = document- .querySelector(".category-style .badge-category__wrapper")- .style.getPropertyValue("--category-badge-text-color")- .trim();-- assert.strictEqual(- previewTextColor,- "#FFFFFF",- "has the default text color"- );+ assert+ .dom(".edit-text-color .hex-input")+ .hasValue(CATEGORY_TEXT_COLORS[0], "has the default text color");await fillIn("input.category-name", "testing");await fillIn(".category-color-editor .hex-input", "EEEEEE");- await click("#save-category");- assert.strictEqual(- currentURL(),- "/c/testing/edit/general",- "it transitions to the category edit route"- );-- previewTextColor = document- .querySelector(".category-style .badge-category__wrapper")- .style.getPropertyValue("--category-badge-text-color")- .trim();-- assert.strictEqual(- previewTextColor,- "#000000",- "sets the contrast text color"- );+ assert+ .dom(".edit-text-color .hex-input")+ .hasValue(CATEGORY_TEXT_COLORS[1], "sets the contrast text color");});});diff --git a/app/models/category.rb b/app/models/category.rbindex 5c76d44d922..34efef54297 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -18,6 +18,7 @@ class Category < ActiveRecord::Baseinclude HasDestroyedWebHookSLUG_REF_SEPARATOR = ":"+ DEFAULT_TEXT_COLORS = %w[FFFFFF 000000]belongs_to :topicbelongs_to :topic_only_relative_url,diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex fd07d3a2700..79903500a7f 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4189,6 +4189,7 @@ en:background_image_dark: "Dark Category Background Image"style: "Styles"background_color: "Color"+ foreground_color: "Text color"styles:type: "Style"icon: "Icon"diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rakeindex d9660771ffa..bc09ea07b1d 100644--- a/lib/tasks/javascript.rake+++ b/lib/tasks/javascript.rake@@ -168,6 +168,8 @@ task "javascript:update_constants" => :environment doexport const CATEGORY_STYLE_TYPES = #{Category.style_types.to_json};+ export const CATEGORY_TEXT_COLORS = #{Category::DEFAULT_TEXT_COLORS};+export const AUTO_GROUPS = #{auto_groups.to_json};export const GROUP_SMTP_SSL_MODES = #{Group.smtp_ssl_modes.to_json};