A platform for community discussion. Free, open, simple.
DEV: Add APIs in the post menu to handle collapsed buttons (#31734)
Sérgio Saquetimb50ac0551032
diff --git a/app/assets/javascripts/discourse/app/components/post/menu.gjs b/app/assets/javascripts/discourse/app/components/post/menu.gjsindex 39827b24fe9..8eaf63b48eb 100644--- a/app/assets/javascripts/discourse/app/components/post/menu.gjs+++ b/app/assets/javascripts/discourse/app/components/post/menu.gjs@@ -18,6 +18,7 @@ import DAG from "discourse/lib/dag";import {applyBehaviorTransformer,applyMutableValueTransformer,+ applyValueTransformer,} from "discourse/lib/transformer";import { i18n } from "discourse-i18n";import PostMenuButtonConfig from "./menu/button-config";@@ -84,7 +85,11 @@ export default class PostMenu extends Component {@service siteSettings;@service store;- @tracked collapsed = true; // TODO (glimmer-post-menu): Some plugins will need a value transformer+ @tracked collapsed = applyValueTransformer(+ "post-menu-collapsed",+ true,+ this.#prepareStaticMethodsState({ collapsed: true })+ );@tracked isWhoLikedVisible = false;@tracked likedUsers = [];@tracked totalLikedUsers;@@ -116,23 +121,7 @@ export default class PostMenu extends Component {@cachedget staticMethodsState() {- return Object.freeze({- canCreatePost: this.args.canCreatePost,- collapsed: this.collapsed,- currentUser: this.currentUser,- filteredRepliesView: this.args.filteredRepliesView,- isWhoLikedVisible: this.isWhoLikedVisible,- isWhoReadVisible: this.isWhoReadVisible,- isWikiMode: this.isWikiMode,- repliesShown: this.args.repliesShown,- replyDirectlyBelow:- this.args.nextPost?.reply_to_post_number ===- this.args.post.post_number &&- this.args.post.post_number !== this.args.post.filteredRepliesPostNumber,- showReadIndicator: this.args.showReadIndicator,- suppressReplyDirectlyBelow:- this.siteSettings.suppress_reply_directly_below,- });+ return this.#prepareStaticMethodsState();}@cached@@ -208,6 +197,8 @@ export default class PostMenu extends Component {// map to keep track of the labels that should be shown for each button if the plugins wants to override the defaultconst buttonLabels = new Map();+ // map to keep track of the collapsed state of each button if the plugins wants to override the default+ const collapsedButtons = new Map();const showMoreButtonPosition = configuredItems.indexOf(buttonKeys.SHOW_MORE@@ -235,6 +226,17 @@ export default class PostMenu extends Component {return buttonLabels.delete(key);},},+ collapsedButtons: {+ hide(key) {+ collapsedButtons.set(key, true);+ },+ show(key) {+ collapsedButtons.set(key, false);+ },+ default(key) {+ return collapsedButtons.delete(key);+ },+ },buttonKeys,firstButtonKey: this.configuredItems[0],lastHiddenButtonKey: hiddenButtonKeys.length@@ -257,6 +259,7 @@ export default class PostMenu extends Component {key,Component: ButtonComponent,apiAdded: addedKeys.has(key), // flag indicating if the button was added using the API+ hidden: collapsedButtons.get(key),owner: getOwner(this), // to be passed as argument to the static methodsposition,replacementMap,@@ -583,6 +586,26 @@ export default class PostMenu extends Component {this.isWhoReadVisible = true;}+ #prepareStaticMethodsState({ collapsed } = {}) {+ return Object.freeze({+ canCreatePost: this.args.canCreatePost,+ collapsed: collapsed ?? this.collapsed,+ currentUser: this.currentUser,+ filteredRepliesView: this.args.filteredRepliesView,+ isWhoLikedVisible: this.isWhoLikedVisible,+ isWhoReadVisible: this.isWhoReadVisible,+ isWikiMode: this.isWikiMode,+ repliesShown: this.args.repliesShown,+ replyDirectlyBelow:+ this.args.nextPost?.reply_to_post_number ===+ this.args.post.post_number &&+ this.args.post.post_number !== this.args.post.filteredRepliesPostNumber,+ showReadIndicator: this.args.showReadIndicator,+ suppressReplyDirectlyBelow:+ this.siteSettings.suppress_reply_directly_below,+ });+ }+<template>{{! The section tag can't be include while we're still using the widget shim }}{{! <section class="post-menu-area clearfix"> }}diff --git a/app/assets/javascripts/discourse/app/components/post/menu/button-config.gjs b/app/assets/javascripts/discourse/app/components/post/menu/button-config.gjsindex 093f6992aac..934497ee9a6 100644--- a/app/assets/javascripts/discourse/app/components/post/menu/button-config.gjs+++ b/app/assets/javascripts/discourse/app/components/post/menu/button-config.gjs@@ -4,6 +4,7 @@ import { helperContext } from "discourse/lib/helpers";export default class PostMenuButtonConfig {#Component;#apiAdded;+ #hidden;#key;#owner;#position;@@ -14,6 +15,7 @@ export default class PostMenuButtonConfig {key,Component,apiAdded,+ hidden,owner,position,replacementMap,@@ -21,6 +23,7 @@ export default class PostMenuButtonConfig {}) {this.#Component = Component;this.#apiAdded = apiAdded;+ this.#hidden = hidden;this.#key = key;this.#owner = owner;this.#position = position;@@ -38,11 +41,14 @@ export default class PostMenuButtonConfig {@bindhidden(args) {- return this.#staticPropertyWithReplacementFallback({- property: "hidden",- args,- defaultValue: null,- });+ return (+ this.#hidden ??+ this.#staticPropertyWithReplacementFallback({+ property: "hidden",+ args,+ defaultValue: null,+ })+ );}@binddiff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.jsindex 759868396c5..fc345e9bb8e 100644--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js@@ -31,6 +31,7 @@ export const VALUE_TRANSFORMERS = Object.freeze(["parent-category-row-class","parent-category-row-class-mobile","post-menu-buttons",+ "post-menu-collapsed","small-user-attrs","tag-separator","topic-list-class",diff --git a/app/assets/javascripts/discourse/tests/unit/components/post-menu-test.gjs b/app/assets/javascripts/discourse/tests/unit/components/post-menu-test.gjsindex abe69cc5d01..6c9b4fe7e5d 100644--- a/app/assets/javascripts/discourse/tests/unit/components/post-menu-test.gjs+++ b/app/assets/javascripts/discourse/tests/unit/components/post-menu-test.gjs@@ -10,6 +10,8 @@ module("Unit | Component | post-menu", function (hooks) {hooks.beforeEach(function () {this.siteSettings.glimmer_post_menu_mode = "enabled";+ this.siteSettings.post_menu =+ "read|like|copyLink|share|flag|edit|bookmark|delete|admin|reply";this.siteSettings.post_menu_hidden_items = "";const store = getOwner(this).lookup("service:store");@@ -23,6 +25,34 @@ module("Unit | Component | post-menu", function (hooks) {});});+ test("post-menu-collapsed value transformer", async function (assert) {+ this.siteSettings.post_menu_hidden_items = "bookmark|copyLink";++ // without the transformer+ const post = this.post; // using this inside the template does not correspond to the test `this` context+ await render(<template><PostMenu @post={{post}} /></template>);+ assert.dom(".post-action-menu__show-more").exists("show more is displayed");+ assert+ .dom(".post-action-menu__bookmark")+ .doesNotExist("bookmark is hidden");+ assert+ .dom(".post-action-menu__copy-link")+ .doesNotExist("copyLink is hidden");++ // with the transformer+ withPluginApi((api) => {+ api.registerValueTransformer("post-menu-collapsed", () => false);+ });++ await render(<template><PostMenu @post={{post}} /></template>);++ assert+ .dom(".post-action-menu__show-more")+ .doesNotExist("show more is hidden");+ assert.dom(".post-action-menu__bookmark").exists("bookmark is displayed");+ assert.dom(".post-action-menu__copy-link").exists("copyLink is displayed");+ });+test("post-menu-toggle-like-action behavior transformer", async function (assert) {withPluginApi("2.0.0", (api) => {api.registerBehaviorTransformer("post-menu-toggle-like-action", () => {@@ -39,4 +69,53 @@ module("Unit | Component | post-menu", function (hooks) {"behavior transformer was called");});++ module("post-menu value transformer", function () {+ test("context/collapsedButtons: allows handling which buttons are collapsed", async function (assert) {+ this.siteSettings.post_menu_hidden_items = "bookmark|copyLink";++ // without the transformer+ const post = this.post; // using this inside the template does not correspond to the test `this` context+ await render(<template><PostMenu @post={{post}} /></template>);+ assert+ .dom(".post-action-menu__show-more")+ .exists("show more is displayed");+ assert.dom(".post-action-menu__like").exists("like is displayed");+ assert+ .dom(".post-action-menu__bookmark")+ .doesNotExist("bookmark is hidden");+ assert+ .dom(".post-action-menu__copy-link")+ .doesNotExist("copyLink is hidden");+ assert.dom(".post-action-menu__share").exists("share is displayed");++ // with the transformer+ withPluginApi((api) => {+ api.registerValueTransformer(+ "post-menu-buttons",+ ({ context: { collapsedButtons } }) => {+ collapsedButtons.default("like");+ collapsedButtons.show("bookmark");+ collapsedButtons.hide("share");+ collapsedButtons.default("copy-link");+ }+ );+ });++ await render(<template><PostMenu @post={{post}} /></template>);+ assert+ .dom(".post-action-menu__show-more")+ .exists("show more is displayed");+ assert.dom(".post-action-menu__like").exists("like still is displayed");+ assert+ .dom(".post-action-menu__bookmark")+ .exists("bookmark is now displayed");+ assert+ .dom(".post-action-menu__copy-link")+ .doesNotExist("copyLink is still hidden");+ assert+ .dom(".post-action-menu__share")+ .doesNotExist("share is now hidden");+ });+ });});