Como substituir a função 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 curtidas

Desculpe por desenterrar um tópico antigo, mas isso ainda está funcionando para você/você teve que atualizar alguma coisa? Tenho tentado fazer isso funcionar e não consigo fazer com que ele dispare, mas tenho tido um tempo terrível com qualquer coisa relacionada a modifyClass.

Acabei de tentar, e ainda está funcionando :+1:

Eu literalmente copiei e colei no meu tema, e funcionou sem nenhuma modificação.

1 curtida

Veja api.modifyClass sometimes(!) not working - #12 by RGJ

Não tenho certeza se isso foi resolvido?

Veja também relacionado, mas diferente: Issues overriding getters in a controller (3.0.0)

2 curtidas

Obrigado @merefield e @Canapin. Tenho tentado aprender a API de plugins do Discourse mexendo com o que parece que deveriam ser coisas simples como esta, mas modifyClass é tão inconsistente quando funciona/não funciona. Tenho um pequeno componente de tema (aquele que você mencionou @merefield) onde duas chamadas modifyClass funcionam, mas uma terceira não, é muito frustrante tentar descobrir se fiz algo errado ou se é apenas modifyClass.

Desculpe pelo off-topic no seu tópico e obrigado pela resposta rápida @Canapin.

Atualização:

1 curtida

Sem problemas, não sei o que significa “OT”, mas estamos aqui para ajudar!

1 curtida

Este tópico foi fechado automaticamente 30 dias após a última resposta. Novas respostas não são mais permitidas.