Friday 10 August 2007

Create Mirror Copy of TiddlyWiki for Mobile Sync

In an attempt to resolve my synchronisation woes keeping a copy of my TiddlyWiki on my N95 I've added a new option to my TiddlyWiki file. This allows me to specify a path to create a mirror copy of the file. This points to my N95 synchonisation folder.

If I do work out how to enable editing on the phone I'll have to work out another approach but this does mean I can have a copy to read. Another benefit is that I can leave the backup copies accumulating in the folder or sub-folder and not have those copy across automatically as well.

I've created a new version of an empty TiddlyWiki and copied it to : ftp://ftp.esendex.com/adam/Tiddlywiki.AEB.v2.MirrorPath.html. The changes are summarised below.

Ln 571, added a new item to the config.options array:

txtMirrorPath: ""

Ln 791, added a new item to the config.optionsDesc array:

txtMirrorPath: "Path to mirror the file to"

Ln 831, added two new items to config.messages array:

mirrorSaved: "Mirror saved",
mirrorFailed: "Failed to save mirror file"

Ln 6152, added a new method saveMirror. Note this expects a complete path and not a relative folder as in the backup folder option.

function saveMirror(localPath,original)
{
 var mirrorPath = config.options.txtMirrorPath;
 var saveMirrorFile = mirrorPath != "";
 if (saveMirrorFile)
 {
 // add backslash if not present at end
     var backslashPos = mirrorPath.lastIndexOf("\\");
     var pathLastIndex = mirrorPath.length - 1;
     var backslashEnd = backslashPos == pathLastIndex;
     
     mirrorPath = mirrorPath + (backslashEnd ? "" : "\\");
     
 // copy to relevant locations
     var mirror = config.browser.isIE ? ieCopyFile(mirrorPath,localPath) : saveFile(mirrorPath,original);
     if(mirror)
      displayMessage(config.messages.mirrorSaved,"file://" + mirrorPath);
     else
      alert(config.messages.mirrorFailed);
 }
}

Ln 6133, added a call to the saveMirror method from the saveChanges method:

saveMirror(localPath,original);

Do let me know what you think.

2 comments:

-Xv said...

Adam, editing the source of a tiddlywiki is a bad idea. The customization that you need can easily be achieved writing your own plugin. Then, you'll be able to easily use it in your different tiddlywikis or share it with the world, and,most of all, you'll be able to upgrade the version of TW without loosing your changes.

Take a look at: http://www.tiddlywiki.org/wiki/Plugin_Development or to any plugin installed in your TW. You'll have to "hijack" the saveChanges function.

Adam said...

Thanks Xavier.

I remember reading something on one of the TiddlyWiki sites about editing code if the feature was generally applicable and a plug-in if it was more niche. Of course I can;t find it now.

I guess I was also getting into understanding the core and got carried away ;)

I'll produce it as a plug-in and re-post. It'll be good practice as I've got another feature I want to add and that should definitely be a plug-in.

Thanks again,

Adam