Skip to content

Commit 7ab5ea0

Browse files
author
Ives van Hoorne
committed
Add more share options
1 parent 9dd83bb commit 7ab5ea0

File tree

5 files changed

+106
-35
lines changed

5 files changed

+106
-35
lines changed

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

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -205,38 +205,14 @@ export default class CodeEditor extends React.Component<Props, State> {
205205
fuzzySearchEnabled: false,
206206
};
207207

208-
syntaxWorker: Worker;
209-
lintWorker: Worker;
210-
typingsFetcherWorker: Worker;
208+
syntaxWorker: ?Worker;
209+
lintWorker: ?Worker;
210+
typingsFetcherWorker: ?Worker;
211211
sizeProbeInterval: number;
212212

213-
setupWorkers = () => {
214-
this.syntaxWorker = new SyntaxHighlightWorker();
215-
this.lintWorker = new LinterWorker();
213+
setupTypeWorker = () => {
216214
this.typingsFetcherWorker = new TypingsFetcherWorker();
217215

218-
this.lint = debounce(this.lint, 400);
219-
220-
this.syntaxWorker.addEventListener('message', event => {
221-
const { classifications, version } = event.data;
222-
223-
requestAnimationFrame(() => {
224-
if (version === this.editor.getModel().getVersionId()) {
225-
this.updateDecorations(classifications);
226-
}
227-
});
228-
});
229-
230-
this.lintWorker.addEventListener('message', event => {
231-
const { markers, version } = event.data;
232-
233-
requestAnimationFrame(() => {
234-
if (version === this.editor.getModel().getVersionId()) {
235-
this.updateLintWarnings(markers);
236-
}
237-
});
238-
});
239-
240216
this.typingsFetcherWorker.addEventListener('message', event => {
241217
const { path, typings } = event.data;
242218

@@ -267,6 +243,48 @@ export default class CodeEditor extends React.Component<Props, State> {
267243
});
268244
};
269245

246+
setupLintWorker = () => {
247+
this.lintWorker = new LinterWorker();
248+
249+
this.lintWorker.addEventListener('message', event => {
250+
const { markers, version } = event.data;
251+
252+
requestAnimationFrame(() => {
253+
if (version === this.editor.getModel().getVersionId()) {
254+
this.updateLintWarnings(markers);
255+
}
256+
});
257+
});
258+
259+
this.lint = debounce(this.lint, 400);
260+
};
261+
262+
setupSyntaxWorker = () => {
263+
this.syntaxWorker = new SyntaxHighlightWorker();
264+
265+
this.syntaxWorker.addEventListener('message', event => {
266+
const { classifications, version } = event.data;
267+
268+
requestAnimationFrame(() => {
269+
if (version === this.editor.getModel().getVersionId()) {
270+
this.updateDecorations(classifications);
271+
}
272+
});
273+
});
274+
};
275+
276+
setupWorkers = () => {
277+
this.setupSyntaxWorker();
278+
279+
if (this.props.preferences.lintEnabled) {
280+
this.setupLintWorker();
281+
}
282+
283+
if (this.props.preferences.autoDownloadTypes) {
284+
this.setupTypeWorker();
285+
}
286+
};
287+
270288
updateDecorations = async (classifications: Array<Object>) => {
271289
const decorations = classifications.map(classification => ({
272290
range: new this.monaco.Range(
@@ -507,11 +525,13 @@ export default class CodeEditor extends React.Component<Props, State> {
507525
lint = async (code: string, title: string, version: string) => {
508526
const mode = await this.getMode(title);
509527
if (mode === 'javascript') {
510-
this.lintWorker.postMessage({
511-
code,
512-
title,
513-
version,
514-
});
528+
if (this.lintWorker) {
529+
this.lintWorker.postMessage({
530+
code,
531+
title,
532+
version,
533+
});
534+
}
515535
}
516536
};
517537

@@ -554,7 +574,9 @@ export default class CodeEditor extends React.Component<Props, State> {
554574
monaco: this.monaco,
555575
};
556576

557-
this.setupWorkers();
577+
requestAnimationFrame(() => {
578+
this.setupWorkers();
579+
});
558580

559581
const hasNativeTypescript = this.hasNativeTypescript();
560582

@@ -651,7 +673,7 @@ export default class CodeEditor extends React.Component<Props, State> {
651673
};
652674

653675
fetchDependencyTypings = (dependencies: Object) => {
654-
if (this.props.preferences.autoDownloadTypes) {
676+
if (this.typingsFetcherWorker) {
655677
this.typingsFetcherWorker.postMessage({ dependencies });
656678
}
657679
};

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class ShareView extends React.PureComponent {
155155
isCurrentModuleView: false,
156156
fontSize: 14,
157157
initialPath: '',
158+
useCodeMirror: false,
159+
enableEslint: false,
158160
};
159161

160162
handleChange = e => this.setState({ message: e.target.value });
@@ -187,6 +189,8 @@ class ShareView extends React.PureComponent {
187189
isCurrentModuleView,
188190
fontSize,
189191
initialPath,
192+
enableEslint,
193+
useCodeMirror,
190194
} = this.state;
191195

192196
const options = {};
@@ -216,6 +220,14 @@ class ShareView extends React.PureComponent {
216220
options.moduleview = 1;
217221
}
218222

223+
if (enableEslint) {
224+
options.eslint = 1;
225+
}
226+
227+
if (useCodeMirror) {
228+
options.codemirror = 1;
229+
}
230+
219231
if (fontSize !== 14) {
220232
options.fontsize = fontSize;
221233
}
@@ -275,6 +287,14 @@ class ShareView extends React.PureComponent {
275287
this.setState({ hideNavigation });
276288
};
277289

290+
setUseCodeMirror = (useCodeMirror: boolean) => {
291+
this.setState({ useCodeMirror });
292+
};
293+
294+
setEnableEslint = (enableEslint: boolean) => {
295+
this.setState({ enableEslint });
296+
};
297+
278298
setIsCurrentModuleView = (isCurrentModuleView: boolean) => {
279299
this.setState({ isCurrentModuleView });
280300
};
@@ -292,6 +312,8 @@ class ShareView extends React.PureComponent {
292312
isCurrentModuleView,
293313
fontSize,
294314
initialPath,
315+
useCodeMirror,
316+
enableEslint,
295317
} = this.state;
296318

297319
const defaultModule =
@@ -328,6 +350,18 @@ class ShareView extends React.PureComponent {
328350
value={hideNavigation}
329351
setValue={this.setHideNavigation}
330352
/>
353+
<PaddedPreference
354+
title="Use CodeMirror instead of Monaco editor"
355+
type="boolean"
356+
value={useCodeMirror}
357+
setValue={this.setUseCodeMirror}
358+
/>
359+
<PaddedPreference
360+
title="Enable eslint (significantly higher bundle size)"
361+
type="boolean"
362+
value={enableEslint}
363+
setValue={this.setEnableEslint}
364+
/>
331365
<PaddedPreference
332366
title="Show current module view"
333367
type="boolean"

src/common/url.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export const getSandboxOptions = (url: string) => {
3131
result.hideNavigation = url.includes('hidenavigation=1');
3232
result.isInProjectView = !url.includes('moduleview=1');
3333
result.autoResize = url.includes('autoresize=1');
34+
result.useCodeMirror = url.includes('codemirror=1');
35+
result.enableEslint = url.includes('eslint=1');
3436

3537
return result;
3638
};

src/embed/App.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type State = {
5858
autoResize: boolean,
5959
hideNavigation: boolean,
6060
fontSize: number,
61+
enableEslint: boolean,
62+
useCodeMirror: boolean,
6163
};
6264

6365
export default class App extends React.PureComponent<{}, State> {
@@ -73,6 +75,8 @@ export default class App extends React.PureComponent<{}, State> {
7375
autoResize,
7476
hideNavigation,
7577
fontSize,
78+
enableEslint,
79+
useCodeMirror,
7680
} = getSandboxOptions(document.location.href);
7781

7882
this.state = {
@@ -87,6 +91,8 @@ export default class App extends React.PureComponent<{}, State> {
8791
sidebarOpen: false,
8892
autoResize,
8993
hideNavigation,
94+
enableEslint,
95+
useCodeMirror,
9096
};
9197
}
9298

@@ -204,6 +210,8 @@ export default class App extends React.PureComponent<{}, State> {
204210
fontSize={this.state.fontSize}
205211
initialPath={this.state.initialPath}
206212
setCurrentModule={this.setCurrentModule}
213+
useCodeMirror={this.state.useCodeMirror}
214+
enableEslint={this.state.enableEslint}
207215
/>
208216
</Container>
209217
</ThemeProvider>

src/embed/components/Content.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type Props = {
3939
initialPath: ?string,
4040
setCurrentModule: (moduleId: string) => void,
4141
setProjectView: (id: string, isInProjectView: boolean) => any,
42+
useCodeMirror: boolean,
43+
enableEslint: boolean,
44+
isInProjectView: boolean,
4245
};
4346

4447
type State = {
@@ -159,6 +162,8 @@ export default class Content extends React.PureComponent<Props, State> {
159162
...this.preferences,
160163
fontSize: this.props.fontSize,
161164
autoDownloadTypes: true,
165+
lintEnabled: this.props.enableEslint,
166+
codeMirror: this.props.useCodeMirror,
162167
});
163168

164169
setCurrentModule = (_, moduleId) => {

0 commit comments

Comments
 (0)