2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
require_dependency 'stylesheet/common'
|
2017-05-08 23:38:48 +08:00
|
|
|
require_dependency 'global_path'
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
class Importer < SassC::Importer
|
2017-05-08 23:38:48 +08:00
|
|
|
include GlobalPath
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-10-02 08:54:31 +08:00
|
|
|
THEME_TARGETS ||= %w{embedded_theme mobile_theme desktop_theme}
|
2019-09-17 00:06:34 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def self.special_imports
|
2018-01-20 05:24:19 +08:00
|
|
|
@special_imports ||= {}
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_import(name, &blk)
|
2018-01-20 05:24:19 +08:00
|
|
|
special_imports[name] = blk
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
# Contained in function so that it can be called repeatedly from test mode
|
|
|
|
def self.register_imports!
|
|
|
|
@special_imports = {}
|
|
|
|
|
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
plugin_directory_name = plugin.directory_name
|
2019-08-21 00:39:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
["", "mobile", "desktop"].each do |type|
|
|
|
|
asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name
|
|
|
|
stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets
|
2019-08-21 00:39:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
if stylesheets[plugin_directory_name].present?
|
|
|
|
register_import asset_name do
|
|
|
|
import_files(stylesheets[plugin_directory_name])
|
|
|
|
end
|
2019-08-21 00:39:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
register_import "font" do
|
2020-10-06 01:40:41 +08:00
|
|
|
body_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.base_font }
|
|
|
|
heading_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.heading_font }
|
|
|
|
contents = +""
|
2020-09-04 21:25:50 +08:00
|
|
|
|
2020-10-06 01:40:41 +08:00
|
|
|
if body_font.present?
|
|
|
|
contents << <<~EOF
|
|
|
|
#{font_css(body_font)}
|
2020-09-23 17:00:07 +08:00
|
|
|
|
|
|
|
:root {
|
2020-10-06 01:40:41 +08:00
|
|
|
--font-family: #{body_font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
if heading_font.present?
|
|
|
|
contents << <<~EOF
|
|
|
|
#{font_css(heading_font)}
|
|
|
|
|
|
|
|
:root {
|
|
|
|
--heading-font-family: #{heading_font[:stack]};
|
2020-09-23 17:00:07 +08:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
2020-09-04 21:25:50 +08:00
|
|
|
|
|
|
|
Import.new("font.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "wizard_fonts" do
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
DiscourseFonts.fonts.each do |font|
|
2020-09-25 21:39:14 +08:00
|
|
|
if font[:key] == "system"
|
|
|
|
# Overwrite font definition because the preview canvases in the wizard require explicit @font-face definitions.
|
|
|
|
# uses same technique as https://github.com/jonathantneal/system-font-css
|
|
|
|
font[:variants] = [
|
|
|
|
{ src: 'local(".SFNS-Regular"), local(".SFNSText-Regular"), local(".HelveticaNeueDeskInterface-Regular"), local(".LucidaGrandeUI"), local("Segoe UI"), local("Ubuntu"), local("Roboto-Regular"), local("DroidSans"), local("Tahoma")', weight: 400 },
|
|
|
|
{ src: 'local(".SFNS-Bold"), local(".SFNSText-Bold"), local(".HelveticaNeueDeskInterface-Bold"), local(".LucidaGrandeUI"), local("Segoe UI Bold"), local("Ubuntu Bold"), local("Roboto-Bold"), local("DroidSans-Bold"), local("Tahoma Bold")', weight: 700 }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
contents << font_css(font)
|
|
|
|
contents << <<~EOF
|
2020-10-06 01:40:41 +08:00
|
|
|
.body-font-#{font[:key].tr("_", "-")} {
|
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
.heading-font-#{font[:key].tr("_", "-")} h2 {
|
2020-09-04 21:25:50 +08:00
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
Import.new("wizard_fonts.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "category_backgrounds" do
|
|
|
|
contents = +""
|
|
|
|
Category.where('uploaded_background_id IS NOT NULL').each do |c|
|
|
|
|
contents << category_css(c) if c.uploaded_background&.url.present?
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
Import.new("category_background.scss", source: contents)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_imports!
|
|
|
|
|
2021-02-20 00:22:24 +08:00
|
|
|
def import_color_definitions
|
2020-08-06 21:46:17 +08:00
|
|
|
contents = +""
|
|
|
|
DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path|
|
|
|
|
contents << "// Color definitions from #{name}\n\n"
|
|
|
|
contents << File.read(path.to_s)
|
|
|
|
contents << "\n\n"
|
|
|
|
end
|
2020-08-19 01:02:13 +08:00
|
|
|
|
2021-02-20 00:22:24 +08:00
|
|
|
theme_id = @theme_id || SiteSetting.default_theme_id
|
2020-09-09 23:43:34 +08:00
|
|
|
resolved_ids = Theme.transform_ids([theme_id])
|
|
|
|
|
|
|
|
if resolved_ids
|
2021-02-03 02:09:41 +08:00
|
|
|
theme = Theme.find_by_id(theme_id)
|
|
|
|
contents << theme&.scss_variables.to_s
|
2021-02-04 21:51:18 +08:00
|
|
|
Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |field|
|
|
|
|
contents << "// Color definitions from #{field.theme.name}\n\n"
|
|
|
|
|
|
|
|
if field.theme_id == theme.id
|
|
|
|
contents << field.value
|
|
|
|
else
|
2021-02-20 00:22:24 +08:00
|
|
|
contents << field.compiled_css(prepended_scss)
|
2021-02-04 21:51:18 +08:00
|
|
|
end
|
|
|
|
contents << "\n\n"
|
2020-08-19 01:02:13 +08:00
|
|
|
end
|
|
|
|
end
|
2020-08-06 21:46:17 +08:00
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2021-02-20 00:22:24 +08:00
|
|
|
def import_wcag_overrides
|
|
|
|
if @color_scheme_id && ColorScheme.find_by_id(@color_scheme_id)&.is_wcag?
|
2020-10-16 02:05:48 +08:00
|
|
|
return "@import \"wcag\";"
|
|
|
|
end
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2021-02-20 00:22:24 +08:00
|
|
|
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
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def initialize(options)
|
2017-05-10 05:20:28 +08:00
|
|
|
@theme = options[:theme]
|
2017-04-12 22:52:52 +08:00
|
|
|
@theme_id = options[:theme_id]
|
2020-08-04 10:57:10 +08:00
|
|
|
@color_scheme_id = options[:color_scheme_id]
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
if @theme && !@theme_id
|
|
|
|
# make up an id so other stuff does not bail out
|
|
|
|
@theme_id = @theme.id || -1
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_files(files)
|
|
|
|
files.map do |file|
|
|
|
|
# we never want inline css imports, they are a mess
|
|
|
|
# this tricks libsass so it imports inline instead
|
|
|
|
if file =~ /\.css$/
|
|
|
|
file = file[0..-5]
|
|
|
|
end
|
|
|
|
Import.new(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-03 02:09:41 +08:00
|
|
|
def theme_import(target)
|
|
|
|
attr = target == :embedded_theme ? :embedded_scss : :scss
|
|
|
|
target = target.to_s.gsub("_theme", "").to_sym
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-02-03 02:09:41 +08:00
|
|
|
contents = +""
|
|
|
|
|
|
|
|
fields = theme.list_baked_fields(target, attr)
|
2017-04-12 22:52:52 +08:00
|
|
|
fields.map do |field|
|
|
|
|
value = field.value
|
|
|
|
if value.present?
|
2021-02-26 21:20:39 +08:00
|
|
|
filename = "theme_#{field.theme.id}/#{field.target_name}-#{field.name}-#{field.theme.name.parameterize}.scss"
|
2021-02-03 02:09:41 +08:00
|
|
|
contents << <<~COMMENT
|
2019-04-12 18:36:08 +08:00
|
|
|
// Theme: #{field.theme.name}
|
|
|
|
// Target: #{field.target_name} #{field.name}
|
|
|
|
// Last Edited: #{field.updated_at}
|
|
|
|
COMMENT
|
2021-02-04 00:02:53 +08:00
|
|
|
|
2021-02-26 21:20:39 +08:00
|
|
|
if field.theme_id == theme.id
|
|
|
|
contents << value
|
|
|
|
else
|
|
|
|
contents << field.compiled_css(prepended_scss)
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2021-02-04 00:02:53 +08:00
|
|
|
|
2021-02-03 02:09:41 +08:00
|
|
|
end
|
|
|
|
contents
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def theme
|
2017-05-03 04:01:01 +08:00
|
|
|
unless @theme
|
|
|
|
@theme = (@theme_id && Theme.find(@theme_id)) || :nil
|
|
|
|
end
|
|
|
|
@theme == :nil ? nil : @theme
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_css(category)
|
2020-09-24 00:22:07 +08:00
|
|
|
full_slug = category.full_slug.split("-")[0..-2].join("-")
|
2020-11-26 02:40:16 +08:00
|
|
|
"body.category-#{full_slug} { background-image: url(#{upload_cdn_path(category.uploaded_background.url)}) }\n"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
def font_css(font)
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
if font[:variants].present?
|
|
|
|
font[:variants].each do |variant|
|
2020-09-25 21:39:14 +08:00
|
|
|
src = variant[:src] ? variant[:src] : "asset-url(\"/fonts/#{variant[:filename]}?v=#{DiscourseFonts::VERSION}\") format(\"#{variant[:format]}\")"
|
2020-09-04 21:25:50 +08:00
|
|
|
contents << <<~EOF
|
|
|
|
@font-face {
|
|
|
|
font-family: #{font[:name]};
|
2020-09-25 21:39:14 +08:00
|
|
|
src: #{src};
|
2020-09-04 21:25:50 +08:00
|
|
|
font-weight: #{variant[:weight]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def imports(asset, parent_path)
|
2020-12-31 04:29:10 +08:00
|
|
|
if callback = Importer.special_imports[asset]
|
2017-04-12 22:52:52 +08:00
|
|
|
instance_eval(&callback)
|
|
|
|
else
|
2021-02-03 02:09:41 +08:00
|
|
|
Import.new(asset + ".scss")
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|