-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
css-resource-panel.tsx
140 lines (122 loc) · 3.7 KB
/
css-resource-panel.tsx
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Component, Fragment } from 'react';
import { SyntaxHighlighter, Placeholder, Spaced, Icons } from '@storybook/components';
import { STORY_RENDERED } from '@storybook/core-events';
import { API } from '@storybook/api';
import { styled } from '@storybook/theming';
import { EVENTS, PARAM_KEY } from './constants';
import { CssResource } from './CssResource';
interface Props {
active: boolean;
api: API;
}
interface State {
currentStoryId: string;
list: CssResource[];
}
interface CssResourceLookup {
[key: string]: CssResource;
}
const maxLimitToUseSyntaxHighlighter = 100000;
const PlainCode = styled.pre({
textAlign: 'left',
fontWeight: 'normal',
});
const Warning = styled.div({
display: 'flex',
padding: '10px',
justifyContent: 'center',
alignItems: 'center',
background: '#fff3cd',
fontSize: 12,
'& svg': {
marginRight: 10,
width: 24,
height: 24,
},
});
export class CssResourcePanel extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
currentStoryId: '',
list: [],
};
}
componentDidMount() {
const { api } = this.props;
api.on(STORY_RENDERED, this.onStoryChange);
}
componentWillUnmount() {
const { api } = this.props;
api.off(STORY_RENDERED, this.onStoryChange);
}
onStoryChange = (id: string) => {
const { list: currentList, currentStoryId } = this.state;
const { api } = this.props;
const list = api.getParameters(id, PARAM_KEY) as CssResource[];
if (list && currentStoryId !== id) {
const existingIds = currentList.reduce((lookup: CssResourceLookup, res) => {
// eslint-disable-next-line no-param-reassign
lookup[res.id] = res;
return lookup;
}, {}) as CssResourceLookup;
const mergedList = list.map(res => {
const existingItem = existingIds[res.id];
return existingItem
? {
...res,
picked: existingItem.picked,
}
: res;
});
const picked = mergedList.filter(res => res.picked);
this.setState({ list: mergedList, currentStoryId: id }, () => this.emit(picked));
}
};
onChange = (event: any) => {
const { list: oldList } = this.state;
const list = oldList.map(i => ({
...i,
picked: i.id === event.target.id ? event.target.checked : i.picked,
}));
this.setState({ list }, () => this.emit(list.filter((res: any) => res.picked)));
};
emit(list: CssResource[]) {
const { api } = this.props;
api.emit(EVENTS.SET, list);
}
render() {
const { list } = this.state;
const { active } = this.props;
if (!active) {
return null;
}
return (
<div>
{list &&
list.map(({ id, code, picked, hideCode = false }) => (
<div key={id} style={{ padding: 10 }}>
<label>
<input type="checkbox" checked={picked} onChange={this.onChange} id={id} />
<span>#{id}</span>
</label>
{code && !hideCode && code.length < maxLimitToUseSyntaxHighlighter && (
<SyntaxHighlighter language="html">{code}</SyntaxHighlighter>
)}
{code && !hideCode && code.length >= maxLimitToUseSyntaxHighlighter && (
<Placeholder>
<Spaced row={1}>
<PlainCode>{code.substring(0, maxLimitToUseSyntaxHighlighter)} ...</PlainCode>
<Warning>
<Icons icon="alert" />
Rest of the content cannot be displayed
</Warning>
</Spaced>
</Placeholder>
)}
</div>
))}
</div>
);
}
}