This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset): Implement Preset decorator
And make preset type as abstract class so we can validate all presets
- Loading branch information
Showing
21 changed files
with
277 additions
and
128 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
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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { PresetService } from '../../preset'; | ||
import { Preset } from '../../preset'; | ||
import { MyPreset } from './my-preset'; | ||
|
||
@Component({ | ||
selector: 'prst-my', | ||
templateUrl: './my.component.html', | ||
styleUrls: ['./my.component.css'] | ||
styleUrls: ['./my.component.css'], | ||
}) | ||
export class MyComponent { | ||
|
||
presetComp = this.presetService.getPreset<MyPreset>(); | ||
|
||
constructor( | ||
private presetService: PresetService, | ||
) { } | ||
|
||
@Preset() presetComp: MyPreset; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { PresetType } from './preset-token'; | ||
export * from './preset-method'; | ||
export * from './provide'; | ||
export * from './metadata'; | ||
export * from './preset.service'; | ||
export * from './preset.module'; | ||
export * from './preset-default.module'; |
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 @@ | ||
export * from './preset'; |
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,34 @@ | ||
export const PRESET_METADATA_KEY = '__Preset-Key__'; | ||
|
||
export interface PresetMetadata { | ||
propName: string | symbol; | ||
options?: PresetMetadataOptions; | ||
} | ||
|
||
// tslint:disable-next-line:no-empty-interface | ||
export interface PresetMetadataOptions {} | ||
|
||
export function setPresetMetadataOn( | ||
type: any, | ||
metadata: PresetMetadata | PresetMetadata[], | ||
) { | ||
if (!Array.isArray(metadata)) { | ||
metadata = [metadata]; | ||
} | ||
|
||
const ctor = type.constructor; | ||
const existingMetadata = ctor[PRESET_METADATA_KEY] || []; | ||
ctor[PRESET_METADATA_KEY] = [...existingMetadata, ...metadata]; | ||
} | ||
|
||
export function getPresetMetadataFrom(type: any): PresetMetadata[] { | ||
return type[PRESET_METADATA_KEY] || []; | ||
} | ||
|
||
export function Preset(options?: PresetMetadataOptions): PropertyDecorator { | ||
return (target, propName) => | ||
setPresetMetadataOn(target, { | ||
propName, | ||
options, | ||
}); | ||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import { InjectionToken, Type } from '@angular/core'; | ||
|
||
// tslint:disable-next-line:no-empty-interface | ||
export interface PresetType<T = any> extends Type<T> { } | ||
export abstract class PresetType {} | ||
|
||
export const PRESET_TYPES_TOKEN = new InjectionToken<PresetType[]>('PresetTypes'); | ||
export const PRESET_TYPES_TOKEN = new InjectionToken<PresetType[]>( | ||
'PresetTypes', | ||
); | ||
export const PRESET_COMPS_TOKEN = new InjectionToken<Type<any>[][]>( | ||
'PresetComps', | ||
); |
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
Oops, something went wrong.