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
149 lines (136 loc) · 3.67 KB
/
utils.tsx
File metadata and controls
149 lines (136 loc) · 3.67 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
import * as React from 'react'
import marksy from 'marksy/components'
import styled from './styled-components'
import * as Prism from './prismjs.js'
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;
`
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'].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 'Missing example'
}
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%' }} />
),
},
})