From 59c3fc69a255c54475daa6a2efad3ae8a4b3efd8 Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 28 Aug 2024 11:41:29 +0200 Subject: [PATCH] fix(pdf): reuse content width function --- src/hooks/useScale.tsx | 19 +------------------ src/utils/pdf.ts | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/hooks/useScale.tsx b/src/hooks/useScale.tsx index 86de61f..406928b 100644 --- a/src/hooks/useScale.tsx +++ b/src/hooks/useScale.tsx @@ -1,29 +1,12 @@ import { useEffect, useState } from 'react' import { singletonHook } from 'react-singleton-hook' +import { getInnerContentWidth } from '../utils/pdf' const noScaleInit = { to: (_: number, v: number) => v, from: (_: number, v: number) => v } -const getInnerContentWidth = () => { - // Fetch the first container element we find - const element = document.querySelector('#content-preview') - - if (element) { - const style = getComputedStyle(element) - - // Calculate width without padding - const widthWithoutPadding = - element.clientWidth - parseFloat(style.padding) * 2 - - return widthWithoutPadding - } - - // Default value - return 620 -} - const useScaleImpl = () => { const [width, setWidth] = useState(getInnerContentWidth()) diff --git a/src/utils/pdf.ts b/src/utils/pdf.ts index b1e9847..1dd75f6 100644 --- a/src/utils/pdf.ts +++ b/src/utils/pdf.ts @@ -60,6 +60,24 @@ export const readPdf = (file: File): Promise => { }) } +export const getInnerContentWidth = () => { + // Fetch the first container element we find + const element = document.querySelector('#content-preview') + + if (element) { + const style = getComputedStyle(element) + + // Calculate width without padding + const widthWithoutPadding = + element.clientWidth - parseFloat(style.padding) * 2 + + return widthWithoutPadding + } + + // Default value + return 620 +} + /** * Converts pdf to the images * @param data pdf file bytes @@ -70,7 +88,7 @@ export const pdfToImages = async ( const pages: PdfPage[] = [] const pdf = await PDFJS.getDocument(data).promise const canvas = document.createElement('canvas') - const width = document.querySelector('#content-preview > *')?.clientWidth || 1 + const width = getInnerContentWidth() for (let i = 0; i < pdf.numPages; i++) { const page = await pdf.getPage(i + 1)