0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 18:32:41 +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(){
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');
},

View File

@@ -7,6 +7,10 @@ const { Meta } = require('vitreum/headtags');
const MarkdownLegacy = require('naturalcrit/markdownLegacy.js');
const Markdown = require('naturalcrit/markdown.js');
const BREWKEY = 'homebrewery-new';
const STYLEKEY = 'homebrewery-new-style';
const METAKEY = 'homebrewery-new-meta';
const PrintPage = createClass({
getDefaultProps : function() {
return {
@@ -21,28 +25,42 @@ const PrintPage = createClass({
getInitialState : function() {
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() {
if(this.props.query.local){
this.setState((prevState, prevProps)=>({
brewText : localStorage.getItem(prevProps.query.local)
}));
if(this.props.query.local == 'print'){
const brewStorage = localStorage.getItem(BREWKEY);
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();
},
renderStyle : function() {
if(!this.props.brew.style) return;
return <div style={{ display: 'none' }} dangerouslySetInnerHTML={{ __html: `<style> ${this.props.brew.style} </style>` }} />;
if(!this.state.brew.style) return;
return <div style={{ display: 'none' }} dangerouslySetInnerHTML={{ __html: `<style> ${this.state.brew.style} </style>` }} />;
},
renderPages : function(){
if(this.props.brew.renderer == 'legacy') {
return _.map(this.state.brewText.split('\\page'), (pageText, index)=>{
if(this.state.brew.renderer == 'legacy') {
return _.map(this.state.brew.text.split('\\page'), (pageText, index)=>{
return <div
className='phb page'
id={`p${index + 1}`}
@@ -50,7 +68,7 @@ const PrintPage = createClass({
key={index} />;
});
} 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)
return (
<div className='page' id={`p${index + 1}`} key={index} >
@@ -65,7 +83,7 @@ const PrintPage = createClass({
render : function(){
return <div>
<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 */}
{this.renderStyle()}
<div className='pages' ref='pages'>