Skip to content

A platform for community discussion. Free, open, simple.

UX: add description field to simplified category creation form (#38678)

Kris78c37cfd2b1b
diff --git a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
index 2d074f1dc78..865b4b9da3d 100644
--- a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
+++ b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
@@ -54,7 +54,7 @@
.form-kit__conditional-display-content {
// prevent content shift when changing style options
- min-height: 2.8em;
+ min-height: 2.6em;
&:has(.badge-category__wrapper) {
justify-content: center;
@@ -219,3 +219,54 @@
}
}
}
+
+.edit-category-description-container {
+ .readonly-field {
+ width: 100%;
+ font-size: var(--font-down-1);
+ color: var(--primary-high);
+
+ > :first-child {
+ margin-top: 0;
+ }
+
+ > :last-child {
+ margin-bottom: 0;
+ }
+ }
+
+ .description-content {
+ &.--collapsed {
+ max-height: calc(4 * 1.5em);
+ overflow: hidden;
+ position: relative;
+
+ &.--overflowing::after {
+ content: "";
+ position: absolute;
+ bottom: 0;
+ left: 0;
+ right: 0;
+ height: 1.5em;
+ background: linear-gradient(transparent, var(--secondary, white));
+ pointer-events: none;
+ }
+ }
+ }
+
+ .description-actions {
+ width: 100%;
+ display: flex;
+ align-items: center;
+ gap: var(--space-2);
+ margin-top: var(--space-2);
+
+ .toggle-description {
+ color: var(--primary-medium);
+
+ &:hover {
+ color: var(--primary-high);
+ }
+ }
+ }
+}
diff --git a/app/assets/stylesheets/common/components/buttons.scss b/app/assets/stylesheets/common/components/buttons.scss
index 33386c2d3d3..c6503c66170 100644
--- a/app/assets/stylesheets/common/components/buttons.scss
+++ b/app/assets/stylesheets/common/components/buttons.scss
@@ -217,6 +217,7 @@
@include hover {
background-color: transparent;
color: var(--tertiary-hover);
+ border: 0;
.d-icon {
color: var(--tertiary-hover);
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 58a599503f9..35f720a01cd 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4756,6 +4756,9 @@ en:
list: "List categories"
no_description: "Please add a description for this category."
change_in_category_topic: "Edit description"
+ description_updated: "Description updated"
+ description_expand: "Show more"
+ description_collapse: "Show less"
already_used: "This color has been used by another category"
color_palette: "Color palette"
security: "Security"
diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjs
index bd2553f09b3..1e9458113c7 100644
--- a/frontend/discourse/admin/components/upsert-category/general.gjs
+++ b/frontend/discourse/admin/components/upsert-category/general.gjs
@@ -1,19 +1,26 @@
import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
import { concat, hash } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
+import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";
import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
+import DecoratedHtml from "discourse/components/decorated-html";
import EmojiPicker from "discourse/components/emoji-picker";
import DTooltip from "discourse/float-kit/components/d-tooltip";
import categoryBadge from "discourse/helpers/category-badge";
import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
+import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { uniqueItemsFromArray } from "discourse/lib/array-tools";
import { AUTO_GROUPS, CATEGORY_TEXT_COLORS } from "discourse/lib/constants";
import getURL from "discourse/lib/get-url";
import Category from "discourse/models/category";
+import Composer from "discourse/models/composer";
import PermissionType from "discourse/models/permission-type";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import GroupChooser from "discourse/select-kit/components/group-chooser";
@@ -22,8 +29,17 @@ import { eq, or } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
export default class UpsertCategoryGeneral extends Component {
+ @service appEvents;
@service site;
@service siteSettings;
+ @service toasts;
+ @service composer;
+ @service store;
+
+ @tracked loadingDescription = false;
+ @tracked descriptionHtml = null;
+ @tracked descriptionExpanded = false;
+ @tracked descriptionOverflows = false;
uncategorizedSiteSettingLink = getURL(
"/admin/site_settings/category/all_results?filter=allow_uncategorized_topics"
@@ -35,6 +51,96 @@ export default class UpsertCategoryGeneral extends Component {
#previousPermissions = null;
+ @action
+ registerDescriptionListener() {
+ this.appEvents.on("composer:edited-post", this, this._refreshDescription);
+ }
+
+ @action
+ unregisterDescriptionListener() {
+ this.appEvents.off("composer:edited-post", this, this._refreshDescription);
+ }
+
+ @action
+ checkDescriptionOverflow(element) {
+ if (!this.descriptionExpanded) {
+ this.descriptionOverflows = element.scrollHeight > element.clientHeight;
+ }
+ }
+
+ @action
+ toggleDescriptionExpanded() {
+ this.descriptionExpanded = !this.descriptionExpanded;
+ }
+
+ async _refreshDescription() {
+ const category = this.args.category;
+ if (!category?.id) {
+ return;
+ }
+
+ const result = await Category.reloadById(category.id);
+ if (result?.category?.description) {
+ category.set("description", result.category.description);
+ this.descriptionHtml = result.category.description;
+
+ this.toasts.success({
+ duration: "short",
+ data: {
+ message: i18n("category.description_updated"),
+ },
+ });
+ }
+ }
+
+ get showDescription() {
+ const category = this.args.category;
+ return (
+ !category.isUncategorizedCategory && category.id && category.topic_url
+ );
+ }
+
+ get categoryDescription() {
+ const description = this.descriptionHtml ?? this.args.category.description;
+ if (description) {
+ return trustHTML(description);
+ }
+
+ return trustHTML(i18n("category.no_description"));
+ }
+
+ @action
+ async editCategoryDescription() {
+ this.loadingDescription = true;
+
+ try {
+ const topicData = await ajax(`${this.args.category.topic_url}.json`);
+ const firstPost = topicData.post_stream?.posts?.[0];
+ if (!firstPost) {
+ return;
+ }
+
+ this.composer.close();
+
+ const post = this.store.createRecord("post", firstPost);
+ const topic = this.store.createRecord("topic", topicData);
+ post.set("topic", topic);
+
+ await this.composer.open({
+ post,
+ topic,
+ action: Composer.EDIT,
+ draftKey: topicData.draft_key || `topic_${topicData.id}`,
+ draftSequence: topicData.draft_sequence ?? 0,
+ skipJumpOnSave: true,
+ });
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.loadingDescription = false;
+ }
+ }
+
// This needs to be dynamic because the name of the everyone group can be changed by admins
get #everyoneFullPermission() {
return {
@@ -482,6 +588,64 @@ export default class UpsertCategoryGeneral extends Component {
</styleField.Control>
</@form.Field>
+ {{#if this.showDescription}}
+ <div
+ {{didInsert this.registerDescriptionListener}}
+ {{willDestroy this.unregisterDescriptionListener}}
+ >
+ <@form.Container
+ @title={{i18n "category.description"}}
+ class="edit-category-description-container"
+ >
+ <div
+ class={{concatClass
+ "description-content"
+ (unless this.descriptionExpanded "--collapsed")
+ (if this.descriptionOverflows "--overflowing")
+ }}
+ {{didInsert this.checkDescriptionOverflow}}
+ {{didUpdate
+ this.checkDescriptionOverflow
+ this.categoryDescription
+ }}
+ >
+ <DecoratedHtml
+ @html={{this.categoryDescription}}
+ @className="readonly-field"
+ />
+ </div>
+
+ <div class="description-actions">
+ {{#if @category.topic_url}}
+ <@form.Button
+ @action={{this.editCategoryDescription}}
+ @icon="pencil"
+ @label="edit"
+ @isLoading={{this.loadingDescription}}
+ class="btn-default btn-small edit-category-description"
+ />
+ {{/if}}
+ {{#if this.descriptionOverflows}}
+ <@form.Button
+ @action={{this.toggleDescriptionExpanded}}
+ @label={{if
+ this.descriptionExpanded
+ "category.description_collapse"
+ "category.description_expand"
+ }}
+ @icon={{if
+ this.descriptionExpanded
+ "chevron-up"
+ "chevron-down"
+ }}
+ class="btn-flat btn-small toggle-description"
+ />
+ {{/if}}
+ </div>
+ </@form.Container>
+ </div>
+ {{/if}}
+
{{#unless @category.isUncategorizedCategory}}
<@form.Field
@name="parent_category_id"
diff --git a/spec/system/simplified_category_creation_spec.rb b/spec/system/simplified_category_creation_spec.rb
index c6413e5daa5..2c8fa114d37 100644
--- a/spec/system/simplified_category_creation_spec.rb
+++ b/spec/system/simplified_category_creation_spec.rb
@@ -9,6 +9,7 @@ describe "Simplified Category Creation" do
let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
let(:category_type_card) { PageObjects::Components::CategoryTypeCard.new }
let(:category_permission_row) { PageObjects::Components::CategoryPermissionRow.new }
+ let(:toasts) { PageObjects::Components::Toasts.new }
before do
SiteSetting.enable_simplified_category_creation = true
@@ -198,6 +199,55 @@ describe "Simplified Category Creation" do
group_chooser = PageObjects::Components::SelectKit.new(".group-chooser")
expect(group_chooser).to have_selected_name(group.name)
end
+
+ it "collapses long descriptions with a show more toggle" do
+ category_with_definition = Fabricate(:category_with_definition)
+ long_description = (["This is a long paragraph of text."] * 20).join(" ")
+ post = category_with_definition.topic.first_post
+ post.update!(cooked: "<p>#{long_description}</p>")
+ category_with_definition.update!(description: "<p>#{long_description}</p>")
+
+ category_page.visit_general(category_with_definition)
+
+ expect(page).to have_css(".description-content.--collapsed.--overflowing")
+ expect(page).to have_css(".toggle-description")
+
+ find(".toggle-description").click
+
+ expect(page).to have_no_css(".description-content.--collapsed")
+ expect(page).to have_css(".description-content.--overflowing")
+
+ find(".toggle-description").click
+
+ expect(page).to have_css(".description-content.--collapsed.--overflowing")
+ end
+
+ it "does not show expand toggle for short descriptions" do
+ category_with_definition = Fabricate(:category_with_definition)
+ category_page.visit_general(category_with_definition)
+
+ expect(page).to have_css(".description-content")
+ expect(page).to have_no_css(".toggle-description")
+ expect(page).to have_no_css(".description-content.--overflowing")
+ end
+
+ it "opens the composer to edit the category description and updates it after save" do
+ category_with_definition = Fabricate(:category_with_definition)
+ category_page.visit_general(category_with_definition)
+
+ composer = PageObjects::Components::Composer.new
+
+ find(".edit-category-description").click
+ expect(composer).to be_opened
+
+ composer.fill_content("Updated category description")
+ composer.submit
+
+ expect(composer).to be_closed
+ expect(page).to have_css(".edit-category-description-container .readonly-field")
+ expect(page).to have_content("Updated category description")
+ expect(toasts).to have_success(I18n.t("js.category.description_updated"))
+ end
end
describe "Security Tab" do