שינויים ב-Discourse Plugin API: שימוש ב- api.registerValueTransformer עבור addPostMenuButton ו- component:topic-list

סיכום מבוסס בינה מלאכותית

omppatil is developing a plugin for Discourse and encountered API changes affecting addPostMenuButton and component:topic-list in version 3.2.0.beta4-dev (plugin API 0.8.7). They asked how to migrate these to use api.registerValueTransformer and which plugin API version to target for compatibility. Their existing code for both functions is shared for context. In post 2, they updated to Discourse 3.5.0.beta2-dev (plugin API 2.1.1) and repeated the question. merefield directed them to a meta topic explaining upcoming post-menu changes. In post 4, omppatil confirmed they read the topic, implemented the new API changes, and resolved the component:topic-list issue — implying progress, though the addPostMenuButton migration via registerValueTransformer remains unaddressed in the thread.

{“content”:"שלום, אני משתמש בגרסה 3.2.0.beta4-dev לפיתוח מקומי וגרסת API תוספים 0.8.7 לפיתוח תוספים. עם זאת, בעקבות שינויים אחרונים ב-Discourse, הפונקציות addPostMenuButton ו-component:topic-list שונו. איך אפשר להשתמש ב-api.registerValueTransformer עבור פונקציות API אלה? בנוסף, באיזו גרסת 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 ? "Hide Post" : "Show Post",
  };
});

והנה הקוד שלי עבור 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++;
          });
        }
      }
    }
  }
});

האם מישהו יכול לעזור לי בכך?"}

I updated my local development Discourse to version 3.5.0.beta2-dev, and the plugin API version is 2.1.1. How can I use api.registerValueTransformer for the addPostMenuButton and component:topic-list functions?

Have you read the Topic?: Upcoming post menu changes - How to prepare themes and plugins

Yes, I have read about that topic, implemented changes based on the new plugin API updates, and figured out component:topic-list as well.