mirror of
https://github.com/discourse/discourse.git
synced 2025-01-18 14:52:46 +08:00
DEV: Plugins can extend color definitions (#10383)
This commit is contained in:
parent
6fdc711b4a
commit
87e2c9de24
|
@ -54,6 +54,7 @@ class DiscoursePluginRegistry
|
||||||
define_register :stylesheets, Hash
|
define_register :stylesheets, Hash
|
||||||
define_register :mobile_stylesheets, Hash
|
define_register :mobile_stylesheets, Hash
|
||||||
define_register :desktop_stylesheets, Hash
|
define_register :desktop_stylesheets, Hash
|
||||||
|
define_register :color_definition_stylesheets, Hash
|
||||||
define_register :sass_variables, Set
|
define_register :sass_variables, Set
|
||||||
define_register :handlebars, Set
|
define_register :handlebars, Set
|
||||||
define_register :serialized_current_user_fields, Set
|
define_register :serialized_current_user_fields, Set
|
||||||
|
@ -153,6 +154,8 @@ class DiscoursePluginRegistry
|
||||||
elsif opts == :desktop
|
elsif opts == :desktop
|
||||||
self.desktop_stylesheets[plugin_directory_name] ||= Set.new
|
self.desktop_stylesheets[plugin_directory_name] ||= Set.new
|
||||||
self.desktop_stylesheets[plugin_directory_name] << asset
|
self.desktop_stylesheets[plugin_directory_name] << asset
|
||||||
|
elsif opts == :color_definitions
|
||||||
|
self.color_definition_stylesheets[plugin_directory_name] = asset
|
||||||
elsif opts == :variables
|
elsif opts == :variables
|
||||||
self.sass_variables << asset
|
self.sass_variables << asset
|
||||||
else
|
else
|
||||||
|
|
|
@ -19,6 +19,10 @@ module Stylesheet
|
||||||
filename = "#{asset}.scss"
|
filename = "#{asset}.scss"
|
||||||
path = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}"
|
path = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}"
|
||||||
file = File.read path
|
file = File.read path
|
||||||
|
|
||||||
|
if asset.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET
|
||||||
|
file += Stylesheet::Importer.import_color_definitions
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
compile(file, filename, options)
|
compile(file, filename, options)
|
||||||
|
|
|
@ -115,6 +115,17 @@ module Stylesheet
|
||||||
|
|
||||||
register_imports!
|
register_imports!
|
||||||
|
|
||||||
|
def self.import_color_definitions
|
||||||
|
return "" unless DiscoursePluginRegistry.color_definition_stylesheets.length
|
||||||
|
contents = +""
|
||||||
|
DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path|
|
||||||
|
contents << "// Color definitions from #{name}\n\n"
|
||||||
|
contents << File.read(path.to_s)
|
||||||
|
contents << "\n\n"
|
||||||
|
end
|
||||||
|
contents
|
||||||
|
end
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@theme = options[:theme]
|
@theme = options[:theme]
|
||||||
@theme_id = options[:theme_id]
|
@theme_id = options[:theme_id]
|
||||||
|
|
|
@ -227,6 +227,12 @@ describe DiscoursePluginRegistry do
|
||||||
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "registers color definitions properly" do
|
||||||
|
registry.register_asset("test.css", :color_definitions, plugin_directory_name)
|
||||||
|
expect(registry.color_definition_stylesheets[plugin_directory_name]).to eq('test.css')
|
||||||
|
expect(registry.stylesheets[plugin_directory_name]).to eq(nil)
|
||||||
|
end
|
||||||
|
|
||||||
it "registers sass variable properly" do
|
it "registers sass variable properly" do
|
||||||
registry.register_asset("test.css", :variables)
|
registry.register_asset("test.css", :variables)
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,8 @@ describe Plugin::Instance do
|
||||||
context "find_all" do
|
context "find_all" do
|
||||||
it "can find plugins correctly" do
|
it "can find plugins correctly" do
|
||||||
plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
|
plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
|
||||||
expect(plugins.count).to eq(3)
|
expect(plugins.count).to eq(4)
|
||||||
plugin = plugins[2]
|
plugin = plugins[3]
|
||||||
|
|
||||||
expect(plugin.name).to eq("plugin-name")
|
expect(plugin.name).to eq("plugin-name")
|
||||||
expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
|
expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
|
||||||
|
|
|
@ -84,5 +84,26 @@ describe Stylesheet::Compiler do
|
||||||
expect(css).to include("--header_primary: #88af8e")
|
expect(css).to include("--header_primary: #88af8e")
|
||||||
expect(css).to include("--header_background-rgb: 248,116,92")
|
expect(css).to include("--header_background-rgb: 248,116,92")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with a plugin" do
|
||||||
|
before do
|
||||||
|
plugin = Plugin::Instance.new
|
||||||
|
plugin.path = "#{Rails.root}/spec/fixtures/plugins/color_definition/plugin.rb"
|
||||||
|
Discourse.plugins << plugin
|
||||||
|
plugin.activate!
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Discourse.plugins.pop
|
||||||
|
DiscoursePluginRegistry.reset!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "includes color definitions from plugins" do
|
||||||
|
css, _map = Stylesheet::Compiler.compile_asset("color_definitions")
|
||||||
|
|
||||||
|
expect(css).to include("--plugin-color")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
3
spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss
vendored
Normal file
3
spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
:root {
|
||||||
|
--plugin-color: #{$primary};
|
||||||
|
}
|
8
spec/fixtures/plugins/color_definition/plugin.rb
vendored
Normal file
8
spec/fixtures/plugins/color_definition/plugin.rb
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# name: color_definition
|
||||||
|
# about: Fixture plugin that extends color definitions
|
||||||
|
# version: 1.0
|
||||||
|
# authors: pmusaraj
|
||||||
|
|
||||||
|
register_asset "stylesheets/colors.scss", :color_definitions
|
Loading…
Reference in New Issue
Block a user