2012-04-30 11:23:03 +00:00
|
|
|
/*\
|
2012-05-03 20:47:16 +00:00
|
|
|
title: $:/core/modules/macros/edit/editors/texteditor.js
|
2012-04-30 11:23:03 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: editor
|
|
|
|
|
2012-05-19 17:23:14 +00:00
|
|
|
An editor plugin for editting text
|
|
|
|
|
2012-04-30 11:23:03 +00:00
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
2012-05-04 17:49:04 +00:00
|
|
|
/*global $tw: false */
|
2012-04-30 11:23:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function TextEditor(macroNode) {
|
|
|
|
this.macroNode = macroNode;
|
|
|
|
}
|
|
|
|
|
2012-06-09 17:36:32 +00:00
|
|
|
TextEditor.prototype.getChild = function() {
|
2012-04-30 11:23:03 +00:00
|
|
|
var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle),
|
|
|
|
field = this.macroNode.hasParameter("field") ? this.macroNode.params.field : "title",
|
|
|
|
value;
|
|
|
|
if(tiddler) {
|
|
|
|
value = tiddler.getFieldString(field);
|
|
|
|
} else {
|
|
|
|
switch(field) {
|
|
|
|
case "text":
|
|
|
|
value = "Type the text for the tiddler '" + this.macroNode.tiddlerTitle + "'";
|
|
|
|
break;
|
|
|
|
case "title":
|
|
|
|
value = this.macroNode.tiddlerTitle;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var attributes = {
|
|
|
|
"class": ["tw-edit-field"]
|
|
|
|
},
|
|
|
|
tagName,
|
|
|
|
content = [];
|
|
|
|
if(field === "text") {
|
|
|
|
tagName = "textarea";
|
|
|
|
content.push($tw.Tree.Text(value));
|
|
|
|
} else {
|
|
|
|
tagName = "input";
|
|
|
|
attributes.type = "text";
|
|
|
|
attributes.value = value;
|
|
|
|
}
|
2012-06-09 17:36:32 +00:00
|
|
|
return $tw.Tree.Element("div",{},[$tw.Tree.Element(tagName,attributes,content)]);
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.addEventHandlers = function() {
|
2012-06-09 17:36:32 +00:00
|
|
|
this.macroNode.child.domNode.addEventListener("focus",this,false);
|
|
|
|
this.macroNode.child.domNode.addEventListener("keyup",this,false);
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.handleEvent = function(event) {
|
|
|
|
// Get the value of the field if it might have changed
|
|
|
|
if("keyup".split(" ").indexOf(event.type) !== -1) {
|
|
|
|
this.saveChanges();
|
|
|
|
}
|
|
|
|
// Whatever the event, fix the height of the textarea if required
|
|
|
|
var self = this;
|
|
|
|
window.setTimeout(function() {
|
|
|
|
self.fixHeight();
|
|
|
|
},5);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.saveChanges = function() {
|
2012-06-09 17:36:32 +00:00
|
|
|
var text = this.macroNode.child.children[0].domNode.value,
|
2012-04-30 11:23:03 +00:00
|
|
|
tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle);
|
|
|
|
if(tiddler && text !== tiddler.fields[this.macroNode.params.field]) {
|
|
|
|
var update = {};
|
|
|
|
update[this.macroNode.params.field] = text;
|
|
|
|
this.macroNode.wiki.addTiddler(new $tw.Tiddler(tiddler,update));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.fixHeight = function() {
|
2012-06-09 17:36:32 +00:00
|
|
|
if(this.macroNode.child && this.macroNode.child.domNode) {
|
|
|
|
var wrapper = this.macroNode.child.domNode,
|
|
|
|
textarea = this.macroNode.child.children[0].domNode;
|
2012-04-30 11:23:03 +00:00
|
|
|
// Set the text area height to 1px temporarily, which allows us to read the true scrollHeight
|
|
|
|
var prevWrapperHeight = wrapper.style.height;
|
|
|
|
wrapper.style.height = textarea.style.height + "px";
|
|
|
|
textarea.style.overflow = "hidden";
|
|
|
|
textarea.style.height = "1px";
|
|
|
|
textarea.style.height = textarea.scrollHeight + "px";
|
|
|
|
wrapper.style.height = prevWrapperHeight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.postRenderInDom = function() {
|
|
|
|
this.fixHeight();
|
|
|
|
};
|
|
|
|
|
|
|
|
TextEditor.prototype.isRefreshable = function() {
|
|
|
|
// Don't refresh the editor if it contains the caret or selection
|
2012-06-09 17:36:32 +00:00
|
|
|
return document.activeElement !== this.macroNode.child.children[0].domNode;
|
2012-04-30 11:23:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports["text/x-tiddlywiki"] = TextEditor;
|
|
|
|
exports["text/plain"] = TextEditor;
|
|
|
|
|
|
|
|
})();
|