Skip to content

Commit 59e21e9

Browse files
authored
Use module path for selecting current module (codesandbox#94)
1 parent 0768408 commit 59e21e9

File tree

11 files changed

+102
-63
lines changed

11 files changed

+102
-63
lines changed

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,34 @@ const Buttons = styled.div`
3232
`;
3333

3434
type Props = {
35-
title: string,
3635
path: string,
3736
saveComponent: ?() => void,
3837
prettify: ?Function,
3938
};
4039

41-
export default ({ path, title, saveComponent, prettify }: Props) => (
42-
<Container>
43-
<div>
44-
<Path>{path}</Path>
45-
{title}
46-
</div>
40+
export default ({ path, saveComponent, prettify }: Props) => {
41+
const pathParts = path.split('/');
42+
const fileName = pathParts[pathParts.length - 1];
43+
const pathName = pathParts.slice(0, pathParts.length - 1).join('/');
44+
return (
45+
<Container>
46+
<div>
47+
<Path>
48+
{pathName}/
49+
</Path>
50+
{fileName}
51+
</div>
4752

48-
<Buttons>
49-
<Tooltip position="bottom" title="Made possible by Prettier">
50-
<Button disabled={!prettify} onClick={prettify} small>
51-
Prettify
53+
<Buttons>
54+
<Tooltip position="bottom" title="Made possible by Prettier">
55+
<Button disabled={!prettify} onClick={prettify} small>
56+
Prettify
57+
</Button>
58+
</Tooltip>
59+
<Button disabled={!saveComponent} onClick={saveComponent} small>
60+
Save
5261
</Button>
53-
</Tooltip>
54-
<Button disabled={!saveComponent} onClick={saveComponent} small>
55-
Save
56-
</Button>
57-
</Buttons>
58-
</Container>
59-
);
62+
</Buttons>
63+
</Container>
64+
);
65+
};

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,20 +460,13 @@ export default class CodeEditor extends React.PureComponent {
460460
server: typeof CodeMirror.TernServer;
461461

462462
render() {
463-
const {
464-
title,
465-
canSave,
466-
onlyViewMode,
467-
modulePath,
468-
preferences,
469-
} = this.props;
463+
const { canSave, onlyViewMode, modulePath, preferences } = this.props;
470464

471465
return (
472466
<Container>
473467
<Header
474468
saveComponent={canSave && !onlyViewMode && this.handleSaveCode}
475469
prettify={!onlyViewMode && this.prettify}
476-
title={title}
477470
path={modulePath}
478471
/>
479472
<CodeContainer

src/app/pages/Sandbox/Editor/Content/Header/ShareView.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ModeIcons from 'app/components/sandbox/ModeIcons';
88
import {
99
findMainModule,
1010
modulesFromSandboxSelector,
11+
getModulePath,
1112
} from 'app/store/entities/sandboxes/modules/selectors';
1213
import { directoriesFromSandboxSelector } from 'app/store/entities/sandboxes/directories/selectors';
1314
import {
@@ -172,9 +173,11 @@ class ShareView extends React.PureComponent {
172173
setMixedView = () => this.setState({ showEditor: true, showPreview: true });
173174

174175
setDefaultModule = id => this.setState({ defaultModule: id });
176+
175177
clearDefaultModule = () => this.setState({ defaultModule: null });
176178

177179
getOptionsUrl = () => {
180+
const { modules, directories } = this.props;
178181
const {
179182
defaultModule,
180183
showEditor,
@@ -188,9 +191,10 @@ class ShareView extends React.PureComponent {
188191

189192
const options = {};
190193

191-
const mainModuleShortid = findMainModule(this.props.modules).shortid;
192-
if (defaultModule && defaultModule !== mainModuleShortid) {
193-
options.module = defaultModule;
194+
const mainModuleId = findMainModule(modules).id;
195+
if (defaultModule && defaultModule !== mainModuleId) {
196+
const modulePath = getModulePath(modules, directories, defaultModule);
197+
options.module = modulePath;
194198
}
195199

196200
if (showEditor && !showPreview) {
@@ -291,7 +295,7 @@ class ShareView extends React.PureComponent {
291295
} = this.state;
292296

293297
const defaultModule =
294-
this.state.defaultModule || findMainModule(modules).shortid;
298+
this.state.defaultModule || findMainModule(modules).id;
295299

296300
return (
297301
<Container>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class EditorPreview extends React.PureComponent {
139139

140140
const currentModule = findCurrentModule(
141141
modules,
142+
directories,
142143
currentModuleId,
143144
mainModule,
144145
);

src/app/pages/Sandbox/Editor/Workspace/Files/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Files extends React.PureComponent {
5454
const { currentModule: currentModuleId } = sandbox;
5555
const currentModule = findCurrentModule(
5656
modules,
57+
directories,
5758
currentModuleId,
5859
mainModule,
5960
);

src/app/store/entities/sandboxes/modules/selectors.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { Directory, Module } from 'common/types';
33
import { createSelector } from 'reselect';
44
import { values } from 'lodash';
5+
import resolveModule from 'sandbox/utils/resolve-module';
56

67
export const modulesSelector = state => state.entities.modules;
78

@@ -13,13 +14,26 @@ export const findMainModule = (modules: Module[]) =>
1314

1415
export const findCurrentModule = (
1516
modules: Module[],
16-
currentModuleId: string,
17+
directories: Directory[],
18+
modulePath: string,
1719
mainModule: Module,
18-
): Module =>
19-
modules.find(m => m.id === currentModuleId) ||
20-
modules.find(m => m.shortid === currentModuleId) || // deep-links requires this
21-
mainModule;
20+
): Module => {
21+
// cleanPath, encode and replace first /
22+
const cleanPath = decodeURIComponent(modulePath).replace('/', '');
23+
let foundModule = null;
24+
try {
25+
foundModule = resolveModule(cleanPath, modules, directories);
26+
} catch (e) {
27+
/* leave empty */
28+
}
2229

30+
return (
31+
foundModule ||
32+
modules.find(m => m.id === modulePath) ||
33+
modules.find(m => m.shortid === modulePath) || // deep-links requires this
34+
mainModule
35+
);
36+
};
2337
function findById(entities: Array<Module | Directory>, id: string) {
2438
return entities.find(e => e.id === id);
2539
}
@@ -43,7 +57,7 @@ export const getModulePath = (
4357
path = `/${directory.title}${path}`;
4458
directory = findByShortid(directories, directory.directoryShortid);
4559
}
46-
return path;
60+
return `${path}${module.title}`;
4761
};
4862

4963
/**

src/embed/App.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import styled from 'styled-components';
44
import { camelizeKeys } from 'humps';
55
import 'whatwg-fetch';
66

7-
import type { Sandbox } from 'common/types';
7+
import type { Sandbox, Module } from 'common/types';
88
import Centered from 'app/components/flex/Centered';
99
import Title from 'app/components/text/Title';
1010
import SubTitle from 'app/components/text/SubTitle';
@@ -13,6 +13,10 @@ import Header from './components/Header';
1313
import Content from './components/Content';
1414
import Sidebar from './components/Sidebar';
1515
import { getSandboxOptions } from '../common/url';
16+
import {
17+
findCurrentModule,
18+
findMainModule,
19+
} from 'app/store/entities/sandboxes/modules/selectors';
1620

1721
const Container = styled.div`
1822
display: flex;
@@ -106,16 +110,10 @@ export default class App extends React.PureComponent {
106110
.then(res => res.json())
107111
.then(camelizeKeys);
108112

109-
const currentModule =
110-
this.state.currentModule ||
111-
response.data.modules.find(
112-
m => m.title === 'index.js' && m.directoryShortid == null,
113-
).shortid;
114-
115113
document.title = response.data.title
116114
? `${response.data.title} - CodeSandbox`
117115
: 'Embed - CodeSandbox';
118-
this.setState({ sandbox: response.data, currentModule });
116+
this.setState({ sandbox: response.data });
119117
} catch (e) {
120118
this.setState({ notFound: true });
121119
}
@@ -144,6 +142,18 @@ export default class App extends React.PureComponent {
144142
setProjectView = (sandboxId: string, isOpen: boolean) =>
145143
this.setState({ isInProjectView: isOpen });
146144

145+
getCurrentModuleFromPath = () => {
146+
const { sandbox, currentModule: currentModulePath } = this.state;
147+
if (!sandbox) return null;
148+
149+
return findCurrentModule(
150+
sandbox.modules,
151+
sandbox.directories,
152+
currentModulePath,
153+
findMainModule(sandbox.modules),
154+
);
155+
};
156+
147157
content = () => {
148158
if (this.state.notFound) {
149159
return (
@@ -183,7 +193,7 @@ export default class App extends React.PureComponent {
183193
isInProjectView={isInProjectView}
184194
setProjectView={this.setProjectView}
185195
sandbox={this.state.sandbox}
186-
currentModule={this.state.currentModule}
196+
currentModule={this.getCurrentModuleFromPath().id}
187197
hideNavigation={this.state.hideNavigation}
188198
autoResize={this.state.autoResize}
189199
fontSize={this.state.fontSize}
@@ -199,7 +209,7 @@ export default class App extends React.PureComponent {
199209
{this.state.sandbox &&
200210
<Sidebar
201211
setCurrentModule={this.setCurrentModule}
202-
currentModule={this.state.currentModule}
212+
currentModule={this.getCurrentModuleFromPath().id}
203213
sandbox={this.state.sandbox}
204214
/>}
205215
<Moving sidebarOpen={this.state.sidebarOpen}>

src/embed/components/Content.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { getModulePath } from 'app/store/entities/sandboxes/modules/selectors';
88

99
import type { Sandbox, Module, ModuleError } from 'common/types';
1010
import fetchBundle from 'app/store/entities/sandboxes/bundler';
11+
import {
12+
findCurrentModule,
13+
findMainModule,
14+
} from '../../app/store/entities/sandboxes/modules/selectors';
1115

1216
const Container = styled.div`
1317
display: flex;
@@ -178,6 +182,7 @@ export default class Content extends React.PureComponent {
178182
sandbox,
179183
showEditor,
180184
showPreview,
185+
181186
isInProjectView,
182187
currentModule,
183188
hideNavigation,
@@ -188,11 +193,12 @@ export default class Content extends React.PureComponent {
188193
const alteredModules = this.getAlteredModules();
189194

190195
// $FlowIssue
191-
const mainModule: Module =
192-
alteredModules.find((m: Module) => m.shortid === currentModule) ||
193-
alteredModules.find(
194-
(m: Module) => m.title === 'index.js' && m.directoryShortid == null,
195-
);
196+
const mainModule: Module = findCurrentModule(
197+
sandbox.modules,
198+
sandbox.directories,
199+
currentModule,
200+
findMainModule(sandbox.modules),
201+
);
196202

197203
if (!mainModule) throw new Error('Cannot find main module');
198204

src/embed/components/File.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ import Entry from 'app/pages/Sandbox/Editor/Workspace/EntryContainer';
88

99
type Props = {
1010
id: string,
11+
shortid: string,
1112
title: string,
1213
type: 'module' | 'directory',
13-
setCurrentModule: (id: string) => void,
14+
setCurrentModule: (shortid: string, id: string) => void,
1415
depth: number,
1516
active?: boolean,
1617
alternative?: boolean,
1718
};
18-
const LeftOffset = styled.div`
19-
padding-left: ${props => props.depth}rem;
20-
`;
19+
const LeftOffset = styled.div`padding-left: ${props => props.depth}rem;`;
2120

2221
export default class File extends React.PureComponent {
2322
props: Props;
@@ -40,9 +39,9 @@ export default class File extends React.PureComponent {
4039
};
4140

4241
setCurrentModule = () => {
43-
const { id, setCurrentModule } = this.props;
42+
const { id, shortid, setCurrentModule } = this.props;
4443

45-
setCurrentModule(id);
44+
setCurrentModule(id, shortid);
4645
};
4746

4847
render() {
@@ -54,7 +53,9 @@ export default class File extends React.PureComponent {
5453
active={active}
5554
onClick={this.setCurrentModule}
5655
>
57-
<LeftOffset depth={depth}>{this.getIcon()} {title}</LeftOffset>
56+
<LeftOffset depth={depth}>
57+
{this.getIcon()} {title}
58+
</LeftOffset>
5859
</Entry>
5960
</div>
6061
);

src/embed/components/Files.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Props = {
1515
directoryId: string,
1616
depth: number,
1717
currentModule: string,
18-
setCurrentModule: (id: string) => any,
18+
setCurrentModule: (id: string, shortid: string) => any,
1919
};
2020

2121
const Files = ({
@@ -39,7 +39,8 @@ const Files = ({
3939
{sortBy(childrenDirectories, d => d.title).map(d =>
4040
<div key={d.shortid}>
4141
<File
42-
id={d.shortid}
42+
id={d.id}
43+
shortid={d.shortid}
4344
title={d.title}
4445
type="directory"
4546
depth={depth}
@@ -57,13 +58,14 @@ const Files = ({
5758
)}
5859
{sortBy(childrenModules, m => m.title).map(m =>
5960
<File
60-
id={m.shortid}
61+
id={m.id}
62+
shortid={m.shortid}
6163
title={m.title}
6264
key={m.shortid}
6365
type="module"
6466
depth={depth}
6567
setCurrentModule={setCurrentModule}
66-
active={m.shortid === currentModule}
68+
active={m.id === currentModule}
6769
alternative={m.title === 'index.js' && m.directoryShortid == null}
6870
/>,
6971
)}

0 commit comments

Comments
 (0)