From 6e86de5652b7bbd8c98de51c0606ffbb441bb1c4 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Fri, 13 Jul 2012 11:26:34 +0100 Subject: [PATCH] Update edit macro to be able to edit a specified tiddler Previously you could only edit fields on the current tiddler --- core/modules/macros/edit/edit.js | 29 +++++++++++++++---- .../macros/edit/editors/bitmapeditor.js | 4 +-- .../modules/macros/edit/editors/texteditor.js | 22 +++++++------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/core/modules/macros/edit/edit.js b/core/modules/macros/edit/edit.js index 27671cd02..3a2f71397 100644 --- a/core/modules/macros/edit/edit.js +++ b/core/modules/macros/edit/edit.js @@ -14,20 +14,39 @@ Edit macro for editting fields and tiddlers exports.info = { name: "edit", - dependentOnContextTiddler: true, params: { - field: {byPos: 0, type: "text"} + field: {byPos: 0, type: "text"}, + tiddler: {byName: true, type: "tiddler"} } }; +exports.evaluateDependencies = function() { + var dependencies = new $tw.Dependencies(); + if(!this.srcParams.tiddler) { + dependencies.dependentOnContextTiddler = true; + } + for(var m in this.info.params) { + var paramInfo = this.info.params[m]; + if(m in this.srcParams && paramInfo.type === "tiddler") { + if(typeof this.srcParams[m] === "function") { + dependencies.dependentAll = true; + } else { + dependencies.addDependency(this.srcParams[m],!paramInfo.skinny); + } + } + } + return dependencies; +}; + exports.executeMacro = function() { // Get the tiddler being editted - var field = this.hasParameter("field") ? this.params.field : "text", - tiddler = this.wiki.getTiddler(this.tiddlerTitle), + this.editField = this.hasParameter("field") ? this.params.field : "text"; + this.editTiddler = this.hasParameter("tiddler") ? this.params.tiddler : this.tiddlerTitle; + var tiddler = this.wiki.getTiddler(this.editTiddler), Editor; // Figure out which editor to use // TODO: Tiddler field plugins should be able to specify a field type from which the editor is derived - if(field === "text" && tiddler.fields.type) { + if(this.editField === "text" && tiddler && tiddler.fields.type) { Editor = this.wiki.macros.edit.editors[tiddler.fields.type]; } if(!Editor) { diff --git a/core/modules/macros/edit/editors/bitmapeditor.js b/core/modules/macros/edit/editors/bitmapeditor.js index e00fbaf6e..79a8e2490 100644 --- a/core/modules/macros/edit/editors/bitmapeditor.js +++ b/core/modules/macros/edit/editors/bitmapeditor.js @@ -26,7 +26,7 @@ BitmapEditor.prototype.getChild = function() { }; BitmapEditor.prototype.postRenderInDom = function() { - var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle), + var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.editTiddler), canvas = this.macroNode.child.domNode, currImage = new Image(); ///////////////////// // Set the macro node itself to be position: relative @@ -142,7 +142,7 @@ BitmapEditor.prototype.strokeEnd = function() { }; BitmapEditor.prototype.saveChanges = function() { - var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle); + var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.editTiddler); if(tiddler) { // data URIs look like "data:;base64," var dataURL = this.macroNode.child.domNode.toDataURL(tiddler.fields.type,1.0), diff --git a/core/modules/macros/edit/editors/texteditor.js b/core/modules/macros/edit/editors/texteditor.js index 118139b6d..3ff5ba242 100644 --- a/core/modules/macros/edit/editors/texteditor.js +++ b/core/modules/macros/edit/editors/texteditor.js @@ -23,27 +23,26 @@ Get the tiddler being editted, field name and current value */ TextEditor.prototype.getEditText = function() { // Get the current tiddler and the field name - var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle), - field = this.macroNode.hasParameter("field") ? this.macroNode.params.field : "title", + var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.editTiddler), value; // If we've got a tiddler, the value to display is the field string value if(tiddler) { - value = tiddler.getFieldString(field); + value = tiddler.getFieldString(this.macroNode.editField); } else { // Otherwise, we need to construct a default value for the editor - switch(field) { + switch(this.macroNode.editField) { case "text": - value = "Type the text for the tiddler '" + this.macroNode.tiddlerTitle + "'"; + value = "Type the text for the tiddler '" + this.macroNode.editTiddler + "'"; break; case "title": - value = this.macroNode.tiddlerTitle; + value = this.macroNode.editTiddler; break; default: value = ""; break; } } - return {tiddler: tiddler, field: field, value: value}; + return {tiddler: tiddler, field: this.macroNode.editField, value: value}; }; TextEditor.prototype.getChild = function() { @@ -84,10 +83,13 @@ TextEditor.prototype.handleEvent = function(event) { TextEditor.prototype.saveChanges = function() { var text = this.macroNode.child.children[0].domNode.value, - tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle); - if(tiddler && text !== tiddler.fields[this.macroNode.params.field]) { + tiddler = this.macroNode.wiki.getTiddler(this.macroNode.editTiddler); + if(!tiddler) { + tiddler = new $tw.Tiddler({title: this.macroNode.editTiddler}); + } + if(text !== tiddler.fields[this.macroNode.editField]) { var update = {}; - update[this.macroNode.params.field] = text; + update[this.macroNode.editField] = text; this.macroNode.wiki.addTiddler(new $tw.Tiddler(tiddler,update)); } };