FEATURE: maintain preview theme, while previewing

This means you can browse around in preview mode without losing the theme.
At any point you can refresh page and maintain the preview theme.
This commit is contained in:
Sam 2018-03-15 16:17:09 +11:00
parent 2097f5330c
commit ba15273d3f
3 changed files with 24 additions and 3 deletions

View File

@ -7,6 +7,28 @@ export default {
initialize(container) { initialize(container) {
const messageBus = container.lookup('message-bus:main'); const messageBus = container.lookup('message-bus:main');
if (window.history && window.location.search.indexOf("?preview_theme_key=") === 0) {
// force preview theme key to always be carried along
const themeKey = window.location.search.slice(19).split('&')[0];
if (themeKey.match(/^[a-z0-9-]+$/i)) {
const patchState = function(f) {
const patched = window.history[f];
window.history[f] = function(stateObj, name, url) {
if (url.indexOf("preview_theme_key=") === -1) {
const joiner = url.indexOf("?") === -1 ? "?" : "&";
url = `${url}${joiner}preview_theme_key=${themeKey}`;
}
return patched.call(window.history, stateObj, name, url);
};
};
patchState("replaceState");
patchState("pushState");
}
}
// Custom header changes // Custom header changes
$('header.custom').each(function() { $('header.custom').each(function() {
const header = $(this); const header = $(this);

View File

@ -7,8 +7,7 @@ class Admin::ThemesController < Admin::AdminController
def preview def preview
@theme = Theme.find(params[:id]) @theme = Theme.find(params[:id])
redirect_to path("/?preview_theme_key=#{@theme.key}")
redirect_to path("/"), flash: { preview_theme_key: @theme.key }
end end
def upload_asset def upload_asset

View File

@ -315,7 +315,7 @@ class ApplicationController < ActionController::Base
resolve_safe_mode resolve_safe_mode
return if request.env[NO_CUSTOM] return if request.env[NO_CUSTOM]
theme_key = flash[:preview_theme_key] theme_key = request[:preview_theme_key]
user_option = current_user&.user_option user_option = current_user&.user_option