Skip to content

Commit 4dacee5

Browse files
committed
Preview uploaded files in the editor.
1 parent 7212f76 commit 4dacee5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import mime from 'mime';
2+
import PropTypes from 'prop-types';
3+
import React from 'react';
4+
import styled from 'styled-components';
5+
6+
const Audio = styled.audio`
7+
width: 90%;
8+
margin: 30px 5%;
9+
`;
10+
11+
const Image = styled.img`
12+
max-width: 100%;
13+
height: auto;
14+
`;
15+
16+
const Video = styled.video`
17+
max-width: 100%;
18+
height: auto;
19+
`;
20+
21+
function AssetPreview({ url, name }) {
22+
const contentType = mime.getType(url);
23+
const type = contentType?.split('/')[0];
24+
25+
switch (type) {
26+
case 'image':
27+
return <Image src={url} alt={`Preview of file ${name}`} />;
28+
case 'audio':
29+
// eslint-disable-next-line jsx-a11y/media-has-caption
30+
return <Audio src={url} controls preload="metadata" />;
31+
case 'video':
32+
return (
33+
// eslint-disable-next-line jsx-a11y/media-has-caption
34+
<Video controls preload="metadata">
35+
<source src={url} type={contentType} />
36+
</Video>
37+
);
38+
default:
39+
return null;
40+
}
41+
}
42+
43+
AssetPreview.propTypes = {
44+
url: PropTypes.string.isRequired,
45+
name: PropTypes.string.isRequired
46+
};
47+
48+
export default AssetPreview;

client/modules/IDE/components/Editor.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import '../../../utils/htmlmixed';
4242
import '../../../utils/p5-javascript';
4343
import Timer from '../components/Timer';
4444
import EditorAccessibility from '../components/EditorAccessibility';
45+
import AssetPreview from './AssetPreview';
4546
import { metaKey } from '../../../utils/metaKey';
4647

4748
import '../../../utils/codemirror-search';
@@ -429,6 +430,9 @@ class Editor extends React.Component {
429430
}}
430431
className={editorHolderClass}
431432
/>
433+
{this.props.file.url ? (
434+
<AssetPreview url={this.props.file.url} name={this.props.file.name} />
435+
) : null}
432436
<EditorAccessibility lintMessages={this.props.lintMessages} />
433437
</section>
434438
);

0 commit comments

Comments
 (0)