forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
74 lines (62 loc) · 1.83 KB
/
index.ts
File metadata and controls
74 lines (62 loc) · 1.83 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
// @ts-ignore
import indicatorHtml from '!raw-loader!./indicator-screen.html';
// This is the loading screen
// @ts-ignore
import loadingHtml from '!raw-loader!./loading-screen.html';
import { createOverlay, resetOverlay } from './overlay-manager';
type LoadingScreen = {
type: 'loading';
text: string;
showFullScreen?: boolean;
};
type Screen = LoadingScreen;
let currentScreen: Screen = null;
let loadTimeoutID: number = null;
let iframeReference = null;
function changeText(text: string) {
if (iframeReference) {
if (
iframeReference.contentDocument &&
iframeReference.contentDocument.getElementsByClassName('text') &&
iframeReference.contentDocument.getElementsByClassName('text').item(0)
) {
iframeReference.contentDocument
.getElementsByClassName('text')
.item(0).textContent = text;
}
}
}
export function resetScreen() {
currentScreen = null;
iframeReference = null;
resetOverlay();
clearTimeout(loadTimeoutID);
loadTimeoutID = null;
}
export default function setScreen(screen: Screen) {
const showFullScreen =
typeof screen.showFullScreen === 'undefined' ? true : screen.showFullScreen;
if (!iframeReference) {
if (!loadTimeoutID) {
// Give the illusion of faster loading by showing the loader screen later
loadTimeoutID = window.setTimeout(
async () => {
if (!iframeReference && currentScreen) {
iframeReference = await createOverlay(
showFullScreen ? loadingHtml : indicatorHtml,
showFullScreen
);
}
if (currentScreen) {
changeText(currentScreen.text);
}
loadTimeoutID = null;
},
showFullScreen ? 1000 : 0
);
}
} else if (currentScreen) {
changeText(currentScreen.text);
}
currentScreen = screen;
}