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

[FEATURE]: Add default page size setting #3498

Open
wants to merge 4 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
28 changes: 28 additions & 0 deletions client/homebrew/pages/accountPage/accountPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const UIPage = require('../basePages/uiPage/uiPage.jsx');
const NaturalCritIcon = require('naturalcrit/svg/naturalcrit.svg.jsx');

let SAVEKEY = '';
const SIZEKEY = 'HOMEBREWERY-DEFAULT-SIZE';

const AccountPage = (props)=>{
// destructure props and set state for save location
const { accountDetails, brew } = props;
const [saveLocation, setSaveLocation] = React.useState('');
const [pageSize, setPageSize] = React.useState('');

// initialize save location from local storage based on user id
React.useEffect(()=>{
Expand All @@ -19,6 +21,9 @@ const AccountPage = (props)=>{
saveLocation = saveLocation ?? (accountDetails.googleId ? 'GOOGLE-DRIVE' : 'HOMEBREWERY');
setActiveSaveLocation(saveLocation);
}
if(!pageSize) {
setPageSize(window.localStorage.getItem(SIZEKEY) ?? 'letter');
}
}, []);

const setActiveSaveLocation = (newSelection)=>{
Expand All @@ -37,6 +42,24 @@ const AccountPage = (props)=>{
);
};

const setActivePageSize = (size) => {
if (typeof window !== 'undefined' && window.localStorage) {
window.localStorage.setItem(SIZEKEY, size);
setPageSize(size);
}
}

const renderDefaultPageSizeOptions = () => {

return (
<div className='pageSize'>
<h5>Default Page Size: </h5>
<label>Letter:<input type="radio" name="size" id="letterSize" onChange={() => setActivePageSize('letter')} defaultChecked={pageSize === 'letter'}/></label>
<label>A4:<input type="radio" name="size" id="A4Size" onChange={() => setActivePageSize('A4')} defaultChecked={pageSize === 'A4'}/></label>
</div>
);
};

// render the entirety of the account page content
const renderAccountPage = ()=>{
return (
Expand Down Expand Up @@ -68,6 +91,11 @@ const AccountPage = (props)=>{
{renderSaveLocationButton('Homebrewery', 'HOMEBREWERY')}
{renderSaveLocationButton('Google Drive', 'GOOGLE-DRIVE', accountDetails.googleId)}
</div>
<div className="dataGroup">
<h4>Brew Defaults</h4>
{renderDefaultPageSizeOptions()}
{/*renderDefaultThemeUrl()*/}
</div>
</>
);
};
Expand Down
18 changes: 18 additions & 0 deletions client/homebrew/pages/basePages/uiPage/uiPage.less
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
}
}
}

.pageSize {
h5 {
font-weight: 800;
font-size: 13px;
}

label {
background: #ccc;
border-radius: 5px;
padding: 5px 10px;
margin-right:5px;

input {
vertical-align: text-top;
}
}
}
}
h1, h2, h3, h4 {
width : 100%;
Expand Down
23 changes: 18 additions & 5 deletions client/homebrew/pages/newPage/newPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { printCurrentBrew } = require('../../../../shared/helpers.js');
const BREWKEY = 'homebrewery-new';
const STYLEKEY = 'homebrewery-new-style';
const METAKEY = 'homebrewery-new-meta';
const SIZEKEY = 'HOMEBREWERY-DEFAULT-SIZE'
let SAVEKEY;


Expand Down Expand Up @@ -55,19 +56,31 @@ const NewPage = createClass({

const brew = this.state.brew;

if(!this.props.brew.shareId && typeof window !== 'undefined') { //Load from localStorage if in client browser
if (!this.props.brew.shareId && typeof window !== 'undefined') {
// Load from localStorage if in client browser
const brewStorage = localStorage.getItem(BREWKEY);
const styleStorage = localStorage.getItem(STYLEKEY);
const metaStorage = JSON.parse(localStorage.getItem(METAKEY));

const sizeStorage = localStorage.getItem(SIZEKEY);
const metaStorage = JSON.parse(localStorage.getItem(METAKEY));

brew.text = brewStorage ?? brew.text;
brew.style = styleStorage ?? brew.style;
// brew.title = metaStorage?.title || this.state.brew.title;
// brew.description = metaStorage?.description || this.state.brew.description;

// Check if a page size is already defined in brew.style
const pageSizeRegex = /\.page\s*{[^}]*\b(width|height)\b[^}]*}/;
if (!pageSizeRegex.test(brew.style)) {
// No page size defined, apply the default size if sizeStorage is 'A4', otherwise the default letter stays
if (sizeStorage === 'A4') {
brew.style = brew.style + '\n\n/* A4 Page Size */\n.page{\n width : 210mm;\n height : 296.8mm;\n}\n';
}
}

// Apply other properties from metaStorage
brew.renderer = metaStorage?.renderer ?? brew.renderer;
brew.theme = metaStorage?.theme ?? brew.theme;
brew.lang = metaStorage?.lang ?? brew.lang;
}


SAVEKEY = `HOMEBREWERY-DEFAULT-SAVE-LOCATION-${global.account?.username || ''}`;
const saveStorage = localStorage.getItem(SAVEKEY) || 'HOMEBREWERY';
Expand Down