Skip to content

Commit

Permalink
refactor(scheme): style tag refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
yinonov committed Sep 24, 2020
1 parent 74dec22 commit b5c14cb
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions common/scheme/src/vvd-scheme-style-tag-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ type ModuleType =
| typeof import('./scheme.dark.css')
| typeof import('./scheme.light.css');

export async function applySchemeCSS(scheme: PredefinedScheme): Promise<void> {
const cssResult: CSSResult = (await getSchemeModule(scheme)).style;
style.innerHTML = cssResult.cssText || '';
}
const importSchemeMap = new Map<PredefinedScheme, Promise<ModuleType>>([
['dark', import('./scheme.dark.css')],
['light', import('./scheme.light.css')],
]);

// private area
//
const style = mountStyle();

function mountStyle() {
Expand All @@ -22,14 +20,27 @@ function mountStyle() {
return style;
}

async function getSchemeModule(
schemeOption: PredefinedScheme
): Promise<ModuleType> {
switch (schemeOption) {
case 'dark':
return import('./scheme.dark.css');
case 'light':
default:
return import('./scheme.light.css');
export async function applySchemeCSS(scheme: PredefinedScheme): Promise<void> {
console.log(scheme);
if (!importSchemeMap.has(scheme)) {
throw new Error('scheme not found');
}
const schemeModule = await importSchemeMap.get(scheme);
console.log('yuri', schemeModule);
const cssResult: CSSResult | undefined = schemeModule?.style;
if (cssResult) {
style.innerHTML = cssResult.cssText || '';
}
}

// async function getSchemeModule(
// schemeOption: PredefinedScheme
// ): Promise<ModuleType> {
// switch (schemeOption) {
// case 'dark':
// return import('./scheme.dark.css');
// case 'light':
// default:
// return import('./scheme.light.css');
// }
// }

0 comments on commit b5c14cb

Please sign in to comment.