Skip to content

Commit 0756d71

Browse files
authored
fix: apply sandboxing changes to embed mode (#221)
1 parent 285f355 commit 0756d71

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

scripts/build-client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ async function main() {
9696

9797
const entries = ['src/index.html', 'src/embed.js', 'src/sandbox.html'];
9898

99-
if (process.env.NODE_ENV === 'development') {
100-
entries.push('src/embed.html');
101-
}
102-
10399
const parcel = await build({
104100
entries,
105101
dest: 'dist/client',
@@ -118,6 +114,10 @@ async function main() {
118114
copy('.well-known', join(dest, '.well-known')),
119115
]);
120116

117+
if (process.env.NODE_ENV === 'development') {
118+
copy('src/embed.html', join(dest, 'embed.html'));
119+
}
120+
121121
if (parcel.watching) {
122122
openInBrowser(`http://localhost:${parcel.config.serve.port}`);
123123
}

src/components/Embedded.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ const SUPPORTED_PANES = {
2323
result: true,
2424
};
2525

26+
const styles = {
27+
offscreen: {
28+
position: 'absolute',
29+
left: -300,
30+
width: 100,
31+
},
32+
};
33+
2634
// TODO: we should support readonly mode
2735
function Embedded() {
2836
const [{ markup, query, result }, dispatch] = usePlayground({
@@ -61,14 +69,27 @@ function Embedded() {
6169
<div
6270
className={`h-full overflow-hidden grid grid-flow-col gap-4 p-4 bg-white shadow rounded ${columnClass}`}
6371
>
72+
{/*the sandbox must always be rendered!*/}
73+
{!panes.includes('preview') && (
74+
<div style={styles.offscreen}>
75+
<Preview
76+
markup={markup}
77+
elements={result.elements}
78+
accessibleRoles={result.accessibleRoles}
79+
dispatch={dispatch}
80+
/>
81+
</div>
82+
)}
83+
6484
{panes.map((area) => {
6585
switch (area) {
6686
case 'preview':
6787
return (
6888
<Preview
6989
key={area}
7090
markup={markup}
71-
result={result}
91+
elements={result.elements}
92+
accessibleRoles={result.accessibleRoles}
7293
dispatch={dispatch}
7394
/>
7495
);
@@ -83,6 +104,7 @@ function Embedded() {
83104
query={query}
84105
result={result}
85106
dispatch={dispatch}
107+
variant="minimal"
86108
/>
87109
);
88110
case 'result':

src/components/Expandable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Expandable({ children, className, variant }) {
1818
>
1919
{expanded && (
2020
<div className="absolute bottom-0 -ml-4 h-full w-full overflow-hidden bg-inherit rounded-inherit">
21-
<Scrollable variant={variant} minHeight="100%" maxHeight="100%">
21+
<Scrollable variant={variant}>
2222
<div className="whitespace-pre-wrap p-4">
2323
{children} <div className="py-2 px-4">&nbsp;</div>
2424
</div>

src/lib/postMessage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function postMessage(target, action) {
88
source: 'testing-playground',
99
...action,
1010
},
11-
window.location.origin,
11+
target.origin,
1212
);
1313
}
1414

src/sandbox.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ const state = {
1616
};
1717

1818
function postMessage(action) {
19-
top.postMessage(
19+
parent.postMessage(
2020
{
2121
source: 'testing-playground-sandbox',
2222
...action,
2323
},
24-
top.location.origin,
24+
parent.location.origin,
2525
);
2626
}
2727

@@ -33,6 +33,7 @@ function runQuery(rootNode, query) {
3333
}
3434

3535
function setInnerHTML(node, html) {
36+
console.log('set html', { node, html });
3637
const doc = node.ownerDocument;
3738
node.innerHTML = html;
3839

@@ -123,7 +124,7 @@ function updateSandbox(rootNode, markup, query) {
123124
}
124125

125126
function onMessage({ source, data }) {
126-
if (source !== top || data.source !== 'testing-playground') {
127+
if (source !== parent || data.source !== 'testing-playground') {
127128
return;
128129
}
129130

0 commit comments

Comments
 (0)