forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.tsx
More file actions
180 lines (165 loc) · 4.6 KB
/
utils.tsx
File metadata and controls
180 lines (165 loc) · 4.6 KB
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
import * as React from 'react'
import marksy from 'marksy'
import styled from './styled-components'
import * as Prism from './prismjs.js'
export const viewport = {
isMobile: window.innerWidth <= 1024,
set() {
this.isMobile = window.innerWidth <= 1024
},
}
export const getTheme = () => localStorage.getItem('theme') || 'react'
export const getTypescript = () => localStorage.getItem('typescript') || false
const LoadingExample = styled.div`
display: flex;
align-items: center;
justify-content: center;
height: 100px;
color: ${({ theme }) => theme.color.fade(theme.color.black, 0.5)};
background-color: ${({ theme }) => theme.color.gray};
border-radius: ${({ theme }) => theme.borderRadius.normal};
`
const FileName = styled.div`
font-size: ${({ theme }) => theme.fontSize.small};
font-weight: bold;
`
const Notice = styled.div`
background-color: ${({ theme }) =>
theme.color.lighten(theme.color.white, -0.02)};
border: 1px solid ${({ theme }) => theme.color.primary};
border-left: 6px solid ${({ theme }) => theme.color.primary};
border-radius: ${({ theme }) => theme.borderRadius.normal};
padding: ${({ theme }) => `${theme.padding.small} ${theme.padding.normal}`};
font-size: ${({ theme }) => theme.fontSize.normal};
`
type TExample = {
fileName: string
code: string
target: string
}
class Example extends React.Component<{
name: string
view?: boolean
}> {
state: {
isLoading: boolean
content: TExample[]
}
currentExampleName = null
constructor(props) {
super(props)
const localContent = JSON.parse(
localStorage.getItem(this.getKey()) || 'null'
)
this.state = {
isLoading: !localContent,
content: localContent,
}
}
componentDidMount() {
this.getExample()
}
componentDidUpdate() {
if (this.currentExampleName !== this.getExampleName()) {
this.getExample()
}
}
verifyExamples(module) {
const examples = Object.keys(module)
if (this.props.view) {
;['react', 'vue', 'angular'].forEach((type) => {
if (!examples.includes(type)) {
console.warn(`Missing example for "${type}" in ${this.props.name}`)
}
if (!examples.includes(`${type}Ts`)) {
console.warn(
`Missing example for "${type}" with TypeScript in ${
this.props.name
}`
)
}
})
} else {
;['js', 'ts'].forEach((type) => {
if (!examples.includes(type)) {
console.warn(
`Missing example for "${
type === 'js' ? 'Vanilla JS' : 'TypeScript'
}" in ${this.props.name}`
)
}
})
}
}
getExample() {
import('../examples/' + this.props.name).then((module) => {
this.currentExampleName = this.getExampleName()
this.verifyExamples(module)
if (module[this.currentExampleName]) {
localStorage.setItem(
this.getKey(),
JSON.stringify(module[this.currentExampleName])
)
}
this.setState({
content: module[this.currentExampleName],
isLoading: false,
})
})
}
getKey() {
return `${this.props.name}.${this.getExampleName()}`
}
getExampleName() {
if (this.props.view) {
const theme = getTheme()
const ts = getTypescript()
return ts ? `${theme}Ts` : theme
}
return getTypescript() ? 'ts' : 'js'
}
render() {
if (this.state.isLoading) {
return <LoadingExample>{'<loading/>'}</LoadingExample>
}
if (!this.state.content) {
return (
<span style={{ color: 'red', fontWeight: 'bold' }}>
MISSING EXAMPLE ({this.props.name})
</span>
)
}
return this.state.content.map((example, index) => (
<React.Fragment key={index}>
{example.fileName ? <FileName>{example.fileName}</FileName> : null}
{
compile(
`\`\`\`${example.target || 'ts'}\n${example.code.trim()}\n\`\`\``
).tree
}
</React.Fragment>
))
}
}
export const compile = marksy({
createElement: React.createElement,
highlight(language, code) {
return Prism.highlight(code, Prism.languages[language], language)
},
components: {
Example: ({ name, view }) => <Example key={name} name={name} view={view} />,
Image: ({ src }) => (
<img src={`/images/${src}`} style={{ width: '100%' }} />
),
Notice: ({ children }) => <Notice>{compile(children[0]).tree}</Notice>,
},
elements: {
a({ href, children }) {
return (
<a href={href} target={href.indexOf('http') === 0 ? '_blank' : null}>
{children}
</a>
)
},
},
})