跳转到主要内容

话题列表自定义

topic-list 是 Discourse 中使用最多、定制最多的用户界面之一。主题和插件开发者可以使用许多不同的工具来实现这种定制,每种工具都有各自的优点、缺点和维护考量。

基于 CSS 的定制

最安全也最常见的定制方法是 CSS。与任何其他 Discourse 定制一样:你的修改越复杂,主题或插件中可能需要的维护就越多。

关于 CSS 定制的通用指南可在 设计师指南 中找到。

添加、移除和重新排列列

在桌面上,话题列表渲染为表格。在移动设备上,每个话题行使用独立的单一单元格布局,因此 topic-list-columns 仅影响桌面表格。

可以使用 插件 API 通过 topic-list-columns Value Transformer 自定义桌面列。这是在不接管整行标记的情况下添加、移除、替换或重新排列列的最精细方式。

topic-list-columns 是一个可变的转换器。核心代码构建一个包含各列的 DAG,把这个可变对象传递给已注册的转换器,之后再对其进行解析。实际使用中,这意味着你应该直接修改 columns 对象。

转换器回调目前接收:

  • context.listContext
  • context.category
  • context.filter

当前核心列的密钥有:

  • bulk-select(启用批量选择时)
  • topic
  • posters(启用发帖人头像时)
  • replies
  • likes 或 op-likes(取决于当前排序顺序)
  • views
  • activity

每个列的值可以定义:

  • header:在表格标题中为该列渲染的组件
  • item:在每个桌面行中为该列渲染的组件

DAG 实例支持以下方法:

  • add(key, value, position?)
  • delete(key)
  • replace(key, value, position?)
  • reposition(key, position)

调用 replace() 时,如果你的替换只提供其中之一,核心代码会保留现有的 header 或 item。

标题单元格组件目前接收以下参数:

参数 描述
@sortable 该列是否应表现为可排序的标题
@activeOrder 当前有效的排序顺序
@changeSort 用于更改排序的回调
@ascending 当前排序是否为升序
@category 当前类别(可用时)
@name 标题使用的翻译密钥或标签
@bulkSelectEnabled 批量选择模式是否有效
@showBulkToggle 是否应显示批量选择切换按钮
@canBulkSelect 批量选择是否可用
@canDoBulkActions 批量操作当前是否可用
@bulkSelectHelper 批量选择辅助对象

条目单元格组件目前接收以下参数:

参数 描述
@topic 当前话题
@bulkSelectEnabled 批量选择模式是否有效
@onBulkSelectToggle 批量选择复选框使用的回调
@isSelected 话题当前是否被选中
@showTopicPostBadges 是否应显示未读/新徽章
@hideCategory 是否应隐藏类别标记
@tagsForUser 当前的标签公开范围上下文
@expandPinned 是否应显示已置顶摘录

此 API 的当前源代码位于 frontend/discourse/app/components/topic-list/list.gjs、frontend/discourse/app/components/topic-list/header.gjs 和 frontend/discourse/app/components/topic-list/item.gjs。

以下示例在桌面话题列表中添加、移除和重新定位列:

// .../discourse/api-initializers/my-topic-list-customization.gjs

import { apiInitializer } from "discourse/lib/api";

const StaffHeaderCell = <template>
  <th>Staff?</th>
</template>;

const StaffItemCell = <template>
  <td>{{if @topic.creator.staff "✅"}}</td>
</template>;

export default apiInitializer((api) => {
  api.registerValueTransformer(
    "topic-list-columns",
    ({ value: columns, context }) => {
      // Remove the core column which shows poster avatars:
      columns.delete("posters");

      // Swap the "replies" and "views" columns:
      columns.reposition("views", { before: "replies" });

      if (context.category?.slug === "announcements") {
        columns.add(
          "created-by-staff",
          {
            header: StaffHeaderCell,
            item: StaffItemCell,
          },
          { before: "activity" }
        );
      }
    }
  );
});

通过插件出口引入内容

当你想在不接管整行结构的前提下注入内容时,推荐使用插件出口。

