Skip to content

Commit 2412947

Browse files
author
Ives van Hoorne
committed
Tweaks
1 parent 8b102dc commit 2412947

File tree

10 files changed

+60
-24
lines changed

10 files changed

+60
-24
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React from 'react';
33
import CodeMirror from 'codemirror';
44
import styled from 'styled-components';
5+
import { debounce } from 'lodash';
56

67
import { getCodeMirror } from 'app/utils/codemirror';
78

@@ -70,6 +71,12 @@ const handleError = (cm, currentError, nextError, nextCode, nextId) => {
7071
export default class CodeEditor extends React.PureComponent {
7172
props: Props;
7273

74+
constructor(props) {
75+
super(props);
76+
77+
this.handleChange = debounce(this.handleChange, 200);
78+
}
79+
7380
shouldComponentUpdate(nextProps: Props) {
7481
return nextProps.id !== this.props.id ||
7582
nextProps.error !== this.props.error ||
@@ -242,13 +249,20 @@ export default class CodeEditor extends React.PureComponent {
242249
}
243250
};
244251

252+
getCode = () => {
253+
return this.codemirror.getValue();
254+
};
255+
245256
prettify = async () => {
246-
const { id, code } = this.props;
257+
const { id } = this.props;
258+
const code = this.getCode();
247259
try {
248260
const prettier = await System.import('prettier');
249261
const newCode = prettier.format(code);
250-
this.props.changeCode(id, newCode);
251-
this.updateCodeMirrorCode(newCode);
262+
if (newCode !== code) {
263+
this.props.changeCode(id, newCode);
264+
this.updateCodeMirrorCode(newCode);
265+
}
252266
} catch (e) {}
253267
};
254268

@@ -257,7 +271,8 @@ export default class CodeEditor extends React.PureComponent {
257271
if (preferences.prettifyOnSaveEnabled) {
258272
await this.prettify();
259273
}
260-
274+
const { id } = this.props;
275+
this.props.changeCode(id, this.getCode());
261276
saveCode();
262277
};
263278

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import styled from 'styled-components';
44

55
import { debounce } from 'lodash';
6+
67
import type { Preferences } from 'app/store/preferences/reducer';
78

89
import type { Module } from 'app/store/entities/sandboxes/modules/entity';
@@ -65,7 +66,7 @@ export default class Preview extends React.PureComponent {
6566
url: null,
6667
};
6768

68-
this.executeCode = debounce(this.executeCode, 500);
69+
this.executeCode = debounce(this.executeCode, 400);
6970
}
7071

7172
fetchBundle = () => {
@@ -99,8 +100,16 @@ export default class Preview extends React.PureComponent {
99100
return;
100101
}
101102

103+
if (prevProps.module.isNotSynced && !this.props.module.isNotSynced) {
104+
// After save
105+
this.executeCodeImmediately();
106+
return;
107+
}
108+
102109
if (
103-
prevProps.module.code !== this.props.module.code &&
110+
(prevProps.module.code !== this.props.module.code ||
111+
prevProps.modules !== this.props.modules ||
112+
prevProps.directories !== this.props.directories) &&
104113
this.state.frameInitialized
105114
) {
106115
if (this.props.preferences.livePreviewEnabled) {
@@ -129,7 +138,6 @@ export default class Preview extends React.PureComponent {
129138
const { error } = e.data;
130139
this.setError(error);
131140
} else if (type === 'success') {
132-
// To reset the debounce, but still quickly remove errors
133141
this.setError(null);
134142
} else if (type === 'urlchange') {
135143
const url = e.data.url.replace('/', '');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Preferences extends React.PureComponent {
6161
title="Live Preview"
6262
enabled={preferences.livePreviewEnabled}
6363
onClick={preferencesActions.setLivePreview}
64-
tooltip="The alternative is to press the refresh button"
64+
tooltip="Only update on save"
6565
/>
6666
</PreferenceContainer>
6767
</Container>

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,38 @@ function moduleReducer(module, action) {
2323
}
2424
}
2525

26+
function shouldUpdate(module, action) {
27+
switch (action.type) {
28+
case SET_MODULE_ERROR:
29+
return (action.error == null && module.error) ||
30+
(action.error != null && module.error == null) ||
31+
(action.error != null &&
32+
module.error != null &&
33+
(action.error.line !== module.error.line ||
34+
action.error.title !== module.error.title ||
35+
action.error.type !== module.error.type));
36+
37+
default:
38+
return true;
39+
}
40+
}
41+
2642
export default function reducer(state: {}, action) {
2743
switch (action.type) {
2844
case RENAME_MODULE:
2945
case MOVE_MODULE:
3046
case SET_CODE:
3147
case SET_MODULE_SYNCED:
32-
case SET_MODULE_ERROR:
33-
if (state[action.id]) {
48+
case SET_MODULE_ERROR: {
49+
const module = state[action.id];
50+
if (module && shouldUpdate(module, action)) {
3451
return {
3552
...state,
36-
[action.id]: moduleReducer(state[action.id], action),
53+
[action.id]: moduleReducer(module, action),
3754
};
3855
}
3956
return state;
57+
}
4058
default:
4159
return state;
4260
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@ import type { Directory } from '../directories/entity';
44
export const validateTitle = (
55
id: string,
66
title: string,
7-
siblings: Array<Module | Directory>,
7+
siblings: Array<Module | Directory>
88
) => {
99
if (title.length === 0) return 'title cannot be empty';
1010
if (/^[09azAZ\_.]+$/.test(title)) {
1111
// It has whitespaces
1212
return 'Title cannot have whitespaces or special characters';
1313
}
14-
const siblingTitles: Array<string> = siblings
15-
.filter(x => x != null)
16-
.filter(m => m.id !== id)
17-
.map(m => m.title);
18-
if (siblingTitles.indexOf(title) > 1) {
19-
return 'There is already a module with the same title in this scope';
20-
}
2114

2215
if (title.length > 32) {
2316
return "The title can't be more than 32 characters long";

src/app/utils/codemirror.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const getCodeMirror = (el, doc) => {
3232
font-size: 14px;
3333
font-weight: 500;
3434
}
35-
.cm-s-oceanic div.CodeMirror-selected { background: ${theme.background()}; }
35+
.cm-s-oceanic div.CodeMirror-selected { background: #374140; }
3636
.cm-s-oceanic .CodeMirror-line::selection, .cm-s-oceanic .CodeMirror-line > span::selection, .cm-s-oceanic .CodeMirror-line > span > span::selection { background: #65737E; }
3737
.cm-s-oceanic .CodeMirror-line::-moz-selection, .cm-s-oceanic .CodeMirror-line > span::-moz-selection, .cm-s-oceanic .CodeMirror-line > span > span::-moz-selection { background: #65737E; }
3838
.cm-s-oceanic .CodeMirror-gutters {

src/homepage/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ <h3>CodeSandbox is an online editor with a focus on creating, sharing and import
104104
<h3>Example Projects</h3>
105105

106106
<div class="projects">
107-
<a href="/s/9G30GVB4" class="project">
107+
<a href="/s/r0wXp0Njw" class="project">
108108
React Router
109109
</a>
110-
<a href="/s/r06mAWWWk" class="project">
110+
<a href="/s/rkmNRByE4" class="project">
111111
Styled Components
112112
</a>
113-
<a href="/s/R95LjzwE" class="project">
113+
<a href="/s/lOmyoxQw1" class="project">
114114
Redux
115115
</a>
116116
<a href="/s/0R9pxQv3L" class="project">

src/sandbox/errors/dependency-not-found-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export default class DependencyNotFoundError extends Error {
77
}
88
type = 'dependency-not-found';
99
severity = 'error';
10+
line = -1;
1011
}

src/sandbox/errors/no-dom-change-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export default class NoDomChangeError extends Error {
1010

1111
type = 'no-dom-change';
1212
severity = 'warning';
13+
line = -1;
1314
}

src/sandbox/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function compile(message) {
115115
deleteCache(changedModule);
116116

117117
const evalled = evalModule(module, modules, directories, manifest);
118-
const domChanged = document.body.innerHTML !== '';
118+
const domChanged = document.body.innerHTML !== '<div id="root"></div>';
119119

120120
if (!domChanged) {
121121
const isReact = module.code.includes('react');

0 commit comments

Comments
 (0)