2022-06-01 10:38:08 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
/** @typedef {import("./annotation_editor_layer.js").AnnotationEditorLayer} AnnotationEditorLayer */
|
2022-12-05 12:25:06 +01:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
/** @typedef {import("./tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
|
2022-06-01 10:38:08 +02:00
|
|
|
|
2022-11-29 12:14:40 +01:00
|
|
|
import { bindEvents, ColorManager } from "./tools.js";
|
2023-08-04 18:21:27 +02:00
|
|
|
import { FeatureTest, shadow, unreachable } from "../../shared/util.js";
|
2022-06-01 10:38:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} AnnotationEditorParameters
|
2022-12-05 12:25:06 +01:00
|
|
|
* @property {AnnotationEditorUIManager} uiManager - the global manager
|
2022-06-01 10:38:08 +02:00
|
|
|
* @property {AnnotationEditorLayer} parent - the layer containing this editor
|
|
|
|
* @property {string} id - editor id
|
|
|
|
* @property {number} x - x-coordinate
|
|
|
|
* @property {number} y - y-coordinate
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for editors.
|
|
|
|
*/
|
|
|
|
class AnnotationEditor {
|
2023-07-06 09:21:34 +02:00
|
|
|
#keepAspectRatio = false;
|
2023-07-05 18:09:53 +02:00
|
|
|
|
2023-07-24 09:49:49 +02:00
|
|
|
#resizersDiv = null;
|
|
|
|
|
2022-07-21 10:42:15 +02:00
|
|
|
#boundFocusin = this.focusin.bind(this);
|
|
|
|
|
|
|
|
#boundFocusout = this.focusout.bind(this);
|
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
#hasBeenClicked = false;
|
2022-07-21 10:42:15 +02:00
|
|
|
|
2022-07-22 15:49:42 +02:00
|
|
|
#isEditing = false;
|
2022-07-21 10:42:15 +02:00
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
#isInEditMode = false;
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
_uiManager = null;
|
|
|
|
|
2023-08-08 19:47:24 +02:00
|
|
|
_focusEventsAllowed = true;
|
|
|
|
|
2023-08-02 20:08:09 +02:00
|
|
|
#isDraggable = false;
|
|
|
|
|
2022-07-20 14:21:30 +02:00
|
|
|
#zIndex = AnnotationEditor._zIndex++;
|
|
|
|
|
2022-06-29 15:39:02 +02:00
|
|
|
static _colorManager = new ColorManager();
|
|
|
|
|
2022-07-20 14:21:30 +02:00
|
|
|
static _zIndex = 1;
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* @param {AnnotationEditorParameters} parameters
|
|
|
|
*/
|
|
|
|
constructor(parameters) {
|
|
|
|
if (this.constructor === AnnotationEditor) {
|
|
|
|
unreachable("Cannot initialize AnnotationEditor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parent = parameters.parent;
|
|
|
|
this.id = parameters.id;
|
|
|
|
this.width = this.height = null;
|
|
|
|
this.pageIndex = parameters.parent.pageIndex;
|
|
|
|
this.name = parameters.name;
|
|
|
|
this.div = null;
|
2022-12-05 12:25:06 +01:00
|
|
|
this._uiManager = parameters.uiManager;
|
2023-06-06 17:18:02 +02:00
|
|
|
this.annotationElementId = null;
|
2023-07-24 09:49:49 +02:00
|
|
|
this._willKeepAspectRatio = false;
|
2022-06-23 15:47:45 +02:00
|
|
|
|
2022-12-07 18:27:32 +01:00
|
|
|
const {
|
|
|
|
rotation,
|
2022-12-08 12:37:18 +01:00
|
|
|
rawDims: { pageWidth, pageHeight, pageX, pageY },
|
2022-12-07 18:27:32 +01:00
|
|
|
} = this.parent.viewport;
|
|
|
|
|
2022-12-08 12:37:18 +01:00
|
|
|
this.rotation = rotation;
|
2023-04-16 21:36:26 +02:00
|
|
|
this.pageRotation =
|
|
|
|
(360 + rotation - this._uiManager.viewParameters.rotation) % 360;
|
2022-12-07 18:27:32 +01:00
|
|
|
this.pageDimensions = [pageWidth, pageHeight];
|
2022-12-08 12:37:18 +01:00
|
|
|
this.pageTranslation = [pageX, pageY];
|
2022-12-07 18:27:32 +01:00
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
const [width, height] = this.parentDimensions;
|
2022-06-23 15:47:45 +02:00
|
|
|
this.x = parameters.x / width;
|
|
|
|
this.y = parameters.y / height;
|
2022-06-01 10:38:08 +02:00
|
|
|
|
|
|
|
this.isAttachedToDOM = false;
|
2023-06-06 17:18:02 +02:00
|
|
|
this.deleted = false;
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:39:02 +02:00
|
|
|
static get _defaultLineColor() {
|
|
|
|
return shadow(
|
|
|
|
this,
|
|
|
|
"_defaultLineColor",
|
|
|
|
this._colorManager.getHexCode("CanvasText")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-06 17:18:02 +02:00
|
|
|
static deleteAnnotationElement(editor) {
|
|
|
|
const fakeEditor = new FakeEditor({
|
|
|
|
id: editor.parent.getNextId(),
|
|
|
|
parent: editor.parent,
|
|
|
|
uiManager: editor._uiManager,
|
|
|
|
});
|
|
|
|
fakeEditor.annotationElementId = editor.annotationElementId;
|
|
|
|
fakeEditor.deleted = true;
|
|
|
|
fakeEditor._uiManager.addToAnnotationStorage(fakeEditor);
|
|
|
|
}
|
|
|
|
|
2023-06-22 12:16:07 +02:00
|
|
|
/**
|
|
|
|
* Initialize the l10n stuff for this type of editor.
|
|
|
|
* @param {Object} _l10n
|
|
|
|
*/
|
|
|
|
static initialize(_l10n) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the default parameters for this type of editor.
|
|
|
|
* @param {number} _type
|
|
|
|
* @param {*} _value
|
|
|
|
*/
|
|
|
|
static updateDefaultParams(_type, _value) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default properties to set in the UI for this type of editor.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
static get defaultPropertiesToUpdate() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the properties to update in the UI for this editor.
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
|
|
|
get propertiesToUpdate() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-08-02 20:08:09 +02:00
|
|
|
get _isDraggable() {
|
|
|
|
return this.#isDraggable;
|
|
|
|
}
|
|
|
|
|
|
|
|
set _isDraggable(value) {
|
|
|
|
this.#isDraggable = value;
|
|
|
|
this.div?.classList.toggle("draggable", value);
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
/**
|
|
|
|
* Add some commands into the CommandManager (undo/redo stuff).
|
|
|
|
* @param {Object} params
|
|
|
|
*/
|
|
|
|
addCommands(params) {
|
|
|
|
this._uiManager.addCommands(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentLayer() {
|
|
|
|
return this._uiManager.currentLayer;
|
2022-12-06 16:16:24 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 18:16:49 +02:00
|
|
|
/**
|
|
|
|
* This editor will be behind the others.
|
|
|
|
*/
|
|
|
|
setInBackground() {
|
2022-07-20 14:21:30 +02:00
|
|
|
this.div.style.zIndex = 0;
|
2022-06-16 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This editor will be in the foreground.
|
|
|
|
*/
|
|
|
|
setInForeground() {
|
2022-07-20 14:21:30 +02:00
|
|
|
this.div.style.zIndex = this.#zIndex;
|
2022-06-16 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
setParent(parent) {
|
|
|
|
if (parent !== null) {
|
|
|
|
this.pageIndex = parent.pageIndex;
|
|
|
|
this.pageDimensions = parent.pageDimensions;
|
|
|
|
}
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* onfocus callback.
|
|
|
|
*/
|
2022-07-21 10:42:15 +02:00
|
|
|
focusin(event) {
|
2023-08-08 19:47:24 +02:00
|
|
|
if (!this._focusEventsAllowed) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-09 11:23:26 +02:00
|
|
|
if (!this.#hasBeenClicked) {
|
2022-07-22 15:49:42 +02:00
|
|
|
this.parent.setSelected(this);
|
|
|
|
} else {
|
2023-08-09 11:23:26 +02:00
|
|
|
this.#hasBeenClicked = false;
|
2022-07-21 10:42:15 +02:00
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* onblur callback.
|
|
|
|
* @param {FocusEvent} event
|
|
|
|
*/
|
|
|
|
focusout(event) {
|
2023-08-08 19:47:24 +02:00
|
|
|
if (!this._focusEventsAllowed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
if (!this.isAttachedToDOM) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case of focusout, the relatedTarget is the element which
|
|
|
|
// is grabbing the focus.
|
|
|
|
// So if the related target is an element under the div for this
|
|
|
|
// editor, then the editor isn't unactive.
|
|
|
|
const target = event.relatedTarget;
|
|
|
|
if (target?.closest(`#${this.id}`)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
if (!this.parent?.isMultipleSelection) {
|
2022-07-21 10:42:15 +02:00
|
|
|
this.commitOrRemove();
|
2022-07-12 17:32:14 +02:00
|
|
|
}
|
2022-06-16 18:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
commitOrRemove() {
|
2022-06-01 10:38:08 +02:00
|
|
|
if (this.isEmpty()) {
|
|
|
|
this.remove();
|
|
|
|
} else {
|
|
|
|
this.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-29 18:00:52 +02:00
|
|
|
* Commit the data contained in this editor.
|
|
|
|
*/
|
|
|
|
commit() {
|
2022-12-05 12:25:06 +01:00
|
|
|
this.addToAnnotationStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
addToAnnotationStorage() {
|
|
|
|
this._uiManager.addToAnnotationStorage(this);
|
2022-07-29 18:00:52 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Set the editor position within its parent.
|
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
2022-06-23 15:47:45 +02:00
|
|
|
* @param {number} tx - x-translation in screen coordinates.
|
|
|
|
* @param {number} ty - y-translation in screen coordinates.
|
2022-06-01 10:38:08 +02:00
|
|
|
*/
|
2022-06-23 15:47:45 +02:00
|
|
|
setAt(x, y, tx, ty) {
|
2022-12-05 12:25:06 +01:00
|
|
|
const [width, height] = this.parentDimensions;
|
2022-06-23 15:47:45 +02:00
|
|
|
[tx, ty] = this.screenToPageTranslation(tx, ty);
|
|
|
|
|
|
|
|
this.x = (x + tx) / width;
|
|
|
|
this.y = (y + ty) / height;
|
2022-06-01 10:38:08 +02:00
|
|
|
|
2023-07-13 18:31:08 +02:00
|
|
|
this.fixAndSetPosition();
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-26 12:57:59 +02:00
|
|
|
#translate([width, height], x, y) {
|
|
|
|
[x, y] = this.screenToPageTranslation(x, y);
|
|
|
|
|
|
|
|
this.x += x / width;
|
|
|
|
this.y += y / height;
|
|
|
|
|
|
|
|
this.fixAndSetPosition();
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Translate the editor position within its parent.
|
2022-06-23 15:47:45 +02:00
|
|
|
* @param {number} x - x-translation in screen coordinates.
|
|
|
|
* @param {number} y - y-translation in screen coordinates.
|
|
|
|
*/
|
|
|
|
translate(x, y) {
|
2023-07-26 12:57:59 +02:00
|
|
|
this.#translate(this.parentDimensions, x, y);
|
|
|
|
}
|
2022-06-23 15:47:45 +02:00
|
|
|
|
2023-07-26 12:57:59 +02:00
|
|
|
/**
|
|
|
|
* Translate the editor position within its page and adjust the scroll
|
|
|
|
* in order to have the editor in the view.
|
|
|
|
* @param {number} x - x-translation in page coordinates.
|
|
|
|
* @param {number} y - y-translation in page coordinates.
|
|
|
|
*/
|
|
|
|
translateInPage(x, y) {
|
|
|
|
this.#translate(this.pageDimensions, x, y);
|
2023-08-09 11:23:26 +02:00
|
|
|
this.moveInDOM();
|
|
|
|
this.div.scrollIntoView({ block: "nearest" });
|
|
|
|
}
|
|
|
|
|
|
|
|
drag(tx, ty) {
|
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
|
|
|
this.x += tx / parentWidth;
|
|
|
|
this.y += ty / parentHeight;
|
|
|
|
if (this.x < 0 || this.x > 1 || this.y < 0 || this.y > 1) {
|
|
|
|
// The element will be outside of its parent so change the parent.
|
|
|
|
const { x, y } = this.div.getBoundingClientRect();
|
|
|
|
if (this.parent.findNewParent(this, x, y)) {
|
|
|
|
this.x -= Math.floor(this.x);
|
|
|
|
this.y -= Math.floor(this.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The editor can be moved wherever the user wants, so we don't need to fix
|
|
|
|
// the position: it'll be done when the user will release the mouse button.
|
|
|
|
this.div.style.left = `${(100 * this.x).toFixed(2)}%`;
|
|
|
|
this.div.style.top = `${(100 * this.y).toFixed(2)}%`;
|
2023-07-26 12:57:59 +02:00
|
|
|
this.div.scrollIntoView({ block: "nearest" });
|
2023-07-13 18:31:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fixAndSetPosition() {
|
|
|
|
const [pageWidth, pageHeight] = this.pageDimensions;
|
|
|
|
let { x, y, width, height } = this;
|
|
|
|
width *= pageWidth;
|
|
|
|
height *= pageHeight;
|
|
|
|
x *= pageWidth;
|
|
|
|
y *= pageHeight;
|
|
|
|
|
|
|
|
switch (this.rotation) {
|
|
|
|
case 0:
|
|
|
|
x = Math.max(0, Math.min(pageWidth - width, x));
|
|
|
|
y = Math.max(0, Math.min(pageHeight - height, y));
|
|
|
|
break;
|
|
|
|
case 90:
|
|
|
|
x = Math.max(0, Math.min(pageWidth - height, x));
|
|
|
|
y = Math.min(pageHeight, Math.max(width, y));
|
|
|
|
break;
|
|
|
|
case 180:
|
|
|
|
x = Math.min(pageWidth, Math.max(width, x));
|
|
|
|
y = Math.min(pageHeight, Math.max(height, y));
|
|
|
|
break;
|
|
|
|
case 270:
|
|
|
|
x = Math.min(pageWidth, Math.max(height, x));
|
|
|
|
y = Math.max(0, Math.min(pageHeight - width, y));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.x = x / pageWidth;
|
|
|
|
this.y = y / pageHeight;
|
|
|
|
|
|
|
|
this.div.style.left = `${(100 * this.x).toFixed(2)}%`;
|
|
|
|
this.div.style.top = `${(100 * this.y).toFixed(2)}%`;
|
2022-06-23 15:47:45 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 18:21:27 +02:00
|
|
|
static #rotatePoint(x, y, angle) {
|
|
|
|
switch (angle) {
|
2022-06-23 15:47:45 +02:00
|
|
|
case 90:
|
|
|
|
return [y, -x];
|
|
|
|
case 180:
|
|
|
|
return [-x, -y];
|
|
|
|
case 270:
|
|
|
|
return [-y, x];
|
|
|
|
default:
|
|
|
|
return [x, y];
|
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 18:21:27 +02:00
|
|
|
/**
|
|
|
|
* Convert a screen translation into a page one.
|
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
|
|
|
*/
|
|
|
|
screenToPageTranslation(x, y) {
|
|
|
|
return AnnotationEditor.#rotatePoint(x, y, this.parentRotation);
|
|
|
|
}
|
|
|
|
|
2023-07-05 19:46:21 +02:00
|
|
|
/**
|
|
|
|
* Convert a page translation into a screen one.
|
|
|
|
* @param {number} x
|
|
|
|
* @param {number} y
|
|
|
|
*/
|
|
|
|
pageTranslationToScreen(x, y) {
|
2023-08-04 18:21:27 +02:00
|
|
|
return AnnotationEditor.#rotatePoint(x, y, 360 - this.parentRotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
#getRotationMatrix(rotation) {
|
|
|
|
switch (rotation) {
|
|
|
|
case 90: {
|
|
|
|
const [pageWidth, pageHeight] = this.pageDimensions;
|
|
|
|
return [0, -pageWidth / pageHeight, pageHeight / pageWidth, 0];
|
|
|
|
}
|
2023-07-05 19:46:21 +02:00
|
|
|
case 180:
|
2023-08-04 18:21:27 +02:00
|
|
|
return [-1, 0, 0, -1];
|
|
|
|
case 270: {
|
|
|
|
const [pageWidth, pageHeight] = this.pageDimensions;
|
|
|
|
return [0, pageWidth / pageHeight, -pageHeight / pageWidth, 0];
|
|
|
|
}
|
2023-07-05 19:46:21 +02:00
|
|
|
default:
|
2023-08-04 18:21:27 +02:00
|
|
|
return [1, 0, 0, 1];
|
2023-07-05 19:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
get parentScale() {
|
|
|
|
return this._uiManager.viewParameters.realScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
get parentRotation() {
|
2023-04-16 21:36:26 +02:00
|
|
|
return (this._uiManager.viewParameters.rotation + this.pageRotation) % 360;
|
2022-12-05 12:25:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get parentDimensions() {
|
2023-08-06 21:05:49 +02:00
|
|
|
const {
|
|
|
|
parentScale,
|
|
|
|
pageDimensions: [pageWidth, pageHeight],
|
|
|
|
} = this;
|
|
|
|
const scaledWidth = pageWidth * parentScale;
|
|
|
|
const scaledHeight = pageHeight * parentScale;
|
|
|
|
return FeatureTest.isCSSRoundSupported
|
|
|
|
? [Math.round(scaledWidth), Math.round(scaledHeight)]
|
|
|
|
: [scaledWidth, scaledHeight];
|
2022-12-05 12:25:06 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Set the dimensions of this editor.
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
*/
|
|
|
|
setDims(width, height) {
|
2022-12-05 12:25:06 +01:00
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
2023-07-17 15:03:27 +02:00
|
|
|
this.div.style.width = `${((100 * width) / parentWidth).toFixed(2)}%`;
|
2023-07-06 09:21:34 +02:00
|
|
|
if (!this.#keepAspectRatio) {
|
2023-07-17 15:03:27 +02:00
|
|
|
this.div.style.height = `${((100 * height) / parentHeight).toFixed(2)}%`;
|
2023-07-06 09:21:34 +02:00
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 19:05:42 +02:00
|
|
|
fixDims() {
|
|
|
|
const { style } = this.div;
|
|
|
|
const { height, width } = style;
|
|
|
|
const widthPercent = width.endsWith("%");
|
2023-07-06 09:21:34 +02:00
|
|
|
const heightPercent = !this.#keepAspectRatio && height.endsWith("%");
|
2022-10-15 19:05:42 +02:00
|
|
|
if (widthPercent && heightPercent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
2022-10-15 19:05:42 +02:00
|
|
|
if (!widthPercent) {
|
2023-07-17 15:03:27 +02:00
|
|
|
style.width = `${((100 * parseFloat(width)) / parentWidth).toFixed(2)}%`;
|
2022-10-15 19:05:42 +02:00
|
|
|
}
|
2023-07-06 09:21:34 +02:00
|
|
|
if (!this.#keepAspectRatio && !heightPercent) {
|
2023-07-17 15:03:27 +02:00
|
|
|
style.height = `${((100 * parseFloat(height)) / parentHeight).toFixed(
|
|
|
|
2
|
|
|
|
)}%`;
|
2022-10-15 19:05:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:42:46 +02:00
|
|
|
/**
|
|
|
|
* Get the translation used to position this editor when it's created.
|
|
|
|
* @returns {Array<number>}
|
|
|
|
*/
|
|
|
|
getInitialTranslation() {
|
|
|
|
return [0, 0];
|
|
|
|
}
|
|
|
|
|
2023-08-10 18:24:54 +02:00
|
|
|
static #noContextMenu(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2023-07-24 09:49:49 +02:00
|
|
|
#createResizers() {
|
|
|
|
if (this.#resizersDiv) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.#resizersDiv = document.createElement("div");
|
|
|
|
this.#resizersDiv.classList.add("resizers");
|
|
|
|
const classes = ["topLeft", "topRight", "bottomRight", "bottomLeft"];
|
|
|
|
if (!this._willKeepAspectRatio) {
|
|
|
|
classes.push("topMiddle", "middleRight", "bottomMiddle", "middleLeft");
|
|
|
|
}
|
|
|
|
for (const name of classes) {
|
|
|
|
const div = document.createElement("div");
|
|
|
|
this.#resizersDiv.append(div);
|
|
|
|
div.classList.add("resizer", name);
|
|
|
|
div.addEventListener(
|
|
|
|
"pointerdown",
|
|
|
|
this.#resizerPointerdown.bind(this, name)
|
|
|
|
);
|
2023-08-10 18:24:54 +02:00
|
|
|
div.addEventListener("contextmenu", AnnotationEditor.#noContextMenu);
|
2023-07-24 09:49:49 +02:00
|
|
|
}
|
|
|
|
this.div.prepend(this.#resizersDiv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#resizerPointerdown(name, event) {
|
|
|
|
event.preventDefault();
|
2023-08-10 18:24:54 +02:00
|
|
|
const { isMac } = FeatureTest.platform;
|
|
|
|
if (event.button !== 0 || (event.ctrlKey && isMac)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-24 09:49:49 +02:00
|
|
|
const boundResizerPointermove = this.#resizerPointermove.bind(this, name);
|
2023-08-02 20:08:09 +02:00
|
|
|
const savedDraggable = this._isDraggable;
|
|
|
|
this._isDraggable = false;
|
2023-07-24 09:49:49 +02:00
|
|
|
const pointerMoveOptions = { passive: true, capture: true };
|
|
|
|
window.addEventListener(
|
|
|
|
"pointermove",
|
|
|
|
boundResizerPointermove,
|
|
|
|
pointerMoveOptions
|
|
|
|
);
|
2023-08-04 18:21:27 +02:00
|
|
|
const savedX = this.x;
|
|
|
|
const savedY = this.y;
|
|
|
|
const savedWidth = this.width;
|
|
|
|
const savedHeight = this.height;
|
|
|
|
const savedParentCursor = this.parent.div.style.cursor;
|
|
|
|
const savedCursor = this.div.style.cursor;
|
|
|
|
this.div.style.cursor = this.parent.div.style.cursor =
|
|
|
|
window.getComputedStyle(event.target).cursor;
|
|
|
|
|
2023-07-24 09:49:49 +02:00
|
|
|
const pointerUpCallback = () => {
|
2023-08-02 20:08:09 +02:00
|
|
|
this._isDraggable = savedDraggable;
|
2023-08-03 10:51:47 +02:00
|
|
|
window.removeEventListener("pointerup", pointerUpCallback);
|
|
|
|
window.removeEventListener("blur", pointerUpCallback);
|
2023-07-24 09:49:49 +02:00
|
|
|
window.removeEventListener(
|
|
|
|
"pointermove",
|
|
|
|
boundResizerPointermove,
|
|
|
|
pointerMoveOptions
|
|
|
|
);
|
2023-08-04 18:21:27 +02:00
|
|
|
this.parent.div.style.cursor = savedParentCursor;
|
|
|
|
this.div.style.cursor = savedCursor;
|
|
|
|
|
|
|
|
const newX = this.x;
|
|
|
|
const newY = this.y;
|
|
|
|
const newWidth = this.width;
|
|
|
|
const newHeight = this.height;
|
|
|
|
if (
|
|
|
|
newX === savedX &&
|
|
|
|
newY === savedY &&
|
|
|
|
newWidth === savedWidth &&
|
|
|
|
newHeight === savedHeight
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addCommands({
|
|
|
|
cmd: () => {
|
|
|
|
this.width = newWidth;
|
|
|
|
this.height = newHeight;
|
|
|
|
this.x = newX;
|
|
|
|
this.y = newY;
|
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
|
|
|
this.setDims(parentWidth * newWidth, parentHeight * newHeight);
|
|
|
|
this.fixAndSetPosition();
|
2023-08-09 11:23:26 +02:00
|
|
|
this.moveInDOM();
|
2023-08-04 18:21:27 +02:00
|
|
|
},
|
|
|
|
undo: () => {
|
|
|
|
this.width = savedWidth;
|
|
|
|
this.height = savedHeight;
|
|
|
|
this.x = savedX;
|
|
|
|
this.y = savedY;
|
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
|
|
|
this.setDims(parentWidth * savedWidth, parentHeight * savedHeight);
|
|
|
|
this.fixAndSetPosition();
|
2023-08-09 11:23:26 +02:00
|
|
|
this.moveInDOM();
|
2023-08-04 18:21:27 +02:00
|
|
|
},
|
|
|
|
mustExec: true,
|
|
|
|
});
|
2023-07-24 09:49:49 +02:00
|
|
|
};
|
2023-08-03 10:51:47 +02:00
|
|
|
window.addEventListener("pointerup", pointerUpCallback);
|
2023-08-04 18:21:27 +02:00
|
|
|
// If the user switches to another window (with alt+tab), then we end the
|
2023-08-03 10:51:47 +02:00
|
|
|
// resize session.
|
|
|
|
window.addEventListener("blur", pointerUpCallback);
|
2023-07-24 09:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#resizerPointermove(name, event) {
|
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
|
|
|
const savedX = this.x;
|
|
|
|
const savedY = this.y;
|
|
|
|
const savedWidth = this.width;
|
|
|
|
const savedHeight = this.height;
|
|
|
|
const minWidth = AnnotationEditor.MIN_SIZE / parentWidth;
|
|
|
|
const minHeight = AnnotationEditor.MIN_SIZE / parentHeight;
|
|
|
|
|
|
|
|
// 10000 because we multiply by 100 and use toFixed(2) in fixAndSetPosition.
|
|
|
|
// Without rounding, the positions of the corners other than the top left
|
|
|
|
// one can be slightly wrong.
|
|
|
|
const round = x => Math.round(x * 10000) / 10000;
|
2023-08-04 18:21:27 +02:00
|
|
|
const rotationMatrix = this.#getRotationMatrix(this.rotation);
|
|
|
|
const transf = (x, y) => [
|
|
|
|
rotationMatrix[0] * x + rotationMatrix[2] * y,
|
|
|
|
rotationMatrix[1] * x + rotationMatrix[3] * y,
|
|
|
|
];
|
|
|
|
const invRotationMatrix = this.#getRotationMatrix(360 - this.rotation);
|
|
|
|
const invTransf = (x, y) => [
|
|
|
|
invRotationMatrix[0] * x + invRotationMatrix[2] * y,
|
|
|
|
invRotationMatrix[1] * x + invRotationMatrix[3] * y,
|
|
|
|
];
|
|
|
|
let getPoint;
|
|
|
|
let getOpposite;
|
|
|
|
let isDiagonal = false;
|
|
|
|
let isHorizontal = false;
|
2023-07-24 09:49:49 +02:00
|
|
|
|
|
|
|
switch (name) {
|
2023-08-04 18:21:27 +02:00
|
|
|
case "topLeft":
|
|
|
|
isDiagonal = true;
|
|
|
|
getPoint = (w, h) => [0, 0];
|
|
|
|
getOpposite = (w, h) => [w, h];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "topMiddle":
|
|
|
|
getPoint = (w, h) => [w / 2, 0];
|
|
|
|
getOpposite = (w, h) => [w / 2, h];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "topRight":
|
|
|
|
isDiagonal = true;
|
|
|
|
getPoint = (w, h) => [w, 0];
|
|
|
|
getOpposite = (w, h) => [0, h];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "middleRight":
|
|
|
|
isHorizontal = true;
|
|
|
|
getPoint = (w, h) => [w, h / 2];
|
|
|
|
getOpposite = (w, h) => [0, h / 2];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "bottomRight":
|
|
|
|
isDiagonal = true;
|
|
|
|
getPoint = (w, h) => [w, h];
|
|
|
|
getOpposite = (w, h) => [0, 0];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "bottomMiddle":
|
|
|
|
getPoint = (w, h) => [w / 2, h];
|
|
|
|
getOpposite = (w, h) => [w / 2, 0];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "bottomLeft":
|
|
|
|
isDiagonal = true;
|
|
|
|
getPoint = (w, h) => [0, h];
|
|
|
|
getOpposite = (w, h) => [w, 0];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
2023-08-04 18:21:27 +02:00
|
|
|
case "middleLeft":
|
|
|
|
isHorizontal = true;
|
|
|
|
getPoint = (w, h) => [0, h / 2];
|
|
|
|
getOpposite = (w, h) => [w, h / 2];
|
2023-07-24 09:49:49 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-08-04 18:21:27 +02:00
|
|
|
|
|
|
|
const point = getPoint(savedWidth, savedHeight);
|
|
|
|
const oppositePoint = getOpposite(savedWidth, savedHeight);
|
|
|
|
let transfOppositePoint = transf(...oppositePoint);
|
|
|
|
const oppositeX = round(savedX + transfOppositePoint[0]);
|
|
|
|
const oppositeY = round(savedY + transfOppositePoint[1]);
|
|
|
|
let ratioX = 1;
|
|
|
|
let ratioY = 1;
|
|
|
|
|
|
|
|
let [deltaX, deltaY] = this.screenToPageTranslation(
|
|
|
|
event.movementX,
|
|
|
|
event.movementY
|
|
|
|
);
|
|
|
|
[deltaX, deltaY] = invTransf(deltaX / parentWidth, deltaY / parentHeight);
|
|
|
|
|
|
|
|
if (isDiagonal) {
|
|
|
|
const oldDiag = Math.hypot(savedWidth, savedHeight);
|
|
|
|
ratioX = ratioY = Math.max(
|
|
|
|
Math.min(
|
|
|
|
Math.hypot(
|
|
|
|
oppositePoint[0] - point[0] - deltaX,
|
|
|
|
oppositePoint[1] - point[1] - deltaY
|
|
|
|
) / oldDiag,
|
|
|
|
// Avoid the editor to be larger than the page.
|
|
|
|
1 / savedWidth,
|
|
|
|
1 / savedHeight
|
|
|
|
),
|
|
|
|
// Avoid the editor to be smaller than the minimum size.
|
|
|
|
minWidth / savedWidth,
|
|
|
|
minHeight / savedHeight
|
|
|
|
);
|
|
|
|
} else if (isHorizontal) {
|
|
|
|
ratioX =
|
|
|
|
Math.max(
|
|
|
|
minWidth,
|
|
|
|
Math.min(1, Math.abs(oppositePoint[0] - point[0] - deltaX))
|
|
|
|
) / savedWidth;
|
|
|
|
} else {
|
|
|
|
ratioY =
|
|
|
|
Math.max(
|
|
|
|
minHeight,
|
|
|
|
Math.min(1, Math.abs(oppositePoint[1] - point[1] - deltaY))
|
|
|
|
) / savedHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newWidth = round(savedWidth * ratioX);
|
|
|
|
const newHeight = round(savedHeight * ratioY);
|
|
|
|
transfOppositePoint = transf(...getOpposite(newWidth, newHeight));
|
|
|
|
const newX = oppositeX - transfOppositePoint[0];
|
|
|
|
const newY = oppositeY - transfOppositePoint[1];
|
|
|
|
|
|
|
|
this.width = newWidth;
|
|
|
|
this.height = newHeight;
|
|
|
|
this.x = newX;
|
|
|
|
this.y = newY;
|
|
|
|
|
|
|
|
this.setDims(parentWidth * newWidth, parentHeight * newHeight);
|
|
|
|
this.fixAndSetPosition();
|
2023-07-24 09:49:49 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Render this editor in a div.
|
|
|
|
* @returns {HTMLDivElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
this.div = document.createElement("div");
|
2022-06-23 15:47:45 +02:00
|
|
|
this.div.setAttribute("data-editor-rotation", (360 - this.rotation) % 360);
|
2022-06-01 10:38:08 +02:00
|
|
|
this.div.className = this.name;
|
|
|
|
this.div.setAttribute("id", this.id);
|
2022-07-19 18:20:56 +02:00
|
|
|
this.div.setAttribute("tabIndex", 0);
|
2022-06-01 15:42:46 +02:00
|
|
|
|
2022-07-20 14:21:30 +02:00
|
|
|
this.setInForeground();
|
|
|
|
|
2022-07-21 10:42:15 +02:00
|
|
|
this.div.addEventListener("focusin", this.#boundFocusin);
|
|
|
|
this.div.addEventListener("focusout", this.#boundFocusout);
|
|
|
|
|
2023-07-13 18:31:08 +02:00
|
|
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
|
|
|
if (this.parentRotation % 180 !== 0) {
|
|
|
|
this.div.style.maxWidth = `${((100 * parentHeight) / parentWidth).toFixed(
|
|
|
|
2
|
|
|
|
)}%`;
|
|
|
|
this.div.style.maxHeight = `${(
|
|
|
|
(100 * parentWidth) /
|
|
|
|
parentHeight
|
|
|
|
).toFixed(2)}%`;
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:42:46 +02:00
|
|
|
const [tx, ty] = this.getInitialTranslation();
|
2022-06-23 15:47:45 +02:00
|
|
|
this.translate(tx, ty);
|
2022-06-01 10:38:08 +02:00
|
|
|
|
2023-08-02 20:08:09 +02:00
|
|
|
bindEvents(this, this.div, ["pointerdown"]);
|
2022-06-01 10:38:08 +02:00
|
|
|
|
|
|
|
return this.div;
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:04:32 +02:00
|
|
|
/**
|
2022-07-19 18:20:56 +02:00
|
|
|
* Onpointerdown callback.
|
|
|
|
* @param {PointerEvent} event
|
2022-07-04 18:04:32 +02:00
|
|
|
*/
|
2022-07-19 18:20:56 +02:00
|
|
|
pointerdown(event) {
|
2022-11-29 12:14:40 +01:00
|
|
|
const { isMac } = FeatureTest.platform;
|
2022-07-24 16:52:18 +02:00
|
|
|
if (event.button !== 0 || (event.ctrlKey && isMac)) {
|
2022-07-04 18:04:32 +02:00
|
|
|
// Avoid to focus this editor because of a non-left click.
|
|
|
|
event.preventDefault();
|
2022-07-27 14:11:29 +02:00
|
|
|
return;
|
2022-07-04 18:04:32 +02:00
|
|
|
}
|
2022-07-21 10:42:15 +02:00
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
this.#hasBeenClicked = true;
|
2023-08-02 20:08:09 +02:00
|
|
|
|
|
|
|
this.#setUpDragSession(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
#setUpDragSession(event) {
|
|
|
|
if (!this._isDraggable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
const isSelected = this._uiManager.isSelected(this);
|
|
|
|
this._uiManager.setUpDragSession();
|
|
|
|
|
|
|
|
let pointerMoveOptions, pointerMoveCallback;
|
|
|
|
if (isSelected) {
|
|
|
|
pointerMoveOptions = { passive: true, capture: true };
|
|
|
|
pointerMoveCallback = e => {
|
|
|
|
const [tx, ty] = this.screenToPageTranslation(e.movementX, e.movementY);
|
|
|
|
this._uiManager.dragSelectedEditors(tx, ty);
|
|
|
|
};
|
|
|
|
window.addEventListener(
|
2023-08-02 20:08:09 +02:00
|
|
|
"pointermove",
|
|
|
|
pointerMoveCallback,
|
|
|
|
pointerMoveOptions
|
|
|
|
);
|
2023-08-09 11:23:26 +02:00
|
|
|
}
|
2023-08-02 20:08:09 +02:00
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
const pointerUpCallback = () => {
|
|
|
|
window.removeEventListener("pointerup", pointerUpCallback);
|
|
|
|
window.removeEventListener("blur", pointerUpCallback);
|
|
|
|
if (isSelected) {
|
|
|
|
window.removeEventListener(
|
|
|
|
"pointermove",
|
|
|
|
pointerMoveCallback,
|
|
|
|
pointerMoveOptions
|
|
|
|
);
|
|
|
|
}
|
2023-08-02 20:08:09 +02:00
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
this.#hasBeenClicked = false;
|
|
|
|
if (!this._uiManager.endDragSession()) {
|
|
|
|
const { isMac } = FeatureTest.platform;
|
|
|
|
if (
|
|
|
|
(event.ctrlKey && !isMac) ||
|
|
|
|
event.shiftKey ||
|
|
|
|
(event.metaKey && isMac)
|
|
|
|
) {
|
|
|
|
this.parent.toggleSelected(this);
|
|
|
|
} else {
|
|
|
|
this.parent.setSelected(this);
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 20:08:09 +02:00
|
|
|
};
|
|
|
|
window.addEventListener("pointerup", pointerUpCallback);
|
|
|
|
// If the user is using alt+tab during the dragging session, the pointerup
|
|
|
|
// event could be not fired, but a blur event is fired so we can use it in
|
|
|
|
// order to interrupt the dragging session.
|
|
|
|
window.addEventListener("blur", pointerUpCallback);
|
2022-07-04 18:04:32 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 11:23:26 +02:00
|
|
|
moveInDOM() {
|
|
|
|
this.parent.moveEditorInDOM(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setParentAndPosition(parent, x, y) {
|
|
|
|
parent.changeParent(this);
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.fixAndSetPosition();
|
|
|
|
this.moveInDOM();
|
|
|
|
}
|
|
|
|
|
2023-07-05 19:46:21 +02:00
|
|
|
/**
|
|
|
|
* Convert the current rect into a page one.
|
|
|
|
*/
|
2022-06-23 15:47:45 +02:00
|
|
|
getRect(tx, ty) {
|
2022-12-05 12:25:06 +01:00
|
|
|
const scale = this.parentScale;
|
|
|
|
const [pageWidth, pageHeight] = this.pageDimensions;
|
2022-12-07 18:27:32 +01:00
|
|
|
const [pageX, pageY] = this.pageTranslation;
|
2022-12-05 12:25:06 +01:00
|
|
|
const shiftX = tx / scale;
|
|
|
|
const shiftY = ty / scale;
|
2022-06-23 15:47:45 +02:00
|
|
|
const x = this.x * pageWidth;
|
|
|
|
const y = this.y * pageHeight;
|
|
|
|
const width = this.width * pageWidth;
|
|
|
|
const height = this.height * pageHeight;
|
|
|
|
|
|
|
|
switch (this.rotation) {
|
|
|
|
case 0:
|
|
|
|
return [
|
2022-12-07 18:27:32 +01:00
|
|
|
x + shiftX + pageX,
|
|
|
|
pageHeight - y - shiftY - height + pageY,
|
|
|
|
x + shiftX + width + pageX,
|
|
|
|
pageHeight - y - shiftY + pageY,
|
2022-06-23 15:47:45 +02:00
|
|
|
];
|
|
|
|
case 90:
|
|
|
|
return [
|
2022-12-07 18:27:32 +01:00
|
|
|
x + shiftY + pageX,
|
|
|
|
pageHeight - y + shiftX + pageY,
|
|
|
|
x + shiftY + height + pageX,
|
|
|
|
pageHeight - y + shiftX + width + pageY,
|
2022-06-23 15:47:45 +02:00
|
|
|
];
|
|
|
|
case 180:
|
|
|
|
return [
|
2022-12-07 18:27:32 +01:00
|
|
|
x - shiftX - width + pageX,
|
|
|
|
pageHeight - y + shiftY + pageY,
|
|
|
|
x - shiftX + pageX,
|
|
|
|
pageHeight - y + shiftY + height + pageY,
|
2022-06-23 15:47:45 +02:00
|
|
|
];
|
|
|
|
case 270:
|
|
|
|
return [
|
2022-12-07 18:27:32 +01:00
|
|
|
x - shiftY - height + pageX,
|
|
|
|
pageHeight - y - shiftX - width + pageY,
|
|
|
|
x - shiftY + pageX,
|
|
|
|
pageHeight - y - shiftX + pageY,
|
2022-06-23 15:47:45 +02:00
|
|
|
];
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid rotation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:47:09 +02:00
|
|
|
getRectInCurrentCoords(rect, pageHeight) {
|
|
|
|
const [x1, y1, x2, y2] = rect;
|
|
|
|
|
|
|
|
const width = x2 - x1;
|
|
|
|
const height = y2 - y1;
|
|
|
|
|
|
|
|
switch (this.rotation) {
|
|
|
|
case 0:
|
|
|
|
return [x1, pageHeight - y2, width, height];
|
|
|
|
case 90:
|
|
|
|
return [x1, pageHeight - y1, height, width];
|
|
|
|
case 180:
|
|
|
|
return [x2, pageHeight - y1, width, height];
|
|
|
|
case 270:
|
|
|
|
return [x2, pageHeight - y2, height, width];
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid rotation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Executed once this editor has been rendered.
|
|
|
|
*/
|
|
|
|
onceAdded() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the editor contains something.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isEmpty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable edit mode.
|
|
|
|
*/
|
|
|
|
enableEditMode() {
|
|
|
|
this.#isInEditMode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable edit mode.
|
|
|
|
*/
|
|
|
|
disableEditMode() {
|
|
|
|
this.#isInEditMode = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the editor is edited.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isInEditMode() {
|
|
|
|
return this.#isInEditMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If it returns true, then this editor handle the keyboard
|
|
|
|
* events itself.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
shouldGetKeyboardEvents() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this editor needs to be rebuilt or not.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
needsToBeRebuilt() {
|
|
|
|
return this.div && !this.isAttachedToDOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rebuild the editor in case it has been removed on undo.
|
|
|
|
*
|
|
|
|
* To implement in subclasses.
|
|
|
|
*/
|
|
|
|
rebuild() {
|
2022-07-21 10:42:15 +02:00
|
|
|
this.div?.addEventListener("focusin", this.#boundFocusin);
|
2023-06-08 12:50:30 +02:00
|
|
|
this.div?.addEventListener("focusout", this.#boundFocusout);
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the editor.
|
|
|
|
* The result of the serialization will be used to construct a
|
|
|
|
* new annotation to add to the pdf document.
|
|
|
|
*
|
|
|
|
* To implement in subclasses.
|
2023-06-16 13:00:00 +02:00
|
|
|
* @param {boolean} isForCopying
|
2023-06-22 12:16:07 +02:00
|
|
|
* @param {Object} [context]
|
2022-06-01 10:38:08 +02:00
|
|
|
*/
|
2023-06-22 12:16:07 +02:00
|
|
|
serialize(_isForCopying = false, _context = null) {
|
2022-06-01 10:38:08 +02:00
|
|
|
unreachable("An editor must be serializable");
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:47:09 +02:00
|
|
|
/**
|
|
|
|
* Deserialize the editor.
|
|
|
|
* The result of the deserialization is a new editor.
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @param {AnnotationEditorLayer} parent
|
2022-12-05 12:25:06 +01:00
|
|
|
* @param {AnnotationEditorUIManager} uiManager
|
2022-07-18 14:47:09 +02:00
|
|
|
* @returns {AnnotationEditor}
|
|
|
|
*/
|
2022-12-05 12:25:06 +01:00
|
|
|
static deserialize(data, parent, uiManager) {
|
2022-07-18 14:47:09 +02:00
|
|
|
const editor = new this.prototype.constructor({
|
|
|
|
parent,
|
|
|
|
id: parent.getNextId(),
|
2022-12-05 12:25:06 +01:00
|
|
|
uiManager,
|
2022-07-18 14:47:09 +02:00
|
|
|
});
|
|
|
|
editor.rotation = data.rotation;
|
|
|
|
|
2022-12-05 12:25:06 +01:00
|
|
|
const [pageWidth, pageHeight] = editor.pageDimensions;
|
2022-07-18 14:47:09 +02:00
|
|
|
const [x, y, width, height] = editor.getRectInCurrentCoords(
|
|
|
|
data.rect,
|
|
|
|
pageHeight
|
|
|
|
);
|
|
|
|
editor.x = x / pageWidth;
|
|
|
|
editor.y = y / pageHeight;
|
|
|
|
editor.width = width / pageWidth;
|
|
|
|
editor.height = height / pageHeight;
|
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Remove this editor.
|
|
|
|
* It's used on ctrl+backspace action.
|
|
|
|
*/
|
|
|
|
remove() {
|
2022-07-21 10:42:15 +02:00
|
|
|
this.div.removeEventListener("focusin", this.#boundFocusin);
|
|
|
|
this.div.removeEventListener("focusout", this.#boundFocusout);
|
|
|
|
|
2022-07-04 18:04:32 +02:00
|
|
|
if (!this.isEmpty()) {
|
|
|
|
// The editor is removed but it can be back at some point thanks to
|
|
|
|
// undo/redo so we must commit it before.
|
|
|
|
this.commit();
|
|
|
|
}
|
2023-07-06 16:23:53 +02:00
|
|
|
if (this.parent) {
|
|
|
|
this.parent.remove(this);
|
|
|
|
} else {
|
|
|
|
this._uiManager.removeEditor(this);
|
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 09:49:49 +02:00
|
|
|
/**
|
|
|
|
* @returns {boolean} true if this editor can be resized.
|
|
|
|
*/
|
|
|
|
get isResizable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the resizers to this editor.
|
|
|
|
*/
|
|
|
|
makeResizable() {
|
|
|
|
if (this.isResizable) {
|
|
|
|
this.#createResizers();
|
|
|
|
this.#resizersDiv.classList.remove("hidden");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:38:08 +02:00
|
|
|
/**
|
|
|
|
* Select this editor.
|
|
|
|
*/
|
|
|
|
select() {
|
2023-07-24 09:49:49 +02:00
|
|
|
this.makeResizable();
|
2022-07-21 10:42:15 +02:00
|
|
|
this.div?.classList.add("selectedEditor");
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unselect this editor.
|
|
|
|
*/
|
|
|
|
unselect() {
|
2023-07-24 09:49:49 +02:00
|
|
|
this.#resizersDiv?.classList.add("hidden");
|
2022-07-21 10:42:15 +02:00
|
|
|
this.div?.classList.remove("selectedEditor");
|
2023-07-27 13:04:14 +02:00
|
|
|
if (this.div?.contains(document.activeElement)) {
|
|
|
|
// Don't use this.div.blur() because we don't know where the focus will
|
|
|
|
// go.
|
2023-07-27 20:12:38 +02:00
|
|
|
this._uiManager.currentLayer.div.focus();
|
2023-07-27 13:04:14 +02:00
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
2022-06-13 18:23:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update some parameters which have been changed through the UI.
|
|
|
|
* @param {number} type
|
|
|
|
* @param {*} value
|
|
|
|
*/
|
|
|
|
updateParams(type, value) {}
|
|
|
|
|
2022-06-28 18:21:32 +02:00
|
|
|
/**
|
|
|
|
* When the user disables the editing mode some editors can change some of
|
|
|
|
* their properties.
|
|
|
|
*/
|
|
|
|
disableEditing() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the user enables the editing mode some editors can change some of
|
|
|
|
* their properties.
|
|
|
|
*/
|
|
|
|
enableEditing() {}
|
|
|
|
|
2023-07-07 15:51:48 +02:00
|
|
|
/**
|
|
|
|
* The editor is about to be edited.
|
|
|
|
*/
|
|
|
|
enterInEditMode() {}
|
|
|
|
|
2022-06-28 18:21:32 +02:00
|
|
|
/**
|
|
|
|
* Get the div which really contains the displayed content.
|
|
|
|
*/
|
|
|
|
get contentDiv() {
|
|
|
|
return this.div;
|
|
|
|
}
|
2022-07-21 10:42:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If true then the editor is currently edited.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
get isEditing() {
|
|
|
|
return this.#isEditing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When set to true, it means that this editor is currently edited.
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
set isEditing(value) {
|
|
|
|
this.#isEditing = value;
|
2023-07-06 16:23:53 +02:00
|
|
|
if (!this.parent) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-21 10:42:15 +02:00
|
|
|
if (value) {
|
|
|
|
this.parent.setSelected(this);
|
|
|
|
this.parent.setActiveEditor(this);
|
|
|
|
} else {
|
|
|
|
this.parent.setActiveEditor(null);
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 18:09:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the aspect ratio to use when resizing.
|
|
|
|
* @param {number} width
|
|
|
|
* @param {number} height
|
|
|
|
*/
|
|
|
|
setAspectRatio(width, height) {
|
2023-07-06 09:21:34 +02:00
|
|
|
this.#keepAspectRatio = true;
|
|
|
|
const aspectRatio = width / height;
|
|
|
|
const { style } = this.div;
|
|
|
|
style.aspectRatio = aspectRatio;
|
|
|
|
style.height = "auto";
|
2023-07-05 18:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static get MIN_SIZE() {
|
2023-07-24 09:49:49 +02:00
|
|
|
return 16;
|
2023-07-05 18:09:53 +02:00
|
|
|
}
|
2023-06-06 17:18:02 +02:00
|
|
|
}
|
2023-06-05 11:32:44 +02:00
|
|
|
|
2023-06-06 17:18:02 +02:00
|
|
|
// This class is used to fake an editor which has been deleted.
|
|
|
|
class FakeEditor extends AnnotationEditor {
|
|
|
|
constructor(params) {
|
|
|
|
super(params);
|
|
|
|
this.annotationElementId = params.annotationElementId;
|
|
|
|
this.deleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
serialize() {
|
|
|
|
return {
|
|
|
|
id: this.annotationElementId,
|
|
|
|
deleted: true,
|
|
|
|
pageIndex: this.pageIndex,
|
|
|
|
};
|
2023-06-05 11:32:44 +02:00
|
|
|
}
|
2022-06-01 10:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { AnnotationEditor };
|