editormd.js 164 KB

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