ace/lib/ace/TextInput.js
2010-10-27 13:11:21 +02:00

118 lines
No EOL
3 KiB
JavaScript

/**
* Ajax.org Code Editor (ACE)
*
* @copyright 2010, Ajax.org Services B.V.
* @license LGPLv3 <http://www.gnu.org/licenses/lgpl-3.0.txt>
* @author Fabian Jakobs <fabian AT ajax DOT org>
*/
require.def("ace/TextInput", ["ace/lib/event"], function(event) {
var TextInput = function(parentNode, host) {
var text = document.createElement("textarea");
var style = text.style;
style.position = "absolute";
style.left = "-10000px";
style.top = "-10000px";
parentNode.appendChild(text);
var PLACEHOLDER = String.fromCharCode(0);
sendText();
var inCompostion = false;
var copied = false;
function sendText() {
if (!copied) {
var value = text.value;
if (value) {
if (value.charCodeAt(value.length-1) == PLACEHOLDER.charCodeAt(0)) {
value = value.slice(0, -1);
if (value)
host.onTextInput(value);
} else
host.onTextInput(value);
}
}
copied = false;
// Safari doesn't fire copy events if no text is selected
text.value = PLACEHOLDER;
text.select();
}
var onTextInput = function(e) {
setTimeout(function() {
if (!inCompostion)
sendText();
}, 0);
};
var onCompositionStart = function(e) {
inCompostion = true;
sendText();
text.value = "";
host.onCompositionStart();
setTimeout(onCompositionUpdate, 0);
};
var onCompositionUpdate = function() {
host.onCompositionUpdate(text.value);
};
var onCompositionEnd = function() {
inCompostion = false;
host.onCompositionEnd();
onTextInput();
};
var onCopy = function() {
copied = true;
text.value = host.getCopyText();
text.select();
copied = true;
setTimeout(sendText, 0);
};
var onCut = function() {
copied = true;
text.value = host.getCopyText();
host.onCut();
text.select();
setTimeout(sendText, 0);
};
event.addListener(text, "keypress", onTextInput);
event.addListener(text, "textInput", onTextInput);
event.addListener(text, "paste", onTextInput);
event.addListener(text, "propertychange", onTextInput);
event.addListener(text, "copy", onCopy);
event.addListener(text, "cut", onCut);
event.addListener(text, "compositionstart", onCompositionStart);
event.addListener(text, "compositionupdate", onCompositionUpdate);
event.addListener(text, "compositionend", onCompositionEnd);
event.addListener(text, "blur", function() {
host.onBlur();
});
event.addListener(text, "focus", function() {
host.onFocus();
text.select();
});
this.focus = function() {
host.onFocus();
text.select();
text.focus();
};
this.blur = function() {
text.blur();
};
};
return TextInput;
});