A platform for community discussion. Free, open, simple.
UX: allow custom splash image (#37316)
Krise33d6ef4d2bc
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 6444beb27a8..230c5d9c963 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -494,6 +494,85 @@ module ApplicationHelperSiteSetting.splash_screenend+ def custom_splash_screen_enabled?+ return @custom_splash_screen_enabled if defined?(@custom_splash_screen_enabled)++ @custom_splash_screen_enabled =+ UpcomingChanges.enabled_for_user?(:enable_custom_splash_screen, current_user) &&+ SiteSetting.splash_screen_image.is_a?(Upload)+ end++ def splash_screen_image_animated?+ build_splash_screen_image unless defined?(@splash_screen_image_animated)+ @splash_screen_image_animated+ end++ def splash_screen_image_data_uri+ build_splash_screen_image unless defined?(@splash_screen_image_svg)+ return nil if @splash_screen_image_svg.blank?++ # Replace CSS variable references with actual theme colors+ svg_with_colors = @splash_screen_image_svg.dup++ primary = "##{light_color_hex_for_name("primary")}"+ secondary = "##{light_color_hex_for_name("secondary")}"+ tertiary = "##{light_color_hex_for_name("tertiary")}"++ svg_with_colors.gsub!(/var\(\s*--primary\s*\)/, primary)+ svg_with_colors.gsub!(/var\(\s*--secondary\s*\)/, secondary)+ svg_with_colors.gsub!(/var\(\s*--tertiary\s*\)/, tertiary)++ # Use base64 encoding for better compatibility with complex SVGs+ "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_with_colors)}"+ end++ private++ def build_splash_screen_image+ @splash_screen_image_svg = nil+ @splash_screen_image_animated = false++ upload = SiteSetting.splash_screen_image+ return unless upload.is_a?(Upload)++ content =+ Discourse+ .cache+ .fetch("splash_screen_image_svg_#{upload.id}_#{upload.sha1}", expires_in: 1.day) do+ upload.content+ rescue StandardError => e+ Discourse.warn_exception(e, message: "Failed to fetch splash screen logo SVG")+ nil+ end++ return if content.blank?++ doc = Nokogiri.XML(content)+ svg = doc.at_css("svg")+ return unless svg++ @splash_screen_image_animated =+ svg.at_xpath(+ ".//*[local-name()='animate' or local-name()='animateTransform' or local-name()='animateMotion' or local-name()='set']",+ ).present? || svg.at_css("style")&.text&.include?("@keyframes")++ has_scripts = svg.xpath("//script").present?+ has_event_handlers = svg.xpath("//@*[starts-with(name(), 'on')]").present?+ # Avoid animations targeting href attributes+ has_href_animations =+ svg+ .xpath(".//*[local-name()='animate' or local-name()='set']/@attributeName")+ .any? { |attr| attr.value.downcase.include?("href") }++ return if has_scripts || has_event_handlers || has_href_animations++ @splash_screen_image_svg = content.dup++ @splash_screen_image_svg.sub!(/^<\?xml[^>]*\?>\s*/, "")+ end++ public+def allow_plugins?!request.env[ApplicationController::NO_PLUGINS]enddiff --git a/app/views/common/_discourse_splash.html.erb b/app/views/common/_discourse_splash.html.erbindex 39b22651f91..63df3e32fe3 100644--- a/app/views/common/_discourse_splash.html.erb+++ b/app/views/common/_discourse_splash.html.erb@@ -1,7 +1,11 @@<section id="d-splash"><style>html {+ <%- if custom_splash_screen_enabled? %>+ overflow: hidden !important;+ <%- else %>overflow-y: hidden !important;+ <%- end %>}/* user picked a theme where the "regular" scheme is dark */@@ -68,7 +72,16 @@#d-splash .preloader-image {--splash-dot-size: max(1vw, 25px);--splash-dot-spacing: calc(var(--splash-dot-size) * 1.5);+ <%- if custom_splash_screen_enabled? %>+ width: 700px;+ display: flex;+ flex-direction: column;+ align-items: center;+ justify-content: center;+ gap: 3rem;+ <%- else %>width: calc((var(--splash-dot-size) + var(--splash-dot-spacing)) * 5);+ <%- end %>height: 100vh;background-size: cover;}@@ -82,6 +95,14 @@}}+ <%- if custom_splash_screen_enabled? %>+ #d-splash .dots-container {+ position: relative;+ width: calc((var(--splash-dot-size) + var(--splash-dot-spacing)) * 5);+ height: var(--splash-dot-size);+ }+ <%- end %>+.dots {animation-name: d-splash-loader;animation-timing-function: ease-in-out;@@ -90,7 +111,11 @@animation-delay: calc(var(--n) * 0.15s);position: absolute;+ <%- if custom_splash_screen_enabled? %>+ top: 0;+ <%- else %>top: calc(50% - var(--splash-dot-size) / 2);+ <%- end %>left: calc((50% - var(--splash-dot-size) / 2) + (var(--n) * var(--splash-dot-spacing)));transform-origin: calc((var(--splash-dot-spacing) * var(--n) * -1) + var(--splash-dot-size)/2) center;@@ -121,14 +146,54 @@transform: scale(1);}}++ <%- if custom_splash_screen_enabled? %>+ #d-splash .preloader-image {+ --splash-dot-size: max(0.7vw, 14px);+ }+ #d-splash .splash-logo-container {+ width: min(200px, 55vw);+ height: min(130px, 20vh);+ background-size: contain;+ background-repeat: no-repeat;+ background-position: center;+ background-image: url("<%= splash_screen_image_data_uri %>");+ <%- unless splash_screen_image_animated? %>+ opacity: 0.4;+ animation: d-splash-logo-pulse 3s ease-in-out infinite;+ <%- end %>+ }++ @keyframes d-splash-logo-pulse {+ 0%, 100% {+ opacity: 0.4;+ }+ 25%, 65% {+ opacity: 1;+ }+ }+ <%- end %></style><div class="preloader-image" elementtiming="discourse-splash-visible">- <div class="dots" style="--n:-2;"></div>- <div class="dots" style="--n:-1;"></div>- <div class="dots" style="--n:0;"></div>- <div class="dots" style="--n:1;"></div>- <div class="dots" style="--n:2;"></div>+ <%- if custom_splash_screen_enabled? %>+ <div class="splash-logo-container"></div>+ <%- unless splash_screen_image_animated? %>+ <div class="dots-container">+ <div class="dots" style="--n:-2;"></div>+ <div class="dots" style="--n:-1;"></div>+ <div class="dots" style="--n:0;"></div>+ <div class="dots" style="--n:1;"></div>+ <div class="dots" style="--n:2;"></div>+ </div>+ <%- end %>+ <%- else %>+ <div class="dots" style="--n:-2;"></div>+ <div class="dots" style="--n:-1;"></div>+ <div class="dots" style="--n:0;"></div>+ <div class="dots" style="--n:1;"></div>+ <div class="dots" style="--n:2;"></div>+ <%- end %></div><script nonce="<%= csp_nonce_placeholder %>">const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"><!-- LCP candidate image ${".".repeat(5000)} --></svg>`;diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex a82c733327b..09ff8efe41a 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2720,6 +2720,7 @@ en:welcome_banner_text_color: "When the 'Welcome banner image' setting has been configured, use this setting to change the banner text color to ensure it remains readable. When left blank or when no image has been uploaded, the active theme controls the color."splash_screen: "Displays a temporary loading screen while site assets load"+ splash_screen_image: "An SVG image to display on the splash screen while the site loads. Supports animated SVGs. You can use var(--primary), var(--secondary), and var(--tertiary) in your SVG to reference theme colors."navigation_menu: "Specify sidebar or header dropdown as the main navigation menu for your site. Sidebar is recommended."default_navigation_menu_categories: "Selected categories will be displayed under Navigation Menu's Categories section by default."default_navigation_menu_tags: "Selected tags will be displayed under Navigation Menu's Tags section by default."@@ -2755,6 +2756,7 @@ en:floating_dismiss_topics_on_mobile: "Shows a floating 'Dismiss...' button on mobile for the topic list for easier access and to free up space in the top of the topic list"experimental_rename_faq_to_guidelines: "This change renames the FAQ page to Guidelines. The FAQ page will still be available at /faq. The 'faq url' setting works as before."enable_simplified_category_creation: "Enables a simplified category creation flow with fewer options and a cleaner interface."+ enable_custom_splash_screen: "Allows customizing the loading splash screen with your own SVG image. Upload an image via the 'splash screen image' setting. Warning: This may negatively impact LCP scores and Google indexing."errors:invalid_css_color: "Invalid color. Enter a color name or hex value."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex b28b8dd8cf7..455d1b43c68 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -3757,6 +3757,24 @@ uncategorized:default: truearea: "interface"+ enable_custom_splash_screen:+ default: false+ client: true+ hidden: true+ area: "interface"+ upcoming_change:+ status: "experimental"+ impact: "feature,all_members"++ splash_screen_image:+ default: ""+ type: upload+ authorized_extensions: "svg"+ area: "interface"+ depends_on:+ - "enable_custom_splash_screen"+ depends_behavior: "hidden"+suggest_weekends_in_date_pickers:client: truedefault: truediff --git a/lib/upload_creator.rb b/lib/upload_creator.rbindex 4f3682b7cdb..3f4f1b97c16 100644--- a/lib/upload_creator.rb+++ b/lib/upload_creator.rb@@ -6,6 +6,9 @@ class UploadCreatorTYPES_TO_CROP = %w[avatar card_background custom_emoji profile_background].each(&:freeze)ALLOWED_SVG_ELEMENTS = %w[+ animate+ animateMotion+ animateTransformcircleclipPathdefs@@ -21,6 +24,7 @@ class UploadCreatorpolylineradialGradientrect+ setstopstylesvg@@ -512,6 +516,15 @@ class UploadCreatorenduse_el.remove_attribute("xlink:href")end++ # Remove animation elements that target href/xlink:href attributes+ doc+ .css("animate, animateTransform, animateMotion, set")+ .each do |anim_el|+ attr_name = anim_el.attr("attributeName")&.downcase+ anim_el.remove if attr_name&.include?("href")+ end+File.write(@file.path, doc.to_s)@file.rewindend