Merge eb1f31d1c84c20b453be708949a73ceef3ab5044 into 3676ffde176830f01e91ede7bfdad47073da535a
This commit is contained in:
commit
dabd86ae02
@ -568,6 +568,7 @@ class AnnotationEditorLayer {
|
|||||||
|
|
||||||
this.attach(editor);
|
this.attach(editor);
|
||||||
editor.parent?.detach(editor);
|
editor.parent?.detach(editor);
|
||||||
|
this.resetRotation(editor);
|
||||||
editor.setParent(this);
|
editor.setParent(this);
|
||||||
if (editor.div && editor.isAttachedToDOM) {
|
if (editor.div && editor.isAttachedToDOM) {
|
||||||
editor.div.remove();
|
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.
|
* Add a new editor in the current view.
|
||||||
* @param {AnnotationEditor} editor
|
* @param {AnnotationEditor} editor
|
||||||
|
|||||||
@ -291,6 +291,42 @@ class AnnotationEditor {
|
|||||||
return false;
|
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
|
* Extract the data from the clipboard item and delegate the creation of the
|
||||||
* editor to the parent.
|
* editor to the parent.
|
||||||
@ -532,6 +568,9 @@ class AnnotationEditor {
|
|||||||
} = this;
|
} = this;
|
||||||
this.x += tx / parentWidth;
|
this.x += tx / parentWidth;
|
||||||
this.y += ty / parentHeight;
|
this.y += ty / parentHeight;
|
||||||
|
|
||||||
|
const oldRotation = this.rotation;
|
||||||
|
|
||||||
if (this.parent && (this.x < 0 || this.x > 1 || this.y < 0 || this.y > 1)) {
|
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
|
// 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
|
// dragging all the selected editors but this one on a page which has been
|
||||||
@ -539,12 +578,43 @@ class AnnotationEditor {
|
|||||||
// It's why we need to check for it. In such a situation, it isn't really
|
// 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
|
// 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.
|
// 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();
|
const { x, y } = this.div.getBoundingClientRect();
|
||||||
if (this.parent.findNewParent(this, x, y)) {
|
if (this.parent.findNewParent(this, x, y)) {
|
||||||
this.x -= Math.floor(this.x);
|
const newRotation = this.rotation;
|
||||||
this.y -= Math.floor(this.y);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user