44 * @param event
55 */
66const handleServiceMessages = function ( event ) {
7+ const diagrams = getDiagramsEditor ( ) ;
8+ // early exit
9+ if ( ! diagrams ) {
10+ return ;
11+ }
12+
13+ // some browsers stubbornly cache request data and mess up subsequent edits
14+ disableRequestCaching ( ) ;
15+
716 // get diagram info passed to the function
817 const fullId = event . data . fullId ;
918 const { ns, id} = splitFullId ( fullId ) ;
1019
1120 const msg = JSON . parse ( event . originalEvent . data ) ;
12- const diagrams = jQuery ( '#diagrams-frame' ) [ 0 ] . contentWindow ;
1321 if ( msg . event === 'init' ) {
1422 // try loading existing diagram file
1523 jQuery . get ( DOKU_BASE + 'lib/exe/fetch.php?media=' + fullId , function ( data ) {
@@ -33,8 +41,6 @@ const handleServiceMessages = function( event ) {
3341 } ) . join ( '' ) ) ;
3442 jQuery . post ( getLocalDiagramUrl ( ns , id ) , datastr )
3543 . done ( function ( ) {
36- jQuery ( window ) . off ( 'message' , { fullId : fullId } , handleServiceMessages ) ;
37- jQuery ( '#diagrams-frame' ) . remove ( ) ;
3844 const url = new URL ( location . href ) ;
3945 // media manager window should show current namespace
4046 url . searchParams . set ( 'ns' , ns ) ;
@@ -44,10 +50,12 @@ const handleServiceMessages = function( event ) {
4450 } )
4551 . fail ( function ( ) {
4652 alert ( LANG . plugins . diagrams . errorSaving ) ;
53+ } )
54+ . always ( function ( ) {
55+ removeDiagramsEditor ( handleServiceMessages ) ;
4756 } ) ;
4857 }
4958 } else if ( msg . event === 'exit' ) {
50- jQuery ( window ) . off ( 'message' , { fullId : fullId } , handleServiceMessages ) ;
51- jQuery ( '#diagrams-frame' ) . remove ( ) ;
59+ removeDiagramsEditor ( handleServiceMessages ) ;
5260 }
5361} ;
0 commit comments