Skip to content

A platform for community discussion. Free, open, simple.

DEV: Let plugins supply SVG icons without loading them all into memory (#45467)

David Taylor9d2d84a42ca2
diff --git a/app/controllers/svg_sprite_controller.rb b/app/controllers/svg_sprite_controller.rb
index 7381451113e..10fec54eee4 100644
--- a/app/controllers/svg_sprite_controller.rb
+++ b/app/controllers/svg_sprite_controller.rb
@@ -57,8 +57,11 @@ class SvgSpriteController < ApplicationController
filter = permitted[:filter].to_s
page = fetch_page_from_params(max: MAX_PAGE)
- etag = [Discourse.git_version, SvgSprite.version(@theme_id), filter, only_available, page]
- return if !stale?(etag:)
+ # A symbol source's icons can change without changing the sprite version
+ if only_available || DiscoursePluginRegistry.svg_symbol_sources.empty?
+ etag = [Discourse.git_version, SvgSprite.version(@theme_id), filter, only_available, page]
+ return if !stale?(etag:)
+ end
render json:
SvgSprite.icon_picker_search(
diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb
index 3b3658236ac..aa7a3db0ee5 100644
--- a/lib/discourse_plugin_registry.rb
+++ b/lib/discourse_plugin_registry.rb
@@ -157,6 +157,8 @@ class DiscoursePluginRegistry
define_filtered_register :svg_icon_sources
+ define_filtered_register :svg_symbol_sources
+
define_filtered_register :reviewable_types do |singleton|
singleton.define_singleton_method("reviewable_types_lookup") do
public_send(:_raw_reviewable_types)
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 6a15c435bce..ddd6dd889af 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -832,6 +832,26 @@ class Plugin::Instance
DiscoursePluginRegistry.register_svg_icon_source(block, self)
end
+ # Registers a source of extra SVG symbols, such as icons stored in the
+ # database. Unlike the sprites in a plugin's `svg-icons` directory, a symbol
+ # is only added to the SVG sprite when one of the sprite's icons uses it.
+ #
+ # The source must respond to:
+ #
+ # - `symbols(icon_ids)`, returning a hash of the requested names it has to
+ # their `<symbol>` markup.
+ # - `search_icon_ids(keyword, limit:)`, returning up to `limit` of its icon
+ # names that contain `keyword`, ignoring case, sorted in byte order. The
+ # keyword is blank when the icon picker lists every icon.
+ #
+ # Both are called while the sprite is built or searched, so they must not run
+ # at boot, and their results are scoped to the current site.
+ #
+ # Call `SvgSprite.expire_cache` when the underlying data changes.
+ def register_svg_symbol_source(source)
+ DiscoursePluginRegistry.register_svg_symbol_source(source, self)
+ end
+
def extend_content_security_policy(extension)
csp_extensions << extension
end
diff --git a/lib/svg_sprite.rb b/lib/svg_sprite.rb
index d82c9802ea8..ca6812489f2 100644
--- a/lib/svg_sprite.rb
+++ b/lib/svg_sprite.rb
@@ -357,6 +357,19 @@ module SvgSprite
end
end
+ def self.site_symbols(icon_ids)
+ symbols = core_svgs.slice(*icon_ids)
+
+ DiscoursePluginRegistry.svg_symbol_sources.each do |source|
+ missing = icon_ids - symbols.keys
+ break if missing.empty?
+
+ symbols.merge!(source.symbols(missing).slice(*missing))
+ end
+
+ symbols
+ end
+
# Just used in tests
def self.clear_plugin_svg_sprite_cache!
@plugin_svgs = nil
@@ -459,12 +472,6 @@ module SvgSprite
cache&.clear
end
- def self.svgs_for(theme_id)
- svgs = core_svgs
- svgs = svgs.merge(custom_svgs(theme_id)) if theme_id.present?
- svgs
- end
-
def self.bundle(theme_id = nil)
icons = all_icons(theme_id)
@@ -478,46 +485,71 @@ License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL
" \
"".dup
- svg_subset << core_svgs.slice(*icons).values.join
+ symbols = site_symbols(icons)
+ svg_subset << icons.filter_map { |icon| symbols[icon] }.join
svg_subset << custom_svgs(theme_id).values.join
svg_subset << "</svg>"
end
def self.search(searched_icon)
- svgs_for(SiteSetting.default_theme_id)[searched_icon.strip] || false
+ icon = searched_icon.strip
+ theme_id = SiteSetting.default_theme_id
+ custom = theme_id.present? ? custom_svgs(theme_id) : {}
+
+ custom[icon] || site_symbols([icon])[icon] || false
end
def self.icon_picker_search(keyword, only_available, page:, per_page:, theme_id: nil)
- ids = picker_icon_ids(theme_id, only_available)
+ keyword = keyword.to_s.downcase
+ limit = (page + 1) * per_page + 1
+ in_sprite = picker_sprite_icon_ids(theme_id)
- if keyword.present?
- downcased_keyword = keyword.downcase
- ids = ids.lazy.select { |id| id.downcase.include?(downcased_keyword) }
+ ids = in_sprite.lazy.select { |id| id.downcase.include?(keyword) }.first(limit)
+ if !only_available && ids.size < limit
+ ids.concat(picker_other_icon_ids(theme_id, keyword, limit - ids.size))
end
- ids = ids.drop(page * per_page).first(per_page + 1)
-
+ ids = ids.drop(page * per_page)
has_more = ids.size > per_page
ids = ids.take(per_page)
- missing = only_available ? [] : (ids - picker_icon_ids(theme_id, true)).to_set
+ missing = only_available ? [] : (ids - in_sprite).to_set
return { icons: ids.map { |id| { id: } }, has_more: } if missing.empty?
custom = theme_id.present? ? custom_svgs(theme_id) : {}
+ site = site_symbols(missing.to_a)
icons =
- ids.map { |id| missing.include?(id) ? { id:, symbol: custom[id] || core_svgs[id] } : { id: } }
+ ids.map { |id| missing.include?(id) ? { id:, symbol: custom[id] || site[id] } : { id: } }
{ icons:, has_more: }
end
- def self.picker_icon_ids(theme_id, only_available)
- get_set_cache("picker_icon_ids_#{Theme.transform_ids(theme_id).join(",")}_#{only_available}") do
- symbols = svgs_for(theme_id)
- in_sprite = all_icons(theme_id).select { |id| symbols.key?(id) }
+ def self.picker_sprite_icon_ids(theme_id)
+ get_set_cache("picker_sprite_icon_ids_#{Theme.transform_ids(theme_id).join(",")}") do
+ icons = all_icons(theme_id)
+ available = site_symbols(icons).keys.to_set
+ available.merge(custom_svgs(theme_id).keys) if theme_id.present?
+ icons.select { |id| available.include?(id) }
+ end
+ end
+
+ def self.picker_other_icon_ids(theme_id, keyword, limit)
+ in_sprite = picker_sprite_icon_ids(theme_id)
+
+ local =
+ get_set_cache("picker_other_icon_ids_#{Theme.transform_ids(theme_id).join(",")}") do
+ ids = core_svgs.keys
+ ids |= custom_svgs(theme_id).keys if theme_id.present?
+ # Legacy names still render where already used, but aren't offered for new picks
+ (ids - in_sprite - legacy_icon_ids).sort
+ end
+ ids = local.lazy.select { |id| id.downcase.include?(keyword) }.first(limit)
- # Legacy names still render where already used, but aren't offered for new picks
- only_available ? in_sprite : in_sprite + (symbols.keys - in_sprite - legacy_icon_ids).sort
+ DiscoursePluginRegistry.svg_symbol_sources.each do |source|
+ ids.concat(source.search_icon_ids(keyword, limit: limit + in_sprite.size) - in_sprite)
end
+
+ ids.uniq.sort.first(limit)
end
# For use in no_ember .html.erb layouts
diff --git a/spec/lib/svg_sprite/svg_sprite_spec.rb b/spec/lib/svg_sprite/svg_sprite_spec.rb
index 2cee868c06d..7615b190e1a 100644
--- a/spec/lib/svg_sprite/svg_sprite_spec.rb
+++ b/spec/lib/svg_sprite/svg_sprite_spec.rb
@@ -104,17 +104,20 @@ RSpec.describe SvgSprite do
end
it "picks up a new badge icon in the picker list" do
- expect(SvgSprite.picker_icon_ids(nil, true)).not_to include("seedling")
+ expect(SvgSprite.picker_sprite_icon_ids(nil)).not_to include("seedling")
Fabricate(:badge, name: "Seedling Badge", icon: "seedling")
- expect(SvgSprite.picker_icon_ids(nil, true)).to include("seedling")
+ expect(SvgSprite.picker_sprite_icon_ids(nil)).to include("seedling")
end
it "renders legacy icon names without offering them in the full picker list" do
+ ids =
+ SvgSprite.icon_picker_search("user", false, page: 0, per_page: 1000)[:icons].map { _1[:id] }
+
expect(SvgSprite.search("user-large")).to match(/user-large/)
- expect(SvgSprite.picker_icon_ids(nil, false)).to include("user")
- expect(SvgSprite.picker_icon_ids(nil, false)).not_to include("user-large")
+ expect(ids).to include("user")
+ expect(ids).not_to include("user-large")
end
it "includes icons from badges" do
@@ -360,6 +363,99 @@ RSpec.describe SvgSprite do
end
end
+ describe "symbol sources registered by plugins" do
+ let(:plugin) do
+ Class
+ .new(Plugin::Instance) do
+ attr_accessor :enabled
+ attr_accessor :name
+
+ def enabled?
+ @enabled
+ end
+ end
+ .new
+ end
+
+ let(:pro_symbols) do
+ {
+ "fal-blender" => '<symbol id="fal-blender" viewBox="0 0 512 512"><path d="M1"/></symbol>',
+ "fal-guitar" => '<symbol id="fal-guitar" viewBox="0 0 512 512"><path d="M2"/></symbol>',
+ }
+ end
+
+ after do
+ DiscoursePluginRegistry._raw_svg_symbol_sources.clear
+ DiscoursePluginRegistry._raw_svg_icon_sources.clear
+ end
+
+ def symbol_source(symbols)
+ Struct
+ .new(:all) do
+ def symbols(icon_ids) = all.slice(*icon_ids)
+
+ def search_icon_ids(keyword, limit:)
+ all.keys.select { |id| id.include?(keyword) }.sort.first(limit)
+ end
+ end
+ .new(symbols)
+ end
+
+ it "adds the symbols of icons in use to the sprite" do
+ plugin.enabled = true
+ DiscoursePluginRegistry.register_svg_symbol_source(symbol_source(pro_symbols), plugin)
+ DiscoursePluginRegistry.register_svg_icon_source(-> { ["fal-blender"] }, plugin)
+
+ expect(SvgSprite.bundle).to include(pro_symbols["fal-blender"])
+ expect(SvgSprite.bundle).not_to include(pro_symbols["fal-guitar"])
+ expect(SvgSprite.search("fal-guitar")).to eq(pro_symbols["fal-guitar"])
+ end
+
+ it "lists the source's icons in the icon picker" do
+ plugin.enabled = true
+ DiscoursePluginRegistry.register_svg_symbol_source(symbol_source(pro_symbols), plugin)
+
+ result = SvgSprite.icon_picker_search("fal-", false, page: 0, per_page: 10)
+
+ expect(result[:icons]).to contain_exactly(
+ { id: "fal-blender", symbol: pro_symbols["fal-blender"] },
+ { id: "fal-guitar", symbol: pro_symbols["fal-guitar"] },
+ )
+ end
+
+ it "pages through the sprite's icons before the source's other icons" do
+ plugin.enabled = true
+ DiscoursePluginRegistry.register_svg_symbol_source(symbol_source(pro_symbols), plugin)
+ DiscoursePluginRegistry.register_svg_icon_source(-> { ["fal-guitar"] }, plugin)
+
+ first = SvgSprite.icon_picker_search("fal-", false, page: 0, per_page: 1)
+ second = SvgSprite.icon_picker_search("fal-", false, page: 1, per_page: 1)
+
+ expect(first).to eq(icons: [{ id: "fal-guitar" }], has_more: true)
+ expect(second).to eq(
+ icons: [{ id: "fal-blender", symbol: pro_symbols["fal-blender"] }],
+ has_more: false,
+ )
+ end
+
+ it "ignores sources belonging to disabled plugins" do
+ plugin.enabled = false
+ DiscoursePluginRegistry.register_svg_symbol_source(symbol_source(pro_symbols), plugin)
+
+ expect(SvgSprite.search("fal-guitar")).to eq(false)
+ end
+
+ it "keeps core symbols when a source provides the same icon" do
+ plugin.enabled = true
+ DiscoursePluginRegistry.register_svg_symbol_source(
+ symbol_source("user" => '<symbol id="user"><path d="M3"/></symbol>'),
+ plugin,
+ )
+
+ expect(SvgSprite.search("user")).to eq(SvgSprite.core_svgs["user"])
+ end
+ end
+
it "includes Font Awesome icon from groups" do
_group = Fabricate(:group, flair_icon: "far-building")
expect(SvgSprite.bundle).to match(/far-building/)
diff --git a/spec/requests/svg_sprite_controller_spec.rb b/spec/requests/svg_sprite_controller_spec.rb
index d0f624d91d6..056d79e8237 100644
--- a/spec/requests/svg_sprite_controller_spec.rb
+++ b/spec/requests/svg_sprite_controller_spec.rb
@@ -166,6 +166,42 @@ RSpec.describe SvgSpriteController do
expect(ids).to eq(ids.sort_by { |id| [available.include?(id) ? 0 : 1, id] })
end
+ it "answers a full search afresh after a plugin's symbol source changes" do
+ symbols = { "fal-blender" => '<symbol id="fal-blender"><path d="M1"/></symbol>' }
+ source =
+ Struct
+ .new(:all) do
+ def symbols(icon_ids) = all.slice(*icon_ids)
+
+ def search_icon_ids(keyword, limit:)
+ all.keys.select { |id| id.include?(keyword) }.sort.first(limit)
+ end
+ end
+ .new(symbols)
+ plugin = Plugin::Instance.new
+ plugin.stubs(:enabled?).returns(true)
+ DiscoursePluginRegistry.register_svg_symbol_source(source, plugin)
+ sign_in(user)
+
+ get "/svg-sprite/picker-search", params: { filter: "fal-", only_available: "false" }
+ etag = response.headers["ETag"]
+
+ symbols["fal-guitar"] = '<symbol id="fal-guitar"><path d="M2"/></symbol>'
+ SvgSprite.expire_cache
+
+ get "/svg-sprite/picker-search",
+ params: {
+ filter: "fal-",
+ only_available: "false",
+ },
+ headers: etag ? { "If-None-Match" => etag } : {}
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["icons"].map { |i| i["id"] }).to include("fal-guitar")
+ ensure
+ DiscoursePluginRegistry._raw_svg_symbol_sources.clear
+ end
+
it "keeps the restricted set when only_available is blank" do
sign_in(user)