FEATURE: Let plugins register themes easily
This commit is contained in:
parent
d49473757e
commit
adb73180f7
|
@ -20,11 +20,11 @@
|
|||
{{outlet "user-card"}}
|
||||
</div>
|
||||
|
||||
{{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}}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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 = ""
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue