Converter o botão Novo Tópico para ter um submenu

Eu fiz algo parecido na semana passada…

Em um tema, você pode adicionar isso a javascripts/discourse/components/new-topic-dropdown.js

import { action } from "@ember/object";
import { getOwner } from "discourse-common/lib/get-owner";
import Composer from "discourse/models/composer";
import DropdownSelectBoxComponent from "select-kit/components/dropdown-select-box";
import { computed } from "@ember/object";

export default DropdownSelectBoxComponent.extend({
  classNames: ["new-topic-dropdown"],

  selectKitOptions: {
    icons: ["plus"],
    showFullTitle: true,
    autoFilterable: false,
    filterable: false,
    showCaret: true,
    none: "topic.create",
  },

  content: computed(function () {
    const items = [
      {
        id: "new_discussion",
        name: "Novo Tópico",
        description: "Iniciar uma nova discussão",
        icon: "comment",
      },
    ];

    items.push({
      id: "new_ticket",
      name: "Ticket de Suporte",
      description: "Iniciar uma solicitação de suporte",
      icon: "tag",
    });

    return items;
  }),

  @action
  onChange(selectedAction) {
    const composerController = getOwner(this).lookup("controller:composer");

    let tags = null;
    let categoryId = this.category ? this.category.id : null;

    if (selectedAction === "new_ticket") {
      tags = "ticket";
    }

    composerController.open({
      action: Composer.CREATE_TOPIC,
      draftKey: Composer.DRAFT,
      categoryId: categoryId,
      tags: tags,
    });
  },
});

Isso adiciona as opções “novo tópico” e “ticket de suporte” a um botão suspenso… “novo tópico” abre o composer como de costume, e “ticket de suporte” abre o composer com a tag “ticket” já adicionada.

…e então em javascripts/discourse/connectors/after-create-topic-button/new-topic-dropdown.hbs

{{#if currentUser}}
  {{new-topic-dropdown category=category}}
{{/if}}

…então, com algum CSS, você provavelmente vai querer ocultar o botão padrão:

.navigation-controls {
  #create-topic {
    display: none;
  }
}
19 curtidas