@@ -515,46 +515,68 @@ ShapeManager.prototype.pasteShapesJson = function pasteShapesJson(
515515) {
516516 var self = this ,
517517 newShapes = [ ] ,
518- allPasted = true ;
518+ pastedShapes = [ ] ,
519+ allPasted = true ,
520+ idsMap = new Map ( ) ;
521+
519522 // For each shape we want to paste...
520523 jsonShapes . forEach ( function ( sh ) {
521524 var csh = $ . extend ( true , { } , sh ) ;
522525 if ( csh . type == "Label" ) {
523526 csh = self . convertOmeroLabelToFigureText ( csh )
524527 }
525528
526- if ( csh . type != "Text" || csh . linkedShapeId == undefined ||
527- csh . linkedShapeId == - 1 || csh . isFromOmero ) {
528- // Create a shape to resolve any transform matrix -> coords
529- var tempTextId = csh . textId
530- csh . textId = - 1
531-
532- var temp = self . createShapeJson ( csh ) ;
533- var s = temp . toJson ( ) ;
534- temp . destroy ( ) ;
535-
536- if ( ! csh . isFromOmero )
537- s . textId = - 1
538- else
539- s . textId = tempTextId
540-
541- // check if a shape is at the same coordinates...
542- var match = self . findShapeAtCoords ( s ) ;
543- // if so, keep offsetting until we find a spot...
544- while ( match ) {
545- s = $ . extend ( { } , s ) ;
546- s = match . offsetCoords ( s , 20 , 10 ) ;
547- match = self . findShapeAtCoords ( s ) ;
548- }
549- // Create shape and test if it's in the specified region
550- var added = self . addShapeJson ( s , constrainRegion ) ;
551- if ( added ) {
552- newShapes . push ( added ) ;
553- } else {
554- allPasted = false ;
555- }
529+ // get the ids before cloning/deleteing temp shape
530+ var tempTextId = csh . textId
531+ var tempLinkedShapeId = csh . linkedShapeId
532+ var oldId = csh . id
533+ csh . textId = - 1
534+ csh . linkedShapeId = - 1
535+ csh . id = self . getRandomId ( )
536+
537+ // create the new JSON shape
538+ var temp = self . createShapeJson ( csh ) ;
539+ var s = temp . toJson ( ) ;
540+ temp . destroy ( ) ;
541+
542+ // reset ids
543+ s . textId = tempTextId
544+ s . linkedShapeId = tempLinkedShapeId
545+
546+ // check if a shape is at the same coordinates...
547+ var match = self . findShapeAtCoords ( s ) ;
548+ // if so, keep offsetting until we find a spot...
549+ while ( match ) {
550+ s = $ . extend ( { } , s ) ;
551+ s = match . offsetCoords ( s , 20 , 10 ) ;
552+ match = self . findShapeAtCoords ( s ) ;
556553 }
554+
555+ pastedShapes . push ( s ) ;
556+ idsMap . set ( s . id , oldId )
557557 } ) ;
558+
559+ // reassign right text & shape ids to link text to the right shape
560+ pastedShapes . forEach ( function ( s ) {
561+ if ( s . textId != undefined && s . textId != - 1 ) {
562+ var oldShapeId = idsMap . get ( s . id )
563+ pastedShapes . forEach ( function ( s2 ) {
564+ if ( s2 . linkedShapeId == oldShapeId ) {
565+ s2 . linkedShapeId = s . id
566+ s . textId = s2 . id
567+ }
568+ } )
569+ }
570+
571+ // add the news shapes to the manager
572+ var added = self . addShapeJson ( s , constrainRegion ) ;
573+ if ( added ) {
574+ newShapes . push ( added ) ;
575+ } else {
576+ allPasted = false ;
577+ }
578+ } )
579+
558580 // Select the newly added shapes
559581 this . selectShapes ( newShapes ) ;
560582 return allPasted ;
0 commit comments