Skip to content

Commit

Permalink
experimental use of HTML5DownloadFileSave
Browse files Browse the repository at this point in the history
alpha code - fallback handling for saveFile() using link to data: URI.
Still not creating 100% correct content, but it's getting there...
  • Loading branch information
ericshulman committed Apr 23, 2013
1 parent c1799ca commit 9042417
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
25 changes: 25 additions & 0 deletions js/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ window.saveFile = window.saveFile || function(fileUrl,content)
r = ieSaveFile(fileUrl,content);
if(!r)
r = javaSaveFile(fileUrl,content);
if(!r)
r = HTML5DownloadSaveFile(fileUrl,content);
return r;
}

Expand Down Expand Up @@ -261,3 +263,26 @@ function javaLoadFile(filePath)
return content.join("\n");
}


// Returns null if it can't do it, false if there's an error, true if it saved OK
function HTML5DownloadSaveFile(filePath,content)
{
if(document.createElement("a").download !== undefined) {
try {
var slashpos=filePath.lastIndexOf("/");
if (slashpos==-1) slashpos=filePath.lastIndexOf("\\");
var filename=filePath.substr(slashpos+1);
var link = document.createElement("a");
link.setAttribute("target","_blank");
link.setAttribute("href","data:text/html," + encodeURIComponent(content));
link.setAttribute("download",filename);
link.click();
return true;
} catch(ex) {
//# alert("Exception while attempting to save\n\n" + ex);
return false;
}

}
return null;
}
19 changes: 18 additions & 1 deletion js/Saving.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,24 @@ function autoSaveChanges(onlyIfDirty,tiddlers)

function loadOriginal(localPath)
{
return loadFile(localPath);
var content=loadFile(localPath);
if (!content) content=recreateOriginal(localPath);
return content;
}

function recreateOriginal(localPath)
{
// construct doctype
var t=document.doctype;
var content = "<!DOCTYPE "+t.name;
if (t.publicId) content+=' PUBLIC "'+t.publicId+'"';
else if (t.systemId) content+=' SYSTEM "'+t.systemId+'"';
content+=">\n";
// append document content
content+=document.documentElement.outerHTML;
// clear 'savetest' marker
content=content.replace(/<div id="saveTest">savetest<\/div>/,'<div id="saveTest"></div>');
return content;
}

// Save this tiddlywiki with the pending changes
Expand Down

0 comments on commit 9042417

Please sign in to comment.