Skip to content

Commit f025743

Browse files
authored
Merge pull request #2 from cosmocode/zombie-diagrams
Don't let lingering data from previous requests interfere with the editing workflow
2 parents 95ea965 + 759326f commit f025743

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

script/helpers.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
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+
28+
/**
29+
* Explicitly disable caching of the AJAX request.
30+
*/
31+
function disableRequestCaching() {
32+
jQuery.ajaxSetup({
33+
cache: false,
34+
});
35+
}
36+
437
/**
538
* check if name/id of new diagram is valid
639
*

script/service.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
* @param event
55
*/
66
const 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

Comments
 (0)