Skip to content

Commit fc21a9c

Browse files
authored
Add more embed options (codesandbox#238)
* Add more embed options * Update snapshots
1 parent ac3a03a commit fc21a9c

File tree

7 files changed

+63
-21
lines changed

7 files changed

+63
-21
lines changed

src/app/components/sandbox/CodeEditor/CodeMirror.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Props = {
3131
sandboxId: string,
3232
modules: Array,
3333
directories: Array,
34+
hideNavigation: boolean,
3435
};
3536

3637
const Container = styled.div`
@@ -55,7 +56,7 @@ const CodeContainer = styled.div`
5556
position: relative;
5657
overflow: auto;
5758
width: 100%;
58-
height: calc(100% - 6rem);
59+
height: calc(100% - ${props => (props.hideNavigation ? 3 : 6)}rem);
5960
.CodeMirror {
6061
font-family: ${props =>
6162
fontFamilies(props.fontFamily, 'Source Code Pro', 'monospace')};
@@ -501,18 +502,22 @@ export default class CodeEditor extends React.Component<Props, State> {
501502
modules,
502503
directories,
503504
id,
505+
hideNavigation,
504506
} = this.props;
505507

506508
return (
507509
<Container>
508-
<Header
509-
saveComponent={canSave && !onlyViewMode && this.handleSaveCode}
510-
prettify={!onlyViewMode && this.prettify}
511-
path={modulePath}
512-
/>
510+
{!hideNavigation && (
511+
<Header
512+
saveComponent={canSave && !onlyViewMode && this.handleSaveCode}
513+
prettify={!onlyViewMode && this.prettify}
514+
path={modulePath}
515+
/>
516+
)}
513517
<CodeContainer
514518
fontFamily={preferences.fontFamily}
515519
lineHeight={preferences.lineHeight}
520+
hideNavigation={hideNavigation}
516521
>
517522
{this.state.fuzzySearchEnabled && (
518523
<FuzzySearch

src/app/components/sandbox/CodeEditor/Monaco.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Props = {
5050
setCurrentModule: ?(sandboxId: string, moduleId: string) => void,
5151
template: string,
5252
addDependency: ?(sandboxId: string, dependency: string) => void,
53+
hideNavigation: boolean,
5354
};
5455

5556
const Container = styled.div`
@@ -71,7 +72,7 @@ const fontFamilies = (...families) =>
7172
const CodeContainer = styled.div`
7273
position: relative;
7374
width: 100%;
74-
height: calc(100% - 6rem);
75+
height: calc(100% - ${props => (props.hideNavigation ? 3 : 6)}rem);
7576
z-index: 30;
7677
7778
.margin-view-overlays {
@@ -889,18 +890,23 @@ export default class CodeEditor extends React.Component<Props, State> {
889890
directories,
890891
onlyViewMode,
891892
modulePath,
893+
hideNavigation,
892894
} = this.props;
893895

894896
const options = this.getEditorOptions();
895897

896898
return (
897899
<Container>
898-
<Header
899-
saveComponent={canSave && !onlyViewMode ? this.handleSaveCode : null}
900-
prettify={!onlyViewMode && this.prettify}
901-
path={modulePath}
902-
/>
903-
<CodeContainer>
900+
{!hideNavigation && (
901+
<Header
902+
saveComponent={
903+
canSave && !onlyViewMode ? this.handleSaveCode : null
904+
}
905+
prettify={!onlyViewMode && this.prettify}
906+
path={modulePath}
907+
/>
908+
)}
909+
<CodeContainer hideNavigation={hideNavigation}>
904910
{this.state.fuzzySearchEnabled && (
905911
<FuzzySearch
906912
closeFuzzySearch={this.closeFuzzySearch}

src/common/__snapshots__/url.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`url parameters can autoresize 1`] = `
44
Object {
55
"autoResize": true,
6+
"editorSize": 50,
67
"enableEslint": false,
78
"hideNavigation": false,
89
"isEditorScreen": false,
@@ -16,6 +17,7 @@ exports[`url parameters can handle multiple options 1`] = `
1617
Object {
1718
"autoResize": true,
1819
"currentModule": "test",
20+
"editorSize": 50,
1921
"enableEslint": false,
2022
"hideNavigation": true,
2123
"isEditorScreen": true,
@@ -28,6 +30,7 @@ Object {
2830
exports[`url parameters can hide navigation 1`] = `
2931
Object {
3032
"autoResize": false,
33+
"editorSize": 50,
3134
"enableEslint": false,
3235
"hideNavigation": true,
3336
"isEditorScreen": false,
@@ -40,6 +43,7 @@ Object {
4043
exports[`url parameters doesn't set unknown fields 1`] = `
4144
Object {
4245
"autoResize": false,
46+
"editorSize": 50,
4347
"enableEslint": false,
4448
"hideNavigation": false,
4549
"isEditorScreen": false,
@@ -52,6 +56,7 @@ Object {
5256
exports[`url parameters keeps everything false on normal urls 1`] = `
5357
Object {
5458
"autoResize": false,
59+
"editorSize": 50,
5560
"enableEslint": false,
5661
"hideNavigation": false,
5762
"isEditorScreen": false,
@@ -65,6 +70,7 @@ exports[`url parameters sets current module if there is one 1`] = `
6570
Object {
6671
"autoResize": false,
6772
"currentModule": "test",
73+
"editorSize": 50,
6874
"enableEslint": false,
6975
"hideNavigation": false,
7076
"isEditorScreen": false,
@@ -77,6 +83,7 @@ Object {
7783
exports[`url parameters sets editor view 1`] = `
7884
Object {
7985
"autoResize": false,
86+
"editorSize": 50,
8087
"enableEslint": false,
8188
"hideNavigation": false,
8289
"isEditorScreen": true,
@@ -89,6 +96,7 @@ Object {
8996
exports[`url parameters sets preview view 1`] = `
9097
Object {
9198
"autoResize": false,
99+
"editorSize": 50,
92100
"enableEslint": false,
93101
"hideNavigation": false,
94102
"isEditorScreen": false,

src/common/url.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
22

33
export const getSandboxOptions = (url: string) => {
4-
const result = {};
4+
const result: Object = {
5+
editorSize: 50,
6+
};
57
const moduleMatch = url.match(/(\?|&)(module)=([^&]+)/);
68
if (moduleMatch) {
79
result.currentModule = moduleMatch[3];
@@ -17,6 +19,11 @@ export const getSandboxOptions = (url: string) => {
1719
result.fontSize = +fontSizeMatch[3];
1820
}
1921

22+
const editorSizeMatch = url.match(/(\?|&)(editorsize)=([^&]+)/);
23+
if (editorSizeMatch) {
24+
result.editorSize = +editorSizeMatch[3];
25+
}
26+
2027
result.isPreviewScreen = url.includes('view=preview');
2128
result.isEditorScreen = url.includes('view=editor');
2229

src/embed/App.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type State = {
6060
fontSize: number,
6161
enableEslint: boolean,
6262
useCodeMirror: boolean,
63+
editorSize: number,
6364
};
6465

6566
export default class App extends React.PureComponent<{}, State> {
@@ -77,6 +78,7 @@ export default class App extends React.PureComponent<{}, State> {
7778
fontSize,
7879
enableEslint,
7980
useCodeMirror,
81+
editorSize,
8082
} = getSandboxOptions(document.location.href);
8183

8284
this.state = {
@@ -93,6 +95,7 @@ export default class App extends React.PureComponent<{}, State> {
9395
hideNavigation,
9496
enableEslint,
9597
useCodeMirror,
98+
editorSize,
9699
};
97100
}
98101

@@ -212,6 +215,7 @@ export default class App extends React.PureComponent<{}, State> {
212215
setCurrentModule={this.setCurrentModule}
213216
useCodeMirror={this.state.useCodeMirror}
214217
enableEslint={this.state.enableEslint}
218+
editorSize={this.state.editorSize}
215219
/>
216220
</Container>
217221
</ThemeProvider>

src/embed/components/Content.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const Container = styled.div`
2121

2222
const Split = styled.div`
2323
position: relative;
24-
width: ${props => (props.show ? '50%' : '0px')};
25-
max-width: ${props => (props.only ? '100%' : '50%')};
26-
min-width: ${props => (props.only ? '100%' : '50%')};
24+
width: ${props => (props.show ? `${props.size}%` : '0px')};
25+
max-width: ${props => (props.only ? '100%' : `${props.size}%`)};
26+
min-width: ${props => (props.only ? '100%' : `${props.size}%`)};
2727
height: calc(100% + 3rem);
2828
`;
2929

@@ -42,6 +42,7 @@ type Props = {
4242
useCodeMirror: boolean,
4343
enableEslint: boolean,
4444
isInProjectView: boolean,
45+
editorSize: number,
4546
};
4647

4748
type State = {
@@ -164,6 +165,7 @@ export default class Content extends React.PureComponent<Props, State> {
164165
autoDownloadTypes: true,
165166
lintEnabled: this.props.enableEslint,
166167
codeMirror: this.props.useCodeMirror,
168+
lineHeight: 1.4,
167169
});
168170

169171
setCurrentModule = (_, moduleId) => {
@@ -178,6 +180,7 @@ export default class Content extends React.PureComponent<Props, State> {
178180
isInProjectView,
179181
currentModule,
180182
hideNavigation,
183+
editorSize,
181184
} = this.props;
182185

183186
const { errors } = this.state;
@@ -203,7 +206,11 @@ export default class Content extends React.PureComponent<Props, State> {
203206
return (
204207
<Container>
205208
{showEditor && (
206-
<Split show={showEditor} only={showEditor && !showPreview}>
209+
<Split
210+
show={showEditor}
211+
only={showEditor && !showPreview}
212+
size={editorSize}
213+
>
207214
<CodeEditor
208215
code={alteredMainModule.code}
209216
id={alteredMainModule.id}
@@ -221,14 +228,19 @@ export default class Content extends React.PureComponent<Props, State> {
221228
setCurrentModule={this.setCurrentModule}
222229
template={sandbox.template}
223230
dependencies={sandbox.npmDependencies}
231+
hideNavigation={hideNavigation}
224232
canSave={false}
225233
corrections={[]}
226234
/>
227235
</Split>
228236
)}
229237

230238
{showPreview && (
231-
<Split show={showPreview} only={showPreview && !showEditor}>
239+
<Split
240+
show={showPreview}
241+
only={showPreview && !showEditor}
242+
size={100 - editorSize}
243+
>
232244
<Preview
233245
sandboxId={sandbox.id}
234246
template={sandbox.template}

src/embed/components/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const Container = styled.div`
1515
height: 3rem;
1616
padding: 0 1rem;
1717
box-sizing: border-box;
18-
border-bottom: 1px solid ${props => props.theme.background2.darken(0.3)};
19-
background-color: ${props => props.theme.background2};
18+
border-bottom: 1px solid ${props => props.theme.background.darken(0.3)};
19+
background-color: ${props => props.theme.background};
2020
`;
2121

2222
const MenuIcon = styled(MenuIconSVG)`

0 commit comments

Comments
 (0)