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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-14 23:59:17 +01:00
|
|
|
/** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
|
2023-08-31 12:01:33 +02:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/annotation_storage").AnnotationStorage} AnnotationStorage */
|
2021-12-14 23:59:17 +01:00
|
|
|
/** @typedef {import("./interfaces").IDownloadManager} IDownloadManager */
|
|
|
|
|
/** @typedef {import("./interfaces").IPDFLinkService} IPDFLinkService */
|
2022-07-28 17:59:03 +02:00
|
|
|
// eslint-disable-next-line max-len
|
2023-08-31 12:01:33 +02:00
|
|
|
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
|
2024-04-09 15:25:49 +02:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
|
/** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
|
2021-08-25 18:44:06 -04:00
|
|
|
|
2017-05-30 20:38:22 -05:00
|
|
|
import { AnnotationLayer } from "pdfjs-lib";
|
2022-10-30 17:36:20 +01:00
|
|
|
import { PresentationModeState } from "./ui_utils.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
|
2021-12-14 23:59:17 +01:00
|
|
|
* @property {PDFPageProxy} 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.
|
2021-08-08 14:36:28 +02:00
|
|
|
* @property {boolean} renderForms
|
2014-09-29 11:05:28 -05:00
|
|
|
* @property {IPDFLinkService} linkService
|
2023-09-27 12:47:50 +05:30
|
|
|
* @property {IDownloadManager} [downloadManager]
|
2020-10-28 19:16:56 +01:00
|
|
|
* @property {boolean} [enableScripting]
|
|
|
|
|
* @property {Promise<boolean>} [hasJSActionsPromise]
|
2021-09-14 12:06:28 +02:00
|
|
|
* @property {Promise<Object<string, Array<Object>> | null>}
|
|
|
|
|
* [fieldObjectsPromise]
|
2021-12-14 23:59:17 +01:00
|
|
|
* @property {Map<string, HTMLCanvasElement>} [annotationCanvasMap]
|
2022-12-07 16:12:48 +01:00
|
|
|
* @property {TextAccessibilityManager} [accessibilityManager]
|
2024-04-09 15:25:49 +02:00
|
|
|
* @property {AnnotationEditorUIManager} [annotationEditorUIManager]
|
2024-03-11 17:03:44 +01:00
|
|
|
* @property {function} [onAppend]
|
2014-09-29 11:05:28 -05:00
|
|
|
*/
|
|
|
|
|
|
2017-04-23 20:52:58 +02:00
|
|
|
class AnnotationLayerBuilder {
|
2024-03-11 17:03:44 +01:00
|
|
|
#onAppend = null;
|
|
|
|
|
|
2024-04-21 09:41:21 +02:00
|
|
|
#eventAbortController = null;
|
2022-10-30 17:36:20 +01:00
|
|
|
|
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
|
|
|
*/
|
2017-06-30 12:55:22 +02:00
|
|
|
constructor({
|
|
|
|
|
pdfPage,
|
|
|
|
|
linkService,
|
|
|
|
|
downloadManager,
|
2020-07-31 16:13:26 +02:00
|
|
|
annotationStorage = null,
|
2018-02-13 13:17:11 +01:00
|
|
|
imageResourcesPath = "",
|
2021-08-08 14:36:28 +02:00
|
|
|
renderForms = true,
|
2020-10-28 19:16:56 +01:00
|
|
|
enableScripting = false,
|
|
|
|
|
hasJSActionsPromise = null,
|
2021-09-14 12:06:28 +02:00
|
|
|
fieldObjectsPromise = null,
|
2021-11-06 18:36:49 +01:00
|
|
|
annotationCanvasMap = null,
|
2022-07-28 17:59:03 +02:00
|
|
|
accessibilityManager = null,
|
2024-04-09 15:25:49 +02:00
|
|
|
annotationEditorUIManager = null,
|
2024-03-11 17:03:44 +01:00
|
|
|
onAppend = null,
|
2018-02-13 13:17:11 +01:00
|
|
|
}) {
|
2017-06-30 12:55:22 +02:00
|
|
|
this.pdfPage = pdfPage;
|
|
|
|
|
this.linkService = linkService;
|
|
|
|
|
this.downloadManager = downloadManager;
|
2018-02-13 13:17:11 +01:00
|
|
|
this.imageResourcesPath = imageResourcesPath;
|
2021-08-08 14:36:28 +02:00
|
|
|
this.renderForms = renderForms;
|
2020-07-22 13:55:52 +02:00
|
|
|
this.annotationStorage = annotationStorage;
|
2020-10-28 19:16:56 +01:00
|
|
|
this.enableScripting = enableScripting;
|
2022-12-11 14:51:28 +01:00
|
|
|
this._hasJSActionsPromise = hasJSActionsPromise || Promise.resolve(false);
|
|
|
|
|
this._fieldObjectsPromise = fieldObjectsPromise || Promise.resolve(null);
|
2021-11-06 18:36:49 +01:00
|
|
|
this._annotationCanvasMap = annotationCanvasMap;
|
2022-07-28 17:59:03 +02:00
|
|
|
this._accessibilityManager = accessibilityManager;
|
2024-04-09 15:25:49 +02:00
|
|
|
this._annotationEditorUIManager = annotationEditorUIManager;
|
2024-03-11 17:03:44 +01:00
|
|
|
this.#onAppend = onAppend;
|
2014-09-29 11:05:28 -05:00
|
|
|
|
2023-06-01 20:31:05 +02:00
|
|
|
this.annotationLayer = null;
|
2014-09-29 11:05:28 -05:00
|
|
|
this.div = null;
|
2017-10-06 17:26:54 +02:00
|
|
|
this._cancelled = false;
|
2022-10-30 17:36:20 +01:00
|
|
|
this._eventBus = linkService.eventBus;
|
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
|
2024-09-03 16:49:22 +02:00
|
|
|
* @param {Object} options
|
2017-04-23 20:52:58 +02:00
|
|
|
* @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
|
|
|
*/
|
2024-09-03 16:49:22 +02:00
|
|
|
async render(viewport, options, intent = "display") {
|
2022-12-11 14:51:28 +01:00
|
|
|
if (this.div) {
|
2023-06-01 20:31:05 +02:00
|
|
|
if (this._cancelled || !this.annotationLayer) {
|
2022-12-11 14:51:28 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// If an annotationLayer already exists, refresh its children's
|
|
|
|
|
// transformation matrices.
|
2023-06-01 20:31:05 +02:00
|
|
|
this.annotationLayer.update({
|
2022-12-11 14:51:28 +01:00
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
|
|
|
|
});
|
2021-09-14 12:06:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2014-09-29 11:05:28 -05:00
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
const [annotations, hasJSActions, fieldObjects] = await Promise.all([
|
|
|
|
|
this.pdfPage.getAnnotations({ intent }),
|
|
|
|
|
this._hasJSActionsPromise,
|
|
|
|
|
this._fieldObjectsPromise,
|
|
|
|
|
]);
|
|
|
|
|
if (this._cancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create an annotation layer div and render the annotations
|
|
|
|
|
// if there is at least one annotation.
|
2023-06-01 20:31:05 +02:00
|
|
|
const div = (this.div = document.createElement("div"));
|
|
|
|
|
div.className = "annotationLayer";
|
2024-03-11 17:03:44 +01:00
|
|
|
this.#onAppend?.(div);
|
2022-12-11 14:51:28 +01:00
|
|
|
|
2023-06-01 20:31:05 +02:00
|
|
|
if (annotations.length === 0) {
|
2022-12-11 14:51:28 +01:00
|
|
|
this.hide();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-01 20:31:05 +02:00
|
|
|
|
|
|
|
|
this.annotationLayer = new AnnotationLayer({
|
|
|
|
|
div,
|
|
|
|
|
accessibilityManager: this._accessibilityManager,
|
|
|
|
|
annotationCanvasMap: this._annotationCanvasMap,
|
2024-04-09 15:25:49 +02:00
|
|
|
annotationEditorUIManager: this._annotationEditorUIManager,
|
2023-06-15 11:59:59 +02:00
|
|
|
page: this.pdfPage,
|
|
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
2024-09-03 16:49:22 +02:00
|
|
|
structTreeLayer: options?.structTreeLayer || null,
|
2023-06-01 20:31:05 +02:00
|
|
|
});
|
|
|
|
|
|
2023-06-20 16:46:51 +02:00
|
|
|
await this.annotationLayer.render({
|
2021-09-14 12:06:28 +02:00
|
|
|
annotations,
|
|
|
|
|
imageResourcesPath: this.imageResourcesPath,
|
|
|
|
|
renderForms: this.renderForms,
|
|
|
|
|
linkService: this.linkService,
|
|
|
|
|
downloadManager: this.downloadManager,
|
|
|
|
|
annotationStorage: this.annotationStorage,
|
|
|
|
|
enableScripting: this.enableScripting,
|
|
|
|
|
hasJSActions,
|
|
|
|
|
fieldObjects,
|
2022-12-11 14:51:28 +01:00
|
|
|
});
|
2017-04-23 20:52:58 +02:00
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
// Ensure that interactive form elements in the annotationLayer are
|
|
|
|
|
// disabled while PresentationMode is active (see issue 12232).
|
|
|
|
|
if (this.linkService.isInPresentationMode) {
|
|
|
|
|
this.#updatePresentationModeState(PresentationModeState.FULLSCREEN);
|
|
|
|
|
}
|
2024-04-21 09:41:21 +02:00
|
|
|
if (!this.#eventAbortController) {
|
|
|
|
|
this.#eventAbortController = new AbortController();
|
|
|
|
|
|
2022-12-11 14:51:28 +01:00
|
|
|
this._eventBus?._on(
|
|
|
|
|
"presentationmodechanged",
|
2024-04-21 09:41:21 +02:00
|
|
|
evt => {
|
|
|
|
|
this.#updatePresentationModeState(evt.state);
|
|
|
|
|
},
|
|
|
|
|
{ signal: this.#eventAbortController.signal }
|
2022-12-11 14:51:28 +01:00
|
|
|
);
|
2021-09-14 12:06:28 +02: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;
|
2022-10-30 17:36:20 +01:00
|
|
|
|
2024-04-21 09:41:21 +02:00
|
|
|
this.#eventAbortController?.abort();
|
|
|
|
|
this.#eventAbortController = null;
|
2017-10-06 17:26:54 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2022-10-30 17:36:20 +01:00
|
|
|
|
2024-05-21 14:41:07 +02:00
|
|
|
hasEditableAnnotations() {
|
|
|
|
|
return !!this.annotationLayer?.hasEditableAnnotations();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 17:36:20 +01:00
|
|
|
#updatePresentationModeState(state) {
|
|
|
|
|
if (!this.div) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let disableFormElements = false;
|
|
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
|
case PresentationModeState.FULLSCREEN:
|
|
|
|
|
disableFormElements = true;
|
|
|
|
|
break;
|
|
|
|
|
case PresentationModeState.NORMAL:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (const section of this.div.childNodes) {
|
|
|
|
|
if (section.hasAttribute("data-internal-link")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
section.inert = disableFormElements;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-23 20:52:58 +02:00
|
|
|
}
|
2014-12-17 14:47:14 -06:00
|
|
|
|
2021-12-15 13:54:29 +01:00
|
|
|
export { AnnotationLayerBuilder };
|