tui-editor
Version:
GFM Markdown Wysiwyg Editor - Productive and Extensible
7 lines • 12.6 kB
JavaScript
/*!
* tui-editor
* @version 1.4.6
* @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&&e.tui.util,e.tui&&e.tui.Editor,e.tui&&e.tui.Editor);for(var o in i)("object"==typeof exports?exports:e)[o]=i[o]}}("undefined"!=typeof self?self:this,function(e,t,i,o){return function(e){function t(o){if(i[o])return i[o].exports;var n=i[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var i={};return t.m=e,t.c=i,t.d=function(e,i,o){t.o(e,i)||Object.defineProperty(e,i,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var i=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(i,"a",i),i},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="dist/",t(t.s=46)}({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},46:function(e,t,i){"use strict";function o(e){return e&&e.__esModule?e:{default:e}}function n(e){function t(){"vertical"===e.mdPreviewStyle&&"markdown"===e.currentMode?(d.$el.show(),h.show()):(d.$el.hide(),h.hide())}var i=e.i18n,o={active:i.get("Auto scroll enabled"),inactive:i.get("Auto scroll disabled")};if(!e.isViewer()){var n=e.getCodeMirror(),r=new f.default(n,e.preview),l=new u.default(r,n,e.preview.$el),s=!1,a=!0,d=void 0,h=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:o.active,$el:(0,c.default)('<button class="active tui-scrollsync" type="button"></button>')}});var p=v.getItems();h=p[p.length-2].$el,d=p[p.length-1],t(),e.on("changeMode",t),e.on("changePreviewStyle",t),e.addCommand("markdown",{name:"scrollSyncToggle",exec:function(){a=!a,d._onOut(),a?(d.$el.addClass("active"),d.setTooltip(o.active)):(d.$el.removeClass("active"),d.setTooltip(o.inactive)),d._onOver()}})}n.on("change",function(){s=!1,r.makeSectionList()}),e.on("previewRenderAfter",function(){setTimeout(function(){r.sectionMatch(),a&&l.syncPreviewScrollTopToMarkdown(!0),s=!0},200)}),e.eventManager.listen("scroll",function(t){a&&(s&&e.preview.isVisible()?"markdown"!==t.source||l.isMarkdownScrollEventBlocked?"preview"!==t.source||l.isPreviewScrollEventBlocked||l.syncMarkdownScrollTopToPreview():l.syncPreviewScrollTopToMarkdown(!1):l.saveScrollInfo())})}}Object.defineProperty(t,"__esModule",{value:!0});var r=i(0),c=o(r),l=i(1),s=o(l),a=i(47),u=o(a),d=i(48),f=o(d);s.default.defineExtension("scrollSync",n),t.default=n},47:function(e,t,i){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var 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=function(e){return e&&e.__esModule?e:{default:e}}(r),l=function(){function e(t,i,n){o(this,e),this.sectionManager=t,this.cm=i,this.$previewContainerEl=n,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){var i=e.end===e.start;return i?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=void 0;t=this._fallbackScrollInfoIfIncorrect(t);var c=t.height-t.top<=t.clientHeight;return c?r={isEditorBottom:c}:(i=e.coordsChar({left:t.left,top:t.top},"local").line,o=this.sectionManager.sectionByLine(i),n=this._getEditorSectionScrollRatio(o,i),r={section:o,sectionRatio:n}),r}},{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);if(n&&n.$previewSectionEl&&n.$previewSectionEl.length){return{section:n,sectionRatio:this._getEditorSectionScrollRatio(n,o),relativeCursorTop:t.top-i.top}}return 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-=o,n=n&&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(),o=i.top,n=this._getScrollTopForMarkdown();this.isMarkdownScrollEventBlocked=!0,this._animateRun(o,n,function(i){clearTimeout(e.releaseTimer),t.scrollTo(0,i),e.releaseTimer=setTimeout(function(){e.isMarkdownScrollEventBlocked=!1},15)})}},{key:"_animateRun",value:function(e,t,i){function o(){var l=Date.now(),s=(l-r)/200,a=void 0;s<1?(a=e+n*Math.cos((1-s)*Math.PI/2),i(Math.ceil(a)),c._currentTimeoutId=setTimeout(o,1)):(i(t),c._currentTimeoutId=null)}var n=t-e,r=Date.now(),c=this;this._currentTimeoutId&&clearTimeout(this._currentTimeoutId),o()}},{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(),r=i>=o-57,c=i>o+n;return r&&!c}}]),e}();t.default=l},48:function(e,t,i){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(){return this.nodeType===Node.ELEMENT_NODE}Object.defineProperty(t,"__esModule",{value:!0});var r=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}}(),c=i(0),l=function(e){return e&&e.__esModule?e:{default:e}}(c),s=/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/,a=/^ *(\*|-|\d+\.|[*-] \[[ xX]])\s/,u=/^ {0,3}(> ?)+\s/,d=/^ {0,3}!\[([^[\]]*)]\(([^)]*)\)/,f=/^ *(?:={1,}|-{1,})\s*$/,h=/^ *(`{3,}|~{3,})[ ]*$/,v=/^ *(`{3,}|~{3,})[ .]*(\S+)? */,p=/\s/g,_=function(){function e(t,i){o(this,e),this.cm=t,this.preview=i,this.$previewContent=i.$el.find(".tui-editor-contents"),this._sectionList=null,this._currentSection=null}return r(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,h=this.cm.getDoc().lineCount();for(i=0;i<h;i+=1){t=!1,o=this.cm.getLine(i),n=this.cm.getLine(i+1)||"",r=this.cm.getLine(i-1)||"";var v=this._isCodeBlockEnd(r)&&f!==i-1;if(!l||o&&this._isTableCode(o)?!l&&this._isTable(o,n)&&(l=!0):l=!1,s&&v&&(s=!1),!s&&this._isCodeBlockStart(o)&&(s=this._doFollowedLinesHaveCodeBlockEnd(i,h),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 h.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 s.test(e)}},{key:"_isSeTextHeader",value:function(e,t){return""!==e.replace(p,"")&&!this._isQuote(e)&&t&&f.test(t)}},{key:"_isImage",value:function(e){return d.test(e)}},{key:"_isList",value:function(e){return a.test(e)}},{key:"_isQuote",value:function(e){return u.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,l.default)("<div class='content-id-"+i+"'></div>");o.$previewSectionEl=(0,l.default)(e).wrapAll(n).parent()}})}},{key:"_getPreviewSections",value:function(){var e=[],t=0,i=!1;return e[0]=[],this.$previewContent.contents().filter(n).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}();t.default=_},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}})});