[WIP] SCHOL-857: New pdf.js reader - #207
Draft
jackiequach wants to merge 2 commits into
Draft
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TODO/in progress:
Adds
NewReaderanduseNewReaderbuilt directly with pdf.js, replacing the react-pdfusePdfReaderin the mainuseWebReader. Both are exported and can be used independently.Initial load
Every page is always in the DOM as a fixed-size
divplaceholder whose dimensions are set from the page's intrinsic size (fetched viapdfDoc.getPage(i)at load time, before any canvas is painted). This gives the scroll container a correct, stable total height and a working scrollbar immediately, before any rendering has happened. The canvas, text layer, and annotation layer inside each page are only painted once the page scrolls into anIntersectionObserverwindow (300px above/below the viewport) and are torn down again when the page scrolls out.State management
Reader state (current page, total pages, scale, fit mode, rotation, navigation request counter) is managed by a
pdfReaderReducerwith auseReducerinuseNewReader. Actions mirror the pattern inusePdfReader's reducer but are simpler becauseNewReaderis a single-file viewer.Note: there is no multi-resource state machine (INACTIVE → FETCHING_RESOURCE → RENDERING_IFRAME → READY) implemented in the new reader to simplify the implementation.
Actions that require DOM measurements before the state change can commit (ZOOM_IN, ZOOM_OUT, ROTATE_CCW) are queued as a
pendingActionstate rather than dispatched directly.PdfReaderprocesses them in auseEffect, callingcaptureViewportAnchor()synchronously against the current DOM before passing the action to the reducer. This preserves the pre-change scroll position across layout recalculations.Scroll position preservation (viewport anchor)
On every zoom or rotation, before the state change is committed,
captureViewportAnchor()records:SCROLLSPY_ANCHOR_RATIO)intraPageRatio)viewportOffset)After the state change renders, a
useLayoutEffectruns synchronously before the browser paints, recomputes the anchor page's newtopandheightfrom the updated scale/rotation, and setsscrollTopto restore the exact same content to the same screen position. The anchor is idempotent, a second capture within the same user action (e.g. rotate + refit scale) is a no-op, so both state writes resolve against the same pre-action snapshot.CSS over Chakra
Plain CSS is the better fit for the PDF reader since it is better for performance. Chakra's runtime style injection and prop-driven re-renders add overhead that compounds across all of the page components, whereas CSS rules are static and resolved entirely by the browser. The viewer also needs control over text/annotation layer positioning (position: absolute, transform-origin, z-index stacking) that maps naturally to CSS but would be awkward to express through Chakra's prop system.
Testing
Locally replace the urls in
single-resource-short.jsonwith a larger pdf and go to/pdf/new-readeror/pdf/single-resource-short