Skip to content

Commit 9b48877

Browse files
author
Ives van Hoorne
committed
Playground improvements
1 parent ade8bbe commit 9b48877

File tree

6 files changed

+150
-18
lines changed

6 files changed

+150
-18
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"circular-json": "^0.4.0",
117117
"codemirror": "^5.27.4",
118118
"codesandbox-api": "^0.0.14",
119+
"codesandbox-import-utils": "^1.1.19",
119120
"color": "^0.11.4",
120121
"compare-versions": "^3.1.0",
121122
"css-modules-loader-core": "^1.1.0",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { getParameters } from 'codesandbox-import-utils/lib/api/define';
2+
3+
export default function createOverlay(modules) {
4+
const normalized = Object.keys(modules).reduce(
5+
(prev, next) => ({
6+
...prev,
7+
[next.replace('/', '')]: {
8+
content: modules[next].code,
9+
isBinary: false,
10+
},
11+
}),
12+
{}
13+
);
14+
15+
const parameters = getParameters({ files: normalized });
16+
17+
return new Promise(resolve => {
18+
const iframe = document.createElement('iframe');
19+
20+
iframe.setAttribute(
21+
'style',
22+
`transition: 0.3s ease background-color; position: fixed; bottom: 8px; right: 8px; height: 40px; width: 196px; background-color: rgba(0, 0, 0, 0.6); border-radius: 4px; border: 0; outline: 0; z-index: 214748366;`
23+
);
24+
iframe.setAttribute(
25+
'onmouseover',
26+
"this.style.backgroundColor='rgba(0, 0, 0, 0.7)';"
27+
);
28+
iframe.setAttribute(
29+
'onmouseout',
30+
"this.style.backgroundColor='rgba(0, 0, 0, 0.6)';"
31+
);
32+
33+
iframe.onload = () => {
34+
iframe.contentDocument.body.innerHTML = `
35+
<form
36+
action="https://codesandbox.io/api/v1/sandboxes/define"
37+
method="POST"
38+
target="_blank"
39+
style="cursor:pointer;"
40+
>
41+
<input
42+
type="hidden"
43+
name="parameters"
44+
value="${parameters}"
45+
/>
46+
<div style="display:flex;align-items:center" onclick="javascript:document.forms[0].submit();">
47+
<svg style="width:24px;height:24px;margin-right:8px;" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1024px"
48+
height="1024px" viewBox="0 0 1024 1024" enable-background="new 0 0 1024 1024" xml:space="preserve">
49+
<g id="Layer_1">
50+
<polyline
51+
fill="#FFFFFF"
52+
points="719.001,851 719.001,639.848 902,533.802 902,745.267 719.001,851"
53+
/>
54+
<polyline
55+
fill="#FFFFFF"
56+
points="302.082,643.438 122.167,539.135 122.167,747.741 302.082,852.573 302.082,643.438"
57+
/>
58+
<polyline
59+
fill="#FFFFFF"
60+
points="511.982,275.795 694.939,169.633 512.06,63 328.436,169.987 511.982,275.795"
61+
/>
62+
</g>
63+
<g id="Layer_2">
64+
<polyline
65+
fill="none"
66+
stroke="#FFFFFF"
67+
stroke-width="80"
68+
stroke-miterlimit="10"
69+
points="899,287.833 509,513 509,963"
70+
/>
71+
<line
72+
fill="none"
73+
stroke="#FFFFFF"
74+
stroke-width="80"
75+
stroke-miterlimit="10"
76+
x1="122.167"
77+
y1="289"
78+
x2="511.5"
79+
y2="513"
80+
/>
81+
<polygon
82+
fill="none"
83+
stroke="#FFFFFF"
84+
stroke-width="80"
85+
stroke-miterlimit="10"
86+
points="121,739.083 510.917,963.042 901,738.333 901,288 511,62 121,289"
87+
/>
88+
</g>
89+
</svg>
90+
<div style="font-size:.875rem; font-weight: 300; color: white; font-family: sans-serif">Open in CodeSandbox</div>
91+
</div>
92+
</form>
93+
`;
94+
95+
resolve();
96+
};
97+
98+
document.body.appendChild(iframe);
99+
});
100+
}

packages/app/src/sandbox/compile.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Manager from './eval/manager';
1010
import { resetScreen } from './status-screen';
1111

1212
import { inject, unmount } from './react-error-overlay/overlay';
13+
import createCodeSandboxOverlay from './codesandbox-overlay';
1314
import handleExternalResources from './external-resources';
1415

