Skip to content

Commit ed9656f

Browse files
committed
Properly remove no longer needed editors
Some failed requests used to leave defunct editors listening to messages. In the worst case those zombie editors would receive diagram data not intended for them and overwrite the contents of wrong files.
1 parent 95ea965 commit ed9656f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

script/helpers.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
const serviceUrl = 'https://embed.diagrams.net/?embed=1&proto=json&spin=1';
22
const doctypeXML = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
33

4+
/**
5+
* Return the diagrams editor object or false
6+
* if it does not exist.
7+
*
8+
* @returns {boolean|WindowProxy}
9+
*/
10+
function getDiagramsEditor() {
11+
const diagramsFrame = jQuery('#diagrams-frame');
12+
if (diagramsFrame && typeof diagramsFrame[0] !== 'undefined') {
13+
return diagramsFrame[0].contentWindow;
14+
}
15+
return false;
16+
}
17+
18+
/**
19+
* Detach the message event handler and remove the editor
20+
*
21+
* @param handler
22+
*/
23+
function removeDiagramsEditor(handler) {
24+
jQuery(window).off( 'message', handler );
25+
jQuery('#diagrams-frame').remove();
26+
}
27+
428
/**
529
* check if name/id of new diagram is valid
630
*

script/service.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
* @param event
55
*/
66
const handleServiceMessages = function( event ) {
7+
const diagrams = getDiagramsEditor();
8+
// early exit
9+
if (!diagrams) {
10+
return;
11+
}
12+
713
// get diagram info passed to the function
814
const fullId = event.data.fullId;
915
const {ns, id} = splitFullId(fullId);
1016

1117
const msg = JSON.parse( event.originalEvent.data );
12-
const diagrams = jQuery( '#diagrams-frame' )[0].contentWindow;
1318
if( msg.event === 'init' ) {
1419
// try loading existing diagram file
1520
jQuery.get(DOKU_BASE + 'lib/exe/fetch.php?media=' + fullId, function (data) {
@@ -33,8 +38,6 @@ const handleServiceMessages = function( event ) {
3338
} ).join( '' ) );
3439
jQuery.post( getLocalDiagramUrl(ns, id), datastr )
3540
.done( function() {
36-
jQuery( window ).off( 'message', {fullId: fullId}, handleServiceMessages );
37-
jQuery( '#diagrams-frame' ).remove();
3841
const url = new URL(location.href);
3942
// media manager window should show current namespace
4043
url.searchParams.set('ns', ns);
@@ -44,10 +47,12 @@ const handleServiceMessages = function( event ) {
4447
})
4548
.fail( function() {
4649
alert( LANG.plugins.diagrams.errorSaving );
50+
})
51+
.always( function() {
52+
removeDiagramsEditor(handleServiceMessages);
4753
});
4854
}
4955
} else if( msg.event === 'exit' ) {
50-
jQuery( window ).off( 'message', {fullId: fullId}, handleServiceMessages );
51-
jQuery( '#diagrams-frame' ).remove();
56+
removeDiagramsEditor(handleServiceMessages);
5257
}
5358
};

0 commit comments

Comments
 (0)