-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (74 loc) · 2.07 KB
/
index.js
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
let examples = [];
let currentIndex = 0;
const happoStatic = {
init() {
window.happo = window.happo || {};
window.happo.init = ({ targetName, chunk, only }) => {
currentIndex = 0;
if (only) {
examples = examples.filter(
e => e.component === only.component && e.variant === only.variant,
);
} else if (chunk) {
const examplesPerChunk = Math.ceil(examples.length / chunk.total);
const startIndex = chunk.index * examplesPerChunk;
const endIndex = startIndex + examplesPerChunk;
examples = examples.slice(startIndex, endIndex);
}
examples = examples.filter(e => {
if (!e.targets || !Array.isArray(e.targets)) {
// This story hasn't been filtered for specific targets
return true;
}
return e.targets.includes(targetName);
});
};
window.happo.nextExample = async () => {
const e = examples[currentIndex];
if (!e) {
// we're done
return;
}
await e.render();
currentIndex++;
const clone = { ...e };
delete clone.render;
return clone;
};
},
registerExample(props) {
if (!props.component) {
throw new Error('Missing `component` property');
}
if (!props.variant) {
throw new Error('Missing `variant` property');
}
if (!props.render) {
throw new Error('Missing `render` property');
}
const compType = typeof props.component;
if (compType !== 'string') {
throw new Error(
`Property \`component\` must be a string. Got "${compType}".`,
);
}
const varType = typeof props.variant;
if (varType !== 'string') {
throw new Error(
`Property \`variant\` must be a string. Got "${varType}".`,
);
}
const rendType = typeof props.render;
if (rendType !== 'function') {
throw new Error(
`Property \`render\` must be a function. Got "${rendType}".`,
);
}
examples.push(props);
},
reset() {
examples = [];
currentIndex = 0;
}
};
module.exports = happoStatic;