Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Preview uploaded files in the editor #2232

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions client/modules/IDE/components/AssetPreview.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import mime from 'mime';
import PropTypes from 'prop-types';
import React from 'react';
import styled from 'styled-components';

const Audio = styled.audio`
width: 90%;
margin: 30px 5%;
`;

const Image = styled.img`
max-width: 100%;
height: auto;
`;

const Video = styled.video`
max-width: 100%;
height: auto;
`;

function AssetPreview({ url, name }) {
const contentType = mime.getType(url);
const type = contentType?.split('/')[0];

switch (type) {
case 'image':
return <Image src={url} alt={`Preview of file ${name}`} />;
case 'audio':
// eslint-disable-next-line jsx-a11y/media-has-caption
return <Audio src={url} controls preload="metadata" />;
case 'video':
return (
// eslint-disable-next-line jsx-a11y/media-has-caption
<Video controls preload="metadata">
<source src={url} type={contentType} />
</Video>
);
default:
return null;
}
}

AssetPreview.propTypes = {
url: PropTypes.string.isRequired,
name: PropTypes.string.isRequired
};

export default AssetPreview;
4 changes: 4 additions & 0 deletions client/modules/IDE/components/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import '../../../utils/htmlmixed';
import '../../../utils/p5-javascript';
import Timer from '../components/Timer';
import EditorAccessibility from '../components/EditorAccessibility';
import AssetPreview from './AssetPreview';
import { metaKey } from '../../../utils/metaKey';
import './show-hint';
import * as hinter from '../../../utils/p5-hinter';
Expand Down Expand Up @@ -532,6 +533,9 @@ class Editor extends React.Component {
}}
className={editorHolderClass}
/>
{this.props.file.url ? (
<AssetPreview url={this.props.file.url} name={this.props.file.name} />
) : null}
<EditorAccessibility lintMessages={this.props.lintMessages} />
</section>
);
Expand Down
Loading