forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.ts
More file actions
115 lines (101 loc) · 3.39 KB
/
url.ts
File metadata and controls
115 lines (101 loc) · 3.39 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
export interface SandboxOptions {
editorSize: number;
tabs?: string[];
currentModule?: string;
initialPath?: string;
fontSize?: number;
highlightedLines?: number[];
isPreviewScreen?: boolean;
isEditorScreen?: boolean;
isSplitScreen?: boolean;
isTestPreviewWindow?: boolean;
isConsolePreviewWindow?: boolean;
hideNavigation?: boolean;
theme?: string;
isInProjectView?: boolean;
autoResize?: boolean;
useCodeMirror?: boolean;
enableEslint?: boolean;
forceRefresh?: boolean;
expandDevTools?: boolean;
hideDevTools?: boolean;
verticalMode?: boolean;
runOnClick?: boolean;
previewWindow?: 'tests' | 'console';
}
export const getSandboxOptions = (url: string) => {
const result: SandboxOptions = {
editorSize: 50,
};
const moduleMatch = url.match(/(\?|&)(module)=([^&]+)/);
if (moduleMatch) {
if (moduleMatch[3].indexOf(',') > -1) {
const tabs = moduleMatch[3].split(',');
result.tabs = tabs;
result.currentModule = tabs[0];
} else {
result.currentModule = moduleMatch[3];
}
}
const initialPathMatch = url.match(/(\?|&)(initialpath)=([^&]+)/);
if (initialPathMatch) {
result.initialPath = decodeURIComponent(initialPathMatch[3]);
}
const fontSizeMatch = url.match(/(\?|&)(fontsize)=([^&]+)/);
if (fontSizeMatch) {
result.fontSize = +fontSizeMatch[3];
}
const highlightMatch = url.match(/(\?|&)(highlights)=([^&]+)/);
if (highlightMatch && highlightMatch[3]) {
result.highlightedLines = highlightMatch[3]
.split(',')
.map(number => Number(number));
}
const editorSizeMatch = url.match(/(\?|&)(editorsize)=([^&]+)/);
if (editorSizeMatch) {
result.editorSize = +editorSizeMatch[3];
}
const theme = url.match(/(\?|&)(theme)=([^&]+)/);
if (theme) result.theme = theme[3];
result.isPreviewScreen = url.includes('view=preview');
result.isEditorScreen = url.includes('view=editor');
result.isSplitScreen = url.includes('view=split');
result.isTestPreviewWindow = url.includes('previewwindow=tests');
result.isConsolePreviewWindow = url.includes('previewwindow=console');
if (result.isTestPreviewWindow && !result.isConsolePreviewWindow) {
result.previewWindow = 'tests';
}
if (!result.isTestPreviewWindow && result.isConsolePreviewWindow) {
result.previewWindow = 'console';
}
// If there is no view specified and the width of the window is <800 we want
// to default to preview
if (
!result.isPreviewScreen &&
!result.isEditorScreen &&
!result.isSplitScreen
) {
const windowWidth =
window.innerWidth ||
(document.documentElement ? document.documentElement.clientWidth : 0);
result.isEditorScreen = windowWidth >= 800;
result.isPreviewScreen = true;
}
result.hideNavigation = url.includes('hidenavigation=1');
result.isInProjectView = !url.includes('moduleview=1');
result.autoResize = url.includes('autoresize=1');
result.useCodeMirror = url.includes('codemirror=1');
result.enableEslint = url.includes('eslint=1');
result.forceRefresh = url.includes('forcerefresh=1');
result.expandDevTools = url.includes('expanddevtools=1');
result.hideDevTools = url.includes('hidedevtools=1');
if (url.includes('verticallayout=')) {
result.verticalMode = url.includes('verticallayout=1');
}
result.runOnClick = url.includes('runonclick=0')
? false
: url.includes('runonclick=1')
? true
: undefined;
return result;
};