From adb73180f727be5b5bf4772c81f99e5ac9d10eea Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 12 Jan 2017 15:43:09 -0500 Subject: [PATCH] FEATURE: Let plugins register themes easily --- .../discourse/templates/application.hbs | 4 +-- app/models/color_scheme.rb | 18 +++++++---- lib/discourse.rb | 4 +++ lib/plugin/instance.rb | 26 ++++++++++++++- lib/plugin/theme.rb | 32 +++++++++++++++++++ lib/sass/discourse_sass_importer.rb | 12 ++----- spec/components/plugin/instance_spec.rb | 25 +++++++++++++-- 7 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 lib/plugin/theme.rb diff --git a/app/assets/javascripts/discourse/templates/application.hbs b/app/assets/javascripts/discourse/templates/application.hbs index bf2de38396f..6250c28d1fd 100644 --- a/app/assets/javascripts/discourse/templates/application.hbs +++ b/app/assets/javascripts/discourse/templates/application.hbs @@ -20,11 +20,11 @@ {{outlet "user-card"}} -{{plugin-outlet name="above-footer"}} +{{plugin-outlet name="above-footer" args=(hash showFooter=showFooter)}} {{#if showFooter}} {{custom-html name="footer"}} {{/if}} -{{plugin-outlet name="below-footer"}} +{{plugin-outlet name="below-footer" args=(hash showFooter=showFooter)}} {{outlet "modal"}} {{topic-entrance}} diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb index c2af572183d..d9c9216c277 100644 --- a/app/models/color_scheme.rb +++ b/app/models/color_scheme.rb @@ -92,15 +92,21 @@ class ColorScheme < ActiveRecord::Base new_color_scheme end - def self.hex_for_name(name) - val = begin - hex_cache[name] ||= begin - # Can't use `where` here because base doesn't allow it - (enabled || base).colors.find {|c| c.name == name }.try(:hex) || :nil + def self.lookup_hex_for_name(name) + Discourse.plugin_themes.each do |pt| + if pt.color_scheme + found = pt.color_scheme[name.to_sym] + return found if found end end - val == :nil ? nil : val + # Can't use `where` here because base doesn't allow it + (enabled || base).colors.find {|c| c.name == name }.try(:hex) || :nil + end + + def self.hex_for_name(name) + hex_cache[name] ||= lookup_hex_for_name(name) + hex_cache[name] == :nil ? nil : hex_cache[name] end def colors=(arr) diff --git a/lib/discourse.rb b/lib/discourse.rb index 9c960ae3de9..20227f2dfcd 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -121,6 +121,10 @@ module Discourse @plugins ||= [] end + def self.plugin_themes + @plugin_themes ||= plugins.map(&:themes).flatten + end + def self.official_plugins plugins.find_all{|p| p.metadata.official?} end diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 393e4eb0867..043ae454161 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -2,6 +2,7 @@ require 'digest/sha1' require 'fileutils' require_dependency 'plugin/metadata' require_dependency 'plugin/auth_provider' +require_dependency 'plugin/theme' class Plugin::CustomEmoji def self.cache_key @@ -24,7 +25,13 @@ class Plugin::Instance attr_reader :admin_route # Memoized array readers - [:assets, :auth_providers, :color_schemes, :initializers, :javascripts, :styles].each do |att| + [:assets, + :auth_providers, + :color_schemes, + :initializers, + :javascripts, + :styles, + :themes].each do |att| class_eval %Q{ def #{att} @#{att} ||= [] @@ -173,6 +180,10 @@ class Plugin::Instance end end + def directory + File.dirname(path) + end + def auto_generated_path File.dirname(path) << "/auto_generated" end @@ -244,6 +255,19 @@ class Plugin::Instance Plugin::CustomEmoji.register(name, url) end + def register_theme(name) + return unless enabled? + + theme = Plugin::Theme.new(self, name) + yield theme + themes << theme + end + + # def register_public + # return unless enabled? + # + # end + def automatic_assets css = styles.join("\n") js = javascripts.join("\n") diff --git a/lib/plugin/theme.rb b/lib/plugin/theme.rb new file mode 100644 index 00000000000..15cb38c8d01 --- /dev/null +++ b/lib/plugin/theme.rb @@ -0,0 +1,32 @@ +class Plugin::Theme + attr_reader :color_scheme + + def initialize(plugin, name) + @plugin = plugin + @name = name + end + + def css(name) + @plugin.register_asset("stylesheets/#{name}.scss") + end + + def set_color_scheme(scheme) + @color_scheme = scheme + end + + def register_public + public_dir = "#{@plugin.directory}/public" + if File.exist?(public_dir) + if Rails.env.development? + Rails.application.config.before_initialize do |app| + app.middleware.insert_before( + ::ActionDispatch::Static, + ::ActionDispatch::Static, + public_dir + ) + end + end + end + end +end + diff --git a/lib/sass/discourse_sass_importer.rb b/lib/sass/discourse_sass_importer.rb index a3e877b8e50..62230db937f 100644 --- a/lib/sass/discourse_sass_importer.rb +++ b/lib/sass/discourse_sass_importer.rb @@ -41,15 +41,9 @@ class DiscourseSassImporter < Sass::Importers::Filesystem case name when "theme_variables" contents = "" - if color_scheme = ColorScheme.enabled - ColorScheme.base_colors.each do |n, base_hex| - override = color_scheme.colors_by_name[n] - contents << "$#{n}: ##{override ? override.hex : base_hex} !default;\n" - end - else - special_imports[name].each do |css_file| - contents << File.read(css_file) - end + ColorScheme.base_colors.each do |n, base_hex| + hex_val = ColorScheme.hex_for_name(n) || base_hex + contents << "$#{n}: ##{hex_val} !default;\n" end when "category_backgrounds" contents = "" diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rb index d5c71508503..0e9c0fd592b 100644 --- a/spec/components/plugin/instance_spec.rb +++ b/spec/components/plugin/instance_spec.rb @@ -160,6 +160,27 @@ describe Plugin::Instance do end end + context "themes" do + it "can register a theme" do + plugin = Plugin::Instance.new nil, "/tmp/test.rb" + plugin.register_theme('plugin') do |theme| + theme.set_color_scheme( + primary: 'ffff00', + secondary: '222222', + tertiary: '0f82af', + quaternary: 'c14924', + header_background: '111111', + header_primary: '333333', + highlight: 'a87137', + danger: 'e45735', + success: '1ca551', + love: 'fa6c8d' + ) + end + expect(plugin.themes).to be_present + end + end + context "register_color_scheme" do it "can add a color scheme for the first time" do plugin = Plugin::Instance.new nil, "/tmp/test.rb" @@ -207,7 +228,7 @@ describe Plugin::Instance do it 'should add the right callback' do called = 0 - method_name = plugin_instance.add_model_callback(User, :after_create) do + plugin_instance.add_model_callback(User, :after_create) do called += 1 end @@ -223,7 +244,7 @@ describe Plugin::Instance do it 'should add the right callback with options' do called = 0 - method_name = plugin_instance.add_model_callback(User, :after_commit, on: :create) do + plugin_instance.add_model_callback(User, :after_commit, on: :create) do called += 1 end