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

Improved Automatic Brew thumbnail generation option #3501

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 58 additions & 2 deletions client/homebrew/editor/metadataEditor/metadataEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ 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');
const base64url = require('base64-url');


const SYSTEMS = ['5e', '4e', '3.5e', 'Pathfinder'];

Expand All @@ -30,6 +32,7 @@ const MetadataEditor = createClass({
title : '',
description : '',
thumbnail : '',
thumbnailSm : null,
tags : [],
published : false,
authors : [],
Expand All @@ -55,9 +58,59 @@ const MetadataEditor = createClass({
});
},


thumbnailCapture : async function() {

function urlReplacer(urlMatch, url) {
return (`url(/xssp/${base64url.encode(url)})`);
}
const bR = parent.document.getElementById('BrewRenderer');
const brewRenderer = bR.contentDocument || bR.contentWindow.document;
const pageOne = brewRenderer.getElementsByClassName('page')[0];
const topPage = pageOne.cloneNode(true);
pageOne.parentNode.appendChild(topPage);
// Walk through Top Page's Source and convert all Images to inline data *in* topPage.
const srcImages = pageOne.getElementsByTagName('img');
const topImages = topPage.getElementsByTagName('img');
const topLinks = brewRenderer.getElementsByTagName('link');
const topStyles = brewRenderer.getElementsByTagName('style');
// These two should start off with identical contents.
for (let imgPos = 0; imgPos < srcImages.length; imgPos++) {
topImages[imgPos].src = `/xssp/${base64url.encode(srcImages[imgPos].src)}`;
}
for (let linkPos = 0; linkPos < topLinks.length; linkPos++) {
topLinks[linkPos].href = `/xssp/${base64url.encode(topLinks[linkPos].href)}`;
}
for (let stylePos = 0; stylePos < topStyles.length; stylePos++) {
const urlRegex = /url\(([^\'\"].*[^\'\"])\)/gs;
const urlRegexWrapped = /url\(\'(.*)\'\)/gs;
topStyles[stylePos].innerText = topStyles[stylePos].innerText.replace(urlRegex, urlReplacer);
topStyles[stylePos].innerText = topStyles[stylePos].innerText.replace(urlRegexWrapped, urlReplacer);
}
const props = this.props;

const clientHeightLg = topPage.clientHeight * 0.5;
const clientWidthSm = topPage.clientWidth * (115/topPage.clientHeight);
const clientWidthLg = topPage.clientWidth * 0.5;

htmlimg.toPng(topPage, { canvasHeight : clientHeightLg, canvasWidth : clientWidthLg
}).then(function(dataURL){
props.metadata.thumbnailLg = dataURL;
htmlimg.toJpeg(topPage, { canvasHeight : 115, canvasWidth : clientWidthSm, quality : 0.95
}).then(function(dataURL){
props.metadata.thumbnail = 'Page 1';
props.metadata.thumbnailSm = dataURL;
props.onChange(props.metadata);
topPage.remove();
});
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 @@ -332,6 +385,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
4 changes: 3 additions & 1 deletion client/homebrew/editor/metadataEditor/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ module.exports = {
(value)=>{
if(value?.length == 0){return null;}
try {
Boolean(new URL(value));
if(value != 'Page 1') {
Boolean(new URL(value));
}
return null;
} catch (e) {
return 'Must be a valid URL';
Expand Down
4 changes: 3 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"web_port" : 8000,
"enable_v3" : true,
"local_environments" : ["docker", "local"],
"publicUrl" : "https://homebrewery.naturalcrit.com"
"publicUrl" : "https://homebrewery.naturalcrit.com",
"proxyHost" : "https://172.17.0.1",
"proxyPort" : 3128
}
147 changes: 140 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@babel/preset-env": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@googleapis/drive": "^8.10.0",
"base64-url": "^2.3.3",
"body-parser": "^1.20.2",
"classnames": "^2.5.1",
"codemirror": "^5.65.6",
Expand All @@ -97,8 +98,10 @@
"expr-eval": "^2.0.2",
"express": "^4.19.2",
"express-async-handler": "^1.2.0",
"express-requests-logger": "^4.0.0",
"express-static-gzip": "2.1.7",
"fs-extra": "11.2.0",
"html-to-image": "^1.11.11",
"js-yaml": "^4.1.0",
"jwt-simple": "^0.5.6",
"less": "^3.13.1",
Expand Down
Loading