@@ -259,6 +259,42 @@ class AnnotationEditor {
259259 return false ;
260260 }
261261
262+ /**
263+ * Rotate a point about 0.5 0.5 origin
264+ * @param {number } x
265+ * @param {number } y
266+ * @param {number } angle
267+ * @returns {any } The rotated point
268+ */
269+ static rotatePointByMidPoint ( x , y , angle ) {
270+ const originX = 0.5 ;
271+ const originY = 0.5 ;
272+ // Translate the point to the origin (originX, originY)
273+ const translatedX = x - originX ;
274+ const translatedY = y - originY ;
275+ let rotatedX , rotatedY ;
276+
277+ // Perform the rotation based on the given angle
278+ switch ( angle ) {
279+ case 90 :
280+ rotatedX = - translatedY ;
281+ rotatedY = translatedX ;
282+ break ;
283+ case 270 :
284+ rotatedX = translatedY ;
285+ rotatedY = - translatedX ;
286+ break ;
287+ default :
288+ throw new Error ( "Invalid angle. Valid angles are 90 and 270 degrees." ) ;
289+ }
290+
291+ // Translate the point back
292+ const finalX = rotatedX + originX ;
293+ const finalY = rotatedY + originY ;
294+
295+ return { x : finalX , y : finalY } ;
296+ }
297+
262298 /**
263299 * Extract the data from the clipboard item and delegate the creation of the
264300 * editor to the parent.
@@ -469,19 +505,53 @@ class AnnotationEditor {
469505 const [ parentWidth , parentHeight ] = this . parentDimensions ;
470506 this . x += tx / parentWidth ;
471507 this . y += ty / parentHeight ;
508+
509+ const oldRotation = this . rotation ;
510+
472511 if ( this . parent && ( this . x < 0 || this . x > 1 || this . y < 0 || this . y > 1 ) ) {
473512 // It's possible to not have a parent: for example, when the user is
474513 // dragging all the selected editors but this one on a page which has been
475514 // destroyed.
476515 // It's why we need to check for it. In such a situation, it isn't really
477516 // a problem to not find a new parent: it's something which is related to
478517 // what the user is seeing, hence it depends on how pages are layed out.
479-
480- // The element will be outside of its parent so change the parent.
481518 const { x, y } = this . div . getBoundingClientRect ( ) ;
482519 if ( this . parent . findNewParent ( this , x , y ) ) {
483- this . x -= Math . floor ( this . x ) ;
484- this . y -= Math . floor ( this . y ) ;
520+ const newRotation = this . rotation ;
521+ if ( oldRotation !== newRotation ) {
522+ const {
523+ x : layerX ,
524+ y : layerY ,
525+ width,
526+ height,
527+ } = this . parent . div . getBoundingClientRect ( ) ;
528+ this . x = ( x - layerX ) / width ;
529+ this . y = ( y - layerY ) / height ;
530+
531+ if ( newRotation === 90 ) {
532+ const points = AnnotationEditor . rotatePointByMidPoint (
533+ this . x ,
534+ this . y ,
535+ 270
536+ ) ;
537+ this . x = points . x ;
538+ this . y = points . y ;
539+ } else if ( newRotation === 270 ) {
540+ const points = AnnotationEditor . rotatePointByMidPoint (
541+ this . x ,
542+ this . y ,
543+ 90
544+ ) ;
545+ this . x = points . x ;
546+ this . y = points . y ;
547+ } else if ( newRotation === 180 ) {
548+ this . x = 1 - this . x ;
549+ this . y = 1 - this . y ;
550+ }
551+ } else {
552+ this . x -= Math . floor ( this . x ) ;
553+ this . y -= Math . floor ( this . y ) ;
554+ }
485555 }
486556 }
487557
0 commit comments