mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-06-25 22:52:49 +00:00
On startup selectively just load the tiddler files that have changed
This commit is contained in:
parent
c28d304205
commit
060729e66e
@ -31,13 +31,16 @@ exports.startup = function() {
|
|||||||
if(index) {
|
if(index) {
|
||||||
$tw.wiki.addTiddlers(index.tiddlers);
|
$tw.wiki.addTiddlers(index.tiddlers);
|
||||||
$tw.wiki.addTiddlers(index.shadows,true);
|
$tw.wiki.addTiddlers(index.shadows,true);
|
||||||
|
$tw.plugins.dropbox.fileRevisions = index.fileRevisions;
|
||||||
}
|
}
|
||||||
// Check for later versions of files on Dropbox
|
// Check for later versions of files on Dropbox
|
||||||
$tw.plugins.dropbox.loadTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(fileRevisions) {
|
$tw.plugins.dropbox.loadTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(hadChanges) {
|
||||||
// Save the tiddler index
|
// Save the tiddler index if we had changes
|
||||||
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",fileRevisions,function(error) {
|
if(hadChanges) {
|
||||||
console.log("Saved tiddler index");
|
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
|
||||||
});
|
console.log("Saved tiddler index");
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
alert("This TiddlyWiki file must be in Dropbox");
|
alert("This TiddlyWiki file must be in Dropbox");
|
||||||
|
@ -20,6 +20,7 @@ var queryLoginMarker = "login=true";
|
|||||||
|
|
||||||
$tw.plugins.dropbox = {
|
$tw.plugins.dropbox = {
|
||||||
client: null, // Dropbox.js client object
|
client: null, // Dropbox.js client object
|
||||||
|
fileRevisions: {}, // Hashmap of revision strings for each tiddler file that has been retrieved from Dropbox
|
||||||
titleIsLoggedIn: "$:/plugins/dropbox/IsLoggedIn",
|
titleIsLoggedIn: "$:/plugins/dropbox/IsLoggedIn",
|
||||||
titleUserName: "$:/plugins/dropbox/UserName",
|
titleUserName: "$:/plugins/dropbox/UserName",
|
||||||
titlePublicAppUrl: "$:/plugins/dropbox/PublicAppUrl",
|
titlePublicAppUrl: "$:/plugins/dropbox/PublicAppUrl",
|
||||||
@ -114,26 +115,40 @@ $tw.plugins.dropbox.loadTiddlerFiles = function(path,callback) {
|
|||||||
$tw.plugins.dropbox.loadTiddlerFile(task.path,task.type,task.stats,callback);
|
$tw.plugins.dropbox.loadTiddlerFile(task.path,task.type,task.stats,callback);
|
||||||
}, 2);
|
}, 2);
|
||||||
// Call the callback when we've processed all the files
|
// Call the callback when we've processed all the files
|
||||||
q.drain = function() {
|
q.drain = function () {
|
||||||
var fileRevisions = {};
|
callback(true); // Indicate that there were changes
|
||||||
for(var t=0; t<stats.length; t++) {
|
|
||||||
fileRevisions[stats[t].name] = stats[t].versionTag;
|
|
||||||
}
|
|
||||||
callback(fileRevisions);
|
|
||||||
};
|
};
|
||||||
// Push a task onto the queue for each file to be processed
|
// Push a task onto the queue for each file to be processed
|
||||||
for(var s=0; s<stats.length; s++) {
|
for(var s=0; s<stats.length; s++) {
|
||||||
var stat = stats[s],
|
var stat = stats[s],
|
||||||
isMetaFile = stat.path.lastIndexOf(".meta") === stat.path.length - 5;
|
isMetaFile = stat.path.lastIndexOf(".meta") === stat.path.length - 5;
|
||||||
if(stat.isFile && !stat.isFolder && !isMetaFile) {
|
if(stat.isFile && !stat.isFolder && !isMetaFile) {
|
||||||
q.push({path: stat.path, type: stat.mimeType, stats: stats});
|
// Don't load the file if the version tag shows it hasn't changed
|
||||||
|
var hasChanged = stat.versionTag !== $tw.plugins.dropbox.fileRevisions[stat.name];
|
||||||
|
if(!hasChanged) {
|
||||||
|
// Check if there is a metafile and whether it has changed
|
||||||
|
var metafileName = stat.name + ".meta";
|
||||||
|
for(var p=0; p<stats.length; p++) {
|
||||||
|
if(stats[p].name === metafileName) {
|
||||||
|
hasChanged = stats[p].versionTag !== $tw.plugins.dropbox.fileRevisions[metafileName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(hasChanged) {
|
||||||
|
q.push({path: stat.path, type: stat.mimeType, stats: stats});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// If we didn't queue anything for loading we'll have to manually trigger our callback
|
||||||
|
if(q.length() === 0) {
|
||||||
|
callback(false); // And tell it that there weren't any changes
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load a tiddler file
|
// Load a tiddler file
|
||||||
$tw.plugins.dropbox.loadTiddlerFile = function(path,mimeType,stats,callback) {
|
$tw.plugins.dropbox.loadTiddlerFile = function(path,mimeType,stats,callback) {
|
||||||
|
console.log("loading tiddler from",path);
|
||||||
// If the mime type is "application/octet-stream" then we'll take the type from the extension
|
// If the mime type is "application/octet-stream" then we'll take the type from the extension
|
||||||
var isBinary = false,
|
var isBinary = false,
|
||||||
p = path.lastIndexOf(".");
|
p = path.lastIndexOf(".");
|
||||||
@ -146,7 +161,7 @@ $tw.plugins.dropbox.loadTiddlerFile = function(path,mimeType,stats,callback) {
|
|||||||
if($tw.utils.hop($tw.config.contentTypeInfo,mimeType)) {
|
if($tw.utils.hop($tw.config.contentTypeInfo,mimeType)) {
|
||||||
isBinary = $tw.config.contentTypeInfo[mimeType].encoding === "base64";
|
isBinary = $tw.config.contentTypeInfo[mimeType].encoding === "base64";
|
||||||
}
|
}
|
||||||
var xhr = $tw.plugins.dropbox.client.readFile(path,{binary: isBinary},function(error,data) {
|
var xhr = $tw.plugins.dropbox.client.readFile(path,{binary: isBinary},function(error,data,stat) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
return $tw.plugins.dropbox.showError(error);
|
return $tw.plugins.dropbox.showError(error);
|
||||||
@ -168,6 +183,8 @@ $tw.plugins.dropbox.loadTiddlerFile = function(path,mimeType,stats,callback) {
|
|||||||
} else {
|
} else {
|
||||||
tiddlers = $tw.wiki.deserializeTiddlers(mimeType,data,{title: defaultTitle});
|
tiddlers = $tw.wiki.deserializeTiddlers(mimeType,data,{title: defaultTitle});
|
||||||
}
|
}
|
||||||
|
// Save the revision of this file so we can detect changes
|
||||||
|
$tw.plugins.dropbox.fileRevisions[stat.name] = stat.versionTag;
|
||||||
// Check to see if there's a metafile
|
// Check to see if there's a metafile
|
||||||
var metafilePath = path + ".meta",
|
var metafilePath = path + ".meta",
|
||||||
metafileIndex = null;
|
metafileIndex = null;
|
||||||
@ -178,16 +195,20 @@ $tw.plugins.dropbox.loadTiddlerFile = function(path,mimeType,stats,callback) {
|
|||||||
}
|
}
|
||||||
// Process the metafile if it's there
|
// Process the metafile if it's there
|
||||||
if(tiddlers.length === 1 && metafileIndex !== null) {
|
if(tiddlers.length === 1 && metafileIndex !== null) {
|
||||||
$tw.plugins.dropbox.client.readFile(metafilePath,function(error,data) {
|
$tw.plugins.dropbox.client.readFile(metafilePath,function(error,data,stat) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
return $tw.plugins.dropbox.showError(error);
|
return $tw.plugins.dropbox.showError(error);
|
||||||
}
|
}
|
||||||
|
// Save the revision of the metafile so we can detect changes later
|
||||||
|
$tw.plugins.dropbox.fileRevisions[stat.name] = stat.versionTag;
|
||||||
|
// Extract the metadata and add the tiddlers
|
||||||
tiddlers = [$tw.utils.parseFields(data,tiddlers[0])];
|
tiddlers = [$tw.utils.parseFields(data,tiddlers[0])];
|
||||||
$tw.wiki.addTiddlers(tiddlers);
|
$tw.wiki.addTiddlers(tiddlers);
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
// Add the tiddlers
|
||||||
$tw.wiki.addTiddlers(tiddlers);
|
$tw.wiki.addTiddlers(tiddlers);
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
@ -285,9 +306,9 @@ $tw.plugins.dropbox.createWiki = function(wikiName) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Save the index file
|
// Save the index file
|
||||||
$tw.plugins.dropbox.saveTiddlerIndex = function(path,fileRevisions,callback) {
|
$tw.plugins.dropbox.saveTiddlerIndex = function(path,callback) {
|
||||||
// Get the tiddler index information
|
// Get the tiddler index information
|
||||||
var index = {tiddlers: [],shadows: [], fileRevisions: {}}
|
var index = {tiddlers: [],shadows: [], fileRevisions: $tw.plugins.dropbox.fileRevisions};
|
||||||
// First all the tiddlers
|
// First all the tiddlers
|
||||||
$tw.wiki.forEachTiddler(function(title,tiddler) {
|
$tw.wiki.forEachTiddler(function(title,tiddler) {
|
||||||
if(tiddler.isShadow) {
|
if(tiddler.isShadow) {
|
||||||
@ -296,10 +317,8 @@ $tw.plugins.dropbox.saveTiddlerIndex = function(path,fileRevisions,callback) {
|
|||||||
index.tiddlers.push(tiddler.fields);
|
index.tiddlers.push(tiddler.fields);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Then all the revision information
|
|
||||||
index.fileRevisions = fileRevisions;
|
|
||||||
// Save everything to a tiddler
|
// Save everything to a tiddler
|
||||||
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleTiddlerIndex, type: "application/json", text: JSON.stringify(index)});
|
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleTiddlerIndex, type: "application/json", text: JSON.stringify(index)},true);
|
||||||
// Generate the index file
|
// Generate the index file
|
||||||
var file = $tw.wiki.renderTiddler("text/plain",$tw.plugins.dropbox.titleAppIndexTemplate);
|
var file = $tw.wiki.renderTiddler("text/plain",$tw.plugins.dropbox.titleAppIndexTemplate);
|
||||||
// Save the index to Dropbox
|
// Save the index to Dropbox
|
||||||
|
Loading…
x
Reference in New Issue
Block a user