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:
parent
0dcb4bef20
commit
f061aee818
|
@ -4,6 +4,11 @@ import computed from "ember-addons/ember-computed-decorators";
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
editorId: Ember.computed.reads("fieldName"),
|
editorId: Ember.computed.reads("fieldName"),
|
||||||
|
|
||||||
|
@computed("fieldName")
|
||||||
|
currentEditorMode(fieldName) {
|
||||||
|
return fieldName === "css" ? "scss" : fieldName;
|
||||||
|
},
|
||||||
|
|
||||||
@computed("fieldName", "styles.html", "styles.css")
|
@computed("fieldName", "styles.html", "styles.css")
|
||||||
resetDisabled(fieldName) {
|
resetDisabled(fieldName) {
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{ace-editor content=editorContents mode=fieldName editorId=editorId}}
|
{{ace-editor content=editorContents mode=currentEditorMode editorId=editorId}}
|
||||||
|
|
||||||
<div class='admin-footer'>
|
<div class='admin-footer'>
|
||||||
<div class='buttons'>
|
<div class='buttons'>
|
||||||
|
|
|
@ -17,6 +17,10 @@ class EmailStyle
|
||||||
SiteSetting.email_custom_css || default_css
|
SiteSetting.email_custom_css || default_css
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compiled_css
|
||||||
|
SiteSetting.email_custom_css_compiled || self.class.default_css_compiled
|
||||||
|
end
|
||||||
|
|
||||||
def default_html
|
def default_html
|
||||||
self.class.default_template
|
self.class.default_template
|
||||||
end
|
end
|
||||||
|
@ -34,4 +38,8 @@ class EmailStyle
|
||||||
def self.default_css
|
def self.default_css
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.default_css_compiled
|
||||||
|
''
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,26 +10,39 @@ class EmailStyleUpdater
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(attrs)
|
def update(attrs)
|
||||||
|
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.has_key?(:html)
|
||||||
if attrs[:html] == EmailStyle.default_template
|
if attrs[:html] == EmailStyle.default_template
|
||||||
SiteSetting.remove_override!(:email_custom_template)
|
SiteSetting.remove_override!(:email_custom_template)
|
||||||
else
|
else
|
||||||
if !attrs[:html].include?('%{email_content}')
|
SiteSetting.email_custom_template = attrs[:html]
|
||||||
@errors << I18n.t(
|
|
||||||
'email_style.html_missing_placeholder',
|
|
||||||
placeholder: '%{email_content}'
|
|
||||||
)
|
|
||||||
else
|
|
||||||
SiteSetting.email_custom_template = attrs[:html]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if attrs.has_key?(:css)
|
if attrs.has_key?(:css)
|
||||||
if attrs[:css] == EmailStyle.default_css
|
if attrs[:css] == EmailStyle.default_css
|
||||||
SiteSetting.remove_override!(:email_custom_css)
|
SiteSetting.remove_override!(:email_custom_css)
|
||||||
|
SiteSetting.remove_override!(:email_custom_css_compiled)
|
||||||
else
|
else
|
||||||
SiteSetting.email_custom_css = attrs[:css]
|
SiteSetting.email_custom_css = attrs[:css]
|
||||||
|
SiteSetting.email_custom_css_compiled = compiled_css
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1044,6 +1044,9 @@ email:
|
||||||
email_custom_css:
|
email_custom_css:
|
||||||
default: ""
|
default: ""
|
||||||
hidden: true
|
hidden: true
|
||||||
|
email_custom_css_compiled:
|
||||||
|
default: ""
|
||||||
|
hidden: true
|
||||||
email_total_attachment_size_limit_kb:
|
email_total_attachment_size_limit_kb:
|
||||||
default: 0
|
default: 0
|
||||||
max: 51200
|
max: 51200
|
||||||
|
|
|
@ -36,7 +36,7 @@ module Email
|
||||||
def custom_styles
|
def custom_styles
|
||||||
return @custom_styles unless @custom_styles.nil?
|
return @custom_styles unless @custom_styles.nil?
|
||||||
|
|
||||||
css = EmailStyle.new.css
|
css = EmailStyle.new.compiled_css
|
||||||
@custom_styles = {}
|
@custom_styles = {}
|
||||||
|
|
||||||
if !css.blank?
|
if !css.blank?
|
||||||
|
|
|
@ -6,6 +6,7 @@ describe EmailStyle do
|
||||||
before do
|
before do
|
||||||
SiteSetting.email_custom_template = "<body><h1>FOR YOU</h1><div>%{email_content}</div></body>"
|
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 = 'h1 { color: red; } div.body { color: #FAB; }'
|
||||||
|
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
@ -93,6 +94,7 @@ describe EmailStyle do
|
||||||
context 'with some bad css' do
|
context 'with some bad css' do
|
||||||
before do
|
before do
|
||||||
SiteSetting.email_custom_css = '@import "nope.css"; h1 {{{ size: really big; '
|
SiteSetting.email_custom_css = '@import "nope.css"; h1 {{{ size: really big; '
|
||||||
|
SiteSetting.email_custom_css_compiled = SiteSetting.email_custom_css
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can render the html" do
|
it "can render the html" do
|
||||||
|
|
|
@ -7,6 +7,12 @@ describe EmailStyleUpdater do
|
||||||
let(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }
|
let(:default_html) { File.read("#{Rails.root}/app/views/email/default_template.html") }
|
||||||
let(:updater) { EmailStyleUpdater.new(admin) }
|
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
|
describe 'update' do
|
||||||
it 'can change the settings' do
|
it 'can change the settings' do
|
||||||
expect(
|
expect(
|
||||||
|
@ -17,20 +23,20 @@ describe EmailStyleUpdater do
|
||||||
).to eq(true)
|
).to eq(true)
|
||||||
expect(SiteSetting.email_custom_template).to eq('For you: %{email_content}')
|
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).to eq('h1 { color: blue; }')
|
||||||
|
expect(SiteSetting.email_custom_css_compiled.strip).to eq('h1{color:blue}')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not store defaults' do
|
it 'will not store defaults' do
|
||||||
updater.update(html: default_html, css: '')
|
updater.update(html: default_html, css: '')
|
||||||
expect(SiteSetting.email_custom_template).to_not be_present
|
expect_settings_to_be_unset
|
||||||
expect(SiteSetting.email_custom_css).to_not be_present
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can clear settings if defaults given' do
|
it 'can clear settings if defaults given' do
|
||||||
SiteSetting.email_custom_template = 'For you: %{email_content}'
|
SiteSetting.email_custom_template = 'For you: %{email_content}'
|
||||||
SiteSetting.email_custom_css = 'h1 { color: blue; }'
|
SiteSetting.email_custom_css = 'h1 { color: blue; }'
|
||||||
|
SiteSetting.email_custom_css_compiled = 'h1{color:blue}'
|
||||||
updater.update(html: default_html, css: '')
|
updater.update(html: default_html, css: '')
|
||||||
expect(SiteSetting.email_custom_template).to_not be_present
|
expect_settings_to_be_unset
|
||||||
expect(SiteSetting.email_custom_css).to_not be_present
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'fails if html is missing email_content' do
|
it 'fails if html is missing email_content' do
|
||||||
|
@ -41,6 +47,14 @@ describe EmailStyleUpdater do
|
||||||
placeholder: '%{email_content}'
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue