force webkit to output <br> when enter pressed, instead of div

this should make it easier for mobile clients to convert into newlines
This commit is contained in:
Damien Elmes 2013-05-17 13:22:58 +09:00
parent 8f36841281
commit e63c8e5619

View file

@ -74,6 +74,29 @@ function onKey() {
}
};
function onKeyPress() {
if (window.event.which == 13) {
if (window.getSelection) {
var selection = window.getSelection(),
range = selection.getRangeAt(0),
br = document.createElement("br");
range.deleteContents();
range.insertNode(br);
range.setStartAfter(br);
range.setEndAfter(br);
selection.removeAllRanges();
selection.addRange(range);
return false;
}
}
}
function onKeyUp(elem) {
if (!elem.lastChild || elem.lastChild.nodeName.toLowerCase() != "br") {
elem.appendChild(document.createElement("br"));
}
}
function sendState() {
var r = {
'bold': document.queryCommandState("bold"),
@ -196,7 +219,8 @@ function setFields(fields, focusTo) {
txt += "<tr><td class=fname>{0}</td></tr><tr><td width=100%%>".format(n);
txt += "<div id=f{0} onkeydown='onKey();' onmouseup='onKey();'".format(i);
txt += " onfocus='onFocus(this);' onblur='onBlur();' class=field ";
txt += "ondragover='onDragOver(this);' ";
txt += "ondragover='onDragOver(this);' onkeyup='onKeyUp(this)' ";
txt += "onkeypress='return onKeyPress();' ";
txt += "contentEditable=true class=field>{0}</div>".format(f);
txt += "</td></tr>";
}
@ -237,11 +261,9 @@ $(function () {
document.body.onmousedown = function () {
mouseDown++;
}
document.body.onmouseup = function () {
mouseDown--;
}
document.onclick = function (evt) {
var src = window.event.srcElement;
if (src.tagName == "IMG") {
@ -255,7 +277,6 @@ document.onclick = function (evt) {
}
}
}
});
</script></head><body>