UNPKG

tui-editor

Version:

GFM Markdown Wysiwyg Editor - Productive and Extensible

7 lines 12.9 kB
/*! * tui-editor * @version 1.4.10 * @author NHN FE Development Lab <dl_javascript@nhn.com> (https://nhn.github.io/tui.editor/) * @license MIT */ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("jquery"),require("tui-code-snippet"),function(){try{return require("tui-editor")}catch(e){}}(),function(){try{return require("tui-editor/dist/tui-editor-Viewer")}catch(e){}}());else if("function"==typeof define&&define.amd)define(["jquery","tui-code-snippet","tui-editor","tui-editor/dist/tui-editor-Viewer"],t);else{var i="object"==typeof exports?t(require("jquery"),require("tui-code-snippet"),function(){try{return require("tui-editor")}catch(e){}}(),function(){try{return require("tui-editor/dist/tui-editor-Viewer")}catch(e){}}()):t(e.$,e.tui.util,e.tui.Editor,e.tui.Editor);for(var o in i)("object"==typeof exports?exports:e)[o]=i[o]}}(window,(function(e,t,i,o){return function(e){var t={};function i(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,i),n.l=!0,n.exports}return i.m=e,i.c=t,i.d=function(e,t,o){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(i.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)i.d(o,n,function(t){return e[t]}.bind(null,n));return o},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="/dist",i(i.s=47)}({0:function(t,i){t.exports=e},1:function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=void 0;try{o=i(4)}catch(e){}if(!o)try{o=i(5)}catch(e){}t.default=o},2:function(e,i){e.exports=t},4:function(e,t){if(void 0===i){var o=new Error("Cannot find module 'undefined'");throw o.code="MODULE_NOT_FOUND",o}e.exports=i},47:function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o=l(i(0)),n=l(i(1)),r=l(i(48)),c=l(i(49));function l(e){return e&&e.__esModule?e:{default:e}}function s(e){var t=e.i18n,i={active:t.get("Auto scroll enabled"),inactive:t.get("Auto scroll disabled")};if(!e.isViewer()){var n=e.getCodeMirror(),l=new c.default(n,e.preview),s=new r.default(l,n,e.preview.$el),a=!1,u=!0,d=void 0,f=void 0;if("default"===e.getUI().name){var v=e.getUI().getToolbar();v.addItem("divider"),v.addItem({type:"button",options:{className:"tui-scrollsync",command:"scrollSyncToggle",tooltip:i.active,$el:(0,o.default)('<button class="active tui-scrollsync" type="button"></button>')}});var h=v.getItems();f=h[h.length-2].$el,d=h[h.length-1],p(),e.on("changeMode",p),e.on("changePreviewStyle",p),e.addCommand("markdown",{name:"scrollSyncToggle",exec:function(){u=!u,d._onOut(),u?(d.$el.addClass("active"),d.setTooltip(i.active)):(d.$el.removeClass("active"),d.setTooltip(i.inactive)),d._onOver()}})}n.on("change",(function(){a=!1,l.makeSectionList()})),e.on("previewRenderAfter",(function(){setTimeout((function(){l.sectionMatch(),u&&s.syncPreviewScrollTopToMarkdown(!0),a=!0}),200)})),e.eventManager.listen("scroll",(function(t){u&&(a&&e.preview.isVisible()?"markdown"!==t.source||s.isMarkdownScrollEventBlocked?"preview"!==t.source||s.isPreviewScrollEventBlocked||s.syncMarkdownScrollTopToPreview():s.syncPreviewScrollTopToMarkdown(!1):s.saveScrollInfo())}))}function p(){"vertical"===e.mdPreviewStyle&&"markdown"===e.currentMode?(d.$el.show(),f.show()):(d.$el.hide(),f.hide())}}n.default.defineExtension("scrollSync",s),t.default=s},48:function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o,n=function(){function e(e,t){for(var i=0;i<t.length;i++){var o=t[i];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,i,o){return i&&e(t.prototype,i),o&&e(t,o),t}}(),r=i(2),c=(o=r)&&o.__esModule?o:{default:o};var l=function(){function e(t,i,o){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.sectionManager=t,this.cm=i,this.$previewContainerEl=o,this.$contents=this.$previewContainerEl.find(".tui-editor-contents"),this.releaseTimer=null,this._currentTimeoutId=null,this._savedScrollInfo=null}return n(e,[{key:"_getEditorSectionHeight",value:function(e){var t=this.cm.heightAtLine(e.end,"local");return t-=this.cm.heightAtLine(e.start>0?e.start-1:0,"local")}},{key:"_getEditorLineHeightGapInSection",value:function(e,t){var i=this.cm.heightAtLine(t,"local");return i-=this.cm.heightAtLine(e.start>0?e.start-1:0,"local"),Math.max(i,0)}},{key:"_getEditorSectionScrollRatio",value:function(e,t){return e.end===e.start?0:this._getEditorLineHeightGapInSection(e,t)/this._getEditorSectionHeight(e)}},{key:"_getScrollFactorsOfEditor",value:function(){var e=this.cm,t=e.getScrollInfo(),i=void 0,o=void 0,n=void 0,r=(t=this._fallbackScrollInfoIfIncorrect(t)).height-t.top<=t.clientHeight;return r?n={isEditorBottom:r}:(i=e.coordsChar({left:t.left,top:t.top},"local").line,n={section:o=this.sectionManager.sectionByLine(i),sectionRatio:this._getEditorSectionScrollRatio(o,i)}),n}},{key:"_getCursorFactorsOfEditor",value:function(){var e=this.cm,t=e.cursorCoords(!0,"local"),i=this._fallbackScrollInfoIfIncorrect(e.getScrollInfo()),o=e.coordsChar({left:t.left,top:t.top},"local").line,n=this.sectionManager.sectionByLine(o);return n&&n.$previewSectionEl&&n.$previewSectionEl.length?{section:n,sectionRatio:this._getEditorSectionScrollRatio(n,o),relativeCursorTop:t.top-i.top}:null}},{key:"_getScrollInfoForMarkdown",value:function(){var e=this,t=this.sectionManager.getSectionList(),i=void 0;return c.default.forEachArray(t,(function(t){var o=t.$previewSectionEl,n=o.parent().parent(),r=n[0].clientHeight-n.scrollTop()<=n[0].height,c=!0;return r?(i={isPreviewBottom:r},c=!1):e._isTopSection(n,o)&&(i={section:t,sectionRatio:e._getMarkdownEditorScrollRatio(n,o)},c=!1),c})),i}},{key:"_getMarkdownEditorScrollRatio",value:function(e,t){return(e.scrollTop()-t[0].offsetTop)/t.height()}},{key:"_getScrollTopForPreview",value:function(){var e=void 0,t=this._getScrollFactorsOfEditor(),i=t.section,o=t.sectionRatio;return t.isEditorBottom?e=this.$contents.height():i.$previewSectionEl&&(e=i.$previewSectionEl[0].offsetTop,e+=i.$previewSectionEl.height()*o-20),e=e&&Math.max(e,0)}},{key:"_getScrollTopForPreviewBaseCursor",value:function(){var e=this._getCursorFactorsOfEditor();if(!e)return 0;var t=e.section,i=e.sectionRatio,o=e.relativeCursorTop,n=t.$previewSectionEl[0].offsetTop;return n+=t.$previewSectionEl.height()*i-20,n=(n-=o)&&Math.max(n,0)}},{key:"_getScrollTopForMarkdown",value:function(){var e=void 0,t=this._getScrollInfoForMarkdown(),i=t.sectionRatio;if(t.isPreviewBottom)e=this.cm.getScrollInfo().height;else if(t.section){var o=t.section,n=this.cm.charCoords({line:o.start,char:0},"local"),r=this.cm.charCoords({line:o.end,char:0},"local");e=n.top,e+=(r.top-n.top)*i}return e=e&&Math.max(e,0)}},{key:"syncPreviewScrollTopToMarkdown",value:function(e){var t=this,i=this.$previewContainerEl,o=i.scrollTop(),n=e?this._getScrollTopForPreviewBaseCursor():this._getScrollTopForPreview();this.isPreviewScrollEventBlocked=!0,this._animateRun(o,n,(function(e){clearTimeout(t.releaseTimer),i.scrollTop(e),t.releaseTimer=setTimeout((function(){t.isPreviewScrollEventBlocked=!1}),15)}))}},{key:"syncMarkdownScrollTopToPreview",value:function(){var e=this,t=this.cm,i=t.getScrollInfo().top,o=this._getScrollTopForMarkdown();this.isMarkdownScrollEventBlocked=!0,this._animateRun(i,o,(function(i){clearTimeout(e.releaseTimer),t.scrollTo(0,i),e.releaseTimer=setTimeout((function(){e.isMarkdownScrollEventBlocked=!1}),15)}))}},{key:"_animateRun",value:function(e,t,i){var o=t-e,n=Date.now(),r=this;this._currentTimeoutId&&clearTimeout(this._currentTimeoutId),function c(){var l=(Date.now()-n)/200,s=void 0;l<1?(s=e+o*Math.cos((1-l)*Math.PI/2),i(Math.ceil(s)),r._currentTimeoutId=setTimeout(c,1)):(i(t),r._currentTimeoutId=null)}()}},{key:"_fallbackScrollInfoIfIncorrect",value:function(e){return e.height<0&&this._savedScrollInfo?this._savedScrollInfo:e}},{key:"saveScrollInfo",value:function(){this._savedScrollInfo=this.cm.getScrollInfo()}},{key:"_isTopSection",value:function(e,t){var i=e.scrollTop(),o=t[0].offsetTop,n=t.height();return i>=o-57&&!(i>o+n)}}]),e}();t.default=l},49:function(e,t,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o,n=function(){function e(e,t){for(var i=0;i<t.length;i++){var o=t[i];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,i,o){return i&&e(t.prototype,i),o&&e(t,o),t}}(),r=i(0),c=(o=r)&&o.__esModule?o:{default:o};var l=/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/,s=/^ *(\*|-|\d+\.|[*-] \[[ xX]])\s/,a=/^ {0,3}(> ?)+\s/,u=/^ {0,3}!\[([^[\]]*)]\(([^)]*)\)/,d=/^ *(?:={1,}|-{1,})\s*$/,f=/^ *(`{3,}|~{3,})[ ]*$/,v=/^ *(`{3,}|~{3,})[ .]*(\S+)? */,h=/\s/g,p=function(){function e(t,i){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.cm=t,this.preview=i,this.$previewContent=i.$el.find(".tui-editor-contents"),this._sectionList=null,this._currentSection=null}return n(e,[{key:"_addNewSection",value:function(e,t){var i=this._makeSectionData(e,t);this._sectionList.push(i),this._currentSection=i}},{key:"getSectionList",value:function(){return this._sectionList||this.makeSectionList(),this._sectionList}},{key:"_makeSectionData",value:function(e,t){return{start:e,end:t,$previewSectionEl:null}}},{key:"_updateCurrentSectionEnd",value:function(e){this._currentSection.end=e}},{key:"_eachLineState",value:function(e){var t=void 0,i=void 0,o=void 0,n=void 0,r=void 0,c=!0,l=!1,s=!1,a="",u=!1,d=!1,f=void 0,v=this.cm.getDoc().lineCount();for(i=0;i<v;i+=1){t=!1,o=this.cm.getLine(i),n=this.cm.getLine(i+1)||"",r=this.cm.getLine(i-1)||"";var h=this._isCodeBlockEnd(r)&&f!==i-1;if(!l||o&&this._isTableCode(o)?!l&&this._isTable(o,n)&&(l=!0):l=!1,s&&h&&(s=!1),!s&&this._isCodeBlockStart(o)&&(s=this._doFollowedLinesHaveCodeBlockEnd(i,v),f=i),d&&0!==o.length?(this._isIndependentImage(s,l,o,r)?(u=!0,d=!0):(u=!1,d=!1),t=!0):!s&&this._isAtxHeader(o)?(u=!1,t=!0,d=!1):this._isCodeBlockEnd(o)||l||!this._isSeTextHeader(o,n)?this._isIndependentImage(s,l,o,r)?(u=!0,t=!0,d=!1):u&&0===o.length&&(u=!1,d=!0):(u=!1,t=!0,d=!1),c){if(!(a+=o.trim()))continue;c=!1}e(t,i)}}},{key:"_isIndependentImage",value:function(e,t,i,o){return!e&&!t&&this._isImage(i)&&!this._isList(i)&&!this._isQuote(i)&&0===o.length}},{key:"_doFollowedLinesHaveCodeBlockEnd",value:function(e,t){for(var i=!1,o=e+1;o<t;o+=1)if(this._isCodeBlockEnd(this.cm.getLine(o))){i=!0;break}return i}},{key:"_isCodeBlockStart",value:function(e){return v.test(e)}},{key:"_isCodeBlockEnd",value:function(e){return f.test(e)}},{key:"_isTable",value:function(e,t){return this._isTableCode(e)&&this._isTableAligner(t)}},{key:"_isTableCode",value:function(e){return/(^\S?.*\|.*)/.test(e)}},{key:"_isTableAligner",value:function(e){return/(\s*[-:]+\s*\|)+/.test(e)}},{key:"_isAtxHeader",value:function(e){return l.test(e)}},{key:"_isSeTextHeader",value:function(e,t){return""!==e.replace(h,"")&&!this._isQuote(e)&&t&&d.test(t)}},{key:"_isImage",value:function(e){return u.test(e)}},{key:"_isList",value:function(e){return s.test(e)}},{key:"_isQuote",value:function(e){return a.test(e)}},{key:"makeSectionList",value:function(){var e=this;this._sectionList=[],this._eachLineState((function(t,i){t||!e._sectionList.length?e._addNewSection(i,i):e._updateCurrentSectionEnd(i)}))}},{key:"sectionMatch",value:function(){if(this.getSectionList()){var e=this._getPreviewSections();this._matchPreviewSectionsWithSectionlist(e)}}},{key:"_matchPreviewSectionsWithSectionlist",value:function(e){var t=this.getSectionList();e.forEach((function(e,i){var o=t[i];if(o){var n=(0,c.default)("<div class='content-id-"+i+"'></div>");o.$previewSectionEl=(0,c.default)(e).wrapAll(n).parent()}}))}},{key:"_getPreviewSections",value:function(){var e=[],t=0,i=!1;return e[0]=[],this.$previewContent.contents().filter(_).each((function(o,n){var r="P"===n.tagName,c=n.tagName.match(/^(H1|H2|H3|H4|H5|H6)$/),l=r&&n.hasChildNodes()&&"IMG"===n.childNodes[0].nodeName;(c||l||i)&&e[t].length&&(e.push([]),t+=1,i=!1),l&&(i=!0),e[t].push(n)})),e}},{key:"sectionByLine",value:function(e){var t=void 0,i=this.getSectionList(),o=i.length;for(t=0;t<o&&!(e<=i[t].end);t+=1);return t===o&&(t=o-1),i[t]}}]),e}();function _(){return this.nodeType===Node.ELEMENT_NODE}t.default=p},5:function(e,t){if(void 0===o){var i=new Error("Cannot find module 'undefined'");throw i.code="MODULE_NOT_FOUND",i}e.exports=o}})}));