-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(styles): rename shadycss path to styles, add Polymer style modul…
…e support Closes #70
- Loading branch information
Showing
21 changed files
with
406 additions
and
14 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
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,10 @@ | ||
export * from './src/modules/import-style-module'; | ||
export * from './src/modules/include-styles.module'; | ||
export * from './src/modules/include-styles'; | ||
export * from './src/modules/inject-styles'; | ||
export * from './src/modules/style-to-emulated-encapsulation'; | ||
export * from './src/modules/type-selectors'; | ||
export * from './src/shadycss/models'; | ||
export * from './src/shadycss/process-stylesheets'; | ||
export * from './src/shadycss/shadycss.module'; | ||
export * from './src/shadycss/shared-styles-host'; |
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,24 @@ | ||
import { DomModule } from '@polymer/polymer/lib/elements/dom-module'; | ||
|
||
const CACHED_STYLE_MODULES = new Map<string, string | undefined>(); | ||
|
||
export function importStyleModule(styleModule: string): string | undefined { | ||
if (!CACHED_STYLE_MODULES.has(styleModule)) { | ||
const styleTemplate = <HTMLTemplateElement | undefined>( | ||
DomModule.import(styleModule, 'template') | ||
); | ||
if (styleTemplate) { | ||
const styles = styleTemplate.content.querySelectorAll('style'); | ||
CACHED_STYLE_MODULES.set( | ||
styleModule, | ||
Array.from(styles) | ||
.map(style => style.innerText) | ||
.join('\n') | ||
); | ||
} else { | ||
CACHED_STYLE_MODULES.set(styleModule, undefined); | ||
} | ||
} | ||
|
||
return CACHED_STYLE_MODULES.get(styleModule); | ||
} |
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,20 @@ | ||
import { | ||
APP_INITIALIZER, | ||
ModuleWithProviders, | ||
NgModule, | ||
NgModuleRef, | ||
Provider | ||
} from '@angular/core'; | ||
import { injectIncludeStyles } from './inject-styles'; | ||
|
||
export const INJECT_STYLES_PROVIDER: Provider = { | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useFactory: injectIncludeStyles, | ||
deps: [NgModuleRef] | ||
}; | ||
|
||
@NgModule({ | ||
providers: [INJECT_STYLES_PROVIDER] | ||
}) | ||
export class IncludeStylesModule {} |
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,33 @@ | ||
import { Type } from '@angular/core'; | ||
|
||
const TYPE_STYLE_MODULES = new Map<Type<any>, string[]>(); | ||
|
||
function IncludeStylesDecorator(...styleModules: string[]): ClassDecorator { | ||
return (target: any) => { | ||
TYPE_STYLE_MODULES.set(target, styleModules); | ||
return target; | ||
}; | ||
} | ||
|
||
function getRegisteredTypes(): Array<Type<any>> { | ||
return Array.from(TYPE_STYLE_MODULES.keys()); | ||
} | ||
|
||
function getStyleModulesFor(type: Type<any>): string[] { | ||
return TYPE_STYLE_MODULES.get(type) || []; | ||
} | ||
|
||
export type IncludeStyles = (( | ||
styleModule: string, | ||
...styleModules: string[] | ||
) => ClassDecorator) & | ||
IncludeStylesStatic; | ||
|
||
export interface IncludeStylesStatic { | ||
getRegisteredTypes(): Array<Type<any>>; | ||
getStyleModulesFor(type?: Type<any>): string[]; | ||
} | ||
|
||
export const IncludeStyles = <IncludeStyles>IncludeStylesDecorator; | ||
IncludeStyles.getRegisteredTypes = getRegisteredTypes; | ||
IncludeStyles.getStyleModulesFor = getStyleModulesFor; |
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,66 @@ | ||
import { | ||
ComponentFactoryResolver, | ||
NgModuleRef, | ||
RendererFactory2, | ||
Type, | ||
ViewEncapsulation | ||
} from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { whenSet } from '@codebakery/origami/util'; | ||
import { importStyleModule } from './import-style-module'; | ||
import { IncludeStyles } from './include-styles'; | ||
import { styleToEmulatedEncapsulation } from './style-to-emulated-encapsulation'; | ||
import { getTypeFor, scanComponentFactoryResolver } from './type-selectors'; | ||
|
||
export function injectIncludeStyles(ngModule: NgModuleRef<any>): () => void { | ||
return () => { | ||
patchRendererFactory(ngModule.injector.get(RendererFactory2)); | ||
scanComponentFactoryResolver( | ||
ngModule.injector.get(ComponentFactoryResolver) | ||
); | ||
const router = <Router>ngModule.injector.get(Router); | ||
router.events.subscribe(e => { | ||
if ('route' in e && !(<any>e.route)._loadedConfig) { | ||
whenSet(<any>e.route, '_loadedConfig', undefined, config => { | ||
scanComponentFactoryResolver( | ||
config.module.injector.get(ComponentFactoryResolver) | ||
); | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
const INJECTED_SELECTORS: string[] = []; | ||
|
||
export function patchRendererFactory(factory: RendererFactory2) { | ||
const $createRenderer = factory.createRenderer; | ||
factory.createRenderer = function(element, type) { | ||
const selector = element && element.localName; | ||
if (selector && type && INJECTED_SELECTORS.indexOf(selector) === -1) { | ||
const styleModules = IncludeStyles.getStyleModulesFor( | ||
getTypeFor(selector) | ||
); | ||
let styles = styleModules.map( | ||
styleModule => importStyleModule(styleModule) || '' | ||
); | ||
switch (type.encapsulation) { | ||
case ViewEncapsulation.Emulated: | ||
default: | ||
styles = styles.map(style => | ||
styleToEmulatedEncapsulation(style, type.id) | ||
); | ||
break; | ||
case ViewEncapsulation.Native: | ||
case ViewEncapsulation.None: | ||
case ViewEncapsulation.ShadowDom: | ||
break; | ||
} | ||
|
||
type.styles.push(...styles); | ||
INJECTED_SELECTORS.push(selector); | ||
} | ||
|
||
return $createRenderer.apply(this, arguments); | ||
}; | ||
} |
Oops, something went wrong.