2018-11-27 05:49:57 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_dependency 'distributed_cache'
|
|
|
|
|
|
|
|
module SvgSprite
|
|
|
|
SVG_ICONS ||= Set.new([
|
|
|
|
"adjust",
|
2018-12-07 20:00:02 +08:00
|
|
|
"ambulance",
|
2018-11-27 05:49:57 +08:00
|
|
|
"anchor",
|
|
|
|
"angle-double-down",
|
|
|
|
"angle-double-up",
|
2019-02-19 20:56:01 +08:00
|
|
|
"angle-double-right",
|
|
|
|
"angle-double-left",
|
2018-11-27 05:49:57 +08:00
|
|
|
"angle-down",
|
|
|
|
"angle-right",
|
|
|
|
"angle-up",
|
|
|
|
"archive",
|
|
|
|
"arrow-down",
|
|
|
|
"arrow-left",
|
2019-01-24 19:25:37 +08:00
|
|
|
"arrow-up",
|
|
|
|
"arrows-alt-h",
|
|
|
|
"arrows-alt-v",
|
2018-11-27 05:49:57 +08:00
|
|
|
"at",
|
|
|
|
"backward",
|
|
|
|
"ban",
|
|
|
|
"bars",
|
|
|
|
"bed",
|
|
|
|
"bell-slash",
|
|
|
|
"bold",
|
|
|
|
"book",
|
|
|
|
"bookmark",
|
|
|
|
"briefcase",
|
|
|
|
"calendar-alt",
|
|
|
|
"caret-down",
|
|
|
|
"caret-left",
|
|
|
|
"caret-right",
|
|
|
|
"caret-up",
|
|
|
|
"certificate",
|
|
|
|
"chart-bar",
|
|
|
|
"chart-pie",
|
|
|
|
"check",
|
|
|
|
"check-circle",
|
|
|
|
"check-square",
|
|
|
|
"chevron-down",
|
|
|
|
"chevron-right",
|
|
|
|
"chevron-up",
|
|
|
|
"circle",
|
|
|
|
"code",
|
|
|
|
"cog",
|
|
|
|
"columns",
|
|
|
|
"comment",
|
|
|
|
"compress",
|
|
|
|
"copy",
|
|
|
|
"crosshairs",
|
|
|
|
"cube",
|
|
|
|
"desktop",
|
2018-12-04 11:29:20 +08:00
|
|
|
"discourse-compress",
|
|
|
|
"discourse-expand",
|
2018-11-27 05:49:57 +08:00
|
|
|
"download",
|
|
|
|
"ellipsis-h",
|
|
|
|
"ellipsis-v",
|
|
|
|
"envelope",
|
|
|
|
"envelope-square",
|
|
|
|
"exchange-alt",
|
|
|
|
"exclamation-circle",
|
|
|
|
"exclamation-triangle",
|
|
|
|
"external-link-alt",
|
2018-12-07 23:49:18 +08:00
|
|
|
"fab-android",
|
2019-01-24 19:25:37 +08:00
|
|
|
"fab-apple",
|
2018-11-28 22:53:06 +08:00
|
|
|
"fab-discourse",
|
2018-11-27 05:49:57 +08:00
|
|
|
"fab-facebook-square",
|
2019-02-27 22:52:06 +08:00
|
|
|
"fab-facebook",
|
2018-11-27 05:49:57 +08:00
|
|
|
"fab-github",
|
|
|
|
"fab-google-plus-square",
|
|
|
|
"fab-instagram",
|
|
|
|
"fab-linux",
|
2019-01-24 19:25:37 +08:00
|
|
|
"fab-twitter",
|
2018-11-27 05:49:57 +08:00
|
|
|
"fab-twitter-square",
|
|
|
|
"fab-windows",
|
|
|
|
"fab-yahoo",
|
|
|
|
"far-bell",
|
|
|
|
"far-bell-slash",
|
|
|
|
"far-calendar-plus",
|
|
|
|
"far-chart-bar",
|
|
|
|
"far-check-square",
|
|
|
|
"far-circle",
|
|
|
|
"far-clipboard",
|
|
|
|
"far-clock",
|
|
|
|
"far-comment",
|
2019-01-11 03:26:53 +08:00
|
|
|
"far-copyright",
|
2018-11-27 05:49:57 +08:00
|
|
|
"far-dot-circle",
|
|
|
|
"far-edit",
|
|
|
|
"far-envelope",
|
|
|
|
"far-eye",
|
|
|
|
"far-eye-slash",
|
|
|
|
"far-file-alt",
|
|
|
|
"far-frown",
|
|
|
|
"far-heart",
|
|
|
|
"far-image",
|
|
|
|
"far-list-alt",
|
|
|
|
"far-moon",
|
|
|
|
"far-smile",
|
|
|
|
"far-square",
|
|
|
|
"far-sun",
|
|
|
|
"far-thumbs-down",
|
|
|
|
"far-thumbs-up",
|
|
|
|
"far-trash-alt",
|
|
|
|
"fast-backward",
|
|
|
|
"fast-forward",
|
|
|
|
"file",
|
|
|
|
"file-alt",
|
|
|
|
"filter",
|
|
|
|
"flag",
|
|
|
|
"folder",
|
|
|
|
"folder-open",
|
|
|
|
"forward",
|
|
|
|
"gavel",
|
|
|
|
"globe",
|
|
|
|
"globe-americas",
|
|
|
|
"hand-point-right",
|
2019-03-08 16:48:35 +08:00
|
|
|
"hands-helping",
|
2018-11-27 05:49:57 +08:00
|
|
|
"heading",
|
|
|
|
"heart",
|
|
|
|
"home",
|
|
|
|
"info-circle",
|
|
|
|
"italic",
|
|
|
|
"key",
|
|
|
|
"link",
|
|
|
|
"list",
|
|
|
|
"list-ol",
|
|
|
|
"list-ul",
|
|
|
|
"lock",
|
|
|
|
"magic",
|
2019-01-24 19:25:37 +08:00
|
|
|
"map-marker-alt",
|
2018-11-27 05:49:57 +08:00
|
|
|
"microphone-slash",
|
|
|
|
"minus",
|
|
|
|
"minus-circle",
|
|
|
|
"mobile-alt",
|
|
|
|
"paint-brush",
|
|
|
|
"paper-plane",
|
|
|
|
"pencil-alt",
|
2018-12-07 20:00:02 +08:00
|
|
|
"play",
|
2018-11-27 05:49:57 +08:00
|
|
|
"plug",
|
|
|
|
"plus",
|
|
|
|
"plus-circle",
|
|
|
|
"plus-square",
|
|
|
|
"power-off",
|
2019-02-21 03:58:31 +08:00
|
|
|
"puzzle-piece",
|
2018-11-27 05:49:57 +08:00
|
|
|
"question",
|
|
|
|
"question-circle",
|
|
|
|
"quote-left",
|
|
|
|
"quote-right",
|
|
|
|
"random",
|
|
|
|
"redo",
|
|
|
|
"reply",
|
|
|
|
"rocket",
|
|
|
|
"search",
|
|
|
|
"share",
|
|
|
|
"shield-alt",
|
|
|
|
"shower",
|
|
|
|
"sign-out-alt",
|
2019-01-24 19:25:37 +08:00
|
|
|
"signal",
|
2018-11-27 05:49:57 +08:00
|
|
|
"step-backward",
|
|
|
|
"step-forward",
|
|
|
|
"sync",
|
|
|
|
"table",
|
|
|
|
"tag",
|
|
|
|
"tasks",
|
|
|
|
"thermometer-three-quarters",
|
|
|
|
"thumbs-down",
|
|
|
|
"thumbs-up",
|
|
|
|
"thumbtack",
|
|
|
|
"times",
|
|
|
|
"times-circle",
|
|
|
|
"trash-alt",
|
2019-01-24 19:25:37 +08:00
|
|
|
"tv",
|
2018-11-27 05:49:57 +08:00
|
|
|
"undo",
|
|
|
|
"unlink",
|
|
|
|
"unlock",
|
|
|
|
"unlock-alt",
|
|
|
|
"upload",
|
|
|
|
"user",
|
|
|
|
"user-plus",
|
|
|
|
"user-secret",
|
|
|
|
"user-times",
|
|
|
|
"users",
|
|
|
|
"wrench"
|
|
|
|
])
|
|
|
|
|
|
|
|
FA_ICON_MAP = { 'far fa-' => 'far-', 'fab fa-' => 'fab-', 'fas fa-' => '', 'fa-' => '' }
|
|
|
|
|
2019-01-12 22:59:12 +08:00
|
|
|
SVG_SPRITE_PATHS = Dir.glob(["#{Rails.root}/vendor/assets/svg-icons/**/*.svg",
|
|
|
|
"#{Rails.root}/plugins/*/svg-icons/*.svg"])
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
def self.all_icons(theme_ids = [])
|
|
|
|
get_set_cache("icons_#{Theme.transform_ids(theme_ids).join(',')}") do
|
|
|
|
Set.new()
|
|
|
|
.merge(settings_icons)
|
|
|
|
.merge(plugin_icons)
|
|
|
|
.merge(badge_icons)
|
|
|
|
.merge(group_icons)
|
|
|
|
.merge(theme_icons(theme_ids))
|
|
|
|
.delete_if { |i| i.blank? || i.include?("/") }
|
|
|
|
.map! { |i| process(i.dup) }
|
|
|
|
.merge(SVG_ICONS)
|
|
|
|
.sort
|
|
|
|
end
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
def self.version(theme_ids = [])
|
|
|
|
get_set_cache("version_#{Theme.transform_ids(theme_ids).join(',')}") do
|
|
|
|
Digest::SHA1.hexdigest(all_icons(theme_ids).join('|'))
|
|
|
|
end
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
def self.path(theme_ids = [])
|
|
|
|
"/svg-sprite/#{Discourse.current_hostname}/svg-#{theme_ids&.join(",")}-#{version(theme_ids)}.js"
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.expire_cache
|
2019-02-06 23:51:23 +08:00
|
|
|
cache&.clear
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
def self.bundle(theme_ids = [])
|
|
|
|
icons = all_icons(theme_ids)
|
2018-11-27 05:49:57 +08:00
|
|
|
|
|
|
|
doc = File.open("#{Rails.root}/vendor/assets/svg-icons/fontawesome/solid.svg") { |f| Nokogiri::XML(f) }
|
|
|
|
|
|
|
|
svg_subset = """<!--
|
2019-03-06 22:21:31 +08:00
|
|
|
Discourse SVG subset of Font Awesome Free by @fontawesome - https://fontawesome.com
|
|
|
|
License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
|
2018-11-27 05:49:57 +08:00
|
|
|
-->
|
|
|
|
<svg xmlns='http://www.w3.org/2000/svg' style='display: none;'>
|
|
|
|
""".dup
|
|
|
|
|
2019-01-12 22:59:12 +08:00
|
|
|
SVG_SPRITE_PATHS.each do |fname|
|
2018-11-27 05:49:57 +08:00
|
|
|
svg_file = Nokogiri::XML(File.open(fname)) do |config|
|
|
|
|
config.options = Nokogiri::XML::ParseOptions::NOBLANKS
|
|
|
|
end
|
|
|
|
|
|
|
|
svg_filename = "#{File.basename(fname, ".svg")}"
|
|
|
|
|
|
|
|
svg_file.css('symbol').each do |sym|
|
|
|
|
icon_id = prepare_symbol(sym, svg_filename)
|
|
|
|
if icons.include? icon_id
|
|
|
|
sym.attributes['id'].value = icon_id
|
2019-01-24 19:25:37 +08:00
|
|
|
sym.css('title').each(&:remove)
|
2018-11-27 05:49:57 +08:00
|
|
|
svg_subset << sym.to_xml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
svg_subset << '</svg>'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.search(searched_icon)
|
|
|
|
searched_icon = process(searched_icon.dup)
|
|
|
|
|
2019-01-12 22:59:12 +08:00
|
|
|
SVG_SPRITE_PATHS.each do |fname|
|
2018-11-27 05:49:57 +08:00
|
|
|
svg_file = Nokogiri::XML(File.open(fname))
|
|
|
|
svg_filename = "#{File.basename(fname, ".svg")}"
|
|
|
|
|
|
|
|
svg_file.css('symbol').each do |sym|
|
|
|
|
icon_id = prepare_symbol(sym, svg_filename)
|
|
|
|
|
|
|
|
if searched_icon == icon_id
|
|
|
|
sym.attributes['id'].value = icon_id
|
2019-01-24 19:25:37 +08:00
|
|
|
sym.css('title').each(&:remove)
|
2018-11-27 05:49:57 +08:00
|
|
|
return sym.to_xml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-24 19:25:37 +08:00
|
|
|
false
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.prepare_symbol(symbol, svg_filename)
|
|
|
|
icon_id = symbol.attr('id')
|
|
|
|
|
|
|
|
case svg_filename
|
|
|
|
when "regular"
|
|
|
|
icon_id = icon_id.prepend('far-')
|
|
|
|
when "brands"
|
|
|
|
icon_id = icon_id.prepend('fab-')
|
|
|
|
end
|
|
|
|
|
|
|
|
icon_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.settings_icons
|
|
|
|
# includes svg_icon_subset and any settings containing _icon (incl. plugin settings)
|
|
|
|
site_setting_icons = []
|
|
|
|
|
|
|
|
SiteSetting.settings_hash.select do |key, value|
|
2018-11-30 08:11:32 +08:00
|
|
|
if key.to_s.include?("_icon") && String === value
|
2018-11-27 05:49:57 +08:00
|
|
|
site_setting_icons |= value.split('|')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
site_setting_icons
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseEvent.on(:site_setting_saved) do |site_setting|
|
|
|
|
expire_cache if site_setting.name.to_s.include?("_icon")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.plugin_icons
|
|
|
|
DiscoursePluginRegistry.svg_icons
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.badge_icons
|
2019-01-24 19:25:37 +08:00
|
|
|
Badge.pluck(:icon).uniq
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.group_icons
|
|
|
|
Group.where("flair_url LIKE '%fa-%'").pluck(:flair_url).uniq
|
|
|
|
end
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
def self.theme_icons(theme_ids)
|
2018-11-27 05:49:57 +08:00
|
|
|
theme_icon_settings = []
|
|
|
|
|
2019-02-06 23:51:23 +08:00
|
|
|
# Need to load full records for default values
|
|
|
|
Theme.where(id: Theme.transform_ids(theme_ids)).each do |theme|
|
2018-11-27 05:49:57 +08:00
|
|
|
settings = theme.cached_settings.each do |key, value|
|
2018-11-30 08:11:32 +08:00
|
|
|
if key.to_s.include?("_icon") && String === value
|
2018-11-27 05:49:57 +08:00
|
|
|
theme_icon_settings |= value.split('|')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
theme_icon_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fa4_shim_file
|
|
|
|
"#{Rails.root}/lib/svg_sprite/fa4-renames.json"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fa4_to_fa5_names
|
2019-01-24 19:25:37 +08:00
|
|
|
@db ||= File.open(fa4_shim_file, "r:UTF-8") { |f| JSON.parse(f.read) }
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.process(icon_name)
|
2019-01-24 19:25:37 +08:00
|
|
|
icon_name.strip!
|
2018-11-27 05:49:57 +08:00
|
|
|
FA_ICON_MAP.each { |k, v| icon_name.sub!(k, v) }
|
|
|
|
fa4_to_fa5_names[icon_name] || icon_name
|
|
|
|
end
|
2019-02-06 23:51:23 +08:00
|
|
|
|
|
|
|
def self.get_set_cache(key)
|
|
|
|
cache[key] ||= yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cache
|
|
|
|
@cache ||= DistributedCache.new('svg_sprite')
|
|
|
|
end
|
2018-11-27 05:49:57 +08:00
|
|
|
end
|