diff --git a/markdownx/static/markdownx/js/markdownx.js b/markdownx/static/markdownx/js/markdownx.js index 444f853..4ffc424 100644 --- a/markdownx/static/markdownx/js/markdownx.js +++ b/markdownx/static/markdownx/js/markdownx.js @@ -1,507 +1,844 @@ -(function e(t, n, r) { - function s(o, u) { - if (!n[o]) { - if (!t[o]) { - var a = typeof require == "function" && require; - if (!u && a) return a(o, !0); - if (i) return i(o, !0); - var f = new Error("Cannot find module '" + o + "'"); - throw f.code = "MODULE_NOT_FOUND", f; - } - var l = n[o] = { - exports: {} - }; - t[o][0].call(l.exports, function(e) { - var n = t[o][1][e]; - return s(n ? n : e); - }, l, l.exports, e, t, n, r); - } - return n[o].exports; +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i 0 && + properties.value[properties.start - 1] // -1 is to account any tabs just before the cursor. + .match(/\t/) !== null ? // if there's no `\t`, check the preceding character. + properties.start - 1 : properties.start; + substitution = properties.value + .substring(properties.start) + .replace("\t", ''); // Remove only a single `\t`. } - }; - var keyboardEvents = { - keys: { - TAB: "Tab", - DUPLICATE: "d", - UNINDENT: "[", - INDENT: "]" - }, - handlers: { - applyTab: function(properties) { - return properties.value.substring(0, properties.start) + (properties.value.substring(properties.start, properties.end).match(/\n/gm) === null ? "\t" + properties.value.substring(properties.start) : properties.value.substring(properties.start, properties.end).replace(/^/gm, "\t") + properties.value.substring(properties.end)); - }, - removeTab: function(properties) { - var substitution = null, lineTotal = (properties.value.substring(properties.start, properties.end).match(/\n/g) || []).length; - if (properties.start === properties.end) { - properties.start = properties.start > 0 && properties.value[properties.start - 1].match(/\t/) !== null ? properties.start - 1 : properties.start; - substitution = properties.value.substring(properties.start).replace("\t", ""); - } else if (!lineTotal) { - substitution = properties.value.substring(properties.start).replace("\t", ""); - } else { - substitution = properties.value.substring(properties.start, properties.end).replace(/^\t/gm, "") + properties.value.substring(properties.end); - } - return properties.value.substring(0, properties.start) + substitution; - }, - _multiLineIndentation: function(properties) { - var endLine = new RegExp("(?:\n|.){0," + properties.end + "}(^.*$)", "m").exec(properties.value)[1]; - return properties.value.substring(properties.value.indexOf(new RegExp("(?:\n|.){0," + properties.start + "}(^.*$)", "m").exec(properties.value)[1]), properties.value.indexOf(endLine) ? properties.value.indexOf(endLine) + endLine.length : properties.end); - }, - applyIndentation: function(properties) { - if (properties.start === properties.end) { - var line = new RegExp("(?:\n|.){0," + properties.start + "}(^.+$)", "m").exec(properties.value)[1]; - return properties.value.replace(line, "\t" + line); - } - var content = this._multiLineIndentation({ - start: properties.start, - end: properties.end, - value: properties.value - }); - return properties.value.replace(content, content.replace(/(^.+$)\n*/gim, "\t$&")); - }, - removeIndentation: function(properties) { - if (properties.start === properties.end) { - var line = new RegExp("(?:\n|.){0," + properties.start + "}(^\t.+$)", "m").exec(properties.value)[1]; - return properties.value.replace(line, line.substring(1)); - } - var content = this._multiLineIndentation({ - start: properties.start, - end: properties.end, - value: properties.value - }); - return properties.value.replace(content, content.replace(/^\t(.+)\n*$/gim, "$1")); - }, - applyDuplication: function(properties) { - if (properties.start !== properties.end) return properties.value.substring(0, properties.start) + properties.value.substring(properties.start, properties.end) + (~properties.value.charAt(properties.start - 1).indexOf("\n") || ~properties.value.charAt(properties.start).indexOf("\n") ? "\n" : "") + properties.value.substring(properties.start, properties.end) + properties.value.substring(properties.end); - var pattern = new RegExp("(?:.|\n){0,160}(^.*$)", "m"), line = ""; - properties.value.replace(pattern, function(match, p1) { - return line += p1; - }); - return properties.value.replace(line, line + "\n" + line); - } - }, - hub: function(event) { - switch (event.key) { - case this.keys.TAB: - return event.shiftKey ? this.handlers.removeTab : this.handlers.applyTab; - - case this.keys.DUPLICATE: - return event.ctrlKey || event.metaKey ? this.handlers.applyDuplication : false; - - case this.keys.INDENT: - return event.ctrlKey || event.metaKey ? this.handlers.applyIndentation : false; - - case this.keys.UNINDENT: - return event.ctrlKey || event.metaKey ? this.handlers.removeIndentation : false; - - default: - return false; - } + else if (!lineTotal) { + // Replacing `\t` within a single line selection. + substitution = + properties.value + .substring(properties.start) + .replace("\t", ''); } - }; - function getHeight(element) { - return Math.max(parseInt(window.getComputedStyle(element).height), parseInt(element.style.height) || 0); - } - function updateHeight(editor) { - if (editor.scrollTop) editor.style.height = editor.scrollTop + getHeight(editor) + "px"; - return editor; - } - var MarkdownX = function(parent, editor, preview) { - var _this = this; - var properties = { - editor: editor, - preview: preview, - parent: parent, - _latency: null, - _editorIsResizable: null - }; - var _initialize = function() { - _this.timeout = null; - var documentListeners = { - object: document, - listeners: [ { - type: "drop", - capture: false, - listener: EventHandlers.inhibitDefault - }, { - type: "dragover", - capture: false, - listener: EventHandlers.inhibitDefault - }, { - type: "dragenter", - capture: false, - listener: EventHandlers.inhibitDefault - }, { - type: "dragleave", - capture: false, - listener: EventHandlers.inhibitDefault - } ] - }, editorListeners = { - object: properties.editor, - listeners: [ { - type: "drop", - capture: false, - listener: onDrop - }, { - type: "input", - capture: true, - listener: inputChanged - }, { - type: "keydown", - capture: true, - listener: onKeyDown - }, { - type: "dragover", - capture: false, - listener: EventHandlers.onDragEnter - }, { - type: "dragenter", - capture: false, - listener: EventHandlers.onDragEnter - }, { - type: "dragleave", - capture: false, - listener: EventHandlers.inhibitDefault - }, { - type: "compositionstart", - capture: true, - listener: onKeyDown - } ] - }; - utils_1.mountEvents(editorListeners, documentListeners); - properties.editor.setAttribute("data-markdownx-init", ""); - properties.editor.style.transition = "opacity 1s ease"; - properties.editor.style.webkitTransition = "opacity 1s ease"; - properties._latency = Math.max(parseInt(properties.editor.getAttribute(LATENCY_ATTRIBUTE)) || 0, LATENCY_MINIMUM); - properties._editorIsResizable = (properties.editor.getAttribute(RESIZABILITY_ATTRIBUTE).match(/true/i) || []).length > 0 && properties.editor.offsetHeight > 0 && properties.editor.offsetWidth > 0; - getMarkdown(); - utils_1.triggerCustomEvent("markdownx.init"); - }; - var _markdownify = function() { - clearTimeout(_this.timeout); - _this.timeout = setTimeout(getMarkdown, properties._latency); - }; - var inputChanged = function() { - properties.editor = properties._editorIsResizable ? updateHeight(properties.editor) : properties.editor; - return _markdownify(); - }; - var onDrop = function(event) { - if (event.dataTransfer && event.dataTransfer.files.length) Object.keys(event.dataTransfer.files).map(function(fileKey) { - return sendFile(event.dataTransfer.files[fileKey]); - }); - EventHandlers.inhibitDefault(event); - }; - var onKeyDown = function(event) { - var handlerFunc = keyboardEvents.hub(event); - if (typeof handlerFunc != "function") return false; - EventHandlers.inhibitDefault(event); - var SELECTION_START = properties.editor.selectionStart; - properties.editor.value = handlerFunc({ - start: properties.editor.selectionStart, - end: properties.editor.selectionEnd, - value: properties.editor.value - }); - _markdownify(); - properties.editor.focus(); - properties.editor.selectionEnd = properties.editor.selectionStart = SELECTION_START; + else { + // Replacing `\t` in the beginning of each line + // in a multi-line selection. + substitution = + properties.value.substring(properties.start, properties.end).replace(/^\t/gm, '') + // Selection. + properties.value.substring(properties.end); // After the selection + } + return properties.value + .substring(0, properties.start) + // Text preceding to selection / cursor. + substitution; + }, + /** + * Handles multi line indentations. + * + * @param {JSON} properties + * @returns {string} + * @private + */ + _multiLineIndentation: function (properties) { + // Last line in the selection; regardless of + // where of not the entire line is selected. + var endLine = new RegExp("(?:\n|.){0," + properties.end + "}(^.*$)", "m") + .exec(properties.value)[1]; + // Do not replace with variables; this + // feature is optimised for swift response. + return properties.value.substring( + // First line of the selection, regardless of + // whether or not the entire line is selected. + properties.value.indexOf(new RegExp("(?:\n|.){0," + properties.start + "}(^.*$)", "m") + .exec(properties.value)[1] // Start line. + ), ( + // If there is a last line in a multi line selected + // value where the last line is not empty or `\n`: + properties.value.indexOf(endLine) ? + // Location where the last line finishes with + // respect to the entire value. + properties.value.indexOf(endLine) + endLine.length : + // Otherwise, where the selection ends. + properties.end)); + }, + /** + * Smart application of indentation at the beginning of the line. + * + * @param {JSON} properties + * @returns {string} + */ + applyIndentation: function (properties) { + // Single line? + if (properties.start === properties.end) { + // Current line, from the beginning to the end, regardless of any selections. + var line = new RegExp("(?:\n|.){0," + properties.start + "}(^.+$)", "m") + .exec(properties.value)[1]; + return properties.value.replace(line, "\t" + line); + } + // Multi line + var content = this._multiLineIndentation({ + start: properties.start, + end: properties.end, + value: properties.value + }); + return properties.value + .replace(content, // Existing contents. + content.replace(/(^.+$)\n*/gmi, "\t$&") // Indented contents. + ); + }, + /** + * Smart removal of indentation from the beginning of the line. + * + * @param {JSON} properties + * @returns {string} + */ + removeIndentation: function (properties) { + // Single Line + if (properties.start === properties.end) { + // Entire line where the line immediately begins + // with a one or more `\t`, regardless of any + // selections. + var line = new RegExp("(?:\n|.){0," + properties.start + "}(^\t.+$)", "m") + .exec(properties.value)[1]; + return properties.value + .replace(line, // Existing content. + line.substring(1) // First character (necessarily a `\t`) removed. + ); + } + // Multi line + var content = this._multiLineIndentation({ + start: properties.start, + end: properties.end, + value: properties.value + }); + return properties.value + .replace(content, // Existing content. + content.replace(/^\t(.+)\n*$/gmi, "$1") // A single `\t` removed from the beginning. + ); + }, + /** + * Duplication of the current or selected lines. + * + * @param {JSON} properties + * @returns {string} + */ + applyDuplication: function (properties) { + // With selection. + // Do not replace with variables. This + // feature is optimised for swift response. + if (properties.start !== properties.end) + return (properties.value.substring(// Text preceding the selected area. + 0, properties.start) + + properties.value.substring(// Selected area + properties.start, properties.end) + + (~properties.value // First character before the cursor is linebreak? + .charAt(properties.start - 1) + .indexOf('\n') || // --> or + ~properties.value // Character on the cursor is linebreak? + .charAt(properties.start) + .indexOf('\n') ? '\n' : '' // If either, add linebreak, otherwise add nothing. + ) + + properties.value.substring(// Selected area (again for duplication). + properties.start, properties.end) + + properties.value.substring(properties.end) // Text succeeding the selected area. + ); + // Without selection. + var pattern = new RegExp("(?:.|\n){0,160}(^.*$)", 'm'), line = ''; + // Add anything found to the `line`. Note that + // `replace` is used a simple hack; it functions + // in a similar way to `regex.search` in Python. + properties.value + .replace(pattern, function (match, p1) { return line += p1; }); + return properties.value + .replace(line, // Existing line. + line + "\n" + line // Doubled ... magic! + ); + }, + }, + /** + * Mapping of hotkeys from keyboard events to their corresponding functions. + * + * @param {KeyboardEvent} event + * @returns {Function | Boolean} + */ + hub: function (event) { + switch (event.key) { + case this.keys.TAB: // Tab. + // Shift pressed: un-indent, otherwise indent. + return event.shiftKey ? this.handlers.removeTab : this.handlers.applyTab; + case this.keys.DUPLICATE: // Line duplication. + // Is CTRL or CMD (on Mac) pressed? + return (event.ctrlKey || event.metaKey) ? this.handlers.applyDuplication : false; + case this.keys.INDENT: // Indentation. + // Is CTRL or CMD (on Mac) pressed? + return (event.ctrlKey || event.metaKey) ? this.handlers.applyIndentation : false; + case this.keys.UNINDENT: // Unindentation. + // Is CTRL or CMD (on Mac) pressed? + return (event.ctrlKey || event.metaKey) ? this.handlers.removeIndentation : false; + default: + // default would prevent the + // inhibition of default settings. return false; - }; - var sendFile = function(file) { - properties.editor.style.opacity = UPLOAD_START_OPACITY; - var xhr = new utils_1.Request(properties.editor.getAttribute(UPLOAD_URL_ATTRIBUTE), utils_1.preparePostData({ - image: file - })); - xhr.success = function(resp) { - var response = JSON.parse(resp); - if (response.image_code) { - insertImage(response.image_code); - utils_1.triggerCustomEvent("markdownx.fileUploadEnd", properties.parent, [ response ]); - } else if (response.image_path) { - insertImage('![]("' + response.image_path + '")'); - utils_1.triggerCustomEvent("markdownx.fileUploadEnd", properties.parent, [ response ]); - } else { - console.error(XHR_RESPONSE_ERROR, response); - utils_1.triggerCustomEvent("markdownx.fileUploadError", properties.parent, [ response ]); - insertImage(XHR_RESPONSE_ERROR); - } - properties.editor.style.opacity = NORMAL_OPACITY; - }; - xhr.error = function(response) { - console.error(response); - utils_1.triggerCustomEvent("fileUploadError", properties.parent, [ response ]); - insertImage(XHR_RESPONSE_ERROR); - properties.editor.style.opacity = NORMAL_OPACITY; - }; - return xhr.send(); - }; - var getMarkdown = function() { - var xhr = new utils_1.Request(properties.editor.getAttribute(PROCESSING_URL_ATTRIBUTE), utils_1.preparePostData({ - content: properties.editor.value - })); - xhr.success = function(response) { - properties.preview.innerHTML = response; - properties.editor = updateHeight(properties.editor); - utils_1.triggerCustomEvent("markdownx.update", properties.parent, [ response ]); - }; - xhr.error = function(response) { - console.error(response); - utils_1.triggerCustomEvent("markdownx.updateError", properties.parent, [ response ]); - }; - return xhr.send(); - }; - var insertImage = function(textToInsert) { - properties.editor.value = "" + properties.editor.value.substring(0, properties.editor.selectionStart) + textToInsert + ("" + properties.editor.value.substring(properties.editor.selectionEnd)); - properties.editor.selectionStart = properties.editor.selectionEnd = properties.editor.selectionStart + textToInsert.length; - utils_1.triggerEvent(properties.editor, "keyup"); - inputChanged(); - }; - _initialize(); + } + } +}; +/** + * Get either the height of an element as defined in style/CSS or its browser-computed height. + * + * @param {HTMLElement} element + * @returns {number} + */ +function getHeight(element) { + return Math.max(// Maximum of computed or set heights. + parseInt(window.getComputedStyle(element).height), // Height is not set in styles. + (parseInt(element.style.height) || 0) // Property's own height if set, otherwise 0. + ); +} +/** + * Update the height of an element based on its scroll height. + * + * @param {HTMLTextAreaElement} editor + * @returns {HTMLTextAreaElement} + */ +function updateHeight(editor) { + // Ensure that the editor is resizable before anything else. + // Change size if scroll is larger that height, otherwise do nothing. + if (editor.scrollTop) + editor.style.height = editor.scrollTop + getHeight(editor) + "px"; + return editor; +} +/** + * @example + * + * let element = document.getElementsByClassName('markdownx'); + * + * new MarkdownX( + * element, + * element.querySelector('.markdownx-editor'), + * element.querySelector('.markdownx-preview') + * ) + * + * @param {HTMLElement} parent - Markdown editor element. + * @param {HTMLTextAreaElement} editor - Markdown editor element. + * @param {HTMLElement} preview - Markdown preview element. + */ +var MarkdownX = function (parent, editor, preview) { + var _this = this; + /** + * MarkdownX properties. + */ + var properties = { + editor: editor, + preview: preview, + parent: parent, + _latency: null, + _editorIsResizable: null + }; + /** + * Initialisation settings (mounting events, retrieval of initial data, + * setting animation properties, latency, timeout, and resizability). + * + * @private + */ + var _initialize = function () { + _this.timeout = null; + // Events + // ---------------------------------------------------------------------------------------------- + var documentListeners = { + object: document, + listeners: [ + { type: "drop", capture: false, listener: EventHandlers.inhibitDefault }, + { type: "dragover", capture: false, listener: EventHandlers.inhibitDefault }, + { type: "dragenter", capture: false, listener: EventHandlers.inhibitDefault }, + { type: "dragleave", capture: false, listener: EventHandlers.inhibitDefault } + ] + }, editorListeners = { + object: properties.editor, + listeners: [ + { type: "drop", capture: false, listener: onDrop }, + { type: "input", capture: true, listener: inputChanged }, + { type: "keydown", capture: true, listener: onKeyDown }, + { type: "dragover", capture: false, listener: EventHandlers.onDragEnter }, + { type: "dragenter", capture: false, listener: EventHandlers.onDragEnter }, + { type: "dragleave", capture: false, listener: EventHandlers.inhibitDefault }, + { type: "compositionstart", capture: true, listener: onKeyDown } + ] }; - exports.MarkdownX = MarkdownX; - (function(funcName, baseObj) { - funcName = funcName || "docReady"; - baseObj = baseObj || window; - var readyList = [], readyFired = false, readyEventHandlersInstalled = false; - var ready = function() { - if (!readyFired) { - readyFired = true; - readyList.map(function(ready) { - return ready.fn.call(window, ready.ctx); - }); - readyList = []; - } - }; - var readyStateChange = function() { - return document.readyState === "complete" ? ready() : null; - }; - baseObj[funcName] = function(callback, context) { - if (readyFired) { - setTimeout(function() { - return callback(context); - }, 1); - return; - } else { - readyList.push({ - fn: callback, - ctx: context - }); - } - if (document.readyState === "complete") { - setTimeout(ready, 1); - } else if (!readyEventHandlersInstalled) { - document.addEventListener("DOMContentLoaded", ready, false); - window.addEventListener("load", ready, false); - readyEventHandlersInstalled = true; - } - }; - })("docReady", window); - docReady(function() { - var ELEMENTS = document.getElementsByClassName("markdownx"); - return Object.keys(ELEMENTS).map(function(key) { - var element = ELEMENTS[key], editor = element.querySelector(".markdownx-editor"), preview = element.querySelector(".markdownx-preview"); - if (!editor.hasAttribute("data-markdownx-init")) return new MarkdownX(element, editor, preview); + // Initialise + // -------------------------------------------------------- + // Mounting the defined events. + utils_1.mountEvents(editorListeners, documentListeners); + properties.editor.setAttribute('data-markdownx-init', ''); + // Set animation for image uploads lock down. + properties.editor.style.transition = "opacity 1s ease"; + properties.editor.style.webkitTransition = "opacity 1s ease"; + // Upload latency - must be a value >= 500 microseconds. + properties._latency = + Math.max(parseInt(properties.editor.getAttribute(LATENCY_ATTRIBUTE)) || 0, LATENCY_MINIMUM); + // If `true`, the editor will expand to scrollHeight when needed. + properties._editorIsResizable = ((properties.editor.getAttribute(RESIZABILITY_ATTRIBUTE).match(/true/i) || []).length > 0 && + properties.editor.offsetHeight > 0 && + properties.editor.offsetWidth > 0); + getMarkdown(); + utils_1.triggerCustomEvent("markdownx.init"); + }; + /** + * settings for `timeout`. + * + * @private + */ + var _markdownify = function () { + clearTimeout(_this.timeout); + _this.timeout = setTimeout(getMarkdown, properties._latency); + }; + /** + * Handling changes in the editor. + */ + var inputChanged = function () { + properties.editor = properties._editorIsResizable ? + updateHeight(properties.editor) : properties.editor; + return _markdownify(); + }; + /** + * Handling of drop events (when a file is dropped into `properties.editor`). + * + * @param {DragEvent} event + */ + var onDrop = function (event) { + if (event.dataTransfer && event.dataTransfer.files.length) + Object.keys(event.dataTransfer.files).map(function (fileKey) { + return sendFile(event.dataTransfer.files[fileKey]); }); + EventHandlers.inhibitDefault(event); + }; + /** + * Handling of keyboard events (i.e. primarily hotkeys). + * + * @param {KeyboardEvent} event + * @returns {Boolean | null} + */ + var onKeyDown = function (event) { + var handlerFunc = keyboardEvents.hub(event); + if (typeof handlerFunc != 'function') + return false; + EventHandlers.inhibitDefault(event); + // Holding the start location before anything changes. + var SELECTION_START = properties.editor.selectionStart; + properties.editor.value = handlerFunc({ + start: properties.editor.selectionStart, + end: properties.editor.selectionEnd, + value: properties.editor.value }); - }, { - "./utils": 2 - } ], - 2: [ function(require, module, exports) { - "use strict"; - Object.defineProperty(exports, "__esModule", { - value: true + _markdownify(); + properties.editor.focus(); + // Set the cursor location to the start location of the selection. + properties.editor.selectionEnd = properties.editor.selectionStart = SELECTION_START; + return false; + }; + /** + * Uploading the `file` onto the server through an AJAX request. + * + * @param {File} file + */ + var sendFile = function (file) { + properties.editor.style.opacity = UPLOAD_START_OPACITY; + var xhr = new utils_1.Request(properties.editor.getAttribute(UPLOAD_URL_ATTRIBUTE), // URL + utils_1.preparePostData({ image: file }) // Data + ); + xhr.success = function (resp) { + var response = JSON.parse(resp); + if (response.image_code) { + insertImage(response.image_code); + utils_1.triggerCustomEvent('markdownx.fileUploadEnd', properties.parent, [response]); + } + else if (response.image_path) { + // ToDo: Deprecate. + insertImage("![](\"" + response.image_path + "\")"); + utils_1.triggerCustomEvent('markdownx.fileUploadEnd', properties.parent, [response]); + } + else { + console.error(XHR_RESPONSE_ERROR, response); + utils_1.triggerCustomEvent('markdownx.fileUploadError', properties.parent, [response]); + insertImage(XHR_RESPONSE_ERROR); + } + properties.editor.style.opacity = NORMAL_OPACITY; + }; + xhr.error = function (response) { + console.error(response); + utils_1.triggerCustomEvent('fileUploadError', properties.parent, [response]); + insertImage(XHR_RESPONSE_ERROR); + properties.editor.style.opacity = NORMAL_OPACITY; + }; + return xhr.send(); + }; + /** + * Uploading the markdown text from `properties.editor` onto the server + * through an AJAX request, and upon receiving the HTML encoded text + * in response, the response will be display in `properties.preview`. + */ + var getMarkdown = function () { + var xhr = new utils_1.Request(properties.editor.getAttribute(PROCESSING_URL_ATTRIBUTE), // URL + utils_1.preparePostData({ content: properties.editor.value }) // Data + ); + xhr.success = function (response) { + properties.preview.innerHTML = response; + properties.editor = updateHeight(properties.editor); + utils_1.triggerCustomEvent('markdownx.update', properties.parent, [response]); + }; + xhr.error = function (response) { + console.error(response); + utils_1.triggerCustomEvent('markdownx.updateError', properties.parent, [response]); + }; + return xhr.send(); + }; + /** + * Inserts markdown encoded image URL into `properties.editor` where + * the cursor is located. + * + * @param textToInsert + */ + var insertImage = function (textToInsert) { + properties.editor.value = + "" + properties.editor.value.substring(0, properties.editor.selectionStart) + // Preceding text. + textToInsert + + ("" + properties.editor.value.substring(properties.editor.selectionEnd)); // Succeeding text. + properties.editor.selectionStart = + properties.editor.selectionEnd = + properties.editor.selectionStart + textToInsert.length; + utils_1.triggerEvent(properties.editor, 'keyup'); + inputChanged(); + }; + _initialize(); +}; +exports.MarkdownX = MarkdownX; +(function (funcName, baseObj) { + // The public function name defaults to window.docReady + // but you can pass in your own object and own function + // name and those will be used. + // if you want to put them in a different namespace + funcName = funcName || "docReady"; + baseObj = baseObj || window; + var readyList = [], readyFired = false, readyEventHandlersInstalled = false; + /** + * Called when the document is ready. This function protects itself + * against being called more than once. + */ + var ready = function () { + if (!readyFired) { + // Must be `true` before the callbacks are called. + readyFired = true; + // if a callback here happens to add new ready handlers, + // the docReady() function will see that it already fired + // and will schedule the callback to run right after + // this event loop finishes so all handlers will still execute + // in order and no new ones will be added to the readyList + // while we are processing the list + readyList.map(function (ready) { return ready.fn.call(window, ready.ctx); }); + // allow any closures held by these functions to free + readyList = []; + } + }; + var readyStateChange = function () { return document.readyState === "complete" ? ready() : null; }; + // This is the one public interface + // docReady(fn, context); + // the context argument is optional - if present, it will be passed + // as an argument to the callback + baseObj[funcName] = function (callback, context) { + // if ready has already fired, then just schedule the callback + // to fire asynchronously, but right away + if (readyFired) { + setTimeout(function () { return callback(context); }, 1); + return; + } + else { + // add the function and context to the list + readyList.push({ fn: callback, ctx: context }); + } + // If the document is already ready, schedule the ready + // function to run. + if (document.readyState === "complete") { + setTimeout(ready, 1); + } + else if (!readyEventHandlersInstalled) { + // otherwise if we don't have event handlers installed, + // install them first choice is DOMContentLoaded event. + document.addEventListener("DOMContentLoaded", ready, false); + // backup is window load event + window.addEventListener("load", ready, false); + readyEventHandlersInstalled = true; + } + }; +})("docReady", window); +docReady(function () { + var ELEMENTS = document.getElementsByClassName('markdownx'); + return Object.keys(ELEMENTS).map(function (key) { + var element = ELEMENTS[key], editor = element.querySelector('.markdownx-editor'), preview = element.querySelector('.markdownx-preview'); + // Only add the new MarkdownX instance to fields that have no MarkdownX instance yet. + if (!editor.hasAttribute('data-markdownx-init')) + return new MarkdownX(element, editor, preview); + }); +}); + +},{"./utils":2}],2:[function(require,module,exports){ +"use strict"; +var __spreadArrays = (this && this.__spreadArrays) || function () { + for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; + for (var r = Array(s), k = 0, i = 0; i < il; i++) + for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) + r[k] = a[j]; + return r; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +/** + * Looks for a cookie, and if found, returns the values. + * + * ... note:: Only the first item in the array is returned + * to eliminate the need for array deconstruction + * in the target. + * + * @param {string} name - The name of the cookie. + * @returns {string | null} + */ +function getCookie(name) { + if (document.cookie && document.cookie.length) { + var cookies = document.cookie + .split(';') + .filter(function (cookie) { return cookie.indexOf(name + "=") === 0; })[0]; + try { + return decodeURIComponent(cookies.trim().substring(name.length + 1)); + } + catch (e) { + if (e instanceof TypeError) { + console.info("No cookie with key \"" + name + "\". Wrong name?"); + return null; + } + throw e; + } + } + return null; +} +exports.getCookie = getCookie; +/** + * @example + * + * + * @param rows + * @returns + */ +function zip() { + var rows = []; + for (var _i = 0; _i < arguments.length; _i++) { + rows[_i] = arguments[_i]; + } + if (rows[0].constructor == Array) + return __spreadArrays(rows[0]).map(function (_, c) { return rows.map(function (row) { return row[c]; }); }); + // ToDo: To be updated to Objects.values in ECMA2017 after the method is fully ratified. + var asArray = rows.map(function (row) { return Object.keys(row).map(function (key) { return row[key]; }); }); + return __spreadArrays(asArray[0]).map(function (_, c) { return asArray.map(function (row) { return row[c]; }); }); +} +exports.zip = zip; +/** + * + * @param collections + * @returns + */ +function mountEvents() { + var collections = []; + for (var _i = 0; _i < arguments.length; _i++) { + collections[_i] = arguments[_i]; + } + return collections.map(function (events) { + return events.listeners + .map(function (series) { + return events.object + .addEventListener(series.type, series.listener, series.capture); }); - function getCookie(name) { - if (document.cookie && document.cookie.length) { - var cookies = document.cookie.split(";").filter(function(cookie) { - return cookie.indexOf(name + "=") !== -1; - })[0]; - try { - return decodeURIComponent(cookies.trim().substring(name.length + 1)); - } catch (e) { - if (e instanceof TypeError) { - console.info('No cookie with key "' + name + '". Wrong name?'); - return null; - } - throw e; + }); +} +exports.mountEvents = mountEvents; +/** + * + * @param {JSON} data + * @param {Boolean} csrf + * @returns {FormData} + */ +function preparePostData(data, csrf) { + if (csrf === void 0) { csrf = true; } + var form = new FormData(); + if (csrf) { + var csrfToken = getCookie('csrftoken'); + if (!csrfToken) + csrfToken = document.querySelector("input[name='csrfmiddlewaretoken']").value; + form.append("csrfmiddlewaretoken", csrfToken); + } + Object.keys(data).map(function (key) { return form.append(key, data[key]); }); + return form; +} +exports.preparePostData = preparePostData; +/** + * + * @returns {XMLHttpRequest} + * @throws TypeError - AJAX request is not supported. + */ +function AJAXRequest() { + // Chrome, Firefox, IE7+, Opera, Safari + // and everything else that has come post 2010. + if ("XMLHttpRequest" in window) + return new XMLHttpRequest(); + // ToDo: Deprecate. + // Other IE versions (with all their glories). + // Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is + // redundant - but you never know with Microsoft. + try { + return new ActiveXObject("Msxml2.XMLHTTP.6.0"); + } + catch (e) { } + try { + return new ActiveXObject("Msxml2.XMLHTTP.3.0"); + } + catch (e) { } + try { + return new ActiveXObject("Microsoft.XMLHTTP"); + } + catch (e) { } + // Just throw the computer outta the window! + alert("Your browser belongs to history!"); + throw new TypeError("This browser does not support AJAX requests."); +} +/** + * Handles AJAX POST requests. + */ +var Request = /** @class */ (function () { + /** + * + * @param url + * @param data + */ + function Request(url, data) { + this.xhr = AJAXRequest(); + this.url = url; + this.data = data; + } + /** + * + * @param event + */ + Request.prototype.progress = function (event) { + if (event.lengthComputable) { + // TODO: Show only when debug is on + // console.log((event.loaded / event.total) * 100 + '% uploaded'); + } + }; + /** + * + * @param response + */ + Request.prototype.error = function (response) { + console.error(response); + }; + /** + * + * @param response + */ + Request.prototype.success = function (response) { + console.info(response); + }; + /** + * + */ + Request.prototype.send = function () { + var _this = this; + var SUCCESS = this.success, ERROR = this.error, PROGRESS = this.progress; + this.xhr.open('POST', this.url, true); + this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + this.xhr.upload.onprogress = function (event) { return PROGRESS(event); }; + this.xhr.onerror = function (event) { + ERROR(_this.xhr.responseText); + }; + this.xhr.onload = function (event) { + var data = null; + if (_this.xhr.readyState == XMLHttpRequest.DONE) { + if (!_this.xhr.responseType || _this.xhr.responseType === "text") { + data = _this.xhr.responseText; + } + else if (_this.xhr.responseType === "document") { + data = _this.xhr.responseXML; + } + else { + data = _this.xhr.response; } } - return null; + SUCCESS(data); + }; + this.xhr.send(this.data); + }; + return Request; +}()); +exports.Request = Request; +/** + * + * @param {Element} element + * @param {string} type + */ +function triggerEvent(element, type) { + // modern browsers, IE9+ + var event = document.createEvent('HTMLEvents'); + event.initEvent(type, false, true); + element.dispatchEvent(event); +} +exports.triggerEvent = triggerEvent; +/** + * + * @param {string} type + * @param {Element | Document} element + * @param {any} args + */ +function triggerCustomEvent(type, element, args) { + if (element === void 0) { element = document; } + if (args === void 0) { args = null; } + // modern browsers, IE9+ + var event = new CustomEvent(type, { 'detail': args }); + element.dispatchEvent(event); +} +exports.triggerCustomEvent = triggerCustomEvent; +/** + * + * @param {Element} element + * @param {string[]} className + */ +function addClass(element) { + var className = []; + for (var _i = 1; _i < arguments.length; _i++) { + className[_i - 1] = arguments[_i]; + } + className.map(function (cname) { + if (element.classList) + element.classList.add(cname); + else { + var classes = element.className.split(' '); + if (classes.indexOf(cname) < 0) + classes.push(cname); + element.className = classes.join(' '); } - exports.getCookie = getCookie; - function zip() { - var rows = []; - for (var _i = 0; _i < arguments.length; _i++) { - rows[_i] = arguments[_i]; - } - if (rows[0].constructor == Array) return rows[0].slice().map(function(_, c) { - return rows.map(function(row) { - return row[c]; - }); - }); - var asArray = rows.map(function(row) { - return Object.keys(row).map(function(key) { - return row[key]; - }); - }); - return asArray[0].slice().map(function(_, c) { - return asArray.map(function(row) { - return row[c]; - }); - }); + }); +} +exports.addClass = addClass; +/** + * + * @param {Element} element + * @param {string[]} className + */ +function removeClass(element) { + var className = []; + for (var _i = 1; _i < arguments.length; _i++) { + className[_i - 1] = arguments[_i]; + } + className.map(function (cname) { + if (element.classList) + element.classList.remove(cname); + else { + var classes = element.className.split(' '), idx = classes.indexOf(cname); + if (idx > -1) + classes.splice(idx, 1); + element.className = classes.join(' '); } - exports.zip = zip; - function mountEvents() { - var collections = []; - for (var _i = 0; _i < arguments.length; _i++) { - collections[_i] = arguments[_i]; - } - return collections.map(function(events) { - return events.listeners.map(function(series) { - return events.object.addEventListener(series.type, series.listener, series.capture); - }); - }); - } - exports.mountEvents = mountEvents; - function preparePostData(data, csrf) { - if (csrf === void 0) { - csrf = true; - } - var form = new FormData(); - if (csrf) { - var csrfToken = getCookie("csrftoken"); - if (!csrfToken) csrfToken = document.querySelector("input[name='csrfmiddlewaretoken']").value; - form.append("csrfmiddlewaretoken", csrfToken); - } - Object.keys(data).map(function(key) { - return form.append(key, data[key]); - }); - return form; - } - exports.preparePostData = preparePostData; - function AJAXRequest() { - if ("XMLHttpRequest" in window) return new XMLHttpRequest(); - try { - return new ActiveXObject("Msxml2.XMLHTTP.6.0"); - } catch (e) {} - try { - return new ActiveXObject("Msxml2.XMLHTTP.3.0"); - } catch (e) {} - try { - return new ActiveXObject("Microsoft.XMLHTTP"); - } catch (e) {} - alert("Your browser belongs to history!"); - throw new TypeError("This browser does not support AJAX requests."); - } - var Request = function() { - function Request(url, data) { - this.xhr = AJAXRequest(); - this.url = url; - this.data = data; - } - Request.prototype.progress = function(event) { - if (event.lengthComputable) {} - }; - Request.prototype.error = function(response) { - console.error(response); - }; - Request.prototype.success = function(response) { - console.info(response); - }; - Request.prototype.send = function() { - var _this = this; - var SUCCESS = this.success, ERROR = this.error, PROGRESS = this.progress; - this.xhr.open("POST", this.url, true); - this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); - this.xhr.upload.onprogress = function(event) { - return PROGRESS(event); - }; - this.xhr.onerror = function(event) { - ERROR(_this.xhr.responseText); - }; - this.xhr.onload = function(event) { - var data = null; - if (_this.xhr.readyState == XMLHttpRequest.DONE) { - if (!_this.xhr.responseType || _this.xhr.responseType === "text") { - data = _this.xhr.responseText; - } else if (_this.xhr.responseType === "document") { - data = _this.xhr.responseXML; - } else { - data = _this.xhr.response; - } - } - SUCCESS(data); - }; - this.xhr.send(this.data); - }; - return Request; - }(); - exports.Request = Request; - function triggerEvent(element, type) { - var event = document.createEvent("HTMLEvents"); - event.initEvent(type, false, true); - element.dispatchEvent(event); - } - exports.triggerEvent = triggerEvent; - function triggerCustomEvent(type, element, args) { - if (element === void 0) { - element = document; - } - if (args === void 0) { - args = null; - } - var event = new CustomEvent(type, { - detail: args - }); - element.dispatchEvent(event); - } - exports.triggerCustomEvent = triggerCustomEvent; - function addClass(element) { - var className = []; - for (var _i = 1; _i < arguments.length; _i++) { - className[_i - 1] = arguments[_i]; - } - className.map(function(cname) { - if (element.classList) element.classList.add(cname); else { - var classes = element.className.split(" "); - if (classes.indexOf(cname) < 0) classes.push(cname); - element.className = classes.join(" "); - } - }); - } - exports.addClass = addClass; - function removeClass(element) { - var className = []; - for (var _i = 1; _i < arguments.length; _i++) { - className[_i - 1] = arguments[_i]; - } - className.map(function(cname) { - if (element.classList) element.classList.remove(cname); else { - var classes = element.className.split(" "), idx = classes.indexOf(cname); - if (idx > -1) classes.splice(idx, 1); - element.className = classes.join(" "); - } - }); - } - exports.removeClass = removeClass; - }, {} ] -}, {}, [ 1 ]); \ No newline at end of file + }); +} +exports.removeClass = removeClass; + +},{}]},{},[1]); diff --git a/markdownx/static/markdownx/js/markdownx.min.js b/markdownx/static/markdownx/js/markdownx.min.js index 781d5a1..0b5d02e 100644 --- a/markdownx/static/markdownx/js/markdownx.min.js +++ b/markdownx/static/markdownx/js/markdownx.min.js @@ -1 +1 @@ -(function e(t,r,n){function a(o,s){if(!r[o]){if(!t[o]){var u=typeof require=="function"&&require;if(!s&&u)return u(o,!0);if(i)return i(o,!0);var l=new Error("Cannot find module '"+o+"'");throw l.code="MODULE_NOT_FOUND",l}var c=r[o]={exports:{}};t[o][0].call(c.exports,function(e){var r=t[o][1][e];return a(r?r:e)},c,c.exports,e,t,r,n)}return r[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0&&e.value[e.start-1].match(/\t/)!==null?e.start-1:e.start;t=e.value.substring(e.start).replace("\t","")}else if(!r){t=e.value.substring(e.start).replace("\t","")}else{t=e.value.substring(e.start,e.end).replace(/^\t/gm,"")+e.value.substring(e.end)}return e.value.substring(0,e.start)+t},_multiLineIndentation:function(e){var t=new RegExp("(?:\n|.){0,"+e.end+"}(^.*$)","m").exec(e.value)[1];return e.value.substring(e.value.indexOf(new RegExp("(?:\n|.){0,"+e.start+"}(^.*$)","m").exec(e.value)[1]),e.value.indexOf(t)?e.value.indexOf(t)+t.length:e.end)},applyIndentation:function(e){if(e.start===e.end){var t=new RegExp("(?:\n|.){0,"+e.start+"}(^.+$)","m").exec(e.value)[1];return e.value.replace(t,"\t"+t)}var r=this._multiLineIndentation({start:e.start,end:e.end,value:e.value});return e.value.replace(r,r.replace(/(^.+$)\n*/gim,"\t$&"))},removeIndentation:function(e){if(e.start===e.end){var t=new RegExp("(?:\n|.){0,"+e.start+"}(^\t.+$)","m").exec(e.value)[1];return e.value.replace(t,t.substring(1))}var r=this._multiLineIndentation({start:e.start,end:e.end,value:e.value});return e.value.replace(r,r.replace(/^\t(.+)\n*$/gim,"$1"))},applyDuplication:function(e){if(e.start!==e.end)return e.value.substring(0,e.start)+e.value.substring(e.start,e.end)+(~e.value.charAt(e.start-1).indexOf("\n")||~e.value.charAt(e.start).indexOf("\n")?"\n":"")+e.value.substring(e.start,e.end)+e.value.substring(e.end);var t=new RegExp("(?:.|\n){0,160}(^.*$)","m"),r="";e.value.replace(t,function(e,t){return r+=t});return e.value.replace(r,r+"\n"+r)}},hub:function(e){switch(e.key){case this.keys.TAB:return e.shiftKey?this.handlers.removeTab:this.handlers.applyTab;case this.keys.DUPLICATE:return e.ctrlKey||e.metaKey?this.handlers.applyDuplication:false;case this.keys.INDENT:return e.ctrlKey||e.metaKey?this.handlers.applyIndentation:false;case this.keys.UNINDENT:return e.ctrlKey||e.metaKey?this.handlers.removeIndentation:false;default:return false}}};function v(e){return Math.max(parseInt(window.getComputedStyle(e).height),parseInt(e.style.height)||0)}function m(e){if(e.scrollTop)e.style.height=e.scrollTop+v(e)+"px";return e}var h=function(e,t,r){var v=this;var h={editor:t,preview:r,parent:e,_latency:null,_editorIsResizable:null};var g=function(){v.timeout=null;var e={object:document,listeners:[{type:"drop",capture:false,listener:f.inhibitDefault},{type:"dragover",capture:false,listener:f.inhibitDefault},{type:"dragenter",capture:false,listener:f.inhibitDefault},{type:"dragleave",capture:false,listener:f.inhibitDefault}]},t={object:h.editor,listeners:[{type:"drop",capture:false,listener:x},{type:"input",capture:true,listener:b},{type:"keydown",capture:true,listener:w},{type:"dragover",capture:false,listener:f.onDragEnter},{type:"dragenter",capture:false,listener:f.onDragEnter},{type:"dragleave",capture:false,listener:f.inhibitDefault},{type:"compositionstart",capture:true,listener:w}]};n.mountEvents(t,e);h.editor.setAttribute("data-markdownx-init","");h.editor.style.transition="opacity 1s ease";h.editor.style.webkitTransition="opacity 1s ease";h._latency=Math.max(parseInt(h.editor.getAttribute(s))||0,u);h._editorIsResizable=(h.editor.getAttribute(o).match(/true/i)||[]).length>0&&h.editor.offsetHeight>0&&h.editor.offsetWidth>0;T();n.triggerCustomEvent("markdownx.init")};var y=function(){clearTimeout(v.timeout);v.timeout=setTimeout(T,h._latency)};var b=function(){h.editor=h._editorIsResizable?m(h.editor):h.editor;return y()};var x=function(e){if(e.dataTransfer&&e.dataTransfer.files.length)Object.keys(e.dataTransfer.files).map(function(t){return E(e.dataTransfer.files[t])});f.inhibitDefault(e)};var w=function(e){var t=p.hub(e);if(typeof t!="function")return false;f.inhibitDefault(e);var r=h.editor.selectionStart;h.editor.value=t({start:h.editor.selectionStart,end:h.editor.selectionEnd,value:h.editor.value});y();h.editor.focus();h.editor.selectionEnd=h.editor.selectionStart=r;return false};var E=function(e){h.editor.style.opacity=c;var t=new n.Request(h.editor.getAttribute(a),n.preparePostData({image:e}));t.success=function(e){var t=JSON.parse(e);if(t.image_code){k(t.image_code);n.triggerCustomEvent("markdownx.fileUploadEnd",h.parent,[t])}else if(t.image_path){k('![]("'+t.image_path+'")');n.triggerCustomEvent("markdownx.fileUploadEnd",h.parent,[t])}else{console.error(l,t);n.triggerCustomEvent("markdownx.fileUploadError",h.parent,[t]);k(l)}h.editor.style.opacity=d};t.error=function(e){console.error(e);h.editor.style.opacity=d};return t.send()};var T=function(){var e=new n.Request(h.editor.getAttribute(i),n.preparePostData({content:h.editor.value}));e.success=function(e){h.preview.innerHTML=e;h.editor=m(h.editor);n.triggerCustomEvent("markdownx.update",h.parent,[e])};e.error=function(e){console.error(e);n.triggerCustomEvent("markdownx.updateError",h.parent,[e])};return e.send()};var k=function(e){h.editor.value=""+h.editor.value.substring(0,h.editor.selectionStart)+e+(""+h.editor.value.substring(h.editor.selectionEnd));h.editor.selectionStart=h.editor.selectionEnd=h.editor.selectionStart+e.length;n.triggerEvent(h.editor,"keyup");b()};g()};r.MarkdownX=h;(function(e,t){e=e||"docReady";t=t||window;var r=[],n=false,a=false;var i=function(){if(!n){n=true;r.map(function(e){return e.fn.call(window,e.ctx)});r=[]}};var o=function(){return document.readyState==="complete"?i():null};t[e]=function(e,t){if(n){setTimeout(function(){return e(t)},1);return}else{r.push({fn:e,ctx:t})}if(document.readyState==="complete"){setTimeout(i,1)}else if(!a){document.addEventListener("DOMContentLoaded",i,false);window.addEventListener("load",i,false);a=true}}})("docReady",window);docReady(function(){var e=document.getElementsByClassName("markdownx");return Object.keys(e).map(function(t){var r=e[t],n=r.querySelector(".markdownx-editor"),a=r.querySelector(".markdownx-preview");if(!n.hasAttribute("data-markdownx-init"))return new h(r,n,a)})})},{"./utils":2}],2:[function(e,t,r){"use strict";Object.defineProperty(r,"__esModule",{value:true});function n(e){if(document.cookie&&document.cookie.length){var t=document.cookie.split(";").filter(function(t){return t.indexOf(e+"=")!==-1})[0];try{return decodeURIComponent(t.trim().substring(e.length+1))}catch(t){if(t instanceof TypeError){console.info('No cookie with key "'+e+'". Wrong name?');return null}throw t}}return null}r.getCookie=n;function a(){var e=[];for(var t=0;t-1)r.splice(n,1);e.className=r.join(" ")}})}r.removeClass=f},{}]},{},[1]); \ No newline at end of file +(function(){function c(i,o,s){function u(r,e){if(!o[r]){if(!i[r]){var t="function"==typeof require&&require;if(!e&&t)return t(r,!0);if(l)return l(r,!0);var n=new Error("Cannot find module '"+r+"'");throw n.code="MODULE_NOT_FOUND",n}var a=o[r]={exports:{}};i[r][0].call(a.exports,function(e){var t=i[r][1][e];return u(t||e)},a,a.exports,c,i,o,s)}return o[r].exports}for(var l="function"==typeof require&&require,e=0;e0&&e.value[e.start-1].match(/\t/)!==null?e.start-1:e.start;t=e.value.substring(e.start).replace("\t","")}else if(!r){t=e.value.substring(e.start).replace("\t","")}else{t=e.value.substring(e.start,e.end).replace(/^\t/gm,"")+e.value.substring(e.end)}return e.value.substring(0,e.start)+t},_multiLineIndentation:function(e){var t=new RegExp("(?:\n|.){0,"+e.end+"}(^.*$)","m").exec(e.value)[1];return e.value.substring(e.value.indexOf(new RegExp("(?:\n|.){0,"+e.start+"}(^.*$)","m").exec(e.value)[1]),e.value.indexOf(t)?e.value.indexOf(t)+t.length:e.end)},applyIndentation:function(e){if(e.start===e.end){var t=new RegExp("(?:\n|.){0,"+e.start+"}(^.+$)","m").exec(e.value)[1];return e.value.replace(t,"\t"+t)}var r=this._multiLineIndentation({start:e.start,end:e.end,value:e.value});return e.value.replace(r,r.replace(/(^.+$)\n*/gim,"\t$&"))},removeIndentation:function(e){if(e.start===e.end){var t=new RegExp("(?:\n|.){0,"+e.start+"}(^\t.+$)","m").exec(e.value)[1];return e.value.replace(t,t.substring(1))}var r=this._multiLineIndentation({start:e.start,end:e.end,value:e.value});return e.value.replace(r,r.replace(/^\t(.+)\n*$/gim,"$1"))},applyDuplication:function(e){if(e.start!==e.end)return e.value.substring(0,e.start)+e.value.substring(e.start,e.end)+(~e.value.charAt(e.start-1).indexOf("\n")||~e.value.charAt(e.start).indexOf("\n")?"\n":"")+e.value.substring(e.start,e.end)+e.value.substring(e.end);var t=new RegExp("(?:.|\n){0,160}(^.*$)","m"),r="";e.value.replace(t,function(e,t){return r+=t});return e.value.replace(r,r+"\n"+r)}},hub:function(e){switch(e.key){case this.keys.TAB:return e.shiftKey?this.handlers.removeTab:this.handlers.applyTab;case this.keys.DUPLICATE:return e.ctrlKey||e.metaKey?this.handlers.applyDuplication:false;case this.keys.INDENT:return e.ctrlKey||e.metaKey?this.handlers.applyIndentation:false;case this.keys.UNINDENT:return e.ctrlKey||e.metaKey?this.handlers.removeIndentation:false;default:return false}}};function n(e){return Math.max(parseInt(window.getComputedStyle(e).height),parseInt(e.style.height)||0)}function k(e){if(e.scrollTop)e.style.height=e.scrollTop+n(e)+"px";return e}var i=function(e,t,r){var n=this;var a={editor:t,preview:r,parent:e,_latency:null,_editorIsResizable:null};var i=function(){n.timeout=null;var e={object:document,listeners:[{type:"drop",capture:false,listener:E.inhibitDefault},{type:"dragover",capture:false,listener:E.inhibitDefault},{type:"dragenter",capture:false,listener:E.inhibitDefault},{type:"dragleave",capture:false,listener:E.inhibitDefault}]},t={object:a.editor,listeners:[{type:"drop",capture:false,listener:u},{type:"input",capture:true,listener:s},{type:"keydown",capture:true,listener:l},{type:"dragover",capture:false,listener:E.onDragEnter},{type:"dragenter",capture:false,listener:E.onDragEnter},{type:"dragleave",capture:false,listener:E.inhibitDefault},{type:"compositionstart",capture:true,listener:l}]};p.mountEvents(t,e);a.editor.setAttribute("data-markdownx-init","");a.editor.style.transition="opacity 1s ease";a.editor.style.webkitTransition="opacity 1s ease";a._latency=Math.max(parseInt(a.editor.getAttribute(g))||0,y);a._editorIsResizable=(a.editor.getAttribute(m).match(/true/i)||[]).length>0&&a.editor.offsetHeight>0&&a.editor.offsetWidth>0;d();p.triggerCustomEvent("markdownx.init")};var o=function(){clearTimeout(n.timeout);n.timeout=setTimeout(d,a._latency)};var s=function(){a.editor=a._editorIsResizable?k(a.editor):a.editor;return o()};var u=function(t){if(t.dataTransfer&&t.dataTransfer.files.length)Object.keys(t.dataTransfer.files).map(function(e){return c(t.dataTransfer.files[e])});E.inhibitDefault(t)};var l=function(e){var t=T.hub(e);if(typeof t!="function")return false;E.inhibitDefault(e);var r=a.editor.selectionStart;a.editor.value=t({start:a.editor.selectionStart,end:a.editor.selectionEnd,value:a.editor.value});o();a.editor.focus();a.editor.selectionEnd=a.editor.selectionStart=r;return false};var c=function(e){a.editor.style.opacity=x;var t=new p.Request(a.editor.getAttribute(v),p.preparePostData({image:e}));t.success=function(e){var t=JSON.parse(e);if(t.image_code){f(t.image_code);p.triggerCustomEvent("markdownx.fileUploadEnd",a.parent,[t])}else if(t.image_path){f('![]("'+t.image_path+'")');p.triggerCustomEvent("markdownx.fileUploadEnd",a.parent,[t])}else{console.error(b,t);p.triggerCustomEvent("markdownx.fileUploadError",a.parent,[t]);f(b)}a.editor.style.opacity=w};t.error=function(e){console.error(e);p.triggerCustomEvent("fileUploadError",a.parent,[e]);f(b);a.editor.style.opacity=w};return t.send()};var d=function(){var e=new p.Request(a.editor.getAttribute(h),p.preparePostData({content:a.editor.value}));e.success=function(e){a.preview.innerHTML=e;a.editor=k(a.editor);p.triggerCustomEvent("markdownx.update",a.parent,[e])};e.error=function(e){console.error(e);p.triggerCustomEvent("markdownx.updateError",a.parent,[e])};return e.send()};var f=function(e){a.editor.value=""+a.editor.value.substring(0,a.editor.selectionStart)+e+(""+a.editor.value.substring(a.editor.selectionEnd));a.editor.selectionStart=a.editor.selectionEnd=a.editor.selectionStart+e.length;p.triggerEvent(a.editor,"keyup");s()};i()};r.MarkdownX=i;(function(e,t){e=e||"docReady";t=t||window;var r=[],n=false,a=false;var i=function(){if(!n){n=true;r.map(function(e){return e.fn.call(window,e.ctx)});r=[]}};var o=function(){return document.readyState==="complete"?i():null};t[e]=function(e,t){if(n){setTimeout(function(){return e(t)},1);return}else{r.push({fn:e,ctx:t})}if(document.readyState==="complete"){setTimeout(i,1)}else if(!a){document.addEventListener("DOMContentLoaded",i,false);window.addEventListener("load",i,false);a=true}}})("docReady",window);docReady(function(){var a=document.getElementsByClassName("markdownx");return Object.keys(a).map(function(e){var t=a[e],r=t.querySelector(".markdownx-editor"),n=t.querySelector(".markdownx-preview");if(!r.hasAttribute("data-markdownx-init"))return new i(t,r,n)})})},{"./utils":2}],2:[function(e,t,r){"use strict";var a=this&&this.__spreadArrays||function(){for(var e=0,t=0,r=arguments.length;t-1)t.splice(r,1);n.className=t.join(" ")}})}r.removeClass=p},{}]},{},[1]); \ No newline at end of file diff --git a/static-src/markdownx/js/utils.ts b/static-src/markdownx/js/utils.ts index 5293576..dcc8284 100644 --- a/static-src/markdownx/js/utils.ts +++ b/static-src/markdownx/js/utils.ts @@ -40,7 +40,7 @@ export function getCookie (name: string): string | null { const cookies: string = document.cookie .split(';') - .filter(cookie => cookie.indexOf(`${name}=`) !== -1)[0]; + .filter(cookie => cookie.indexOf(`${name}=`) === 0)[0]; try{