2018-10-31 17:00:31 +01:00
|
|
|
import * as DataEvent from '../events/DataEvent';
|
2018-11-11 20:49:26 +01:00
|
|
|
import DateUtils from '../utilities/DateUtils';
|
2018-11-14 17:32:35 +01:00
|
|
|
import
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
position,
|
|
|
|
offset
|
2018-11-14 17:32:35 +01:00
|
|
|
}
|
|
|
|
from 'caret-pos';
|
2018-11-09 02:45:37 +01:00
|
|
|
import EventEmitter from '../events/EventEmitter';
|
|
|
|
import * as EditorEvent from '../events/EditorEvent';
|
2018-11-14 17:32:35 +01:00
|
|
|
class TextEditor extends EventEmitter
|
|
|
|
{
|
2018-11-21 01:42:52 +01:00
|
|
|
/**
|
|
|
|
* Text Editor UI Component
|
|
|
|
* @constructor
|
|
|
|
* @param {object} textEditor - Text area that will edit text
|
|
|
|
* @param {number} scrollLimit - YPos where editor position will become fixed
|
|
|
|
*/
|
2018-10-31 17:00:31 +01:00
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
2018-11-14 17:32:35 +01:00
|
|
|
constructor(textEditor, scrollLimit)
|
|
|
|
{
|
2018-11-09 02:45:37 +01:00
|
|
|
super();
|
2018-10-31 17:00:31 +01:00
|
|
|
hljs.initHighlightingOnLoad();
|
2018-11-03 19:06:09 +01:00
|
|
|
this.dateUtils = new DateUtils();
|
2018-11-09 02:45:37 +01:00
|
|
|
this.textEditor = textEditor;
|
|
|
|
this.fixLimit = scrollLimit;
|
|
|
|
this.caretPos = null;
|
2018-10-31 17:00:31 +01:00
|
|
|
this.url = '';
|
2018-11-03 19:06:09 +01:00
|
|
|
let self = this;
|
2018-11-09 02:45:37 +01:00
|
|
|
this.setInputs();
|
2018-11-14 17:32:35 +01:00
|
|
|
window.addEventListener("scroll", f =>
|
|
|
|
{
|
2018-11-09 02:45:37 +01:00
|
|
|
var fixLimit = this.fixLimit;
|
2018-11-14 17:32:35 +01:00
|
|
|
if (window.pageYOffset >= fixLimit)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
document.getElementById('edit-control').style.position = "fixed"
|
2018-11-14 17:32:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
document.getElementById('edit-control').style.position = "relative"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
//--------------------------
|
2018-11-14 17:32:35 +01:00
|
|
|
setInputs()
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
let self = this;
|
|
|
|
var editorButtons = document.querySelectorAll('.editor-button');
|
2018-11-14 17:32:35 +01:00
|
|
|
for (var i = 0, length = editorButtons.length; i < length; i++)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
editorButtons[i].addEventListener('click', e => this.handleEditorOption(e), false);
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 17:32:35 +01:00
|
|
|
refresh()
|
|
|
|
{
|
2018-11-09 02:45:37 +01:00
|
|
|
var spiffed = hljs.highlight('markdown', this.textEditor.innerText).value;
|
2018-12-06 22:49:49 +01:00
|
|
|
spiffed = spiffed.replace(new RegExp('\r?\n','g'), '<br>');
|
2018-10-31 17:00:31 +01:00
|
|
|
var temp = document.createElement("div");
|
|
|
|
temp.innerText = spiffed;
|
2018-11-09 02:45:37 +01:00
|
|
|
this.textEditor.innerHTML = temp.innerText;
|
2018-12-06 22:49:49 +01:00
|
|
|
this.textEditor.style.maxWidth = '900px';
|
2018-10-31 17:00:31 +01:00
|
|
|
}
|
2018-11-14 17:32:35 +01:00
|
|
|
notify(type, data)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2018-11-05 23:32:33 +01:00
|
|
|
case DataEvent.POST_UPDATED:
|
2018-11-09 02:45:37 +01:00
|
|
|
document.getElementById('submit-update').classList.add('icon-hide');
|
2018-11-05 23:32:33 +01:00
|
|
|
document.getElementById('submit-good').classList.remove('icon-hide');
|
2018-11-11 00:08:00 +01:00
|
|
|
document.getElementById('edit-update').classList.remove('submit-start');
|
|
|
|
document.getElementById('edit-update').classList.add('submit-cool');
|
2018-11-14 17:32:35 +01:00
|
|
|
setTimeout(f =>
|
|
|
|
{
|
2018-11-09 02:45:37 +01:00
|
|
|
document.getElementById('submit-update').classList.remove('icon-hide');
|
2018-11-05 23:32:33 +01:00
|
|
|
document.getElementById('submit-good').classList.add('icon-hide');
|
2018-11-11 00:08:00 +01:00
|
|
|
document.getElementById('edit-update').classList.add('submit-start');
|
|
|
|
document.getElementById('edit-update').classList.remove('submit-cool');
|
2018-11-05 23:32:33 +01:00
|
|
|
}, 2000);
|
2018-11-09 02:45:37 +01:00
|
|
|
break;
|
|
|
|
case DataEvent.POST_ADDED:
|
2018-11-09 18:44:13 +01:00
|
|
|
// do nothing
|
2018-11-09 02:45:37 +01:00
|
|
|
break;
|
|
|
|
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
|
|
|
|
position(this.textEditor, this.caretPos);
|
|
|
|
var sel, range, pulled;
|
|
|
|
sel = window.getSelection(); //console.log(sel)
|
|
|
|
//console.log(note.message)
|
2018-11-14 17:32:35 +01:00
|
|
|
if (sel.rangeCount)
|
|
|
|
{
|
2018-11-09 02:45:37 +01:00
|
|
|
range = sel.getRangeAt(0);
|
|
|
|
pulled = sel.getRangeAt(0).toString();
|
|
|
|
range.deleteContents();
|
|
|
|
range.insertNode(document.createTextNode("![image alt text](" + data + " 'image title')"));
|
|
|
|
}
|
|
|
|
this.refresh();
|
2018-11-05 23:32:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 17:00:31 +01:00
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
2018-11-14 17:32:35 +01:00
|
|
|
handleEditorOption(e)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
e.preventDefault();
|
|
|
|
var self = this;
|
|
|
|
var sel, range, pulled;
|
|
|
|
sel = window.getSelection(); //console.log(sel)
|
2018-11-14 17:32:35 +01:00
|
|
|
if (sel.rangeCount)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
range = sel.getRangeAt(0);
|
|
|
|
pulled = sel.getRangeAt(0).toString();
|
|
|
|
range.deleteContents();
|
2018-11-14 17:32:35 +01:00
|
|
|
switch (e.target.id)
|
|
|
|
{
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-bold":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("**" + pulled + "**"));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-italic":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("*" + pulled + "*"));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-strikethrough":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("<del>" + pulled + "</del>"));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-header1":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("# " + pulled));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-header2":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("## " + pulled));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-header3":
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("### " + pulled));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-image":
|
2018-11-09 02:45:37 +01:00
|
|
|
this.caretPos = position(this.textEditor).pos;
|
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPLOAD_POST_IMAGE);
|
|
|
|
break;
|
|
|
|
case "submit-save":
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-save":
|
2018-11-09 02:45:37 +01:00
|
|
|
this.emitEvent(EditorEvent.EDITOR_SAVE);
|
|
|
|
break;
|
|
|
|
case "submit-update":
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-update":
|
2018-11-09 02:45:37 +01:00
|
|
|
this.emitEvent(EditorEvent.EDITOR_UPDATE);
|
2018-11-14 17:32:35 +01:00
|
|
|
break
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-link":
|
2019-02-24 04:07:14 +01:00
|
|
|
console.log("PULLED", pulled)
|
2018-10-31 17:00:31 +01:00
|
|
|
range.insertNode(document.createTextNode("[" + pulled + "](PASTE URL HERE)"));
|
|
|
|
break;
|
2018-11-11 00:08:00 +01:00
|
|
|
case "edit-delete":
|
2018-11-09 02:45:37 +01:00
|
|
|
this.emitEvent(EditorEvent.EDITOR_DELETE);
|
2018-10-31 17:00:31 +01:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
//range.insertNode(document.createTextNode("[" + self.url + "](PASTE URL HERE)"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export default TextEditor
|