/
index.tsx
290 lines (249 loc) · 7.69 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import * as React from 'react'
import { Component, Fragment } from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { RenderComponentProps } from 'docz'
import styled, { css } from 'react-emotion'
import lighten from 'polished/lib/color/lighten'
import darken from 'polished/lib/color/darken'
import rgba from 'polished/lib/color/rgba'
import Resizable from 're-resizable'
import Maximize from 'react-feather/dist/icons/maximize'
import Minimize from 'react-feather/dist/icons/minimize'
import hotkeys from 'hotkeys-js'
import getIn from 'lodash.get'
import pretty from 'pretty'
import { ResizeBar } from './ResizeBar'
import { Handle, HANDLE_SIZE } from './Handle'
import { Pre as PreBase, ActionButton, ClipboardAction } from '../Pre'
import { localStorage } from '../../../utils/local-storage'
interface OverlayProps {
full: boolean
}
const whenFullscreen = (on: any, off: any) => (p: OverlayProps) =>
p.full ? on : off
const Overlay = styled('div')`
z-index: ${whenFullscreen(9999, 0)};
position: ${whenFullscreen('fixed', 'initial')};
top: 0;
left: 0;
width: ${whenFullscreen('100vw', 'auto')};
height: ${whenFullscreen('100vh', 'auto')};
padding: ${whenFullscreen('60px 20px 20px', '0px')};
margin: ${whenFullscreen('0px', '0 0 30px')};
background: ${whenFullscreen('rgba(0,0,0,0.5)', 'transparent')};
transition: background 0.3s;
`
const minusHandleSize = `calc(100% - ${HANDLE_SIZE} + 10px)`
const Wrapper = styled('div')`
display: flex;
flex-direction: column;
height: ${whenFullscreen(minusHandleSize, '100%')};
width: ${minusHandleSize};
`
const Playground = styled('div')`
overflow-y: hidden;
flex: 1;
background: ${p => p.theme.docz.colors.background};
border: 1px solid ${p => p.theme.docz.colors.border};
border-radius: 4px 4px 0 0;
${p => p.theme.docz.mq(p.theme.docz.styles.playground)};
`
const Pre = styled(PreBase)`
margin: 0;
border-radius: 0 0 4px 4px;
border-top: 0;
`
const Actions = styled('div')`
display: flex;
padding: 0 5px;
background: ${p =>
p.theme.docz.mode === 'light'
? lighten(0.13, p.theme.docz.colors.border)
: darken(0.04, p.theme.docz.colors.border)};
border-left: 1px solid ${p => p.theme.docz.colors.border};
border-bottom: 1px solid ${p => p.theme.docz.colors.border};
`
const actionClass = (p: any) => css`
padding: 3px 10px;
border-left: 1px solid ${p.theme.docz.colors.border};
`
const Action = styled(ActionButton)`
${actionClass};
`
const Clipboard = styled(ClipboardAction)`
${actionClass};
`
const Tabs = styled('div')`
flex: 1;
display: flex;
align-items: center;
`
interface TabProps {
active: boolean
theme?: any
}
const Tab = styled('button')`
position: relative;
cursor: pointer;
display: block;
outline: none;
height: 100%;
background: none;
border: none;
font-size: 14px;
color: ${(p: TabProps) =>
rgba(p.theme.docz.colors.text, p.active ? 0.8 : 0.4)};
transition: color 0.3s;
`
const storage = localStorage()
const get = (pos: number): any => storage.get(pos.toString())
const remove = (pos: number): void => storage.remove(pos.toString())
const set = (pos: number, size: string): void =>
storage.set(pos.toString(), size)
const parse = (position: number, key: string, defaultValue: any) => {
const obj = JSON.parse(get(position))
return obj ? getIn(obj, key) : defaultValue
}
export interface RenderState {
fullscreen: boolean
width: string
height: string
showing: 'jsx' | 'html'
}
export class Render extends Component<RenderComponentProps, RenderState> {
public state: RenderState = {
fullscreen: parse(this.props.position, 'fullscreen', false),
width: parse(this.props.position, 'width', '100%'),
height: parse(this.props.position, 'height', '100%'),
showing: parse(this.props.position, 'showing', 'jsx'),
}
public componentDidMount(): void {
this.addUnloadListener()
hotkeys('esc', this.closeOnEsc)
}
public componentWillUnmount(): void {
this.removeUnloadListener()
hotkeys.unbind('esc')
}
get showingContent(): string {
const { code, component } = this.props
const { showing } = this.state
const content = {
jsx: () => code,
html: () => pretty(renderToStaticMarkup(component)),
}
return content[showing]()
}
get actions(): JSX.Element {
const { showing, fullscreen } = this.state
const showJsx = this.handleShow('jsx')
const showHtml = this.handleShow('html')
return (
<Actions>
<Tabs>
<Tab active={showing === 'jsx'} onClick={showJsx}>
JSX
</Tab>
<Tab active={showing === 'html'} onClick={showHtml}>
HTML
</Tab>
</Tabs>
<Clipboard content={this.showingContent} />
<Action
onClick={this.handleToggle}
title={fullscreen ? 'Minimize' : 'Maximize'}
>
{fullscreen ? <Minimize width={15} /> : <Maximize width={15} />}
</Action>
</Actions>
)
}
get resizableProps(): Record<string, any> {
const { fullscreen, width, height } = this.state
return {
minHeight: fullscreen ? 360 : '100%',
minWidth: 320,
maxWidth: '100%',
maxHeight: '100%',
size: {
width,
height,
},
style: {
margin: '0 auto ',
},
enable: {
top: false,
right: true,
bottom: fullscreen,
left: false,
topRight: false,
bottomRight: fullscreen,
bottomLeft: false,
topLeft: false,
},
handleComponent: {
right: () => <Handle full={fullscreen} horizontal />,
bottom: () => <Handle full={fullscreen} horizontal={false} />,
},
onResizeStop: (e: any, direction: any, ref: any, d: any) => {
const width = ref.style.width
const height = ref.style.height
this.handleSetSize(width, height)
},
}
}
public render(): JSX.Element {
const { className, style, component } = this.props
const { fullscreen } = this.state
return (
<Overlay full={fullscreen}>
{fullscreen && <ResizeBar onChangeSize={this.handleSetSize} />}
<Resizable {...this.resizableProps}>
<Wrapper full={fullscreen}>
<Playground className={className} style={style}>
{component}
</Playground>
{this.actions}
<Pre actions={<Fragment />}>{this.showingContent}</Pre>
</Wrapper>
</Resizable>
</Overlay>
)
}
private unloadListener = (): void => remove(this.props.position)
private addUnloadListener = () => {
if (window && typeof window !== 'undefined') {
window.addEventListener('beforeunload', this.unloadListener, false)
}
}
private removeUnloadListener = () => {
if (window && typeof window !== 'undefined') {
window.removeEventListener('beforeunload', this.unloadListener, false)
}
}
private setSize = (fullscreen: boolean) => {
set(this.props.position, JSON.stringify({ ...this.state, fullscreen }))
}
private handleToggle = () => {
const { position } = this.props
const { fullscreen } = this.state
if (fullscreen) remove(position)
else this.setSize(true)
this.setState({
fullscreen: parse(position, 'fullscreen', false),
width: parse(position, 'width', '100%'),
height: parse(position, 'height', '100%'),
})
}
private handleShow = (showing: 'jsx' | 'html') => () => {
this.setState({ showing })
}
private closeOnEsc = () => {
this.state.fullscreen && this.handleToggle()
}
private handleSetSize = (width: string, height: string) => {
const fullscreen = parse(this.props.position, 'fullscreen', false)
this.setState({ width, height }, () => this.setSize(fullscreen))
}
}