FEATURE: support SCSS in custom email style

In the CSS tab of Admin > Customize > Email Style, SCSS can now be used.
This commit is contained in:
Neil Lalonde 2019-10-23 15:41:58 -04:00
parent 0dcb4bef20
commit f061aee818
8 changed files with 59 additions and 14 deletions

View File

@ -4,6 +4,11 @@ import computed from "ember-addons/ember-computed-decorators";
export default Component.extend({
editorId: Ember.computed.reads("fieldName"),
@computed("fieldName")
currentEditorMode(fieldName) {
return fieldName === "css" ? "scss" : fieldName;
},
@computed("fieldName", "styles.html", "styles.css")
resetDisabled(fieldName) {
return (

View File

@ -9,7 +9,7 @@
</div>
</div>
{{ace-editor content=editorContents mode=fieldName editorId=editorId}}
{{ace-editor content=editorContents mode=currentEditorMode editorId=editorId}}
<div class='admin-footer'>
<div class='buttons'>

View File

@ -17,6 +17,10 @@ class EmailStyle
SiteSetting.email_custom_css || default_css
end
def compiled_css
SiteSetting.email_custom_css_compiled || self.class.default_css_compiled
end
def default_html
self.class.default_template
end
@ -34,4 +38,8 @@ class EmailStyle
def self.default_css
''
end
def self.default_css_compiled
''
end
end

View File

@ -10,26 +10,39 @@ class EmailStyleUpdater
end
def update(attrs)
if attrs.has_key?(:html)
if attrs[:html] == EmailStyle.default_template
SiteSetting.remove_override!(:email_custom_template)
else
if !attrs[:html].include?('%{email_content}')
if attrs.has_key?(:html) && !attrs[:html].include?('%{email_content}')
@errors << I18n.t(
'email_style.html_missing_placeholder',
placeholder: '%{email_content}'
)
end
if attrs.has_key?(:css)
begin
compiled_css = SassC::Engine.new(attrs[:css], style: :compressed).render
rescue SassC::SyntaxError => e
# @errors << I18n.t('email_style.css_syntax_error')
@errors << e.message[0...(e.message.index("\n"))]
end
end
return false unless @errors.empty?
if attrs.has_key?(:html)
if attrs[:html] == EmailStyle.default_template
SiteSetting.remove_override!(:email_custom_template)
else
SiteSetting.email_custom_template = attrs[:html]
end
end
end
if attrs.has_key?(:css)
if attrs[:css] == EmailStyle.default_css
SiteSetting.remove_override!(:email_custom_css)
SiteSetting.remove_override!(:email_custom_css_compiled)
else
SiteSetting.email_custom_css = attrs[:css]
SiteSetting.email_custom_css_compiled = compiled_css
end
end

View File

@ -1044,6 +1044,9 @@ email:
email_custom_css:
default: ""
hidden: true
email_custom_css_compiled:
default: ""
hidden: true
email_total_attachment_size_limit_kb:
default: 0
max: 51200

View File

@ -36,7 +36,7 @@ module Email
def custom_styles
return @custom_styles unless @custom_styles.nil?
css = EmailStyle.new.css
css = EmailStyle.new.compiled_css
@custom_styles = {}
if !css.blank?

View File

@ -6,6 +6,7 @@ describe EmailStyle do
before do
SiteSetting.email_custom_template = "<body><h1>FOR YOU</h1><div>%{email_content}</div></body>"
SiteSetting.email_custom_css = 'h1 { color: red; } div.body { color: #FAB; }'
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
end
after do
@ -93,6 +94,7 @@ describe EmailStyle do
context 'with some bad css' do
before do
SiteSetting.email_custom_css = '@import "nope.css"; h1 {{{ size: really big; '
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
end
it "can render the html" do

View File

@ -7,6 +7,12 @@ describe EmailStyleUpdater do
let(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }
let(:updater) { EmailStyleUpdater.new(admin) }
def expect_settings_to_be_unset
expect(SiteSetting.email_custom_template).to_not be_present
expect(SiteSetting.email_custom_css).to_not be_present
expect(SiteSetting.email_custom_css_compiled).to_not be_present
end
describe 'update' do
it 'can change the settings' do
expect(
@ -17,20 +23,20 @@ describe EmailStyleUpdater do
).to eq(true)
expect(SiteSetting.email_custom_template).to eq('For you: %{email_content}')
expect(SiteSetting.email_custom_css).to eq('h1 { color: blue; }')
expect(SiteSetting.email_custom_css_compiled.strip).to eq('h1{color:blue}')
end
it 'will not store defaults' do
updater.update(html: default_html, css: '')
expect(SiteSetting.email_custom_template).to_not be_present
expect(SiteSetting.email_custom_css).to_not be_present
expect_settings_to_be_unset
end
it 'can clear settings if defaults given' do
SiteSetting.email_custom_template = 'For you: %{email_content}'
SiteSetting.email_custom_css = 'h1 { color: blue; }'
SiteSetting.email_custom_css_compiled = 'h1{color:blue}'
updater.update(html: default_html, css: '')
expect(SiteSetting.email_custom_template).to_not be_present
expect(SiteSetting.email_custom_css).to_not be_present
expect_settings_to_be_unset
end
it 'fails if html is missing email_content' do
@ -41,6 +47,14 @@ describe EmailStyleUpdater do
placeholder: '%{email_content}'
)
)
expect_settings_to_be_unset
end
it 'fails if css is not valid scss' do
expect(updater.update(html: 'For you: %{email_content}', css: 'h1 { color: blue;')).to eq(false)
expect(updater.errors).to_not be_empty
expect(updater.errors.first).to include('Invalid CSS after')
expect_settings_to_be_unset
end
end
end