simditor.js 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define('simditor', ["jquery",
  5. "simple-module",
  6. "simple-hotkeys",
  7. "simple-uploader"], function ($, SimpleModule, simpleHotkeys, simpleUploader) {
  8. return (root.returnExportsGlobal = factory($, SimpleModule, simpleHotkeys, simpleUploader));
  9. });
  10. } else if (typeof exports === 'object') {
  11. // Node. Does not work with strict CommonJS, but
  12. // only CommonJS-like enviroments that support module.exports,
  13. // like Node.
  14. module.exports = factory(require("jquery"),
  15. require("simple-module"),
  16. require("simple-hotkeys"),
  17. require("simple-uploader"));
  18. } else {
  19. root['Simditor'] = factory(jQuery,
  20. SimpleModule,
  21. simple.hotkeys,
  22. simple.uploader);
  23. }
  24. }(this, function ($, SimpleModule, simpleHotkeys, simpleUploader) {
  25. var Selection,
  26. __hasProp = {}.hasOwnProperty,
  27. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  28. Selection = (function(_super) {
  29. __extends(Selection, _super);
  30. function Selection() {
  31. return Selection.__super__.constructor.apply(this, arguments);
  32. }
  33. Selection.pluginName = 'Selection';
  34. Selection.prototype._init = function() {
  35. this.editor = this._module;
  36. return this.sel = document.getSelection();
  37. };
  38. Selection.prototype.clear = function() {
  39. var e;
  40. try {
  41. return this.sel.removeAllRanges();
  42. } catch (_error) {
  43. e = _error;
  44. }
  45. };
  46. Selection.prototype.getRange = function() {
  47. if (!this.editor.inputManager.focused || !this.sel.rangeCount) {
  48. return null;
  49. }
  50. return this.sel.getRangeAt(0);
  51. };
  52. Selection.prototype.selectRange = function(range) {
  53. this.clear();
  54. this.sel.addRange(range);
  55. if (!this.editor.inputManager.focused && (this.editor.util.browser.firefox || this.editor.util.browser.msie)) {
  56. this.editor.body.focus();
  57. }
  58. return range;
  59. };
  60. Selection.prototype.rangeAtEndOf = function(node, range) {
  61. var endNode, endNodeLength, result;
  62. if (range == null) {
  63. range = this.getRange();
  64. }
  65. if (!((range != null) && range.collapsed)) {
  66. return;
  67. }
  68. node = $(node)[0];
  69. endNode = range.endContainer;
  70. endNodeLength = this.editor.util.getNodeLength(endNode);
  71. if (!(range.endOffset === endNodeLength - 1 && $(endNode).contents().last().is('br')) && range.endOffset !== endNodeLength) {
  72. return false;
  73. }
  74. if (node === endNode) {
  75. return true;
  76. } else if (!$.contains(node, endNode)) {
  77. return false;
  78. }
  79. result = true;
  80. $(endNode).parentsUntil(node).addBack().each((function(_this) {
  81. return function(i, n) {
  82. var $lastChild, nodes;
  83. nodes = $(n).parent().contents().filter(function() {
  84. return !(this !== n && this.nodeType === 3 && !this.nodeValue);
  85. });
  86. $lastChild = nodes.last();
  87. if (!($lastChild.get(0) === n || ($lastChild.is('br') && $lastChild.prev().get(0) === n))) {
  88. result = false;
  89. return false;
  90. }
  91. };
  92. })(this));
  93. return result;
  94. };
  95. Selection.prototype.rangeAtStartOf = function(node, range) {
  96. var result, startNode;
  97. if (range == null) {
  98. range = this.getRange();
  99. }
  100. if (!((range != null) && range.collapsed)) {
  101. return;
  102. }
  103. node = $(node)[0];
  104. startNode = range.startContainer;
  105. if (range.startOffset !== 0) {
  106. return false;
  107. }
  108. if (node === startNode) {
  109. return true;
  110. } else if (!$.contains(node, startNode)) {
  111. return false;
  112. }
  113. result = true;
  114. $(startNode).parentsUntil(node).addBack().each((function(_this) {
  115. return function(i, n) {
  116. var nodes;
  117. nodes = $(n).parent().contents().filter(function() {
  118. return !(this !== n && this.nodeType === 3 && !this.nodeValue);
  119. });
  120. if (nodes.first().get(0) !== n) {
  121. return result = false;
  122. }
  123. };
  124. })(this));
  125. return result;
  126. };
  127. Selection.prototype.insertNode = function(node, range) {
  128. if (range == null) {
  129. range = this.getRange();
  130. }
  131. if (range == null) {
  132. return;
  133. }
  134. node = $(node)[0];
  135. range.insertNode(node);
  136. return this.setRangeAfter(node, range);
  137. };
  138. Selection.prototype.setRangeAfter = function(node, range) {
  139. if (range == null) {
  140. range = this.getRange();
  141. }
  142. if (range == null) {
  143. return;
  144. }
  145. node = $(node)[0];
  146. range.setEndAfter(node);
  147. range.collapse(false);
  148. return this.selectRange(range);
  149. };
  150. Selection.prototype.setRangeBefore = function(node, range) {
  151. if (range == null) {
  152. range = this.getRange();
  153. }
  154. if (range == null) {
  155. return;
  156. }
  157. node = $(node)[0];
  158. range.setEndBefore(node);
  159. range.collapse(false);
  160. return this.selectRange(range);
  161. };
  162. Selection.prototype.setRangeAtStartOf = function(node, range) {
  163. if (range == null) {
  164. range = this.getRange();
  165. }
  166. node = $(node).get(0);
  167. range.setEnd(node, 0);
  168. range.collapse(false);
  169. return this.selectRange(range);
  170. };
  171. Selection.prototype.setRangeAtEndOf = function(node, range) {
  172. var $lastNode, $node, contents, lastChild, lastText, nodeLength;
  173. if (range == null) {
  174. range = this.getRange();
  175. }
  176. $node = $(node);
  177. node = $node.get(0);
  178. if ($node.is('pre')) {
  179. contents = $node.contents();
  180. if (contents.length > 0) {
  181. lastChild = contents.last();
  182. lastText = lastChild.text();
  183. if (lastText.charAt(lastText.length - 1) === '\n') {
  184. range.setEnd(lastChild[0], this.editor.util.getNodeLength(lastChild[0]) - 1);
  185. } else {
  186. range.setEnd(lastChild[0], this.editor.util.getNodeLength(lastChild[0]));
  187. }
  188. } else {
  189. range.setEnd(node, 0);
  190. }
  191. } else {
  192. nodeLength = this.editor.util.getNodeLength(node);
  193. if (node.nodeType !== 3 && nodeLength > 0) {
  194. $lastNode = $(node).contents().last();
  195. if ($lastNode.is('br')) {
  196. nodeLength -= 1;
  197. } else if ($lastNode[0].nodeType !== 3 && this.editor.util.isEmptyNode($lastNode)) {
  198. $lastNode.append(this.editor.util.phBr);
  199. node = $lastNode[0];
  200. nodeLength = 0;
  201. }
  202. }
  203. range.setEnd(node, nodeLength);
  204. }
  205. range.collapse(false);
  206. return this.selectRange(range);
  207. };
  208. Selection.prototype.deleteRangeContents = function(range) {
  209. var endRange, startRange;
  210. if (range == null) {
  211. range = this.getRange();
  212. }
  213. startRange = range.cloneRange();
  214. endRange = range.cloneRange();
  215. startRange.collapse(true);
  216. endRange.collapse(false);
  217. if (!range.collapsed && this.rangeAtStartOf(this.editor.body, startRange) && this.rangeAtEndOf(this.editor.body, endRange)) {
  218. this.editor.body.empty();
  219. range.setStart(this.editor.body[0], 0);
  220. range.collapse(true);
  221. this.selectRange(range);
  222. } else {
  223. range.deleteContents();
  224. }
  225. return range;
  226. };
  227. Selection.prototype.breakBlockEl = function(el, range) {
  228. var $el;
  229. if (range == null) {
  230. range = this.getRange();
  231. }
  232. $el = $(el);
  233. if (!range.collapsed) {
  234. return $el;
  235. }
  236. range.setStartBefore($el.get(0));
  237. if (range.collapsed) {
  238. return $el;
  239. }
  240. return $el.before(range.extractContents());
  241. };
  242. Selection.prototype.save = function(range) {
  243. var endCaret, endRange, startCaret;
  244. if (range == null) {
  245. range = this.getRange();
  246. }
  247. if (this._selectionSaved) {
  248. return;
  249. }
  250. endRange = range.cloneRange();
  251. endRange.collapse(false);
  252. startCaret = $('<span/>').addClass('simditor-caret-start');
  253. endCaret = $('<span/>').addClass('simditor-caret-end');
  254. endRange.insertNode(endCaret[0]);
  255. range.insertNode(startCaret[0]);
  256. this.clear();
  257. return this._selectionSaved = true;
  258. };
  259. Selection.prototype.restore = function() {
  260. var endCaret, endContainer, endOffset, range, startCaret, startContainer, startOffset;
  261. if (!this._selectionSaved) {
  262. return false;
  263. }
  264. startCaret = this.editor.body.find('.simditor-caret-start');
  265. endCaret = this.editor.body.find('.simditor-caret-end');
  266. if (startCaret.length && endCaret.length) {
  267. startContainer = startCaret.parent();
  268. startOffset = startContainer.contents().index(startCaret);
  269. endContainer = endCaret.parent();
  270. endOffset = endContainer.contents().index(endCaret);
  271. if (startContainer[0] === endContainer[0]) {
  272. endOffset -= 1;
  273. }
  274. range = document.createRange();
  275. range.setStart(startContainer.get(0), startOffset);
  276. range.setEnd(endContainer.get(0), endOffset);
  277. startCaret.remove();
  278. endCaret.remove();
  279. this.selectRange(range);
  280. } else {
  281. startCaret.remove();
  282. endCaret.remove();
  283. }
  284. this._selectionSaved = false;
  285. return range;
  286. };
  287. return Selection;
  288. })(SimpleModule);
  289. var Formatter,
  290. __hasProp = {}.hasOwnProperty,
  291. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  292. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  293. Formatter = (function(_super) {
  294. __extends(Formatter, _super);
  295. function Formatter() {
  296. return Formatter.__super__.constructor.apply(this, arguments);
  297. }
  298. Formatter.pluginName = 'Formatter';
  299. Formatter.prototype._init = function() {
  300. this.editor = this._module;
  301. this._allowedTags = ['br', 'a', 'img', 'b', 'strong', 'i', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'hr'];
  302. this._allowedAttributes = {
  303. img: ['src', 'alt', 'width', 'height', 'data-image-src', 'data-image-size', 'data-image-name', 'data-non-image'],
  304. a: ['href', 'target'],
  305. font: ['color'],
  306. pre: ['data-lang', 'class'],
  307. p: ['data-indent'],
  308. h1: ['data-indent'],
  309. h2: ['data-indent'],
  310. h3: ['data-indent'],
  311. h4: ['data-indent']
  312. };
  313. return this.editor.body.on('click', 'a', (function(_this) {
  314. return function(e) {
  315. return false;
  316. };
  317. })(this));
  318. };
  319. Formatter.prototype.decorate = function($el) {
  320. if ($el == null) {
  321. $el = this.editor.body;
  322. }
  323. return this.editor.trigger('decorate', [$el]);
  324. };
  325. Formatter.prototype.undecorate = function($el) {
  326. if ($el == null) {
  327. $el = this.editor.body.clone();
  328. }
  329. this.editor.trigger('undecorate', [$el]);
  330. return $.trim($el.html());
  331. };
  332. Formatter.prototype.autolink = function($el) {
  333. var $node, findLinkNode, lastIndex, linkNodes, match, re, replaceEls, text, uri, _i, _len;
  334. if ($el == null) {
  335. $el = this.editor.body;
  336. }
  337. linkNodes = [];
  338. findLinkNode = function($parentNode) {
  339. return $parentNode.contents().each(function(i, node) {
  340. var $node, text;
  341. $node = $(node);
  342. if ($node.is('a') || $node.closest('a, pre', $el).length) {
  343. return;
  344. }
  345. if ($node.contents().length) {
  346. return findLinkNode($node);
  347. } else if ((text = $node.text()) && /https?:\/\/|www\./ig.test(text)) {
  348. return linkNodes.push($node);
  349. }
  350. });
  351. };
  352. findLinkNode($el);
  353. re = /(https?:\/\/|www\.)[\w\-\.\?&=\/#%:,@\!\+]+/ig;
  354. for (_i = 0, _len = linkNodes.length; _i < _len; _i++) {
  355. $node = linkNodes[_i];
  356. text = $node.text();
  357. replaceEls = [];
  358. match = null;
  359. lastIndex = 0;
  360. while ((match = re.exec(text)) !== null) {
  361. replaceEls.push(document.createTextNode(text.substring(lastIndex, match.index)));
  362. lastIndex = re.lastIndex;
  363. uri = /^(http(s)?:\/\/|\/)/.test(match[0]) ? match[0] : 'http://' + match[0];
  364. replaceEls.push($('<a href="' + uri + '" rel="nofollow"></a>').text(match[0])[0]);
  365. }
  366. replaceEls.push(document.createTextNode(text.substring(lastIndex)));
  367. $node.replaceWith($(replaceEls));
  368. }
  369. return $el;
  370. };
  371. Formatter.prototype.format = function($el) {
  372. var $node, blockNode, n, node, _i, _j, _len, _len1, _ref, _ref1;
  373. if ($el == null) {
  374. $el = this.editor.body;
  375. }
  376. if ($el.is(':empty')) {
  377. $el.append('<p>' + this.editor.util.phBr + '</p>');
  378. return $el;
  379. }
  380. _ref = $el.contents();
  381. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  382. n = _ref[_i];
  383. this.cleanNode(n, true);
  384. }
  385. _ref1 = $el.contents();
  386. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  387. node = _ref1[_j];
  388. $node = $(node);
  389. if ($node.is('br')) {
  390. if (typeof blockNode !== "undefined" && blockNode !== null) {
  391. blockNode = null;
  392. }
  393. $node.remove();
  394. } else if (this.editor.util.isBlockNode(node)) {
  395. if ($node.is('li')) {
  396. if (blockNode && blockNode.is('ul, ol')) {
  397. blockNode.append(node);
  398. } else {
  399. blockNode = $('<ul/>').insertBefore(node);
  400. blockNode.append(node);
  401. }
  402. } else {
  403. blockNode = null;
  404. }
  405. } else {
  406. if (!blockNode || blockNode.is('ul, ol')) {
  407. blockNode = $('<p/>').insertBefore(node);
  408. }
  409. blockNode.append(node);
  410. }
  411. }
  412. return $el;
  413. };
  414. Formatter.prototype.cleanNode = function(node, recursive) {
  415. var $childImg, $node, $p, $td, allowedAttributes, attr, contents, isDecoration, n, text, textNode, _i, _j, _len, _len1, _ref, _ref1;
  416. $node = $(node);
  417. if (!($node.length > 0)) {
  418. return;
  419. }
  420. if ($node[0].nodeType === 3) {
  421. text = $node.text().replace(/(\r\n|\n|\r)/gm, '');
  422. if (text) {
  423. textNode = document.createTextNode(text);
  424. $node.replaceWith(textNode);
  425. } else {
  426. $node.remove();
  427. }
  428. return;
  429. }
  430. contents = $node.contents();
  431. isDecoration = $node.is('[class^="simditor-"]');
  432. if ($node.is(this._allowedTags.join(',')) || isDecoration) {
  433. if ($node.is('a') && ($childImg = $node.find('img')).length > 0) {
  434. $node.replaceWith($childImg);
  435. $node = $childImg;
  436. contents = null;
  437. }
  438. if ($node.is('img') && $node.hasClass('uploading')) {
  439. $node.remove();
  440. }
  441. if (!isDecoration) {
  442. allowedAttributes = this._allowedAttributes[$node[0].tagName.toLowerCase()];
  443. _ref = $.makeArray($node[0].attributes);
  444. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  445. attr = _ref[_i];
  446. if (!((allowedAttributes != null) && (_ref1 = attr.name, __indexOf.call(allowedAttributes, _ref1) >= 0))) {
  447. $node.removeAttr(attr.name);
  448. }
  449. }
  450. }
  451. } else if ($node[0].nodeType === 1 && !$node.is(':empty')) {
  452. if ($node.is('div, article, dl, header, footer, tr')) {
  453. $node.append('<br/>');
  454. contents.first().unwrap();
  455. } else if ($node.is('table')) {
  456. $p = $('<p/>');
  457. $node.find('tr').each((function(_this) {
  458. return function(i, tr) {
  459. return $p.append($(tr).text() + '<br/>');
  460. };
  461. })(this));
  462. $node.replaceWith($p);
  463. contents = null;
  464. } else if ($node.is('thead, tfoot')) {
  465. $node.remove();
  466. contents = null;
  467. } else if ($node.is('th')) {
  468. $td = $('<td/>').append($node.contents());
  469. $node.replaceWith($td);
  470. } else {
  471. contents.first().unwrap();
  472. }
  473. } else {
  474. $node.remove();
  475. contents = null;
  476. }
  477. if (recursive && (contents != null) && !$node.is('pre')) {
  478. for (_j = 0, _len1 = contents.length; _j < _len1; _j++) {
  479. n = contents[_j];
  480. this.cleanNode(n, true);
  481. }
  482. }
  483. return null;
  484. };
  485. Formatter.prototype.clearHtml = function(html, lineBreak) {
  486. var container, contents, result;
  487. if (lineBreak == null) {
  488. lineBreak = true;
  489. }
  490. container = $('<div/>').append(html);
  491. contents = container.contents();
  492. result = '';
  493. contents.each((function(_this) {
  494. return function(i, node) {
  495. var $node, children;
  496. if (node.nodeType === 3) {
  497. return result += node.nodeValue;
  498. } else if (node.nodeType === 1) {
  499. $node = $(node);
  500. children = $node.contents();
  501. if (children.length > 0) {
  502. result += _this.clearHtml(children);
  503. }
  504. if (lineBreak && i < contents.length - 1 && $node.is('br, p, div, li, tr, pre, address, artticle, aside, dl, figcaption, footer, h1, h2, h3, h4, header')) {
  505. return result += '\n';
  506. }
  507. }
  508. };
  509. })(this));
  510. return result;
  511. };
  512. Formatter.prototype.beautify = function($contents) {
  513. var uselessP;
  514. uselessP = function($el) {
  515. return !!($el.is('p') && !$el.text() && $el.children(':not(br)').length < 1);
  516. };
  517. return $contents.each((function(_this) {
  518. return function(i, el) {
  519. var $el;
  520. $el = $(el);
  521. if ($el.is(':not(img, br, col, td, hr, [class^="simditor-"]):empty')) {
  522. $el.remove();
  523. }
  524. if (uselessP($el)) {
  525. $el.remove();
  526. }
  527. return $el.find(':not(img, br, col, td, hr, [class^="simditor-"]):empty').remove();
  528. };
  529. })(this));
  530. };
  531. return Formatter;
  532. })(SimpleModule);
  533. var InputManager,
  534. __hasProp = {}.hasOwnProperty,
  535. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  536. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  537. InputManager = (function(_super) {
  538. __extends(InputManager, _super);
  539. function InputManager() {
  540. return InputManager.__super__.constructor.apply(this, arguments);
  541. }
  542. InputManager.pluginName = 'InputManager';
  543. InputManager.prototype.opts = {
  544. pasteImage: false
  545. };
  546. InputManager.prototype._modifierKeys = [16, 17, 18, 91, 93, 224];
  547. InputManager.prototype._arrowKeys = [37, 38, 39, 40];
  548. InputManager.prototype._init = function() {
  549. var submitKey;
  550. this.editor = this._module;
  551. if (this.opts.pasteImage && typeof this.opts.pasteImage !== 'string') {
  552. this.opts.pasteImage = 'inline';
  553. }
  554. this._keystrokeHandlers = {};
  555. this.hotkeys = simpleHotkeys({
  556. el: this.editor.body
  557. });
  558. this._pasteArea = $('<div/>').css({
  559. width: '1px',
  560. height: '1px',
  561. overflow: 'hidden',
  562. position: 'fixed',
  563. right: '0',
  564. bottom: '100px'
  565. }).attr({
  566. tabIndex: '-1',
  567. contentEditable: true
  568. }).addClass('simditor-paste-area').appendTo(this.editor.el);
  569. this._cleanPasteArea = $('<textarea/>').css({
  570. width: '1px',
  571. height: '1px',
  572. overflow: 'hidden',
  573. position: 'fixed',
  574. right: '0',
  575. bottom: '101px'
  576. }).attr({
  577. tabIndex: '-1'
  578. }).addClass('simditor-clean-paste-area').appendTo(this.editor.el);
  579. $(document).on('selectionchange.simditor' + this.editor.id, (function(_this) {
  580. return function(e) {
  581. if (!_this.focused) {
  582. return;
  583. }
  584. if (_this._selectionTimer) {
  585. clearTimeout(_this._selectionTimer);
  586. _this._selectionTimer = null;
  587. }
  588. return _this._selectionTimer = setTimeout(function() {
  589. return _this.editor.trigger('selectionchanged');
  590. }, 20);
  591. };
  592. })(this));
  593. this.editor.on('valuechanged', (function(_this) {
  594. return function() {
  595. if (!_this.editor.util.closestBlockEl() && _this.focused) {
  596. _this.editor.selection.save();
  597. _this.editor.formatter.format();
  598. _this.editor.selection.restore();
  599. }
  600. _this.editor.body.find('hr, pre, .simditor-table').each(function(i, el) {
  601. var $el, formatted;
  602. $el = $(el);
  603. if ($el.parent().is('blockquote') || $el.parent()[0] === _this.editor.body[0]) {
  604. formatted = false;
  605. if ($el.next().length === 0) {
  606. $('<p/>').append(_this.editor.util.phBr).insertAfter($el);
  607. formatted = true;
  608. }
  609. if ($el.prev().length === 0) {
  610. $('<p/>').append(_this.editor.util.phBr).insertBefore($el);
  611. formatted = true;
  612. }
  613. if (formatted) {
  614. return setTimeout(function() {
  615. return _this.editor.trigger('valuechanged');
  616. }, 10);
  617. }
  618. }
  619. });
  620. _this.editor.body.find('pre:empty').append(_this.editor.util.phBr);
  621. if (!_this.editor.util.supportSelectionChange && _this.focused) {
  622. return _this.editor.trigger('selectionchanged');
  623. }
  624. };
  625. })(this));
  626. this.editor.on('selectionchanged', (function(_this) {
  627. return function(e) {
  628. return _this.editor.undoManager.update();
  629. };
  630. })(this));
  631. this.editor.body.on('keydown', $.proxy(this._onKeyDown, this)).on('keypress', $.proxy(this._onKeyPress, this)).on('keyup', $.proxy(this._onKeyUp, this)).on('mouseup', $.proxy(this._onMouseUp, this)).on('focus', $.proxy(this._onFocus, this)).on('blur', $.proxy(this._onBlur, this)).on('paste', $.proxy(this._onPaste, this)).on('drop', $.proxy(this._onDrop, this));
  632. if (this.editor.util.browser.firefox) {
  633. this.addShortcut('cmd+left', (function(_this) {
  634. return function(e) {
  635. e.preventDefault();
  636. _this.editor.selection.sel.modify('move', 'backward', 'lineboundary');
  637. return false;
  638. };
  639. })(this));
  640. this.addShortcut('cmd+right', (function(_this) {
  641. return function(e) {
  642. e.preventDefault();
  643. _this.editor.selection.sel.modify('move', 'forward', 'lineboundary');
  644. return false;
  645. };
  646. })(this));
  647. this.addShortcut('cmd+a', (function(_this) {
  648. return function(e) {
  649. var $children, firstBlock, lastBlock, range;
  650. $children = _this.editor.body.children();
  651. if (!($children.length > 0)) {
  652. return;
  653. }
  654. firstBlock = $children.first().get(0);
  655. lastBlock = $children.last().get(0);
  656. range = document.createRange();
  657. range.setStart(firstBlock, 0);
  658. range.setEnd(lastBlock, _this.editor.util.getNodeLength(lastBlock));
  659. _this.editor.selection.selectRange(range);
  660. return false;
  661. };
  662. })(this));
  663. }
  664. submitKey = this.editor.util.os.mac ? 'cmd+enter' : 'ctrl+enter';
  665. this.addShortcut(submitKey, (function(_this) {
  666. return function(e) {
  667. _this.editor.el.closest('form').find('button:submit').click();
  668. return false;
  669. };
  670. })(this));
  671. if (this.editor.textarea.attr('autofocus')) {
  672. return setTimeout((function(_this) {
  673. return function() {
  674. return _this.editor.focus();
  675. };
  676. })(this), 0);
  677. }
  678. };
  679. InputManager.prototype._onFocus = function(e) {
  680. this.editor.el.addClass('focus').removeClass('error');
  681. this.focused = true;
  682. this.lastCaretPosition = null;
  683. return setTimeout((function(_this) {
  684. return function() {
  685. _this.editor.triggerHandler('focus');
  686. return _this.editor.trigger('selectionchanged');
  687. };
  688. })(this), 0);
  689. };
  690. InputManager.prototype._onBlur = function(e) {
  691. var _ref;
  692. this.editor.el.removeClass('focus');
  693. this.editor.sync();
  694. this.focused = false;
  695. this.lastCaretPosition = (_ref = this.editor.undoManager.currentState()) != null ? _ref.caret : void 0;
  696. return this.editor.triggerHandler('blur');
  697. };
  698. InputManager.prototype._onMouseUp = function(e) {
  699. if (!this.editor.util.supportSelectionChange) {
  700. return setTimeout((function(_this) {
  701. return function() {
  702. return _this.editor.trigger('selectionchanged');
  703. };
  704. })(this), 0);
  705. }
  706. };
  707. InputManager.prototype._onKeyDown = function(e) {
  708. var $blockEl, metaKey, result, _base, _ref, _ref1;
  709. if (this.editor.triggerHandler(e) === false) {
  710. return false;
  711. }
  712. if (this.hotkeys.respondTo(e)) {
  713. return;
  714. }
  715. if (e.which in this._keystrokeHandlers) {
  716. result = typeof (_base = this._keystrokeHandlers[e.which])['*'] === "function" ? _base['*'](e) : void 0;
  717. if (result) {
  718. this.editor.trigger('valuechanged');
  719. return false;
  720. }
  721. this.editor.util.traverseUp((function(_this) {
  722. return function(node) {
  723. var handler, _ref;
  724. if (node.nodeType !== 1) {
  725. return;
  726. }
  727. handler = (_ref = _this._keystrokeHandlers[e.which]) != null ? _ref[node.tagName.toLowerCase()] : void 0;
  728. result = typeof handler === "function" ? handler(e, $(node)) : void 0;
  729. if (result === true || result === false) {
  730. return false;
  731. }
  732. };
  733. })(this));
  734. if (result) {
  735. this.editor.trigger('valuechanged');
  736. return false;
  737. }
  738. }
  739. if ((_ref = e.which, __indexOf.call(this._modifierKeys, _ref) >= 0) || (_ref1 = e.which, __indexOf.call(this._arrowKeys, _ref1) >= 0)) {
  740. return;
  741. }
  742. metaKey = this.editor.util.metaKey(e);
  743. $blockEl = this.editor.util.closestBlockEl();
  744. if (metaKey && e.which === 86) {
  745. return;
  746. }
  747. if (this.editor.util.browser.webkit && e.which === 8 && this.editor.selection.rangeAtStartOf($blockEl)) {
  748. setTimeout((function(_this) {
  749. return function() {
  750. var $newBlockEl;
  751. if (!_this.focused) {
  752. return;
  753. }
  754. $newBlockEl = _this.editor.util.closestBlockEl();
  755. _this.editor.selection.save();
  756. _this.editor.formatter.cleanNode($newBlockEl, true);
  757. _this.editor.selection.restore();
  758. return _this.editor.trigger('valuechanged');
  759. };
  760. })(this), 10);
  761. this.typing = true;
  762. } else if (this._typing) {
  763. if (this._typing !== true) {
  764. clearTimeout(this._typing);
  765. }
  766. this._typing = setTimeout((function(_this) {
  767. return function() {
  768. _this.editor.trigger('valuechanged');
  769. return _this._typing = false;
  770. };
  771. })(this), 200);
  772. } else {
  773. setTimeout((function(_this) {
  774. return function() {
  775. return _this.editor.trigger('valuechanged');
  776. };
  777. })(this), 10);
  778. this._typing = true;
  779. }
  780. return null;
  781. };
  782. InputManager.prototype._onKeyPress = function(e) {
  783. if (this.editor.triggerHandler(e) === false) {
  784. return false;
  785. }
  786. };
  787. InputManager.prototype._onKeyUp = function(e) {
  788. var p, _ref;
  789. if (this.editor.triggerHandler(e) === false) {
  790. return false;
  791. }
  792. if (!this.editor.util.supportSelectionChange && (_ref = e.which, __indexOf.call(this._arrowKeys, _ref) >= 0)) {
  793. this.editor.trigger('selectionchanged');
  794. return;
  795. }
  796. if ((e.which === 8 || e.which === 46) && this.editor.util.isEmptyNode(this.editor.body)) {
  797. this.editor.body.empty();
  798. p = $('<p/>').append(this.editor.util.phBr).appendTo(this.editor.body);
  799. this.editor.selection.setRangeAtStartOf(p);
  800. }
  801. };
  802. InputManager.prototype._onPaste = function(e) {
  803. var $blockEl, cleanPaste, imageFile, pasteItem, range, uploadOpt, _ref;
  804. if (this.editor.triggerHandler(e) === false) {
  805. return false;
  806. }
  807. range = this.editor.selection.deleteRangeContents();
  808. if (!range.collapsed) {
  809. range.collapse(true);
  810. }
  811. $blockEl = this.editor.util.closestBlockEl();
  812. cleanPaste = $blockEl.is('pre, table');
  813. if (e.originalEvent.clipboardData && e.originalEvent.clipboardData.items && e.originalEvent.clipboardData.items.length > 0) {
  814. pasteItem = e.originalEvent.clipboardData.items[0];
  815. if (/^image\//.test(pasteItem.type) && !cleanPaste) {
  816. imageFile = pasteItem.getAsFile();
  817. if (!((imageFile != null) && this.opts.pasteImage)) {
  818. return;
  819. }
  820. if (!imageFile.name) {
  821. imageFile.name = "Clipboard Image.png";
  822. }
  823. uploadOpt = {};
  824. uploadOpt[this.opts.pasteImage] = true;
  825. if ((_ref = this.editor.uploader) != null) {
  826. _ref.upload(imageFile, uploadOpt);
  827. }
  828. return false;
  829. }
  830. }
  831. this.editor.selection.save(range);
  832. if (cleanPaste) {
  833. this._cleanPasteArea.focus();
  834. if (this.editor.util.browser.firefox) {
  835. e.preventDefault();
  836. this._cleanPasteArea.val(e.originalEvent.clipboardData.getData('text/plain'));
  837. } else if (this.editor.util.browser.msie && this.editor.util.browser.version === 10) {
  838. e.preventDefault();
  839. this._cleanPasteArea.val(window.clipboardData.getData('Text'));
  840. }
  841. } else {
  842. this._pasteArea.focus();
  843. if (this.editor.util.browser.msie && this.editor.util.browser.version === 10) {
  844. e.preventDefault();
  845. this._pasteArea.html(window.clipboardData.getData('Text'));
  846. }
  847. }
  848. return setTimeout((function(_this) {
  849. return function() {
  850. var $img, blob, children, insertPosition, lastLine, line, lines, node, pasteContent, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3;
  851. if (_this._pasteArea.is(':empty') && !_this._cleanPasteArea.val()) {
  852. pasteContent = null;
  853. } else if (cleanPaste) {
  854. pasteContent = _this._cleanPasteArea.val();
  855. } else {
  856. pasteContent = $('<div/>').append(_this._pasteArea.contents());
  857. pasteContent.find('table colgroup').remove();
  858. _this.editor.formatter.format(pasteContent);
  859. _this.editor.formatter.decorate(pasteContent);
  860. _this.editor.formatter.beautify(pasteContent.children());
  861. pasteContent = pasteContent.contents();
  862. }
  863. _this._pasteArea.empty();
  864. _this._cleanPasteArea.val('');
  865. range = _this.editor.selection.restore();
  866. if (_this.editor.triggerHandler('pasting', [pasteContent]) === false) {
  867. return;
  868. }
  869. if (!pasteContent) {
  870. return;
  871. } else if (cleanPaste) {
  872. if ($blockEl.is('table')) {
  873. lines = pasteContent.split('\n');
  874. lastLine = lines.pop();
  875. for (_i = 0, _len = lines.length; _i < _len; _i++) {
  876. line = lines[_i];
  877. _this.editor.selection.insertNode(document.createTextNode(line));
  878. _this.editor.selection.insertNode($('<br/>'));
  879. }
  880. _this.editor.selection.insertNode(document.createTextNode(lastLine));
  881. } else {
  882. pasteContent = $('<div/>').text(pasteContent);
  883. _ref1 = pasteContent.contents();
  884. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  885. node = _ref1[_j];
  886. _this.editor.selection.insertNode($(node)[0], range);
  887. }
  888. }
  889. } else if ($blockEl.is(_this.editor.body)) {
  890. for (_k = 0, _len2 = pasteContent.length; _k < _len2; _k++) {
  891. node = pasteContent[_k];
  892. _this.editor.selection.insertNode(node, range);
  893. }
  894. } else if (pasteContent.length < 1) {
  895. return;
  896. } else if (pasteContent.length === 1) {
  897. if (pasteContent.is('p')) {
  898. children = pasteContent.contents();
  899. if (children.length === 1 && children.is('img')) {
  900. $img = children;
  901. if (/^data:image/.test($img.attr('src'))) {
  902. if (!_this.opts.pasteImage) {
  903. return;
  904. }
  905. blob = _this.editor.util.dataURLtoBlob($img.attr("src"));
  906. blob.name = "Clipboard Image.png";
  907. uploadOpt = {};
  908. uploadOpt[_this.opts.pasteImage] = true;
  909. if ((_ref2 = _this.editor.uploader) != null) {
  910. _ref2.upload(blob, uploadOpt);
  911. }
  912. return;
  913. } else if ($img.is('img[src^="webkit-fake-url://"]')) {
  914. return;
  915. }
  916. }
  917. for (_l = 0, _len3 = children.length; _l < _len3; _l++) {
  918. node = children[_l];
  919. _this.editor.selection.insertNode(node, range);
  920. }
  921. } else if ($blockEl.is('p') && _this.editor.util.isEmptyNode($blockEl)) {
  922. $blockEl.replaceWith(pasteContent);
  923. _this.editor.selection.setRangeAtEndOf(pasteContent, range);
  924. } else if (pasteContent.is('ul, ol')) {
  925. if (pasteContent.find('li').length === 1) {
  926. pasteContent = $('<div/>').text(pasteContent.text());
  927. _ref3 = pasteContent.contents();
  928. for (_m = 0, _len4 = _ref3.length; _m < _len4; _m++) {
  929. node = _ref3[_m];
  930. _this.editor.selection.insertNode($(node)[0], range);
  931. }
  932. } else if ($blockEl.is('li')) {
  933. $blockEl.parent().after(pasteContent);
  934. _this.editor.selection.setRangeAtEndOf(pasteContent, range);
  935. } else {
  936. $blockEl.after(pasteContent);
  937. _this.editor.selection.setRangeAtEndOf(pasteContent, range);
  938. }
  939. } else {
  940. $blockEl.after(pasteContent);
  941. _this.editor.selection.setRangeAtEndOf(pasteContent, range);
  942. }
  943. } else {
  944. if ($blockEl.is('li')) {
  945. $blockEl = $blockEl.parent();
  946. }
  947. if (_this.editor.selection.rangeAtStartOf($blockEl, range)) {
  948. insertPosition = 'before';
  949. } else if (_this.editor.selection.rangeAtEndOf($blockEl, range)) {
  950. insertPosition = 'after';
  951. } else {
  952. _this.editor.selection.breakBlockEl($blockEl, range);
  953. insertPosition = 'before';
  954. }
  955. $blockEl[insertPosition](pasteContent);
  956. _this.editor.selection.setRangeAtEndOf(pasteContent.last(), range);
  957. }
  958. return _this.editor.trigger('valuechanged');
  959. };
  960. })(this), 10);
  961. };
  962. InputManager.prototype._onDrop = function(e) {
  963. if (this.editor.triggerHandler(e) === false) {
  964. return false;
  965. }
  966. return setTimeout((function(_this) {
  967. return function() {
  968. return _this.editor.trigger('valuechanged');
  969. };
  970. })(this), 0);
  971. };
  972. InputManager.prototype.addKeystrokeHandler = function(key, node, handler) {
  973. if (!this._keystrokeHandlers[key]) {
  974. this._keystrokeHandlers[key] = {};
  975. }
  976. return this._keystrokeHandlers[key][node] = handler;
  977. };
  978. InputManager.prototype.addShortcut = function(keys, handler) {
  979. return this.hotkeys.add(keys, $.proxy(handler, this));
  980. };
  981. return InputManager;
  982. })(SimpleModule);
  983. var Keystroke,
  984. __hasProp = {}.hasOwnProperty,
  985. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  986. Keystroke = (function(_super) {
  987. __extends(Keystroke, _super);
  988. function Keystroke() {
  989. return Keystroke.__super__.constructor.apply(this, arguments);
  990. }
  991. Keystroke.pluginName = 'Keystroke';
  992. Keystroke.prototype._init = function() {
  993. var titleEnterHandler;
  994. this.editor = this._module;
  995. if (this.editor.util.browser.safari) {
  996. this.editor.inputManager.addKeystrokeHandler('13', '*', (function(_this) {
  997. return function(e) {
  998. var $blockEl, $br;
  999. if (!e.shiftKey) {
  1000. return;
  1001. }
  1002. $blockEl = _this.editor.util.closestBlockEl();
  1003. if ($blockEl.is('pre')) {
  1004. return;
  1005. }
  1006. $br = $('<br/>');
  1007. if (_this.editor.selection.rangeAtEndOf($blockEl)) {
  1008. _this.editor.selection.insertNode($br);
  1009. _this.editor.selection.insertNode($('<br/>'));
  1010. _this.editor.selection.setRangeBefore($br);
  1011. } else {
  1012. _this.editor.selection.insertNode($br);
  1013. }
  1014. return true;
  1015. };
  1016. })(this));
  1017. }
  1018. if (this.editor.util.browser.webkit || this.editor.util.browser.msie) {
  1019. titleEnterHandler = (function(_this) {
  1020. return function(e, $node) {
  1021. var $p;
  1022. if (!_this.editor.selection.rangeAtEndOf($node)) {
  1023. return;
  1024. }
  1025. $p = $('<p/>').append(_this.editor.util.phBr).insertAfter($node);
  1026. _this.editor.selection.setRangeAtStartOf($p);
  1027. return true;
  1028. };
  1029. })(this);
  1030. this.editor.inputManager.addKeystrokeHandler('13', 'h1', titleEnterHandler);
  1031. this.editor.inputManager.addKeystrokeHandler('13', 'h2', titleEnterHandler);
  1032. this.editor.inputManager.addKeystrokeHandler('13', 'h3', titleEnterHandler);
  1033. this.editor.inputManager.addKeystrokeHandler('13', 'h4', titleEnterHandler);
  1034. this.editor.inputManager.addKeystrokeHandler('13', 'h5', titleEnterHandler);
  1035. this.editor.inputManager.addKeystrokeHandler('13', 'h6', titleEnterHandler);
  1036. }
  1037. this.editor.inputManager.addKeystrokeHandler('8', '*', (function(_this) {
  1038. return function(e) {
  1039. var $prevBlockEl, $rootBlock;
  1040. $rootBlock = _this.editor.util.furthestBlockEl();
  1041. $prevBlockEl = $rootBlock.prev();
  1042. if ($prevBlockEl.is('hr') && _this.editor.selection.rangeAtStartOf($rootBlock)) {
  1043. _this.editor.selection.save();
  1044. $prevBlockEl.remove();
  1045. _this.editor.selection.restore();
  1046. return true;
  1047. }
  1048. };
  1049. })(this));
  1050. this.editor.inputManager.addKeystrokeHandler('9', '*', (function(_this) {
  1051. return function(e) {
  1052. var codeButton;
  1053. codeButton = _this.editor.toolbar.findButton('code');
  1054. if (!(_this.editor.opts.tabIndent || (codeButton && codeButton.active))) {
  1055. return;
  1056. }
  1057. if (e.shiftKey) {
  1058. _this.editor.util.outdent();
  1059. } else {
  1060. _this.editor.util.indent();
  1061. }
  1062. return true;
  1063. };
  1064. })(this));
  1065. this.editor.inputManager.addKeystrokeHandler('13', 'li', (function(_this) {
  1066. return function(e, $node) {
  1067. var $cloneNode, listEl, newBlockEl, newListEl;
  1068. $cloneNode = $node.clone();
  1069. $cloneNode.find('ul, ol').remove();
  1070. if (!(_this.editor.util.isEmptyNode($cloneNode) && $node.is(_this.editor.util.closestBlockEl()))) {
  1071. return;
  1072. }
  1073. listEl = $node.parent();
  1074. if ($node.next('li').length > 0) {
  1075. if (!_this.editor.util.isEmptyNode($node)) {
  1076. return;
  1077. }
  1078. if (listEl.parent('li').length > 0) {
  1079. newBlockEl = $('<li/>').append(_this.editor.util.phBr).insertAfter(listEl.parent('li'));
  1080. newListEl = $('<' + listEl[0].tagName + '/>').append($node.nextAll('li'));
  1081. newBlockEl.append(newListEl);
  1082. } else {
  1083. newBlockEl = $('<p/>').append(_this.editor.util.phBr).insertAfter(listEl);
  1084. newListEl = $('<' + listEl[0].tagName + '/>').append($node.nextAll('li'));
  1085. newBlockEl.after(newListEl);
  1086. }
  1087. } else {
  1088. if (listEl.parent('li').length > 0) {
  1089. newBlockEl = $('<li/>').insertAfter(listEl.parent('li'));
  1090. if ($node.contents().length > 0) {
  1091. newBlockEl.append($node.contents());
  1092. } else {
  1093. newBlockEl.append(_this.editor.util.phBr);
  1094. }
  1095. } else {
  1096. newBlockEl = $('<p/>').append(_this.editor.util.phBr).insertAfter(listEl);
  1097. if ($node.children('ul, ol').length > 0) {
  1098. newBlockEl.after($node.children('ul, ol'));
  1099. }
  1100. }
  1101. }
  1102. if ($node.prev('li').length) {
  1103. $node.remove();
  1104. } else {
  1105. listEl.remove();
  1106. }
  1107. _this.editor.selection.setRangeAtStartOf(newBlockEl);
  1108. return true;
  1109. };
  1110. })(this));
  1111. this.editor.inputManager.addKeystrokeHandler('13', 'pre', (function(_this) {
  1112. return function(e, $node) {
  1113. var $p, breakNode, range;
  1114. e.preventDefault();
  1115. if (e.shiftKey) {
  1116. $p = $('<p/>').append(_this.editor.util.phBr).insertAfter($node);
  1117. _this.editor.selection.setRangeAtStartOf($p);
  1118. return true;
  1119. }
  1120. range = _this.editor.selection.getRange();
  1121. breakNode = null;
  1122. range.deleteContents();
  1123. if (!_this.editor.util.browser.msie && _this.editor.selection.rangeAtEndOf($node)) {
  1124. breakNode = document.createTextNode('\n\n');
  1125. range.insertNode(breakNode);
  1126. range.setEnd(breakNode, 1);
  1127. } else {
  1128. breakNode = document.createTextNode('\n');
  1129. range.insertNode(breakNode);
  1130. range.setStartAfter(breakNode);
  1131. }
  1132. range.collapse(false);
  1133. _this.editor.selection.selectRange(range);
  1134. return true;
  1135. };
  1136. })(this));
  1137. this.editor.inputManager.addKeystrokeHandler('13', 'blockquote', (function(_this) {
  1138. return function(e, $node) {
  1139. var $closestBlock, range;
  1140. $closestBlock = _this.editor.util.closestBlockEl();
  1141. if (!($closestBlock.is('p') && !$closestBlock.next().length && _this.editor.util.isEmptyNode($closestBlock))) {
  1142. return;
  1143. }
  1144. $node.after($closestBlock);
  1145. range = document.createRange();
  1146. _this.editor.selection.setRangeAtStartOf($closestBlock, range);
  1147. return true;
  1148. };
  1149. })(this));
  1150. this.editor.inputManager.addKeystrokeHandler('8', 'li', (function(_this) {
  1151. return function(e, $node) {
  1152. var $br, $childList, $newLi, $prevChildList, $prevNode, $textNode, range, text;
  1153. $childList = $node.children('ul, ol');
  1154. $prevNode = $node.prev('li');
  1155. if (!($childList.length > 0 && $prevNode.length > 0)) {
  1156. return false;
  1157. }
  1158. text = '';
  1159. $textNode = null;
  1160. $node.contents().each(function(i, n) {
  1161. if (n.nodeType === 1 && /UL|OL/.test(n.nodeName)) {
  1162. return false;
  1163. }
  1164. if (n.nodeType === 1 && /BR/.test(n.nodeName)) {
  1165. return;
  1166. }
  1167. if (n.nodeType === 3 && n.nodeValue) {
  1168. text += n.nodeValue;
  1169. } else if (n.nodeType === 1) {
  1170. text += $(n).text();
  1171. }
  1172. return $textNode = $(n);
  1173. });
  1174. if ($textNode && text.length === 1 && _this.editor.util.browser.firefox && !$textNode.next('br').length) {
  1175. $br = $(_this.editor.util.phBr).insertAfter($textNode);
  1176. $textNode.remove();
  1177. _this.editor.selection.setRangeBefore($br);
  1178. return true;
  1179. } else if (text.length > 0) {
  1180. return false;
  1181. }
  1182. range = document.createRange();
  1183. $prevChildList = $prevNode.children('ul, ol');
  1184. if ($prevChildList.length > 0) {
  1185. $newLi = $('<li/>').append(_this.editor.util.phBr).appendTo($prevChildList);
  1186. $prevChildList.append($childList.children('li'));
  1187. $node.remove();
  1188. _this.editor.selection.setRangeAtEndOf($newLi, range);
  1189. } else {
  1190. _this.editor.selection.setRangeAtEndOf($prevNode, range);
  1191. $prevNode.append($childList);
  1192. $node.remove();
  1193. _this.editor.selection.selectRange(range);
  1194. }
  1195. return true;
  1196. };
  1197. })(this));
  1198. this.editor.inputManager.addKeystrokeHandler('8', 'pre', (function(_this) {
  1199. return function(e, $node) {
  1200. var $newNode, codeStr, range;
  1201. if (!_this.editor.selection.rangeAtStartOf($node)) {
  1202. return;
  1203. }
  1204. codeStr = $node.html().replace('\n', '<br/>');
  1205. $newNode = $('<p/>').append(codeStr || _this.editor.util.phBr).insertAfter($node);
  1206. $node.remove();
  1207. range = document.createRange();
  1208. _this.editor.selection.setRangeAtStartOf($newNode, range);
  1209. return true;
  1210. };
  1211. })(this));
  1212. return this.editor.inputManager.addKeystrokeHandler('8', 'blockquote', (function(_this) {
  1213. return function(e, $node) {
  1214. var $firstChild, range;
  1215. if (!_this.editor.selection.rangeAtStartOf($node)) {
  1216. return;
  1217. }
  1218. $firstChild = $node.children().first().unwrap();
  1219. range = document.createRange();
  1220. _this.editor.selection.setRangeAtStartOf($firstChild, range);
  1221. return true;
  1222. };
  1223. })(this));
  1224. };
  1225. return Keystroke;
  1226. })(SimpleModule);
  1227. var UndoManager,
  1228. __hasProp = {}.hasOwnProperty,
  1229. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1230. UndoManager = (function(_super) {
  1231. __extends(UndoManager, _super);
  1232. function UndoManager() {
  1233. return UndoManager.__super__.constructor.apply(this, arguments);
  1234. }
  1235. UndoManager.pluginName = 'UndoManager';
  1236. UndoManager.prototype._index = -1;
  1237. UndoManager.prototype._capacity = 50;
  1238. UndoManager.prototype._timer = null;
  1239. UndoManager.prototype._init = function() {
  1240. var redoShortcut, undoShortcut;
  1241. this.editor = this._module;
  1242. this._stack = [];
  1243. if (this.editor.util.os.mac) {
  1244. undoShortcut = 'cmd+z';
  1245. redoShortcut = 'shift+cmd+z';
  1246. } else if (this.editor.util.os.win) {
  1247. undoShortcut = 'ctrl+z';
  1248. redoShortcut = 'ctrl+y';
  1249. } else {
  1250. undoShortcut = 'ctrl+z';
  1251. redoShortcut = 'shift+ctrl+z';
  1252. }
  1253. this.editor.inputManager.addShortcut(undoShortcut, (function(_this) {
  1254. return function(e) {
  1255. e.preventDefault();
  1256. _this.undo();
  1257. return false;
  1258. };
  1259. })(this));
  1260. this.editor.inputManager.addShortcut(redoShortcut, (function(_this) {
  1261. return function(e) {
  1262. e.preventDefault();
  1263. _this.redo();
  1264. return false;
  1265. };
  1266. })(this));
  1267. return this.editor.on('valuechanged', (function(_this) {
  1268. return function(e, src) {
  1269. if (src === 'undo') {
  1270. return;
  1271. }
  1272. if (_this._timer) {
  1273. clearTimeout(_this._timer);
  1274. _this._timer = null;
  1275. }
  1276. return _this._timer = setTimeout(function() {
  1277. _this._pushUndoState();
  1278. return _this._timer = null;
  1279. }, 200);
  1280. };
  1281. })(this));
  1282. };
  1283. UndoManager.prototype._pushUndoState = function() {
  1284. var currentState, html;
  1285. if (this.editor.triggerHandler('pushundostate') === false) {
  1286. return;
  1287. }
  1288. currentState = this.currentState();
  1289. html = this.editor.body.html();
  1290. if (currentState && currentState.html === html) {
  1291. return;
  1292. }
  1293. this._index += 1;
  1294. this._stack.length = this._index;
  1295. this._stack.push({
  1296. html: html,
  1297. caret: this.caretPosition()
  1298. });
  1299. if (this._stack.length > this._capacity) {
  1300. this._stack.shift();
  1301. return this._index -= 1;
  1302. }
  1303. };
  1304. UndoManager.prototype.currentState = function() {
  1305. if (this._stack.length && this._index > -1) {
  1306. return this._stack[this._index];
  1307. } else {
  1308. return null;
  1309. }
  1310. };
  1311. UndoManager.prototype.undo = function() {
  1312. var state;
  1313. if (this._index < 1 || this._stack.length < 2) {
  1314. return;
  1315. }
  1316. this.editor.hidePopover();
  1317. this._index -= 1;
  1318. state = this._stack[this._index];
  1319. this.editor.body.html(state.html);
  1320. this.caretPosition(state.caret);
  1321. this.editor.body.find('.selected').removeClass('selected');
  1322. this.editor.sync();
  1323. return this.editor.trigger('valuechanged', ['undo']);
  1324. };
  1325. UndoManager.prototype.redo = function() {
  1326. var state;
  1327. if (this._index < 0 || this._stack.length < this._index + 2) {
  1328. return;
  1329. }
  1330. this.editor.hidePopover();
  1331. this._index += 1;
  1332. state = this._stack[this._index];
  1333. this.editor.body.html(state.html);
  1334. this.caretPosition(state.caret);
  1335. this.editor.body.find('.selected').removeClass('selected');
  1336. this.editor.sync();
  1337. return this.editor.trigger('valuechanged', ['undo']);
  1338. };
  1339. UndoManager.prototype.update = function() {
  1340. var currentState, html;
  1341. if (this._timer) {
  1342. return;
  1343. }
  1344. currentState = this.currentState();
  1345. if (!currentState) {
  1346. return;
  1347. }
  1348. html = this.editor.body.html();
  1349. currentState.html = html;
  1350. return currentState.caret = this.caretPosition();
  1351. };
  1352. UndoManager.prototype._getNodeOffset = function(node, index) {
  1353. var $parent, merging, offset;
  1354. if (index) {
  1355. $parent = $(node);
  1356. } else {
  1357. $parent = $(node).parent();
  1358. }
  1359. offset = 0;
  1360. merging = false;
  1361. $parent.contents().each((function(_this) {
  1362. return function(i, child) {
  1363. if (index === i || node === child) {
  1364. return false;
  1365. }
  1366. if (child.nodeType === 3) {
  1367. if (!merging) {
  1368. offset += 1;
  1369. merging = true;
  1370. }
  1371. } else {
  1372. offset += 1;
  1373. merging = false;
  1374. }
  1375. return null;
  1376. };
  1377. })(this));
  1378. return offset;
  1379. };
  1380. UndoManager.prototype._getNodePosition = function(node, offset) {
  1381. var position, prevNode;
  1382. if (node.nodeType === 3) {
  1383. prevNode = node.previousSibling;
  1384. while (prevNode && prevNode.nodeType === 3) {
  1385. node = prevNode;
  1386. offset += this.editor.util.getNodeLength(prevNode);
  1387. prevNode = prevNode.previousSibling;
  1388. }
  1389. } else {
  1390. offset = this._getNodeOffset(node, offset);
  1391. }
  1392. position = [];
  1393. position.unshift(offset);
  1394. this.editor.util.traverseUp((function(_this) {
  1395. return function(n) {
  1396. return position.unshift(_this._getNodeOffset(n));
  1397. };
  1398. })(this), node);
  1399. return position;
  1400. };
  1401. UndoManager.prototype._getNodeByPosition = function(position) {
  1402. var child, childNodes, i, node, offset, _i, _len, _ref;
  1403. node = this.editor.body[0];
  1404. _ref = position.slice(0, position.length - 1);
  1405. for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
  1406. offset = _ref[i];
  1407. childNodes = node.childNodes;
  1408. if (offset > childNodes.length - 1) {
  1409. if (i === position.length - 2 && $(node).is('pre')) {
  1410. child = document.createTextNode('');
  1411. node.appendChild(child);
  1412. childNodes = node.childNodes;
  1413. } else {
  1414. node = null;
  1415. break;
  1416. }
  1417. }
  1418. node = childNodes[offset];
  1419. }
  1420. return node;
  1421. };
  1422. UndoManager.prototype.caretPosition = function(caret) {
  1423. var endContainer, endOffset, range, startContainer, startOffset;
  1424. if (!caret) {
  1425. range = this.editor.selection.getRange();
  1426. if (!(this.editor.inputManager.focused && (range != null))) {
  1427. return {};
  1428. }
  1429. caret = {
  1430. start: [],
  1431. end: null,
  1432. collapsed: true
  1433. };
  1434. caret.start = this._getNodePosition(range.startContainer, range.startOffset);
  1435. if (!range.collapsed) {
  1436. caret.end = this._getNodePosition(range.endContainer, range.endOffset);
  1437. caret.collapsed = false;
  1438. }
  1439. return caret;
  1440. } else {
  1441. if (!this.editor.inputManager.focused) {
  1442. this.editor.body.focus();
  1443. }
  1444. if (!caret.start) {
  1445. this.editor.body.blur();
  1446. return;
  1447. }
  1448. startContainer = this._getNodeByPosition(caret.start);
  1449. startOffset = caret.start[caret.start.length - 1];
  1450. if (caret.collapsed) {
  1451. endContainer = startContainer;
  1452. endOffset = startOffset;
  1453. } else {
  1454. endContainer = this._getNodeByPosition(caret.end);
  1455. endOffset = caret.start[caret.start.length - 1];
  1456. }
  1457. if (!startContainer || !endContainer) {
  1458. throw new Error('simditor: invalid caret state');
  1459. return;
  1460. }
  1461. range = document.createRange();
  1462. range.setStart(startContainer, startOffset);
  1463. range.setEnd(endContainer, endOffset);
  1464. return this.editor.selection.selectRange(range);
  1465. }
  1466. };
  1467. return UndoManager;
  1468. })(SimpleModule);
  1469. var Util,
  1470. __hasProp = {}.hasOwnProperty,
  1471. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1472. Util = (function(_super) {
  1473. __extends(Util, _super);
  1474. function Util() {
  1475. return Util.__super__.constructor.apply(this, arguments);
  1476. }
  1477. Util.pluginName = 'Util';
  1478. Util.prototype._init = function() {
  1479. this.editor = this._module;
  1480. if (this.browser.msie && this.browser.version < 11) {
  1481. return this.phBr = '';
  1482. }
  1483. };
  1484. Util.prototype.phBr = '<br/>';
  1485. Util.prototype.os = (function() {
  1486. var os;
  1487. os = {};
  1488. if (/Mac/.test(navigator.appVersion)) {
  1489. os.mac = true;
  1490. } else if (/Linux/.test(navigator.appVersion)) {
  1491. os.linux = true;
  1492. } else if (/Win/.test(navigator.appVersion)) {
  1493. os.win = true;
  1494. } else if (/X11/.test(navigator.appVersion)) {
  1495. os.unix = true;
  1496. }
  1497. if (/Mobi/.test(navigator.appVersion)) {
  1498. os.mobile = true;
  1499. }
  1500. return os;
  1501. })();
  1502. Util.prototype.browser = (function() {
  1503. var chrome, firefox, ie, safari, ua, _ref, _ref1, _ref2, _ref3;
  1504. ua = navigator.userAgent;
  1505. ie = /(msie|trident)/i.test(ua);
  1506. chrome = /chrome|crios/i.test(ua);
  1507. safari = /safari/i.test(ua) && !chrome;
  1508. firefox = /firefox/i.test(ua);
  1509. if (ie) {
  1510. return {
  1511. msie: true,
  1512. version: ((_ref = ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)) != null ? _ref[2] : void 0) * 1
  1513. };
  1514. } else if (chrome) {
  1515. return {
  1516. webkit: true,
  1517. chrome: true,
  1518. version: ((_ref1 = ua.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)) != null ? _ref1[1] : void 0) * 1
  1519. };
  1520. } else if (safari) {
  1521. return {
  1522. webkit: true,
  1523. safari: true,
  1524. version: ((_ref2 = ua.match(/version\/(\d+(\.\d+)?)/i)) != null ? _ref2[1] : void 0) * 1
  1525. };
  1526. } else if (firefox) {
  1527. return {
  1528. mozilla: true,
  1529. firefox: true,
  1530. version: ((_ref3 = ua.match(/firefox\/(\d+(\.\d+)?)/i)) != null ? _ref3[1] : void 0) * 1
  1531. };
  1532. } else {
  1533. return {};
  1534. }
  1535. })();
  1536. Util.prototype.supportSelectionChange = (function() {
  1537. var e, onselectionchange;
  1538. onselectionchange = document.onselectionchange;
  1539. if (onselectionchange !== void 0) {
  1540. try {
  1541. document.onselectionchange = 0;
  1542. return document.onselectionchange === null;
  1543. } catch (_error) {
  1544. e = _error;
  1545. } finally {
  1546. document.onselectionchange = onselectionchange;
  1547. }
  1548. }
  1549. return false;
  1550. })();
  1551. Util.prototype.reflow = function(el) {
  1552. if (el == null) {
  1553. el = document;
  1554. }
  1555. return $(el)[0].offsetHeight;
  1556. };
  1557. Util.prototype.metaKey = function(e) {
  1558. var isMac;
  1559. isMac = /Mac/.test(navigator.userAgent);
  1560. if (isMac) {
  1561. return e.metaKey;
  1562. } else {
  1563. return e.ctrlKey;
  1564. }
  1565. };
  1566. Util.prototype.isEmptyNode = function(node) {
  1567. var $node;
  1568. $node = $(node);
  1569. return $node.is(':empty') || (!$node.text() && !$node.find(':not(br, span, div)').length);
  1570. };
  1571. Util.prototype.isBlockNode = function(node) {
  1572. node = $(node)[0];
  1573. if (!node || node.nodeType === 3) {
  1574. return false;
  1575. }
  1576. return /^(div|p|ul|ol|li|blockquote|hr|pre|h1|h2|h3|h4|table)$/.test(node.nodeName.toLowerCase());
  1577. };
  1578. Util.prototype.closestBlockEl = function(node) {
  1579. var $node, blockEl, range;
  1580. if (node == null) {
  1581. range = this.editor.selection.getRange();
  1582. node = range != null ? range.commonAncestorContainer : void 0;
  1583. }
  1584. $node = $(node);
  1585. if (!$node.length) {
  1586. return null;
  1587. }
  1588. blockEl = $node.parentsUntil(this.editor.body).addBack();
  1589. blockEl = blockEl.filter((function(_this) {
  1590. return function(i) {
  1591. return _this.isBlockNode(blockEl.eq(i));
  1592. };
  1593. })(this));
  1594. if (blockEl.length) {
  1595. return blockEl.last();
  1596. } else {
  1597. return null;
  1598. }
  1599. };
  1600. Util.prototype.furthestNode = function(node, filter) {
  1601. var $node, blockEl, range;
  1602. if (node == null) {
  1603. range = this.editor.selection.getRange();
  1604. node = range != null ? range.commonAncestorContainer : void 0;
  1605. }
  1606. $node = $(node);
  1607. if (!$node.length) {
  1608. return null;
  1609. }
  1610. blockEl = $node.parentsUntil(this.editor.body).addBack();
  1611. blockEl = blockEl.filter((function(_this) {
  1612. return function(i) {
  1613. var $n;
  1614. $n = blockEl.eq(i);
  1615. if ($.isFunction(filter)) {
  1616. return filter($n);
  1617. } else {
  1618. return $n.is(filter);
  1619. }
  1620. };
  1621. })(this));
  1622. if (blockEl.length) {
  1623. return blockEl.first();
  1624. } else {
  1625. return null;
  1626. }
  1627. };
  1628. Util.prototype.furthestBlockEl = function(node) {
  1629. return this.furthestNode(node, this.isBlockNode);
  1630. };
  1631. Util.prototype.getNodeLength = function(node) {
  1632. switch (node.nodeType) {
  1633. case 7:
  1634. case 10:
  1635. return 0;
  1636. case 3:
  1637. case 8:
  1638. return node.length;
  1639. default:
  1640. return node.childNodes.length;
  1641. }
  1642. };
  1643. Util.prototype.traverseUp = function(callback, node) {
  1644. var n, nodes, range, result, _i, _len, _results;
  1645. if (node == null) {
  1646. range = this.editor.selection.getRange();
  1647. node = range != null ? range.commonAncestorContainer : void 0;
  1648. }
  1649. if ((node == null) || !$.contains(this.editor.body[0], node)) {
  1650. return false;
  1651. }
  1652. nodes = $(node).parentsUntil(this.editor.body).get();
  1653. nodes.unshift(node);
  1654. _results = [];
  1655. for (_i = 0, _len = nodes.length; _i < _len; _i++) {
  1656. n = nodes[_i];
  1657. result = callback(n);
  1658. if (result === false) {
  1659. break;
  1660. } else {
  1661. _results.push(void 0);
  1662. }
  1663. }
  1664. return _results;
  1665. };
  1666. Util.prototype.indent = function() {
  1667. var $blockEl, $childList, $nextTd, $parentLi, $td, indentLevel, range, spaceNode, tagName, _ref;
  1668. $blockEl = this.editor.util.closestBlockEl();
  1669. if (!($blockEl && $blockEl.length > 0)) {
  1670. return false;
  1671. }
  1672. if ($blockEl.is('pre')) {
  1673. spaceNode = document.createTextNode('\u00A0\u00A0');
  1674. this.editor.selection.insertNode(spaceNode);
  1675. } else if ($blockEl.is('li')) {
  1676. $parentLi = $blockEl.prev('li');
  1677. if ($parentLi.length < 1) {
  1678. return false;
  1679. }
  1680. this.editor.selection.save();
  1681. tagName = $blockEl.parent()[0].tagName;
  1682. $childList = $parentLi.children('ul, ol');
  1683. if ($childList.length > 0) {
  1684. $childList.append($blockEl);
  1685. } else {
  1686. $('<' + tagName + '/>').append($blockEl).appendTo($parentLi);
  1687. }
  1688. this.editor.selection.restore();
  1689. } else if ($blockEl.is('p, h1, h2, h3, h4')) {
  1690. indentLevel = (_ref = $blockEl.attr('data-indent')) != null ? _ref : 0;
  1691. indentLevel = indentLevel * 1 + 1;
  1692. if (indentLevel > 10) {
  1693. indentLevel = 10;
  1694. }
  1695. $blockEl.attr('data-indent', indentLevel);
  1696. } else if ($blockEl.is('table')) {
  1697. range = this.editor.selection.getRange();
  1698. $td = $(range.commonAncestorContainer).closest('td');
  1699. $nextTd = $td.next('td');
  1700. if (!($nextTd.length > 0)) {
  1701. $nextTd = $td.parent('tr').next('tr').find('td:first');
  1702. }
  1703. if (!($td.length > 0 && $nextTd.length > 0)) {
  1704. return false;
  1705. }
  1706. this.editor.selection.setRangeAtEndOf($nextTd);
  1707. } else {
  1708. spaceNode = document.createTextNode('\u00A0\u00A0\u00A0\u00A0');
  1709. this.editor.selection.insertNode(spaceNode);
  1710. }
  1711. this.editor.trigger('valuechanged');
  1712. return true;
  1713. };
  1714. Util.prototype.outdent = function() {
  1715. var $blockEl, $parent, $parentLi, $prevTd, $td, button, indentLevel, range, _ref;
  1716. $blockEl = this.editor.util.closestBlockEl();
  1717. if (!($blockEl && $blockEl.length > 0)) {
  1718. return false;
  1719. }
  1720. if ($blockEl.is('pre')) {
  1721. return false;
  1722. } else if ($blockEl.is('li')) {
  1723. $parent = $blockEl.parent();
  1724. $parentLi = $parent.parent('li');
  1725. if ($parentLi.length < 1) {
  1726. button = this.editor.toolbar.findButton($parent[0].tagName.toLowerCase());
  1727. if (button != null) {
  1728. button.command();
  1729. }
  1730. return false;
  1731. }
  1732. this.editor.selection.save();
  1733. if ($blockEl.next('li').length > 0) {
  1734. $('<' + $parent[0].tagName + '/>').append($blockEl.nextAll('li')).appendTo($blockEl);
  1735. }
  1736. $blockEl.insertAfter($parentLi);
  1737. if ($parent.children('li').length < 1) {
  1738. $parent.remove();
  1739. }
  1740. this.editor.selection.restore();
  1741. } else if ($blockEl.is('p, h1, h2, h3, h4')) {
  1742. indentLevel = (_ref = $blockEl.attr('data-indent')) != null ? _ref : 0;
  1743. indentLevel = indentLevel * 1 - 1;
  1744. if (indentLevel < 0) {
  1745. indentLevel = 0;
  1746. }
  1747. $blockEl.attr('data-indent', indentLevel);
  1748. } else if ($blockEl.is('table')) {
  1749. range = this.editor.selection.getRange();
  1750. $td = $(range.commonAncestorContainer).closest('td');
  1751. $prevTd = $td.prev('td');
  1752. if (!($prevTd.length > 0)) {
  1753. $prevTd = $td.parent('tr').prev('tr').find('td:last');
  1754. }
  1755. if (!($td.length > 0 && $prevTd.length > 0)) {
  1756. return false;
  1757. }
  1758. this.editor.selection.setRangeAtEndOf($prevTd);
  1759. } else {
  1760. return false;
  1761. }
  1762. this.editor.trigger('valuechanged');
  1763. return true;
  1764. };
  1765. Util.prototype.dataURLtoBlob = function(dataURL) {
  1766. var BlobBuilder, arrayBuffer, bb, byteString, hasArrayBufferViewSupport, hasBlobConstructor, i, intArray, mimeString, _i, _ref;
  1767. hasBlobConstructor = window.Blob && (function() {
  1768. var e;
  1769. try {
  1770. return Boolean(new Blob());
  1771. } catch (_error) {
  1772. e = _error;
  1773. return false;
  1774. }
  1775. })();
  1776. hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array && (function() {
  1777. var e;
  1778. try {
  1779. return new Blob([new Uint8Array(100)]).size === 100;
  1780. } catch (_error) {
  1781. e = _error;
  1782. return false;
  1783. }
  1784. })();
  1785. BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
  1786. if (!((hasBlobConstructor || BlobBuilder) && window.atob && window.ArrayBuffer && window.Uint8Array)) {
  1787. return false;
  1788. }
  1789. if (dataURL.split(',')[0].indexOf('base64') >= 0) {
  1790. byteString = atob(dataURL.split(',')[1]);
  1791. } else {
  1792. byteString = decodeURIComponent(dataURL.split(',')[1]);
  1793. }
  1794. arrayBuffer = new ArrayBuffer(byteString.length);
  1795. intArray = new Uint8Array(arrayBuffer);
  1796. for (i = _i = 0, _ref = byteString.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
  1797. intArray[i] = byteString.charCodeAt(i);
  1798. }
  1799. mimeString = dataURL.split(',')[0].split(':')[1].split(';')[0];
  1800. if (hasBlobConstructor) {
  1801. return new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
  1802. type: mimeString
  1803. });
  1804. }
  1805. bb = new BlobBuilder();
  1806. bb.append(arrayBuffer);
  1807. return bb.getBlob(mimeString);
  1808. };
  1809. return Util;
  1810. })(SimpleModule);
  1811. var Toolbar,
  1812. __hasProp = {}.hasOwnProperty,
  1813. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1814. Toolbar = (function(_super) {
  1815. __extends(Toolbar, _super);
  1816. function Toolbar() {
  1817. return Toolbar.__super__.constructor.apply(this, arguments);
  1818. }
  1819. Toolbar.pluginName = 'Toolbar';
  1820. Toolbar.prototype.opts = {
  1821. toolbar: true,
  1822. toolbarFloat: true,
  1823. toolbarHidden: false,
  1824. toolbarFloatOffset: 0
  1825. };
  1826. Toolbar.prototype._tpl = {
  1827. wrapper: '<div class="simditor-toolbar"><ul></ul></div>',
  1828. separator: '<li><span class="separator"></span></li>'
  1829. };
  1830. Toolbar.prototype._init = function() {
  1831. var toolbarHeight;
  1832. this.editor = this._module;
  1833. if (!this.opts.toolbar) {
  1834. return;
  1835. }
  1836. if (!$.isArray(this.opts.toolbar)) {
  1837. this.opts.toolbar = ['bold', 'italic', 'underline', 'strikethrough', '|', 'ol', 'ul', 'blockquote', 'code', '|', 'link', 'image', '|', 'indent', 'outdent'];
  1838. }
  1839. this._render();
  1840. this.list.on('click', (function(_this) {
  1841. return function(e) {
  1842. return false;
  1843. };
  1844. })(this));
  1845. this.wrapper.on('mousedown', (function(_this) {
  1846. return function(e) {
  1847. return _this.list.find('.menu-on').removeClass('.menu-on');
  1848. };
  1849. })(this));
  1850. $(document).on('mousedown.simditor' + this.editor.id, (function(_this) {
  1851. return function(e) {
  1852. return _this.list.find('.menu-on').removeClass('.menu-on');
  1853. };
  1854. })(this));
  1855. if (!this.opts.toolbarHidden && this.opts.toolbarFloat) {
  1856. this.wrapper.width(this.wrapper.outerWidth());
  1857. this.wrapper.css('top', this.opts.toolbarFloatOffset);
  1858. toolbarHeight = this.wrapper.outerHeight();
  1859. if (!this.editor.util.os.mobile) {
  1860. $(window).on('resize.simditor-' + this.editor.id, (function(_this) {
  1861. return function(e) {
  1862. _this.wrapper.css('position', 'static');
  1863. _this.editor.util.reflow(_this.wrapper);
  1864. _this.wrapper.css('left', _this.wrapper.offset().left);
  1865. return _this.wrapper.css('position', '');
  1866. };
  1867. })(this)).resize();
  1868. }
  1869. $(window).on('scroll.simditor-' + this.editor.id, (function(_this) {
  1870. return function(e) {
  1871. var bottomEdge, scrollTop, topEdge;
  1872. topEdge = _this.editor.wrapper.offset().top;
  1873. bottomEdge = topEdge + _this.editor.wrapper.outerHeight() - 80;
  1874. scrollTop = $(document).scrollTop() + _this.opts.toolbarFloatOffset;
  1875. if (scrollTop <= topEdge || scrollTop >= bottomEdge) {
  1876. _this.editor.wrapper.removeClass('toolbar-floating').css('padding-top', '');
  1877. if (_this.editor.util.os.mobile) {
  1878. return _this.wrapper.css('top', _this.opts.toolbarFloatOffset);
  1879. }
  1880. } else {
  1881. _this.editor.wrapper.addClass('toolbar-floating').css('padding-top', toolbarHeight);
  1882. if (_this.editor.util.os.mobile) {
  1883. return _this.wrapper.css('top', scrollTop - topEdge + _this.opts.toolbarFloatOffset);
  1884. }
  1885. }
  1886. };
  1887. })(this));
  1888. }
  1889. this.editor.on('selectionchanged', (function(_this) {
  1890. return function() {
  1891. return _this.toolbarStatus();
  1892. };
  1893. })(this));
  1894. this.editor.on('destroy', (function(_this) {
  1895. return function() {
  1896. return _this.buttons.length = 0;
  1897. };
  1898. })(this));
  1899. return $(document).on('mousedown.simditor-' + this.editor.id, (function(_this) {
  1900. return function(e) {
  1901. return _this.list.find('li.menu-on').removeClass('menu-on');
  1902. };
  1903. })(this));
  1904. };
  1905. Toolbar.prototype._render = function() {
  1906. var name, _i, _len, _ref;
  1907. this.buttons = [];
  1908. this.wrapper = $(this._tpl.wrapper).prependTo(this.editor.wrapper);
  1909. this.list = this.wrapper.find('ul');
  1910. _ref = this.opts.toolbar;
  1911. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  1912. name = _ref[_i];
  1913. if (name === '|') {
  1914. $(this._tpl.separator).appendTo(this.list);
  1915. continue;
  1916. }
  1917. if (!this.constructor.buttons[name]) {
  1918. throw new Error('simditor: invalid toolbar button "' + name + '"');
  1919. continue;
  1920. }
  1921. this.buttons.push(new this.constructor.buttons[name]({
  1922. editor: this.editor
  1923. }));
  1924. }
  1925. if (this.opts.toolbarHidden) {
  1926. return this.wrapper.hide();
  1927. } else {
  1928. return this.editor.placeholderEl.css('top', this.wrapper.outerHeight());
  1929. }
  1930. };
  1931. Toolbar.prototype.toolbarStatus = function(name) {
  1932. var buttons;
  1933. if (!this.editor.inputManager.focused) {
  1934. return;
  1935. }
  1936. buttons = this.buttons.slice(0);
  1937. return this.editor.util.traverseUp((function(_this) {
  1938. return function(node) {
  1939. var button, i, removeButtons, _i, _j, _len, _len1;
  1940. removeButtons = [];
  1941. for (i = _i = 0, _len = buttons.length; _i < _len; i = ++_i) {
  1942. button = buttons[i];
  1943. if ((name != null) && button.name !== name) {
  1944. continue;
  1945. }
  1946. if (!button.status || button.status($(node)) === true) {
  1947. removeButtons.push(button);
  1948. }
  1949. }
  1950. for (_j = 0, _len1 = removeButtons.length; _j < _len1; _j++) {
  1951. button = removeButtons[_j];
  1952. i = $.inArray(button, buttons);
  1953. buttons.splice(i, 1);
  1954. }
  1955. if (buttons.length === 0) {
  1956. return false;
  1957. }
  1958. };
  1959. })(this));
  1960. };
  1961. Toolbar.prototype.findButton = function(name) {
  1962. var button;
  1963. button = this.list.find('.toolbar-item-' + name).data('button');
  1964. return button != null ? button : null;
  1965. };
  1966. Toolbar.addButton = function(btn) {
  1967. return this.buttons[btn.prototype.name] = btn;
  1968. };
  1969. Toolbar.buttons = {};
  1970. return Toolbar;
  1971. })(SimpleModule);
  1972. var Simditor,
  1973. __hasProp = {}.hasOwnProperty,
  1974. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  1975. Simditor = (function(_super) {
  1976. __extends(Simditor, _super);
  1977. function Simditor() {
  1978. return Simditor.__super__.constructor.apply(this, arguments);
  1979. }
  1980. Simditor.connect(Util);
  1981. Simditor.connect(InputManager);
  1982. Simditor.connect(UndoManager);
  1983. Simditor.connect(Keystroke);
  1984. Simditor.connect(Formatter);
  1985. Simditor.connect(Selection);
  1986. Simditor.connect(Toolbar);
  1987. Simditor.count = 0;
  1988. Simditor.prototype.opts = {
  1989. textarea: null,
  1990. placeholder: '',
  1991. defaultImage: 'images/image.png',
  1992. params: {},
  1993. upload: false,
  1994. tabIndent: true
  1995. };
  1996. Simditor.prototype._init = function() {
  1997. var e, editor, form, uploadOpts;
  1998. this.textarea = $(this.opts.textarea);
  1999. this.opts.placeholder = this.opts.placeholder || this.textarea.attr('placeholder');
  2000. if (!this.textarea.length) {
  2001. throw new Error('simditor: param textarea is required.');
  2002. return;
  2003. }
  2004. editor = this.textarea.data('simditor');
  2005. if (editor != null) {
  2006. editor.destroy();
  2007. }
  2008. this.id = ++Simditor.count;
  2009. this._render();
  2010. if (this.opts.upload && simpleUploader) {
  2011. uploadOpts = typeof this.opts.upload === 'object' ? this.opts.upload : {};
  2012. this.uploader = simpleUploader(uploadOpts);
  2013. }
  2014. form = this.textarea.closest('form');
  2015. if (form.length) {
  2016. form.on('submit.simditor-' + this.id, (function(_this) {
  2017. return function() {
  2018. return _this.sync();
  2019. };
  2020. })(this));
  2021. form.on('reset.simditor-' + this.id, (function(_this) {
  2022. return function() {
  2023. return _this.setValue('');
  2024. };
  2025. })(this));
  2026. }
  2027. this.on('initialized', (function(_this) {
  2028. return function() {
  2029. if (_this.opts.placeholder) {
  2030. _this.on('valuechanged', function() {
  2031. return _this._placeholder();
  2032. });
  2033. }
  2034. return _this.setValue(_this.textarea.val().trim() || '');
  2035. };
  2036. })(this));
  2037. if (this.util.browser.mozilla) {
  2038. this.util.reflow();
  2039. try {
  2040. document.execCommand("enableObjectResizing", false, false);
  2041. return document.execCommand("enableInlineTableEditing", false, false);
  2042. } catch (_error) {
  2043. e = _error;
  2044. }
  2045. }
  2046. };
  2047. Simditor.prototype._tpl = "<div class=\"simditor\">\n <div class=\"simditor-wrapper\">\n <div class=\"simditor-placeholder\"></div>\n <div class=\"simditor-body\" contenteditable=\"true\">\n </div>\n </div>\n</div>";
  2048. Simditor.prototype._render = function() {
  2049. var key, val, _ref, _results;
  2050. this.el = $(this._tpl).insertBefore(this.textarea);
  2051. this.wrapper = this.el.find('.simditor-wrapper');
  2052. this.body = this.wrapper.find('.simditor-body');
  2053. this.placeholderEl = this.wrapper.find('.simditor-placeholder').append(this.opts.placeholder);
  2054. this.el.append(this.textarea).data('simditor', this);
  2055. this.textarea.data('simditor', this).hide().blur();
  2056. this.body.attr('tabindex', this.textarea.attr('tabindex'));
  2057. if (this.util.os.mac) {
  2058. this.el.addClass('simditor-mac');
  2059. } else if (this.util.os.linux) {
  2060. this.el.addClass('simditor-linux');
  2061. }
  2062. if (this.util.os.mobile) {
  2063. this.el.addClass('simditor-mobile');
  2064. }
  2065. if (this.opts.params) {
  2066. _ref = this.opts.params;
  2067. _results = [];
  2068. for (key in _ref) {
  2069. val = _ref[key];
  2070. _results.push($('<input/>', {
  2071. type: 'hidden',
  2072. name: key,
  2073. value: val
  2074. }).insertAfter(this.textarea));
  2075. }
  2076. return _results;
  2077. }
  2078. };
  2079. Simditor.prototype._placeholder = function() {
  2080. var children, _ref;
  2081. children = this.body.children();
  2082. if (children.length === 0 || (children.length === 1 && this.util.isEmptyNode(children) && ((_ref = children.data('indent')) != null ? _ref : 0) < 1)) {
  2083. return this.placeholderEl.show();
  2084. } else {
  2085. return this.placeholderEl.hide();
  2086. }
  2087. };
  2088. Simditor.prototype.setValue = function(val) {
  2089. this.hidePopover();
  2090. this.textarea.val(val);
  2091. this.body.html(val);
  2092. this.formatter.format();
  2093. this.formatter.decorate();
  2094. this.util.reflow(this.body);
  2095. this.inputManager.lastCaretPosition = null;
  2096. return this.trigger('valuechanged');
  2097. };
  2098. Simditor.prototype.getValue = function() {
  2099. return this.sync();
  2100. };
  2101. Simditor.prototype.sync = function() {
  2102. var children, cloneBody, emptyP, firstP, lastP, val;
  2103. cloneBody = this.body.clone();
  2104. this.formatter.undecorate(cloneBody);
  2105. this.formatter.format(cloneBody);
  2106. this.formatter.autolink(cloneBody);
  2107. children = cloneBody.children();
  2108. lastP = children.last('p');
  2109. firstP = children.first('p');
  2110. while (lastP.is('p') && this.util.isEmptyNode(lastP)) {
  2111. emptyP = lastP;
  2112. lastP = lastP.prev('p');
  2113. emptyP.remove();
  2114. }
  2115. while (firstP.is('p') && this.util.isEmptyNode(firstP)) {
  2116. emptyP = firstP;
  2117. firstP = lastP.next('p');
  2118. emptyP.remove();
  2119. }
  2120. cloneBody.find('img.uploading').remove();
  2121. val = $.trim(cloneBody.html());
  2122. this.textarea.val(val);
  2123. return val;
  2124. };
  2125. Simditor.prototype.focus = function() {
  2126. var $blockEl, range;
  2127. if (this.inputManager.lastCaretPosition) {
  2128. return this.undoManager.caretPosition(this.inputManager.lastCaretPosition);
  2129. } else {
  2130. $blockEl = this.body.find('p, li, pre, h1, h2, h3, h4, td').first();
  2131. if (!($blockEl.length > 0)) {
  2132. return;
  2133. }
  2134. range = document.createRange();
  2135. this.selection.setRangeAtStartOf($blockEl, range);
  2136. return this.body.focus();
  2137. }
  2138. };
  2139. Simditor.prototype.blur = function() {
  2140. return this.body.blur();
  2141. };
  2142. Simditor.prototype.hidePopover = function() {
  2143. return this.el.find('.simditor-popover').each((function(_this) {
  2144. return function(i, popover) {
  2145. popover = $(popover).data('popover');
  2146. if (popover.active) {
  2147. return popover.hide();
  2148. }
  2149. };
  2150. })(this));
  2151. };
  2152. Simditor.prototype.destroy = function() {
  2153. this.triggerHandler('destroy');
  2154. this.textarea.closest('form').off('.simditor .simditor-' + this.id);
  2155. this.selection.clear();
  2156. this.inputManager.focused = false;
  2157. this.textarea.insertBefore(this.el).hide().val('').removeData('simditor');
  2158. this.el.remove();
  2159. $(document).off('.simditor-' + this.id);
  2160. $(window).off('.simditor-' + this.id);
  2161. return this.off();
  2162. };
  2163. return Simditor;
  2164. })(SimpleModule);
  2165. Simditor.i18n = {
  2166. 'zh-CN': {
  2167. 'blockquote': '引用',
  2168. 'bold': '加粗文字',
  2169. 'code': '插入代码',
  2170. 'color': '文字颜色',
  2171. 'hr': '分隔线',
  2172. 'image': '插入图片',
  2173. 'localImage': '本地图片',
  2174. 'externalImage': '外链图片',
  2175. 'uploadImage': '上传图片',
  2176. 'uploadFailed': '上传失败了',
  2177. 'uploadError': '上传出错了',
  2178. 'imageUrl': '图片地址',
  2179. 'imageSize': '图片尺寸',
  2180. 'restoreImageSize': '还原图片尺寸',
  2181. 'uploading': '正在上传',
  2182. 'indent': '向右缩进',
  2183. 'outdent': '向左缩进',
  2184. 'italic': '斜体文字',
  2185. 'link': '插入链接',
  2186. 'text': '文本',
  2187. 'linkText': '链接文字',
  2188. 'linkUrl': '地址',
  2189. 'removeLink': '移除链接',
  2190. 'ol': '有序列表',
  2191. 'ul': '无序列表',
  2192. 'strikethrough': '删除线文字',
  2193. 'table': '表格',
  2194. 'deleteRow': '删除行',
  2195. 'insertRowAbove': '在上面插入行',
  2196. 'insertRowBelow': '在下面插入行',
  2197. 'deleteColumn': '删除列',
  2198. 'insertColumnLeft': '在左边插入列',
  2199. 'insertColumnRight': '在右边插入列',
  2200. 'deleteTable': '删除表格',
  2201. 'title': '标题',
  2202. 'normalText': '普通文本',
  2203. 'underline': '下划线文字'
  2204. }
  2205. };
  2206. var Button,
  2207. __hasProp = {}.hasOwnProperty,
  2208. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  2209. __slice = [].slice;
  2210. Button = (function(_super) {
  2211. __extends(Button, _super);
  2212. Button.prototype._tpl = {
  2213. item: '<li><a tabindex="-1" unselectable="on" class="toolbar-item" href="javascript:;"><span></span></a></li>',
  2214. menuWrapper: '<div class="toolbar-menu"></div>',
  2215. menuItem: '<li><a tabindex="-1" unselectable="on" class="menu-item" href="javascript:;"><span></span></a></li>',
  2216. separator: '<li><span class="separator"></span></li>'
  2217. };
  2218. Button.prototype.name = '';
  2219. Button.prototype.icon = '';
  2220. Button.prototype.title = '';
  2221. Button.prototype.text = '';
  2222. Button.prototype.htmlTag = '';
  2223. Button.prototype.disableTag = '';
  2224. Button.prototype.menu = false;
  2225. Button.prototype.active = false;
  2226. Button.prototype.disabled = false;
  2227. Button.prototype.needFocus = true;
  2228. Button.prototype.shortcut = null;
  2229. function Button(opts) {
  2230. this.editor = opts.editor;
  2231. this.title = this._t(this.name);
  2232. Button.__super__.constructor.call(this, opts);
  2233. }
  2234. Button.prototype._init = function() {
  2235. var tag, _i, _len, _ref, _results;
  2236. this.render();
  2237. this.el.on('mousedown', (function(_this) {
  2238. return function(e) {
  2239. var exceed, param;
  2240. e.preventDefault();
  2241. if (_this.el.hasClass('disabled') || (_this.needFocus && !_this.editor.inputManager.focused)) {
  2242. return false;
  2243. }
  2244. if (_this.menu) {
  2245. _this.wrapper.toggleClass('menu-on').siblings('li').removeClass('menu-on');
  2246. if (_this.wrapper.is('.menu-on')) {
  2247. exceed = _this.menuWrapper.offset().left + _this.menuWrapper.outerWidth() + 5 - _this.editor.wrapper.offset().left - _this.editor.wrapper.outerWidth();
  2248. if (exceed > 0) {
  2249. _this.menuWrapper.css({
  2250. 'left': 'auto',
  2251. 'right': 0
  2252. });
  2253. }
  2254. _this.trigger('menuexpand');
  2255. }
  2256. return false;
  2257. }
  2258. param = _this.el.data('param');
  2259. _this.command(param);
  2260. return false;
  2261. };
  2262. })(this));
  2263. this.wrapper.on('click', 'a.menu-item', (function(_this) {
  2264. return function(e) {
  2265. var btn, param;
  2266. e.preventDefault();
  2267. btn = $(e.currentTarget);
  2268. _this.wrapper.removeClass('menu-on');
  2269. if (btn.hasClass('disabled') || (_this.needFocus && !_this.editor.inputManager.focused)) {
  2270. return false;
  2271. }
  2272. _this.editor.toolbar.wrapper.removeClass('menu-on');
  2273. param = btn.data('param');
  2274. _this.command(param);
  2275. return false;
  2276. };
  2277. })(this));
  2278. this.wrapper.on('mousedown', 'a.menu-item', (function(_this) {
  2279. return function(e) {
  2280. return false;
  2281. };
  2282. })(this));
  2283. this.editor.on('blur', (function(_this) {
  2284. return function() {
  2285. _this.setActive(false);
  2286. return _this.setDisabled(false);
  2287. };
  2288. })(this));
  2289. if (this.shortcut != null) {
  2290. this.editor.inputManager.addShortcut(this.shortcut, (function(_this) {
  2291. return function(e) {
  2292. _this.el.mousedown();
  2293. return false;
  2294. };
  2295. })(this));
  2296. }
  2297. _ref = this.htmlTag.split(',');
  2298. _results = [];
  2299. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2300. tag = _ref[_i];
  2301. tag = $.trim(tag);
  2302. if (tag && $.inArray(tag, this.editor.formatter._allowedTags) < 0) {
  2303. _results.push(this.editor.formatter._allowedTags.push(tag));
  2304. } else {
  2305. _results.push(void 0);
  2306. }
  2307. }
  2308. return _results;
  2309. };
  2310. Button.prototype.render = function() {
  2311. this.wrapper = $(this._tpl.item).appendTo(this.editor.toolbar.list);
  2312. this.el = this.wrapper.find('a.toolbar-item');
  2313. this.el.attr('title', this.title).addClass('toolbar-item-' + this.name).data('button', this);
  2314. this.el.find('span').addClass(this.icon ? 'fa fa-' + this.icon : '').text(this.text);
  2315. if (!this.menu) {
  2316. return;
  2317. }
  2318. this.menuWrapper = $(this._tpl.menuWrapper).appendTo(this.wrapper);
  2319. this.menuWrapper.addClass('toolbar-menu-' + this.name);
  2320. return this.renderMenu();
  2321. };
  2322. Button.prototype.renderMenu = function() {
  2323. var $menuBtntnEl, $menuItemEl, menuItem, _i, _len, _ref, _ref1, _results;
  2324. if (!$.isArray(this.menu)) {
  2325. return;
  2326. }
  2327. this.menuEl = $('<ul/>').appendTo(this.menuWrapper);
  2328. _ref = this.menu;
  2329. _results = [];
  2330. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2331. menuItem = _ref[_i];
  2332. if (menuItem === '|') {
  2333. $(this._tpl.separator).appendTo(this.menuEl);
  2334. continue;
  2335. }
  2336. $menuItemEl = $(this._tpl.menuItem).appendTo(this.menuEl);
  2337. _results.push($menuBtntnEl = $menuItemEl.find('a.menu-item').attr({
  2338. 'title': (_ref1 = menuItem.title) != null ? _ref1 : menuItem.text,
  2339. 'data-param': menuItem.param
  2340. }).addClass('menu-item-' + menuItem.name).find('span').text(menuItem.text));
  2341. }
  2342. return _results;
  2343. };
  2344. Button.prototype.setActive = function(active) {
  2345. if (active === this.active) {
  2346. return;
  2347. }
  2348. this.active = active;
  2349. this.el.toggleClass('active', this.active);
  2350. return this.editor.toolbar.trigger('buttonstatus', [this]);
  2351. };
  2352. Button.prototype.setDisabled = function(disabled) {
  2353. if (disabled === this.disabled) {
  2354. return;
  2355. }
  2356. this.disabled = disabled;
  2357. this.el.toggleClass('disabled', this.disabled);
  2358. return this.editor.toolbar.trigger('buttonstatus', [this]);
  2359. };
  2360. Button.prototype.status = function($node) {
  2361. if ($node != null) {
  2362. this.setDisabled($node.is(this.disableTag));
  2363. }
  2364. if (this.disabled) {
  2365. return true;
  2366. }
  2367. if ($node != null) {
  2368. this.setActive($node.is(this.htmlTag));
  2369. }
  2370. return this.active;
  2371. };
  2372. Button.prototype.command = function(param) {};
  2373. Button.prototype._t = function() {
  2374. var args, result, _ref;
  2375. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  2376. result = Button.__super__._t.apply(this, args);
  2377. if (!result) {
  2378. result = (_ref = this.editor)._t.apply(_ref, args);
  2379. }
  2380. return result;
  2381. };
  2382. return Button;
  2383. })(SimpleModule);
  2384. Simditor.Button = Button;
  2385. var Popover,
  2386. __hasProp = {}.hasOwnProperty,
  2387. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2388. Popover = (function(_super) {
  2389. __extends(Popover, _super);
  2390. Popover.prototype.offset = {
  2391. top: 4,
  2392. left: 0
  2393. };
  2394. Popover.prototype.target = null;
  2395. Popover.prototype.active = false;
  2396. function Popover(opts) {
  2397. this.button = opts.button;
  2398. this.editor = opts.button.editor;
  2399. Popover.__super__.constructor.call(this, opts);
  2400. }
  2401. Popover.prototype._init = function() {
  2402. this.el = $('<div class="simditor-popover"></div>').appendTo(this.editor.el).data('popover', this);
  2403. this.render();
  2404. this.el.on('mouseenter', (function(_this) {
  2405. return function(e) {
  2406. return _this.el.addClass('hover');
  2407. };
  2408. })(this));
  2409. return this.el.on('mouseleave', (function(_this) {
  2410. return function(e) {
  2411. return _this.el.removeClass('hover');
  2412. };
  2413. })(this));
  2414. };
  2415. Popover.prototype.render = function() {};
  2416. Popover.prototype.show = function($target, position) {
  2417. if (position == null) {
  2418. position = 'bottom';
  2419. }
  2420. if ($target == null) {
  2421. return;
  2422. }
  2423. this.el.siblings('.simditor-popover').each((function(_this) {
  2424. return function(i, popover) {
  2425. popover = $(popover).data('popover');
  2426. if (popover.active) {
  2427. return popover.hide();
  2428. }
  2429. };
  2430. })(this));
  2431. this.target = $target.addClass('selected');
  2432. if (this.active) {
  2433. this.refresh(position);
  2434. return this.trigger('popovershow');
  2435. } else {
  2436. this.active = true;
  2437. this.el.css({
  2438. left: -9999
  2439. }).show();
  2440. return setTimeout((function(_this) {
  2441. return function() {
  2442. _this.refresh(position);
  2443. return _this.trigger('popovershow');
  2444. };
  2445. })(this), 0);
  2446. }
  2447. };
  2448. Popover.prototype.hide = function() {
  2449. if (!this.active) {
  2450. return;
  2451. }
  2452. if (this.target) {
  2453. this.target.removeClass('selected');
  2454. }
  2455. this.target = null;
  2456. this.active = false;
  2457. this.el.hide();
  2458. return this.trigger('popoverhide');
  2459. };
  2460. Popover.prototype.refresh = function(position) {
  2461. var editorOffset, left, targetH, targetOffset, top;
  2462. if (position == null) {
  2463. position = 'bottom';
  2464. }
  2465. if (!this.active) {
  2466. return;
  2467. }
  2468. editorOffset = this.editor.el.offset();
  2469. targetOffset = this.target.offset();
  2470. targetH = this.target.outerHeight();
  2471. if (position === 'bottom') {
  2472. top = targetOffset.top - editorOffset.top + targetH;
  2473. } else if (position === 'top') {
  2474. top = targetOffset.top - editorOffset.top - this.el.height();
  2475. }
  2476. left = Math.min(targetOffset.left - editorOffset.left, this.editor.wrapper.width() - this.el.outerWidth() - 10);
  2477. return this.el.css({
  2478. top: top + this.offset.top,
  2479. left: left + this.offset.left
  2480. });
  2481. };
  2482. Popover.prototype.destroy = function() {
  2483. this.target = null;
  2484. this.active = false;
  2485. this.editor.off('.linkpopover');
  2486. return this.el.remove();
  2487. };
  2488. return Popover;
  2489. })(SimpleModule);
  2490. Simditor.Popover = Popover;
  2491. var TitleButton,
  2492. __hasProp = {}.hasOwnProperty,
  2493. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2494. TitleButton = (function(_super) {
  2495. __extends(TitleButton, _super);
  2496. function TitleButton() {
  2497. return TitleButton.__super__.constructor.apply(this, arguments);
  2498. }
  2499. TitleButton.prototype.name = 'title';
  2500. TitleButton.prototype.htmlTag = 'h1, h2, h3, h4';
  2501. TitleButton.prototype.disableTag = 'pre, table';
  2502. TitleButton.prototype._init = function() {
  2503. this.menu = [
  2504. {
  2505. name: 'normal',
  2506. text: this._t('normalText'),
  2507. param: 'p'
  2508. }, '|', {
  2509. name: 'h1',
  2510. text: this._t('title') + ' 1',
  2511. param: 'h1'
  2512. }, {
  2513. name: 'h2',
  2514. text: this._t('title') + ' 2',
  2515. param: 'h2'
  2516. }, {
  2517. name: 'h3',
  2518. text: this._t('title') + ' 3',
  2519. param: 'h3'
  2520. }, {
  2521. name: 'h4',
  2522. text: this._t('title') + ' 4',
  2523. param: 'h4'
  2524. }, {
  2525. name: 'h5',
  2526. text: this._t('title') + ' 5',
  2527. param: 'h5'
  2528. }
  2529. ];
  2530. return TitleButton.__super__._init.call(this);
  2531. };
  2532. TitleButton.prototype.setActive = function(active, param) {
  2533. TitleButton.__super__.setActive.call(this, active);
  2534. this.el.removeClass('active-p active-h1 active-h2 active-h3');
  2535. if (active) {
  2536. return this.el.addClass('active active-' + param);
  2537. }
  2538. };
  2539. TitleButton.prototype.status = function($node) {
  2540. var param, _ref;
  2541. if ($node != null) {
  2542. this.setDisabled($node.is(this.disableTag));
  2543. }
  2544. if (this.disabled) {
  2545. return true;
  2546. }
  2547. if ($node != null) {
  2548. param = (_ref = $node[0].tagName) != null ? _ref.toLowerCase() : void 0;
  2549. this.setActive($node.is(this.htmlTag), param);
  2550. }
  2551. return this.active;
  2552. };
  2553. TitleButton.prototype.command = function(param) {
  2554. var $contents, $endBlock, $startBlock, endNode, node, range, results, startNode, _i, _len, _ref;
  2555. range = this.editor.selection.getRange();
  2556. startNode = range.startContainer;
  2557. endNode = range.endContainer;
  2558. $startBlock = this.editor.util.closestBlockEl(startNode);
  2559. $endBlock = this.editor.util.closestBlockEl(endNode);
  2560. this.editor.selection.save();
  2561. range.setStartBefore($startBlock[0]);
  2562. range.setEndAfter($endBlock[0]);
  2563. $contents = $(range.extractContents());
  2564. results = [];
  2565. $contents.children().each((function(_this) {
  2566. return function(i, el) {
  2567. var c, converted, _i, _len, _results;
  2568. converted = _this._convertEl(el, param);
  2569. _results = [];
  2570. for (_i = 0, _len = converted.length; _i < _len; _i++) {
  2571. c = converted[_i];
  2572. _results.push(results.push(c));
  2573. }
  2574. return _results;
  2575. };
  2576. })(this));
  2577. _ref = results.reverse();
  2578. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2579. node = _ref[_i];
  2580. range.insertNode(node[0]);
  2581. }
  2582. this.editor.selection.restore();
  2583. return this.editor.trigger('valuechanged');
  2584. };
  2585. TitleButton.prototype._convertEl = function(el, param) {
  2586. var $block, $el, results;
  2587. $el = $(el);
  2588. results = [];
  2589. if ($el.is(param)) {
  2590. results.push($el);
  2591. } else {
  2592. $block = $('<' + param + '/>').append($el.contents());
  2593. results.push($block);
  2594. }
  2595. return results;
  2596. };
  2597. return TitleButton;
  2598. })(Button);
  2599. Simditor.Toolbar.addButton(TitleButton);
  2600. var BoldButton,
  2601. __hasProp = {}.hasOwnProperty,
  2602. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2603. BoldButton = (function(_super) {
  2604. __extends(BoldButton, _super);
  2605. function BoldButton() {
  2606. return BoldButton.__super__.constructor.apply(this, arguments);
  2607. }
  2608. BoldButton.prototype.name = 'bold';
  2609. BoldButton.prototype.icon = 'bold';
  2610. BoldButton.prototype.htmlTag = 'b, strong';
  2611. BoldButton.prototype.disableTag = 'pre';
  2612. BoldButton.prototype.shortcut = 'cmd+b';
  2613. BoldButton.prototype._init = function() {
  2614. if (this.editor.util.os.mac) {
  2615. this.title = this.title + ' ( Cmd + b )';
  2616. } else {
  2617. this.title = this.title + ' ( Ctrl + b )';
  2618. this.shortcut = 'ctrl+b';
  2619. }
  2620. return BoldButton.__super__._init.call(this);
  2621. };
  2622. BoldButton.prototype.status = function($node) {
  2623. var active;
  2624. if ($node != null) {
  2625. this.setDisabled($node.is(this.disableTag));
  2626. }
  2627. if (this.disabled) {
  2628. return true;
  2629. }
  2630. active = document.queryCommandState('bold') === true;
  2631. this.setActive(active);
  2632. return active;
  2633. };
  2634. BoldButton.prototype.command = function() {
  2635. document.execCommand('bold');
  2636. this.editor.trigger('valuechanged');
  2637. return $(document).trigger('selectionchange');
  2638. };
  2639. return BoldButton;
  2640. })(Button);
  2641. Simditor.Toolbar.addButton(BoldButton);
  2642. var ItalicButton,
  2643. __hasProp = {}.hasOwnProperty,
  2644. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2645. ItalicButton = (function(_super) {
  2646. __extends(ItalicButton, _super);
  2647. function ItalicButton() {
  2648. return ItalicButton.__super__.constructor.apply(this, arguments);
  2649. }
  2650. ItalicButton.prototype.name = 'italic';
  2651. ItalicButton.prototype.icon = 'italic';
  2652. ItalicButton.prototype.htmlTag = 'i';
  2653. ItalicButton.prototype.disableTag = 'pre';
  2654. ItalicButton.prototype.shortcut = 'cmd+i';
  2655. ItalicButton.prototype._init = function() {
  2656. if (this.editor.util.os.mac) {
  2657. this.title = this.title + ' ( Cmd + i )';
  2658. } else {
  2659. this.title = this.title + ' ( Ctrl + i )';
  2660. this.shortcut = 'ctrl+i';
  2661. }
  2662. return ItalicButton.__super__._init.call(this);
  2663. };
  2664. ItalicButton.prototype.status = function($node) {
  2665. var active;
  2666. if ($node != null) {
  2667. this.setDisabled($node.is(this.disableTag));
  2668. }
  2669. if (this.disabled) {
  2670. return this.disabled;
  2671. }
  2672. active = document.queryCommandState('italic') === true;
  2673. this.setActive(active);
  2674. return active;
  2675. };
  2676. ItalicButton.prototype.command = function() {
  2677. document.execCommand('italic');
  2678. this.editor.trigger('valuechanged');
  2679. return $(document).trigger('selectionchange');
  2680. };
  2681. return ItalicButton;
  2682. })(Button);
  2683. Simditor.Toolbar.addButton(ItalicButton);
  2684. var UnderlineButton,
  2685. __hasProp = {}.hasOwnProperty,
  2686. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2687. UnderlineButton = (function(_super) {
  2688. __extends(UnderlineButton, _super);
  2689. function UnderlineButton() {
  2690. return UnderlineButton.__super__.constructor.apply(this, arguments);
  2691. }
  2692. UnderlineButton.prototype.name = 'underline';
  2693. UnderlineButton.prototype.icon = 'underline';
  2694. UnderlineButton.prototype.htmlTag = 'u';
  2695. UnderlineButton.prototype.disableTag = 'pre';
  2696. UnderlineButton.prototype.shortcut = 'cmd+u';
  2697. UnderlineButton.prototype.render = function() {
  2698. if (this.editor.util.os.mac) {
  2699. this.title = this.title + ' ( Cmd + u )';
  2700. } else {
  2701. this.title = this.title + ' ( Ctrl + u )';
  2702. this.shortcut = 'ctrl+u';
  2703. }
  2704. return UnderlineButton.__super__.render.call(this);
  2705. };
  2706. UnderlineButton.prototype.status = function($node) {
  2707. var active;
  2708. if ($node != null) {
  2709. this.setDisabled($node.is(this.disableTag));
  2710. }
  2711. if (this.disabled) {
  2712. return this.disabled;
  2713. }
  2714. active = document.queryCommandState('underline') === true;
  2715. this.setActive(active);
  2716. return active;
  2717. };
  2718. UnderlineButton.prototype.command = function() {
  2719. document.execCommand('underline');
  2720. this.editor.trigger('valuechanged');
  2721. return $(document).trigger('selectionchange');
  2722. };
  2723. return UnderlineButton;
  2724. })(Button);
  2725. Simditor.Toolbar.addButton(UnderlineButton);
  2726. var ColorButton,
  2727. __hasProp = {}.hasOwnProperty,
  2728. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  2729. __slice = [].slice;
  2730. ColorButton = (function(_super) {
  2731. __extends(ColorButton, _super);
  2732. function ColorButton() {
  2733. return ColorButton.__super__.constructor.apply(this, arguments);
  2734. }
  2735. ColorButton.prototype.name = 'color';
  2736. ColorButton.prototype.icon = 'font';
  2737. ColorButton.prototype.disableTag = 'pre';
  2738. ColorButton.prototype.menu = true;
  2739. ColorButton.prototype.render = function() {
  2740. var args;
  2741. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  2742. return ColorButton.__super__.render.apply(this, args);
  2743. };
  2744. ColorButton.prototype.renderMenu = function() {
  2745. $('<ul class="color-list">\n <li><a href="javascript:;" class="font-color font-color-1" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-2" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-3" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-4" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-5" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-6" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-7" data-color=""></a></li>\n <li><a href="javascript:;" class="font-color font-color-default" data-color=""></a></li>\n</ul>').appendTo(this.menuWrapper);
  2746. this.menuWrapper.on('mousedown', '.color-list', function(e) {
  2747. return false;
  2748. });
  2749. return this.menuWrapper.on('click', '.font-color', (function(_this) {
  2750. return function(e) {
  2751. var $link, $p, hex, rgb;
  2752. _this.wrapper.removeClass('menu-on');
  2753. $link = $(e.currentTarget);
  2754. if ($link.hasClass('font-color-default')) {
  2755. $p = _this.editor.body.find('p, li');
  2756. if (!($p.length > 0)) {
  2757. return;
  2758. }
  2759. rgb = window.getComputedStyle($p[0], null).getPropertyValue('color');
  2760. hex = _this._convertRgbToHex(rgb);
  2761. } else {
  2762. rgb = window.getComputedStyle($link[0], null).getPropertyValue('background-color');
  2763. hex = _this._convertRgbToHex(rgb);
  2764. }
  2765. if (!hex) {
  2766. return;
  2767. }
  2768. document.execCommand('foreColor', false, hex);
  2769. return _this.editor.trigger('valuechanged');
  2770. };
  2771. })(this));
  2772. };
  2773. ColorButton.prototype._convertRgbToHex = function(rgb) {
  2774. var match, re, rgbToHex;
  2775. re = /rgb\((\d+),\s?(\d+),\s?(\d+)\)/g;
  2776. match = re.exec(rgb);
  2777. if (!match) {
  2778. return '';
  2779. }
  2780. rgbToHex = function(r, g, b) {
  2781. var componentToHex;
  2782. componentToHex = function(c) {
  2783. var hex;
  2784. hex = c.toString(16);
  2785. if (hex.length === 1) {
  2786. return '0' + hex;
  2787. } else {
  2788. return hex;
  2789. }
  2790. };
  2791. return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
  2792. };
  2793. return rgbToHex(match[1] * 1, match[2] * 1, match[3] * 1);
  2794. };
  2795. return ColorButton;
  2796. })(Button);
  2797. Simditor.Toolbar.addButton(ColorButton);
  2798. var ListButton, OrderListButton, UnorderListButton,
  2799. __hasProp = {}.hasOwnProperty,
  2800. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2801. ListButton = (function(_super) {
  2802. __extends(ListButton, _super);
  2803. function ListButton() {
  2804. return ListButton.__super__.constructor.apply(this, arguments);
  2805. }
  2806. ListButton.prototype.type = '';
  2807. ListButton.prototype.disableTag = 'pre, table';
  2808. ListButton.prototype.status = function($node) {
  2809. var anotherType;
  2810. if ($node != null) {
  2811. this.setDisabled($node.is(this.disableTag));
  2812. }
  2813. if (this.disabled) {
  2814. return true;
  2815. }
  2816. if ($node == null) {
  2817. return this.active;
  2818. }
  2819. anotherType = this.type === 'ul' ? 'ol' : 'ul';
  2820. if ($node.is(anotherType)) {
  2821. this.setActive(false);
  2822. return true;
  2823. } else {
  2824. this.setActive($node.is(this.htmlTag));
  2825. return this.active;
  2826. }
  2827. };
  2828. ListButton.prototype.command = function(param) {
  2829. var $contents, $endBlock, $furthestEnd, $furthestStart, $parent, $startBlock, endLevel, endNode, getListLevel, node, range, results, startLevel, startNode, _i, _len, _ref;
  2830. range = this.editor.selection.getRange();
  2831. startNode = range.startContainer;
  2832. endNode = range.endContainer;
  2833. $startBlock = this.editor.util.closestBlockEl(startNode);
  2834. $endBlock = this.editor.util.closestBlockEl(endNode);
  2835. this.editor.selection.save();
  2836. range.setStartBefore($startBlock[0]);
  2837. range.setEndAfter($endBlock[0]);
  2838. if ($startBlock.is('li') && $endBlock.is('li')) {
  2839. $furthestStart = this.editor.util.furthestNode($startBlock, 'ul, ol');
  2840. $furthestEnd = this.editor.util.furthestNode($endBlock, 'ul, ol');
  2841. if ($furthestStart.is($furthestEnd)) {
  2842. getListLevel = function($li) {
  2843. var lvl;
  2844. lvl = 1;
  2845. while (!$li.parent().is($furthestStart)) {
  2846. lvl += 1;
  2847. $li = $li.parent();
  2848. }
  2849. return lvl;
  2850. };
  2851. startLevel = getListLevel($startBlock);
  2852. endLevel = getListLevel($endBlock);
  2853. if (startLevel > endLevel) {
  2854. $parent = $endBlock.parent();
  2855. } else {
  2856. $parent = $startBlock.parent();
  2857. }
  2858. range.setStartBefore($parent[0]);
  2859. range.setEndAfter($parent[0]);
  2860. } else {
  2861. range.setStartBefore($furthestStart[0]);
  2862. range.setEndAfter($furthestEnd[0]);
  2863. }
  2864. }
  2865. $contents = $(range.extractContents());
  2866. results = [];
  2867. $contents.children().each((function(_this) {
  2868. return function(i, el) {
  2869. var c, converted, _i, _len, _results;
  2870. converted = _this._convertEl(el);
  2871. _results = [];
  2872. for (_i = 0, _len = converted.length; _i < _len; _i++) {
  2873. c = converted[_i];
  2874. if (results.length && results[results.length - 1].is(_this.type) && c.is(_this.type)) {
  2875. _results.push(results[results.length - 1].append(c.children()));
  2876. } else {
  2877. _results.push(results.push(c));
  2878. }
  2879. }
  2880. return _results;
  2881. };
  2882. })(this));
  2883. _ref = results.reverse();
  2884. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2885. node = _ref[_i];
  2886. range.insertNode(node[0]);
  2887. }
  2888. this.editor.selection.restore();
  2889. return this.editor.trigger('valuechanged');
  2890. };
  2891. ListButton.prototype._convertEl = function(el) {
  2892. var $el, anotherType, block, child, children, results, _i, _len, _ref;
  2893. $el = $(el);
  2894. results = [];
  2895. anotherType = this.type === 'ul' ? 'ol' : 'ul';
  2896. if ($el.is(this.type)) {
  2897. $el.children('li').each((function(_this) {
  2898. return function(i, li) {
  2899. var $childList, $li, block;
  2900. $li = $(li);
  2901. $childList = $li.children('ul, ol').remove();
  2902. block = $('<p/>').append($(li).html() || _this.editor.util.phBr);
  2903. results.push(block);
  2904. if ($childList.length > 0) {
  2905. return results.push($childList);
  2906. }
  2907. };
  2908. })(this));
  2909. } else if ($el.is(anotherType)) {
  2910. block = $('<' + this.type + '/>').append($el.html());
  2911. results.push(block);
  2912. } else if ($el.is('blockquote')) {
  2913. _ref = $el.children().get();
  2914. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  2915. child = _ref[_i];
  2916. children = this._convertEl(child);
  2917. }
  2918. $.merge(results, children);
  2919. } else if ($el.is('table')) {
  2920. } else {
  2921. block = $('<' + this.type + '><li></li></' + this.type + '>');
  2922. block.find('li').append($el.html() || this.editor.util.phBr);
  2923. results.push(block);
  2924. }
  2925. return results;
  2926. };
  2927. return ListButton;
  2928. })(Button);
  2929. OrderListButton = (function(_super) {
  2930. __extends(OrderListButton, _super);
  2931. function OrderListButton() {
  2932. return OrderListButton.__super__.constructor.apply(this, arguments);
  2933. }
  2934. OrderListButton.prototype.type = 'ol';
  2935. OrderListButton.prototype.name = 'ol';
  2936. OrderListButton.prototype.icon = 'list-ol';
  2937. OrderListButton.prototype.htmlTag = 'ol';
  2938. OrderListButton.prototype.shortcut = 'cmd+/';
  2939. OrderListButton.prototype._init = function() {
  2940. if (this.editor.util.os.mac) {
  2941. this.title = this.title + ' ( Cmd + / )';
  2942. } else {
  2943. this.title = this.title + ' ( ctrl + / )';
  2944. this.shortcut = 'ctrl+/';
  2945. }
  2946. return OrderListButton.__super__._init.call(this);
  2947. };
  2948. return OrderListButton;
  2949. })(ListButton);
  2950. UnorderListButton = (function(_super) {
  2951. __extends(UnorderListButton, _super);
  2952. function UnorderListButton() {
  2953. return UnorderListButton.__super__.constructor.apply(this, arguments);
  2954. }
  2955. UnorderListButton.prototype.type = 'ul';
  2956. UnorderListButton.prototype.name = 'ul';
  2957. UnorderListButton.prototype.icon = 'list-ul';
  2958. UnorderListButton.prototype.htmlTag = 'ul';
  2959. UnorderListButton.prototype.shortcut = 'cmd+.';
  2960. UnorderListButton.prototype._init = function() {
  2961. if (this.editor.util.os.mac) {
  2962. this.title = this.title + ' ( Cmd + . )';
  2963. } else {
  2964. this.title = this.title + ' ( Ctrl + . )';
  2965. this.shortcut = 'ctrl+.';
  2966. }
  2967. return UnorderListButton.__super__._init.call(this);
  2968. };
  2969. return UnorderListButton;
  2970. })(ListButton);
  2971. Simditor.Toolbar.addButton(OrderListButton);
  2972. Simditor.Toolbar.addButton(UnorderListButton);
  2973. var BlockquoteButton,
  2974. __hasProp = {}.hasOwnProperty,
  2975. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  2976. BlockquoteButton = (function(_super) {
  2977. __extends(BlockquoteButton, _super);
  2978. function BlockquoteButton() {
  2979. return BlockquoteButton.__super__.constructor.apply(this, arguments);
  2980. }
  2981. BlockquoteButton.prototype.name = 'blockquote';
  2982. BlockquoteButton.prototype.icon = 'quote-left';
  2983. BlockquoteButton.prototype.htmlTag = 'blockquote';
  2984. BlockquoteButton.prototype.disableTag = 'pre, table';
  2985. BlockquoteButton.prototype.command = function() {
  2986. var $contents, $endBlock, $startBlock, endNode, node, range, results, startNode, _i, _len, _ref;
  2987. range = this.editor.selection.getRange();
  2988. startNode = range.startContainer;
  2989. endNode = range.endContainer;
  2990. $startBlock = this.editor.util.furthestBlockEl(startNode);
  2991. $endBlock = this.editor.util.furthestBlockEl(endNode);
  2992. this.editor.selection.save();
  2993. range.setStartBefore($startBlock[0]);
  2994. range.setEndAfter($endBlock[0]);
  2995. $contents = $(range.extractContents());
  2996. results = [];
  2997. $contents.children().each((function(_this) {
  2998. return function(i, el) {
  2999. var c, converted, _i, _len, _results;
  3000. converted = _this._convertEl(el);
  3001. _results = [];
  3002. for (_i = 0, _len = converted.length; _i < _len; _i++) {
  3003. c = converted[_i];
  3004. if (results.length && results[results.length - 1].is(_this.htmlTag) && c.is(_this.htmlTag)) {
  3005. _results.push(results[results.length - 1].append(c.children()));
  3006. } else {
  3007. _results.push(results.push(c));
  3008. }
  3009. }
  3010. return _results;
  3011. };
  3012. })(this));
  3013. _ref = results.reverse();
  3014. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  3015. node = _ref[_i];
  3016. range.insertNode(node[0]);
  3017. }
  3018. this.editor.selection.restore();
  3019. return this.editor.trigger('valuechanged');
  3020. };
  3021. BlockquoteButton.prototype._convertEl = function(el) {
  3022. var $el, block, results;
  3023. $el = $(el);
  3024. results = [];
  3025. if ($el.is(this.htmlTag)) {
  3026. $el.children().each((function(_this) {
  3027. return function(i, node) {
  3028. return results.push($(node));
  3029. };
  3030. })(this));
  3031. } else {
  3032. block = $('<' + this.htmlTag + '/>').append($el);
  3033. results.push(block);
  3034. }
  3035. return results;
  3036. };
  3037. return BlockquoteButton;
  3038. })(Button);
  3039. Simditor.Toolbar.addButton(BlockquoteButton);
  3040. var CodeButton, CodePopover,
  3041. __hasProp = {}.hasOwnProperty,
  3042. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  3043. __slice = [].slice;
  3044. CodeButton = (function(_super) {
  3045. __extends(CodeButton, _super);
  3046. function CodeButton() {
  3047. return CodeButton.__super__.constructor.apply(this, arguments);
  3048. }
  3049. CodeButton.prototype.name = 'code';
  3050. CodeButton.prototype.icon = 'code';
  3051. CodeButton.prototype.htmlTag = 'pre';
  3052. CodeButton.prototype.disableTag = 'li, table';
  3053. CodeButton.prototype._init = function() {
  3054. CodeButton.__super__._init.call(this);
  3055. this.editor.on('decorate', (function(_this) {
  3056. return function(e, $el) {
  3057. return $el.find('pre').each(function(i, pre) {
  3058. return _this.decorate($(pre));
  3059. });
  3060. };
  3061. })(this));
  3062. return this.editor.on('undecorate', (function(_this) {
  3063. return function(e, $el) {
  3064. return $el.find('pre').each(function(i, pre) {
  3065. return _this.undecorate($(pre));
  3066. });
  3067. };
  3068. })(this));
  3069. };
  3070. CodeButton.prototype.render = function() {
  3071. var args;
  3072. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3073. CodeButton.__super__.render.apply(this, args);
  3074. return this.popover = new CodePopover({
  3075. button: this
  3076. });
  3077. };
  3078. CodeButton.prototype.status = function($node) {
  3079. var result;
  3080. result = CodeButton.__super__.status.call(this, $node);
  3081. if (this.active) {
  3082. this.popover.show($node);
  3083. } else if (this.editor.util.isBlockNode($node)) {
  3084. this.popover.hide();
  3085. }
  3086. return result;
  3087. };
  3088. CodeButton.prototype.decorate = function($pre) {
  3089. var lang;
  3090. lang = $pre.attr('data-lang');
  3091. $pre.removeClass();
  3092. if (lang && lang !== -1) {
  3093. return $pre.addClass('lang-' + lang);
  3094. }
  3095. };
  3096. CodeButton.prototype.undecorate = function($pre) {
  3097. var lang;
  3098. lang = $pre.attr('data-lang');
  3099. $pre.removeClass();
  3100. if (lang && lang !== -1) {
  3101. return $pre.addClass('lang-' + lang);
  3102. }
  3103. };
  3104. CodeButton.prototype.command = function() {
  3105. var $contents, $endBlock, $startBlock, endNode, node, range, results, startNode, _i, _len, _ref;
  3106. range = this.editor.selection.getRange();
  3107. startNode = range.startContainer;
  3108. endNode = range.endContainer;
  3109. $startBlock = this.editor.util.closestBlockEl(startNode);
  3110. $endBlock = this.editor.util.closestBlockEl(endNode);
  3111. range.setStartBefore($startBlock[0]);
  3112. range.setEndAfter($endBlock[0]);
  3113. $contents = $(range.extractContents());
  3114. results = [];
  3115. $contents.children().each((function(_this) {
  3116. return function(i, el) {
  3117. var c, converted, _i, _len, _results;
  3118. converted = _this._convertEl(el);
  3119. _results = [];
  3120. for (_i = 0, _len = converted.length; _i < _len; _i++) {
  3121. c = converted[_i];
  3122. if (results.length && results[results.length - 1].is(_this.htmlTag) && c.is(_this.htmlTag)) {
  3123. _results.push(results[results.length - 1].append(c.contents()));
  3124. } else {
  3125. _results.push(results.push(c));
  3126. }
  3127. }
  3128. return _results;
  3129. };
  3130. })(this));
  3131. _ref = results.reverse();
  3132. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  3133. node = _ref[_i];
  3134. range.insertNode(node[0]);
  3135. }
  3136. this.editor.selection.setRangeAtEndOf(results[0]);
  3137. return this.editor.trigger('valuechanged');
  3138. };
  3139. CodeButton.prototype._convertEl = function(el) {
  3140. var $el, block, codeStr, results;
  3141. $el = $(el);
  3142. results = [];
  3143. if ($el.is(this.htmlTag)) {
  3144. block = $('<p/>').append($el.html().replace('\n', '<br/>'));
  3145. results.push(block);
  3146. } else {
  3147. if (!$el.text() && $el.children().length === 1 && $el.children().is('br')) {
  3148. codeStr = '\n';
  3149. } else {
  3150. codeStr = this.editor.formatter.clearHtml($el);
  3151. }
  3152. block = $('<' + this.htmlTag + '/>').text(codeStr);
  3153. results.push(block);
  3154. }
  3155. return results;
  3156. };
  3157. return CodeButton;
  3158. })(Button);
  3159. CodePopover = (function(_super) {
  3160. __extends(CodePopover, _super);
  3161. function CodePopover() {
  3162. return CodePopover.__super__.constructor.apply(this, arguments);
  3163. }
  3164. CodePopover.prototype._tpl = "<div class=\"code-settings\">\n <div class=\"settings-field\">\n <select class=\"select-lang\">\n <option value=\"-1\">选择程序语言</option>\n <option value=\"bash\">Bash</option>\n <option value=\"c++\">C++</option>\n <option value=\"cs\">C#</option>\n <option value=\"css\">CSS</option>\n <option value=\"erlang\">Erlang</option>\n <option value=\"less\">Less</option>\n <option value=\"scss\">Sass</option>\n <option value=\"diff\">Diff</option>\n <option value=\"coffeeScript\">CoffeeScript</option>\n <option value=\"html\">Html,XML</option>\n <option value=\"json\">JSON</option>\n <option value=\"java\">Java</option>\n <option value=\"js\">JavaScript</option>\n <option value=\"markdown\">Markdown</option>\n <option value=\"oc\">Objective C</option>\n <option value=\"php\">PHP</option>\n <option value=\"perl\">Perl</option>\n <option value=\"python\">Python</option>\n <option value=\"ruby\">Ruby</option>\n <option value=\"sql\">SQL</option>\n </select>\n </div>\n</div>";
  3165. CodePopover.prototype.render = function() {
  3166. this.el.addClass('code-popover').append(this._tpl);
  3167. this.selectEl = this.el.find('.select-lang');
  3168. return this.selectEl.on('change', (function(_this) {
  3169. return function(e) {
  3170. var selected;
  3171. _this.lang = _this.selectEl.val();
  3172. selected = _this.target.hasClass('selected');
  3173. _this.target.removeClass().removeAttr('data-lang');
  3174. if (_this.lang !== -1) {
  3175. _this.target.addClass('lang-' + _this.lang);
  3176. _this.target.attr('data-lang', _this.lang);
  3177. }
  3178. if (selected) {
  3179. return _this.target.addClass('selected');
  3180. }
  3181. };
  3182. })(this));
  3183. };
  3184. CodePopover.prototype.show = function() {
  3185. var args;
  3186. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3187. CodePopover.__super__.show.apply(this, args);
  3188. this.lang = this.target.attr('data-lang');
  3189. if (this.lang != null) {
  3190. return this.selectEl.val(this.lang);
  3191. } else {
  3192. return this.selectEl.val(-1);
  3193. }
  3194. };
  3195. return CodePopover;
  3196. })(Popover);
  3197. Simditor.Toolbar.addButton(CodeButton);
  3198. var LinkButton, LinkPopover,
  3199. __hasProp = {}.hasOwnProperty,
  3200. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  3201. __slice = [].slice;
  3202. LinkButton = (function(_super) {
  3203. __extends(LinkButton, _super);
  3204. function LinkButton() {
  3205. return LinkButton.__super__.constructor.apply(this, arguments);
  3206. }
  3207. LinkButton.prototype.name = 'link';
  3208. LinkButton.prototype.icon = 'link';
  3209. LinkButton.prototype.htmlTag = 'a';
  3210. LinkButton.prototype.disableTag = 'pre';
  3211. LinkButton.prototype.render = function() {
  3212. var args;
  3213. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3214. LinkButton.__super__.render.apply(this, args);
  3215. return this.popover = new LinkPopover({
  3216. button: this
  3217. });
  3218. };
  3219. LinkButton.prototype.status = function($node) {
  3220. var showPopover;
  3221. if ($node != null) {
  3222. this.setDisabled($node.is(this.disableTag));
  3223. }
  3224. if (this.disabled) {
  3225. return true;
  3226. }
  3227. if ($node == null) {
  3228. return this.active;
  3229. }
  3230. showPopover = true;
  3231. if (!$node.is(this.htmlTag) || $node.is('[class^="simditor-"]')) {
  3232. this.setActive(false);
  3233. showPopover = false;
  3234. } else if (this.editor.selection.rangeAtEndOf($node)) {
  3235. this.setActive(true);
  3236. showPopover = false;
  3237. } else {
  3238. this.setActive(true);
  3239. }
  3240. if (showPopover) {
  3241. this.popover.show($node);
  3242. } else if (this.editor.util.isBlockNode($node)) {
  3243. this.popover.hide();
  3244. }
  3245. return this.active;
  3246. };
  3247. LinkButton.prototype.command = function() {
  3248. var $contents, $endBlock, $link, $newBlock, $startBlock, endNode, linkText, range, startNode, txtNode;
  3249. range = this.editor.selection.getRange();
  3250. if (this.active) {
  3251. $link = $(range.commonAncestorContainer).closest('a');
  3252. txtNode = document.createTextNode($link.text());
  3253. $link.replaceWith(txtNode);
  3254. range.selectNode(txtNode);
  3255. } else {
  3256. startNode = range.startContainer;
  3257. endNode = range.endContainer;
  3258. $startBlock = this.editor.util.closestBlockEl(startNode);
  3259. $endBlock = this.editor.util.closestBlockEl(endNode);
  3260. $contents = $(range.extractContents());
  3261. linkText = this.editor.formatter.clearHtml($contents.contents(), false);
  3262. $link = $('<a/>', {
  3263. href: 'http://www.example.com',
  3264. target: '_blank',
  3265. text: linkText || this._t('linkText')
  3266. });
  3267. if ($startBlock[0] === $endBlock[0]) {
  3268. range.insertNode($link[0]);
  3269. } else {
  3270. $newBlock = $('<p/>').append($link);
  3271. range.insertNode($newBlock[0]);
  3272. }
  3273. range.selectNodeContents($link[0]);
  3274. this.popover.one('popovershow', (function(_this) {
  3275. return function() {
  3276. if (linkText) {
  3277. _this.popover.urlEl.focus();
  3278. return _this.popover.urlEl[0].select();
  3279. } else {
  3280. _this.popover.textEl.focus();
  3281. return _this.popover.textEl[0].select();
  3282. }
  3283. };
  3284. })(this));
  3285. }
  3286. this.editor.selection.selectRange(range);
  3287. return this.editor.trigger('valuechanged');
  3288. };
  3289. return LinkButton;
  3290. })(Button);
  3291. LinkPopover = (function(_super) {
  3292. __extends(LinkPopover, _super);
  3293. function LinkPopover() {
  3294. return LinkPopover.__super__.constructor.apply(this, arguments);
  3295. }
  3296. LinkPopover.prototype.render = function() {
  3297. var tpl;
  3298. tpl = "<div class=\"link-settings\">\n <div class=\"settings-field\">\n <label>" + (this._t('text')) + "</label>\n <input class=\"link-text\" type=\"text\"/>\n <a class=\"btn-unlink\" href=\"javascript:;\" title=\"" + (this._t('removeLink')) + "\" tabindex=\"-1\"><span class=\"fa fa-unlink\"></span></a>\n </div>\n <div class=\"settings-field\">\n <label>" + (this._t('linkUrl')) + "</label>\n <input class=\"link-url\" type=\"text\"/>\n </div>\n</div>";
  3299. this.el.addClass('link-popover').append(tpl);
  3300. this.textEl = this.el.find('.link-text');
  3301. this.urlEl = this.el.find('.link-url');
  3302. this.unlinkEl = this.el.find('.btn-unlink');
  3303. this.textEl.on('keyup', (function(_this) {
  3304. return function(e) {
  3305. if (e.which === 13) {
  3306. return;
  3307. }
  3308. return _this.target.text(_this.textEl.val());
  3309. };
  3310. })(this));
  3311. this.urlEl.on('keyup', (function(_this) {
  3312. return function(e) {
  3313. var val;
  3314. if (e.which === 13) {
  3315. return;
  3316. }
  3317. val = _this.urlEl.val();
  3318. if (!(/https?:\/\/|^\//ig.test(val) || !val)) {
  3319. val = 'http://' + val;
  3320. }
  3321. return _this.target.attr('href', val);
  3322. };
  3323. })(this));
  3324. $([this.urlEl[0], this.textEl[0]]).on('keydown', (function(_this) {
  3325. return function(e) {
  3326. if (e.which === 13 || e.which === 27 || (!e.shiftKey && e.which === 9 && $(e.target).hasClass('link-url'))) {
  3327. e.preventDefault();
  3328. return setTimeout(function() {
  3329. var range;
  3330. range = document.createRange();
  3331. _this.editor.selection.setRangeAfter(_this.target, range);
  3332. _this.hide();
  3333. return _this.editor.trigger('valuechanged');
  3334. }, 0);
  3335. }
  3336. };
  3337. })(this));
  3338. return this.unlinkEl.on('click', (function(_this) {
  3339. return function(e) {
  3340. var range, txtNode;
  3341. txtNode = document.createTextNode(_this.target.text());
  3342. _this.target.replaceWith(txtNode);
  3343. _this.hide();
  3344. range = document.createRange();
  3345. _this.editor.selection.setRangeAfter(txtNode, range);
  3346. return _this.editor.trigger('valuechanged');
  3347. };
  3348. })(this));
  3349. };
  3350. LinkPopover.prototype.show = function() {
  3351. var args;
  3352. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3353. LinkPopover.__super__.show.apply(this, args);
  3354. this.textEl.val(this.target.text());
  3355. return this.urlEl.val(this.target.attr('href'));
  3356. };
  3357. return LinkPopover;
  3358. })(Popover);
  3359. Simditor.Toolbar.addButton(LinkButton);
  3360. var ImageButton, ImagePopover,
  3361. __hasProp = {}.hasOwnProperty,
  3362. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  3363. __slice = [].slice;
  3364. ImageButton = (function(_super) {
  3365. __extends(ImageButton, _super);
  3366. function ImageButton() {
  3367. return ImageButton.__super__.constructor.apply(this, arguments);
  3368. }
  3369. ImageButton.prototype.name = 'image';
  3370. ImageButton.prototype.icon = 'picture-o';
  3371. ImageButton.prototype.htmlTag = 'img';
  3372. ImageButton.prototype.disableTag = 'pre, table';
  3373. ImageButton.prototype.defaultImage = '';
  3374. ImageButton.prototype.needFocus = false;
  3375. ImageButton.prototype._init = function() {
  3376. if (this.editor.uploader != null) {
  3377. this.menu = [
  3378. {
  3379. name: 'upload-image',
  3380. text: this._t('localImage')
  3381. }, {
  3382. name: 'external-image',
  3383. text: this._t('externalImage')
  3384. }
  3385. ];
  3386. } else {
  3387. this.menu = false;
  3388. }
  3389. this.defaultImage = this.editor.opts.defaultImage;
  3390. this.editor.body.on('click', 'img:not([data-non-image])', (function(_this) {
  3391. return function(e) {
  3392. var $img, range;
  3393. $img = $(e.currentTarget);
  3394. range = document.createRange();
  3395. range.selectNode($img[0]);
  3396. _this.editor.selection.selectRange(range);
  3397. if (!_this.editor.util.supportSelectionChange) {
  3398. _this.editor.trigger('selectionchanged');
  3399. }
  3400. return false;
  3401. };
  3402. })(this));
  3403. this.editor.body.on('mouseup', 'img:not([data-non-image])', (function(_this) {
  3404. return function(e) {
  3405. return false;
  3406. };
  3407. })(this));
  3408. this.editor.on('selectionchanged.image', (function(_this) {
  3409. return function() {
  3410. var $contents, $img, range;
  3411. range = _this.editor.selection.getRange();
  3412. if (range == null) {
  3413. return;
  3414. }
  3415. $contents = $(range.cloneContents()).contents();
  3416. if ($contents.length === 1 && $contents.is('img:not([data-non-image])')) {
  3417. $img = $(range.startContainer).contents().eq(range.startOffset);
  3418. return _this.popover.show($img);
  3419. } else {
  3420. return _this.popover.hide();
  3421. }
  3422. };
  3423. })(this));
  3424. this.editor.on('valuechanged.image', (function(_this) {
  3425. return function() {
  3426. var $masks;
  3427. $masks = _this.editor.wrapper.find('.simditor-image-loading');
  3428. if (!($masks.length > 0)) {
  3429. return;
  3430. }
  3431. return $masks.each(function(i, mask) {
  3432. var $img, $mask, file;
  3433. $mask = $(mask);
  3434. $img = $mask.data('img');
  3435. if (!($img && $img.parent().length > 0)) {
  3436. $mask.remove();
  3437. if ($img) {
  3438. file = $img.data('file');
  3439. if (file) {
  3440. _this.editor.uploader.cancel(file);
  3441. if (_this.editor.body.find('img.uploading').length < 1) {
  3442. return _this.editor.uploader.trigger('uploadready', [file]);
  3443. }
  3444. }
  3445. }
  3446. }
  3447. });
  3448. };
  3449. })(this));
  3450. return ImageButton.__super__._init.call(this);
  3451. };
  3452. ImageButton.prototype.render = function() {
  3453. var args;
  3454. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3455. ImageButton.__super__.render.apply(this, args);
  3456. return this.popover = new ImagePopover({
  3457. button: this
  3458. });
  3459. };
  3460. ImageButton.prototype.renderMenu = function() {
  3461. var $input, $uploadItem, createInput;
  3462. ImageButton.__super__.renderMenu.call(this);
  3463. $uploadItem = this.menuEl.find('.menu-item-upload-image');
  3464. $input = null;
  3465. createInput = (function(_this) {
  3466. return function() {
  3467. if ($input) {
  3468. $input.remove();
  3469. }
  3470. return $input = $('<input type="file" title="' + _this._t('uploadImage') + '" accept="image/*">').appendTo($uploadItem);
  3471. };
  3472. })(this);
  3473. createInput();
  3474. $uploadItem.on('click mousedown', 'input[type=file]', (function(_this) {
  3475. return function(e) {
  3476. return e.stopPropagation();
  3477. };
  3478. })(this));
  3479. $uploadItem.on('change', 'input[type=file]', (function(_this) {
  3480. return function(e) {
  3481. if (_this.editor.inputManager.focused) {
  3482. _this.editor.uploader.upload($input, {
  3483. inline: true
  3484. });
  3485. createInput();
  3486. } else {
  3487. _this.editor.one('focus', function(e) {
  3488. _this.editor.uploader.upload($input, {
  3489. inline: true
  3490. });
  3491. return createInput();
  3492. });
  3493. _this.editor.focus();
  3494. }
  3495. return _this.wrapper.removeClass('menu-on');
  3496. };
  3497. })(this));
  3498. return this._initUploader();
  3499. };
  3500. ImageButton.prototype._initUploader = function() {
  3501. if (this.editor.uploader == null) {
  3502. this.el.find('.btn-upload').remove();
  3503. return;
  3504. }
  3505. this.editor.uploader.on('beforeupload', (function(_this) {
  3506. return function(e, file) {
  3507. var $img;
  3508. if (!file.inline) {
  3509. return;
  3510. }
  3511. if (file.img) {
  3512. $img = $(file.img);
  3513. } else {
  3514. $img = _this.createImage(file.name);
  3515. file.img = $img;
  3516. }
  3517. $img.addClass('uploading');
  3518. $img.data('file', file);
  3519. return _this.editor.uploader.readImageFile(file.obj, function(img) {
  3520. var src;
  3521. if (!$img.hasClass('uploading')) {
  3522. return;
  3523. }
  3524. src = img ? img.src : _this.defaultImage;
  3525. return _this.loadImage($img, src, function() {
  3526. if (_this.popover.active) {
  3527. _this.popover.refresh();
  3528. return _this.popover.srcEl.val(_this._t('uploading')).prop('disabled', true);
  3529. }
  3530. });
  3531. });
  3532. };
  3533. })(this));
  3534. this.editor.uploader.on('uploadprogress', (function(_this) {
  3535. return function(e, file, loaded, total) {
  3536. var $img, $mask, $txt, percent;
  3537. if (!file.inline) {
  3538. return;
  3539. }
  3540. percent = loaded / total;
  3541. percent = (percent * 100).toFixed(0);
  3542. if (percent > 99) {
  3543. percent = 99;
  3544. }
  3545. $mask = file.img.data('mask');
  3546. if ($mask) {
  3547. $img = $mask.data('img');
  3548. $txt = $mask.find('span');
  3549. if ($img && $img.parent().length > 0 && percent !== $txt.text()) {
  3550. return $txt.text(percent);
  3551. } else {
  3552. return $mask.remove();
  3553. }
  3554. }
  3555. };
  3556. })(this));
  3557. this.editor.uploader.on('uploadsuccess', (function(_this) {
  3558. return function(e, file, result) {
  3559. var $img, $mask, msg;
  3560. if (!file.inline) {
  3561. return;
  3562. }
  3563. $img = file.img;
  3564. $img.removeData('file');
  3565. $img.removeClass('uploading');
  3566. $mask = $img.data('mask');
  3567. if ($mask) {
  3568. $mask.remove();
  3569. }
  3570. $img.removeData('mask');
  3571. if (result.success === false) {
  3572. msg = result.msg || _this._t('uploadFailed');
  3573. alert(msg);
  3574. $img.attr('src', _this.defaultImage);
  3575. } else {
  3576. $img.attr('src', result.file_path);
  3577. }
  3578. if (_this.popover.active) {
  3579. _this.popover.srcEl.prop('disabled', false);
  3580. _this.popover.srcEl.val(result.file_path);
  3581. }
  3582. _this.editor.trigger('valuechanged');
  3583. if (_this.editor.body.find('img.uploading').length < 1) {
  3584. return _this.editor.uploader.trigger('uploadready', [file, result]);
  3585. }
  3586. };
  3587. })(this));
  3588. return this.editor.uploader.on('uploaderror', (function(_this) {
  3589. return function(e, file, xhr) {
  3590. var $img, $mask, msg, result;
  3591. if (!file.inline) {
  3592. return;
  3593. }
  3594. if (xhr.statusText === 'abort') {
  3595. return;
  3596. }
  3597. if (xhr.responseText) {
  3598. try {
  3599. result = $.parseJSON(xhr.responseText);
  3600. msg = result.msg;
  3601. } catch (_error) {
  3602. e = _error;
  3603. msg = _this._t('uploadError');
  3604. }
  3605. alert(msg);
  3606. }
  3607. $img = file.img;
  3608. $img.removeData('file');
  3609. $img.removeClass('uploading');
  3610. $mask = $img.data('mask');
  3611. if ($mask) {
  3612. $mask.remove();
  3613. }
  3614. $img.removeData('mask');
  3615. $img.attr('src', _this.defaultImage);
  3616. if (_this.popover.active) {
  3617. _this.popover.srcEl.prop('disabled', false);
  3618. _this.popover.srcEl.val(_this.defaultImage);
  3619. }
  3620. _this.editor.trigger('valuechanged');
  3621. if (_this.editor.body.find('img.uploading').length < 1) {
  3622. return _this.editor.uploader.trigger('uploadready', [file, result]);
  3623. }
  3624. };
  3625. })(this));
  3626. };
  3627. ImageButton.prototype.status = function($node) {
  3628. if ($node != null) {
  3629. this.setDisabled($node.is(this.disableTag));
  3630. }
  3631. if (this.disabled) {
  3632. return true;
  3633. }
  3634. };
  3635. ImageButton.prototype.loadImage = function($img, src, callback) {
  3636. var $mask, img;
  3637. $mask = $img.data('mask');
  3638. if (!$mask) {
  3639. $mask = $('<div class="simditor-image-loading"><span></span></div>').hide().appendTo(this.editor.wrapper);
  3640. if ($img.hasClass('uploading')) {
  3641. $mask.addClass('uploading');
  3642. }
  3643. $img.data('mask', $mask);
  3644. $mask.data('img', $img);
  3645. }
  3646. img = new Image();
  3647. img.onload = (function(_this) {
  3648. return function() {
  3649. var height, imgOffset, width, wrapperOffset;
  3650. if ($mask.hasClass('uploading') && !$img.hasClass('uploading')) {
  3651. return;
  3652. }
  3653. width = img.width;
  3654. height = img.height;
  3655. $img.attr({
  3656. src: src,
  3657. 'data-image-size': width + ',' + height
  3658. });
  3659. if ($img.hasClass('uploading')) {
  3660. _this.editor.util.reflow(_this.editor.body);
  3661. wrapperOffset = _this.editor.wrapper.offset();
  3662. imgOffset = $img.offset();
  3663. $mask.css({
  3664. top: imgOffset.top - wrapperOffset.top,
  3665. left: imgOffset.left - wrapperOffset.left,
  3666. width: $img.width(),
  3667. height: $img.height()
  3668. }).show();
  3669. } else {
  3670. $mask.remove();
  3671. $img.removeData('mask');
  3672. }
  3673. return callback(img);
  3674. };
  3675. })(this);
  3676. img.onerror = (function(_this) {
  3677. return function() {
  3678. callback(false);
  3679. $mask.remove();
  3680. return $img.removeData('mask');
  3681. };
  3682. })(this);
  3683. return img.src = src;
  3684. };
  3685. ImageButton.prototype.createImage = function(name) {
  3686. var $block, $img, $nextBlock, range;
  3687. if (name == null) {
  3688. name = 'Image';
  3689. }
  3690. if (!this.editor.inputManager.focused) {
  3691. this.editor.focus();
  3692. }
  3693. range = this.editor.selection.getRange();
  3694. range.deleteContents();
  3695. $block = this.editor.util.closestBlockEl();
  3696. if ($block.is('p') && !this.editor.util.isEmptyNode($block)) {
  3697. $block = $('<p/>').append(this.editor.util.phBr).insertAfter($block);
  3698. this.editor.selection.setRangeAtStartOf($block, range);
  3699. }
  3700. $img = $('<img/>').attr('alt', name);
  3701. range.insertNode($img[0]);
  3702. $nextBlock = $block.next('p');
  3703. if (!($nextBlock.length > 0)) {
  3704. $nextBlock = $('<p/>').append(this.editor.util.phBr).insertAfter($block);
  3705. }
  3706. this.editor.selection.setRangeAtStartOf($nextBlock);
  3707. return $img;
  3708. };
  3709. ImageButton.prototype.command = function(src) {
  3710. var $img;
  3711. $img = this.createImage();
  3712. return this.loadImage($img, src || this.defaultImage, (function(_this) {
  3713. return function() {
  3714. _this.editor.trigger('valuechanged');
  3715. _this.editor.util.reflow($img);
  3716. $img.click();
  3717. return _this.popover.one('popovershow', function() {
  3718. _this.popover.srcEl.focus();
  3719. return _this.popover.srcEl[0].select();
  3720. });
  3721. };
  3722. })(this));
  3723. };
  3724. return ImageButton;
  3725. })(Button);
  3726. ImagePopover = (function(_super) {
  3727. __extends(ImagePopover, _super);
  3728. function ImagePopover() {
  3729. return ImagePopover.__super__.constructor.apply(this, arguments);
  3730. }
  3731. ImagePopover.prototype.offset = {
  3732. top: 6,
  3733. left: -4
  3734. };
  3735. ImagePopover.prototype.render = function() {
  3736. var tpl;
  3737. tpl = "<div class=\"link-settings\">\n <div class=\"settings-field\">\n <label>" + (this._t('imageUrl')) + "</label>\n <input class=\"image-src\" type=\"text\" tabindex=\"1\" />\n <a class=\"btn-upload\" href=\"javascript:;\" title=\"" + (this._t('uploadImage')) + "\" tabindex=\"-1\">\n <span class=\"fa fa-upload\"></span>\n </a>\n </div>\n <div class=\"settings-field\">\n <label>" + (this._t('imageSize')) + "</label>\n <input class=\"image-size\" id=\"image-width\" type=\"text\" tabindex=\"2\" />\n <span class=\"times\">×</span>\n <input class=\"image-size\" id=\"image-height\" type=\"text\" tabindex=\"3\" />\n <a class=\"btn-restore\" href=\"javascript:;\" title=\"" + (this._t('restoreImageSize')) + "\" tabindex=\"-1\">\n <span class=\"fa fa-reply\"></span>\n </a>\n </div>\n</div>";
  3738. this.el.addClass('image-popover').append(tpl);
  3739. this.srcEl = this.el.find('.image-src');
  3740. this.srcEl.on('keydown', (function(_this) {
  3741. return function(e) {
  3742. var hideAndFocus, src;
  3743. if (!(e.which === 13 || e.which === 27)) {
  3744. return;
  3745. }
  3746. e.preventDefault();
  3747. hideAndFocus = function() {
  3748. _this.button.editor.body.focus();
  3749. _this.button.editor.selection.setRangeAfter(_this.target);
  3750. return _this.hide();
  3751. };
  3752. if (e.which === 13 && !_this.target.hasClass('uploading')) {
  3753. src = _this.srcEl.val();
  3754. if (/^data:image/.test(src) && !_this.editor.uploader) {
  3755. hideAndFocus();
  3756. return;
  3757. }
  3758. return _this.button.loadImage(_this.target, src, function(success) {
  3759. var blob;
  3760. if (!success) {
  3761. return;
  3762. }
  3763. if (/^data:image/.test(src)) {
  3764. blob = _this.editor.util.dataURLtoBlob(src);
  3765. blob.name = "Base64 Image.png";
  3766. return _this.editor.uploader.upload(blob, {
  3767. inline: true,
  3768. img: _this.target
  3769. });
  3770. } else {
  3771. hideAndFocus();
  3772. return _this.editor.trigger('valuechanged');
  3773. }
  3774. });
  3775. } else {
  3776. return hideAndFocus();
  3777. }
  3778. };
  3779. })(this));
  3780. this.widthEl = this.el.find('#image-width');
  3781. this.heightEl = this.el.find('#image-height');
  3782. this.el.find('.image-size').on('blur', (function(_this) {
  3783. return function(e) {
  3784. _this._resizeImg($(e.currentTarget));
  3785. return _this.el.data('popover').refresh();
  3786. };
  3787. })(this));
  3788. this.el.find('.image-size').on('keyup', (function(_this) {
  3789. return function(e) {
  3790. var inputEl;
  3791. inputEl = $(e.currentTarget);
  3792. if (!(e.which === 13 || e.which === 27 || e.which === 9)) {
  3793. return _this._resizeImg(inputEl, true);
  3794. }
  3795. };
  3796. })(this));
  3797. this.el.find('.image-size').on('keydown', (function(_this) {
  3798. return function(e) {
  3799. var inputEl;
  3800. inputEl = $(e.currentTarget);
  3801. if (e.which === 13 || e.which === 27) {
  3802. e.preventDefault();
  3803. if (e.which === 13) {
  3804. _this._resizeImg(inputEl);
  3805. } else {
  3806. _this._restoreImg();
  3807. }
  3808. _this.button.editor.body.focus();
  3809. _this.button.editor.selection.setRangeAfter(_this.target);
  3810. return _this.hide();
  3811. } else if (e.which === 9) {
  3812. return _this.el.data('popover').refresh();
  3813. }
  3814. };
  3815. })(this));
  3816. this.el.find('.btn-restore').on('click', (function(_this) {
  3817. return function(e) {
  3818. _this._restoreImg();
  3819. return _this.el.data('popover').refresh();
  3820. };
  3821. })(this));
  3822. this.editor.on('valuechanged', (function(_this) {
  3823. return function(e) {
  3824. if (_this.active) {
  3825. return _this.refresh();
  3826. }
  3827. };
  3828. })(this));
  3829. return this._initUploader();
  3830. };
  3831. ImagePopover.prototype._initUploader = function() {
  3832. var $uploadBtn, createInput;
  3833. $uploadBtn = this.el.find('.btn-upload');
  3834. if (this.editor.uploader == null) {
  3835. $uploadBtn.remove();
  3836. return;
  3837. }
  3838. createInput = (function(_this) {
  3839. return function() {
  3840. if (_this.input) {
  3841. _this.input.remove();
  3842. }
  3843. return _this.input = $('<input type="file" title="' + _this._t('uploadImage') + '" accept="image/*">').appendTo($uploadBtn);
  3844. };
  3845. })(this);
  3846. createInput();
  3847. this.el.on('click mousedown', 'input[type=file]', (function(_this) {
  3848. return function(e) {
  3849. return e.stopPropagation();
  3850. };
  3851. })(this));
  3852. return this.el.on('change', 'input[type=file]', (function(_this) {
  3853. return function(e) {
  3854. _this.editor.uploader.upload(_this.input, {
  3855. inline: true,
  3856. img: _this.target
  3857. });
  3858. return createInput();
  3859. };
  3860. })(this));
  3861. };
  3862. ImagePopover.prototype._resizeImg = function(inputEl, onlySetVal) {
  3863. var height, value, width;
  3864. if (onlySetVal == null) {
  3865. onlySetVal = false;
  3866. }
  3867. value = inputEl.val() * 1;
  3868. if (!($.isNumeric(value) || value < 0)) {
  3869. return;
  3870. }
  3871. if (inputEl.is(this.widthEl)) {
  3872. height = this.height * value / this.width;
  3873. this.heightEl.val(height);
  3874. } else {
  3875. width = this.width * value / this.height;
  3876. this.widthEl.val(width);
  3877. }
  3878. if (!onlySetVal) {
  3879. return this.target.attr({
  3880. width: width || value,
  3881. height: height || value
  3882. });
  3883. }
  3884. };
  3885. ImagePopover.prototype._restoreImg = function() {
  3886. var size, _ref;
  3887. size = ((_ref = this.target.data('image-size')) != null ? _ref.split(",") : void 0) || [this.width, this.height];
  3888. this.target.attr({
  3889. width: size[0] * 1,
  3890. height: size[1] * 1
  3891. });
  3892. this.widthEl.val(size[0]);
  3893. return this.heightEl.val(size[1]);
  3894. };
  3895. ImagePopover.prototype.show = function() {
  3896. var $img, args;
  3897. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  3898. ImagePopover.__super__.show.apply(this, args);
  3899. $img = this.target;
  3900. this.width = $img.width();
  3901. this.height = $img.height();
  3902. if ($img.hasClass('uploading')) {
  3903. return this.srcEl.val(this._t('uploading')).prop('disabled', true);
  3904. } else {
  3905. this.srcEl.val($img.attr('src')).prop('disabled', false);
  3906. this.widthEl.val(this.width);
  3907. return this.heightEl.val(this.height);
  3908. }
  3909. };
  3910. return ImagePopover;
  3911. })(Popover);
  3912. Simditor.Toolbar.addButton(ImageButton);
  3913. var IndentButton,
  3914. __hasProp = {}.hasOwnProperty,
  3915. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3916. IndentButton = (function(_super) {
  3917. __extends(IndentButton, _super);
  3918. function IndentButton() {
  3919. return IndentButton.__super__.constructor.apply(this, arguments);
  3920. }
  3921. IndentButton.prototype.name = 'indent';
  3922. IndentButton.prototype.icon = 'indent';
  3923. IndentButton.prototype._init = function() {
  3924. this.title = this._t(this.name) + ' (Tab)';
  3925. return IndentButton.__super__._init.call(this);
  3926. };
  3927. IndentButton.prototype.status = function($node) {
  3928. return true;
  3929. };
  3930. IndentButton.prototype.command = function() {
  3931. return this.editor.util.indent();
  3932. };
  3933. return IndentButton;
  3934. })(Button);
  3935. Simditor.Toolbar.addButton(IndentButton);
  3936. var OutdentButton,
  3937. __hasProp = {}.hasOwnProperty,
  3938. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3939. OutdentButton = (function(_super) {
  3940. __extends(OutdentButton, _super);
  3941. function OutdentButton() {
  3942. return OutdentButton.__super__.constructor.apply(this, arguments);
  3943. }
  3944. OutdentButton.prototype.name = 'outdent';
  3945. OutdentButton.prototype.icon = 'outdent';
  3946. OutdentButton.prototype._init = function() {
  3947. this.title = this._t(this.name) + ' (Shift + Tab)';
  3948. return OutdentButton.__super__._init.call(this);
  3949. };
  3950. OutdentButton.prototype.status = function($node) {
  3951. return true;
  3952. };
  3953. OutdentButton.prototype.command = function() {
  3954. return this.editor.util.outdent();
  3955. };
  3956. return OutdentButton;
  3957. })(Button);
  3958. Simditor.Toolbar.addButton(OutdentButton);
  3959. var HrButton,
  3960. __hasProp = {}.hasOwnProperty,
  3961. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3962. HrButton = (function(_super) {
  3963. __extends(HrButton, _super);
  3964. function HrButton() {
  3965. return HrButton.__super__.constructor.apply(this, arguments);
  3966. }
  3967. HrButton.prototype.name = 'hr';
  3968. HrButton.prototype.icon = 'minus';
  3969. HrButton.prototype.htmlTag = 'hr';
  3970. HrButton.prototype.status = function($node) {
  3971. return true;
  3972. };
  3973. HrButton.prototype.command = function() {
  3974. var $hr, $newBlock, $nextBlock, $rootBlock;
  3975. $rootBlock = this.editor.util.furthestBlockEl();
  3976. $nextBlock = $rootBlock.next();
  3977. if ($nextBlock.length > 0) {
  3978. this.editor.selection.save();
  3979. } else {
  3980. $newBlock = $('<p/>').append(this.editor.util.phBr);
  3981. }
  3982. $hr = $('<hr/>').insertAfter($rootBlock);
  3983. if ($newBlock) {
  3984. $newBlock.insertAfter($hr);
  3985. this.editor.selection.setRangeAtStartOf($newBlock);
  3986. } else {
  3987. this.editor.selection.restore();
  3988. }
  3989. return this.editor.trigger('valuechanged');
  3990. };
  3991. return HrButton;
  3992. })(Button);
  3993. Simditor.Toolbar.addButton(HrButton);
  3994. var TableButton,
  3995. __hasProp = {}.hasOwnProperty,
  3996. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  3997. TableButton = (function(_super) {
  3998. __extends(TableButton, _super);
  3999. function TableButton() {
  4000. return TableButton.__super__.constructor.apply(this, arguments);
  4001. }
  4002. TableButton.prototype.name = 'table';
  4003. TableButton.prototype.icon = 'table';
  4004. TableButton.prototype.htmlTag = 'table';
  4005. TableButton.prototype.disableTag = 'pre, li, blockquote';
  4006. TableButton.prototype.menu = true;
  4007. TableButton.prototype._init = function() {
  4008. TableButton.__super__._init.call(this);
  4009. $.merge(this.editor.formatter._allowedTags, ['tbody', 'tr', 'td', 'colgroup', 'col']);
  4010. $.extend(this.editor.formatter._allowedAttributes, {
  4011. td: ['rowspan', 'colspan'],
  4012. col: ['width']
  4013. });
  4014. this._initShortcuts();
  4015. this.editor.on('decorate', (function(_this) {
  4016. return function(e, $el) {
  4017. return $el.find('table').each(function(i, table) {
  4018. return _this.decorate($(table));
  4019. });
  4020. };
  4021. })(this));
  4022. this.editor.on('undecorate', (function(_this) {
  4023. return function(e, $el) {
  4024. return $el.find('table').each(function(i, table) {
  4025. return _this.undecorate($(table));
  4026. });
  4027. };
  4028. })(this));
  4029. this.editor.on('selectionchanged.table', (function(_this) {
  4030. return function(e) {
  4031. var $container, range;
  4032. _this.editor.body.find('.simditor-table td').removeClass('active');
  4033. range = _this.editor.selection.getRange();
  4034. if (range == null) {
  4035. return;
  4036. }
  4037. $container = $(range.commonAncestorContainer);
  4038. if (range.collapsed && $container.is('.simditor-table')) {
  4039. if (_this.editor.selection.rangeAtStartOf($container)) {
  4040. $container = $container.find('td:first');
  4041. } else {
  4042. $container = $container.find('td:last');
  4043. }
  4044. _this.editor.selection.setRangeAtEndOf($container);
  4045. }
  4046. return $container.closest('td', _this.editor.body).addClass('active');
  4047. };
  4048. })(this));
  4049. this.editor.on('blur.table', (function(_this) {
  4050. return function(e) {
  4051. return _this.editor.body.find('.simditor-table td').removeClass('active');
  4052. };
  4053. })(this));
  4054. this.editor.inputManager.addKeystrokeHandler('38', 'td', (function(_this) {
  4055. return function(e, $node) {
  4056. var $prevTr, $tr, index;
  4057. $tr = $node.parent('tr');
  4058. $prevTr = $tr.prev('tr');
  4059. if (!($prevTr.length > 0)) {
  4060. return true;
  4061. }
  4062. index = $tr.find('td').index($node);
  4063. _this.editor.selection.setRangeAtEndOf($prevTr.find('td').eq(index));
  4064. return true;
  4065. };
  4066. })(this));
  4067. return this.editor.inputManager.addKeystrokeHandler('40', 'td', (function(_this) {
  4068. return function(e, $node) {
  4069. var $nextTr, $tr, index;
  4070. $tr = $node.parent('tr');
  4071. $nextTr = $tr.next('tr');
  4072. if (!($nextTr.length > 0)) {
  4073. return true;
  4074. }
  4075. index = $tr.find('td').index($node);
  4076. _this.editor.selection.setRangeAtEndOf($nextTr.find('td').eq(index));
  4077. return true;
  4078. };
  4079. })(this));
  4080. };
  4081. TableButton.prototype.initResize = function($table) {
  4082. var $colgroup, $resizeHandle, $wrapper;
  4083. $wrapper = $table.parent('.simditor-table');
  4084. $colgroup = $table.find('colgroup');
  4085. if ($colgroup.length < 1) {
  4086. $colgroup = $('<colgroup/>').prependTo($table);
  4087. $table.find('tr:first td').each((function(_this) {
  4088. return function(i, td) {
  4089. var $col;
  4090. return $col = $('<col/>').appendTo($colgroup);
  4091. };
  4092. })(this));
  4093. this.refreshTableWidth($table);
  4094. }
  4095. $resizeHandle = $('<div class="simditor-resize-handle" contenteditable="false"></div>').appendTo($wrapper);
  4096. $wrapper.on('mousemove', 'td', (function(_this) {
  4097. return function(e) {
  4098. var $col, $td, index, x, _ref, _ref1;
  4099. if ($wrapper.hasClass('resizing')) {
  4100. return;
  4101. }
  4102. $td = $(e.currentTarget);
  4103. x = e.pageX - $(e.currentTarget).offset().left;
  4104. if (x < 5 && $td.prev().length > 0) {
  4105. $td = $td.prev();
  4106. }
  4107. if ($td.next('td').length < 1) {
  4108. $resizeHandle.hide();
  4109. return;
  4110. }
  4111. if ((_ref = $resizeHandle.data('td')) != null ? _ref.is($td) : void 0) {
  4112. $resizeHandle.show();
  4113. return;
  4114. }
  4115. index = $td.parent().find('td').index($td);
  4116. $col = $colgroup.find('col').eq(index);
  4117. if ((_ref1 = $resizeHandle.data('col')) != null ? _ref1.is($col) : void 0) {
  4118. $resizeHandle.show();
  4119. return;
  4120. }
  4121. return $resizeHandle.css('left', $td.position().left + $td.outerWidth() - 5).data('td', $td).data('col', $col).show();
  4122. };
  4123. })(this));
  4124. $wrapper.on('mouseleave', (function(_this) {
  4125. return function(e) {
  4126. return $resizeHandle.hide();
  4127. };
  4128. })(this));
  4129. return $wrapper.on('mousedown', '.simditor-resize-handle', (function(_this) {
  4130. return function(e) {
  4131. var $handle, $leftCol, $leftTd, $rightCol, $rightTd, minWidth, startHandleLeft, startLeftWidth, startRightWidth, startX, tableWidth;
  4132. $handle = $(e.currentTarget);
  4133. $leftTd = $handle.data('td');
  4134. $leftCol = $handle.data('col');
  4135. $rightTd = $leftTd.next('td');
  4136. $rightCol = $leftCol.next('col');
  4137. startX = e.pageX;
  4138. startLeftWidth = $leftTd.outerWidth() * 1;
  4139. startRightWidth = $rightTd.outerWidth() * 1;
  4140. startHandleLeft = parseFloat($handle.css('left'));
  4141. tableWidth = $leftTd.closest('table').width();
  4142. minWidth = 50;
  4143. $(document).on('mousemove.simditor-resize-table', function(e) {
  4144. var deltaX, leftWidth, rightWidth;
  4145. deltaX = e.pageX - startX;
  4146. leftWidth = startLeftWidth + deltaX;
  4147. rightWidth = startRightWidth - deltaX;
  4148. if (leftWidth < minWidth) {
  4149. leftWidth = minWidth;
  4150. deltaX = minWidth - startLeftWidth;
  4151. rightWidth = startRightWidth - deltaX;
  4152. } else if (rightWidth < minWidth) {
  4153. rightWidth = minWidth;
  4154. deltaX = startRightWidth - minWidth;
  4155. leftWidth = startLeftWidth + deltaX;
  4156. }
  4157. $leftCol.attr('width', (leftWidth / tableWidth * 100) + '%');
  4158. $rightCol.attr('width', (rightWidth / tableWidth * 100) + '%');
  4159. return $handle.css('left', startHandleLeft + deltaX);
  4160. });
  4161. $(document).one('mouseup.simditor-resize-table', function(e) {
  4162. $(document).off('.simditor-resize-table');
  4163. return $wrapper.removeClass('resizing');
  4164. });
  4165. $wrapper.addClass('resizing');
  4166. return false;
  4167. };
  4168. })(this));
  4169. };
  4170. TableButton.prototype._initShortcuts = function() {
  4171. this.editor.inputManager.addShortcut('ctrl+alt+up', (function(_this) {
  4172. return function(e) {
  4173. _this.editMenu.find('.menu-item[data-param=insertRowAbove]').click();
  4174. return false;
  4175. };
  4176. })(this));
  4177. this.editor.inputManager.addShortcut('ctrl+alt+down', (function(_this) {
  4178. return function(e) {
  4179. _this.editMenu.find('.menu-item[data-param=insertRowBelow]').click();
  4180. return false;
  4181. };
  4182. })(this));
  4183. this.editor.inputManager.addShortcut('ctrl+alt+left', (function(_this) {
  4184. return function(e) {
  4185. _this.editMenu.find('.menu-item[data-param=insertColLeft]').click();
  4186. return false;
  4187. };
  4188. })(this));
  4189. return this.editor.inputManager.addShortcut('ctrl+alt+right', (function(_this) {
  4190. return function(e) {
  4191. _this.editMenu.find('.menu-item[data-param=insertColRight]').click();
  4192. return false;
  4193. };
  4194. })(this));
  4195. };
  4196. TableButton.prototype.decorate = function($table) {
  4197. if ($table.parent('.simditor-table').length > 0) {
  4198. this.undecorate($table);
  4199. }
  4200. $table.wrap('<div class="simditor-table"></div>');
  4201. this.initResize($table);
  4202. return $table.parent();
  4203. };
  4204. TableButton.prototype.undecorate = function($table) {
  4205. if (!($table.parent('.simditor-table').length > 0)) {
  4206. return;
  4207. }
  4208. return $table.parent().replaceWith($table);
  4209. };
  4210. TableButton.prototype.renderMenu = function() {
  4211. $("<div class=\"menu-create-table\">\n</div>\n<div class=\"menu-edit-table\">\n <ul>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"deleteRow\"><span>" + (this._t('deleteRow')) + " ( Ctrl + Alt + → )</span></a></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"insertRowAbove\"><span>" + (this._t('insertRowAbove')) + " ( Ctrl + Alt + ↑ )</span></a></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"insertRowBelow\"><span>" + (this._t('insertRowBelow')) + " ( Ctrl + Alt + ↓ )</span></a></li>\n <li><span class=\"separator\"></span></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"deleteCol\"><span>" + (this._t('deleteColumn')) + "</span></a></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"insertColLeft\"><span>" + (this._t('insertColumnLeft')) + " ( Ctrl + Alt + ← )</span></a></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"insertColRight\"><span>" + (this._t('insertColumnRight')) + " ( Ctrl + Alt + → )</span></a></li>\n <li><span class=\"separator\"></span></li>\n <li><a tabindex=\"-1\" unselectable=\"on\" class=\"menu-item\" href=\"javascript:;\" data-param=\"deleteTable\"><span>" + (this._t('deleteTable')) + "</span></a></li>\n </ul>\n</div>").appendTo(this.menuWrapper);
  4212. this.createMenu = this.menuWrapper.find('.menu-create-table');
  4213. this.editMenu = this.menuWrapper.find('.menu-edit-table');
  4214. this.createTable(6, 6).appendTo(this.createMenu);
  4215. this.createMenu.on('mouseenter', 'td', (function(_this) {
  4216. return function(e) {
  4217. var $td, $tr, num;
  4218. _this.createMenu.find('td').removeClass('selected');
  4219. $td = $(e.currentTarget);
  4220. $tr = $td.parent();
  4221. num = $tr.find('td').index($td) + 1;
  4222. return $tr.prevAll('tr').addBack().find('td:lt(' + num + ')').addClass('selected');
  4223. };
  4224. })(this));
  4225. this.createMenu.on('mouseleave', (function(_this) {
  4226. return function(e) {
  4227. return $(e.currentTarget).find('td').removeClass('selected');
  4228. };
  4229. })(this));
  4230. return this.createMenu.on('mousedown', 'td', (function(_this) {
  4231. return function(e) {
  4232. var $closestBlock, $table, $td, $tr, colNum, rowNum;
  4233. _this.wrapper.removeClass('menu-on');
  4234. if (!_this.editor.inputManager.focused) {
  4235. return;
  4236. }
  4237. $td = $(e.currentTarget);
  4238. $tr = $td.parent();
  4239. colNum = $tr.find('td').index($td) + 1;
  4240. rowNum = $tr.prevAll('tr').length + 1;
  4241. $table = _this.createTable(rowNum, colNum, true);
  4242. $closestBlock = _this.editor.util.closestBlockEl();
  4243. if (_this.editor.util.isEmptyNode($closestBlock)) {
  4244. $closestBlock.replaceWith($table);
  4245. } else {
  4246. $closestBlock.after($table);
  4247. }
  4248. _this.decorate($table);
  4249. _this.editor.selection.setRangeAtStartOf($table.find('td:first'));
  4250. _this.editor.trigger('valuechanged');
  4251. return false;
  4252. };
  4253. })(this));
  4254. };
  4255. TableButton.prototype.createTable = function(row, col, phBr) {
  4256. var $table, $tbody, $td, $tr, c, r, _i, _j;
  4257. $table = $('<table/>');
  4258. $tbody = $('<tbody/>').appendTo($table);
  4259. for (r = _i = 0; 0 <= row ? _i < row : _i > row; r = 0 <= row ? ++_i : --_i) {
  4260. $tr = $('<tr/>').appendTo($tbody);
  4261. for (c = _j = 0; 0 <= col ? _j < col : _j > col; c = 0 <= col ? ++_j : --_j) {
  4262. $td = $('<td/>').appendTo($tr);
  4263. if (phBr) {
  4264. $td.append(this.editor.util.phBr);
  4265. }
  4266. }
  4267. }
  4268. return $table;
  4269. };
  4270. TableButton.prototype.refreshTableWidth = function($table) {
  4271. var cols, tableWidth;
  4272. tableWidth = $table.width();
  4273. cols = $table.find('col');
  4274. return $table.find('tr:first td').each((function(_this) {
  4275. return function(i, td) {
  4276. var $col;
  4277. $col = cols.eq(i);
  4278. return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
  4279. };
  4280. })(this));
  4281. };
  4282. TableButton.prototype.setActive = function(active) {
  4283. TableButton.__super__.setActive.call(this, active);
  4284. if (active) {
  4285. this.createMenu.hide();
  4286. return this.editMenu.show();
  4287. } else {
  4288. this.createMenu.show();
  4289. return this.editMenu.hide();
  4290. }
  4291. };
  4292. TableButton.prototype.deleteRow = function($td) {
  4293. var $newTr, $tr, index;
  4294. $tr = $td.parent('tr');
  4295. if ($tr.siblings('tr').length < 1) {
  4296. return this.deleteTable($td);
  4297. } else {
  4298. $newTr = $tr.next('tr');
  4299. if (!($newTr.length > 0)) {
  4300. $newTr = $tr.prev('tr');
  4301. }
  4302. index = $tr.find('td').index($td);
  4303. $tr.remove();
  4304. return this.editor.selection.setRangeAtEndOf($newTr.find('td').eq(index));
  4305. }
  4306. };
  4307. TableButton.prototype.insertRow = function($td, direction) {
  4308. var $newTr, $table, $tr, colNum, i, index, _i;
  4309. if (direction == null) {
  4310. direction = 'after';
  4311. }
  4312. $tr = $td.parent('tr');
  4313. $table = $tr.closest('table');
  4314. colNum = 0;
  4315. $table.find('tr').each((function(_this) {
  4316. return function(i, tr) {
  4317. return colNum = Math.max(colNum, $(tr).find('td').length);
  4318. };
  4319. })(this));
  4320. $newTr = $('<tr/>');
  4321. for (i = _i = 1; 1 <= colNum ? _i <= colNum : _i >= colNum; i = 1 <= colNum ? ++_i : --_i) {
  4322. $('<td/>').append(this.editor.util.phBr).appendTo($newTr);
  4323. }
  4324. $tr[direction]($newTr);
  4325. index = $tr.find('td').index($td);
  4326. return this.editor.selection.setRangeAtStartOf($newTr.find('td').eq(index));
  4327. };
  4328. TableButton.prototype.deleteCol = function($td) {
  4329. var $newTd, $table, $tr, index;
  4330. $tr = $td.parent('tr');
  4331. if ($tr.siblings('tr').length < 1 && $td.siblings('td').length < 1) {
  4332. return this.deleteTable($td);
  4333. } else {
  4334. index = $tr.find('td').index($td);
  4335. $newTd = $td.next('td');
  4336. if (!($newTd.length > 0)) {
  4337. $newTd = $tr.prev('td');
  4338. }
  4339. $table = $tr.closest('table');
  4340. $table.find('col').eq(index).remove();
  4341. $table.find('tr').each((function(_this) {
  4342. return function(i, tr) {
  4343. return $(tr).find('td').eq(index).remove();
  4344. };
  4345. })(this));
  4346. this.refreshTableWidth($table);
  4347. return this.editor.selection.setRangeAtEndOf($newTd);
  4348. }
  4349. };
  4350. TableButton.prototype.insertCol = function($td, direction) {
  4351. var $col, $newCol, $newTd, $table, $tr, index, tableWidth, width;
  4352. if (direction == null) {
  4353. direction = 'after';
  4354. }
  4355. $tr = $td.parent('tr');
  4356. index = $tr.find('td').index($td);
  4357. $table = $td.closest('table');
  4358. $col = $table.find('col').eq(index);
  4359. $table.find('tr').each((function(_this) {
  4360. return function(i, tr) {
  4361. var $newTd;
  4362. $newTd = $('<td/>').append(_this.editor.util.phBr);
  4363. return $(tr).find('td').eq(index)[direction]($newTd);
  4364. };
  4365. })(this));
  4366. $newCol = $('<col/>');
  4367. $col[direction]($newCol);
  4368. tableWidth = $table.width();
  4369. width = Math.max(parseFloat($col.attr('width')) / 2, 50 / tableWidth * 100);
  4370. $col.attr('width', width + '%');
  4371. $newCol.attr('width', width + '%');
  4372. this.refreshTableWidth($table);
  4373. $newTd = direction === 'after' ? $td.next('td') : $td.prev('td');
  4374. return this.editor.selection.setRangeAtStartOf($newTd);
  4375. };
  4376. TableButton.prototype.deleteTable = function($td) {
  4377. var $block, $table;
  4378. $table = $td.closest('.simditor-table');
  4379. $block = $table.next('p');
  4380. $table.remove();
  4381. if ($block.length > 0) {
  4382. return this.editor.selection.setRangeAtStartOf($block);
  4383. }
  4384. };
  4385. TableButton.prototype.command = function(param) {
  4386. var $td, range;
  4387. range = this.editor.selection.getRange();
  4388. $td = $(range.commonAncestorContainer).closest('td');
  4389. if (!($td.length > 0)) {
  4390. return;
  4391. }
  4392. if (param === 'deleteRow') {
  4393. this.deleteRow($td);
  4394. } else if (param === 'insertRowAbove') {
  4395. this.insertRow($td, 'before');
  4396. } else if (param === 'insertRowBelow') {
  4397. this.insertRow($td);
  4398. } else if (param === 'deleteCol') {
  4399. this.deleteCol($td);
  4400. } else if (param === 'insertColLeft') {
  4401. this.insertCol($td, 'before');
  4402. } else if (param === 'insertColRight') {
  4403. this.insertCol($td);
  4404. } else if (param === 'deleteTable') {
  4405. this.deleteTable($td);
  4406. } else {
  4407. return;
  4408. }
  4409. return this.editor.trigger('valuechanged');
  4410. };
  4411. return TableButton;
  4412. })(Button);
  4413. Simditor.Toolbar.addButton(TableButton);
  4414. var StrikethroughButton,
  4415. __hasProp = {}.hasOwnProperty,
  4416. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  4417. StrikethroughButton = (function(_super) {
  4418. __extends(StrikethroughButton, _super);
  4419. function StrikethroughButton() {
  4420. return StrikethroughButton.__super__.constructor.apply(this, arguments);
  4421. }
  4422. StrikethroughButton.prototype.name = 'strikethrough';
  4423. StrikethroughButton.prototype.icon = 'strikethrough';
  4424. StrikethroughButton.prototype.htmlTag = 'strike';
  4425. StrikethroughButton.prototype.disableTag = 'pre';
  4426. StrikethroughButton.prototype.status = function($node) {
  4427. var active;
  4428. if ($node != null) {
  4429. this.setDisabled($node.is(this.disableTag));
  4430. }
  4431. if (this.disabled) {
  4432. return true;
  4433. }
  4434. active = document.queryCommandState('strikethrough') === true;
  4435. this.setActive(active);
  4436. return active;
  4437. };
  4438. StrikethroughButton.prototype.command = function() {
  4439. document.execCommand('strikethrough');
  4440. this.editor.trigger('valuechanged');
  4441. return $(document).trigger('selectionchange');
  4442. };
  4443. return StrikethroughButton;
  4444. })(Button);
  4445. Simditor.Toolbar.addButton(StrikethroughButton);
  4446. return Simditor;
  4447. }));