Skip to content

Commit 7f1dad6

Browse files
authored
feat: improve embed experience (#286)
1 parent d3f0fc8 commit 7f1dad6

File tree

6 files changed

+68
-36
lines changed

6 files changed

+68
-36
lines changed

src/components/Embed.js

+59-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import Input from './Input';
33
import CopyButton from './CopyButton';
44
import Embedded from './Embedded';
5-
import { XIcon } from '@primer/octicons-react';
5+
import { SyncIcon, XIcon } from '@primer/octicons-react';
66

77
function TabButton({ children, active, onClick, disabled }) {
88
return (
@@ -24,30 +24,41 @@ function TabButton({ children, active, onClick, disabled }) {
2424

2525
const possiblePanes = ['markup', 'preview', 'query', 'result'];
2626

27-
function Embed({ dirty, gistId, gistVersion }) {
28-
const [panes, setPanes] = useState(['preview', 'result']);
27+
const styles = {
28+
section: { width: 850 },
29+
frame: { width: 850, height: 375 },
30+
};
31+
32+
// TODO: make the preview frame height match the end result, and let
33+
// the user modify the frame height
34+
function Embed({ dispatch, dirty, gistId, gistVersion }) {
35+
useEffect(() => {
36+
if (!dirty) {
37+
return;
38+
}
2939

30-
const width = 850;
31-
const height = 300;
40+
dispatch({ type: 'SAVE' });
41+
}, [dirty, gistId, dispatch]);
42+
43+
const [panes, setPanes] = useState(['preview', 'result']);
3244

3345
const embedUrl =
3446
[location.origin, 'embed', gistId, gistVersion].filter(Boolean).join('/') +
3547
`?panes=${panes.join(',')}`;
3648

37-
const embedCode = `<iframe src="${embedUrl}" height="${height}" width="100%" scrolling="no" frameBorder="0" allowTransparency="true" title="Testing Playground" style="overflow: hidden; display: block; width: 100%"></iframe>`;
49+
const embedCode = `<iframe src="${embedUrl}" height="450" width="100%" scrolling="no" frameBorder="0" allowTransparency="true" title="Testing Playground" style="overflow: hidden; display: block; width: 100%"></iframe>`;
3850
const canAddPane = panes.length < 3;
3951

52+
const loader = (
53+
<div className="flex space-x-4 items-center border rounded w-full py-2 px-3 bg-white text-gray-800 leading-tight">
54+
<SyncIcon size={12} className="spinner" />
55+
<span>one sec...</span>
56+
</div>
57+
);
58+
4059
return (
4160
<div className="settings text-sm pb-2">
4261
<div className="space-y-6">
43-
{dirty && (
44-
<section className="bg-blue-100 p-2 text-xs rounded my-2 text-blue-800">
45-
Please note that this playground has
46-
<strong> unsaved changes </strong>. The embed
47-
<strong> will not include </strong> your latest changes!
48-
</section>
49-
)}
50-
5162
<section className="flex flex-col space-y-4">
5263
<div className="flex items-center justify-between">
5364
<h3 className="text-sm font-bold">Configure</h3>
@@ -98,35 +109,51 @@ function Embed({ dirty, gistId, gistVersion }) {
98109
))}
99110
</div>
100111

101-
<div style={{ width, height }}>
102-
<Embedded
103-
panes={panes}
104-
gistId={gistId}
105-
gistVersion={gistVersion}
106-
/>
112+
<div style={styles.frame}>
113+
{dirty ? null : (
114+
<Embedded
115+
panes={panes}
116+
gistId={gistId}
117+
gistVersion={gistVersion}
118+
height={styles.frame.height}
119+
/>
120+
)}
107121
</div>
108122
</div>
109123
</section>
110124

111-
<section className="flex flex-col space-y-4" style={{ width }}>
125+
<section className="flex flex-col space-y-4" style={styles.section}>
112126
<h3 className="text-sm font-bold">Copy & Paste</h3>
113127

114128
<label className="text-xs">
115129
embed link:
116-
<div className="flex space-x-4">
117-
<Input value={embedUrl} onChange={() => {}} readOnly name="url" />
118-
<CopyButton text={embedUrl} />
119-
</div>
130+
{dirty ? (
131+
loader
132+
) : (
133+
<div className="flex space-x-4">
134+
<Input
135+
value={embedUrl}
136+
onChange={() => {}}
137+
readOnly
138+
name="url"
139+
/>
140+
<CopyButton text={embedUrl} />
141+
</div>
142+
)}
120143
</label>
121144

122145
<label className="text-xs">
123146
embed code:
124-
<div className="w-full flex space-x-4">
125-
<code className="p-4 rounded bg-gray-200 text-gray-800 font-mono text-xs">
126-
{embedCode}
127-
</code>
128-
<CopyButton text={embedCode} />
129-
</div>
147+
{dirty ? (
148+
loader
149+
) : (
150+
<div className="w-full flex space-x-4">
151+
<code className="p-4 rounded bg-gray-200 text-gray-800 font-mono text-xs">
152+
{embedCode}
153+
</code>
154+
<CopyButton text={embedCode} />
155+
</div>
156+
)}
130157
</label>
131158
</section>
132159
</div>

src/components/Embedded.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ function Embedded(props) {
3333
});
3434
const { markup, query, result, status } = state;
3535
const isLoading = status === 'loading';
36+
// props.height because it describes better, params.maxheight because oembed
37+
const height = props.height || params.maxheight || params.height;
3638

3739
const location = useLocation();
3840
const searchParams = queryString.parse(location.search);
@@ -71,7 +73,10 @@ function Embedded(props) {
7173
useParentMessaging(dispatch);
7274

7375
return (
74-
<div className="relative w-full h-full">
76+
<div
77+
className="relative w-full h-screen"
78+
style={height ? { height } : undefined}
79+
>
7580
<Loader loading={isLoading} />
7681
<div
7782
className={[

src/components/Header.js

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function Header({
117117
<ModalContents>
118118
<Embed
119119
dirty={dirty}
120+
dispatch={dispatch}
120121
gistId={gistId}
121122
gistVersion={gistVersion}
122123
/>

src/components/Share.js

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function Share({ dirty, dispatch, gistId, gistVersion }) {
1818
? [location.origin, 'gist', gistId, gistVersion].filter(Boolean).join('/')
1919
: location.href;
2020

21-
console.log('share url');
2221
return (
2322
<div className="settings text-sm pb-2">
2423
<div>

src/lambda/oembed/oembed.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function handler(event, context, callback) {
3434
);
3535
}
3636

37-
const { url, referrer, maxwidth = 900, maxheight = 300 } = params;
37+
const { url, referrer, maxwidth = 900, maxheight = 450 } = params;
3838

3939
callback(null, {
4040
statusCode: 200,

src/lambda/server/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function handler(event, context, callback) {
3535

3636
const oembedLink = [
3737
`<link rel="alternate" type="application/json+oembed" href="${host}/oembed?${oembedSearch}" title="Testing Playground" />`,
38-
`<link rel="iframely player" type="text/html" href="${frameSrc}" media="height: 300" />`,
38+
`<link rel="iframely player" type="text/html" href="${frameSrc}" media="height: 450" />`,
3939
].join('');
4040

4141
let body = indexHtml.replace(

0 commit comments

Comments
 (0)