-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.tsx
146 lines (136 loc) · 3.67 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
import React, { useState, useMemo } from 'react'
import styled from 'styled-components'
import { importCode, Runner, Scope } from 'react-runner'
import {
Container,
CodeMirror,
PreviewContainer,
Preview,
PreviewError,
} from '../LiveRunner'
// @ts-ignore
import todoApp from '!!raw-loader!./App.js'
// @ts-ignore
import todoAddTask from '!!raw-loader!./AddTask.js'
// @ts-ignore
import todoTaskList from '!!raw-loader!./TaskList.js'
const Tab = styled.div`
display: inline-block;
color: ${(props) => (props['aria-current'] ? 'steelblue' : 'gray')};
padding: 4px;
cursor: pointer;
`
const withFiles = (scope: Scope, files: Record<string, string>) => {
const imports: Scope = { ...scope.import }
const lookup = new Set<string>()
const importsProxy = new Proxy(imports, {
getOwnPropertyDescriptor(target, prop) {
if (target.hasOwnProperty(prop)) {
return Object.getOwnPropertyDescriptor(target, prop)
}
if (files.hasOwnProperty(prop)) {
return { writable: true, enumerable: true, configurable: true }
}
},
get(target, prop: string) {
if (prop in target) return target[prop]
if (files.hasOwnProperty(prop)) {
if (lookup.has(prop)) {
throw new Error(
`Circular dependency detected: ${[...lookup, prop].join(' -> ')}`
)
}
lookup.add(prop)
return (target[prop] = importCode(files[prop], {
...scope,
import: importsProxy,
}))
}
},
})
Object.keys(files).forEach((file) => {
try {
imports[file] = importsProxy[file]
lookup.clear()
} catch (error) {
error.filename = file
throw error
}
})
return { ...scope, import: imports }
}
const baseScope = {
import: {
react: React,
},
}
export const MultiFilesExample = () => {
const [codes, setCodes] = useState<string[]>([
todoApp,
todoAddTask,
todoTaskList,
])
const [importsError, setImportsError] = useState<string | null>(null)
const [renderError, setRenderError] = useState<string | null>(null)
const [tab, setTab] = useState(0)
const scope = useMemo(() => {
try {
const scope = withFiles(baseScope, {
'./AddTask.js': codes[1],
'./TaskList.js': codes[2],
})
if (importsError) setImportsError(null)
return scope
} catch (error) {
setImportsError(
`${error.filename ? `[${error.filename}] ` : ''}${error.toString()}`
)
}
}, [codes, importsError])
return (
<>
<Container>
<CodeMirror
filename={`${tab}`}
defaultValue={codes[tab]}
onChange={(newCode) => {
const newCodes = [...codes]
newCodes[tab] = newCode
setCodes(newCodes)
}}
/>
<PreviewContainer>
<Preview>
{importsError ? (
<PreviewError>{importsError}</PreviewError>
) : (
<Runner
code={codes[0]}
scope={scope}
onRendered={(error) => {
if (error) {
setRenderError(error.toString())
} else if (renderError) {
setRenderError(null)
}
}}
/>
)}
</Preview>
{renderError && <PreviewError>{renderError}</PreviewError>}
</PreviewContainer>
</Container>
<div>
{['App.js', 'AddTask.js', 'TaskList.js'].map((item, idx) => (
<Tab
key={item}
aria-current={idx === tab}
onClick={() => setTab(idx)}
>
{item}
</Tab>
))}
</div>
</>
)
}