editormd.js.html 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: editormd.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: editormd.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/*
  20. * Editor.md
  21. *
  22. * @file editormd.js
  23. * @version v1.4.5
  24. * @description Open source online markdown editor.
  25. * @license MIT License
  26. * @author Pandao
  27. * {@link https://github.com/pandao/editor.md}
  28. * @updateTime 2015-06-02
  29. */
  30. ;(function(factory) {
  31. "use strict";
  32. // CommonJS/Node.js
  33. if (typeof require === "function" &amp;&amp; typeof exports === "object" &amp;&amp; typeof module === "object")
  34. {
  35. module.exports = factory;
  36. }
  37. else if (typeof define === "function") // AMD/CMD/Sea.js
  38. {
  39. if (define.amd) // for Require.js
  40. {
  41. /* Require.js define replace */
  42. }
  43. else
  44. {
  45. define(["jquery"], factory); // for Sea.js
  46. }
  47. }
  48. else
  49. {
  50. window.editormd = factory();
  51. }
  52. }(function() {
  53. /* Require.js assignment replace */
  54. "use strict";
  55. var $ = (typeof (jQuery) !== "undefined") ? jQuery : Zepto;
  56. if (typeof ($) === "undefined") {
  57. return ;
  58. }
  59. /**
  60. * editormd
  61. *
  62. * @param {String} id 编辑器的ID
  63. * @param {Object} options 配置选项 Key/Value
  64. * @returns {Object} editormd 返回editormd对象
  65. */
  66. var editormd = function (id, options) {
  67. return new editormd.fn.init(id, options);
  68. };
  69. editormd.title = editormd.$name = "Editor.md";
  70. editormd.version = "1.4.5";
  71. editormd.homePage = "https://pandao.github.io/editor.md/";
  72. editormd.classPrefix = "editormd-";
  73. editormd.toolbarModes = {
  74. full : [
  75. "undo", "redo", "|",
  76. "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
  77. "h1", "h2", "h3", "h4", "h5", "h6", "|",
  78. "list-ul", "list-ol", "hr", "|",
  79. "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|",
  80. "goto-line", "watch", "preview", "fullscreen", "clear", "search", "|",
  81. "help", "info"
  82. ],
  83. simple : [
  84. "undo", "redo", "|",
  85. "bold", "del", "italic", "quote", "uppercase", "lowercase", "|",
  86. "h1", "h2", "h3", "h4", "h5", "h6", "|",
  87. "list-ul", "list-ol", "hr", "|",
  88. "watch", "preview", "fullscreen", "|",
  89. "help", "info"
  90. ],
  91. mini : [
  92. "undo", "redo", "|",
  93. "watch", "preview", "|",
  94. "help", "info"
  95. ]
  96. };
  97. editormd.defaults = {
  98. mode : "gfm", //gfm or markdown
  99. theme : "default",
  100. name : "",
  101. value : "", // value for CodeMirror, if mode not gfm/markdown
  102. markdown : "",
  103. appendMarkdown : "", // if in init textarea value not empty, append markdown to textarea
  104. width : "100%",
  105. height : "100%",
  106. path : "./lib/", // Dependents module file directory
  107. pluginPath : "", // If this empty, default use settings.path + "../plugins/"
  108. delay : 300, // Delay parse markdown to html, Uint : ms
  109. autoLoadModules : true, // Automatic load dependent module files
  110. watch : true,
  111. placeholder : "Enjoy Markdown! coding now...",
  112. gotoLine : true,
  113. codeFold : false,
  114. autoHeight : false,
  115. autoFocus : true,
  116. autoCloseTags : true,
  117. searchReplace : true,
  118. syncScrolling : true,
  119. readOnly : false,
  120. tabSize : 4,
  121. indentUnit : 4,
  122. lineNumbers : true,
  123. lineWrapping : true,
  124. autoCloseBrackets : true,
  125. showTrailingSpace : true,
  126. matchBrackets : true,
  127. indentWithTabs : true,
  128. styleSelectedText : true,
  129. matchWordHighlight : true, // options: true, false, "onselected"
  130. styleActiveLine : true, // Highlight the current line
  131. dialogLockScreen : true,
  132. dialogShowMask : true,
  133. dialogDraggable : true,
  134. dialogMaskBgColor : "#fff",
  135. dialogMaskOpacity : 0.1,
  136. fontSize : "13px",
  137. saveHTMLToTextarea : false,
  138. disabledKeyMaps : [],
  139. onload : function() {},
  140. onresize : function() {},
  141. onchange : function() {},
  142. onwatch : null,
  143. onunwatch : null,
  144. onpreviewing : function() {},
  145. onpreviewed : function() {},
  146. onfullscreen : function() {},
  147. onfullscreenExit : function() {},
  148. onscroll : function() {},
  149. onpreviewscroll : function() {},
  150. imageUpload : false,
  151. imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  152. imageUploadURL : "",
  153. crossDomainUpload : false,
  154. uploadCallbackURL : "",
  155. toc : true, // Table of contents
  156. tocm : false, // Using [TOCM], auto create ToC dropdown menu
  157. tocTitle : "", // for ToC dropdown menu btn
  158. tocDropdown : false,
  159. tocContainer : "",
  160. tocStartLevel : 1, // Said from H1 to create ToC
  161. htmlDecode : false, // Open the HTML tag identification
  162. pageBreak : true, // Enable parse page break [========]
  163. atLink : true, // for @link
  164. emailLink : true, // for email address auto link
  165. taskList : false, // Enable Github Flavored Markdown task lists
  166. emoji : false, // :emoji: , Support Github emoji, Twitter Emoji (Twemoji);
  167. // Support FontAwesome icon emoji :fa-xxx: > Using fontAwesome icon web fonts;
  168. // Support Editor.md logo icon emoji :editormd-logo: :editormd-logo-1x: > 1~8x;
  169. tex : false, // TeX(LaTeX), based on KaTeX
  170. flowChart : false, // flowChart.js only support IE9+
  171. sequenceDiagram : false, // sequenceDiagram.js only support IE9+
  172. previewCodeHighlight : true,
  173. toolbar : true, // show/hide toolbar
  174. toolbarAutoFixed : true, // on window scroll auto fixed position
  175. toolbarIcons : "full",
  176. toolbarTitles : {},
  177. toolbarHandlers : {
  178. ucwords : function() {
  179. return editormd.toolbarHandlers.ucwords;
  180. },
  181. lowercase : function() {
  182. return editormd.toolbarHandlers.lowercase;
  183. }
  184. },
  185. toolbarCustomIcons : { // using html tag create toolbar icon, unused default &lt;a> tag.
  186. lowercase : "&lt;a href=\"javascript:;\" title=\"Lowercase\" unselectable=\"on\">&lt;i class=\"fa\" name=\"lowercase\" style=\"font-size:24px;margin-top: -10px;\">a&lt;/i>&lt;/a>",
  187. "ucwords" : "&lt;a href=\"javascript:;\" title=\"ucwords\" unselectable=\"on\">&lt;i class=\"fa\" name=\"ucwords\" style=\"font-size:20px;margin-top: -3px;\">Aa&lt;/i>&lt;/a>"
  188. },
  189. toolbarIconsClass : {
  190. undo : "fa-undo",
  191. redo : "fa-repeat",
  192. bold : "fa-bold",
  193. del : "fa-strikethrough",
  194. italic : "fa-italic",
  195. quote : "fa-quote-left",
  196. uppercase : "fa-font",
  197. h1 : editormd.classPrefix + "bold",
  198. h2 : editormd.classPrefix + "bold",
  199. h3 : editormd.classPrefix + "bold",
  200. h4 : editormd.classPrefix + "bold",
  201. h5 : editormd.classPrefix + "bold",
  202. h6 : editormd.classPrefix + "bold",
  203. "list-ul" : "fa-list-ul",
  204. "list-ol" : "fa-list-ol",
  205. hr : "fa-minus",
  206. link : "fa-link",
  207. "reference-link" : "fa-anchor",
  208. image : "fa-picture-o",
  209. code : "fa-code",
  210. "preformatted-text" : "fa-file-code-o",
  211. "code-block" : "fa-file-code-o",
  212. table : "fa-table",
  213. datetime : "fa-clock-o",
  214. emoji : "fa-smile-o",
  215. "html-entities" : "fa-copyright",
  216. pagebreak : "fa-newspaper-o",
  217. "goto-line" : "fa-terminal", // fa-crosshairs
  218. watch : "fa-eye-slash",
  219. unwatch : "fa-eye",
  220. preview : "fa-desktop",
  221. search : "fa-search",
  222. fullscreen : "fa-arrows-alt",
  223. clear : "fa-eraser",
  224. help : "fa-question-circle",
  225. info : "fa-info-circle"
  226. },
  227. toolbarIconTexts : {},
  228. lang : {
  229. name : "zh-cn",
  230. description : "开源在线Markdown编辑器&lt;br/>Open source online Markdown editor.",
  231. tocTitle : "目录",
  232. toolbar : {
  233. undo : "撤销(Ctrl+Z)",
  234. redo : "重做(Ctrl+Y)",
  235. bold : "粗体",
  236. del : "删除线",
  237. italic : "斜体",
  238. quote : "引用",
  239. ucwords : "将每个单词首字母转成大写",
  240. uppercase : "将所选转换成大写",
  241. lowercase : "将所选转换成小写",
  242. h1 : "标题1",
  243. h2 : "标题2",
  244. h3 : "标题3",
  245. h4 : "标题4",
  246. h5 : "标题5",
  247. h6 : "标题6",
  248. "list-ul" : "无序列表",
  249. "list-ol" : "有序列表",
  250. hr : "横线",
  251. link : "链接",
  252. "reference-link" : "引用链接",
  253. image : "添加图片",
  254. code : "行内代码",
  255. "preformatted-text" : "预格式文本 / 代码块(缩进风格)",
  256. "code-block" : "代码块(多语言风格)",
  257. table : "添加表格",
  258. datetime : "日期时间",
  259. emoji : "Emoji表情",
  260. "html-entities" : "HTML实体字符",
  261. pagebreak : "插入分页符",
  262. "goto-line" : "跳转到行",
  263. watch : "关闭实时预览",
  264. unwatch : "开启实时预览",
  265. preview : "全窗口预览HTML(按 Shift + ESC还原)",
  266. fullscreen : "全屏(按ESC还原)",
  267. clear : "清空",
  268. search : "搜索",
  269. help : "使用帮助",
  270. info : "关于" + editormd.title
  271. },
  272. buttons : {
  273. enter : "确定",
  274. cancel : "取消",
  275. close : "关闭"
  276. },
  277. dialog : {
  278. link : {
  279. title : "添加链接",
  280. url : "链接地址",
  281. urlTitle : "链接标题",
  282. urlEmpty : "错误:请填写链接地址。"
  283. },
  284. referenceLink : {
  285. title : "添加引用链接",
  286. name : "引用名称",
  287. url : "链接地址",
  288. urlId : "链接ID",
  289. urlTitle : "链接标题",
  290. nameEmpty: "错误:引用链接的名称不能为空。",
  291. idEmpty : "错误:请填写引用链接的ID。",
  292. urlEmpty : "错误:请填写引用链接的URL地址。"
  293. },
  294. image : {
  295. title : "添加图片",
  296. url : "图片地址",
  297. link : "图片链接",
  298. alt : "图片描述",
  299. uploadButton : "本地上传",
  300. imageURLEmpty : "错误:图片地址不能为空。",
  301. uploadFileEmpty : "错误:上传的图片不能为空。",
  302. formatNotAllowed : "错误:只允许上传图片文件,允许上传的图片文件格式有:"
  303. },
  304. preformattedText : {
  305. title : "添加预格式文本或代码块",
  306. emptyAlert : "错误:请填写预格式文本或代码的内容。"
  307. },
  308. codeBlock : {
  309. title : "添加代码块",
  310. selectLabel : "代码语言:",
  311. selectDefaultText : "请选择代码语言",
  312. otherLanguage : "其他语言",
  313. unselectedLanguageAlert : "错误:请选择代码所属的语言类型。",
  314. codeEmptyAlert : "错误:请填写代码内容。"
  315. },
  316. htmlEntities : {
  317. title : "HTML 实体字符"
  318. },
  319. help : {
  320. title : "使用帮助"
  321. }
  322. }
  323. }
  324. };
  325. editormd.classNames = {
  326. tex : editormd.classPrefix + "tex"
  327. };
  328. editormd.dialogZindex = 99999;
  329. editormd.$katex = null;
  330. editormd.$marked = null;
  331. editormd.$CodeMirror = null;
  332. editormd.$prettyPrint = null;
  333. var timer, flowchartTimer;
  334. editormd.prototype = editormd.fn = {
  335. state : {
  336. watching : false,
  337. loaded : false,
  338. preview : false,
  339. fullscreen : false
  340. },
  341. /**
  342. * 构造函数/实例初始化
  343. * Constructor / instance initialization
  344. *
  345. * @param {String} id 编辑器的ID
  346. * @param {Object} [options={}] 配置选项 Key/Value
  347. * @returns {editormd} 返回editormd的实例对象
  348. */
  349. init : function (id, options) {
  350. options = options || {};
  351. if (typeof id === "object")
  352. {
  353. options = id;
  354. }
  355. var _this = this;
  356. var classPrefix = this.classPrefix = editormd.classPrefix;
  357. var settings = this.settings = $.extend(true, editormd.defaults, options);
  358. id = (typeof id === "object") ? settings.id : id;
  359. var editor = this.editor = $("#" + id);
  360. this.id = id;
  361. this.lang = settings.lang;
  362. var classNames = this.classNames = {
  363. textarea : {
  364. html : classPrefix + "html-textarea",
  365. markdown : classPrefix + "markdown-textarea"
  366. }
  367. };
  368. settings.pluginPath = (settings.pluginPath === "") ? settings.path + "../plugins/" : settings.pluginPath;
  369. this.state.watching = (settings.watch) ? true : false;
  370. if ( !editor.hasClass("editormd") ) {
  371. editor.addClass("editormd");
  372. }
  373. editor.css({
  374. width : (typeof settings.width === "number") ? settings.width + "px" : settings.width,
  375. height : (typeof settings.height === "number") ? settings.height + "px" : settings.height
  376. });
  377. if (settings.autoHeight)
  378. {
  379. editor.css("height", "auto");
  380. }
  381. var markdownTextarea = this.markdownTextarea = editor.children("textarea");
  382. if (markdownTextarea.length &lt; 1)
  383. {
  384. editor.append("&lt;textarea>&lt;/textarea>");
  385. markdownTextarea = this.markdownTextarea = editor.children("textarea");
  386. }
  387. markdownTextarea.addClass(classNames.textarea.markdown).attr("placeholder", settings.placeholder);
  388. if (typeof markdownTextarea.attr("name") === "undefined" || markdownTextarea.attr("name") === "")
  389. {
  390. markdownTextarea.attr("name", (settings.name !== "") ? settings.name : id + "-markdown-doc");
  391. }
  392. var appendElements = [
  393. (!settings.readOnly) ? "&lt;a href=\"javascript:;\" class=\"fa fa-close " + classPrefix + "preview-close-btn\">&lt;/a>" : "",
  394. ( (settings.saveHTMLToTextarea) ? "&lt;textarea class=\"" + classNames.textarea.html + "\" name=\"" + id + "-html-code\">&lt;/textarea>" : "" ),
  395. "&lt;div class=\"" + classPrefix + "preview\">&lt;div class=\"markdown-body " + classPrefix + "preview-container\">&lt;/div>&lt;/div>",
  396. "&lt;div class=\"" + classPrefix + "container-mask\" style=\"display:block;\">&lt;/div>",
  397. "&lt;div class=\"" + classPrefix + "mask\">&lt;/div>"
  398. ].join("\n");
  399. editor.append(appendElements).addClass(classPrefix + "vertical");
  400. this.mask = editor.children("." + classPrefix + "mask");
  401. this.containerMask = editor.children("." + classPrefix + "container-mask");
  402. if (settings.markdown !== "")
  403. {
  404. markdownTextarea.val(settings.markdown);
  405. }
  406. if (settings.appendMarkdown !== "")
  407. {
  408. markdownTextarea.val(markdownTextarea.val() + settings.appendMarkdown);
  409. }
  410. this.htmlTextarea = editor.children("." + classNames.textarea.html);
  411. this.preview = editor.children("." + classPrefix + "preview");
  412. this.previewContainer = this.preview.children("." + classPrefix + "preview-container");
  413. if (typeof define === "function" &amp;&amp; define.amd)
  414. {
  415. if (typeof katex !== "undefined")
  416. {
  417. editormd.$katex = katex;
  418. }
  419. if (settings.searchReplace &amp;&amp; !settings.readOnly)
  420. {
  421. editormd.loadCSS(settings.path + "codemirror/addon/dialog/dialog");
  422. editormd.loadCSS(settings.path + "codemirror/addon/search/matchesonscrollbar");
  423. }
  424. }
  425. if ((typeof define === "function" &amp;&amp; define.amd) || !settings.autoLoadModules)
  426. {
  427. if (typeof CodeMirror !== "undefined") {
  428. editormd.$CodeMirror = CodeMirror;
  429. }
  430. if (typeof marked !== "undefined") {
  431. editormd.$marked = marked;
  432. }
  433. this.setCodeMirror().setToolbar().loadedDisplay();
  434. }
  435. else
  436. {
  437. this.loadQueues();
  438. }
  439. return this;
  440. },
  441. /**
  442. * 所需组件加载队列
  443. * Required components loading queue
  444. *
  445. * @returns {editormd} 返回editormd的实例对象
  446. */
  447. loadQueues : function() {
  448. var _this = this;
  449. var settings = this.settings;
  450. var loadPath = settings.path;
  451. var loadFlowChartOrSequenceDiagram = function() {
  452. if (editormd.isIE8)
  453. {
  454. _this.loadedDisplay();
  455. return ;
  456. }
  457. if (settings.flowChart || settings.sequenceDiagram)
  458. {
  459. editormd.loadScript(loadPath + "raphael.min", function() {
  460. editormd.loadScript(loadPath + "underscore.min", function() {
  461. if (!settings.flowChart &amp;&amp; settings.sequenceDiagram)
  462. {
  463. editormd.loadScript(loadPath + "sequence-diagram.min", function() {
  464. _this.loadedDisplay();
  465. });
  466. }
  467. else if (settings.flowChart &amp;&amp; !settings.sequenceDiagram)
  468. {
  469. editormd.loadScript(loadPath + "flowchart.min", function() {
  470. editormd.loadScript(loadPath + "jquery.flowchart.min", function() {
  471. _this.loadedDisplay();
  472. });
  473. });
  474. }
  475. else if (settings.flowChart &amp;&amp; settings.sequenceDiagram)
  476. {
  477. editormd.loadScript(loadPath + "flowchart.min", function() {
  478. editormd.loadScript(loadPath + "jquery.flowchart.min", function() {
  479. editormd.loadScript(loadPath + "sequence-diagram.min", function() {
  480. _this.loadedDisplay();
  481. });
  482. });
  483. });
  484. }
  485. });
  486. });
  487. }
  488. else
  489. {
  490. _this.loadedDisplay();
  491. }
  492. };
  493. editormd.loadCSS(loadPath + "codemirror/codemirror.min");
  494. if (settings.searchReplace &amp;&amp; !settings.readOnly)
  495. {
  496. editormd.loadCSS(loadPath + "codemirror/addon/dialog/dialog");
  497. editormd.loadCSS(loadPath + "codemirror/addon/search/matchesonscrollbar");
  498. }
  499. if (settings.codeFold)
  500. {
  501. editormd.loadCSS(loadPath + "codemirror/addon/fold/foldgutter");
  502. }
  503. editormd.loadScript(loadPath + "codemirror/codemirror.min", function() {
  504. editormd.$CodeMirror = CodeMirror;
  505. editormd.loadScript(loadPath + "codemirror/modes.min", function() {
  506. editormd.loadScript(loadPath + "codemirror/addons.min", function() {
  507. _this.setCodeMirror();
  508. if (settings.mode !== "gfm" &amp;&amp; settings.mode !== "markdown")
  509. {
  510. _this.loadedDisplay();
  511. return false;
  512. }
  513. _this.setToolbar();
  514. editormd.loadScript(loadPath + "marked.min", function() {
  515. editormd.$marked = marked;
  516. if (settings.previewCodeHighlight)
  517. {
  518. editormd.loadScript(loadPath + "prettify.min", function() {
  519. loadFlowChartOrSequenceDiagram();
  520. });
  521. }
  522. else
  523. {
  524. loadFlowChartOrSequenceDiagram();
  525. }
  526. });
  527. });
  528. });
  529. });
  530. return this;
  531. },
  532. /**
  533. * 设置CodeMirror的主题
  534. * Setting CodeMirror theme
  535. *
  536. * @returns {editormd} 返回editormd的实例对象
  537. */
  538. setTheme : function(theme) {
  539. var settings = this.settings;
  540. settings.theme = theme;
  541. if (theme !== "default")
  542. {
  543. editormd.loadCSS(settings.path + "codemirror/theme/" + settings.theme);
  544. }
  545. this.cm.setOption("theme", theme);
  546. return this;
  547. },
  548. /**
  549. * 配置和初始化CodeMirror组件
  550. * CodeMirror initialization
  551. *
  552. * @returns {editormd} 返回editormd的实例对象
  553. */
  554. setCodeMirror : function() {
  555. var settings = this.settings;
  556. var editor = this.editor;
  557. if (settings.theme !== "default")
  558. {
  559. editormd.loadCSS(settings.path + "codemirror/theme/" + settings.theme);
  560. }
  561. var codeMirrorConfig = {
  562. mode : settings.mode,
  563. theme : settings.theme,
  564. tabSize : settings.tabSize,
  565. dragDrop : false,
  566. autofocus : settings.autoFocus,
  567. autoCloseTags : settings.autoCloseTags,
  568. readOnly : (settings.readOnly) ? "nocursor" : false,
  569. indentUnit : settings.indentUnit,
  570. lineNumbers : settings.lineNumbers,
  571. lineWrapping : settings.lineWrapping,
  572. extraKeys : {
  573. "Ctrl-Q": function(cm) {
  574. cm.foldCode(cm.getCursor());
  575. }
  576. },
  577. foldGutter : settings.codeFold,
  578. gutters : ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
  579. matchBrackets : settings.matchBrackets,
  580. indentWithTabs : settings.indentWithTabs,
  581. styleActiveLine : settings.styleActiveLine,
  582. styleSelectedText : settings.styleSelectedText,
  583. autoCloseBrackets : settings.autoCloseBrackets,
  584. showTrailingSpace : settings.showTrailingSpace,
  585. highlightSelectionMatches : ( (!settings.matchWordHighlight) ? false : { showToken: (settings.matchWordHighlight === "onselected") ? false : /\w/ } )
  586. };
  587. this.codeEditor = this.cm = editormd.$CodeMirror.fromTextArea(this.markdownTextarea[0], codeMirrorConfig);
  588. this.codeMirror = this.cmElement = editor.children(".CodeMirror");
  589. if (settings.value !== "")
  590. {
  591. this.cm.setValue(settings.value);
  592. }
  593. this.codeMirror.css({
  594. fontSize : settings.fontSize,
  595. width : (!settings.watch) ? "100%" : "50%"
  596. });
  597. if (settings.autoHeight)
  598. {
  599. this.codeMirror.css("height", "auto");
  600. this.cm.setOption("viewportMargin", Infinity);
  601. }
  602. return this;
  603. },
  604. /**
  605. * 获取CodeMirror的配置选项
  606. * Get CodeMirror setting options
  607. *
  608. * @returns {Mixed} return CodeMirror setting option value
  609. */
  610. getCodeMirrorOption : function(key) {
  611. return this.cm.getOption(key);
  612. },
  613. /**
  614. * 配置和重配置CodeMirror的选项
  615. * CodeMirror setting options / resettings
  616. *
  617. * @returns {editormd} 返回editormd的实例对象
  618. */
  619. setCodeMirrorOption : function(key, value) {
  620. this.cm.setOption(key, value);
  621. return this;
  622. },
  623. /**
  624. * 添加 CodeMirror 键盘快捷键
  625. * Add CodeMirror keyboard shortcuts key map
  626. *
  627. * @returns {editormd} 返回editormd的实例对象
  628. */
  629. addKeyMap : function(map, bottom) {
  630. this.cm.addKeyMap(map, bottom);
  631. return this;
  632. },
  633. /**
  634. * 移除 CodeMirror 键盘快捷键
  635. * Remove CodeMirror keyboard shortcuts key map
  636. *
  637. * @returns {editormd} 返回editormd的实例对象
  638. */
  639. removeKeyMap : function(map) {
  640. this.cm.removeKeyMap(map);
  641. return this;
  642. },
  643. /**
  644. * 跳转到指定的行
  645. * Goto CodeMirror line
  646. *
  647. * @param {String|Intiger} line line number or "first"|"last"
  648. * @returns {editormd} 返回editormd的实例对象
  649. */
  650. gotoLine : function (line) {
  651. var settings = this.settings;
  652. if (!settings.gotoLine)
  653. {
  654. return this;
  655. }
  656. var cm = this.cm;
  657. var editor = this.editor;
  658. var count = cm.lineCount();
  659. var preview = this.preview;
  660. if (typeof line === "string")
  661. {
  662. if(line === "last")
  663. {
  664. line = count;
  665. }
  666. if (line === "first")
  667. {
  668. line = 1;
  669. }
  670. }
  671. if (typeof line !== "number")
  672. {
  673. alert("Error: The line number must be an integer.");
  674. return this;
  675. }
  676. line = parseInt(line) - 1;
  677. if (line > count)
  678. {
  679. alert("Error: The line number range 1-" + count);
  680. return this;
  681. }
  682. cm.setCursor( {line : line, ch : 0} );
  683. var scrollInfo = cm.getScrollInfo();
  684. var clientHeight = scrollInfo.clientHeight;
  685. var coords = cm.charCoords({line : line, ch : 0}, "local");
  686. cm.scrollTo(null, (coords.top + coords.bottom - clientHeight) / 2);
  687. if (settings.watch)
  688. {
  689. var cmScroll = this.codeMirror.find(".CodeMirror-scroll")[0];
  690. var height = $(cmScroll).height();
  691. var scrollTop = cmScroll.scrollTop;
  692. var percent = (scrollTop / cmScroll.scrollHeight);
  693. if (scrollTop === 0)
  694. {
  695. preview.scrollTop(0);
  696. }
  697. else if (scrollTop + height >= cmScroll.scrollHeight - 16)
  698. {
  699. preview.scrollTop(preview[0].scrollHeight);
  700. }
  701. else
  702. {
  703. preview.scrollTop(preview[0].scrollHeight * percent);
  704. }
  705. }
  706. cm.focus();
  707. return this;
  708. },
  709. /**
  710. * 扩展当前实例对象,可同时设置多个或者只设置一个
  711. * Extend editormd instance object, can mutil setting.
  712. *
  713. * @returns {editormd} this(editormd instance object.)
  714. */
  715. extend : function() {
  716. if (typeof arguments[1] !== "undefined")
  717. {
  718. if (typeof arguments[1] === "function")
  719. {
  720. arguments[1] = $.proxy(arguments[1], this);
  721. }
  722. this[arguments[0]] = arguments[1];
  723. }
  724. if (typeof arguments[0] === "object" &amp;&amp; typeof arguments[0].length === "undefined")
  725. {
  726. $.extend(true, this, arguments[0]);
  727. }
  728. return this;
  729. },
  730. /**
  731. * 设置或扩展当前实例对象,单个设置
  732. * Extend editormd instance object, one by one
  733. *
  734. * @param {String|Object} key option key
  735. * @param {String|Object} value option value
  736. * @returns {editormd} this(editormd instance object.)
  737. */
  738. set : function (key, value) {
  739. if (typeof value !== "undefined" &amp;&amp; typeof value === "function")
  740. {
  741. value = $.proxy(value, this);
  742. }
  743. this[key] = value;
  744. return this;
  745. },
  746. /**
  747. * 重新配置
  748. * Resetting editor options
  749. *
  750. * @param {String|Object} key option key
  751. * @param {String|Object} value option value
  752. * @returns {editormd} this(editormd instance object.)
  753. */
  754. config : function(key, value) {
  755. var settings = this.settings;
  756. if (typeof key === "object")
  757. {
  758. settings = $.extend(true, settings, key);
  759. }
  760. if (typeof key === "string")
  761. {
  762. settings[key] = value;
  763. }
  764. this.settings = settings;
  765. this.recreate();
  766. return this;
  767. },
  768. /**
  769. * 注册事件处理方法
  770. * Bind editor event handle
  771. *
  772. * @param {String} eventType event type
  773. * @param {Function} callback 回调函数
  774. * @returns {editormd} this(editormd instance object.)
  775. */
  776. on : function(eventType, callback) {
  777. var settings = this.settings;
  778. if (typeof settings["on" + eventType] !== "undefined")
  779. {
  780. settings["on" + eventType] = $.proxy(callback, this);
  781. }
  782. return this;
  783. },
  784. /**
  785. * 解除事件处理方法
  786. * Unbind editor event handle
  787. *
  788. * @param {String} eventType event type
  789. * @returns {editormd} this(editormd instance object.)
  790. */
  791. off : function(eventType) {
  792. var settings = this.settings;
  793. if (typeof settings["on" + eventType] !== "undefined")
  794. {
  795. settings["on" + eventType] = function(){};
  796. }
  797. return this;
  798. },
  799. /**
  800. * 显示工具栏
  801. * Display toolbar
  802. *
  803. * @param {Function} [callback=function(){}] 回调函数
  804. * @returns {editormd} 返回editormd的实例对象
  805. */
  806. showToolbar : function(callback) {
  807. var settings = this.settings;
  808. if(settings.readOnly) {
  809. return this;
  810. }
  811. if (settings.toolbar &amp;&amp; (this.toolbar.length &lt; 1 || this.toolbar.find("." + this.classPrefix + "menu").html() === "") )
  812. {
  813. this.setToolbar();
  814. }
  815. settings.toolbar = true;
  816. this.toolbar.show();
  817. this.resize();
  818. $.proxy(callback || function(){}, this)();
  819. return this;
  820. },
  821. /**
  822. * 隐藏工具栏
  823. * Hide toolbar
  824. *
  825. * @param {Function} [callback=function(){}] 回调函数
  826. * @returns {editormd} this(editormd instance object.)
  827. */
  828. hideToolbar : function(callback) {
  829. var settings = this.settings;
  830. settings.toolbar = false;
  831. this.toolbar.hide();
  832. this.resize();
  833. $.proxy(callback || function(){}, this)();
  834. return this;
  835. },
  836. /**
  837. * 页面滚动时工具栏的固定定位
  838. * Set toolbar in window scroll auto fixed position
  839. *
  840. * @returns {editormd} 返回editormd的实例对象
  841. */
  842. setToolbarAutoFixed : function(fixed) {
  843. var state = this.state;
  844. var editor = this.editor;
  845. var toolbar = this.toolbar;
  846. var settings = this.settings;
  847. if (typeof fixed !== "undefined")
  848. {
  849. settings.toolbarAutoFixed = fixed;
  850. }
  851. var autoFixedHandle = function(){
  852. var $window = $(window);
  853. var top = $window.scrollTop();
  854. if (!settings.toolbarAutoFixed)
  855. {
  856. return false;
  857. }
  858. if (top - editor.offset().top > 10 &amp;&amp; top &lt; editor.height())
  859. {
  860. toolbar.css({
  861. position : "fixed",
  862. width : editor.width() + "px",
  863. left : ($window.width() - editor.width()) / 2 + "px"
  864. });
  865. }
  866. else
  867. {
  868. toolbar.css({
  869. position : "absolute",
  870. width : "100%",
  871. left : 0
  872. });
  873. }
  874. };
  875. if (!state.fullscreen &amp;&amp; !state.preview &amp;&amp; settings.toolbar &amp;&amp; settings.toolbarAutoFixed)
  876. {
  877. $(window).bind("scroll", autoFixedHandle);
  878. }
  879. return this;
  880. },
  881. /**
  882. * 配置和初始化工具栏
  883. * Set toolbar and Initialization
  884. *
  885. * @returns {editormd} 返回editormd的实例对象
  886. */
  887. setToolbar : function() {
  888. var settings = this.settings;
  889. if(settings.readOnly) {
  890. return this;
  891. }
  892. var editor = this.editor;
  893. var preview = this.preview;
  894. var classPrefix = this.classPrefix;
  895. var toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar");
  896. if (settings.toolbar &amp;&amp; toolbar.length &lt; 1)
  897. {
  898. var toolbarHTML = "&lt;div class=\"" + classPrefix + "toolbar\">&lt;div class=\"" + classPrefix + "toolbar-container\">&lt;ul class=\"" + classPrefix + "menu\">&lt;/ul>&lt;/div>&lt;/div>";
  899. editor.append(toolbarHTML);
  900. toolbar = this.toolbar = editor.children("." + classPrefix + "toolbar");
  901. }
  902. if (!settings.toolbar)
  903. {
  904. toolbar.hide();
  905. return this;
  906. }
  907. toolbar.show();
  908. var icons = (typeof settings.toolbarIcons === "function") ? settings.toolbarIcons()
  909. : ((typeof settings.toolbarIcons === "string") ? editormd.toolbarModes[settings.toolbarIcons] : settings.toolbarIcons);
  910. var toolbarMenu = toolbar.find("." + this.classPrefix + "menu"), menu = "";
  911. var pullRight = false;
  912. for (var i = 0, len = icons.length; i &lt; len; i++)
  913. {
  914. var name = icons[i];
  915. if (name === "||")
  916. {
  917. pullRight = true;
  918. }
  919. else if (name === "|")
  920. {
  921. menu += "&lt;li class=\"divider\" unselectable=\"on\">|&lt;/li>";
  922. }
  923. else
  924. {
  925. var isHeader = (/h(\d)/.test(name));
  926. var index = name;
  927. if (name === "watch" &amp;&amp; !settings.watch) {
  928. index = "unwatch";
  929. }
  930. var title = settings.lang.toolbar[index];
  931. var iconTexts = settings.toolbarIconTexts[index];
  932. var iconClass = settings.toolbarIconsClass[index];
  933. title = (typeof title === "undefined") ? "" : title;
  934. iconTexts = (typeof iconTexts === "undefined") ? "" : iconTexts;
  935. iconClass = (typeof iconClass === "undefined") ? "" : iconClass;
  936. var menuItem = pullRight ? "&lt;li class=\"pull-right\">" : "&lt;li>";
  937. if (typeof settings.toolbarCustomIcons[name] !== "undefined" &amp;&amp; typeof settings.toolbarCustomIcons[name] !== "function")
  938. {
  939. menuItem += settings.toolbarCustomIcons[name];
  940. }
  941. else
  942. {
  943. menuItem += "&lt;a href=\"javascript:;\" title=\"" + title + "\" unselectable=\"on\">";
  944. menuItem += "&lt;i class=\"fa " + iconClass + "\" name=\""+name+"\" unselectable=\"on\">"+((isHeader) ? name.toUpperCase() : ( (iconClass === "") ? iconTexts : "") ) + "&lt;/i>";
  945. menuItem += "&lt;/a>";
  946. }
  947. menuItem += "&lt;/li>";
  948. menu = pullRight ? menuItem + menu : menu + menuItem;
  949. }
  950. }
  951. toolbarMenu.html(menu);
  952. toolbarMenu.find("[title=\"Lowercase\"]").attr("title", settings.lang.toolbar.lowercase);
  953. toolbarMenu.find("[title=\"ucwords\"]").attr("title", settings.lang.toolbar.ucwords);
  954. this.setToolbarHandler();
  955. this.setToolbarAutoFixed();
  956. return this;
  957. },
  958. /**
  959. * 工具栏图标事件处理对象序列
  960. * Get toolbar icons event handlers
  961. *
  962. * @param {Object} cm CodeMirror的实例对象
  963. * @param {String} name 要获取的事件处理器名称
  964. * @returns {Object} 返回处理对象序列
  965. */
  966. dialogLockScreen : function() {
  967. $.proxy(editormd.dialogLockScreen, this)();
  968. return this;
  969. },
  970. dialogShowMask : function(dialog) {
  971. $.proxy(editormd.dialogShowMask, this)(dialog);
  972. return this;
  973. },
  974. getToolbarHandles : function(name) {
  975. var toolbarHandlers = this.toolbarHandlers = editormd.toolbarHandlers;
  976. return (name &amp;&amp; typeof toolbarIconHandlers[name] !== "undefined") ? toolbarHandlers[name] : toolbarHandlers;
  977. },
  978. /**
  979. * 工具栏图标事件处理器
  980. * Bind toolbar icons event handle
  981. *
  982. * @returns {editormd} 返回editormd的实例对象
  983. */
  984. setToolbarHandler : function() {
  985. var _this = this;
  986. var settings = this.settings;
  987. if (!settings.toolbar || settings.readOnly) {
  988. return this;
  989. }
  990. var toolbar = this.toolbar;
  991. var cm = this.cm;
  992. var classPrefix = this.classPrefix;
  993. var toolbarIcons = this.toolbarIcons = toolbar.find("." + classPrefix + "menu > li > a");
  994. var toolbarIconHandlers = this.getToolbarHandles();
  995. toolbarIcons.bind(editormd.mouseOrTouch("click", "touchend"), function(event) {
  996. var icon = $(this).children(".fa");
  997. var name = icon.attr("name");
  998. var cursor = cm.getCursor();
  999. var selection = cm.getSelection();
  1000. if (name === "") {
  1001. return ;
  1002. }
  1003. _this.activeIcon = icon;
  1004. if (typeof toolbarIconHandlers[name] !== "undefined")
  1005. {
  1006. $.proxy(toolbarIconHandlers[name], _this)(cm);
  1007. }
  1008. else
  1009. {
  1010. if (typeof settings.toolbarHandlers[name] !== "undefined")
  1011. {
  1012. $.proxy(settings.toolbarHandlers[name], _this)(cm, icon, cursor, selection);
  1013. }
  1014. }
  1015. if (name !== "link" &amp;&amp; name !== "reference-link" &amp;&amp; name !== "image" &amp;&amp; name !== "code-block" &amp;&amp;
  1016. name !== "preformatted-text" &amp;&amp; name !== "watch" &amp;&amp; name !== "preview" &amp;&amp; name !== "search" &amp;&amp; name !== "fullscreen" &amp;&amp; name !== "info")
  1017. {
  1018. cm.focus();
  1019. }
  1020. return false;
  1021. });
  1022. return this;
  1023. },
  1024. /**
  1025. * 动态创建对话框
  1026. * Creating custom dialogs
  1027. *
  1028. * @param {Object} options 配置项键值对 Key/Value
  1029. * @returns {dialog} 返回创建的dialog的jQuery实例对象
  1030. */
  1031. createDialog : function(options) {
  1032. return $.proxy(editormd.createDialog, this)(options);
  1033. },
  1034. /**
  1035. * 创建关于Editor.md的对话框
  1036. * Create about Editor.md dialog
  1037. *
  1038. * @returns {editormd} 返回editormd的实例对象
  1039. */
  1040. createInfoDialog : function() {
  1041. var _this = this;
  1042. var editor = this.editor;
  1043. var classPrefix = this.classPrefix;
  1044. var infoDialogHTML = [
  1045. "&lt;div class=\"" + classPrefix + "dialog " + classPrefix + "dialog-info\" style=\"\">",
  1046. "&lt;div class=\"" + classPrefix + "dialog-container\">",
  1047. "&lt;h1>&lt;i class=\"editormd-logo editormd-logo-lg editormd-logo-color\">&lt;/i> " + editormd.title + "&lt;small>v" + editormd.version + "&lt;/small>&lt;/h1>",
  1048. "&lt;p>" + this.lang.description + "&lt;/p>",
  1049. "&lt;p style=\"margin: 10px 0 20px 0;\">&lt;a href=\"" + editormd.homePage + "\" target=\"_blank\">" + editormd.homePage + " &lt;i class=\"fa fa-external-link\">&lt;/i>&lt;/a>&lt;/p>",
  1050. "&lt;p style=\"font-size: 0.85em;\">Copyright &amp;copy; 2015 &lt;a href=\"https://github.com/pandao\" target=\"_blank\" class=\"hover-link\">Pandao&lt;/a>, The &lt;a href=\"https://github.com/pandao/editor.md/blob/master/LICENSE\" target=\"_blank\" class=\"hover-link\">MIT&lt;/a> License.&lt;/p>",
  1051. "&lt;/div>",
  1052. "&lt;a href=\"javascript:;\" class=\"fa fa-close " + classPrefix + "dialog-close\">&lt;/a>",
  1053. "&lt;/div>"
  1054. ].join("\n");
  1055. editor.append(infoDialogHTML);
  1056. var infoDialog = this.infoDialog = editor.children("." + classPrefix + "dialog-info");
  1057. infoDialog.find("." + classPrefix + "dialog-close").bind(editormd.mouseOrTouch("click", "touchend"), function() {
  1058. _this.hideInfoDialog();
  1059. });
  1060. infoDialog.css("border", (editormd.isIE8) ? "1px solid #ddd" : "").css("z-index", editormd.dialogZindex).show();
  1061. this.infoDialogPosition();
  1062. return this;
  1063. },
  1064. /**
  1065. * 关于Editor.md对话居中定位
  1066. * Editor.md dialog position handle
  1067. *
  1068. * @returns {editormd} 返回editormd的实例对象
  1069. */
  1070. infoDialogPosition : function() {
  1071. var infoDialog = this.infoDialog;
  1072. var _infoDialogPosition = function() {
  1073. infoDialog.css({
  1074. top : ($(window).height() - infoDialog.height()) / 2 + "px",
  1075. left : ($(window).width() - infoDialog.width()) / 2 + "px"
  1076. });
  1077. };
  1078. _infoDialogPosition();
  1079. $(window).resize(_infoDialogPosition);
  1080. return this;
  1081. },
  1082. /**
  1083. * 显示关于Editor.md
  1084. * Display about Editor.md dialog
  1085. *
  1086. * @returns {editormd} 返回editormd的实例对象
  1087. */
  1088. showInfoDialog : function() {
  1089. $("html,body").css("overflow-x", "hidden");
  1090. var _this = this;
  1091. var editor = this.editor;
  1092. var settings = this.settings;
  1093. var infoDialog = this.infoDialog = editor.children("." + this.classPrefix + "dialog-info");
  1094. if (infoDialog.length &lt; 1)
  1095. {
  1096. this.createInfoDialog();
  1097. }
  1098. this.lockScreen(true);
  1099. this.mask.css({
  1100. opacity : settings.dialogMaskOpacity,
  1101. backgroundColor : settings.dialogMaskBgColor
  1102. }).show();
  1103. infoDialog.css("z-index", editormd.dialogZindex).show();
  1104. this.infoDialogPosition();
  1105. return this;
  1106. },
  1107. /**
  1108. * 隐藏关于Editor.md
  1109. * Hide about Editor.md dialog
  1110. *
  1111. * @returns {editormd} 返回editormd的实例对象
  1112. */
  1113. hideInfoDialog : function() {
  1114. $("html,body").css("overflow-x", "");
  1115. this.infoDialog.hide();
  1116. this.mask.hide();
  1117. this.lockScreen(false);
  1118. return this;
  1119. },
  1120. /**
  1121. * 锁屏
  1122. * lock screen
  1123. *
  1124. * @param {Boolean} lock Boolean 布尔值,是否锁屏
  1125. * @returns {editormd} 返回editormd的实例对象
  1126. */
  1127. lockScreen : function(lock) {
  1128. editormd.lockScreen(lock);
  1129. return this;
  1130. },
  1131. /**
  1132. * 编辑器界面重建,用于动态语言包或模块加载等
  1133. * Recreate editor
  1134. *
  1135. * @returns {editormd} 返回editormd的实例对象
  1136. */
  1137. recreate : function() {
  1138. var _this = this;
  1139. var editor = this.editor;
  1140. var settings = this.settings;
  1141. this.codeMirror.remove();
  1142. this.setCodeMirror();
  1143. if (!settings.readOnly)
  1144. {
  1145. if (editor.find(".editormd-dialog").length > 0) {
  1146. editor.find(".editormd-dialog").remove();
  1147. }
  1148. if (settings.toolbar)
  1149. {
  1150. this.getToolbarHandles();
  1151. this.setToolbar();
  1152. }
  1153. }
  1154. this.loadedDisplay(true);
  1155. return this;
  1156. },
  1157. /**
  1158. * 高亮预览HTML的pre代码部分
  1159. * highlight of preview codes
  1160. *
  1161. * @returns {editormd} 返回editormd的实例对象
  1162. */
  1163. previewCodeHighlight : function() {
  1164. var settings = this.settings;
  1165. var previewContainer = this.previewContainer;
  1166. if (settings.previewCodeHighlight)
  1167. {
  1168. previewContainer.find("pre").addClass("prettyprint linenums");
  1169. if (typeof prettyPrint !== "undefined")
  1170. {
  1171. prettyPrint();
  1172. }
  1173. }
  1174. return this;
  1175. },
  1176. /**
  1177. * 解析TeX(KaTeX)科学公式
  1178. * TeX(KaTeX) Renderer
  1179. *
  1180. * @returns {editormd} 返回editormd的实例对象
  1181. */
  1182. katexRender : function() {
  1183. if (timer === null)
  1184. {
  1185. return this;
  1186. }
  1187. this.previewContainer.find("." + editormd.classNames.tex).each(function(){
  1188. var tex = $(this);
  1189. editormd.$katex.render(tex.text(), tex[0]);
  1190. });
  1191. return this;
  1192. },
  1193. /**
  1194. * 解析和渲染流程图及时序图
  1195. * FlowChart and SequenceDiagram Renderer
  1196. *
  1197. * @returns {editormd} 返回editormd的实例对象
  1198. */
  1199. flowChartAndSequenceDiagramRender : function() {
  1200. var settings = this.settings;
  1201. var previewContainer = this.previewContainer;
  1202. if (editormd.isIE8) {
  1203. return this;
  1204. }
  1205. if (settings.flowChart) {
  1206. if (flowchartTimer === null) {
  1207. return this;
  1208. }
  1209. previewContainer.find(".flowchart").flowChart();
  1210. }
  1211. if (settings.sequenceDiagram) {
  1212. previewContainer.find(".sequence-diagram").sequenceDiagram({theme: "simple"});
  1213. }
  1214. return this;
  1215. },
  1216. /**
  1217. * 注册键盘快捷键处理
  1218. * Register CodeMirror keyMaps (keyboard shortcuts).
  1219. *
  1220. * @param {Object} keyMap KeyMap key/value {"(Ctrl/Shift/Alt)-Key" : function(){}}
  1221. * @returns {editormd} return this
  1222. */
  1223. registerKeyMaps : function(keyMap) {
  1224. var _this = this;
  1225. var cm = this.cm;
  1226. var settings = this.settings;
  1227. var toolbarHandlers = editormd.toolbarHandlers;
  1228. var disabledKeyMaps = settings.disabledKeyMaps;
  1229. keyMap = keyMap || null;
  1230. if (keyMap)
  1231. {
  1232. for (var i in keyMap)
  1233. {
  1234. if ($.inArray(i, disabledKeyMaps) &lt; 0)
  1235. {
  1236. var map = {};
  1237. map[i] = keyMap[i];
  1238. cm.addKeyMap(keyMap);
  1239. }
  1240. }
  1241. }
  1242. else
  1243. {
  1244. for (var k in editormd.keyMaps)
  1245. {
  1246. var _keyMap = editormd.keyMaps[k];
  1247. var handle = (typeof _keyMap === "string") ? $.proxy(toolbarHandlers[_keyMap], _this) : $.proxy(_keyMap, _this);
  1248. if ($.inArray(k, ["F9", "F10", "F11"]) &lt; 0 &amp;&amp; $.inArray(k, disabledKeyMaps) &lt; 0)
  1249. {
  1250. var _map = {};
  1251. _map[k] = handle;
  1252. cm.addKeyMap(_map);
  1253. }
  1254. }
  1255. $(window).keydown(function(event) {
  1256. var keymaps = {
  1257. "120" : "F9",
  1258. "121" : "F10",
  1259. "122" : "F11"
  1260. };
  1261. if ( $.inArray(keymaps[event.keyCode], disabledKeyMaps) &lt; 0 )
  1262. {
  1263. switch (event.keyCode)
  1264. {
  1265. case 120:
  1266. $.proxy(toolbarHandlers["watch"], _this)();
  1267. return false;
  1268. break;
  1269. case 121:
  1270. $.proxy(toolbarHandlers["preview"], _this)();
  1271. return false;
  1272. break;
  1273. case 122:
  1274. $.proxy(toolbarHandlers["fullscreen"], _this)();
  1275. return false;
  1276. break;
  1277. default:
  1278. break;
  1279. }
  1280. }
  1281. });
  1282. }
  1283. return this;
  1284. },
  1285. bindScrollEvent : function() {
  1286. var _this = this;
  1287. var preview = this.preview;
  1288. var settings = this.settings;
  1289. var codeMirror = this.codeMirror;
  1290. var mouseOrTouch = editormd.mouseOrTouch;
  1291. if (!settings.syncScrolling) {
  1292. return this;
  1293. }
  1294. var cmBindScroll = function() {
  1295. codeMirror.find(".CodeMirror-scroll").bind(mouseOrTouch("scroll", "touchmove"), function(event) {
  1296. var height = $(this).height();
  1297. var scrollTop = $(this).scrollTop();
  1298. var percent = (scrollTop / $(this)[0].scrollHeight);
  1299. if (scrollTop === 0)
  1300. {
  1301. preview.scrollTop(0);
  1302. }
  1303. else if (scrollTop + height >= $(this)[0].scrollHeight - 16)
  1304. {
  1305. preview.scrollTop(preview[0].scrollHeight);
  1306. }
  1307. else
  1308. {
  1309. preview.scrollTop(preview[0].scrollHeight * percent);
  1310. }
  1311. $.proxy(settings.onscroll, _this)(event);
  1312. });
  1313. };
  1314. var cmUnbindScroll = function() {
  1315. codeMirror.find(".CodeMirror-scroll").unbind(mouseOrTouch("scroll", "touchmove"));
  1316. };
  1317. var previewBindScroll = function() {
  1318. preview.bind(mouseOrTouch("scroll", "touchmove"), function(event) {
  1319. var height = $(this).height();
  1320. var scrollTop = $(this).scrollTop();
  1321. var percent = (scrollTop / $(this)[0].scrollHeight);
  1322. var codeView = codeMirror.find(".CodeMirror-scroll");
  1323. if(scrollTop === 0)
  1324. {
  1325. codeView.scrollTop(0);
  1326. }
  1327. else if (scrollTop + height >= $(this)[0].scrollHeight)
  1328. {
  1329. codeView.scrollTop(codeView[0].scrollHeight);
  1330. }
  1331. else
  1332. {
  1333. codeView.scrollTop(codeView[0].scrollHeight * percent);
  1334. }
  1335. $.proxy(settings.onpreviewscroll, _this)(event);
  1336. });
  1337. };
  1338. var previewUnbindScroll = function() {
  1339. preview.unbind(mouseOrTouch("scroll", "touchmove"));
  1340. };
  1341. codeMirror.bind({
  1342. mouseover : cmBindScroll,
  1343. mouseout : cmUnbindScroll,
  1344. touchstart : cmBindScroll,
  1345. touchend : cmUnbindScroll
  1346. });
  1347. preview.bind({
  1348. mouseover : previewBindScroll,
  1349. mouseout : previewUnbindScroll,
  1350. touchstart : previewBindScroll,
  1351. touchend : previewUnbindScroll
  1352. });
  1353. return this;
  1354. },
  1355. bindChangeEvent : function() {
  1356. var _this = this;
  1357. var cm = this.cm;
  1358. var settings = this.settings;
  1359. if (!settings.syncScrolling) {
  1360. return this;
  1361. }
  1362. cm.on("change", function(_cm, changeObj) {
  1363. if (settings.watch)
  1364. {
  1365. _this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px");
  1366. }
  1367. timer = setTimeout(function() {
  1368. clearTimeout(timer);
  1369. _this.save();
  1370. timer = null;
  1371. }, settings.delay);
  1372. });
  1373. return this;
  1374. },
  1375. /**
  1376. * 加载队列完成之后的显示处理
  1377. * Display handle of the module queues loaded after.
  1378. *
  1379. * @param {Boolean} recreate 是否为重建编辑器
  1380. * @returns {editormd} 返回editormd的实例对象
  1381. */
  1382. loadedDisplay : function(recreate) {
  1383. recreate = recreate || false;
  1384. var _this = this;
  1385. var editor = this.editor;
  1386. var preview = this.preview;
  1387. var settings = this.settings;
  1388. this.containerMask.hide();
  1389. this.save();
  1390. if (settings.watch) {
  1391. preview.show();
  1392. }
  1393. editor.data("oldWidth", editor.width()).data("oldHeight", editor.height()); // 为了兼容Zepto
  1394. this.resize();
  1395. this.registerKeyMaps();
  1396. $(window).resize(function(){
  1397. _this.resize();
  1398. });
  1399. this.bindScrollEvent().bindChangeEvent();
  1400. if (!recreate)
  1401. {
  1402. $.proxy(settings.onload, this)();
  1403. }
  1404. this.state.loaded = true;
  1405. return this;
  1406. },
  1407. /**
  1408. * 设置编辑器的宽度
  1409. * Set editor width
  1410. *
  1411. * @param {Number|String} width 编辑器宽度值
  1412. * @returns {editormd} 返回editormd的实例对象
  1413. */
  1414. width : function(width) {
  1415. this.editor.css("width", (typeof width === "number") ? width + "px" : width);
  1416. this.resize();
  1417. return this;
  1418. },
  1419. /**
  1420. * 设置编辑器的高度
  1421. * Set editor height
  1422. *
  1423. * @param {Number|String} height 编辑器高度值
  1424. * @returns {editormd} 返回editormd的实例对象
  1425. */
  1426. height : function(height) {
  1427. this.editor.css("height", (typeof height === "number") ? height + "px" : height);
  1428. this.resize();
  1429. return this;
  1430. },
  1431. /**
  1432. * 调整编辑器的尺寸和布局
  1433. * Resize editor layout
  1434. *
  1435. * @param {Number|String} [width=null] 编辑器宽度值
  1436. * @param {Number|String} [height=null] 编辑器高度值
  1437. * @returns {editormd} 返回editormd的实例对象
  1438. */
  1439. resize : function(width, height) {
  1440. width = width || null;
  1441. height = height || null;
  1442. var state = this.state;
  1443. var editor = this.editor;
  1444. var preview = this.preview;
  1445. var toolbar = this.toolbar;
  1446. var settings = this.settings;
  1447. var codeMirror = this.codeMirror;
  1448. if (width)
  1449. {
  1450. editor.css("width", (typeof width === "number") ? width + "px" : width);
  1451. }
  1452. if (settings.autoHeight &amp;&amp; !state.fullscreen &amp;&amp; !state.preview)
  1453. {
  1454. editor.css("height", "auto");
  1455. codeMirror.css("height", "auto");
  1456. }
  1457. else
  1458. {
  1459. if (height)
  1460. {
  1461. editor.css("height", (typeof height === "number") ? height + "px" : height);
  1462. }
  1463. if (state.fullscreen)
  1464. {
  1465. editor.height($(window).height());
  1466. }
  1467. if (settings.toolbar &amp;&amp; !settings.readOnly)
  1468. {
  1469. codeMirror.css("margin-top", toolbar.height() + 1).height(editor.height() - toolbar.height());
  1470. }
  1471. else
  1472. {
  1473. codeMirror.css("margin-top", 0).height(editor.height());
  1474. }
  1475. }
  1476. if(settings.watch)
  1477. {
  1478. codeMirror.width(editor.width() / 2);
  1479. preview.width((!state.preview) ? editor.width() / 2 : editor.width());
  1480. this.previewContainer.css("padding", settings.autoHeight ? "20px 20px 50px 40px" : "20px");
  1481. if (settings.toolbar &amp;&amp; !settings.readOnly)
  1482. {
  1483. preview.css("top", toolbar.height());
  1484. }
  1485. else
  1486. {
  1487. preview.css("top", 0);
  1488. }
  1489. if (settings.autoHeight &amp;&amp; !state.fullscreen &amp;&amp; !state.preview)
  1490. {
  1491. preview.height("");
  1492. }
  1493. else
  1494. {
  1495. preview.height((settings.toolbar &amp;&amp; !settings.readOnly) ? editor.height() - toolbar.height() : editor.height());
  1496. }
  1497. }
  1498. else
  1499. {
  1500. codeMirror.width(editor.width());
  1501. preview.hide();
  1502. }
  1503. if (state.loaded)
  1504. {
  1505. $.proxy(settings.onresize, this)();
  1506. }
  1507. return this;
  1508. },
  1509. /**
  1510. * 解析和保存Markdown代码
  1511. * Parse &amp; Saving Markdown source code
  1512. *
  1513. * @returns {editormd} 返回editormd的实例对象
  1514. */
  1515. save : function() {
  1516. if (timer === null)
  1517. {
  1518. return this;
  1519. }
  1520. var _this = this;
  1521. var state = this.state;
  1522. var settings = this.settings;
  1523. var cm = this.cm;
  1524. var cmValue = cm.getValue();
  1525. var previewContainer = this.previewContainer;
  1526. if (settings.mode !== "gfm" &amp;&amp; settings.mode !== "markdown")
  1527. {
  1528. this.markdownTextarea.val(cmValue);
  1529. return this;
  1530. }
  1531. var marked = editormd.$marked;
  1532. var markdownToC = this.markdownToC = [];
  1533. var rendererOptions = this.markedRendererOptions = {
  1534. toc : settings.toc,
  1535. tocm : settings.tocm,
  1536. tocStartLevel : settings.tocStartLevel,
  1537. pageBreak : settings.pageBreak,
  1538. taskList : settings.taskList,
  1539. emoji : settings.emoji,
  1540. tex : settings.tex,
  1541. atLink : settings.atLink, // for @link
  1542. emailLink : settings.emailLink, // for mail address auto link
  1543. flowChart : settings.flowChart,
  1544. sequenceDiagram : settings.sequenceDiagram,
  1545. previewCodeHighlight : settings.previewCodeHighlight,
  1546. };
  1547. var markedOptions = this.markedOptions = {
  1548. renderer : editormd.markedRenderer(markdownToC, rendererOptions),
  1549. gfm : true,
  1550. tables : true,
  1551. breaks : true,
  1552. pedantic : false,
  1553. sanitize : (settings.htmlDecode) ? false : true, // 关闭忽略HTML标签,即开启识别HTML标签,默认为false
  1554. smartLists : true,
  1555. smartypants : true
  1556. };
  1557. marked.setOptions(markedOptions);
  1558. cmValue = editormd.filterHTMLTags(cmValue, settings.htmlDecode);
  1559. var newMarkdownDoc = editormd.$marked(cmValue, markedOptions);
  1560. //console.log("cmValue", cmValue, this.markdownTextarea, this.htmlTextarea);
  1561. this.markdownTextarea.text(cmValue);
  1562. cm.save();
  1563. if (settings.saveHTMLToTextarea)
  1564. {
  1565. this.htmlTextarea.text(newMarkdownDoc);
  1566. }
  1567. if(settings.watch || (!settings.watch &amp;&amp; state.preview))
  1568. {
  1569. previewContainer.html(newMarkdownDoc);
  1570. this.previewCodeHighlight();
  1571. if (settings.toc)
  1572. {
  1573. var tocContainer = (settings.tocContainer === "") ? previewContainer : $(settings.tocContainer);
  1574. var tocMenu = tocContainer.find("." + this.classPrefix + "toc-menu");
  1575. tocContainer.attr("previewContainer", (settings.tocContainer === "") ? "true" : "false");
  1576. if (settings.tocContainer !== "" &amp;&amp; tocMenu.length > 0)
  1577. {
  1578. tocMenu.remove();
  1579. }
  1580. editormd.markdownToCRenderer(markdownToC, tocContainer, settings.tocDropdown, settings.tocStartLevel);
  1581. if (settings.tocDropdown || tocContainer.find("." + this.classPrefix + "toc-menu").length > 0)
  1582. {
  1583. editormd.tocDropdownMenu(tocContainer, (settings.tocTitle !== "") ? settings.tocTitle : this.lang.tocTitle);
  1584. }
  1585. if (settings.tocContainer !== "")
  1586. {
  1587. previewContainer.find(".markdown-toc").css("border", "none");
  1588. }
  1589. }
  1590. if (settings.tex)
  1591. {
  1592. if (!editormd.kaTeXLoaded &amp;&amp; settings.autoLoadModules)
  1593. {
  1594. editormd.loadKaTeX(function() {
  1595. editormd.$katex = katex;
  1596. editormd.kaTeXLoaded = true;
  1597. _this.katexRender();
  1598. });
  1599. }
  1600. else
  1601. {
  1602. editormd.$katex = katex;
  1603. this.katexRender();
  1604. }
  1605. }
  1606. if (settings.flowChart || settings.sequenceDiagram)
  1607. {
  1608. flowchartTimer = setTimeout(function(){
  1609. clearTimeout(flowchartTimer);
  1610. _this.flowChartAndSequenceDiagramRender();
  1611. flowchartTimer = null;
  1612. }, 10);
  1613. }
  1614. if (state.loaded)
  1615. {
  1616. $.proxy(settings.onchange, this)();
  1617. }
  1618. }
  1619. return this;
  1620. },
  1621. /**
  1622. * 聚焦光标位置
  1623. * Focusing the cursor position
  1624. *
  1625. * @returns {editormd} 返回editormd的实例对象
  1626. */
  1627. focus : function() {
  1628. this.cm.focus();
  1629. return this;
  1630. },
  1631. /**
  1632. * 设置光标的位置
  1633. * Set cursor position
  1634. *
  1635. * @param {Object} cursor 要设置的光标位置键值对象,例:{line:1, ch:0}
  1636. * @returns {editormd} 返回editormd的实例对象
  1637. */
  1638. setCursor : function(cursor) {
  1639. this.cm.setCursor(cursor);
  1640. return this;
  1641. },
  1642. /**
  1643. * 获取当前光标的位置
  1644. * Get the current position of the cursor
  1645. *
  1646. * @returns {Cursor} 返回一个光标Cursor对象
  1647. */
  1648. getCursor : function() {
  1649. return this.cm.getCursor();
  1650. },
  1651. /**
  1652. * 设置光标选中的范围
  1653. * Set cursor selected ranges
  1654. *
  1655. * @param {Object} from 开始位置的光标键值对象,例:{line:1, ch:0}
  1656. * @param {Object} to 结束位置的光标键值对象,例:{line:1, ch:0}
  1657. * @returns {editormd} 返回editormd的实例对象
  1658. */
  1659. setSelection : function(from, to) {
  1660. this.cm.setSelection(from, to);
  1661. return this;
  1662. },
  1663. /**
  1664. * 获取光标选中的文本
  1665. * Get the texts from cursor selected
  1666. *
  1667. * @returns {String} 返回选中文本的字符串形式
  1668. */
  1669. getSelection : function() {
  1670. return this.cm.getSelection();
  1671. },
  1672. /**
  1673. * 设置光标选中的文本范围
  1674. * Set the cursor selection ranges
  1675. *
  1676. * @param {Array} ranges cursor selection ranges array
  1677. * @returns {Array} return this
  1678. */
  1679. setSelections : function(ranges) {
  1680. this.cm.setSelections(ranges);
  1681. return this;
  1682. },
  1683. /**
  1684. * 获取光标选中的文本范围
  1685. * Get the cursor selection ranges
  1686. *
  1687. * @returns {Array} return selection ranges array
  1688. */
  1689. getSelections : function() {
  1690. return this.cm.getSelections();
  1691. },
  1692. /**
  1693. * 替换当前光标选中的文本或在当前光标处插入新字符
  1694. * Replace the text at the current cursor selected or insert a new character at the current cursor position
  1695. *
  1696. * @param {String} value 要插入的字符值
  1697. * @returns {editormd} 返回editormd的实例对象
  1698. */
  1699. replaceSelection : function(value) {
  1700. this.cm.replaceSelection(value);
  1701. return this;
  1702. },
  1703. /**
  1704. * 在当前光标处插入新字符
  1705. * Insert a new character at the current cursor position
  1706. *
  1707. * 同replaceSelection()方法
  1708. * With the replaceSelection() method
  1709. *
  1710. * @param {String} value 要插入的字符值
  1711. * @returns {editormd} 返回editormd的实例对象
  1712. */
  1713. insertValue : function(value) {
  1714. this.replaceSelection(value);
  1715. return this;
  1716. },
  1717. /**
  1718. * 追加markdown
  1719. * append Markdown to editor
  1720. *
  1721. * @param {String} md 要追加的markdown源文档
  1722. * @returns {editormd} 返回editormd的实例对象
  1723. */
  1724. appendMarkdown : function(md) {
  1725. var settings = this.settings;
  1726. var cm = this.cm;
  1727. cm.setValue(cm.getValue() + md);
  1728. return this;
  1729. },
  1730. /**
  1731. * 设置和传入编辑器的markdown源文档
  1732. * Set Markdown source document
  1733. *
  1734. * @param {String} md 要传入的markdown源文档
  1735. * @returns {editormd} 返回editormd的实例对象
  1736. */
  1737. setMarkdown : function(md) {
  1738. this.cm.setValue(md || this.settings.markdown);
  1739. return this;
  1740. },
  1741. /**
  1742. * 获取编辑器的markdown源文档
  1743. * Set Editor.md markdown/CodeMirror value
  1744. *
  1745. * @returns {editormd} 返回editormd的实例对象
  1746. */
  1747. getMarkdown : function() {
  1748. return this.cm.getValue();
  1749. },
  1750. /**
  1751. * 获取编辑器的源文档
  1752. * Get CodeMirror value
  1753. *
  1754. * @returns {editormd} 返回editormd的实例对象
  1755. */
  1756. getValue : function() {
  1757. return this.cm.getValue();
  1758. },
  1759. /**
  1760. * 设置编辑器的源文档
  1761. * Set CodeMirror value
  1762. *
  1763. * @param {String} value set code/value/string/text
  1764. * @returns {editormd} 返回editormd的实例对象
  1765. */
  1766. setValue : function(value) {
  1767. this.cm.setValue(value);
  1768. return this;
  1769. },
  1770. /**
  1771. * 清空编辑器
  1772. * Empty CodeMirror editor container
  1773. *
  1774. * @returns {editormd} 返回editormd的实例对象
  1775. */
  1776. clear : function() {
  1777. this.cm.setValue("");
  1778. return this;
  1779. },
  1780. /**
  1781. * 获取解析后存放在Textarea的HTML源码
  1782. * Get parsed html code from Textarea
  1783. *
  1784. * @returns {String} 返回HTML源码
  1785. */
  1786. getHTML : function() {
  1787. if (!this.settings.saveHTMLToTextarea)
  1788. {
  1789. alert("Error: settings.saveHTMLToTextarea == false");
  1790. return false;
  1791. }
  1792. return this.htmlTextarea.val();
  1793. },
  1794. /**
  1795. * getHTML()的别名
  1796. * getHTML (alias)
  1797. *
  1798. * @returns {String} Return html code 返回HTML源码
  1799. */
  1800. getTextareaSavedHTML : function() {
  1801. return this.getHTML();
  1802. },
  1803. /**
  1804. * 获取预览窗口的HTML源码
  1805. * Get html from preview container
  1806. *
  1807. * @returns {editormd} 返回editormd的实例对象
  1808. */
  1809. getPreviewedHTML : function() {
  1810. if (!this.settings.watch)
  1811. {
  1812. alert("Error: settings.watch == false");
  1813. return false;
  1814. }
  1815. return this.previewContainer.html();
  1816. },
  1817. /**
  1818. * 开启实时预览
  1819. * Enable real-time watching
  1820. *
  1821. * @returns {editormd} 返回editormd的实例对象
  1822. */
  1823. watch : function(callback) {
  1824. var settings = this.settings;
  1825. if ($.inArray(settings.mode, ["gfm", "markdown"]) &lt; 0)
  1826. {
  1827. return this;
  1828. }
  1829. this.state.watching = settings.watch = true;
  1830. this.preview.show();
  1831. if (this.toolbar)
  1832. {
  1833. var watchIcon = settings.toolbarIconsClass.watch;
  1834. var unWatchIcon = settings.toolbarIconsClass.unwatch;
  1835. var icon = this.toolbar.find(".fa[name=watch]");
  1836. icon.parent().attr("title", settings.lang.toolbar.watch);
  1837. icon.removeClass(unWatchIcon).addClass(watchIcon);
  1838. }
  1839. this.codeMirror.css("border-right", "1px solid #ddd").width(this.editor.width() / 2);
  1840. timer = 0;
  1841. this.save().resize();
  1842. if (!settings.onwatch)
  1843. {
  1844. settings.onwatch = callback || function() {};
  1845. }
  1846. $.proxy(settings.onwatch, this)();
  1847. return this;
  1848. },
  1849. /**
  1850. * 关闭实时预览
  1851. * Disable real-time watching
  1852. *
  1853. * @returns {editormd} 返回editormd的实例对象
  1854. */
  1855. unwatch : function(callback) {
  1856. var settings = this.settings;
  1857. this.state.watching = settings.watch = false;
  1858. this.preview.hide();
  1859. if (this.toolbar)
  1860. {
  1861. var watchIcon = settings.toolbarIconsClass.watch;
  1862. var unWatchIcon = settings.toolbarIconsClass.unwatch;
  1863. var icon = this.toolbar.find(".fa[name=watch]");
  1864. icon.parent().attr("title", settings.lang.toolbar.unwatch);
  1865. icon.removeClass(watchIcon).addClass(unWatchIcon);
  1866. }
  1867. this.codeMirror.css("border-right", "none").width(this.editor.width());
  1868. this.resize();
  1869. if (!settings.onunwatch)
  1870. {
  1871. settings.onunwatch = callback || function() {};
  1872. }
  1873. $.proxy(settings.onunwatch, this)();
  1874. return this;
  1875. },
  1876. /**
  1877. * 显示编辑器
  1878. * Show editor
  1879. *
  1880. * @param {Function} [callback=function()] 回调函数
  1881. * @returns {editormd} 返回editormd的实例对象
  1882. */
  1883. show : function(callback) {
  1884. callback = callback || function() {};
  1885. var _this = this;
  1886. this.editor.show(0, function() {
  1887. $.proxy(callback, _this)();
  1888. });
  1889. return this;
  1890. },
  1891. /**
  1892. * 隐藏编辑器
  1893. * Hide editor
  1894. *
  1895. * @param {Function} [callback=function()] 回调函数
  1896. * @returns {editormd} 返回editormd的实例对象
  1897. */
  1898. hide : function(callback) {
  1899. callback = callback || function() {};
  1900. var _this = this;
  1901. this.editor.hide(0, function() {
  1902. $.proxy(callback, _this)();
  1903. });
  1904. return this;
  1905. },
  1906. /**
  1907. * 隐藏编辑器部分,只预览HTML
  1908. * Enter preview html state
  1909. *
  1910. * @returns {editormd} 返回editormd的实例对象
  1911. */
  1912. previewing : function() {
  1913. var _this = this;
  1914. var editor = this.editor;
  1915. var preview = this.preview;
  1916. var toolbar = this.toolbar;
  1917. var settings = this.settings;
  1918. var codeMirror = this.codeMirror;
  1919. if ($.inArray(settings.mode, ["gfm", "markdown"]) &lt; 0) {
  1920. return this;
  1921. }
  1922. if (settings.toolbar &amp;&amp; toolbar) {
  1923. toolbar.toggle();
  1924. toolbar.find(".fa[name=preview]").toggleClass("active");
  1925. }
  1926. codeMirror.toggle();
  1927. var escHandle = function(event) {
  1928. if (event.shiftKey &amp;&amp; event.keyCode === 27) {
  1929. _this.previewed();
  1930. }
  1931. };
  1932. if (codeMirror.css("display") === "none") // 为了兼容Zepto,而不使用codeMirror.is(":hidden")
  1933. {
  1934. this.state.preview = true;
  1935. if (this.state.fullscreen) {
  1936. preview.css("background", "#fff");
  1937. }
  1938. editor.find("." + this.classPrefix + "preview-close-btn").show().bind(editormd.mouseOrTouch("click", "touchend"), function(){
  1939. _this.previewed();
  1940. });
  1941. if (!settings.watch)
  1942. {
  1943. this.save();
  1944. }
  1945. preview.show().css({
  1946. position : "static",
  1947. top : 0,
  1948. width : editor.width(),
  1949. height : (settings.autoHeight &amp;&amp; !this.state.fullscreen) ? "auto" : editor.height()
  1950. });
  1951. if (this.state.loaded)
  1952. {
  1953. $.proxy(settings.onpreviewing, this)();
  1954. }
  1955. $(window).bind("keyup", escHandle);
  1956. }
  1957. else
  1958. {
  1959. $(window).unbind("keyup", escHandle);
  1960. this.previewed();
  1961. }
  1962. },
  1963. /**
  1964. * 显示编辑器部分,退出只预览HTML
  1965. * Exit preview html state
  1966. *
  1967. * @returns {editormd} 返回editormd的实例对象
  1968. */
  1969. previewed : function() {
  1970. var editor = this.editor;
  1971. var preview = this.preview;
  1972. var toolbar = this.toolbar;
  1973. var settings = this.settings;
  1974. var previewCloseBtn = editor.find("." + this.classPrefix + "preview-close-btn");
  1975. this.state.preview = false;
  1976. this.codeMirror.show();
  1977. if (settings.toolbar) {
  1978. toolbar.show();
  1979. }
  1980. preview[(settings.watch) ? "show" : "hide"]();
  1981. previewCloseBtn.hide().unbind(editormd.mouseOrTouch("click", "touchend"));
  1982. preview.css({
  1983. background : null,
  1984. position : "absolute",
  1985. width : editor.width() / 2,
  1986. height : (settings.autoHeight &amp;&amp; !this.state.fullscreen) ? "auto" : editor.height() - toolbar.height(),
  1987. top : (settings.toolbar) ? toolbar.height() : 0
  1988. });
  1989. if (this.state.loaded)
  1990. {
  1991. $.proxy(settings.onpreviewed, this)();
  1992. }
  1993. return this;
  1994. },
  1995. /**
  1996. * 编辑器全屏显示
  1997. * Fullscreen show
  1998. *
  1999. * @returns {editormd} 返回editormd的实例对象
  2000. */
  2001. fullscreen : function() {
  2002. var _this = this;
  2003. var state = this.state;
  2004. var editor = this.editor;
  2005. var preview = this.preview;
  2006. var toolbar = this.toolbar;
  2007. var settings = this.settings;
  2008. var fullscreenClass = this.classPrefix + "fullscreen";
  2009. if (toolbar) {
  2010. toolbar.find(".fa[name=fullscreen]").parent().toggleClass("active");
  2011. }
  2012. var escHandle = function(event) {
  2013. if (!event.shiftKey &amp;&amp; event.keyCode === 27)
  2014. {
  2015. if (state.fullscreen)
  2016. {
  2017. _this.fullscreenExit();
  2018. }
  2019. }
  2020. };
  2021. if (!editor.hasClass(fullscreenClass))
  2022. {
  2023. state.fullscreen = true;
  2024. $("html,body").css("overflow", "hidden");
  2025. editor.css({
  2026. position : "fixed",
  2027. top : 0,
  2028. left : 0,
  2029. margin : 0,
  2030. border : "none",
  2031. width : $(window).width(),
  2032. height : $(window).height()
  2033. }).addClass(fullscreenClass);
  2034. this.resize();
  2035. $.proxy(settings.onfullscreen, this)();
  2036. $(window).bind("keyup", escHandle);
  2037. }
  2038. else
  2039. {
  2040. $(window).unbind("keyup", escHandle);
  2041. this.fullscreenExit();
  2042. }
  2043. return this;
  2044. },
  2045. /**
  2046. * 编辑器退出全屏显示
  2047. * Exit fullscreen state
  2048. *
  2049. * @returns {editormd} 返回editormd的实例对象
  2050. */
  2051. fullscreenExit : function() {
  2052. var editor = this.editor;
  2053. var settings = this.settings;
  2054. var toolbar = this.toolbar;
  2055. var fullscreenClass = this.classPrefix + "fullscreen";
  2056. this.state.fullscreen = false;
  2057. if (toolbar) {
  2058. toolbar.find(".fa[name=fullscreen]").parent().removeClass("active");
  2059. }
  2060. $("html,body").css("overflow", "");
  2061. editor.css({
  2062. position : "",
  2063. top : "",
  2064. left : "",
  2065. margin : "0 auto 15px",
  2066. width : editor.data("oldWidth"),
  2067. height : editor.data("oldHeight"),
  2068. border : "1px solid #ddd"
  2069. }).removeClass(fullscreenClass);
  2070. this.resize();
  2071. $.proxy(settings.onfullscreenExit, this)();
  2072. return this;
  2073. },
  2074. /**
  2075. * 加载并执行插件
  2076. * Load and execute the plugin
  2077. *
  2078. * @param {String} name plugin name / function name
  2079. * @param {String} path plugin load path
  2080. * @returns {editormd} 返回editormd的实例对象
  2081. */
  2082. executePlugin : function(name, path) {
  2083. var _this = this;
  2084. var cm = this.cm;
  2085. var settings = this.settings;
  2086. path = settings.pluginPath + path;
  2087. if (typeof define === "function")
  2088. {
  2089. if (typeof this[name] === "undefined")
  2090. {
  2091. alert("Error: " + name + " plugin is not found, you are not load this plugin.");
  2092. return this;
  2093. }
  2094. this[name](cm);
  2095. return this;
  2096. }
  2097. if ($.inArray(path, editormd.loadFiles.plugin) &lt; 0)
  2098. {
  2099. editormd.loadPlugin(path, function() {
  2100. editormd.loadPlugins[name] = _this[name];
  2101. _this[name](cm);
  2102. });
  2103. }
  2104. else
  2105. {
  2106. $.proxy(editormd.loadPlugins[name], this)(cm);
  2107. }
  2108. return this;
  2109. },
  2110. /**
  2111. * 搜索替换
  2112. * Search &amp; replace
  2113. *
  2114. * @param {String} command CodeMirror serach commands, "find, fintNext, fintPrev, clearSearch, replace, replaceAll"
  2115. * @returns {editormd} return this
  2116. */
  2117. search : function(command) {
  2118. var settings = this.settings;
  2119. if (!settings.searchReplace)
  2120. {
  2121. alert("Error: settings.searchReplace == false");
  2122. return this;
  2123. }
  2124. if (!settings.readOnly)
  2125. {
  2126. this.cm.execCommand(command || "find");
  2127. }
  2128. return this;
  2129. },
  2130. searchReplace : function() {
  2131. this.search("replace");
  2132. return this;
  2133. },
  2134. searchReplaceAll : function() {
  2135. this.search("replaceAll");
  2136. return this;
  2137. }
  2138. };
  2139. editormd.fn.init.prototype = editormd.fn;
  2140. /**
  2141. * 锁屏
  2142. * lock screen when dialog opening
  2143. *
  2144. * @returns {void}
  2145. */
  2146. editormd.dialogLockScreen = function() {
  2147. var settings = this.settings || {dialogLockScreen : true};
  2148. if (settings.dialogLockScreen)
  2149. {
  2150. $("html,body").css("overflow", "hidden");
  2151. }
  2152. };
  2153. /**
  2154. * 显示透明背景层
  2155. * Display mask layer when dialog opening
  2156. *
  2157. * @param {Object} dialog dialog jQuery object
  2158. * @returns {void}
  2159. */
  2160. editormd.dialogShowMask = function(dialog) {
  2161. var editor = this.editor;
  2162. var settings = this.settings || {dialogShowMask : true};
  2163. dialog.css({
  2164. top : ($(window).height() - dialog.height()) / 2 + "px",
  2165. left : ($(window).width() - dialog.width()) / 2 + "px"
  2166. });
  2167. if (settings.dialogShowMask) {
  2168. editor.children("." + this.classPrefix + "mask").css("z-index", parseInt(dialog.css("z-index")) - 1).show();
  2169. }
  2170. };
  2171. editormd.toolbarHandlers = {
  2172. undo : function() {
  2173. this.cm.undo();
  2174. },
  2175. redo : function() {
  2176. this.cm.redo();
  2177. },
  2178. bold : function() {
  2179. var cm = this.cm;
  2180. var cursor = cm.getCursor();
  2181. var selection = cm.getSelection();
  2182. cm.replaceSelection("**" + selection + "**");
  2183. if(selection === "") {
  2184. cm.setCursor(cursor.line, cursor.ch + 2);
  2185. }
  2186. },
  2187. del : function() {
  2188. var cm = this.cm;
  2189. var cursor = cm.getCursor();
  2190. var selection = cm.getSelection();
  2191. cm.replaceSelection("~~" + selection + "~~");
  2192. if(selection === "") {
  2193. cm.setCursor(cursor.line, cursor.ch + 2);
  2194. }
  2195. },
  2196. italic : function() {
  2197. var cm = this.cm;
  2198. var cursor = cm.getCursor();
  2199. var selection = cm.getSelection();
  2200. cm.replaceSelection("*" + selection + "*");
  2201. if(selection === "") {
  2202. cm.setCursor(cursor.line, cursor.ch + 1);
  2203. }
  2204. },
  2205. quote : function() {
  2206. var cm = this.cm;
  2207. var cursor = cm.getCursor();
  2208. var selection = cm.getSelection();
  2209. cm.replaceSelection("> " + selection);
  2210. cm.setCursor(cursor.line, (selection === "") ? cursor.ch + 2 : cursor.ch + selection.length + 2);
  2211. },
  2212. ucfirst : function() {
  2213. var cm = this.cm;
  2214. var selection = cm.getSelection();
  2215. var selections = cm.listSelections();
  2216. cm.replaceSelection(editormd.firstUpperCase(selection));
  2217. cm.setSelections(selections);
  2218. },
  2219. ucwords : function() {
  2220. var cm = this.cm;
  2221. var selection = cm.getSelection();
  2222. var selections = cm.listSelections();
  2223. cm.replaceSelection(editormd.wordsFirstUpperCase(selection));
  2224. cm.setSelections(selections);
  2225. },
  2226. uppercase : function() {
  2227. var cm = this.cm;
  2228. var selection = cm.getSelection();
  2229. var selections = cm.listSelections();
  2230. cm.replaceSelection(selection.toUpperCase());
  2231. cm.setSelections(selections);
  2232. },
  2233. lowercase : function() {
  2234. var cm = this.cm;
  2235. var cursor = cm.getCursor();
  2236. var selection = cm.getSelection();
  2237. var selections = cm.listSelections();
  2238. cm.replaceSelection(selection.toLowerCase());
  2239. cm.setSelections(selections);
  2240. },
  2241. h1 : function() {
  2242. var cm = this.cm;
  2243. var selection = cm.getSelection();
  2244. cm.replaceSelection("# " + selection);
  2245. },
  2246. h2 : function() {
  2247. var cm = this.cm;
  2248. var selection = cm.getSelection();
  2249. cm.replaceSelection("## " + selection);
  2250. },
  2251. h3 : function() {
  2252. var cm = this.cm;
  2253. var selection = cm.getSelection();
  2254. cm.replaceSelection("### " + selection);
  2255. },
  2256. h4 : function() {
  2257. var cm = this.cm;
  2258. var selection = cm.getSelection();
  2259. cm.replaceSelection("#### " + selection);
  2260. },
  2261. h5 : function() {
  2262. var cm = this.cm;
  2263. var selection = cm.getSelection();
  2264. cm.replaceSelection("##### " + selection);
  2265. },
  2266. h6 : function() {
  2267. var cm = this.cm;
  2268. var selection = cm.getSelection();
  2269. cm.replaceSelection("###### " + selection);
  2270. },
  2271. "list-ul" : function() {
  2272. var cm = this.cm;
  2273. var cursor = cm.getCursor();
  2274. var selection = cm.getSelection();
  2275. if (selection === "")
  2276. {
  2277. cm.replaceSelection("- " + selection);
  2278. }
  2279. else
  2280. {
  2281. var selectionText = selection.split("\n");
  2282. for (var i = 0, len = selectionText.length; i &lt; len; i++)
  2283. {
  2284. selectionText[i] = (selectionText[i] === "") ? "" : "- " + selectionText[i];
  2285. }
  2286. cm.replaceSelection(selectionText.join("\n"));
  2287. }
  2288. },
  2289. "list-ol" : function() {
  2290. var cm = this.cm;
  2291. var cursor = cm.getCursor();
  2292. var selection = cm.getSelection();
  2293. if(selection === "")
  2294. {
  2295. cm.replaceSelection("1. " + selection);
  2296. }
  2297. else
  2298. {
  2299. var selectionText = selection.split("\n");
  2300. for (var i = 0, len = selectionText.length; i &lt; len; i++)
  2301. {
  2302. selectionText[i] = (selectionText[i] === "") ? "" : (i+1) + ". " + selectionText[i];
  2303. }
  2304. cm.replaceSelection(selectionText.join("\n"));
  2305. }
  2306. },
  2307. hr : function() {
  2308. var cm = this.cm;
  2309. var cursor = cm.getCursor();
  2310. var selection = cm.getSelection();
  2311. cm.replaceSelection("------------");
  2312. },
  2313. tex : function() {
  2314. if (!this.settings.tex)
  2315. {
  2316. alert("settings.tex === false");
  2317. return this;
  2318. }
  2319. var cm = this.cm;
  2320. var cursor = cm.getCursor();
  2321. var selection = cm.getSelection();
  2322. cm.replaceSelection("$$" + selection + "$$");
  2323. if(selection === "") {
  2324. cm.setCursor(cursor.line, cursor.ch + 2);
  2325. }
  2326. },
  2327. link : function() {
  2328. this.executePlugin("linkDialog", "link-dialog/link-dialog");
  2329. },
  2330. "reference-link" : function() {
  2331. this.executePlugin("referenceLinkDialog", "reference-link-dialog/reference-link-dialog");
  2332. },
  2333. pagebreak : function() {
  2334. if (!this.settings.pageBreak)
  2335. {
  2336. alert("settings.pageBreak === false");
  2337. return this;
  2338. }
  2339. var cm = this.cm;
  2340. var selection = cm.getSelection();
  2341. cm.replaceSelection("\r\n[========]\r\n");
  2342. },
  2343. image : function() {
  2344. this.executePlugin("imageDialog", "image-dialog/image-dialog");
  2345. },
  2346. code : function() {
  2347. var cm = this.cm;
  2348. var cursor = cm.getCursor();
  2349. var selection = cm.getSelection();
  2350. cm.replaceSelection("`" + selection + "`");
  2351. if (selection === "") {
  2352. cm.setCursor(cursor.line, cursor.ch + 1);
  2353. }
  2354. },
  2355. "code-block" : function() {
  2356. this.executePlugin("codeBlockDialog", "code-block-dialog/code-block-dialog");
  2357. },
  2358. "preformatted-text" : function() {
  2359. this.executePlugin("preformattedTextDialog", "preformatted-text-dialog/preformatted-text-dialog");
  2360. },
  2361. table : function() {
  2362. this.executePlugin("tableDialog", "table-dialog/table-dialog");
  2363. },
  2364. datetime : function() {
  2365. var cm = this.cm;
  2366. var selection = cm.getSelection();
  2367. var date = new Date();
  2368. var langName = this.settings.lang.name;
  2369. var datefmt = editormd.dateFormat() + " " + editormd.dateFormat((langName === "zh-cn" || langName === "zh-tw") ? "cn-week-day" : "week-day");
  2370. cm.replaceSelection(datefmt);
  2371. },
  2372. emoji : function() {
  2373. this.executePlugin("emojiDialog", "emoji-dialog/emoji-dialog");
  2374. },
  2375. "html-entities" : function() {
  2376. this.executePlugin("htmlEntitiesDialog", "html-entities-dialog/html-entities-dialog");
  2377. },
  2378. "goto-line" : function() {
  2379. this.executePlugin("gotoLineDialog", "goto-line-dialog/goto-line-dialog");
  2380. },
  2381. watch : function() {
  2382. this[this.settings.watch ? "unwatch" : "watch"]();
  2383. },
  2384. preview : function() {
  2385. this.previewing();
  2386. },
  2387. fullscreen : function() {
  2388. this.fullscreen();
  2389. },
  2390. clear : function() {
  2391. this.clear();
  2392. },
  2393. search : function() {
  2394. this.search();
  2395. },
  2396. help : function() {
  2397. this.executePlugin("helpDialog", "help-dialog/help-dialog");
  2398. },
  2399. info : function() {
  2400. this.showInfoDialog();
  2401. }
  2402. };
  2403. editormd.keyMaps = {
  2404. "Ctrl-1" : "h1",
  2405. "Ctrl-2" : "h2",
  2406. "Ctrl-3" : "h3",
  2407. "Ctrl-4" : "h4",
  2408. "Ctrl-5" : "h5",
  2409. "Ctrl-6" : "h6",
  2410. "Ctrl-B" : "bold", // if this is string == editormd.toolbarHandlers.xxxx
  2411. "Ctrl-D" : "datetime",
  2412. "Ctrl-E" : function() { // emoji
  2413. var cm = this.cm;
  2414. var cursor = cm.getCursor();
  2415. var selection = cm.getSelection();
  2416. if (!this.settings.emoji)
  2417. {
  2418. alert("Error: settings.emoji == false");
  2419. return ;
  2420. }
  2421. cm.replaceSelection(":" + selection + ":");
  2422. if (selection === "") {
  2423. cm.setCursor(cursor.line, cursor.ch + 1);
  2424. }
  2425. },
  2426. "Ctrl-Alt-G" : "goto-line",
  2427. "Ctrl-H" : "hr",
  2428. "Ctrl-I" : "italic",
  2429. "Ctrl-K" : "code",
  2430. "Ctrl-L" : function() {
  2431. var cm = this.cm;
  2432. var cursor = cm.getCursor();
  2433. var selection = cm.getSelection();
  2434. var title = (selection === "") ? "" : " \""+selection+"\"";
  2435. cm.replaceSelection("[" + selection + "]("+title+")");
  2436. if (selection === "") {
  2437. cm.setCursor(cursor.line, cursor.ch + 1);
  2438. }
  2439. },
  2440. "Ctrl-U" : "list-ul",
  2441. "Shift-Ctrl-A" : function() {
  2442. var cm = this.cm;
  2443. var cursor = cm.getCursor();
  2444. var selection = cm.getSelection();
  2445. if (!this.settings.atLink)
  2446. {
  2447. alert("Error: settings.atLink == false");
  2448. return ;
  2449. }
  2450. cm.replaceSelection("@" + selection);
  2451. if (selection === "") {
  2452. cm.setCursor(cursor.line, cursor.ch + 1);
  2453. }
  2454. },
  2455. "Shift-Ctrl-C" : "code",
  2456. "Shift-Ctrl-Q" : "quote",
  2457. "Shift-Ctrl-S" : "del",
  2458. "Shift-Ctrl-K" : "tex", // KaTeX
  2459. "Shift-Alt-C" : function() {
  2460. var cm = this.cm;
  2461. var cursor = cm.getCursor();
  2462. var selection = cm.getSelection();
  2463. cm.replaceSelection(["```", selection, "```"].join("\n"));
  2464. if (selection === "") {
  2465. cm.setCursor(cursor.line, cursor.ch + 3);
  2466. }
  2467. },
  2468. "Shift-Ctrl-Alt-C" : "code-block",
  2469. "Shift-Ctrl-H" : "html-entities",
  2470. "Shift-Alt-H" : "help",
  2471. "Shift-Ctrl-E" : "emoji",
  2472. "Shift-Ctrl-U" : "uppercase",
  2473. "Shift-Alt-U" : "ucwords",
  2474. "Shift-Ctrl-Alt-U" : "ucfirst",
  2475. "Shift-Alt-L" : "lowercase",
  2476. "Shift-Ctrl-I" : function() {
  2477. var cm = this.cm;
  2478. var cursor = cm.getCursor();
  2479. var selection = cm.getSelection();
  2480. var title = (selection === "") ? "" : " \""+selection+"\"";
  2481. cm.replaceSelection("![" + selection + "]("+title+")");
  2482. if (selection === "") {
  2483. cm.setCursor(cursor.line, cursor.ch + 4);
  2484. }
  2485. },
  2486. "Shift-Ctrl-Alt-I" : "image",
  2487. "Shift-Ctrl-L" : "link",
  2488. "Shift-Ctrl-O" : "list-ol",
  2489. "Shift-Ctrl-P" : "preformatted-text",
  2490. "Shift-Ctrl-T" : "table",
  2491. "Shift-Alt-P" : "pagebreak",
  2492. "F9" : "watch",
  2493. "F10" : "preview",
  2494. "F11" : "fullscreen",
  2495. };
  2496. /**
  2497. * 清除字符串两边的空格
  2498. * Clear the space of strings both sides.
  2499. *
  2500. * @param {String} str string
  2501. * @returns {String} trimed string
  2502. */
  2503. var trim = function(str) {
  2504. return (!String.prototype.trim) ? str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "") : str.trim();
  2505. };
  2506. editormd.trim = trim;
  2507. /**
  2508. * 所有单词首字母大写
  2509. * Words first to uppercase
  2510. *
  2511. * @param {String} str string
  2512. * @returns {String} string
  2513. */
  2514. var ucwords = function (str) {
  2515. return str.toLowerCase().replace(/\b(\w)|\s(\w)/g, function($1) {
  2516. return $1.toUpperCase();
  2517. });
  2518. };
  2519. editormd.ucwords = editormd.wordsFirstUpperCase = ucwords;
  2520. /**
  2521. * 字符串首字母大写
  2522. * Only string first char to uppercase
  2523. *
  2524. * @param {String} str string
  2525. * @returns {String} string
  2526. */
  2527. var firstUpperCase = function(str) {
  2528. return str.toLowerCase().replace(/\b(\w)/, function($1){
  2529. return $1.toUpperCase();
  2530. });
  2531. };
  2532. var ucfirst = firstUpperCase;
  2533. editormd.firstUpperCase = editormd.ucfirst = firstUpperCase;
  2534. editormd.urls = {
  2535. atLinkBase : "https://github.com/"
  2536. };
  2537. editormd.regexs = {
  2538. atLink : /@(\w+)/g,
  2539. email : /(\w+)@(\w+)\.(\w+)\.?(\w+)?/g,
  2540. emailLink : /(mailto:)?([\w\.\_]+)@(\w+)\.(\w+)\.?(\w+)?/g,
  2541. emoji : /:([\w\+-]+):/g,
  2542. emojiDatetime : /(\d{2}:\d{2}:\d{2})/g,
  2543. twemoji : /:(tw-([\w]+)-?(\w+)?):/g,
  2544. fontAwesome : /:(fa-([\w]+)(-(\w+)){0,}):/g,
  2545. editormdLogo : /:(editormd-logo-?(\w+)?):/g,
  2546. pageBreak : /^\[[=]{8,}\]$/
  2547. };
  2548. // Emoji graphics files url path
  2549. editormd.emoji = {
  2550. path : "http://www.emoji-cheat-sheet.com/graphics/emojis/",
  2551. ext : ".png"
  2552. };
  2553. // Twitter Emoji (Twemoji) graphics files url path
  2554. editormd.twemoji = {
  2555. path : "http://twemoji.maxcdn.com/36x36/",
  2556. ext : ".png"
  2557. };
  2558. /**
  2559. * 自定义marked的解析器
  2560. * Custom Marked renderer rules
  2561. *
  2562. * @param {Array} markdownToC 传入用于接收TOC的数组
  2563. * @returns {Renderer} markedRenderer 返回marked的Renderer自定义对象
  2564. */
  2565. editormd.markedRenderer = function(markdownToC, options) {
  2566. var defaults = {
  2567. toc : true, // Table of contents
  2568. tocm : false,
  2569. tocStartLevel : 1, // Said from H1 to create ToC
  2570. pageBreak : true,
  2571. atLink : true, // for @link
  2572. emailLink : true, // for mail address auto link
  2573. taskList : false, // Enable Github Flavored Markdown task lists
  2574. emoji : false, // :emoji: , Support Twemoji, fontAwesome, Editor.md logo emojis.
  2575. tex : false, // TeX(LaTeX), based on KaTeX
  2576. flowChart : false, // flowChart.js only support IE9+
  2577. sequenceDiagram : false, // sequenceDiagram.js only support IE9+
  2578. };
  2579. var settings = $.extend(defaults, options || {});
  2580. var marked = editormd.$marked;
  2581. var markedRenderer = new marked.Renderer();
  2582. markdownToC = markdownToC || [];
  2583. var regexs = editormd.regexs;
  2584. var atLinkReg = regexs.atLink;
  2585. var emojiReg = regexs.emoji;
  2586. var emailReg = regexs.email;
  2587. var emailLinkReg = regexs.emailLink;
  2588. var twemojiReg = regexs.twemoji;
  2589. var faIconReg = regexs.fontAwesome;
  2590. var editormdLogoReg = regexs.editormdLogo;
  2591. var pageBreakReg = regexs.pageBreak;
  2592. markedRenderer.emoji = function(text) {
  2593. text = text.replace(editormd.regexs.emojiDatetime, function($1) {
  2594. return $1.replace(/:/g, "&amp;#58;");
  2595. });
  2596. var matchs = text.match(emojiReg);
  2597. if (!matchs || !settings.emoji) {
  2598. return text;
  2599. }
  2600. for (var i = 0, len = matchs.length; i &lt; len; i++)
  2601. {
  2602. if (matchs[i] === ":+1:") {
  2603. matchs[i] = ":\\+1:";
  2604. }
  2605. text = text.replace(new RegExp(matchs[i]), function($1, $2){
  2606. var faMatchs = $1.match(faIconReg);
  2607. var name = $1.replace(/:/g, "");
  2608. if (faMatchs)
  2609. {
  2610. for (var fa = 0, len1 = faMatchs.length; fa &lt; len1; fa++)
  2611. {
  2612. var faName = faMatchs[fa].replace(/:/g, "");
  2613. return "&lt;i class=\"fa " + faName + " fa-emoji\" title=\"" + faName.replace("fa-", "") + "\">&lt;/i>";
  2614. }
  2615. }
  2616. else
  2617. {
  2618. var emdlogoMathcs = $1.match(editormdLogoReg);
  2619. var twemojiMatchs = $1.match(twemojiReg);
  2620. if (emdlogoMathcs)
  2621. {
  2622. for (var x = 0, len2 = emdlogoMathcs.length; x &lt; len2; x++)
  2623. {
  2624. var logoName = emdlogoMathcs[x].replace(/:/g, "");
  2625. return "&lt;i class=\"" + logoName + "\" title=\"Editor.md logo (" + logoName + ")\">&lt;/i>";
  2626. }
  2627. }
  2628. else if (twemojiMatchs)
  2629. {
  2630. for (var t = 0, len3 = twemojiMatchs.length; t &lt; len3; t++)
  2631. {
  2632. var twe = twemojiMatchs[t].replace(/:/g, "").replace("tw-", "");
  2633. return "&lt;img src=\"" + editormd.twemoji.path + twe + editormd.twemoji.ext + "\" title=\"twemoji-" + twe + "\" alt=\"twemoji-" + twe + "\" class=\"emoji twemoji\" />";
  2634. }
  2635. }
  2636. else
  2637. {
  2638. var src = (name === "+1") ? "plus1" : name;
  2639. src = (src === "black_large_square") ? "black_square" : src;
  2640. return "&lt;img src=\"" + editormd.emoji.path + src + editormd.emoji.ext + "\" class=\"emoji\" title=\"&amp;#58;" + name + "&amp;#58;\" alt=\"&amp;#58;" + name + "&amp;#58;\" />";
  2641. }
  2642. }
  2643. });
  2644. }
  2645. return text;
  2646. };
  2647. markedRenderer.atLink = function(text) {
  2648. if (atLinkReg.test(text))
  2649. {
  2650. if (settings.atLink)
  2651. {
  2652. text = text.replace(emailReg, function($1, $2, $3, $4) {
  2653. return $1.replace(/@/g, "_#_&amp;#64;_#_");
  2654. });
  2655. text = text.replace(atLinkReg, function($1, $2) {
  2656. return "&lt;a href=\"" + editormd.urls.atLinkBase + "" + $2 + "\" title=\"&amp;#64;" + $2 + "\" class=\"at-link\">" + $1 + "&lt;/a>";
  2657. }).replace(/_#_&amp;#64;_#_/g, "@");
  2658. }
  2659. if (settings.emailLink)
  2660. {
  2661. text = text.replace(emailLinkReg, function($1, $2, $3, $4, $5) {
  2662. return (!$2 &amp;&amp; $.inArray($5, "jpg|jpeg|png|gif|webp|ico|icon|pdf".split("|")) &lt; 0) ? "&lt;a href=\"mailto:" + $1 + "\">"+$1+"&lt;/a>" : $1;
  2663. });
  2664. }
  2665. return text;
  2666. }
  2667. return text;
  2668. };
  2669. markedRenderer.link = function (href, title, text) {
  2670. if (this.options.sanitize) {
  2671. try {
  2672. var prot = decodeURIComponent(unescape(href)).replace(/[^\w:]/g,"").toLowerCase();
  2673. } catch(e) {
  2674. return "";
  2675. }
  2676. if (prot.indexOf("javascript:") === 0) {
  2677. return "";
  2678. }
  2679. }
  2680. var out = "&lt;a href=\"" + href + "\"";
  2681. if (atLinkReg.test(title) || atLinkReg.test(text))
  2682. {
  2683. if (title)
  2684. {
  2685. out += " title=\"" + title.replace(/@/g, "&amp;#64;");
  2686. }
  2687. return out + "\">" + text.replace(/@/g, "&amp;#64;") + "&lt;/a>";
  2688. }
  2689. if (title) {
  2690. out += " title=\"" + title + "\"";
  2691. }
  2692. out += ">" + text + "&lt;/a>";
  2693. return out;
  2694. };
  2695. markedRenderer.heading = function(text, level, raw) {
  2696. var linkText = text;
  2697. var hasLinkReg = /\s*\&lt;a\s*href\=\"(.*)\"\s*([^\>]*)\>(.*)\&lt;\/a\>\s*/;
  2698. var getLinkTextReg = /\s*\&lt;a\s*([^\>]+)\>([^\>]*)\&lt;\/a\>\s*/g;
  2699. if (hasLinkReg.test(text))
  2700. {
  2701. var tempText = [];
  2702. text = text.split(/\&lt;a\s*([^\>]+)\>([^\>]*)\&lt;\/a\>/);
  2703. for (var i = 0, len = text.length; i &lt; len; i++)
  2704. {
  2705. tempText.push(text[i].replace(/\s*href\=\"(.*)\"\s*/g, ""));
  2706. }
  2707. text = tempText.join(" ");
  2708. }
  2709. text = trim(text);
  2710. var escapedText = text.toLowerCase().replace(/[^\w]+/g, "-");
  2711. var toc = {
  2712. text : text,
  2713. level : level,
  2714. slug : escapedText
  2715. };
  2716. var isChinese = /^[\u4e00-\u9fa5]+$/.test(text);
  2717. var id = (isChinese) ? escape(text).replace(/\%/g, "") : text.toLowerCase().replace(/[^\w]+/g, "-");
  2718. markdownToC.push(toc);
  2719. var headingHTML = "&lt;h" + level + " id=\"h"+ level + "-" + this.options.headerPrefix + id +"\">";
  2720. headingHTML += "&lt;a name=\"" + text + "\" class=\"reference-link\">&lt;/a>";
  2721. headingHTML += "&lt;span class=\"header-link octicon octicon-link\">&lt;/span>";
  2722. headingHTML += (hasLinkReg) ? this.atLink(this.emoji(linkText)) : this.atLink(this.emoji(text));
  2723. headingHTML += "&lt;/h" + level + ">";
  2724. return headingHTML;
  2725. };
  2726. markedRenderer.pageBreak = function(text) {
  2727. if (pageBreakReg.test(text) &amp;&amp; settings.pageBreak)
  2728. {
  2729. text = "&lt;hr style=\"page-break-after:always;\" class=\"page-break editormd-page-break\" />";
  2730. }
  2731. return text;
  2732. };
  2733. markedRenderer.paragraph = function(text) {
  2734. var isTeXInline = /\$\$(.*)\$\$/g.test(text);
  2735. var isTeXLine = /^\$\$(.*)\$\$$/.test(text);
  2736. var isTeXAddClass = (isTeXLine) ? " class=\"" + editormd.classNames.tex + "\"" : "";
  2737. var isToC = (settings.tocm) ? /^(\[TOC\]|\[TOCM\])$/.test(text) : /^\[TOC\]$/.test(text);
  2738. var isToCMenu = /^\[TOCM\]$/.test(text);
  2739. if (!isTeXLine &amp;&amp; isTeXInline)
  2740. {
  2741. text = text.replace(/(\$\$([^\$]*)\$\$)+/g, function($1, $2) {
  2742. return "&lt;span class=\"" + editormd.classNames.tex + "\">" + $2.replace(/\$/g, "") + "&lt;/span>";
  2743. });
  2744. }
  2745. else
  2746. {
  2747. text = (isTeXLine) ? text.replace(/\$/g, "") : text;
  2748. }
  2749. var tocHTML = "&lt;div class=\"markdown-toc editormd-markdown-toc\">" + text + "&lt;/div>";
  2750. return (isToC) ? ( (isToCMenu) ? "&lt;div class=\"editormd-toc-menu\">" + tocHTML + "&lt;/div>&lt;br/>" : tocHTML )
  2751. : ( (pageBreakReg.test(text)) ? this.pageBreak(text) : "&lt;p" + isTeXAddClass + ">" + this.atLink(this.emoji(text)) + "&lt;/p>\n" );
  2752. };
  2753. markedRenderer.code = function (code, lang, escaped) {
  2754. if (lang === "seq" || lang === "sequence")
  2755. {
  2756. return "&lt;div class=\"sequence-diagram\">" + code + "&lt;/div>";
  2757. }
  2758. else if ( lang === "flow")
  2759. {
  2760. return "&lt;div class=\"flowchart\">" + code + "&lt;/div>";
  2761. }
  2762. else
  2763. {
  2764. return marked.Renderer.prototype.code.apply(this, arguments);
  2765. }
  2766. };
  2767. markedRenderer.tablecell = function(content, flags) {
  2768. var type = (flags.header) ? "th" : "td";
  2769. var tag = (flags.align) ? "&lt;" + type +" style=\"text-align:" + flags.align + "\">" : "&lt;" + type + ">";
  2770. return tag + this.atLink(this.emoji(content)) + "&lt;/" + type + ">\n";
  2771. };
  2772. markedRenderer.listitem = function(text) {
  2773. if (settings.taskList &amp;&amp; /^\s*\[[x\s]\]\s*/.test(text))
  2774. {
  2775. text = text.replace(/^\s*\[\s\]\s*/, "&lt;input type=\"checkbox\" class=\"task-list-item-checkbox\" /> ")
  2776. .replace(/^\s*\[x\]\s*/, "&lt;input type=\"checkbox\" class=\"task-list-item-checkbox\" checked disabled /> ");
  2777. return "&lt;li style=\"list-style: none;\">" + this.atLink(this.emoji(text)) + "&lt;/li>";
  2778. }
  2779. else
  2780. {
  2781. return "&lt;li>" + this.atLink(this.emoji(text)) + "&lt;/li>";
  2782. }
  2783. };
  2784. return markedRenderer;
  2785. };
  2786. /**
  2787. *
  2788. * 生成TOC(Table of Contents)
  2789. * Creating ToC (Table of Contents)
  2790. *
  2791. * @param {Array} toc 从marked获取的TOC数组列表
  2792. * @param {Element} container 插入TOC的容器元素
  2793. * @param {Integer} startLevel Hx 起始层级
  2794. * @returns {Object} tocContainer 返回ToC列表容器层的jQuery对象元素
  2795. */
  2796. editormd.markdownToCRenderer = function(toc, container, tocDropdown, startLevel) {
  2797. var html = "";
  2798. var lastLevel = 0;
  2799. var classPrefix = this.classPrefix;
  2800. startLevel = startLevel || 1;
  2801. for (var i = 0, len = toc.length; i &lt; len; i++)
  2802. {
  2803. var text = toc[i].text;
  2804. var level = toc[i].level;
  2805. if (level &lt; startLevel) {
  2806. continue;
  2807. }
  2808. if (level > lastLevel)
  2809. {
  2810. html += "";
  2811. }
  2812. else if (level &lt; lastLevel)
  2813. {
  2814. html += (new Array(lastLevel - level + 2)).join("&lt;/ul>&lt;/li>");
  2815. }
  2816. else
  2817. {
  2818. html += "&lt;/ul>&lt;/li>";
  2819. }
  2820. html += "&lt;li>&lt;a class=\"toc-level-" + level + "\" href=\"#" + text + "\" level=\"" + level + "\">" + text + "&lt;/a>&lt;ul>";
  2821. lastLevel = level;
  2822. }
  2823. var tocContainer = container.find(".markdown-toc");
  2824. if (tocContainer.length &lt; 1 &amp;&amp; container.attr("previewContainer") === "false")
  2825. {
  2826. var tocHTML = "&lt;div class=\"markdown-toc " + classPrefix + "markdown-toc\">&lt;/div>";
  2827. tocHTML = (tocDropdown) ? "&lt;div class=\"" + classPrefix + "toc-menu\">" + tocHTML + "&lt;/div>" : tocHTML;
  2828. container.html(tocHTML);
  2829. tocContainer = container.find(".markdown-toc");
  2830. }
  2831. if (tocDropdown)
  2832. {
  2833. tocContainer.wrap("&lt;div class=\"" + classPrefix + "toc-menu\">&lt;/div>&lt;br/>");
  2834. }
  2835. tocContainer.html("&lt;ul class=\"markdown-toc-list\">&lt;/ul>").children(".markdown-toc-list").html(html.replace(/\r?\n?\&lt;ul\>\&lt;\/ul\>/g, ""));
  2836. return tocContainer;
  2837. };
  2838. /**
  2839. *
  2840. * 生成TOC下拉菜单
  2841. * Creating ToC dropdown menu
  2842. *
  2843. * @param {Object} container 插入TOC的容器jQuery对象元素
  2844. * @param {String} tocTitle ToC title
  2845. * @returns {Object} return toc-menu object
  2846. */
  2847. editormd.tocDropdownMenu = function(container, tocTitle) {
  2848. tocTitle = tocTitle || "Table of Contents";
  2849. var zindex = 400;
  2850. var tocMenus = container.find("." + this.classPrefix + "toc-menu");
  2851. tocMenus.each(function() {
  2852. var $this = $(this);
  2853. var toc = $this.children(".markdown-toc");
  2854. var icon = "&lt;i class=\"fa fa-angle-down\">&lt;/i>";
  2855. var btn = "&lt;a href=\"javascript:;\" class=\"toc-menu-btn\">" + icon + tocTitle + "&lt;/a>";
  2856. var menu = toc.children("ul");
  2857. var list = menu.find("li");
  2858. toc.append(btn);
  2859. list.first().before("&lt;li>&lt;h1>" + tocTitle + " " + icon + "&lt;/h1>&lt;/li>");
  2860. $this.mouseover(function(){
  2861. menu.show();
  2862. list.each(function(){
  2863. var li = $(this);
  2864. var ul = li.children("ul");
  2865. if (ul.html() === "")
  2866. {
  2867. ul.remove();
  2868. }
  2869. if (ul.length > 0 &amp;&amp; ul.html() !== "")
  2870. {
  2871. var firstA = li.children("a").first();
  2872. if (firstA.children(".fa").length &lt; 1)
  2873. {
  2874. firstA.append( $(icon).css({ float:"right", paddingTop:"4px" }) );
  2875. }
  2876. }
  2877. li.mouseover(function(){
  2878. ul.css("z-index", zindex).show();
  2879. zindex += 1;
  2880. }).mouseleave(function(){
  2881. ul.hide();
  2882. });
  2883. });
  2884. }).mouseleave(function(){
  2885. menu.hide();
  2886. });
  2887. });
  2888. return tocMenus;
  2889. };
  2890. /**
  2891. * 简单地过滤指定的HTML标签
  2892. * Filter custom html tags
  2893. *
  2894. * @param {String} html 要过滤HTML
  2895. * @param {String} filters 要过滤的标签
  2896. * @returns {String} html 返回过滤的HTML
  2897. */
  2898. editormd.filterHTMLTags = function(html, filters) {
  2899. if (typeof html !== "string") {
  2900. html = new String(html);
  2901. }
  2902. if (typeof filters !== "string") {
  2903. return html;
  2904. }
  2905. var expression = filters.split("|");
  2906. var filterTags = expression[0].split(",");
  2907. var attrs = expression[1];
  2908. for (var i = 0, len = filterTags.length; i &lt; len; i++)
  2909. {
  2910. var tag = filterTags[i];
  2911. html = html.replace(new RegExp("\&lt;\s*" + tag + "\s*([^\>]*)\>([^\>]*)\&lt;\s*\/" + tag + "\s*\>", "igm"), "");
  2912. }
  2913. if (typeof attrs !== "undefined")
  2914. {
  2915. var htmlTagRegex = /\&lt;(\w+)\s*([^\>]*)\>([^\>]*)\&lt;\/(\w+)\>/ig;
  2916. if (attrs === "*")
  2917. {
  2918. html = html.replace(htmlTagRegex, function($1, $2, $3, $4, $5) {
  2919. return "&lt;" + $2 + ">" + $4 + "&lt;/" + $5 + ">";
  2920. });
  2921. }
  2922. else if (attrs === "on*")
  2923. {
  2924. html = html.replace(htmlTagRegex, function($1, $2, $3, $4, $5) {
  2925. var el = $("&lt;" + $2 + ">" + $4 + "&lt;/" + $5 + ">");
  2926. var _attrs = $($1)[0].attributes;
  2927. var $attrs = {};
  2928. $.each(_attrs, function(i, e) {
  2929. $attrs[e.nodeName] = e.nodeValue;
  2930. });
  2931. $.each($attrs, function(i) {
  2932. if (i.indexOf("on") === 0) {
  2933. delete $attrs[i];
  2934. }
  2935. });
  2936. el.attr($attrs);
  2937. return el[0].outerHTML;
  2938. });
  2939. }
  2940. else
  2941. {
  2942. html = html.replace(htmlTagRegex, function($1, $2, $3, $4) {
  2943. var filterAttrs = attrs.split(",");
  2944. var el = $($1);
  2945. el.html($4);
  2946. $.each(filterAttrs, function(i) {
  2947. el.attr(filterAttrs[i], null);
  2948. });
  2949. return el[0].outerHTML;
  2950. });
  2951. }
  2952. }
  2953. return html;
  2954. };
  2955. /**
  2956. * 将Markdown文档解析为HTML用于前台显示
  2957. * Parse Markdown to HTML for Font-end preview.
  2958. *
  2959. * @param {String} id 用于显示HTML的对象ID
  2960. * @param {Object} [options={}] 配置选项,可选
  2961. * @returns {Object} div 返回jQuery对象元素
  2962. */
  2963. editormd.markdownToHTML = function(id, options) {
  2964. var defaults = {
  2965. gfm : true,
  2966. toc : true,
  2967. tocm : false,
  2968. tocStartLevel : 1,
  2969. tocTitle : "目录",
  2970. tocDropdown : false,
  2971. markdown : "",
  2972. htmlDecode : false,
  2973. autoLoadKaTeX : true,
  2974. pageBreak : true,
  2975. atLink : true, // for @link
  2976. emailLink : true, // for mail address auto link
  2977. tex : false,
  2978. taskList : false, // Github Flavored Markdown task lists
  2979. emoji : false,
  2980. flowChart : false,
  2981. sequenceDiagram : false,
  2982. previewCodeHighlight : true
  2983. };
  2984. editormd.$marked = marked;
  2985. var div = $("#" + id);
  2986. var settings = div.settings = $.extend(true, defaults, options || {});
  2987. var saveTo = div.find("textarea");
  2988. if (saveTo.length &lt; 1)
  2989. {
  2990. div.append("&lt;textarea>&lt;/textarea>");
  2991. saveTo = div.find("textarea");
  2992. }
  2993. var markdownDoc = (settings.markdown === "") ? saveTo.val() : settings.markdown;
  2994. var markdownToC = [];
  2995. var rendererOptions = {
  2996. toc : settings.toc,
  2997. tocm : settings.tocm,
  2998. tocStartLevel : settings.tocStartLevel,
  2999. taskList : settings.taskList,
  3000. emoji : settings.emoji,
  3001. tex : settings.tex,
  3002. pageBreak : settings.pageBreak,
  3003. atLink : settings.atLink, // for @link
  3004. emailLink : settings.emailLink, // for mail address auto link
  3005. flowChart : settings.flowChart,
  3006. sequenceDiagram : settings.sequenceDiagram,
  3007. previewCodeHighlight : settings.previewCodeHighlight,
  3008. };
  3009. var markedOptions = {
  3010. renderer : editormd.markedRenderer(markdownToC, rendererOptions),
  3011. gfm : settings.gfm,
  3012. tables : true,
  3013. breaks : true,
  3014. pedantic : false,
  3015. sanitize : (settings.htmlDecode) ? false : true, // 是否忽略HTML标签,即是否开启HTML标签解析,为了安全性,默认不开启
  3016. smartLists : true,
  3017. smartypants : true
  3018. };
  3019. markdownDoc = new String(markdownDoc);
  3020. markdownDoc = editormd.filterHTMLTags(markdownDoc, settings.htmlDecode);
  3021. var markdownParsed = marked(markdownDoc, markedOptions);
  3022. saveTo.val(markdownDoc);
  3023. div.addClass("markdown-body " + this.classPrefix + "html-preview").append(markdownParsed);
  3024. if (settings.toc)
  3025. {
  3026. div.tocContainer = this.markdownToCRenderer(markdownToC, div, settings.tocDropdown, settings.tocStartLevel);
  3027. if (settings.tocDropdown || div.find("." + this.classPrefix + "toc-menu").length > 0)
  3028. {
  3029. this.tocDropdownMenu(div, settings.tocTitle);
  3030. }
  3031. }
  3032. if (settings.previewCodeHighlight)
  3033. {
  3034. div.find("pre").addClass("prettyprint linenums");
  3035. prettyPrint();
  3036. }
  3037. if (!editormd.isIE8)
  3038. {
  3039. if (settings.flowChart) {
  3040. div.find(".flowchart").flowChart();
  3041. }
  3042. if (settings.sequenceDiagram) {
  3043. div.find(".sequence-diagram").sequenceDiagram({theme: "simple"});
  3044. }
  3045. }
  3046. if (settings.tex)
  3047. {
  3048. var katexHandle = function() {
  3049. div.find("." + editormd.classNames.tex).each(function(){
  3050. var tex = $(this);
  3051. katex.render(tex.html().replace(/&amp;lt;/g, "&lt;").replace(/&amp;gt;/g, ">"), tex[0]);
  3052. });
  3053. };
  3054. if (settings.autoLoadKaTeX &amp;&amp; !editormd.$katex &amp;&amp; !editormd.kaTeXLoaded)
  3055. {
  3056. this.loadKaTeX(function() {
  3057. editormd.$katex = katex;
  3058. editormd.kaTeXLoaded = true;
  3059. katexHandle();
  3060. });
  3061. }
  3062. else
  3063. {
  3064. katexHandle();
  3065. }
  3066. }
  3067. div.getMarkdown = function() {
  3068. return saveTo.val();
  3069. };
  3070. return div;
  3071. };
  3072. editormd.themes = [
  3073. "default", "3024-day", "3024-night",
  3074. "ambiance", "ambiance-mobile",
  3075. "base16-dark", "base16-light", "blackboard",
  3076. "cobalt",
  3077. "eclipse", "elegant", "erlang-dark",
  3078. "lesser-dark",
  3079. "mbo", "mdn-like", "midnight", "monokai",
  3080. "neat", "neo", "night",
  3081. "paraiso-dark", "paraiso-light", "pastel-on-dark",
  3082. "rubyblue",
  3083. "solarized",
  3084. "the-matrix", "tomorrow-night-eighties", "twilight",
  3085. "vibrant-ink",
  3086. "xq-dark", "xq-light"
  3087. ];
  3088. editormd.loadPlugins = {};
  3089. editormd.loadFiles = {
  3090. js : [],
  3091. css : [],
  3092. plugin : []
  3093. };
  3094. /**
  3095. * 动态加载Editor.md插件,但不立即执行
  3096. * Load editor.md plugins
  3097. *
  3098. * @param {String} fileName 插件文件路径
  3099. * @param {Function} [callback=function()] 加载成功后执行的回调函数
  3100. * @param {String} [into="head"] 嵌入页面的位置
  3101. */
  3102. editormd.loadPlugin = function(fileName, callback, into) {
  3103. callback = callback || function() {};
  3104. this.loadScript(fileName, function() {
  3105. editormd.loadFiles.plugin.push(fileName);
  3106. callback();
  3107. }, into);
  3108. };
  3109. /**
  3110. * 动态加载CSS文件的方法
  3111. * Load css file method
  3112. *
  3113. * @param {String} fileName CSS文件名
  3114. * @param {Function} [callback=function()] 加载成功后执行的回调函数
  3115. * @param {String} [into="head"] 嵌入页面的位置
  3116. */
  3117. editormd.loadCSS = function(fileName, callback, into) {
  3118. into = into || "head";
  3119. callback = callback || function() {};
  3120. var css = document.createElement("link");
  3121. css.type = "text/css";
  3122. css.rel = "stylesheet";
  3123. css.onload = css.onreadystatechange = function() {
  3124. editormd.loadFiles.css.push(fileName);
  3125. callback();
  3126. };
  3127. css.href = fileName + ".css";
  3128. if(into === "head") {
  3129. document.getElementsByTagName("head")[0].appendChild(css);
  3130. } else {
  3131. document.body.appendChild(css);
  3132. }
  3133. };
  3134. editormd.isIE = (navigator.appName == "Microsoft Internet Explorer");
  3135. editormd.isIE8 = (editormd.isIE &amp;&amp; navigator.appVersion.match(/8./i) == "8.");
  3136. /**
  3137. * 动态加载JS文件的方法
  3138. * Load javascript file method
  3139. *
  3140. * @param {String} fileName JS文件名
  3141. * @param {Function} [callback=function()] 加载成功后执行的回调函数
  3142. * @param {String} [into="head"] 嵌入页面的位置
  3143. */
  3144. editormd.loadScript = function(fileName, callback, into) {
  3145. into = into || "head";
  3146. callback = callback || function() {};
  3147. var script = null;
  3148. script = document.createElement("script");
  3149. script.id = fileName.replace(/[\./]+/g, "-");
  3150. script.type = "text/javascript";
  3151. script.src = fileName + ".js";
  3152. if (editormd.isIE8)
  3153. {
  3154. script.onreadystatechange = function() {
  3155. if(script.readyState)
  3156. {
  3157. if (script.readyState === "loaded" || script.readyState === "complete")
  3158. {
  3159. script.onreadystatechange = null;
  3160. editormd.loadFiles.js.push(fileName);
  3161. callback();
  3162. }
  3163. }
  3164. };
  3165. }
  3166. else
  3167. {
  3168. script.onload = function() {
  3169. editormd.loadFiles.js.push(fileName);
  3170. callback();
  3171. };
  3172. }
  3173. if (into === "head") {
  3174. document.getElementsByTagName("head")[0].appendChild(script);
  3175. } else {
  3176. document.body.appendChild(script);
  3177. }
  3178. };
  3179. // 使用国外的CDN,加载速度有时会很慢,或者自定义URL
  3180. // You can custom KaTeX load url.
  3181. editormd.katexURL = {
  3182. css : "//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.3.0/katex.min",
  3183. js : "//cdnjs.cloudflare.com/ajax/libs/KaTeX/0.3.0/katex.min"
  3184. };
  3185. editormd.kaTeXLoaded = false;
  3186. /**
  3187. * 加载KaTeX文件
  3188. * load KaTeX files
  3189. *
  3190. * @param {Function} [callback=function()] 加载成功后执行的回调函数
  3191. */
  3192. editormd.loadKaTeX = function (callback) {
  3193. editormd.loadCSS(editormd.katexURL.css, function(){
  3194. editormd.loadScript(editormd.katexURL.js, callback || function(){});
  3195. });
  3196. };
  3197. /**
  3198. * 锁屏
  3199. * lock screen
  3200. *
  3201. * @param {Boolean} lock Boolean 布尔值,是否锁屏
  3202. * @returns {void}
  3203. */
  3204. editormd.lockScreen = function(lock) {
  3205. $("html,body").css("overflow", (lock) ? "hidden" : "");
  3206. };
  3207. /**
  3208. * 动态创建对话框
  3209. * Creating custom dialogs
  3210. *
  3211. * @param {Object} options 配置项键值对 Key/Value
  3212. * @returns {dialog} 返回创建的dialog的jQuery实例对象
  3213. */
  3214. editormd.createDialog = function(options) {
  3215. var defaults = {
  3216. name : "",
  3217. width : 420,
  3218. height: 240,
  3219. title : "",
  3220. drag : true,
  3221. closed : true,
  3222. content : "",
  3223. mask : true,
  3224. maskStyle : {
  3225. backgroundColor : "#fff",
  3226. opacity : 0.1
  3227. },
  3228. lockScreen : true,
  3229. footer : true,
  3230. buttons : false
  3231. };
  3232. options = $.extend(true, defaults, options);
  3233. var editor = this.editor;
  3234. var classPrefix = editormd.classPrefix;
  3235. var guid = (new Date()).getTime();
  3236. var dialogName = ( (options.name === "") ? classPrefix + "dialog-" + guid : options.name);
  3237. var mouseOrTouch = editormd.mouseOrTouch;
  3238. var html = "&lt;div class=\"" + classPrefix + "dialog " + dialogName + "\">";
  3239. if (options.title !== "")
  3240. {
  3241. html += "&lt;div class=\"" + classPrefix + "dialog-header\"" + ( (options.drag) ? " style=\"cursor: move;\"" : "" ) + ">";
  3242. html += "&lt;strong class=\"" + classPrefix + "dialog-title\">" + options.title + "&lt;/strong>";
  3243. html += "&lt;/div>";
  3244. }
  3245. if (options.closed)
  3246. {
  3247. html += "&lt;a href=\"javascript:;\" class=\"fa fa-close " + classPrefix + "dialog-close\">&lt;/a>";
  3248. }
  3249. html += "&lt;div class=\"" + classPrefix + "dialog-container\">" + options.content;
  3250. if (options.footer || typeof options.footer === "string")
  3251. {
  3252. html += "&lt;div class=\"" + classPrefix + "dialog-footer\">" + ( (typeof options.footer === "boolean") ? "" : options.footer) + "&lt;/div>";
  3253. }
  3254. html += "&lt;/div>";
  3255. html += "&lt;div class=\"" + classPrefix + "dialog-mask " + classPrefix + "dialog-mask-bg\">&lt;/div>";
  3256. html += "&lt;div class=\"" + classPrefix + "dialog-mask " + classPrefix + "dialog-mask-con\">&lt;/div>";
  3257. html += "&lt;/div>";
  3258. editor.append(html);
  3259. var dialog = editor.find("." + dialogName);
  3260. dialog.lockScreen = function(lock) {
  3261. if (options.lockScreen)
  3262. {
  3263. $("html,body").css("overflow", (lock) ? "hidden" : "");
  3264. }
  3265. return dialog;
  3266. };
  3267. dialog.showMask = function() {
  3268. if (options.mask)
  3269. {
  3270. editor.find("." + classPrefix + "mask").css(options.maskStyle).css("z-index", editormd.dialogZindex - 1).show();
  3271. }
  3272. return dialog;
  3273. };
  3274. dialog.hideMask = function() {
  3275. if (options.mask)
  3276. {
  3277. editor.find("." + classPrefix + "mask").hide();
  3278. }
  3279. return dialog;
  3280. };
  3281. dialog.loading = function(show) {
  3282. var loading = dialog.find("." + classPrefix + "dialog-mask");
  3283. loading[(show) ? "show" : "hide"]();
  3284. return dialog;
  3285. };
  3286. dialog.lockScreen(true).showMask();
  3287. dialog.show().css({
  3288. zIndex : editormd.dialogZindex,
  3289. border : (editormd.isIE8) ? "1px solid #ddd" : "",
  3290. width : (typeof options.width === "number") ? options.width + "px" : options.width,
  3291. height : (typeof options.height === "number") ? options.height + "px" : options.height
  3292. });
  3293. var dialogPosition = function(){
  3294. dialog.css({
  3295. top : ($(window).height() - dialog.height()) / 2 + "px",
  3296. left : ($(window).width() - dialog.width()) / 2 + "px"
  3297. });
  3298. };
  3299. dialogPosition();
  3300. $(window).resize(dialogPosition);
  3301. dialog.children("." + classPrefix + "dialog-close").bind(mouseOrTouch("click", "touchend"), function() {
  3302. dialog.hide().lockScreen(false).hideMask();
  3303. });
  3304. if (typeof options.buttons === "object")
  3305. {
  3306. var footer = dialog.footer = dialog.find("." + classPrefix + "dialog-footer");
  3307. for (var key in options.buttons)
  3308. {
  3309. var btn = options.buttons[key];
  3310. var btnClassName = classPrefix + key + "-btn";
  3311. footer.append("&lt;button class=\"" + classPrefix + "btn " + btnClassName + "\">" + btn[0] + "&lt;/button>");
  3312. btn[1] = $.proxy(btn[1], dialog);
  3313. footer.children("." + btnClassName).bind(mouseOrTouch("click", "touchend"), btn[1]);
  3314. }
  3315. }
  3316. if (options.title !== "" &amp;&amp; options.drag)
  3317. {
  3318. var posX, posY;
  3319. var dialogHeader = dialog.children("." + classPrefix + "dialog-header");
  3320. if (!options.mask) {
  3321. dialogHeader.bind(mouseOrTouch("click", "touchend"), function(){
  3322. editormd.dialogZindex += 2;
  3323. dialog.css("z-index", editormd.dialogZindex);
  3324. });
  3325. }
  3326. dialogHeader.mousedown(function(e) {
  3327. e = e || window.event; //IE
  3328. posX = e.clientX - parseInt(dialog[0].style.left);
  3329. posY = e.clientY - parseInt(dialog[0].style.top);
  3330. document.onmousemove = moveAction;
  3331. });
  3332. var userCanSelect = function (obj) {
  3333. obj.removeClass(classPrefix + "user-unselect").off("selectstart");
  3334. };
  3335. var userUnselect = function (obj) {
  3336. obj.addClass(classPrefix + "user-unselect").on("selectstart", function(event) { // selectstart for IE
  3337. return false;
  3338. });
  3339. };
  3340. var moveAction = function (e) {
  3341. e = e || window.event; //IE
  3342. var left, top, nowLeft = parseInt(dialog[0].style.left), nowTop = parseInt(dialog[0].style.top);
  3343. if( nowLeft >= 0 ) {
  3344. if( nowLeft + dialog.width() &lt;= $(window).width()) {
  3345. left = e.clientX - posX;
  3346. } else {
  3347. left = $(window).width() - dialog.width();
  3348. document.onmousemove = null;
  3349. }
  3350. } else {
  3351. left = 0;
  3352. document.onmousemove = null;
  3353. }
  3354. if( nowTop >= 0 ) {
  3355. top = e.clientY - posY;
  3356. } else {
  3357. top = 0;
  3358. document.onmousemove = null;
  3359. }
  3360. document.onselectstart = function() {
  3361. return false;
  3362. };
  3363. userUnselect($("body"));
  3364. userUnselect(dialog);
  3365. dialog[0].style.left = left + "px";
  3366. dialog[0].style.top = top + "px";
  3367. };
  3368. document.onmouseup = function() {
  3369. userCanSelect($("body"));
  3370. userCanSelect(dialog);
  3371. document.onselectstart = null;
  3372. document.onmousemove = null;
  3373. };
  3374. dialogHeader.touchDraggable = function() {
  3375. var offset = null;
  3376. var start = function(e) {
  3377. var orig = e.originalEvent;
  3378. var pos = $(this).parent().position();
  3379. offset = {
  3380. x : orig.changedTouches[0].pageX - pos.left,
  3381. y : orig.changedTouches[0].pageY - pos.top
  3382. };
  3383. };
  3384. var move = function(e) {
  3385. e.preventDefault();
  3386. var orig = e.originalEvent;
  3387. $(this).parent().css({
  3388. top : orig.changedTouches[0].pageY - offset.y,
  3389. left : orig.changedTouches[0].pageX - offset.x
  3390. });
  3391. };
  3392. this.bind("touchstart", start).bind("touchmove", move);
  3393. };
  3394. dialogHeader.touchDraggable();
  3395. }
  3396. editormd.dialogZindex += 2;
  3397. return dialog;
  3398. };
  3399. /**
  3400. * 鼠标和触摸事件的判断/选择方法
  3401. * MouseEvent or TouchEvent type switch
  3402. *
  3403. * @param {String} [mouseEventType="click"] 供选择的鼠标事件
  3404. * @param {String} [touchEventType="touchend"] 供选择的触摸事件
  3405. * @returns {String} EventType 返回事件类型名称
  3406. */
  3407. editormd.mouseOrTouch = function(mouseEventType, touchEventType) {
  3408. mouseEventType = mouseEventType || "click";
  3409. touchEventType = touchEventType || "touchend";
  3410. var eventType = mouseEventType;
  3411. try {
  3412. document.createEvent("TouchEvent");
  3413. eventType = touchEventType;
  3414. } catch(e) {}
  3415. return eventType;
  3416. };
  3417. /**
  3418. * 日期时间的格式化方法
  3419. * Datetime format method
  3420. *
  3421. * @param {String} [format=""] 日期时间的格式,类似PHP的格式
  3422. * @returns {String} datefmt 返回格式化后的日期时间字符串
  3423. */
  3424. editormd.dateFormat = function(format) {
  3425. format = format || "";
  3426. var addZero = function(d) {
  3427. return (d &lt; 10) ? "0" + d : d;
  3428. };
  3429. var date = new Date();
  3430. var year = date.getFullYear();
  3431. var year2 = year.toString().slice(2, 4);
  3432. var month = addZero(date.getMonth() + 1);
  3433. var day = addZero(date.getDate());
  3434. var weekDay = date.getDay();
  3435. var hour = addZero(date.getHours());
  3436. var min = addZero(date.getMinutes());
  3437. var second = addZero(date.getSeconds());
  3438. var ms = addZero(date.getMilliseconds());
  3439. var datefmt = "";
  3440. var ymd = year2 + "-" + month + "-" + day;
  3441. var fymd = year + "-" + month + "-" + day;
  3442. var hms = hour + ":" + min + ":" + second;
  3443. switch (format)
  3444. {
  3445. case "UNIX Time" :
  3446. datefmt = date.getTime();
  3447. break;
  3448. case "UTC" :
  3449. datefmt = date.toUTCString();
  3450. break;
  3451. case "yy" :
  3452. datefmt = year2;
  3453. break;
  3454. case "year" :
  3455. case "yyyy" :
  3456. datefmt = year;
  3457. break;
  3458. case "month" :
  3459. case "mm" :
  3460. datefmt = month;
  3461. break;
  3462. case "cn-week-day" :
  3463. case "cn-wd" :
  3464. var cnWeekDays = ["日", "一", "二", "三", "四", "五", "六"];
  3465. datefmt = "星期" + cnWeekDays[weekDay];
  3466. break;
  3467. case "week-day" :
  3468. case "wd" :
  3469. var weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  3470. datefmt = weekDays[weekDay];
  3471. break;
  3472. case "day" :
  3473. case "dd" :
  3474. datefmt = day;
  3475. break;
  3476. case "hour" :
  3477. case "hh" :
  3478. datefmt = hour;
  3479. break;
  3480. case "min" :
  3481. case "ii" :
  3482. datefmt = min;
  3483. break;
  3484. case "second" :
  3485. case "ss" :
  3486. datefmt = second;
  3487. break;
  3488. case "ms" :
  3489. datefmt = ms;
  3490. break;
  3491. case "yy-mm-dd" :
  3492. datefmt = ymd;
  3493. break;
  3494. case "yyyy-mm-dd" :
  3495. datefmt = fymd;
  3496. break;
  3497. case "yyyy-mm-dd h:i:s ms" :
  3498. case "full + ms" :
  3499. datefmt = fymd + " " + hms + " " + ms;
  3500. break;
  3501. case "full" :
  3502. case "yyyy-mm-dd h:i:s" :
  3503. default:
  3504. datefmt = fymd + " " + hms;
  3505. break;
  3506. }
  3507. return datefmt;
  3508. };
  3509. return editormd;
  3510. }));
  3511. </code></pre>
  3512. </article>
  3513. </section>
  3514. </div>
  3515. <nav>
  3516. <h2><a href="index.html">Home</a></h2>
  3517. </nav>
  3518. <br class="clear">
  3519. <footer>
  3520. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0</a> on Mon Jun 08 2015 01:07:40 GMT+0800 (中国标准时间)
  3521. </footer>
  3522. <script> prettyPrint(); </script>
  3523. <script src="scripts/linenumber.js"> </script>
  3524. </body>
  3525. </html>