forked from ritz078/transform
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorPanel.tsx
More file actions
298 lines (275 loc) · 7.1 KB
/
EditorPanel.tsx
File metadata and controls
298 lines (275 loc) · 7.1 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
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
291
292
293
294
295
296
297
298
import {
Button,
FilePicker,
Heading,
HTMLInputEvent,
IconButton,
Pane,
Popover,
TextInput,
toaster,
Tooltip
} from "evergreen-ui";
import React, { useCallback, useEffect, useRef, useState } from "react";
import dynamic from "next/dynamic";
import copy from "clipboard-copy";
import Npm from "@assets/svgs/Npm";
import { useDropzone } from "react-dropzone";
export interface EditorPanelProps {
editable?: boolean;
language?: string;
defaultValue: string;
title: React.ReactNode;
hasCopy?: boolean;
hasPrettier?: boolean;
id: string | number;
onChange?: (value: string) => void;
hasLoad?: boolean;
hasClear?: boolean;
settingElement?: (args: { toggle: () => void; open: boolean }) => JSX.Element;
alertMessage?: React.ReactNode;
topNotifications?: (args: {
toggleSettings: () => void;
isSettingsOpen: boolean;
}) => React.ReactNode;
previewElement?: (value: string) => React.ReactNode;
acceptFiles?: string | string[];
packageDetails?: {
name: string;
url: string;
};
}
const Monaco = dynamic(() => import("../components/Monaco"), {
ssr: false
});
export default function EditorPanel({
editable = true,
title,
settingElement,
hasLoad,
acceptFiles,
hasClear,
hasCopy = true,
topNotifications,
language,
defaultValue,
onChange,
id,
packageDetails
}: EditorPanelProps) {
const [showSettingsDialogue, setSettingsDialog] = useState(false);
const [value, setValue] = useState(defaultValue);
const [fetchingUrl, setFetchingUrl] = useState("");
const options = {
fontSize: 14,
readOnly: !editable,
codeLens: false,
fontFamily: "Menlo, Consolas, monospace, sans-serif",
minimap: {
enabled: false
},
quickSuggestions: false,
lineNumbers: "on",
renderValidationDecorations: "off"
};
const _toggleSettingsDialog = useCallback(
() => setSettingsDialog(!showSettingsDialogue),
[showSettingsDialogue]
);
useEffect(() => {
// @ts-ignore
window.__webpack_public_path__ = "/_next/static/";
}, []);
const getSettings = useCallback(
() => (
<>
<Button
marginRight={10}
iconBefore="cog"
onClick={_toggleSettingsDialog}
height={28}
>
Settings
</Button>
{settingElement({
toggle: _toggleSettingsDialog,
open: showSettingsDialogue
})}
</>
),
[showSettingsDialogue]
);
const onFilePicked = useCallback((files, close = () => {}) => {
if (!(files && files.length)) return;
const file = files[0];
const reader = new FileReader();
reader.readAsText(file, "utf-8");
reader.onload = () => {
setValue(reader.result as string);
onChange(reader.result as string);
close();
};
}, []);
const { getRootProps } = useDropzone({
onDrop: files => onFilePicked(files),
disabled: !editable,
accept: acceptFiles,
onDropRejected: () =>
toaster.danger("This file type is not supported.", {
id
})
});
const copyValue = useCallback(() => {
copy(value);
toaster.success("Copied to clipboard.", {
id
});
}, [value]);
const fetchFile = useCallback(
close => {
(async () => {
if (!fetchingUrl) return;
const res = await fetch(fetchingUrl);
const value = await res.text();
setValue(value);
setFetchingUrl("");
close();
onChange(value);
})();
},
[fetchingUrl, onChange]
);
// whenever defaultValue changes, change the value of the editor.
useEffect(() => {
setValue(defaultValue);
}, [defaultValue]);
return (
<Pane display="flex" flex={1} flexDirection="column" overflow="hidden">
<Pane
display="flex"
height={40}
paddingX={10}
alignItems={"center"}
borderBottom
zIndex={2}
backgroundColor="#FFFFFF"
flexShrink={0}
>
<Pane flex={1}>
<Heading size={500} marginTop={0}>
{title}
</Heading>
</Pane>
{settingElement && getSettings()}
{hasLoad && (
<Popover
content={({ close }) => (
<Pane
paddingY={20}
paddingX={20}
display="flex"
flex={1}
alignItems="center"
justifyContent="center"
flexDirection="column"
backgroundColor="#FFFFFF"
>
<FilePicker
width={"100%"}
name="filepicker"
onChange={files => onFilePicked(files, close)}
accept={acceptFiles}
/>
<Heading paddingY={10} size={200}>
OR
</Heading>
<Pane display="flex" flexDirection="row">
<TextInput
borderBottomRightRadius={0}
borderTopRightRadius={0}
placeholder="Enter URL"
onChange={(e: HTMLInputEvent) =>
setFetchingUrl(e.target.value)
}
/>
<Button
borderLeftWidth={0}
borderBottomLeftRadius={0}
borderTopLeftRadius={0}
onClick={() => fetchFile(close)}
>
Fetch URL
</Button>
</Pane>
</Pane>
)}
shouldCloseOnExternalClick
>
<Tooltip content="Load File">
<IconButton height={28} marginRight={10} icon="upload" />
</Tooltip>
</Popover>
)}
{hasClear && (
<Tooltip content="Clear">
<IconButton
height={28}
icon="trash"
intent="danger"
marginRight={10}
onClick={() => setValue("")}
/>
</Tooltip>
)}
{packageDetails && (
<a
href={packageDetails.url}
style={{
display: "inline-flex"
}}
target="_blank"
>
<Tooltip content={packageDetails.name}>
<Npm />
</Tooltip>
</a>
)}
{hasCopy && (
<Button
appearance="primary"
marginRight={10}
iconBefore="duplicate"
onClick={copyValue}
height={28}
>
Copy
</Button>
)}
</Pane>
<div
style={{
display: "flex",
flexDirection: "column",
flex: 1,
overflow: "hidden"
}}
{...getRootProps()}
>
{topNotifications &&
topNotifications({
isSettingsOpen: showSettingsDialogue,
toggleSettings: _toggleSettingsDialog
})}
<Monaco
language={language}
value={value}
options={options}
onChange={value => {
setValue(value);
onChange(value);
}}
/>
</div>
</Pane>
);
}