A platform for community discussion. Free, open, simple.
DEV: Allow plugins to register admin-panel-specific CSS (#40345)
David Taylor32fc73357d97
diff --git a/app/controllers/bootstrap_controller.rb b/app/controllers/bootstrap_controller.rbindex 60c92365988..524ddb6df70 100644--- a/app/controllers/bootstrap_controller.rb+++ b/app/controllers/bootstrap_controller.rb@@ -61,7 +61,12 @@ class BootstrapController < ApplicationControllerplugin_css_string =Discourse- .find_plugin_css_assets(include_disabled: true, desktop_view: true, only: required_plugins)+ .find_plugin_css_assets(+ include_disabled: true,+ desktop_view: true,+ include_admin: true,+ only: required_plugins,+ ).map { |file| helpers.discourse_stylesheet_link_tag(file) }.join("\n")diff --git a/app/models/theme.rb b/app/models/theme.rbindex c4cbdd7a050..c3d560cd84c 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -506,7 +506,13 @@ class Theme < ActiveRecord::Baseif with_schemetargets.prepend(:common, :desktop, :mobile, :admin)- targets.append(*Discourse.find_plugin_css_assets(mobile_view: true, desktop_view: true))+ targets.append(+ *Discourse.find_plugin_css_assets(+ mobile_view: true,+ desktop_view: true,+ include_admin: true,+ ),+ )Stylesheet::Manager.cache.clear if clear_manager_cacheenddiff --git a/app/views/common/_discourse_preload_stylesheet.html.erb b/app/views/common/_discourse_preload_stylesheet.html.erbindex d1068a34e19..e050c0d1e53 100644--- a/app/views/common/_discourse_preload_stylesheet.html.erb+++ b/app/views/common/_discourse_preload_stylesheet.html.erb@@ -24,7 +24,7 @@<%- end %><%- end %>-<%- Discourse.find_plugin_css_assets(include_official: allow_plugins?, include_unofficial: allow_third_party_plugins?, mobile_view: mobile_device?, desktop_view: !mobile_device?, request: request, rtl: rtl?).each do |file| %>+<%- Discourse.find_plugin_css_assets(include_official: allow_plugins?, include_unofficial: allow_third_party_plugins?, mobile_view: mobile_device?, desktop_view: !mobile_device?, include_admin: staff?, request: request, rtl: rtl?).each do |file| %><%= discourse_stylesheet_preload_tag(file) %><%- end %>diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erbindex 9a188299ad8..7c12e85aed9 100644--- a/app/views/common/_discourse_stylesheet.html.erb+++ b/app/views/common/_discourse_stylesheet.html.erb@@ -13,7 +13,7 @@<%= discourse_stylesheet_link_tag(:wizard, supports_rtl: true) %><%- end %>-<%- Discourse.find_plugin_css_assets(include_official: allow_plugins?, include_unofficial: allow_third_party_plugins?, mobile_view: true, desktop_view: true, request: request, rtl: false).each do |file| %>+<%- Discourse.find_plugin_css_assets(include_official: allow_plugins?, include_unofficial: allow_third_party_plugins?, mobile_view: true, desktop_view: true, include_admin: staff?, request: request, rtl: false).each do |file| %><%=media = if file.end_with?("_mobile")"(width < 40rem)"diff --git a/lib/discourse.rb b/lib/discourse.rbindex 5c021429be8..a1f215fd65b 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -437,6 +437,7 @@ module Discoursetargets = [nil]targets << :mobile if args[:mobile_view]targets << :desktop if args[:desktop_view]+ targets << :admin if args[:include_admin]targets.each do |target|assets +=diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex 8542d9e56be..c322387caa2 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -58,6 +58,7 @@ class DiscoursePluginRegistrydefine_register :stylesheets, Hashdefine_register :mobile_stylesheets, Hashdefine_register :desktop_stylesheets, Hash+ define_register :admin_stylesheets, Hashdefine_register :color_definition_stylesheets, Hashdefine_register :serialized_current_user_fields, Setdefine_register :seed_data, ActiveSupport::HashWithIndifferentAccess@@ -203,6 +204,9 @@ class DiscoursePluginRegistryelsif opts == :desktopdesktop_stylesheets[plugin_directory_name] ||= Set.newdesktop_stylesheets[plugin_directory_name] << asset+ elsif opts == :admin+ admin_stylesheets[plugin_directory_name] ||= Set.new+ admin_stylesheets[plugin_directory_name] << assetelsif opts == :color_definitionscolor_definition_stylesheets[plugin_directory_name] = assetelse@@ -218,6 +222,8 @@ class DiscoursePluginRegistrydesktop_stylesheets[plugin_directory_name].present?when :mobilemobile_stylesheets[plugin_directory_name].present?+ when :admin+ admin_stylesheets[plugin_directory_name].present?elsestylesheets[plugin_directory_name].present?enddiff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rbindex 249674595db..bb019343227 100644--- a/lib/stylesheet/importer.rb+++ b/lib/stylesheet/importer.rb@@ -16,7 +16,7 @@ module StylesheetDiscourse.plugins.each do |plugin|plugin_directory_name = plugin.directory_name- ["", "mobile", "desktop"].each do |type|+ ["", "mobile", "desktop", "admin"].each do |type|asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_namestylesheets =(diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rbindex 547ca73503d..9e00abf1910 100644--- a/lib/stylesheet/manager.rb+++ b/lib/stylesheet/manager.rb@@ -57,12 +57,14 @@ class Stylesheet::Managerinclude_disabled: true,mobile_view: true,desktop_view: true,+ include_admin: true,)targets +=Discourse.find_plugin_css_assets(include_disabled: true,mobile_view: true,desktop_view: true,+ include_admin: true,rtl: true,)diff --git a/lib/stylesheet/manager/builder.rb b/lib/stylesheet/manager/builder.rbindex e7d3fb862ff..112911aae6c 100644--- a/lib/stylesheet/manager/builder.rb+++ b/lib/stylesheet/manager/builder.rb@@ -196,6 +196,7 @@ class Stylesheet::Manager::BuilderDiscoursePluginRegistry.stylesheets.each { |_, paths| assets += paths.to_a }DiscoursePluginRegistry.mobile_stylesheets.each { |_, paths| assets += paths.to_a }DiscoursePluginRegistry.desktop_stylesheets.each { |_, paths| assets += paths.to_a }+ DiscoursePluginRegistry.admin_stylesheets.each { |_, paths| assets += paths.to_a }Digest::SHA1.hexdigest(assets.sort.join)enddiff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scsssimilarity index 100%rename from plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scssrename to plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scssdiff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rbindex 02ee588612c..f5fff147ead 100644--- a/plugins/discourse-ai/plugin.rb+++ b/plugins/discourse-ai/plugin.rb@@ -19,9 +19,9 @@ register_asset "stylesheets/common/ai-blinking-animation.scss"register_asset "stylesheets/common/ai-user-settings.scss"register_asset "stylesheets/common/ai-features.scss"-register_asset "stylesheets/admin/ai-features-editor.scss"+register_asset "stylesheets/admin/ai-features-editor.scss", :admin-register_asset "stylesheets/modules/translation/common/admin-translations.scss"+register_asset "stylesheets/modules/translation/admin/translations.scss", :adminregister_asset "stylesheets/modules/ai-helper/common/ai-helper.scss"register_asset "stylesheets/modules/ai-helper/desktop/ai-helper-fk-modals.scss", :desktopdiff --git a/plugins/discourse-gamification/assets/stylesheets/common/leaderboard-admin.scss b/plugins/discourse-gamification/assets/stylesheets/admin/leaderboard.scsssimilarity index 100%rename from plugins/discourse-gamification/assets/stylesheets/common/leaderboard-admin.scssrename to plugins/discourse-gamification/assets/stylesheets/admin/leaderboard.scssdiff --git a/plugins/discourse-gamification/plugin.rb b/plugins/discourse-gamification/plugin.rbindex 5be26ea63b5..7359049b720 100644--- a/plugins/discourse-gamification/plugin.rb+++ b/plugins/discourse-gamification/plugin.rb@@ -15,7 +15,7 @@ register_asset "stylesheets/desktop/leaderboard.scss", :desktopregister_asset "stylesheets/mobile/leaderboard.scss", :mobileregister_asset "stylesheets/common/leaderboard-info-modal.scss"register_asset "stylesheets/common/leaderboard-minimal.scss"-register_asset "stylesheets/common/leaderboard-admin.scss"+register_asset "stylesheets/admin/leaderboard.scss", :adminregister_asset "stylesheets/common/gamification-score.scss"register_svg_icon "crown"diff --git a/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/admin/admin.scss b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/admin/admin.scssnew file mode 100644index 00000000000..07b4b5f8e9e--- /dev/null+++ b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/admin/admin.scss@@ -0,0 +1 @@+/* admin */diff --git a/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/common/common.scss b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/common/common.scssnew file mode 100644index 00000000000..731fb791474--- /dev/null+++ b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/common/common.scss@@ -0,0 +1 @@+/* common */diff --git a/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/desktop/desktop.scss b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/desktop/desktop.scssnew file mode 100644index 00000000000..6d07c0875f0--- /dev/null+++ b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/desktop/desktop.scss@@ -0,0 +1 @@+/* desktop */diff --git a/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/mobile/mobile.scss b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/mobile/mobile.scssnew file mode 100644index 00000000000..a01c2eb0bac--- /dev/null+++ b/spec/fixtures/plugins/stylesheet_targets_plugin/assets/stylesheets/mobile/mobile.scss@@ -0,0 +1 @@+/* mobile */diff --git a/spec/fixtures/plugins/stylesheet_targets_plugin/plugin.rb b/spec/fixtures/plugins/stylesheet_targets_plugin/plugin.rbnew file mode 100644index 00000000000..57122cdeca5--- /dev/null+++ b/spec/fixtures/plugins/stylesheet_targets_plugin/plugin.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++# name: stylesheet-targets-plugin+# about: fixture used to exercise per-target plugin stylesheet registration+# version: 0.1+# authors: Discourse++register_asset "stylesheets/common/common.scss"+register_asset "stylesheets/mobile/mobile.scss", :mobile+register_asset "stylesheets/desktop/desktop.scss", :desktop+register_asset "stylesheets/admin/admin.scss", :admindiff --git a/spec/lib/discourse_plugin_registry_spec.rb b/spec/lib/discourse_plugin_registry_spec.rbindex d0799f76bd2..18ade3fd201 100644--- a/spec/lib/discourse_plugin_registry_spec.rb+++ b/spec/lib/discourse_plugin_registry_spec.rb@@ -232,6 +232,15 @@ RSpec.describe DiscoursePluginRegistry doexpect(registry.stylesheets[plugin_directory_name]).to eq(nil)end+ it "registers admin css properly" do+ registry.register_asset("test.css", :admin, plugin_directory_name)++ expect(registry.admin_stylesheets[plugin_directory_name].count).to eq(1)+ expect(registry.stylesheets[plugin_directory_name]).to eq(nil)+ expect(registry.desktop_stylesheets[plugin_directory_name]).to eq(nil)+ expect(registry.mobile_stylesheets[plugin_directory_name]).to eq(nil)+ end+it "registers color definitions properly" doregistry.register_asset("test.css", :color_definitions, plugin_directory_name)expect(registry.color_definition_stylesheets[plugin_directory_name]).to eq("test.css")diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rbindex 888dad46b85..2e229caea72 100644--- a/spec/lib/discourse_spec.rb+++ b/spec/lib/discourse_spec.rb@@ -204,6 +204,13 @@ RSpec.describe Discourse doplugin1.register_asset_filter { |type, request, opts| false }expect(Discourse.find_plugin_css_assets({}).length).to eq(1)end++ it "includes admin plugin css assets when include_admin is true" do+ plugin2.enabled = true++ expect(Discourse.find_plugin_css_assets(include_admin: true).length).to eq(4)+ expect(Discourse.find_plugin_css_assets({}).length).to eq(2)+ endenddescribe "authenticators" dodiff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rbindex 2a6a9c760d9..838dd333726 100644--- a/spec/lib/plugin/instance_spec.rb+++ b/spec/lib/plugin/instance_spec.rb@@ -65,7 +65,7 @@ TEXTdescribe "find_all" doit "can find plugins correctly" doplugins = Plugin::Instance.find_all("#{Rails.root.join("spec/fixtures/plugins")}")- expect(plugins.count).to eq(5)+ expect(plugins.count).to eq(6)plugin = plugins[3]expect(plugin.name).to eq("plugin-name")@@ -439,6 +439,7 @@ TEXTplugin.register_asset("mobile.css", :mobile)plugin.register_asset("desktop.css", :desktop)plugin.register_asset("desktop2.css", :desktop)+ plugin.register_asset("admin.css", :admin)plugin.activate!@@ -446,6 +447,7 @@ TEXTexpect(DiscoursePluginRegistry.desktop_stylesheets[plugin.directory_name].count).to eq(2)expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name].count).to eq(1)+ expect(DiscoursePluginRegistry.admin_stylesheets[plugin.directory_name].count).to eq(1)endenddiff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rbindex d19ecdefcd3..3f5e5c5dbcf 100644--- a/spec/requests/stylesheets_controller_spec.rb+++ b/spec/requests/stylesheets_controller_spec.rb@@ -116,6 +116,61 @@ RSpec.describe StylesheetsController doendend+ context "when a plugin registers assets for each target" do+ fab!(:user)+ fab!(:admin)++ let(:plugin) { plugin_from_fixtures("stylesheet_targets_plugin") }++ matcher :have_stylesheet do |target|+ match { |body| body.include?(%(data-target="stylesheet_targets_#{target}")) }+ end++ before do+ Discourse.plugins << plugin+ plugin.activate!+ Stylesheet::Importer.register_imports!+ StylesheetCache.destroy_all+ end++ after do+ Discourse.plugins.delete(plugin)+ Stylesheet::Importer.register_imports!+ DiscoursePluginRegistry.reset!+ end++ it "renders a link tag for every target when viewer is staff" do+ sign_in(admin)+ get "/latest"++ expect(response.body).to have_stylesheet(:plugin)+ expect(response.body).to have_stylesheet(:plugin_mobile)+ expect(response.body).to have_stylesheet(:plugin_desktop)+ expect(response.body).to have_stylesheet(:plugin_admin)+ end++ it "does not render the admin link tag for non-staff users" do+ sign_in(user)+ get "/latest"++ expect(response.body).to have_stylesheet(:plugin)+ expect(response.body).to have_stylesheet(:plugin_mobile)+ expect(response.body).to have_stylesheet(:plugin_desktop)+ expect(response.body).not_to have_stylesheet(:plugin_admin)+ end++ it "does not render any link tags when the plugin is disabled" do+ plugin.stubs(:enabled?).returns(false)+ sign_in(admin)+ get "/latest"++ expect(response.body).not_to have_stylesheet(:plugin)+ expect(response.body).not_to have_stylesheet(:plugin_mobile)+ expect(response.body).not_to have_stylesheet(:plugin_desktop)+ expect(response.body).not_to have_stylesheet(:plugin_admin)+ end+ end+it "ignores Accept header and does not include Vary header" doStylesheetCache.destroy_allmanager = Stylesheet::Manager.new(theme_id: nil)