Discourse 插件 API 更改:使用 api.registerValueTransformer 为 addPostMenuButton 和 component:topic-list

你好,我正在为本地开发使用版本3.2.0.beta4-dev和插件API版本0.8.7进行插件开发。然而,由于Discourse的近期变化,addPostMenuButtoncomponent:topic-list函数已经被修改。

我如何可以为这些API函数使用api.registerValueTransformer?此外,我应该使用哪个插件API版本来匹配3.2.0.beta4-dev,以确保与api.registerValueTransformer的兼容性?

以下是我的addPostMenuButton的代码:

api.addPostMenuButton("togglePostButton", (model) => {
  const siteSettings = api.container.lookup("site-settings:main");
  const posts = siteSettings.posts;
  const array_of_posts = posts.split("|");
  const postId = model.id;
  const postIdstr = postId.toString();
  const isPost = array_of_posts.includes(postIdstr);

  return {
    action: "togglePost",
    position: "first",
    className: isPost
      ? "button.topic_hidden custom-class-hidden"
      : "button.topic_visible custom-class-visible",
    icon: isPost ? "far-eye-slash" : "far-eye",
    title: isPost ? "隐藏帖子" : "显示帖子",
  };
});

以及component:topic-list的代码:

api.modifyClass("component:topic-list", {
  pluginId: "post-toggle",
  didInsertElement: function () {
    this._super();
    const topics = this.topics;
    for (let key in topics) {
      if (topics.hasOwnProperty(key)) {
        let newTopicLatest = [];
        const topic_id = topics[key].id;
        const topic_posters = topics[key].posters;

        topic_posters.forEach((poster) => {
          if (poster && poster.extras !== null && poster.extras.includes("latest")) {
            newTopicLatest = poster["description"].split(",");
          }
        });

        let userListLatest = newTopicLatest;
        if (userListLatest.length > 1 && userListLatest[0] === "Original Poster") {
          userListLatest = userListLatest.slice(1);
          userListLatest[0] = userListLatest[0].trim();
        }

        let i = 0;
        const element = $('tr[data-topic-id="' + topic_id + '"]').find(
          "a[data-user-card]"
        );

        if (element.length > 0) {
          element.each(function () {
            const poster_inside = topic_posters[i];
            if (poster_inside !== undefined && Object.keys(poster_inside).length !== 0) {
              const userElement = $(this);
              if (!userListLatest.includes(poster_inside.user.id.toString())) {
                userElement.hide();
              }
            }
            i++;
          });
        }
      }
    }
  }
});

有人能帮我解决这个问题吗?

我已将本地开发的 Discourse 更新到 3.5.0.beta2-dev 版本,插件 API 版本为 2.1.1。如何使用 api.registerValueTransformer 来处理 addPostMenuButtoncomponent:topic-list 函数?

您是否阅读了该主题? Upcoming post menu changes - How to prepare themes and plugins

3 个赞

是的,我已经阅读了该主题,并根据新的插件 API 更新实施了更改,也弄懂了 component:topic-list

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.