spee.ch/react/containers/PublishThumbnailInput/view.jsx

130 lines
4.1 KiB
React
Raw Normal View History

2018-01-04 23:14:03 +01:00
import React from 'react';
2018-02-06 03:14:12 +01:00
const ThumbnailPreview = ({dataUrl}) => {
2018-02-06 05:07:22 +01:00
const divStyle = {
2018-02-06 03:14:12 +01:00
width : '30%',
2018-02-06 05:07:22 +01:00
margin : '1%',
2018-02-06 03:14:12 +01:00
display: 'inline-block',
2018-02-06 05:07:22 +01:00
border : 'solid 1px black',
}
const imageStyle = {
width : '100%',
display: 'block',
2018-02-06 03:14:12 +01:00
}
return (
2018-02-06 05:07:22 +01:00
<div style={divStyle}>
2018-02-06 03:14:12 +01:00
{ dataUrl ? (
2018-02-06 05:07:22 +01:00
<img style={imageStyle} src={dataUrl} alt='image preview here' />
2018-02-06 03:14:12 +01:00
) : (
<p>loading...</p>
2018-02-28 00:30:20 +01:00
)}
2018-02-06 03:14:12 +01:00
</div>
);
}
2018-01-17 19:49:57 +01:00
class PublishThumbnailInput extends React.Component {
2018-01-11 02:41:17 +01:00
constructor (props) {
super(props);
this.state = {
videoPreviewSrc: null,
2018-01-17 21:54:26 +01:00
thumbnailError : null,
thumbnailInput : '',
2018-01-11 02:41:17 +01:00
}
2018-01-17 21:54:26 +01:00
this.handleInput = this.handleInput.bind(this);
2018-01-11 02:41:17 +01:00
this.updateVideoThumb = this.updateVideoThumb.bind(this);
}
2018-02-06 03:14:12 +01:00
componentDidMount () {
2018-02-06 05:07:22 +01:00
this.setClaimAndThumbailUrl(this.props.publishClaim);
this.previewThumbnails(this.props.publishFile);
2018-02-06 03:14:12 +01:00
}
componentWillReceiveProps (nextProps) {
2018-02-06 05:07:22 +01:00
// if (nextProps.publishFile !== this.publishFile) {
// this.createThumbnails(nextProps.publishFile);
// }
2018-02-06 03:14:12 +01:00
if (nextProps.publishClaim !== this.props.publishClaim) {
console.log(nextProps.publishClaim, this.props.publishClaim);
2018-02-06 05:07:22 +01:00
this.setClaimAndThumbailUrl(nextProps.publishClaim);
2018-02-06 03:14:12 +01:00
}
2018-01-17 21:54:26 +01:00
}
2018-02-06 05:07:22 +01:00
previewThumbnails (videoFile) {
console.log('video file:', videoFile);
this.loadFileAndReturnThumbnails(videoFile)
.then((thumbnail) => {
console.log('thumbs:', thumbnail);
this.selectVideoThumb(thumbnail);
this.setPossibleThumbnailFiles(thumbnail, thumbnail, thumbnail);
2018-02-06 03:14:12 +01:00
})
.catch(error => {
2018-02-06 05:07:22 +01:00
console.log('error:', error);
2018-02-06 03:14:12 +01:00
this.setState({error: error.message});
});
2018-01-11 02:41:17 +01:00
}
2018-02-06 05:07:22 +01:00
loadFileAndReturnThumbnails (file) {
2018-01-17 21:54:26 +01:00
return new Promise((resolve, reject) => {
2018-02-06 03:14:12 +01:00
var fileReader = new FileReader();
fileReader.onload = () => {
2018-02-06 05:07:22 +01:00
console.log('file loaded');
2018-02-06 03:14:12 +01:00
const blob = new Blob([fileReader.result], {type: file.type});
const url = URL.createObjectURL(blob);
let video = document.createElement('video');
2018-02-06 05:07:22 +01:00
const snapShot = (time) => {
console.log('creating thubmnail @', time);
// video.currentTime = time;
2018-02-06 03:14:12 +01:00
let canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
const imageDataUrl = canvas.toDataURL();
const success = imageDataUrl.length > 1000;
2018-02-06 05:07:22 +01:00
if (success) {
return imageDataUrl;
2018-02-06 03:14:12 +01:00
}
2018-02-06 05:07:22 +01:00
return success;
}
const loadedata = () => {
console.log('loadeddata');
console.log('readyState', video.readyState);
const duration = video.duration;
console.log('readyState', duration);
const thumb = snapShot(duration / 2);
resolve(thumb);
}
video.addEventListener('loadeddata', loadedata);
2018-02-06 03:14:12 +01:00
video.preload = 'metadata';
video.src = url;
// Load video in Safari / IE11
video.muted = true;
video.playsInline = true;
video.play();
2018-01-11 02:41:17 +01:00
};
2018-02-06 03:14:12 +01:00
fileReader.readAsArrayBuffer(file);
2018-01-11 02:41:17 +01:00
});
}
2018-02-06 03:14:12 +01:00
selectVideoThumb (dataUrl) {
// update this.props.selectedFile
this.props.onThumbnailFileSelect(dataUrl);
}
setPossibleThumbnailFiles (fileOne, fileTwo, fileThree) {
console.log('updating thumbnail file options');
this.props.onThumbnailFileOptionsChange(fileOne, fileTwo, fileThree);
}
2018-02-06 05:07:22 +01:00
setClaimAndThumbailUrl (claim) {
2018-02-06 03:14:12 +01:00
// set thumbnail claim based on publish claim name
const url = `${this.props.host}/${this.props.channel}/${claim}.jpeg`;
this.props.onThumbnailClaimChange(claim, url);
2018-01-11 02:41:17 +01:00
}
2018-01-04 23:14:03 +01:00
render () {
return (
2018-01-11 21:51:38 +01:00
<div>
2018-02-06 03:14:12 +01:00
<label className="label">Thumbnail:</label>
<div>
<p className="info-message-placeholder info-message--failure">{this.state.error}</p>
{this.props.potentialFiles.map((file, index) => <ThumbnailPreview dataUrl={file} key={index}/>)}
2018-01-11 02:41:17 +01:00
</div>
2018-01-04 23:14:03 +01:00
</div>
);
}
}
2018-01-17 19:49:57 +01:00
export default PublishThumbnailInput;