simditor.js 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585
  1. /*!
  2. * Simditor v2.3.5
  3. * http://simditor.tower.im/
  4. * 2015-11-19
  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. return _this.resetCaretPosition();
  1270. };
  1271. })(this));
  1272. this.editor.on('focus', (function(_this) {
  1273. return function(e) {
  1274. if (_this._stack.length === 0) {
  1275. return _this._pushUndoState();
  1276. }
  1277. };
  1278. })(this));
  1279. return this.editor.on('blur', (function(_this) {
  1280. return function(e) {
  1281. return _this.resetCaretPosition();
  1282. };
  1283. })(this));
  1284. };
  1285. UndoManager.prototype.resetCaretPosition = function() {
  1286. this._startPosition = null;
  1287. return this._endPosition = null;
  1288. };
  1289. UndoManager.prototype.startPosition = function() {
  1290. if (this.editor.selection._range) {
  1291. this._startPosition || (this._startPosition = this._getPosition('start'));
  1292. }
  1293. return this._startPosition;
  1294. };
  1295. UndoManager.prototype.endPosition = function() {
  1296. if (this.editor.selection._range) {
  1297. this._endPosition || (this._endPosition = (function(_this) {
  1298. return function() {
  1299. var range;
  1300. range = _this.editor.selection.range();
  1301. if (range.collapsed) {
  1302. return _this._startPosition;
  1303. }
  1304. return _this._getPosition('end');
  1305. };
  1306. })(this)());
  1307. }
  1308. return this._endPosition;
  1309. };
  1310. UndoManager.prototype._pushUndoState = function() {
  1311. var caret;
  1312. if (this.editor.triggerHandler('pushundostate') === false) {
  1313. return;
  1314. }
  1315. caret = this.caretPosition();
  1316. if (!caret.start) {
  1317. return;
  1318. }
  1319. this._index += 1;
  1320. this._stack.length = this._index;
  1321. this._stack.push({
  1322. html: this.editor.body.html(),
  1323. caret: this.caretPosition()
  1324. });
  1325. if (this._stack.length > this._capacity) {
  1326. this._stack.shift();
  1327. return this._index -= 1;
  1328. }
  1329. };
  1330. UndoManager.prototype.currentState = function() {
  1331. if (this._stack.length && this._index > -1) {
  1332. return this._stack[this._index];
  1333. } else {
  1334. return null;
  1335. }
  1336. };
  1337. UndoManager.prototype.undo = function() {
  1338. var state;
  1339. if (this._index < 1 || this._stack.length < 2) {
  1340. return;
  1341. }
  1342. this.editor.hidePopover();
  1343. this._index -= 1;
  1344. state = this._stack[this._index];
  1345. this.editor.body.html(state.html);
  1346. this.caretPosition(state.caret);
  1347. this.editor.body.find('.selected').removeClass('selected');
  1348. this.editor.sync();
  1349. return this.editor.trigger('valuechanged', ['undo']);
  1350. };
  1351. UndoManager.prototype.redo = function() {
  1352. var state;
  1353. if (this._index < 0 || this._stack.length < this._index + 2) {
  1354. return;
  1355. }
  1356. this.editor.hidePopover();
  1357. this._index += 1;
  1358. state = this._stack[this._index];
  1359. this.editor.body.html(state.html);
  1360. this.caretPosition(state.caret);
  1361. this.editor.body.find('.selected').removeClass('selected');
  1362. this.editor.sync();
  1363. return this.editor.trigger('valuechanged', ['redo']);
  1364. };
  1365. UndoManager.prototype.update = function() {
  1366. var currentState, html;
  1367. currentState = this.currentState();
  1368. if (!currentState) {
  1369. return;
  1370. }
  1371. html = this.editor.body.html();
  1372. currentState.html = 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. };
  2072. Clipboard.prototype._init = function() {
  2073. this.editor = this._module;
  2074. if (this.opts.pasteImage && typeof this.opts.pasteImage !== 'string') {
  2075. this.opts.pasteImage = 'inline';
  2076. }
  2077. return this.editor.body.on('paste', (function(_this) {
  2078. return function(e) {
  2079. var range;
  2080. if (_this.pasting || _this._pasteBin) {
  2081. return;
  2082. }
  2083. if (_this.editor.triggerHandler(e) === false) {
  2084. return false;
  2085. }
  2086. range = _this.editor.selection.deleteRangeContents();
  2087. if (_this.editor.body.html()) {
  2088. if (!range.collapsed) {
  2089. range.collapse(true);
  2090. }
  2091. } else {
  2092. _this.editor.formatter.format();
  2093. _this.editor.selection.setRangeAtStartOf(_this.editor.body.find('p:first'));
  2094. }
  2095. if (_this._processPasteByClipboardApi(e)) {
  2096. return false;
  2097. }
  2098. _this.editor.inputManager.throttledValueChanged.clear();
  2099. _this.editor.inputManager.throttledSelectionChanged.clear();
  2100. _this.editor.undoManager.throttledPushState.clear();
  2101. _this.editor.selection.reset();
  2102. _this.editor.undoManager.resetCaretPosition();
  2103. _this.pasting = true;
  2104. return _this._getPasteContent(function(pasteContent) {
  2105. _this._processPasteContent(pasteContent);
  2106. _this._pasteInBlockEl = null;
  2107. _this._pastePlainText = null;
  2108. return _this.pasting = false;
  2109. });
  2110. };
  2111. })(this));
  2112. };
  2113. Clipboard.prototype._processPasteByClipboardApi = function(e) {
  2114. var imageFile, pasteItem, ref, uploadOpt;
  2115. if (this.editor.util.browser.edge) {
  2116. return;
  2117. }
  2118. if (e.originalEvent.clipboardData && e.originalEvent.clipboardData.items && e.originalEvent.clipboardData.items.length > 0) {
  2119. pasteItem = e.originalEvent.clipboardData.items[0];
  2120. if (/^image\//.test(pasteItem.type)) {
  2121. imageFile = pasteItem.getAsFile();
  2122. if (!((imageFile != null) && this.opts.pasteImage)) {
  2123. return;
  2124. }
  2125. if (!imageFile.name) {
  2126. imageFile.name = "Clipboard Image.png";
  2127. }
  2128. if (this.editor.triggerHandler('pasting', [imageFile]) === false) {
  2129. return;
  2130. }
  2131. uploadOpt = {};
  2132. uploadOpt[this.opts.pasteImage] = true;
  2133. if ((ref = this.editor.uploader) != null) {
  2134. ref.upload(imageFile, uploadOpt);
  2135. }
  2136. return true;
  2137. }
  2138. }
  2139. };
  2140. Clipboard.prototype._getPasteContent = function(callback) {
  2141. var state;
  2142. this._pasteBin = $('<div contenteditable="true" />').addClass('simditor-paste-bin').attr('tabIndex', '-1').appendTo(this.editor.el);
  2143. state = {
  2144. html: this.editor.body.html(),
  2145. caret: this.editor.undoManager.caretPosition()
  2146. };
  2147. this._pasteBin.focus();
  2148. return setTimeout((function(_this) {
  2149. return function() {
  2150. var pasteContent;
  2151. _this.editor.hidePopover();
  2152. _this.editor.body.html(state.html);
  2153. _this.editor.undoManager.caretPosition(state.caret);
  2154. _this.editor.body.focus();
  2155. _this.editor.selection.reset();
  2156. _this.editor.selection.range();
  2157. _this._pasteInBlockEl = _this.editor.selection.blockNodes().last();
  2158. _this._pastePlainText = _this._pasteInBlockEl.is('pre, table');
  2159. if (_this._pastePlainText) {
  2160. pasteContent = _this.editor.formatter.clearHtml(_this._pasteBin.html(), true);
  2161. } else {
  2162. pasteContent = $('<div/>').append(_this._pasteBin.contents());
  2163. pasteContent.find('table colgroup').remove();
  2164. _this.editor.formatter.format(pasteContent);
  2165. _this.editor.formatter.decorate(pasteContent);
  2166. _this.editor.formatter.beautify(pasteContent.children());
  2167. pasteContent = pasteContent.contents();
  2168. }
  2169. _this._pasteBin.remove();
  2170. _this._pasteBin = null;
  2171. return callback(pasteContent);
  2172. };
  2173. })(this), 0);
  2174. };
  2175. Clipboard.prototype._processPasteContent = function(pasteContent) {
  2176. var $blockEl, $img, blob, children, insertPosition, k, l, lastLine, len, len1, len2, len3, len4, line, lines, m, node, o, q, ref, ref1, ref2, uploadOpt;
  2177. if (this.editor.triggerHandler('pasting', [pasteContent]) === false) {
  2178. return;
  2179. }
  2180. $blockEl = this._pasteInBlockEl;
  2181. if (!pasteContent) {
  2182. return;
  2183. } else if (this._pastePlainText) {
  2184. if ($blockEl.is('table')) {
  2185. lines = pasteContent.split('\n');
  2186. lastLine = lines.pop();
  2187. for (k = 0, len = lines.length; k < len; k++) {
  2188. line = lines[k];
  2189. this.editor.selection.insertNode(document.createTextNode(line));
  2190. this.editor.selection.insertNode($('<br/>'));
  2191. }
  2192. this.editor.selection.insertNode(document.createTextNode(lastLine));
  2193. } else {
  2194. pasteContent = $('<div/>').text(pasteContent);
  2195. ref = pasteContent.contents();
  2196. for (l = 0, len1 = ref.length; l < len1; l++) {
  2197. node = ref[l];
  2198. this.editor.selection.insertNode($(node)[0]);
  2199. }
  2200. }
  2201. } else if ($blockEl.is(this.editor.body)) {
  2202. for (m = 0, len2 = pasteContent.length; m < len2; m++) {
  2203. node = pasteContent[m];
  2204. this.editor.selection.insertNode(node);
  2205. }
  2206. } else if (pasteContent.length < 1) {
  2207. return;
  2208. } else if (pasteContent.length === 1) {
  2209. if (pasteContent.is('p')) {
  2210. children = pasteContent.contents();
  2211. if (children.length === 1 && children.is('img')) {
  2212. $img = children;
  2213. if (/^data:image/.test($img.attr('src'))) {
  2214. if (!this.opts.pasteImage) {
  2215. return;
  2216. }
  2217. blob = this.editor.util.dataURLtoBlob($img.attr("src"));
  2218. blob.name = "Clipboard Image.png";
  2219. uploadOpt = {};
  2220. uploadOpt[this.opts.pasteImage] = true;
  2221. if ((ref1 = this.editor.uploader) != null) {
  2222. ref1.upload(blob, uploadOpt);
  2223. }
  2224. return;
  2225. } else if ($img.is('img[src^="webkit-fake-url://"]')) {
  2226. return;
  2227. }
  2228. }
  2229. for (o = 0, len3 = children.length; o < len3; o++) {
  2230. node = children[o];
  2231. this.editor.selection.insertNode(node);
  2232. }
  2233. } else if ($blockEl.is('p') && this.editor.util.isEmptyNode($blockEl)) {
  2234. $blockEl.replaceWith(pasteContent);
  2235. this.editor.selection.setRangeAtEndOf(pasteContent);
  2236. } else if (pasteContent.is('ul, ol')) {
  2237. if (pasteContent.find('li').length === 1) {
  2238. pasteContent = $('<div/>').text(pasteContent.text());
  2239. ref2 = pasteContent.contents();
  2240. for (q = 0, len4 = ref2.length; q < len4; q++) {
  2241. node = ref2[q];
  2242. this.editor.selection.insertNode($(node)[0]);
  2243. }
  2244. } else if ($blockEl.is('li')) {
  2245. $blockEl.parent().after(pasteContent);
  2246. this.editor.selection.setRangeAtEndOf(pasteContent);
  2247. } else {
  2248. $blockEl.after(pasteContent);
  2249. this.editor.selection.setRangeAtEndOf(pasteContent);
  2250. }
  2251. } else {
  2252. $blockEl.after(pasteContent);
  2253. this.editor.selection.setRangeAtEndOf(pasteContent);
  2254. }
  2255. } else {
  2256. if ($blockEl.is('li')) {
  2257. $blockEl = $blockEl.parent();
  2258. }
  2259. if (this.editor.selection.rangeAtStartOf($blockEl)) {
  2260. insertPosition = 'before';
  2261. } else if (this.editor.selection.rangeAtEndOf($blockEl)) {
  2262. insertPosition = 'after';
  2263. } else {
  2264. this.editor.selection.breakBlockEl($blockEl);
  2265. insertPosition = 'before';
  2266. }
  2267. $blockEl[insertPosition](pasteContent);
  2268. this.editor.selection.setRangeAtEndOf(pasteContent.last());
  2269. }
  2270. return this.editor.inputManager.throttledValueChanged();
  2271. };
  2272. return Clipboard;
  2273. })(SimpleModule);
  2274. Simditor = (function(superClass) {
  2275. extend(Simditor, superClass);
  2276. function Simditor() {
  2277. return Simditor.__super__.constructor.apply(this, arguments);
  2278. }
  2279. Simditor.connect(Util);
  2280. Simditor.connect(InputManager);
  2281. Simditor.connect(Selection);
  2282. Simditor.connect(UndoManager);
  2283. Simditor.connect(Keystroke);
  2284. Simditor.connect(Formatter);
  2285. Simditor.connect(Toolbar);
  2286. Simditor.connect(Indentation);
  2287. Simditor.connect(Clipboard);
  2288. Simditor.count = 0;
  2289. Simditor.prototype.opts = {
  2290. textarea: null,
  2291. placeholder: '',
  2292. defaultImage: 'images/image.png',
  2293. params: {},
  2294. upload: false,
  2295. indentWidth: 40
  2296. };
  2297. Simditor.prototype._init = function() {
  2298. var e, editor, form, uploadOpts;
  2299. this.textarea = $(this.opts.textarea);
  2300. this.opts.placeholder = this.opts.placeholder || this.textarea.attr('placeholder');
  2301. if (!this.textarea.length) {
  2302. throw new Error('simditor: param textarea is required.');
  2303. return;
  2304. }
  2305. editor = this.textarea.data('simditor');
  2306. if (editor != null) {
  2307. editor.destroy();
  2308. }
  2309. this.id = ++Simditor.count;
  2310. this._render();
  2311. if (simpleHotkeys) {
  2312. this.hotkeys = simpleHotkeys({
  2313. el: this.body
  2314. });
  2315. } else {
  2316. throw new Error('simditor: simple-hotkeys is required.');
  2317. return;
  2318. }
  2319. if (this.opts.upload && simpleUploader) {
  2320. uploadOpts = typeof this.opts.upload === 'object' ? this.opts.upload : {};
  2321. this.uploader = simpleUploader(uploadOpts);
  2322. }
  2323. form = this.textarea.closest('form');
  2324. if (form.length) {
  2325. form.on('submit.simditor-' + this.id, (function(_this) {
  2326. return function() {
  2327. return _this.sync();
  2328. };
  2329. })(this));
  2330. form.on('reset.simditor-' + this.id, (function(_this) {
  2331. return function() {
  2332. return _this.setValue('');
  2333. };
  2334. })(this));
  2335. }
  2336. this.on('initialized', (function(_this) {
  2337. return function() {
  2338. if (_this.opts.placeholder) {
  2339. _this.on('valuechanged', function() {
  2340. return _this._placeholder();
  2341. });
  2342. }
  2343. _this.setValue(_this.textarea.val().trim() || '');
  2344. if (_this.textarea.attr('autofocus')) {
  2345. return _this.focus();
  2346. }
  2347. };
  2348. })(this));
  2349. if (this.util.browser.mozilla) {
  2350. this.util.reflow();
  2351. try {
  2352. document.execCommand('enableObjectResizing', false, false);
  2353. return document.execCommand('enableInlineTableEditing', false, false);
  2354. } catch (_error) {
  2355. e = _error;
  2356. }
  2357. }
  2358. };
  2359. 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>";
  2360. Simditor.prototype._render = function() {
  2361. var key, ref, results, val;
  2362. this.el = $(this._tpl).insertBefore(this.textarea);
  2363. this.wrapper = this.el.find('.simditor-wrapper');
  2364. this.body = this.wrapper.find('.simditor-body');
  2365. this.placeholderEl = this.wrapper.find('.simditor-placeholder').append(this.opts.placeholder);
  2366. this.el.data('simditor', this);
  2367. this.wrapper.append(this.textarea);
  2368. this.textarea.data('simditor', this).blur();
  2369. this.body.attr('tabindex', this.textarea.attr('tabindex'));
  2370. if (this.util.os.mac) {
  2371. this.el.addClass('simditor-mac');
  2372. } else if (this.util.os.linux) {
  2373. this.el.addClass('simditor-linux');
  2374. }
  2375. if (this.util.os.mobile) {
  2376. this.el.addClass('simditor-mobile');
  2377. }
  2378. if (this.opts.params) {
  2379. ref = this.opts.params;
  2380. results = [];
  2381. for (key in ref) {
  2382. val = ref[key];
  2383. results.push($('<input/>', {
  2384. type: 'hidden',
  2385. name: key,
  2386. value: val
  2387. }).insertAfter(this.textarea));
  2388. }
  2389. return results;
  2390. }
  2391. };
  2392. Simditor.prototype._placeholder = function() {
  2393. var children;
  2394. children = this.body.children();
  2395. if (children.length === 0 || (children.length === 1 && this.util.isEmptyNode(children) && parseInt(children.css('margin-left') || 0) < this.opts.indentWidth)) {
  2396. return this.placeholderEl.show();
  2397. } else {
  2398. return this.placeholderEl.hide();
  2399. }
  2400. };
  2401. Simditor.prototype.setValue = function(val) {
  2402. this.hidePopover();
  2403. this.textarea.val(val);
  2404. this.body.html(val);
  2405. this.formatter.format();
  2406. this.formatter.decorate();
  2407. this.util.reflow(this.body);
  2408. this.inputManager.lastCaretPosition = null;
  2409. return this.trigger('valuechanged');
  2410. };
  2411. Simditor.prototype.getValue = function() {
  2412. return this.sync();
  2413. };
  2414. Simditor.prototype.sync = function() {
  2415. var children, cloneBody, emptyP, firstP, lastP, val;
  2416. cloneBody = this.body.clone();
  2417. this.formatter.undecorate(cloneBody);
  2418. this.formatter.format(cloneBody);
  2419. this.formatter.autolink(cloneBody);
  2420. children = cloneBody.children();
  2421. lastP = children.last('p');
  2422. firstP = children.first('p');
  2423. while (lastP.is('p') && this.util.isEmptyNode(lastP)) {
  2424. emptyP = lastP;
  2425. lastP = lastP.prev('p');
  2426. emptyP.remove();
  2427. }
  2428. while (firstP.is('p') && this.util.isEmptyNode(firstP)) {
  2429. emptyP = firstP;
  2430. firstP = lastP.next('p');
  2431. emptyP.remove();
  2432. }
  2433. cloneBody.find('img.uploading').remove();
  2434. val = $.trim(cloneBody.html());
  2435. this.textarea.val(val);
  2436. return val;
  2437. };
  2438. Simditor.prototype.focus = function() {
  2439. var $blockEl, range;
  2440. if (!(this.body.is(':visible') && this.body.is('[contenteditable]'))) {
  2441. this.el.find('textarea:visible').focus();
  2442. return;
  2443. }
  2444. if (this.inputManager.lastCaretPosition) {
  2445. this.undoManager.caretPosition(this.inputManager.lastCaretPosition);
  2446. return this.inputManager.lastCaretPosition = null;
  2447. } else {
  2448. $blockEl = this.body.children().last();
  2449. if (!$blockEl.is('p')) {
  2450. $blockEl = $('<p/>').append(this.util.phBr).appendTo(this.body);
  2451. }
  2452. range = document.createRange();
  2453. return this.selection.setRangeAtEndOf($blockEl, range);
  2454. }
  2455. };
  2456. Simditor.prototype.blur = function() {
  2457. if (this.body.is(':visible') && this.body.is('[contenteditable]')) {
  2458. return this.body.blur();
  2459. } else {
  2460. return this.body.find('textarea:visible').blur();
  2461. }
  2462. };
  2463. Simditor.prototype.hidePopover = function() {
  2464. return this.el.find('.simditor-popover').each(function(i, popover) {
  2465. popover = $(popover).data('popover');
  2466. if (popover.active) {
  2467. return popover.hide();
  2468. }
  2469. });
  2470. };
  2471. Simditor.prototype.destroy = function() {
  2472. this.triggerHandler('destroy');
  2473. this.textarea.closest('form').off('.simditor .simditor-' + this.id);
  2474. this.selection.clear();
  2475. this.inputManager.focused = false;
  2476. this.textarea.insertBefore(this.el).hide().val('').removeData('simditor');
  2477. this.el.remove();
  2478. $(document).off('.simditor-' + this.id);
  2479. $(window).off('.simditor-' + this.id);
  2480. return this.off();
  2481. };
  2482. return Simditor;
  2483. })(SimpleModule);
  2484. Simditor.i18n = {
  2485. 'zh-CN': {
  2486. 'blockquote': '引用',
  2487. 'bold': '加粗文字',
  2488. 'code': '插入代码',
  2489. 'color': '文字颜色',
  2490. 'coloredText': '彩色文字',
  2491. 'hr': '分隔线',
  2492. 'image': '插入图片',
  2493. 'externalImage': '外链图片',
  2494. 'uploadImage': '上传图片',
  2495. 'uploadFailed': '上传失败了',
  2496. 'uploadError': '上传出错了',
  2497. 'imageUrl': '图片地址',
  2498. 'imageSize': '图片尺寸',
  2499. 'imageAlt': '图片描述',
  2500. 'restoreImageSize': '还原图片尺寸',
  2501. 'uploading': '正在上传',
  2502. 'indent': '向右缩进',
  2503. 'outdent': '向左缩进',
  2504. 'italic': '斜体文字',
  2505. 'link': '插入链接',
  2506. 'linkText': '链接文字',
  2507. 'linkUrl': '链接地址',
  2508. 'linkTarget': '打开方式',
  2509. 'openLinkInCurrentWindow': '在新窗口中打开',
  2510. 'openLinkInNewWindow': '在当前窗口中打开',
  2511. 'removeLink': '移除链接',
  2512. 'ol': '有序列表',
  2513. 'ul': '无序列表',
  2514. 'strikethrough': '删除线文字',
  2515. 'table': '表格',
  2516. 'deleteRow': '删除行',
  2517. 'insertRowAbove': '在上面插入行',
  2518. 'insertRowBelow': '在下面插入行',
  2519. 'deleteColumn': '删除列',
  2520. 'insertColumnLeft': '在左边插入列',
  2521. 'insertColumnRight': '在右边插入列',
  2522. 'deleteTable': '删除表格',
  2523. 'title': '标题',
  2524. 'normalText': '普通文本',
  2525. 'underline': '下划线文字',
  2526. 'alignment': '水平对齐',
  2527. 'alignCenter': '居中',
  2528. 'alignLeft': '居左',
  2529. 'alignRight': '居右',
  2530. 'selectLanguage': '选择程序语言',
  2531. 'fontScale': '字体大小',
  2532. 'fontScaleXLarge': '超大字体',
  2533. 'fontScaleLarge': '大号字体',
  2534. 'fontScaleNormal': '正常大小',
  2535. 'fontScaleSmall': '小号字体',
  2536. 'fontScaleXSmall': '超小字体'
  2537. },
  2538. 'en-US': {
  2539. 'blockquote': 'Block Quote',
  2540. 'bold': 'Bold',
  2541. 'code': 'Code',
  2542. 'color': 'Text Color',
  2543. 'coloredText': 'Colored Text',
  2544. 'hr': 'Horizontal Line',
  2545. 'image': 'Insert Image',
  2546. 'externalImage': 'External Image',
  2547. 'uploadImage': 'Upload Image',
  2548. 'uploadFailed': 'Upload failed',
  2549. 'uploadError': 'Error occurs during upload',
  2550. 'imageUrl': 'Url',
  2551. 'imageSize': 'Size',
  2552. 'imageAlt': 'Alt',
  2553. 'restoreImageSize': 'Restore Origin Size',
  2554. 'uploading': 'Uploading',
  2555. 'indent': 'Indent',
  2556. 'outdent': 'Outdent',
  2557. 'italic': 'Italic',
  2558. 'link': 'Insert Link',
  2559. 'linkText': 'Text',
  2560. 'linkUrl': 'Url',
  2561. 'linkTarget': 'Target',
  2562. 'openLinkInCurrentWindow': 'Open link in current window',
  2563. 'openLinkInNewWindow': 'Open link in new window',
  2564. 'removeLink': 'Remove Link',
  2565. 'ol': 'Ordered List',
  2566. 'ul': 'Unordered List',
  2567. 'strikethrough': 'Strikethrough',
  2568. 'table': 'Table',
  2569. 'deleteRow': 'Delete Row',
  2570. 'insertRowAbove': 'Insert Row Above',
  2571. 'insertRowBelow': 'Insert Row Below',
  2572. 'deleteColumn': 'Delete Column',
  2573. 'insertColumnLeft': 'Insert Column Left',
  2574. 'insertColumnRight': 'Insert Column Right',
  2575. 'deleteTable': 'Delete Table',
  2576. 'title': 'Title',
  2577. 'normalText': 'Text',
  2578. 'underline': 'Underline',
  2579. 'alignment': 'Alignment',
  2580. 'alignCenter': 'Align Center',
  2581. 'alignLeft': 'Align Left',
  2582. 'alignRight': 'Align Right',
  2583. 'selectLanguage': 'Select Language',
  2584. 'fontScale': 'Font Size',
  2585. 'fontScaleXLarge': 'X Large Size',
  2586. 'fontScaleLarge': 'Large Size',
  2587. 'fontScaleNormal': 'Normal Size',
  2588. 'fontScaleSmall': 'Small Size',
  2589. 'fontScaleXSmall': 'X Small Size'
  2590. }
  2591. };
  2592. Button = (function(superClass) {
  2593. extend(Button, superClass);
  2594. Button.prototype._tpl = {
  2595. item: '<li><a tabindex="-1" unselectable="on" class="toolbar-item" href="javascript:;"><span></span></a></li>',
  2596. menuWrapper: '<div class="toolbar-menu"></div>',
  2597. menuItem: '<li><a tabindex="-1" unselectable="on" class="menu-item" href="javascript:;"><span></span></a></li>',
  2598. separator: '<li><span class="separator"></span></li>'
  2599. };
  2600. Button.prototype.name = '';
  2601. Button.prototype.icon = '';
  2602. Button.prototype.title = '';
  2603. Button.prototype.text = '';
  2604. Button.prototype.htmlTag = '';
  2605. Button.prototype.disableTag = '';
  2606. Button.prototype.menu = false;
  2607. Button.prototype.active = false;
  2608. Button.prototype.disabled = false;
  2609. Button.prototype.needFocus = true;
  2610. Button.prototype.shortcut = null;
  2611. function Button(opts) {
  2612. this.editor = opts.editor;
  2613. this.title = this._t(this.name);
  2614. Button.__super__.constructor.call(this, opts);
  2615. }
  2616. Button.prototype._init = function() {
  2617. var k, len, ref, tag;
  2618. this.render();
  2619. this.el.on('mousedown', (function(_this) {
  2620. return function(e) {
  2621. var exceed, noFocus, param;
  2622. e.preventDefault();
  2623. noFocus = _this.needFocus && !_this.editor.inputManager.focused;
  2624. if (_this.el.hasClass('disabled') || noFocus) {
  2625. return false;
  2626. }
  2627. if (_this.menu) {
  2628. _this.wrapper.toggleClass('menu-on').siblings('li').removeClass('menu-on');
  2629. if (_this.wrapper.is('.menu-on')) {
  2630. exceed = _this.menuWrapper.offset().left + _this.menuWrapper.outerWidth() + 5 - _this.editor.wrapper.offset().left - _this.editor.wrapper.outerWidth();
  2631. if (exceed > 0) {
  2632. _this.menuWrapper.css({
  2633. 'left': 'auto',
  2634. 'right': 0
  2635. });
  2636. }
  2637. _this.trigger('menuexpand');
  2638. }
  2639. return false;
  2640. }
  2641. param = _this.el.data('param');
  2642. _this.command(param);
  2643. return false;
  2644. };
  2645. })(this));
  2646. this.wrapper.on('click', 'a.menu-item', (function(_this) {
  2647. return function(e) {
  2648. var btn, noFocus, param;
  2649. e.preventDefault();
  2650. btn = $(e.currentTarget);
  2651. _this.wrapper.removeClass('menu-on');
  2652. noFocus = _this.needFocus && !_this.editor.inputManager.focused;
  2653. if (btn.hasClass('disabled') || noFocus) {
  2654. return false;
  2655. }
  2656. _this.editor.toolbar.wrapper.removeClass('menu-on');
  2657. param = btn.data('param');
  2658. _this.command(param);
  2659. return false;
  2660. };
  2661. })(this));
  2662. this.wrapper.on('mousedown', 'a.menu-item', function(e) {
  2663. return false;
  2664. });
  2665. this.editor.on('blur', (function(_this) {
  2666. return function() {
  2667. var editorActive;
  2668. editorActive = _this.editor.body.is(':visible') && _this.editor.body.is('[contenteditable]');
  2669. if (!(editorActive && !_this.editor.clipboard.pasting)) {
  2670. return;
  2671. }
  2672. _this.setActive(false);
  2673. return _this.setDisabled(false);
  2674. };
  2675. })(this));
  2676. if (this.shortcut != null) {
  2677. this.editor.hotkeys.add(this.shortcut, (function(_this) {
  2678. return function(e) {
  2679. _this.el.mousedown();
  2680. return false;
  2681. };
  2682. })(this));
  2683. }
  2684. ref = this.htmlTag.split(',');
  2685. for (k = 0, len = ref.length; k < len; k++) {
  2686. tag = ref[k];
  2687. tag = $.trim(tag);
  2688. if (tag && $.inArray(tag, this.editor.formatter._allowedTags) < 0) {
  2689. this.editor.formatter._allowedTags.push(tag);
  2690. }
  2691. }
  2692. return this.editor.on('selectionchanged', (function(_this) {
  2693. return function(e) {
  2694. if (_this.editor.inputManager.focused) {
  2695. return _this._status();
  2696. }
  2697. };
  2698. })(this));
  2699. };
  2700. Button.prototype.iconClassOf = function(icon) {
  2701. if (icon) {
  2702. return "simditor-icon simditor-icon-" + icon;
  2703. } else {
  2704. return '';
  2705. }
  2706. };
  2707. Button.prototype.setIcon = function(icon) {
  2708. return this.el.find('span').removeClass().addClass(this.iconClassOf(icon)).text(this.text);
  2709. };
  2710. Button.prototype.render = function() {
  2711. this.wrapper = $(this._tpl.item).appendTo(this.editor.toolbar.list);
  2712. this.el = this.wrapper.find('a.toolbar-item');
  2713. this.el.attr('title', this.title).addClass("toolbar-item-" + this.name).data('button', this);
  2714. this.setIcon(this.icon);
  2715. if (!this.menu) {
  2716. return;
  2717. }
  2718. this.menuWrapper = $(this._tpl.menuWrapper).appendTo(this.wrapper);
  2719. this.menuWrapper.addClass("toolbar-menu-" + this.name);
  2720. return this.renderMenu();
  2721. };
  2722. Button.prototype.renderMenu = function() {
  2723. var $menuBtnEl, $menuItemEl, k, len, menuItem, ref, ref1, results;
  2724. if (!$.isArray(this.menu)) {
  2725. return;
  2726. }
  2727. this.menuEl = $('<ul/>').appendTo(this.menuWrapper);
  2728. ref = this.menu;
  2729. results = [];
  2730. for (k = 0, len = ref.length; k < len; k++) {
  2731. menuItem = ref[k];
  2732. if (menuItem === '|') {
  2733. $(this._tpl.separator).appendTo(this.menuEl);
  2734. continue;
  2735. }
  2736. $menuItemEl = $(this._tpl.menuItem).appendTo(this.menuEl);
  2737. $menuBtnEl = $menuItemEl.find('a.menu-item').attr({
  2738. 'title': (ref1 = menuItem.title) != null ? ref1 : menuItem.text,
  2739. 'data-param': menuItem.param
  2740. }).addClass('menu-item-' + menuItem.name);
  2741. if (menuItem.icon) {
  2742. results.push($menuBtnEl.find('span').addClass(this.iconClassOf(menuItem.icon)));
  2743. } else {
  2744. results.push($menuBtnEl.find('span').text(menuItem.text));
  2745. }
  2746. }
  2747. return results;
  2748. };
  2749. Button.prototype.setActive = function(active) {
  2750. if (active === this.active) {
  2751. return;
  2752. }
  2753. this.active = active;
  2754. return this.el.toggleClass('active', this.active);
  2755. };
  2756. Button.prototype.setDisabled = function(disabled) {
  2757. if (disabled === this.disabled) {
  2758. return;
  2759. }
  2760. this.disabled = disabled;
  2761. return this.el.toggleClass('disabled', this.disabled);
  2762. };
  2763. Button.prototype._disableStatus = function() {
  2764. var disabled, endNodes, startNodes;
  2765. startNodes = this.editor.selection.startNodes();
  2766. endNodes = this.editor.selection.endNodes();
  2767. disabled = startNodes.filter(this.disableTag).length > 0 || endNodes.filter(this.disableTag).length > 0;
  2768. this.setDisabled(disabled);
  2769. if (this.disabled) {
  2770. this.setActive(false);
  2771. }
  2772. return this.disabled;
  2773. };
  2774. Button.prototype._activeStatus = function() {
  2775. var active, endNode, endNodes, startNode, startNodes;
  2776. startNodes = this.editor.selection.startNodes();
  2777. endNodes = this.editor.selection.endNodes();
  2778. startNode = startNodes.filter(this.htmlTag);
  2779. endNode = endNodes.filter(this.htmlTag);
  2780. active = startNode.length > 0 && endNode.length > 0 && startNode.is(endNode);
  2781. this.node = active ? startNode : null;
  2782. this.setActive(active);
  2783. return this.active;
  2784. };
  2785. Button.prototype._status = function() {
  2786. this._disableStatus();
  2787. if (this.disabled) {
  2788. return;
  2789. }
  2790. return this._activeStatus();
  2791. };
  2792. Button.prototype.command = function(param) {};
  2793. Button.prototype._t = function() {
  2794. var args, ref, result;
  2795. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  2796. result = Button.__super__._t.apply(this, args);
  2797. if (!result) {
  2798. result = (ref = this.editor)._t.apply(ref, args);
  2799. }
  2800. return result;
  2801. };
  2802. return Button;
  2803. })(SimpleModule);
  2804. Simditor.Button = Button;
  2805. Popover = (function(superClass) {
  2806. extend(Popover, superClass);
  2807. Popover.prototype.offset = {
  2808. top: 4,
  2809. left: 0
  2810. };
  2811. Popover.prototype.target = null;
  2812. Popover.prototype.active = false;
  2813. function Popover(opts) {
  2814. this.button = opts.button;
  2815. this.editor = opts.button.editor;
  2816. Popover.__super__.constructor.call(this, opts);
  2817. }
  2818. Popover.prototype._init = function() {
  2819. this.el = $('<div class="simditor-popover"></div>').appendTo(this.editor.el).data('popover', this);
  2820. this.render();
  2821. this.el.on('mouseenter', (function(_this) {
  2822. return function(e) {
  2823. return _this.el.addClass('hover');
  2824. };
  2825. })(this));
  2826. return this.el.on('mouseleave', (function(_this) {
  2827. return function(e) {
  2828. return _this.el.removeClass('hover');
  2829. };
  2830. })(this));
  2831. };
  2832. Popover.prototype.render = function() {};
  2833. Popover.prototype._initLabelWidth = function() {
  2834. var $fields;
  2835. $fields = this.el.find('.settings-field');
  2836. if (!($fields.length > 0)) {
  2837. return;
  2838. }
  2839. this._labelWidth = 0;
  2840. $fields.each((function(_this) {
  2841. return function(i, field) {
  2842. var $field, $label;
  2843. $field = $(field);
  2844. $label = $field.find('label');
  2845. if (!($label.length > 0)) {
  2846. return;
  2847. }
  2848. return _this._labelWidth = Math.max(_this._labelWidth, $label.width());
  2849. };
  2850. })(this));
  2851. return $fields.find('label').width(this._labelWidth);
  2852. };
  2853. Popover.prototype.show = function($target, position) {
  2854. if (position == null) {
  2855. position = 'bottom';
  2856. }
  2857. if ($target == null) {
  2858. return;
  2859. }
  2860. this.el.siblings('.simditor-popover').each(function(i, popover) {
  2861. popover = $(popover).data('popover');
  2862. if (popover.active) {
  2863. return popover.hide();
  2864. }
  2865. });
  2866. if (this.active && this.target) {
  2867. this.target.removeClass('selected');
  2868. }
  2869. this.target = $target.addClass('selected');
  2870. if (this.active) {
  2871. this.refresh(position);
  2872. return this.trigger('popovershow');
  2873. } else {
  2874. this.active = true;
  2875. this.el.css({
  2876. left: -9999
  2877. }).show();
  2878. if (!this._labelWidth) {
  2879. this._initLabelWidth();
  2880. }
  2881. this.editor.util.reflow();
  2882. this.refresh(position);
  2883. return this.trigger('popovershow');
  2884. }
  2885. };
  2886. Popover.prototype.hide = function() {
  2887. if (!this.active) {
  2888. return;
  2889. }
  2890. if (this.target) {
  2891. this.target.removeClass('selected');
  2892. }
  2893. this.target = null;
  2894. this.active = false;
  2895. this.el.hide();
  2896. return this.trigger('popoverhide');
  2897. };
  2898. Popover.prototype.refresh = function(position) {
  2899. var editorOffset, left, maxLeft, targetH, targetOffset, top;
  2900. if (position == null) {
  2901. position = 'bottom';
  2902. }
  2903. if (!this.active) {
  2904. return;
  2905. }
  2906. editorOffset = this.editor.el.offset();
  2907. targetOffset = this.target.offset();
  2908. targetH = this.target.outerHeight();
  2909. if (position === 'bottom') {
  2910. top = targetOffset.top - editorOffset.top + targetH;
  2911. } else if (position === 'top') {
  2912. top = targetOffset.top - editorOffset.top - this.el.height();
  2913. }
  2914. maxLeft = this.editor.wrapper.width() - this.el.outerWidth() - 10;
  2915. left = Math.min(targetOffset.left - editorOffset.left, maxLeft);
  2916. return this.el.css({
  2917. top: top + this.offset.top,
  2918. left: left + this.offset.left
  2919. });
  2920. };
  2921. Popover.prototype.destroy = function() {
  2922. this.target = null;
  2923. this.active = false;
  2924. this.editor.off('.linkpopover');
  2925. return this.el.remove();
  2926. };
  2927. Popover.prototype._t = function() {
  2928. var args, ref, result;
  2929. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  2930. result = Popover.__super__._t.apply(this, args);
  2931. if (!result) {
  2932. result = (ref = this.button)._t.apply(ref, args);
  2933. }
  2934. return result;
  2935. };
  2936. return Popover;
  2937. })(SimpleModule);
  2938. Simditor.Popover = Popover;
  2939. TitleButton = (function(superClass) {
  2940. extend(TitleButton, superClass);
  2941. function TitleButton() {
  2942. return TitleButton.__super__.constructor.apply(this, arguments);
  2943. }
  2944. TitleButton.prototype.name = 'title';
  2945. TitleButton.prototype.htmlTag = 'h1, h2, h3, h4, h5';
  2946. TitleButton.prototype.disableTag = 'pre, table';
  2947. TitleButton.prototype._init = function() {
  2948. this.menu = [
  2949. {
  2950. name: 'normal',
  2951. text: this._t('normalText'),
  2952. param: 'p'
  2953. }, '|', {
  2954. name: 'h1',
  2955. text: this._t('title') + ' 1',
  2956. param: 'h1'
  2957. }, {
  2958. name: 'h2',
  2959. text: this._t('title') + ' 2',
  2960. param: 'h2'
  2961. }, {
  2962. name: 'h3',
  2963. text: this._t('title') + ' 3',
  2964. param: 'h3'
  2965. }, {
  2966. name: 'h4',
  2967. text: this._t('title') + ' 4',
  2968. param: 'h4'
  2969. }, {
  2970. name: 'h5',
  2971. text: this._t('title') + ' 5',
  2972. param: 'h5'
  2973. }
  2974. ];
  2975. return TitleButton.__super__._init.call(this);
  2976. };
  2977. TitleButton.prototype.setActive = function(active, param) {
  2978. TitleButton.__super__.setActive.call(this, active);
  2979. if (active) {
  2980. param || (param = this.node[0].tagName.toLowerCase());
  2981. }
  2982. this.el.removeClass('active-p active-h1 active-h2 active-h3 active-h4 active-h5');
  2983. if (active) {
  2984. return this.el.addClass('active active-' + param);
  2985. }
  2986. };
  2987. TitleButton.prototype.command = function(param) {
  2988. var $rootNodes;
  2989. $rootNodes = this.editor.selection.rootNodes();
  2990. this.editor.selection.save();
  2991. $rootNodes.each((function(_this) {
  2992. return function(i, node) {
  2993. var $node;
  2994. $node = $(node);
  2995. if ($node.is('blockquote') || $node.is(param) || $node.is(_this.disableTag) || _this.editor.util.isDecoratedNode($node)) {
  2996. return;
  2997. }
  2998. return $('<' + param + '/>').append($node.contents()).replaceAll($node);
  2999. };
  3000. })(this));
  3001. this.editor.selection.restore();
  3002. return this.editor.trigger('valuechanged');
  3003. };
  3004. return TitleButton;
  3005. })(Button);
  3006. Simditor.Toolbar.addButton(TitleButton);
  3007. FontScaleButton = (function(superClass) {
  3008. extend(FontScaleButton, superClass);
  3009. function FontScaleButton() {
  3010. return FontScaleButton.__super__.constructor.apply(this, arguments);
  3011. }
  3012. FontScaleButton.prototype.name = 'fontScale';
  3013. FontScaleButton.prototype.icon = 'font';
  3014. FontScaleButton.prototype.disableTag = 'pre';
  3015. FontScaleButton.prototype.htmlTag = 'span';
  3016. FontScaleButton.prototype.sizeMap = {
  3017. 'x-large': '1.5em',
  3018. 'large': '1.25em',
  3019. 'small': '.75em',
  3020. 'x-small': '.5em'
  3021. };
  3022. FontScaleButton.prototype._init = function() {
  3023. this.menu = [
  3024. {
  3025. name: '150%',
  3026. text: this._t('fontScaleXLarge'),
  3027. param: '5'
  3028. }, {
  3029. name: '125%',
  3030. text: this._t('fontScaleLarge'),
  3031. param: '4'
  3032. }, {
  3033. name: '100%',
  3034. text: this._t('fontScaleNormal'),
  3035. param: '3'
  3036. }, {
  3037. name: '75%',
  3038. text: this._t('fontScaleSmall'),
  3039. param: '2'
  3040. }, {
  3041. name: '50%',
  3042. text: this._t('fontScaleXSmall'),
  3043. param: '1'
  3044. }
  3045. ];
  3046. return FontScaleButton.__super__._init.call(this);
  3047. };
  3048. FontScaleButton.prototype._activeStatus = function() {
  3049. var active, endNode, endNodes, range, startNode, startNodes;
  3050. range = this.editor.selection.range();
  3051. startNodes = this.editor.selection.startNodes();
  3052. endNodes = this.editor.selection.endNodes();
  3053. startNode = startNodes.filter('span[style*="font-size"]');
  3054. endNode = endNodes.filter('span[style*="font-size"]');
  3055. active = startNodes.length > 0 && endNodes.length > 0 && startNode.is(endNode);
  3056. this.setActive(active);
  3057. return this.active;
  3058. };
  3059. FontScaleButton.prototype.command = function(param) {
  3060. var $scales, containerNode, range;
  3061. range = this.editor.selection.range();
  3062. if (range.collapsed) {
  3063. return;
  3064. }
  3065. document.execCommand('styleWithCSS', false, true);
  3066. document.execCommand('fontSize', false, param);
  3067. document.execCommand('styleWithCSS', false, false);
  3068. this.editor.selection.reset();
  3069. this.editor.selection.range();
  3070. containerNode = this.editor.selection.containerNode();
  3071. if (containerNode[0].nodeType === Node.TEXT_NODE) {
  3072. $scales = containerNode.closest('span[style*="font-size"]');
  3073. } else {
  3074. $scales = containerNode.find('span[style*="font-size"]');
  3075. }
  3076. $scales.each((function(_this) {
  3077. return function(i, n) {
  3078. var $span, size;
  3079. $span = $(n);
  3080. size = n.style.fontSize;
  3081. if (/large|x-large|small|x-small/.test(size)) {
  3082. return $span.css('fontSize', _this.sizeMap[size]);
  3083. } else if (size === 'medium') {
  3084. return $span.replaceWith($span.contents());
  3085. }
  3086. };
  3087. })(this));
  3088. return this.editor.trigger('valuechanged');
  3089. };
  3090. return FontScaleButton;
  3091. })(Button);
  3092. Simditor.Toolbar.addButton(FontScaleButton);
  3093. BoldButton = (function(superClass) {
  3094. extend(BoldButton, superClass);
  3095. function BoldButton() {
  3096. return BoldButton.__super__.constructor.apply(this, arguments);
  3097. }
  3098. BoldButton.prototype.name = 'bold';
  3099. BoldButton.prototype.icon = 'bold';
  3100. BoldButton.prototype.htmlTag = 'b, strong';
  3101. BoldButton.prototype.disableTag = 'pre';
  3102. BoldButton.prototype.shortcut = 'cmd+b';
  3103. BoldButton.prototype._init = function() {
  3104. if (this.editor.util.os.mac) {
  3105. this.title = this.title + ' ( Cmd + b )';
  3106. } else {
  3107. this.title = this.title + ' ( Ctrl + b )';
  3108. this.shortcut = 'ctrl+b';
  3109. }
  3110. return BoldButton.__super__._init.call(this);
  3111. };
  3112. BoldButton.prototype._activeStatus = function() {
  3113. var active;
  3114. active = document.queryCommandState('bold') === true;
  3115. this.setActive(active);
  3116. return this.active;
  3117. };
  3118. BoldButton.prototype.command = function() {
  3119. document.execCommand('bold');
  3120. if (!this.editor.util.support.oninput) {
  3121. this.editor.trigger('valuechanged');
  3122. }
  3123. return $(document).trigger('selectionchange');
  3124. };
  3125. return BoldButton;
  3126. })(Button);
  3127. Simditor.Toolbar.addButton(BoldButton);
  3128. ItalicButton = (function(superClass) {
  3129. extend(ItalicButton, superClass);
  3130. function ItalicButton() {
  3131. return ItalicButton.__super__.constructor.apply(this, arguments);
  3132. }
  3133. ItalicButton.prototype.name = 'italic';
  3134. ItalicButton.prototype.icon = 'italic';
  3135. ItalicButton.prototype.htmlTag = 'i';
  3136. ItalicButton.prototype.disableTag = 'pre';
  3137. ItalicButton.prototype.shortcut = 'cmd+i';
  3138. ItalicButton.prototype._init = function() {
  3139. if (this.editor.util.os.mac) {
  3140. this.title = this.title + " ( Cmd + i )";
  3141. } else {
  3142. this.title = this.title + " ( Ctrl + i )";
  3143. this.shortcut = 'ctrl+i';
  3144. }
  3145. return ItalicButton.__super__._init.call(this);
  3146. };
  3147. ItalicButton.prototype._activeStatus = function() {
  3148. var active;
  3149. active = document.queryCommandState('italic') === true;
  3150. this.setActive(active);
  3151. return this.active;
  3152. };
  3153. ItalicButton.prototype.command = function() {
  3154. document.execCommand('italic');
  3155. if (!this.editor.util.support.oninput) {
  3156. this.editor.trigger('valuechanged');
  3157. }
  3158. return $(document).trigger('selectionchange');
  3159. };
  3160. return ItalicButton;
  3161. })(Button);
  3162. Simditor.Toolbar.addButton(ItalicButton);
  3163. UnderlineButton = (function(superClass) {
  3164. extend(UnderlineButton, superClass);
  3165. function UnderlineButton() {
  3166. return UnderlineButton.__super__.constructor.apply(this, arguments);
  3167. }
  3168. UnderlineButton.prototype.name = 'underline';
  3169. UnderlineButton.prototype.icon = 'underline';
  3170. UnderlineButton.prototype.htmlTag = 'u';
  3171. UnderlineButton.prototype.disableTag = 'pre';
  3172. UnderlineButton.prototype.shortcut = 'cmd+u';
  3173. UnderlineButton.prototype.render = function() {
  3174. if (this.editor.util.os.mac) {
  3175. this.title = this.title + ' ( Cmd + u )';
  3176. } else {
  3177. this.title = this.title + ' ( Ctrl + u )';
  3178. this.shortcut = 'ctrl+u';
  3179. }
  3180. return UnderlineButton.__super__.render.call(this);
  3181. };
  3182. UnderlineButton.prototype._activeStatus = function() {
  3183. var active;
  3184. active = document.queryCommandState('underline') === true;
  3185. this.setActive(active);
  3186. return this.active;
  3187. };
  3188. UnderlineButton.prototype.command = function() {
  3189. document.execCommand('underline');
  3190. if (!this.editor.util.support.oninput) {
  3191. this.editor.trigger('valuechanged');
  3192. }
  3193. return $(document).trigger('selectionchange');
  3194. };
  3195. return UnderlineButton;
  3196. })(Button);
  3197. Simditor.Toolbar.addButton(UnderlineButton);
  3198. ColorButton = (function(superClass) {
  3199. extend(ColorButton, superClass);
  3200. function ColorButton() {
  3201. return ColorButton.__super__.constructor.apply(this, arguments);
  3202. }
  3203. ColorButton.prototype.name = 'color';
  3204. ColorButton.prototype.icon = 'tint';
  3205. ColorButton.prototype.disableTag = 'pre';
  3206. ColorButton.prototype.menu = true;
  3207. ColorButton.prototype.render = function() {
  3208. var args;
  3209. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3210. return ColorButton.__super__.render.apply(this, args);
  3211. };
  3212. ColorButton.prototype.renderMenu = function() {
  3213. $('<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);
  3214. this.menuWrapper.on('mousedown', '.color-list', function(e) {
  3215. return false;
  3216. });
  3217. return this.menuWrapper.on('click', '.font-color', (function(_this) {
  3218. return function(e) {
  3219. var $link, $p, hex, range, rgb, textNode;
  3220. _this.wrapper.removeClass('menu-on');
  3221. $link = $(e.currentTarget);
  3222. if ($link.hasClass('font-color-default')) {
  3223. $p = _this.editor.body.find('p, li');
  3224. if (!($p.length > 0)) {
  3225. return;
  3226. }
  3227. rgb = window.getComputedStyle($p[0], null).getPropertyValue('color');
  3228. hex = _this._convertRgbToHex(rgb);
  3229. } else {
  3230. rgb = window.getComputedStyle($link[0], null).getPropertyValue('background-color');
  3231. hex = _this._convertRgbToHex(rgb);
  3232. }
  3233. if (!hex) {
  3234. return;
  3235. }
  3236. range = _this.editor.selection.range();
  3237. if (!$link.hasClass('font-color-default') && range.collapsed) {
  3238. textNode = document.createTextNode(_this._t('coloredText'));
  3239. range.insertNode(textNode);
  3240. range.selectNodeContents(textNode);
  3241. _this.editor.selection.range(range);
  3242. }
  3243. document.execCommand('styleWithCSS', false, true);
  3244. document.execCommand('foreColor', false, hex);
  3245. document.execCommand('styleWithCSS', false, false);
  3246. if (!_this.editor.util.support.oninput) {
  3247. return _this.editor.trigger('valuechanged');
  3248. }
  3249. };
  3250. })(this));
  3251. };
  3252. ColorButton.prototype._convertRgbToHex = function(rgb) {
  3253. var match, re, rgbToHex;
  3254. re = /rgb\((\d+),\s?(\d+),\s?(\d+)\)/g;
  3255. match = re.exec(rgb);
  3256. if (!match) {
  3257. return '';
  3258. }
  3259. rgbToHex = function(r, g, b) {
  3260. var componentToHex;
  3261. componentToHex = function(c) {
  3262. var hex;
  3263. hex = c.toString(16);
  3264. if (hex.length === 1) {
  3265. return '0' + hex;
  3266. } else {
  3267. return hex;
  3268. }
  3269. };
  3270. return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
  3271. };
  3272. return rgbToHex(match[1] * 1, match[2] * 1, match[3] * 1);
  3273. };
  3274. return ColorButton;
  3275. })(Button);
  3276. Simditor.Toolbar.addButton(ColorButton);
  3277. ListButton = (function(superClass) {
  3278. extend(ListButton, superClass);
  3279. function ListButton() {
  3280. return ListButton.__super__.constructor.apply(this, arguments);
  3281. }
  3282. ListButton.prototype.type = '';
  3283. ListButton.prototype.disableTag = 'pre, table';
  3284. ListButton.prototype.command = function(param) {
  3285. var $list, $rootNodes, anotherType;
  3286. $rootNodes = this.editor.selection.blockNodes();
  3287. anotherType = this.type === 'ul' ? 'ol' : 'ul';
  3288. this.editor.selection.save();
  3289. $list = null;
  3290. $rootNodes.each((function(_this) {
  3291. return function(i, node) {
  3292. var $node;
  3293. $node = $(node);
  3294. if ($node.is('blockquote, li') || $node.is(_this.disableTag) || _this.editor.util.isDecoratedNode($node) || !$.contains(document, node)) {
  3295. return;
  3296. }
  3297. if ($node.is(_this.type)) {
  3298. $node.children('li').each(function(i, li) {
  3299. var $childList, $li;
  3300. $li = $(li);
  3301. $childList = $li.children('ul, ol').insertAfter($node);
  3302. return $('<p/>').append($(li).html() || _this.editor.util.phBr).insertBefore($node);
  3303. });
  3304. return $node.remove();
  3305. } else if ($node.is(anotherType)) {
  3306. return $('<' + _this.type + '/>').append($node.contents()).replaceAll($node);
  3307. } else if ($list && $node.prev().is($list)) {
  3308. $('<li/>').append($node.html() || _this.editor.util.phBr).appendTo($list);
  3309. return $node.remove();
  3310. } else {
  3311. $list = $("<" + _this.type + "><li></li></" + _this.type + ">");
  3312. $list.find('li').append($node.html() || _this.editor.util.phBr);
  3313. return $list.replaceAll($node);
  3314. }
  3315. };
  3316. })(this));
  3317. this.editor.selection.restore();
  3318. return this.editor.trigger('valuechanged');
  3319. };
  3320. return ListButton;
  3321. })(Button);
  3322. OrderListButton = (function(superClass) {
  3323. extend(OrderListButton, superClass);
  3324. function OrderListButton() {
  3325. return OrderListButton.__super__.constructor.apply(this, arguments);
  3326. }
  3327. OrderListButton.prototype.type = 'ol';
  3328. OrderListButton.prototype.name = 'ol';
  3329. OrderListButton.prototype.icon = 'list-ol';
  3330. OrderListButton.prototype.htmlTag = 'ol';
  3331. OrderListButton.prototype.shortcut = 'cmd+/';
  3332. OrderListButton.prototype._init = function() {
  3333. if (this.editor.util.os.mac) {
  3334. this.title = this.title + ' ( Cmd + / )';
  3335. } else {
  3336. this.title = this.title + ' ( ctrl + / )';
  3337. this.shortcut = 'ctrl+/';
  3338. }
  3339. return OrderListButton.__super__._init.call(this);
  3340. };
  3341. return OrderListButton;
  3342. })(ListButton);
  3343. UnorderListButton = (function(superClass) {
  3344. extend(UnorderListButton, superClass);
  3345. function UnorderListButton() {
  3346. return UnorderListButton.__super__.constructor.apply(this, arguments);
  3347. }
  3348. UnorderListButton.prototype.type = 'ul';
  3349. UnorderListButton.prototype.name = 'ul';
  3350. UnorderListButton.prototype.icon = 'list-ul';
  3351. UnorderListButton.prototype.htmlTag = 'ul';
  3352. UnorderListButton.prototype.shortcut = 'cmd+.';
  3353. UnorderListButton.prototype._init = function() {
  3354. if (this.editor.util.os.mac) {
  3355. this.title = this.title + ' ( Cmd + . )';
  3356. } else {
  3357. this.title = this.title + ' ( Ctrl + . )';
  3358. this.shortcut = 'ctrl+.';
  3359. }
  3360. return UnorderListButton.__super__._init.call(this);
  3361. };
  3362. return UnorderListButton;
  3363. })(ListButton);
  3364. Simditor.Toolbar.addButton(OrderListButton);
  3365. Simditor.Toolbar.addButton(UnorderListButton);
  3366. BlockquoteButton = (function(superClass) {
  3367. extend(BlockquoteButton, superClass);
  3368. function BlockquoteButton() {
  3369. return BlockquoteButton.__super__.constructor.apply(this, arguments);
  3370. }
  3371. BlockquoteButton.prototype.name = 'blockquote';
  3372. BlockquoteButton.prototype.icon = 'quote-left';
  3373. BlockquoteButton.prototype.htmlTag = 'blockquote';
  3374. BlockquoteButton.prototype.disableTag = 'pre, table';
  3375. BlockquoteButton.prototype.command = function() {
  3376. var $rootNodes, clearCache, nodeCache;
  3377. $rootNodes = this.editor.selection.rootNodes();
  3378. $rootNodes = $rootNodes.filter(function(i, node) {
  3379. return !$(node).parent().is('blockquote');
  3380. });
  3381. this.editor.selection.save();
  3382. nodeCache = [];
  3383. clearCache = (function(_this) {
  3384. return function() {
  3385. if (nodeCache.length > 0) {
  3386. $("<" + _this.htmlTag + "/>").insertBefore(nodeCache[0]).append(nodeCache);
  3387. return nodeCache.length = 0;
  3388. }
  3389. };
  3390. })(this);
  3391. $rootNodes.each((function(_this) {
  3392. return function(i, node) {
  3393. var $node;
  3394. $node = $(node);
  3395. if (!$node.parent().is(_this.editor.body)) {
  3396. return;
  3397. }
  3398. if ($node.is(_this.htmlTag)) {
  3399. clearCache();
  3400. return $node.children().unwrap();
  3401. } else if ($node.is(_this.disableTag) || _this.editor.util.isDecoratedNode($node)) {
  3402. return clearCache();
  3403. } else {
  3404. return nodeCache.push(node);
  3405. }
  3406. };
  3407. })(this));
  3408. clearCache();
  3409. this.editor.selection.restore();
  3410. return this.editor.trigger('valuechanged');
  3411. };
  3412. return BlockquoteButton;
  3413. })(Button);
  3414. Simditor.Toolbar.addButton(BlockquoteButton);
  3415. CodeButton = (function(superClass) {
  3416. extend(CodeButton, superClass);
  3417. function CodeButton() {
  3418. return CodeButton.__super__.constructor.apply(this, arguments);
  3419. }
  3420. CodeButton.prototype.name = 'code';
  3421. CodeButton.prototype.icon = 'code';
  3422. CodeButton.prototype.htmlTag = 'pre';
  3423. CodeButton.prototype.disableTag = 'ul, ol, table';
  3424. CodeButton.prototype._init = function() {
  3425. CodeButton.__super__._init.call(this);
  3426. this.editor.on('decorate', (function(_this) {
  3427. return function(e, $el) {
  3428. return $el.find('pre').each(function(i, pre) {
  3429. return _this.decorate($(pre));
  3430. });
  3431. };
  3432. })(this));
  3433. return this.editor.on('undecorate', (function(_this) {
  3434. return function(e, $el) {
  3435. return $el.find('pre').each(function(i, pre) {
  3436. return _this.undecorate($(pre));
  3437. });
  3438. };
  3439. })(this));
  3440. };
  3441. CodeButton.prototype.render = function() {
  3442. var args;
  3443. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3444. CodeButton.__super__.render.apply(this, args);
  3445. return this.popover = new CodePopover({
  3446. button: this
  3447. });
  3448. };
  3449. CodeButton.prototype._checkMode = function() {
  3450. var $blockNodes, range;
  3451. range = this.editor.selection.range();
  3452. if (($blockNodes = $(range.cloneContents()).find(this.editor.util.blockNodes.join(','))) > 0 || (range.collapsed && this.editor.selection.startNodes().filter('code').length === 0)) {
  3453. this.inlineMode = false;
  3454. return this.htmlTag = 'pre';
  3455. } else {
  3456. this.inlineMode = true;
  3457. return this.htmlTag = 'code';
  3458. }
  3459. };
  3460. CodeButton.prototype._status = function() {
  3461. this._checkMode();
  3462. CodeButton.__super__._status.call(this);
  3463. if (this.inlineMode) {
  3464. return;
  3465. }
  3466. if (this.active) {
  3467. return this.popover.show(this.node);
  3468. } else {
  3469. return this.popover.hide();
  3470. }
  3471. };
  3472. CodeButton.prototype.decorate = function($pre) {
  3473. var $code, lang, ref, ref1;
  3474. $code = $pre.find('> code');
  3475. if ($code.length > 0) {
  3476. lang = (ref = $code.attr('class')) != null ? (ref1 = ref.match(/lang-(\S+)/)) != null ? ref1[1] : void 0 : void 0;
  3477. $code.contents().unwrap();
  3478. if (lang) {
  3479. return $pre.attr('data-lang', lang);
  3480. }
  3481. }
  3482. };
  3483. CodeButton.prototype.undecorate = function($pre) {
  3484. var $code, lang;
  3485. lang = $pre.attr('data-lang');
  3486. $code = $('<code/>');
  3487. if (lang && lang !== -1) {
  3488. $code.addClass('lang-' + lang);
  3489. }
  3490. return $pre.wrapInner($code).removeAttr('data-lang');
  3491. };
  3492. CodeButton.prototype.command = function() {
  3493. if (this.inlineMode) {
  3494. return this._inlineCommand();
  3495. } else {
  3496. return this._blockCommand();
  3497. }
  3498. };
  3499. CodeButton.prototype._blockCommand = function() {
  3500. var $rootNodes, clearCache, nodeCache, resultNodes;
  3501. $rootNodes = this.editor.selection.rootNodes();
  3502. nodeCache = [];
  3503. resultNodes = [];
  3504. clearCache = (function(_this) {
  3505. return function() {
  3506. var $pre;
  3507. if (!(nodeCache.length > 0)) {
  3508. return;
  3509. }
  3510. $pre = $("<" + _this.htmlTag + "/>").insertBefore(nodeCache[0]).text(_this.editor.formatter.clearHtml(nodeCache));
  3511. resultNodes.push($pre[0]);
  3512. return nodeCache.length = 0;
  3513. };
  3514. })(this);
  3515. $rootNodes.each((function(_this) {
  3516. return function(i, node) {
  3517. var $node, $p;
  3518. $node = $(node);
  3519. if ($node.is(_this.htmlTag)) {
  3520. clearCache();
  3521. $p = $('<p/>').append($node.html().replace('\n', '<br/>')).replaceAll($node);
  3522. return resultNodes.push($p[0]);
  3523. } else if ($node.is(_this.disableTag) || _this.editor.util.isDecoratedNode($node) || $node.is('blockquote')) {
  3524. return clearCache();
  3525. } else {
  3526. return nodeCache.push(node);
  3527. }
  3528. };
  3529. })(this));
  3530. clearCache();
  3531. this.editor.selection.setRangeAtEndOf($(resultNodes).last());
  3532. return this.editor.trigger('valuechanged');
  3533. };
  3534. CodeButton.prototype._inlineCommand = function() {
  3535. var $code, $contents, range;
  3536. range = this.editor.selection.range();
  3537. if (this.active) {
  3538. range.selectNodeContents(this.node[0]);
  3539. this.editor.selection.save(range);
  3540. this.node.contents().unwrap();
  3541. this.editor.selection.restore();
  3542. } else {
  3543. $contents = $(range.extractContents());
  3544. $code = $("<" + this.htmlTag + "/>").append($contents.contents());
  3545. range.insertNode($code[0]);
  3546. range.selectNodeContents($code[0]);
  3547. this.editor.selection.range(range);
  3548. }
  3549. return this.editor.trigger('valuechanged');
  3550. };
  3551. return CodeButton;
  3552. })(Button);
  3553. CodePopover = (function(superClass) {
  3554. extend(CodePopover, superClass);
  3555. function CodePopover() {
  3556. return CodePopover.__super__.constructor.apply(this, arguments);
  3557. }
  3558. CodePopover.prototype.render = function() {
  3559. var $option, k, lang, len, ref;
  3560. 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>";
  3561. this.langs = this.editor.opts.codeLanguages || [
  3562. {
  3563. name: 'Bash',
  3564. value: 'bash'
  3565. }, {
  3566. name: 'C++',
  3567. value: 'c++'
  3568. }, {
  3569. name: 'C#',
  3570. value: 'cs'
  3571. }, {
  3572. name: 'CSS',
  3573. value: 'css'
  3574. }, {
  3575. name: 'Erlang',
  3576. value: 'erlang'
  3577. }, {
  3578. name: 'Less',
  3579. value: 'less'
  3580. }, {
  3581. name: 'Sass',
  3582. value: 'sass'
  3583. }, {
  3584. name: 'Diff',
  3585. value: 'diff'
  3586. }, {
  3587. name: 'CoffeeScript',
  3588. value: 'coffeescript'
  3589. }, {
  3590. name: 'HTML,XML',
  3591. value: 'html'
  3592. }, {
  3593. name: 'JSON',
  3594. value: 'json'
  3595. }, {
  3596. name: 'Java',
  3597. value: 'java'
  3598. }, {
  3599. name: 'JavaScript',
  3600. value: 'js'
  3601. }, {
  3602. name: 'Markdown',
  3603. value: 'markdown'
  3604. }, {
  3605. name: 'Objective C',
  3606. value: 'oc'
  3607. }, {
  3608. name: 'PHP',
  3609. value: 'php'
  3610. }, {
  3611. name: 'Perl',
  3612. value: 'parl'
  3613. }, {
  3614. name: 'Python',
  3615. value: 'python'
  3616. }, {
  3617. name: 'Ruby',
  3618. value: 'ruby'
  3619. }, {
  3620. name: 'SQL',
  3621. value: 'sql'
  3622. }
  3623. ];
  3624. this.el.addClass('code-popover').append(this._tpl);
  3625. this.selectEl = this.el.find('.select-lang');
  3626. ref = this.langs;
  3627. for (k = 0, len = ref.length; k < len; k++) {
  3628. lang = ref[k];
  3629. $option = $('<option/>', {
  3630. text: lang.name,
  3631. value: lang.value
  3632. }).appendTo(this.selectEl);
  3633. }
  3634. this.selectEl.on('change', (function(_this) {
  3635. return function(e) {
  3636. var selected;
  3637. _this.lang = _this.selectEl.val();
  3638. selected = _this.target.hasClass('selected');
  3639. _this.target.removeClass().removeAttr('data-lang');
  3640. if (_this.lang !== -1) {
  3641. _this.target.attr('data-lang', _this.lang);
  3642. }
  3643. if (selected) {
  3644. _this.target.addClass('selected');
  3645. }
  3646. return _this.editor.trigger('valuechanged');
  3647. };
  3648. })(this));
  3649. return this.editor.on('valuechanged', (function(_this) {
  3650. return function(e) {
  3651. if (_this.active) {
  3652. return _this.refresh();
  3653. }
  3654. };
  3655. })(this));
  3656. };
  3657. CodePopover.prototype.show = function() {
  3658. var args;
  3659. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3660. CodePopover.__super__.show.apply(this, args);
  3661. this.lang = this.target.attr('data-lang');
  3662. if (this.lang != null) {
  3663. return this.selectEl.val(this.lang);
  3664. } else {
  3665. return this.selectEl.val(-1);
  3666. }
  3667. };
  3668. return CodePopover;
  3669. })(Popover);
  3670. Simditor.Toolbar.addButton(CodeButton);
  3671. LinkButton = (function(superClass) {
  3672. extend(LinkButton, superClass);
  3673. function LinkButton() {
  3674. return LinkButton.__super__.constructor.apply(this, arguments);
  3675. }
  3676. LinkButton.prototype.name = 'link';
  3677. LinkButton.prototype.icon = 'link';
  3678. LinkButton.prototype.htmlTag = 'a';
  3679. LinkButton.prototype.disableTag = 'pre';
  3680. LinkButton.prototype.render = function() {
  3681. var args;
  3682. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3683. LinkButton.__super__.render.apply(this, args);
  3684. return this.popover = new LinkPopover({
  3685. button: this
  3686. });
  3687. };
  3688. LinkButton.prototype._status = function() {
  3689. LinkButton.__super__._status.call(this);
  3690. if (this.active && !this.editor.selection.rangeAtEndOf(this.node)) {
  3691. return this.popover.show(this.node);
  3692. } else {
  3693. return this.popover.hide();
  3694. }
  3695. };
  3696. LinkButton.prototype.command = function() {
  3697. var $contents, $link, $newBlock, linkText, range, txtNode;
  3698. range = this.editor.selection.range();
  3699. if (this.active) {
  3700. txtNode = document.createTextNode(this.node.text());
  3701. this.node.replaceWith(txtNode);
  3702. range.selectNode(txtNode);
  3703. } else {
  3704. $contents = $(range.extractContents());
  3705. linkText = this.editor.formatter.clearHtml($contents.contents(), false);
  3706. $link = $('<a/>', {
  3707. href: 'http://www.example.com',
  3708. target: '_blank',
  3709. text: linkText || this._t('linkText')
  3710. });
  3711. if (this.editor.selection.blockNodes().length > 0) {
  3712. range.insertNode($link[0]);
  3713. } else {
  3714. $newBlock = $('<p/>').append($link);
  3715. range.insertNode($newBlock[0]);
  3716. }
  3717. range.selectNodeContents($link[0]);
  3718. this.popover.one('popovershow', (function(_this) {
  3719. return function() {
  3720. if (linkText) {
  3721. _this.popover.urlEl.focus();
  3722. return _this.popover.urlEl[0].select();
  3723. } else {
  3724. _this.popover.textEl.focus();
  3725. return _this.popover.textEl[0].select();
  3726. }
  3727. };
  3728. })(this));
  3729. }
  3730. this.editor.selection.range(range);
  3731. return this.editor.trigger('valuechanged');
  3732. };
  3733. return LinkButton;
  3734. })(Button);
  3735. LinkPopover = (function(superClass) {
  3736. extend(LinkPopover, superClass);
  3737. function LinkPopover() {
  3738. return LinkPopover.__super__.constructor.apply(this, arguments);
  3739. }
  3740. LinkPopover.prototype.render = function() {
  3741. var tpl;
  3742. 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>";
  3743. this.el.addClass('link-popover').append(tpl);
  3744. this.textEl = this.el.find('.link-text');
  3745. this.urlEl = this.el.find('.link-url');
  3746. this.unlinkEl = this.el.find('.btn-unlink');
  3747. this.selectTarget = this.el.find('.link-target');
  3748. this.textEl.on('keyup', (function(_this) {
  3749. return function(e) {
  3750. if (e.which === 13) {
  3751. return;
  3752. }
  3753. _this.target.text(_this.textEl.val());
  3754. return _this.editor.inputManager.throttledValueChanged();
  3755. };
  3756. })(this));
  3757. this.urlEl.on('keyup', (function(_this) {
  3758. return function(e) {
  3759. var val;
  3760. if (e.which === 13) {
  3761. return;
  3762. }
  3763. val = _this.urlEl.val();
  3764. if (!(/https?:\/\/|^\//ig.test(val) || !val)) {
  3765. val = 'http://' + val;
  3766. }
  3767. _this.target.attr('href', val);
  3768. return _this.editor.inputManager.throttledValueChanged();
  3769. };
  3770. })(this));
  3771. $([this.urlEl[0], this.textEl[0]]).on('keydown', (function(_this) {
  3772. return function(e) {
  3773. var range;
  3774. if (e.which === 13 || e.which === 27 || (!e.shiftKey && e.which === 9 && $(e.target).hasClass('link-url'))) {
  3775. e.preventDefault();
  3776. range = document.createRange();
  3777. _this.editor.selection.setRangeAfter(_this.target, range);
  3778. _this.hide();
  3779. return _this.editor.inputManager.throttledValueChanged();
  3780. }
  3781. };
  3782. })(this));
  3783. this.unlinkEl.on('click', (function(_this) {
  3784. return function(e) {
  3785. var range, txtNode;
  3786. txtNode = document.createTextNode(_this.target.text());
  3787. _this.target.replaceWith(txtNode);
  3788. _this.hide();
  3789. range = document.createRange();
  3790. _this.editor.selection.setRangeAfter(txtNode, range);
  3791. return _this.editor.inputManager.throttledValueChanged();
  3792. };
  3793. })(this));
  3794. return this.selectTarget.on('change', (function(_this) {
  3795. return function(e) {
  3796. _this.target.attr('target', _this.selectTarget.val());
  3797. return _this.editor.inputManager.throttledValueChanged();
  3798. };
  3799. })(this));
  3800. };
  3801. LinkPopover.prototype.show = function() {
  3802. var args;
  3803. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3804. LinkPopover.__super__.show.apply(this, args);
  3805. this.textEl.val(this.target.text());
  3806. return this.urlEl.val(this.target.attr('href'));
  3807. };
  3808. return LinkPopover;
  3809. })(Popover);
  3810. Simditor.Toolbar.addButton(LinkButton);
  3811. ImageButton = (function(superClass) {
  3812. extend(ImageButton, superClass);
  3813. function ImageButton() {
  3814. return ImageButton.__super__.constructor.apply(this, arguments);
  3815. }
  3816. ImageButton.prototype.name = 'image';
  3817. ImageButton.prototype.icon = 'picture-o';
  3818. ImageButton.prototype.htmlTag = 'img';
  3819. ImageButton.prototype.disableTag = 'pre, table';
  3820. ImageButton.prototype.defaultImage = '';
  3821. ImageButton.prototype.needFocus = false;
  3822. ImageButton.prototype._init = function() {
  3823. var item, k, len, ref;
  3824. if (this.editor.opts.imageButton) {
  3825. if (Array.isArray(this.editor.opts.imageButton)) {
  3826. this.menu = [];
  3827. ref = this.editor.opts.imageButton;
  3828. for (k = 0, len = ref.length; k < len; k++) {
  3829. item = ref[k];
  3830. this.menu.push({
  3831. name: item + '-image',
  3832. text: this._t(item + 'Image')
  3833. });
  3834. }
  3835. } else {
  3836. this.menu = false;
  3837. }
  3838. } else {
  3839. if (this.editor.uploader != null) {
  3840. this.menu = [
  3841. {
  3842. name: 'upload-image',
  3843. text: this._t('uploadImage')
  3844. }, {
  3845. name: 'external-image',
  3846. text: this._t('externalImage')
  3847. }
  3848. ];
  3849. } else {
  3850. this.menu = false;
  3851. }
  3852. }
  3853. this.defaultImage = this.editor.opts.defaultImage;
  3854. this.editor.body.on('click', 'img:not([data-non-image])', (function(_this) {
  3855. return function(e) {
  3856. var $img, range;
  3857. $img = $(e.currentTarget);
  3858. range = document.createRange();
  3859. range.selectNode($img[0]);
  3860. _this.editor.selection.range(range);
  3861. if (!_this.editor.util.support.onselectionchange) {
  3862. _this.editor.trigger('selectionchanged');
  3863. }
  3864. return false;
  3865. };
  3866. })(this));
  3867. this.editor.body.on('mouseup', 'img:not([data-non-image])', function(e) {
  3868. return false;
  3869. });
  3870. this.editor.on('selectionchanged.image', (function(_this) {
  3871. return function() {
  3872. var $contents, $img, range;
  3873. range = _this.editor.selection.range();
  3874. if (range == null) {
  3875. return;
  3876. }
  3877. $contents = $(range.cloneContents()).contents();
  3878. if ($contents.length === 1 && $contents.is('img:not([data-non-image])')) {
  3879. $img = $(range.startContainer).contents().eq(range.startOffset);
  3880. return _this.popover.show($img);
  3881. } else {
  3882. return _this.popover.hide();
  3883. }
  3884. };
  3885. })(this));
  3886. this.editor.on('valuechanged.image', (function(_this) {
  3887. return function() {
  3888. var $masks;
  3889. $masks = _this.editor.wrapper.find('.simditor-image-loading');
  3890. if (!($masks.length > 0)) {
  3891. return;
  3892. }
  3893. return $masks.each(function(i, mask) {
  3894. var $img, $mask, file;
  3895. $mask = $(mask);
  3896. $img = $mask.data('img');
  3897. if (!($img && $img.parent().length > 0)) {
  3898. $mask.remove();
  3899. if ($img) {
  3900. file = $img.data('file');
  3901. if (file) {
  3902. _this.editor.uploader.cancel(file);
  3903. if (_this.editor.body.find('img.uploading').length < 1) {
  3904. return _this.editor.uploader.trigger('uploadready', [file]);
  3905. }
  3906. }
  3907. }
  3908. }
  3909. });
  3910. };
  3911. })(this));
  3912. return ImageButton.__super__._init.call(this);
  3913. };
  3914. ImageButton.prototype.render = function() {
  3915. var args;
  3916. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  3917. ImageButton.__super__.render.apply(this, args);
  3918. this.popover = new ImagePopover({
  3919. button: this
  3920. });
  3921. if (this.editor.opts.imageButton === 'upload') {
  3922. return this._initUploader(this.el);
  3923. }
  3924. };
  3925. ImageButton.prototype.renderMenu = function() {
  3926. ImageButton.__super__.renderMenu.call(this);
  3927. return this._initUploader();
  3928. };
  3929. ImageButton.prototype._initUploader = function($uploadItem) {
  3930. var $input, createInput, uploadProgress;
  3931. if ($uploadItem == null) {
  3932. $uploadItem = this.menuEl.find('.menu-item-upload-image');
  3933. }
  3934. if (this.editor.uploader == null) {
  3935. this.el.find('.btn-upload').remove();
  3936. return;
  3937. }
  3938. $input = null;
  3939. createInput = (function(_this) {
  3940. return function() {
  3941. if ($input) {
  3942. $input.remove();
  3943. }
  3944. return $input = $('<input/>', {
  3945. type: 'file',
  3946. title: _this._t('uploadImage'),
  3947. multiple: true,
  3948. accept: 'image/*'
  3949. }).appendTo($uploadItem);
  3950. };
  3951. })(this);
  3952. createInput();
  3953. $uploadItem.on('click mousedown', 'input[type=file]', function(e) {
  3954. return e.stopPropagation();
  3955. });
  3956. $uploadItem.on('change', 'input[type=file]', (function(_this) {
  3957. return function(e) {
  3958. if (_this.editor.inputManager.focused) {
  3959. _this.editor.uploader.upload($input, {
  3960. inline: true
  3961. });
  3962. createInput();
  3963. } else {
  3964. _this.editor.one('focus', function(e) {
  3965. _this.editor.uploader.upload($input, {
  3966. inline: true
  3967. });
  3968. return createInput();
  3969. });
  3970. _this.editor.focus();
  3971. }
  3972. return _this.wrapper.removeClass('menu-on');
  3973. };
  3974. })(this));
  3975. this.editor.uploader.on('beforeupload', (function(_this) {
  3976. return function(e, file) {
  3977. var $img;
  3978. if (!file.inline) {
  3979. return;
  3980. }
  3981. if (file.img) {
  3982. $img = $(file.img);
  3983. } else {
  3984. $img = _this.createImage(file.name);
  3985. file.img = $img;
  3986. }
  3987. $img.addClass('uploading');
  3988. $img.data('file', file);
  3989. return _this.editor.uploader.readImageFile(file.obj, function(img) {
  3990. var src;
  3991. if (!$img.hasClass('uploading')) {
  3992. return;
  3993. }
  3994. src = img ? img.src : _this.defaultImage;
  3995. return _this.loadImage($img, src, function() {
  3996. if (_this.popover.active) {
  3997. _this.popover.refresh();
  3998. return _this.popover.srcEl.val(_this._t('uploading')).prop('disabled', true);
  3999. }
  4000. });
  4001. });
  4002. };
  4003. })(this));
  4004. uploadProgress = $.proxy(this.editor.util.throttle(function(e, file, loaded, total) {
  4005. var $img, $mask, percent;
  4006. if (!file.inline) {
  4007. return;
  4008. }
  4009. $mask = file.img.data('mask');
  4010. if (!$mask) {
  4011. return;
  4012. }
  4013. $img = $mask.data('img');
  4014. if (!($img.hasClass('uploading') && $img.parent().length > 0)) {
  4015. $mask.remove();
  4016. return;
  4017. }
  4018. percent = loaded / total;
  4019. percent = (percent * 100).toFixed(0);
  4020. if (percent > 99) {
  4021. percent = 99;
  4022. }
  4023. return $mask.find('.progress').height((100 - percent) + "%");
  4024. }, 500), this);
  4025. this.editor.uploader.on('uploadprogress', uploadProgress);
  4026. this.editor.uploader.on('uploadsuccess', (function(_this) {
  4027. return function(e, file, result) {
  4028. var $img, img_path, msg;
  4029. if (!file.inline) {
  4030. return;
  4031. }
  4032. $img = file.img;
  4033. if (!($img.hasClass('uploading') && $img.parent().length > 0)) {
  4034. return;
  4035. }
  4036. if (typeof result !== 'object') {
  4037. try {
  4038. result = $.parseJSON(result);
  4039. } catch (_error) {
  4040. e = _error;
  4041. result = {
  4042. success: false
  4043. };
  4044. }
  4045. }
  4046. if (result.success === false) {
  4047. msg = result.msg || _this._t('uploadFailed');
  4048. alert(msg);
  4049. img_path = _this.defaultImage;
  4050. } else {
  4051. img_path = result.file_path;
  4052. }
  4053. _this.loadImage($img, img_path, function() {
  4054. var $mask;
  4055. $img.removeData('file');
  4056. $img.removeClass('uploading').removeClass('loading');
  4057. $mask = $img.data('mask');
  4058. if ($mask) {
  4059. $mask.remove();
  4060. }
  4061. $img.removeData('mask');
  4062. _this.editor.trigger('valuechanged');
  4063. if (_this.editor.body.find('img.uploading').length < 1) {
  4064. return _this.editor.uploader.trigger('uploadready', [file, result]);
  4065. }
  4066. });
  4067. if (_this.popover.active) {
  4068. _this.popover.srcEl.prop('disabled', false);
  4069. return _this.popover.srcEl.val(result.file_path);
  4070. }
  4071. };
  4072. })(this));
  4073. return this.editor.uploader.on('uploaderror', (function(_this) {
  4074. return function(e, file, xhr) {
  4075. var $img, msg, result;
  4076. if (!file.inline) {
  4077. return;
  4078. }
  4079. if (xhr.statusText === 'abort') {
  4080. return;
  4081. }
  4082. if (xhr.responseText) {
  4083. try {
  4084. result = $.parseJSON(xhr.responseText);
  4085. msg = result.msg;
  4086. } catch (_error) {
  4087. e = _error;
  4088. msg = _this._t('uploadError');
  4089. }
  4090. alert(msg);
  4091. }
  4092. $img = file.img;
  4093. if (!($img.hasClass('uploading') && $img.parent().length > 0)) {
  4094. return;
  4095. }
  4096. _this.loadImage($img, _this.defaultImage, function() {
  4097. var $mask;
  4098. $img.removeData('file');
  4099. $img.removeClass('uploading').removeClass('loading');
  4100. $mask = $img.data('mask');
  4101. if ($mask) {
  4102. $mask.remove();
  4103. }
  4104. return $img.removeData('mask');
  4105. });
  4106. if (_this.popover.active) {
  4107. _this.popover.srcEl.prop('disabled', false);
  4108. _this.popover.srcEl.val(_this.defaultImage);
  4109. }
  4110. _this.editor.trigger('valuechanged');
  4111. if (_this.editor.body.find('img.uploading').length < 1) {
  4112. return _this.editor.uploader.trigger('uploadready', [file, result]);
  4113. }
  4114. };
  4115. })(this));
  4116. };
  4117. ImageButton.prototype._status = function() {
  4118. return this._disableStatus();
  4119. };
  4120. ImageButton.prototype.loadImage = function($img, src, callback) {
  4121. var $mask, img, positionMask;
  4122. positionMask = (function(_this) {
  4123. return function() {
  4124. var imgOffset, wrapperOffset;
  4125. imgOffset = $img.offset();
  4126. wrapperOffset = _this.editor.wrapper.offset();
  4127. return $mask.css({
  4128. top: imgOffset.top - wrapperOffset.top,
  4129. left: imgOffset.left - wrapperOffset.left,
  4130. width: $img.width(),
  4131. height: $img.height()
  4132. }).show();
  4133. };
  4134. })(this);
  4135. $img.addClass('loading');
  4136. $mask = $img.data('mask');
  4137. if (!$mask) {
  4138. $mask = $('<div class="simditor-image-loading">\n <div class="progress"></div>\n</div>').hide().appendTo(this.editor.wrapper);
  4139. positionMask();
  4140. $img.data('mask', $mask);
  4141. $mask.data('img', $img);
  4142. }
  4143. img = new Image();
  4144. img.onload = (function(_this) {
  4145. return function() {
  4146. var height, width;
  4147. if (!$img.hasClass('loading') && !$img.hasClass('uploading')) {
  4148. return;
  4149. }
  4150. width = img.width;
  4151. height = img.height;
  4152. $img.attr({
  4153. src: src,
  4154. width: width,
  4155. height: height,
  4156. 'data-image-size': width + ',' + height
  4157. }).removeClass('loading');
  4158. if ($img.hasClass('uploading')) {
  4159. _this.editor.util.reflow(_this.editor.body);
  4160. positionMask();
  4161. } else {
  4162. $mask.remove();
  4163. $img.removeData('mask');
  4164. }
  4165. if ($.isFunction(callback)) {
  4166. return callback(img);
  4167. }
  4168. };
  4169. })(this);
  4170. img.onerror = function() {
  4171. if ($.isFunction(callback)) {
  4172. callback(false);
  4173. }
  4174. $mask.remove();
  4175. return $img.removeData('mask').removeClass('loading');
  4176. };
  4177. return img.src = src;
  4178. };
  4179. ImageButton.prototype.createImage = function(name) {
  4180. var $img, range;
  4181. if (name == null) {
  4182. name = 'Image';
  4183. }
  4184. if (!this.editor.inputManager.focused) {
  4185. this.editor.focus();
  4186. }
  4187. range = this.editor.selection.range();
  4188. range.deleteContents();
  4189. this.editor.selection.range(range);
  4190. $img = $('<img/>').attr('alt', name);
  4191. range.insertNode($img[0]);
  4192. this.editor.selection.setRangeAfter($img, range);
  4193. this.editor.trigger('valuechanged');
  4194. return $img;
  4195. };
  4196. ImageButton.prototype.command = function(src) {
  4197. var $img;
  4198. $img = this.createImage();
  4199. return this.loadImage($img, src || this.defaultImage, (function(_this) {
  4200. return function() {
  4201. _this.editor.trigger('valuechanged');
  4202. _this.editor.util.reflow($img);
  4203. $img.click();
  4204. return _this.popover.one('popovershow', function() {
  4205. _this.popover.srcEl.focus();
  4206. return _this.popover.srcEl[0].select();
  4207. });
  4208. };
  4209. })(this));
  4210. };
  4211. return ImageButton;
  4212. })(Button);
  4213. ImagePopover = (function(superClass) {
  4214. extend(ImagePopover, superClass);
  4215. function ImagePopover() {
  4216. return ImagePopover.__super__.constructor.apply(this, arguments);
  4217. }
  4218. ImagePopover.prototype.offset = {
  4219. top: 6,
  4220. left: -4
  4221. };
  4222. ImagePopover.prototype.render = function() {
  4223. var tpl;
  4224. 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>";
  4225. this.el.addClass('image-popover').append(tpl);
  4226. this.srcEl = this.el.find('.image-src');
  4227. this.widthEl = this.el.find('#image-width');
  4228. this.heightEl = this.el.find('#image-height');
  4229. this.altEl = this.el.find('#image-alt');
  4230. this.srcEl.on('keydown', (function(_this) {
  4231. return function(e) {
  4232. var range;
  4233. if (!(e.which === 13 && !_this.target.hasClass('uploading'))) {
  4234. return;
  4235. }
  4236. e.preventDefault();
  4237. range = document.createRange();
  4238. _this.button.editor.selection.setRangeAfter(_this.target, range);
  4239. return _this.hide();
  4240. };
  4241. })(this));
  4242. this.srcEl.on('blur', (function(_this) {
  4243. return function(e) {
  4244. return _this._loadImage(_this.srcEl.val());
  4245. };
  4246. })(this));
  4247. this.el.find('.image-size').on('blur', (function(_this) {
  4248. return function(e) {
  4249. _this._resizeImg($(e.currentTarget));
  4250. return _this.el.data('popover').refresh();
  4251. };
  4252. })(this));
  4253. this.el.find('.image-size').on('keyup', (function(_this) {
  4254. return function(e) {
  4255. var inputEl;
  4256. inputEl = $(e.currentTarget);
  4257. if (!(e.which === 13 || e.which === 27 || e.which === 9)) {
  4258. return _this._resizeImg(inputEl, true);
  4259. }
  4260. };
  4261. })(this));
  4262. this.el.find('.image-size').on('keydown', (function(_this) {
  4263. return function(e) {
  4264. var $img, inputEl, range;
  4265. inputEl = $(e.currentTarget);
  4266. if (e.which === 13 || e.which === 27) {
  4267. e.preventDefault();
  4268. if (e.which === 13) {
  4269. _this._resizeImg(inputEl);
  4270. } else {
  4271. _this._restoreImg();
  4272. }
  4273. $img = _this.target;
  4274. _this.hide();
  4275. range = document.createRange();
  4276. return _this.button.editor.selection.setRangeAfter($img, range);
  4277. } else if (e.which === 9) {
  4278. return _this.el.data('popover').refresh();
  4279. }
  4280. };
  4281. })(this));
  4282. this.altEl.on('keydown', (function(_this) {
  4283. return function(e) {
  4284. var range;
  4285. if (e.which === 13) {
  4286. e.preventDefault();
  4287. range = document.createRange();
  4288. _this.button.editor.selection.setRangeAfter(_this.target, range);
  4289. return _this.hide();
  4290. }
  4291. };
  4292. })(this));
  4293. this.altEl.on('keyup', (function(_this) {
  4294. return function(e) {
  4295. if (e.which === 13 || e.which === 27 || e.which === 9) {
  4296. return;
  4297. }
  4298. _this.alt = _this.altEl.val();
  4299. return _this.target.attr('alt', _this.alt);
  4300. };
  4301. })(this));
  4302. this.el.find('.btn-restore').on('click', (function(_this) {
  4303. return function(e) {
  4304. _this._restoreImg();
  4305. return _this.el.data('popover').refresh();
  4306. };
  4307. })(this));
  4308. this.editor.on('valuechanged', (function(_this) {
  4309. return function(e) {
  4310. if (_this.active) {
  4311. return _this.refresh();
  4312. }
  4313. };
  4314. })(this));
  4315. return this._initUploader();
  4316. };
  4317. ImagePopover.prototype._initUploader = function() {
  4318. var $uploadBtn, createInput;
  4319. $uploadBtn = this.el.find('.btn-upload');
  4320. if (this.editor.uploader == null) {
  4321. $uploadBtn.remove();
  4322. return;
  4323. }
  4324. createInput = (function(_this) {
  4325. return function() {
  4326. if (_this.input) {
  4327. _this.input.remove();
  4328. }
  4329. return _this.input = $('<input/>', {
  4330. type: 'file',
  4331. title: _this._t('uploadImage'),
  4332. multiple: true,
  4333. accept: 'image/*'
  4334. }).appendTo($uploadBtn);
  4335. };
  4336. })(this);
  4337. createInput();
  4338. this.el.on('click mousedown', 'input[type=file]', function(e) {
  4339. return e.stopPropagation();
  4340. });
  4341. return this.el.on('change', 'input[type=file]', (function(_this) {
  4342. return function(e) {
  4343. _this.editor.uploader.upload(_this.input, {
  4344. inline: true,
  4345. img: _this.target
  4346. });
  4347. return createInput();
  4348. };
  4349. })(this));
  4350. };
  4351. ImagePopover.prototype._resizeImg = function(inputEl, onlySetVal) {
  4352. var height, value, width;
  4353. if (onlySetVal == null) {
  4354. onlySetVal = false;
  4355. }
  4356. value = inputEl.val() * 1;
  4357. if (!(this.target && ($.isNumeric(value) || value < 0))) {
  4358. return;
  4359. }
  4360. if (inputEl.is(this.widthEl)) {
  4361. width = value;
  4362. height = this.height * value / this.width;
  4363. this.heightEl.val(height);
  4364. } else {
  4365. height = value;
  4366. width = this.width * value / this.height;
  4367. this.widthEl.val(width);
  4368. }
  4369. if (!onlySetVal) {
  4370. this.target.attr({
  4371. width: width,
  4372. height: height
  4373. });
  4374. return this.editor.trigger('valuechanged');
  4375. }
  4376. };
  4377. ImagePopover.prototype._restoreImg = function() {
  4378. var ref, size;
  4379. size = ((ref = this.target.data('image-size')) != null ? ref.split(",") : void 0) || [this.width, this.height];
  4380. this.target.attr({
  4381. width: size[0] * 1,
  4382. height: size[1] * 1
  4383. });
  4384. this.widthEl.val(size[0]);
  4385. this.heightEl.val(size[1]);
  4386. return this.editor.trigger('valuechanged');
  4387. };
  4388. ImagePopover.prototype._loadImage = function(src, callback) {
  4389. if (/^data:image/.test(src) && !this.editor.uploader) {
  4390. if (callback) {
  4391. callback(false);
  4392. }
  4393. return;
  4394. }
  4395. if (this.target.attr('src') === src) {
  4396. return;
  4397. }
  4398. return this.button.loadImage(this.target, src, (function(_this) {
  4399. return function(img) {
  4400. var blob;
  4401. if (!img) {
  4402. return;
  4403. }
  4404. if (_this.active) {
  4405. _this.width = img.width;
  4406. _this.height = img.height;
  4407. _this.widthEl.val(_this.width);
  4408. _this.heightEl.val(_this.height);
  4409. }
  4410. if (/^data:image/.test(src)) {
  4411. blob = _this.editor.util.dataURLtoBlob(src);
  4412. blob.name = "Base64 Image.png";
  4413. _this.editor.uploader.upload(blob, {
  4414. inline: true,
  4415. img: _this.target
  4416. });
  4417. } else {
  4418. _this.editor.trigger('valuechanged');
  4419. }
  4420. if (callback) {
  4421. return callback(img);
  4422. }
  4423. };
  4424. })(this));
  4425. };
  4426. ImagePopover.prototype.show = function() {
  4427. var $img, args;
  4428. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  4429. ImagePopover.__super__.show.apply(this, args);
  4430. $img = this.target;
  4431. this.width = $img.width();
  4432. this.height = $img.height();
  4433. this.alt = $img.attr('alt');
  4434. if ($img.hasClass('uploading')) {
  4435. return this.srcEl.val(this._t('uploading')).prop('disabled', true);
  4436. } else {
  4437. this.srcEl.val($img.attr('src')).prop('disabled', false);
  4438. this.widthEl.val(this.width);
  4439. this.heightEl.val(this.height);
  4440. return this.altEl.val(this.alt);
  4441. }
  4442. };
  4443. return ImagePopover;
  4444. })(Popover);
  4445. Simditor.Toolbar.addButton(ImageButton);
  4446. IndentButton = (function(superClass) {
  4447. extend(IndentButton, superClass);
  4448. function IndentButton() {
  4449. return IndentButton.__super__.constructor.apply(this, arguments);
  4450. }
  4451. IndentButton.prototype.name = 'indent';
  4452. IndentButton.prototype.icon = 'indent';
  4453. IndentButton.prototype._init = function() {
  4454. this.title = this._t(this.name) + ' (Tab)';
  4455. return IndentButton.__super__._init.call(this);
  4456. };
  4457. IndentButton.prototype._status = function() {};
  4458. IndentButton.prototype.command = function() {
  4459. return this.editor.indentation.indent();
  4460. };
  4461. return IndentButton;
  4462. })(Button);
  4463. Simditor.Toolbar.addButton(IndentButton);
  4464. OutdentButton = (function(superClass) {
  4465. extend(OutdentButton, superClass);
  4466. function OutdentButton() {
  4467. return OutdentButton.__super__.constructor.apply(this, arguments);
  4468. }
  4469. OutdentButton.prototype.name = 'outdent';
  4470. OutdentButton.prototype.icon = 'outdent';
  4471. OutdentButton.prototype._init = function() {
  4472. this.title = this._t(this.name) + ' (Shift + Tab)';
  4473. return OutdentButton.__super__._init.call(this);
  4474. };
  4475. OutdentButton.prototype._status = function() {};
  4476. OutdentButton.prototype.command = function() {
  4477. return this.editor.indentation.indent(true);
  4478. };
  4479. return OutdentButton;
  4480. })(Button);
  4481. Simditor.Toolbar.addButton(OutdentButton);
  4482. HrButton = (function(superClass) {
  4483. extend(HrButton, superClass);
  4484. function HrButton() {
  4485. return HrButton.__super__.constructor.apply(this, arguments);
  4486. }
  4487. HrButton.prototype.name = 'hr';
  4488. HrButton.prototype.icon = 'minus';
  4489. HrButton.prototype.htmlTag = 'hr';
  4490. HrButton.prototype._status = function() {};
  4491. HrButton.prototype.command = function() {
  4492. var $hr, $newBlock, $nextBlock, $rootBlock;
  4493. $rootBlock = this.editor.selection.rootNodes().first();
  4494. $nextBlock = $rootBlock.next();
  4495. if ($nextBlock.length > 0) {
  4496. this.editor.selection.save();
  4497. } else {
  4498. $newBlock = $('<p/>').append(this.editor.util.phBr);
  4499. }
  4500. $hr = $('<hr/>').insertAfter($rootBlock);
  4501. if ($newBlock) {
  4502. $newBlock.insertAfter($hr);
  4503. this.editor.selection.setRangeAtStartOf($newBlock);
  4504. } else {
  4505. this.editor.selection.restore();
  4506. }
  4507. return this.editor.trigger('valuechanged');
  4508. };
  4509. return HrButton;
  4510. })(Button);
  4511. Simditor.Toolbar.addButton(HrButton);
  4512. TableButton = (function(superClass) {
  4513. extend(TableButton, superClass);
  4514. function TableButton() {
  4515. return TableButton.__super__.constructor.apply(this, arguments);
  4516. }
  4517. TableButton.prototype.name = 'table';
  4518. TableButton.prototype.icon = 'table';
  4519. TableButton.prototype.htmlTag = 'table';
  4520. TableButton.prototype.disableTag = 'pre, li, blockquote';
  4521. TableButton.prototype.menu = true;
  4522. TableButton.prototype._init = function() {
  4523. TableButton.__super__._init.call(this);
  4524. $.merge(this.editor.formatter._allowedTags, ['thead', 'th', 'tbody', 'tr', 'td', 'colgroup', 'col']);
  4525. $.extend(this.editor.formatter._allowedAttributes, {
  4526. td: ['rowspan', 'colspan'],
  4527. col: ['width']
  4528. });
  4529. $.extend(this.editor.formatter._allowedStyles, {
  4530. td: ['text-align'],
  4531. th: ['text-align']
  4532. });
  4533. this._initShortcuts();
  4534. this.editor.on('decorate', (function(_this) {
  4535. return function(e, $el) {
  4536. return $el.find('table').each(function(i, table) {
  4537. return _this.decorate($(table));
  4538. });
  4539. };
  4540. })(this));
  4541. this.editor.on('undecorate', (function(_this) {
  4542. return function(e, $el) {
  4543. return $el.find('table').each(function(i, table) {
  4544. return _this.undecorate($(table));
  4545. });
  4546. };
  4547. })(this));
  4548. this.editor.on('selectionchanged.table', (function(_this) {
  4549. return function(e) {
  4550. var $container, range;
  4551. _this.editor.body.find('.simditor-table td, .simditor-table th').removeClass('active');
  4552. range = _this.editor.selection.range();
  4553. if (!range) {
  4554. return;
  4555. }
  4556. $container = _this.editor.selection.containerNode();
  4557. if (range.collapsed && $container.is('.simditor-table')) {
  4558. if (_this.editor.selection.rangeAtStartOf($container)) {
  4559. $container = $container.find('th:first');
  4560. } else {
  4561. $container = $container.find('td:last');
  4562. }
  4563. _this.editor.selection.setRangeAtEndOf($container);
  4564. }
  4565. return $container.closest('td, th', _this.editor.body).addClass('active');
  4566. };
  4567. })(this));
  4568. this.editor.on('blur.table', (function(_this) {
  4569. return function(e) {
  4570. return _this.editor.body.find('.simditor-table td, .simditor-table th').removeClass('active');
  4571. };
  4572. })(this));
  4573. this.editor.keystroke.add('up', 'td', (function(_this) {
  4574. return function(e, $node) {
  4575. _this._tdNav($node, 'up');
  4576. return true;
  4577. };
  4578. })(this));
  4579. this.editor.keystroke.add('up', 'th', (function(_this) {
  4580. return function(e, $node) {
  4581. _this._tdNav($node, 'up');
  4582. return true;
  4583. };
  4584. })(this));
  4585. this.editor.keystroke.add('down', 'td', (function(_this) {
  4586. return function(e, $node) {
  4587. _this._tdNav($node, 'down');
  4588. return true;
  4589. };
  4590. })(this));
  4591. return this.editor.keystroke.add('down', 'th', (function(_this) {
  4592. return function(e, $node) {
  4593. _this._tdNav($node, 'down');
  4594. return true;
  4595. };
  4596. })(this));
  4597. };
  4598. TableButton.prototype._tdNav = function($td, direction) {
  4599. var $anotherTr, $tr, action, anotherTag, index, parentTag, ref;
  4600. if (direction == null) {
  4601. direction = 'up';
  4602. }
  4603. action = direction === 'up' ? 'prev' : 'next';
  4604. ref = direction === 'up' ? ['tbody', 'thead'] : ['thead', 'tbody'], parentTag = ref[0], anotherTag = ref[1];
  4605. $tr = $td.parent('tr');
  4606. $anotherTr = this["_" + action + "Row"]($tr);
  4607. if (!($anotherTr.length > 0)) {
  4608. return true;
  4609. }
  4610. index = $tr.find('td, th').index($td);
  4611. return this.editor.selection.setRangeAtEndOf($anotherTr.find('td, th').eq(index));
  4612. };
  4613. TableButton.prototype._nextRow = function($tr) {
  4614. var $nextTr;
  4615. $nextTr = $tr.next('tr');
  4616. if ($nextTr.length < 1 && $tr.parent('thead').length > 0) {
  4617. $nextTr = $tr.parent('thead').next('tbody').find('tr:first');
  4618. }
  4619. return $nextTr;
  4620. };
  4621. TableButton.prototype._prevRow = function($tr) {
  4622. var $prevTr;
  4623. $prevTr = $tr.prev('tr');
  4624. if ($prevTr.length < 1 && $tr.parent('tbody').length > 0) {
  4625. $prevTr = $tr.parent('tbody').prev('thead').find('tr');
  4626. }
  4627. return $prevTr;
  4628. };
  4629. TableButton.prototype.initResize = function($table) {
  4630. var $colgroup, $resizeHandle, $wrapper;
  4631. $wrapper = $table.parent('.simditor-table');
  4632. $colgroup = $table.find('colgroup');
  4633. if ($colgroup.length < 1) {
  4634. $colgroup = $('<colgroup/>').prependTo($table);
  4635. $table.find('thead tr th').each(function(i, td) {
  4636. var $col;
  4637. return $col = $('<col/>').appendTo($colgroup);
  4638. });
  4639. this.refreshTableWidth($table);
  4640. }
  4641. $resizeHandle = $('<div />', {
  4642. "class": 'simditor-resize-handle',
  4643. contenteditable: 'false'
  4644. }).appendTo($wrapper);
  4645. $wrapper.on('mousemove', 'td, th', function(e) {
  4646. var $col, $td, index, ref, ref1, x;
  4647. if ($wrapper.hasClass('resizing')) {
  4648. return;
  4649. }
  4650. $td = $(e.currentTarget);
  4651. x = e.pageX - $(e.currentTarget).offset().left;
  4652. if (x < 5 && $td.prev().length > 0) {
  4653. $td = $td.prev();
  4654. }
  4655. if ($td.next('td, th').length < 1) {
  4656. $resizeHandle.hide();
  4657. return;
  4658. }
  4659. if ((ref = $resizeHandle.data('td')) != null ? ref.is($td) : void 0) {
  4660. $resizeHandle.show();
  4661. return;
  4662. }
  4663. index = $td.parent().find('td, th').index($td);
  4664. $col = $colgroup.find('col').eq(index);
  4665. if ((ref1 = $resizeHandle.data('col')) != null ? ref1.is($col) : void 0) {
  4666. $resizeHandle.show();
  4667. return;
  4668. }
  4669. return $resizeHandle.css('left', $td.position().left + $td.outerWidth() - 5).data('td', $td).data('col', $col).show();
  4670. });
  4671. $wrapper.on('mouseleave', function(e) {
  4672. return $resizeHandle.hide();
  4673. });
  4674. return $wrapper.on('mousedown', '.simditor-resize-handle', function(e) {
  4675. var $handle, $leftCol, $leftTd, $rightCol, $rightTd, minWidth, startHandleLeft, startLeftWidth, startRightWidth, startX, tableWidth;
  4676. $handle = $(e.currentTarget);
  4677. $leftTd = $handle.data('td');
  4678. $leftCol = $handle.data('col');
  4679. $rightTd = $leftTd.next('td, th');
  4680. $rightCol = $leftCol.next('col');
  4681. startX = e.pageX;
  4682. startLeftWidth = $leftTd.outerWidth() * 1;
  4683. startRightWidth = $rightTd.outerWidth() * 1;
  4684. startHandleLeft = parseFloat($handle.css('left'));
  4685. tableWidth = $leftTd.closest('table').width();
  4686. minWidth = 50;
  4687. $(document).on('mousemove.simditor-resize-table', function(e) {
  4688. var deltaX, leftWidth, rightWidth;
  4689. deltaX = e.pageX - startX;
  4690. leftWidth = startLeftWidth + deltaX;
  4691. rightWidth = startRightWidth - deltaX;
  4692. if (leftWidth < minWidth) {
  4693. leftWidth = minWidth;
  4694. deltaX = minWidth - startLeftWidth;
  4695. rightWidth = startRightWidth - deltaX;
  4696. } else if (rightWidth < minWidth) {
  4697. rightWidth = minWidth;
  4698. deltaX = startRightWidth - minWidth;
  4699. leftWidth = startLeftWidth + deltaX;
  4700. }
  4701. $leftCol.attr('width', (leftWidth / tableWidth * 100) + '%');
  4702. $rightCol.attr('width', (rightWidth / tableWidth * 100) + '%');
  4703. return $handle.css('left', startHandleLeft + deltaX);
  4704. });
  4705. $(document).one('mouseup.simditor-resize-table', function(e) {
  4706. $(document).off('.simditor-resize-table');
  4707. return $wrapper.removeClass('resizing');
  4708. });
  4709. $wrapper.addClass('resizing');
  4710. return false;
  4711. });
  4712. };
  4713. TableButton.prototype._initShortcuts = function() {
  4714. this.editor.hotkeys.add('ctrl+alt+up', (function(_this) {
  4715. return function(e) {
  4716. _this.editMenu.find('.menu-item[data-param=insertRowAbove]').click();
  4717. return false;
  4718. };
  4719. })(this));
  4720. this.editor.hotkeys.add('ctrl+alt+down', (function(_this) {
  4721. return function(e) {
  4722. _this.editMenu.find('.menu-item[data-param=insertRowBelow]').click();
  4723. return false;
  4724. };
  4725. })(this));
  4726. this.editor.hotkeys.add('ctrl+alt+left', (function(_this) {
  4727. return function(e) {
  4728. _this.editMenu.find('.menu-item[data-param=insertColLeft]').click();
  4729. return false;
  4730. };
  4731. })(this));
  4732. return this.editor.hotkeys.add('ctrl+alt+right', (function(_this) {
  4733. return function(e) {
  4734. _this.editMenu.find('.menu-item[data-param=insertColRight]').click();
  4735. return false;
  4736. };
  4737. })(this));
  4738. };
  4739. TableButton.prototype.decorate = function($table) {
  4740. var $headRow, $tbody, $thead;
  4741. if ($table.parent('.simditor-table').length > 0) {
  4742. this.undecorate($table);
  4743. }
  4744. $table.wrap('<div class="simditor-table"></div>');
  4745. if ($table.find('thead').length < 1) {
  4746. $thead = $('<thead />');
  4747. $headRow = $table.find('tr').first();
  4748. $thead.append($headRow);
  4749. this._changeCellTag($headRow, 'th');
  4750. $tbody = $table.find('tbody');
  4751. if ($tbody.length > 0) {
  4752. $tbody.before($thead);
  4753. } else {
  4754. $table.prepend($thead);
  4755. }
  4756. }
  4757. this.initResize($table);
  4758. return $table.parent();
  4759. };
  4760. TableButton.prototype.undecorate = function($table) {
  4761. if (!($table.parent('.simditor-table').length > 0)) {
  4762. return;
  4763. }
  4764. return $table.parent().replaceWith($table);
  4765. };
  4766. TableButton.prototype.renderMenu = function() {
  4767. var $table;
  4768. $("<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);
  4769. this.createMenu = this.menuWrapper.find('.menu-create-table');
  4770. this.editMenu = this.menuWrapper.find('.menu-edit-table');
  4771. $table = this.createTable(6, 6).appendTo(this.createMenu);
  4772. this.createMenu.on('mouseenter', 'td, th', (function(_this) {
  4773. return function(e) {
  4774. var $td, $tr, $trs, num;
  4775. _this.createMenu.find('td, th').removeClass('selected');
  4776. $td = $(e.currentTarget);
  4777. $tr = $td.parent();
  4778. num = $tr.find('td, th').index($td) + 1;
  4779. $trs = $tr.prevAll('tr').addBack();
  4780. if ($tr.parent().is('tbody')) {
  4781. $trs = $trs.add($table.find('thead tr'));
  4782. }
  4783. return $trs.find("td:lt(" + num + "), th:lt(" + num + ")").addClass('selected');
  4784. };
  4785. })(this));
  4786. this.createMenu.on('mouseleave', function(e) {
  4787. return $(e.currentTarget).find('td, th').removeClass('selected');
  4788. });
  4789. return this.createMenu.on('mousedown', 'td, th', (function(_this) {
  4790. return function(e) {
  4791. var $closestBlock, $td, $tr, colNum, rowNum;
  4792. _this.wrapper.removeClass('menu-on');
  4793. if (!_this.editor.inputManager.focused) {
  4794. return;
  4795. }
  4796. $td = $(e.currentTarget);
  4797. $tr = $td.parent();
  4798. colNum = $tr.find('td').index($td) + 1;
  4799. rowNum = $tr.prevAll('tr').length + 1;
  4800. if ($tr.parent().is('tbody')) {
  4801. rowNum += 1;
  4802. }
  4803. $table = _this.createTable(rowNum, colNum, true);
  4804. $closestBlock = _this.editor.selection.blockNodes().last();
  4805. if (_this.editor.util.isEmptyNode($closestBlock)) {
  4806. $closestBlock.replaceWith($table);
  4807. } else {
  4808. $closestBlock.after($table);
  4809. }
  4810. _this.decorate($table);
  4811. _this.editor.selection.setRangeAtStartOf($table.find('th:first'));
  4812. _this.editor.trigger('valuechanged');
  4813. return false;
  4814. };
  4815. })(this));
  4816. };
  4817. TableButton.prototype.createTable = function(row, col, phBr) {
  4818. var $table, $tbody, $td, $thead, $tr, c, k, l, r, ref, ref1;
  4819. $table = $('<table/>');
  4820. $thead = $('<thead/>').appendTo($table);
  4821. $tbody = $('<tbody/>').appendTo($table);
  4822. for (r = k = 0, ref = row; 0 <= ref ? k < ref : k > ref; r = 0 <= ref ? ++k : --k) {
  4823. $tr = $('<tr/>');
  4824. $tr.appendTo(r === 0 ? $thead : $tbody);
  4825. for (c = l = 0, ref1 = col; 0 <= ref1 ? l < ref1 : l > ref1; c = 0 <= ref1 ? ++l : --l) {
  4826. $td = $(r === 0 ? '<th/>' : '<td/>').appendTo($tr);
  4827. if (phBr) {
  4828. $td.append(this.editor.util.phBr);
  4829. }
  4830. }
  4831. }
  4832. return $table;
  4833. };
  4834. TableButton.prototype.refreshTableWidth = function($table) {
  4835. var cols, tableWidth;
  4836. tableWidth = $table.width();
  4837. cols = $table.find('col');
  4838. return $table.find('thead tr th').each(function(i, td) {
  4839. var $col;
  4840. $col = cols.eq(i);
  4841. return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
  4842. });
  4843. };
  4844. TableButton.prototype.setActive = function(active) {
  4845. TableButton.__super__.setActive.call(this, active);
  4846. if (active) {
  4847. this.createMenu.hide();
  4848. return this.editMenu.show();
  4849. } else {
  4850. this.createMenu.show();
  4851. return this.editMenu.hide();
  4852. }
  4853. };
  4854. TableButton.prototype._changeCellTag = function($tr, tagName) {
  4855. return $tr.find('td, th').each(function(i, cell) {
  4856. var $cell;
  4857. $cell = $(cell);
  4858. return $cell.replaceWith("<" + tagName + ">" + ($cell.html()) + "</" + tagName + ">");
  4859. });
  4860. };
  4861. TableButton.prototype.deleteRow = function($td) {
  4862. var $newTr, $tr, index;
  4863. $tr = $td.parent('tr');
  4864. if ($tr.closest('table').find('tr').length < 1) {
  4865. return this.deleteTable($td);
  4866. } else {
  4867. $newTr = this._nextRow($tr);
  4868. if (!($newTr.length > 0)) {
  4869. $newTr = this._prevRow($tr);
  4870. }
  4871. index = $tr.find('td, th').index($td);
  4872. if ($tr.parent().is('thead')) {
  4873. $newTr.appendTo($tr.parent());
  4874. this._changeCellTag($newTr, 'th');
  4875. }
  4876. $tr.remove();
  4877. return this.editor.selection.setRangeAtEndOf($newTr.find('td, th').eq(index));
  4878. }
  4879. };
  4880. TableButton.prototype.insertRow = function($td, direction) {
  4881. var $newTr, $table, $tr, cellTag, colNum, i, index, k, ref;
  4882. if (direction == null) {
  4883. direction = 'after';
  4884. }
  4885. $tr = $td.parent('tr');
  4886. $table = $tr.closest('table');
  4887. colNum = 0;
  4888. $table.find('tr').each(function(i, tr) {
  4889. return colNum = Math.max(colNum, $(tr).find('td').length);
  4890. });
  4891. index = $tr.find('td, th').index($td);
  4892. $newTr = $('<tr/>');
  4893. cellTag = 'td';
  4894. if (direction === 'after' && $tr.parent().is('thead')) {
  4895. $tr.parent().next('tbody').prepend($newTr);
  4896. } else if (direction === 'before' && $tr.parent().is('thead')) {
  4897. $tr.before($newTr);
  4898. $tr.parent().next('tbody').prepend($tr);
  4899. this._changeCellTag($tr, 'td');
  4900. cellTag = 'th';
  4901. } else {
  4902. $tr[direction]($newTr);
  4903. }
  4904. for (i = k = 1, ref = colNum; 1 <= ref ? k <= ref : k >= ref; i = 1 <= ref ? ++k : --k) {
  4905. $("<" + cellTag + "/>").append(this.editor.util.phBr).appendTo($newTr);
  4906. }
  4907. return this.editor.selection.setRangeAtStartOf($newTr.find('td, th').eq(index));
  4908. };
  4909. TableButton.prototype.deleteCol = function($td) {
  4910. var $newTd, $table, $tr, index, noOtherCol, noOtherRow;
  4911. $tr = $td.parent('tr');
  4912. noOtherRow = $tr.closest('table').find('tr').length < 2;
  4913. noOtherCol = $td.siblings('td, th').length < 1;
  4914. if (noOtherRow && noOtherCol) {
  4915. return this.deleteTable($td);
  4916. } else {
  4917. index = $tr.find('td, th').index($td);
  4918. $newTd = $td.next('td, th');
  4919. if (!($newTd.length > 0)) {
  4920. $newTd = $tr.prev('td, th');
  4921. }
  4922. $table = $tr.closest('table');
  4923. $table.find('col').eq(index).remove();
  4924. $table.find('tr').each(function(i, tr) {
  4925. return $(tr).find('td, th').eq(index).remove();
  4926. });
  4927. this.refreshTableWidth($table);
  4928. return this.editor.selection.setRangeAtEndOf($newTd);
  4929. }
  4930. };
  4931. TableButton.prototype.insertCol = function($td, direction) {
  4932. var $col, $newCol, $newTd, $table, $tr, index, tableWidth, width;
  4933. if (direction == null) {
  4934. direction = 'after';
  4935. }
  4936. $tr = $td.parent('tr');
  4937. index = $tr.find('td, th').index($td);
  4938. $table = $td.closest('table');
  4939. $col = $table.find('col').eq(index);
  4940. $table.find('tr').each((function(_this) {
  4941. return function(i, tr) {
  4942. var $newTd, cellTag;
  4943. cellTag = $(tr).parent().is('thead') ? 'th' : 'td';
  4944. $newTd = $("<" + cellTag + "/>").append(_this.editor.util.phBr);
  4945. return $(tr).find('td, th').eq(index)[direction]($newTd);
  4946. };
  4947. })(this));
  4948. $newCol = $('<col/>');
  4949. $col[direction]($newCol);
  4950. tableWidth = $table.width();
  4951. width = Math.max(parseFloat($col.attr('width')) / 2, 50 / tableWidth * 100);
  4952. $col.attr('width', width + '%');
  4953. $newCol.attr('width', width + '%');
  4954. this.refreshTableWidth($table);
  4955. $newTd = direction === 'after' ? $td.next('td, th') : $td.prev('td, th');
  4956. return this.editor.selection.setRangeAtStartOf($newTd);
  4957. };
  4958. TableButton.prototype.deleteTable = function($td) {
  4959. var $block, $table;
  4960. $table = $td.closest('.simditor-table');
  4961. $block = $table.next('p');
  4962. $table.remove();
  4963. if ($block.length > 0) {
  4964. return this.editor.selection.setRangeAtStartOf($block);
  4965. }
  4966. };
  4967. TableButton.prototype.command = function(param) {
  4968. var $td;
  4969. $td = this.editor.selection.containerNode().closest('td, th');
  4970. if (!($td.length > 0)) {
  4971. return;
  4972. }
  4973. if (param === 'deleteRow') {
  4974. this.deleteRow($td);
  4975. } else if (param === 'insertRowAbove') {
  4976. this.insertRow($td, 'before');
  4977. } else if (param === 'insertRowBelow') {
  4978. this.insertRow($td);
  4979. } else if (param === 'deleteCol') {
  4980. this.deleteCol($td);
  4981. } else if (param === 'insertColLeft') {
  4982. this.insertCol($td, 'before');
  4983. } else if (param === 'insertColRight') {
  4984. this.insertCol($td);
  4985. } else if (param === 'deleteTable') {
  4986. this.deleteTable($td);
  4987. } else {
  4988. return;
  4989. }
  4990. return this.editor.trigger('valuechanged');
  4991. };
  4992. return TableButton;
  4993. })(Button);
  4994. Simditor.Toolbar.addButton(TableButton);
  4995. StrikethroughButton = (function(superClass) {
  4996. extend(StrikethroughButton, superClass);
  4997. function StrikethroughButton() {
  4998. return StrikethroughButton.__super__.constructor.apply(this, arguments);
  4999. }
  5000. StrikethroughButton.prototype.name = 'strikethrough';
  5001. StrikethroughButton.prototype.icon = 'strikethrough';
  5002. StrikethroughButton.prototype.htmlTag = 'strike';
  5003. StrikethroughButton.prototype.disableTag = 'pre';
  5004. StrikethroughButton.prototype._activeStatus = function() {
  5005. var active;
  5006. active = document.queryCommandState('strikethrough') === true;
  5007. this.setActive(active);
  5008. return this.active;
  5009. };
  5010. StrikethroughButton.prototype.command = function() {
  5011. document.execCommand('strikethrough');
  5012. if (!this.editor.util.support.oninput) {
  5013. this.editor.trigger('valuechanged');
  5014. }
  5015. return $(document).trigger('selectionchange');
  5016. };
  5017. return StrikethroughButton;
  5018. })(Button);
  5019. Simditor.Toolbar.addButton(StrikethroughButton);
  5020. AlignmentButton = (function(superClass) {
  5021. extend(AlignmentButton, superClass);
  5022. function AlignmentButton() {
  5023. return AlignmentButton.__super__.constructor.apply(this, arguments);
  5024. }
  5025. AlignmentButton.prototype.name = "alignment";
  5026. AlignmentButton.prototype.icon = 'align-left';
  5027. AlignmentButton.prototype.htmlTag = 'p, h1, h2, h3, h4, td, th';
  5028. AlignmentButton.prototype._init = function() {
  5029. this.menu = [
  5030. {
  5031. name: 'left',
  5032. text: this._t('alignLeft'),
  5033. icon: 'align-left',
  5034. param: 'left'
  5035. }, {
  5036. name: 'center',
  5037. text: this._t('alignCenter'),
  5038. icon: 'align-center',
  5039. param: 'center'
  5040. }, {
  5041. name: 'right',
  5042. text: this._t('alignRight'),
  5043. icon: 'align-right',
  5044. param: 'right'
  5045. }
  5046. ];
  5047. return AlignmentButton.__super__._init.call(this);
  5048. };
  5049. AlignmentButton.prototype.setActive = function(active, align) {
  5050. if (align == null) {
  5051. align = 'left';
  5052. }
  5053. if (align !== 'left' && align !== 'center' && align !== 'right') {
  5054. align = 'left';
  5055. }
  5056. if (align === 'left') {
  5057. AlignmentButton.__super__.setActive.call(this, false);
  5058. } else {
  5059. AlignmentButton.__super__.setActive.call(this, active);
  5060. }
  5061. this.el.removeClass('align-left align-center align-right');
  5062. if (active) {
  5063. this.el.addClass('align-' + align);
  5064. }
  5065. this.setIcon('align-' + align);
  5066. return this.menuEl.find('.menu-item').show().end().find('.menu-item-' + align).hide();
  5067. };
  5068. AlignmentButton.prototype._status = function() {
  5069. this.nodes = this.editor.selection.nodes().filter(this.htmlTag);
  5070. if (this.nodes.length < 1) {
  5071. this.setDisabled(true);
  5072. return this.setActive(false);
  5073. } else {
  5074. this.setDisabled(false);
  5075. return this.setActive(true, this.nodes.first().css('text-align'));
  5076. }
  5077. };
  5078. AlignmentButton.prototype.command = function(align) {
  5079. if (align !== 'left' && align !== 'center' && align !== 'right') {
  5080. throw new Error("simditor alignment button: invalid align " + align);
  5081. }
  5082. this.nodes.css({
  5083. 'text-align': align === 'left' ? '' : align
  5084. });
  5085. this.editor.trigger('valuechanged');
  5086. return this.editor.inputManager.throttledSelectionChanged();
  5087. };
  5088. return AlignmentButton;
  5089. })(Button);
  5090. Simditor.Toolbar.addButton(AlignmentButton);
  5091. return Simditor;
  5092. }));