主题页脚按钮API

Discourse 2.3 中即将推出的新 API 将允许您在主题的底部添加按钮,既可以作为内联按钮,也可以添加到 {{topic-footer-mobile-dropdown}} 中,而无需重复逻辑。

用法

api.registerTopicFooterButton({
    // 按钮的 ID,必需
    id: null,

    // 按钮上显示的图标
    icon: null,

    // 标题属性的本地化键路径
    title: null,

    // 已翻译的标题
    translatedTitle: null,

    // 标签的本地化键路径
    label: null,

    // 已翻译的标签
    translatedLabel: null,

    // 此按钮是显示在移动下拉菜单中还是作为内联按钮?
    dropdown: false,

    // 附加到按钮的 CSS 类
    classNames: [],

    // 应强制刷新按钮状态的计算属性
    // 例如:["topic.bookmarked", "topic.category_id"]
    dependentKeys: [],

    // 是否显示此按钮?
    displayed: true,

    // 此按钮是否被禁用?
    disabled: false,

    // 显示顺序,数值越大越靠前
    priority: 0
  }
});

除了 id 之外的所有键,既可以是值,也可以是函数:

icon: "user"
icon() {
  // 这里的 this 是 topic-footer-buttons 组件的上下文
  // 允许您访问
  // 例如 topic 模型或 this.site
  return this.get("topic.some_icon_variable");
}

重点关注 dependentKeys

dependentKeys 视为您构建按钮所需的所有计算属性。当这些键中的任何一个发生变化时,按钮将重新渲染。基本上,在定义按钮时,只要您执行了 this.get("something"),那么 something 就应该包含在 dependentKeys 中。

重点关注 dropdown

dropdown 键将允许您定义希望按钮是作为内联按钮添加,还是添加到 {{topic-footer-mobile-dropdown}} 中。

例如,一个在桌面端内联创建按钮而在移动端下拉菜单中创建按钮的实现,其行为如下:

dropdown() { return this.site.mobileView; }

注意:目前,下拉菜单仅会在移动端显示。因此,如果您永远不想在下拉菜单中显示它,或者在展示移动端视图时少数情况下希望强制在桌面端显示,这可能会很有用。

一些实际示例

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/initializers/topic-footer-buttons.js.es6#L7

https://github.com/discourse/discourse-code-review/blob/master/assets/javascripts/discourse/initializers/init-code-review.js.es6#L78

19 个赞