-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathpreview.js
42 lines (37 loc) · 1.18 KB
/
preview.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
/* eslint-disable no-underscore-dangle */
import addons from '@storybook/addons';
import stringify from 'json-stringify-safe';
import uuid from 'uuid/v1';
import { EVENT_ID } from './';
function _format(arg) {
if (arg && typeof arg.preventDefault !== 'undefined') {
return stringify('[SyntheticEvent]');
}
return stringify(arg);
}
export function action(name) {
// eslint-disable-next-line no-unused-vars, func-names
const handler = function(..._args) {
const args = Array.from(_args).map(_format);
const channel = addons.getChannel();
const id = uuid();
channel.emit(EVENT_ID, {
id,
data: { name, args },
});
};
const fnName = name && typeof name === 'string' ? name.replace(/\W+/g, '_') : 'action';
Object.defineProperty(handler, 'name', { value: fnName });
return handler;
}
export function decorateAction(decorators) {
// eslint-disable-next-line no-unused-vars, func-names
return function(name) {
const callAction = action(name);
// eslint-disable-next-line no-unused-vars, func-names
return function(..._args) {
const decorated = decorators.reduce((args, fn) => fn(args), _args);
callAction(...decorated);
};
};
}