discourse/app/models/theme_field.rb
Sam a3e8c3cd7b FEATURE: Native theme support
This feature introduces the concept of themes. Themes are an evolution
of site customizations.

Themes introduce two very big conceptual changes:

- A theme may include other "child themes", children can include grand
children and so on.

- A theme may specify a color scheme

The change does away with the idea of "enabled" color schemes.

It also adds a bunch of big niceties like

- You can source a theme from a git repo

- History for themes is much improved

- You can only have a single enabled theme. Themes can be selected by
    users, if you opt for it.

On a technical level this change comes with a whole bunch of goodies

- All CSS is now compiled using a custom pipeline that uses libsass
    see /lib/stylesheet

- There is a single pipeline for css compilation (in the past we used
    one for customizations and another one for the rest of the app

- The stylesheet pipeline is now divorced of sprockets, there is no
   reliance on sprockets for CSS bundling

- CSS is generated with source maps everywhere (including themes) this
    makes debugging much easier

- Our "live reloader" is smarter and avoid a flash of unstyled content
   we run a file watcher in "puma" in dev so you no longer need to run
   rake autospec to watch for CSS changes
2017-04-12 10:53:49 -04:00

118 lines
3.2 KiB
Ruby

class ThemeField < ActiveRecord::Base
COMPILER_VERSION = 5
belongs_to :theme
def transpile(es6_source, version)
template = Tilt::ES6ModuleTranspilerTemplate.new {}
wrapped = <<PLUGIN_API_JS
Discourse._registerPluginCode('#{version}', api => {
#{es6_source}
});
PLUGIN_API_JS
template.babel_transpile(wrapped)
end
def process_html(html)
doc = Nokogiri::HTML.fragment(html)
doc.css('script[type="text/x-handlebars"]').each do |node|
name = node["name"] || node["data-template-name"] || "broken"
is_raw = name =~ /\.raw$/
if is_raw
template = "require('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(node.inner_html)})"
node.replace <<COMPILED
<script>
(function() {
Discourse.RAW_TEMPLATES[#{name.sub(/\.raw$/, '').inspect}] = #{template};
})();
</script>
COMPILED
else
template = "Ember.HTMLBars.template(#{Barber::Ember::Precompiler.compile(node.inner_html)})"
node.replace <<COMPILED
<script>
(function() {
Ember.TEMPLATES[#{name.inspect}] = #{template};
})();
</script>
COMPILED
end
end
doc.css('script[type="text/discourse-plugin"]').each do |node|
if node['version'].present?
begin
code = transpile(node.inner_html, node['version'])
node.replace("<script>#{code}</script>")
rescue MiniRacer::RuntimeError => ex
node.replace("<script type='text/discourse-js-error'>#{ex.message}</script>")
end
end
end
doc.to_s
end
def self.html_fields
%w(body_tag head_tag header footer after_header)
end
def ensure_baked!
if ThemeField.html_fields.include?(self.name)
if !self.value_baked || compiler_version != COMPILER_VERSION
self.value_baked = process_html(self.value)
self.compiler_version = COMPILER_VERSION
if self.value_baked_changed? || compiler_version.changed?
self.update_columns(value_baked: value_baked, compiler_version: compiler_version)
end
end
end
end
def target_name
Theme.targets.invert[target].to_s
end
before_save do
if value_changed? && !value_baked_changed?
self.value_baked = nil
end
end
after_commit do
ensure_baked!
Stylesheet::Manager.clear_theme_cache! if self.name.include?("scss")
# TODO message for mobile vs desktop
MessageBus.publish "/header-change/#{theme.key}", self.value if self.name == "header"
MessageBus.publish "/footer-change/#{theme.key}", self.value if self.name == "footer"
end
end
# == Schema Information
#
# Table name: theme_fields
#
# id :integer not null, primary key
# theme_id :integer not null
# target :integer not null
# name :string not null
# value :text not null
# value_baked :text
# created_at :datetime
# updated_at :datetime
# compiler_version :integer default(0), not null
#
# Indexes
#
# index_theme_fields_on_theme_id_and_target_and_name (theme_id,target,name) UNIQUE
#