Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Allow overriding theme from query params #7591

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cypress/e2e/31-demo.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import workflow from '../fixtures/Manual_wait_set.json';
import { importWorkflow, vistDemoPage } from '../pages/demo';
import { WorkflowPage } from '../pages/workflow';

const workflowPage = new WorkflowPage();

describe('Demo', () => {
it('can import template', () => {
vistDemoPage();
importWorkflow(workflow);
workflowPage.getters.canvasNodes().should('have.length', 3);
});

it('can override theme to dark', () => {
vistDemoPage('dark');
cy.get('body').should('have.attr', 'data-theme', 'dark');
});

it('can override theme to light', () => {
vistDemoPage('light');
cy.get('body').should('have.attr', 'data-theme', 'light');
});
});
21 changes: 21 additions & 0 deletions cypress/pages/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Actions
*/

export function vistDemoPage(theme?: 'dark' | 'light') {
const query = theme ? `?theme=${theme}` : '';
cy.visit('/workflows/demo' + query);
cy.waitForLoad();
cy.window().then((win) => {
// @ts-ignore
win.preventNodeViewBeforeUnload = true;
});
}

export function importWorkflow(workflow: object) {
const OPEN_WORKFLOW = {command: 'openWorkflow', workflow};
cy.window().then($window => {
const message = JSON.stringify(OPEN_WORKFLOW);
$window.postMessage(message, '*')
});
}
2 changes: 0 additions & 2 deletions cypress/pages/templates.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { BasePage } from './base';
import { WorkflowPage } from './workflow';

const workflowPage = new WorkflowPage();
export class TemplatesPage extends BasePage {
url = '/templates';

Expand Down
33 changes: 9 additions & 24 deletions packages/editor-ui/src/stores/ui.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
DEBUG_PAYWALL_MODAL_KEY,
N8N_PRICING_PAGE_URL,
WORKFLOW_HISTORY_VERSION_RESTORE,
LOCAL_STORAGE_THEME,
} from '@/constants';
import type {
CloudUpdateLinkSourceType,
Expand All @@ -64,37 +63,23 @@ import { useCloudPlanStore } from '@/stores/cloudPlan.store';
import { useTelemetryStore } from '@/stores/telemetry.store';
import { dismissBannerPermanently } from '@/api/ui';
import type { BannerName } from 'n8n-workflow';
import {
addThemeToBody,
getPreferredTheme,
getThemeOverride,
isValidTheme,
updateTheme,
} from './ui.utils';

let savedTheme: ThemeOption = 'system';
try {
const value = localStorage.getItem(LOCAL_STORAGE_THEME) as AppliedThemeOption;
if (['light', 'dark'].includes(value)) {
const value = getThemeOverride();
if (isValidTheme(value)) {
savedTheme = value;
addThemeToBody(value);
}
} catch (e) {}

function addThemeToBody(theme: AppliedThemeOption) {
window.document.body.setAttribute('data-theme', theme);
}

function updateTheme(theme: ThemeOption) {
if (theme === 'system') {
window.document.body.removeAttribute('data-theme');
localStorage.removeItem(LOCAL_STORAGE_THEME);
} else {
addThemeToBody(theme);
localStorage.setItem(LOCAL_STORAGE_THEME, theme);
}
}

function getPreferredTheme(): AppliedThemeOption {
const isDarkMode =
!!window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)')?.matches;

return isDarkMode ? 'dark' : 'light';
}

export const useUIStore = defineStore(STORES.UI, {
state: (): UIState => ({
activeActions: [],
Expand Down
36 changes: 36 additions & 0 deletions packages/editor-ui/src/stores/ui.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { AppliedThemeOption, ThemeOption } from '@/Interface';
import { LOCAL_STORAGE_THEME } from '@/constants';

export function addThemeToBody(theme: AppliedThemeOption) {
window.document.body.setAttribute('data-theme', theme);
}

export function isValidTheme(theme: string | null): theme is AppliedThemeOption {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we move this to typeGuards.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay here.. we need to start splitting our types / guards more..

return !!theme && ['light', 'dark'].includes(theme);
}

// query param allows overriding theme for demo view in preview iframe without flickering
export function getThemeOverride() {
return getQueryParam('theme') || localStorage.getItem(LOCAL_STORAGE_THEME);
}

function getQueryParam(paramName: string): string | null {
return new URLSearchParams(window.location.search).get(paramName);
}

export function updateTheme(theme: ThemeOption) {
if (theme === 'system') {
window.document.body.removeAttribute('data-theme');
localStorage.removeItem(LOCAL_STORAGE_THEME);
} else {
addThemeToBody(theme);
localStorage.setItem(LOCAL_STORAGE_THEME, theme);
}
}

export function getPreferredTheme(): AppliedThemeOption {
const isDarkMode =
!!window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)')?.matches;

return isDarkMode ? 'dark' : 'light';
}
Loading