Merge pull request #925 from hgiesel/preferbr

Prefer <br> over <div></div> in Editor
This commit is contained in:
Damien Elmes 2021-01-16 13:03:28 +10:00 committed by GitHub
commit a674712916
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -55,6 +55,13 @@ function onKey(evt: KeyboardEvent) {
return; return;
} }
// prefer <br> instead of <div></div>
if (evt.which === 13 && !inListItem()) {
evt.preventDefault();
document.execCommand("insertLineBreak");
return;
}
// fix Ctrl+right/left handling in RTL fields // fix Ctrl+right/left handling in RTL fields
if (currentField.dir === "rtl") { if (currentField.dir === "rtl") {
const selection = window.getSelection(); const selection = window.getSelection();
@ -80,6 +87,42 @@ function onKey(evt: KeyboardEvent) {
triggerKeyTimer(); triggerKeyTimer();
} }
function onKeyUp(evt: KeyboardEvent) {
// Avoid div element on remove
if (evt.which === 8 || evt.which === 13) {
const anchor = window.getSelection().anchorNode;
if (
nodeIsElement(anchor) &&
anchor.tagName === "DIV" &&
!anchor.classList.contains("field") &&
anchor.childElementCount === 1 &&
anchor.children[0].tagName === "BR"
) {
anchor.replaceWith(anchor.children[0]);
}
}
}
function nodeIsElement(node: Node): node is Element {
return node.nodeType == Node.ELEMENT_NODE;
}
function inListItem(): boolean {
const anchor = window.getSelection().anchorNode;
let n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
let inList = false;
while (n) {
inList = inList || window.getComputedStyle(n).display == "list-item";
n = n.parentElement;
}
return inList;
}
function insertNewline() { function insertNewline() {
if (!inPreEnvironment()) { if (!inPreEnvironment()) {
setFormat("insertText", "\n"); setFormat("insertText", "\n");
@ -106,11 +149,10 @@ function insertNewline() {
} }
// is the cursor in an environment that respects whitespace? // is the cursor in an environment that respects whitespace?
function inPreEnvironment() { function inPreEnvironment(): boolean {
let n = window.getSelection().anchorNode as Element; const anchor = window.getSelection().anchorNode;
if (n.nodeType === 3) { const n = nodeIsElement(anchor) ? anchor : anchor.parentElement;
n = n.parentNode as Element;
}
return window.getComputedStyle(n).whiteSpace.startsWith("pre"); return window.getComputedStyle(n).whiteSpace.startsWith("pre");
} }
@ -338,19 +380,19 @@ function setFields(fields) {
</tr> </tr>
<tr> <tr>
<td width=100%> <td width=100%>
<div id=f${i} <div id="f${i}"
onkeydown='onKey(window.event);' onkeydown="onKey(window.event);"
oninput='onInput();' onkeyup="onKeyUp(window.event);"
onmouseup='onKey(window.event);' oninput="onInput();"
onfocus='onFocus(this);' onmouseup="onKey(window.event);"
onblur='onBlur();' onfocus="onFocus(this);"
class='field clearfix' onblur="onBlur();"
onpaste='onPaste(this);' class="field clearfix"
oncopy='onCutOrCopy(this);' onpaste="onPaste(this);"
oncut='onCutOrCopy(this);' oncopy="onCutOrCopy(this);"
contentEditable=true oncut="onCutOrCopy(this);"
class=field contentEditable
style='color: ${color}' style="color: ${color}"
>${f}</div> >${f}</div>
</td> </td>
</tr>`; </tr>`;