-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
index.ts
107 lines (97 loc) · 4.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Constructable, CustomElement } from 'aurelia';
/* eslint-disable prefer-destructuring */
import { RenderStoryFunction, start } from '@storybook/core/client';
import { ClientStoryApi, Loadable } from '@storybook/addons';
import { text, boolean, number, date } from '@storybook/addon-knobs';
import './globals';
import render from './render';
import { IStorybookSection, StoryFnAureliaReturnType } from './types';
import { addRegistries, addContainer, Component, addComponents } from './decorators';
const framework = 'Aurelia';
interface ClientApi extends ClientStoryApi<Partial<StoryFnAureliaReturnType>> {
setAddon(addon: any): void;
configure(loader: Loadable, module: NodeModule): void;
getStorybook(): IStorybookSection[];
clearDecorators(): void;
forceReRender(): void;
raw: () => any; // todo add type
load: (...args: any[]) => void;
}
const api = start((render as any) as RenderStoryFunction);
export const storiesOf: ClientApi['storiesOf'] = (kind, m) => {
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({
framework,
});
};
export { addRegistries, addContainer, addComponents };
export type { StoryFnAureliaReturnType, Component };
export const configure: ClientApi['configure'] = (...args) => api.configure(framework, ...args);
export const addDecorator: ClientApi['addDecorator'] = api.clientApi
.addDecorator as ClientApi['addDecorator'];
export const addParameters: ClientApi['addParameters'] = api.clientApi
.addParameters as ClientApi['addParameters'];
export const clearDecorators: ClientApi['clearDecorators'] = api.clientApi.clearDecorators;
export const setAddon: ClientApi['setAddon'] = api.clientApi.setAddon;
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender;
export const getStorybook: ClientApi['getStorybook'] = api.clientApi.getStorybook;
export const raw: ClientApi['raw'] = api.clientApi.raw;
export function generateKnobsFor(CustomElementClass: Constructable) {
const def = CustomElement.getDefinition(CustomElementClass);
const bindables = def && def.bindables;
if (!bindables) return class {};
const result = class {} as any;
const elementConstructed = new CustomElementClass() as any;
Object.keys(bindables)
.map((y) => bindables[y])
.forEach((bindableDef) => {
const bindable = bindableDef.property;
const currentVal = elementConstructed[bindable];
switch (typeof currentVal) {
case 'boolean':
result[bindable] = boolean(bindable, elementConstructed[bindable]);
return;
case 'string':
result[bindable] = text(bindable, elementConstructed[bindable] || 'lorem ipsum');
return;
case 'number':
case 'bigint':
result[bindable] = number(bindable, elementConstructed[bindable] || 0);
return;
case 'undefined':
if (bindable.toLocaleLowerCase().includes('is')) {
result[bindable] = boolean(bindable, elementConstructed[bindable]);
return;
}
if (
bindable.toLocaleLowerCase().includes('count') ||
bindable.toLocaleLowerCase().includes('max') ||
bindable.toLocaleLowerCase().includes('min')
) {
result[bindable] = number(bindable, elementConstructed[bindable] || 0);
return;
}
if (
bindable.toLocaleLowerCase().includes('date') ||
bindable.toLocaleLowerCase().includes('time')
) {
result[bindable] = date(bindable, elementConstructed[bindable] || new Date());
return;
}
result[bindable] = text(bindable, elementConstructed[bindable] || 'lorem ipsum');
return;
case 'object':
if (currentVal instanceof Date) {
result[bindable] = date(bindable, elementConstructed[bindable] || new Date());
return;
}
if (currentVal instanceof Date) {
result[bindable] = date(bindable, elementConstructed[bindable] || new Date());
return;
}
return;
default:
result[bindable] = text(bindable, elementConstructed[bindable] || 'lorem ipsum');
}
});
return result;
}