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

Automatic Brew thumbnail generation option #3219

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 41 additions & 2 deletions client/homebrew/editor/metadataEditor/metadataEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const request = require('../../utils/request-middleware.js');
const Nav = require('naturalcrit/nav/nav.jsx');
const Combobox = require('client/components/combobox.jsx');
const StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx');

const htmlimg = require('html-to-image');
const Themes = require('themes/themes.json');
const validations = require('./validations.js');

Expand All @@ -31,6 +31,8 @@ const MetadataEditor = createClass({
title : '',
description : '',
thumbnail : '',
thumbnailSm : null,
thumbnailLg : null,
tags : [],
published : false,
authors : [],
Expand All @@ -56,9 +58,43 @@ const MetadataEditor = createClass({
});
},


ThumbnailCapture : async function() {
const bR = parent.document.getElementById('BrewRenderer');
const brewRenderer = bR.contentDocument || bR.contentWindow.document;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why this is an OR between contentDocument and contentWindow.document? From what I can tell, it is the same thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My memory of the reference code is that it should be the same but for some browser situations, it may not be.

const topPage = brewRenderer.getElementsByClassName('page')[0].cloneNode(true);
// Walk through Top Page's Source and convert all Images to inline data *in* topPage.
const srcImages = brewRenderer.getElementsByClassName('page')[0].getElementsByTagName('img');
const topImages = topPage.getElementsByTagName('img');
// These two should start off with identical contents.
for (let imgPos = 0; imgPos < srcImages.length; imgPos++) {
const imgCanvas = document.createElement('canvas');
const imgConext = imgCanvas.getContext('2d');
imgCanvas.width = srcImages[imgPos].width;
imgCanvas.height = srcImages[imgPos].height;
const newImage = new Image();
newImage.crossOrigin = 'anonymous';
newImage.src = srcImages[imgPos].src;
imgConext.drawImage(newImage, 0, 0, srcImages[imgPos].width, srcImages[imgPos].height);
topImages[imgPos].src = imgCanvas.toDataURL('image/png');
}
const props = this.props;

htmlimg.toPng(topPage, {
preferredFontFormat : 'woff2',
style : { margin: 'unset' },
cacheBust : true
}).then(function(dataURL){
props.metadata.thumbnail = 'Page 1';
props.metadata.thumbnailSm = dataURL;
props.onChange(props.metadata);
});
},

renderThumbnail : function(){
if(!this.state.showThumbnail) return;
return <img className='thumbnail-preview' src={this.props.metadata.thumbnail || homebreweryThumbnail}></img>;
const imgURL = this.props.metadata.thumbnail.startsWith('Page 1') ? this.props.metadata.thumbnailSm : this.props.metadata.thumbnail;
return <img className='thumbnail-preview' src={imgURL || homebreweryThumbnail}></img>;
},

handleFieldChange : function(name, e){
Expand Down Expand Up @@ -333,6 +369,9 @@ const MetadataEditor = createClass({
<button className='display' onClick={this.toggleThumbnailDisplay}>
<i className={`fas fa-caret-${this.state.showThumbnail ? 'right' : 'left'}`} />
</button>
<button className='display' onClick={this.ThumbnailCapture}>
<i className={`fas fa-camera`} />
</button>
</div>
</div>
{this.renderThumbnail()}
Expand Down