Commit 7807f9c8 authored by Robert Knight's avatar Robert Knight

Rename PDF => PDFIntegration and move to `src/annotator/integrations`

Create an `integrations` directory within `src/annotator` which will
house the `*Integration` classes that encapsulate document
type/viewer-specific functionality, and create the first version of the
PDF integration by moving/renaming the existing `PDF` class. The
supporting `PDFMetadata` class has also been moved.

Per recently agreed code conventions [1], the `PDFIntegration` and
`PDFMetadata` classes are no longer default exports.

[1] https://github.com/hypothesis/frontend-toolkit/blob/master/docs/js-guide.md#default-exports
parent 91fa33cd
import scrollIntoView from 'scroll-into-view'; import scrollIntoView from 'scroll-into-view';
import { Adder } from './adder'; import { Adder } from './adder';
import { PDFIntegration } from './integrations/pdf';
import CrossFrame from './plugin/cross-frame'; import CrossFrame from './plugin/cross-frame';
import DocumentMeta from './plugin/document'; import DocumentMeta from './plugin/document';
import PDFIntegration from './plugin/pdf';
import * as htmlAnchoring from './anchoring/html'; import * as htmlAnchoring from './anchoring/html';
import { TextRange } from './anchoring/text-range'; import { TextRange } from './anchoring/text-range';
......
...@@ -59,7 +59,7 @@ function pdfViewerInitialized(app) { ...@@ -59,7 +59,7 @@ function pdfViewerInitialized(app) {
* // Do something with the URL of the PDF. * // Do something with the URL of the PDF.
* }) * })
*/ */
export default class PDFMetadata { export class PDFMetadata {
/** /**
* Construct a `PDFMetadata` that returns URIs/metadata associated with a * Construct a `PDFMetadata` that returns URIs/metadata associated with a
* given PDF viewer. * given PDF viewer.
......
...@@ -7,7 +7,7 @@ import RenderingStates from '../pdfjs-rendering-states'; ...@@ -7,7 +7,7 @@ import RenderingStates from '../pdfjs-rendering-states';
import { createShadowRoot } from '../util/shadow-root'; import { createShadowRoot } from '../util/shadow-root';
import { ListenerCollection } from '../util/listener-collection'; import { ListenerCollection } from '../util/listener-collection';
import PDFMetadata from './pdf-metadata'; import { PDFMetadata } from './pdf-metadata';
/** /**
* @typedef {import('../../types/annotator').Anchor} Anchor * @typedef {import('../../types/annotator').Anchor} Anchor
...@@ -21,7 +21,7 @@ import PDFMetadata from './pdf-metadata'; ...@@ -21,7 +21,7 @@ import PDFMetadata from './pdf-metadata';
// is enough room. Otherwise, allow sidebar to overlap PDF // is enough room. Otherwise, allow sidebar to overlap PDF
const MIN_PDF_WIDTH = 680; const MIN_PDF_WIDTH = 680;
export default class PDF { export class PDFIntegration {
/** /**
* @param {Annotator} annotator * @param {Annotator} annotator
*/ */
......
import EventEmitter from 'tiny-emitter'; import EventEmitter from 'tiny-emitter';
import PDFMetadata from '../pdf-metadata'; import { PDFMetadata } from '../pdf-metadata';
/** /**
* Fake implementation of PDF.js `window.PDFViewerApplication.metadata`. * Fake implementation of PDF.js `window.PDFViewerApplication.metadata`.
...@@ -126,7 +126,7 @@ function delay(ms) { ...@@ -126,7 +126,7 @@ function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
describe('annotator/plugin/pdf-metadata', function () { describe('PDFMetadata', function () {
[ [
{ {
// PDF.js < 1.6.210: `documentload` event dispatched via DOM. // PDF.js < 1.6.210: `documentload` event dispatched via DOM.
......
import PDF, { $imports } from '../pdf'; import { PDFIntegration, $imports } from '../pdf';
import FakePDFViewerApplication from '../../anchoring/test/fake-pdf-viewer-application'; import FakePDFViewerApplication from '../../anchoring/test/fake-pdf-viewer-application';
...@@ -14,7 +14,7 @@ function awaitEvent(target, eventName) { ...@@ -14,7 +14,7 @@ function awaitEvent(target, eventName) {
}); });
} }
describe('annotator/plugin/pdf', () => { describe('PDFIntegration', () => {
// Fake for the top-level `#outerContainer` DOM element created by PDF.js. // Fake for the top-level `#outerContainer` DOM element created by PDF.js.
let outerContainer; let outerContainer;
// Fake for the `#viewerContainer` DOM element created by PDF.js that contains // Fake for the `#viewerContainer` DOM element created by PDF.js that contains
...@@ -25,10 +25,10 @@ describe('annotator/plugin/pdf', () => { ...@@ -25,10 +25,10 @@ describe('annotator/plugin/pdf', () => {
let fakePdfAnchoring; let fakePdfAnchoring;
let fakePDFMetadata; let fakePDFMetadata;
let fakePDFViewerApplication; let fakePDFViewerApplication;
let pdfPlugin; let pdfIntegration;
function createPDFPlugin() { function createPDFIntegration() {
return new PDF(fakeAnnotator); return new PDFIntegration(fakeAnnotator);
} }
beforeEach(() => { beforeEach(() => {
...@@ -63,7 +63,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -63,7 +63,7 @@ describe('annotator/plugin/pdf', () => {
}; };
$imports.$mock({ $imports.$mock({
'./pdf-metadata': sinon.stub().returns(fakePDFMetadata), './pdf-metadata': { PDFMetadata: sinon.stub().returns(fakePDFMetadata) },
'../anchoring/pdf': fakePdfAnchoring, '../anchoring/pdf': fakePdfAnchoring,
// Disable debouncing of updates. // Disable debouncing of updates.
...@@ -72,7 +72,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -72,7 +72,7 @@ describe('annotator/plugin/pdf', () => {
}); });
afterEach(() => { afterEach(() => {
pdfPlugin?.destroy(); pdfIntegration?.destroy();
delete window.PDFViewerApplication; delete window.PDFViewerApplication;
outerContainer.remove(); outerContainer.remove();
$imports.$restore(); $imports.$restore();
...@@ -86,7 +86,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -86,7 +86,7 @@ describe('annotator/plugin/pdf', () => {
describe('#constructor', () => { describe('#constructor', () => {
it('adds CSS classes to override PDF.js styles', () => { it('adds CSS classes to override PDF.js styles', () => {
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
assert.isTrue(pdfViewerHasClass('has-transparent-text-layer')); assert.isTrue(pdfViewerHasClass('has-transparent-text-layer'));
}); });
}); });
...@@ -100,7 +100,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -100,7 +100,7 @@ describe('annotator/plugin/pdf', () => {
if (!selection.isCollapsed) { if (!selection.isCollapsed) {
selection.collapseToStart(); selection.collapseToStart();
} }
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
assert.isFalse(pdfViewerHasClass('is-selecting')); assert.isFalse(pdfViewerHasClass('is-selecting'));
// Make the selection non-empty. // Make the selection non-empty.
...@@ -121,10 +121,10 @@ describe('annotator/plugin/pdf', () => { ...@@ -121,10 +121,10 @@ describe('annotator/plugin/pdf', () => {
describe('#destroy', () => { describe('#destroy', () => {
it('removes CSS classes to override PDF.js styles', () => { it('removes CSS classes to override PDF.js styles', () => {
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
pdfPlugin.destroy(); pdfIntegration.destroy();
pdfPlugin = null; pdfIntegration = null;
assert.isFalse( assert.isFalse(
fakePDFViewerApplication.pdfViewer.viewer.classList.contains( fakePDFViewerApplication.pdfViewer.viewer.classList.contains(
...@@ -141,7 +141,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -141,7 +141,7 @@ describe('annotator/plugin/pdf', () => {
it('does not show a warning when PDF has selectable text', async () => { it('does not show a warning when PDF has selectable text', async () => {
fakePdfAnchoring.documentHasText.resolves(true); fakePdfAnchoring.documentHasText.resolves(true);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
await delay(0); // Wait for text check to complete. await delay(0); // Wait for text check to complete.
assert.called(fakePdfAnchoring.documentHasText); assert.called(fakePdfAnchoring.documentHasText);
...@@ -151,7 +151,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -151,7 +151,7 @@ describe('annotator/plugin/pdf', () => {
it('does not show a warning if PDF does not load', async () => { it('does not show a warning if PDF does not load', async () => {
fakePDFMetadata.getUri.rejects(new Error('Something went wrong')); fakePDFMetadata.getUri.rejects(new Error('Something went wrong'));
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
await delay(0); // Wait for text check to complete. await delay(0); // Wait for text check to complete.
assert.notCalled(fakePdfAnchoring.documentHasText); assert.notCalled(fakePdfAnchoring.documentHasText);
...@@ -161,7 +161,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -161,7 +161,7 @@ describe('annotator/plugin/pdf', () => {
it('shows a warning when PDF has no selectable text', async () => { it('shows a warning when PDF has no selectable text', async () => {
fakePdfAnchoring.documentHasText.resolves(false); fakePdfAnchoring.documentHasText.resolves(false);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
await delay(0); // Wait for text check to complete. await delay(0); // Wait for text check to complete.
assert.called(fakePdfAnchoring.documentHasText); assert.called(fakePdfAnchoring.documentHasText);
...@@ -194,7 +194,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -194,7 +194,7 @@ describe('annotator/plugin/pdf', () => {
it('re-anchors annotations whose highlights are no longer in the page', async () => { it('re-anchors annotations whose highlights are no longer in the page', async () => {
const anchor = createAnchor(); const anchor = createAnchor();
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
await triggerUpdate(); await triggerUpdate();
...@@ -205,7 +205,7 @@ describe('annotator/plugin/pdf', () => { ...@@ -205,7 +205,7 @@ describe('annotator/plugin/pdf', () => {
it('does not re-anchor annotations whose highlights are still in the page', async () => { it('does not re-anchor annotations whose highlights are still in the page', async () => {
const anchor = createAnchor(); const anchor = createAnchor();
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
document.body.appendChild(anchor.highlights[0]); document.body.appendChild(anchor.highlights[0]);
await triggerUpdate(); await triggerUpdate();
...@@ -227,9 +227,9 @@ describe('annotator/plugin/pdf', () => { ...@@ -227,9 +227,9 @@ describe('annotator/plugin/pdf', () => {
container.id = 'viewerContainer'; container.id = 'viewerContainer';
document.body.appendChild(container); document.body.appendChild(container);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
assert.equal(pdfPlugin.contentContainer(), container); assert.equal(pdfIntegration.contentContainer(), container);
}); });
}); });
...@@ -245,9 +245,9 @@ describe('annotator/plugin/pdf', () => { ...@@ -245,9 +245,9 @@ describe('annotator/plugin/pdf', () => {
it('resizes and activates side-by-side mode when sidebar expanded', () => { it('resizes and activates side-by-side mode when sidebar expanded', () => {
sandbox.stub(window, 'innerWidth').value(1350); sandbox.stub(window, 'innerWidth').value(1350);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
const active = pdfPlugin.fitSideBySide({ const active = pdfIntegration.fitSideBySide({
expanded: true, expanded: true,
width: 428, width: 428,
height: 728, height: 728,
...@@ -269,9 +269,9 @@ describe('annotator/plugin/pdf', () => { ...@@ -269,9 +269,9 @@ describe('annotator/plugin/pdf', () => {
it('activates side-by-side mode for each relative zoom mode', () => { it('activates side-by-side mode for each relative zoom mode', () => {
fakePDFViewerApplication.pdfViewer.currentScaleValue = zoomMode; fakePDFViewerApplication.pdfViewer.currentScaleValue = zoomMode;
sandbox.stub(window, 'innerWidth').value(1350); sandbox.stub(window, 'innerWidth').value(1350);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
const active = pdfPlugin.fitSideBySide({ const active = pdfIntegration.fitSideBySide({
expanded: true, expanded: true,
width: 428, width: 428,
height: 728, height: 728,
...@@ -285,9 +285,9 @@ describe('annotator/plugin/pdf', () => { ...@@ -285,9 +285,9 @@ describe('annotator/plugin/pdf', () => {
it('deactivates side-by-side mode when sidebar collapsed', () => { it('deactivates side-by-side mode when sidebar collapsed', () => {
sandbox.stub(window, 'innerWidth').value(1350); sandbox.stub(window, 'innerWidth').value(1350);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
const active = pdfPlugin.fitSideBySide({ const active = pdfIntegration.fitSideBySide({
expanded: false, expanded: false,
width: 428, width: 428,
height: 728, height: 728,
...@@ -299,9 +299,9 @@ describe('annotator/plugin/pdf', () => { ...@@ -299,9 +299,9 @@ describe('annotator/plugin/pdf', () => {
it('does not activate side-by-side mode if there is not enough room', () => { it('does not activate side-by-side mode if there is not enough room', () => {
sandbox.stub(window, 'innerWidth').value(800); sandbox.stub(window, 'innerWidth').value(800);
pdfPlugin = createPDFPlugin(); pdfIntegration = createPDFIntegration();
const active = pdfPlugin.fitSideBySide({ const active = pdfIntegration.fitSideBySide({
expanded: true, expanded: true,
width: 428, width: 428,
height: 728, height: 728,
......
...@@ -114,11 +114,11 @@ describe('Guest', () => { ...@@ -114,11 +114,11 @@ describe('Guest', () => {
'./anchoring/text-range': { './anchoring/text-range': {
TextRange: FakeTextRange, TextRange: FakeTextRange,
}, },
'./integrations/pdf': { PDFIntegration: PdfIntegration },
'./highlighter': highlighter, './highlighter': highlighter,
'./range-util': rangeUtil, './range-util': rangeUtil,
'./plugin/cross-frame': CrossFrame, './plugin/cross-frame': CrossFrame,
'./plugin/document': DocumentMeta, './plugin/document': DocumentMeta,
'./plugin/pdf': PdfIntegration,
'./selection-observer': { './selection-observer': {
SelectionObserver: FakeSelectionObserver, SelectionObserver: FakeSelectionObserver,
}, },
......
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