A platform for community discussion. Free, open, simple.
DEV: let user hide composer buttons (#43812)
chapoi9970307b9533
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rbindex 271a651e8d2..0b48bcd3c5a 100644--- a/app/controllers/users_controller.rb+++ b/app/controllers/users_controller.rb@@ -2301,9 +2301,12 @@ class UsersController < ApplicationControllereditable_custom_fields = User.editable_user_custom_fields(by_staff: current_user.try(:staff?))permitted << { custom_fields: editable_custom_fields } if editable_custom_fields.present?- permitted.concat(UserUpdater::OPTION_ATTR - [:understood_languages])+ permitted.concat(+ UserUpdater::OPTION_ATTR - %i[understood_languages hidden_composer_toolbar_buttons],+ )permitted << UserUpdater::LEGACY_SHOW_ORIGINAL_CONTENT_ATTRpermitted << { understood_languages: [] }+ permitted << { hidden_composer_toolbar_buttons: [] }permitted.concat UserUpdater::CATEGORY_IDS.keys.map { |k| { k => [] } }permitted.concat UserUpdater::TAG_NAMES.keyspermitted << UserUpdater::NOTIFICATION_SCHEDULE_ATTRSdiff --git a/app/models/user_option.rb b/app/models/user_option.rbindex e7b20fbd124..5cfa06f0869 100644--- a/app/models/user_option.rb+++ b/app/models/user_option.rb@@ -1,6 +1,9 @@# frozen_string_literal: trueclass UserOption < ActiveRecord::Base+ MAX_HIDDEN_COMPOSER_TOOLBAR_BUTTONS = 50+ MAX_HIDDEN_COMPOSER_TOOLBAR_BUTTON_LENGTH = 100+AUTO_MODE = 1LIGHT_MODE = 2DARK_MODE = 3@@ -87,6 +90,8 @@ class UserOption < ActiveRecord::Basevalidates :email_messages_level, inclusion: { in: UserOption.email_level_types.values }validates :timezone, timezone: truevalidate :understood_languages_are_supported, if: :will_save_change_to_understood_languages?+ validate :hidden_composer_toolbar_buttons_are_bounded,+ if: :will_save_change_to_hidden_composer_toolbar_buttons?def set_defaultsself.mailing_list_mode = SiteSetting.default_email_mailing_list_mode@@ -259,6 +264,15 @@ class UserOption < ActiveRecord::Baseprivate+ def hidden_composer_toolbar_buttons_are_bounded+ values = hidden_composer_toolbar_buttons++ if values.size > MAX_HIDDEN_COMPOSER_TOOLBAR_BUTTONS ||+ values.any? { |id| id.to_s.size > MAX_HIDDEN_COMPOSER_TOOLBAR_BUTTON_LENGTH }+ errors.add(:hidden_composer_toolbar_buttons, :invalid)+ end+ end+def understood_languages_are_supportedif understood_languages.all? { |locale| LocaleSiteSetting.supported_locales.include?(locale) }return@@ -330,6 +344,7 @@ end# enable_upcoming_change_available_notifications :boolean default(TRUE), not null# event_reminder_preference :integer default("personal_message"), not null# external_links_in_new_tab :boolean default(FALSE), not null+# hidden_composer_toolbar_buttons :string default([]), not null, is an Array# hide_presence :boolean default(FALSE), not null# hide_profile :boolean default(FALSE), not null# hide_profile_and_presence :boolean default(FALSE), not nulldiff --git a/app/serializers/current_user_option_serializer.rb b/app/serializers/current_user_option_serializer.rbindex 0b33be7bee3..5cb3cdac12f 100644--- a/app/serializers/current_user_option_serializer.rb+++ b/app/serializers/current_user_option_serializer.rb@@ -29,7 +29,8 @@ class CurrentUserOptionSerializer < ApplicationSerializer:show_original_content,:send_shortcut,:automatically_translate,- :understood_languages+ :understood_languages,+ :hidden_composer_toolbar_buttonsdef likes_notifications_disabledobject.likes_notifications_disabled?diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rbindex 3c4074ef43c..ae773be6326 100644--- a/app/serializers/user_option_serializer.rb+++ b/app/serializers/user_option_serializer.rb@@ -51,7 +51,8 @@ class UserOptionSerializer < ApplicationSerializer:show_original_content,:send_shortcut,:automatically_translate,- :understood_languages+ :understood_languages,+ :hidden_composer_toolbar_buttonsdef auto_track_topics_after_msecsobject.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecsdiff --git a/app/services/user_updater.rb b/app/services/user_updater.rbindex b386fa3c2ef..a694c4c13e2 100644--- a/app/services/user_updater.rb+++ b/app/services/user_updater.rb@@ -65,6 +65,7 @@ class UserUpdatersend_shortcutautomatically_translateunderstood_languages+ hidden_composer_toolbar_buttons]NOTIFICATION_SCHEDULE_ATTRS = -> dodiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 8b239a85e24..d60742fdb66 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -1917,6 +1917,9 @@ en:external_links_in_new_tab: "Open all external links in a new tab"enable_quoting: "Enable quote reply for highlighted text"enable_smart_lists: "Enable smart lists when writing in the composer"+ composer_toolbar:+ title: "Composer toolbar"+ instructions: "Choose which buttons appear in the composer toolbar. Keyboard shortcuts keep working for hidden buttons."enable_markdown_monospace_font: "Use monospace font in composer's Markdown mode"automatically_translate: "Automatically translate topics and posts in other languages"content_languages:diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex c512a61de58..eb9d2ff1179 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3117,6 +3117,7 @@ en:enable_new_checkbox_style: "Enable the redesigned checkbox style for FormKit checkboxes."enable_new_post_reactions_menu: "Enable the redesigned post likes and reactions menu, with infinite scroll and a native mobile experience."enable_composer_redesign: "Enable the redesigned composer."+ enable_composer_toolbar_customization: "Let members choose which buttons appear in their composer toolbar, from their interface preferences."prioritize_recently_used_tags: "When adding tags while creating or editing a topic, show the tags the member has recently used on their own topics at the top of the list, before the most popular tags of the forum."experimental_impersonation_time_limit_minutes: "How long before an impersonation session is automatically ended."page_loading_indicator: "Configure the loading indicator which appears during page navigations within Discourse. 'Spinner' is a full page indicator. 'Slider' shows a narrow bar at the top of the screen."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 0bad3e4e232..d084fe6e102 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -4837,6 +4837,15 @@ experimental:impact: "other,all_members"body_class: truelearn_more_url: "https://meta.discourse.org/t/-/407849"+ enable_composer_toolbar_customization:+ default: false+ client: true+ hidden: true+ area: "experimental"+ upcoming_change:+ status: "experimental"+ impact: "feature,all_members"+ learn_more_url: "https://meta.discourse.org/t/-/412749"enable_category_hashtag_cards:default: falseclient: truediff --git a/db/migrate/20260918061735_add_hidden_composer_toolbar_buttons_to_user_options.rb b/db/migrate/20260918061735_add_hidden_composer_toolbar_buttons_to_user_options.rbnew file mode 100644index 00000000000..93553ca4fbe--- /dev/null+++ b/db/migrate/20260918061735_add_hidden_composer_toolbar_buttons_to_user_options.rb@@ -0,0 +1,12 @@+# frozen_string_literal: true++class AddHiddenComposerToolbarButtonsToUserOptions < ActiveRecord::Migration[8.0]+ def change+ add_column :user_options,+ :hidden_composer_toolbar_buttons,+ :string,+ array: true,+ default: [],+ null: false+ end+enddiff --git a/db/structure.sql b/db/structure.sqlindex 4d401995cfe..a1d11cbbf98 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -12733,7 +12733,8 @@ CREATE TABLE public.user_options (chat_channel_list_sort_dms integer DEFAULT 2 NOT NULL,chat_channel_list_filter_starred integer DEFAULT 0 NOT NULL,chat_channel_list_filter_dms integer DEFAULT 0 NOT NULL,- event_reminder_preference integer DEFAULT 0 NOT NULL+ event_reminder_preference integer DEFAULT 0 NOT NULL,+ hidden_composer_toolbar_buttons character varying[] DEFAULT '{}'::character varying[] NOT NULL);@@ -25391,6 +25392,7 @@ SET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES('20260921015711'),+('20260918061735'),('20260915204557'),('20260915191328'),('20260914213908'),diff --git a/frontend/discourse/app/components/composer-editor.gjs b/frontend/discourse/app/components/composer-editor.gjsindex e5be4d6cf4c..ac0f53669f6 100644--- a/frontend/discourse/app/components/composer-editor.gjs+++ b/frontend/discourse/app/components/composer-editor.gjs@@ -1023,6 +1023,7 @@ export default class ComposerEditor extends Component {@showLink={{this.showLink}}@toolbarPortalTarget={{this.toolbarPortalTarget}}@topicId={{this.composer.model.topic.id}}+ @userToolbarPreferences={{true}}@validation={{this.validation}}@value={{this.composer.model.reply}}{{didInsert this._composerEditorInitEditor}}diff --git a/frontend/discourse/app/components/composer/toolbar-buttons.gjs b/frontend/discourse/app/components/composer/toolbar-buttons.gjsindex 44a7c40d5ad..aefb6cc11c5 100644--- a/frontend/discourse/app/components/composer/toolbar-buttons.gjs+++ b/frontend/discourse/app/components/composer/toolbar-buttons.gjs@@ -15,11 +15,9 @@ export default class ComposerToolbarButtons extends Component {return false;}- const { context } = this.args.data;-return this.args.data.groups.flatMap((group) => group.buttons ?? [])- .find((button) => this.isActionable(button) && button.condition(context));+ .find((button) => this.isActionable(button) && this.isVisible(button));}get rovingButtonBar() {@@ -35,6 +33,11 @@ export default class ComposerToolbarButtons extends Component {return button.type !== "separator" && !button.disabled;}+ @action+ isVisible(button) {+ return !button.hiddenByUser && button.condition(this.args.data.context);+ }+/** The title with the drawn shortcut appended, when one is shown. */@actiontitleFor(button, shortcutLabel) {@@ -53,7 +56,7 @@ export default class ComposerToolbarButtons extends Component {<template>{{#each @data.groups key="group" as |group|}}{{#each group.buttons key="id" as |button|}}- {{#if (button.condition @data.context)}}+ {{#if (this.isVisible button)}}{{#if (eq button.type "separator")}}<div class="toolbar-separator"></div>{{else if button.popupMenu}}diff --git a/frontend/discourse/app/controllers/preferences/interface.js b/frontend/discourse/app/controllers/preferences/interface.jsindex ade44cc44c1..fd354b443b1 100644--- a/frontend/discourse/app/controllers/preferences/interface.js+++ b/frontend/discourse/app/controllers/preferences/interface.js@@ -9,6 +9,7 @@ import {loadColorSchemeStylesheet,updateColorSchemeCookie,} from "discourse/lib/color-scheme-picker";+import { HIDEABLE_BUTTONS } from "discourse/lib/composer/toolbar";import {INTERFACE_COLOR_MODES,SEND_SHORTCUT_ENTER,@@ -91,6 +92,7 @@ export default class InterfaceController extends Controller {"send_shortcut","automatically_translate","understood_languages",+ "hidden_composer_toolbar_buttons",];if (this.siteSettings.allow_user_locale) {@@ -106,6 +108,19 @@ export default class InterfaceController extends Controller {});}+ @computed("model.user_option.hidden_composer_toolbar_buttons.[]")+ get composerToolbarButtons() {+ const hidden = this.model.user_option.hidden_composer_toolbar_buttons ?? [];++ return HIDEABLE_BUTTONS.filter(({ id }) =>+ this.#composerToolbarButtonAvailable(id)+ ).map(({ id, label }) => ({+ id,+ label,+ shown: !hidden.includes(id),+ }));+ }+@computed("model.user_option.understood_languages.[]")get understoodLanguages() {return normalizeUnderstoodLanguages(@@ -397,6 +412,21 @@ export default class InterfaceController extends Controller {this.model.set("locale", locale);}+ @action+ toggleComposerToolbarButton(buttonId) {+ const hidden = new Set(+ this.model.user_option.hidden_composer_toolbar_buttons ?? []+ );++ if (hidden.has(buttonId)) {+ hidden.delete(buttonId);+ } else {+ hidden.add(buttonId);+ }++ this.model.set("user_option.hidden_composer_toolbar_buttons", [...hidden]);+ }+@actionsetUnderstoodLanguages(locales) {this.model.set(@@ -642,6 +672,21 @@ export default class InterfaceController extends Controller {return this.model.save(["skip_new_user_tips", "seen_popups"]);}+ #composerToolbarButtonAvailable(buttonId) {+ switch (buttonId) {+ case "emoji":+ return this.siteSettings.enable_emoji;+ case "gifs":+ return this.siteSettings.enable_gifs;+ case "toggle-direction":+ return this.siteSettings.support_mixed_text_direction;+ case "post-language-selector":+ return this.siteSettings.content_localization_enabled;+ default:+ return true;+ }+ }+#shouldEnablePreview(isDarkMode) {return (this.isViewingOwnProfile &&diff --git a/frontend/discourse/app/lib/composer/toolbar.ts b/frontend/discourse/app/lib/composer/toolbar.tsindex 2ca21fb4454..8ff03e9ddcc 100644--- a/frontend/discourse/app/lib/composer/toolbar.ts+++ b/frontend/discourse/app/lib/composer/toolbar.ts@@ -132,6 +132,8 @@ export interface ToolbarButton {shortcutAction?: (event: ToolbarEvent) => void;/** Whether the button is disabled. */disabled?: boolean;+ /** Whether the user's preferences exclude the button from the toolbar. */+ hiddenByUser?: boolean;}type ToolbarButtonAttrs = Omit<@@ -171,6 +173,8 @@ export interface ToolbarOptions {site?: Partial<Site>;/** Whether the link button is included. */showLink?: boolean;+ /** Ids of the buttons the user has chosen to hide. Only honoured for ids in `HIDEABLE_BUTTONS`. */+ hiddenButtons?: string[];}function getButtonLabel(labelKey: string, defaultLabel: string): string | null {@@ -178,6 +182,30 @@ function getButtonLabel(labelKey: string, defaultLabel: string): string | null {return i18n(labelKey) === defaultLabel ? null : labelKey;}+/**+ * Core buttons a user may remove from the composer toolbar, with the translation+ * key naming each one. Buttons outside this list — including every button+ * contributed by a plugin — are always shown.+ */+export const HIDEABLE_BUTTONS = [+ { id: "bold", label: "composer.bold_title" },+ { id: "italic", label: "composer.italic_title" },+ { id: "heading", label: "composer.text_size_title" },+ { id: "link", label: "composer.link_title" },+ { id: "blockquote", label: "composer.blockquote_title" },+ { id: "code", label: "composer.code_title" },+ { id: "list", label: "composer.list_title" },+ { id: "toggle-direction", label: "composer.toggle_direction" },+ { id: "emoji", label: "composer.emoji" },+ { id: "gifs", label: "gifs.composer_title" },+ {+ id: "post-language-selector",+ label: "post.localizations.post_language_selector.title",+ },+];++const HIDEABLE_BUTTON_IDS = new Set(HIDEABLE_BUTTONS.map(({ id }) => id));+const DEFAULT_GROUP = "main";export class ToolbarBase {@@ -199,6 +227,9 @@ export class ToolbarBase {/** Site state used to configure toolbar behavior. */site: Partial<Site>;+ /** Ids of the buttons the user has chosen to hide, read once when the toolbar is built. */+ hiddenButtons: string[];+constructor(opts: ToolbarOptions = {}) {this.shortcuts = {};this.context = {};@@ -206,6 +237,7 @@ export class ToolbarBase {this.siteSettings = opts.siteSettings || {};this.capabilities = opts.capabilities || {};this.site = opts.site || {};+ this.hiddenButtons = opts.hiddenButtons || [];}/** Adds a button to its configured toolbar group. */@@ -225,6 +257,9 @@ export class ToolbarBase {createdButton.tabindex ??= "-1";createdButton.className ||= buttonAttrs.id;createdButton.condition ||= () => true;+ createdButton.hiddenByUser =+ HIDEABLE_BUTTON_IDS.has(buttonAttrs.id) &&+ this.hiddenButtons.includes(buttonAttrs.id);createdButton.action = async () => {if (buttonAttrs.popupMenu) {diff --git a/frontend/discourse/app/models/user.js b/frontend/discourse/app/models/user.jsindex 4431065d336..b7b30f20c85 100644--- a/frontend/discourse/app/models/user.js+++ b/frontend/discourse/app/models/user.js@@ -131,6 +131,7 @@ let userOptionFields = ["enable_smart_lists","enable_upcoming_change_available_notifications","external_links_in_new_tab",+ "hidden_composer_toolbar_buttons","hide_presence","hide_profile","homepage_id",diff --git a/frontend/discourse/app/templates/preferences/interface.gjs b/frontend/discourse/app/templates/preferences/interface.gjsindex 1044adf1345..15d5575cac6 100644--- a/frontend/discourse/app/templates/preferences/interface.gjs+++ b/frontend/discourse/app/templates/preferences/interface.gjs@@ -1,4 +1,5 @@import { fn, hash } from "@ember/helper";+import { on } from "@ember/modifier";import PluginOutlet from "discourse/components/plugin-outlet";import PreferenceCheckbox from "discourse/components/preference-checkbox";import lazyHash from "discourse/helpers/lazy-hash";@@ -210,6 +211,35 @@ export default <template></div></div>+ {{#if @controller.siteSettings.enable_composer_toolbar_customization}}+ <fieldset+ class="control-group pref-composer-toolbar"+ data-setting-name="user-composer-toolbar"+ >+ <legend class="control-label">{{i18n+ "user.composer_toolbar.title"+ }}</legend>+ <div class="instructions">+ {{i18n "user.composer_toolbar.instructions"}}+ </div>+ {{#each @controller.composerToolbarButtons key="id" as |button|}}+ <div class="controls">+ <label class="checkbox-label">+ <input+ checked={{button.shown}}+ type="checkbox"+ {{on+ "change"+ (fn @controller.toggleComposerToolbarButton button.id)+ }}+ />+ {{i18n button.label}}+ </label>+ </div>+ {{/each}}+ </fieldset>+ {{/if}}+<fieldset class="control-group other" data-setting-name="user-other-settings"><legend class="control-label">{{i18n "user.other_settings"}}</legend>diff --git a/frontend/discourse/app/ui-kit/d-editor.gjs b/frontend/discourse/app/ui-kit/d-editor.gjsindex 0754df6c391..6d4506056d1 100644--- a/frontend/discourse/app/ui-kit/d-editor.gjs+++ b/frontend/discourse/app/ui-kit/d-editor.gjs@@ -181,10 +181,22 @@ export default class DEditor extends Component {return this.editorComponent !== TextareaEditor;}+ get hiddenToolbarButtons() {+ if (+ !this.userToolbarPreferences ||+ !this.siteSettings.enable_composer_toolbar_customization+ ) {+ return [];+ }++ return this.currentUser?.user_option?.hidden_composer_toolbar_buttons ?? [];+ }+setupToolbar() {- this.toolbar = new Toolbar(- this.getProperties("siteSettings", "showLink", "capabilities", "site")- );+ this.toolbar = new Toolbar({+ ...this.getProperties("siteSettings", "showLink", "capabilities", "site"),+ hiddenButtons: this.hiddenToolbarButtons,+ });this.toolbar.context = this;_createCallbacks.forEach((cb) => cb(this.toolbar));diff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.jsonindex 8c054bf39ef..4b4049aafbb 100644--- a/spec/requests/api/schemas/json/user_get_response.json+++ b/spec/requests/api/schemas/json/user_get_response.json@@ -856,6 +856,12 @@"type": "string"}},+ "hidden_composer_toolbar_buttons": {+ "type": "array",+ "items": {+ "type": "string"+ }+ },"show_original_content": {"type": "boolean"},@@ -906,7 +912,8 @@"show_original_content","send_shortcut","automatically_translate",- "understood_languages"+ "understood_languages",+ "hidden_composer_toolbar_buttons"]}},