2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
require "global_path"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
module Stylesheet
|
2021-03-13 00:17:42 +08:00
|
|
|
class 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
|
|
|
|
2021-03-13 00:17:42 +08:00
|
|
|
def self.plugin_assets
|
|
|
|
@plugin_assets ||= {}
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
def self.register_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 =
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2019-09-17 00:06:34 +08:00
|
|
|
if type.present?
|
|
|
|
DiscoursePluginRegistry.send("#{type}_stylesheets")
|
2023-01-09 20:10:19 +08:00
|
|
|
else
|
2019-09-17 00:06:34 +08:00
|
|
|
DiscoursePluginRegistry.stylesheets
|
2021-03-13 00:17:42 +08:00
|
|
|
end
|
2023-01-09 20:10:19 +08:00
|
|
|
)
|
|
|
|
|
2021-03-13 00:17:42 +08:00
|
|
|
plugin_assets[asset_name] = stylesheets[
|
|
|
|
plugin_directory_name
|
|
|
|
] if plugin_directory_name.present?
|
2019-08-21 00:39:52 +08:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 00:05:56 +08:00
|
|
|
end
|
2020-10-06 01:40:41 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
register_imports!
|
2020-10-06 01:40:41 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
def font
|
|
|
|
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
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents << <<~CSS if body_font.present?
|
|
|
|
#{font_css(body_font)}
|
2020-09-04 21:25:50 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
:root {
|
|
|
|
--font-family: #{body_font[:stack]};
|
|
|
|
}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
CSS
|
2020-09-25 21:39:14 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents << <<~CSS if heading_font.present?
|
|
|
|
#{font_css(heading_font)}
|
2020-09-04 21:25:50 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
:root {
|
|
|
|
--heading-font-family: #{heading_font[:stack]};
|
|
|
|
}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
CSS
|
2020-09-04 21:25:50 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
|
|
|
def wizard_fonts
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
DiscourseFonts.fonts.each do |font|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
]
|
2019-09-17 00:06:34 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents << font_css(font)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
contents << <<~CSS
|
2021-03-11 00:05:56 +08:00
|
|
|
.body-font-#{font[:key].tr("_", "-")} {
|
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
.heading-font-#{font[:key].tr("_", "-")} h2 {
|
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
CSS
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
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|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents << "\n\n// Color definitions from #{name}\n\n"
|
2020-08-06 21:46:17 +08:00
|
|
|
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
|
2021-06-15 14:57:17 +08:00
|
|
|
resolved_ids = Theme.transform_ids(theme_id)
|
2020-09-09 23:43:34 +08:00
|
|
|
|
|
|
|
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|
|
2021-03-11 00:05:56 +08:00
|
|
|
contents << "\n\n// Color definitions from #{field.theme.name}\n\n"
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2021-02-04 21:51:18 +08:00
|
|
|
if field.theme_id == theme.id
|
|
|
|
contents << field.value
|
|
|
|
else
|
2021-02-20 00:22:24 +08:00
|
|
|
contents << field.compiled_css(prepended_scss)
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-02-04 21:51:18 +08:00
|
|
|
contents << "\n\n"
|
|
|
|
end
|
2020-08-19 01:02:13 +08:00
|
|
|
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 StandardError
|
|
|
|
ColorScheme.base_colors
|
|
|
|
end
|
2021-03-24 00:46:25 +08:00
|
|
|
elsif (@theme_id && !theme.component)
|
|
|
|
colors = theme&.color_scheme&.resolved_colors || ColorScheme.base_colors
|
2021-02-20 00:22:24 +08:00
|
|
|
else
|
2021-03-24 00:46:25 +08:00
|
|
|
# this is a slightly ugly backwards compatibility fix,
|
|
|
|
# we shouldn't be using the default theme color scheme for components
|
|
|
|
# (most components use CSS custom properties which work fine without this)
|
2021-03-18 01:34:15 +08:00
|
|
|
colors =
|
|
|
|
Theme.find_by_id(SiteSetting.default_theme_id)&.color_scheme&.resolved_colors ||
|
|
|
|
ColorScheme.base_colors
|
2021-02-20 00:22:24 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
colors.each { |n, hex| contents << "$#{n}: ##{hex} !default; " }
|
|
|
|
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2021-04-12 11:57:39 +08:00
|
|
|
def public_image_path
|
|
|
|
image_path = UrlHelper.absolute("#{Discourse.base_path}/images")
|
|
|
|
"$public_image_path: \"#{image_path}\"; "
|
|
|
|
end
|
|
|
|
|
2021-02-20 00:22:24 +08:00
|
|
|
def prepended_scss
|
2021-04-12 11:57:39 +08:00
|
|
|
"#{color_variables} #{public_image_path} @import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; "
|
2021-02-20 00:22:24 +08:00
|
|
|
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
|
|
|
|
|
2021-02-03 02:09:41 +08:00
|
|
|
def theme_import(target)
|
2021-06-18 10:16:26 +08:00
|
|
|
return "" if !@theme_id
|
|
|
|
|
2021-02-03 02:09:41 +08:00
|
|
|
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?
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
contents << <<~SCSS
|
2019-04-12 18:36:08 +08:00
|
|
|
// Theme: #{field.theme.name}
|
|
|
|
// Target: #{field.target_name} #{field.name}
|
|
|
|
// Last Edited: #{field.updated_at}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
SCSS
|
2021-02-04 00:02:53 +08:00
|
|
|
|
2021-02-27 01:30:23 +08:00
|
|
|
contents << value
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
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
|
|
|
@theme = (@theme_id && Theme.find(@theme_id)) || :nil unless @theme
|
|
|
|
@theme == :nil ? nil : @theme
|
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?
|
2021-06-21 21:33:12 +08:00
|
|
|
fonts_dir = UrlHelper.absolute("#{Discourse.base_path}/fonts")
|
2020-09-04 21:25:50 +08:00
|
|
|
font[:variants].each do |variant|
|
2021-06-21 21:33:12 +08:00
|
|
|
src =
|
2023-01-09 20:10:19 +08:00
|
|
|
(
|
2021-06-21 21:33:12 +08:00
|
|
|
if variant[:src]
|
|
|
|
variant[:src]
|
2023-01-09 20:10:19 +08:00
|
|
|
else
|
2021-06-21 21:33:12 +08:00
|
|
|
"url(\"#{fonts_dir}/#{variant[:filename]}?v=#{DiscourseFonts::VERSION}\") format(\"#{variant[:format]}\")"
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
|
|
|
)
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
contents << <<~CSS
|
2020-09-04 21:25:50 +08:00
|
|
|
@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]};
|
|
|
|
}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-03-01 03:50:55 +08:00
|
|
|
CSS
|
2020-09-04 21:25:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contents
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|