forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.js
More file actions
293 lines (257 loc) · 6.92 KB
/
overlay.js
File metadata and controls
293 lines (257 loc) · 6.92 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* @flow */
import { transformError } from 'codesandbox-api';
import { getCurrentManager } from 'app/src/sandbox/compile';
import { listenToRuntimeErrors } from './listenToRuntimeErrors';
import {
consume as consumeError,
getErrorRecord,
drain as drainErrors,
} from './utils/errorRegister';
import type { ErrorRecordReference } from './utils/errorRegister';
import type { StackFrame } from './utils/stack-frame';
import { iframeStyle } from './styles';
import { applyStyles } from './utils/dom/css';
import { createOverlay } from './components/overlay';
import { updateAdditional } from './components/additional';
import buildError from '../errors';
const CONTEXT_SIZE: number = 3;
let iframeReference: HTMLIFrameElement | null = null;
let additionalReference = null;
let errorReferences: ErrorRecordReference[] = [];
let currReferenceIndex: number = -1;
function render(
name: ?string,
message: string,
resolvedFrames: StackFrame[],
error
) {
disposeCurrentView();
const iframe = window.document.createElement('iframe');
applyStyles(iframe, iframeStyle);
iframeReference = iframe;
iframe.onload = () => {
if (iframeReference == null) {
return;
}
const w = iframeReference.contentWindow;
const document = iframeReference.contentDocument;
const { overlay, additional } = createOverlay(
document,
error,
name,
message,
resolvedFrames,
CONTEXT_SIZE,
currReferenceIndex + 1,
errorReferences.length,
offset => {
switchError(offset);
},
() => {
unmount();
}
);
if (w != null) {
w.onkeydown = event => {
keyEventHandler(type => shortcutHandler(type), event);
};
}
if (document.body != null) {
document.body.style.margin = '0';
// Keep popup within body boundaries for iOS Safari
// $FlowFixMe
document.body.style['max-width'] = '100vw';
(document.body: any).appendChild(overlay);
}
additionalReference = additional;
};
window.document.body.appendChild(iframe);
}
function renderErrorByIndex(index: number) {
currReferenceIndex = index;
const { error, unhandledRejection, enhancedFrames } = getErrorRecord(
errorReferences[index]
);
if (unhandledRejection) {
render(
'Unhandled Rejection (' + error.name + ')',
error.message,
enhancedFrames,
error
);
} else {
render(error.name, error.message, enhancedFrames, error);
}
}
function switchError(offset) {
if (errorReferences.length === 0) {
return;
}
let nextView = currReferenceIndex + offset;
if (nextView < 0) {
nextView = errorReferences.length - 1;
} else if (nextView >= errorReferences.length) {
nextView = 0;
}
renderErrorByIndex(nextView);
}
function disposeCurrentView(force: boolean) {
// CodeSandbox already resets this, only do this if force is on
if (force) {
if (iframeReference == null) {
return;
}
try {
window.document.body.removeChild(iframeReference);
} catch (e) {
console.error(e);
}
}
iframeReference = null;
additionalReference = null;
}
function unmount(force: boolean = true) {
disposeCurrentView(force);
drainErrors();
errorReferences = [];
currReferenceIndex = -1;
}
function sendErrorsToEditor() {
errorReferences.forEach(ref => {
const error = getErrorRecord(ref);
buildError(error);
});
}
/**
* Transforms the error with give transformers to codesandbox-api, this adds
* suggestions and can alter the error name + message.
*/
function transformErrors() {
const manager = getCurrentManager();
if (manager) {
errorReferences.forEach(ref => {
const errRef = getErrorRecord(ref);
const relevantFrame = errRef.enhancedFrames.find(r => {
try {
return (
manager &&
!!manager.resolveTranspiledModule(
(r._originalFileName || r.fileName || '').replace(
location.origin,
''
),
'/'
)
);
} catch (e) {
/* don't do anything */
return false;
}
});
let { tModule } = errRef.error;
if (!tModule && relevantFrame) {
const fileName =
relevantFrame._originalFileName || relevantFrame.fileName || '';
tModule = manager.resolveTranspiledModule(
fileName.replace(location.origin, ''),
'/'
);
}
if (!tModule) {
return;
}
try {
const transformation = transformError(
errRef.error,
tModule,
manager.getTranspiledModules()
);
if (transformation) {
const newError = new Error(transformation.name || errRef.error.name);
newError.message = transformation.message;
newError.suggestions = transformation.suggestions;
newError.originalName = errRef.error.name;
newError.originalMessage = errRef.error.message;
errRef.error = newError;
}
} catch (ex) {
/* just catch */
console.error(ex);
}
});
}
}
function crash(error: Error, unhandledRejection = false) {
if (module.hot && typeof module.hot.decline === 'function') {
module.hot.decline();
}
consumeError(error, unhandledRejection, CONTEXT_SIZE)
.then(ref => {
if (ref == null) {
return;
}
errorReferences.push(ref);
sendErrorsToEditor();
transformErrors();
if (iframeReference !== null && additionalReference !== null) {
updateAdditional(
iframeReference.contentDocument,
additionalReference,
currReferenceIndex + 1,
errorReferences.length,
offset => {
switchError(offset);
}
);
} else {
if (errorReferences.length !== 1) {
throw new Error('Something is *really* wrong.');
}
renderErrorByIndex((currReferenceIndex = 0));
}
})
.catch(e => {
console.log('Could not consume error:', e);
});
}
function shortcutHandler(type: string) {
switch (type) {
case SHORTCUT_ESCAPE: {
unmount();
break;
}
case SHORTCUT_LEFT: {
switchError(-1);
break;
}
case SHORTCUT_RIGHT: {
switchError(1);
break;
}
default: {
// TODO: this
break;
}
}
}
let listenToRuntimeErrorsUnmounter;
function inject() {
listenToRuntimeErrorsUnmounter = listenToRuntimeErrors(error => {
crash(error.error);
});
}
function uninject() {
if (listenToRuntimeErrorsUnmounter) {
listenToRuntimeErrorsUnmounter();
}
unmount();
}
export { inject, uninject, unmount };