163 lines
4.6 KiB
JavaScript
163 lines
4.6 KiB
JavaScript
function requestContentFocus() {
|
|
$("#editor").focus();
|
|
}
|
|
|
|
function setupWhenContentEditable() {
|
|
var editor = $("#editor");
|
|
if (!editor[0].hasAttribute("contenteditable")) {
|
|
return;
|
|
}
|
|
|
|
// paste
|
|
editor.on("paste", function(e) {
|
|
e.preventDefault();
|
|
var text = (e.originalEvent || e).clipboardData.getData("text/plain");
|
|
text = text.replace(/\n/g, "<br>");
|
|
if ("" != text) {
|
|
document.execCommand("insertHTML", false, text);
|
|
} else {
|
|
window.onPasteListener.onPaste();
|
|
}
|
|
});
|
|
|
|
requestContentFocus();
|
|
}
|
|
|
|
function getStyle(dom, name) {
|
|
return window.getComputedStyle(dom)[name];
|
|
}
|
|
|
|
function customLinkgo(self) {
|
|
var datas = self.dataset.datas;
|
|
console.log(datas)
|
|
window.OnLinkClickListener.onClick(datas);
|
|
}
|
|
|
|
var typeClassList = [
|
|
"community_article-container",
|
|
"answer-container",
|
|
"game-container"
|
|
];
|
|
|
|
function removeDomByParent(curDom) {
|
|
if (curDom.parentElement) {
|
|
curDom.parentElement.removeChild(curDom);
|
|
}
|
|
}
|
|
|
|
window.addEventListener("load", function() {
|
|
var EditorDom = document.querySelector("#editor");
|
|
setupWhenContentEditable();
|
|
|
|
document.addEventListener("keydown", function(e) {
|
|
var event = e || window.event;
|
|
|
|
if (event.keyCode === 8) {
|
|
var s = document.getSelection();
|
|
var r = s.getRangeAt(0);
|
|
|
|
if (r.startOffset === r.endOffset && r.endOffset === 0) {
|
|
var preDOM = s.focusNode.previousElementSibling;
|
|
|
|
if (
|
|
preDOM &&
|
|
preDOM instanceof Element &&
|
|
preDOM.nodeName === "IMG" &&
|
|
getStyle(preDOM, "display") === "block"
|
|
) {
|
|
preDOM.parentElement.removeChild(preDOM);
|
|
}
|
|
}
|
|
|
|
var customDom = s.focusNode;
|
|
if (customDom) {
|
|
if (
|
|
r.startContainer.nodeName.toLowerCase() === "blockquote" &&
|
|
r.startOffset === 0
|
|
) {
|
|
RE.formatBlock();
|
|
e.preventDefault();
|
|
} else if (
|
|
customDom.nodeName === "#text" &&
|
|
customDom.previousElementSibling &&
|
|
typeClassList.indexOf(customDom.previousElementSibling.className) >
|
|
-1 &&
|
|
r.startOffset === 1
|
|
) {
|
|
var needDeleteDom = customDom.previousElementSibling;
|
|
needDeleteDom.insertAdjacentElement(
|
|
"afterend",
|
|
document.createElement("br")
|
|
);
|
|
} else if (
|
|
customDom instanceof Element &&
|
|
customDom.childNodes[s.focusOffset] &&
|
|
customDom.childNodes[s.focusOffset].previousElementSibling &&
|
|
typeClassList.indexOf(
|
|
customDom.childNodes[s.focusOffset].previousElementSibling.className
|
|
) > -1
|
|
) {
|
|
customDom =
|
|
customDom.childNodes[s.focusOffset].previousElementSibling;
|
|
customDom.parentElement.removeChild(customDom);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
document.addEventListener("keyup", function(e) {
|
|
var event = e || window.event;
|
|
if (event.keyCode === 13) {
|
|
var s = document.getSelection();
|
|
var curDom = s.focusNode;
|
|
var preDom = curDom.previousElementSibling;
|
|
|
|
if (
|
|
curDom.nodeName.toLowerCase() === "blockquote" &&
|
|
preDom.nodeName.toLowerCase() === "blockquote"
|
|
) {
|
|
if (
|
|
preDom.childNodes.length > 1 ||
|
|
(preDom.childNodes.length === 1 &&
|
|
preDom.childNodes[0].tagName !== "BR")
|
|
) {
|
|
curDom.style.marginTop = 0;
|
|
preDom.style.marginBottom = 0;
|
|
} else if (
|
|
(curDom.childNodes.length === 0 ||
|
|
(curDom.childNodes.length === 1 &&
|
|
curDom.childNodes[0].tagName === "BR")) &&
|
|
(preDom.childNodes.length === 0 ||
|
|
(preDom.childNodes.length === 1 &&
|
|
preDom.childNodes[0].tagName === "BR"))
|
|
) {
|
|
|
|
removeDomByParent(curDom);
|
|
|
|
var startQuoteDom = preDom.previousElementSibling;
|
|
startQuoteDom && startQuoteDom.nodeName.toLowerCase() === "blockquote"
|
|
? (startQuoteDom.style.marginBottom = "10px")
|
|
: null;
|
|
|
|
var range = document.createRange();
|
|
range.selectNode(preDom);
|
|
s.removeAllRanges();
|
|
s.addRange(range);
|
|
|
|
RE.formatBlock();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
document.addEventListener("selectionchange", function(e) {
|
|
var event = e || window.event;
|
|
var targetDom = event.target.activeElement;
|
|
if (targetDom.id === "editor" && targetDom.lastElementChild) {
|
|
if (typeClassList.indexOf(targetDom.lastElementChild.className) > -1) {
|
|
var brDom = document.createElement("br");
|
|
EditorDom.appendChild(brDom);
|
|
}
|
|
}
|
|
});
|
|
}); |