FEATURE: Serve RTL versions of admin and plugins CSS bundles for RTL locales (#21876)

Prior to this commit, we didn't have RTL versions of our admin and plugins CSS bundles and we always served LTR versions of those bundles even when users used an RTL locale, causing admin and plugins UI elements to never look as good as when an LTR locale was used. Example of UI issues prior to this commit were: missing margins, borders on the wrong side and buttons too close to each other etc.

This commit creates an RTL version for the admin CSS bundle as well as RTL bundles for all the installed plugins and serves those RTL bundles to users/sites who use RTL locales.
This commit is contained in:
Osama Sayegh
2023-06-01 05:27:11 +03:00
committed by GitHub
parent d10a050da2
commit c2fcd55a80
15 changed files with 187 additions and 58 deletions

View File

@ -89,6 +89,20 @@ RSpec.describe Stylesheet::Compiler do
expect(css).to include("background:")
end
context "with the `rtl` option" do
it "generates an RTL version of the plugin CSS if the option is true" do
css, _ = Stylesheet::Compiler.compile_asset("scss_plugin", theme_id: theme.id, rtl: true)
expect(css).to include(".pull-left{float:right}")
expect(css).not_to include(".pull-left{float:left}")
end
it "returns an unchanged version of the plugin CSS" do
css, _ = Stylesheet::Compiler.compile_asset("scss_plugin", theme_id: theme.id, rtl: false)
expect(css).to include(".pull-left{float:left}")
expect(css).not_to include(".pull-left{float:right}")
end
end
it "supports SCSS imports" do
css, _map = Stylesheet::Compiler.compile_asset("scss_plugin", theme_id: theme.id)

View File

@ -877,7 +877,11 @@ RSpec.describe Stylesheet::Manager do
end
end
describe ".precompile css" do
describe ".precompile_css" do
let(:core_targets) do
%w[desktop mobile admin wizard desktop_rtl mobile_rtl admin_rtl wizard_rtl]
end
before { STDERR.stubs(:write) }
after do
@ -888,7 +892,6 @@ RSpec.describe Stylesheet::Manager do
it "correctly generates precompiled CSS" do
scheme1 = ColorScheme.create!(name: "scheme1")
scheme2 = ColorScheme.create!(name: "scheme2")
core_targets = %i[desktop mobile desktop_rtl mobile_rtl admin wizard]
theme_targets = %i[desktop_theme mobile_theme]
Theme.update_all(user_selectable: false)
@ -922,7 +925,7 @@ RSpec.describe Stylesheet::Manager do
output = capture_output(:stderr) { Stylesheet::Manager.precompile_css }
results = StylesheetCache.pluck(:target)
expect(results.size).to eq(core_targets.size)
expect(results).to contain_exactly(*core_targets)
StylesheetCache.destroy_all
@ -937,7 +940,7 @@ RSpec.describe Stylesheet::Manager do
# themes + core
Stylesheet::Manager.precompile_css
results = StylesheetCache.pluck(:target)
expect(results.size).to eq(28) # 9 core targets + 9 theme + 10 color schemes
expect(results.size).to eq(30) # 11 core targets + 9 theme + 10 color schemes
theme_targets.each do |tar|
expect(
@ -952,7 +955,7 @@ RSpec.describe Stylesheet::Manager do
Stylesheet::Manager.precompile_css
Stylesheet::Manager.precompile_theme_css
results = StylesheetCache.pluck(:target)
expect(results.size).to eq(28) # 9 core targets + 9 theme + 10 color schemes
expect(results.size).to eq(30) # 11 core targets + 9 theme + 10 color schemes
expect(results).to include("color_definitions_#{scheme1.name}_#{scheme1.id}_#{user_theme.id}")
expect(results).to include(
@ -969,7 +972,6 @@ RSpec.describe Stylesheet::Manager do
upload = UploadCreator.new(image, "logo.png").create_for(-1)
scheme = ColorScheme.create!(name: "scheme")
core_targets = %i[desktop mobile desktop_rtl mobile_rtl admin wizard]
theme_targets = %i[desktop_theme mobile_theme]
default_theme =
@ -1009,6 +1011,71 @@ RSpec.describe Stylesheet::Manager do
css = File.read(theme_builder.stylesheet_fullpath)
expect(css).to include("border:3px solid green}")
end
context "when there are enabled plugins" do
let(:plugin1) do
plugin1 = Plugin::Instance.new
plugin1.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
plugin1.register_css "body { padding: 1px 2px 3px 4px; }"
plugin1
end
let(:plugin2) do
plugin2 = Plugin::Instance.new
plugin2.path = "#{Rails.root}/spec/fixtures/plugins/scss_plugin/plugin.rb"
plugin2
end
before do
Discourse.plugins << plugin1
Discourse.plugins << plugin2
plugin1.activate!
plugin2.activate!
Stylesheet::Importer.register_imports!
StylesheetCache.destroy_all
end
after do
Discourse.plugins.delete(plugin1)
Discourse.plugins.delete(plugin2)
Stylesheet::Importer.register_imports!
DiscoursePluginRegistry.reset!
end
it "generates LTR and RTL CSS for plugins" do
output = capture_output(:stderr) { Stylesheet::Manager.precompile_css }
results = StylesheetCache.pluck(:target)
expect(results).to contain_exactly(
*core_targets,
"my_plugin",
"my_plugin_rtl",
"scss_plugin",
"scss_plugin_rtl",
)
expect(output.scan(/my_plugin$/).length).to eq(1)
expect(output.scan(/my_plugin_rtl$/).length).to eq(1)
expect(output.scan(/scss_plugin$/).length).to eq(1)
expect(output.scan(/scss_plugin_rtl$/).length).to eq(1)
plugin1_ltr_css = StylesheetCache.where(target: "my_plugin").pluck(:content).first
plugin1_rtl_css = StylesheetCache.where(target: "my_plugin_rtl").pluck(:content).first
expect(plugin1_ltr_css).to include("body{padding:1px 2px 3px 4px}")
expect(plugin1_ltr_css).not_to include("body{padding:1px 4px 3px 2px}")
expect(plugin1_rtl_css).to include("body{padding:1px 4px 3px 2px}")
expect(plugin1_rtl_css).not_to include("body{padding:1px 2px 3px 4px}")
plugin2_ltr_css = StylesheetCache.where(target: "scss_plugin").pluck(:content).first
plugin2_rtl_css = StylesheetCache.where(target: "scss_plugin_rtl").pluck(:content).first
expect(plugin2_ltr_css).to include(".pull-left{float:left}")
expect(plugin2_ltr_css).not_to include(".pull-left{float:right}")
expect(plugin2_rtl_css).to include(".pull-left{float:right}")
expect(plugin2_rtl_css).not_to include(".pull-left{float:left}")
end
end
end
describe ".fs_asset_cachebuster" do