correct watcher so it handles color scheme changes correctly

This commit is contained in:
Sam 2017-04-18 16:48:15 -04:00
parent bbeb69ddc9
commit 0a67d859d5
6 changed files with 40 additions and 34 deletions

View File

@ -1,4 +1,5 @@
import DiscourseURL from 'discourse/lib/url'; import DiscourseURL from 'discourse/lib/url';
import { currentThemeKey } from 'discourse/lib/theme-selector';
export function refreshCSS(node, hash, newHref, options) { export function refreshCSS(node, hash, newHref, options) {
@ -87,8 +88,16 @@ export default {
// Refresh if necessary // Refresh if necessary
document.location.reload(true); document.location.reload(true);
} else { } else {
let themeKey = currentThemeKey();
$('link').each(function() { $('link').each(function() {
if (this.href.match(me.name) && (me.hash || me.new_href)) { if (me.hasOwnProperty('theme_key') && me.new_href) {
let target = $(this).data('target');
if (me.theme_key === themeKey && target === me.target) {
refreshCSS(this, null, me.new_href);
}
}
else if (this.href.match(me.name) && (me.hash || me.new_href)) {
refreshCSS(this, me.hash, me.new_href); refreshCSS(this, me.hash, me.new_href);
} }
}); });

View File

@ -7,6 +7,9 @@ export function currentThemeKey() {
let elem = _.first($(keySelector)); let elem = _.first($(keySelector));
if (elem) { if (elem) {
themeKey = elem.content; themeKey = elem.content;
if (_.isEmpty(themeKey)) {
themeKey = null;
}
} }
return themeKey; return themeKey;
} }

View File

@ -63,12 +63,18 @@
} }
.nav.target { .nav.target {
li {
position: relative;
}
margin-top: 15px; margin-top: 15px;
.fa { .fa {
margin-left: 3px; margin-left: 3px;
} }
.fa-mobile { .fa-mobile {
font-size: 1.3em; position: absolute;
right: -4px;
top: 2px;
font-size: 1.5em;
} }
} }

View File

@ -116,7 +116,7 @@ class Theme < ActiveRecord::Base
def notify_scheme_change(clear_manager_cache=true) def notify_scheme_change(clear_manager_cache=true)
Stylesheet::Manager.cache.clear if clear_manager_cache Stylesheet::Manager.cache.clear if clear_manager_cache
message = refresh_message_for_targets(["desktop", "mobile", "admin"], self.color_scheme_id, self, Rails.env.development?) message = refresh_message_for_targets(["desktop", "mobile", "admin"], self)
MessageBus.publish('/file-change', message) MessageBus.publish('/file-change', message)
end end
@ -126,23 +126,19 @@ class Theme < ActiveRecord::Base
themes = [self] + dependant_themes themes = [self] + dependant_themes
message = themes.map do |theme| message = themes.map do |theme|
refresh_message_for_targets([:mobile_theme,:desktop_theme], theme.id, theme) refresh_message_for_targets([:mobile_theme,:desktop_theme], theme)
end.compact.flatten end.compact.flatten
MessageBus.publish('/file-change', message) MessageBus.publish('/file-change', message)
end end
def refresh_message_for_targets(targets, id, theme, add_cache_breaker=false) def refresh_message_for_targets(targets, theme)
targets.map do |target| targets.map do |target|
link = Stylesheet::Manager.stylesheet_link_tag(target.to_sym, 'all', theme.key) href = Stylesheet::Manager.stylesheet_href(target.to_sym, theme.key)
if link if href
href = link.split(/["']/)[1]
if add_cache_breaker
href << (href.include?("?") ? "&" : "?")
href << SecureRandom.hex
end
{ {
name: "/stylesheets/#{target}#{id ? "_#{id}": ""}", target: target,
new_href: href new_href: href,
theme_key: theme.key
} }
end end
end end

View File

@ -19,6 +19,13 @@ class Stylesheet::Manager
cache.hash.keys.select{|k| k =~ /theme/}.each{|k|cache.delete(k)} cache.hash.keys.select{|k| k =~ /theme/}.each{|k|cache.delete(k)}
end end
def self.stylesheet_href(target = :desktop, theme_key = :missing)
href = stylesheet_link_tag(target, 'all', theme_key)
if href
href.split(/["']/)[1]
end
end
def self.stylesheet_link_tag(target = :desktop, media = 'all', theme_key = :missing) def self.stylesheet_link_tag(target = :desktop, media = 'all', theme_key = :missing)
target = target.to_sym target = target.to_sym
@ -35,8 +42,7 @@ class Stylesheet::Manager
@lock.synchronize do @lock.synchronize do
builder = self.new(target, theme_key) builder = self.new(target, theme_key)
builder.compile unless File.exists?(builder.stylesheet_fullpath) builder.compile unless File.exists?(builder.stylesheet_fullpath)
builder.ensure_digestless_file tag = %[<link href="#{builder.stylesheet_path}" media="#{media}" rel="stylesheet" data-target="#{target}"/>]
tag = %[<link href="#{builder.stylesheet_path}" media="#{media}" rel="stylesheet" data-target="#{target}" rel="preload"/>]
cache[cache_key] = tag cache[cache_key] = tag
tag.dup.html_safe tag.dup.html_safe
@ -142,13 +148,6 @@ class Stylesheet::Manager
css css
end end
def ensure_digestless_file
# file without digest is only for auto-reloading css in dev env
unless Rails.env.production? || (File.exist?(stylesheet_fullpath_no_digest) && File.mtime(stylesheet_fullpath) == File.mtime(stylesheet_fullpath_no_digest))
FileUtils.cp(stylesheet_fullpath, stylesheet_fullpath_no_digest)
end
end
def self.cache_fullpath def self.cache_fullpath
"#{Rails.root}/#{CACHE_PATH}" "#{Rails.root}/#{CACHE_PATH}"
end end
@ -178,15 +177,7 @@ class Stylesheet::Manager
end end
def stylesheet_path def stylesheet_path
if Rails.env.development? stylesheet_cdnpath
if @target.to_s =~ /theme/
stylesheet_relpath
else
stylesheet_relpath_no_digest
end
else
stylesheet_cdnpath
end
end end
def root_path def root_path

View File

@ -52,11 +52,12 @@ module Stylesheet
@queue.pop @queue.pop
end end
Stylesheet::Manager.cache.clear
message = ["desktop", "mobile", "admin"].map do |name| message = ["desktop", "mobile", "admin"].map do |name|
{hash: SecureRandom.hex, name: "/stylesheets/#{name}.css"} {target: name, new_href: Stylesheet::Manager.stylesheet_href(name.to_sym) , theme_key: SiteSetting.default_theme_key}
end end
Stylesheet::Manager.cache.clear
MessageBus.publish '/file-change', message MessageBus.publish '/file-change', message
end end