|
|
@ -15,11 +15,30 @@ class TextEditor extends EventEmitter {
|
|
|
|
//--------------------------
|
|
|
|
//--------------------------
|
|
|
|
constructor(textEditor, scrollLimit) {
|
|
|
|
constructor(textEditor, scrollLimit) {
|
|
|
|
super();
|
|
|
|
super();
|
|
|
|
//hljs.initHighlightingOnLoad();
|
|
|
|
|
|
|
|
this.textEditor = textEditor;
|
|
|
|
document.getElementById("edit").addEventListener("input", (e) => {
|
|
|
|
this.fixLimit = scrollLimit;
|
|
|
|
let result_element = document.querySelector("#highlight-content");
|
|
|
|
this.caretPos = null;
|
|
|
|
this.textEditor = textEditor;
|
|
|
|
this.url = "";
|
|
|
|
// Update code
|
|
|
|
|
|
|
|
let text = e.target.value;
|
|
|
|
|
|
|
|
result_element.innerHTML = text
|
|
|
|
|
|
|
|
.replace(new RegExp("&", "g"), "&")
|
|
|
|
|
|
|
|
.replace(new RegExp("<", "g"), "<");
|
|
|
|
|
|
|
|
let editorHeight = document.getElementById("highlight").offsetHeight;
|
|
|
|
|
|
|
|
document.getElementById("edit-post-wrapper").style.height =
|
|
|
|
|
|
|
|
editorHeight + "px";
|
|
|
|
|
|
|
|
e.target.style.height = editorHeight + "px";
|
|
|
|
|
|
|
|
// Syntax Highlight
|
|
|
|
|
|
|
|
Prism.highlightElement(result_element);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("edit").addEventListener("scroll", (e) => {
|
|
|
|
|
|
|
|
/* Scroll result to scroll coords of event - sync with textarea */
|
|
|
|
|
|
|
|
let result_element = document.querySelector("#highlight");
|
|
|
|
|
|
|
|
// Get and set x and y
|
|
|
|
|
|
|
|
result_element.scrollTop = e.scrollTop;
|
|
|
|
|
|
|
|
result_element.scrollLeft = e.scrollLeft;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("edit").dispatchEvent(new Event("input"));
|
|
|
|
this.setInputs();
|
|
|
|
this.setInputs();
|
|
|
|
window.addEventListener("scroll", () => {
|
|
|
|
window.addEventListener("scroll", () => {
|
|
|
|
//var fixLimit = this.fixLimit;
|
|
|
|
//var fixLimit = this.fixLimit;
|
|
|
@ -32,7 +51,7 @@ class TextEditor extends EventEmitter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.refresh();
|
|
|
|
//this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
// methods
|
|
|
@ -47,32 +66,6 @@ class TextEditor extends EventEmitter {
|
|
|
|
false
|
|
|
|
false
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.textEditor.addEventListener("input", (e) => {
|
|
|
|
|
|
|
|
let htmlTagRe = /<[a-z][\s\S]*>/g;
|
|
|
|
|
|
|
|
let text = this.textEditor.innerText;
|
|
|
|
|
|
|
|
if (e.inputType == "insertParagraph") return; //cursor setting gets weird on return, so just back out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (text.search(htmlTagRe) > -1) {
|
|
|
|
|
|
|
|
let caret = position(this.textEditor).pos;
|
|
|
|
|
|
|
|
self.refresh();
|
|
|
|
|
|
|
|
position(this.textEditor, caret);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var spiffed = Prism.highlight(
|
|
|
|
|
|
|
|
this.textEditor.innerText,
|
|
|
|
|
|
|
|
Prism.languages.md,
|
|
|
|
|
|
|
|
"md"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
//var spiffed = this.textEditor.innerText;
|
|
|
|
|
|
|
|
spiffed = spiffed.replace(new RegExp("\r?\n", "g"), "<br>");
|
|
|
|
|
|
|
|
var temp = document.createElement("div");
|
|
|
|
|
|
|
|
temp.innerText = spiffed;
|
|
|
|
|
|
|
|
this.textEditor.innerHTML = temp.innerText;
|
|
|
|
|
|
|
|
this.textEditor.style.maxWidth = "900px";
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notify(type, data) {
|
|
|
|
notify(type, data) {
|
|
|
|
switch (type) {
|
|
|
|
switch (type) {
|
|
|
@ -96,22 +89,16 @@ class TextEditor extends EventEmitter {
|
|
|
|
// do nothing
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
|
|
|
|
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
|
|
|
|
position(this.textEditor, this.caretPos);
|
|
|
|
let len = this.textEditor.value.length;
|
|
|
|
var sel, range;
|
|
|
|
let start = this.textEditor.selectionStart;
|
|
|
|
//var pulled;
|
|
|
|
let end = this.textEditor.selectionEnd;
|
|
|
|
sel = window.getSelection(); //console.log(sel)
|
|
|
|
let insert = "![image alt text](" + data + " 'image title')";
|
|
|
|
//console.log(note.message)
|
|
|
|
|
|
|
|
if (sel.rangeCount) {
|
|
|
|
this.textEditor.value =
|
|
|
|
range = sel.getRangeAt(0);
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
//pulled = sel.getRangeAt(0).toString();
|
|
|
|
insert +
|
|
|
|
range.deleteContents();
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
range.insertNode(
|
|
|
|
document.getElementById("edit").dispatchEvent(new Event("input"));
|
|
|
|
document.createTextNode(
|
|
|
|
|
|
|
|
"![image alt text](" + data + " 'image title')"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -120,59 +107,86 @@ class TextEditor extends EventEmitter {
|
|
|
|
//--------------------------
|
|
|
|
//--------------------------
|
|
|
|
handleEditorOption(e) {
|
|
|
|
handleEditorOption(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.preventDefault();
|
|
|
|
var sel, range, pulled;
|
|
|
|
let len = this.textEditor.value.length;
|
|
|
|
sel = window.getSelection(); //console.log(sel)
|
|
|
|
let start = this.textEditor.selectionStart;
|
|
|
|
if (sel.rangeCount) {
|
|
|
|
let end = this.textEditor.selectionEnd;
|
|
|
|
range = sel.getRangeAt(0);
|
|
|
|
|
|
|
|
pulled = sel.getRangeAt(0).toString();
|
|
|
|
let selectedText = this.textEditor.value.substring(start, end);
|
|
|
|
range.deleteContents();
|
|
|
|
let insert = "";
|
|
|
|
switch (e.target.id) {
|
|
|
|
switch (e.target.id) {
|
|
|
|
case "edit-bold":
|
|
|
|
case "edit-bold":
|
|
|
|
range.insertNode(document.createTextNode("**" + pulled + "**"));
|
|
|
|
insert = "**" + selectedText + "**";
|
|
|
|
break;
|
|
|
|
this.textEditor.value =
|
|
|
|
case "edit-italic":
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
range.insertNode(document.createTextNode("*" + pulled + "*"));
|
|
|
|
insert +
|
|
|
|
break;
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
case "edit-strikethrough":
|
|
|
|
|
|
|
|
range.insertNode(
|
|
|
|
break;
|
|
|
|
document.createTextNode("<del>" + pulled + "</del>")
|
|
|
|
case "edit-italic":
|
|
|
|
);
|
|
|
|
insert = "*" + selectedText + "*";
|
|
|
|
break;
|
|
|
|
//console.log(this.textEditor);
|
|
|
|
case "edit-header1":
|
|
|
|
this.textEditor.value =
|
|
|
|
range.insertNode(document.createTextNode("# " + pulled + "\n"));
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
break;
|
|
|
|
insert +
|
|
|
|
case "edit-header2":
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
range.insertNode(document.createTextNode("## " + pulled + "\n"));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "edit-strikethrough":
|
|
|
|
case "edit-header3":
|
|
|
|
insert = "~~" + selectedText + "~~";
|
|
|
|
range.insertNode(document.createTextNode("### " + pulled + "\n"));
|
|
|
|
this.textEditor.value =
|
|
|
|
break;
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
case "edit-image":
|
|
|
|
insert +
|
|
|
|
this.caretPos = position(this.textEditor).pos;
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPLOAD_POST_IMAGE);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "edit-header1":
|
|
|
|
case "submit-save":
|
|
|
|
insert = "# " + selectedText + "\n";
|
|
|
|
case "edit-save":
|
|
|
|
this.textEditor.value =
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_SAVE);
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
break;
|
|
|
|
insert +
|
|
|
|
case "submit-update":
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
case "edit-update":
|
|
|
|
break;
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPDATE);
|
|
|
|
case "edit-header2":
|
|
|
|
break;
|
|
|
|
insert = "## " + selectedText + "\n";
|
|
|
|
case "edit-link":
|
|
|
|
this.textEditor.value =
|
|
|
|
range.insertNode(
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
document.createTextNode("[" + pulled + "](PASTE URL HERE)")
|
|
|
|
insert +
|
|
|
|
);
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "edit-delete":
|
|
|
|
case "edit-header3":
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_DELETE);
|
|
|
|
insert = "### " + selectedText + "\n";
|
|
|
|
break;
|
|
|
|
this.textEditor.value =
|
|
|
|
default:
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
//range.insertNode(document.createTextNode("[" + self.url + "](PASTE URL HERE)"));
|
|
|
|
insert +
|
|
|
|
break;
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "edit-link":
|
|
|
|
|
|
|
|
let url = prompt("Let's get that url, boss");
|
|
|
|
|
|
|
|
let link = url.toLowerCase();
|
|
|
|
|
|
|
|
insert = "[" + selectedText + "](" + link + ")";
|
|
|
|
|
|
|
|
this.textEditor.value =
|
|
|
|
|
|
|
|
this.textEditor.value.substring(0, start) +
|
|
|
|
|
|
|
|
insert +
|
|
|
|
|
|
|
|
this.textEditor.value.substring(end, len);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "edit-image":
|
|
|
|
|
|
|
|
this.caretPos = position(this.textEditor).pos;
|
|
|
|
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPLOAD_POST_IMAGE);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "submit-save":
|
|
|
|
|
|
|
|
case "edit-save":
|
|
|
|
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_SAVE);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "submit-update":
|
|
|
|
|
|
|
|
case "edit-update":
|
|
|
|
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPDATE);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "edit-delete":
|
|
|
|
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_DELETE);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
//do stuff
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.refresh();
|
|
|
|
document.getElementById("edit").dispatchEvent(new Event("input"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default TextEditor;
|
|
|
|
export default TextEditor;
|
|
|
|