FEATURE: Let plugins register themes easily

This commit is contained in:
Robin Ward 2017-01-12 15:43:09 -05:00
parent d49473757e
commit adb73180f7
7 changed files with 101 additions and 20 deletions

View File

@ -20,11 +20,11 @@
{{outlet "user-card"}} {{outlet "user-card"}}
</div> </div>
{{plugin-outlet name="above-footer"}} {{plugin-outlet name="above-footer" args=(hash showFooter=showFooter)}}
{{#if showFooter}} {{#if showFooter}}
{{custom-html name="footer"}} {{custom-html name="footer"}}
{{/if}} {{/if}}
{{plugin-outlet name="below-footer"}} {{plugin-outlet name="below-footer" args=(hash showFooter=showFooter)}}
{{outlet "modal"}} {{outlet "modal"}}
{{topic-entrance}} {{topic-entrance}}

View File

@ -92,15 +92,21 @@ class ColorScheme < ActiveRecord::Base
new_color_scheme new_color_scheme
end end
def self.hex_for_name(name) def self.lookup_hex_for_name(name)
val = begin Discourse.plugin_themes.each do |pt|
hex_cache[name] ||= begin if pt.color_scheme
# Can't use `where` here because base doesn't allow it found = pt.color_scheme[name.to_sym]
(enabled || base).colors.find {|c| c.name == name }.try(:hex) || :nil return found if found
end end
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 end
def colors=(arr) def colors=(arr)

View File

@ -121,6 +121,10 @@ module Discourse
@plugins ||= [] @plugins ||= []
end end
def self.plugin_themes
@plugin_themes ||= plugins.map(&:themes).flatten
end
def self.official_plugins def self.official_plugins
plugins.find_all{|p| p.metadata.official?} plugins.find_all{|p| p.metadata.official?}
end end

View File

@ -2,6 +2,7 @@ require 'digest/sha1'
require 'fileutils' require 'fileutils'
require_dependency 'plugin/metadata' require_dependency 'plugin/metadata'
require_dependency 'plugin/auth_provider' require_dependency 'plugin/auth_provider'
require_dependency 'plugin/theme'
class Plugin::CustomEmoji class Plugin::CustomEmoji
def self.cache_key def self.cache_key
@ -24,7 +25,13 @@ class Plugin::Instance
attr_reader :admin_route attr_reader :admin_route
# Memoized array readers # 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{ class_eval %Q{
def #{att} def #{att}
@#{att} ||= [] @#{att} ||= []
@ -173,6 +180,10 @@ class Plugin::Instance
end end
end end
def directory
File.dirname(path)
end
def auto_generated_path def auto_generated_path
File.dirname(path) << "/auto_generated" File.dirname(path) << "/auto_generated"
end end
@ -244,6 +255,19 @@ class Plugin::Instance
Plugin::CustomEmoji.register(name, url) Plugin::CustomEmoji.register(name, url)
end 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 def automatic_assets
css = styles.join("\n") css = styles.join("\n")
js = javascripts.join("\n") js = javascripts.join("\n")

32
lib/plugin/theme.rb Normal file
View File

@ -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

View File

@ -41,15 +41,9 @@ class DiscourseSassImporter < Sass::Importers::Filesystem
case name case name
when "theme_variables" when "theme_variables"
contents = "" contents = ""
if color_scheme = ColorScheme.enabled
ColorScheme.base_colors.each do |n, base_hex| ColorScheme.base_colors.each do |n, base_hex|
override = color_scheme.colors_by_name[n] hex_val = ColorScheme.hex_for_name(n) || base_hex
contents << "$#{n}: ##{override ? override.hex : base_hex} !default;\n" contents << "$#{n}: ##{hex_val} !default;\n"
end
else
special_imports[name].each do |css_file|
contents << File.read(css_file)
end
end end
when "category_backgrounds" when "category_backgrounds"
contents = "" contents = ""

View File

@ -160,6 +160,27 @@ describe Plugin::Instance do
end end
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 context "register_color_scheme" do
it "can add a color scheme for the first time" do it "can add a color scheme for the first time" do
plugin = Plugin::Instance.new nil, "/tmp/test.rb" plugin = Plugin::Instance.new nil, "/tmp/test.rb"
@ -207,7 +228,7 @@ describe Plugin::Instance do
it 'should add the right callback' do it 'should add the right callback' do
called = 0 called = 0
method_name = plugin_instance.add_model_callback(User, :after_create) do plugin_instance.add_model_callback(User, :after_create) do
called += 1 called += 1
end end
@ -223,7 +244,7 @@ describe Plugin::Instance do
it 'should add the right callback with options' do it 'should add the right callback with options' do
called = 0 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 called += 1
end end