Skip to content

Commit ec1b361

Browse files
author
Ives van Hoorne
committed
Updates
1 parent c5e50db commit ec1b361

File tree

9 files changed

+122
-29
lines changed

9 files changed

+122
-29
lines changed

packages/app/config/webpack.common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const babelDev = require('./babel.dev');
1313
const babelProd = require('./babel.prod');
1414

1515
const NODE_ENV = JSON.parse(env['process.env.NODE_ENV']);
16+
const SANDBOX_ONLY = !!process.env.SANDBOX_ONLY;
1617
const __DEV__ = NODE_ENV === 'development'; // eslint-disable-line no-underscore-dangle
1718
const __PROD__ = NODE_ENV === 'production'; // eslint-disable-line no-underscore-dangle
18-
const __TEST__ = NODE_ENV === 'test'; // eslint-disable-line no-underscore-dangle
19+
// const __TEST__ = NODE_ENV === 'test'; // eslint-disable-line no-underscore-dangle
1920
const babelConfig = __DEV__ ? babelDev : babelProd;
2021

2122
// Shim for `eslint-plugin-vue/lib/index.js`
@@ -35,7 +36,7 @@ const ESLINT_PLUGIN_VUE_INDEX = `module.exports = {
3536
}`;
3637

3738
module.exports = {
38-
entry: __TEST__
39+
entry: SANDBOX_ONLY
3940
? {
4041
sandbox: [
4142
require.resolve('./polyfills'),

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"start": "cross-env LOCAL_SERVER=1 node scripts/start.js",
88
"start:dev_api": "node scripts/start.js",
9-
"start:test": "cross-env LOCAL_SERVER=1 NODE_ENV=test node scripts/start.js",
9+
"start:test": "cross-env LOCAL_SERVER=1 NODE_ENV=test SANDBOX_ONLY=true node scripts/start.js",
1010
"build": "NODE_ENV=production node scripts/build.js",
1111
"test": "jest --env=jsdom",
1212
"test:watch": "jest --watch --env=jsdom",

packages/app/src/app/components/Preview/DevTools/Console/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import ClearIcon from 'react-icons/lib/md/clear-all';
55

66
import CircularJSON from 'circular-json';
77

8-
import { evaluateInSandbox } from 'app/components/Preview';
9-
108
import Message from './Message';
119
import Input from './Input';
1210

@@ -135,7 +133,7 @@ class Console extends React.Component {
135133
this.addMessage('log', [command], 'command');
136134

137135
// TODO move everything of frames to store and this command too
138-
evaluateInSandbox(this.props.sandboxId, command);
136+
dispatch({ type: 'evaluate', command });
139137
};
140138

141139
render() {

packages/app/src/app/components/Preview/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import { generateFileFromSandbox } from 'common/templates/configuration/package-
1212
import Navigator from './Navigator';
1313
import { Container, StyledFrame } from './elements';
1414

15-
export function evaluateInSandbox(command: string) {
16-
dispatch({ type: 'evaluate', command });
17-
}
18-
1915
type Props = {
2016
onInitialized: (preview: BasePreview) => void, // eslint-disable-line no-use-before-define
2117
sandbox: Sandbox,
@@ -190,7 +186,10 @@ class BasePreview extends React.Component<Props, State> {
190186
this.handleRefresh();
191187
} else {
192188
if (!this.props.isInProjectView) {
193-
evaluateInSandbox(`history.pushState({}, null, '/')`);
189+
dispatch({
190+
type: 'evaluate',
191+
command: `history.pushState({}, null, '/')`,
192+
});
194193
}
195194

196195
const modulesObject = {};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as React from 'react';
2+
3+
import File from '../File';
4+
import ModuleList from '../ModuleList';
5+
6+
import { IFiles } from '../../../types';
7+
8+
export interface Props {
9+
prefixedPath: string;
10+
files: IFiles;
11+
selectFile: (path: string) => void;
12+
openedPath: string;
13+
depth: number;
14+
}
15+
16+
export default class Directory extends React.Component<Props> {
17+
state = {
18+
open: true,
19+
};
20+
21+
toggleOpen = () => {
22+
this.setState({ open: !this.state.open });
23+
};
24+
25+
render() {
26+
const { prefixedPath, files, selectFile, openedPath, depth } = this.props;
27+
28+
return (
29+
<div key={prefixedPath}>
30+
<File onClick={this.toggleOpen} path={prefixedPath + '/'} />
31+
{this.state.open && (
32+
<ModuleList
33+
prefixedPath={prefixedPath}
34+
files={files}
35+
selectFile={selectFile}
36+
openedPath={openedPath}
37+
depth={depth}
38+
/>
39+
)}
40+
</div>
41+
);
42+
}
43+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Directory from './Directory';
2+
3+
export default Directory;

packages/react-sandpack/src/components/FileExplorer/File/File.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import cn from '../../../utils/cn';
55

66
export interface Props {
77
path: string;
8-
selectFile: (path: string) => void;
8+
selectFile?: (path: string) => void;
99
active?: boolean;
10+
onClick?: (e: React.MouseEvent<HTMLDivElement>) => void;
1011
}
1112

1213
export default class File extends React.PureComponent<Props> {
1314
selectFile = () => {
14-
this.props.selectFile(this.props.path);
15+
if (this.props.selectFile) {
16+
this.props.selectFile(this.props.path);
17+
}
1518
};
1619

1720
render() {
@@ -25,7 +28,10 @@ export default class File extends React.PureComponent<Props> {
2528
});
2629

2730
return (
28-
<div onClick={this.selectFile} className={className}>
31+
<div
32+
onClick={this.props.selectFile ? this.selectFile : this.props.onClick}
33+
className={className}
34+
>
2935
{fileName}
3036
</div>
3137
);

packages/react-sandpack/src/components/FileExplorer/ModuleList/ModuleList.tsx

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,57 @@ import * as React from 'react';
22
import { IFiles, IFile } from '../../../types';
33

44
import File from '../File';
5+
import Directory from '../Directory';
56

67
export interface Props {
78
prefixedPath: string;
89
files: IFiles;
910
selectFile: (path: string) => void;
1011
openedPath: string;
12+
depth?: number;
1113
}
1214

1315
export default class ModuleList extends React.PureComponent<Props> {
14-
render() {
15-
const { openedPath } = this.props;
16+
render(): JSX.Element {
17+
const {
18+
depth = 0,
19+
openedPath,
20+
selectFile,
21+
prefixedPath,
22+
files,
23+
} = this.props;
1624

17-
const filesToShow: { path: string; code: string }[] = [];
18-
const directoriesToShow: { path: string }[] = [];
19-
const pathParts = this.props.prefixedPath.split('/');
25+
const filesToShow: { path: string }[] = [];
26+
const directoriesToShow: Set<string> = new Set();
27+
const pathParts = prefixedPath.split('/');
2028

21-
Object.keys(this.props.files).forEach(path => {
22-
if (path.startsWith(this.props.prefixedPath)) {
29+
Object.keys(files).forEach(path => {
30+
if (path.startsWith(prefixedPath)) {
2331
const filePathParts = path.split('/');
2432

2533
if (filePathParts.length === pathParts.length) {
2634
if (path.endsWith('/')) {
27-
directoriesToShow.push({ path });
35+
directoriesToShow.add(path);
2836
} else {
29-
filesToShow.push({ path, code: this.props.files[path].code });
37+
filesToShow.push({ path });
3038
}
39+
} else if (filePathParts.length === pathParts.length + 1) {
40+
filePathParts.pop();
41+
directoriesToShow.add(filePathParts.join('/') + '/');
3142
}
3243
}
3344
});
3445

3546
return (
36-
<div>
37-
{directoriesToShow.map(dir => (
38-
<File
39-
key={dir.path}
40-
selectFile={this.props.selectFile}
41-
path={dir.path}
47+
<div style={{ marginLeft: `${0.5 * depth}rem` }}>
48+
{Array.from(directoriesToShow).map(dir => (
49+
<Directory
50+
key={dir}
51+
prefixedPath={dir}
52+
files={files}
53+
selectFile={selectFile}
54+
openedPath={openedPath}
55+
depth={depth + 1}
4256
/>
4357
))}
4458

packages/react-sandpack/stories/InTheWild.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@ stories.addWithJSX('one file', () => (
5252
</SandpackProvider>
5353
));
5454

55+
stories.addWithJSX('subdirectories', () => (
56+
<SandpackProvider
57+
files={{
58+
'/index.js': {
59+
code: `document.body.innerHTML = \`<div>$\{require('uuid')()}</div>\``,
60+
},
61+
'/test/index.js': {
62+
code: 'module.exports = "test"',
63+
},
64+
}}
65+
dependencies={{ uuid: 'latest' }}
66+
entry="/index.js"
67+
skipEval
68+
>
69+
<div
70+
style={{
71+
display: 'flex',
72+
width: '100vw',
73+
height: '100vh',
74+
}}
75+
>
76+
<FileExplorer style={{ width: 300 }} />
77+
78+
<CodeEditor style={{ width: '100%', overflowX: 'hidden' }} />
79+
<TranspiledCodeView style={{ width: '100%', overflowX: 'hidden' }} />
80+
</div>
81+
</SandpackProvider>
82+
));
83+
5584
stories.addWithJSX('transpiled view', () => (
5685
<SandpackProvider
5786
files={{

0 commit comments

Comments
 (0)