Unverified Commit d53e41e8 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #887 from hypothesis/convert-pdf-anchoring-to-js

Convert PDF anchoring code to JS
parents c3faed17 9cfdc3c2
This diff is collapsed.
This diff is collapsed.
...@@ -108,16 +108,18 @@ function createPage(content, rendered) { ...@@ -108,16 +108,18 @@ function createPage(content, rendered) {
/** /**
* Set the index of the page which is currently visible in the viewport. * Set the index of the page which is currently visible in the viewport.
* *
* The page which is visible will be "rendered" and have a text layer available. * Pages from `index` up to and including `lastRenderedPage` will be
* For other pages, there will only be a placeholder element for the whole page. * "rendered" and have a text layer available. Other pages will be "un-rendered"
* with no text layer available, but only a placeholder element for the whole
* page.
*/ */
FakePDFViewerApplication.prototype.setCurrentPage = function (index) { FakePDFViewerApplication.prototype.setCurrentPage = function (index, lastRenderedPage=index) {
const self = this; const self = this;
this._checkBounds(index); this._checkBounds(index);
const pages = this._content.map(function (text, idx) { const pages = this._content.map(function (text, idx) {
return createPage(text, idx === index /* rendered */); return createPage(text, idx >= index && idx <= lastRenderedPage);
}); });
this._container.innerHTML = ''; this._container.innerHTML = '';
......
...@@ -134,6 +134,15 @@ describe('annotator.anchoring.pdf', function () { ...@@ -134,6 +134,15 @@ describe('annotator.anchoring.pdf', function () {
assert.equal(position.end, expectedPos + quote.length); assert.equal(position.end, expectedPos + quote.length);
}); });
}); });
it('rejects when text selection spans multiple pages', () => {
viewer.setCurrentPage(2, 3);
const range = findText(container, 'occupied again? NODE A');
return pdfAnchoring.describe(container, range).catch(err => {
assert.equal(err.message, 'selecting across page breaks is not supported');
});
});
}); });
describe('#anchor', function () { describe('#anchor', function () {
...@@ -203,6 +212,41 @@ describe('annotator.anchoring.pdf', function () { ...@@ -203,6 +212,41 @@ describe('annotator.anchoring.pdf', function () {
assert.equal(anchoredRange.toString(), 'Loading annotations…'); assert.equal(anchoredRange.toString(), 'Loading annotations…');
}); });
}); });
it('rejects if quote cannot be anchored', () => {
viewer.setCurrentPage(2);
const selectors = [{
type: 'TextQuoteSelector',
exact: 'phrase that does not exist in the PDF',
}];
return pdfAnchoring.anchor(container, selectors)
.catch(err => {
assert.equal(err.message, 'Quote not found');
});
});
it('re-anchors successfully using caches', () => {
viewer.setCurrentPage(2);
const range = findText(container, 'said his lady');
let selectors;
return pdfAnchoring.describe(container, range).then(selectors_ => {
selectors = selectors_;
// Adjust the position selector so that anchoring fails, and a fallback
// to the quote selector is required.
const position = selectors.find(s => s.type === 'TextPositionSelector');
position.start += 100;
position.end += 100;
return pdfAnchoring.anchor(container, selectors);
}).then(() => {
// Anchor again using the same selectors. This time anchoring will
// use the existing cache.
return pdfAnchoring.anchor(container, selectors);
}).then(range => {
assert.equal(range.toString(), 'said his lady');
});
});
}); });
}); });
...@@ -5,6 +5,7 @@ require('core-js/es6/promise'); ...@@ -5,6 +5,7 @@ require('core-js/es6/promise');
require('core-js/es6/map'); require('core-js/es6/map');
require('core-js/es6/set'); require('core-js/es6/set');
require('core-js/es6/symbol'); require('core-js/es6/symbol');
require('core-js/fn/array/fill');
require('core-js/fn/array/find'); require('core-js/fn/array/find');
require('core-js/fn/array/find-index'); require('core-js/fn/array/find-index');
require('core-js/fn/array/from'); require('core-js/fn/array/from');
......
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