Skip to content

Commit 82c7b16

Browse files
fix: make default oembed panes equal to embed modal (#292)
* fix: Bring embed panes in line with the embed in the GUI * chore: change default panes to something more useful * chore: change default panes to something more useful * revert: appending `?...` can corrupt the url * feat: provide default panes to oembed frame Co-authored-by: Stephan Meijer <[email protected]>
1 parent 0dd614c commit 82c7b16

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

Diff for: src/components/Embed.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import CopyButton from './CopyButton';
44
import Embedded from './Embedded';
55
import { SyncIcon, XIcon } from '@primer/octicons-react';
66

7+
import { defaultPanes } from '../constants';
8+
79
function TabButton({ children, active, onClick, disabled }) {
810
return (
911
<button
@@ -40,7 +42,7 @@ function Embed({ dispatch, dirty, gistId, gistVersion }) {
4042
dispatch({ type: 'SAVE' });
4143
}, [dirty, gistId, dispatch]);
4244

43-
const [panes, setPanes] = useState(['preview', 'result']);
45+
const [panes, setPanes] = useState(defaultPanes);
4446

4547
const embedUrl =
4648
[location.origin, 'embed', gistId, gistVersion].filter(Boolean).join('/') +

Diff for: src/components/Embedded.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import useParentMessaging from '../hooks/useParentMessaging';
99
import usePlayground from '../hooks/usePlayground';
1010
import Loader from './Loader';
1111

12+
import { defaultPanes } from '../constants';
13+
1214
const SUPPORTED_PANES = {
1315
markup: true,
1416
preview: true,
@@ -46,7 +48,7 @@ function Embedded(props) {
4648
.split(',')
4749
.map((x) => x.trim())
4850
.filter((x) => SUPPORTED_PANES[x])
49-
: ['query', 'preview'];
51+
: defaultPanes;
5052

5153
// TODO: we should add tabs to handle > 2 panes
5254
const areaCount = panes.length;

Diff for: src/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,5 @@ export const links = {
113113
url: 'https://testing-library.com/docs/guide-which-query',
114114
},
115115
};
116+
117+
export const defaultPanes = ['query', 'preview'];

Diff for: src/lambda/oembed/oembed.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function getHostname(event, context) {
1616
return JSON.parse(Buffer.from(netlify, 'base64').toString('utf-8')).site_url;
1717
}
1818

19+
const PROVIDER = 'testing-playground.com';
1920
const allowedPathsRegexp = new RegExp(/^\/(gist|embed)\/.*/);
2021

2122
function handler(event, context, callback) {
@@ -38,22 +39,28 @@ function handler(event, context, callback) {
3839
);
3940
}
4041

41-
const { hostname, pathname } = new URL(params.url);
42+
const url = new URL(params.url);
4243

4344
// verify if the url is supported, basically we only allow localhost if we're
4445
// running at localhost, and testing-playground.com as host. And either no
4546
// path or /gist and /embed paths.
4647
if (
47-
(!host.includes(hostname) && hostname !== 'testing-playground.com') ||
48-
(pathname && !allowedPathsRegexp.test(pathname))
48+
(!host.includes(url.hostname) && url.hostname !== PROVIDER) ||
49+
(url.pathname !== '/' && !allowedPathsRegexp.test(url.pathname))
4950
) {
5051
return callback(null, incorrectParams('unsupported url provided :/'));
5152
}
5253

53-
// map /gist urls to /embed
54-
const url = pathname.startsWith('/gist/')
55-
? params.url.replace('/gist/', '/embed/')
56-
: params.url;
54+
// map / and /gist to /embed
55+
url.pathname =
56+
url.pathname === '/'
57+
? '/embed'
58+
: url.pathname.replace(/^\/gist\//, '/embed/');
59+
60+
// set default panes if no panes are provided, KEEP IN SYNC WITH /constants#defaultPanes !
61+
if (!url.searchParams.has('panes')) {
62+
url.searchParams.set('panes', 'query,preview');
63+
}
5764

5865
callback(null, {
5966
statusCode: 200,
@@ -63,7 +70,7 @@ function handler(event, context, callback) {
6370
type: 'rich',
6471
success: true,
6572

66-
provider_name: 'testing-playground.com',
73+
provider_name: PROVIDER,
6774
provider_url: host,
6875

6976
html: `<iframe src="${url}" height="${maxheight}" width="${maxwidth}" scrolling="no" frameBorder="0" allowTransparency="true" title="Testing Playground" style="overflow: hidden; display: block; width: 100%"></iframe>`,

0 commit comments

Comments
 (0)