1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-03-14 07:28:11 +00:00

TiddlyFox now saves with TiddlyWiki5 as well as TiddlyWiki Classic

This commit is contained in:
Jeremy Ruston 2012-08-29 22:40:58 +01:00
parent ce1509f940
commit 68f7072c94
3 changed files with 95 additions and 12 deletions

View File

@ -0,0 +1,59 @@
/*\
title: $:/core/modules/savers/tiddlyfox.js
type: application/javascript
module-type: saver
Handles saving changes via the TiddlyFox file extension
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false, netscape: false, Components: false */
"use strict";
var TiddlyFoxSaver = function(wiki) {
};
TiddlyFoxSaver.prototype.save = function(text) {
var messageBox = document.getElementById("tiddlyfox-message-box");
console.log("messageBox",messageBox);
if(messageBox) {
// Create the message element and put it in the message box
var message = document.createElement("div");
message.setAttribute("tiddlyfox-path",document.location.pathname);
message.setAttribute("tiddlyfox-content",text);
messageBox.appendChild(message);
// Create and dispatch the custom event to the extension
var event = document.createEvent("Events");
event.initEvent("tiddlyfox-save-file",true,false);
message.dispatchEvent(event);
return true;
} else {
return false;
}
};
/*
Information about this saver
*/
TiddlyFoxSaver.prototype.info = {
name: "tiddlyfox",
priority: 1500
};
/*
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
return (window.location.protocol === "file:");
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new TiddlyFoxSaver(wiki);
};
})();

View File

@ -20,22 +20,21 @@ var TiddlyFox = {
// Get the document and window
var doc = event.originalTarget,
win = doc.defaultView;
// If it is a TiddlyWiki
if(TiddlyFox.isTiddlyWiki(doc,win)) {
// If it is a TiddlyWiki classic
if(TiddlyFox.isTiddlyWikiClassic(doc,win)) {
if(confirm("TiddlyFox: Enabling TiddlyWiki file saving capability")) {
TiddlyFox.injectPage(doc);
TiddlyFox.injectScript(doc);
TiddlyFox.injectMessageBox(doc);
}
// If it is a TiddlyWiki5
} else if(TiddlyFox.isTiddlyWiki5(doc,win)) {
if(confirm("TiddlyFox: Enabling TiddlyWiki5 file saving capability")) {
TiddlyFox.injectMessageBox(doc);
}
}
},
injectPage: function(doc) {
// Inject the message box
var messageBox = doc.createElement("div");
messageBox.id = "tiddlyfox-message-box";
messageBox.style.display = "none";
doc.body.appendChild(messageBox);
// Attach the event handler to the message box
messageBox.addEventListener("tiddlyfox-save-file",TiddlyFox.onSaveFile,false);
injectScript: function(doc) {
// Load the script text
var xhReq = new XMLHttpRequest();
xhReq.open("GET","chrome://tiddlyfox/content/inject.js",false);
@ -49,6 +48,19 @@ var TiddlyFox = {
doc.getElementsByTagName("head")[0].appendChild(scr)
},
injectMessageBox: function(doc) {
// Inject the message box
var messageBox = doc.getElementById("tiddlyfox-message-box");
if(!messageBox) {
messageBox = doc.createElement("div");
messageBox.id = "tiddlyfox-message-box";
messageBox.style.display = "none";
doc.body.appendChild(messageBox);
}
// Attach the event handler to the message box
messageBox.addEventListener("tiddlyfox-save-file",TiddlyFox.onSaveFile,false);
},
saveFile: function(filePath,content) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
@ -84,10 +96,22 @@ var TiddlyFox = {
window.open("chrome://tiddlyfox/content/hello.xul", "", "chrome");
},
isTiddlyWiki: function(doc,win) {
isTiddlyWikiClassic: function(doc,win) {
// Test whether the document is a TiddlyWiki (we don't have access to JS objects in it)
return (doc.location.protocol === "file:") &&
(doc.scripts[0].id === "versionArea");
},
isTiddlyWiki5: function(doc,win) {
// Test whether the document is a TiddlyWiki5 (we don't have access to JS objects in it)
var metaTags = doc.getElementsByTagName("meta"),
generator = false;
for(var t=0; t<metaTags.length; t++) {
if(metaTags[t].name === "application-name" && metaTags[t].content === "TiddlyWiki") {
generator = true;
}
}
return (doc.location.protocol === "file:") && generator;
}
};

Binary file not shown.