Skip to content

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.rb
index 60c92365988..524ddb6df70 100644
--- a/app/controllers/bootstrap_controller.rb
+++ b/app/controllers/bootstrap_controller.rb
@@ -61,7 +61,12 @@ class BootstrapController < ApplicationController
plugin_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.rb
index c4cbdd7a050..c3d560cd84c 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -506,7 +506,13 @@ class Theme < ActiveRecord::Base
if with_scheme
targets.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_cache
end
diff --git a/app/views/common/_discourse_preload_stylesheet.html.erb b/app/views/common/_discourse_preload_stylesheet.html.erb
index 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.erb
index 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.rb
index 5c021429be8..a1f215fd65b 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -437,6 +437,7 @@ module Discourse
targets = [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.rb
index 8542d9e56be..c322387caa2 100644
--- a/lib/discourse_plugin_registry.rb
+++ b/lib/discourse_plugin_registry.rb
@@ -58,6 +58,7 @@ class DiscoursePluginRegistry
define_register :stylesheets, Hash
define_register :mobile_stylesheets, Hash
define_register :desktop_stylesheets, Hash
+ define_register :admin_stylesheets, Hash
define_register :color_definition_stylesheets, Hash
define_register :serialized_current_user_fields, Set
define_register :seed_data, ActiveSupport::HashWithIndifferentAccess
@@ -203,6 +204,9 @@ class DiscoursePluginRegistry
elsif opts == :desktop
desktop_stylesheets[plugin_directory_name] ||= Set.new
desktop_stylesheets[plugin_directory_name] << asset
+ elsif opts == :admin
+ admin_stylesheets[plugin_directory_name] ||= Set.new
+ admin_stylesheets[plugin_directory_name] << asset
elsif opts == :color_definitions
color_definition_stylesheets[plugin_directory_name] = asset
else
@@ -218,6 +222,8 @@ class DiscoursePluginRegistry
desktop_stylesheets[plugin_directory_name].present?
when :mobile
mobile_stylesheets[plugin_directory_name].present?
+ when :admin
+ admin_stylesheets[plugin_directory_name].present?
else
stylesheets[plugin_directory_name].present?
end
diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rb
index 249674595db..bb019343227 100644
--- a/lib/stylesheet/importer.rb
+++ b/lib/stylesheet/importer.rb
@@ -16,7 +16,7 @@ module Stylesheet
Discourse.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_name
stylesheets =
(
diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 547ca73503d..9e00abf1910 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -57,12 +57,14 @@ class Stylesheet::Manager
include_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.rb
index e7d3fb862ff..112911aae6c 100644
--- a/lib/stylesheet/manager/builder.rb
+++ b/lib/stylesheet/manager/builder.rb
@@ -196,6 +196,7 @@ class Stylesheet::Manager::Builder
DiscoursePluginRegistry.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)
end
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss b/plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss
similarity index 100%
rename from plugins/discourse-ai/assets/stylesheets/modules/translation/common/admin-translations.scss
rename to plugins/discourse-ai/assets/stylesheets/modules/translation/admin/translations.scss
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 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", :admin
register_asset "stylesheets/modules/ai-helper/common/ai-helper.scss"
register_asset "stylesheets/modules/ai-helper/desktop/ai-helper-fk-modals.scss", :desktop
diff --git a/plugins/discourse-gamification/assets/stylesheets/common/leaderboard-admin.scss b/plugins/discourse-gamification/assets/stylesheets/admin/leaderboard.scss
similarity index 100%
rename from plugins/discourse-gamification/assets/stylesheets/common/leaderboard-admin.scss
rename to plugins/discourse-gamification/assets/stylesheets/admin/leaderboard.scss
diff --git a/plugins/discourse-gamification/plugin.rb b/plugins/discourse-gamification/plugin.rb
index 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", :desktop
register_asset "stylesheets/mobile/leaderboard.scss", :mobile
register_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", :admin
register_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.scss
new file mode 100644
index 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.scss
new file mode 100644
index 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.scss
new file mode 100644
index 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.scss
new file mode 100644
index 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.rb
new file mode 100644
index 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", :admin
diff --git a/spec/lib/discourse_plugin_registry_spec.rb b/spec/lib/discourse_plugin_registry_spec.rb
index 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 do
expect(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" do
registry.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.rb
index 888dad46b85..2e229caea72 100644
--- a/spec/lib/discourse_spec.rb
+++ b/spec/lib/discourse_spec.rb
@@ -204,6 +204,13 @@ RSpec.describe Discourse do
plugin1.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)
+ end
end
describe "authenticators" do
diff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rb
index 2a6a9c760d9..838dd333726 100644
--- a/spec/lib/plugin/instance_spec.rb
+++ b/spec/lib/plugin/instance_spec.rb
@@ -65,7 +65,7 @@ TEXT
describe "find_all" do
it "can find plugins correctly" do
plugins = 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 @@ TEXT
plugin.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 @@ TEXT
expect(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)
end
end
diff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rb
index d19ecdefcd3..3f5e5c5dbcf 100644
--- a/spec/requests/stylesheets_controller_spec.rb
+++ b/spec/requests/stylesheets_controller_spec.rb
@@ -116,6 +116,61 @@ RSpec.describe StylesheetsController do
end
end
+ 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" do
StylesheetCache.destroy_all
manager = Stylesheet::Manager.new(theme_id: nil)