1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-03 02:33:15 +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() { EditWidget.prototype.generate = function() {
// Get parameters from our attributes // Get parameters from our attributes
this.tiddlerTitle = this.renderer.getAttribute("tiddler",this.renderer.tiddlerTitle); 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 // Choose the editor to use
// TODO: Tiddler field modules should be able to specify a field type from which the editor is derived // TODO: Tiddler field modules should be able to specify a field type from which the editor is derived
this.editorName = this.chooseEditor(); this.editorName = this.chooseEditor();
var Editor = this.editors[this.editorName]; var Editor = this.editors[this.editorName];
// Instantiate the editor // 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 // Ask the editor to create the widget element
this.editor.render(); this.editor.render();
}; };
@ -57,7 +61,7 @@ EditWidget.prototype.postRenderInDom = function() {
EditWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) { EditWidget.prototype.refreshInDom = function(changedAttributes,changedTiddlers) {
// We'll completely regenerate ourselves if any of our attributes have changed // 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 // Regenerate and rerender the widget and replace the existing DOM node
this.generate(); this.generate();
var oldDomNode = this.renderer.domNode, var oldDomNode = this.renderer.domNode,

View File

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

View File

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