From 50c730b9a446f7a1542eadb7b000e2d1e9212d1a Mon Sep 17 00:00:00 2001 From: Sebastiaan ten Pas Date: Fri, 7 Jul 2017 11:46:15 +0200 Subject: [PATCH] Add data-markdownx-init check to typescript file --- markdownx/static/markdownx/js/markdownx.js | 3 ++- .../static/markdownx/js/markdownx.min.js | 2 +- static-src/markdownx/js/markdownx.ts | 21 ++++++++++++++----- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/markdownx/static/markdownx/js/markdownx.js b/markdownx/static/markdownx/js/markdownx.js index 6dba2a5..bff245f 100644 --- a/markdownx/static/markdownx/js/markdownx.js +++ b/markdownx/static/markdownx/js/markdownx.js @@ -363,8 +363,8 @@ var MarkdownX = function (parent, editor, preview) { // -------------------------------------------------------- // Mounting the defined events. utils_1.mountEvents(editorListeners, documentListeners); - // Set animation for image uploads lock down. 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. @@ -566,6 +566,7 @@ exports.MarkdownX = MarkdownX; docReady(function () { var ELEMENTS = document.getElementsByClassName('markdownx'); return Object.keys(ELEMENTS).map(function (key) { + // Only add the new MarkdownX instance to fields that have no MarkdownX instance yet if (!ELEMENTS[key].querySelector('.markdownx-editor').hasAttribute('data-markdownx-init')) { return new MarkdownX(ELEMENTS[key], ELEMENTS[key].querySelector('.markdownx-editor'), ELEMENTS[key].querySelector('.markdownx-preview')); } diff --git a/markdownx/static/markdownx/js/markdownx.min.js b/markdownx/static/markdownx/js/markdownx.min.js index fc03fb7..1634273 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 h(e){if(e.scrollTop)e.style.height=e.scrollTop+v(e)+"px";return e}var m=function(e,t,r){var v=this;var m={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:m.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);m.editor.style.transition="opacity 1s ease";m.editor.style.webkitTransition="opacity 1s ease";m._latency=Math.max(parseInt(m.editor.getAttribute(s))||0,u);m._editorIsResizable=(m.editor.getAttribute(o).match(/true/i)||[]).length>0&&m.editor.offsetHeight>0&&m.editor.offsetWidth>0;T();n.triggerCustomEvent("markdownx.init")};var y=function(){clearTimeout(v.timeout);v.timeout=setTimeout(T,m._latency)};var b=function(){m.editor=m._editorIsResizable?h(m.editor):m.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=m.editor.selectionStart;m.editor.value=t({start:m.editor.selectionStart,end:m.editor.selectionEnd,value:m.editor.value});y();m.editor.focus();m.editor.selectionEnd=m.editor.selectionStart=r;return false};var E=function(e){m.editor.style.opacity=c;var t=new n.Request(m.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",m.parent,[t])}else if(t.image_path){k('![]("'+t.image_path+'")');n.triggerCustomEvent("markdownx.fileUploadEnd",m.parent,[t])}else{console.error(l,t);n.triggerCustomEvent("markdownx.fileUploadError",m.parent,[t]);return null}m.editor.style.opacity=d};t.error=function(e){m.editor.style.opacity=d;console.error(e);n.triggerCustomEvent("fileUploadError",m.parent,[e])};return t.send()};var T=function(){var e=new n.Request(m.editor.getAttribute(i),n.preparePostData({content:m.editor.value}));e.success=function(e){m.preview.innerHTML=e;m.editor=h(m.editor);n.triggerCustomEvent("markdownx.update",m.parent,[e])};e.error=function(e){console.error(e);n.triggerCustomEvent("markdownx.updateError",m.parent,[e])};return e.send()};var k=function(e){m.editor.value=m.editor.value.substring(0,m.editor.selectionStart)+"\n\n"+e+("\n\n"+m.editor.value.substring(m.editor.selectionEnd));m.editor.selectionStart=m.editor.selectionEnd=m.editor.selectionStart+e.length;n.triggerEvent(m.editor,"keyup");b()};g()};r.MarkdownX=m;(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){return new m(e[t],e[t].querySelector(".markdownx-editor"),e[t].querySelector(".markdownx-preview"))})})},{"./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 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]);return null}h.editor.style.opacity=d};t.error=function(e){h.editor.style.opacity=d;console.error(e);n.triggerCustomEvent("fileUploadError",h.parent,[e])};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)+"\n\n"+e+("\n\n"+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){if(!e[t].querySelector(".markdownx-editor").hasAttribute("data-markdownx-init")){return new h(e[t],e[t].querySelector(".markdownx-editor"),e[t].querySelector(".markdownx-preview"))}})})},{"./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 diff --git a/static-src/markdownx/js/markdownx.ts b/static-src/markdownx/js/markdownx.ts index 93a4db0..8340a10 100644 --- a/static-src/markdownx/js/markdownx.ts +++ b/static-src/markdownx/js/markdownx.ts @@ -534,6 +534,8 @@ const MarkdownX = function (parent: HTMLElement, editor: HTMLTextAreaElement, pr // Mounting the defined events. 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"; @@ -828,11 +830,20 @@ docReady(() => { const ELEMENTS = document.getElementsByClassName('markdownx'); - return Object.keys(ELEMENTS).map(key => new MarkdownX( - ELEMENTS[key], - ELEMENTS[key].querySelector('.markdownx-editor'), - ELEMENTS[key].querySelector('.markdownx-preview') - )); + return Object.keys(ELEMENTS).map(key => { + + // Only add the new MarkdownX instance to fields that have no MarkdownX instance yet + if (!ELEMENTS[key].querySelector('.markdownx-editor').hasAttribute('data-markdownx-init')) { + + return new MarkdownX( + ELEMENTS[key], + ELEMENTS[key].querySelector('.markdownx-editor'), + ELEMENTS[key].querySelector('.markdownx-preview') + ) + + } + + }); });