mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-07-05 19:42:51 +00:00
JSZip Plugin: Add ability to build and download ZIP files
This commit is contained in:
parent
85fd43a38c
commit
6a0ff7db18
@ -15,7 +15,8 @@
|
|||||||
"tiddlywiki/dynannotate",
|
"tiddlywiki/dynannotate",
|
||||||
"tiddlywiki/codemirror",
|
"tiddlywiki/codemirror",
|
||||||
"tiddlywiki/comments",
|
"tiddlywiki/comments",
|
||||||
"tiddlywiki/menubar"
|
"tiddlywiki/menubar",
|
||||||
|
"tiddlywiki/jszip"
|
||||||
],
|
],
|
||||||
"themes": [
|
"themes": [
|
||||||
"tiddlywiki/vanilla",
|
"tiddlywiki/vanilla",
|
||||||
|
43
plugins/tiddlywiki/jszip/docs.tid
Normal file
43
plugins/tiddlywiki/jszip/docs.tid
Normal file
@ -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
|
24
plugins/tiddlywiki/jszip/examples.tid
Normal file
24
plugins/tiddlywiki/jszip/examples.tid
Normal file
@ -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={{{ [<currentTiddler>encodeuricomponent[]addsuffix[.html]] }}} tiddler=<<currentTiddler>> template="$:/core/templates/static.tiddler.html"/>
|
||||||
|
</$list>
|
||||||
|
<$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:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<$text text=<<actions-render-static-site>>/>
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<$button actions=<<actions-render-static-site>>>
|
||||||
|
Render site
|
||||||
|
</$button>
|
||||||
|
|
||||||
|
Temporary zip file: $:/temp/_ZipTiddler
|
@ -3,5 +3,5 @@
|
|||||||
"name": "JSZip",
|
"name": "JSZip",
|
||||||
"description": "JSZip library",
|
"description": "JSZip library",
|
||||||
"author": "Stuart Knightley, David Duponchel, Franz Buchinger, António Afonso",
|
"author": "Stuart Knightley, David Duponchel, Franz Buchinger, António Afonso",
|
||||||
"list": "readme license"
|
"list": "readme docs examples license"
|
||||||
}
|
}
|
||||||
|
103
plugins/tiddlywiki/jszip/startup.js
Normal file
103
plugins/tiddlywiki/jszip/startup.js
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})();
|
Loading…
x
Reference in New Issue
Block a user