simditor.js 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892
  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. }));