2016-11-19 03:50:29 +09:00
|
|
|
/* Copyright 2016 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.
|
|
|
|
*/
|
|
|
|
|
2023-12-01 00:21:13 +09:00
|
|
|
import { AnnotationEditorType, ColorPicker, noContextMenu } from "pdfjs-lib";
|
2017-03-28 08:07:27 +09:00
|
|
|
import {
|
2017-05-04 10:05:53 +09:00
|
|
|
DEFAULT_SCALE,
|
|
|
|
DEFAULT_SCALE_VALUE,
|
|
|
|
MAX_SCALE,
|
|
|
|
MIN_SCALE,
|
2023-04-13 19:36:39 +09:00
|
|
|
toggleCheckedBtn,
|
2020-01-02 20:00:16 +09:00
|
|
|
} from "./ui_utils.js";
|
2016-11-19 03:50:29 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ToolbarOptions
|
|
|
|
* @property {HTMLDivElement} container - Container for the secondary toolbar.
|
|
|
|
* @property {HTMLSpanElement} numPages - Label that contains number of pages.
|
|
|
|
* @property {HTMLInputElement} pageNumber - Control for display and user input
|
|
|
|
* of the current page number.
|
|
|
|
* @property {HTMLSelectElement} scaleSelect - Scale selection control.
|
2021-09-15 22:00:56 +09:00
|
|
|
* Its width is adjusted, when necessary, on UI localization.
|
2016-11-19 03:50:29 +09:00
|
|
|
* @property {HTMLOptionElement} customScaleOption - The item used to display
|
|
|
|
* a non-predefined scale.
|
|
|
|
* @property {HTMLButtonElement} previous - Button to go to the previous page.
|
|
|
|
* @property {HTMLButtonElement} next - Button to go to the next page.
|
|
|
|
* @property {HTMLButtonElement} zoomIn - Button to zoom in the pages.
|
|
|
|
* @property {HTMLButtonElement} zoomOut - Button to zoom out the pages.
|
|
|
|
* @property {HTMLButtonElement} viewFind - Button to open find bar.
|
2022-06-05 04:32:27 +09:00
|
|
|
* @property {HTMLButtonElement} editorFreeTextButton - Button to switch to
|
|
|
|
* FreeText editing.
|
2016-11-19 03:50:29 +09:00
|
|
|
* @property {HTMLButtonElement} download - Button to download the document.
|
|
|
|
*/
|
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
class Toolbar {
|
2023-12-26 18:56:38 +09:00
|
|
|
#opts;
|
|
|
|
|
2016-11-19 03:50:29 +09:00
|
|
|
/**
|
|
|
|
* @param {ToolbarOptions} options
|
|
|
|
* @param {EventBus} eventBus
|
|
|
|
*/
|
2023-10-19 23:30:57 +09:00
|
|
|
constructor(options, eventBus) {
|
2023-12-26 18:56:38 +09:00
|
|
|
this.#opts = options;
|
2016-11-19 03:50:29 +09:00
|
|
|
this.eventBus = eventBus;
|
2023-12-26 18:56:38 +09:00
|
|
|
const buttons = [
|
2019-11-16 07:19:35 +09:00
|
|
|
{ element: options.previous, eventName: "previouspage" },
|
|
|
|
{ element: options.next, eventName: "nextpage" },
|
|
|
|
{ element: options.zoomIn, eventName: "zoomin" },
|
|
|
|
{ element: options.zoomOut, eventName: "zoomout" },
|
|
|
|
{ element: options.print, eventName: "print" },
|
|
|
|
{ element: options.download, eventName: "download" },
|
2022-06-01 17:38:08 +09:00
|
|
|
{
|
|
|
|
element: options.editorFreeTextButton,
|
|
|
|
eventName: "switchannotationeditormode",
|
2022-08-26 06:18:02 +09:00
|
|
|
eventDetails: {
|
|
|
|
get mode() {
|
|
|
|
const { classList } = options.editorFreeTextButton;
|
|
|
|
return classList.contains("toggled")
|
|
|
|
? AnnotationEditorType.NONE
|
|
|
|
: AnnotationEditorType.FREETEXT;
|
|
|
|
},
|
|
|
|
},
|
2022-06-01 17:38:08 +09:00
|
|
|
},
|
2023-11-23 03:02:42 +09:00
|
|
|
{
|
|
|
|
element: options.editorHighlightButton,
|
|
|
|
eventName: "switchannotationeditormode",
|
|
|
|
eventDetails: {
|
|
|
|
get mode() {
|
|
|
|
const { classList } = options.editorHighlightButton;
|
|
|
|
return classList.contains("toggled")
|
|
|
|
? AnnotationEditorType.NONE
|
|
|
|
: AnnotationEditorType.HIGHLIGHT;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-06-05 06:28:19 +09:00
|
|
|
{
|
|
|
|
element: options.editorInkButton,
|
|
|
|
eventName: "switchannotationeditormode",
|
2022-08-26 06:18:02 +09:00
|
|
|
eventDetails: {
|
|
|
|
get mode() {
|
|
|
|
const { classList } = options.editorInkButton;
|
|
|
|
return classList.contains("toggled")
|
|
|
|
? AnnotationEditorType.NONE
|
|
|
|
: AnnotationEditorType.INK;
|
|
|
|
},
|
|
|
|
},
|
2022-06-05 06:28:19 +09:00
|
|
|
},
|
2023-06-22 19:16:07 +09:00
|
|
|
{
|
|
|
|
element: options.editorStampButton,
|
|
|
|
eventName: "switchannotationeditormode",
|
|
|
|
eventDetails: {
|
|
|
|
get mode() {
|
|
|
|
const { classList } = options.editorStampButton;
|
|
|
|
return classList.contains("toggled")
|
|
|
|
? AnnotationEditorType.NONE
|
|
|
|
: AnnotationEditorType.STAMP;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-16 07:19:35 +09:00
|
|
|
];
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2019-11-16 07:19:35 +09:00
|
|
|
// Bind the event listeners for click and various other actions.
|
2023-12-26 18:56:38 +09:00
|
|
|
this.#bindListeners(buttons);
|
2022-08-10 04:33:18 +09:00
|
|
|
|
2023-12-01 00:21:13 +09:00
|
|
|
if (options.editorHighlightColorPicker) {
|
2023-12-26 18:56:38 +09:00
|
|
|
eventBus._on(
|
2023-12-07 00:10:29 +09:00
|
|
|
"annotationeditoruimanager",
|
|
|
|
({ uiManager }) => {
|
|
|
|
this.#setAnnotationEditorUIManager(
|
|
|
|
uiManager,
|
|
|
|
options.editorHighlightColorPicker
|
|
|
|
);
|
|
|
|
},
|
|
|
|
// Once the color picker has been added, we don't want to add it again.
|
|
|
|
{ once: true }
|
|
|
|
);
|
2023-12-01 00:21:13 +09:00
|
|
|
}
|
|
|
|
|
2024-02-22 02:36:07 +09:00
|
|
|
eventBus._on("showannotationeditorui", ({ mode }) => {
|
|
|
|
switch (mode) {
|
|
|
|
case AnnotationEditorType.HIGHLIGHT:
|
|
|
|
options.editorHighlightButton.click();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-10 04:33:18 +09:00
|
|
|
this.reset();
|
2016-11-19 03:50:29 +09:00
|
|
|
}
|
|
|
|
|
2023-12-01 00:21:13 +09:00
|
|
|
#setAnnotationEditorUIManager(uiManager, parentContainer) {
|
|
|
|
const colorPicker = new ColorPicker({ uiManager });
|
|
|
|
uiManager.setMainHighlightColorPicker(colorPicker);
|
|
|
|
parentContainer.append(colorPicker.renderMainDropdown());
|
|
|
|
}
|
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPageNumber(pageNumber, pageLabel) {
|
|
|
|
this.pageNumber = pageNumber;
|
|
|
|
this.pageLabel = pageLabel;
|
2022-08-10 04:32:02 +09:00
|
|
|
this.#updateUIState(false);
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPagesCount(pagesCount, hasPageLabels) {
|
|
|
|
this.pagesCount = pagesCount;
|
|
|
|
this.hasPageLabels = hasPageLabels;
|
2022-08-10 04:32:02 +09:00
|
|
|
this.#updateUIState(true);
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPageScale(pageScaleValue, pageScale) {
|
2018-10-05 19:13:48 +09:00
|
|
|
this.pageScaleValue = (pageScaleValue || pageScale).toString();
|
2017-06-18 06:07:17 +09:00
|
|
|
this.pageScale = pageScale;
|
2022-08-10 04:32:02 +09:00
|
|
|
this.#updateUIState(false);
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
reset() {
|
|
|
|
this.pageNumber = 0;
|
|
|
|
this.pageLabel = null;
|
|
|
|
this.hasPageLabels = false;
|
|
|
|
this.pagesCount = 0;
|
|
|
|
this.pageScaleValue = DEFAULT_SCALE_VALUE;
|
|
|
|
this.pageScale = DEFAULT_SCALE;
|
2022-08-10 04:32:02 +09:00
|
|
|
this.#updateUIState(true);
|
2019-08-01 23:17:34 +09:00
|
|
|
this.updateLoadingIndicatorState();
|
[editor] Support disabling of editing when `pdfjs.enablePermissions` is set (issue 15049)
For encrypted PDF documents without the required permissions set, this patch adds support for disabling of Annotation-editing. However, please note that it also requires that the `pdfjs.enablePermissions` preference is set to `true` (since PDF document permissions could be seen as user hostile).[1]
As I started looking at the issue, it soon became clear that *only* trying to fix the issue without slightly re-factor the surrounding code would be somewhat difficult.
The following is an overview of the changes in this patch; sorry about the size/scope of this!
- Use a new `AnnotationEditorUIManager`-instance *for each* PDF document opened in the GENERIC viewer, to prevent user-added Annotations from "leaking" from one document into the next.
- Re-factor the `BaseViewer.#initializePermissions`-method, to simplify handling of temporarily disabled modes (e.g. for both Annotation-rendering and Annotation-editing).
- When editing is enabled, let the Editor-buttons be `disabled` until the document has loaded. This way we avoid the buttons becoming clickable temporarily, for PDF documents that use permissions.
- Slightly re-factor how the Editor-buttons are shown/hidden in the viewer, and reset the toolbar-state when a new PDF document is opened.
- Flip the order of the Editor-buttons and the pre-exising toolbarButtons in the "toolbarViewerRight"-div. (To help reduce the size, a little bit, for the PR that adds new Editor-toolbars.)
- Enable editing by default in the development viewer, i.e. `gulp server`, since having to (repeatedly) do that manually becomes annoying after a while.
- Finally, support disabling of editing when `pdfjs.enablePermissions` is set; fixes issue 15049.
---
[1] Either manually with `about:config`, or using e.g. a [Group Policy](https://github.com/mozilla/policy-templates).
2022-06-21 01:08:41 +09:00
|
|
|
|
|
|
|
// Reset the Editor buttons too, since they're document specific.
|
2023-12-26 18:56:38 +09:00
|
|
|
this.#editorModeChanged({ mode: AnnotationEditorType.DISABLE });
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
#bindListeners(buttons) {
|
|
|
|
const { eventBus } = this;
|
|
|
|
const { pageNumber, scaleSelect } = this.#opts;
|
2019-11-16 07:19:35 +09:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
// The buttons within the toolbar.
|
2023-12-26 18:56:38 +09:00
|
|
|
for (const { element, eventName, eventDetails } of buttons) {
|
2019-11-16 07:19:35 +09:00
|
|
|
element.addEventListener("click", evt => {
|
|
|
|
if (eventName !== null) {
|
2023-12-26 18:56:38 +09:00
|
|
|
eventBus.dispatch(eventName, {
|
2023-10-06 02:25:36 +09:00
|
|
|
source: this,
|
|
|
|
...eventDetails,
|
|
|
|
// evt.detail is the number of clicks.
|
|
|
|
isFromKeyboard: evt.detail === 0,
|
|
|
|
});
|
2019-11-16 07:19:35 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// The non-button elements within the toolbar.
|
2020-04-14 19:28:14 +09:00
|
|
|
pageNumber.addEventListener("click", function () {
|
2017-06-18 06:07:17 +09:00
|
|
|
this.select();
|
|
|
|
});
|
2020-04-14 19:28:14 +09:00
|
|
|
pageNumber.addEventListener("change", function () {
|
2023-12-26 18:56:38 +09:00
|
|
|
eventBus.dispatch("pagenumberchanged", {
|
2017-06-18 06:07:17 +09:00
|
|
|
source: self,
|
|
|
|
value: this.value,
|
2016-11-19 03:50:29 +09:00
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2020-04-14 19:28:14 +09:00
|
|
|
scaleSelect.addEventListener("change", function () {
|
2017-06-18 06:07:17 +09:00
|
|
|
if (this.value === "custom") {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-26 18:56:38 +09:00
|
|
|
eventBus.dispatch("scalechanged", {
|
2017-06-18 06:07:17 +09:00
|
|
|
source: self,
|
|
|
|
value: this.value,
|
2016-11-19 03:50:29 +09:00
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2021-05-20 22:17:01 +09:00
|
|
|
// Here we depend on browsers dispatching the "click" event *after* the
|
|
|
|
// "change" event, when the <select>-element changes.
|
2023-12-26 18:56:38 +09:00
|
|
|
scaleSelect.addEventListener("click", function ({ target }) {
|
2021-05-20 22:17:01 +09:00
|
|
|
// Remove focus when an <option>-element was *clicked*, to improve the UX
|
|
|
|
// for mouse users (fixes bug 1300525 and issue 4923).
|
|
|
|
if (
|
|
|
|
this.value === self.pageScaleValue &&
|
|
|
|
target.tagName.toUpperCase() === "OPTION"
|
|
|
|
) {
|
|
|
|
this.blur();
|
|
|
|
}
|
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
// Suppress context menus for some controls.
|
2023-09-23 22:37:13 +09:00
|
|
|
scaleSelect.oncontextmenu = noContextMenu;
|
2017-06-18 06:07:17 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
eventBus._on(
|
|
|
|
"annotationeditormodechanged",
|
|
|
|
this.#editorModeChanged.bind(this)
|
|
|
|
);
|
2022-06-01 17:38:08 +09:00
|
|
|
}
|
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
#editorModeChanged({ mode }) {
|
|
|
|
const {
|
|
|
|
editorFreeTextButton,
|
|
|
|
editorFreeTextParamsToolbar,
|
|
|
|
editorHighlightButton,
|
|
|
|
editorHighlightParamsToolbar,
|
|
|
|
editorInkButton,
|
|
|
|
editorInkParamsToolbar,
|
|
|
|
editorStampButton,
|
|
|
|
editorStampParamsToolbar,
|
|
|
|
} = this.#opts;
|
2023-04-13 19:36:39 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
toggleCheckedBtn(
|
|
|
|
editorFreeTextButton,
|
|
|
|
mode === AnnotationEditorType.FREETEXT,
|
|
|
|
editorFreeTextParamsToolbar
|
|
|
|
);
|
|
|
|
toggleCheckedBtn(
|
|
|
|
editorHighlightButton,
|
|
|
|
mode === AnnotationEditorType.HIGHLIGHT,
|
|
|
|
editorHighlightParamsToolbar
|
|
|
|
);
|
|
|
|
toggleCheckedBtn(
|
|
|
|
editorInkButton,
|
|
|
|
mode === AnnotationEditorType.INK,
|
|
|
|
editorInkParamsToolbar
|
|
|
|
);
|
|
|
|
toggleCheckedBtn(
|
|
|
|
editorStampButton,
|
|
|
|
mode === AnnotationEditorType.STAMP,
|
|
|
|
editorStampParamsToolbar
|
|
|
|
);
|
2022-08-10 04:32:02 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
const isDisable = mode === AnnotationEditorType.DISABLE;
|
|
|
|
editorFreeTextButton.disabled = isDisable;
|
|
|
|
editorHighlightButton.disabled = isDisable;
|
|
|
|
editorInkButton.disabled = isDisable;
|
|
|
|
editorStampButton.disabled = isDisable;
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
|
|
|
|
2022-08-10 04:32:02 +09:00
|
|
|
#updateUIState(resetNumPages = false) {
|
2023-12-26 18:56:38 +09:00
|
|
|
const { pageNumber, pagesCount, pageScaleValue, pageScale } = this;
|
|
|
|
const opts = this.#opts;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
if (resetNumPages) {
|
2016-11-19 03:50:29 +09:00
|
|
|
if (this.hasPageLabels) {
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.pageNumber.type = "text";
|
2023-10-19 23:30:57 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.numPages.setAttribute("data-l10n-id", "pdfjs-page-of-pages");
|
2017-06-18 06:07:17 +09:00
|
|
|
} else {
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.pageNumber.type = "number";
|
2023-10-19 23:30:57 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.numPages.setAttribute("data-l10n-id", "pdfjs-of-pages");
|
|
|
|
opts.numPages.setAttribute(
|
2023-10-19 23:30:57 +09:00
|
|
|
"data-l10n-args",
|
|
|
|
JSON.stringify({ pagesCount })
|
|
|
|
);
|
2016-11-19 03:50:29 +09:00
|
|
|
}
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.pageNumber.max = pagesCount;
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasPageLabels) {
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.pageNumber.value = this.pageLabel;
|
2023-10-19 23:30:57 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.numPages.setAttribute(
|
2023-10-19 23:30:57 +09:00
|
|
|
"data-l10n-args",
|
|
|
|
JSON.stringify({ pageNumber, pagesCount })
|
|
|
|
);
|
2017-06-18 06:07:17 +09:00
|
|
|
} else {
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.pageNumber.value = pageNumber;
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.previous.disabled = pageNumber <= 1;
|
|
|
|
opts.next.disabled = pageNumber >= pagesCount;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.zoomOut.disabled = pageScale <= MIN_SCALE;
|
|
|
|
opts.zoomIn.disabled = pageScale >= MAX_SCALE;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2023-10-19 23:30:57 +09:00
|
|
|
let predefinedValueFound = false;
|
2023-12-26 18:56:38 +09:00
|
|
|
for (const option of opts.scaleSelect.options) {
|
2023-10-19 23:30:57 +09:00
|
|
|
if (option.value !== pageScaleValue) {
|
|
|
|
option.selected = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
option.selected = true;
|
|
|
|
predefinedValueFound = true;
|
|
|
|
}
|
|
|
|
if (!predefinedValueFound) {
|
2023-12-26 18:56:38 +09:00
|
|
|
opts.customScaleOption.selected = true;
|
|
|
|
opts.customScaleOption.setAttribute(
|
2023-10-19 23:30:57 +09:00
|
|
|
"data-l10n-args",
|
|
|
|
JSON.stringify({
|
|
|
|
scale: Math.round(pageScale * 10000) / 100,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
updateLoadingIndicatorState(loading = false) {
|
2023-12-26 18:56:38 +09:00
|
|
|
const { pageNumber } = this.#opts;
|
2023-11-16 22:24:16 +09:00
|
|
|
pageNumber.classList.toggle("loading", loading);
|
2022-06-05 17:00:08 +09:00
|
|
|
}
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export { Toolbar };
|