Merge eb1f31d1c84c20b453be708949a73ceef3ab5044 into 3676ffde176830f01e91ede7bfdad47073da535a

This commit is contained in:
pilipovicmilan04 2025-11-10 13:56:05 +00:00 committed by GitHub
commit dabd86ae02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 96 additions and 4 deletions

View File

@ -568,6 +568,7 @@ class AnnotationEditorLayer {
this.attach(editor);
editor.parent?.detach(editor);
this.resetRotation(editor);
editor.setParent(this);
if (editor.div && editor.isAttachedToDOM) {
editor.div.remove();
@ -575,6 +576,27 @@ class AnnotationEditorLayer {
}
}
/**
* When a page is rotated, the editor must be rotated as well
* to maintain the same orientation.
* @param {AnnotationEditor} editor
*/
resetRotation(editor) {
if (this.viewport.rotation === editor.parent.viewport.rotation) {
return;
}
const rotationOfThisPage = this.viewport.rotation;
const currentRotation =
360 - Number(this.div.getAttribute("data-main-rotation"));
const pageRotation =
(360 + rotationOfThisPage - editor._uiManager.viewParameters.rotation) %
360;
editor.pageRotation = pageRotation;
editor.rotation = rotationOfThisPage;
editor.div.setAttribute("data-editor-rotation", currentRotation);
}
/**
* Add a new editor in the current view.
* @param {AnnotationEditor} editor

View File

@ -291,6 +291,42 @@ class AnnotationEditor {
return false;
}
/**
* Rotate a point about 0.5 0.5 origin
* @param {number} x
* @param {number} y
* @param {number} angle
* @returns {any} The rotated point
*/
static rotatePointByMidPoint(x, y, angle) {
const originX = 0.5;
const originY = 0.5;
// Translate the point to the origin (originX, originY)
const translatedX = x - originX;
const translatedY = y - originY;
let rotatedX, rotatedY;
// Perform the rotation based on the given angle
switch (angle) {
case 90:
rotatedX = -translatedY;
rotatedY = translatedX;
break;
case 270:
rotatedX = translatedY;
rotatedY = -translatedX;
break;
default:
throw new Error("Invalid angle. Valid angles are 90 and 270 degrees.");
}
// Translate the point back
const finalX = rotatedX + originX;
const finalY = rotatedY + originY;
return { x: finalX, y: finalY };
}
/**
* Extract the data from the clipboard item and delegate the creation of the
* editor to the parent.
@ -532,6 +568,9 @@ class AnnotationEditor {
} = this;
this.x += tx / parentWidth;
this.y += ty / parentHeight;
const oldRotation = this.rotation;
if (this.parent && (this.x < 0 || this.x > 1 || this.y < 0 || this.y > 1)) {
// It's possible to not have a parent: for example, when the user is
// dragging all the selected editors but this one on a page which has been
@ -539,14 +578,45 @@ class AnnotationEditor {
// It's why we need to check for it. In such a situation, it isn't really
// a problem to not find a new parent: it's something which is related to
// what the user is seeing, hence it depends on how pages are layed out.
// 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)) {
const newRotation = this.rotation;
if (oldRotation !== newRotation) {
const {
x: layerX,
y: layerY,
width,
height,
} = this.parent.div.getBoundingClientRect();
this.x = (x - layerX) / width;
this.y = (y - layerY) / height;
if (newRotation === 90) {
const points = AnnotationEditor.rotatePointByMidPoint(
this.x,
this.y,
270
);
this.x = points.x;
this.y = points.y;
} else if (newRotation === 270) {
const points = AnnotationEditor.rotatePointByMidPoint(
this.x,
this.y,
90
);
this.x = points.x;
this.y = points.y;
} else if (newRotation === 180) {
this.x = 1 - this.x;
this.y = 1 - this.y;
}
} else {
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.