mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 17:26:29 +08:00
FEATURE: Let plugins register themes easily
This commit is contained in:
@ -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")
|
||||
|
32
lib/plugin/theme.rb
Normal file
32
lib/plugin/theme.rb
Normal 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
|
||||
|
Reference in New Issue
Block a user