话题列表中最有用的出口包括:

出口 用途 出口参数
before-topic-list-body / after-topic-list-body 在表格正文之前或之后添加标记 topics, selected, bulkSelectEnabled, lastVisitedTopic, discoveryList, hideCategory
after-topic-list-item 在每一行之后插入额外的 <tr> topic, index
topic-list-item 包裹或替换整行 topic, bulkSelectEnabled, onBulkSelectToggle, isSelected, hideCategory, tagsForUser, showTopicPostBadges, navigateToTopic
topic-list-topic-cell-link-top-line 扩展桌面端的标题/状态行 topic, tagsForUser
topic-list-topic-cell-link-bottom-line 扩展桌面端的类别/标签/元数据行 topic, tagsForUser
topic-list-item-mobile-avatar / topic-list-item-mobile-bumped-at 仅限移动端的自定义 topic
topic-list-before-reply-count / topic-list-before-view-count / topic-list-before-relative-date 在计数/日期单元格附近添加内容 topic
topic-list-heading-bottom 在可排序的表头单元格内添加内容 name, bulkSelectEnabled

frontend/discourse/app/components/topic-list/ 中还有更多出口,尤其是在 item/topic-cell.gjs、item/replies-cell.gjs、item/views-cell.gjs 和 item/activity-cell.gjs 中。

替换整个 topic-list-item

如果想完全替换核心的行实现,请使用 topic-list-item 包装出口。只有当你的设计与核心差异极大,不希望未来的核心变更影响它,并且不需要与其他主题或插件兼容时,才应这样做。

核心有意不将 @columns 传入该包装出口。完整替换行意味着你需要自行接管行结构。

如果采用这种策略,应格外注意确保代码经过充分测试,并确保你的主题/插件用户了解相关注意事项。

通过 Transformers 和主题修饰器进行其他微调

许多 Transformers 允许对 topic-list 实现做出小幅、有针对性的更改:

  • topic-list-columns(可变上下文:{ listContext, category, filter })- 修改桌面列 DAG
  • topic-list-class(上下文:{ topics, listContext })- 返回要添加到话题列表 <table> 的类名
  • topic-list-item-class(上下文:{ topic, index, listContext })- 返回要添加到每个话题行的类名
  • topic-list-item-style(上下文:{ topic, index, listContext })- 返回一个 htmlSafe CSS 声明数组,这些声明会被拼接到该行的内联 style 中
  • topic-list-item-expand-pinned(上下文:{ topic, mobileView })- 控制是否显示话题摘要
  • topic-list-item-mobile-layout(上下文:{ topic, listContext })- 选择某一行是否使用移动布局
  • topic-list-header-sortable-column - 覆盖标题单元格是否可排序,例如基于当前类别

还有一个用于行点击的行为 transformer:

  • topic-list-item-click(上下文:{ event, topic, listContext, navigateToTopic })- 包装或覆盖默认的点击行为。应使用 api.registerBehaviorTransformer(...) 注册,而非 api.registerValueTransformer(...)。

例如,topic-list-item-style 必须返回 htmlSafe 字符串:

// .../discourse/api-initializers/topic-list-style.gjs

import { htmlSafe } from "@ember/template";
import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  api.registerValueTransformer(
    "topic-list-item-style",
    ({ value, context }) => {
      if (context.topic.pinned) {
        value.push(htmlSafe("outline: 2px solid var(--primary-low);"));
      }

      return value;
    }
  );
});

一些 主题修饰器 也与话题列表相关,因为它们会更改序列化时包含的数据:

  • serialize_topic_excerpts(boolean,默认值 false)- 序列化话题列表时始终包含摘要
  • topic_thumbnail_sizes(array)- 在话题缩略图集中请求额外的分辨率
  • serialize_topic_op_likes_data(boolean)- 包含第一个帖子的点赞数据,例如 op_like_count、op_can_like、op_liked 和 first_post_id
  • serialize_topic_is_hot(boolean)- 在话题列表项中包含 is_hot