Skip to content

Commit

Permalink
Merge pull request #13 from lejmr/autosave
Browse files Browse the repository at this point in the history
Draft support
  • Loading branch information
lejmr authored Mar 17, 2020
2 parents ab32dce + 0807980 commit 0f3df9e
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 8 deletions.
34 changes: 31 additions & 3 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ function _ajax_call(Doku_Event $event, $param) {
global $INPUT; //available since release 2012-10-13 "Adora Belle"
$name = $INPUT->str('imageName');
$action = $INPUT->str('action');

$media_id = $name . '.png';

$suffix = strpos($action, "draft_") === 0 ? '.png.draft':'.png';
$media_id = $name . $suffix;
$media_id = cleanID($media_id);
$fl = mediaFN($media_id);

Expand Down Expand Up @@ -104,7 +105,7 @@ function _ajax_call(Doku_Event $event, $param) {
} else {
addMediaLogEntry($new, $media_id, DOKU_CHANGE_TYPE_CREATE, $lang['created'], '', null, $sizechange);
}
}
}
if($action == 'get'){
if (!file_exists($fl)) return;
// Return image in the base64 for draw.io
Expand All @@ -114,6 +115,33 @@ function _ajax_call(Doku_Event $event, $param) {
$fc = file_get_contents($fl);
echo $json->encode(array("content" => "data:image/png;base64,".base64_encode($fc)));
}

// Draft section
if($action == 'draft_save'){
// prepare directory
io_createNamespace($media_id, 'media');

// Format content of draft file
$json = new JSON();
$content = $INPUT->str('content');

// Write content to file
$whandle = fopen($fl, 'w');
fwrite($whandle, $content);
fclose($whandle);
}
if($action == 'draft_rm'){
unlink($fl);
}
if($action == 'draft_get'){
header('Content-Type: application/json');
$json = new JSON();
if (file_exists($fl)){
echo file_get_contents($fl);
}else {
echo $json->encode(array("content" => "NaN"));
}
}
}
}
?>
93 changes: 88 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,49 @@ function edit_cb(image)
};

var draft = localStorage.getItem('.draft-' + name);

if (draft != null)

// Prefer the draft from browser cache
if(draft == null){
// Try to find on-disk stored draft file
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
action: 'draft_get'
},
function(data) {
if (data.content != 'NaN') {

// Set draft from received data
draft = data;

// Handle the discard - remove on disk
if (!confirm("A version of this diagram from " + new Date(data.lastModified) + " is available. Would you like to continue editing?"))
{
// clean draft variable
draft = null;

// Remove all draft files
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
action: 'draft_rm'
}
);
}
}
}
);
}
else
{

draft = JSON.parse(draft);

if (!confirm("A version of this page from " + new Date(draft.lastModified) + " is available. Would you like to continue editing?"))
if (!confirm("A version of this diagram from " + new Date(draft.lastModified) + " is available. Would you like to continue editing?"))
{
draft = null;
}
Expand Down Expand Up @@ -97,6 +134,16 @@ function edit_cb(image)
action: 'save'
}
);

// Remove all draft files
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
action: 'draft_rm'
}
);

// Clean cache of this page
var url = new URL(window.location.href);
Expand All @@ -105,18 +152,54 @@ function edit_cb(image)
}
else if (msg.event == 'autosave')
{
localStorage.setItem('.draft-' + name, JSON.stringify({lastModified: new Date(), xml: msg.xml}));
dr = JSON.stringify({lastModified: new Date(), xml: msg.xml});
localStorage.setItem('.draft-' + name, dr);

// Save on-disk
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
content: dr,
action: 'draft_save'
}
);
}
else if (msg.event == 'save')
{
iframe.contentWindow.postMessage(JSON.stringify({action: 'export',
format: 'xmlpng', xml: msg.xml, spin: 'Updating page'}), '*');
localStorage.setItem('.draft-' + name, JSON.stringify({lastModified: new Date(), xml: msg.xml}));
dr = JSON.stringify({lastModified: new Date(), xml: msg.xml});
localStorage.setItem('.draft-' + name, dr);

// Save on-disk
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
content: dr,
action: 'draft_save'
}
);
}
else if (msg.event == 'exit')
{
localStorage.removeItem('.draft-' + name);
draft = null;

// Remove all draft files
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
action: 'draft_rm'
}
);

// Final close (dont know why though)
close();
}
}
Expand Down

0 comments on commit 0f3df9e

Please sign in to comment.