1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-11 18:00:26 +00:00

Update edit macro to be able to edit a specified tiddler

Previously you could only edit fields on the current tiddler
This commit is contained in:
Jeremy Ruston 2012-07-13 11:26:34 +01:00
parent a7c1b1896e
commit 6e86de5652
3 changed files with 38 additions and 17 deletions

View File

@ -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) {

View File

@ -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:<type>;base64,<text>"
var dataURL = this.macroNode.child.domNode.toDataURL(tiddler.fields.type,1.0),

View File

@ -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));
}
};