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 { currentThemeKey } from 'discourse/lib/theme-selector';
export function refreshCSS(node, hash, newHref, options) {
@ -87,8 +88,16 @@ export default {
// Refresh if necessary
document.location.reload(true);
} else {
let themeKey = currentThemeKey();
$('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);
}
});

View File

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

View File

@ -63,12 +63,18 @@
}
.nav.target {
li {
position: relative;
}
margin-top: 15px;
.fa {
margin-left: 3px;
}
.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)
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)
end
@ -126,23 +126,19 @@ class Theme < ActiveRecord::Base
themes = [self] + dependant_themes
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
MessageBus.publish('/file-change', message)
end
def refresh_message_for_targets(targets, id, theme, add_cache_breaker=false)
def refresh_message_for_targets(targets, theme)
targets.map do |target|
link = Stylesheet::Manager.stylesheet_link_tag(target.to_sym, 'all', theme.key)
if link
href = link.split(/["']/)[1]
if add_cache_breaker
href << (href.include?("?") ? "&" : "?")
href << SecureRandom.hex
end
href = Stylesheet::Manager.stylesheet_href(target.to_sym, theme.key)
if href
{
name: "/stylesheets/#{target}#{id ? "_#{id}": ""}",
new_href: href
target: target,
new_href: href,
theme_key: theme.key
}
end
end

View File

@ -19,6 +19,13 @@ class Stylesheet::Manager
cache.hash.keys.select{|k| k =~ /theme/}.each{|k|cache.delete(k)}
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)
target = target.to_sym
@ -35,8 +42,7 @@ class Stylesheet::Manager
@lock.synchronize do
builder = self.new(target, theme_key)
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}" rel="preload"/>]
tag = %[<link href="#{builder.stylesheet_path}" media="#{media}" rel="stylesheet" data-target="#{target}"/>]
cache[cache_key] = tag
tag.dup.html_safe
@ -142,13 +148,6 @@ class Stylesheet::Manager
css
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
"#{Rails.root}/#{CACHE_PATH}"
end
@ -178,15 +177,7 @@ class Stylesheet::Manager
end
def stylesheet_path
if Rails.env.development?
if @target.to_s =~ /theme/
stylesheet_relpath
else
stylesheet_relpath_no_digest
end
else
stylesheet_cdnpath
end
stylesheet_cdnpath
end
def root_path

View File

@ -52,11 +52,12 @@ module Stylesheet
@queue.pop
end
Stylesheet::Manager.cache.clear
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
Stylesheet::Manager.cache.clear
MessageBus.publish '/file-change', message
end