Skip to content

Commit

Permalink
Merge branch 'use_position_fixed'
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Nov 5, 2024
2 parents 3d47115 + 3dc091c commit b48e4b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
53 changes: 24 additions & 29 deletions viewer/components/viewerloading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,15 @@ export class ViewerLoading {
// https://github.com/James-Yu/LaTeX-Workshop/issues/1871
PDFViewerApplicationOptions.set('spreadModeOnLoad', pack.spreadMode)

storeScaleRounds()
const maskArray = makeMasksForAllVisiblePages()
const viewerContainerSpacer = createViewerContainerSpacer()
void PDFViewerApplication.open({ url: `${pdfFilePrefix}${this.lwApp.encodedPdfFilePath}` }).then(() => {
// reset the document title to the original value to avoid duplication
document.title = this.lwApp.documentTitle
})
const disposable = this.lwApp.lwEventBus.onPageRendered(() => {
viewerContainerSpacer.remove()
if (isAllVisiblePagesRendered()) {
viewerContainerSpacer.remove()
disposable.dispose()
// Remove the maskt with a transition effect.
for (const mask of maskArray) {
Expand Down Expand Up @@ -146,57 +145,53 @@ function makeMasksForAllVisiblePages() {
const visiblePages = PDFViewerApplication.pdfViewer._getVisiblePages()
for (const visiblePage of visiblePages.views) {
const page = visiblePage.view.div
const pageRect = page.getBoundingClientRect()
const canvas = visiblePage.view.canvas
if (!canvas) {
continue
}
const canvasRect = canvas.getBoundingClientRect()
const div = document.createElement('div')
div.className = 'divMask'
maskArray.push(div)
div.style.display = 'none'
div.style.top = page.offsetTop + 'px'
div.style.left = page.offsetLeft + 'px'
div.style.width = Math.min(viewerDom.clientWidth, page.clientWidth) + 'px'
div.style.height = page.clientHeight + 'px'
div.style.top = pageRect.top + 'px'
div.style.left = pageRect.left + 'px'
div.style.width = pageRect.width + 'px'
div.style.height = pageRect.height + 'px'
const img = new Image()
img.src = canvas.toDataURL() ?? ''
img.style.left = canvas.offsetLeft + 'px'
img.style.width = canvas.clientWidth + 'px'
img.style.height = canvas.clientHeight + 'px'
img.src = canvas.toDataURL()
img.style.left = canvasRect.left - pageRect.left + 'px'
img.style.width = canvasRect.width + 'px'
img.style.height = canvasRect.height + 'px'
div.appendChild(img)
viewerContainer.appendChild(div)
div.style.display = 'inherit'
debugPrint('page')
debugPrint({ offsetTop: page.offsetTop, offetLeft: page.offsetLeft, width: page.clientWidth, height: page.clientHeight})
debugPrint('canvas')
debugPrint({ offsetTop: canvas.offsetTop, offetLeft: canvas.offsetLeft, width: canvas.clientWidth, height: canvas.clientHeight})
debugPrint('div')
debugPrint({ offsetTop: div.offsetTop, offetLeft: div.offsetLeft, width: div.clientWidth, height: div.clientHeight})
debugPrint('img')
debugPrint({ offsetTop: img.offsetTop, offetLeft: img.offsetLeft, width: img.clientWidth, height: img.clientHeight})
debugPrintElements([['page', page], ['canvas', canvas], ['div', div], ['img', img]])
}
return maskArray
}

/**
* We must store the values to prevent glitches when refreshing the PDF viewer.
*/
function storeScaleRounds() {
const page = viewerDom.firstElementChild as HTMLElement
const scaleRoundX = page.style.getPropertyValue('--scale-round-x')
const scaleRoundY = page.style.getPropertyValue('--scale-round-y')
viewerContainer.style.setProperty('--stored-scale-round-x', scaleRoundX)
viewerContainer.style.setProperty('--stored-scale-round-y', scaleRoundY)
function debugPrintElements(elems: [string, HTMLElement][]) {
for (const [name, elem] of elems) {
debugPrint(name)
const rect = elem.getBoundingClientRect()
const { top, left, width, height } = rect
debugPrint({ top, left, width, height })
}
}

function createViewerContainerSpacer() {
const spacer = document.createElement('div')
const viewerDomRect = viewerDom.getBoundingClientRect()
spacer.id = 'viewerContainerSpacer'
spacer.style.top = viewerDom.offsetTop + 'px'
spacer.style.left = viewerDom.offsetLeft + 'px'
spacer.style.width = viewerDom.clientWidth + 'px'
spacer.style.height = viewerDom.clientHeight + 'px'
spacer.style.width = viewerDomRect.width + 'px'
const pageBottomMargin = 10
spacer.style.height = viewerDomRect.height + pageBottomMargin + 'px'
viewerContainer.appendChild(spacer)
debugPrintElements([['viewer', viewerDom], ['spacer', spacer]])
return spacer
}

Expand Down
13 changes: 4 additions & 9 deletions viewer/latextoybox.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ html[dir='rtl'] .findbar {
*/

#viewerContainerSpacer {
padding: 0;
margin: 0;
border: none;
position: absolute;
z-index: -1;
}
Expand All @@ -89,7 +92,7 @@ html[dir='rtl'] .findbar {
border: none;
box-shadow: 0px 0px 0px 1px lightgrey;
outline: none;
position: absolute;
position: fixed;
overflow: hidden;
z-index: 10;
}
Expand All @@ -112,14 +115,6 @@ html[dir='rtl'] .findbar {
mask-image: var(--findbarButton-next-icon);
}

/**
* Tweak for reloading.
*/
.pdfViewer .page{
--scale-round-x: var(--stored-scale-round-x, 1px);
--scale-round-y: var(--stored-scale-round-y, 1px);
}

.pdfViewer.removePageBorders .page {
border: none;
box-shadow: 0px 0px 0px 1px lightgrey;
Expand Down

0 comments on commit b48e4b4

Please sign in to comment.