如何覆盖 buildQuote 函数?

Hi!

I’d like to try to modify the buildQuote function so it adds a new line before the opening [quote] tag.

Basically, modifying this:

  return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

to this:

  return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;

Is it possible to do such a thing with a theme component? How do I override this function? :smiley:

I’ve managed to achieve it, but is there a prettier solution?

<script type="text/discourse-plugin" version="0.8.23">
    function buildQuote(post, contents, opts = {}) {
      if (!post || !contents) {
        return "";
      }
    
      const params = [
        opts.username || post.username,
        `post:${opts.post || post.post_number}`,
        `topic:${opts.topic || post.topic_id}`
      ];
    
      if (opts.full) params.push("full:true");
    
      return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
    }
    api.modifyClass('controller:composer', {
        actions: {
            importQuote(toolbarEvent) {
              const postStream = this.get("topic.postStream");
              let postId = this.get("model.post.id");
        
              // If there is no current post, use the first post id from the stream
              if (!postId && postStream) {
                postId = postStream.get("stream.firstObject");
              }
        
              // If we're editing a post, fetch the reply when importing a quote
              if (this.get("model.editingPost")) {
                const replyToPostNumber = this.get("model.post.reply_to_post_number");
                if (replyToPostNumber) {
                  const replyPost = postStream.posts.findBy(
                    "post_number",
                    replyToPostNumber
                  );
        
                  if (replyPost) {
                    postId = replyPost.id;
                  }
                }
              }
        
              if (postId) {
                this.set("model.loading", true);
        
                return this.store.find("post", postId).then(post => {
                  const quote = buildQuote(post, post.raw, {
                    full: true
                  });
        
                  toolbarEvent.addText(quote);
                  this.set("model.loading", false);
                });
              }
            }
        }
    });
</script>
2 个赞

抱歉打扰了旧话题,但这对你来说仍然有效吗/你是否需要更新它?我一直在尝试让它生效,但它根本无法触发,而且我在处理任何与 modifyClass 相关的事情时都遇到了麻烦。

我刚试了一下,它仍然有效 :+1:

我把它直接复制粘贴到我的主题中,它就能正常工作,无需任何修改。

1 个赞

请参阅 api.modifyClass sometimes(!) not working - #12 by RGJ

我不确定这是否已解决?

另请参阅相关但不同的内容:Issues overriding getters in a controller (3.0.0)

2 个赞

感谢 @merefield@Canapin。我一直在尝试学习 Discourse 插件 API,摆弄一些感觉上应该很简单的事情,比如这个,但 modifyClass 在何时/何时不起作用方面非常不一致。我有一个小主题组件(就是你提到的那个 @merefield),其中两个 modifyClass 调用有效,但第三个无效,弄清楚是我做错了什么还是仅仅是 modifyClass 的问题,非常令人沮丧。

抱歉在你的话题上跑题了,感谢 @Canapin 的快速回复。

更新:

1 个赞

没关系,我不知道 OT 是什么意思,但我们在这里提供帮助!

1 个赞

此主题在上次回复后 30 天自动关闭。不再允许回复。