跳转到主要内容

插件出口连接器

Discourse 包含数百个插件出口,可用于在 Discourse 界面中注入新内容或替换现有内容。"出口参数"会被提供,以便根据上下文自定义内容。

选择出口

要查找插件出口的名称,可以在 Discourse 核心中搜索 "<PluginOutlet",或者使用 插件出口位置 主题组件。(例如 topic-above-posts)。

包装出口

核心中的一些出口形如 <PluginOutlet @name="foo" />。这些出口允许你注入新内容。其他出口会像这样"包装"一个现有的核心实现

<PluginOutlet @name="foo">
  core implementation
</PluginOutlet>

为这类"包装"出口定义连接器会替换核心实现。对于一个包装插件出口,只能有一个已启用的主题/插件提供连接器。

对于包装插件出口,你可以使用 {{yield}} 关键字渲染原始的核心实现。如果你只想在某些条件下替换核心实现,或者想将它包装在其他内容中,这会很有帮助。

定义连接器

选好出口后,为你的连接器确定一个名称。该名称在给定社区安装的所有主题/插件中必须唯一。例如 brand-official-topics

在你的主题/插件中,定义一个新的 .gjs 连接器,路径格式如下:

:art: {theme}/javascripts/discourse/connectors/{outlet-name}/{connector-name}.gjs

:electric_plug: {plugin}/assets/javascripts/discourse/connectors/{outlet-name}/{connector-name}.gjs

这些文件的内容会被渲染为一个 Ember 组件。关于 Ember 和 .gjs 格式的一般信息,查看 Ember 指南。

对于我们假设的 "brand official topics" 连接器,文件可能如下所示

<template>
  <div class="alert alert-info">
    This topic was created by a member of the
    <a href="https://discourse.org/team">Discourse Team</a>
  </div>
</template>

[quote] [details=ℹ️ 旧版包装元素] 过去,连接器使用 .hbs 文件编写。使用这些文件时,插件出口可能会自动引入一个包装元素。元素类型由 <PluginOutlet /> 上的 @connectorTagName 定义。

现代的基于 .gjs 的连接器可以完全控制自己的 DOM。不会自动引入包装元素。 [/details] [/quote]

使用出口参数

插件出口通过 @outletArgs 提供周围上下文的信息。传递给每个出口的参数各不相同。查看参数的一个简单方法是在模板中添加以下内容:

{{log @outletArgs}}

这会将参数记录到浏览器的开发者控制台。它们会显示为一个 Proxy 对象,要查看参数列表,展开该代理的 [[Target]]。

在我们的 topic-above-posts 示例中,渲染的话题可以通过 @outletArgs.model 获取。因此我们可以像这样添加团队成员的用户名:

<template>
  <div class="alert alert-info">
    This topic was created by
    {{@outletArgs.model.details.created_by.username}}
    (a member of the
    <a href="https://discourse.org/team">Discourse Team</a>)
  </div>
</template>

[quote] [details=ℹ️ 访问参数的旧版方式] 在许多插件出口中,默认可以使用 {{argName}} 或 {{this.argName}} 访问参数。目前,这在现有出口中仍然有效。

新的插件出口(带有 @defaultGlimmer={{true}})将连接器渲染为 "template only glimmer components",它们没有 this 上下文。最终,现有的插件出口也将迁移到这种模式。@outletArgs 技术是最佳选择,因为它在经典和 glimmer 插件出口中都能一致地工作。 [/details] [/quote]

添加更复杂的逻辑

有时,简单的模板不够用。要向连接器添加 Javascript 逻辑,将你的 .gjs 文件升级为导出一个基于类的组件。其功能与任何其他组件定义相同,并且可以包含服务注入。

在我们的 topic-above-posts 示例中,我们可能希望根据 'prioritize username in ux' 站点设置以不同方式渲染用户。.gjs 文件可能如下所示:

.../connectors/topic-above-posts/brand-official-topic.gjs:

import Component from "@glimmer/component";
import { service } from "@ember/service";

export default class BrandOfficialTopics extends Component {
  @service siteSettings;

  get displayName() {
    const user = this.args.outletArgs.model.details.created_by;
    if (this.siteSettings.prioritize_username_in_ux) {
      return user.username;
    } else {
      return user.name;
    }
  }

  <template>
    <div class="alert alert-info">
      This topic was created by
      {{this.displayName}}
      (a member of the
      <a href="https://discourse.org/team">Discourse Team</a>)
    </div>
  </template>
}

[quote] [details=ℹ️ 定义复杂逻辑的旧版方式] 在 Discourse 的旧版本中,连接器被定义为一个 .hbs 模板加上一个相邻的 .js 文件,并且无法导出自定义组件定义。作为替代,你可以导出一个带有 setupComponent(args, component) 和 teardownComponent(component) 函数的对象。这些旧技术尚未被正式弃用,但我们建议今后切换到带有基于类组件的单个 .gjs 文件。 [/details] [/quote]

条件渲染

如果你只想让内容在某些条件下渲染,通常只需用 handlebars {{#if}} 块包装你的模板即可。如果这还不够,你可以使用 shouldRender 钩子来控制连接器模板是否渲染。

首先,确保你有一个如上所述的基于类的 .gjs 连接器。然后,添加一个 static shouldRender() 函数。扩展我们的示例:

import Component from "@glimmer/component";

export default class BrandOfficialTopics extends Component {
  static shouldRender(outletArgs, helper) {
    const firstPost = outletArgs.model.postStream.posts[0];
    return firstPost.primary_group_name === "team";
  }
  // ... (any other logic)

  <template>
    {{! ... }}
  </template>
}

现在,只有当话题的第一个帖子由团队成员创建时,连接器才会渲染。

shouldRender 在 Glimmer 自动跟踪上下文中求值。之后对任何被引用属性(例如 outletArgs)的更改都会导致该函数重新求值。

[quote] [details=ℹ️ 旧版 shouldRender 实现] 自动跟踪: 在 Discourse 3.1 之前,shouldRender 只会在初始渲染时求值。对被引用属性的更改不会导致该函数重新求值。

非类语法: 目前,在普通(非类)javascript 对象中定义 shouldRender 函数仍然受支持,但我们建议今后转向基于类或基于 templateOnly 的语法。 [/details] [/quote]

介绍新的出口

如果你需要一个尚不存在的出口,请随时提交拉取请求,或在 #dev 中开启一个话题。