Skip to content

Commit b664ca5

Browse files
committed
Add custom config
1 parent 1614684 commit b664ca5

File tree

7 files changed

+100
-36
lines changed

7 files changed

+100
-36
lines changed

dev/config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { setConfig } from '../src/config';
2+
3+
setConfig({
4+
addId: 'dev_adk',
5+
panelTitle: 'ADK DEV'
6+
});

dev/register.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { register } from '../src/register';
3+
import './config'
34

45
const blockStyle = {
56
margin: 2,

dev/withAdk.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { createDecorator, setParameters } from '../src/decorator';
3+
import './config'
34

45
const DecoratorUI = ({ context, getStory, data, parameters }) => (
56
<div>
@@ -17,4 +18,4 @@ const DecoratorUI = ({ context, getStory, data, parameters }) => (
1718
);
1819

1920
export const withAdk = createDecorator(DecoratorUI);
20-
export const adkParams = setParameters
21+
export const adkParams = setParameters()

src/ChannelStore.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default class ChannelStore {
55
EVENT_ID_INIT,
66
EVENT_ID_DATA,
77
EVENT_ID_BACK,
8+
89
name = 'store',
910
initData = {},
1011
isPanel = false,
@@ -16,7 +17,13 @@ export default class ChannelStore {
1617
this.initData = initData;
1718
this.isPanel = isPanel;
1819

19-
// console.log(`New Store Created for ${isPanel ? 'Panel' : 'Preview'}`);
20+
21+
22+
console.log(`New Store Created for ${isPanel ? 'Panel' : 'Preview'}`,
23+
EVENT_ID_INIT,
24+
EVENT_ID_DATA,
25+
EVENT_ID_BACK,
26+
);
2027
}
2128

2229
store = this.initData;

src/config.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1-
export const ADDON_ID = 'adk';
2-
export const PANEL_ID = `${ADDON_ID}/panel`;
3-
export const PANEL_Title = `${ADDON_ID}/addon`;
4-
export const PARAM_Key = `${ADDON_ID}/parameters`;
5-
export const EVENT_ID_INIT = `${ADDON_ID}/event/init`;
6-
export const EVENT_ID_DATA = `${ADDON_ID}/event/data`;
7-
export const EVENT_ID_BACK = `${ADDON_ID}/event/back`;
1+
export let ADDON_ID = 'adk';
2+
export let PANEL_ID = `${ADDON_ID}/panel`;
3+
export let PANEL_Title = `${ADDON_ID}/addon`;
4+
export let PARAM_Key = `${ADDON_ID}/parameters`;
5+
export let EVENT_ID_INIT = `${ADDON_ID}/event/init`;
6+
export let EVENT_ID_DATA = `${ADDON_ID}/event/data`;
7+
export let EVENT_ID_BACK = `${ADDON_ID}/event/back`;
8+
9+
export const setConfig = ({
10+
addId,
11+
panelId,
12+
panelTitle,
13+
paramKey,
14+
eventInit,
15+
eventData,
16+
eventBack,
17+
}) => {
18+
ADDON_ID = addId || ADDON_ID;
19+
PANEL_ID = `${ADDON_ID}/panel`;
20+
PANEL_Title = `${ADDON_ID}/addon`;
21+
PARAM_Key = `${ADDON_ID}/parameters`;
22+
EVENT_ID_INIT = `${ADDON_ID}/event/init`;
23+
EVENT_ID_DATA = `${ADDON_ID}/event/data`;
24+
EVENT_ID_BACK = `${ADDON_ID}/event/back`;
25+
26+
PANEL_ID = panelId || PANEL_ID;
27+
PANEL_Title = panelTitle || PANEL_Title;
28+
PARAM_Key = paramKey || PARAM_Key;
29+
EVENT_ID_INIT = eventInit || EVENT_ID_INIT;
30+
EVENT_ID_DATA = eventData || EVENT_ID_DATA;
31+
EVENT_ID_BACK = eventBack || EVENT_ID_BACK;
32+
};
33+
34+
export const getConfig = () => ({
35+
ADDON_ID,
36+
PANEL_ID,
37+
PANEL_Title,
38+
PARAM_Key,
39+
EVENT_ID_INIT,
40+
EVENT_ID_DATA,
41+
EVENT_ID_BACK,
42+
})

src/decorator.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import React from 'react';
22
import withChannel from './withChannel';
33

44
import {
5-
ADDON_ID,
6-
EVENT_ID_INIT,
7-
EVENT_ID_DATA,
8-
EVENT_ID_BACK,
9-
PARAM_Key,
5+
getConfig,
106
} from './config';
117

128
export const createDecorator = Component => initData => (getStory, context) => {
9+
const {
10+
ADDON_ID,
11+
EVENT_ID_INIT,
12+
EVENT_ID_DATA,
13+
EVENT_ID_BACK,
14+
PARAM_Key,
15+
} = getConfig();
1316
const WithChannel = withChannel({
1417
EVENT_ID_INIT,
1518
EVENT_ID_DATA,
@@ -30,6 +33,9 @@ export const createDecorator = Component => initData => (getStory, context) => {
3033
);
3134
};
3235

33-
export const setParameters = params => ({
34-
[PARAM_Key]: params,
35-
});
36+
export const setParameters = () => {
37+
const { PARAM_Key } = getConfig();
38+
return params => ({
39+
[PARAM_Key]: params,
40+
});
41+
};

src/register.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import { STORY_CHANGED } from '@storybook/core-events';
44
import Rect from '@reach/rect';
55

66
import {
7-
ADDON_ID,
8-
PANEL_ID,
9-
PANEL_Title,
10-
EVENT_ID_INIT,
11-
EVENT_ID_DATA,
12-
EVENT_ID_BACK,
7+
getConfig,
138
} from './config';
149
import withChannel from './withChannel';
1510

@@ -73,11 +68,14 @@ class PanelHOC extends React.Component {
7368
this.state = {
7469
...urlState,
7570
};
76-
props.api.on(STORY_CHANGED, (kind, story) => this.setState({ kind, story }));
71+
props.api.on(STORY_CHANGED, (kind, story) =>
72+
this.setState({ kind, story })
73+
);
7774
}
7875
render() {
7976
const Panel = this.props.component;
80-
const { api, active, data, setData } = this.props;
77+
const { api, active, data, setData, config } = this.props;
78+
const {ADDON_ID, PANEL_ID, PANEL_Title} = config;
8179
const { kind, story } = this.state;
8280

8381
if (!active) return null;
@@ -112,16 +110,26 @@ class PanelHOC extends React.Component {
112110
}
113111
}
114112

115-
const WithChannel = withChannel({
116-
EVENT_ID_INIT,
117-
EVENT_ID_DATA,
118-
EVENT_ID_BACK,
119-
ADDON_ID,
120-
initData: { panel: 'foo' },
121-
panel: true,
122-
})(PanelHOC);
113+
export const register = (Panel, { type = addonTypes.PANEL, initData } = {}) => {
114+
const config = getConfig();
115+
const {
116+
EVENT_ID_INIT,
117+
EVENT_ID_DATA,
118+
EVENT_ID_BACK,
119+
ADDON_ID,
120+
PANEL_Title,
121+
PANEL_ID,
122+
} = config;
123+
124+
const WithChannel = withChannel({
125+
EVENT_ID_INIT,
126+
EVENT_ID_DATA,
127+
EVENT_ID_BACK,
128+
ADDON_ID,
129+
initData,
130+
panel: true,
131+
})(PanelHOC);
123132

124-
export const register = (Panel, type = addonTypes.PANEL) =>
125133
addons.register(ADDON_ID, api => {
126134
addons.add(PANEL_ID, {
127135
title: PANEL_Title,
@@ -132,10 +140,10 @@ export const register = (Panel, type = addonTypes.PANEL) =>
132140
key={key}
133141
api={api}
134142
active={active}
135-
PANEL_Title={PANEL_Title}
136143
component={Panel}
144+
config={config}
137145
/>
138146
),
139147
});
140-
141148
});
149+
};

0 commit comments

Comments
 (0)