diff --git a/editions/prerelease/tiddlywiki.info b/editions/prerelease/tiddlywiki.info index 2dba4aed6..5d52d5e4a 100644 --- a/editions/prerelease/tiddlywiki.info +++ b/editions/prerelease/tiddlywiki.info @@ -15,7 +15,8 @@ "tiddlywiki/dynannotate", "tiddlywiki/codemirror", "tiddlywiki/comments", - "tiddlywiki/menubar" + "tiddlywiki/menubar", + "tiddlywiki/jszip" ], "themes": [ "tiddlywiki/vanilla", diff --git a/plugins/tiddlywiki/jszip/docs.tid b/plugins/tiddlywiki/jszip/docs.tid new file mode 100644 index 000000000..7e605a9cc --- /dev/null +++ b/plugins/tiddlywiki/jszip/docs.tid @@ -0,0 +1,43 @@ +title: $:/plugins/tiddlywiki/jszip/docs + +The following messages are provided to allow programmatic manipulation of ZIP files stored within tiddlers: + +!! Create ZIP file + +``` +<$action-sendmessage $message="tm-zip-create" $param="MyZipTiddler"/> +``` + +* ''$param'': title of tiddler to contain ZIP file + +!! Add/replace text file within ZIP file + +``` +<$action-sendmessage $message="tm-zip-add-text-file" $param="MyZipTiddler" filename="my/newfilename.txt" text="The content"/> +``` + +* ''$param'': title of tiddler containing ZIP file +* ''filename'': filename of file to be added +* ''text'': text content of file to be added + +!! Render tiddler to ZIP file + +``` +<$action-sendmessage $message="tm-zip-render-file" $param="MyZipTiddler" filename="my/newfilename.txt" tiddler="HelloThere" template="The content" mode="block" output="text/plain"/> +``` + +* ''$param'': title of tiddler containing ZIP file +* ''filename'': filename of output file +* ''tiddler'': optional title of currentTiddler for rendering template +* ''template'': title of template tiddler to be rendered +* ''mode'': optional parsing mode "block" (default) or "inline" +* ''output'': output format: "text/plain" (default) for the text content or "text/html" for the full HTML content, including tags + +!! Download a ZIP file + +``` +<$action-sendmessage $message="tm-zip-download" $param="MyZipTiddler" filename="myzipfile.zip"/> +``` + +* ''$param'': title of tiddler containing ZIP file +* ''filename'': filename to be suggested to browser for downloaded file diff --git a/plugins/tiddlywiki/jszip/examples.tid b/plugins/tiddlywiki/jszip/examples.tid new file mode 100644 index 000000000..681a0c364 --- /dev/null +++ b/plugins/tiddlywiki/jszip/examples.tid @@ -0,0 +1,24 @@ +title: $:/plugins/tiddlywiki/jszip/examples + +\define actions-render-static-site() +<$action-sendmessage $message="tm-zip-create" $param="$:/temp/_ZipTiddler"/> +<$list filter="[all[tiddlers]!is[system]limit[100]]"> +<$action-sendmessage $message="tm-zip-render-file" $param="$:/temp/_ZipTiddler" filename={{{ [encodeuricomponent[]addsuffix[.html]] }}} tiddler=<> template="$:/core/templates/static.tiddler.html"/> + +<$action-sendmessage $message="tm-zip-render-file" $param="$:/temp/_ZipTiddler" filename="static.css" template="$:/core/templates/static.template.css"/> +<$action-sendmessage $message="tm-zip-download" $param="$:/temp/_ZipTiddler" filename="myzip.zip"/> +\end + +! Rendering a Static Site to a Zip File + +The actions below create a ZIP file containing a static HTML rendering of the first 100 non-system tiddlers: + +
+<$text text=<>/>
+
+ +<$button actions=<>> +Render site + + +Temporary zip file: $:/temp/_ZipTiddler diff --git a/plugins/tiddlywiki/jszip/plugin.info b/plugins/tiddlywiki/jszip/plugin.info index ee0d00145..dcf94eeb2 100644 --- a/plugins/tiddlywiki/jszip/plugin.info +++ b/plugins/tiddlywiki/jszip/plugin.info @@ -3,5 +3,5 @@ "name": "JSZip", "description": "JSZip library", "author": "Stuart Knightley, David Duponchel, Franz Buchinger, António Afonso", - "list": "readme license" + "list": "readme docs examples license" } diff --git a/plugins/tiddlywiki/jszip/startup.js b/plugins/tiddlywiki/jszip/startup.js new file mode 100644 index 000000000..588ef1476 --- /dev/null +++ b/plugins/tiddlywiki/jszip/startup.js @@ -0,0 +1,103 @@ +/*\ +title: $:/plugins/tiddlywiki/jszip/startup.js +type: application/javascript +module-type: startup + +Setup the root widget event handlers + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var JSZip = require("$:/plugins/tiddlywiki/jszip/jszip.js"); + +// Export name and synchronous status +exports.name = "jszip"; +exports.platforms = ["browser"]; +exports.after = ["startup"]; +exports.synchronous = true; + +// Install the root widget event handlers +exports.startup = function() { + $tw.rootWidget.addEventListener("tm-zip-create",function(event) { + if(event.param) { + var zip = new JSZip(); + saveZipTiddler(event.param,zip); + } + }); + $tw.rootWidget.addEventListener("tm-zip-add-text-file",function(event) { + var paramObject = event.paramObject || {}; + if(event.param && paramObject.filename && paramObject.text) { + var zip = loadZipTiddler(event.param); + zip.file(paramObject.filename,paramObject.text); + saveZipTiddler(event.param,zip); + } + }); + $tw.rootWidget.addEventListener("tm-zip-render-file",function(event) { + var paramObject = event.paramObject || {}; + if(event.param && paramObject.filename && paramObject.template) { + var zip = loadZipTiddler(event.param), + outputType = paramObject.output || "text/plain", + templateTitle = paramObject.template, + text = $tw.wiki.renderTiddler(outputType,templateTitle,{ + parseAsInline: paramObject.mode === "inline", + variables: { + currentTiddler: paramObject.tiddler + } + }); + zip.file(paramObject.filename,text); + saveZipTiddler(event.param,zip); + } + }); + $tw.rootWidget.addEventListener("tm-zip-download",function(event) { + var paramObject = event.paramObject || {}; + if(event.param) { + downloadZipFile(event.param,paramObject.filename || "file.zip"); + } + }); +}; + +function loadZipTiddler(title) { + return $tw.wiki.getGlobalCache("jszip",function() { + var zip = new JSZip(), + tiddler = $tw.wiki.getTiddler(title); + if(tiddler && tiddler.fields.type === "application/zip") { + try { + zip.load(tiddler.fields.text,{ + base64: true + }); + } catch(e) { + console.log("JSZip error: " + e) + } + } + return zip; + }); +} + +function saveZipTiddler(title,zip) { + var data = zip.generate({ + type: "base64" + }); + $tw.wiki.addTiddler({ + title: title, + type: "application/zip", + text: data + }); +} + +function downloadZipFile(title,filename) { + var tiddler = $tw.wiki.getTiddler(title); + if(tiddler && tiddler.fields.text && tiddler.fields.type === "application/zip") { + var link = document.createElement("a"); + link.setAttribute("href","data:application/zip;base64," + encodeURIComponent(tiddler.fields.text)); + link.setAttribute("download",filename); + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + } +} + +})();