1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-12 23:14:21 +00:00
TiddlyWiki5/core/modules/widgets/edit/edit.js
Jeremy Ruston 8564602256 Refactor rendertree to simplify context handling
Get rid of the separate renderContext stack and instead have a parent
pointer on renderer nodes. This lets us walk back up the render tree to
resolve context references
2013-05-15 17:32:17 +01:00

79 lines
2.4 KiB
JavaScript

/*\
title: $:/core/modules/widgets/edit/edit.js
type: application/javascript
module-type: widget
The edit widget uses editor plugins to edit tiddlers of different types.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var EditWidget = function(renderer) {
// Save state
this.renderer = renderer;
// Initialise the editors if they've not been done already
if(!this.editors) {
EditWidget.prototype.editors = {};
$tw.modules.applyMethods("editor",this.editors);
}
// Generate child nodes
this.generate();
};
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");
// Choose the editor to use
// TODO: Tiddler field modules should be able to specify a field type from which the editor is derived
var tiddler = this.renderer.renderTree.wiki.getTiddler(this.tiddlerTitle),
Editor;
if(this.fieldName === "text" && tiddler && tiddler.fields.type) {
Editor = this.editors[tiddler.fields.type];
}
if(!Editor) {
Editor = this.editors["text/vnd.tiddlywiki"];
}
// Instantiate the editor
this.editor = new Editor(this,this.tiddlerTitle,this.fieldName);
// Ask the editor to create the widget element
this.editor.render();
};
EditWidget.prototype.postRenderInDom = function() {
if(this.editor && this.editor.postRenderInDom) {
this.editor.postRenderInDom();
}
};
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) {
// Regenerate and rerender the widget and replace the existing DOM node
this.generate();
var oldDomNode = this.renderer.domNode,
newDomNode = this.renderer.renderInDom();
oldDomNode.parentNode.replaceChild(newDomNode,oldDomNode);
} else if(this.tiddlerTitle && changedTiddlers[this.tiddlerTitle]) {
// Refresh the editor if our tiddler has changed
if(this.editor && this.editor.refreshInDom) {
this.editor.refreshInDom(changedTiddlers);
}
} else {
// Otherwise, just refresh any child nodes
$tw.utils.each(this.children,function(node) {
if(node.refreshInDom) {
node.refreshInDom(changedTiddlers);
}
});
}
};
exports.edit = EditWidget;
})();