1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00

Beginnings of support for syncing

This commit is contained in:
Jeremy Ruston 2012-11-11 14:13:08 +00:00
parent 030f16981a
commit d7d6e59865
4 changed files with 100 additions and 15 deletions

View File

@ -116,19 +116,20 @@ $tw.config.fileExtensionInfo = {
// Content type mappings
$tw.config.contentTypeInfo = {
"application/x-tiddler": {encoding: "utf8"},
"application/x-tiddler-html-div": {encoding: "utf8"},
"application/x-tiddlywiki-recipe": {encoding: "utf8"},
"text/plain": {encoding: "utf8"},
"text/css": {encoding: "utf8"},
"text/html": {encoding: "utf8"},
"application/javascript": {encoding: "utf8"},
"application/json": {encoding: "utf8"},
"application/pdf": {encoding: "base64"},
"image/jpeg": {encoding: "base64"},
"image/png": {encoding: "base64"},
"image/gif": {encoding: "base64"},
"image/svg+xml": {encoding: "utf8"}
"text/x-tiddlywiki": {encoding: "utf8", extension: ".tid"},
"application/x-tiddler": {encoding: "utf8", extension: ".tid"},
"application/x-tiddler-html-div": {encoding: "utf8", extension: ".tiddler"},
"application/x-tiddlywiki-recipe": {encoding: "utf8", extension: ".recipe"},
"text/plain": {encoding: "utf8", extension: ".txt"},
"text/css": {encoding: "utf8", extension: ".css"},
"text/html": {encoding: "utf8", extension: ".html"},
"application/javascript": {encoding: "utf8", extension: ".js"},
"application/json": {encoding: "utf8", extension: ".json"},
"application/pdf": {encoding: "base64", extension: ".pdf"},
"image/jpeg": {encoding: "base64", extension: ".jpg"},
"image/png": {encoding: "base64", extension: ".png"},
"image/gif": {encoding: "base64", extension: ".gif"},
"image/svg+xml": {encoding: "utf8", extension: ".svg"}
};
/////////////////////////// Utility functions

View File

@ -36,6 +36,12 @@ exports["text/html"] = function(tiddlers) {
});
};
exports["application/x-tiddler"] = function(tiddlers) {
return mapEachTiddler(this,tiddlers,function(tiddler) {
return tiddler.getFieldStringBlock({exclude: ["text"]}) + "\n\n" + tiddler.fields.text;
});
}
exports["application/x-tiddler-css"] = function(tiddlers) {
return mapEachTiddler(this,tiddlers,function(tiddler) {
var attributes = {type: "text/css"}; // The script type is set to text/javascript for compatibility with old browsers

View File

@ -40,6 +40,8 @@ exports.startup = function(loggedIn) {
if(hadChanges) {
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
console.log("Saved tiddler index");
// Sync any subsequent tiddler changes
$tw.plugins.dropbox.setupSyncer($tw.wiki);
});
}
});

View File

@ -3,7 +3,9 @@ title: $:/plugins/dropbox/dropbox.js
type: application/javascript
module-type: startup
Startup the Dropbox integration plugin
Main Dropbox integration plugin
\*/
(function(){
@ -20,7 +22,8 @@ var queryLoginMarker = "login=true";
$tw.plugins.dropbox = {
client: null, // Dropbox.js client object
fileInfo: {}, // Hashmap of each file as retrieved from Dropbox: {versionTag:,title:}
fileInfo: {}, // Hashmap of each filename as retrieved from Dropbox: {versionTag:,title:}
titleInfo: {}, // Hashmap of each tiddler title retrieved from Dropbox to filename
titleIsLoggedIn: "$:/plugins/dropbox/IsLoggedIn",
titleUserName: "$:/plugins/dropbox/UserName",
titlePublicAppUrl: "$:/plugins/dropbox/PublicAppUrl",
@ -225,6 +228,7 @@ console.log("loading tiddler from",path);
$tw.wiki.addTiddlers(tiddlers);
// Save the revision of the files so we can detect changes later
$tw.plugins.dropbox.fileInfo[mainStat.name] = {versionTag: mainStat.versionTag,title: tiddlers[0].title};
$tw.plugins.dropbox.titleInfo[tiddlers[0].title] = mainStat.name;
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag,title: tiddlers[0].title};
callback();
});
@ -233,6 +237,9 @@ console.log("loading tiddler from",path);
$tw.wiki.addTiddlers(tiddlers);
// Save the revision of this file so we can detect changes
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag,title: tiddlers[0].title};
for(t=0; t<tiddlers.length; t++) {
$tw.plugins.dropbox.titleInfo[tiddlers[t].title] = stat.name;
}
callback();
}
});
@ -350,6 +357,75 @@ $tw.plugins.dropbox.saveTiddlerIndex = function(path,callback) {
});
};
// Setup synchronisation back to Dropbox
$tw.plugins.dropbox.setupSyncer = function(wiki) {
wiki.addEventListener("",function(changes) {
$tw.plugins.dropbox.syncChanges(changes,wiki);
});
};
$tw.plugins.dropbox.syncChanges = function(changes,wiki) {
// Create a queue of tasks to save or delete tiddlers
var q = async.queue($tw.plugins.dropbox.syncTask,2);
// Called when we've processed all the files
q.drain = function () {
};
// Process each of the changes
for(var title in changes) {
var tiddler = wiki.getTiddler(title),
filename = $tw.plugins.dropbox.titleInfo[title],
contentType = tiddler ? tiddler.fields.type : null;
contentType = contentType || "text/x-tiddlywiki";
var contentTypeInfo = $tw.config.contentTypeInfo[contentType],
isNew = false;
// Figure out the pathname of the tiddler
if(!filename) {
var extension = contentTypeInfo ? contentTypeInfo.extension : "";
filename = encodeURIComponent(title) + extension;
$tw.plugins.dropbox.titleInfo[title] = filename;
isNew = true;
}
// Push the appropriate task
if(tiddler) {
if(contentType === "text/x-tiddlywiki") {
// .tid file
q.push({
type: "save",
title: title,
path: $tw.plugins.dropbox.titleInfo[title],
content: wiki.serializeTiddlers([tiddler],"application/x-tiddler"),
isNew: isNew
});
} else {
// main file plus meta file
q.push({
type: "save",
title: title,
path: $tw.plugins.dropbox.titleInfo[title],
content: tiddler.fields.text,
metadata: tiddler.getFieldStringBlock({exclude: ["text"]}),
isNew: isNew
});
}
} else {
q.push({
type: "delete",
title: title,
path: $tw.plugins.dropbox.titleInfo[title]
});
}
}
};
// Perform a single sync task
$tw.plugins.dropbox.syncTask = function(task,callback) {
if(task.type === "delete") {
console.log("Deleting",task.path);
} else if(task.type === "save") {
console.log("Saving",task.path,task);
}
};
exports.startup = function() {
if(!$tw.browser) {
return;