Как настроить заголовок с помощью плагина?

Я пытаюсь создать плагин, который изменит заголовок примерно вот так:


Внешний вид не так важен; главное — я не хочу использовать кастомизацию через путь /admin/customize/css_html. Я хочу создать плагин, который магическим образом изменит всё необходимое.

Из того, что я пока обнаружил, следует, что для заголовка нет шаблона; он рендерится через виртуальный DOM.

Процесс рендеринга выглядит следующим образом:

  1. Шаблон assets/javascripts/discourse/templates/application.hbs, который можно переопределить, рендерит компонент site-header.

    {{plugin-outlet “above-site-header”}}
    {{site-header canSignUp=canSignUp
    showCreateAccount=“showCreateAccount”
    showLogin=“showLogin”
    showKeyboard=“showKeyboardShortcutsHelp”
    toggleMobileView=“toggleMobileView”
    toggleAnonymous=“toggleAnonymous”
    logout=“logout”}}
    {{plugin-outlet “below-site-header”}}

  2. Компонент assets/javascripts/discourse/components/site-header.js.es6 расширяет и возвращает виджет header.

    const SiteHeaderComponent = MountWidget.extend(Docking, {
    widget: ‘header’,
    ..
    });

    export default SiteHeaderComponent;

  3. Виджет assets/javascripts/discourse/widgets/header.js.es6 генерирует виртуальный DOM для заголовка.

    export default createWidget(‘header’, {
    tagName: ‘header.d-header.clearfix’,

    html(attrs, state) {}
    });

Мои мысли

Я могу создать свой собственный виджет my-header, который будет расширять существующий header; там я просто обновлю метод html. Я не хочу ничего копировать туда; я хочу просто переопределить метод html().

/plugins/discourse-foo/assets/javascripts/discourse/widgets/my-header.js.es6

Затем я могу создать свой собственный компонент my-site-header, который будет расширять существующий site-header, просто переопределив его так, чтобы он указывал на виджет my-header.

/plugins/discourse-foo/assets/javascripts/discourse/components/my-site-header.js.es6

Затем я переопределю шаблон application, чтобы показывать компонент my-site-header вместо site-header.

/plugins/discourse-foo/assets/javascripts/discourse/templates/application.hbs

{{plugin-outlet "above-site-header"}}
{{my-site-header canSignUp=canSignUp
              showCreateAccount="showCreateAccount"
              showLogin="showLogin"
              showKeyboard="showKeyboardShortcutsHelp"
              toggleMobileView="toggleMobileView"
              toggleAnonymous="toggleAnonymous"
              logout="logout"}}
{{plugin-outlet "below-site-header"}}

Вопрос в том, как можно расширить все эти компоненты и виджеты? Не могли бы вы указать мне правильный путь для этого? Спасибо.

Based on the screenshot, this seems like something that is plausible to do with just CSS customizations. That would also be much simpler than writing a plugin and would not require updating with new versions of Discourse code.

Well, I knew you going to say that, that’s why I mentioned that I need a plugin :slight_smile:

First of all, because I want to learn how to do plugins, I’ll have to do whole lot more complicated things later. That menu is just an example.

Secondly, I really need that menu to be a plugin, because:

  1. We may reuse it on different projects;
  2. We need a version control, to roll back sometimes;
  3. Some links will be generated from external json;
  4. I want a clean solution. I believe doing it with a plugin is a clean solution;

Sitepoint use a plugin to override the header.
Take a look here:

Thank you for your link, it will be definitely interesting to see how other people are doing it.

As I see now, they are not extending existing header widget. They just copy-pasted it from the core and customised it.
In that case they have to support that code themselves, and Discourse most likely to break it on next update.