Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Ubiquity Command to interact with a TiddlyWiki
/**
* Ubiquity Command to interact with a TiddlyWiki
*
* contact@garyhodgson.com
*/
var TiddlyUbiquity = {};
TiddlyUbiquity.SETUP_WARNING = "Warning: You must supply the URL of your TiddlyWiki. Please use tiddlywiki-setup.";
TiddlyUbiquity.NO_URL_MSG = "No TiddlyWiki URL found. Please use tiddlywiki-setup.";
CmdUtils.CreateCommand({
name: "tw-config",
description: "Sets the URL of your TiddlyWiki.",
author: {
name: "Gary Hodgson",
homepage: "http://www.garyhodgson.com/",
email: "contact@garyhodgson.com"
},
license: "MPL",
icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
help: "Setting the URL allows you use the search, create and open functions. If the TiddlyWiki is open in a tab you can select it from the suggestions list.",
takes: {"url": noun_arb_text},
modifiers: {"on": noun_type_tab},
preview: function( previewBlock, url, mods ) {
var subs = new Object();
var ptemplate = "Sets the TiddlyWiki URL to ${url}<br><br>Current value:<br><font size=2>${current}</font>";
subs.url = mods.on.summary || url.summary || '';
subs.current = Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
previewBlock.innerHTML =CmdUtils.renderTemplate(ptemplate, subs );
},
execute: function( url, mods) {
if ((!mods.on.text) && (!url.text || url.text.length < 1)) {
displayMessage("No URL given, or tab selected.");
return;
}
if (mods.on.text){
var tabName = mods.on.text;
var tabs = noun_type_tab.getTabs();
Application.prefs.setValue("TIDDLYUBIQUITY_URL", tabs[tabName].uri.spec);
} else {
Application.prefs.setValue("TIDDLYUBIQUITY_URL", url.text);
}
displayMessage("TiddlyWiki URL set to: " + Application.prefs.getValue("TIDDLYUBIQUITY_URL", "Error Setting URL."));
return;
}
});
CmdUtils.CreateCommand({
name: "tw-new",
description: "Creates a new Tiddler.",
author: {
name: "Gary Hodgson",
homepage: "http://www.garyhodgson.com/",
email: "contact@garyhodgson.com"
},
license: "MPL",
icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
help: "Allows you to create Tiddlers in your TiddlyWiki.",
takes: {"name": noun_arb_text},
preview: function( previewBlock, name, mods ) {
var name = name.text || 'NewTiddler';
var subs = {name: name};
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
msg = TiddlyUbiquity.SETUP_WARNING;
} else {
msg = "Creates a new Tiddler called ${name}."
}
previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
},
execute: function( name, mods) {
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
displayMessage(TiddlyUbiquity.NO_URL_MSG);
return;
}
var name = name.text || 'NewTiddler';
var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
var p='/#newTiddler:\"' + encodeURIComponent(name)+'\"';
TiddlyUbiquity.openTiddlyWiki(h,p);
}
});
CmdUtils.CreateCommand({
name: "tw-search",
description: "Performs a TiddlyWiki search.",
author: {
name: "Gary Hodgson",
homepage: "http://www.garyhodgson.com/",
email: "contact@garyhodgson.com"
},
license: "MPL",
icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
help: "Allows you to search for a Tiddler in your TiddlyWiki.",
takes: {"name": noun_arb_text},
preview: function( previewBlock, name, mods ) {
//TODO - add live search functionality
var name = name.text || '';
var subs = {name: name};
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
msg = TiddlyUbiquity.SETUP_WARNING;
} else {
msg = "Searches for Tiddlers containing : ${name}."
}
previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
},
execute: function( name, mods) {
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
displayMessage(TiddlyUbiquity.NO_URL_MSG);
return;
}
if (!name.text || name.text.length < 1) {
displayMessage("No name given.");
return;
}
var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
var p='/#search:\"' + encodeURIComponent(name.text)+'\"';
TiddlyUbiquity.openTiddlyWiki(h,p);
}
});
CmdUtils.CreateCommand({
name: "tw-open",
description: "Opens a TiddlyWiki.",
author: {
name: "Gary Hodgson",
homepage: "http://www.garyhodgson.com/",
email: "contact@garyhodgson.com"
},
license: "MPL",
icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
help: "Allows you to open a Tiddler in your TiddlyWiki.",
takes: {"name": noun_arb_text},
preview: function( previewBlock, name, mods ) {
var name = name.text || '';
var subs = {name: name};
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
msg = TiddlyUbiquity.SETUP_WARNING;
} else {
msg = "Opens a Tiddler matching ${name}."
}
previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
},
execute: function( name, mods) {
if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
displayMessage(TiddlyUbiquity.NO_URL_MSG);
return;
}
if (!name.text || name.text.length < 1) {
displayMessage("No name given.");
return;
}
var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
var p='/#open:\"' + encodeURIComponent(name.text)+'\"';
TiddlyUbiquity.openTiddlyWiki(h,p);
}
});
var noun_type_tab = {
_name: "tab name",
// Returns all tabs from all windows.
getTabs: function(){
var tabs = {};
for( var j=0; j < Application.windows.length; j++ ) {
var window = Application.windows[j];
for (var i = 0; i < window.tabs.length; i++) {
var tab = window.tabs[i];
tabs[tab.document.title] = tab;
}
}
return tabs;
},
suggest: function( text, html ) {
var suggestions = [];
var tabs = noun_type_tab.getTabs();
//TODO: implement a better match algorithm
for ( var tabName in tabs ) {
if (tabName.match(text, "i"))
suggestions.push( CmdUtils.makeSugg(tabName) );
}
// Return a list of input objects, limited to at most five:
return suggestions.splice(0, 5);
}
}
TiddlyUbiquity.openTiddlyWiki = function(host, path){
var tabs = noun_type_tab.getTabs();
for (var tabName in tabs){
if (tabs[tabName].uri.spec.match(host)){
var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
if (ios){
tabs[tabName].load(ios.newURI(host+path, null, null));
tabs[tabName]._window.focus();
tabs[tabName].focus();
if (tabs[tabName].uri.spec.length > host.length) {
// modified URL, so force page reload
tabs[tabName]._window.BrowserReload();
}
return;
}
}
}
Utils.openUrlInBrowser(host+path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment