FEATURE: we need access to settings in theme js
This commit is contained in:
parent
95555b5ec8
commit
9331b4849d
|
@ -24,10 +24,28 @@ class ThemeField < ActiveRecord::Base
|
||||||
|
|
||||||
belongs_to :theme
|
belongs_to :theme
|
||||||
|
|
||||||
|
def settings(source)
|
||||||
|
|
||||||
|
settings = {}
|
||||||
|
|
||||||
|
theme.cached_settings.each do |k, v|
|
||||||
|
if source.include?("settings.#{k}")
|
||||||
|
settings[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if settings.length > 0
|
||||||
|
"let settings = #{settings.to_json};"
|
||||||
|
else
|
||||||
|
""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def transpile(es6_source, version)
|
def transpile(es6_source, version)
|
||||||
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
||||||
wrapped = <<PLUGIN_API_JS
|
wrapped = <<PLUGIN_API_JS
|
||||||
Discourse._registerPluginCode('#{version}', api => {
|
Discourse._registerPluginCode('#{version}', api => {
|
||||||
|
#{settings(es6_source)}
|
||||||
#{es6_source}
|
#{es6_source}
|
||||||
});
|
});
|
||||||
PLUGIN_API_JS
|
PLUGIN_API_JS
|
||||||
|
|
|
@ -7,6 +7,8 @@ class ThemeSetting < ActiveRecord::Base
|
||||||
|
|
||||||
after_save do
|
after_save do
|
||||||
theme.clear_cached_settings!
|
theme.clear_cached_settings!
|
||||||
|
theme.remove_from_cache!
|
||||||
|
theme.theme_fields.update_all(value_baked: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.types
|
def self.types
|
||||||
|
|
|
@ -214,7 +214,7 @@ HTML
|
||||||
end
|
end
|
||||||
|
|
||||||
context "theme settings" do
|
context "theme settings" do
|
||||||
it "values can be used in scss" do
|
it "allows values to be used in scss" do
|
||||||
theme = Theme.new(name: "awesome theme", user_id: -1)
|
theme = Theme.new(name: "awesome theme", user_id: -1)
|
||||||
theme.set_field(target: :settings, name: :yaml, value: "background_color: red\nfont_size: 25px")
|
theme.set_field(target: :settings, name: :yaml, value: "background_color: red\nfont_size: 25px")
|
||||||
theme.set_field(target: :common, name: :scss, value: 'body {background-color: $background_color; font-size: $font-size}')
|
theme.set_field(target: :common, name: :scss, value: 'body {background-color: $background_color; font-size: $font-size}')
|
||||||
|
@ -223,7 +223,42 @@ HTML
|
||||||
scss, _map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
|
scss, _map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
|
||||||
expect(scss).to include("background-color:red")
|
expect(scss).to include("background-color:red")
|
||||||
expect(scss).to include("font-size:25px")
|
expect(scss).to include("font-size:25px")
|
||||||
|
|
||||||
|
setting = theme.settings.find { |s| s.name == :font_size }
|
||||||
|
setting.value = '30px'
|
||||||
|
|
||||||
|
scss, _map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
|
||||||
|
expect(scss).to include("font-size:30px")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "allows values to be used in JS" do
|
||||||
|
theme = Theme.new(name: "awesome theme", user_id: -1)
|
||||||
|
theme.set_field(target: :settings, name: :yaml, value: "name: bob")
|
||||||
|
theme.set_field(target: :common, name: :after_header, value: '<script type="text/discourse-plugin" version="1.0">alert(settings.name); let a = ()=>{};</script>')
|
||||||
|
theme.save!
|
||||||
|
|
||||||
|
transpiled = <<~HTML
|
||||||
|
<script>Discourse._registerPluginCode('1.0', function (api) {
|
||||||
|
var settings = { "name": "bob" };
|
||||||
|
alert(settings.name);var a = function a() {};
|
||||||
|
});</script>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
expect(Theme.lookup_field(theme.key, :desktop, :after_header)).to eq(transpiled.strip)
|
||||||
|
|
||||||
|
setting = theme.settings.find { |s| s.name == :name }
|
||||||
|
setting.value = 'bill'
|
||||||
|
|
||||||
|
transpiled = <<~HTML
|
||||||
|
<script>Discourse._registerPluginCode('1.0', function (api) {
|
||||||
|
var settings = { "name": "bill" };
|
||||||
|
alert(settings.name);var a = function a() {};
|
||||||
|
});</script>
|
||||||
|
HTML
|
||||||
|
expect(Theme.lookup_field(theme.key, :desktop, :after_header)).to eq(transpiled.strip)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'correctly caches theme keys' do
|
it 'correctly caches theme keys' do
|
||||||
|
|
Loading…
Reference in New Issue