123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698 |
- /**
- * @licstart The following is the entire license notice for the
- * JavaScript code in this page
- *
- * Copyright 2022 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.
- *
- * @licend The above is the entire license notice for the
- * JavaScript code in this page
- */
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.KeyboardManager = exports.CommandManager = exports.ColorManager = exports.AnnotationEditorUIManager = void 0;
- exports.bindEvents = bindEvents;
- exports.opacityToHex = opacityToHex;
- var _util = require("../../shared/util.js");
- var _display_utils = require("../display_utils.js");
- function bindEvents(obj, element, names) {
- for (const name of names) {
- element.addEventListener(name, obj[name].bind(obj));
- }
- }
- function opacityToHex(opacity) {
- return Math.round(Math.min(255, Math.max(1, 255 * opacity))).toString(16).padStart(2, "0");
- }
- class IdManager {
- #id = 0;
- getId() {
- return `${_util.AnnotationEditorPrefix}${this.#id++}`;
- }
- }
- class CommandManager {
- #commands = [];
- #locked = false;
- #maxSize;
- #position = -1;
- constructor(maxSize = 128) {
- this.#maxSize = maxSize;
- }
- add({
- cmd,
- undo,
- mustExec,
- type = NaN,
- overwriteIfSameType = false,
- keepUndo = false
- }) {
- if (mustExec) {
- cmd();
- }
- if (this.#locked) {
- return;
- }
- const save = {
- cmd,
- undo,
- type
- };
- if (this.#position === -1) {
- if (this.#commands.length > 0) {
- this.#commands.length = 0;
- }
- this.#position = 0;
- this.#commands.push(save);
- return;
- }
- if (overwriteIfSameType && this.#commands[this.#position].type === type) {
- if (keepUndo) {
- save.undo = this.#commands[this.#position].undo;
- }
- this.#commands[this.#position] = save;
- return;
- }
- const next = this.#position + 1;
- if (next === this.#maxSize) {
- this.#commands.splice(0, 1);
- } else {
- this.#position = next;
- if (next < this.#commands.length) {
- this.#commands.splice(next);
- }
- }
- this.#commands.push(save);
- }
- undo() {
- if (this.#position === -1) {
- return;
- }
- this.#locked = true;
- this.#commands[this.#position].undo();
- this.#locked = false;
- this.#position -= 1;
- }
- redo() {
- if (this.#position < this.#commands.length - 1) {
- this.#position += 1;
- this.#locked = true;
- this.#commands[this.#position].cmd();
- this.#locked = false;
- }
- }
- hasSomethingToUndo() {
- return this.#position !== -1;
- }
- hasSomethingToRedo() {
- return this.#position < this.#commands.length - 1;
- }
- destroy() {
- this.#commands = null;
- }
- }
- exports.CommandManager = CommandManager;
- class KeyboardManager {
- constructor(callbacks) {
- this.buffer = [];
- this.callbacks = new Map();
- this.allKeys = new Set();
- const {
- isMac
- } = _util.FeatureTest.platform;
- for (const [keys, callback] of callbacks) {
- for (const key of keys) {
- const isMacKey = key.startsWith("mac+");
- if (isMac && isMacKey) {
- this.callbacks.set(key.slice(4), callback);
- this.allKeys.add(key.split("+").at(-1));
- } else if (!isMac && !isMacKey) {
- this.callbacks.set(key, callback);
- this.allKeys.add(key.split("+").at(-1));
- }
- }
- }
- }
- #serialize(event) {
- if (event.altKey) {
- this.buffer.push("alt");
- }
- if (event.ctrlKey) {
- this.buffer.push("ctrl");
- }
- if (event.metaKey) {
- this.buffer.push("meta");
- }
- if (event.shiftKey) {
- this.buffer.push("shift");
- }
- this.buffer.push(event.key);
- const str = this.buffer.join("+");
- this.buffer.length = 0;
- return str;
- }
- exec(self, event) {
- if (!this.allKeys.has(event.key)) {
- return;
- }
- const callback = this.callbacks.get(this.#serialize(event));
- if (!callback) {
- return;
- }
- callback.bind(self)();
- event.stopPropagation();
- event.preventDefault();
- }
- }
- exports.KeyboardManager = KeyboardManager;
- class ColorManager {
- static _colorsMapping = new Map([["CanvasText", [0, 0, 0]], ["Canvas", [255, 255, 255]]]);
- get _colors() {
- if (typeof document === "undefined") {
- return (0, _util.shadow)(this, "_colors", ColorManager._colorsMapping);
- }
- const colors = new Map([["CanvasText", null], ["Canvas", null]]);
- (0, _display_utils.getColorValues)(colors);
- return (0, _util.shadow)(this, "_colors", colors);
- }
- convert(color) {
- const rgb = (0, _display_utils.getRGB)(color);
- if (!window.matchMedia("(forced-colors: active)").matches) {
- return rgb;
- }
- for (const [name, RGB] of this._colors) {
- if (RGB.every((x, i) => x === rgb[i])) {
- return ColorManager._colorsMapping.get(name);
- }
- }
- return rgb;
- }
- getHexCode(name) {
- const rgb = this._colors.get(name);
- if (!rgb) {
- return name;
- }
- return _util.Util.makeHexColor(...rgb);
- }
- }
- exports.ColorManager = ColorManager;
- class AnnotationEditorUIManager {
- #activeEditor = null;
- #allEditors = new Map();
- #allLayers = new Map();
- #annotationStorage = null;
- #commandManager = new CommandManager();
- #currentPageIndex = 0;
- #editorTypes = null;
- #editorsToRescale = new Set();
- #eventBus = null;
- #idManager = new IdManager();
- #isEnabled = false;
- #mode = _util.AnnotationEditorType.NONE;
- #selectedEditors = new Set();
- #boundCopy = this.copy.bind(this);
- #boundCut = this.cut.bind(this);
- #boundPaste = this.paste.bind(this);
- #boundKeydown = this.keydown.bind(this);
- #boundOnEditingAction = this.onEditingAction.bind(this);
- #boundOnPageChanging = this.onPageChanging.bind(this);
- #boundOnScaleChanging = this.onScaleChanging.bind(this);
- #boundOnRotationChanging = this.onRotationChanging.bind(this);
- #previousStates = {
- isEditing: false,
- isEmpty: true,
- hasSomethingToUndo: false,
- hasSomethingToRedo: false,
- hasSelectedEditor: false
- };
- #container = null;
- static _keyboardManager = new KeyboardManager([[["ctrl+a", "mac+meta+a"], AnnotationEditorUIManager.prototype.selectAll], [["ctrl+z", "mac+meta+z"], AnnotationEditorUIManager.prototype.undo], [["ctrl+y", "ctrl+shift+Z", "mac+meta+shift+Z"], AnnotationEditorUIManager.prototype.redo], [["Backspace", "alt+Backspace", "ctrl+Backspace", "shift+Backspace", "mac+Backspace", "mac+alt+Backspace", "mac+ctrl+Backspace", "Delete", "ctrl+Delete", "shift+Delete"], AnnotationEditorUIManager.prototype.delete], [["Escape", "mac+Escape"], AnnotationEditorUIManager.prototype.unselectAll]]);
- constructor(container, eventBus, annotationStorage) {
- this.#container = container;
- this.#eventBus = eventBus;
- this.#eventBus._on("editingaction", this.#boundOnEditingAction);
- this.#eventBus._on("pagechanging", this.#boundOnPageChanging);
- this.#eventBus._on("scalechanging", this.#boundOnScaleChanging);
- this.#eventBus._on("rotationchanging", this.#boundOnRotationChanging);
- this.#annotationStorage = annotationStorage;
- this.viewParameters = {
- realScale: _display_utils.PixelsPerInch.PDF_TO_CSS_UNITS,
- rotation: 0
- };
- }
- destroy() {
- this.#removeKeyboardManager();
- this.#eventBus._off("editingaction", this.#boundOnEditingAction);
- this.#eventBus._off("pagechanging", this.#boundOnPageChanging);
- this.#eventBus._off("scalechanging", this.#boundOnScaleChanging);
- this.#eventBus._off("rotationchanging", this.#boundOnRotationChanging);
- for (const layer of this.#allLayers.values()) {
- layer.destroy();
- }
- this.#allLayers.clear();
- this.#allEditors.clear();
- this.#editorsToRescale.clear();
- this.#activeEditor = null;
- this.#selectedEditors.clear();
- this.#commandManager.destroy();
- }
- onPageChanging({
- pageNumber
- }) {
- this.#currentPageIndex = pageNumber - 1;
- }
- focusMainContainer() {
- this.#container.focus();
- }
- addShouldRescale(editor) {
- this.#editorsToRescale.add(editor);
- }
- removeShouldRescale(editor) {
- this.#editorsToRescale.delete(editor);
- }
- onScaleChanging({
- scale
- }) {
- this.commitOrRemove();
- this.viewParameters.realScale = scale * _display_utils.PixelsPerInch.PDF_TO_CSS_UNITS;
- for (const editor of this.#editorsToRescale) {
- editor.onScaleChanging();
- }
- }
- onRotationChanging({
- pagesRotation
- }) {
- this.commitOrRemove();
- this.viewParameters.rotation = pagesRotation;
- }
- addToAnnotationStorage(editor) {
- if (!editor.isEmpty() && this.#annotationStorage && !this.#annotationStorage.has(editor.id)) {
- this.#annotationStorage.setValue(editor.id, editor);
- }
- }
- #addKeyboardManager() {
- this.#container.addEventListener("keydown", this.#boundKeydown);
- }
- #removeKeyboardManager() {
- this.#container.removeEventListener("keydown", this.#boundKeydown);
- }
- #addCopyPasteListeners() {
- document.addEventListener("copy", this.#boundCopy);
- document.addEventListener("cut", this.#boundCut);
- document.addEventListener("paste", this.#boundPaste);
- }
- #removeCopyPasteListeners() {
- document.removeEventListener("copy", this.#boundCopy);
- document.removeEventListener("cut", this.#boundCut);
- document.removeEventListener("paste", this.#boundPaste);
- }
- copy(event) {
- event.preventDefault();
- if (this.#activeEditor) {
- this.#activeEditor.commitOrRemove();
- }
- if (!this.hasSelection) {
- return;
- }
- const editors = [];
- for (const editor of this.#selectedEditors) {
- if (!editor.isEmpty()) {
- editors.push(editor.serialize());
- }
- }
- if (editors.length === 0) {
- return;
- }
- event.clipboardData.setData("application/pdfjs", JSON.stringify(editors));
- }
- cut(event) {
- this.copy(event);
- this.delete();
- }
- paste(event) {
- event.preventDefault();
- let data = event.clipboardData.getData("application/pdfjs");
- if (!data) {
- return;
- }
- try {
- data = JSON.parse(data);
- } catch (ex) {
- (0, _util.warn)(`paste: "${ex.message}".`);
- return;
- }
- if (!Array.isArray(data)) {
- return;
- }
- this.unselectAll();
- const layer = this.#allLayers.get(this.#currentPageIndex);
- try {
- const newEditors = [];
- for (const editor of data) {
- const deserializedEditor = layer.deserialize(editor);
- if (!deserializedEditor) {
- return;
- }
- newEditors.push(deserializedEditor);
- }
- const cmd = () => {
- for (const editor of newEditors) {
- this.#addEditorToLayer(editor);
- }
- this.#selectEditors(newEditors);
- };
- const undo = () => {
- for (const editor of newEditors) {
- editor.remove();
- }
- };
- this.addCommands({
- cmd,
- undo,
- mustExec: true
- });
- } catch (ex) {
- (0, _util.warn)(`paste: "${ex.message}".`);
- }
- }
- keydown(event) {
- if (!this.getActive()?.shouldGetKeyboardEvents()) {
- AnnotationEditorUIManager._keyboardManager.exec(this, event);
- }
- }
- onEditingAction(details) {
- if (["undo", "redo", "delete", "selectAll"].includes(details.name)) {
- this[details.name]();
- }
- }
- #dispatchUpdateStates(details) {
- const hasChanged = Object.entries(details).some(([key, value]) => this.#previousStates[key] !== value);
- if (hasChanged) {
- this.#eventBus.dispatch("annotationeditorstateschanged", {
- source: this,
- details: Object.assign(this.#previousStates, details)
- });
- }
- }
- #dispatchUpdateUI(details) {
- this.#eventBus.dispatch("annotationeditorparamschanged", {
- source: this,
- details
- });
- }
- setEditingState(isEditing) {
- if (isEditing) {
- this.#addKeyboardManager();
- this.#addCopyPasteListeners();
- this.#dispatchUpdateStates({
- isEditing: this.#mode !== _util.AnnotationEditorType.NONE,
- isEmpty: this.#isEmpty(),
- hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
- hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
- hasSelectedEditor: false
- });
- } else {
- this.#removeKeyboardManager();
- this.#removeCopyPasteListeners();
- this.#dispatchUpdateStates({
- isEditing: false
- });
- }
- }
- registerEditorTypes(types) {
- if (this.#editorTypes) {
- return;
- }
- this.#editorTypes = types;
- for (const editorType of this.#editorTypes) {
- this.#dispatchUpdateUI(editorType.defaultPropertiesToUpdate);
- }
- }
- getId() {
- return this.#idManager.getId();
- }
- get currentLayer() {
- return this.#allLayers.get(this.#currentPageIndex);
- }
- get currentPageIndex() {
- return this.#currentPageIndex;
- }
- addLayer(layer) {
- this.#allLayers.set(layer.pageIndex, layer);
- if (this.#isEnabled) {
- layer.enable();
- } else {
- layer.disable();
- }
- }
- removeLayer(layer) {
- this.#allLayers.delete(layer.pageIndex);
- }
- updateMode(mode) {
- this.#mode = mode;
- if (mode === _util.AnnotationEditorType.NONE) {
- this.setEditingState(false);
- this.#disableAll();
- } else {
- this.setEditingState(true);
- this.#enableAll();
- for (const layer of this.#allLayers.values()) {
- layer.updateMode(mode);
- }
- }
- }
- updateToolbar(mode) {
- if (mode === this.#mode) {
- return;
- }
- this.#eventBus.dispatch("switchannotationeditormode", {
- source: this,
- mode
- });
- }
- updateParams(type, value) {
- if (!this.#editorTypes) {
- return;
- }
- for (const editor of this.#selectedEditors) {
- editor.updateParams(type, value);
- }
- for (const editorType of this.#editorTypes) {
- editorType.updateDefaultParams(type, value);
- }
- }
- #enableAll() {
- if (!this.#isEnabled) {
- this.#isEnabled = true;
- for (const layer of this.#allLayers.values()) {
- layer.enable();
- }
- }
- }
- #disableAll() {
- this.unselectAll();
- if (this.#isEnabled) {
- this.#isEnabled = false;
- for (const layer of this.#allLayers.values()) {
- layer.disable();
- }
- }
- }
- getEditors(pageIndex) {
- const editors = [];
- for (const editor of this.#allEditors.values()) {
- if (editor.pageIndex === pageIndex) {
- editors.push(editor);
- }
- }
- return editors;
- }
- getEditor(id) {
- return this.#allEditors.get(id);
- }
- addEditor(editor) {
- this.#allEditors.set(editor.id, editor);
- }
- removeEditor(editor) {
- this.#allEditors.delete(editor.id);
- this.unselect(editor);
- this.#annotationStorage?.remove(editor.id);
- }
- #addEditorToLayer(editor) {
- const layer = this.#allLayers.get(editor.pageIndex);
- if (layer) {
- layer.addOrRebuild(editor);
- } else {
- this.addEditor(editor);
- }
- }
- setActiveEditor(editor) {
- if (this.#activeEditor === editor) {
- return;
- }
- this.#activeEditor = editor;
- if (editor) {
- this.#dispatchUpdateUI(editor.propertiesToUpdate);
- }
- }
- toggleSelected(editor) {
- if (this.#selectedEditors.has(editor)) {
- this.#selectedEditors.delete(editor);
- editor.unselect();
- this.#dispatchUpdateStates({
- hasSelectedEditor: this.hasSelection
- });
- return;
- }
- this.#selectedEditors.add(editor);
- editor.select();
- this.#dispatchUpdateUI(editor.propertiesToUpdate);
- this.#dispatchUpdateStates({
- hasSelectedEditor: true
- });
- }
- setSelected(editor) {
- for (const ed of this.#selectedEditors) {
- if (ed !== editor) {
- ed.unselect();
- }
- }
- this.#selectedEditors.clear();
- this.#selectedEditors.add(editor);
- editor.select();
- this.#dispatchUpdateUI(editor.propertiesToUpdate);
- this.#dispatchUpdateStates({
- hasSelectedEditor: true
- });
- }
- isSelected(editor) {
- return this.#selectedEditors.has(editor);
- }
- unselect(editor) {
- editor.unselect();
- this.#selectedEditors.delete(editor);
- this.#dispatchUpdateStates({
- hasSelectedEditor: this.hasSelection
- });
- }
- get hasSelection() {
- return this.#selectedEditors.size !== 0;
- }
- undo() {
- this.#commandManager.undo();
- this.#dispatchUpdateStates({
- hasSomethingToUndo: this.#commandManager.hasSomethingToUndo(),
- hasSomethingToRedo: true,
- isEmpty: this.#isEmpty()
- });
- }
- redo() {
- this.#commandManager.redo();
- this.#dispatchUpdateStates({
- hasSomethingToUndo: true,
- hasSomethingToRedo: this.#commandManager.hasSomethingToRedo(),
- isEmpty: this.#isEmpty()
- });
- }
- addCommands(params) {
- this.#commandManager.add(params);
- this.#dispatchUpdateStates({
- hasSomethingToUndo: true,
- hasSomethingToRedo: false,
- isEmpty: this.#isEmpty()
- });
- }
- #isEmpty() {
- if (this.#allEditors.size === 0) {
- return true;
- }
- if (this.#allEditors.size === 1) {
- for (const editor of this.#allEditors.values()) {
- return editor.isEmpty();
- }
- }
- return false;
- }
- delete() {
- this.commitOrRemove();
- if (!this.hasSelection) {
- return;
- }
- const editors = [...this.#selectedEditors];
- const cmd = () => {
- for (const editor of editors) {
- editor.remove();
- }
- };
- const undo = () => {
- for (const editor of editors) {
- this.#addEditorToLayer(editor);
- }
- };
- this.addCommands({
- cmd,
- undo,
- mustExec: true
- });
- }
- commitOrRemove() {
- this.#activeEditor?.commitOrRemove();
- }
- #selectEditors(editors) {
- this.#selectedEditors.clear();
- for (const editor of editors) {
- if (editor.isEmpty()) {
- continue;
- }
- this.#selectedEditors.add(editor);
- editor.select();
- }
- this.#dispatchUpdateStates({
- hasSelectedEditor: true
- });
- }
- selectAll() {
- for (const editor of this.#selectedEditors) {
- editor.commit();
- }
- this.#selectEditors(this.#allEditors.values());
- }
- unselectAll() {
- if (this.#activeEditor) {
- this.#activeEditor.commitOrRemove();
- return;
- }
- if (this.#selectedEditors.size === 0) {
- return;
- }
- for (const editor of this.#selectedEditors) {
- editor.unselect();
- }
- this.#selectedEditors.clear();
- this.#dispatchUpdateStates({
- hasSelectedEditor: false
- });
- }
- isActive(editor) {
- return this.#activeEditor === editor;
- }
- getActive() {
- return this.#activeEditor;
- }
- getMode() {
- return this.#mode;
- }
- }
- exports.AnnotationEditorUIManager = AnnotationEditorUIManager;
|