2019-09-13 22:47:07 +12:00
|
|
|
(function(window) {
|
|
|
|
"use strict";
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
window.ls.container.get("view").add({
|
|
|
|
selector: "data-forms-pell",
|
|
|
|
controller: function(element, window, document, markdown) {
|
|
|
|
var div = document.createElement("div");
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
element.className = "pell hide";
|
|
|
|
div.className = "input pell";
|
|
|
|
element.parentNode.insertBefore(div, element);
|
|
|
|
element.tabIndex = -1;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
var turndownService = new TurndownService();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
turndownService.addRule("underline", {
|
|
|
|
filter: ["u"],
|
|
|
|
replacement: function(content) {
|
|
|
|
return "__" + content + "__";
|
|
|
|
}
|
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
var editor = window.pell.init({
|
|
|
|
element: div,
|
|
|
|
onChange: function onChange(html) {
|
|
|
|
element.value = turndownService.turndown(html); // Change HTML to Markdown
|
|
|
|
},
|
|
|
|
defaultParagraphSeparator: "p",
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
name: "bold",
|
|
|
|
icon: '<i class="icon-bold"></i>'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "underline",
|
|
|
|
icon: '<i class="icon-underline"></i>'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "italic",
|
|
|
|
icon: '<i class="icon-italic"></i>'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "olist",
|
|
|
|
icon: '<i class="icon-list-numbered"></i>'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ulist",
|
|
|
|
icon: '<i class="icon-list-bullet"></i>'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "link",
|
|
|
|
icon: '<i class="icon-link"></i>'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
element.addEventListener("change", function() {
|
|
|
|
editor.content.innerHTML = markdown.render(element.value); // Change Markdown to HTML (mainly for editing purpose)
|
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
editor.content.setAttribute("placeholder", element.placeholder);
|
|
|
|
editor.content.innerHTML = markdown.render(element.value);
|
|
|
|
editor.content.tabIndex = 0;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
editor.content.onkeydown = function preventTab(event) {
|
|
|
|
if (event.which === 9) {
|
|
|
|
event.preventDefault();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
// Inspired by: https://stackoverflow.com/a/35173443/2299554
|
|
|
|
//add all elements we want to include in our selection
|
|
|
|
if (document.activeElement) {
|
|
|
|
var focussable = Array.prototype.filter.call(
|
|
|
|
document.querySelectorAll(
|
|
|
|
'a:not([disabled]), button:not([disabled]), select:not([disabled]), input[type=text]:not([disabled]), input[type=checkbox]:not([disabled]), [tabindex]:not([disabled]):not([tabindex="-1"])'
|
|
|
|
),
|
|
|
|
function(element) {
|
|
|
|
//check for visibility while always include the current activeElement
|
|
|
|
return (
|
|
|
|
element.offsetWidth > 0 ||
|
|
|
|
element.offsetHeight > 0 ||
|
|
|
|
element === document.activeElement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
var index = focussable.indexOf(document.activeElement);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
if (index > -1) {
|
|
|
|
if (event.shiftKey) {
|
|
|
|
// Move up
|
|
|
|
var prevElement =
|
|
|
|
focussable[index - 1] || focussable[focussable.length - 1];
|
|
|
|
prevElement.focus();
|
|
|
|
} else {
|
|
|
|
// Move down
|
|
|
|
var nextElement = focussable[index + 1] || focussable[0];
|
|
|
|
nextElement.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
var clean = function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
var clipboardData = e.clipboardData || window.clipboardData;
|
|
|
|
window.pell.exec("insertText", clipboardData.getData("Text"));
|
|
|
|
return true;
|
|
|
|
};
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-13 22:47:07 +12:00
|
|
|
div.addEventListener("paste", clean);
|
|
|
|
div.addEventListener("drop", clean);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})(window);
|