Skip to content

Commit a6f3c5c

Browse files
authored
Align preview and set min width/height (codesandbox#519)
1 parent 1a86286 commit a6f3c5c

File tree

3 files changed

+91
-57
lines changed

3 files changed

+91
-57
lines changed

packages/app/src/app/pages/Sandbox/Editor/Content/Preview/FlyingContainer.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { MouseEvent } from 'react';
1+
// @flow
2+
import * as React from 'react';
23
import { inject, observer } from 'mobx-react';
34

45
import { TweenMax, Elastic } from 'gsap';
@@ -17,8 +18,35 @@ import {
1718
ResizingNotice,
1819
} from './elements';
1920

20-
class FlyingContainer extends React.Component {
21-
state = { resizing: false, dragging: false };
21+
type Props = {
22+
signals: any,
23+
store: any,
24+
children: (funcs: { resize: Function }) => React.Node,
25+
onPositionChange?: () => void,
26+
};
27+
28+
type State = {
29+
resizing: boolean,
30+
dragging: boolean,
31+
x: ?number,
32+
y: ?number,
33+
width: ?number,
34+
height: ?number,
35+
};
36+
37+
class FlyingContainer extends React.Component<Props, State> {
38+
state = {
39+
resizing: false,
40+
dragging: false,
41+
x: undefined,
42+
y: undefined,
43+
width: undefined,
44+
height: undefined,
45+
};
46+
47+
el: ?HTMLElement;
48+
initialWidth: ?number;
49+
initialHeight: ?number;
2250

2351
updateBounds = el => {
2452
if (el) {
@@ -34,6 +62,10 @@ class FlyingContainer extends React.Component {
3462
handleStartDrag = () => {
3563
this.setState({ dragging: true });
3664
this.setResizingStarted();
65+
66+
if (this.props.onPositionChange) {
67+
this.props.onPositionChange();
68+
}
3769
};
3870

3971
handleStopDrag = (e, data) => {
@@ -48,6 +80,10 @@ class FlyingContainer extends React.Component {
4880

4981
setResizingStarted = () => {
5082
this.props.signals.editor.resizingStarted();
83+
84+
if (this.props.onPositionChange) {
85+
this.props.onPositionChange();
86+
}
5187
};
5288

5389
setResizingStopped = () => {
@@ -115,10 +151,10 @@ class FlyingContainer extends React.Component {
115151
const update = {};
116152

117153
if (vertical) {
118-
update.height = newSizeY;
154+
update.height = Math.max(48, newSizeY);
119155
}
120156
if (horizontal) {
121-
update.width = newSizeX;
157+
update.width = Math.max(48, newSizeX);
122158
}
123159

124160
if (changePositionY) {

packages/app/src/app/pages/Sandbox/Editor/Content/Preview/index.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ type Props = {
1313
signals: any,
1414
};
1515

16-
class Preview extends React.Component<Props> {
16+
type State = {
17+
aligned: ?'right' | 'bottom',
18+
};
19+
20+
class Preview extends React.Component<Props, State> {
21+
state = {
22+
aligned: 'right',
23+
};
24+
1725
onPreviewInitialized = preview => {
1826
let preventCodeExecution = false;
1927
const disposeHandleProjectViewChange = reaction(
@@ -93,7 +101,20 @@ class Preview extends React.Component<Props> {
93101

94102
componentWillReceiveProps(props: Props) {
95103
const { width, height } = props;
96-
if (width && height) {
104+
105+
if (this.state.aligned) {
106+
if (width !== this.props.width || height !== this.props.height) {
107+
if (this.state.aligned === 'bottom') {
108+
this.props.signals.editor.setPreviewBounds(
109+
this.getBottomCoordinates(props)
110+
);
111+
} else {
112+
this.props.signals.editor.setPreviewBounds(
113+
this.getRightCoordinates(props)
114+
);
115+
}
116+
}
117+
} else if (width && height) {
97118
let newWidth = props.store.editor.previewWindow.width;
98119
if (
99120
width - 16 <
@@ -173,6 +194,24 @@ class Preview extends React.Component<Props> {
173194
});
174195
};
175196

197+
resetAlignment = () => {
198+
this.setState({ aligned: null });
199+
};
200+
201+
getBottomCoordinates = (props = this.props) => ({
202+
x: 0,
203+
y: (props.height || 0) / 2 - 16,
204+
width: (props.width || 0) - 16,
205+
height: (props.height || 0) / 2,
206+
});
207+
208+
getRightCoordinates = (props = this.props) => ({
209+
x: 0,
210+
y: 0,
211+
width: (props.width || 0) / 2,
212+
height: (props.height || 0) - 16,
213+
});
214+
176215
render() {
177216
const { store, signals } = this.props;
178217

@@ -182,7 +221,7 @@ class Preview extends React.Component<Props> {
182221
};
183222

184223
return (
185-
<FlyingContainer>
224+
<FlyingContainer onPositionChange={this.resetAlignment}>
186225
{({ resize }) => (
187226
<BasePreview
188227
onInitialized={this.onPreviewInitialized}
@@ -207,22 +246,14 @@ class Preview extends React.Component<Props> {
207246
onToggleProjectView={() => signals.editor.projectViewToggled()}
208247
showDevtools={store.preferences.showDevtools}
209248
isResizing={store.editor.isResizing}
210-
alignRight={() =>
211-
resize({
212-
x: 0,
213-
y: 0,
214-
width: (this.props.width || 0) / 2,
215-
height: (this.props.height || 0) - 16,
216-
})
217-
}
218-
alignBottom={() =>
219-
resize({
220-
x: 0,
221-
y: (this.props.height || 0) / 2 - 16,
222-
width: (this.props.width || 0) - 16,
223-
height: (this.props.height || 0) / 2,
224-
})
225-
}
249+
alignRight={() => {
250+
resize(this.getRightCoordinates());
251+
this.setState({ aligned: 'right' });
252+
}}
253+
alignBottom={() => {
254+
resize(this.getBottomCoordinates());
255+
this.setState({ aligned: 'bottom' });
256+
}}
226257
/>
227258
)}
228259
</FlyingContainer>

packages/common/utils/keybindings.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,39 +53,6 @@ export const KEYBINDINGS = {
5353
signal: 'workspace.toggleCurrentWorkspaceItem',
5454
},
5555

56-
'editor.editor-mode': {
57-
title: 'Editor View',
58-
type: 'View',
59-
bindings: [[metaKey, 'K', 'E']],
60-
signal: 'preferences.viewModeChanged',
61-
payload: {
62-
showEditor: true,
63-
showPreview: false,
64-
},
65-
},
66-
67-
'editor.preview-mode': {
68-
title: 'Preview View',
69-
type: 'View',
70-
bindings: [[metaKey, 'K', 'P']],
71-
signal: 'preferences.viewModeChanged',
72-
payload: {
73-
showEditor: false,
74-
showPreview: true,
75-
},
76-
},
77-
78-
'editor.split-mode': {
79-
title: 'Split View',
80-
type: 'View',
81-
bindings: [[metaKey, 'K', 'S']],
82-
signal: 'preferences.viewModeChanged',
83-
payload: {
84-
showEditor: true,
85-
showPreview: true,
86-
},
87-
},
88-
8956
'editor.zen-mode': {
9057
title: 'Toggle Zen Mode',
9158
type: 'View',

0 commit comments

Comments
 (0)