From d2796d0c9c7ed7a971ae6b0752d7418384072bb5 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 11 Jun 2014 10:05:35 +0100 Subject: [PATCH] Introduce setfield command This new command will let us do the manipulations needed to set up external image tiddlers --- core/language/en-GB/Help/setfield.tid | 18 ++++++ core/modules/commands/setfield.js | 58 +++++++++++++++++++ core/templates/canonical-uri.tid | 10 ++++ .../tiddlers/commands/SetFieldCommand.tid | 6 ++ 4 files changed, 92 insertions(+) create mode 100644 core/language/en-GB/Help/setfield.tid create mode 100644 core/modules/commands/setfield.js create mode 100644 core/templates/canonical-uri.tid create mode 100644 editions/tw5.com/tiddlers/commands/SetFieldCommand.tid diff --git a/core/language/en-GB/Help/setfield.tid b/core/language/en-GB/Help/setfield.tid new file mode 100644 index 000000000..aaaaef9be --- /dev/null +++ b/core/language/en-GB/Help/setfield.tid @@ -0,0 +1,18 @@ +title: $:/language/Help/setfield +description: Prepares external tiddlers for use + +//Note that this command is experimental and may change or be replaced during the TiddlyWiki version 5 beta// + +Sets the specified field of a group of tiddlers to the result of wikifying a template tiddler with the `currentTiddler` variable set to the tiddler. + +``` +--setfield +``` + +The parameters are: + +* ''filter'' - filter identifying the tiddlers to be affected +* ''fieldname'' - the field to modify (defaults to "text") +* ''templatetitle'' - the tiddler to wikify into the specified field. If blank or missing then the specified field is deleted +* ''type'' - the text type to render (defaults to "text/plain"; "text/html" can be used to include HTML tags) + diff --git a/core/modules/commands/setfield.js b/core/modules/commands/setfield.js new file mode 100644 index 000000000..3f8ec1d14 --- /dev/null +++ b/core/modules/commands/setfield.js @@ -0,0 +1,58 @@ +/*\ +title: $:/core/modules/commands/setfield.js +type: application/javascript +module-type: command + +Command to modify selected tiddlers to set a field to the text of a template tiddler that has been wikified with the selected tiddler as the current tiddler. + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +var widget = require("$:/core/modules/widgets/widget.js"); + +exports.info = { + name: "setfield", + synchronous: true +}; + +var Command = function(params,commander,callback) { + this.params = params; + this.commander = commander; + this.callback = callback; +}; + +Command.prototype.execute = function() { + if(this.params.length < 4) { + return "Missing parameters"; + } + var self = this, + wiki = this.commander.wiki, + filter = this.params[0], + fieldname = this.params[1] || "text", + templatetitle = this.params[2], + rendertype = this.params[3] || "text/plain", + tiddlers = wiki.filterTiddlers(filter); + $tw.utils.each(tiddlers,function(title) { + var parser = wiki.parseTiddler(templatetitle), + newFields = {}, + tiddler = wiki.getTiddler(title); + if(parser) { + var widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}}); + var container = $tw.fakeDocument.createElement("div"); + widgetNode.render(container,null); + newFields[fieldname] = rendertype === "text/html" ? container.innerHTML : container.textContent; + } else { + newFields[fieldname] = undefined; + } + wiki.addTiddler(new $tw.Tiddler(tiddler,newFields)); + }); + return null; +}; + +exports.Command = Command; + +})(); diff --git a/core/templates/canonical-uri.tid b/core/templates/canonical-uri.tid new file mode 100644 index 000000000..32aed449f --- /dev/null +++ b/core/templates/canonical-uri.tid @@ -0,0 +1,10 @@ +title: $:/core/templates/canonical-uri + + +./images/<$view field="title" format="doubleurlencoded"/> \ No newline at end of file diff --git a/editions/tw5.com/tiddlers/commands/SetFieldCommand.tid b/editions/tw5.com/tiddlers/commands/SetFieldCommand.tid new file mode 100644 index 000000000..ae1ef61f6 --- /dev/null +++ b/editions/tw5.com/tiddlers/commands/SetFieldCommand.tid @@ -0,0 +1,6 @@ +title: SetFieldCommand +tags: command +created: 20140609121606089 +modified: 20140609121606089 + +{{$:/language/Help/setfield}}