2014-09-29 11:05:28 -05: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-12-25 15:59:37 +01:00
|
|
|
import { AnnotationLayer } from "pdfjs-lib";
|
2021-02-22 12:43:16 +01:00
|
|
|
import { NullL10n } from "./l10n_utils.js";
|
2020-01-02 12:00:16 +01:00
|
|
|
import { SimpleLinkService } from "./pdf_link_service.js";
|
2016-04-08 12:34:27 -05:00
|
|
|
|
2014-09-29 11:05:28 -05:00
|
|
|
/**
|
2015-12-17 12:54:53 +01:00
|
|
|
* @typedef {Object} AnnotationLayerBuilderOptions
|
2014-09-29 11:05:28 -05:00
|
|
|
* @property {HTMLDivElement} pageDiv
|
|
|
|
|
* @property {PDFPage} pdfPage
|
2020-07-31 16:13:26 +02:00
|
|
|
* @property {AnnotationStorage} [annotationStorage]
|
2019-10-12 16:30:32 +02:00
|
|
|
* @property {string} [imageResourcesPath] - Path for image resources, mainly
|
|
|
|
|
* for annotation icons. Include trailing slash.
|
2016-09-17 19:44:25 +02:00
|
|
|
* @property {boolean} renderInteractiveForms
|
2014-09-29 11:05:28 -05:00
|
|
|
* @property {IPDFLinkService} linkService
|
2016-02-14 20:44:00 +01:00
|
|
|
* @property {DownloadManager} downloadManager
|
2017-05-03 20:05:53 -05:00
|
|
|
* @property {IL10n} l10n - Localization service.
|
2020-10-28 19:16:56 +01:00
|
|
|
* @property {boolean} [enableScripting]
|
|
|
|
|
* @property {Promise<boolean>} [hasJSActionsPromise]
|
2020-12-17 12:55:58 +01:00
|
|
|
* @property {Object} [mouseState]
|
2014-09-29 11:05:28 -05:00
|
|
|
*/
|
|
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
class AnnotationLayerBuilder {
|
2014-09-29 11:05:28 -05:00
|
|
|
/**
|
2015-12-17 12:54:53 +01:00
|
|
|
* @param {AnnotationLayerBuilderOptions} options
|
2014-09-29 11:05:28 -05:00
|
|
|
*/
|
2019-12-25 15:59:37 +01:00
|
|
|
constructor({
|
|
|
|
|
pageDiv,
|
|
|
|
|
pdfPage,
|
|
|
|
|
linkService,
|
|
|
|
|
downloadManager,
|
2020-07-31 16:13:26 +02:00
|
|
|
annotationStorage = null,
|
2019-12-25 15:59:37 +01:00
|
|
|
imageResourcesPath = "",
|
2020-08-22 13:58:33 +02:00
|
|
|
renderInteractiveForms = true,
|
2019-12-25 15:59:37 +01:00
|
|
|
l10n = NullL10n,
|
2020-10-28 19:16:56 +01:00
|
|
|
enableScripting = false,
|
|
|
|
|
hasJSActionsPromise = null,
|
2020-11-18 14:59:57 +01:00
|
|
|
mouseState = null,
|
2019-12-25 15:59:37 +01:00
|
|
|
}) {
|
2017-06-30 12:55:22 +02:00
|
|
|
this.pageDiv = pageDiv;
|
|
|
|
|
this.pdfPage = pdfPage;
|
|
|
|
|
this.linkService = linkService;
|
|
|
|
|
this.downloadManager = downloadManager;
|
2018-02-13 13:17:11 +01:00
|
|
|
this.imageResourcesPath = imageResourcesPath;
|
2017-06-30 12:55:22 +02:00
|
|
|
this.renderInteractiveForms = renderInteractiveForms;
|
|
|
|
|
this.l10n = l10n;
|
2020-07-22 13:55:52 +02:00
|
|
|
this.annotationStorage = annotationStorage;
|
2020-10-28 19:16:56 +01:00
|
|
|
this.enableScripting = enableScripting;
|
|
|
|
|
this._hasJSActionsPromise = hasJSActionsPromise;
|
2020-11-18 14:59:57 +01:00
|
|
|
this._mouseState = mouseState;
|
2014-09-29 11:05:28 -05:00
|
|
|
|
|
|
|
|
this.div = null;
|
2017-10-06 17:26:54 +02:00
|
|
|
this._cancelled = false;
|
2014-09-29 11:05:28 -05:00
|
|
|
}
|
2015-12-03 00:20:18 +01:00
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
/**
|
|
|
|
|
* @param {PageViewport} viewport
|
|
|
|
|
* @param {string} intent (default value is 'display')
|
2020-08-17 13:19:03 -07:00
|
|
|
* @returns {Promise<void>} A promise that is resolved when rendering of the
|
|
|
|
|
* annotations is complete.
|
2017-04-23 20:52:58 +02:00
|
|
|
*/
|
2019-12-25 15:59:37 +01:00
|
|
|
render(viewport, intent = "display") {
|
2020-10-28 19:16:56 +01:00
|
|
|
return Promise.all([
|
|
|
|
|
this.pdfPage.getAnnotations({ intent }),
|
|
|
|
|
this._hasJSActionsPromise,
|
|
|
|
|
]).then(([annotations, hasJSActions = false]) => {
|
2017-10-06 17:26:54 +02:00
|
|
|
if (this._cancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-31 16:42:49 +02:00
|
|
|
if (annotations.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-06 17:26:54 +02:00
|
|
|
|
2019-12-27 00:22:32 +01:00
|
|
|
const parameters = {
|
2019-12-25 15:59:37 +01:00
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
2017-04-23 20:52:58 +02:00
|
|
|
div: this.div,
|
|
|
|
|
annotations,
|
|
|
|
|
page: this.pdfPage,
|
2018-02-13 13:17:11 +01:00
|
|
|
imageResourcesPath: this.imageResourcesPath,
|
2017-04-23 20:52:58 +02:00
|
|
|
renderInteractiveForms: this.renderInteractiveForms,
|
|
|
|
|
linkService: this.linkService,
|
|
|
|
|
downloadManager: this.downloadManager,
|
2020-07-22 13:55:52 +02:00
|
|
|
annotationStorage: this.annotationStorage,
|
2020-10-28 19:16:56 +01:00
|
|
|
enableScripting: this.enableScripting,
|
|
|
|
|
hasJSActions,
|
2020-11-18 14:59:57 +01:00
|
|
|
mouseState: this._mouseState,
|
2015-11-22 13:56:52 +01:00
|
|
|
};
|
2014-09-29 11:05:28 -05:00
|
|
|
|
2017-04-23 20:52:58 +02: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.
|
2019-12-25 15:59:37 +01:00
|
|
|
this.div = document.createElement("div");
|
|
|
|
|
this.div.className = "annotationLayer";
|
2017-04-23 20:52:58 +02:00
|
|
|
this.pageDiv.appendChild(this.div);
|
|
|
|
|
parameters.div = this.div;
|
|
|
|
|
|
|
|
|
|
AnnotationLayer.render(parameters);
|
2017-05-03 20:05:53 -05:00
|
|
|
this.l10n.translate(this.div);
|
2014-09-29 11:05:28 -05:00
|
|
|
}
|
2017-04-23 20:52:58 +02:00
|
|
|
});
|
|
|
|
|
}
|
2015-12-03 00:20:18 +01:00
|
|
|
|
2017-10-06 17:26:54 +02:00
|
|
|
cancel() {
|
|
|
|
|
this._cancelled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
hide() {
|
|
|
|
|
if (!this.div) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-08 00:21:49 +01:00
|
|
|
this.div.hidden = true;
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-12-17 14:47:14 -06:00
|
|
|
|
|
|
|
|
/**
|
2015-12-17 12:54:53 +01:00
|
|
|
* @implements IPDFAnnotationLayerFactory
|
2014-12-17 14:47:14 -06:00
|
|
|
*/
|
2017-04-23 20:52:58 +02:00
|
|
|
class DefaultAnnotationLayerFactory {
|
2014-12-17 14:47:14 -06:00
|
|
|
/**
|
|
|
|
|
* @param {HTMLDivElement} pageDiv
|
|
|
|
|
* @param {PDFPage} pdfPage
|
2020-07-31 16:13:26 +02:00
|
|
|
* @param {AnnotationStorage} [annotationStorage]
|
2019-10-12 16:30:32 +02:00
|
|
|
* @param {string} [imageResourcesPath] - Path for image resources, mainly
|
|
|
|
|
* for annotation icons. Include trailing slash.
|
2016-09-17 19:44:25 +02:00
|
|
|
* @param {boolean} renderInteractiveForms
|
2017-05-03 20:05:53 -05:00
|
|
|
* @param {IL10n} l10n
|
2020-11-14 13:38:36 +01:00
|
|
|
* @param {boolean} [enableScripting]
|
|
|
|
|
* @param {Promise<boolean>} [hasJSActionsPromise]
|
2020-11-18 14:59:57 +01:00
|
|
|
* @param {Object} [mouseState]
|
2015-12-17 12:54:53 +01:00
|
|
|
* @returns {AnnotationLayerBuilder}
|
2014-12-17 14:47:14 -06:00
|
|
|
*/
|
2019-12-25 15:59:37 +01:00
|
|
|
createAnnotationLayerBuilder(
|
|
|
|
|
pageDiv,
|
|
|
|
|
pdfPage,
|
2020-07-31 16:13:26 +02:00
|
|
|
annotationStorage = null,
|
2019-12-25 15:59:37 +01:00
|
|
|
imageResourcesPath = "",
|
2020-08-22 13:58:33 +02:00
|
|
|
renderInteractiveForms = true,
|
2020-10-28 19:16:56 +01:00
|
|
|
l10n = NullL10n,
|
|
|
|
|
enableScripting = false,
|
2020-11-18 14:59:57 +01:00
|
|
|
hasJSActionsPromise = null,
|
|
|
|
|
mouseState = null
|
2019-12-25 15:59:37 +01:00
|
|
|
) {
|
2015-12-17 12:54:53 +01:00
|
|
|
return new AnnotationLayerBuilder({
|
2017-04-23 20:52:58 +02:00
|
|
|
pageDiv,
|
|
|
|
|
pdfPage,
|
2018-02-13 13:17:11 +01:00
|
|
|
imageResourcesPath,
|
2017-04-23 20:52:58 +02:00
|
|
|
renderInteractiveForms,
|
2015-06-11 22:20:04 +02:00
|
|
|
linkService: new SimpleLinkService(),
|
2017-05-03 20:05:53 -05:00
|
|
|
l10n,
|
2020-07-22 13:55:52 +02:00
|
|
|
annotationStorage,
|
2020-10-28 19:16:56 +01:00
|
|
|
enableScripting,
|
|
|
|
|
hasJSActionsPromise,
|
2020-11-18 14:59:57 +01:00
|
|
|
mouseState,
|
2014-12-17 14:47:14 -06:00
|
|
|
});
|
|
|
|
|
}
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
2016-04-08 12:34:27 -05:00
|
|
|
|
2019-12-25 15:59:37 +01:00
|
|
|
export { AnnotationLayerBuilder, DefaultAnnotationLayerFactory };
|