var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var repeat = function (string, count) {
var result = '';
for (var i = 0; i < count; i++) {
result += string;
}
return result;
};
var DownArea = (function () {
function DownArea(args) {
var _a;
var _b, _c, _d, _e, _f;
this.minWidth = 464;
this.minHeight = 120;
this.tools = [];
this.actions = (_a = {},
_a['heading-1'] = { fn: this.addHeading },
_a['heading-2'] = { fn: this.addHeading, args: [2] },
_a['heading-3'] = { fn: this.addHeading, args: [3] },
_a['heading-4'] = { fn: this.addHeading, args: [4] },
_a['heading-5'] = { fn: this.addHeading, args: [5] },
_a['heading-6'] = { fn: this.addHeading, args: [6] },
_a['bold'] = { fn: this.addBold },
_a['italic'] = { fn: this.addItalic },
_a['bold-italic'] = { fn: this.addBoldItalic },
_a['normal-link'] = { fn: this.addLink },
_a['quick-link'] = { fn: this.addLink, args: [1] },
_a['email'] = { fn: this.addEmail },
_a['image'] = { fn: this.addImage },
_a['blockquote'] = { fn: this.addBlockquote },
_a['u-list'] = { fn: this.addUnorderedList },
_a['o-list'] = { fn: this.addOrderedList },
_a['sl-code'] = { fn: this.addSingleLineCode },
_a['code-block'] = { fn: this.addCodeBlock },
_a);
this.element = args.elem;
this.attr = (_b = args.attr) !== null && _b !== void 0 ? _b : {};
this.textareaName = (_c = args.name) !== null && _c !== void 0 ? _c : null;
this.textareaValue = (_d = args.value) !== null && _d !== void 0 ? _d : '';
this.resize = (_e = args.resize) !== null && _e !== void 0 ? _e : DownArea.RESIZE_VERTICAL;
this.hiddenTools = (_f = args.hide) !== null && _f !== void 0 ? _f : [];
this.init();
}
DownArea.prototype.init = function () {
var _a;
if (this.element instanceof HTMLTextAreaElement) {
var containerElement = document.createElement('div');
if (this.attr.id) {
containerElement.id = this.attr.id.join(' ');
}
if (this.attr.class) {
(_a = containerElement.classList).add.apply(_a, this.attr.class);
}
this.textarea = this.element;
this.element.parentNode.replaceChild(containerElement, this.element);
this.element = containerElement;
this.createElements();
}
else {
this.createElements();
}
this.registerElements();
this.initResizer();
this.listenTools();
this.initDropdowns();
this.listenTextareaEvents();
this.setTextareaSize();
};
DownArea.prototype.createElements = function () {
var _a;
var downareaElement = document.createElement('div');
downareaElement.classList.add('downarea');
this.element.appendChild(downareaElement);
var resizer = document.createElement('div');
resizer.classList.add('resizer');
downareaElement.appendChild(resizer);
var wrapper = document.createElement('div');
wrapper.classList.add('downarea-wrapper');
downareaElement.appendChild(wrapper);
var toolbar = document.createElement('div');
toolbar.classList.add('downarea-toolbar');
wrapper.appendChild(toolbar);
if (this.hiddenTools.indexOf('heading') < 0) {
var headingTool = document.createElement('div');
headingTool.classList.add('downarea-toolbar-tool');
headingTool.dataset.dropdown = 'heading-dropdown';
headingTool.title = 'Heading';
headingTool.innerText = 'Heading';
toolbar.appendChild(headingTool);
var headingDropdown = document.createElement('div');
headingDropdown.classList.add('downarea-tool-dropdown');
headingDropdown.id = 'heading-dropdown';
headingDropdown.innerHTML = "
\n - Heading 1
\n - Heading 2
\n - Heading 3
\n - Heading 4
\n - Heading 5
\n - Heading 6
\n
";
headingTool.appendChild(headingDropdown);
}
if (this.hiddenTools.indexOf('bold') < 0) {
var boldTool = document.createElement('div');
boldTool.classList.add('downarea-toolbar-tool');
boldTool.dataset.action = 'bold';
boldTool.title = 'Bold';
boldTool.innerHTML = "";
toolbar.appendChild(boldTool);
}
if (this.hiddenTools.indexOf('italic') < 0) {
var italicTool = document.createElement('div');
italicTool.classList.add('downarea-toolbar-tool');
italicTool.dataset.action = 'italic';
italicTool.title = 'Italic';
italicTool.innerHTML = "";
toolbar.appendChild(italicTool);
}
if (this.hiddenTools.indexOf('bold-italic') < 0) {
var boldItalicTool = document.createElement('div');
boldItalicTool.classList.add('downarea-toolbar-tool');
boldItalicTool.dataset.action = 'bold-italic';
boldItalicTool.title = 'Bold Italic';
boldItalicTool.innerHTML = "";
toolbar.appendChild(boldItalicTool);
}
if (this.hiddenTools.indexOf('link') < 0) {
var linkTool = document.createElement('div');
linkTool.classList.add('downarea-toolbar-tool');
linkTool.dataset.dropdown = 'link-dropdown';
linkTool.title = 'Link';
linkTool.innerHTML = "";
toolbar.appendChild(linkTool);
var linkDropdown = document.createElement('div');
linkDropdown.classList.add('downarea-tool-dropdown');
linkDropdown.id = 'link-dropdown';
linkDropdown.innerHTML = "\n - Normal
\n - Quick
\n - Email
\n
";
linkTool.appendChild(linkDropdown);
}
if (this.hiddenTools.indexOf('image') < 0) {
var imageTool = document.createElement('div');
imageTool.classList.add('downarea-toolbar-tool');
imageTool.dataset.action = 'image';
imageTool.title = 'Image';
imageTool.innerHTML = "";
toolbar.appendChild(imageTool);
}
if (this.hiddenTools.indexOf('blockquote') < 0) {
var blockquoteTool = document.createElement('div');
blockquoteTool.classList.add('downarea-toolbar-tool');
blockquoteTool.dataset.action = 'blockquote';
blockquoteTool.title = 'Blockquote';
blockquoteTool.innerHTML = "";
toolbar.appendChild(blockquoteTool);
}
if (this.hiddenTools.indexOf('u-list') < 0) {
var unorderedListTool = document.createElement('div');
unorderedListTool.classList.add('downarea-toolbar-tool');
unorderedListTool.dataset.action = 'u-list';
unorderedListTool.title = 'Unordered List';
unorderedListTool.innerHTML = "";
toolbar.appendChild(unorderedListTool);
}
if (this.hiddenTools.indexOf('o-list') < 0) {
var orderedListTool = document.createElement('div');
orderedListTool.classList.add('downarea-toolbar-tool');
orderedListTool.dataset.action = 'o-list';
orderedListTool.title = 'Ordered List';
orderedListTool.innerHTML = "";
toolbar.appendChild(orderedListTool);
}
if (this.hiddenTools.indexOf('sl-code') < 0) {
var singleLineCodeTool = document.createElement('div');
singleLineCodeTool.classList.add('downarea-toolbar-tool');
singleLineCodeTool.dataset.action = 'sl-code';
singleLineCodeTool.title = 'Single Line Code';
singleLineCodeTool.innerHTML = "";
toolbar.appendChild(singleLineCodeTool);
}
if (this.hiddenTools.indexOf('code-block') < 0) {
var codeBlockTool = document.createElement('div');
codeBlockTool.classList.add('downarea-toolbar-tool');
codeBlockTool.dataset.action = 'code-block';
codeBlockTool.title = 'Code Block';
codeBlockTool.innerHTML = "";
toolbar.appendChild(codeBlockTool);
}
var textareaContainer = document.createElement('div');
textareaContainer.classList.add('downarea-textarea');
wrapper.appendChild(textareaContainer);
var textarea = (_a = this.textarea) !== null && _a !== void 0 ? _a : document.createElement('textarea');
if (this.textareaName) {
textarea.name = this.textareaName;
}
textarea.value = this.textareaValue.toString();
textareaContainer.appendChild(textarea);
var bottom = document.createElement('div');
bottom.classList.add('downarea-bottom');
wrapper.appendChild(bottom);
};
DownArea.prototype.registerElements = function () {
this.downareaElement = this.element.querySelector('.downarea');
this.resizer = this.downareaElement.querySelector('.resizer');
this.toolbar = this.downareaElement.querySelector('.downarea-toolbar');
this.textarea = this.downareaElement.querySelector('.downarea-textarea textarea');
};
DownArea.prototype.listenTextareaEvents = function () {
var self = this;
this.textarea.addEventListener('focusin', function () {
self.downareaElement.classList.add('focused');
});
this.textarea.addEventListener('focusout', function () {
self.downareaElement.classList.remove('focused');
});
this.textarea.addEventListener('keydown', function (e) {
if (e.key.toLowerCase() === 'enter' && DownArea.onListing !== null) {
e.preventDefault();
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var splittedStart = start.split(/\n/g);
var list = '';
var pattern = DownArea.onListing.type === 'unordered' ? /-\s?/ : /\d+\.\s?/;
if (splittedStart.length) {
var match = splittedStart[splittedStart.length - 1].trim().match(pattern);
if (match && match[0] === match.input) {
splittedStart.pop();
self.textarea.value = "".concat(splittedStart.join('\n'), "\n").concat(end);
self.textarea.selectionStart = splittedStart.join('\n').length + '\n'.length;
self.textarea.selectionEnd = self.textarea.selectionStart;
return DownArea.onListing = null;
}
}
if (DownArea.onListing.type === 'unordered') {
list = "\n- ";
}
if (DownArea.onListing.type === 'ordered') {
list = "\n".concat(DownArea.onListing.number, ". ");
DownArea.onListing.number++;
}
self.textarea.value = "".concat(start).concat(list).concat(end);
self.textarea.selectionStart = start.length + list.length;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
}
if (e.key.toLowerCase() === 'tab') {
e.preventDefault();
if (e.shiftKey) {
self.shiftTab(self);
}
else {
self.addTab(self);
}
}
});
};
DownArea.prototype.setTextareaSize = function () {
var downareaRect = this.downareaElement.getBoundingClientRect();
var toolbarRect = this.toolbar.getBoundingClientRect();
var bottomRect = this.downareaElement.querySelector('.downarea-bottom').getBoundingClientRect();
this.textarea.style.height = downareaRect.height - toolbarRect.height - bottomRect.height + 'px';
};
DownArea.prototype.initResizer = function () {
var _this = this;
if (this.resize == DownArea.RESIZE_OFF) {
this.resizer.remove();
}
else if (this.resize == DownArea.RESIZE_VERTICAL) {
this.resizer.classList.add('vertical');
}
else if (this.resize == DownArea.RESIZE_HORIZONTAL) {
this.resizer.classList.add('horizontal');
}
else if (this.resize == DownArea.RESIZE_BOTH) {
this.resizer.classList.add('both');
}
var isResizing = false;
var self = this;
var mousedown = function (e) {
e.preventDefault();
var rect = self.downareaElement.getBoundingClientRect();
if (rect.width <= self.minWidth) {
self.downareaElement.style.width = self.minWidth + 0.1 + 'px';
}
if (rect.height <= self.minHeight) {
self.downareaElement.style.height = self.minHeight + 0.1 + 'px';
}
isResizing = true;
var prevX = e.clientX;
var prevY = e.clientY;
var mousemove = function (e) {
var rect = self.downareaElement.getBoundingClientRect();
if (rect.width <= self.minWidth) {
self.downareaElement.style.width = self.minWidth + 0.1 + 'px';
}
if (rect.height <= self.minHeight) {
self.downareaElement.style.height = self.minHeight + 0.1 + 'px';
}
if (self.resizer.classList.contains('vertical')) {
if (rect.height > self.minHeight) {
self.downareaElement.style.height = rect.height - (prevY - e.clientY) + 'px';
}
}
else if (self.resizer.classList.contains('horizontal')) {
if (rect.width > self.minWidth) {
self.downareaElement.style.width = rect.width - (prevX - e.clientX) + 'px';
}
}
else if (self.resizer.classList.contains('both')) {
if (rect.width > self.minWidth) {
self.downareaElement.style.width = rect.width - (prevX - e.clientX) + 'px';
}
if (rect.height > self.minHeight) {
self.downareaElement.style.height = rect.height - (prevY - e.clientY) + 'px';
}
}
prevX = e.clientX;
prevY = e.clientY;
_this.setTextareaSize();
};
var mouseup = function () {
removeEventListener('mousemove', mousemove);
removeEventListener('mouseup', mouseup);
isResizing = false;
};
addEventListener('mousemove', mousemove);
addEventListener('mouseup', mouseup);
};
this.resizer.addEventListener('mousedown', mousedown);
};
DownArea.prototype.listenTools = function () {
this.addTools();
var self = this;
this.tools.map(function (tool) {
tool.addEventListener('click', function () {
var _a;
var action = (_a = self.actions[tool.dataset.action]) !== null && _a !== void 0 ? _a : null;
if (action) {
if (action.args) {
action.fn.apply(action, __spreadArray([self], action.args, false));
}
else {
action.fn(self);
}
}
});
});
};
DownArea.prototype.addTools = function () {
var _this = this;
var tools = this.downareaElement.querySelectorAll('div.downarea-toolbar *');
tools.forEach(function (tool) {
if (tool.dataset.action) {
_this.tools.push(tool);
}
});
};
DownArea.prototype.initDropdowns = function () {
var tools = this.downareaElement.querySelectorAll('.downarea-toolbar .downarea-toolbar-tool');
tools.forEach(function (tool) {
if (tool.dataset.dropdown) {
var dropdown_1 = tool.querySelector('#' + tool.dataset.dropdown);
tool.addEventListener('click', function () {
if (dropdown_1.classList.contains('active')) {
dropdown_1.classList.remove('active');
}
else {
dropdown_1.classList.add('active');
}
});
}
});
};
DownArea.prototype.addTab = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var tab = "\t";
self.textarea.value = "".concat(start).concat(tab).concat(end);
self.textarea.selectionStart = start.length + tab.length;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.shiftTab = function (self) {
var _a;
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
if (start[start.length - 1] == '\t') {
start = (_a = start.substr(0, start.length - 1)) !== null && _a !== void 0 ? _a : '';
}
else if (end[0] == '\t') {
end = end.substr(1);
}
self.textarea.value = "".concat(start).concat(end);
self.textarea.selectionStart = start.length;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addHeading = function (self, level) {
if (level === void 0) { level = 1; }
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var heading = "".concat(repeat('#', level), " ");
var offset = 0;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
heading = "".concat(heading).concat(range.trim());
offset = 0;
}
if (start.length && start[start.length - 1] != '\n') {
heading = "\n".concat(heading);
}
if (end.length && end[0] != '\n') {
heading = "".concat(heading, "\n");
offset = 1;
}
self.textarea.value = "".concat(start).concat(heading).concat(end);
self.textarea.selectionStart = start.length + heading.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addBold = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var bold = "****";
var offset = bold.length - 2;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
bold = "**".concat(range, "**");
offset = 2;
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
bold = " ".concat(bold);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
bold = "".concat(bold, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(bold).concat(end);
self.textarea.selectionStart = start.length + bold.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addItalic = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var italic = "__";
var offset = italic.length - 1;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
italic = "_".concat(range, "_");
offset = 1;
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
italic = " ".concat(italic);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
italic = "".concat(italic, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(italic).concat(end);
self.textarea.selectionStart = start.length + italic.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addBoldItalic = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var bold = "******";
var offset = bold.length - 3;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
bold = "***".concat(range, "***");
offset = 3;
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
bold = " ".concat(bold);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
bold = "".concat(bold, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(bold).concat(end);
self.textarea.selectionStart = start.length + bold.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addLink = function (self, type) {
if (type === void 0) { type = 0; }
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var link = '';
var offset = 0;
if (type === 0) {
link = '[](https://)';
offset = link.length - 1;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
var rangeIsURL = range.match(/https?:\/\/(.+)/);
link = rangeIsURL ? "[](".concat(range.trim(), ")") : "[".concat(range.trim(), "](https://)");
offset = rangeIsURL ? range.trim().length + 3 : 1;
}
}
else {
link = '<>';
offset = link.length - 1;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
link = "<".concat(range, ">");
offset = 1;
}
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
link = " ".concat(link);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
link = "".concat(link, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(link).concat(end);
self.textarea.selectionStart = start.length + link.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addEmail = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var emailLink = '<@>';
var offset = emailLink.length - 1;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
emailLink = "<".concat(range, ">");
offset = 1;
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
emailLink = " ".concat(emailLink);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
emailLink = "".concat(emailLink, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(emailLink).concat(end);
self.textarea.selectionStart = start.length + emailLink.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addImage = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var link = "![](https://)";
var offset = link.length - 2;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
var rangeIsURL = range.match(/https?:\/\/(.+)/);
link = rangeIsURL ? "![](".concat(range.trim(), ")") : "![".concat(range.trim(), "](https://)");
offset = rangeIsURL ? range.trim().length + 3 : 1;
}
if (start.length && start[start.length - 1] != '\n' && start[start.length - 1] != ' ') {
link = " ".concat(link);
}
if (end.length && end[0] != '\n' && end[0] != ' ') {
link = "".concat(link, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(link).concat(end);
self.textarea.selectionStart = start.length + link.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addBlockquote = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var blockquote = "> ";
var offset = 0;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
var lines = range.trim().split('\n');
var modifiedLines = lines.map(function (line) {
return "> " + line.trim();
});
blockquote = modifiedLines.join('\n') + '\n';
}
if (start.length && start[start.length - 1] != '\n') {
blockquote = "\n".concat(blockquote);
}
if (end.length && end[0] != '\n') {
blockquote = "".concat(blockquote, "\n");
offset = 1;
}
self.textarea.value = "".concat(start).concat(blockquote).concat(end);
self.textarea.selectionStart = start.length + blockquote.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addUnorderedList = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var list = "- ";
var offset = 0;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
list = "".concat(list).concat(range.trim());
}
if (start.length && start[start.length - 1] != '\n') {
list = "\n".concat(list);
}
if (end.length && end[0] != '\n') {
list = "".concat(list, "\n");
offset = 1;
}
self.textarea.value = "".concat(start).concat(list).concat(end);
self.textarea.selectionStart = start.length + list.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
DownArea.onListing = {
type: 'unordered'
};
};
DownArea.prototype.addOrderedList = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var list = "1. ";
var offset = 0;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
list = "".concat(list).concat(range.trim());
}
if (start.length && start[start.length - 1] != '\n') {
list = "\n".concat(list);
}
if (end.length && end[0] != '\n') {
list = "".concat(list, "\n");
offset = 1;
}
self.textarea.value = "".concat(start).concat(list).concat(end);
self.textarea.selectionStart = start.length + list.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
DownArea.onListing = {
type: 'ordered',
number: 2,
};
};
DownArea.prototype.addSingleLineCode = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var code = '``';
var offset = 1;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
code = "`".concat(range, "`");
}
if (start.length && start[start.length - 1] != '\n') {
code = " ".concat(code);
}
if (end.length && end[0] != '\n') {
code = "".concat(code, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(code).concat(end);
self.textarea.selectionStart = start.length + code.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.prototype.addCodeBlock = function (self) {
var start = self.textarea.value.substr(0, self.textarea.selectionStart);
var end = self.textarea.value.substr(self.textarea.selectionStart);
var code = '```\n```';
var offset = 4;
if (self.textarea.selectionStart != self.textarea.selectionEnd) {
end = self.textarea.value.substr(self.textarea.selectionEnd);
var range = self.textarea.value.slice(self.textarea.selectionStart, self.textarea.selectionEnd);
code = "```\n".concat(range, "\n```");
offset = range.length + 5;
}
if (start.length && start[start.length - 1] != '\n') {
code = "\n".concat(code);
}
if (end.length && end[0] != '\n') {
code = "".concat(code, " ");
offset += 1;
}
self.textarea.value = "".concat(start).concat(code).concat(end);
self.textarea.selectionStart = start.length + code.length - offset;
self.textarea.selectionEnd = self.textarea.selectionStart;
self.textarea.focus();
};
DownArea.RESIZE_OFF = 0;
DownArea.RESIZE_VERTICAL = 1;
DownArea.RESIZE_HORIZONTAL = 2;
DownArea.RESIZE_BOTH = 3;
DownArea.onListing = null;
return DownArea;
}());