From 98aef7eb7723fe8023d5dc0d65e79d00d75a3e15 Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Thu, 7 Jan 2021 09:35:31 +0530 Subject: [PATCH] [2.5.2] Fix fontSize not reflecting in exported PDF --- gatsby-config.js | 2 +- .../builder/right/sections/FontSize.js | 23 +++++-------------- src/pages/r/view.js | 10 ++++++++ src/utils/index.js | 11 +++++++++ 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index 3bda720f..68a637a1 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -6,7 +6,7 @@ module.exports = { title: 'Reactive Resume', siteUrl: 'https://rxresu.me', description: 'A free and open source resume builder.', - version: '2.5.1', + version: '2.5.2', }, plugins: [ 'gatsby-plugin-react-helmet', diff --git a/src/components/builder/right/sections/FontSize.js b/src/components/builder/right/sections/FontSize.js index becfc58d..dd796915 100644 --- a/src/components/builder/right/sections/FontSize.js +++ b/src/components/builder/right/sections/FontSize.js @@ -2,42 +2,31 @@ import React, { memo, useEffect, useState } from 'react'; import { useDispatch, useSelector } from '../../../../contexts/ResumeContext'; import fontSizeOptions from '../../../../data/fontSizeOptions'; +import { scaler } from '../../../../utils'; import Heading from '../../../shared/Heading'; const FontSizes = ({ id }) => { - // precompute some stuff for the logarithmic slider - const logMax = 2.5; - const logDefault = 1; - const logMin = 0.6; const steps = 20; - const logRange = logMax / logMin; - const logStepSize = logRange ** (1 / steps); const min = 0; const max = min + steps - 1; - const scaleDefault = Math.log(logDefault / logMin) / Math.log(logStepSize); const dispatch = useDispatch(); const fontSize = useSelector('metadata.fontSize'); - const [fontScale, setFontScale] = useState(fontSize || scaleDefault); - - // translate a linearly scaled value from the slider into a scale factor - const scale = (value) => logStepSize ** (value - min) * logMin; + const [scale, setScale] = useState(fontSize || 7); useEffect(() => { - /* loop through the css variables we need to set and set them to the default - for that variable multiplied by the scale factor */ for (const [key, sizeDefault] of Object.entries(fontSizeOptions)) { document.documentElement.style.setProperty( key, - `${scale(fontScale) * sizeDefault}rem`, + `${scaler(scale) * sizeDefault}rem`, ); } - }, [fontScale]); + }, [scale]); const onChange = (event) => { const { value } = event.target; - setFontScale(value); + setScale(value); dispatch({ type: 'on_input', @@ -58,7 +47,7 @@ const FontSizes = ({ id }) => { max={max} type="range" onChange={onChange} - defaultValue={fontScale} + defaultValue={scale} className="rounded-lg overflow-hidden appearance-none bg-gray-400 h-4 w-full" /> diff --git a/src/pages/r/view.js b/src/pages/r/view.js index 3feec42d..5ac529a8 100644 --- a/src/pages/r/view.js +++ b/src/pages/r/view.js @@ -12,6 +12,8 @@ import Onyx from '../../templates/Onyx'; import Pikachu from '../../templates/Pikachu'; import styles from './view.module.css'; import Celebi from '../../templates/Celebi'; +import fontSizeOptions from '../../data/fontSizeOptions'; +import { scaler } from '../../utils'; const ResumeViewer = ({ id }) => { const { t, i18n } = useTranslation(); @@ -33,6 +35,14 @@ const ResumeViewer = ({ id }) => { setResume(data); i18n.changeLanguage(data.metadata.language || 'en'); + + for (const [key, sizeDefault] of Object.entries(fontSizeOptions)) { + document.documentElement.style.setProperty( + key, + `${scaler(data.metadata.fontSize) * sizeDefault}rem`, + ); + } + return setLoading(false); })(); }, [id]); diff --git a/src/utils/index.js b/src/utils/index.js index 9887188f..4acdb57c 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -18,6 +18,17 @@ export const isFileImage = (file) => { return file && acceptedImageTypes.includes(file.type); }; +export const scaler = (value) => { + const logMax = 2.5; + const logMin = 0.6; + const steps = 20; + const logRange = logMax / logMin; + const logStepSize = logRange ** (1 / steps); + const min = 0; + + return logStepSize ** (value - min) * logMin; +}; + export const formatDate = ({ date, language = 'en', includeDay = false }) => { const template = includeDay ? 'DD MMMM YYYY' : 'MMMM YYYY';