From f5ffff35d8f7c1a1a30e3e2e16f0a0a2f8061fbf Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Fri, 24 Nov 2017 12:32:15 +0800 Subject: [PATCH] Add post builder --- .../components/wizard-custom-action.js.es6 | 23 ++++++--- .../components/wizard-custom-action.hbs | 50 +++++++++++++++++-- assets/stylesheets/wizard_custom_admin.scss | 4 ++ config/locales/client.en.yml | 8 ++- lib/builder.rb | 37 +++++++++++++- 5 files changed, 109 insertions(+), 13 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index ce21fdaa..1ecf9184 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -8,18 +8,15 @@ const ACTION_TYPES = [ const PROFILE_FIELDS = [ 'name', - 'email', - 'username', - 'title', 'date_of_birth', - 'muted_usernames', - 'theme_key', + 'title', 'locale', - 'bio_raw', 'location', 'website', + 'bio_raw', 'profile_background', - 'card_background' + 'card_background', + 'theme_key' ]; export default Ember.Component.extend({ @@ -54,5 +51,17 @@ export default Ember.Component.extend({ }); return fields; + }, + + @computed('availableFields') + builderWizardFields(fields) { + return fields.map((f) => ` w{${f.id}}`); + }, + + @computed() + builderUserFields() { + const noThemeKey = PROFILE_FIELDS.filter((f) => f !== 'theme_key'); + const fields = noThemeKey.concat(['email', 'username']); + return fields.map((f) => ` u{${f}}`); } }); diff --git a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs index 29e0b2f5..6df48e7b 100644 --- a/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-custom-action.hbs @@ -40,12 +40,34 @@

{{i18n "admin.wizard.action.post"}}

- {{combo-box value=action.post content=availableFields nameProperty="label" none='admin.wizard.select_field'}} + {{combo-box value=action.post content=availableFields + nameProperty='label' + none='admin.wizard.select_field' + isDisabled=action.post_builder}} +
+ {{input type='checkbox' checked=action.post_builder}} + {{i18n 'admin.wizard.action.post_builder.checkbox'}} +
+ {{#if action.post_builder}} +
+
+

{{i18n 'admin.wizard.action.post_builder.label'}}

+
+
+ + + {{d-editor value=action.post + placeholder='admin.wizard.action.post_builder.placeholder' + classNames='post-builder-editor'}} +
+
+ {{/if}} +
- + {{wizard-custom-input inputs=action.add_fields valueContent=availableFields inputKey='admin.wizard.action.topic_attr' @@ -68,10 +90,32 @@

{{i18n "admin.wizard.action.post"}}

- {{combo-box value=action.post content=availableFields nameProperty='label' none='admin.wizard.select_field'}} + {{combo-box value=action.post content=availableFields + nameProperty='label' + none='admin.wizard.select_field' + isDisabled=action.post_builder}} +
+ {{input type='checkbox' checked=action.post_builder}} + {{i18n 'admin.wizard.action.post_builder.checkbox'}} +
+ {{#if action.post_builder}} +
+
+

{{i18n 'admin.wizard.action.post_builder.label'}}

+
+
+ + + {{d-editor value=action.post + placeholder='admin.wizard.action.post_builder.placeholder' + classNames='post-builder-editor'}} +
+
+ {{/if}} +

{{i18n "admin.wizard.action.send_message.recipient"}}

diff --git a/assets/stylesheets/wizard_custom_admin.scss b/assets/stylesheets/wizard_custom_admin.scss index cb5d42d6..5b5b2695 100644 --- a/assets/stylesheets/wizard_custom_admin.scss +++ b/assets/stylesheets/wizard_custom_admin.scss @@ -236,3 +236,7 @@ } } } + +.post-builder-editor { + min-height: 220px; +} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index ab44cd57..06c50c27 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -89,7 +89,7 @@ en: include: "Include Fields" title: "Title" post: "Post" - add_fields: "Add Fields To {{type}}" + add_fields: "{{type}} Fields" available_fields: "* If 'Save wizard submissions' is disabled, only the fields of the current step are available to the current step's actions." topic_attr: "Topic Attribute" send_message: @@ -101,6 +101,12 @@ en: update_profile: label: "Update Profile" profile_field: "Profile Field" + post_builder: + checkbox: "Post Builder" + label: "Builder" + user_fields: "User Fields: " + wizard_fields: "Wizard Fields: " + placeholder: "Insert wizard fields using the field_id in w{}. Insert user fields using field key in u{}." wizard_js: location: diff --git a/lib/builder.rb b/lib/builder.rb index c3b88280..6eace558 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -34,6 +34,29 @@ class CustomWizard::Builder @sorted_handlers.sort_by! { |h| -h[:priority] } end + USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale'] + PROFILE_FIELDS = ['location', 'website', 'bio_raw', 'profile_background', 'card_background'] + + def self.build_post(template, user, data) + post = template.gsub(/u\{(.*?)\}/) do |match| + result = '' + + if USER_FIELDS.include?($1) + result = user.send($1) + if result.blank? && $1 === 'name' + result = user.send('username') + end + end + + if PROFILE_FIELDS.include?($1) + result = user.user_profile.send($1) + end + + result + end + post.gsub!(/w\{(.*?)\}/) { |match| data[$1.to_sym] } + end + def build unless (@wizard.completed? && !@wizard.multiple_submissions) || !@steps @steps.each do |s| @@ -161,7 +184,12 @@ class CustomWizard::Builder s['actions'].each do |a| if a['type'] === 'create_topic' && data title = data[a['title']] - post = data[a['post']] + + if a['post_builder'] + post = CustomWizard::Builder.build_post(a['post'], user, data) + else + post = data[a['post']] + end if title params = { @@ -218,7 +246,12 @@ class CustomWizard::Builder if a['type'] === 'send_message' && data title = data[a['title']] - post = data[a['post']] + + if a['post_builder'] + post = CustomWizard::Builder.build_post(a['post'], user, data) + else + post = data[a['post']] + end if title && post creator = PostCreator.new(user,