forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
200 lines (179 loc) · 5.4 KB
/
CodeEditor.tsx
File metadata and controls
200 lines (179 loc) · 5.4 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import Centered from '@codesandbox/common/lib/components/flex/Centered';
import Margin from '@codesandbox/common/lib/components/spacing/Margin';
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
import { getModulePath } from '@codesandbox/common/lib/sandbox/modules';
import getDefinition from '@codesandbox/common/lib/templates';
import getUI from '@codesandbox/common/lib/templates/configuration/ui';
import { Sandbox } from '@codesandbox/common/lib/types';
import isImage from '@codesandbox/common/lib/utils/is-image';
import { Configuration } from 'app/components/CodeEditor/Configuration';
import { Icon, Icons } from 'app/components/CodeEditor/elements';
import { Props } from 'app/components/CodeEditor/types'; // eslint-disable-line
import { SubTitle } from 'app/components/SubTitle';
import { Title } from 'app/components/Title';
import Loadable from 'app/utils/Loadable';
import React from 'react';
import UIIcon from 'react-icons/lib/md/dvr';
import { ImageViewer } from './ImageViewer';
import MonacoDiff from './MonacoDiff';
const CodeMirror = Loadable(() =>
import(
/* webpackChunkName: 'codemirror-editor' */ 'app/components/CodeEditor/CodeMirror'
)
);
const Monaco = Loadable(() =>
import(/* webpackChunkName: 'monaco-editor' */ './Monaco')
);
const getDependencies = (sandbox: Sandbox): { [key: string]: string } => {
const packageJSON = sandbox.modules.find(
m => m.title === 'package.json' && m.directoryShortid == null
);
if (packageJSON != null) {
try {
const { dependencies = {}, devDependencies = {} } = JSON.parse(
packageJSON.code || ''
);
const usedDevDependencies = {};
Object.keys(devDependencies).forEach(d => {
if (d.startsWith('@types')) {
usedDevDependencies[d] = devDependencies[d];
}
});
return { ...dependencies, ...usedDevDependencies };
} catch (e) {
console.error(e);
return null;
}
} else {
return typeof sandbox.npmDependencies.toJS === 'function'
? (sandbox.npmDependencies as any).toJS()
: sandbox.npmDependencies;
}
};
type State = {
showConfigUI: boolean;
};
export class CodeEditor extends React.PureComponent<
Props & {
editor?: 'vscode' | 'monaco' | 'codemirror';
style?: React.CSSProperties;
},
State
> {
state = {
showConfigUI: true,
};
toggleConfigUI = () => {
this.setState(state => ({ showConfigUI: !state.showConfigUI }));
};
render() {
const { props } = this;
const {
isModuleSynced,
currentTab,
sandbox,
currentModule: module,
settings,
} = props;
if (currentTab && currentTab.type === 'DIFF') {
return (
<div
style={{
height: props.height || '100%',
width: props.width || '100%',
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
>
<MonacoDiff
originalCode={currentTab.codeA}
modifiedCode={currentTab.codeB}
title={currentTab.fileTitle}
{...props}
/>
</div>
);
}
const dependencies = getDependencies(sandbox);
const template = getDefinition(sandbox.template);
const modulePath = getModulePath(
sandbox.modules,
sandbox.directories,
module.id
);
const config = template.configurationFiles[modulePath];
if (config && getUI(config.type) && this.state.showConfigUI) {
return (
<Configuration
{...props}
dependencies={dependencies}
config={config}
toggleConfigUI={this.toggleConfigUI}
/>
);
}
if (module.isBinary) {
if (isImage(module.title)) {
return <ImageViewer {...props} dependencies={dependencies} />;
}
return (
<Margin
style={{
overflow: 'auto',
height: props.height || '100%',
width: props.width || '100%',
}}
top={2}
>
<Centered horizontal vertical>
<Title>This file is too big to edit</Title>
<SubTitle>
We will add support for this as soon as possible.
</SubTitle>
<a href={module.code} target="_blank" rel="noreferrer noopener">
Open file externally
</a>
</Centered>
</Margin>
);
}
const Editor =
settings.codeMirror && !props.isLive
? ((CodeMirror as unknown) as React.ComponentClass<Props>)
: ((Monaco as unknown) as React.ComponentClass<Props>);
return (
<div
style={{
height: props.height || '100%',
width: props.width || '100%',
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
...props.style,
}}
>
{!isModuleSynced(module.shortid) && module.title === 'index.html' && (
<Icons>
You may have to save this file and refresh the preview to see
changes
</Icons>
)}
{config && getUI(config.type) ? (
<Icons>
<Tooltip content="Switch to UI Configuration">
<Icon onClick={this.toggleConfigUI}>
<UIIcon />
</Icon>
</Tooltip>
</Icons>
) : null}
<Editor {...props} dependencies={dependencies} />
</div>
);
}
}