editormd.js 164 KB

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