simditor.js 173 KB

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