2014-09-30 01:05:28 +09:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-05-31 10:38:22 +09:00
|
|
|
import { AnnotationLayer } from 'pdfjs-lib';
|
2017-05-04 10:05:53 +09:00
|
|
|
import { NullL10n } from './ui_utils';
|
2017-04-15 00:32:36 +09:00
|
|
|
import { SimpleLinkService } from './pdf_link_service';
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2014-09-30 01:05:28 +09:00
|
|
|
/**
|
2015-12-17 20:54:53 +09:00
|
|
|
* @typedef {Object} AnnotationLayerBuilderOptions
|
2014-09-30 01:05:28 +09:00
|
|
|
* @property {HTMLDivElement} pageDiv
|
|
|
|
* @property {PDFPage} pdfPage
|
2016-09-18 02:44:25 +09:00
|
|
|
* @property {boolean} renderInteractiveForms
|
2014-09-30 01:05:28 +09:00
|
|
|
* @property {IPDFLinkService} linkService
|
2016-02-15 04:44:00 +09:00
|
|
|
* @property {DownloadManager} downloadManager
|
2017-05-04 10:05:53 +09:00
|
|
|
* @property {IL10n} l10n - Localization service.
|
2014-09-30 01:05:28 +09:00
|
|
|
*/
|
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
class AnnotationLayerBuilder {
|
2014-09-30 01:05:28 +09:00
|
|
|
/**
|
2015-12-17 20:54:53 +09:00
|
|
|
* @param {AnnotationLayerBuilderOptions} options
|
2014-09-30 01:05:28 +09:00
|
|
|
*/
|
2017-04-24 03:52:58 +09:00
|
|
|
constructor(options) {
|
2014-09-30 01:05:28 +09:00
|
|
|
this.pageDiv = options.pageDiv;
|
|
|
|
this.pdfPage = options.pdfPage;
|
2016-09-18 02:44:25 +09:00
|
|
|
this.renderInteractiveForms = options.renderInteractiveForms;
|
2014-09-30 01:05:28 +09:00
|
|
|
this.linkService = options.linkService;
|
2016-02-15 04:44:00 +09:00
|
|
|
this.downloadManager = options.downloadManager;
|
2017-05-04 10:05:53 +09:00
|
|
|
this.l10n = options.l10n || NullL10n;
|
2014-09-30 01:05:28 +09:00
|
|
|
|
|
|
|
this.div = null;
|
|
|
|
}
|
2015-12-03 08:20:18 +09:00
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
/**
|
|
|
|
* @param {PageViewport} viewport
|
|
|
|
* @param {string} intent (default value is 'display')
|
|
|
|
*/
|
|
|
|
render(viewport, intent = 'display') {
|
2017-04-28 19:02:42 +09:00
|
|
|
this.pdfPage.getAnnotations({ intent, }).then((annotations) => {
|
2015-12-09 07:38:32 +09:00
|
|
|
var parameters = {
|
2017-04-24 03:52:58 +09:00
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
|
|
|
div: this.div,
|
|
|
|
annotations,
|
|
|
|
page: this.pdfPage,
|
|
|
|
renderInteractiveForms: this.renderInteractiveForms,
|
|
|
|
linkService: this.linkService,
|
|
|
|
downloadManager: this.downloadManager,
|
2015-11-22 21:56:52 +09:00
|
|
|
};
|
2014-09-30 01:05:28 +09:00
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
if (this.div) {
|
|
|
|
// If an annotationLayer already exists, refresh its children's
|
|
|
|
// transformation matrices.
|
|
|
|
AnnotationLayer.update(parameters);
|
|
|
|
} else {
|
|
|
|
// Create an annotation layer div and render the annotations
|
|
|
|
// if there is at least one annotation.
|
|
|
|
if (annotations.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-09 07:38:32 +09:00
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
this.div = document.createElement('div');
|
|
|
|
this.div.className = 'annotationLayer';
|
|
|
|
this.pageDiv.appendChild(this.div);
|
|
|
|
parameters.div = this.div;
|
2015-12-09 07:38:32 +09:00
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
AnnotationLayer.render(parameters);
|
2017-05-04 10:05:53 +09:00
|
|
|
this.l10n.translate(this.div);
|
2014-09-30 01:05:28 +09:00
|
|
|
}
|
2017-04-24 03:52:58 +09:00
|
|
|
});
|
|
|
|
}
|
2015-12-03 08:20:18 +09:00
|
|
|
|
2017-04-24 03:52:58 +09:00
|
|
|
hide() {
|
|
|
|
if (!this.div) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.div.setAttribute('hidden', 'true');
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 05:47:14 +09:00
|
|
|
|
|
|
|
/**
|
2015-12-17 20:54:53 +09:00
|
|
|
* @implements IPDFAnnotationLayerFactory
|
2014-12-18 05:47:14 +09:00
|
|
|
*/
|
2017-04-24 03:52:58 +09:00
|
|
|
class DefaultAnnotationLayerFactory {
|
2014-12-18 05:47:14 +09:00
|
|
|
/**
|
|
|
|
* @param {HTMLDivElement} pageDiv
|
|
|
|
* @param {PDFPage} pdfPage
|
2016-09-18 02:44:25 +09:00
|
|
|
* @param {boolean} renderInteractiveForms
|
2017-05-04 10:05:53 +09:00
|
|
|
* @param {IL10n} l10n
|
2015-12-17 20:54:53 +09:00
|
|
|
* @returns {AnnotationLayerBuilder}
|
2014-12-18 05:47:14 +09:00
|
|
|
*/
|
2017-04-24 03:52:58 +09:00
|
|
|
createAnnotationLayerBuilder(pageDiv, pdfPage,
|
2017-05-04 10:05:53 +09:00
|
|
|
renderInteractiveForms = false,
|
|
|
|
l10n = NullL10n) {
|
2015-12-17 20:54:53 +09:00
|
|
|
return new AnnotationLayerBuilder({
|
2017-04-24 03:52:58 +09:00
|
|
|
pageDiv,
|
|
|
|
pdfPage,
|
|
|
|
renderInteractiveForms,
|
Simplify the SimpleLinkService and use it to pass in a linkService instance in DefaultAnnotationsLayerFactory
Considering that most methods of `SimpleLinkService` are complete stubs, or practically "useless" considering what they return, we can actually simplify it even more.
*Note:* This depends on the previous patch, that did a small amount of refactoring of `PDFViewer_scrollPageIntoView`, since `PDFViewer.linkService.page` is no longer accessed.
----------
Currently the `pageviewer` components example doesn't work correctly (an error is printed in the console), since no `linkService` is present when the `AnnotationsLayerBuilder` is created.
*Note:* Given that this uses the `SimpleLinkService`, clicking on e.g. internal links won't actually do anything. However, given that internal links (and similar features) are pretty much useless when only *one* page is loaded the `pageviewer` example, I don't think that really matters.
Also, using the complete `PDFLinkService` would require a `PDFViewer` instance. That would significantly complicate the example, thus making it both less clear and less self contained.
2015-06-12 05:20:04 +09:00
|
|
|
linkService: new SimpleLinkService(),
|
2017-05-04 10:05:53 +09:00
|
|
|
l10n,
|
2014-12-18 05:47:14 +09:00
|
|
|
});
|
|
|
|
}
|
2017-04-24 03:52:58 +09:00
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export {
|
|
|
|
AnnotationLayerBuilder,
|
|
|
|
DefaultAnnotationLayerFactory,
|
|
|
|
};
|