-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): Allow overriding theme from query params (#7591)
Allow overriding theme through query params.. to be able to override it from preview iframe in webcomponent Github issue / Community forum post (link here to close automatically):
- Loading branch information
Showing
5 changed files
with
89 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '*') | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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'; | ||
} |