Commit f4af44b5 authored by Robert Knight's avatar Robert Knight

Fix anchoring failing when last text on a PDF page is selected

Change PDF anchoring to use the new text position => Range
implementation from the `src/annotator/anchoring/text-position` module
which fixes an issue when the last text in a PDF page is selected.

Fixes #1329
parent 07773973
...@@ -9,9 +9,9 @@ const seek = require('dom-seek'); ...@@ -9,9 +9,9 @@ const seek = require('dom-seek');
const createNodeIterator = require('dom-node-iterator/polyfill')(); const createNodeIterator = require('dom-node-iterator/polyfill')();
const xpathRange = require('./range'); const xpathRange = require('./range');
const html = require('./html');
const RenderingStates = require('../pdfjs-rendering-states'); const RenderingStates = require('../pdfjs-rendering-states');
const { TextPositionAnchor, TextQuoteAnchor } = require('./types'); const { TextPositionAnchor, TextQuoteAnchor } = require('./types');
const { toRange: textPositionToRange } = require('./text-position');
// Caches for performance. // Caches for performance.
...@@ -210,10 +210,9 @@ function findPage(offset) { ...@@ -210,10 +210,9 @@ function findPage(offset) {
* *
* @param {number} pageIndex - The PDF page index * @param {number} pageIndex - The PDF page index
* @param {TextPositionAnchor} anchor - Anchor to locate in page * @param {TextPositionAnchor} anchor - Anchor to locate in page
* @param {Object} options - Options for `anchor.toSelector`
* @return {Promise<Range>} * @return {Promise<Range>}
*/ */
async function anchorByPosition(pageIndex, anchor, options) { async function anchorByPosition(pageIndex, anchor) {
const page = await getPageView(pageIndex); const page = await getPageView(pageIndex);
let renderingDone = false; let renderingDone = false;
...@@ -221,11 +220,9 @@ async function anchorByPosition(pageIndex, anchor, options) { ...@@ -221,11 +220,9 @@ async function anchorByPosition(pageIndex, anchor, options) {
renderingDone = page.textLayer.renderingDone; renderingDone = page.textLayer.renderingDone;
} }
if (page.renderingState === RenderingStates.FINISHED && renderingDone) { if (page.renderingState === RenderingStates.FINISHED && renderingDone) {
// The page has been rendered. Use HTML anchoring to locate the quote in // The page has been rendered. Locate the position in the text layer.
// the text layer.
const root = page.textLayer.textLayerDiv; const root = page.textLayer.textLayerDiv;
const selector = anchor.toSelector(options); return textPositionToRange(root, anchor.start, anchor.end);
return html.anchor(root, [selector]);
} }
// The page has not been rendered yet. Create a placeholder element and // The page has not been rendered yet. Create a placeholder element and
...@@ -371,7 +368,7 @@ function anchor(root, selectors, options = {}) { ...@@ -371,7 +368,7 @@ function anchor(root, selectors, options = {}) {
checkQuote(textContent.substr(start, length)); checkQuote(textContent.substr(start, length));
const anchor = new TextPositionAnchor(root, start, end); const anchor = new TextPositionAnchor(root, start, end);
return anchorByPosition(index, anchor, options); return anchorByPosition(index, anchor);
}); });
}); });
} }
...@@ -386,7 +383,7 @@ function anchor(root, selectors, options = {}) { ...@@ -386,7 +383,7 @@ function anchor(root, selectors, options = {}) {
const { pageIndex, anchor } = quotePositionCache[quote.exact][ const { pageIndex, anchor } = quotePositionCache[quote.exact][
position.start position.start
]; ];
return anchorByPosition(pageIndex, anchor, options); return anchorByPosition(pageIndex, anchor);
} }
return prioritizePages(position).then(pageIndices => { return prioritizePages(position).then(pageIndices => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment