Skip to content

Commit

Permalink
Moved 'isServer()' checks to static editor class functions (proper ha…
Browse files Browse the repository at this point in the history
…ndling of window check). Renamed additional "experience editor" > "Sitecore editor".
  • Loading branch information
ambrauer committed Jun 3, 2021
1 parent 2f5c84b commit 63a0249
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
26 changes: 16 additions & 10 deletions packages/sitecore-jss/src/utils/editing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import isServer from './is-server';
*/
export class ExperienceEditor {
static isActive(): boolean {
if (isServer()) {
return false;
}
// eslint-disable-next-line
const sc = window && (window as any).Sitecore;
const sc = (window as any).Sitecore;
return Boolean(sc && sc.PageModes && sc.PageModes.ChromeManager);
}
static resetChromes(): void {
if (isServer()) {
return;
}
// eslint-disable-next-line
window && (window as any).Sitecore.PageModes.ChromeManager.resetChromes();
(window as any).Sitecore.PageModes.ChromeManager.resetChromes();
}
}

Expand All @@ -20,12 +26,18 @@ export class ExperienceEditor {
*/
export class HorizonEditor {
static isActive(): boolean {
if (isServer()) {
return false;
}
// Horizon will add "sc_horizon=editor" query string parameter for the editor and "sc_horizon=simulator" for the preview
return window && window.location.search.indexOf('sc_horizon=editor') > -1;
return window.location.search.indexOf('sc_horizon=editor') > -1;
}
static resetChromes(): void {
if (isServer()) {
return;
}
// No way to reset chromes in Horizon, simply reload the canvas (iframe) instead
window && window.location.reload();
window.location.reload();
}
}

Expand All @@ -34,19 +46,13 @@ export class HorizonEditor {
* @returns true if executing within a Sitecore editor
*/
export const isEditorActive = (): boolean => {
if (isServer()) {
return false;
}
return ExperienceEditor.isActive() || HorizonEditor.isActive();
};

/**
* Resets Sitecore editor "chromes"
*/
export const resetEditorChromes = (): void => {
if (isServer()) {
return;
}
if (ExperienceEditor.isActive()) {
ExperienceEditor.resetChromes();
} else if (HorizonEditor.isActive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class JssRouteResolver implements Resolve<JssState> {
) { }

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<JssState> {
// in experience editor, we need to reload to avoid confusing the editor ribbon
// in Sitecore editor, we need to reload to avoid confusing the editor
if (isEditorActive() && window) {
const currentLocation = window.location.pathname + window.location.search + window.location.hash;
if (currentLocation !== state.url) {
Expand Down
2 changes: 1 addition & 1 deletion samples/react/src/RouteHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class RouteHandler extends React.Component {
return;
}

// if in experience editor - force reload instead of route data update
// if in Sitecore editor - force reload instead of route data update
// avoids confusing Sitecore's editing JS
if (isEditorActive()) {
window.location.assign(newRoute);
Expand Down
2 changes: 1 addition & 1 deletion samples/vue/src/RouteHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default {
if (newRoute.hash !== '' && newRoute.path === oldRoute.path) {
return;
}
// if in experience editor - force reload instead of route data update
// if in Sitecore editor - force reload instead of route data update
// avoids confusing Sitecore's editing JS
if (isEditorActive()) {
window.location.assign(newRoute.path);
Expand Down

0 comments on commit 63a0249

Please sign in to comment.