Comment remplacer la fonction 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 « J'aime »

Désolé de déterrer un vieux sujet, mais est-ce que cela fonctionne toujours pour vous / avez-vous dû le mettre à jour ? J’ai essayé de faire fonctionner cela et je n’arrive pas à le déclencher du tout, mais j’ai eu un très mauvais moment avec tout ce qui concerne modifyClass.

Je viens de l’essayer, et cela fonctionne toujours :+1:

Je l’ai littéralement copié-collé dans mon thème, et cela a fonctionné sans aucune modification.

1 « J'aime »

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

Je ne suis pas sûr si cela a été résolu ?

Voir aussi, lié mais différent : Issues overriding getters in a controller (3.0.0)

2 « J'aime »

Merci @merefield et @Canapin. J’ai essayé d’apprendre l’API des plugins Discourse en trifouillant ce qui me semble être des choses simples comme celle-ci, mais modifyClass est tellement incohérent quant à son fonctionnement. J’ai un petit composant de thème (celui que vous avez mentionné @merefield) où deux appels à modifyClass fonctionnent mais un troisième ne fonctionne pas, c’est très frustrant d’essayer de comprendre si j’ai fait quelque chose de mal ou si c’est juste modifyClass.

Désolé pour le hors-sujet sur votre topic et merci pour la réponse rapide @Canapin.

Mise à jour :

1 « J'aime »

Ne t’inquiète pas, je ne sais pas ce que signifie HS mais nous sommes là pour aider !

1 « J'aime »

Ce sujet a été automatiquement fermé 30 jours après la dernière réponse. Les nouvelles réponses ne sont plus autorisées.