1516
import defaultBoilerplates from './boilerplates/default-boilerplates';
@@ -129,6 +130,7 @@ async function compile({
129130
isModuleView = false,
130131
template,
131132
entry,
133+
showOpenInCodeSandbox = true,
132134
}) {
133135
dispatch({
134136
type: 'start',
@@ -295,6 +297,10 @@ async function compile({
295297
initializeResizeListener();
296298
}
297299

300+
if (showOpenInCodeSandbox) {
301+
createCodeSandboxOverlay(modules);
302+
}
303+
298304
try {
299305
// Testing
300306
const ttt = Date.now();
@@ -352,6 +358,7 @@ type Arguments = {
352358
externalResources: Array<string>,
353359
hasActions: boolean,
354360
template: string,
361+
showOpenInCodeSandbox?: boolean,
355362
};
356363

357364
const tasks: Array<Arguments> = [];

packages/codesandbox-playground/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
# SandPacker
1+
# SandPack
22

33
A bundler that completely works in the browser, utilizing browser APIs.
44

55
## Why?
66

77
Online code playgrounds are getting more popular: they provide an easy way to play with code without installation. Until a year ago it was very hard to play with bigger web applications in the browser; there was no bundler that was comparable with local bundlers and worked in the browser.
88

9-
CodeSandbox came along, and still had a pretty basic bundler. However, as CodeSandbox got more popular its bundler got more advanced. Nowadays the bundler has comparable parity with Webpack, and it would be a shame if others couldn't use the functionality.
9+
CodeSandbox came along, and still had a pretty basic bundler. However, as CodeSandbox got more popular its bundler got more advanced. Nowadays the bundler has comparable feature parity with Webpack, and it would be a shame if others couldn't use the functionality.
1010

1111
This library acts as an interface with the bundler of CodeSandbox. It allows you to run any code on a web page, from Vue projects to React projects. With everything that CodeSandbox supports as well.
1212

1313
## So what can the bundler do?
1414

1515
This is a list of features that the bundler supports, the list may be outdated.
1616

17-
1. Hot Module Reloading API (`module.hot`).
17+
1. Hot Module Reloading API (`module.hot`)
1818
2. npm dependencies
19-
3. 17 transpilers
19+
3. Supports most common transpilers (vue, babel, typescript, css)
2020
4. Friendly error overlay
2121
5. Parallel transpiling
2222
6. On-demand transpiler loading
@@ -37,7 +37,9 @@ const m = new Manager(
3737
'#preview',
3838
{
3939
files: {
40-
'/index.js': `console.log(require('uuid'))`,
40+
'/index.js': {
41+
code: `console.log(require('uuid'))`,
42+
},
4143
},
4244
dependencies: {
4345
uuid: 'latest',
@@ -51,7 +53,9 @@ const m = new Manager(
5153
// how to hot update the preview.
5254
m.sendCode(
5355
{
54-
'/index.js': `console.log('other code')`,
56+
'/index.js': {
57+
code: `console.log('other code')`,
58+
},
5559
},
5660
{
5761
uuid: 'latest',
@@ -68,15 +72,24 @@ We included a React component you can use, the implementation is fairly simple.
6872
import Preview from 'codesandbox-playground/dist/components/Preview';
6973

7074
const files = {
71-
'/index.js': `
72-
import uuid from 'uuid';
75+
'/index.js': {
76+
code: `
77+
import preval from 'preval.macro';
7378
74-
console.log(uuid());
79+
console.log(preval);
7580
`,
81+
},
82+
'.babelrc': {
83+
// used for babel config
84+
code: `
85+
{}
86+
`,
87+
},
7688
};
7789

7890
const dependencies = {
79-
uuid: 'latest',
91+
'babel-macros': 'latest',
92+
'babel-plugin-preval': 'latest',
8093
};
8194

8295
export default () => (

packages/codesandbox-playground/src/utils/frame.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ export interface IDependencies {
1515
[depName: string]: string;
1616
}
1717

18+
export interface IOptions {
19+
showOpenInCodeSandbox?: boolean;
20+
template?: string;
21+
}
22+
1823
export function sendCode(
1924
frame: HTMLIFrameElement,
2025
files: IFiles,
2126
dependencies: IDependencies,
22-
entry: string
27+
entry: string,
28+
options: IOptions = {}
2329
) {
2430
const modules: IModules = Object.keys(files).reduce(
2531
(prev, next) => ({
@@ -33,11 +39,15 @@ export function sendCode(
3339
);
3440

3541
modules['/package.json'] = {
36-
code: JSON.stringify({
37-
name: 'run',
38-
main: entry,
39-
dependencies,
40-
}),
42+
code: JSON.stringify(
43+
{
44+
name: 'run',
45+
main: entry,
46+
dependencies,
47+
},
48+
null,
49+
2
50+
),
4151
path: '/package.json',
4252
};
4353

@@ -50,7 +60,8 @@ export function sendCode(
5060
entry: entry,
5161
dependencies: dependencies,
5262
externalResources: [],
53-
template: 'create-react-app',
63+
template: options.template || 'create-react-app',
64+
showOpenInCodeSandbox: options.showOpenInCodeSandbox || true,
5465
},
5566
'*'
5667
);

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3728,7 +3728,7 @@ codemirror@^5.27.4:
37283728
version "5.31.0"
37293729
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.31.0.tgz#ecf3d057eb74174147066bfc7c5f37b4c4e07df2"
37303730

3731-
codesandbox-import-utils@=1.1.19:
3731+
codesandbox-import-utils@=1.1.19, codesandbox-import-utils@^1.1.19:
37323732
version "1.1.19"
37333733
resolved "https://registry.yarnpkg.com/codesandbox-import-utils/-/codesandbox-import-utils-1.1.19.tgz#e9756c8f6b5df89e28131d73078420b629612342"
37343734
dependencies:

0 commit comments

Comments
 (0)