FEATURE: Add stylesheets to bootstrap.json endpoint

This allows API consumers (such as Ember CLI) to dynamically get a list
of styles to embed.
This commit is contained in:
Robin Ward 2020-09-04 14:09:55 -04:00
parent 110f6ec6dd
commit 0f9a58e06f
3 changed files with 49 additions and 2 deletions

View File

@ -15,15 +15,60 @@ class BootstrapController < ApplicationController
preload_current_user_data preload_current_user_data
end end
@stylesheets = []
add_scheme(scheme_id, 'all')
add_scheme(dark_scheme_id, '(prefers-color-scheme: dark)')
if rtl?
add_style(mobile_view? ? :mobile_rtl : :desktop_rtl)
else
add_style(mobile_view? ? :mobile : :desktop)
end
add_style(:admin) if staff?
Discourse.find_plugin_css_assets(
include_official: allow_plugins?,
include_unofficial: allow_third_party_plugins?,
mobile_view: mobile_view?,
desktop_view: !mobile_view?,
request: request
).each do |file|
add_style(file)
end
add_style(mobile_view? ? :mobile_theme : :desktop_theme) if theme_ids.present?
bootstrap = { bootstrap = {
theme_ids: theme_ids, theme_ids: theme_ids,
title: SiteSetting.title, title: SiteSetting.title,
current_homepage: current_homepage, current_homepage: current_homepage,
locale_script: "#{Discourse.base_url}#{locale}", locale_script: locale,
stylesheets: @stylesheets,
setup_data: client_side_setup_data, setup_data: client_side_setup_data,
preloaded: @preloaded preloaded: @preloaded
} }
render_json_dump(bootstrap: bootstrap) render_json_dump(bootstrap: bootstrap)
end end
private
def add_scheme(scheme_id, media)
return if scheme_id.to_i == -1
theme_id = theme_ids&.first
if style = Stylesheet::Manager.color_scheme_stylesheet_details(scheme_id, media, theme_id)
@stylesheets << { href: style[:new_href], media: media }
end
end
def add_style(target)
if styles = Stylesheet::Manager.stylesheet_details(target, 'all', theme_ids)
styles.each do |style|
@stylesheets << {
href: style[:new_href],
media: 'all',
theme_id: style[:theme_id],
target: style[:target]
}
end
end
end
end end

View File

@ -1,4 +1,4 @@
<%= discourse_color_scheme_stylesheets() %> <%= discourse_color_scheme_stylesheets %>
<%- if rtl? %> <%- if rtl? %>
<%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_rtl : :desktop_rtl) %> <%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_rtl : :desktop_rtl) %>

View File

@ -15,6 +15,8 @@ describe BootstrapController do
expect(bootstrap).to be_present expect(bootstrap).to be_present
expect(bootstrap['title']).to be_present expect(bootstrap['title']).to be_present
expect(bootstrap['setup_data']['base_url']).to eq(Discourse.base_url) expect(bootstrap['setup_data']['base_url']).to eq(Discourse.base_url)
expect(bootstrap['stylesheets']).to be_present
preloaded = bootstrap['preloaded'] preloaded = bootstrap['preloaded']
expect(preloaded['site']).to be_present expect(preloaded['site']).to be_present
expect(preloaded['siteSettings']).to be_present expect(preloaded['siteSettings']).to be_present