2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
require "distributed_cache"
|
|
|
|
require "stylesheet/compiler"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
end
|
|
|
|
|
|
|
|
class Stylesheet::Manager
|
2022-11-08 00:13:35 +08:00
|
|
|
BASE_COMPILER_VERSION = 1
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2024-01-03 13:15:35 +08:00
|
|
|
CACHE_PATH = "tmp/stylesheet-cache"
|
|
|
|
private_constant :CACHE_PATH
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
MANIFEST_DIR ||= "#{Rails.root}/tmp/cache/assets/#{Rails.env}"
|
2023-01-21 02:52:49 +08:00
|
|
|
THEME_REGEX ||= /_theme\z/
|
2020-08-04 10:57:10 +08:00
|
|
|
COLOR_SCHEME_STYLESHEET ||= "color_definitions"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
@@lock = Mutex.new
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@cache ||= DistributedCache.new("discourse_stylesheet")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_theme_cache!
|
2024-02-22 04:19:04 +08:00
|
|
|
cache.clear_regex(/theme/)
|
2019-10-10 13:10:23 +08:00
|
|
|
end
|
|
|
|
|
2020-08-19 01:02:13 +08:00
|
|
|
def self.clear_color_scheme_cache!
|
2024-02-22 04:19:04 +08:00
|
|
|
cache.clear_regex(/color_definitions/)
|
2020-08-19 01:02:13 +08:00
|
|
|
end
|
|
|
|
|
2019-10-10 13:10:23 +08:00
|
|
|
def self.clear_core_cache!(targets)
|
2024-02-22 04:19:04 +08:00
|
|
|
cache.clear_regex(/#{targets.join("|")}/)
|
2019-10-10 13:10:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_plugin_cache!(plugin)
|
2024-02-22 04:19:04 +08:00
|
|
|
cache.clear_regex(/#{plugin}/)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2020-08-19 01:02:13 +08:00
|
|
|
def self.color_scheme_cache_key(color_scheme, theme_id = nil)
|
2020-08-12 04:28:59 +08:00
|
|
|
color_scheme_name = Slug.for(color_scheme.name) + color_scheme&.id.to_s
|
2020-08-19 01:02:13 +08:00
|
|
|
theme_string = theme_id ? "_theme#{theme_id}" : ""
|
2021-07-07 01:11:10 +08:00
|
|
|
"#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}_#{theme_string}_#{Discourse.current_hostname}"
|
2020-08-12 04:28:59 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def self.precompile_css
|
2023-06-01 10:27:11 +08:00
|
|
|
targets = %i[desktop mobile admin wizard desktop_rtl mobile_rtl admin_rtl wizard_rtl]
|
|
|
|
|
|
|
|
targets +=
|
|
|
|
Discourse.find_plugin_css_assets(
|
|
|
|
include_disabled: true,
|
|
|
|
mobile_view: true,
|
|
|
|
desktop_view: true,
|
|
|
|
)
|
2021-07-07 01:11:10 +08:00
|
|
|
targets +=
|
|
|
|
Discourse.find_plugin_css_assets(
|
|
|
|
include_disabled: true,
|
|
|
|
mobile_view: true,
|
|
|
|
desktop_view: true,
|
2023-06-01 10:27:11 +08:00
|
|
|
rtl: true,
|
2021-07-07 01:11:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
targets.each do |target|
|
|
|
|
$stderr.puts "precompile target: #{target}"
|
|
|
|
|
|
|
|
Stylesheet::Manager::Builder.new(target: target, manager: nil).compile(force: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.precompile_theme_css
|
2021-07-01 10:09:39 +08:00
|
|
|
themes =
|
|
|
|
Theme.where("user_selectable OR id = ?", SiteSetting.default_theme_id).pluck(
|
|
|
|
:id,
|
|
|
|
:color_scheme_id,
|
|
|
|
)
|
2020-11-12 07:17:38 +08:00
|
|
|
|
|
|
|
color_schemes = ColorScheme.where(user_selectable: true).to_a
|
|
|
|
color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id)
|
2021-07-07 01:11:10 +08:00
|
|
|
color_schemes << ColorScheme.base
|
2020-11-12 07:17:38 +08:00
|
|
|
color_schemes = color_schemes.compact.uniq
|
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
targets = %i[desktop_theme mobile_theme]
|
2021-07-01 10:09:39 +08:00
|
|
|
compiled = Set.new
|
2020-11-12 07:17:38 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
themes.each do |theme_id, color_scheme_id|
|
2021-06-15 14:57:17 +08:00
|
|
|
manager = self.new(theme_id: theme_id)
|
2017-04-13 00:46:03 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
targets.each do |target|
|
2021-07-07 01:11:10 +08:00
|
|
|
next if theme_id == -1
|
2021-07-01 10:09:39 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
scss_checker = ScssChecker.new(target, manager.theme_ids)
|
2021-06-30 16:06:13 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
manager
|
|
|
|
.load_themes(manager.theme_ids)
|
|
|
|
.each do |theme|
|
|
|
|
next if compiled.include?("#{target}_#{theme.id}")
|
2021-06-30 16:06:13 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
builder =
|
2021-07-01 10:52:21 +08:00
|
|
|
Stylesheet::Manager::Builder.new(target: target, theme: theme, manager: manager)
|
2021-07-07 01:11:10 +08:00
|
|
|
|
|
|
|
next if theme.component && !scss_checker.has_scss(theme.id)
|
|
|
|
$stderr.puts "precompile target: #{target} #{theme.name}"
|
|
|
|
builder.compile(force: true)
|
|
|
|
compiled << "#{target}_#{theme.id}"
|
2021-05-20 14:01:54 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2020-08-04 10:57:10 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
theme_color_scheme = ColorScheme.find_by_id(color_scheme_id)
|
2021-07-01 10:09:39 +08:00
|
|
|
theme = manager.get_theme(theme_id)
|
2020-08-04 10:57:10 +08:00
|
|
|
|
2021-07-07 01:11:10 +08:00
|
|
|
[theme_color_scheme, *color_schemes].compact.uniq.each do |scheme|
|
|
|
|
$stderr.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{theme.name} (#{scheme.name})"
|
2020-11-12 07:17:38 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
Stylesheet::Manager::Builder.new(
|
|
|
|
target: COLOR_SCHEME_STYLESHEET,
|
2021-07-01 10:09:39 +08:00
|
|
|
theme: theme,
|
2021-06-15 14:57:17 +08:00
|
|
|
color_scheme: scheme,
|
|
|
|
manager: manager,
|
|
|
|
).compile(force: true)
|
2020-11-12 07:17:38 +08:00
|
|
|
end
|
2021-07-01 10:09:39 +08:00
|
|
|
|
2020-08-19 01:02:13 +08:00
|
|
|
clear_color_scheme_cache!
|
2020-08-04 10:57:10 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-11-08 00:13:35 +08:00
|
|
|
def self.fs_asset_cachebuster
|
|
|
|
if use_file_hash_for_cachebuster?
|
|
|
|
@cachebuster ||=
|
|
|
|
if File.exist?(manifest_full_path)
|
|
|
|
File.readlines(manifest_full_path, "r")[0]
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
2022-11-08 00:13:35 +08:00
|
|
|
cachebuster = "#{BASE_COMPILER_VERSION}:#{fs_assets_hash}"
|
2017-04-12 22:52:52 +08:00
|
|
|
FileUtils.mkdir_p(MANIFEST_DIR)
|
2022-11-08 00:13:35 +08:00
|
|
|
File.open(manifest_full_path, "w") { |f| f.print(cachebuster) }
|
|
|
|
cachebuster
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
else
|
2022-11-08 00:13:35 +08:00
|
|
|
"#{BASE_COMPILER_VERSION}:#{max_file_mtime}"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-08 00:13:35 +08:00
|
|
|
def self.recalculate_fs_asset_cachebuster!
|
|
|
|
File.delete(manifest_full_path) if File.exist?(manifest_full_path)
|
|
|
|
@cachebuster = nil
|
|
|
|
fs_asset_cachebuster
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.manifest_full_path
|
|
|
|
path = "#{MANIFEST_DIR}/stylesheet-manifest"
|
|
|
|
return path if !Rails.env.test?
|
|
|
|
"#{path}-test_#{ENV["TEST_ENV_NUMBER"].presence || "0"}"
|
|
|
|
end
|
|
|
|
private_class_method :manifest_full_path
|
|
|
|
|
|
|
|
def self.use_file_hash_for_cachebuster?
|
|
|
|
Rails.env.production?
|
|
|
|
end
|
|
|
|
private_class_method :use_file_hash_for_cachebuster?
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2022-11-08 00:13:35 +08:00
|
|
|
def self.list_files
|
|
|
|
globs = [
|
|
|
|
"#{Rails.root}/app/assets/stylesheets/**/*.*css",
|
|
|
|
"#{Rails.root}/app/assets/images/**/*.*",
|
|
|
|
]
|
|
|
|
|
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
path = File.dirname(plugin.path)
|
2017-04-13 00:30:24 +08:00
|
|
|
globs << "#{path}/plugin.rb"
|
2021-04-29 00:27:18 +08:00
|
|
|
globs << "#{path}/assets/stylesheets/**/*.*css"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2022-11-08 00:13:35 +08:00
|
|
|
globs.flat_map { |g| Dir.glob(g) }.compact
|
|
|
|
end
|
|
|
|
private_class_method :list_files
|
|
|
|
|
|
|
|
def self.max_file_mtime
|
|
|
|
list_files.map { |x| File.mtime(x) }.compact.max.to_i
|
|
|
|
end
|
|
|
|
private_class_method :max_file_mtime
|
|
|
|
|
|
|
|
def self.fs_assets_hash
|
|
|
|
hashes = list_files.sort.map { |x| Digest::SHA1.hexdigest("#{x}: #{File.read(x)}") }
|
|
|
|
Digest::SHA1.hexdigest(hashes.join("|"))
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2022-11-08 00:13:35 +08:00
|
|
|
private_class_method :fs_assets_hash
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
def self.cache_fullpath
|
2021-07-26 12:22:54 +08:00
|
|
|
path = "#{Rails.root}/#{CACHE_PATH}"
|
|
|
|
return path if !Rails.env.test?
|
|
|
|
File.join(path, "test_#{ENV["TEST_ENV_NUMBER"].presence || "0"}")
|
2018-08-08 12:46:34 +08:00
|
|
|
end
|
|
|
|
|
2024-01-03 13:15:35 +08:00
|
|
|
if Rails.env.test?
|
|
|
|
def self.rm_cache_folder
|
|
|
|
FileUtils.rm_rf(cache_fullpath)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
attr_reader :theme_ids
|
|
|
|
|
|
|
|
def initialize(theme_id: nil)
|
2021-06-18 10:16:26 +08:00
|
|
|
@theme_id = theme_id
|
2021-06-15 14:57:17 +08:00
|
|
|
@theme_ids = Theme.transform_ids(@theme_id)
|
|
|
|
@themes_cache = {}
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def cache
|
|
|
|
self.class.cache
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def get_theme(theme_id)
|
|
|
|
if theme = @themes_cache[theme_id]
|
|
|
|
theme
|
|
|
|
else
|
|
|
|
load_themes([theme_id]).first
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2021-06-15 14:57:17 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def load_themes(theme_ids)
|
|
|
|
themes = []
|
|
|
|
to_load_theme_ids = []
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
theme_ids.each do |theme_id|
|
|
|
|
if @themes_cache[theme_id]
|
|
|
|
themes << @themes_cache[theme_id]
|
|
|
|
else
|
|
|
|
to_load_theme_ids << theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
Theme
|
|
|
|
.where(id: to_load_theme_ids)
|
|
|
|
.includes(:yaml_theme_fields, :theme_settings, :upload_fields, :builder_theme_fields)
|
|
|
|
.each do |theme|
|
|
|
|
@themes_cache[theme.id] = theme
|
|
|
|
themes << theme
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
themes
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def stylesheet_data(target = :desktop)
|
|
|
|
stylesheet_details(target, "all")
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2022-07-05 00:23:09 +08:00
|
|
|
def stylesheet_preload_tag(target = :desktop, media = "all")
|
|
|
|
stylesheets = stylesheet_details(target, media)
|
|
|
|
stylesheets
|
|
|
|
.map do |stylesheet|
|
|
|
|
href = stylesheet[:new_href]
|
|
|
|
%[<link href="#{href}" rel="preload" as="style"/>]
|
|
|
|
end
|
|
|
|
.join("\n")
|
|
|
|
.html_safe
|
|
|
|
end
|
|
|
|
|
2022-10-12 07:11:44 +08:00
|
|
|
def stylesheet_link_tag(target = :desktop, media = "all", preload_callback = nil)
|
2021-06-15 14:57:17 +08:00
|
|
|
stylesheets = stylesheet_details(target, media)
|
|
|
|
stylesheets
|
|
|
|
.map do |stylesheet|
|
|
|
|
href = stylesheet[:new_href]
|
2022-10-12 07:11:44 +08:00
|
|
|
preload_callback.call(href, "style") if preload_callback
|
2021-06-15 14:57:17 +08:00
|
|
|
theme_id = stylesheet[:theme_id]
|
|
|
|
data_theme_id = theme_id ? "data-theme-id=\"#{theme_id}\"" : ""
|
2021-07-16 00:51:46 +08:00
|
|
|
theme_name = stylesheet[:theme_name]
|
2021-12-13 18:50:09 +08:00
|
|
|
data_theme_name = theme_name ? "data-theme-name=\"#{CGI.escapeHTML(theme_name)}\"" : ""
|
2021-07-16 00:51:46 +08:00
|
|
|
%[<link href="#{href}" media="#{media}" rel="stylesheet" data-target="#{target}" #{data_theme_id} #{data_theme_name}/>]
|
2021-06-15 14:57:17 +08:00
|
|
|
end
|
|
|
|
.join("\n")
|
|
|
|
.html_safe
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def stylesheet_details(target = :desktop, media = "all")
|
|
|
|
target = target.to_sym
|
|
|
|
current_hostname = Discourse.current_hostname
|
2021-07-07 01:11:10 +08:00
|
|
|
is_theme_target = !!(target.to_s =~ THEME_REGEX)
|
|
|
|
|
|
|
|
array_cache_key =
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2021-07-07 01:11:10 +08:00
|
|
|
if is_theme_target
|
|
|
|
"array_themes_#{@theme_ids.join(",")}_#{target}_#{current_hostname}"
|
2023-01-09 20:10:19 +08:00
|
|
|
else
|
2021-07-07 01:11:10 +08:00
|
|
|
"array_#{target}_#{current_hostname}"
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2024-03-01 01:10:24 +08:00
|
|
|
cache.defer_get_set(array_cache_key) do
|
|
|
|
@@lock.synchronize do
|
|
|
|
stylesheets = []
|
|
|
|
|
|
|
|
if is_theme_target
|
|
|
|
scss_checker = ScssChecker.new(target, @theme_ids)
|
|
|
|
themes = load_themes(@theme_ids)
|
|
|
|
themes.each do |theme|
|
|
|
|
theme_id = theme&.id
|
|
|
|
data = {
|
|
|
|
target: target,
|
|
|
|
theme_id: theme_id,
|
|
|
|
theme_name: theme&.name&.downcase,
|
|
|
|
remote: theme.remote_theme_id?,
|
|
|
|
}
|
|
|
|
builder = Builder.new(target: target, theme: theme, manager: self)
|
|
|
|
|
|
|
|
next if builder.theme&.component && !scss_checker.has_scss(theme_id)
|
|
|
|
builder.compile unless File.exist?(builder.stylesheet_fullpath)
|
|
|
|
href = builder.stylesheet_absolute_url
|
|
|
|
|
|
|
|
data[:new_href] = href
|
|
|
|
stylesheets << data
|
|
|
|
end
|
|
|
|
|
|
|
|
if stylesheets.size > 1
|
|
|
|
stylesheets =
|
|
|
|
stylesheets.sort_by do |s|
|
|
|
|
[s[:remote] ? 0 : 1, s[:theme_id] == @theme_id ? 1 : 0, s[:theme_name]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
builder = Builder.new(target: target, manager: self)
|
2022-01-06 01:45:08 +08:00
|
|
|
builder.compile unless File.exist?(builder.stylesheet_fullpath)
|
2022-02-15 03:53:39 +08:00
|
|
|
href = builder.stylesheet_absolute_url
|
2021-06-22 08:27:11 +08:00
|
|
|
|
2024-03-01 01:10:24 +08:00
|
|
|
data = { target: target, new_href: href }
|
2021-07-07 01:11:10 +08:00
|
|
|
stylesheets << data
|
2021-06-30 21:33:15 +08:00
|
|
|
end
|
2021-07-16 00:51:46 +08:00
|
|
|
|
2024-03-01 01:10:24 +08:00
|
|
|
stylesheets
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
def color_scheme_stylesheet_details(color_scheme_id = nil, media)
|
2021-06-18 10:16:26 +08:00
|
|
|
theme_id = @theme_id || SiteSetting.default_theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
color_scheme =
|
|
|
|
begin
|
|
|
|
ColorScheme.find(color_scheme_id)
|
|
|
|
rescue StandardError
|
|
|
|
# don't load fallback when requesting dark color scheme
|
|
|
|
return false if media != "all"
|
2021-04-27 21:33:43 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
get_theme(theme_id)&.color_scheme || ColorScheme.base
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
return false if !color_scheme
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
target = COLOR_SCHEME_STYLESHEET.to_sym
|
|
|
|
current_hostname = Discourse.current_hostname
|
|
|
|
cache_key = self.class.color_scheme_cache_key(color_scheme, theme_id)
|
2018-08-30 18:53:03 +08:00
|
|
|
|
2024-02-21 08:29:01 +08:00
|
|
|
cache.defer_get_set(cache_key) do
|
|
|
|
stylesheet = { color_scheme_id: color_scheme.id }
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2024-02-21 08:29:01 +08:00
|
|
|
theme = get_theme(theme_id)
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2024-02-21 08:29:01 +08:00
|
|
|
builder =
|
|
|
|
Builder.new(
|
|
|
|
target: target,
|
|
|
|
theme: get_theme(theme_id),
|
|
|
|
color_scheme: color_scheme,
|
|
|
|
manager: self,
|
|
|
|
)
|
2021-06-21 12:44:41 +08:00
|
|
|
|
2024-02-21 08:29:01 +08:00
|
|
|
builder.compile unless File.exist?(builder.stylesheet_fullpath)
|
2021-06-21 12:44:41 +08:00
|
|
|
|
2024-02-21 08:29:01 +08:00
|
|
|
href = builder.stylesheet_absolute_url
|
|
|
|
stylesheet[:new_href] = href
|
|
|
|
stylesheet.freeze
|
|
|
|
end
|
2018-06-13 21:54:26 +08:00
|
|
|
end
|
|
|
|
|
2022-07-05 00:23:09 +08:00
|
|
|
def color_scheme_stylesheet_preload_tag(color_scheme_id = nil, media = "all")
|
|
|
|
stylesheet = color_scheme_stylesheet_details(color_scheme_id, media)
|
|
|
|
|
|
|
|
return "" if !stylesheet
|
|
|
|
|
|
|
|
href = stylesheet[:new_href]
|
|
|
|
|
|
|
|
%[<link href="#{href}" rel="preload" as="style"/>].html_safe
|
|
|
|
end
|
|
|
|
|
2022-10-12 07:11:44 +08:00
|
|
|
def color_scheme_stylesheet_link_tag(color_scheme_id = nil, media = "all", preload_callback = nil)
|
2021-06-15 14:57:17 +08:00
|
|
|
stylesheet = color_scheme_stylesheet_details(color_scheme_id, media)
|
2020-08-12 04:28:59 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
return "" if !stylesheet
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
href = stylesheet[:new_href]
|
2022-10-12 07:11:44 +08:00
|
|
|
preload_callback.call(href, "style") if preload_callback
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
css_class = media == "all" ? "light-scheme" : "dark-scheme"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-06-15 14:57:17 +08:00
|
|
|
%[<link href="#{href}" media="#{media}" rel="stylesheet" class="#{css_class}"/>].html_safe
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|