1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-30 20:10:28 +00:00
TiddlyWiki5/core/modules/commands/setfield.js
Jermolene d2796d0c9c Introduce setfield command
This new command will let us do the manipulations needed to set up
external image tiddlers
2014-06-11 10:05:35 +01:00

59 lines
1.5 KiB
JavaScript

/*\
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;
})();