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

[feat] Add Pinch Zoom and Trackpad Zoom to pdf.js (rough) #4534

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
95 changes: 94 additions & 1 deletion packages/web/components/templates/article/pdf.js/PdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
const [clickedHighlight, setClickedHighlight] =
useState<Highlight | null>(null)
const [currentPageNum, setCurrentPageNum] = useState(1)
const [scaling, setScaling] = useState(false)
const [startScale, setStartScale] = useState(0)

const colorMap: Record<string, string> = useMemo(
() => ({
Expand Down Expand Up @@ -311,14 +313,85 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
}
}

const handleWheelZoom = (ev: WheelEvent) => {
// For pinch to zoom on the trackpad, it counts as the ctrl key being set.
// This also means that zooms with a normal mouse wheel + ctrl will also zoom the pdf.
const isPinch = ev.ctrlKey
if (isPinch && props.pdfViewer) {
ev.preventDefault();
ev.stopPropagation()
const currentScale = props.pdfViewer.currentScale
let scale = currentScale - (ev.deltaY * 0.01)
scale = scale > 5 ? 5 : scale;
scale = scale < 0.1 ? 0.1 : scale;

props.pdfViewer.currentScale = scale
}
}

const pinchStart = (event: TouchEvent) => {
if (event.touches.length == 2) {
event.preventDefault()
event.stopPropagation()
const dist = Math.hypot(
event.touches[0].pageX - event.touches[1].pageX,
event.touches[0].pageY - event.touches[1].pageY);

setStartScale(dist)
setScaling(true)

}
}

const pinchZoom = (e: TouchEvent) => {
if (scaling) {
const dist = Math.hypot(
e.touches[0].pageX - e.touches[1].pageX,
e.touches[0].pageY - e.touches[1].pageY);

if (props.pdfViewer) {
const scaled = (dist / startScale)
props.pdfViewer.updateScale({
scaleFactor: scaled,
origin: [e.touches[0].pageX, e.touches[0].pageY]
})
}

setStartScale(dist)

}
}


const pinchEnd = () => {
setScaling(false)
}

if (props.containerRef?.current) {
const isTouch = isTouchScreenDevice()

props.containerRef.current.addEventListener(
'touchstart',
pinchStart
)

props.containerRef.current.addEventListener(
'touchend',
pinchEnd
)

props.containerRef.current.addEventListener(
'touchmove',
pinchZoom
)

if (!isTouch) {
props.containerRef.current.addEventListener(
'mouseup',
detectHighlightedText
)
props.containerRef.current.addEventListener('wheel', handleWheelZoom)

}

if (isTouch) {
Expand All @@ -336,10 +409,28 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
detectHighlightedText
)

props.containerRef.current.addEventListener(
props.containerRef.current.removeEventListener(
'touchend',
detectHighlightedText
)

props.containerRef.current.removeEventListener(
'touchstart',
pinchStart
)

props.containerRef.current.removeEventListener(
'touchend',
pinchEnd
)

props.containerRef.current.removeEventListener(
'touchmove',
pinchZoom
)

props.containerRef.current.removeEventListener('wheel', handleWheelZoom)

}
}
}, [
Expand All @@ -349,6 +440,8 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
props.pdfViewer,
setHighlights,
highlights,
scaling,
startScale
])

// When a page zooms, it internally re-renders using pdf.js. We add a function
Expand Down
Loading