Opening a reply window via URL

I use a simple theme component for this in my instance. Below is an adaptation from mine – which also accepts #upload (a new reply opening the upload window on desktop) and #edit (for wiki OPs) in addition to #reply.

It needs some work, like avoiding setTimeout and correctly managing draftSequence (I don’t think topic.draft_sequence is right), and I don’t know what would be the best practice here. That said, it’s working fine for me.

Navigating to any topic like /t/[slug]/[id]#reply will open the composer with a new empty reply.

If you need some pre-filled text it is doable by setting the reply attribute on the object passed to composer.open. Plus, if there’s already a draft for that topic, it will ask if you want to save/discard it before creating this new reply – draftSequence needs to be correct if the desired behavior is resuming that draft.

<script type="text/discourse-plugin" version="0.4">
  if (/.*#reply$/g.test(document.URL)) {
    const { REPLY } = require('discourse/models/composer').default;
    
    const composer = Discourse.__container__.lookup('controller:composer');
    
    setTimeout(function() {
      const topic = Discourse.__container__.lookup("controller:topic").get("model");
      if (topic) {
        composer.open({
            action: REPLY,
            draftKey: topic.draft_key,
            draftSequence: topic.draft_sequence,
            topic,
        });
      }
    }, 0)
  }
</script>

I hope that helps.

12 Likes