A platform for community discussion. Free, open, simple.
DEV: Plugins can extend color definitions (#10383)
Penar Musaraj87e2c9de2478
diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex 8beb5c33123..335d102fd70 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -54,6 +54,7 @@ class DiscoursePluginRegistrydefine_register :stylesheets, Hashdefine_register :mobile_stylesheets, Hashdefine_register :desktop_stylesheets, Hash+ define_register :color_definition_stylesheets, Hashdefine_register :sass_variables, Setdefine_register :handlebars, Setdefine_register :serialized_current_user_fields, Set@@ -153,6 +154,8 @@ class DiscoursePluginRegistryelsif opts == :desktopself.desktop_stylesheets[plugin_directory_name] ||= Set.newself.desktop_stylesheets[plugin_directory_name] << asset+ elsif opts == :color_definitions+ self.color_definition_stylesheets[plugin_directory_name] = assetelsif opts == :variablesself.sass_variables << assetelsediff --git a/lib/stylesheet/compiler.rb b/lib/stylesheet/compiler.rbindex 54675699bb6..b5575cda494 100644--- a/lib/stylesheet/compiler.rb+++ b/lib/stylesheet/compiler.rb@@ -19,6 +19,10 @@ module Stylesheetfilename = "#{asset}.scss"path = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}"file = File.read path++ if asset.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET+ file += Stylesheet::Importer.import_color_definitions+ endendcompile(file, filename, options)diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rbindex 7798f9cf90e..f1901969ae7 100644--- a/lib/stylesheet/importer.rb+++ b/lib/stylesheet/importer.rb@@ -115,6 +115,17 @@ module Stylesheetregister_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)@theme = options[:theme]@theme_id = options[:theme_id]diff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rbindex 3dc1bce8821..186dc69d270 100644--- a/spec/components/discourse_plugin_registry_spec.rb+++ b/spec/components/discourse_plugin_registry_spec.rb@@ -227,6 +227,12 @@ describe DiscoursePluginRegistry doexpect(registry.stylesheets[plugin_directory_name]).to eq(nil)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" doregistry.register_asset("test.css", :variables)diff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rbindex 1f317903628..1d7cc086734 100644--- a/spec/components/plugin/instance_spec.rb+++ b/spec/components/plugin/instance_spec.rb@@ -11,8 +11,8 @@ describe Plugin::Instance docontext "find_all" doit "can find plugins correctly" doplugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")- expect(plugins.count).to eq(3)- plugin = plugins[2]+ expect(plugins.count).to eq(4)+ plugin = plugins[3]expect(plugin.name).to eq("plugin-name")expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")diff --git a/spec/components/stylesheet/compiler_spec.rb b/spec/components/stylesheet/compiler_spec.rbindex 1d93aec2415..37aefc25e83 100644--- a/spec/components/stylesheet/compiler_spec.rb+++ b/spec/components/stylesheet/compiler_spec.rb@@ -84,5 +84,26 @@ describe Stylesheet::Compiler doexpect(css).to include("--header_primary: #88af8e")expect(css).to include("--header_background-rgb: 248,116,92")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+endenddiff --git a/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss b/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scssnew file mode 100644index 00000000000..a4a156e042d--- /dev/null+++ b/spec/fixtures/plugins/color_definition/assets/stylesheets/colors.scss@@ -0,0 +1,3 @@+:root {+ --plugin-color: #{$primary};+}diff --git a/spec/fixtures/plugins/color_definition/plugin.rb b/spec/fixtures/plugins/color_definition/plugin.rbnew file mode 100644index 00000000000..d7044b3d858--- /dev/null+++ b/spec/fixtures/plugins/color_definition/plugin.rb@@ -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