mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: More refactoring of SCSS importers (#12143)
This commit is contained in:
@@ -9,12 +9,13 @@ module Stylesheet
|
||||
class Compiler
|
||||
|
||||
def self.compile_asset(asset, options = {})
|
||||
file = "@import \"common/foundation/variables\"; @import \"common/foundation/mixins\";"
|
||||
importer = Importer.new(options)
|
||||
file = importer.prepended_scss
|
||||
|
||||
if Importer::THEME_TARGETS.include?(asset.to_s)
|
||||
filename = "theme_#{options[:theme_id]}.scss"
|
||||
file += options[:theme_variables].to_s
|
||||
file += Importer.new({ theme_id: options[:theme_id] }).theme_import(asset)
|
||||
file += importer.theme_import(asset)
|
||||
elsif Importer.special_imports[asset.to_s]
|
||||
filename = "theme_#{options[:theme_id]}.scss"
|
||||
file += " @import \"#{asset}\";"
|
||||
@@ -24,13 +25,12 @@ module Stylesheet
|
||||
file += File.read path
|
||||
|
||||
if asset.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET
|
||||
file += Stylesheet::Importer.import_color_definitions(options[:theme_id])
|
||||
file += Stylesheet::Importer.import_wcag_overrides(options[:color_scheme_id])
|
||||
file += importer.import_color_definitions
|
||||
file += importer.import_wcag_overrides
|
||||
end
|
||||
end
|
||||
|
||||
compile(file, filename, options)
|
||||
|
||||
end
|
||||
|
||||
def self.compile(stylesheet, filename, options = {})
|
||||
|
||||
@@ -95,25 +95,6 @@ module Stylesheet
|
||||
import_files(DiscoursePluginRegistry.sass_variables)
|
||||
end
|
||||
|
||||
register_import "theme_colors" do
|
||||
contents = +""
|
||||
if @color_scheme_id
|
||||
colors = begin
|
||||
ColorScheme.find(@color_scheme_id).resolved_colors
|
||||
rescue
|
||||
ColorScheme.base_colors
|
||||
end
|
||||
else
|
||||
colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
|
||||
end
|
||||
|
||||
colors.each do |n, hex|
|
||||
contents << "$#{n}: ##{hex} !default;\n"
|
||||
end
|
||||
|
||||
Import.new("theme_colors.scss", source: contents)
|
||||
end
|
||||
|
||||
register_import "category_backgrounds" do
|
||||
contents = +""
|
||||
Category.where('uploaded_background_id IS NOT NULL').each do |c|
|
||||
@@ -127,7 +108,7 @@ module Stylesheet
|
||||
|
||||
register_imports!
|
||||
|
||||
def self.import_color_definitions(theme_id)
|
||||
def import_color_definitions
|
||||
contents = +""
|
||||
DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path|
|
||||
contents << "// Color definitions from #{name}\n\n"
|
||||
@@ -135,7 +116,7 @@ module Stylesheet
|
||||
contents << "\n\n"
|
||||
end
|
||||
|
||||
theme_id ||= SiteSetting.default_theme_id
|
||||
theme_id = @theme_id || SiteSetting.default_theme_id
|
||||
resolved_ids = Theme.transform_ids([theme_id])
|
||||
|
||||
if resolved_ids
|
||||
@@ -147,7 +128,7 @@ module Stylesheet
|
||||
if field.theme_id == theme.id
|
||||
contents << field.value
|
||||
else
|
||||
contents << field.compiled_css
|
||||
contents << field.compiled_css(prepended_scss)
|
||||
end
|
||||
contents << "\n\n"
|
||||
end
|
||||
@@ -155,13 +136,36 @@ module Stylesheet
|
||||
contents
|
||||
end
|
||||
|
||||
def self.import_wcag_overrides(color_scheme_id)
|
||||
if color_scheme_id && ColorScheme.find_by_id(color_scheme_id)&.is_wcag?
|
||||
def import_wcag_overrides
|
||||
if @color_scheme_id && ColorScheme.find_by_id(@color_scheme_id)&.is_wcag?
|
||||
return "@import \"wcag\";"
|
||||
end
|
||||
""
|
||||
end
|
||||
|
||||
def color_variables
|
||||
contents = +""
|
||||
if @color_scheme_id
|
||||
colors = begin
|
||||
ColorScheme.find(@color_scheme_id).resolved_colors
|
||||
rescue
|
||||
ColorScheme.base_colors
|
||||
end
|
||||
else
|
||||
colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
|
||||
end
|
||||
|
||||
colors.each do |n, hex|
|
||||
contents << "$#{n}: ##{hex} !default; "
|
||||
end
|
||||
|
||||
contents
|
||||
end
|
||||
|
||||
def prepended_scss
|
||||
"#{color_variables} @import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; "
|
||||
end
|
||||
|
||||
def initialize(options)
|
||||
@theme = options[:theme]
|
||||
@theme_id = options[:theme_id]
|
||||
@@ -204,7 +208,7 @@ module Stylesheet
|
||||
if field.theme_id == theme.id
|
||||
contents << value
|
||||
else
|
||||
contents << field.compiled_css
|
||||
contents << field.compiled_css(prepended_scss)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user