improve(pdf): scroll to hightlight

pull/2471/head
charlie 2021-07-19 14:30:03 +08:00
parent 906ab01d47
commit 1653b7c3db
4 changed files with 31 additions and 2 deletions

View File

@ -231,8 +231,15 @@
(.querySelector el ".pp-holder")))
[:pre
(js/JSON.stringify (bean/->js highlights) nil 2)]]))
[:ul
(for [hl highlights]
[:li
[:a
{:on-click #(pdf-utils/scroll-to-highlight viewer hl)}
(str "#" (:id hl) "# ")]
(:text (:content hl))])
;;(js/JSON.stringify (bean/->js highlights) nil 2)
]]))
(rum/defc pdf-viewer
[url initial-hls ^js pdf-document]

View File

@ -29,6 +29,7 @@
&-hls-text-region {
position: absolute;
opacity: 1;
z-index: 2;
}
&-hls-ctx-menu {

View File

@ -49,6 +49,11 @@
layer)
layer))))
(defn scroll-to-highlight
[^js viewer hl]
(when-let [js-hl (bean/->js hl)]
(js-utils/scrollToHighlight viewer js-hl)))
(defn clear-all-selection
[]
(.removeAllRanges (js/window.getSelection)))

View File

@ -92,3 +92,19 @@ export const getBoundingRect = (clientRects) => {
height: Y1 - Y0,
}
}
export const scrollToHighlight = (viewer, highlight) => {
if (!highlight) return
const { page, bounding } = highlight.position
const viewport = viewer.getPageView(page - 1)?.viewport
if (!viewport) return
viewer.scrollPageIntoView({
pageNumber: page,
destArray: [
null, { name: 'XYZ' },
...viewport.convertToPdfPoint(0, scaledToViewport(bounding, viewport).top - 100),
0 // scale
]
})
}