FEATURE: allows to create new topic and set categoryId

FEATURE: allows to create new topic and set categoryId

Usage:

[wrap=template key="my_template" action="create" categoryId="2"]
Todolist:
- foo
- bar
[/wrap]
diff --git a/javascripts/discourse/initializers/init-discourse-reply-template-component.js.es6 b/javascripts/discourse/initializers/init-discourse-reply-template-component.js.es6
index 66dd517..451d995 100644
--- a/javascripts/discourse/initializers/init-discourse-reply-template-component.js.es6
+++ b/javascripts/discourse/initializers/init-discourse-reply-template-component.js.es6
@@ -3,7 +3,10 @@ import { getOwner } from "discourse-common/lib/get-owner";
 import { withPluginApi } from "discourse/lib/plugin-api";
 import Composer from "discourse/models/composer";
 
-function buildButton(label, extraClass) {
+function buildButton(dataset, extraClass) {
+  const action = dataset.action || "reply";
+  const label = dataset.label;
+
   const button = document.createElement("button");
   button.classList.add("add-template", "btn", "btn-default", "btn-primary");
 
@@ -13,18 +16,20 @@ function buildButton(label, extraClass) {
 
   button.innerText =
     label ||
-    I18n.t(themePrefix("discourse_reply_template_component.use_template"));
+    I18n.t(
+      themePrefix(`discourse_reply_template_component.use_template_${action}`)
+    );
 
   return button;
 }
 
-function openComposerWithTemplate(controller, post, key) {
+function openComposerWithTemplateAndAction(controller, post, dataset) {
   return ajax(`/posts/${post.id}`, {
     cache: false
   }).then(data => {
     const regex = new RegExp(
       '\\[wrap=template.*?\\skey="' +
-        key +
+        dataset.key +
         '".*?\\]\\n((?:.|\n)*?)\\n\\[\\/wrap\\]',
       "gm"
     );
@@ -64,18 +69,39 @@ function openComposerWithTemplate(controller, post, key) {
     ];
 
     if (match && match[1]) {
+      let topicBody;
       replacers.forEach(replacer => {
-        match[1] = match[1].replace(replacer.regex, replacer.fn);
+        topicBody = match[1].replace(replacer.regex, replacer.fn);
       });
 
-      controller.open({
-        action: Composer.REPLY,
-        topicBody: match[1],
+      const controllerOptions = {
+        topicBody,
         draftKey: controller.topicModel.draft_key,
         draftSequence: controller.topicModel.draftSequence,
-        topic: post.topic,
-        skipDraftCheck: true
-      });
+        skipDraftCheck: true,
+        categoryId: dataset.categoryId || null
+      };
+
+      if (dataset.action && dataset.action === "create") {
+        controller.open(
+          Object.assign(
+            {
+              action: Composer.CREATE_TOPIC
+            },
+            controllerOptions
+          )
+        );
+      } else {
+        controller.open(
+          Object.assign(
+            {
+              action: Composer.REPLY,
+              topic: post.topic
+            },
+            controllerOptions
+          )
+        );
+      }
     }
   });
 }
@@ -108,21 +134,29 @@ export default {
                 );
               }
 
-              const label = wrap.dataset.label;
-
               if ((wrap.innerText.match(/\n/g) || []).length >= 20) {
-                const topButton = buildButton(label, "top");
+                const topButton = buildButton(wrap.dataset, "top");
                 topButton.addEventListener(
                   "click",
-                  openComposerWithTemplate.bind(null, controller, post, key)
+                  openComposerWithTemplateAndAction.bind(
+                    null,
+                    controller,
+                    post,
+                    wrap.dataset
+                  )
                 );
                 wrap.prepend(topButton);
               }
 
-              const bottomButton = buildButton(label, "bottom");
+              const bottomButton = buildButton(wrap.dataset, "bottom");
               bottomButton.addEventListener(
                 "click",
-                openComposerWithTemplate.bind(null, controller, post, key)
+                openComposerWithTemplateAndAction.bind(
+                  null,
+                  controller,
+                  post,
+                  wrap.dataset
+                )
               );
               wrap.appendChild(bottomButton);
             });
diff --git a/locales/en.yml b/locales/en.yml
index 1f7e7d6..582e6bc 100644
--- a/locales/en.yml
+++ b/locales/en.yml
@@ -1,4 +1,5 @@
 en:
   discourse_reply_template_component:
-    use_template: "Use template as reply"
+    use_template_reply: "Use template as reply"
+    use_template_create: "Use template as new topic"
     needs_key: 'Each template needs a key attribute. eg: [wrap=template key="weekly_status"][/wrap]'

GitHub sha: 15c6406b