1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-28 11:00:27 +00:00

Extend the edit widget to be able to edit properties of data tiddlers

This commit is contained in:
Jeremy Ruston 2013-08-28 11:23:14 +01:00
parent 861483f75d
commit 954a987d31
3 changed files with 49 additions and 31 deletions

View File

@ -27,13 +27,17 @@ var EditWidget = function(renderer) {
EditWidget.prototype.generate = function() {
// Get parameters from our attributes
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.tiddlerTitle);
this.fieldName = this.renderer.getAttribute("field","text");
this.fieldName = this.renderer.getAttribute("field");
this.indexName = this.renderer.getAttribute("index");
if(!this.fieldName && !this.indexName) {
this.fieldName = "text";
}
// Choose the editor to use
// TODO: Tiddler field modules should be able to specify a field type from which the editor is derived
this.editorName = this.chooseEditor();
var Editor = this.editors[this.editorName];
// Instantiate the editor
this.editor = new Editor(this,this.tiddlerTitle,this.fieldName);
this.editor = new Editor(this,this.tiddlerTitle,this.fieldName,this.indexName);
// Ask the editor to create the widget element
this.editor.render();
};
@ -57,7 +61,7 @@ EditWidget.prototype.postRenderInDom = function() {
EditWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// We'll completely regenerate ourselves if any of our attributes have changed
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.format || this.chooseEditor() !== this.editorName) {
if(changedAttributes.tiddler || changedAttributes.field || changedAttributes.index || changedAttributes.format || this.chooseEditor() !== this.editorName) {
// Regenerate and rerender the widget and replace the existing DOM node
this.generate();
var oldDomNode = this.renderer.domNode,

View File

@ -21,7 +21,7 @@ var DOM_CANVAS = 0,
DOM_WIDTH = 1,
DOM_HEIGHT = 2;
var BitmapEditor = function(editWidget,tiddlerTitle,fieldName) {
var BitmapEditor = function(editWidget,tiddlerTitle,fieldName,indexName) {
this.editWidget = editWidget;
this.tiddlerTitle = tiddlerTitle;
this.fieldName = fieldName;

View File

@ -14,36 +14,41 @@ A plain text editor
var MIN_TEXT_AREA_HEIGHT = 100;
var TextEditor = function(editWidget,tiddlerTitle,fieldName) {
var TextEditor = function(editWidget,tiddlerTitle,fieldName,indexName) {
this.editWidget = editWidget;
this.tiddlerTitle = tiddlerTitle;
this.fieldName = fieldName;
this.indexName = indexName;
};
/*
Get the tiddler being edited and current value
*/
TextEditor.prototype.getEditInfo = function() {
// Get the current tiddler and the field name
var tiddler = this.editWidget.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle),
value;
// If we've got a tiddler, the value to display is the field string value
if(tiddler) {
value = tiddler.getFieldString(this.fieldName);
} else {
// Otherwise, we need to construct a default value for the editor
switch(this.fieldName) {
case "text":
value = "Type the text for the tiddler '" + this.tiddlerTitle + "'";
break;
case "title":
value = this.tiddlerTitle;
break;
default:
value = "";
break;
if(this.fieldName) {
// Get the current tiddler and the field name
if(tiddler) {
// If we've got a tiddler, the value to display is the field string value
value = tiddler.getFieldString(this.fieldName);
} else {
// Otherwise, we need to construct a default value for the editor
switch(this.fieldName) {
case "text":
value = "Type the text for the tiddler '" + this.tiddlerTitle + "'";
break;
case "title":
value = this.tiddlerTitle;
break;
default:
value = "";
break;
}
value = this.editWidget.renderer.getAttribute("default",value);
}
value = this.editWidget.renderer.getAttribute("default",value);
} else {
value = this.editWidget.renderer.renderTree.wiki.extractTiddlerDataItem(this.tiddlerTitle,this.indexName,this.editWidget.renderer.getAttribute("default"));
}
return {tiddler: tiddler, value: value};
};
@ -149,15 +154,24 @@ TextEditor.prototype.handleInputEvent = function(event) {
};
TextEditor.prototype.saveChanges = function() {
var text = this.editWidget.children[0].domNode.value,
tiddler = this.editWidget.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle);
if(!tiddler) {
tiddler = new $tw.Tiddler({title: this.tiddlerTitle});
}
if(text !== tiddler.fields[this.fieldName]) {
var update = {};
update[this.fieldName] = text;
this.editWidget.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,update));
var text = this.editWidget.children[0].domNode.value
if(this.fieldName) {
var tiddler = this.editWidget.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle);
if(!tiddler) {
tiddler = new $tw.Tiddler({title: this.tiddlerTitle});
}
var newValue = tiddler.getFieldString(this.fieldName);
if(text !== newValue) {
var update = {};
update[this.fieldName] = newValue;
this.editWidget.renderer.renderTree.wiki.addTiddler(new $tw.Tiddler(tiddler,update));
}
} else {
var data = this.editWidget.renderer.renderTree.wiki.getTiddlerData(this.tiddlerTitle,{});
if(data[this.indexName] !== text) {
data[this.indexName] = text;
this.editWidget.renderer.renderTree.wiki.setTiddlerData(this.tiddlerTitle,data);
}
}
};