0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-07 01:12:44 +00:00
Get renderer information from localStorage when printing from `/new`
This commit is contained in:
Trevor Buckner
2021-10-07 22:32:49 -04:00
committed by GitHub
2 changed files with 29 additions and 12 deletions

View File

@@ -274,7 +274,6 @@ const NewPage = createClass({
}, },
print : function(){ print : function(){
localStorage.setItem('print', `<style>\n${this.state.brew.style}\n</style>\n\n${this.state.brew.text}`);
window.open('/print?dialog=true&local=print', '_blank'); window.open('/print?dialog=true&local=print', '_blank');
}, },

View File

@@ -7,6 +7,10 @@ const { Meta } = require('vitreum/headtags');
const MarkdownLegacy = require('naturalcrit/markdownLegacy.js'); const MarkdownLegacy = require('naturalcrit/markdownLegacy.js');
const Markdown = require('naturalcrit/markdown.js'); const Markdown = require('naturalcrit/markdown.js');
const BREWKEY = 'homebrewery-new';
const STYLEKEY = 'homebrewery-new-style';
const METAKEY = 'homebrewery-new-meta';
const PrintPage = createClass({ const PrintPage = createClass({
getDefaultProps : function() { getDefaultProps : function() {
return { return {
@@ -21,28 +25,42 @@ const PrintPage = createClass({
getInitialState : function() { getInitialState : function() {
return { return {
brewText : this.props.brew.text brew : {
text : this.props.brew.text || '',
style : this.props.brew.style || undefined,
renderer : this.props.brew.renderer || 'legacy'
}
}; };
}, },
componentDidMount : function() { componentDidMount : function() {
if(this.props.query.local){ if(this.props.query.local == 'print'){
this.setState((prevState, prevProps)=>({ const brewStorage = localStorage.getItem(BREWKEY);
brewText : localStorage.getItem(prevProps.query.local) const styleStorage = localStorage.getItem(STYLEKEY);
})); const metaStorage = JSON.parse(localStorage.getItem(METAKEY));
this.setState((prevState, prevProps)=>{
return {
brew : {
text : brewStorage,
style : styleStorage,
renderer : metaStorage.renderer || 'legacy'
}
};
});
} }
if(this.props.query.dialog) window.print(); if(this.props.query.dialog) window.print();
}, },
renderStyle : function() { renderStyle : function() {
if(!this.props.brew.style) return; if(!this.state.brew.style) return;
return <div style={{ display: 'none' }} dangerouslySetInnerHTML={{ __html: `<style> ${this.props.brew.style} </style>` }} />; return <div style={{ display: 'none' }} dangerouslySetInnerHTML={{ __html: `<style> ${this.state.brew.style} </style>` }} />;
}, },
renderPages : function(){ renderPages : function(){
if(this.props.brew.renderer == 'legacy') { if(this.state.brew.renderer == 'legacy') {
return _.map(this.state.brewText.split('\\page'), (pageText, index)=>{ return _.map(this.state.brew.text.split('\\page'), (pageText, index)=>{
return <div return <div
className='phb page' className='phb page'
id={`p${index + 1}`} id={`p${index + 1}`}
@@ -50,7 +68,7 @@ const PrintPage = createClass({
key={index} />; key={index} />;
}); });
} else { } else {
return _.map(this.state.brewText.split(/^\\page$/gm), (pageText, index)=>{ return _.map(this.state.brew.text.split(/^\\page$/gm), (pageText, index)=>{
pageText += `\n\n&nbsp;\n\\column\n&nbsp;`; //Artificial column break at page end to emulate column-fill:auto (until `wide` is used, when column-fill:balance will reappear) pageText += `\n\n&nbsp;\n\\column\n&nbsp;`; //Artificial column break at page end to emulate column-fill:auto (until `wide` is used, when column-fill:balance will reappear)
return ( return (
<div className='page' id={`p${index + 1}`} key={index} > <div className='page' id={`p${index + 1}`} key={index} >
@@ -65,7 +83,7 @@ const PrintPage = createClass({
render : function(){ render : function(){
return <div> return <div>
<Meta name='robots' content='noindex, nofollow' /> <Meta name='robots' content='noindex, nofollow' />
<link href={`${this.props.brew.renderer == 'legacy' ? '/themes/5ePhbLegacy.style.css' : '/themes/5ePhb.style.css'}`} rel='stylesheet'/> <link href={`${this.state.brew.renderer == 'legacy' ? '/themes/5ePhbLegacy.style.css' : '/themes/5ePhb.style.css'}`} rel='stylesheet'/>
{/* Apply CSS from Style tab */} {/* Apply CSS from Style tab */}
{this.renderStyle()} {this.renderStyle()}
<div className='pages' ref='pages'> <div className='pages' ref='pages'>