mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: In development, refresh client when theme changes are made (#22978)
This brings the theme development experience (via the discourse_theme cli) closer to the experience of making javascript changes in Discourse core/plugins via Ember CLI. Whenever a change is made to a non-css theme field, all clients will be instructed to immediately refresh via message-bus.
This commit is contained in:
parent
28dc222e2f
commit
e76e0ad592
@ -7,7 +7,7 @@ import { bind } from "discourse-common/utils/decorators";
|
|||||||
export default {
|
export default {
|
||||||
initialize(owner) {
|
initialize(owner) {
|
||||||
this.messageBus = owner.lookup("service:message-bus");
|
this.messageBus = owner.lookup("service:message-bus");
|
||||||
const session = owner.lookup("service:session");
|
this.session = owner.lookup("service:session");
|
||||||
|
|
||||||
// Preserve preview_theme_id=## and pp=async-flamegraph parameters across pages
|
// Preserve preview_theme_id=## and pp=async-flamegraph parameters across pages
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
@ -40,7 +40,7 @@ export default {
|
|||||||
this.messageBus.subscribe(
|
this.messageBus.subscribe(
|
||||||
"/file-change",
|
"/file-change",
|
||||||
this.onFileChange,
|
this.onFileChange,
|
||||||
session.mbLastFileChangeId
|
this.session.mbLastFileChangeId
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -54,6 +54,14 @@ export default {
|
|||||||
if (me === "refresh") {
|
if (me === "refresh") {
|
||||||
// Refresh if necessary
|
// Refresh if necessary
|
||||||
document.location.reload(true);
|
document.location.reload(true);
|
||||||
|
} else if (me === "development-mode-theme-changed") {
|
||||||
|
if (window.location.pathname.startsWith("/admin/customize/themes")) {
|
||||||
|
// don't refresh users on routes which make theme changes - would be very inconvenient.
|
||||||
|
// Instead, refresh on their next route navigation.
|
||||||
|
this.session.requiresRefresh = true;
|
||||||
|
} else {
|
||||||
|
document.location.reload(true);
|
||||||
|
}
|
||||||
} else if (me.new_href && me.target) {
|
} else if (me.new_href && me.target) {
|
||||||
let query = `link[data-target='${me.target}']`;
|
let query = `link[data-target='${me.target}']`;
|
||||||
|
|
||||||
|
@ -98,6 +98,9 @@ class Theme < ActiveRecord::Base
|
|||||||
changed_colors.clear
|
changed_colors.clear
|
||||||
changed_schemes.clear
|
changed_schemes.clear
|
||||||
|
|
||||||
|
any_non_css_fields_changed =
|
||||||
|
changed_fields.any? { |f| !(f.basic_scss_field? || f.extra_scss_field?) }
|
||||||
|
|
||||||
changed_fields.each(&:save!)
|
changed_fields.each(&:save!)
|
||||||
changed_fields.clear
|
changed_fields.clear
|
||||||
|
|
||||||
@ -126,6 +129,14 @@ class Theme < ActiveRecord::Base
|
|||||||
self.theme_setting_requests_refresh = false
|
self.theme_setting_requests_refresh = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if any_non_css_fields_changed && should_refresh_development_clients?
|
||||||
|
MessageBus.publish "/file-change", ["development-mode-theme-changed"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def should_refresh_development_clients?
|
||||||
|
Rails.env.development?
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_child_components
|
def update_child_components
|
||||||
@ -513,18 +524,20 @@ class Theme < ActiveRecord::Base
|
|||||||
changed_fields << field
|
changed_fields << field
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
field
|
|
||||||
else
|
else
|
||||||
if value.present? || upload_id.present?
|
if value.present? || upload_id.present?
|
||||||
theme_fields.build(
|
field =
|
||||||
target_id: target_id,
|
theme_fields.build(
|
||||||
value: value,
|
target_id: target_id,
|
||||||
name: name,
|
value: value,
|
||||||
type_id: type_id,
|
name: name,
|
||||||
upload_id: upload_id,
|
type_id: type_id,
|
||||||
)
|
upload_id: upload_id,
|
||||||
|
)
|
||||||
|
changed_fields << field
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
field
|
||||||
end
|
end
|
||||||
|
|
||||||
def child_theme_ids=(theme_ids)
|
def child_theme_ids=(theme_ids)
|
||||||
|
@ -1093,4 +1093,30 @@ HTML
|
|||||||
expect(messages.count).to eq(0)
|
expect(messages.count).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "development experience" do
|
||||||
|
it "sends 'development-mode-theme-changed event when non-css fields are updated" do
|
||||||
|
Theme.any_instance.stubs(:should_refresh_development_clients?).returns(true)
|
||||||
|
|
||||||
|
theme.set_field(target: :common, name: :scss, value: "body {background: green;}")
|
||||||
|
|
||||||
|
messages =
|
||||||
|
MessageBus
|
||||||
|
.track_publish { theme.save! }
|
||||||
|
.filter { |m| m.channel == "/file-change" }
|
||||||
|
.map(&:data)
|
||||||
|
|
||||||
|
expect(messages).not_to include("development-mode-theme-changed")
|
||||||
|
|
||||||
|
theme.set_field(target: :common, name: :header, value: "<p>Hello world</p>")
|
||||||
|
|
||||||
|
messages =
|
||||||
|
MessageBus
|
||||||
|
.track_publish { theme.save! }
|
||||||
|
.filter { |m| m.channel == "/file-change" }
|
||||||
|
.map(&:data)
|
||||||
|
|
||||||
|
expect(messages).to include(["development-mode-theme-changed"])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user