Skip to content

Commit

Permalink
When PDF page sizes vary, fix the issue where annotation positions ar…
Browse files Browse the repository at this point in the history
…e misaligned.
  • Loading branch information
t29mato committed Jan 12, 2024
1 parent b23de6d commit 0495eeb
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions streamlit_pdf_viewer/frontend/src/PdfViewer.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div id="pdfViewer" :style="pdfViewerStyle">
<div id="pdfAnnotations" v-if="args.annotations">
<div v-for="(annotation, index) in args.annotations" :key="index">
<div id="pdfAnnotations" v-if="annotations">
<div v-for="(annotation, index) in annotations" :key="index">
<div :style="getAnnotationStyle(annotation)" :id="index"></div>
</div>
</div>
Expand All @@ -23,7 +23,7 @@ export default {
let maxWidth = 0
const pageScales = []
const getPdfsHeight = (page, ratio = 1) => {
const getPdfsHeight = (page, ratio) => {
let height = 0
for (let i = 1; i < page; i++) {
height += Math.floor(pdfHeight * ratio)
Expand All @@ -35,14 +35,15 @@ export default {
}
const getAnnotationStyle = (annoObj) => {
const scale = pageScales[annoObj.page]
const pageScalesIndex = annoObj.page - 1
const scale = pageScales[pageScalesIndex]
return ({
position: 'absolute',
left: `${annoObj.x * scale}px`,
top: `${getPdfsHeight(annoObj.page) + annoObj.y*scale}px`,
width: `${annoObj.width*scale}px`,
height: `${annoObj.height*scale}px`,
outline: `${2*scale}px solid`,
top: `${getPdfsHeight(annoObj.page, scale) + annoObj.y * scale}px`,
width: `${annoObj.width * scale}px`,
height: `${annoObj.height * scale}px`,
outline: `${2 * scale}px solid`,
outlineColor: annoObj.color,
cursor: 'pointer'
});
Expand Down Expand Up @@ -109,10 +110,13 @@ export default {
Streamlit.setComponentReady()
});
const annotations = props.args.annotations

Check failure on line 113 in streamlit_pdf_viewer/frontend/src/PdfViewer.vue

View workflow job for this annotation

GitHub Actions / build (3.7, 18)

Getting a value from the `props` in root scope of `setup()` will cause the value to lose reactivity
return {
getPdfsHeight,
getAnnotationStyle,
pdfViewerStyle
pdfViewerStyle,
annotations
}
},
}
Expand Down

0 comments on commit 0495eeb

Please sign in to comment.