(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define('simditor', ["jquery", "simple-module", "simple-hotkeys", "simple-uploader"], function ($, SimpleModule, simpleHotkeys, simpleUploader) { return (root.returnExportsGlobal = factory($, SimpleModule, simpleHotkeys, simpleUploader)); }); } else if (typeof exports === 'object') { // Node. Does not work with strict CommonJS, but // only CommonJS-like enviroments that support module.exports, // like Node. module.exports = factory(require("jquery"), require("simple-module"), require("simple-hotkeys"), require("simple-uploader")); } else { root['Simditor'] = factory(jQuery, SimpleModule, simple.hotkeys, simple.uploader); } }(this, function ($, SimpleModule, simpleHotkeys, simpleUploader) { var Selection, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Selection = (function(_super) { __extends(Selection, _super); function Selection() { return Selection.__super__.constructor.apply(this, arguments); } Selection.pluginName = 'Selection'; Selection.prototype._init = function() { this.editor = this._module; return this.sel = document.getSelection(); }; Selection.prototype.clear = function() { var e; try { return this.sel.removeAllRanges(); } catch (_error) { e = _error; } }; Selection.prototype.getRange = function() { if (!this.editor.inputManager.focused || !this.sel.rangeCount) { return null; } return this.sel.getRangeAt(0); }; Selection.prototype.selectRange = function(range) { this.clear(); this.sel.addRange(range); if (!this.editor.inputManager.focused && (this.editor.util.browser.firefox || this.editor.util.browser.msie)) { this.editor.body.focus(); } return range; }; Selection.prototype.rangeAtEndOf = function(node, range) { var endNode, endNodeLength, result; if (range == null) { range = this.getRange(); } if (!((range != null) && range.collapsed)) { return; } node = $(node)[0]; endNode = range.endContainer; endNodeLength = this.editor.util.getNodeLength(endNode); if (!(range.endOffset === endNodeLength - 1 && $(endNode).contents().last().is('br')) && range.endOffset !== endNodeLength) { return false; } if (node === endNode) { return true; } else if (!$.contains(node, endNode)) { return false; } result = true; $(endNode).parentsUntil(node).addBack().each((function(_this) { return function(i, n) { var $lastChild, nodes; nodes = $(n).parent().contents().filter(function() { return !(this !== n && this.nodeType === 3 && !this.nodeValue); }); $lastChild = nodes.last(); if (!($lastChild.get(0) === n || ($lastChild.is('br') && $lastChild.prev().get(0) === n))) { result = false; return false; } }; })(this)); return result; }; Selection.prototype.rangeAtStartOf = function(node, range) { var result, startNode; if (range == null) { range = this.getRange(); } if (!((range != null) && range.collapsed)) { return; } node = $(node)[0]; startNode = range.startContainer; if (range.startOffset !== 0) { return false; } if (node === startNode) { return true; } else if (!$.contains(node, startNode)) { return false; } result = true; $(startNode).parentsUntil(node).addBack().each((function(_this) { return function(i, n) { var nodes; nodes = $(n).parent().contents().filter(function() { return !(this !== n && this.nodeType === 3 && !this.nodeValue); }); if (nodes.first().get(0) !== n) { return result = false; } }; })(this)); return result; }; Selection.prototype.insertNode = function(node, range) { if (range == null) { range = this.getRange(); } if (range == null) { return; } node = $(node)[0]; range.insertNode(node); return this.setRangeAfter(node, range); }; Selection.prototype.setRangeAfter = function(node, range) { if (range == null) { range = this.getRange(); } if (range == null) { return; } node = $(node)[0]; range.setEndAfter(node); range.collapse(false); return this.selectRange(range); }; Selection.prototype.setRangeBefore = function(node, range) { if (range == null) { range = this.getRange(); } if (range == null) { return; } node = $(node)[0]; range.setEndBefore(node); range.collapse(false); return this.selectRange(range); }; Selection.prototype.setRangeAtStartOf = function(node, range) { if (range == null) { range = this.getRange(); } node = $(node).get(0); range.setEnd(node, 0); range.collapse(false); return this.selectRange(range); }; Selection.prototype.setRangeAtEndOf = function(node, range) { var $lastNode, $node, contents, lastChild, lastText, nodeLength; if (range == null) { range = this.getRange(); } $node = $(node); node = $node.get(0); if ($node.is('pre')) { contents = $node.contents(); if (contents.length > 0) { lastChild = contents.last(); lastText = lastChild.text(); if (lastText.charAt(lastText.length - 1) === '\n') { range.setEnd(lastChild[0], this.editor.util.getNodeLength(lastChild[0]) - 1); } else { range.setEnd(lastChild[0], this.editor.util.getNodeLength(lastChild[0])); } } else { range.setEnd(node, 0); } } else { nodeLength = this.editor.util.getNodeLength(node); if (node.nodeType !== 3 && nodeLength > 0) { $lastNode = $(node).contents().last(); if ($lastNode.is('br')) { nodeLength -= 1; } else if ($lastNode[0].nodeType !== 3 && this.editor.util.isEmptyNode($lastNode)) { $lastNode.append(this.editor.util.phBr); node = $lastNode[0]; nodeLength = 0; } } range.setEnd(node, nodeLength); } range.collapse(false); return this.selectRange(range); }; Selection.prototype.deleteRangeContents = function(range) { var endRange, startRange; if (range == null) { range = this.getRange(); } startRange = range.cloneRange(); endRange = range.cloneRange(); startRange.collapse(true); endRange.collapse(false); if (!range.collapsed && this.rangeAtStartOf(this.editor.body, startRange) && this.rangeAtEndOf(this.editor.body, endRange)) { this.editor.body.empty(); range.setStart(this.editor.body[0], 0); range.collapse(true); this.selectRange(range); } else { range.deleteContents(); } return range; }; Selection.prototype.breakBlockEl = function(el, range) { var $el; if (range == null) { range = this.getRange(); } $el = $(el); if (!range.collapsed) { return $el; } range.setStartBefore($el.get(0)); if (range.collapsed) { return $el; } return $el.before(range.extractContents()); }; Selection.prototype.save = function(range) { var endCaret, endRange, startCaret; if (range == null) { range = this.getRange(); } if (this._selectionSaved) { return; } endRange = range.cloneRange(); endRange.collapse(false); startCaret = $('').addClass('simditor-caret-start'); endCaret = $('').addClass('simditor-caret-end'); endRange.insertNode(endCaret[0]); range.insertNode(startCaret[0]); this.clear(); return this._selectionSaved = true; }; Selection.prototype.restore = function() { var endCaret, endContainer, endOffset, range, startCaret, startContainer, startOffset; if (!this._selectionSaved) { return false; } startCaret = this.editor.body.find('.simditor-caret-start'); endCaret = this.editor.body.find('.simditor-caret-end'); if (startCaret.length && endCaret.length) { startContainer = startCaret.parent(); startOffset = startContainer.contents().index(startCaret); endContainer = endCaret.parent(); endOffset = endContainer.contents().index(endCaret); if (startContainer[0] === endContainer[0]) { endOffset -= 1; } range = document.createRange(); range.setStart(startContainer.get(0), startOffset); range.setEnd(endContainer.get(0), endOffset); startCaret.remove(); endCaret.remove(); this.selectRange(range); } else { startCaret.remove(); endCaret.remove(); } this._selectionSaved = false; return range; }; return Selection; })(SimpleModule); var Formatter, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; Formatter = (function(_super) { __extends(Formatter, _super); function Formatter() { return Formatter.__super__.constructor.apply(this, arguments); } Formatter.pluginName = 'Formatter'; Formatter.prototype._init = function() { this.editor = this._module; this._allowedTags = ['br', 'a', 'img', 'b', 'strong', 'i', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'hr']; this._allowedAttributes = { img: ['src', 'alt', 'width', 'height', 'data-image-src', 'data-image-size', 'data-image-name', 'data-non-image'], a: ['href', 'target'], font: ['color'], pre: ['data-lang', 'class'], p: ['data-indent'], h1: ['data-indent'], h2: ['data-indent'], h3: ['data-indent'], h4: ['data-indent'] }; return this.editor.body.on('click', 'a', (function(_this) { return function(e) { return false; }; })(this)); }; Formatter.prototype.decorate = function($el) { if ($el == null) { $el = this.editor.body; } return this.editor.trigger('decorate', [$el]); }; Formatter.prototype.undecorate = function($el) { if ($el == null) { $el = this.editor.body.clone(); } this.editor.trigger('undecorate', [$el]); return $.trim($el.html()); }; Formatter.prototype.autolink = function($el) { var $node, findLinkNode, lastIndex, linkNodes, match, re, replaceEls, text, uri, _i, _len; if ($el == null) { $el = this.editor.body; } linkNodes = []; findLinkNode = function($parentNode) { return $parentNode.contents().each(function(i, node) { var $node, text; $node = $(node); if ($node.is('a') || $node.closest('a, pre', $el).length) { return; } if ($node.contents().length) { return findLinkNode($node); } else if ((text = $node.text()) && /https?:\/\/|www\./ig.test(text)) { return linkNodes.push($node); } }); }; findLinkNode($el); re = /(https?:\/\/|www\.)[\w\-\.\?&=\/#%:,@\!\+]+/ig; for (_i = 0, _len = linkNodes.length; _i < _len; _i++) { $node = linkNodes[_i]; text = $node.text(); replaceEls = []; match = null; lastIndex = 0; while ((match = re.exec(text)) !== null) { replaceEls.push(document.createTextNode(text.substring(lastIndex, match.index))); lastIndex = re.lastIndex; uri = /^(http(s)?:\/\/|\/)/.test(match[0]) ? match[0] : 'http://' + match[0]; replaceEls.push($('').text(match[0])[0]); } replaceEls.push(document.createTextNode(text.substring(lastIndex))); $node.replaceWith($(replaceEls)); } return $el; }; Formatter.prototype.format = function($el) { var $node, blockNode, n, node, _i, _j, _len, _len1, _ref, _ref1; if ($el == null) { $el = this.editor.body; } if ($el.is(':empty')) { $el.append('

' + this.editor.util.phBr + '

'); return $el; } _ref = $el.contents(); for (_i = 0, _len = _ref.length; _i < _len; _i++) { n = _ref[_i]; this.cleanNode(n, true); } _ref1 = $el.contents(); for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { node = _ref1[_j]; $node = $(node); if ($node.is('br')) { if (typeof blockNode !== "undefined" && blockNode !== null) { blockNode = null; } $node.remove(); } else if (this.editor.util.isBlockNode(node)) { if ($node.is('li')) { if (blockNode && blockNode.is('ul, ol')) { blockNode.append(node); } else { blockNode = $('