plugin.js 291 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.3.0 (2020-05-21)
  8. */
  9. (function (domGlobals) {
  10. 'use strict';
  11. var Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var noop = function () {
  25. };
  26. var compose = function (fa, fb) {
  27. return function () {
  28. var args = [];
  29. for (var _i = 0; _i < arguments.length; _i++) {
  30. args[_i] = arguments[_i];
  31. }
  32. return fa(fb.apply(null, args));
  33. };
  34. };
  35. var constant = function (value) {
  36. return function () {
  37. return value;
  38. };
  39. };
  40. var identity = function (x) {
  41. return x;
  42. };
  43. function curry(fn) {
  44. var initialArgs = [];
  45. for (var _i = 1; _i < arguments.length; _i++) {
  46. initialArgs[_i - 1] = arguments[_i];
  47. }
  48. return function () {
  49. var restArgs = [];
  50. for (var _i = 0; _i < arguments.length; _i++) {
  51. restArgs[_i] = arguments[_i];
  52. }
  53. var all = initialArgs.concat(restArgs);
  54. return fn.apply(null, all);
  55. };
  56. }
  57. var not = function (f) {
  58. return function (t) {
  59. return !f(t);
  60. };
  61. };
  62. var die = function (msg) {
  63. return function () {
  64. throw new Error(msg);
  65. };
  66. };
  67. var never = constant(false);
  68. var always = constant(true);
  69. var none = function () {
  70. return NONE;
  71. };
  72. var NONE = function () {
  73. var eq = function (o) {
  74. return o.isNone();
  75. };
  76. var call = function (thunk) {
  77. return thunk();
  78. };
  79. var id = function (n) {
  80. return n;
  81. };
  82. var me = {
  83. fold: function (n, _s) {
  84. return n();
  85. },
  86. is: never,
  87. isSome: never,
  88. isNone: always,
  89. getOr: id,
  90. getOrThunk: call,
  91. getOrDie: function (msg) {
  92. throw new Error(msg || 'error: getOrDie called on none.');
  93. },
  94. getOrNull: constant(null),
  95. getOrUndefined: constant(undefined),
  96. or: id,
  97. orThunk: call,
  98. map: none,
  99. each: noop,
  100. bind: none,
  101. exists: never,
  102. forall: always,
  103. filter: none,
  104. equals: eq,
  105. equals_: eq,
  106. toArray: function () {
  107. return [];
  108. },
  109. toString: constant('none()')
  110. };
  111. return me;
  112. }();
  113. var some = function (a) {
  114. var constant_a = constant(a);
  115. var self = function () {
  116. return me;
  117. };
  118. var bind = function (f) {
  119. return f(a);
  120. };
  121. var me = {
  122. fold: function (n, s) {
  123. return s(a);
  124. },
  125. is: function (v) {
  126. return a === v;
  127. },
  128. isSome: always,
  129. isNone: never,
  130. getOr: constant_a,
  131. getOrThunk: constant_a,
  132. getOrDie: constant_a,
  133. getOrNull: constant_a,
  134. getOrUndefined: constant_a,
  135. or: self,
  136. orThunk: self,
  137. map: function (f) {
  138. return some(f(a));
  139. },
  140. each: function (f) {
  141. f(a);
  142. },
  143. bind: bind,
  144. exists: bind,
  145. forall: bind,
  146. filter: function (f) {
  147. return f(a) ? me : NONE;
  148. },
  149. toArray: function () {
  150. return [a];
  151. },
  152. toString: function () {
  153. return 'some(' + a + ')';
  154. },
  155. equals: function (o) {
  156. return o.is(a);
  157. },
  158. equals_: function (o, elementEq) {
  159. return o.fold(never, function (b) {
  160. return elementEq(a, b);
  161. });
  162. }
  163. };
  164. return me;
  165. };
  166. var from = function (value) {
  167. return value === null || value === undefined ? NONE : some(value);
  168. };
  169. var Option = {
  170. some: some,
  171. none: none,
  172. from: from
  173. };
  174. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  175. var typeOf = function (x) {
  176. var t = typeof x;
  177. if (x === null) {
  178. return 'null';
  179. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  180. return 'array';
  181. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  182. return 'string';
  183. } else {
  184. return t;
  185. }
  186. };
  187. var isType = function (type) {
  188. return function (value) {
  189. return typeOf(value) === type;
  190. };
  191. };
  192. var isSimpleType = function (type) {
  193. return function (value) {
  194. return typeof value === type;
  195. };
  196. };
  197. var isString = isType('string');
  198. var isArray = isType('array');
  199. var isBoolean = isSimpleType('boolean');
  200. var isFunction = isSimpleType('function');
  201. var isNumber = isSimpleType('number');
  202. var nativeSlice = Array.prototype.slice;
  203. var nativeIndexOf = Array.prototype.indexOf;
  204. var nativePush = Array.prototype.push;
  205. var rawIndexOf = function (ts, t) {
  206. return nativeIndexOf.call(ts, t);
  207. };
  208. var contains = function (xs, x) {
  209. return rawIndexOf(xs, x) > -1;
  210. };
  211. var exists = function (xs, pred) {
  212. for (var i = 0, len = xs.length; i < len; i++) {
  213. var x = xs[i];
  214. if (pred(x, i)) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. };
  220. var map = function (xs, f) {
  221. var len = xs.length;
  222. var r = new Array(len);
  223. for (var i = 0; i < len; i++) {
  224. var x = xs[i];
  225. r[i] = f(x, i);
  226. }
  227. return r;
  228. };
  229. var each = function (xs, f) {
  230. for (var i = 0, len = xs.length; i < len; i++) {
  231. var x = xs[i];
  232. f(x, i);
  233. }
  234. };
  235. var eachr = function (xs, f) {
  236. for (var i = xs.length - 1; i >= 0; i--) {
  237. var x = xs[i];
  238. f(x, i);
  239. }
  240. };
  241. var filter = function (xs, pred) {
  242. var r = [];
  243. for (var i = 0, len = xs.length; i < len; i++) {
  244. var x = xs[i];
  245. if (pred(x, i)) {
  246. r.push(x);
  247. }
  248. }
  249. return r;
  250. };
  251. var foldr = function (xs, f, acc) {
  252. eachr(xs, function (x) {
  253. acc = f(acc, x);
  254. });
  255. return acc;
  256. };
  257. var foldl = function (xs, f, acc) {
  258. each(xs, function (x) {
  259. acc = f(acc, x);
  260. });
  261. return acc;
  262. };
  263. var findUntil = function (xs, pred, until) {
  264. for (var i = 0, len = xs.length; i < len; i++) {
  265. var x = xs[i];
  266. if (pred(x, i)) {
  267. return Option.some(x);
  268. } else if (until(x, i)) {
  269. break;
  270. }
  271. }
  272. return Option.none();
  273. };
  274. var find = function (xs, pred) {
  275. return findUntil(xs, pred, never);
  276. };
  277. var findIndex = function (xs, pred) {
  278. for (var i = 0, len = xs.length; i < len; i++) {
  279. var x = xs[i];
  280. if (pred(x, i)) {
  281. return Option.some(i);
  282. }
  283. }
  284. return Option.none();
  285. };
  286. var flatten = function (xs) {
  287. var r = [];
  288. for (var i = 0, len = xs.length; i < len; ++i) {
  289. if (!isArray(xs[i])) {
  290. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  291. }
  292. nativePush.apply(r, xs[i]);
  293. }
  294. return r;
  295. };
  296. var bind = function (xs, f) {
  297. return flatten(map(xs, f));
  298. };
  299. var forall = function (xs, pred) {
  300. for (var i = 0, len = xs.length; i < len; ++i) {
  301. var x = xs[i];
  302. if (pred(x, i) !== true) {
  303. return false;
  304. }
  305. }
  306. return true;
  307. };
  308. var reverse = function (xs) {
  309. var r = nativeSlice.call(xs, 0);
  310. r.reverse();
  311. return r;
  312. };
  313. var last = function (xs) {
  314. return xs.length === 0 ? Option.none() : Option.some(xs[xs.length - 1]);
  315. };
  316. var findMap = function (arr, f) {
  317. for (var i = 0; i < arr.length; i++) {
  318. var r = f(arr[i], i);
  319. if (r.isSome()) {
  320. return r;
  321. }
  322. }
  323. return Option.none();
  324. };
  325. var keys = Object.keys;
  326. var hasOwnProperty = Object.hasOwnProperty;
  327. var each$1 = function (obj, f) {
  328. var props = keys(obj);
  329. for (var k = 0, len = props.length; k < len; k++) {
  330. var i = props[k];
  331. var x = obj[i];
  332. f(x, i);
  333. }
  334. };
  335. var map$1 = function (obj, f) {
  336. return tupleMap(obj, function (x, i) {
  337. return {
  338. k: i,
  339. v: f(x, i)
  340. };
  341. });
  342. };
  343. var tupleMap = function (obj, f) {
  344. var r = {};
  345. each$1(obj, function (x, i) {
  346. var tuple = f(x, i);
  347. r[tuple.k] = tuple.v;
  348. });
  349. return r;
  350. };
  351. var get = function (obj, key) {
  352. return has(obj, key) ? Option.from(obj[key]) : Option.none();
  353. };
  354. var has = function (obj, key) {
  355. return hasOwnProperty.call(obj, key);
  356. };
  357. var Global = typeof domGlobals.window !== 'undefined' ? domGlobals.window : Function('return this;')();
  358. var COMMENT = 8;
  359. var DOCUMENT = 9;
  360. var ELEMENT = 1;
  361. var TEXT = 3;
  362. var name = function (element) {
  363. var r = element.dom().nodeName;
  364. return r.toLowerCase();
  365. };
  366. var type = function (element) {
  367. return element.dom().nodeType;
  368. };
  369. var isType$1 = function (t) {
  370. return function (element) {
  371. return type(element) === t;
  372. };
  373. };
  374. var isComment = function (element) {
  375. return type(element) === COMMENT || name(element) === '#comment';
  376. };
  377. var isElement = isType$1(ELEMENT);
  378. var isText = isType$1(TEXT);
  379. var rawSet = function (dom, key, value) {
  380. if (isString(value) || isBoolean(value) || isNumber(value)) {
  381. dom.setAttribute(key, value + '');
  382. } else {
  383. domGlobals.console.error('Invalid call to Attr.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  384. throw new Error('Attribute value was not simple');
  385. }
  386. };
  387. var set = function (element, key, value) {
  388. rawSet(element.dom(), key, value);
  389. };
  390. var setAll = function (element, attrs) {
  391. var dom = element.dom();
  392. each$1(attrs, function (v, k) {
  393. rawSet(dom, k, v);
  394. });
  395. };
  396. var get$1 = function (element, key) {
  397. var v = element.dom().getAttribute(key);
  398. return v === null ? undefined : v;
  399. };
  400. var getOpt = function (element, key) {
  401. return Option.from(get$1(element, key));
  402. };
  403. var has$1 = function (element, key) {
  404. var dom = element.dom();
  405. return dom && dom.hasAttribute ? dom.hasAttribute(key) : false;
  406. };
  407. var remove = function (element, key) {
  408. element.dom().removeAttribute(key);
  409. };
  410. var clone = function (element) {
  411. return foldl(element.dom().attributes, function (acc, attr) {
  412. acc[attr.name] = attr.value;
  413. return acc;
  414. }, {});
  415. };
  416. var checkRange = function (str, substr, start) {
  417. return substr === '' || str.length >= substr.length && str.substr(start, start + substr.length) === substr;
  418. };
  419. var contains$1 = function (str, substr) {
  420. return str.indexOf(substr) !== -1;
  421. };
  422. var startsWith = function (str, prefix) {
  423. return checkRange(str, prefix, 0);
  424. };
  425. var endsWith = function (str, suffix) {
  426. return checkRange(str, suffix, str.length - suffix.length);
  427. };
  428. var blank = function (r) {
  429. return function (s) {
  430. return s.replace(r, '');
  431. };
  432. };
  433. var trim = blank(/^\s+|\s+$/g);
  434. var isSupported = function (dom) {
  435. return dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  436. };
  437. var fromHtml = function (html, scope) {
  438. var doc = scope || domGlobals.document;
  439. var div = doc.createElement('div');
  440. div.innerHTML = html;
  441. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  442. domGlobals.console.error('HTML does not have a single root node', html);
  443. throw new Error('HTML must have a single root node');
  444. }
  445. return fromDom(div.childNodes[0]);
  446. };
  447. var fromTag = function (tag, scope) {
  448. var doc = scope || domGlobals.document;
  449. var node = doc.createElement(tag);
  450. return fromDom(node);
  451. };
  452. var fromText = function (text, scope) {
  453. var doc = scope || domGlobals.document;
  454. var node = doc.createTextNode(text);
  455. return fromDom(node);
  456. };
  457. var fromDom = function (node) {
  458. if (node === null || node === undefined) {
  459. throw new Error('Node cannot be null or undefined');
  460. }
  461. return { dom: constant(node) };
  462. };
  463. var fromPoint = function (docElm, x, y) {
  464. var doc = docElm.dom();
  465. return Option.from(doc.elementFromPoint(x, y)).map(fromDom);
  466. };
  467. var Element = {
  468. fromHtml: fromHtml,
  469. fromTag: fromTag,
  470. fromText: fromText,
  471. fromDom: fromDom,
  472. fromPoint: fromPoint
  473. };
  474. var inBody = function (element) {
  475. var dom = isText(element) ? element.dom().parentNode : element.dom();
  476. return dom !== undefined && dom !== null && dom.ownerDocument.body.contains(dom);
  477. };
  478. var body = function () {
  479. return getBody(Element.fromDom(domGlobals.document));
  480. };
  481. var getBody = function (doc) {
  482. var b = doc.dom().body;
  483. if (b === null || b === undefined) {
  484. throw new Error('Body is not available yet');
  485. }
  486. return Element.fromDom(b);
  487. };
  488. var internalSet = function (dom, property, value) {
  489. if (!isString(value)) {
  490. domGlobals.console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  491. throw new Error('CSS value must be a string: ' + value);
  492. }
  493. if (isSupported(dom)) {
  494. dom.style.setProperty(property, value);
  495. }
  496. };
  497. var internalRemove = function (dom, property) {
  498. if (isSupported(dom)) {
  499. dom.style.removeProperty(property);
  500. }
  501. };
  502. var set$1 = function (element, property, value) {
  503. var dom = element.dom();
  504. internalSet(dom, property, value);
  505. };
  506. var setAll$1 = function (element, css) {
  507. var dom = element.dom();
  508. each$1(css, function (v, k) {
  509. internalSet(dom, k, v);
  510. });
  511. };
  512. var get$2 = function (element, property) {
  513. var dom = element.dom();
  514. var styles = domGlobals.window.getComputedStyle(dom);
  515. var r = styles.getPropertyValue(property);
  516. return r === '' && !inBody(element) ? getUnsafeProperty(dom, property) : r;
  517. };
  518. var getUnsafeProperty = function (dom, property) {
  519. return isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  520. };
  521. var getRaw = function (element, property) {
  522. var dom = element.dom();
  523. var raw = getUnsafeProperty(dom, property);
  524. return Option.from(raw).filter(function (r) {
  525. return r.length > 0;
  526. });
  527. };
  528. var remove$1 = function (element, property) {
  529. var dom = element.dom();
  530. internalRemove(dom, property);
  531. if (getOpt(element, 'style').map(trim).is('')) {
  532. remove(element, 'style');
  533. }
  534. };
  535. var copy = function (source, target) {
  536. var sourceDom = source.dom();
  537. var targetDom = target.dom();
  538. if (isSupported(sourceDom) && isSupported(targetDom)) {
  539. targetDom.style.cssText = sourceDom.style.cssText;
  540. }
  541. };
  542. var compareDocumentPosition = function (a, b, match) {
  543. return (a.compareDocumentPosition(b) & match) !== 0;
  544. };
  545. var documentPositionContainedBy = function (a, b) {
  546. return compareDocumentPosition(a, b, domGlobals.Node.DOCUMENT_POSITION_CONTAINED_BY);
  547. };
  548. var __assign = function () {
  549. __assign = Object.assign || function __assign(t) {
  550. for (var s, i = 1, n = arguments.length; i < n; i++) {
  551. s = arguments[i];
  552. for (var p in s)
  553. if (Object.prototype.hasOwnProperty.call(s, p))
  554. t[p] = s[p];
  555. }
  556. return t;
  557. };
  558. return __assign.apply(this, arguments);
  559. };
  560. var cached = function (f) {
  561. var called = false;
  562. var r;
  563. return function () {
  564. var args = [];
  565. for (var _i = 0; _i < arguments.length; _i++) {
  566. args[_i] = arguments[_i];
  567. }
  568. if (!called) {
  569. called = true;
  570. r = f.apply(null, args);
  571. }
  572. return r;
  573. };
  574. };
  575. var firstMatch = function (regexes, s) {
  576. for (var i = 0; i < regexes.length; i++) {
  577. var x = regexes[i];
  578. if (x.test(s)) {
  579. return x;
  580. }
  581. }
  582. return undefined;
  583. };
  584. var find$1 = function (regexes, agent) {
  585. var r = firstMatch(regexes, agent);
  586. if (!r) {
  587. return {
  588. major: 0,
  589. minor: 0
  590. };
  591. }
  592. var group = function (i) {
  593. return Number(agent.replace(r, '$' + i));
  594. };
  595. return nu(group(1), group(2));
  596. };
  597. var detect = function (versionRegexes, agent) {
  598. var cleanedAgent = String(agent).toLowerCase();
  599. if (versionRegexes.length === 0) {
  600. return unknown();
  601. }
  602. return find$1(versionRegexes, cleanedAgent);
  603. };
  604. var unknown = function () {
  605. return nu(0, 0);
  606. };
  607. var nu = function (major, minor) {
  608. return {
  609. major: major,
  610. minor: minor
  611. };
  612. };
  613. var Version = {
  614. nu: nu,
  615. detect: detect,
  616. unknown: unknown
  617. };
  618. var edge = 'Edge';
  619. var chrome = 'Chrome';
  620. var ie = 'IE';
  621. var opera = 'Opera';
  622. var firefox = 'Firefox';
  623. var safari = 'Safari';
  624. var unknown$1 = function () {
  625. return nu$1({
  626. current: undefined,
  627. version: Version.unknown()
  628. });
  629. };
  630. var nu$1 = function (info) {
  631. var current = info.current;
  632. var version = info.version;
  633. var isBrowser = function (name) {
  634. return function () {
  635. return current === name;
  636. };
  637. };
  638. return {
  639. current: current,
  640. version: version,
  641. isEdge: isBrowser(edge),
  642. isChrome: isBrowser(chrome),
  643. isIE: isBrowser(ie),
  644. isOpera: isBrowser(opera),
  645. isFirefox: isBrowser(firefox),
  646. isSafari: isBrowser(safari)
  647. };
  648. };
  649. var Browser = {
  650. unknown: unknown$1,
  651. nu: nu$1,
  652. edge: constant(edge),
  653. chrome: constant(chrome),
  654. ie: constant(ie),
  655. opera: constant(opera),
  656. firefox: constant(firefox),
  657. safari: constant(safari)
  658. };
  659. var windows = 'Windows';
  660. var ios = 'iOS';
  661. var android = 'Android';
  662. var linux = 'Linux';
  663. var osx = 'OSX';
  664. var solaris = 'Solaris';
  665. var freebsd = 'FreeBSD';
  666. var chromeos = 'ChromeOS';
  667. var unknown$2 = function () {
  668. return nu$2({
  669. current: undefined,
  670. version: Version.unknown()
  671. });
  672. };
  673. var nu$2 = function (info) {
  674. var current = info.current;
  675. var version = info.version;
  676. var isOS = function (name) {
  677. return function () {
  678. return current === name;
  679. };
  680. };
  681. return {
  682. current: current,
  683. version: version,
  684. isWindows: isOS(windows),
  685. isiOS: isOS(ios),
  686. isAndroid: isOS(android),
  687. isOSX: isOS(osx),
  688. isLinux: isOS(linux),
  689. isSolaris: isOS(solaris),
  690. isFreeBSD: isOS(freebsd),
  691. isChromeOS: isOS(chromeos)
  692. };
  693. };
  694. var OperatingSystem = {
  695. unknown: unknown$2,
  696. nu: nu$2,
  697. windows: constant(windows),
  698. ios: constant(ios),
  699. android: constant(android),
  700. linux: constant(linux),
  701. osx: constant(osx),
  702. solaris: constant(solaris),
  703. freebsd: constant(freebsd),
  704. chromeos: constant(chromeos)
  705. };
  706. var DeviceType = function (os, browser, userAgent, mediaMatch) {
  707. var isiPad = os.isiOS() && /ipad/i.test(userAgent) === true;
  708. var isiPhone = os.isiOS() && !isiPad;
  709. var isMobile = os.isiOS() || os.isAndroid();
  710. var isTouch = isMobile || mediaMatch('(pointer:coarse)');
  711. var isTablet = isiPad || !isiPhone && isMobile && mediaMatch('(min-device-width:768px)');
  712. var isPhone = isiPhone || isMobile && !isTablet;
  713. var iOSwebview = browser.isSafari() && os.isiOS() && /safari/i.test(userAgent) === false;
  714. var isDesktop = !isPhone && !isTablet && !iOSwebview;
  715. return {
  716. isiPad: constant(isiPad),
  717. isiPhone: constant(isiPhone),
  718. isTablet: constant(isTablet),
  719. isPhone: constant(isPhone),
  720. isTouch: constant(isTouch),
  721. isAndroid: os.isAndroid,
  722. isiOS: os.isiOS,
  723. isWebView: constant(iOSwebview),
  724. isDesktop: constant(isDesktop)
  725. };
  726. };
  727. var detect$1 = function (candidates, userAgent) {
  728. var agent = String(userAgent).toLowerCase();
  729. return find(candidates, function (candidate) {
  730. return candidate.search(agent);
  731. });
  732. };
  733. var detectBrowser = function (browsers, userAgent) {
  734. return detect$1(browsers, userAgent).map(function (browser) {
  735. var version = Version.detect(browser.versionRegexes, userAgent);
  736. return {
  737. current: browser.name,
  738. version: version
  739. };
  740. });
  741. };
  742. var detectOs = function (oses, userAgent) {
  743. return detect$1(oses, userAgent).map(function (os) {
  744. var version = Version.detect(os.versionRegexes, userAgent);
  745. return {
  746. current: os.name,
  747. version: version
  748. };
  749. });
  750. };
  751. var UaString = {
  752. detectBrowser: detectBrowser,
  753. detectOs: detectOs
  754. };
  755. var normalVersionRegex = /.*?version\/\ ?([0-9]+)\.([0-9]+).*/;
  756. var checkContains = function (target) {
  757. return function (uastring) {
  758. return contains$1(uastring, target);
  759. };
  760. };
  761. var browsers = [
  762. {
  763. name: 'Edge',
  764. versionRegexes: [/.*?edge\/ ?([0-9]+)\.([0-9]+)$/],
  765. search: function (uastring) {
  766. return contains$1(uastring, 'edge/') && contains$1(uastring, 'chrome') && contains$1(uastring, 'safari') && contains$1(uastring, 'applewebkit');
  767. }
  768. },
  769. {
  770. name: 'Chrome',
  771. versionRegexes: [
  772. /.*?chrome\/([0-9]+)\.([0-9]+).*/,
  773. normalVersionRegex
  774. ],
  775. search: function (uastring) {
  776. return contains$1(uastring, 'chrome') && !contains$1(uastring, 'chromeframe');
  777. }
  778. },
  779. {
  780. name: 'IE',
  781. versionRegexes: [
  782. /.*?msie\ ?([0-9]+)\.([0-9]+).*/,
  783. /.*?rv:([0-9]+)\.([0-9]+).*/
  784. ],
  785. search: function (uastring) {
  786. return contains$1(uastring, 'msie') || contains$1(uastring, 'trident');
  787. }
  788. },
  789. {
  790. name: 'Opera',
  791. versionRegexes: [
  792. normalVersionRegex,
  793. /.*?opera\/([0-9]+)\.([0-9]+).*/
  794. ],
  795. search: checkContains('opera')
  796. },
  797. {
  798. name: 'Firefox',
  799. versionRegexes: [/.*?firefox\/\ ?([0-9]+)\.([0-9]+).*/],
  800. search: checkContains('firefox')
  801. },
  802. {
  803. name: 'Safari',
  804. versionRegexes: [
  805. normalVersionRegex,
  806. /.*?cpu os ([0-9]+)_([0-9]+).*/
  807. ],
  808. search: function (uastring) {
  809. return (contains$1(uastring, 'safari') || contains$1(uastring, 'mobile/')) && contains$1(uastring, 'applewebkit');
  810. }
  811. }
  812. ];
  813. var oses = [
  814. {
  815. name: 'Windows',
  816. search: checkContains('win'),
  817. versionRegexes: [/.*?windows\ nt\ ?([0-9]+)\.([0-9]+).*/]
  818. },
  819. {
  820. name: 'iOS',
  821. search: function (uastring) {
  822. return contains$1(uastring, 'iphone') || contains$1(uastring, 'ipad');
  823. },
  824. versionRegexes: [
  825. /.*?version\/\ ?([0-9]+)\.([0-9]+).*/,
  826. /.*cpu os ([0-9]+)_([0-9]+).*/,
  827. /.*cpu iphone os ([0-9]+)_([0-9]+).*/
  828. ]
  829. },
  830. {
  831. name: 'Android',
  832. search: checkContains('android'),
  833. versionRegexes: [/.*?android\ ?([0-9]+)\.([0-9]+).*/]
  834. },
  835. {
  836. name: 'OSX',
  837. search: checkContains('mac os x'),
  838. versionRegexes: [/.*?mac\ os\ x\ ?([0-9]+)_([0-9]+).*/]
  839. },
  840. {
  841. name: 'Linux',
  842. search: checkContains('linux'),
  843. versionRegexes: []
  844. },
  845. {
  846. name: 'Solaris',
  847. search: checkContains('sunos'),
  848. versionRegexes: []
  849. },
  850. {
  851. name: 'FreeBSD',
  852. search: checkContains('freebsd'),
  853. versionRegexes: []
  854. },
  855. {
  856. name: 'ChromeOS',
  857. search: checkContains('cros'),
  858. versionRegexes: [/.*?chrome\/([0-9]+)\.([0-9]+).*/]
  859. }
  860. ];
  861. var PlatformInfo = {
  862. browsers: constant(browsers),
  863. oses: constant(oses)
  864. };
  865. var detect$2 = function (userAgent, mediaMatch) {
  866. var browsers = PlatformInfo.browsers();
  867. var oses = PlatformInfo.oses();
  868. var browser = UaString.detectBrowser(browsers, userAgent).fold(Browser.unknown, Browser.nu);
  869. var os = UaString.detectOs(oses, userAgent).fold(OperatingSystem.unknown, OperatingSystem.nu);
  870. var deviceType = DeviceType(os, browser, userAgent, mediaMatch);
  871. return {
  872. browser: browser,
  873. os: os,
  874. deviceType: deviceType
  875. };
  876. };
  877. var PlatformDetection = { detect: detect$2 };
  878. var mediaMatch = function (query) {
  879. return domGlobals.window.matchMedia(query).matches;
  880. };
  881. var platform = cached(function () {
  882. return PlatformDetection.detect(domGlobals.navigator.userAgent, mediaMatch);
  883. });
  884. var detect$3 = function () {
  885. return platform();
  886. };
  887. var ELEMENT$1 = ELEMENT;
  888. var DOCUMENT$1 = DOCUMENT;
  889. var is = function (element, selector) {
  890. var dom = element.dom();
  891. if (dom.nodeType !== ELEMENT$1) {
  892. return false;
  893. } else {
  894. var elem = dom;
  895. if (elem.matches !== undefined) {
  896. return elem.matches(selector);
  897. } else if (elem.msMatchesSelector !== undefined) {
  898. return elem.msMatchesSelector(selector);
  899. } else if (elem.webkitMatchesSelector !== undefined) {
  900. return elem.webkitMatchesSelector(selector);
  901. } else if (elem.mozMatchesSelector !== undefined) {
  902. return elem.mozMatchesSelector(selector);
  903. } else {
  904. throw new Error('Browser lacks native selectors');
  905. }
  906. }
  907. };
  908. var bypassSelector = function (dom) {
  909. return dom.nodeType !== ELEMENT$1 && dom.nodeType !== DOCUMENT$1 || dom.childElementCount === 0;
  910. };
  911. var all = function (selector, scope) {
  912. var base = scope === undefined ? domGlobals.document : scope.dom();
  913. return bypassSelector(base) ? [] : map(base.querySelectorAll(selector), Element.fromDom);
  914. };
  915. var one = function (selector, scope) {
  916. var base = scope === undefined ? domGlobals.document : scope.dom();
  917. return bypassSelector(base) ? Option.none() : Option.from(base.querySelector(selector)).map(Element.fromDom);
  918. };
  919. var eq = function (e1, e2) {
  920. return e1.dom() === e2.dom();
  921. };
  922. var regularContains = function (e1, e2) {
  923. var d1 = e1.dom();
  924. var d2 = e2.dom();
  925. return d1 === d2 ? false : d1.contains(d2);
  926. };
  927. var ieContains = function (e1, e2) {
  928. return documentPositionContainedBy(e1.dom(), e2.dom());
  929. };
  930. var contains$2 = function (e1, e2) {
  931. return detect$3().browser.isIE() ? ieContains(e1, e2) : regularContains(e1, e2);
  932. };
  933. var is$1 = is;
  934. var owner = function (element) {
  935. return Element.fromDom(element.dom().ownerDocument);
  936. };
  937. var defaultView = function (element) {
  938. return Element.fromDom(element.dom().ownerDocument.defaultView);
  939. };
  940. var parent = function (element) {
  941. return Option.from(element.dom().parentNode).map(Element.fromDom);
  942. };
  943. var parents = function (element, isRoot) {
  944. var stop = isFunction(isRoot) ? isRoot : never;
  945. var dom = element.dom();
  946. var ret = [];
  947. while (dom.parentNode !== null && dom.parentNode !== undefined) {
  948. var rawParent = dom.parentNode;
  949. var p = Element.fromDom(rawParent);
  950. ret.push(p);
  951. if (stop(p) === true) {
  952. break;
  953. } else {
  954. dom = rawParent;
  955. }
  956. }
  957. return ret;
  958. };
  959. var prevSibling = function (element) {
  960. return Option.from(element.dom().previousSibling).map(Element.fromDom);
  961. };
  962. var nextSibling = function (element) {
  963. return Option.from(element.dom().nextSibling).map(Element.fromDom);
  964. };
  965. var children = function (element) {
  966. return map(element.dom().childNodes, Element.fromDom);
  967. };
  968. var child = function (element, index) {
  969. var cs = element.dom().childNodes;
  970. return Option.from(cs[index]).map(Element.fromDom);
  971. };
  972. var firstChild = function (element) {
  973. return child(element, 0);
  974. };
  975. var before = function (marker, element) {
  976. var parent$1 = parent(marker);
  977. parent$1.each(function (v) {
  978. v.dom().insertBefore(element.dom(), marker.dom());
  979. });
  980. };
  981. var after = function (marker, element) {
  982. var sibling = nextSibling(marker);
  983. sibling.fold(function () {
  984. var parent$1 = parent(marker);
  985. parent$1.each(function (v) {
  986. append(v, element);
  987. });
  988. }, function (v) {
  989. before(v, element);
  990. });
  991. };
  992. var prepend = function (parent, element) {
  993. var firstChild$1 = firstChild(parent);
  994. firstChild$1.fold(function () {
  995. append(parent, element);
  996. }, function (v) {
  997. parent.dom().insertBefore(element.dom(), v.dom());
  998. });
  999. };
  1000. var append = function (parent, element) {
  1001. parent.dom().appendChild(element.dom());
  1002. };
  1003. var wrap = function (element, wrapper) {
  1004. before(element, wrapper);
  1005. append(wrapper, element);
  1006. };
  1007. var before$1 = function (marker, elements) {
  1008. each(elements, function (x) {
  1009. before(marker, x);
  1010. });
  1011. };
  1012. var after$1 = function (marker, elements) {
  1013. each(elements, function (x, i) {
  1014. var e = i === 0 ? marker : elements[i - 1];
  1015. after(e, x);
  1016. });
  1017. };
  1018. var append$1 = function (parent, elements) {
  1019. each(elements, function (x) {
  1020. append(parent, x);
  1021. });
  1022. };
  1023. var empty = function (element) {
  1024. element.dom().textContent = '';
  1025. each(children(element), function (rogue) {
  1026. remove$2(rogue);
  1027. });
  1028. };
  1029. var remove$2 = function (element) {
  1030. var dom = element.dom();
  1031. if (dom.parentNode !== null) {
  1032. dom.parentNode.removeChild(dom);
  1033. }
  1034. };
  1035. var unwrap = function (wrapper) {
  1036. var children$1 = children(wrapper);
  1037. if (children$1.length > 0) {
  1038. before$1(wrapper, children$1);
  1039. }
  1040. remove$2(wrapper);
  1041. };
  1042. var grid = function (rows, columns) {
  1043. return {
  1044. rows: constant(rows),
  1045. columns: constant(columns)
  1046. };
  1047. };
  1048. var address = function (row, column) {
  1049. return {
  1050. row: constant(row),
  1051. column: constant(column)
  1052. };
  1053. };
  1054. var detail = function (element, rowspan, colspan) {
  1055. return {
  1056. element: constant(element),
  1057. rowspan: constant(rowspan),
  1058. colspan: constant(colspan)
  1059. };
  1060. };
  1061. var detailnew = function (element, rowspan, colspan, isNew) {
  1062. return {
  1063. element: constant(element),
  1064. rowspan: constant(rowspan),
  1065. colspan: constant(colspan),
  1066. isNew: constant(isNew)
  1067. };
  1068. };
  1069. var extended = function (element, rowspan, colspan, row, column) {
  1070. return {
  1071. element: constant(element),
  1072. rowspan: constant(rowspan),
  1073. colspan: constant(colspan),
  1074. row: constant(row),
  1075. column: constant(column)
  1076. };
  1077. };
  1078. var rowdata = function (element, cells, section) {
  1079. return {
  1080. element: constant(element),
  1081. cells: constant(cells),
  1082. section: constant(section)
  1083. };
  1084. };
  1085. var elementnew = function (element, isNew) {
  1086. return {
  1087. element: constant(element),
  1088. isNew: constant(isNew)
  1089. };
  1090. };
  1091. var rowdatanew = function (element, cells, section, isNew) {
  1092. return {
  1093. element: constant(element),
  1094. cells: constant(cells),
  1095. section: constant(section),
  1096. isNew: constant(isNew)
  1097. };
  1098. };
  1099. var rowcells = function (cells, section) {
  1100. return {
  1101. cells: constant(cells),
  1102. section: constant(section)
  1103. };
  1104. };
  1105. var rowdetails = function (details, section) {
  1106. return {
  1107. details: constant(details),
  1108. section: constant(section)
  1109. };
  1110. };
  1111. var bounds = function (startRow, startCol, finishRow, finishCol) {
  1112. return {
  1113. startRow: constant(startRow),
  1114. startCol: constant(startCol),
  1115. finishRow: constant(finishRow),
  1116. finishCol: constant(finishCol)
  1117. };
  1118. };
  1119. var ancestors = function (scope, predicate, isRoot) {
  1120. return filter(parents(scope, isRoot), predicate);
  1121. };
  1122. var children$1 = function (scope, predicate) {
  1123. return filter(children(scope), predicate);
  1124. };
  1125. var descendants = function (scope, predicate) {
  1126. var result = [];
  1127. each(children(scope), function (x) {
  1128. if (predicate(x)) {
  1129. result = result.concat([x]);
  1130. }
  1131. result = result.concat(descendants(x, predicate));
  1132. });
  1133. return result;
  1134. };
  1135. var ancestors$1 = function (scope, selector, isRoot) {
  1136. return ancestors(scope, function (e) {
  1137. return is(e, selector);
  1138. }, isRoot);
  1139. };
  1140. var children$2 = function (scope, selector) {
  1141. return children$1(scope, function (e) {
  1142. return is(e, selector);
  1143. });
  1144. };
  1145. var descendants$1 = function (scope, selector) {
  1146. return all(selector, scope);
  1147. };
  1148. function ClosestOrAncestor (is, ancestor, scope, a, isRoot) {
  1149. return is(scope, a) ? Option.some(scope) : isFunction(isRoot) && isRoot(scope) ? Option.none() : ancestor(scope, a, isRoot);
  1150. }
  1151. var ancestor = function (scope, predicate, isRoot) {
  1152. var element = scope.dom();
  1153. var stop = isFunction(isRoot) ? isRoot : constant(false);
  1154. while (element.parentNode) {
  1155. element = element.parentNode;
  1156. var el = Element.fromDom(element);
  1157. if (predicate(el)) {
  1158. return Option.some(el);
  1159. } else if (stop(el)) {
  1160. break;
  1161. }
  1162. }
  1163. return Option.none();
  1164. };
  1165. var closest = function (scope, predicate, isRoot) {
  1166. var is = function (s, test) {
  1167. return test(s);
  1168. };
  1169. return ClosestOrAncestor(is, ancestor, scope, predicate, isRoot);
  1170. };
  1171. var child$1 = function (scope, predicate) {
  1172. var pred = function (node) {
  1173. return predicate(Element.fromDom(node));
  1174. };
  1175. var result = find(scope.dom().childNodes, pred);
  1176. return result.map(Element.fromDom);
  1177. };
  1178. var descendant = function (scope, predicate) {
  1179. var descend = function (node) {
  1180. for (var i = 0; i < node.childNodes.length; i++) {
  1181. var child_1 = Element.fromDom(node.childNodes[i]);
  1182. if (predicate(child_1)) {
  1183. return Option.some(child_1);
  1184. }
  1185. var res = descend(node.childNodes[i]);
  1186. if (res.isSome()) {
  1187. return res;
  1188. }
  1189. }
  1190. return Option.none();
  1191. };
  1192. return descend(scope.dom());
  1193. };
  1194. var ancestor$1 = function (scope, selector, isRoot) {
  1195. return ancestor(scope, function (e) {
  1196. return is(e, selector);
  1197. }, isRoot);
  1198. };
  1199. var child$2 = function (scope, selector) {
  1200. return child$1(scope, function (e) {
  1201. return is(e, selector);
  1202. });
  1203. };
  1204. var descendant$1 = function (scope, selector) {
  1205. return one(selector, scope);
  1206. };
  1207. var closest$1 = function (scope, selector, isRoot) {
  1208. var is$1 = function (element, selector) {
  1209. return is(element, selector);
  1210. };
  1211. return ClosestOrAncestor(is$1, ancestor$1, scope, selector, isRoot);
  1212. };
  1213. var getAttrValue = function (cell, name, fallback) {
  1214. if (fallback === void 0) {
  1215. fallback = 0;
  1216. }
  1217. return getOpt(cell, name).map(function (value) {
  1218. return parseInt(value, 10);
  1219. }).getOr(fallback);
  1220. };
  1221. var getSpan = function (cell, type) {
  1222. return getAttrValue(cell, type, 1);
  1223. };
  1224. var hasColspan = function (cell) {
  1225. return getSpan(cell, 'colspan') > 1;
  1226. };
  1227. var hasRowspan = function (cell) {
  1228. return getSpan(cell, 'rowspan') > 1;
  1229. };
  1230. var getCssValue = function (element, property) {
  1231. return parseInt(get$2(element, property), 10);
  1232. };
  1233. var minWidth = constant(10);
  1234. var minHeight = constant(10);
  1235. var firstLayer = function (scope, selector) {
  1236. return filterFirstLayer(scope, selector, constant(true));
  1237. };
  1238. var filterFirstLayer = function (scope, selector, predicate) {
  1239. return bind(children(scope), function (x) {
  1240. return is(x, selector) ? predicate(x) ? [x] : [] : filterFirstLayer(x, selector, predicate);
  1241. });
  1242. };
  1243. var lookup = function (tags, element, isRoot) {
  1244. if (isRoot === void 0) {
  1245. isRoot = never;
  1246. }
  1247. if (isRoot(element)) {
  1248. return Option.none();
  1249. }
  1250. if (contains(tags, name(element))) {
  1251. return Option.some(element);
  1252. }
  1253. var isRootOrUpperTable = function (elm) {
  1254. return is(elm, 'table') || isRoot(elm);
  1255. };
  1256. return ancestor$1(element, tags.join(','), isRootOrUpperTable);
  1257. };
  1258. var cell = function (element, isRoot) {
  1259. return lookup([
  1260. 'td',
  1261. 'th'
  1262. ], element, isRoot);
  1263. };
  1264. var cells = function (ancestor) {
  1265. return firstLayer(ancestor, 'th,td');
  1266. };
  1267. var neighbours = function (selector, element) {
  1268. return parent(element).map(function (parent) {
  1269. return children$2(parent, selector);
  1270. });
  1271. };
  1272. var neighbourCells = curry(neighbours, 'th,td');
  1273. var neighbourRows = curry(neighbours, 'tr');
  1274. var table = function (element, isRoot) {
  1275. return closest$1(element, 'table', isRoot);
  1276. };
  1277. var rows = function (ancestor) {
  1278. return firstLayer(ancestor, 'tr');
  1279. };
  1280. var fromTable = function (table) {
  1281. var rows$1 = rows(table);
  1282. return map(rows$1, function (row) {
  1283. var element = row;
  1284. var parent$1 = parent(element);
  1285. var parentSection = parent$1.map(function (p) {
  1286. var parentName = name(p);
  1287. return parentName === 'tfoot' || parentName === 'thead' || parentName === 'tbody' ? parentName : 'tbody';
  1288. }).getOr('tbody');
  1289. var cells$1 = map(cells(row), function (cell) {
  1290. var rowspan = getAttrValue(cell, 'rowspan', 1);
  1291. var colspan = getAttrValue(cell, 'colspan', 1);
  1292. return detail(cell, rowspan, colspan);
  1293. });
  1294. return rowdata(element, cells$1, parentSection);
  1295. });
  1296. };
  1297. var fromPastedRows = function (rows, example) {
  1298. return map(rows, function (row) {
  1299. var cells$1 = map(cells(row), function (cell) {
  1300. var rowspan = getAttrValue(cell, 'rowspan', 1);
  1301. var colspan = getAttrValue(cell, 'colspan', 1);
  1302. return detail(cell, rowspan, colspan);
  1303. });
  1304. return rowdata(row, cells$1, example.section());
  1305. });
  1306. };
  1307. var key = function (row, column) {
  1308. return row + ',' + column;
  1309. };
  1310. var getAt = function (warehouse, row, column) {
  1311. var raw = warehouse.access[key(row, column)];
  1312. return raw !== undefined ? Option.some(raw) : Option.none();
  1313. };
  1314. var findItem = function (warehouse, item, comparator) {
  1315. var filtered = filterItems(warehouse, function (detail) {
  1316. return comparator(item, detail.element());
  1317. });
  1318. return filtered.length > 0 ? Option.some(filtered[0]) : Option.none();
  1319. };
  1320. var filterItems = function (warehouse, predicate) {
  1321. var all = bind(warehouse.all, function (r) {
  1322. return r.cells();
  1323. });
  1324. return filter(all, predicate);
  1325. };
  1326. var generate = function (list) {
  1327. var access = {};
  1328. var cells = [];
  1329. var maxRows = list.length;
  1330. var maxColumns = 0;
  1331. each(list, function (details, r) {
  1332. var currentRow = [];
  1333. each(details.cells(), function (detail) {
  1334. var start = 0;
  1335. while (access[key(r, start)] !== undefined) {
  1336. start++;
  1337. }
  1338. var current = extended(detail.element(), detail.rowspan(), detail.colspan(), r, start);
  1339. for (var i = 0; i < detail.colspan(); i++) {
  1340. for (var j = 0; j < detail.rowspan(); j++) {
  1341. var cr = r + j;
  1342. var cc = start + i;
  1343. var newpos = key(cr, cc);
  1344. access[newpos] = current;
  1345. maxColumns = Math.max(maxColumns, cc + 1);
  1346. }
  1347. }
  1348. currentRow.push(current);
  1349. });
  1350. cells.push(rowdata(details.element(), currentRow, details.section()));
  1351. });
  1352. var grid$1 = grid(maxRows, maxColumns);
  1353. return {
  1354. grid: grid$1,
  1355. access: access,
  1356. all: cells
  1357. };
  1358. };
  1359. var justCells = function (warehouse) {
  1360. var rows = map(warehouse.all, function (w) {
  1361. return w.cells();
  1362. });
  1363. return flatten(rows);
  1364. };
  1365. var Warehouse = {
  1366. generate: generate,
  1367. getAt: getAt,
  1368. findItem: findItem,
  1369. filterItems: filterItems,
  1370. justCells: justCells
  1371. };
  1372. var statsStruct = function (minRow, minCol, maxRow, maxCol) {
  1373. return {
  1374. minRow: minRow,
  1375. minCol: minCol,
  1376. maxRow: maxRow,
  1377. maxCol: maxCol
  1378. };
  1379. };
  1380. var findSelectedStats = function (house, isSelected) {
  1381. var totalColumns = house.grid.columns();
  1382. var totalRows = house.grid.rows();
  1383. var minRow = totalRows;
  1384. var minCol = totalColumns;
  1385. var maxRow = 0;
  1386. var maxCol = 0;
  1387. each$1(house.access, function (detail) {
  1388. if (isSelected(detail)) {
  1389. var startRow = detail.row();
  1390. var endRow = startRow + detail.rowspan() - 1;
  1391. var startCol = detail.column();
  1392. var endCol = startCol + detail.colspan() - 1;
  1393. if (startRow < minRow) {
  1394. minRow = startRow;
  1395. } else if (endRow > maxRow) {
  1396. maxRow = endRow;
  1397. }
  1398. if (startCol < minCol) {
  1399. minCol = startCol;
  1400. } else if (endCol > maxCol) {
  1401. maxCol = endCol;
  1402. }
  1403. }
  1404. });
  1405. return statsStruct(minRow, minCol, maxRow, maxCol);
  1406. };
  1407. var makeCell = function (list, seenSelected, rowIndex) {
  1408. var row = list[rowIndex].element();
  1409. var td = Element.fromTag('td');
  1410. append(td, Element.fromTag('br'));
  1411. var f = seenSelected ? append : prepend;
  1412. f(row, td);
  1413. };
  1414. var fillInGaps = function (list, house, stats, isSelected) {
  1415. var totalColumns = house.grid.columns();
  1416. var totalRows = house.grid.rows();
  1417. for (var i = 0; i < totalRows; i++) {
  1418. var seenSelected = false;
  1419. for (var j = 0; j < totalColumns; j++) {
  1420. if (!(i < stats.minRow || i > stats.maxRow || j < stats.minCol || j > stats.maxCol)) {
  1421. var needCell = Warehouse.getAt(house, i, j).filter(isSelected).isNone();
  1422. if (needCell) {
  1423. makeCell(list, seenSelected, i);
  1424. } else {
  1425. seenSelected = true;
  1426. }
  1427. }
  1428. }
  1429. }
  1430. };
  1431. var clean = function (table, stats) {
  1432. var emptyRows = filter(firstLayer(table, 'tr'), function (row) {
  1433. return row.dom().childElementCount === 0;
  1434. });
  1435. each(emptyRows, remove$2);
  1436. if (stats.minCol === stats.maxCol || stats.minRow === stats.maxRow) {
  1437. each(firstLayer(table, 'th,td'), function (cell) {
  1438. remove(cell, 'rowspan');
  1439. remove(cell, 'colspan');
  1440. });
  1441. }
  1442. remove(table, 'width');
  1443. remove(table, 'height');
  1444. remove$1(table, 'width');
  1445. remove$1(table, 'height');
  1446. };
  1447. var extract = function (table, selectedSelector) {
  1448. var isSelected = function (detail) {
  1449. return is(detail.element(), selectedSelector);
  1450. };
  1451. var list = fromTable(table);
  1452. var house = Warehouse.generate(list);
  1453. var stats = findSelectedStats(house, isSelected);
  1454. var selector = 'th:not(' + selectedSelector + ')' + ',td:not(' + selectedSelector + ')';
  1455. var unselectedCells = filterFirstLayer(table, 'th,td', function (cell) {
  1456. return is(cell, selector);
  1457. });
  1458. each(unselectedCells, remove$2);
  1459. fillInGaps(list, house, stats, isSelected);
  1460. clean(table, stats);
  1461. return table;
  1462. };
  1463. var nbsp = '\xA0';
  1464. function NodeValue (is, name) {
  1465. var get = function (element) {
  1466. if (!is(element)) {
  1467. throw new Error('Can only get ' + name + ' value of a ' + name + ' node');
  1468. }
  1469. return getOption(element).getOr('');
  1470. };
  1471. var getOption = function (element) {
  1472. return is(element) ? Option.from(element.dom().nodeValue) : Option.none();
  1473. };
  1474. var set = function (element, value) {
  1475. if (!is(element)) {
  1476. throw new Error('Can only set raw ' + name + ' value of a ' + name + ' node');
  1477. }
  1478. element.dom().nodeValue = value;
  1479. };
  1480. return {
  1481. get: get,
  1482. getOption: getOption,
  1483. set: set
  1484. };
  1485. }
  1486. var api = NodeValue(isText, 'text');
  1487. var get$3 = function (element) {
  1488. return api.get(element);
  1489. };
  1490. var getOption = function (element) {
  1491. return api.getOption(element);
  1492. };
  1493. var set$2 = function (element, value) {
  1494. return api.set(element, value);
  1495. };
  1496. var getEnd = function (element) {
  1497. return name(element) === 'img' ? 1 : getOption(element).fold(function () {
  1498. return children(element).length;
  1499. }, function (v) {
  1500. return v.length;
  1501. });
  1502. };
  1503. var isTextNodeWithCursorPosition = function (el) {
  1504. return getOption(el).filter(function (text) {
  1505. return text.trim().length !== 0 || text.indexOf(nbsp) > -1;
  1506. }).isSome();
  1507. };
  1508. var elementsWithCursorPosition = [
  1509. 'img',
  1510. 'br'
  1511. ];
  1512. var isCursorPosition = function (elem) {
  1513. var hasCursorPosition = isTextNodeWithCursorPosition(elem);
  1514. return hasCursorPosition || contains(elementsWithCursorPosition, name(elem));
  1515. };
  1516. var first = function (element) {
  1517. return descendant(element, isCursorPosition);
  1518. };
  1519. var last$1 = function (element) {
  1520. return descendantRtl(element, isCursorPosition);
  1521. };
  1522. var descendantRtl = function (scope, predicate) {
  1523. var descend = function (element) {
  1524. var children$1 = children(element);
  1525. for (var i = children$1.length - 1; i >= 0; i--) {
  1526. var child = children$1[i];
  1527. if (predicate(child)) {
  1528. return Option.some(child);
  1529. }
  1530. var res = descend(child);
  1531. if (res.isSome()) {
  1532. return res;
  1533. }
  1534. }
  1535. return Option.none();
  1536. };
  1537. return descend(scope);
  1538. };
  1539. var clone$1 = function (original, isDeep) {
  1540. return Element.fromDom(original.dom().cloneNode(isDeep));
  1541. };
  1542. var shallow = function (original) {
  1543. return clone$1(original, false);
  1544. };
  1545. var deep = function (original) {
  1546. return clone$1(original, true);
  1547. };
  1548. var shallowAs = function (original, tag) {
  1549. var nu = Element.fromTag(tag);
  1550. var attributes = clone(original);
  1551. setAll(nu, attributes);
  1552. return nu;
  1553. };
  1554. var copy$1 = function (original, tag) {
  1555. var nu = shallowAs(original, tag);
  1556. var cloneChildren = children(deep(original));
  1557. append$1(nu, cloneChildren);
  1558. return nu;
  1559. };
  1560. var createCell = function () {
  1561. var td = Element.fromTag('td');
  1562. append(td, Element.fromTag('br'));
  1563. return td;
  1564. };
  1565. var replace = function (cell, tag, attrs) {
  1566. var replica = copy$1(cell, tag);
  1567. each$1(attrs, function (v, k) {
  1568. if (v === null) {
  1569. remove(replica, k);
  1570. } else {
  1571. set(replica, k, v);
  1572. }
  1573. });
  1574. return replica;
  1575. };
  1576. var pasteReplace = function (cell) {
  1577. return cell;
  1578. };
  1579. var newRow = function (doc) {
  1580. return function () {
  1581. return Element.fromTag('tr', doc.dom());
  1582. };
  1583. };
  1584. var cloneFormats = function (oldCell, newCell, formats) {
  1585. var first$1 = first(oldCell);
  1586. return first$1.map(function (firstText) {
  1587. var formatSelector = formats.join(',');
  1588. var parents = ancestors$1(firstText, formatSelector, function (element) {
  1589. return eq(element, oldCell);
  1590. });
  1591. return foldr(parents, function (last, parent) {
  1592. var clonedFormat = shallow(parent);
  1593. remove(clonedFormat, 'contenteditable');
  1594. append(last, clonedFormat);
  1595. return clonedFormat;
  1596. }, newCell);
  1597. }).getOr(newCell);
  1598. };
  1599. var cellOperations = function (mutate, doc, formatsToClone) {
  1600. var newCell = function (prev) {
  1601. var docu = owner(prev.element());
  1602. var td = Element.fromTag(name(prev.element()), docu.dom());
  1603. var formats = formatsToClone.getOr([
  1604. 'strong',
  1605. 'em',
  1606. 'b',
  1607. 'i',
  1608. 'span',
  1609. 'font',
  1610. 'h1',
  1611. 'h2',
  1612. 'h3',
  1613. 'h4',
  1614. 'h5',
  1615. 'h6',
  1616. 'p',
  1617. 'div'
  1618. ]);
  1619. var lastNode = formats.length > 0 ? cloneFormats(prev.element(), td, formats) : td;
  1620. append(lastNode, Element.fromTag('br'));
  1621. copy(prev.element(), td);
  1622. remove$1(td, 'height');
  1623. if (prev.colspan() !== 1) {
  1624. remove$1(prev.element(), 'width');
  1625. }
  1626. mutate(prev.element(), td);
  1627. return td;
  1628. };
  1629. return {
  1630. row: newRow(doc),
  1631. cell: newCell,
  1632. replace: replace,
  1633. gap: createCell
  1634. };
  1635. };
  1636. var paste = function (doc) {
  1637. return {
  1638. row: newRow(doc),
  1639. cell: createCell,
  1640. replace: pasteReplace,
  1641. gap: createCell
  1642. };
  1643. };
  1644. var fromHtml$1 = function (html, scope) {
  1645. var doc = scope || domGlobals.document;
  1646. var div = doc.createElement('div');
  1647. div.innerHTML = html;
  1648. return children(Element.fromDom(div));
  1649. };
  1650. var inSelection = function (bounds, detail) {
  1651. var leftEdge = detail.column();
  1652. var rightEdge = detail.column() + detail.colspan() - 1;
  1653. var topEdge = detail.row();
  1654. var bottomEdge = detail.row() + detail.rowspan() - 1;
  1655. return leftEdge <= bounds.finishCol() && rightEdge >= bounds.startCol() && (topEdge <= bounds.finishRow() && bottomEdge >= bounds.startRow());
  1656. };
  1657. var isWithin = function (bounds, detail) {
  1658. return detail.column() >= bounds.startCol() && detail.column() + detail.colspan() - 1 <= bounds.finishCol() && detail.row() >= bounds.startRow() && detail.row() + detail.rowspan() - 1 <= bounds.finishRow();
  1659. };
  1660. var isRectangular = function (warehouse, bounds) {
  1661. var isRect = true;
  1662. var detailIsWithin = curry(isWithin, bounds);
  1663. for (var i = bounds.startRow(); i <= bounds.finishRow(); i++) {
  1664. for (var j = bounds.startCol(); j <= bounds.finishCol(); j++) {
  1665. isRect = isRect && Warehouse.getAt(warehouse, i, j).exists(detailIsWithin);
  1666. }
  1667. }
  1668. return isRect ? Option.some(bounds) : Option.none();
  1669. };
  1670. var getBounds = function (detailA, detailB) {
  1671. return bounds(Math.min(detailA.row(), detailB.row()), Math.min(detailA.column(), detailB.column()), Math.max(detailA.row() + detailA.rowspan() - 1, detailB.row() + detailB.rowspan() - 1), Math.max(detailA.column() + detailA.colspan() - 1, detailB.column() + detailB.colspan() - 1));
  1672. };
  1673. var getAnyBox = function (warehouse, startCell, finishCell) {
  1674. var startCoords = Warehouse.findItem(warehouse, startCell, eq);
  1675. var finishCoords = Warehouse.findItem(warehouse, finishCell, eq);
  1676. return startCoords.bind(function (sc) {
  1677. return finishCoords.map(function (fc) {
  1678. return getBounds(sc, fc);
  1679. });
  1680. });
  1681. };
  1682. var getBox = function (warehouse, startCell, finishCell) {
  1683. return getAnyBox(warehouse, startCell, finishCell).bind(function (bounds) {
  1684. return isRectangular(warehouse, bounds);
  1685. });
  1686. };
  1687. var moveBy = function (warehouse, cell, row, column) {
  1688. return Warehouse.findItem(warehouse, cell, eq).bind(function (detail) {
  1689. var startRow = row > 0 ? detail.row() + detail.rowspan() - 1 : detail.row();
  1690. var startCol = column > 0 ? detail.column() + detail.colspan() - 1 : detail.column();
  1691. var dest = Warehouse.getAt(warehouse, startRow + row, startCol + column);
  1692. return dest.map(function (d) {
  1693. return d.element();
  1694. });
  1695. });
  1696. };
  1697. var intercepts = function (warehouse, start, finish) {
  1698. return getAnyBox(warehouse, start, finish).map(function (bounds) {
  1699. var inside = Warehouse.filterItems(warehouse, curry(inSelection, bounds));
  1700. return map(inside, function (detail) {
  1701. return detail.element();
  1702. });
  1703. });
  1704. };
  1705. var parentCell = function (warehouse, innerCell) {
  1706. var isContainedBy = function (c1, c2) {
  1707. return contains$2(c2, c1);
  1708. };
  1709. return Warehouse.findItem(warehouse, innerCell, isContainedBy).map(function (detail) {
  1710. return detail.element();
  1711. });
  1712. };
  1713. var moveBy$1 = function (cell, deltaRow, deltaColumn) {
  1714. return table(cell).bind(function (table) {
  1715. var warehouse = getWarehouse(table);
  1716. return moveBy(warehouse, cell, deltaRow, deltaColumn);
  1717. });
  1718. };
  1719. var intercepts$1 = function (table, first, last) {
  1720. var warehouse = getWarehouse(table);
  1721. return intercepts(warehouse, first, last);
  1722. };
  1723. var nestedIntercepts = function (table, first, firstTable, last, lastTable) {
  1724. var warehouse = getWarehouse(table);
  1725. var optStartCell = eq(table, firstTable) ? Option.some(first) : parentCell(warehouse, first);
  1726. var optLastCell = eq(table, lastTable) ? Option.some(last) : parentCell(warehouse, last);
  1727. return optStartCell.bind(function (startCell) {
  1728. return optLastCell.bind(function (lastCell) {
  1729. return intercepts(warehouse, startCell, lastCell);
  1730. });
  1731. });
  1732. };
  1733. var getBox$1 = function (table, first, last) {
  1734. var warehouse = getWarehouse(table);
  1735. return getBox(warehouse, first, last);
  1736. };
  1737. var getWarehouse = function (table) {
  1738. var list = fromTable(table);
  1739. return Warehouse.generate(list);
  1740. };
  1741. var TagBoundaries = [
  1742. 'body',
  1743. 'p',
  1744. 'div',
  1745. 'article',
  1746. 'aside',
  1747. 'figcaption',
  1748. 'figure',
  1749. 'footer',
  1750. 'header',
  1751. 'nav',
  1752. 'section',
  1753. 'ol',
  1754. 'ul',
  1755. 'li',
  1756. 'table',
  1757. 'thead',
  1758. 'tbody',
  1759. 'tfoot',
  1760. 'caption',
  1761. 'tr',
  1762. 'td',
  1763. 'th',
  1764. 'h1',
  1765. 'h2',
  1766. 'h3',
  1767. 'h4',
  1768. 'h5',
  1769. 'h6',
  1770. 'blockquote',
  1771. 'pre',
  1772. 'address'
  1773. ];
  1774. function DomUniverse () {
  1775. var clone$1 = function (element) {
  1776. return Element.fromDom(element.dom().cloneNode(false));
  1777. };
  1778. var document = function (element) {
  1779. return element.dom().ownerDocument;
  1780. };
  1781. var isBoundary = function (element) {
  1782. if (!isElement(element)) {
  1783. return false;
  1784. }
  1785. if (name(element) === 'body') {
  1786. return true;
  1787. }
  1788. return contains(TagBoundaries, name(element));
  1789. };
  1790. var isEmptyTag = function (element) {
  1791. if (!isElement(element)) {
  1792. return false;
  1793. }
  1794. return contains([
  1795. 'br',
  1796. 'img',
  1797. 'hr',
  1798. 'input'
  1799. ], name(element));
  1800. };
  1801. var isNonEditable = function (element) {
  1802. return isElement(element) && get$1(element, 'contenteditable') === 'false';
  1803. };
  1804. var comparePosition = function (element, other) {
  1805. return element.dom().compareDocumentPosition(other.dom());
  1806. };
  1807. var copyAttributesTo = function (source, destination) {
  1808. var as = clone(source);
  1809. setAll(destination, as);
  1810. };
  1811. return {
  1812. up: constant({
  1813. selector: ancestor$1,
  1814. closest: closest$1,
  1815. predicate: ancestor,
  1816. all: parents
  1817. }),
  1818. down: constant({
  1819. selector: descendants$1,
  1820. predicate: descendants
  1821. }),
  1822. styles: constant({
  1823. get: get$2,
  1824. getRaw: getRaw,
  1825. set: set$1,
  1826. remove: remove$1
  1827. }),
  1828. attrs: constant({
  1829. get: get$1,
  1830. set: set,
  1831. remove: remove,
  1832. copyTo: copyAttributesTo
  1833. }),
  1834. insert: constant({
  1835. before: before,
  1836. after: after,
  1837. afterAll: after$1,
  1838. append: append,
  1839. appendAll: append$1,
  1840. prepend: prepend,
  1841. wrap: wrap
  1842. }),
  1843. remove: constant({
  1844. unwrap: unwrap,
  1845. remove: remove$2
  1846. }),
  1847. create: constant({
  1848. nu: Element.fromTag,
  1849. clone: clone$1,
  1850. text: Element.fromText
  1851. }),
  1852. query: constant({
  1853. comparePosition: comparePosition,
  1854. prevSibling: prevSibling,
  1855. nextSibling: nextSibling
  1856. }),
  1857. property: constant({
  1858. children: children,
  1859. name: name,
  1860. parent: parent,
  1861. document: document,
  1862. isText: isText,
  1863. isComment: isComment,
  1864. isElement: isElement,
  1865. getText: get$3,
  1866. setText: set$2,
  1867. isBoundary: isBoundary,
  1868. isEmptyTag: isEmptyTag,
  1869. isNonEditable: isNonEditable
  1870. }),
  1871. eq: eq,
  1872. is: is$1
  1873. };
  1874. }
  1875. var all$1 = function (universe, look, elements, f) {
  1876. var head = elements[0];
  1877. var tail = elements.slice(1);
  1878. return f(universe, look, head, tail);
  1879. };
  1880. var oneAll = function (universe, look, elements) {
  1881. return elements.length > 0 ? all$1(universe, look, elements, unsafeOne) : Option.none();
  1882. };
  1883. var unsafeOne = function (universe, look, head, tail) {
  1884. var start = look(universe, head);
  1885. return foldr(tail, function (b, a) {
  1886. var current = look(universe, a);
  1887. return commonElement(universe, b, current);
  1888. }, start);
  1889. };
  1890. var commonElement = function (universe, start, end) {
  1891. return start.bind(function (s) {
  1892. return end.filter(curry(universe.eq, s));
  1893. });
  1894. };
  1895. var eq$1 = function (universe, item) {
  1896. return curry(universe.eq, item);
  1897. };
  1898. var ancestors$2 = function (universe, start, end, isRoot) {
  1899. if (isRoot === void 0) {
  1900. isRoot = never;
  1901. }
  1902. var ps1 = [start].concat(universe.up().all(start));
  1903. var ps2 = [end].concat(universe.up().all(end));
  1904. var prune = function (path) {
  1905. var index = findIndex(path, isRoot);
  1906. return index.fold(function () {
  1907. return path;
  1908. }, function (ind) {
  1909. return path.slice(0, ind + 1);
  1910. });
  1911. };
  1912. var pruned1 = prune(ps1);
  1913. var pruned2 = prune(ps2);
  1914. var shared = find(pruned1, function (x) {
  1915. return exists(pruned2, eq$1(universe, x));
  1916. });
  1917. return {
  1918. firstpath: constant(pruned1),
  1919. secondpath: constant(pruned2),
  1920. shared: constant(shared)
  1921. };
  1922. };
  1923. var sharedOne = oneAll;
  1924. var ancestors$3 = ancestors$2;
  1925. var universe = DomUniverse();
  1926. var sharedOne$1 = function (look, elements) {
  1927. return sharedOne(universe, function (_universe, element) {
  1928. return look(element);
  1929. }, elements);
  1930. };
  1931. var ancestors$4 = function (start, finish, isRoot) {
  1932. return ancestors$3(universe, start, finish, isRoot);
  1933. };
  1934. var lookupTable = function (container) {
  1935. return ancestor$1(container, 'table');
  1936. };
  1937. var identify = function (start, finish, isRoot) {
  1938. var getIsRoot = function (rootTable) {
  1939. return function (element) {
  1940. return isRoot !== undefined && isRoot(element) || eq(element, rootTable);
  1941. };
  1942. };
  1943. if (eq(start, finish)) {
  1944. return Option.some({
  1945. boxes: Option.some([start]),
  1946. start: start,
  1947. finish: finish
  1948. });
  1949. } else {
  1950. return lookupTable(start).bind(function (startTable) {
  1951. return lookupTable(finish).bind(function (finishTable) {
  1952. if (eq(startTable, finishTable)) {
  1953. return Option.some({
  1954. boxes: intercepts$1(startTable, start, finish),
  1955. start: start,
  1956. finish: finish
  1957. });
  1958. } else if (contains$2(startTable, finishTable)) {
  1959. var ancestorCells = ancestors$1(finish, 'td,th', getIsRoot(startTable));
  1960. var finishCell = ancestorCells.length > 0 ? ancestorCells[ancestorCells.length - 1] : finish;
  1961. return Option.some({
  1962. boxes: nestedIntercepts(startTable, start, startTable, finish, finishTable),
  1963. start: start,
  1964. finish: finishCell
  1965. });
  1966. } else if (contains$2(finishTable, startTable)) {
  1967. var ancestorCells = ancestors$1(start, 'td,th', getIsRoot(finishTable));
  1968. var startCell = ancestorCells.length > 0 ? ancestorCells[ancestorCells.length - 1] : start;
  1969. return Option.some({
  1970. boxes: nestedIntercepts(finishTable, start, startTable, finish, finishTable),
  1971. start: start,
  1972. finish: startCell
  1973. });
  1974. } else {
  1975. return ancestors$4(start, finish).shared().bind(function (lca) {
  1976. return closest$1(lca, 'table', isRoot).bind(function (lcaTable) {
  1977. var finishAncestorCells = ancestors$1(finish, 'td,th', getIsRoot(lcaTable));
  1978. var finishCell = finishAncestorCells.length > 0 ? finishAncestorCells[finishAncestorCells.length - 1] : finish;
  1979. var startAncestorCells = ancestors$1(start, 'td,th', getIsRoot(lcaTable));
  1980. var startCell = startAncestorCells.length > 0 ? startAncestorCells[startAncestorCells.length - 1] : start;
  1981. return Option.some({
  1982. boxes: nestedIntercepts(lcaTable, start, startTable, finish, finishTable),
  1983. start: startCell,
  1984. finish: finishCell
  1985. });
  1986. });
  1987. });
  1988. }
  1989. });
  1990. });
  1991. }
  1992. };
  1993. var retrieve = function (container, selector) {
  1994. var sels = descendants$1(container, selector);
  1995. return sels.length > 0 ? Option.some(sels) : Option.none();
  1996. };
  1997. var getLast = function (boxes, lastSelectedSelector) {
  1998. return find(boxes, function (box) {
  1999. return is(box, lastSelectedSelector);
  2000. });
  2001. };
  2002. var getEdges = function (container, firstSelectedSelector, lastSelectedSelector) {
  2003. return descendant$1(container, firstSelectedSelector).bind(function (first) {
  2004. return descendant$1(container, lastSelectedSelector).bind(function (last) {
  2005. return sharedOne$1(lookupTable, [
  2006. first,
  2007. last
  2008. ]).map(function (tbl) {
  2009. return {
  2010. first: constant(first),
  2011. last: constant(last),
  2012. table: constant(tbl)
  2013. };
  2014. });
  2015. });
  2016. });
  2017. };
  2018. var expandTo = function (finish, firstSelectedSelector) {
  2019. return ancestor$1(finish, 'table').bind(function (table) {
  2020. return descendant$1(table, firstSelectedSelector).bind(function (start) {
  2021. return identify(start, finish).bind(function (identified) {
  2022. return identified.boxes.map(function (boxes) {
  2023. return {
  2024. boxes: boxes,
  2025. start: identified.start,
  2026. finish: identified.finish
  2027. };
  2028. });
  2029. });
  2030. });
  2031. });
  2032. };
  2033. var shiftSelection = function (boxes, deltaRow, deltaColumn, firstSelectedSelector, lastSelectedSelector) {
  2034. return getLast(boxes, lastSelectedSelector).bind(function (last) {
  2035. return moveBy$1(last, deltaRow, deltaColumn).bind(function (finish) {
  2036. return expandTo(finish, firstSelectedSelector);
  2037. });
  2038. });
  2039. };
  2040. var retrieve$1 = function (container, selector) {
  2041. return retrieve(container, selector);
  2042. };
  2043. var retrieveBox = function (container, firstSelectedSelector, lastSelectedSelector) {
  2044. return getEdges(container, firstSelectedSelector, lastSelectedSelector).bind(function (edges) {
  2045. var isRoot = function (ancestor) {
  2046. return eq(container, ancestor);
  2047. };
  2048. var firstAncestor = ancestor$1(edges.first(), 'thead,tfoot,tbody,table', isRoot);
  2049. var lastAncestor = ancestor$1(edges.last(), 'thead,tfoot,tbody,table', isRoot);
  2050. return firstAncestor.bind(function (fA) {
  2051. return lastAncestor.bind(function (lA) {
  2052. return eq(fA, lA) ? getBox$1(edges.table(), edges.first(), edges.last()) : Option.none();
  2053. });
  2054. });
  2055. });
  2056. };
  2057. var strSelected = 'data-mce-selected';
  2058. var strSelectedSelector = 'td[' + strSelected + '],th[' + strSelected + ']';
  2059. var strAttributeSelector = '[' + strSelected + ']';
  2060. var strFirstSelected = 'data-mce-first-selected';
  2061. var strFirstSelectedSelector = 'td[' + strFirstSelected + '],th[' + strFirstSelected + ']';
  2062. var strLastSelected = 'data-mce-last-selected';
  2063. var strLastSelectedSelector = 'td[' + strLastSelected + '],th[' + strLastSelected + ']';
  2064. var selected = strSelected;
  2065. var selectedSelector = strSelectedSelector;
  2066. var attributeSelector = strAttributeSelector;
  2067. var firstSelected = strFirstSelected;
  2068. var firstSelectedSelector = strFirstSelectedSelector;
  2069. var lastSelected = strLastSelected;
  2070. var lastSelectedSelector = strLastSelectedSelector;
  2071. var Ephemera = /*#__PURE__*/Object.freeze({
  2072. __proto__: null,
  2073. selected: selected,
  2074. selectedSelector: selectedSelector,
  2075. attributeSelector: attributeSelector,
  2076. firstSelected: firstSelected,
  2077. firstSelectedSelector: firstSelectedSelector,
  2078. lastSelected: lastSelected,
  2079. lastSelectedSelector: lastSelectedSelector
  2080. });
  2081. var generate$1 = function (cases) {
  2082. if (!isArray(cases)) {
  2083. throw new Error('cases must be an array');
  2084. }
  2085. if (cases.length === 0) {
  2086. throw new Error('there must be at least one case');
  2087. }
  2088. var constructors = [];
  2089. var adt = {};
  2090. each(cases, function (acase, count) {
  2091. var keys$1 = keys(acase);
  2092. if (keys$1.length !== 1) {
  2093. throw new Error('one and only one name per case');
  2094. }
  2095. var key = keys$1[0];
  2096. var value = acase[key];
  2097. if (adt[key] !== undefined) {
  2098. throw new Error('duplicate key detected:' + key);
  2099. } else if (key === 'cata') {
  2100. throw new Error('cannot have a case named cata (sorry)');
  2101. } else if (!isArray(value)) {
  2102. throw new Error('case arguments must be an array');
  2103. }
  2104. constructors.push(key);
  2105. adt[key] = function () {
  2106. var argLength = arguments.length;
  2107. if (argLength !== value.length) {
  2108. throw new Error('Wrong number of arguments to case ' + key + '. Expected ' + value.length + ' (' + value + '), got ' + argLength);
  2109. }
  2110. var args = new Array(argLength);
  2111. for (var i = 0; i < args.length; i++) {
  2112. args[i] = arguments[i];
  2113. }
  2114. var match = function (branches) {
  2115. var branchKeys = keys(branches);
  2116. if (constructors.length !== branchKeys.length) {
  2117. throw new Error('Wrong number of arguments to match. Expected: ' + constructors.join(',') + '\nActual: ' + branchKeys.join(','));
  2118. }
  2119. var allReqd = forall(constructors, function (reqKey) {
  2120. return contains(branchKeys, reqKey);
  2121. });
  2122. if (!allReqd) {
  2123. throw new Error('Not all branches were specified when using match. Specified: ' + branchKeys.join(', ') + '\nRequired: ' + constructors.join(', '));
  2124. }
  2125. return branches[key].apply(null, args);
  2126. };
  2127. return {
  2128. fold: function () {
  2129. if (arguments.length !== cases.length) {
  2130. throw new Error('Wrong number of arguments to fold. Expected ' + cases.length + ', got ' + arguments.length);
  2131. }
  2132. var target = arguments[count];
  2133. return target.apply(null, args);
  2134. },
  2135. match: match,
  2136. log: function (label) {
  2137. domGlobals.console.log(label, {
  2138. constructors: constructors,
  2139. constructor: key,
  2140. params: args
  2141. });
  2142. }
  2143. };
  2144. };
  2145. });
  2146. return adt;
  2147. };
  2148. var Adt = { generate: generate$1 };
  2149. var type$1 = Adt.generate([
  2150. { none: [] },
  2151. { multiple: ['elements'] },
  2152. { single: ['selection'] }
  2153. ]);
  2154. var cata = function (subject, onNone, onMultiple, onSingle) {
  2155. return subject.fold(onNone, onMultiple, onSingle);
  2156. };
  2157. var none$1 = type$1.none;
  2158. var multiple = type$1.multiple;
  2159. var single = type$1.single;
  2160. var selection = function (cell, selections) {
  2161. return cata(selections.get(), constant([]), identity, constant([cell]));
  2162. };
  2163. var unmergable = function (cell, selections) {
  2164. var hasSpan = function (elem) {
  2165. return has$1(elem, 'rowspan') && parseInt(get$1(elem, 'rowspan'), 10) > 1 || has$1(elem, 'colspan') && parseInt(get$1(elem, 'colspan'), 10) > 1;
  2166. };
  2167. var candidates = selection(cell, selections);
  2168. return candidates.length > 0 && forall(candidates, hasSpan) ? Option.some(candidates) : Option.none();
  2169. };
  2170. var mergable = function (table, selections) {
  2171. return cata(selections.get(), Option.none, function (cells, _env) {
  2172. if (cells.length === 0) {
  2173. return Option.none();
  2174. }
  2175. return retrieveBox(table, firstSelectedSelector, lastSelectedSelector).bind(function (bounds) {
  2176. return cells.length > 1 ? Option.some({
  2177. bounds: constant(bounds),
  2178. cells: constant(cells)
  2179. }) : Option.none();
  2180. });
  2181. }, Option.none);
  2182. };
  2183. var noMenu = function (cell) {
  2184. return {
  2185. element: constant(cell),
  2186. mergable: Option.none,
  2187. unmergable: Option.none,
  2188. selection: constant([cell])
  2189. };
  2190. };
  2191. var forMenu = function (selections, table, cell) {
  2192. return {
  2193. element: constant(cell),
  2194. mergable: constant(mergable(table, selections)),
  2195. unmergable: constant(unmergable(cell, selections)),
  2196. selection: constant(selection(cell, selections))
  2197. };
  2198. };
  2199. var notCell = function (element) {
  2200. return noMenu(element);
  2201. };
  2202. var paste$1 = function (element, clipboard, generators) {
  2203. return {
  2204. element: constant(element),
  2205. clipboard: constant(clipboard),
  2206. generators: constant(generators)
  2207. };
  2208. };
  2209. var pasteRows = function (selections, table, cell, clipboard, generators) {
  2210. return {
  2211. element: constant(cell),
  2212. mergable: Option.none,
  2213. unmergable: Option.none,
  2214. selection: constant(selection(cell, selections)),
  2215. clipboard: constant(clipboard),
  2216. generators: constant(generators)
  2217. };
  2218. };
  2219. var extractSelected = function (cells) {
  2220. return table(cells[0]).map(deep).map(function (replica) {
  2221. return [extract(replica, attributeSelector)];
  2222. });
  2223. };
  2224. var serializeElements = function (editor, elements) {
  2225. return map(elements, function (elm) {
  2226. return editor.selection.serializer.serialize(elm.dom(), {});
  2227. }).join('');
  2228. };
  2229. var getTextContent = function (elements) {
  2230. return map(elements, function (element) {
  2231. return element.dom().innerText;
  2232. }).join('');
  2233. };
  2234. var registerEvents = function (editor, selections, actions, cellSelection) {
  2235. editor.on('BeforeGetContent', function (e) {
  2236. var multiCellContext = function (cells) {
  2237. e.preventDefault();
  2238. extractSelected(cells).each(function (elements) {
  2239. e.content = e.format === 'text' ? getTextContent(elements) : serializeElements(editor, elements);
  2240. });
  2241. };
  2242. if (e.selection === true) {
  2243. cata(selections.get(), noop, multiCellContext, noop);
  2244. }
  2245. });
  2246. editor.on('BeforeSetContent', function (e) {
  2247. if (e.selection === true && e.paste === true) {
  2248. var cellOpt = Option.from(editor.dom.getParent(editor.selection.getStart(), 'th,td'));
  2249. cellOpt.each(function (domCell) {
  2250. var cell = Element.fromDom(domCell);
  2251. table(cell).each(function (table) {
  2252. var elements = filter(fromHtml$1(e.content), function (content) {
  2253. return name(content) !== 'meta';
  2254. });
  2255. if (elements.length === 1 && name(elements[0]) === 'table') {
  2256. e.preventDefault();
  2257. var doc = Element.fromDom(editor.getDoc());
  2258. var generators = paste(doc);
  2259. var targets = paste$1(cell, elements[0], generators);
  2260. actions.pasteCells(table, targets).each(function (rng) {
  2261. editor.selection.setRng(rng);
  2262. editor.focus();
  2263. cellSelection.clear(table);
  2264. });
  2265. }
  2266. });
  2267. });
  2268. }
  2269. });
  2270. };
  2271. function Dimension (name, getOffset) {
  2272. var set = function (element, h) {
  2273. if (!isNumber(h) && !h.match(/^[0-9]+$/)) {
  2274. throw new Error(name + '.set accepts only positive integer values. Value was ' + h);
  2275. }
  2276. var dom = element.dom();
  2277. if (isSupported(dom)) {
  2278. dom.style[name] = h + 'px';
  2279. }
  2280. };
  2281. var get = function (element) {
  2282. var r = getOffset(element);
  2283. if (r <= 0 || r === null) {
  2284. var css = get$2(element, name);
  2285. return parseFloat(css) || 0;
  2286. }
  2287. return r;
  2288. };
  2289. var getOuter = get;
  2290. var aggregate = function (element, properties) {
  2291. return foldl(properties, function (acc, property) {
  2292. var val = get$2(element, property);
  2293. var value = val === undefined ? 0 : parseInt(val, 10);
  2294. return isNaN(value) ? acc : acc + value;
  2295. }, 0);
  2296. };
  2297. var max = function (element, value, properties) {
  2298. var cumulativeInclusions = aggregate(element, properties);
  2299. var absoluteMax = value > cumulativeInclusions ? value - cumulativeInclusions : 0;
  2300. return absoluteMax;
  2301. };
  2302. return {
  2303. set: set,
  2304. get: get,
  2305. getOuter: getOuter,
  2306. aggregate: aggregate,
  2307. max: max
  2308. };
  2309. }
  2310. var api$1 = Dimension('height', function (element) {
  2311. var dom = element.dom();
  2312. return inBody(element) ? dom.getBoundingClientRect().height : dom.offsetHeight;
  2313. });
  2314. var get$4 = function (element) {
  2315. return api$1.get(element);
  2316. };
  2317. var getOuter = function (element) {
  2318. return api$1.getOuter(element);
  2319. };
  2320. var api$2 = Dimension('width', function (element) {
  2321. return element.dom().offsetWidth;
  2322. });
  2323. var get$5 = function (element) {
  2324. return api$2.get(element);
  2325. };
  2326. var getOuter$1 = function (element) {
  2327. return api$2.getOuter(element);
  2328. };
  2329. var needManualCalc = function () {
  2330. var platform = detect$3();
  2331. return platform.browser.isIE() || platform.browser.isEdge();
  2332. };
  2333. var toNumber = function (px, fallback) {
  2334. var num = parseFloat(px);
  2335. return isNaN(num) ? fallback : num;
  2336. };
  2337. var getProp = function (elm, name, fallback) {
  2338. return toNumber(get$2(elm, name), fallback);
  2339. };
  2340. var getCalculatedHeight = function (cell) {
  2341. var paddingTop = getProp(cell, 'padding-top', 0);
  2342. var paddingBottom = getProp(cell, 'padding-bottom', 0);
  2343. var borderTop = getProp(cell, 'border-top-width', 0);
  2344. var borderBottom = getProp(cell, 'border-bottom-width', 0);
  2345. var height = cell.dom().getBoundingClientRect().height;
  2346. var boxSizing = get$2(cell, 'box-sizing');
  2347. var borders = borderTop + borderBottom;
  2348. return boxSizing === 'border-box' ? height : height - paddingTop - paddingBottom - borders;
  2349. };
  2350. var getHeight = function (cell) {
  2351. return needManualCalc() ? getCalculatedHeight(cell) : getProp(cell, 'height', get$4(cell));
  2352. };
  2353. var rGenericSizeRegex = /(\d+(\.\d+)?)(\w|%)*/;
  2354. var rPercentageBasedSizeRegex = /(\d+(\.\d+)?)%/;
  2355. var rPixelBasedSizeRegex = /(\d+(\.\d+)?)px|em/;
  2356. var setPixelWidth = function (cell, amount) {
  2357. set$1(cell, 'width', amount + 'px');
  2358. };
  2359. var setPercentageWidth = function (cell, amount) {
  2360. set$1(cell, 'width', amount + '%');
  2361. };
  2362. var setHeight = function (cell, amount) {
  2363. set$1(cell, 'height', amount + 'px');
  2364. };
  2365. var getHeightValue = function (cell) {
  2366. return getRaw(cell, 'height').getOrThunk(function () {
  2367. return getHeight(cell) + 'px';
  2368. });
  2369. };
  2370. var convert = function (cell, number, getter, setter) {
  2371. var newSize = table(cell).map(function (table) {
  2372. var total = getter(table);
  2373. return Math.floor(number / 100 * total);
  2374. }).getOr(number);
  2375. setter(cell, newSize);
  2376. return newSize;
  2377. };
  2378. var normalizePixelSize = function (value, cell, getter, setter) {
  2379. var number = parseInt(value, 10);
  2380. return endsWith(value, '%') && name(cell) !== 'table' ? convert(cell, number, getter, setter) : number;
  2381. };
  2382. var getTotalHeight = function (cell) {
  2383. var value = getHeightValue(cell);
  2384. if (!value) {
  2385. return get$4(cell);
  2386. }
  2387. return normalizePixelSize(value, cell, get$4, setHeight);
  2388. };
  2389. var get$6 = function (cell, type, f) {
  2390. var v = f(cell);
  2391. var span = getSpan(cell, type);
  2392. return v / span;
  2393. };
  2394. var getRawWidth = function (element) {
  2395. var cssWidth = getRaw(element, 'width');
  2396. return cssWidth.fold(function () {
  2397. return Option.from(get$1(element, 'width'));
  2398. }, function (width) {
  2399. return Option.some(width);
  2400. });
  2401. };
  2402. var normalizePercentageWidth = function (cellWidth, tableSize) {
  2403. return cellWidth / tableSize.pixelWidth() * 100;
  2404. };
  2405. var choosePercentageSize = function (element, width, tableSize) {
  2406. var percentMatch = rPercentageBasedSizeRegex.exec(width);
  2407. if (percentMatch !== null) {
  2408. return parseFloat(percentMatch[1]);
  2409. } else {
  2410. var intWidth = get$5(element);
  2411. return normalizePercentageWidth(intWidth, tableSize);
  2412. }
  2413. };
  2414. var getPercentageWidth = function (cell, tableSize) {
  2415. var width = getRawWidth(cell);
  2416. return width.fold(function () {
  2417. var intWidth = get$5(cell);
  2418. return normalizePercentageWidth(intWidth, tableSize);
  2419. }, function (w) {
  2420. return choosePercentageSize(cell, w, tableSize);
  2421. });
  2422. };
  2423. var normalizePixelWidth = function (cellWidth, tableSize) {
  2424. return cellWidth / 100 * tableSize.pixelWidth();
  2425. };
  2426. var choosePixelSize = function (element, width, tableSize) {
  2427. var pixelMatch = rPixelBasedSizeRegex.exec(width);
  2428. if (pixelMatch !== null) {
  2429. return parseInt(pixelMatch[1], 10);
  2430. }
  2431. var percentMatch = rPercentageBasedSizeRegex.exec(width);
  2432. if (percentMatch !== null) {
  2433. var floatWidth = parseFloat(percentMatch[1]);
  2434. return normalizePixelWidth(floatWidth, tableSize);
  2435. }
  2436. return get$5(element);
  2437. };
  2438. var getPixelWidth = function (cell, tableSize) {
  2439. var width = getRawWidth(cell);
  2440. return width.fold(function () {
  2441. return get$5(cell);
  2442. }, function (w) {
  2443. return choosePixelSize(cell, w, tableSize);
  2444. });
  2445. };
  2446. var getHeight$1 = function (cell) {
  2447. return get$6(cell, 'rowspan', getTotalHeight);
  2448. };
  2449. var getGenericWidth = function (cell) {
  2450. var width = getRawWidth(cell);
  2451. return width.bind(function (w) {
  2452. var match = rGenericSizeRegex.exec(w);
  2453. if (match !== null) {
  2454. return Option.some({
  2455. width: constant(parseFloat(match[1])),
  2456. unit: constant(match[3])
  2457. });
  2458. } else {
  2459. return Option.none();
  2460. }
  2461. });
  2462. };
  2463. var setGenericWidth = function (cell, amount, unit) {
  2464. set$1(cell, 'width', amount + unit);
  2465. };
  2466. var percentageBasedSizeRegex = constant(rPercentageBasedSizeRegex);
  2467. var pixelBasedSizeRegex = constant(rPixelBasedSizeRegex);
  2468. var halve = function (main, other) {
  2469. var width = getGenericWidth(main);
  2470. width.each(function (w) {
  2471. var newWidth = w.width() / 2;
  2472. setGenericWidth(main, newWidth, w.unit());
  2473. setGenericWidth(other, newWidth, w.unit());
  2474. });
  2475. };
  2476. var r = function (left, top) {
  2477. var translate = function (x, y) {
  2478. return r(left + x, top + y);
  2479. };
  2480. return {
  2481. left: constant(left),
  2482. top: constant(top),
  2483. translate: translate
  2484. };
  2485. };
  2486. var Position = r;
  2487. var boxPosition = function (dom) {
  2488. var box = dom.getBoundingClientRect();
  2489. return Position(box.left, box.top);
  2490. };
  2491. var firstDefinedOrZero = function (a, b) {
  2492. if (a !== undefined) {
  2493. return a;
  2494. } else {
  2495. return b !== undefined ? b : 0;
  2496. }
  2497. };
  2498. var absolute = function (element) {
  2499. var doc = element.dom().ownerDocument;
  2500. var body = doc.body;
  2501. var win = doc.defaultView;
  2502. var html = doc.documentElement;
  2503. if (body === element.dom()) {
  2504. return Position(body.offsetLeft, body.offsetTop);
  2505. }
  2506. var scrollTop = firstDefinedOrZero(win.pageYOffset, html.scrollTop);
  2507. var scrollLeft = firstDefinedOrZero(win.pageXOffset, html.scrollLeft);
  2508. var clientTop = firstDefinedOrZero(html.clientTop, body.clientTop);
  2509. var clientLeft = firstDefinedOrZero(html.clientLeft, body.clientLeft);
  2510. return viewport(element).translate(scrollLeft - clientLeft, scrollTop - clientTop);
  2511. };
  2512. var viewport = function (element) {
  2513. var dom = element.dom();
  2514. var doc = dom.ownerDocument;
  2515. var body = doc.body;
  2516. if (body === dom) {
  2517. return Position(body.offsetLeft, body.offsetTop);
  2518. }
  2519. if (!inBody(element)) {
  2520. return Position(0, 0);
  2521. }
  2522. return boxPosition(dom);
  2523. };
  2524. var rowInfo = function (row, y) {
  2525. return {
  2526. row: row,
  2527. y: y
  2528. };
  2529. };
  2530. var colInfo = function (col, x) {
  2531. return {
  2532. col: col,
  2533. x: x
  2534. };
  2535. };
  2536. var rtlEdge = function (cell) {
  2537. var pos = absolute(cell);
  2538. return pos.left() + getOuter$1(cell);
  2539. };
  2540. var ltrEdge = function (cell) {
  2541. return absolute(cell).left();
  2542. };
  2543. var getLeftEdge = function (index, cell) {
  2544. return colInfo(index, ltrEdge(cell));
  2545. };
  2546. var getRightEdge = function (index, cell) {
  2547. return colInfo(index, rtlEdge(cell));
  2548. };
  2549. var getTop = function (cell) {
  2550. return absolute(cell).top();
  2551. };
  2552. var getTopEdge = function (index, cell) {
  2553. return rowInfo(index, getTop(cell));
  2554. };
  2555. var getBottomEdge = function (index, cell) {
  2556. return rowInfo(index, getTop(cell) + getOuter(cell));
  2557. };
  2558. var findPositions = function (getInnerEdge, getOuterEdge, array) {
  2559. if (array.length === 0) {
  2560. return [];
  2561. }
  2562. var lines = map(array.slice(1), function (cellOption, index) {
  2563. return cellOption.map(function (cell) {
  2564. return getInnerEdge(index, cell);
  2565. });
  2566. });
  2567. var lastLine = array[array.length - 1].map(function (cell) {
  2568. return getOuterEdge(array.length - 1, cell);
  2569. });
  2570. return lines.concat([lastLine]);
  2571. };
  2572. var negate = function (step) {
  2573. return -step;
  2574. };
  2575. var height = {
  2576. delta: identity,
  2577. positions: function (optElements) {
  2578. return findPositions(getTopEdge, getBottomEdge, optElements);
  2579. },
  2580. edge: getTop
  2581. };
  2582. var ltr = {
  2583. delta: identity,
  2584. edge: ltrEdge,
  2585. positions: function (optElements) {
  2586. return findPositions(getLeftEdge, getRightEdge, optElements);
  2587. }
  2588. };
  2589. var rtl = {
  2590. delta: negate,
  2591. edge: rtlEdge,
  2592. positions: function (optElements) {
  2593. return findPositions(getRightEdge, getLeftEdge, optElements);
  2594. }
  2595. };
  2596. var ResizeDirection = {
  2597. ltr: ltr,
  2598. rtl: rtl
  2599. };
  2600. function TableDirection (directionAt) {
  2601. var auto = function (table) {
  2602. return directionAt(table).isRtl() ? ResizeDirection.rtl : ResizeDirection.ltr;
  2603. };
  2604. var delta = function (amount, table) {
  2605. return auto(table).delta(amount, table);
  2606. };
  2607. var positions = function (cols, table) {
  2608. return auto(table).positions(cols, table);
  2609. };
  2610. var edge = function (cell) {
  2611. return auto(cell).edge(cell);
  2612. };
  2613. return {
  2614. delta: delta,
  2615. edge: edge,
  2616. positions: positions
  2617. };
  2618. }
  2619. var getGridSize = function (table) {
  2620. var input = fromTable(table);
  2621. var warehouse = Warehouse.generate(input);
  2622. return warehouse.grid;
  2623. };
  2624. var cat = function (arr) {
  2625. var r = [];
  2626. var push = function (x) {
  2627. r.push(x);
  2628. };
  2629. for (var i = 0; i < arr.length; i++) {
  2630. arr[i].each(push);
  2631. }
  2632. return r;
  2633. };
  2634. var setIfNot = function (element, property, value, ignore) {
  2635. if (value === ignore) {
  2636. remove(element, property);
  2637. } else {
  2638. set(element, property, value);
  2639. }
  2640. };
  2641. var render = function (table, grid) {
  2642. var newRows = [];
  2643. var newCells = [];
  2644. var renderSection = function (gridSection, sectionName) {
  2645. var section = child$2(table, sectionName).getOrThunk(function () {
  2646. var tb = Element.fromTag(sectionName, owner(table).dom());
  2647. append(table, tb);
  2648. return tb;
  2649. });
  2650. empty(section);
  2651. var rows = map(gridSection, function (row) {
  2652. if (row.isNew()) {
  2653. newRows.push(row.element());
  2654. }
  2655. var tr = row.element();
  2656. empty(tr);
  2657. each(row.cells(), function (cell) {
  2658. if (cell.isNew()) {
  2659. newCells.push(cell.element());
  2660. }
  2661. setIfNot(cell.element(), 'colspan', cell.colspan(), 1);
  2662. setIfNot(cell.element(), 'rowspan', cell.rowspan(), 1);
  2663. append(tr, cell.element());
  2664. });
  2665. return tr;
  2666. });
  2667. append$1(section, rows);
  2668. };
  2669. var removeSection = function (sectionName) {
  2670. child$2(table, sectionName).each(remove$2);
  2671. };
  2672. var renderOrRemoveSection = function (gridSection, sectionName) {
  2673. if (gridSection.length > 0) {
  2674. renderSection(gridSection, sectionName);
  2675. } else {
  2676. removeSection(sectionName);
  2677. }
  2678. };
  2679. var headSection = [];
  2680. var bodySection = [];
  2681. var footSection = [];
  2682. each(grid, function (row) {
  2683. switch (row.section()) {
  2684. case 'thead':
  2685. headSection.push(row);
  2686. break;
  2687. case 'tbody':
  2688. bodySection.push(row);
  2689. break;
  2690. case 'tfoot':
  2691. footSection.push(row);
  2692. break;
  2693. }
  2694. });
  2695. renderOrRemoveSection(headSection, 'thead');
  2696. renderOrRemoveSection(bodySection, 'tbody');
  2697. renderOrRemoveSection(footSection, 'tfoot');
  2698. return {
  2699. newRows: newRows,
  2700. newCells: newCells
  2701. };
  2702. };
  2703. var copy$2 = function (grid) {
  2704. return map(grid, function (row) {
  2705. var tr = shallow(row.element());
  2706. each(row.cells(), function (cell) {
  2707. var clonedCell = deep(cell.element());
  2708. setIfNot(clonedCell, 'colspan', cell.colspan(), 1);
  2709. setIfNot(clonedCell, 'rowspan', cell.rowspan(), 1);
  2710. append(tr, clonedCell);
  2711. });
  2712. return tr;
  2713. });
  2714. };
  2715. var read = function (element, attr) {
  2716. var value = get$1(element, attr);
  2717. return value === undefined || value === '' ? [] : value.split(' ');
  2718. };
  2719. var add = function (element, attr, id) {
  2720. var old = read(element, attr);
  2721. var nu = old.concat([id]);
  2722. set(element, attr, nu.join(' '));
  2723. return true;
  2724. };
  2725. var remove$3 = function (element, attr, id) {
  2726. var nu = filter(read(element, attr), function (v) {
  2727. return v !== id;
  2728. });
  2729. if (nu.length > 0) {
  2730. set(element, attr, nu.join(' '));
  2731. } else {
  2732. remove(element, attr);
  2733. }
  2734. return false;
  2735. };
  2736. var supports = function (element) {
  2737. return element.dom().classList !== undefined;
  2738. };
  2739. var get$7 = function (element) {
  2740. return read(element, 'class');
  2741. };
  2742. var add$1 = function (element, clazz) {
  2743. return add(element, 'class', clazz);
  2744. };
  2745. var remove$4 = function (element, clazz) {
  2746. return remove$3(element, 'class', clazz);
  2747. };
  2748. var add$2 = function (element, clazz) {
  2749. if (supports(element)) {
  2750. element.dom().classList.add(clazz);
  2751. } else {
  2752. add$1(element, clazz);
  2753. }
  2754. };
  2755. var cleanClass = function (element) {
  2756. var classList = supports(element) ? element.dom().classList : get$7(element);
  2757. if (classList.length === 0) {
  2758. remove(element, 'class');
  2759. }
  2760. };
  2761. var remove$5 = function (element, clazz) {
  2762. if (supports(element)) {
  2763. var classList = element.dom().classList;
  2764. classList.remove(clazz);
  2765. } else {
  2766. remove$4(element, clazz);
  2767. }
  2768. cleanClass(element);
  2769. };
  2770. var has$2 = function (element, clazz) {
  2771. return supports(element) && element.dom().classList.contains(clazz);
  2772. };
  2773. var repeat = function (repititions, f) {
  2774. var r = [];
  2775. for (var i = 0; i < repititions; i++) {
  2776. r.push(f(i));
  2777. }
  2778. return r;
  2779. };
  2780. var range = function (start, end) {
  2781. var r = [];
  2782. for (var i = start; i < end; i++) {
  2783. r.push(i);
  2784. }
  2785. return r;
  2786. };
  2787. var deduce = function (xs, index) {
  2788. if (index < 0 || index >= xs.length - 1) {
  2789. return Option.none();
  2790. }
  2791. var current = xs[index].fold(function () {
  2792. var rest = reverse(xs.slice(0, index));
  2793. return findMap(rest, function (a, i) {
  2794. return a.map(function (aa) {
  2795. return {
  2796. value: aa,
  2797. delta: i + 1
  2798. };
  2799. });
  2800. });
  2801. }, function (c) {
  2802. return Option.some({
  2803. value: c,
  2804. delta: 0
  2805. });
  2806. });
  2807. var next = xs[index + 1].fold(function () {
  2808. var rest = xs.slice(index + 1);
  2809. return findMap(rest, function (a, i) {
  2810. return a.map(function (aa) {
  2811. return {
  2812. value: aa,
  2813. delta: i + 1
  2814. };
  2815. });
  2816. });
  2817. }, function (n) {
  2818. return Option.some({
  2819. value: n,
  2820. delta: 1
  2821. });
  2822. });
  2823. return current.bind(function (c) {
  2824. return next.map(function (n) {
  2825. var extras = n.delta + c.delta;
  2826. return Math.abs(n.value - c.value) / extras;
  2827. });
  2828. });
  2829. };
  2830. var columns = function (warehouse) {
  2831. var grid = warehouse.grid;
  2832. var cols = range(0, grid.columns());
  2833. var rowsArr = range(0, grid.rows());
  2834. return map(cols, function (col) {
  2835. var getBlock = function () {
  2836. return bind(rowsArr, function (r) {
  2837. return Warehouse.getAt(warehouse, r, col).filter(function (detail) {
  2838. return detail.column() === col;
  2839. }).fold(constant([]), function (detail) {
  2840. return [detail];
  2841. });
  2842. });
  2843. };
  2844. var isSingle = function (detail) {
  2845. return detail.colspan() === 1;
  2846. };
  2847. var getFallback = function () {
  2848. return Warehouse.getAt(warehouse, 0, col);
  2849. };
  2850. return decide(getBlock, isSingle, getFallback);
  2851. });
  2852. };
  2853. var decide = function (getBlock, isSingle, getFallback) {
  2854. var inBlock = getBlock();
  2855. var singleInBlock = find(inBlock, isSingle);
  2856. var detailOption = singleInBlock.orThunk(function () {
  2857. return Option.from(inBlock[0]).orThunk(getFallback);
  2858. });
  2859. return detailOption.map(function (detail) {
  2860. return detail.element();
  2861. });
  2862. };
  2863. var rows$1 = function (warehouse) {
  2864. var grid = warehouse.grid;
  2865. var rowsArr = range(0, grid.rows());
  2866. var cols = range(0, grid.columns());
  2867. return map(rowsArr, function (row) {
  2868. var getBlock = function () {
  2869. return bind(cols, function (c) {
  2870. return Warehouse.getAt(warehouse, row, c).filter(function (detail) {
  2871. return detail.row() === row;
  2872. }).fold(constant([]), function (detail) {
  2873. return [detail];
  2874. });
  2875. });
  2876. };
  2877. var isSingle = function (detail) {
  2878. return detail.rowspan() === 1;
  2879. };
  2880. var getFallback = function () {
  2881. return Warehouse.getAt(warehouse, row, 0);
  2882. };
  2883. return decide(getBlock, isSingle, getFallback);
  2884. });
  2885. };
  2886. var css = function (namespace) {
  2887. var dashNamespace = namespace.replace(/\./g, '-');
  2888. var resolve = function (str) {
  2889. return dashNamespace + '-' + str;
  2890. };
  2891. return { resolve: resolve };
  2892. };
  2893. var styles = css('ephox-snooker');
  2894. var resolve = styles.resolve;
  2895. var col = function (column, x, y, w, h) {
  2896. var bar = Element.fromTag('div');
  2897. setAll$1(bar, {
  2898. position: 'absolute',
  2899. left: x - w / 2 + 'px',
  2900. top: y + 'px',
  2901. height: h + 'px',
  2902. width: w + 'px'
  2903. });
  2904. setAll(bar, {
  2905. 'data-column': column,
  2906. 'role': 'presentation'
  2907. });
  2908. return bar;
  2909. };
  2910. var row = function (r, x, y, w, h) {
  2911. var bar = Element.fromTag('div');
  2912. setAll$1(bar, {
  2913. position: 'absolute',
  2914. left: x + 'px',
  2915. top: y - h / 2 + 'px',
  2916. height: h + 'px',
  2917. width: w + 'px'
  2918. });
  2919. setAll(bar, {
  2920. 'data-row': r,
  2921. 'role': 'presentation'
  2922. });
  2923. return bar;
  2924. };
  2925. var resizeBar = resolve('resizer-bar');
  2926. var resizeRowBar = resolve('resizer-rows');
  2927. var resizeColBar = resolve('resizer-cols');
  2928. var BAR_THICKNESS = 7;
  2929. var destroy = function (wire) {
  2930. var previous = descendants$1(wire.parent(), '.' + resizeBar);
  2931. each(previous, remove$2);
  2932. };
  2933. var drawBar = function (wire, positions, create) {
  2934. var origin = wire.origin();
  2935. each(positions, function (cpOption) {
  2936. cpOption.each(function (cp) {
  2937. var bar = create(origin, cp);
  2938. add$2(bar, resizeBar);
  2939. append(wire.parent(), bar);
  2940. });
  2941. });
  2942. };
  2943. var refreshCol = function (wire, colPositions, position, tableHeight) {
  2944. drawBar(wire, colPositions, function (origin, cp) {
  2945. var colBar = col(cp.col, cp.x - origin.left(), position.top() - origin.top(), BAR_THICKNESS, tableHeight);
  2946. add$2(colBar, resizeColBar);
  2947. return colBar;
  2948. });
  2949. };
  2950. var refreshRow = function (wire, rowPositions, position, tableWidth) {
  2951. drawBar(wire, rowPositions, function (origin, cp) {
  2952. var rowBar = row(cp.row, position.left() - origin.left(), cp.y - origin.top(), tableWidth, BAR_THICKNESS);
  2953. add$2(rowBar, resizeRowBar);
  2954. return rowBar;
  2955. });
  2956. };
  2957. var refreshGrid = function (wire, table, rows, cols, hdirection, vdirection) {
  2958. var position = absolute(table);
  2959. var rowPositions = rows.length > 0 ? hdirection.positions(rows, table) : [];
  2960. refreshRow(wire, rowPositions, position, getOuter$1(table));
  2961. var colPositions = cols.length > 0 ? vdirection.positions(cols, table) : [];
  2962. refreshCol(wire, colPositions, position, getOuter(table));
  2963. };
  2964. var refresh = function (wire, table, hdirection, vdirection) {
  2965. destroy(wire);
  2966. var list = fromTable(table);
  2967. var warehouse = Warehouse.generate(list);
  2968. var rows = rows$1(warehouse);
  2969. var cols = columns(warehouse);
  2970. refreshGrid(wire, table, rows, cols, hdirection, vdirection);
  2971. };
  2972. var each$2 = function (wire, f) {
  2973. var bars = descendants$1(wire.parent(), '.' + resizeBar);
  2974. each(bars, f);
  2975. };
  2976. var hide = function (wire) {
  2977. each$2(wire, function (bar) {
  2978. set$1(bar, 'display', 'none');
  2979. });
  2980. };
  2981. var show = function (wire) {
  2982. each$2(wire, function (bar) {
  2983. set$1(bar, 'display', 'block');
  2984. });
  2985. };
  2986. var isRowBar = function (element) {
  2987. return has$2(element, resizeRowBar);
  2988. };
  2989. var isColBar = function (element) {
  2990. return has$2(element, resizeColBar);
  2991. };
  2992. var addCell = function (gridRow, index, cell) {
  2993. var cells = gridRow.cells();
  2994. var before = cells.slice(0, index);
  2995. var after = cells.slice(index);
  2996. var newCells = before.concat([cell]).concat(after);
  2997. return setCells(gridRow, newCells);
  2998. };
  2999. var mutateCell = function (gridRow, index, cell) {
  3000. var cells = gridRow.cells();
  3001. cells[index] = cell;
  3002. };
  3003. var setCells = function (gridRow, cells) {
  3004. return rowcells(cells, gridRow.section());
  3005. };
  3006. var mapCells = function (gridRow, f) {
  3007. var cells = gridRow.cells();
  3008. var r = map(cells, f);
  3009. return rowcells(r, gridRow.section());
  3010. };
  3011. var getCell = function (gridRow, index) {
  3012. return gridRow.cells()[index];
  3013. };
  3014. var getCellElement = function (gridRow, index) {
  3015. return getCell(gridRow, index).element();
  3016. };
  3017. var cellLength = function (gridRow) {
  3018. return gridRow.cells().length;
  3019. };
  3020. var getColumn = function (grid, index) {
  3021. return map(grid, function (row) {
  3022. return getCell(row, index);
  3023. });
  3024. };
  3025. var getRow = function (grid, index) {
  3026. return grid[index];
  3027. };
  3028. var findDiff = function (xs, comp) {
  3029. if (xs.length === 0) {
  3030. return 0;
  3031. }
  3032. var first = xs[0];
  3033. var index = findIndex(xs, function (x) {
  3034. return !comp(first.element(), x.element());
  3035. });
  3036. return index.fold(function () {
  3037. return xs.length;
  3038. }, function (ind) {
  3039. return ind;
  3040. });
  3041. };
  3042. var subgrid = function (grid, row, column, comparator) {
  3043. var restOfRow = getRow(grid, row).cells().slice(column);
  3044. var endColIndex = findDiff(restOfRow, comparator);
  3045. var restOfColumn = getColumn(grid, column).slice(row);
  3046. var endRowIndex = findDiff(restOfColumn, comparator);
  3047. return {
  3048. colspan: endColIndex,
  3049. rowspan: endRowIndex
  3050. };
  3051. };
  3052. var toDetails = function (grid, comparator) {
  3053. var seen = map(grid, function (row) {
  3054. return map(row.cells(), function () {
  3055. return false;
  3056. });
  3057. });
  3058. var updateSeen = function (ri, ci, rowspan, colspan) {
  3059. for (var r = ri; r < ri + rowspan; r++) {
  3060. for (var c = ci; c < ci + colspan; c++) {
  3061. seen[r][c] = true;
  3062. }
  3063. }
  3064. };
  3065. return map(grid, function (row, ri) {
  3066. var details = bind(row.cells(), function (cell, ci) {
  3067. if (seen[ri][ci] === false) {
  3068. var result = subgrid(grid, ri, ci, comparator);
  3069. updateSeen(ri, ci, result.rowspan, result.colspan);
  3070. return [detailnew(cell.element(), result.rowspan, result.colspan, cell.isNew())];
  3071. } else {
  3072. return [];
  3073. }
  3074. });
  3075. return rowdetails(details, row.section());
  3076. });
  3077. };
  3078. var toGrid = function (warehouse, generators, isNew) {
  3079. var grid = [];
  3080. for (var i = 0; i < warehouse.grid.rows(); i++) {
  3081. var rowCells = [];
  3082. for (var j = 0; j < warehouse.grid.columns(); j++) {
  3083. var element = Warehouse.getAt(warehouse, i, j).map(function (item) {
  3084. return elementnew(item.element(), isNew);
  3085. }).getOrThunk(function () {
  3086. return elementnew(generators.gap(), true);
  3087. });
  3088. rowCells.push(element);
  3089. }
  3090. var row = rowcells(rowCells, warehouse.all[i].section());
  3091. grid.push(row);
  3092. }
  3093. return grid;
  3094. };
  3095. var fromWarehouse = function (warehouse, generators) {
  3096. return toGrid(warehouse, generators, false);
  3097. };
  3098. var deriveRows = function (rendered, generators) {
  3099. var findRow = function (details) {
  3100. var rowOfCells = findMap(details, function (detail) {
  3101. return parent(detail.element()).map(function (row) {
  3102. var isNew = parent(row).isNone();
  3103. return elementnew(row, isNew);
  3104. });
  3105. });
  3106. return rowOfCells.getOrThunk(function () {
  3107. return elementnew(generators.row(), true);
  3108. });
  3109. };
  3110. return map(rendered, function (details) {
  3111. var row = findRow(details.details());
  3112. return rowdatanew(row.element(), details.details(), details.section(), row.isNew());
  3113. });
  3114. };
  3115. var toDetailList = function (grid, generators) {
  3116. var rendered = toDetails(grid, eq);
  3117. return deriveRows(rendered, generators);
  3118. };
  3119. var findInWarehouse = function (warehouse, element) {
  3120. return findMap(warehouse.all, function (r) {
  3121. return find(r.cells(), function (e) {
  3122. return eq(element, e.element());
  3123. });
  3124. });
  3125. };
  3126. var run = function (operation, extract, adjustment, postAction, genWrappers) {
  3127. return function (wire, table, target, generators, direction) {
  3128. var input = fromTable(table);
  3129. var warehouse = Warehouse.generate(input);
  3130. var output = extract(warehouse, target).map(function (info) {
  3131. var model = fromWarehouse(warehouse, generators);
  3132. var result = operation(model, info, eq, genWrappers(generators));
  3133. var grid = toDetailList(result.grid(), generators);
  3134. return {
  3135. grid: constant(grid),
  3136. cursor: result.cursor
  3137. };
  3138. });
  3139. return output.fold(function () {
  3140. return Option.none();
  3141. }, function (out) {
  3142. var newElements = render(table, out.grid());
  3143. adjustment(table, out.grid(), direction);
  3144. postAction(table);
  3145. refresh(wire, table, height, direction);
  3146. return Option.some({
  3147. cursor: out.cursor,
  3148. newRows: constant(newElements.newRows),
  3149. newCells: constant(newElements.newCells)
  3150. });
  3151. });
  3152. };
  3153. };
  3154. var onCell = function (warehouse, target) {
  3155. return cell(target.element()).bind(function (cell) {
  3156. return findInWarehouse(warehouse, cell);
  3157. });
  3158. };
  3159. var onPaste = function (warehouse, target) {
  3160. return cell(target.element()).bind(function (cell) {
  3161. return findInWarehouse(warehouse, cell).map(function (details) {
  3162. var value = __assign(__assign({}, details), {
  3163. generators: target.generators,
  3164. clipboard: target.clipboard
  3165. });
  3166. return value;
  3167. });
  3168. });
  3169. };
  3170. var onPasteRows = function (warehouse, target) {
  3171. var details = map(target.selection(), function (cell$1) {
  3172. return cell(cell$1).bind(function (lc) {
  3173. return findInWarehouse(warehouse, lc);
  3174. });
  3175. });
  3176. var cells = cat(details);
  3177. return cells.length > 0 ? Option.some({
  3178. cells: cells,
  3179. generators: target.generators,
  3180. clipboard: target.clipboard
  3181. }) : Option.none();
  3182. };
  3183. var onMergable = function (_warehouse, target) {
  3184. return target.mergable();
  3185. };
  3186. var onUnmergable = function (_warehouse, target) {
  3187. return target.unmergable();
  3188. };
  3189. var onCells = function (warehouse, target) {
  3190. var details = map(target.selection(), function (cell$1) {
  3191. return cell(cell$1).bind(function (lc) {
  3192. return findInWarehouse(warehouse, lc);
  3193. });
  3194. });
  3195. var cells = cat(details);
  3196. return cells.length > 0 ? Option.some(cells) : Option.none();
  3197. };
  3198. var value = function (o) {
  3199. var is = function (v) {
  3200. return o === v;
  3201. };
  3202. var or = function (_opt) {
  3203. return value(o);
  3204. };
  3205. var orThunk = function (_f) {
  3206. return value(o);
  3207. };
  3208. var map = function (f) {
  3209. return value(f(o));
  3210. };
  3211. var mapError = function (_f) {
  3212. return value(o);
  3213. };
  3214. var each = function (f) {
  3215. f(o);
  3216. };
  3217. var bind = function (f) {
  3218. return f(o);
  3219. };
  3220. var fold = function (_, onValue) {
  3221. return onValue(o);
  3222. };
  3223. var exists = function (f) {
  3224. return f(o);
  3225. };
  3226. var forall = function (f) {
  3227. return f(o);
  3228. };
  3229. var toOption = function () {
  3230. return Option.some(o);
  3231. };
  3232. return {
  3233. is: is,
  3234. isValue: always,
  3235. isError: never,
  3236. getOr: constant(o),
  3237. getOrThunk: constant(o),
  3238. getOrDie: constant(o),
  3239. or: or,
  3240. orThunk: orThunk,
  3241. fold: fold,
  3242. map: map,
  3243. mapError: mapError,
  3244. each: each,
  3245. bind: bind,
  3246. exists: exists,
  3247. forall: forall,
  3248. toOption: toOption
  3249. };
  3250. };
  3251. var error = function (message) {
  3252. var getOrThunk = function (f) {
  3253. return f();
  3254. };
  3255. var getOrDie = function () {
  3256. return die(String(message))();
  3257. };
  3258. var or = function (opt) {
  3259. return opt;
  3260. };
  3261. var orThunk = function (f) {
  3262. return f();
  3263. };
  3264. var map = function (_f) {
  3265. return error(message);
  3266. };
  3267. var mapError = function (f) {
  3268. return error(f(message));
  3269. };
  3270. var bind = function (_f) {
  3271. return error(message);
  3272. };
  3273. var fold = function (onError, _) {
  3274. return onError(message);
  3275. };
  3276. return {
  3277. is: never,
  3278. isValue: never,
  3279. isError: always,
  3280. getOr: identity,
  3281. getOrThunk: getOrThunk,
  3282. getOrDie: getOrDie,
  3283. or: or,
  3284. orThunk: orThunk,
  3285. fold: fold,
  3286. map: map,
  3287. mapError: mapError,
  3288. each: noop,
  3289. bind: bind,
  3290. exists: never,
  3291. forall: always,
  3292. toOption: Option.none
  3293. };
  3294. };
  3295. var fromOption = function (opt, err) {
  3296. return opt.fold(function () {
  3297. return error(err);
  3298. }, value);
  3299. };
  3300. var Result = {
  3301. value: value,
  3302. error: error,
  3303. fromOption: fromOption
  3304. };
  3305. var measure = function (startAddress, gridA, gridB) {
  3306. if (startAddress.row() >= gridA.length || startAddress.column() > cellLength(gridA[0])) {
  3307. return Result.error('invalid start address out of table bounds, row: ' + startAddress.row() + ', column: ' + startAddress.column());
  3308. }
  3309. var rowRemainder = gridA.slice(startAddress.row());
  3310. var colRemainder = rowRemainder[0].cells().slice(startAddress.column());
  3311. var colRequired = cellLength(gridB[0]);
  3312. var rowRequired = gridB.length;
  3313. return Result.value({
  3314. rowDelta: rowRemainder.length - rowRequired,
  3315. colDelta: colRemainder.length - colRequired
  3316. });
  3317. };
  3318. var measureWidth = function (gridA, gridB) {
  3319. var colLengthA = cellLength(gridA[0]);
  3320. var colLengthB = cellLength(gridB[0]);
  3321. return {
  3322. rowDelta: 0,
  3323. colDelta: colLengthA - colLengthB
  3324. };
  3325. };
  3326. var fill = function (cells, generator) {
  3327. return map(cells, function () {
  3328. return elementnew(generator.cell(), true);
  3329. });
  3330. };
  3331. var rowFill = function (grid, amount, generator) {
  3332. return grid.concat(repeat(amount, function (_row) {
  3333. return setCells(grid[grid.length - 1], fill(grid[grid.length - 1].cells(), generator));
  3334. }));
  3335. };
  3336. var colFill = function (grid, amount, generator) {
  3337. return map(grid, function (row) {
  3338. return setCells(row, row.cells().concat(fill(range(0, amount), generator)));
  3339. });
  3340. };
  3341. var tailor = function (gridA, delta, generator) {
  3342. var fillCols = delta.colDelta < 0 ? colFill : identity;
  3343. var fillRows = delta.rowDelta < 0 ? rowFill : identity;
  3344. var modifiedCols = fillCols(gridA, Math.abs(delta.colDelta), generator);
  3345. return fillRows(modifiedCols, Math.abs(delta.rowDelta), generator);
  3346. };
  3347. var merge = function (grid, bounds, comparator, substitution) {
  3348. if (grid.length === 0) {
  3349. return grid;
  3350. }
  3351. for (var i = bounds.startRow(); i <= bounds.finishRow(); i++) {
  3352. for (var j = bounds.startCol(); j <= bounds.finishCol(); j++) {
  3353. mutateCell(grid[i], j, elementnew(substitution(), false));
  3354. }
  3355. }
  3356. return grid;
  3357. };
  3358. var unmerge = function (grid, target, comparator, substitution) {
  3359. var first = true;
  3360. for (var i = 0; i < grid.length; i++) {
  3361. for (var j = 0; j < cellLength(grid[0]); j++) {
  3362. var current = getCellElement(grid[i], j);
  3363. var isToReplace = comparator(current, target);
  3364. if (isToReplace === true && first === false) {
  3365. mutateCell(grid[i], j, elementnew(substitution(), true));
  3366. } else if (isToReplace === true) {
  3367. first = false;
  3368. }
  3369. }
  3370. }
  3371. return grid;
  3372. };
  3373. var uniqueCells = function (row, comparator) {
  3374. return foldl(row, function (rest, cell) {
  3375. return exists(rest, function (currentCell) {
  3376. return comparator(currentCell.element(), cell.element());
  3377. }) ? rest : rest.concat([cell]);
  3378. }, []);
  3379. };
  3380. var splitRows = function (grid, index, comparator, substitution) {
  3381. if (index > 0 && index < grid.length) {
  3382. var rowPrevCells = grid[index - 1].cells();
  3383. var cells = uniqueCells(rowPrevCells, comparator);
  3384. each(cells, function (cell) {
  3385. var replacement = Option.none();
  3386. var _loop_1 = function (i) {
  3387. var _loop_2 = function (j) {
  3388. var current = grid[i].cells()[j];
  3389. var isToReplace = comparator(current.element(), cell.element());
  3390. if (isToReplace) {
  3391. if (replacement.isNone()) {
  3392. replacement = Option.some(substitution());
  3393. }
  3394. replacement.each(function (sub) {
  3395. mutateCell(grid[i], j, elementnew(sub, true));
  3396. });
  3397. }
  3398. };
  3399. for (var j = 0; j < cellLength(grid[0]); j++) {
  3400. _loop_2(j);
  3401. }
  3402. };
  3403. for (var i = index; i < grid.length; i++) {
  3404. _loop_1(i);
  3405. }
  3406. });
  3407. }
  3408. return grid;
  3409. };
  3410. var isSpanning = function (grid, row, col, comparator) {
  3411. var candidate = getCell(grid[row], col);
  3412. var matching = curry(comparator, candidate.element());
  3413. var currentRow = grid[row];
  3414. return grid.length > 1 && cellLength(currentRow) > 1 && (col > 0 && matching(getCellElement(currentRow, col - 1)) || col < currentRow.cells().length - 1 && matching(getCellElement(currentRow, col + 1)) || row > 0 && matching(getCellElement(grid[row - 1], col)) || row < grid.length - 1 && matching(getCellElement(grid[row + 1], col)));
  3415. };
  3416. var mergeTables = function (startAddress, gridA, gridB, generator, comparator) {
  3417. var startRow = startAddress.row();
  3418. var startCol = startAddress.column();
  3419. var mergeHeight = gridB.length;
  3420. var mergeWidth = cellLength(gridB[0]);
  3421. var endRow = startRow + mergeHeight;
  3422. var endCol = startCol + mergeWidth;
  3423. for (var r = startRow; r < endRow; r++) {
  3424. for (var c = startCol; c < endCol; c++) {
  3425. if (isSpanning(gridA, r, c, comparator)) {
  3426. unmerge(gridA, getCellElement(gridA[r], c), comparator, generator.cell);
  3427. }
  3428. var newCell = getCellElement(gridB[r - startRow], c - startCol);
  3429. var replacement = generator.replace(newCell);
  3430. mutateCell(gridA[r], c, elementnew(replacement, true));
  3431. }
  3432. }
  3433. return gridA;
  3434. };
  3435. var merge$1 = function (startAddress, gridA, gridB, generator, comparator) {
  3436. var result = measure(startAddress, gridA, gridB);
  3437. return result.map(function (delta) {
  3438. var fittedGrid = tailor(gridA, delta, generator);
  3439. return mergeTables(startAddress, fittedGrid, gridB, generator, comparator);
  3440. });
  3441. };
  3442. var insert = function (index, gridA, gridB, generator, comparator) {
  3443. splitRows(gridA, index, comparator, generator.cell);
  3444. var delta = measureWidth(gridB, gridA);
  3445. var fittedNewGrid = tailor(gridB, delta, generator);
  3446. var secondDelta = measureWidth(gridA, fittedNewGrid);
  3447. var fittedOldGrid = tailor(gridA, secondDelta, generator);
  3448. return fittedOldGrid.slice(0, index).concat(fittedNewGrid).concat(fittedOldGrid.slice(index, fittedOldGrid.length));
  3449. };
  3450. var insertRowAt = function (grid, index, example, comparator, substitution) {
  3451. var before = grid.slice(0, index);
  3452. var after = grid.slice(index);
  3453. var between = mapCells(grid[example], function (ex, c) {
  3454. var withinSpan = index > 0 && index < grid.length && comparator(getCellElement(grid[index - 1], c), getCellElement(grid[index], c));
  3455. var ret = withinSpan ? getCell(grid[index], c) : elementnew(substitution(ex.element(), comparator), true);
  3456. return ret;
  3457. });
  3458. return before.concat([between]).concat(after);
  3459. };
  3460. var insertColumnAt = function (grid, index, example, comparator, substitution) {
  3461. return map(grid, function (row) {
  3462. var withinSpan = index > 0 && index < cellLength(row) && comparator(getCellElement(row, index - 1), getCellElement(row, index));
  3463. var sub = withinSpan ? getCell(row, index) : elementnew(substitution(getCellElement(row, example), comparator), true);
  3464. return addCell(row, index, sub);
  3465. });
  3466. };
  3467. var deleteColumnsAt = function (grid, start, finish) {
  3468. var rows = map(grid, function (row) {
  3469. var cells = row.cells().slice(0, start).concat(row.cells().slice(finish + 1));
  3470. return rowcells(cells, row.section());
  3471. });
  3472. return filter(rows, function (row) {
  3473. return row.cells().length > 0;
  3474. });
  3475. };
  3476. var deleteRowsAt = function (grid, start, finish) {
  3477. return grid.slice(0, start).concat(grid.slice(finish + 1));
  3478. };
  3479. var replaceIn = function (grid, targets, comparator, substitution) {
  3480. var isTarget = function (cell) {
  3481. return exists(targets, function (target) {
  3482. return comparator(cell.element(), target.element());
  3483. });
  3484. };
  3485. return map(grid, function (row) {
  3486. return mapCells(row, function (cell) {
  3487. return isTarget(cell) ? elementnew(substitution(cell.element(), comparator), true) : cell;
  3488. });
  3489. });
  3490. };
  3491. var notStartRow = function (grid, rowIndex, colIndex, comparator) {
  3492. return getCellElement(grid[rowIndex], colIndex) !== undefined && (rowIndex > 0 && comparator(getCellElement(grid[rowIndex - 1], colIndex), getCellElement(grid[rowIndex], colIndex)));
  3493. };
  3494. var notStartColumn = function (row, index, comparator) {
  3495. return index > 0 && comparator(getCellElement(row, index - 1), getCellElement(row, index));
  3496. };
  3497. var replaceColumn = function (grid, index, comparator, substitution) {
  3498. var targets = bind(grid, function (row, i) {
  3499. var alreadyAdded = notStartRow(grid, i, index, comparator) || notStartColumn(row, index, comparator);
  3500. return alreadyAdded ? [] : [getCell(row, index)];
  3501. });
  3502. return replaceIn(grid, targets, comparator, substitution);
  3503. };
  3504. var replaceRow = function (grid, index, comparator, substitution) {
  3505. var targetRow = grid[index];
  3506. var targets = bind(targetRow.cells(), function (item, i) {
  3507. var alreadyAdded = notStartRow(grid, index, i, comparator) || notStartColumn(targetRow, i, comparator);
  3508. return alreadyAdded ? [] : [item];
  3509. });
  3510. return replaceIn(grid, targets, comparator, substitution);
  3511. };
  3512. var adt = Adt.generate([
  3513. { none: [] },
  3514. { only: ['index'] },
  3515. {
  3516. left: [
  3517. 'index',
  3518. 'next'
  3519. ]
  3520. },
  3521. {
  3522. middle: [
  3523. 'prev',
  3524. 'index',
  3525. 'next'
  3526. ]
  3527. },
  3528. {
  3529. right: [
  3530. 'prev',
  3531. 'index'
  3532. ]
  3533. }
  3534. ]);
  3535. var ColumnContext = __assign({}, adt);
  3536. var neighbours$1 = function (input, index) {
  3537. if (input.length === 0) {
  3538. return ColumnContext.none();
  3539. }
  3540. if (input.length === 1) {
  3541. return ColumnContext.only(0);
  3542. }
  3543. if (index === 0) {
  3544. return ColumnContext.left(0, 1);
  3545. }
  3546. if (index === input.length - 1) {
  3547. return ColumnContext.right(index - 1, index);
  3548. }
  3549. if (index > 0 && index < input.length - 1) {
  3550. return ColumnContext.middle(index - 1, index, index + 1);
  3551. }
  3552. return ColumnContext.none();
  3553. };
  3554. var determine = function (input, column, step, tableSize) {
  3555. var result = input.slice(0);
  3556. var context = neighbours$1(input, column);
  3557. var zero = function (array) {
  3558. return map(array, constant(0));
  3559. };
  3560. var onNone = constant(zero(result));
  3561. var onOnly = function (index) {
  3562. return tableSize.singleColumnWidth(result[index], step);
  3563. };
  3564. var onChange = function (index, next) {
  3565. if (step >= 0) {
  3566. var newNext = Math.max(tableSize.minCellWidth(), result[next] - step);
  3567. return zero(result.slice(0, index)).concat([
  3568. step,
  3569. newNext - result[next]
  3570. ]).concat(zero(result.slice(next + 1)));
  3571. } else {
  3572. var newThis = Math.max(tableSize.minCellWidth(), result[index] + step);
  3573. var diffx = result[index] - newThis;
  3574. return zero(result.slice(0, index)).concat([
  3575. newThis - result[index],
  3576. diffx
  3577. ]).concat(zero(result.slice(next + 1)));
  3578. }
  3579. };
  3580. var onLeft = onChange;
  3581. var onMiddle = function (_prev, index, next) {
  3582. return onChange(index, next);
  3583. };
  3584. var onRight = function (_prev, index) {
  3585. if (step >= 0) {
  3586. return zero(result.slice(0, index)).concat([step]);
  3587. } else {
  3588. var size = Math.max(tableSize.minCellWidth(), result[index] + step);
  3589. return zero(result.slice(0, index)).concat([size - result[index]]);
  3590. }
  3591. };
  3592. return context.fold(onNone, onOnly, onLeft, onMiddle, onRight);
  3593. };
  3594. var getWidthFrom = function (warehouse, direction, getWidth, fallback, tableSize) {
  3595. var columns$1 = columns(warehouse);
  3596. var backups = map(columns$1, function (cellOption) {
  3597. return cellOption.map(direction.edge);
  3598. });
  3599. return map(columns$1, function (cellOption, c) {
  3600. var columnCell = cellOption.filter(not(hasColspan));
  3601. return columnCell.fold(function () {
  3602. var deduced = deduce(backups, c);
  3603. return fallback(deduced);
  3604. }, function (cell) {
  3605. return getWidth(cell, tableSize);
  3606. });
  3607. });
  3608. };
  3609. var getPercentageWidths = function (warehouse, direction, tableSize) {
  3610. return getWidthFrom(warehouse, direction, getPercentageWidth, function (deduced) {
  3611. return deduced.fold(function () {
  3612. return tableSize.minCellWidth();
  3613. }, function (cellWidth) {
  3614. return cellWidth / tableSize.pixelWidth() * 100;
  3615. });
  3616. }, tableSize);
  3617. };
  3618. var getPixelWidths = function (warehouse, direction, tableSize) {
  3619. return getWidthFrom(warehouse, direction, getPixelWidth, function (deduced) {
  3620. return deduced.getOrThunk(tableSize.minCellWidth);
  3621. }, tableSize);
  3622. };
  3623. var getHeightFrom = function (warehouse, direction, getHeight, fallback) {
  3624. var rows = rows$1(warehouse);
  3625. var backups = map(rows, function (cellOption) {
  3626. return cellOption.map(direction.edge);
  3627. });
  3628. return map(rows, function (cellOption, c) {
  3629. var rowCell = cellOption.filter(not(hasRowspan));
  3630. return rowCell.fold(function () {
  3631. var deduced = deduce(backups, c);
  3632. return fallback(deduced);
  3633. }, function (cell) {
  3634. return getHeight(cell);
  3635. });
  3636. });
  3637. };
  3638. var getPixelHeights = function (warehouse, direction) {
  3639. return getHeightFrom(warehouse, direction, getHeight$1, function (deduced) {
  3640. return deduced.getOrThunk(minHeight);
  3641. });
  3642. };
  3643. var total = function (start, end, measures) {
  3644. var r = 0;
  3645. for (var i = start; i < end; i++) {
  3646. r += measures[i] !== undefined ? measures[i] : 0;
  3647. }
  3648. return r;
  3649. };
  3650. var recalculateWidth = function (warehouse, widths) {
  3651. var all = Warehouse.justCells(warehouse);
  3652. return map(all, function (cell) {
  3653. var width = total(cell.column(), cell.column() + cell.colspan(), widths);
  3654. return {
  3655. element: cell.element(),
  3656. width: width,
  3657. colspan: cell.colspan()
  3658. };
  3659. });
  3660. };
  3661. var recalculateHeight = function (warehouse, heights) {
  3662. var all = Warehouse.justCells(warehouse);
  3663. return map(all, function (cell) {
  3664. var height = total(cell.row(), cell.row() + cell.rowspan(), heights);
  3665. return {
  3666. element: cell.element,
  3667. height: constant(height),
  3668. rowspan: cell.rowspan
  3669. };
  3670. });
  3671. };
  3672. var matchRowHeight = function (warehouse, heights) {
  3673. return map(warehouse.all, function (row, i) {
  3674. return {
  3675. element: row.element,
  3676. height: constant(heights[i])
  3677. };
  3678. });
  3679. };
  3680. var percentageSize = function (width, element) {
  3681. var floatWidth = parseFloat(width);
  3682. var pixelWidth = get$5(element);
  3683. var getCellDelta = function (delta) {
  3684. return delta / pixelWidth * 100;
  3685. };
  3686. var singleColumnWidth = function (w, _delta) {
  3687. return [100 - w];
  3688. };
  3689. var minCellWidth = function () {
  3690. return minWidth() / pixelWidth * 100;
  3691. };
  3692. var setTableWidth = function (table, _newWidths, delta) {
  3693. var ratio = delta / 100;
  3694. var change = ratio * floatWidth;
  3695. setPercentageWidth(table, floatWidth + change);
  3696. };
  3697. return {
  3698. width: constant(floatWidth),
  3699. pixelWidth: constant(pixelWidth),
  3700. getWidths: getPercentageWidths,
  3701. getCellDelta: getCellDelta,
  3702. singleColumnWidth: singleColumnWidth,
  3703. minCellWidth: minCellWidth,
  3704. setElementWidth: setPercentageWidth,
  3705. setTableWidth: setTableWidth
  3706. };
  3707. };
  3708. var pixelSize = function (width) {
  3709. var getCellDelta = identity;
  3710. var singleColumnWidth = function (w, delta) {
  3711. var newNext = Math.max(minWidth(), w + delta);
  3712. return [newNext - w];
  3713. };
  3714. var setTableWidth = function (table, newWidths, _delta) {
  3715. var total = foldr(newWidths, function (b, a) {
  3716. return b + a;
  3717. }, 0);
  3718. setPixelWidth(table, total);
  3719. };
  3720. return {
  3721. width: constant(width),
  3722. pixelWidth: constant(width),
  3723. getWidths: getPixelWidths,
  3724. getCellDelta: getCellDelta,
  3725. singleColumnWidth: singleColumnWidth,
  3726. minCellWidth: minWidth,
  3727. setElementWidth: setPixelWidth,
  3728. setTableWidth: setTableWidth
  3729. };
  3730. };
  3731. var chooseSize = function (element, width) {
  3732. var percentMatch = percentageBasedSizeRegex().exec(width);
  3733. if (percentMatch !== null) {
  3734. return percentageSize(percentMatch[1], element);
  3735. }
  3736. var pixelMatch = pixelBasedSizeRegex().exec(width);
  3737. if (pixelMatch !== null) {
  3738. var intWidth = parseInt(pixelMatch[1], 10);
  3739. return pixelSize(intWidth);
  3740. }
  3741. var fallbackWidth = get$5(element);
  3742. return pixelSize(fallbackWidth);
  3743. };
  3744. var getTableSize = function (element) {
  3745. var width = getRawWidth(element);
  3746. return width.fold(function () {
  3747. var fallbackWidth = get$5(element);
  3748. return pixelSize(fallbackWidth);
  3749. }, function (w) {
  3750. return chooseSize(element, w);
  3751. });
  3752. };
  3753. var getWarehouse$1 = function (list) {
  3754. return Warehouse.generate(list);
  3755. };
  3756. var sumUp = function (newSize) {
  3757. return foldr(newSize, function (b, a) {
  3758. return b + a;
  3759. }, 0);
  3760. };
  3761. var getTableWarehouse = function (table) {
  3762. var list = fromTable(table);
  3763. return getWarehouse$1(list);
  3764. };
  3765. var adjustWidth = function (table, delta, index, direction) {
  3766. var tableSize = getTableSize(table);
  3767. var step = tableSize.getCellDelta(delta);
  3768. var warehouse = getTableWarehouse(table);
  3769. var widths = tableSize.getWidths(warehouse, direction, tableSize);
  3770. var deltas = determine(widths, index, step, tableSize);
  3771. var newWidths = map(deltas, function (dx, i) {
  3772. return dx + widths[i];
  3773. });
  3774. var newSizes = recalculateWidth(warehouse, newWidths);
  3775. each(newSizes, function (cell) {
  3776. tableSize.setElementWidth(cell.element, cell.width);
  3777. });
  3778. if (index === warehouse.grid.columns() - 1) {
  3779. tableSize.setTableWidth(table, newWidths, step);
  3780. }
  3781. };
  3782. var adjustHeight = function (table, delta, index, direction) {
  3783. var warehouse = getTableWarehouse(table);
  3784. var heights = getPixelHeights(warehouse, direction);
  3785. var newHeights = map(heights, function (dy, i) {
  3786. return index === i ? Math.max(delta + dy, minHeight()) : dy;
  3787. });
  3788. var newCellSizes = recalculateHeight(warehouse, newHeights);
  3789. var newRowSizes = matchRowHeight(warehouse, newHeights);
  3790. each(newRowSizes, function (row) {
  3791. setHeight(row.element(), row.height());
  3792. });
  3793. each(newCellSizes, function (cell) {
  3794. setHeight(cell.element(), cell.height());
  3795. });
  3796. var total = sumUp(newHeights);
  3797. setHeight(table, total);
  3798. };
  3799. var adjustWidthTo = function (table, list, direction) {
  3800. var tableSize = getTableSize(table);
  3801. var warehouse = getWarehouse$1(list);
  3802. var widths = tableSize.getWidths(warehouse, direction, tableSize);
  3803. var newSizes = recalculateWidth(warehouse, widths);
  3804. each(newSizes, function (cell) {
  3805. tableSize.setElementWidth(cell.element, cell.width);
  3806. });
  3807. if (newSizes.length > 0) {
  3808. tableSize.setTableWidth(table, widths, tableSize.getCellDelta(0));
  3809. }
  3810. };
  3811. var sort = function (arr) {
  3812. return arr.slice(0).sort();
  3813. };
  3814. var reqMessage = function (required, keys) {
  3815. throw new Error('All required keys (' + sort(required).join(', ') + ') were not specified. Specified keys were: ' + sort(keys).join(', ') + '.');
  3816. };
  3817. var unsuppMessage = function (unsupported) {
  3818. throw new Error('Unsupported keys for object: ' + sort(unsupported).join(', '));
  3819. };
  3820. var validateStrArr = function (label, array) {
  3821. if (!isArray(array)) {
  3822. throw new Error('The ' + label + ' fields must be an array. Was: ' + array + '.');
  3823. }
  3824. each(array, function (a) {
  3825. if (!isString(a)) {
  3826. throw new Error('The value ' + a + ' in the ' + label + ' fields was not a string.');
  3827. }
  3828. });
  3829. };
  3830. var invalidTypeMessage = function (incorrect, type) {
  3831. throw new Error('All values need to be of type: ' + type + '. Keys (' + sort(incorrect).join(', ') + ') were not.');
  3832. };
  3833. var checkDupes = function (everything) {
  3834. var sorted = sort(everything);
  3835. var dupe = find(sorted, function (s, i) {
  3836. return i < sorted.length - 1 && s === sorted[i + 1];
  3837. });
  3838. dupe.each(function (d) {
  3839. throw new Error('The field: ' + d + ' occurs more than once in the combined fields: [' + sorted.join(', ') + '].');
  3840. });
  3841. };
  3842. var base = function (handleUnsupported, required) {
  3843. return baseWith(handleUnsupported, required, {
  3844. validate: isFunction,
  3845. label: 'function'
  3846. });
  3847. };
  3848. var baseWith = function (handleUnsupported, required, pred) {
  3849. if (required.length === 0) {
  3850. throw new Error('You must specify at least one required field.');
  3851. }
  3852. validateStrArr('required', required);
  3853. checkDupes(required);
  3854. return function (obj) {
  3855. var keys$1 = keys(obj);
  3856. var allReqd = forall(required, function (req) {
  3857. return contains(keys$1, req);
  3858. });
  3859. if (!allReqd) {
  3860. reqMessage(required, keys$1);
  3861. }
  3862. handleUnsupported(required, keys$1);
  3863. var invalidKeys = filter(required, function (key) {
  3864. return !pred.validate(obj[key], key);
  3865. });
  3866. if (invalidKeys.length > 0) {
  3867. invalidTypeMessage(invalidKeys, pred.label);
  3868. }
  3869. return obj;
  3870. };
  3871. };
  3872. var handleExact = function (required, keys) {
  3873. var unsupported = filter(keys, function (key) {
  3874. return !contains(required, key);
  3875. });
  3876. if (unsupported.length > 0) {
  3877. unsuppMessage(unsupported);
  3878. }
  3879. };
  3880. var exactly = function (required) {
  3881. return base(handleExact, required);
  3882. };
  3883. var verifyGenerators = exactly([
  3884. 'cell',
  3885. 'row',
  3886. 'replace',
  3887. 'gap'
  3888. ]);
  3889. var elementToData = function (element) {
  3890. var colspan = getAttrValue(element, 'colspan', 1);
  3891. var rowspan = getAttrValue(element, 'rowspan', 1);
  3892. return {
  3893. element: constant(element),
  3894. colspan: constant(colspan),
  3895. rowspan: constant(rowspan)
  3896. };
  3897. };
  3898. var modification = function (generators, toData) {
  3899. if (toData === void 0) {
  3900. toData = elementToData;
  3901. }
  3902. verifyGenerators(generators);
  3903. var position = Cell(Option.none());
  3904. var nu = function (data) {
  3905. return generators.cell(data);
  3906. };
  3907. var nuFrom = function (element) {
  3908. var data = toData(element);
  3909. return nu(data);
  3910. };
  3911. var add = function (element) {
  3912. var replacement = nuFrom(element);
  3913. if (position.get().isNone()) {
  3914. position.set(Option.some(replacement));
  3915. }
  3916. recent = Option.some({
  3917. item: element,
  3918. replacement: replacement
  3919. });
  3920. return replacement;
  3921. };
  3922. var recent = Option.none();
  3923. var getOrInit = function (element, comparator) {
  3924. return recent.fold(function () {
  3925. return add(element);
  3926. }, function (p) {
  3927. return comparator(element, p.item) ? p.replacement : add(element);
  3928. });
  3929. };
  3930. return {
  3931. getOrInit: getOrInit,
  3932. cursor: position.get
  3933. };
  3934. };
  3935. var transform = function (scope, tag) {
  3936. return function (generators) {
  3937. var position = Cell(Option.none());
  3938. verifyGenerators(generators);
  3939. var list = [];
  3940. var find$1 = function (element, comparator) {
  3941. return find(list, function (x) {
  3942. return comparator(x.item, element);
  3943. });
  3944. };
  3945. var makeNew = function (element) {
  3946. var attrs = { scope: scope };
  3947. var cell = generators.replace(element, tag, attrs);
  3948. list.push({
  3949. item: element,
  3950. sub: cell
  3951. });
  3952. if (position.get().isNone()) {
  3953. position.set(Option.some(cell));
  3954. }
  3955. return cell;
  3956. };
  3957. var replaceOrInit = function (element, comparator) {
  3958. return find$1(element, comparator).fold(function () {
  3959. return makeNew(element);
  3960. }, function (p) {
  3961. return comparator(element, p.item) ? p.sub : makeNew(element);
  3962. });
  3963. };
  3964. return {
  3965. replaceOrInit: replaceOrInit,
  3966. cursor: position.get
  3967. };
  3968. };
  3969. };
  3970. var merging = function (generators) {
  3971. verifyGenerators(generators);
  3972. var position = Cell(Option.none());
  3973. var combine = function (cell) {
  3974. if (position.get().isNone()) {
  3975. position.set(Option.some(cell));
  3976. }
  3977. return function () {
  3978. var raw = generators.cell({
  3979. element: constant(cell),
  3980. colspan: constant(1),
  3981. rowspan: constant(1)
  3982. });
  3983. remove$1(raw, 'width');
  3984. remove$1(cell, 'width');
  3985. return raw;
  3986. };
  3987. };
  3988. return {
  3989. combine: combine,
  3990. cursor: position.get
  3991. };
  3992. };
  3993. var Generators = {
  3994. modification: modification,
  3995. transform: transform,
  3996. merging: merging
  3997. };
  3998. var blockList = [
  3999. 'body',
  4000. 'p',
  4001. 'div',
  4002. 'article',
  4003. 'aside',
  4004. 'figcaption',
  4005. 'figure',
  4006. 'footer',
  4007. 'header',
  4008. 'nav',
  4009. 'section',
  4010. 'ol',
  4011. 'ul',
  4012. 'table',
  4013. 'thead',
  4014. 'tfoot',
  4015. 'tbody',
  4016. 'caption',
  4017. 'tr',
  4018. 'td',
  4019. 'th',
  4020. 'h1',
  4021. 'h2',
  4022. 'h3',
  4023. 'h4',
  4024. 'h5',
  4025. 'h6',
  4026. 'blockquote',
  4027. 'pre',
  4028. 'address'
  4029. ];
  4030. var isList = function (universe, item) {
  4031. var tagName = universe.property().name(item);
  4032. return contains([
  4033. 'ol',
  4034. 'ul'
  4035. ], tagName);
  4036. };
  4037. var isBlock = function (universe, item) {
  4038. var tagName = universe.property().name(item);
  4039. return contains(blockList, tagName);
  4040. };
  4041. var isEmptyTag = function (universe, item) {
  4042. return contains([
  4043. 'br',
  4044. 'img',
  4045. 'hr',
  4046. 'input'
  4047. ], universe.property().name(item));
  4048. };
  4049. var universe$1 = DomUniverse();
  4050. var isBlock$1 = function (element) {
  4051. return isBlock(universe$1, element);
  4052. };
  4053. var isList$1 = function (element) {
  4054. return isList(universe$1, element);
  4055. };
  4056. var isEmptyTag$1 = function (element) {
  4057. return isEmptyTag(universe$1, element);
  4058. };
  4059. var merge$2 = function (cells) {
  4060. var isBr = function (el) {
  4061. return name(el) === 'br';
  4062. };
  4063. var advancedBr = function (children) {
  4064. return forall(children, function (c) {
  4065. return isBr(c) || isText(c) && get$3(c).trim().length === 0;
  4066. });
  4067. };
  4068. var isListItem = function (el) {
  4069. return name(el) === 'li' || ancestor(el, isList$1).isSome();
  4070. };
  4071. var siblingIsBlock = function (el) {
  4072. return nextSibling(el).map(function (rightSibling) {
  4073. if (isBlock$1(rightSibling)) {
  4074. return true;
  4075. }
  4076. if (isEmptyTag$1(rightSibling)) {
  4077. return name(rightSibling) === 'img' ? false : true;
  4078. }
  4079. return false;
  4080. }).getOr(false);
  4081. };
  4082. var markCell = function (cell) {
  4083. return last$1(cell).bind(function (rightEdge) {
  4084. var rightSiblingIsBlock = siblingIsBlock(rightEdge);
  4085. return parent(rightEdge).map(function (parent) {
  4086. return rightSiblingIsBlock === true || isListItem(parent) || isBr(rightEdge) || isBlock$1(parent) && !eq(cell, parent) ? [] : [Element.fromTag('br')];
  4087. });
  4088. }).getOr([]);
  4089. };
  4090. var markContent = function () {
  4091. var content = bind(cells, function (cell) {
  4092. var children$1 = children(cell);
  4093. return advancedBr(children$1) ? [] : children$1.concat(markCell(cell));
  4094. });
  4095. return content.length === 0 ? [Element.fromTag('br')] : content;
  4096. };
  4097. var contents = markContent();
  4098. empty(cells[0]);
  4099. append$1(cells[0], contents);
  4100. };
  4101. var prune = function (table) {
  4102. var cells$1 = cells(table);
  4103. if (cells$1.length === 0) {
  4104. remove$2(table);
  4105. }
  4106. };
  4107. var outcome = function (grid, cursor) {
  4108. return {
  4109. grid: constant(grid),
  4110. cursor: constant(cursor)
  4111. };
  4112. };
  4113. var elementFromGrid = function (grid, row, column) {
  4114. return findIn(grid, row, column).orThunk(function () {
  4115. return findIn(grid, 0, 0);
  4116. });
  4117. };
  4118. var findIn = function (grid, row, column) {
  4119. return Option.from(grid[row]).bind(function (r) {
  4120. return Option.from(r.cells()[column]).bind(function (c) {
  4121. return Option.from(c.element());
  4122. });
  4123. });
  4124. };
  4125. var bundle = function (grid, row, column) {
  4126. return outcome(grid, findIn(grid, row, column));
  4127. };
  4128. var uniqueRows = function (details) {
  4129. return foldl(details, function (rest, detail) {
  4130. return exists(rest, function (currentDetail) {
  4131. return currentDetail.row() === detail.row();
  4132. }) ? rest : rest.concat([detail]);
  4133. }, []).sort(function (detailA, detailB) {
  4134. return detailA.row() - detailB.row();
  4135. });
  4136. };
  4137. var uniqueColumns = function (details) {
  4138. return foldl(details, function (rest, detail) {
  4139. return exists(rest, function (currentDetail) {
  4140. return currentDetail.column() === detail.column();
  4141. }) ? rest : rest.concat([detail]);
  4142. }, []).sort(function (detailA, detailB) {
  4143. return detailA.column() - detailB.column();
  4144. });
  4145. };
  4146. var opInsertRowsBefore = function (grid, details, comparator, genWrappers) {
  4147. var example = details[0].row();
  4148. var targetIndex = details[0].row();
  4149. var rows = uniqueRows(details);
  4150. var newGrid = foldl(rows, function (newG, _row) {
  4151. return insertRowAt(newG, targetIndex, example, comparator, genWrappers.getOrInit);
  4152. }, grid);
  4153. return bundle(newGrid, targetIndex, details[0].column());
  4154. };
  4155. var opInsertRowsAfter = function (grid, details, comparator, genWrappers) {
  4156. var rows = uniqueRows(details);
  4157. var example = rows[rows.length - 1].row();
  4158. var targetIndex = rows[rows.length - 1].row() + rows[rows.length - 1].rowspan();
  4159. var newGrid = foldl(rows, function (newG, _row) {
  4160. return insertRowAt(newG, targetIndex, example, comparator, genWrappers.getOrInit);
  4161. }, grid);
  4162. return bundle(newGrid, targetIndex, details[0].column());
  4163. };
  4164. var opInsertColumnsBefore = function (grid, details, comparator, genWrappers) {
  4165. var columns = uniqueColumns(details);
  4166. var example = columns[0].column();
  4167. var targetIndex = columns[0].column();
  4168. var newGrid = foldl(columns, function (newG, _row) {
  4169. return insertColumnAt(newG, targetIndex, example, comparator, genWrappers.getOrInit);
  4170. }, grid);
  4171. return bundle(newGrid, details[0].row(), targetIndex);
  4172. };
  4173. var opInsertColumnsAfter = function (grid, details, comparator, genWrappers) {
  4174. var example = details[details.length - 1].column();
  4175. var targetIndex = details[details.length - 1].column() + details[details.length - 1].colspan();
  4176. var columns = uniqueColumns(details);
  4177. var newGrid = foldl(columns, function (newG, _row) {
  4178. return insertColumnAt(newG, targetIndex, example, comparator, genWrappers.getOrInit);
  4179. }, grid);
  4180. return bundle(newGrid, details[0].row(), targetIndex);
  4181. };
  4182. var opMakeRowHeader = function (grid, detail, comparator, genWrappers) {
  4183. var newGrid = replaceRow(grid, detail.row(), comparator, genWrappers.replaceOrInit);
  4184. return bundle(newGrid, detail.row(), detail.column());
  4185. };
  4186. var opMakeColumnHeader = function (grid, detail, comparator, genWrappers) {
  4187. var newGrid = replaceColumn(grid, detail.column(), comparator, genWrappers.replaceOrInit);
  4188. return bundle(newGrid, detail.row(), detail.column());
  4189. };
  4190. var opUnmakeRowHeader = function (grid, detail, comparator, genWrappers) {
  4191. var newGrid = replaceRow(grid, detail.row(), comparator, genWrappers.replaceOrInit);
  4192. return bundle(newGrid, detail.row(), detail.column());
  4193. };
  4194. var opUnmakeColumnHeader = function (grid, detail, comparator, genWrappers) {
  4195. var newGrid = replaceColumn(grid, detail.column(), comparator, genWrappers.replaceOrInit);
  4196. return bundle(newGrid, detail.row(), detail.column());
  4197. };
  4198. var opEraseColumns = function (grid, details, _comparator, _genWrappers) {
  4199. var columns = uniqueColumns(details);
  4200. var newGrid = deleteColumnsAt(grid, columns[0].column(), columns[columns.length - 1].column());
  4201. var cursor = elementFromGrid(newGrid, details[0].row(), details[0].column());
  4202. return outcome(newGrid, cursor);
  4203. };
  4204. var opEraseRows = function (grid, details, _comparator, _genWrappers) {
  4205. var rows = uniqueRows(details);
  4206. var newGrid = deleteRowsAt(grid, rows[0].row(), rows[rows.length - 1].row());
  4207. var cursor = elementFromGrid(newGrid, details[0].row(), details[0].column());
  4208. return outcome(newGrid, cursor);
  4209. };
  4210. var opMergeCells = function (grid, mergable, comparator, _genWrappers) {
  4211. var cells = mergable.cells();
  4212. merge$2(cells);
  4213. var newGrid = merge(grid, mergable.bounds(), comparator, constant(cells[0]));
  4214. return outcome(newGrid, Option.from(cells[0]));
  4215. };
  4216. var opUnmergeCells = function (grid, unmergable, comparator, genWrappers) {
  4217. var newGrid = foldr(unmergable, function (b, cell) {
  4218. return unmerge(b, cell, comparator, genWrappers.combine(cell));
  4219. }, grid);
  4220. return outcome(newGrid, Option.from(unmergable[0]));
  4221. };
  4222. var opPasteCells = function (grid, pasteDetails, comparator, _genWrappers) {
  4223. var gridify = function (table, generators) {
  4224. var list = fromTable(table);
  4225. var wh = Warehouse.generate(list);
  4226. return toGrid(wh, generators, true);
  4227. };
  4228. var gridB = gridify(pasteDetails.clipboard(), pasteDetails.generators());
  4229. var startAddress = address(pasteDetails.row(), pasteDetails.column());
  4230. var mergedGrid = merge$1(startAddress, grid, gridB, pasteDetails.generators(), comparator);
  4231. return mergedGrid.fold(function () {
  4232. return outcome(grid, Option.some(pasteDetails.element()));
  4233. }, function (nuGrid) {
  4234. var cursor = elementFromGrid(nuGrid, pasteDetails.row(), pasteDetails.column());
  4235. return outcome(nuGrid, cursor);
  4236. });
  4237. };
  4238. var gridifyRows = function (rows, generators, example) {
  4239. var pasteDetails = fromPastedRows(rows, example);
  4240. var wh = Warehouse.generate(pasteDetails);
  4241. return toGrid(wh, generators, true);
  4242. };
  4243. var opPasteRowsBefore = function (grid, pasteDetails, comparator, _genWrappers) {
  4244. var example = grid[pasteDetails.cells[0].row()];
  4245. var index = pasteDetails.cells[0].row();
  4246. var gridB = gridifyRows(pasteDetails.clipboard(), pasteDetails.generators(), example);
  4247. var mergedGrid = insert(index, grid, gridB, pasteDetails.generators(), comparator);
  4248. var cursor = elementFromGrid(mergedGrid, pasteDetails.cells[0].row(), pasteDetails.cells[0].column());
  4249. return outcome(mergedGrid, cursor);
  4250. };
  4251. var opPasteRowsAfter = function (grid, pasteDetails, comparator, _genWrappers) {
  4252. var example = grid[pasteDetails.cells[0].row()];
  4253. var index = pasteDetails.cells[pasteDetails.cells.length - 1].row() + pasteDetails.cells[pasteDetails.cells.length - 1].rowspan();
  4254. var gridB = gridifyRows(pasteDetails.clipboard(), pasteDetails.generators(), example);
  4255. var mergedGrid = insert(index, grid, gridB, pasteDetails.generators(), comparator);
  4256. var cursor = elementFromGrid(mergedGrid, pasteDetails.cells[0].row(), pasteDetails.cells[0].column());
  4257. return outcome(mergedGrid, cursor);
  4258. };
  4259. var resize = adjustWidthTo;
  4260. var insertRowsBefore = run(opInsertRowsBefore, onCells, noop, noop, Generators.modification);
  4261. var insertRowsAfter = run(opInsertRowsAfter, onCells, noop, noop, Generators.modification);
  4262. var insertColumnsBefore = run(opInsertColumnsBefore, onCells, resize, noop, Generators.modification);
  4263. var insertColumnsAfter = run(opInsertColumnsAfter, onCells, resize, noop, Generators.modification);
  4264. var eraseColumns = run(opEraseColumns, onCells, resize, prune, Generators.modification);
  4265. var eraseRows = run(opEraseRows, onCells, noop, prune, Generators.modification);
  4266. var makeColumnHeader = run(opMakeColumnHeader, onCell, noop, noop, Generators.transform('row', 'th'));
  4267. var unmakeColumnHeader = run(opUnmakeColumnHeader, onCell, noop, noop, Generators.transform(null, 'td'));
  4268. var makeRowHeader = run(opMakeRowHeader, onCell, noop, noop, Generators.transform('col', 'th'));
  4269. var unmakeRowHeader = run(opUnmakeRowHeader, onCell, noop, noop, Generators.transform(null, 'td'));
  4270. var mergeCells = run(opMergeCells, onMergable, noop, noop, Generators.merging);
  4271. var unmergeCells = run(opUnmergeCells, onUnmergable, resize, noop, Generators.merging);
  4272. var pasteCells = run(opPasteCells, onPaste, resize, noop, Generators.modification);
  4273. var pasteRowsBefore = run(opPasteRowsBefore, onPasteRows, noop, noop, Generators.modification);
  4274. var pasteRowsAfter = run(opPasteRowsAfter, onPasteRows, noop, noop, Generators.modification);
  4275. var getBody$1 = function (editor) {
  4276. return Element.fromDom(editor.getBody());
  4277. };
  4278. var getPixelWidth$1 = function (elm) {
  4279. return elm.getBoundingClientRect().width;
  4280. };
  4281. var getPixelHeight = function (elm) {
  4282. return elm.getBoundingClientRect().height;
  4283. };
  4284. var getIsRoot = function (editor) {
  4285. return function (element) {
  4286. return eq(element, getBody$1(editor));
  4287. };
  4288. };
  4289. var removePxSuffix = function (size) {
  4290. return size ? size.replace(/px$/, '') : '';
  4291. };
  4292. var addSizeSuffix = function (size) {
  4293. if (/^[0-9]+$/.test(size)) {
  4294. size += 'px';
  4295. }
  4296. return size;
  4297. };
  4298. var removeDataStyle = function (table) {
  4299. var dataStyleCells = descendants$1(table, 'td[data-mce-style],th[data-mce-style]');
  4300. remove(table, 'data-mce-style');
  4301. each(dataStyleCells, function (cell) {
  4302. remove(cell, 'data-mce-style');
  4303. });
  4304. };
  4305. var getDirection = function (element) {
  4306. return get$2(element, 'direction') === 'rtl' ? 'rtl' : 'ltr';
  4307. };
  4308. var ltr$1 = { isRtl: constant(false) };
  4309. var rtl$1 = { isRtl: constant(true) };
  4310. var directionAt = function (element) {
  4311. var dir = getDirection(element);
  4312. return dir === 'rtl' ? rtl$1 : ltr$1;
  4313. };
  4314. var defaultTableToolbar = 'tableprops tabledelete | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol';
  4315. var defaultStyles = {
  4316. 'border-collapse': 'collapse',
  4317. 'width': '100%'
  4318. };
  4319. var defaultAttributes = { border: '1' };
  4320. var getDefaultAttributes = function (editor) {
  4321. return editor.getParam('table_default_attributes', defaultAttributes, 'object');
  4322. };
  4323. var getDefaultStyles = function (editor) {
  4324. return editor.getParam('table_default_styles', defaultStyles, 'object');
  4325. };
  4326. var hasTableResizeBars = function (editor) {
  4327. return editor.getParam('table_resize_bars', true, 'boolean');
  4328. };
  4329. var hasTabNavigation = function (editor) {
  4330. return editor.getParam('table_tab_navigation', true, 'boolean');
  4331. };
  4332. var hasAdvancedCellTab = function (editor) {
  4333. return editor.getParam('table_cell_advtab', true, 'boolean');
  4334. };
  4335. var hasAdvancedRowTab = function (editor) {
  4336. return editor.getParam('table_row_advtab', true, 'boolean');
  4337. };
  4338. var hasAdvancedTableTab = function (editor) {
  4339. return editor.getParam('table_advtab', true, 'boolean');
  4340. };
  4341. var hasAppearanceOptions = function (editor) {
  4342. return editor.getParam('table_appearance_options', true, 'boolean');
  4343. };
  4344. var hasTableGrid = function (editor) {
  4345. return editor.getParam('table_grid', true, 'boolean');
  4346. };
  4347. var shouldStyleWithCss = function (editor) {
  4348. return editor.getParam('table_style_by_css', false, 'boolean');
  4349. };
  4350. var getCellClassList = function (editor) {
  4351. return editor.getParam('table_cell_class_list', [], 'array');
  4352. };
  4353. var getRowClassList = function (editor) {
  4354. return editor.getParam('table_row_class_list', [], 'array');
  4355. };
  4356. var getTableClassList = function (editor) {
  4357. return editor.getParam('table_class_list', [], 'array');
  4358. };
  4359. var isPercentagesForced = function (editor) {
  4360. return editor.getParam('table_responsive_width') === true;
  4361. };
  4362. var isPixelsForced = function (editor) {
  4363. return editor.getParam('table_responsive_width') === false;
  4364. };
  4365. var getToolbar = function (editor) {
  4366. return editor.getParam('table_toolbar', defaultTableToolbar);
  4367. };
  4368. var getCloneElements = function (editor) {
  4369. var cloneElements = editor.getParam('table_clone_elements');
  4370. if (isString(cloneElements)) {
  4371. return Option.some(cloneElements.split(/[ ,]/));
  4372. } else if (Array.isArray(cloneElements)) {
  4373. return Option.some(cloneElements);
  4374. } else {
  4375. return Option.none();
  4376. }
  4377. };
  4378. var hasObjectResizing = function (editor) {
  4379. var objectResizing = editor.getParam('object_resizing', true);
  4380. return isString(objectResizing) ? objectResizing === 'table' : objectResizing;
  4381. };
  4382. var fireNewRow = function (editor, row) {
  4383. return editor.fire('newrow', { node: row });
  4384. };
  4385. var fireNewCell = function (editor, cell) {
  4386. return editor.fire('newcell', { node: cell });
  4387. };
  4388. var fireObjectResizeStart = function (editor, target, width, height) {
  4389. editor.fire('ObjectResizeStart', {
  4390. target: target,
  4391. width: width,
  4392. height: height
  4393. });
  4394. };
  4395. var fireObjectResized = function (editor, target, width, height) {
  4396. editor.fire('ObjectResized', {
  4397. target: target,
  4398. width: width,
  4399. height: height
  4400. });
  4401. };
  4402. var fireTableSelectionChange = function (editor, cells, start, finish, otherCells) {
  4403. editor.fire('TableSelectionChange', {
  4404. cells: cells,
  4405. start: start,
  4406. finish: finish,
  4407. otherCells: otherCells
  4408. });
  4409. };
  4410. var fireTableSelectionClear = function (editor) {
  4411. editor.fire('TableSelectionClear');
  4412. };
  4413. var point = function (element, offset) {
  4414. return {
  4415. element: constant(element),
  4416. offset: constant(offset)
  4417. };
  4418. };
  4419. var scan = function (universe, element, direction) {
  4420. if (universe.property().isText(element) && universe.property().getText(element).trim().length === 0 || universe.property().isComment(element)) {
  4421. return direction(element).bind(function (elem) {
  4422. return scan(universe, elem, direction).orThunk(function () {
  4423. return Option.some(elem);
  4424. });
  4425. });
  4426. } else {
  4427. return Option.none();
  4428. }
  4429. };
  4430. var toEnd = function (universe, element) {
  4431. if (universe.property().isText(element)) {
  4432. return universe.property().getText(element).length;
  4433. }
  4434. var children = universe.property().children(element);
  4435. return children.length;
  4436. };
  4437. var freefallRtl = function (universe, element) {
  4438. var candidate = scan(universe, element, universe.query().prevSibling).getOr(element);
  4439. if (universe.property().isText(candidate)) {
  4440. return point(candidate, toEnd(universe, candidate));
  4441. }
  4442. var children = universe.property().children(candidate);
  4443. return children.length > 0 ? freefallRtl(universe, children[children.length - 1]) : point(candidate, toEnd(universe, candidate));
  4444. };
  4445. var freefallRtl$1 = freefallRtl;
  4446. var universe$2 = DomUniverse();
  4447. var freefallRtl$2 = function (element) {
  4448. return freefallRtl$1(universe$2, element);
  4449. };
  4450. var TableActions = function (editor, lazyWire) {
  4451. var isTableBody = function (editor) {
  4452. return name(getBody$1(editor)) === 'table';
  4453. };
  4454. var lastRowGuard = function (table) {
  4455. var size = getGridSize(table);
  4456. return isTableBody(editor) === false || size.rows() > 1;
  4457. };
  4458. var lastColumnGuard = function (table) {
  4459. var size = getGridSize(table);
  4460. return isTableBody(editor) === false || size.columns() > 1;
  4461. };
  4462. var cloneFormats = getCloneElements(editor);
  4463. var execute = function (operation, guard, mutate, lazyWire) {
  4464. return function (table, target) {
  4465. removeDataStyle(table);
  4466. var wire = lazyWire();
  4467. var doc = Element.fromDom(editor.getDoc());
  4468. var direction = TableDirection(directionAt);
  4469. var generators = cellOperations(mutate, doc, cloneFormats);
  4470. return guard(table) ? operation(wire, table, target, generators, direction).bind(function (result) {
  4471. each(result.newRows(), function (row) {
  4472. fireNewRow(editor, row.dom());
  4473. });
  4474. each(result.newCells(), function (cell) {
  4475. fireNewCell(editor, cell.dom());
  4476. });
  4477. return result.cursor().map(function (cell) {
  4478. var des = freefallRtl$2(cell);
  4479. var rng = editor.dom.createRng();
  4480. rng.setStart(des.element().dom(), des.offset());
  4481. rng.setEnd(des.element().dom(), des.offset());
  4482. return rng;
  4483. });
  4484. }) : Option.none();
  4485. };
  4486. };
  4487. var deleteRow = execute(eraseRows, lastRowGuard, noop, lazyWire);
  4488. var deleteColumn = execute(eraseColumns, lastColumnGuard, noop, lazyWire);
  4489. var insertRowsBefore$1 = execute(insertRowsBefore, always, noop, lazyWire);
  4490. var insertRowsAfter$1 = execute(insertRowsAfter, always, noop, lazyWire);
  4491. var insertColumnsBefore$1 = execute(insertColumnsBefore, always, halve, lazyWire);
  4492. var insertColumnsAfter$1 = execute(insertColumnsAfter, always, halve, lazyWire);
  4493. var mergeCells$1 = execute(mergeCells, always, noop, lazyWire);
  4494. var unmergeCells$1 = execute(unmergeCells, always, noop, lazyWire);
  4495. var pasteRowsBefore$1 = execute(pasteRowsBefore, always, noop, lazyWire);
  4496. var pasteRowsAfter$1 = execute(pasteRowsAfter, always, noop, lazyWire);
  4497. var pasteCells$1 = execute(pasteCells, always, noop, lazyWire);
  4498. return {
  4499. deleteRow: deleteRow,
  4500. deleteColumn: deleteColumn,
  4501. insertRowsBefore: insertRowsBefore$1,
  4502. insertRowsAfter: insertRowsAfter$1,
  4503. insertColumnsBefore: insertColumnsBefore$1,
  4504. insertColumnsAfter: insertColumnsAfter$1,
  4505. mergeCells: mergeCells$1,
  4506. unmergeCells: unmergeCells$1,
  4507. pasteRowsBefore: pasteRowsBefore$1,
  4508. pasteRowsAfter: pasteRowsAfter$1,
  4509. pasteCells: pasteCells$1
  4510. };
  4511. };
  4512. var copyRows = function (table, target, generators) {
  4513. var list = fromTable(table);
  4514. var house = Warehouse.generate(list);
  4515. var details = onCells(house, target);
  4516. return details.map(function (selectedCells) {
  4517. var grid = toGrid(house, generators, false);
  4518. var slicedGrid = grid.slice(selectedCells[0].row(), selectedCells[selectedCells.length - 1].row() + selectedCells[selectedCells.length - 1].rowspan());
  4519. var slicedDetails = toDetailList(slicedGrid, generators);
  4520. return copy$2(slicedDetails);
  4521. });
  4522. };
  4523. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  4524. var getSelectionStartFromSelector = function (selector) {
  4525. return function (editor) {
  4526. return Option.from(editor.dom.getParent(editor.selection.getStart(), selector)).map(Element.fromDom);
  4527. };
  4528. };
  4529. var getSelectionStartCell = getSelectionStartFromSelector('th,td');
  4530. var getSelectionStartCellOrCaption = getSelectionStartFromSelector('th,td,caption');
  4531. var getTDTHOverallStyle = function (dom, elm, name) {
  4532. var cells = dom.select('td,th', elm);
  4533. var firstChildStyle;
  4534. var checkChildren = function (firstChildStyle, elms) {
  4535. for (var i = 0; i < elms.length; i++) {
  4536. var currentStyle = dom.getStyle(elms[i], name);
  4537. if (typeof firstChildStyle === 'undefined') {
  4538. firstChildStyle = currentStyle;
  4539. }
  4540. if (firstChildStyle !== currentStyle) {
  4541. return '';
  4542. }
  4543. }
  4544. return firstChildStyle;
  4545. };
  4546. firstChildStyle = checkChildren(firstChildStyle, cells);
  4547. return firstChildStyle;
  4548. };
  4549. var applyAlign = function (editor, elm, name) {
  4550. if (name) {
  4551. editor.formatter.apply('align' + name, {}, elm);
  4552. }
  4553. };
  4554. var applyVAlign = function (editor, elm, name) {
  4555. if (name) {
  4556. editor.formatter.apply('valign' + name, {}, elm);
  4557. }
  4558. };
  4559. var unApplyAlign = function (editor, elm) {
  4560. global$1.each('left center right'.split(' '), function (name) {
  4561. editor.formatter.remove('align' + name, {}, elm);
  4562. });
  4563. };
  4564. var unApplyVAlign = function (editor, elm) {
  4565. global$1.each('top middle bottom'.split(' '), function (name) {
  4566. editor.formatter.remove('valign' + name, {}, elm);
  4567. });
  4568. };
  4569. var buildListItems = function (inputList, itemCallback, startItems) {
  4570. var appendItems = function (values, output) {
  4571. output = output || [];
  4572. global$1.each(values, function (item) {
  4573. var menuItem = { text: item.text || item.title };
  4574. if (item.menu) {
  4575. menuItem.menu = appendItems(item.menu);
  4576. } else {
  4577. menuItem.value = item.value;
  4578. if (itemCallback) {
  4579. itemCallback(menuItem);
  4580. }
  4581. }
  4582. output.push(menuItem);
  4583. });
  4584. return output;
  4585. };
  4586. return appendItems(inputList, startItems || []);
  4587. };
  4588. var extractAdvancedStyles = function (dom, elm) {
  4589. var rgbToHex = function (value) {
  4590. return startsWith(value, 'rgb') ? dom.toHex(value) : value;
  4591. };
  4592. var borderWidth = getRaw(Element.fromDom(elm), 'border-width').getOr('');
  4593. var borderStyle = getRaw(Element.fromDom(elm), 'border-style').getOr('');
  4594. var borderColor = getRaw(Element.fromDom(elm), 'border-color').map(rgbToHex).getOr('');
  4595. var bgColor = getRaw(Element.fromDom(elm), 'background-color').map(rgbToHex).getOr('');
  4596. return {
  4597. borderwidth: borderWidth,
  4598. borderstyle: borderStyle,
  4599. bordercolor: borderColor,
  4600. backgroundcolor: bgColor
  4601. };
  4602. };
  4603. var getSharedValues = function (data) {
  4604. var baseData = data[0];
  4605. var comparisonData = data.slice(1);
  4606. var keys$1 = keys(baseData);
  4607. each(comparisonData, function (items) {
  4608. each(keys$1, function (key) {
  4609. each$1(items, function (itemValue, itemKey) {
  4610. var comparisonValue = baseData[key];
  4611. if (comparisonValue !== '' && key === itemKey) {
  4612. if (comparisonValue !== itemValue) {
  4613. baseData[key] = '';
  4614. }
  4615. }
  4616. });
  4617. });
  4618. });
  4619. return baseData;
  4620. };
  4621. var getAdvancedTab = function (dialogName) {
  4622. var advTabItems = [
  4623. {
  4624. name: 'borderstyle',
  4625. type: 'selectbox',
  4626. label: 'Border style',
  4627. items: [
  4628. {
  4629. text: 'Select...',
  4630. value: ''
  4631. },
  4632. {
  4633. text: 'Solid',
  4634. value: 'solid'
  4635. },
  4636. {
  4637. text: 'Dotted',
  4638. value: 'dotted'
  4639. },
  4640. {
  4641. text: 'Dashed',
  4642. value: 'dashed'
  4643. },
  4644. {
  4645. text: 'Double',
  4646. value: 'double'
  4647. },
  4648. {
  4649. text: 'Groove',
  4650. value: 'groove'
  4651. },
  4652. {
  4653. text: 'Ridge',
  4654. value: 'ridge'
  4655. },
  4656. {
  4657. text: 'Inset',
  4658. value: 'inset'
  4659. },
  4660. {
  4661. text: 'Outset',
  4662. value: 'outset'
  4663. },
  4664. {
  4665. text: 'None',
  4666. value: 'none'
  4667. },
  4668. {
  4669. text: 'Hidden',
  4670. value: 'hidden'
  4671. }
  4672. ]
  4673. },
  4674. {
  4675. name: 'bordercolor',
  4676. type: 'colorinput',
  4677. label: 'Border color'
  4678. },
  4679. {
  4680. name: 'backgroundcolor',
  4681. type: 'colorinput',
  4682. label: 'Background color'
  4683. }
  4684. ];
  4685. var borderWidth = {
  4686. name: 'borderwidth',
  4687. type: 'input',
  4688. label: 'Border width'
  4689. };
  4690. var items = dialogName === 'cell' ? [borderWidth].concat(advTabItems) : advTabItems;
  4691. return {
  4692. title: 'Advanced',
  4693. name: 'advanced',
  4694. items: items
  4695. };
  4696. };
  4697. var getAlignment = function (alignments, formatName, dataName, editor, elm) {
  4698. var alignmentData = {};
  4699. global$1.each(alignments.split(' '), function (name) {
  4700. if (editor.formatter.matchNode(elm, formatName + name)) {
  4701. alignmentData[dataName] = name;
  4702. }
  4703. });
  4704. if (!alignmentData[dataName]) {
  4705. alignmentData[dataName] = '';
  4706. }
  4707. return alignmentData;
  4708. };
  4709. var getHAlignment = curry(getAlignment, 'left center right');
  4710. var getVAlignment = curry(getAlignment, 'top middle bottom');
  4711. var extractDataFromSettings = function (editor, hasAdvTableTab) {
  4712. var style = getDefaultStyles(editor);
  4713. var attrs = getDefaultAttributes(editor);
  4714. var extractAdvancedStyleData = function (dom) {
  4715. var rgbToHex = function (value) {
  4716. return startsWith(value, 'rgb') ? dom.toHex(value) : value;
  4717. };
  4718. var borderStyle = get(style, 'border-style').getOr('');
  4719. var borderColor = get(style, 'border-color').getOr('');
  4720. var bgColor = get(style, 'background-color').getOr('');
  4721. return {
  4722. borderstyle: borderStyle,
  4723. bordercolor: rgbToHex(borderColor),
  4724. backgroundcolor: rgbToHex(bgColor)
  4725. };
  4726. };
  4727. var defaultData = {
  4728. height: '',
  4729. width: '100%',
  4730. cellspacing: '',
  4731. cellpadding: '',
  4732. caption: false,
  4733. class: '',
  4734. align: '',
  4735. border: ''
  4736. };
  4737. var getBorder = function () {
  4738. var borderWidth = style['border-width'];
  4739. if (shouldStyleWithCss(editor) && borderWidth) {
  4740. return { border: borderWidth };
  4741. }
  4742. return get(attrs, 'border').fold(function () {
  4743. return {};
  4744. }, function (border) {
  4745. return { border: border };
  4746. });
  4747. };
  4748. var dom = editor.dom;
  4749. var advStyle = hasAdvTableTab ? extractAdvancedStyleData(dom) : {};
  4750. var getCellPaddingCellSpacing = function () {
  4751. var spacing = get(style, 'border-spacing').or(get(attrs, 'cellspacing')).fold(function () {
  4752. return {};
  4753. }, function (cellspacing) {
  4754. return { cellspacing: cellspacing };
  4755. });
  4756. var padding = get(style, 'border-padding').or(get(attrs, 'cellpadding')).fold(function () {
  4757. return {};
  4758. }, function (cellpadding) {
  4759. return { cellpadding: cellpadding };
  4760. });
  4761. return __assign(__assign({}, spacing), padding);
  4762. };
  4763. var data = __assign(__assign(__assign(__assign(__assign(__assign({}, defaultData), style), attrs), advStyle), getBorder()), getCellPaddingCellSpacing());
  4764. return data;
  4765. };
  4766. var extractDataFromTableElement = function (editor, elm, hasAdvTableTab) {
  4767. var getBorder = function (dom, elm) {
  4768. var optBorderWidth = getRaw(Element.fromDom(elm), 'border-width');
  4769. if (shouldStyleWithCss(editor) && optBorderWidth.isSome()) {
  4770. return optBorderWidth.getOr('');
  4771. }
  4772. return dom.getAttrib(elm, 'border') || getTDTHOverallStyle(editor.dom, elm, 'border-width') || getTDTHOverallStyle(editor.dom, elm, 'border');
  4773. };
  4774. var dom = editor.dom;
  4775. var data = __assign(__assign({
  4776. width: dom.getStyle(elm, 'width') || dom.getAttrib(elm, 'width'),
  4777. height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
  4778. cellspacing: dom.getStyle(elm, 'border-spacing') || dom.getAttrib(elm, 'cellspacing'),
  4779. cellpadding: dom.getAttrib(elm, 'cellpadding') || getTDTHOverallStyle(editor.dom, elm, 'padding'),
  4780. border: getBorder(dom, elm),
  4781. caption: !!dom.select('caption', elm)[0],
  4782. class: dom.getAttrib(elm, 'class', '')
  4783. }, getHAlignment('align', 'align', editor, elm)), hasAdvTableTab ? extractAdvancedStyles(dom, elm) : {});
  4784. return data;
  4785. };
  4786. var extractDataFromRowElement = function (editor, elm, hasAdvancedRowTab) {
  4787. var dom = editor.dom;
  4788. var data = __assign(__assign({
  4789. height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
  4790. scope: dom.getAttrib(elm, 'scope'),
  4791. class: dom.getAttrib(elm, 'class', ''),
  4792. align: '',
  4793. type: elm.parentNode.nodeName.toLowerCase()
  4794. }, getHAlignment('align', 'align', editor, elm)), hasAdvancedRowTab ? extractAdvancedStyles(dom, elm) : {});
  4795. return data;
  4796. };
  4797. var extractDataFromCellElement = function (editor, elm, hasAdvancedCellTab) {
  4798. var dom = editor.dom;
  4799. var data = __assign(__assign(__assign({
  4800. width: dom.getStyle(elm, 'width') || dom.getAttrib(elm, 'width'),
  4801. height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
  4802. scope: dom.getAttrib(elm, 'scope'),
  4803. celltype: elm.nodeName.toLowerCase(),
  4804. class: dom.getAttrib(elm, 'class', '')
  4805. }, getHAlignment('align', 'halign', editor, elm)), getVAlignment('valign', 'valign', editor, elm)), hasAdvancedCellTab ? extractAdvancedStyles(dom, elm) : {});
  4806. return data;
  4807. };
  4808. var getClassList = function (editor) {
  4809. var rowClassList = getCellClassList(editor);
  4810. var classes = buildListItems(rowClassList, function (item) {
  4811. if (item.value) {
  4812. item.textStyle = function () {
  4813. return editor.formatter.getCssText({
  4814. block: 'tr',
  4815. classes: [item.value]
  4816. });
  4817. };
  4818. }
  4819. });
  4820. if (rowClassList.length > 0) {
  4821. return Option.some({
  4822. name: 'class',
  4823. type: 'selectbox',
  4824. label: 'Class',
  4825. items: classes
  4826. });
  4827. }
  4828. return Option.none();
  4829. };
  4830. var children$3 = [
  4831. {
  4832. name: 'width',
  4833. type: 'input',
  4834. label: 'Width'
  4835. },
  4836. {
  4837. name: 'height',
  4838. type: 'input',
  4839. label: 'Height'
  4840. },
  4841. {
  4842. name: 'celltype',
  4843. type: 'selectbox',
  4844. label: 'Cell type',
  4845. items: [
  4846. {
  4847. text: 'Cell',
  4848. value: 'td'
  4849. },
  4850. {
  4851. text: 'Header cell',
  4852. value: 'th'
  4853. }
  4854. ]
  4855. },
  4856. {
  4857. name: 'scope',
  4858. type: 'selectbox',
  4859. label: 'Scope',
  4860. items: [
  4861. {
  4862. text: 'None',
  4863. value: ''
  4864. },
  4865. {
  4866. text: 'Row',
  4867. value: 'row'
  4868. },
  4869. {
  4870. text: 'Column',
  4871. value: 'col'
  4872. },
  4873. {
  4874. text: 'Row group',
  4875. value: 'rowgroup'
  4876. },
  4877. {
  4878. text: 'Column group',
  4879. value: 'colgroup'
  4880. }
  4881. ]
  4882. },
  4883. {
  4884. name: 'halign',
  4885. type: 'selectbox',
  4886. label: 'H Align',
  4887. items: [
  4888. {
  4889. text: 'None',
  4890. value: ''
  4891. },
  4892. {
  4893. text: 'Left',
  4894. value: 'left'
  4895. },
  4896. {
  4897. text: 'Center',
  4898. value: 'center'
  4899. },
  4900. {
  4901. text: 'Right',
  4902. value: 'right'
  4903. }
  4904. ]
  4905. },
  4906. {
  4907. name: 'valign',
  4908. type: 'selectbox',
  4909. label: 'V Align',
  4910. items: [
  4911. {
  4912. text: 'None',
  4913. value: ''
  4914. },
  4915. {
  4916. text: 'Top',
  4917. value: 'top'
  4918. },
  4919. {
  4920. text: 'Middle',
  4921. value: 'middle'
  4922. },
  4923. {
  4924. text: 'Bottom',
  4925. value: 'bottom'
  4926. }
  4927. ]
  4928. }
  4929. ];
  4930. var getItems = function (editor) {
  4931. return getClassList(editor).fold(function () {
  4932. return children$3;
  4933. }, function (classlist) {
  4934. return children$3.concat(classlist);
  4935. });
  4936. };
  4937. var normal = function (dom, node) {
  4938. var setAttrib = function (attr, value) {
  4939. dom.setAttrib(node, attr, value);
  4940. };
  4941. var setStyle = function (prop, value) {
  4942. dom.setStyle(node, prop, value);
  4943. };
  4944. return {
  4945. setAttrib: setAttrib,
  4946. setStyle: setStyle
  4947. };
  4948. };
  4949. var ifTruthy = function (dom, node) {
  4950. var setAttrib = function (attr, value) {
  4951. if (value) {
  4952. dom.setAttrib(node, attr, value);
  4953. }
  4954. };
  4955. var setStyle = function (prop, value) {
  4956. if (value) {
  4957. dom.setStyle(node, prop, value);
  4958. }
  4959. };
  4960. return {
  4961. setAttrib: setAttrib,
  4962. setStyle: setStyle
  4963. };
  4964. };
  4965. var DomModifier = {
  4966. normal: normal,
  4967. ifTruthy: ifTruthy
  4968. };
  4969. var updateSimpleProps = function (modifiers, data) {
  4970. modifiers.setAttrib('scope', data.scope);
  4971. modifiers.setAttrib('class', data.class);
  4972. modifiers.setStyle('width', addSizeSuffix(data.width));
  4973. modifiers.setStyle('height', addSizeSuffix(data.height));
  4974. };
  4975. var updateAdvancedProps = function (modifiers, data) {
  4976. modifiers.setStyle('background-color', data.backgroundcolor);
  4977. modifiers.setStyle('border-color', data.bordercolor);
  4978. modifiers.setStyle('border-style', data.borderstyle);
  4979. modifiers.setStyle('border-width', addSizeSuffix(data.borderwidth));
  4980. };
  4981. var applyToSingle = function (editor, cells, data) {
  4982. var dom = editor.dom;
  4983. var cellElm = data.celltype && cells[0].nodeName.toLowerCase() !== data.celltype ? dom.rename(cells[0], data.celltype) : cells[0];
  4984. var modifiers = DomModifier.normal(dom, cellElm);
  4985. updateSimpleProps(modifiers, data);
  4986. if (hasAdvancedCellTab(editor)) {
  4987. updateAdvancedProps(modifiers, data);
  4988. }
  4989. unApplyAlign(editor, cellElm);
  4990. unApplyVAlign(editor, cellElm);
  4991. if (data.halign) {
  4992. applyAlign(editor, cellElm, data.halign);
  4993. }
  4994. if (data.valign) {
  4995. applyVAlign(editor, cellElm, data.valign);
  4996. }
  4997. };
  4998. var applyToMultiple = function (editor, cells, data) {
  4999. var dom = editor.dom;
  5000. global$1.each(cells, function (cellElm) {
  5001. if (data.celltype && cellElm.nodeName.toLowerCase() !== data.celltype) {
  5002. cellElm = dom.rename(cellElm, data.celltype);
  5003. }
  5004. var modifiers = DomModifier.ifTruthy(dom, cellElm);
  5005. updateSimpleProps(modifiers, data);
  5006. if (hasAdvancedCellTab(editor)) {
  5007. updateAdvancedProps(modifiers, data);
  5008. }
  5009. if (data.halign) {
  5010. applyAlign(editor, cellElm, data.halign);
  5011. }
  5012. if (data.valign) {
  5013. applyVAlign(editor, cellElm, data.valign);
  5014. }
  5015. });
  5016. };
  5017. var onSubmitCellForm = function (editor, cells, api) {
  5018. var data = api.getData();
  5019. api.close();
  5020. editor.undoManager.transact(function () {
  5021. var applicator = cells.length === 1 ? applyToSingle : applyToMultiple;
  5022. applicator(editor, cells, data);
  5023. editor.focus();
  5024. });
  5025. };
  5026. var open = function (editor) {
  5027. var cellElm, cells = [];
  5028. cells = editor.dom.select('td[data-mce-selected],th[data-mce-selected]');
  5029. cellElm = editor.dom.getParent(editor.selection.getStart(), 'td,th');
  5030. if (!cells.length && cellElm) {
  5031. cells.push(cellElm);
  5032. }
  5033. cellElm = cellElm || cells[0];
  5034. if (!cellElm) {
  5035. return;
  5036. }
  5037. var cellsData = global$1.map(cells, function (cellElm) {
  5038. return extractDataFromCellElement(editor, cellElm, hasAdvancedCellTab(editor));
  5039. });
  5040. var data = getSharedValues(cellsData);
  5041. var dialogTabPanel = {
  5042. type: 'tabpanel',
  5043. tabs: [
  5044. {
  5045. title: 'General',
  5046. name: 'general',
  5047. items: getItems(editor)
  5048. },
  5049. getAdvancedTab('cell')
  5050. ]
  5051. };
  5052. var dialogPanel = {
  5053. type: 'panel',
  5054. items: [{
  5055. type: 'grid',
  5056. columns: 2,
  5057. items: getItems(editor)
  5058. }]
  5059. };
  5060. editor.windowManager.open({
  5061. title: 'Cell Properties',
  5062. size: 'normal',
  5063. body: hasAdvancedCellTab(editor) ? dialogTabPanel : dialogPanel,
  5064. buttons: [
  5065. {
  5066. type: 'cancel',
  5067. name: 'cancel',
  5068. text: 'Cancel'
  5069. },
  5070. {
  5071. type: 'submit',
  5072. name: 'save',
  5073. text: 'Save',
  5074. primary: true
  5075. }
  5076. ],
  5077. initialData: data,
  5078. onSubmit: curry(onSubmitCellForm, editor, cells)
  5079. });
  5080. };
  5081. var getClassList$1 = function (editor) {
  5082. var rowClassList = getRowClassList(editor);
  5083. var classes = buildListItems(rowClassList, function (item) {
  5084. if (item.value) {
  5085. item.textStyle = function () {
  5086. return editor.formatter.getCssText({
  5087. block: 'tr',
  5088. classes: [item.value]
  5089. });
  5090. };
  5091. }
  5092. });
  5093. if (rowClassList.length > 0) {
  5094. return Option.some({
  5095. name: 'class',
  5096. type: 'selectbox',
  5097. label: 'Class',
  5098. items: classes
  5099. });
  5100. }
  5101. return Option.none();
  5102. };
  5103. var formChildren = [
  5104. {
  5105. type: 'selectbox',
  5106. name: 'type',
  5107. label: 'Row type',
  5108. items: [
  5109. {
  5110. text: 'Header',
  5111. value: 'thead'
  5112. },
  5113. {
  5114. text: 'Body',
  5115. value: 'tbody'
  5116. },
  5117. {
  5118. text: 'Footer',
  5119. value: 'tfoot'
  5120. }
  5121. ]
  5122. },
  5123. {
  5124. type: 'selectbox',
  5125. name: 'align',
  5126. label: 'Alignment',
  5127. items: [
  5128. {
  5129. text: 'None',
  5130. value: ''
  5131. },
  5132. {
  5133. text: 'Left',
  5134. value: 'left'
  5135. },
  5136. {
  5137. text: 'Center',
  5138. value: 'center'
  5139. },
  5140. {
  5141. text: 'Right',
  5142. value: 'right'
  5143. }
  5144. ]
  5145. },
  5146. {
  5147. label: 'Height',
  5148. name: 'height',
  5149. type: 'input'
  5150. }
  5151. ];
  5152. var getItems$1 = function (editor) {
  5153. return getClassList$1(editor).fold(function () {
  5154. return formChildren;
  5155. }, function (classes) {
  5156. return formChildren.concat(classes);
  5157. });
  5158. };
  5159. var switchRowType = function (dom, rowElm, toType) {
  5160. var tableElm = dom.getParent(rowElm, 'table');
  5161. var oldParentElm = rowElm.parentNode;
  5162. var parentElm = dom.select(toType, tableElm)[0];
  5163. if (!parentElm) {
  5164. parentElm = dom.create(toType);
  5165. if (tableElm.firstChild) {
  5166. if (tableElm.firstChild.nodeName === 'CAPTION') {
  5167. dom.insertAfter(parentElm, tableElm.firstChild);
  5168. } else {
  5169. tableElm.insertBefore(parentElm, tableElm.firstChild);
  5170. }
  5171. } else {
  5172. tableElm.appendChild(parentElm);
  5173. }
  5174. }
  5175. if (toType === 'tbody' && oldParentElm.nodeName === 'THEAD' && parentElm.firstChild) {
  5176. parentElm.insertBefore(rowElm, parentElm.firstChild);
  5177. } else {
  5178. parentElm.appendChild(rowElm);
  5179. }
  5180. if (!oldParentElm.hasChildNodes()) {
  5181. dom.remove(oldParentElm);
  5182. }
  5183. };
  5184. var updateAdvancedProps$1 = function (modifier, data) {
  5185. modifier.setStyle('background-color', data.backgroundcolor);
  5186. modifier.setStyle('border-color', data.bordercolor);
  5187. modifier.setStyle('border-style', data.borderstyle);
  5188. };
  5189. var onSubmitRowForm = function (editor, rows, oldData, api) {
  5190. var dom = editor.dom;
  5191. var data = api.getData();
  5192. api.close();
  5193. var createModifier = rows.length === 1 ? DomModifier.normal : DomModifier.ifTruthy;
  5194. editor.undoManager.transact(function () {
  5195. global$1.each(rows, function (rowElm) {
  5196. if (data.type !== rowElm.parentNode.nodeName.toLowerCase()) {
  5197. switchRowType(editor.dom, rowElm, data.type);
  5198. }
  5199. var modifier = createModifier(dom, rowElm);
  5200. modifier.setAttrib('scope', data.scope);
  5201. modifier.setAttrib('class', data.class);
  5202. modifier.setStyle('height', addSizeSuffix(data.height));
  5203. if (hasAdvancedRowTab(editor)) {
  5204. updateAdvancedProps$1(modifier, data);
  5205. }
  5206. if (data.align !== oldData.align) {
  5207. unApplyAlign(editor, rowElm);
  5208. applyAlign(editor, rowElm, data.align);
  5209. }
  5210. });
  5211. editor.focus();
  5212. });
  5213. };
  5214. var open$1 = function (editor) {
  5215. var dom = editor.dom;
  5216. var tableElm, cellElm, rowElm;
  5217. var rows = [];
  5218. tableElm = dom.getParent(editor.selection.getStart(), 'table');
  5219. if (!tableElm) {
  5220. return;
  5221. }
  5222. cellElm = dom.getParent(editor.selection.getStart(), 'td,th');
  5223. global$1.each(tableElm.rows, function (row) {
  5224. global$1.each(row.cells, function (cell) {
  5225. if ((dom.getAttrib(cell, 'data-mce-selected') || cell === cellElm) && rows.indexOf(row) < 0) {
  5226. rows.push(row);
  5227. return false;
  5228. }
  5229. });
  5230. });
  5231. rowElm = rows[0];
  5232. if (!rowElm) {
  5233. return;
  5234. }
  5235. var rowsData = global$1.map(rows, function (rowElm) {
  5236. return extractDataFromRowElement(editor, rowElm, hasAdvancedRowTab(editor));
  5237. });
  5238. var data = getSharedValues(rowsData);
  5239. var dialogTabPanel = {
  5240. type: 'tabpanel',
  5241. tabs: [
  5242. {
  5243. title: 'General',
  5244. name: 'general',
  5245. items: getItems$1(editor)
  5246. },
  5247. getAdvancedTab('row')
  5248. ]
  5249. };
  5250. var dialogPanel = {
  5251. type: 'panel',
  5252. items: [{
  5253. type: 'grid',
  5254. columns: 2,
  5255. items: getItems$1(editor)
  5256. }]
  5257. };
  5258. editor.windowManager.open({
  5259. title: 'Row Properties',
  5260. size: 'normal',
  5261. body: hasAdvancedRowTab(editor) ? dialogTabPanel : dialogPanel,
  5262. buttons: [
  5263. {
  5264. type: 'cancel',
  5265. name: 'cancel',
  5266. text: 'Cancel'
  5267. },
  5268. {
  5269. type: 'submit',
  5270. name: 'save',
  5271. text: 'Save',
  5272. primary: true
  5273. }
  5274. ],
  5275. initialData: data,
  5276. onSubmit: curry(onSubmitRowForm, editor, rows, data)
  5277. });
  5278. };
  5279. var global$2 = tinymce.util.Tools.resolve('tinymce.Env');
  5280. var DefaultRenderOptions = {
  5281. styles: {
  5282. 'border-collapse': 'collapse',
  5283. 'width': '100%'
  5284. },
  5285. attributes: { border: '1' },
  5286. percentages: true
  5287. };
  5288. var makeTable = function () {
  5289. return Element.fromTag('table');
  5290. };
  5291. var tableBody = function () {
  5292. return Element.fromTag('tbody');
  5293. };
  5294. var tableRow = function () {
  5295. return Element.fromTag('tr');
  5296. };
  5297. var tableHeaderCell = function () {
  5298. return Element.fromTag('th');
  5299. };
  5300. var tableCell = function () {
  5301. return Element.fromTag('td');
  5302. };
  5303. var render$1 = function (rows, columns, rowHeaders, columnHeaders, renderOpts) {
  5304. if (renderOpts === void 0) {
  5305. renderOpts = DefaultRenderOptions;
  5306. }
  5307. var table = makeTable();
  5308. setAll$1(table, renderOpts.styles);
  5309. setAll(table, renderOpts.attributes);
  5310. var tbody = tableBody();
  5311. append(table, tbody);
  5312. var trs = [];
  5313. for (var i = 0; i < rows; i++) {
  5314. var tr = tableRow();
  5315. for (var j = 0; j < columns; j++) {
  5316. var td = i < rowHeaders || j < columnHeaders ? tableHeaderCell() : tableCell();
  5317. if (j < columnHeaders) {
  5318. set(td, 'scope', 'row');
  5319. }
  5320. if (i < rowHeaders) {
  5321. set(td, 'scope', 'col');
  5322. }
  5323. append(td, Element.fromTag('br'));
  5324. if (renderOpts.percentages) {
  5325. set$1(td, 'width', 100 / columns + '%');
  5326. }
  5327. append(tr, td);
  5328. }
  5329. trs.push(tr);
  5330. }
  5331. append$1(tbody, trs);
  5332. return table;
  5333. };
  5334. var get$8 = function (element) {
  5335. return element.dom().innerHTML;
  5336. };
  5337. var getOuter$2 = function (element) {
  5338. var container = Element.fromTag('div');
  5339. var clone = Element.fromDom(element.dom().cloneNode(true));
  5340. append(container, clone);
  5341. return get$8(container);
  5342. };
  5343. var placeCaretInCell = function (editor, cell) {
  5344. editor.selection.select(cell.dom(), true);
  5345. editor.selection.collapse(true);
  5346. };
  5347. var selectFirstCellInTable = function (editor, tableElm) {
  5348. descendant$1(tableElm, 'td,th').each(curry(placeCaretInCell, editor));
  5349. };
  5350. var fireEvents = function (editor, table) {
  5351. each(descendants$1(table, 'tr'), function (row) {
  5352. fireNewRow(editor, row.dom());
  5353. each(descendants$1(row, 'th,td'), function (cell) {
  5354. fireNewCell(editor, cell.dom());
  5355. });
  5356. });
  5357. };
  5358. var isPercentage = function (width) {
  5359. return isString(width) && width.indexOf('%') !== -1;
  5360. };
  5361. var insert$1 = function (editor, columns, rows) {
  5362. var defaultStyles = getDefaultStyles(editor);
  5363. var options = {
  5364. styles: defaultStyles,
  5365. attributes: getDefaultAttributes(editor),
  5366. percentages: isPercentage(defaultStyles.width) && !isPixelsForced(editor)
  5367. };
  5368. var table = render$1(rows, columns, 0, 0, options);
  5369. set(table, 'data-mce-id', '__mce');
  5370. var html = getOuter$2(table);
  5371. editor.insertContent(html);
  5372. return descendant$1(getBody$1(editor), 'table[data-mce-id="__mce"]').map(function (table) {
  5373. if (isPixelsForced(editor)) {
  5374. set$1(table, 'width', get$2(table, 'width'));
  5375. }
  5376. remove(table, 'data-mce-id');
  5377. fireEvents(editor, table);
  5378. selectFirstCellInTable(editor, table);
  5379. return table.dom();
  5380. }).getOr(null);
  5381. };
  5382. var getItems$2 = function (editor, hasClasses, insertNewTable) {
  5383. var rowColCountItems = !insertNewTable ? [] : [
  5384. {
  5385. type: 'input',
  5386. name: 'cols',
  5387. label: 'Cols',
  5388. inputMode: 'numeric'
  5389. },
  5390. {
  5391. type: 'input',
  5392. name: 'rows',
  5393. label: 'Rows',
  5394. inputMode: 'numeric'
  5395. }
  5396. ];
  5397. var alwaysItems = [
  5398. {
  5399. type: 'input',
  5400. name: 'width',
  5401. label: 'Width'
  5402. },
  5403. {
  5404. type: 'input',
  5405. name: 'height',
  5406. label: 'Height'
  5407. }
  5408. ];
  5409. var appearanceItems = hasAppearanceOptions(editor) ? [
  5410. {
  5411. type: 'input',
  5412. name: 'cellspacing',
  5413. label: 'Cell spacing',
  5414. inputMode: 'numeric'
  5415. },
  5416. {
  5417. type: 'input',
  5418. name: 'cellpadding',
  5419. label: 'Cell padding',
  5420. inputMode: 'numeric'
  5421. },
  5422. {
  5423. type: 'input',
  5424. name: 'border',
  5425. label: 'Border width'
  5426. },
  5427. {
  5428. type: 'label',
  5429. label: 'Caption',
  5430. items: [{
  5431. type: 'checkbox',
  5432. name: 'caption',
  5433. label: 'Show caption'
  5434. }]
  5435. }
  5436. ] : [];
  5437. var alignmentItem = [{
  5438. type: 'selectbox',
  5439. name: 'align',
  5440. label: 'Alignment',
  5441. items: [
  5442. {
  5443. text: 'None',
  5444. value: ''
  5445. },
  5446. {
  5447. text: 'Left',
  5448. value: 'left'
  5449. },
  5450. {
  5451. text: 'Center',
  5452. value: 'center'
  5453. },
  5454. {
  5455. text: 'Right',
  5456. value: 'right'
  5457. }
  5458. ]
  5459. }];
  5460. var classListItem = hasClasses ? [{
  5461. type: 'selectbox',
  5462. name: 'class',
  5463. label: 'Class',
  5464. items: buildListItems(getTableClassList(editor), function (item) {
  5465. if (item.value) {
  5466. item.textStyle = function () {
  5467. return editor.formatter.getCssText({
  5468. block: 'table',
  5469. classes: [item.value]
  5470. });
  5471. };
  5472. }
  5473. })
  5474. }] : [];
  5475. return rowColCountItems.concat(alwaysItems).concat(appearanceItems).concat(alignmentItem).concat(classListItem);
  5476. };
  5477. var styleTDTH = function (dom, elm, name, value) {
  5478. if (elm.tagName === 'TD' || elm.tagName === 'TH') {
  5479. if (isString(name)) {
  5480. dom.setStyle(elm, name, value);
  5481. } else {
  5482. dom.setStyle(elm, name);
  5483. }
  5484. } else {
  5485. if (elm.children) {
  5486. for (var i = 0; i < elm.children.length; i++) {
  5487. styleTDTH(dom, elm.children[i], name, value);
  5488. }
  5489. }
  5490. }
  5491. };
  5492. var applyDataToElement = function (editor, tableElm, data) {
  5493. var dom = editor.dom;
  5494. var attrs = {};
  5495. var styles = {};
  5496. attrs.class = data.class;
  5497. styles.height = addSizeSuffix(data.height);
  5498. if (dom.getAttrib(tableElm, 'width') && !shouldStyleWithCss(editor)) {
  5499. attrs.width = removePxSuffix(data.width);
  5500. } else {
  5501. styles.width = addSizeSuffix(data.width);
  5502. }
  5503. if (shouldStyleWithCss(editor)) {
  5504. styles['border-width'] = addSizeSuffix(data.border);
  5505. styles['border-spacing'] = addSizeSuffix(data.cellspacing);
  5506. } else {
  5507. attrs.border = data.border;
  5508. attrs.cellpadding = data.cellpadding;
  5509. attrs.cellspacing = data.cellspacing;
  5510. }
  5511. if (shouldStyleWithCss(editor) && tableElm.children) {
  5512. for (var i = 0; i < tableElm.children.length; i++) {
  5513. styleTDTH(dom, tableElm.children[i], {
  5514. 'border-width': addSizeSuffix(data.border),
  5515. 'padding': addSizeSuffix(data.cellpadding)
  5516. });
  5517. if (hasAdvancedTableTab(editor)) {
  5518. styleTDTH(dom, tableElm.children[i], { 'border-color': data.bordercolor });
  5519. }
  5520. }
  5521. }
  5522. if (hasAdvancedTableTab(editor)) {
  5523. styles['background-color'] = data.backgroundcolor;
  5524. styles['border-color'] = data.bordercolor;
  5525. styles['border-style'] = data.borderstyle;
  5526. }
  5527. attrs.style = dom.serializeStyle(__assign(__assign({}, getDefaultStyles(editor)), styles));
  5528. dom.setAttribs(tableElm, __assign(__assign({}, getDefaultAttributes(editor)), attrs));
  5529. };
  5530. var onSubmitTableForm = function (editor, tableElm, api) {
  5531. var dom = editor.dom;
  5532. var captionElm;
  5533. var data = api.getData();
  5534. api.close();
  5535. if (data.class === '') {
  5536. delete data.class;
  5537. }
  5538. editor.undoManager.transact(function () {
  5539. if (!tableElm) {
  5540. var cols = parseInt(data.cols, 10) || 1;
  5541. var rows = parseInt(data.rows, 10) || 1;
  5542. tableElm = insert$1(editor, cols, rows);
  5543. }
  5544. applyDataToElement(editor, tableElm, data);
  5545. captionElm = dom.select('caption', tableElm)[0];
  5546. if (captionElm && !data.caption) {
  5547. dom.remove(captionElm);
  5548. }
  5549. if (!captionElm && data.caption) {
  5550. captionElm = dom.create('caption');
  5551. captionElm.innerHTML = !global$2.ie ? '<br data-mce-bogus="1"/>' : nbsp;
  5552. tableElm.insertBefore(captionElm, tableElm.firstChild);
  5553. }
  5554. if (data.align === '') {
  5555. unApplyAlign(editor, tableElm);
  5556. } else {
  5557. applyAlign(editor, tableElm, data.align);
  5558. }
  5559. editor.focus();
  5560. editor.addVisual();
  5561. });
  5562. };
  5563. var open$2 = function (editor, insertNewTable) {
  5564. var dom = editor.dom;
  5565. var tableElm;
  5566. var data = extractDataFromSettings(editor, hasAdvancedTableTab(editor));
  5567. if (insertNewTable === false) {
  5568. tableElm = dom.getParent(editor.selection.getStart(), 'table');
  5569. if (tableElm) {
  5570. data = extractDataFromTableElement(editor, tableElm, hasAdvancedTableTab(editor));
  5571. } else {
  5572. if (hasAdvancedTableTab(editor)) {
  5573. data.borderstyle = '';
  5574. data.bordercolor = '';
  5575. data.backgroundcolor = '';
  5576. }
  5577. }
  5578. } else {
  5579. data.cols = '1';
  5580. data.rows = '1';
  5581. if (hasAdvancedTableTab(editor)) {
  5582. data.borderstyle = '';
  5583. data.bordercolor = '';
  5584. data.backgroundcolor = '';
  5585. }
  5586. }
  5587. var hasClasses = getTableClassList(editor).length > 0;
  5588. if (hasClasses) {
  5589. if (data.class) {
  5590. data.class = data.class.replace(/\s*mce\-item\-table\s*/g, '');
  5591. }
  5592. }
  5593. var generalPanel = {
  5594. type: 'grid',
  5595. columns: 2,
  5596. items: getItems$2(editor, hasClasses, insertNewTable)
  5597. };
  5598. var nonAdvancedForm = function () {
  5599. return {
  5600. type: 'panel',
  5601. items: [generalPanel]
  5602. };
  5603. };
  5604. var advancedForm = function () {
  5605. return {
  5606. type: 'tabpanel',
  5607. tabs: [
  5608. {
  5609. title: 'General',
  5610. name: 'general',
  5611. items: [generalPanel]
  5612. },
  5613. getAdvancedTab('table')
  5614. ]
  5615. };
  5616. };
  5617. var dialogBody = hasAdvancedTableTab(editor) ? advancedForm() : nonAdvancedForm();
  5618. editor.windowManager.open({
  5619. title: 'Table Properties',
  5620. size: 'normal',
  5621. body: dialogBody,
  5622. onSubmit: curry(onSubmitTableForm, editor, tableElm),
  5623. buttons: [
  5624. {
  5625. type: 'cancel',
  5626. name: 'cancel',
  5627. text: 'Cancel'
  5628. },
  5629. {
  5630. type: 'submit',
  5631. name: 'save',
  5632. text: 'Save',
  5633. primary: true
  5634. }
  5635. ],
  5636. initialData: data
  5637. });
  5638. };
  5639. var each$3 = global$1.each;
  5640. var registerCommands = function (editor, actions, cellSelection, selections, clipboardRows) {
  5641. var isRoot = getIsRoot(editor);
  5642. var eraseTable = function () {
  5643. getSelectionStartCellOrCaption(editor).each(function (cellOrCaption) {
  5644. var tableOpt = table(cellOrCaption, isRoot);
  5645. tableOpt.filter(not(isRoot)).each(function (table) {
  5646. var cursor = Element.fromText('');
  5647. after(table, cursor);
  5648. remove$2(table);
  5649. if (editor.dom.isEmpty(editor.getBody())) {
  5650. editor.setContent('');
  5651. editor.selection.setCursorLocation();
  5652. } else {
  5653. var rng = editor.dom.createRng();
  5654. rng.setStart(cursor.dom(), 0);
  5655. rng.setEnd(cursor.dom(), 0);
  5656. editor.selection.setRng(rng);
  5657. editor.nodeChanged();
  5658. }
  5659. });
  5660. });
  5661. };
  5662. var getTableFromCell = function (cell) {
  5663. return table(cell, isRoot);
  5664. };
  5665. var actOnSelection = function (execute) {
  5666. getSelectionStartCell(editor).each(function (cell) {
  5667. getTableFromCell(cell).each(function (table) {
  5668. var targets = forMenu(selections, table, cell);
  5669. execute(table, targets).each(function (rng) {
  5670. editor.selection.setRng(rng);
  5671. editor.focus();
  5672. cellSelection.clear(table);
  5673. removeDataStyle(table);
  5674. });
  5675. });
  5676. });
  5677. };
  5678. var copyRowSelection = function (_execute) {
  5679. return getSelectionStartCell(editor).map(function (cell) {
  5680. return getTableFromCell(cell).bind(function (table) {
  5681. var doc = Element.fromDom(editor.getDoc());
  5682. var targets = forMenu(selections, table, cell);
  5683. var generators = cellOperations(noop, doc, Option.none());
  5684. return copyRows(table, targets, generators);
  5685. });
  5686. });
  5687. };
  5688. var pasteOnSelection = function (execute) {
  5689. clipboardRows.get().each(function (rows) {
  5690. var clonedRows = map(rows, function (row) {
  5691. return deep(row);
  5692. });
  5693. getSelectionStartCell(editor).each(function (cell) {
  5694. getTableFromCell(cell).each(function (table) {
  5695. var doc = Element.fromDom(editor.getDoc());
  5696. var generators = paste(doc);
  5697. var targets = pasteRows(selections, table, cell, clonedRows, generators);
  5698. execute(table, targets).each(function (rng) {
  5699. editor.selection.setRng(rng);
  5700. editor.focus();
  5701. cellSelection.clear(table);
  5702. });
  5703. });
  5704. });
  5705. });
  5706. };
  5707. each$3({
  5708. mceTableSplitCells: function () {
  5709. actOnSelection(actions.unmergeCells);
  5710. },
  5711. mceTableMergeCells: function () {
  5712. actOnSelection(actions.mergeCells);
  5713. },
  5714. mceTableInsertRowBefore: function () {
  5715. actOnSelection(actions.insertRowsBefore);
  5716. },
  5717. mceTableInsertRowAfter: function () {
  5718. actOnSelection(actions.insertRowsAfter);
  5719. },
  5720. mceTableInsertColBefore: function () {
  5721. actOnSelection(actions.insertColumnsBefore);
  5722. },
  5723. mceTableInsertColAfter: function () {
  5724. actOnSelection(actions.insertColumnsAfter);
  5725. },
  5726. mceTableDeleteCol: function () {
  5727. actOnSelection(actions.deleteColumn);
  5728. },
  5729. mceTableDeleteRow: function () {
  5730. actOnSelection(actions.deleteRow);
  5731. },
  5732. mceTableCutRow: function (_grid) {
  5733. copyRowSelection().each(function (selection) {
  5734. clipboardRows.set(selection);
  5735. actOnSelection(actions.deleteRow);
  5736. });
  5737. },
  5738. mceTableCopyRow: function (_grid) {
  5739. copyRowSelection().each(function (selection) {
  5740. clipboardRows.set(selection);
  5741. });
  5742. },
  5743. mceTablePasteRowBefore: function (_grid) {
  5744. pasteOnSelection(actions.pasteRowsBefore);
  5745. },
  5746. mceTablePasteRowAfter: function (_grid) {
  5747. pasteOnSelection(actions.pasteRowsAfter);
  5748. },
  5749. mceTableDelete: eraseTable
  5750. }, function (func, name) {
  5751. editor.addCommand(name, func);
  5752. });
  5753. each$3({
  5754. mceInsertTable: curry(open$2, editor, true),
  5755. mceTableProps: curry(open$2, editor, false),
  5756. mceTableRowProps: curry(open$1, editor),
  5757. mceTableCellProps: curry(open, editor)
  5758. }, function (func, name) {
  5759. editor.addCommand(name, function () {
  5760. func();
  5761. });
  5762. });
  5763. };
  5764. var only = function (element) {
  5765. var parent = Option.from(element.dom().documentElement).map(Element.fromDom).getOr(element);
  5766. return {
  5767. parent: constant(parent),
  5768. view: constant(element),
  5769. origin: constant(Position(0, 0))
  5770. };
  5771. };
  5772. var detached = function (editable, chrome) {
  5773. var origin = function () {
  5774. return absolute(chrome);
  5775. };
  5776. return {
  5777. parent: constant(chrome),
  5778. view: constant(editable),
  5779. origin: origin
  5780. };
  5781. };
  5782. var body$1 = function (editable, chrome) {
  5783. return {
  5784. parent: constant(chrome),
  5785. view: constant(editable),
  5786. origin: constant(Position(0, 0))
  5787. };
  5788. };
  5789. var ResizeWire = {
  5790. only: only,
  5791. detached: detached,
  5792. body: body$1
  5793. };
  5794. var Immutable = function () {
  5795. var fields = [];
  5796. for (var _i = 0; _i < arguments.length; _i++) {
  5797. fields[_i] = arguments[_i];
  5798. }
  5799. return function () {
  5800. var values = [];
  5801. for (var _i = 0; _i < arguments.length; _i++) {
  5802. values[_i] = arguments[_i];
  5803. }
  5804. if (fields.length !== values.length) {
  5805. throw new Error('Wrong number of arguments to struct. Expected "[' + fields.length + ']", got ' + values.length + ' arguments');
  5806. }
  5807. var struct = {};
  5808. each(fields, function (name, i) {
  5809. struct[name] = constant(values[i]);
  5810. });
  5811. return struct;
  5812. };
  5813. };
  5814. var Event = function (fields) {
  5815. var struct = Immutable.apply(null, fields);
  5816. var handlers = [];
  5817. var bind = function (handler) {
  5818. if (handler === undefined) {
  5819. throw new Error('Event bind error: undefined handler');
  5820. }
  5821. handlers.push(handler);
  5822. };
  5823. var unbind = function (handler) {
  5824. handlers = filter(handlers, function (h) {
  5825. return h !== handler;
  5826. });
  5827. };
  5828. var trigger = function () {
  5829. var args = [];
  5830. for (var _i = 0; _i < arguments.length; _i++) {
  5831. args[_i] = arguments[_i];
  5832. }
  5833. var event = struct.apply(null, args);
  5834. each(handlers, function (handler) {
  5835. handler(event);
  5836. });
  5837. };
  5838. return {
  5839. bind: bind,
  5840. unbind: unbind,
  5841. trigger: trigger
  5842. };
  5843. };
  5844. var create = function (typeDefs) {
  5845. var registry = map$1(typeDefs, function (event) {
  5846. return {
  5847. bind: event.bind,
  5848. unbind: event.unbind
  5849. };
  5850. });
  5851. var trigger = map$1(typeDefs, function (event) {
  5852. return event.trigger;
  5853. });
  5854. return {
  5855. registry: registry,
  5856. trigger: trigger
  5857. };
  5858. };
  5859. var mkEvent = function (target, x, y, stop, prevent, kill, raw) {
  5860. return {
  5861. target: constant(target),
  5862. x: constant(x),
  5863. y: constant(y),
  5864. stop: stop,
  5865. prevent: prevent,
  5866. kill: kill,
  5867. raw: constant(raw)
  5868. };
  5869. };
  5870. var fromRawEvent = function (rawEvent) {
  5871. var target = Element.fromDom(rawEvent.target);
  5872. var stop = function () {
  5873. return rawEvent.stopPropagation();
  5874. };
  5875. var prevent = function () {
  5876. return rawEvent.preventDefault();
  5877. };
  5878. var kill = compose(prevent, stop);
  5879. return mkEvent(target, rawEvent.clientX, rawEvent.clientY, stop, prevent, kill, rawEvent);
  5880. };
  5881. var handle = function (filter, handler) {
  5882. return function (rawEvent) {
  5883. if (filter(rawEvent)) {
  5884. handler(fromRawEvent(rawEvent));
  5885. }
  5886. };
  5887. };
  5888. var binder = function (element, event, filter, handler, useCapture) {
  5889. var wrapped = handle(filter, handler);
  5890. element.dom().addEventListener(event, wrapped, useCapture);
  5891. return { unbind: curry(unbind, element, event, wrapped, useCapture) };
  5892. };
  5893. var bind$1 = function (element, event, filter, handler) {
  5894. return binder(element, event, filter, handler, false);
  5895. };
  5896. var unbind = function (element, event, handler, useCapture) {
  5897. element.dom().removeEventListener(event, handler, useCapture);
  5898. };
  5899. var filter$1 = constant(true);
  5900. var bind$2 = function (element, event, handler) {
  5901. return bind$1(element, event, filter$1, handler);
  5902. };
  5903. var fromRawEvent$1 = fromRawEvent;
  5904. var styles$1 = css('ephox-dragster');
  5905. var resolve$1 = styles$1.resolve;
  5906. var Blocker = function (options) {
  5907. var settings = __assign({ layerClass: resolve$1('blocker') }, options);
  5908. var div = Element.fromTag('div');
  5909. set(div, 'role', 'presentation');
  5910. setAll$1(div, {
  5911. position: 'fixed',
  5912. left: '0px',
  5913. top: '0px',
  5914. width: '100%',
  5915. height: '100%'
  5916. });
  5917. add$2(div, resolve$1('blocker'));
  5918. add$2(div, settings.layerClass);
  5919. var element = function () {
  5920. return div;
  5921. };
  5922. var destroy = function () {
  5923. remove$2(div);
  5924. };
  5925. return {
  5926. element: element,
  5927. destroy: destroy
  5928. };
  5929. };
  5930. var DragMode = exactly([
  5931. 'compare',
  5932. 'extract',
  5933. 'mutate',
  5934. 'sink'
  5935. ]);
  5936. var DragSink = exactly([
  5937. 'element',
  5938. 'start',
  5939. 'stop',
  5940. 'destroy'
  5941. ]);
  5942. var DragApi = exactly([
  5943. 'forceDrop',
  5944. 'drop',
  5945. 'move',
  5946. 'delayDrop'
  5947. ]);
  5948. var compare = function (old, nu) {
  5949. return Position(nu.left() - old.left(), nu.top() - old.top());
  5950. };
  5951. var extract$1 = function (event) {
  5952. return Option.some(Position(event.x(), event.y()));
  5953. };
  5954. var mutate = function (mutation, info) {
  5955. mutation.mutate(info.left(), info.top());
  5956. };
  5957. var sink = function (dragApi, settings) {
  5958. var blocker = Blocker(settings);
  5959. var mdown = bind$2(blocker.element(), 'mousedown', dragApi.forceDrop);
  5960. var mup = bind$2(blocker.element(), 'mouseup', dragApi.drop);
  5961. var mmove = bind$2(blocker.element(), 'mousemove', dragApi.move);
  5962. var mout = bind$2(blocker.element(), 'mouseout', dragApi.delayDrop);
  5963. var destroy = function () {
  5964. blocker.destroy();
  5965. mup.unbind();
  5966. mmove.unbind();
  5967. mout.unbind();
  5968. mdown.unbind();
  5969. };
  5970. var start = function (parent) {
  5971. append(parent, blocker.element());
  5972. };
  5973. var stop = function () {
  5974. remove$2(blocker.element());
  5975. };
  5976. return DragSink({
  5977. element: blocker.element,
  5978. start: start,
  5979. stop: stop,
  5980. destroy: destroy
  5981. });
  5982. };
  5983. var MouseDrag = DragMode({
  5984. compare: compare,
  5985. extract: extract$1,
  5986. sink: sink,
  5987. mutate: mutate
  5988. });
  5989. var last$2 = function (fn, rate) {
  5990. var timer = null;
  5991. var cancel = function () {
  5992. if (timer !== null) {
  5993. domGlobals.clearTimeout(timer);
  5994. timer = null;
  5995. }
  5996. };
  5997. var throttle = function () {
  5998. var args = [];
  5999. for (var _i = 0; _i < arguments.length; _i++) {
  6000. args[_i] = arguments[_i];
  6001. }
  6002. if (timer !== null) {
  6003. domGlobals.clearTimeout(timer);
  6004. }
  6005. timer = domGlobals.setTimeout(function () {
  6006. fn.apply(null, args);
  6007. timer = null;
  6008. }, rate);
  6009. };
  6010. return {
  6011. cancel: cancel,
  6012. throttle: throttle
  6013. };
  6014. };
  6015. function InDrag () {
  6016. var previous = Option.none();
  6017. var reset = function () {
  6018. previous = Option.none();
  6019. };
  6020. var update = function (mode, nu) {
  6021. var result = previous.map(function (old) {
  6022. return mode.compare(old, nu);
  6023. });
  6024. previous = Option.some(nu);
  6025. return result;
  6026. };
  6027. var onEvent = function (event, mode) {
  6028. var dataOption = mode.extract(event);
  6029. dataOption.each(function (data) {
  6030. var offset = update(mode, data);
  6031. offset.each(function (d) {
  6032. events.trigger.move(d);
  6033. });
  6034. });
  6035. };
  6036. var events = create({ move: Event(['info']) });
  6037. return {
  6038. onEvent: onEvent,
  6039. reset: reset,
  6040. events: events.registry
  6041. };
  6042. }
  6043. function NoDrag () {
  6044. return {
  6045. onEvent: noop,
  6046. reset: noop
  6047. };
  6048. }
  6049. function Movement () {
  6050. var noDragState = NoDrag();
  6051. var inDragState = InDrag();
  6052. var dragState = noDragState;
  6053. var on = function () {
  6054. dragState.reset();
  6055. dragState = inDragState;
  6056. };
  6057. var off = function () {
  6058. dragState.reset();
  6059. dragState = noDragState;
  6060. };
  6061. var onEvent = function (event, mode) {
  6062. dragState.onEvent(event, mode);
  6063. };
  6064. var isOn = function () {
  6065. return dragState === inDragState;
  6066. };
  6067. return {
  6068. on: on,
  6069. off: off,
  6070. isOn: isOn,
  6071. onEvent: onEvent,
  6072. events: inDragState.events
  6073. };
  6074. }
  6075. var setup = function (mutation, mode, settings) {
  6076. var active = false;
  6077. var events = create({
  6078. start: Event([]),
  6079. stop: Event([])
  6080. });
  6081. var movement = Movement();
  6082. var drop = function () {
  6083. sink.stop();
  6084. if (movement.isOn()) {
  6085. movement.off();
  6086. events.trigger.stop();
  6087. }
  6088. };
  6089. var throttledDrop = last$2(drop, 200);
  6090. var go = function (parent) {
  6091. sink.start(parent);
  6092. movement.on();
  6093. events.trigger.start();
  6094. };
  6095. var mousemove = function (event) {
  6096. throttledDrop.cancel();
  6097. movement.onEvent(event, mode);
  6098. };
  6099. movement.events.move.bind(function (event) {
  6100. mode.mutate(mutation, event.info());
  6101. });
  6102. var on = function () {
  6103. active = true;
  6104. };
  6105. var off = function () {
  6106. active = false;
  6107. };
  6108. var runIfActive = function (f) {
  6109. return function () {
  6110. var args = [];
  6111. for (var _i = 0; _i < arguments.length; _i++) {
  6112. args[_i] = arguments[_i];
  6113. }
  6114. if (active) {
  6115. f.apply(null, args);
  6116. }
  6117. };
  6118. };
  6119. var sink = mode.sink(DragApi({
  6120. forceDrop: drop,
  6121. drop: runIfActive(drop),
  6122. move: runIfActive(mousemove),
  6123. delayDrop: runIfActive(throttledDrop.throttle)
  6124. }), settings);
  6125. var destroy = function () {
  6126. sink.destroy();
  6127. };
  6128. return {
  6129. element: sink.element,
  6130. go: go,
  6131. on: on,
  6132. off: off,
  6133. destroy: destroy,
  6134. events: events.registry
  6135. };
  6136. };
  6137. var transform$1 = function (mutation, settings) {
  6138. if (settings === void 0) {
  6139. settings = {};
  6140. }
  6141. var mode = settings.mode !== undefined ? settings.mode : MouseDrag;
  6142. return setup(mutation, mode, settings);
  6143. };
  6144. var isContentEditableTrue = function (elm) {
  6145. return get$1(elm, 'contenteditable') === 'true';
  6146. };
  6147. var findClosestContentEditable = function (target, isRoot) {
  6148. return closest$1(target, '[contenteditable]', isRoot);
  6149. };
  6150. var Mutation = function () {
  6151. var events = create({
  6152. drag: Event([
  6153. 'xDelta',
  6154. 'yDelta'
  6155. ])
  6156. });
  6157. var mutate = function (x, y) {
  6158. events.trigger.drag(x, y);
  6159. };
  6160. return {
  6161. mutate: mutate,
  6162. events: events.registry
  6163. };
  6164. };
  6165. var BarMutation = function () {
  6166. var events = create({
  6167. drag: Event([
  6168. 'xDelta',
  6169. 'yDelta',
  6170. 'target'
  6171. ])
  6172. });
  6173. var target = Option.none();
  6174. var delegate = Mutation();
  6175. delegate.events.drag.bind(function (event) {
  6176. target.each(function (t) {
  6177. events.trigger.drag(event.xDelta(), event.yDelta(), t);
  6178. });
  6179. });
  6180. var assign = function (t) {
  6181. target = Option.some(t);
  6182. };
  6183. var get = function () {
  6184. return target;
  6185. };
  6186. return {
  6187. assign: assign,
  6188. get: get,
  6189. mutate: delegate.mutate,
  6190. events: events.registry
  6191. };
  6192. };
  6193. var resizeBarDragging = resolve('resizer-bar-dragging');
  6194. var BarManager = function (wire, direction, hdirection) {
  6195. var mutation = BarMutation();
  6196. var resizing = transform$1(mutation, {});
  6197. var hoverTable = Option.none();
  6198. var getResizer = function (element, type) {
  6199. return Option.from(get$1(element, type));
  6200. };
  6201. mutation.events.drag.bind(function (event) {
  6202. getResizer(event.target(), 'data-row').each(function (_dataRow) {
  6203. var currentRow = getCssValue(event.target(), 'top');
  6204. set$1(event.target(), 'top', currentRow + event.yDelta() + 'px');
  6205. });
  6206. getResizer(event.target(), 'data-column').each(function (_dataCol) {
  6207. var currentCol = getCssValue(event.target(), 'left');
  6208. set$1(event.target(), 'left', currentCol + event.xDelta() + 'px');
  6209. });
  6210. });
  6211. var getDelta = function (target, dir) {
  6212. var newX = getCssValue(target, dir);
  6213. var oldX = getAttrValue(target, 'data-initial-' + dir, 0);
  6214. return newX - oldX;
  6215. };
  6216. resizing.events.stop.bind(function () {
  6217. mutation.get().each(function (target) {
  6218. hoverTable.each(function (table) {
  6219. getResizer(target, 'data-row').each(function (row) {
  6220. var delta = getDelta(target, 'top');
  6221. remove(target, 'data-initial-top');
  6222. events.trigger.adjustHeight(table, delta, parseInt(row, 10));
  6223. });
  6224. getResizer(target, 'data-column').each(function (column) {
  6225. var delta = getDelta(target, 'left');
  6226. remove(target, 'data-initial-left');
  6227. events.trigger.adjustWidth(table, delta, parseInt(column, 10));
  6228. });
  6229. refresh(wire, table, hdirection, direction);
  6230. });
  6231. });
  6232. });
  6233. var handler = function (target, dir) {
  6234. events.trigger.startAdjust();
  6235. mutation.assign(target);
  6236. set(target, 'data-initial-' + dir, getCssValue(target, dir));
  6237. add$2(target, resizeBarDragging);
  6238. set$1(target, 'opacity', '0.2');
  6239. resizing.go(wire.parent());
  6240. };
  6241. var mousedown = bind$2(wire.parent(), 'mousedown', function (event) {
  6242. if (isRowBar(event.target())) {
  6243. handler(event.target(), 'top');
  6244. }
  6245. if (isColBar(event.target())) {
  6246. handler(event.target(), 'left');
  6247. }
  6248. });
  6249. var isRoot = function (e) {
  6250. return eq(e, wire.view());
  6251. };
  6252. var findClosestEditableTable = function (target) {
  6253. return closest$1(target, 'table', isRoot).filter(function (table) {
  6254. return findClosestContentEditable(table, isRoot).exists(isContentEditableTrue);
  6255. });
  6256. };
  6257. var mouseover = bind$2(wire.view(), 'mouseover', function (event) {
  6258. findClosestEditableTable(event.target()).fold(function () {
  6259. if (inBody(event.target())) {
  6260. destroy(wire);
  6261. }
  6262. }, function (table) {
  6263. hoverTable = Option.some(table);
  6264. refresh(wire, table, hdirection, direction);
  6265. });
  6266. });
  6267. var destroy$1 = function () {
  6268. mousedown.unbind();
  6269. mouseover.unbind();
  6270. resizing.destroy();
  6271. destroy(wire);
  6272. };
  6273. var refresh$1 = function (tbl) {
  6274. refresh(wire, tbl, hdirection, direction);
  6275. };
  6276. var events = create({
  6277. adjustHeight: Event([
  6278. 'table',
  6279. 'delta',
  6280. 'row'
  6281. ]),
  6282. adjustWidth: Event([
  6283. 'table',
  6284. 'delta',
  6285. 'column'
  6286. ]),
  6287. startAdjust: Event([])
  6288. });
  6289. return {
  6290. destroy: destroy$1,
  6291. refresh: refresh$1,
  6292. on: resizing.on,
  6293. off: resizing.off,
  6294. hideBars: curry(hide, wire),
  6295. showBars: curry(show, wire),
  6296. events: events.registry
  6297. };
  6298. };
  6299. var create$1 = function (wire, vdirection) {
  6300. var hdirection = height;
  6301. var manager = BarManager(wire, vdirection, hdirection);
  6302. var events = create({
  6303. beforeResize: Event(['table']),
  6304. afterResize: Event(['table']),
  6305. startDrag: Event([])
  6306. });
  6307. manager.events.adjustHeight.bind(function (event) {
  6308. events.trigger.beforeResize(event.table());
  6309. var delta = hdirection.delta(event.delta(), event.table());
  6310. adjustHeight(event.table(), delta, event.row(), hdirection);
  6311. events.trigger.afterResize(event.table());
  6312. });
  6313. manager.events.startAdjust.bind(function (_event) {
  6314. events.trigger.startDrag();
  6315. });
  6316. manager.events.adjustWidth.bind(function (event) {
  6317. events.trigger.beforeResize(event.table());
  6318. var delta = vdirection.delta(event.delta(), event.table());
  6319. adjustWidth(event.table(), delta, event.column(), vdirection);
  6320. events.trigger.afterResize(event.table());
  6321. });
  6322. return {
  6323. on: manager.on,
  6324. off: manager.off,
  6325. hideBars: manager.hideBars,
  6326. showBars: manager.showBars,
  6327. destroy: manager.destroy,
  6328. events: events.registry
  6329. };
  6330. };
  6331. var TableResize = { create: create$1 };
  6332. var createContainer = function () {
  6333. var container = Element.fromTag('div');
  6334. setAll$1(container, {
  6335. position: 'static',
  6336. height: '0',
  6337. width: '0',
  6338. padding: '0',
  6339. margin: '0',
  6340. border: '0'
  6341. });
  6342. append(body(), container);
  6343. return container;
  6344. };
  6345. var get$9 = function (editor, _container) {
  6346. return editor.inline ? ResizeWire.body(getBody$1(editor), createContainer()) : ResizeWire.only(Element.fromDom(editor.getDoc()));
  6347. };
  6348. var remove$6 = function (editor, wire) {
  6349. if (editor.inline) {
  6350. remove$2(wire.parent());
  6351. }
  6352. };
  6353. var calculatePercentageWidth = function (element, parent) {
  6354. return getPixelWidth$1(element.dom()) / getPixelWidth$1(parent.dom()) * 100 + '%';
  6355. };
  6356. var enforcePercentage = function (rawTable) {
  6357. var table = Element.fromDom(rawTable);
  6358. parent(table).map(function (parent) {
  6359. return calculatePercentageWidth(table, parent);
  6360. }).each(function (tablePercentage) {
  6361. set$1(table, 'width', tablePercentage);
  6362. each(descendants$1(table, 'tr'), function (tr) {
  6363. each(children(tr), function (td) {
  6364. set$1(td, 'width', calculatePercentageWidth(td, tr));
  6365. });
  6366. });
  6367. });
  6368. };
  6369. var enforcePixels = function (table) {
  6370. set$1(Element.fromDom(table), 'width', getPixelWidth$1(table).toString() + 'px');
  6371. };
  6372. var getResizeHandler = function (editor) {
  6373. var selectionRng = Option.none();
  6374. var resize = Option.none();
  6375. var wire = Option.none();
  6376. var percentageBasedSizeRegex = /(\d+(\.\d+)?)%/;
  6377. var startW;
  6378. var startRawW;
  6379. var isTable = function (elm) {
  6380. return elm.nodeName === 'TABLE';
  6381. };
  6382. var getRawWidth = function (elm) {
  6383. var raw = editor.dom.getStyle(elm, 'width') || editor.dom.getAttrib(elm, 'width');
  6384. return Option.from(raw).filter(function (s) {
  6385. return s.length > 0;
  6386. });
  6387. };
  6388. var lazyResize = function () {
  6389. return resize;
  6390. };
  6391. var lazyWire = function () {
  6392. return wire.getOr(ResizeWire.only(Element.fromDom(editor.getBody())));
  6393. };
  6394. var destroy = function () {
  6395. resize.each(function (sz) {
  6396. sz.destroy();
  6397. });
  6398. wire.each(function (w) {
  6399. remove$6(editor, w);
  6400. });
  6401. };
  6402. editor.on('init', function () {
  6403. var direction = TableDirection(directionAt);
  6404. var rawWire = get$9(editor);
  6405. wire = Option.some(rawWire);
  6406. if (hasObjectResizing(editor) && hasTableResizeBars(editor)) {
  6407. var sz = TableResize.create(rawWire, direction);
  6408. sz.on();
  6409. sz.events.startDrag.bind(function (_event) {
  6410. selectionRng = Option.some(editor.selection.getRng());
  6411. });
  6412. sz.events.beforeResize.bind(function (event) {
  6413. var rawTable = event.table().dom();
  6414. fireObjectResizeStart(editor, rawTable, getPixelWidth$1(rawTable), getPixelHeight(rawTable));
  6415. });
  6416. sz.events.afterResize.bind(function (event) {
  6417. var table = event.table();
  6418. var rawTable = table.dom();
  6419. removeDataStyle(table);
  6420. selectionRng.each(function (rng) {
  6421. editor.selection.setRng(rng);
  6422. editor.focus();
  6423. });
  6424. fireObjectResized(editor, rawTable, getPixelWidth$1(rawTable), getPixelHeight(rawTable));
  6425. editor.undoManager.add();
  6426. });
  6427. resize = Option.some(sz);
  6428. }
  6429. });
  6430. editor.on('ObjectResizeStart', function (e) {
  6431. var targetElm = e.target;
  6432. if (isTable(targetElm)) {
  6433. var tableHasPercentage = getRawWidth(targetElm).map(function (w) {
  6434. return percentageBasedSizeRegex.test(w);
  6435. }).getOr(false);
  6436. if (tableHasPercentage && isPixelsForced(editor)) {
  6437. enforcePixels(targetElm);
  6438. } else if (!tableHasPercentage && isPercentagesForced(editor)) {
  6439. enforcePercentage(targetElm);
  6440. }
  6441. startW = e.width;
  6442. startRawW = getRawWidth(targetElm).getOr('');
  6443. }
  6444. });
  6445. editor.on('ObjectResized', function (e) {
  6446. var targetElm = e.target;
  6447. if (isTable(targetElm)) {
  6448. var table = targetElm;
  6449. if (percentageBasedSizeRegex.test(startRawW)) {
  6450. var percentW = parseFloat(percentageBasedSizeRegex.exec(startRawW)[1]);
  6451. var targetPercentW = e.width * percentW / startW;
  6452. editor.dom.setStyle(table, 'width', targetPercentW + '%');
  6453. } else {
  6454. var newCellSizes_1 = [];
  6455. global$1.each(table.rows, function (row) {
  6456. global$1.each(row.cells, function (cell) {
  6457. var width = editor.dom.getStyle(cell, 'width', true);
  6458. newCellSizes_1.push({
  6459. cell: cell,
  6460. width: width
  6461. });
  6462. });
  6463. });
  6464. global$1.each(newCellSizes_1, function (newCellSize) {
  6465. editor.dom.setStyle(newCellSize.cell, 'width', newCellSize.width);
  6466. editor.dom.setAttrib(newCellSize.cell, 'width', null);
  6467. });
  6468. }
  6469. }
  6470. });
  6471. editor.on('SwitchMode', function () {
  6472. lazyResize().each(function (resize) {
  6473. if (editor.mode.isReadOnly()) {
  6474. resize.hideBars();
  6475. } else {
  6476. resize.showBars();
  6477. }
  6478. });
  6479. });
  6480. return {
  6481. lazyResize: lazyResize,
  6482. lazyWire: lazyWire,
  6483. destroy: destroy
  6484. };
  6485. };
  6486. var adt$1 = Adt.generate([
  6487. { none: ['current'] },
  6488. { first: ['current'] },
  6489. {
  6490. middle: [
  6491. 'current',
  6492. 'target'
  6493. ]
  6494. },
  6495. { last: ['current'] }
  6496. ]);
  6497. var none$2 = function (current) {
  6498. if (current === void 0) {
  6499. current = undefined;
  6500. }
  6501. return adt$1.none(current);
  6502. };
  6503. var CellLocation = __assign(__assign({}, adt$1), { none: none$2 });
  6504. var detect$4 = function (current, isRoot) {
  6505. return table(current, isRoot).bind(function (table) {
  6506. var all = cells(table);
  6507. var index = findIndex(all, function (x) {
  6508. return eq(current, x);
  6509. });
  6510. return index.map(function (index) {
  6511. return {
  6512. index: index,
  6513. all: all
  6514. };
  6515. });
  6516. });
  6517. };
  6518. var next = function (current, isRoot) {
  6519. var detection = detect$4(current, isRoot);
  6520. return detection.fold(function () {
  6521. return CellLocation.none(current);
  6522. }, function (info) {
  6523. return info.index + 1 < info.all.length ? CellLocation.middle(current, info.all[info.index + 1]) : CellLocation.last(current);
  6524. });
  6525. };
  6526. var prev = function (current, isRoot) {
  6527. var detection = detect$4(current, isRoot);
  6528. return detection.fold(function () {
  6529. return CellLocation.none();
  6530. }, function (info) {
  6531. return info.index - 1 >= 0 ? CellLocation.middle(current, info.all[info.index - 1]) : CellLocation.first(current);
  6532. });
  6533. };
  6534. var create$2 = function (start, soffset, finish, foffset) {
  6535. return {
  6536. start: constant(start),
  6537. soffset: constant(soffset),
  6538. finish: constant(finish),
  6539. foffset: constant(foffset)
  6540. };
  6541. };
  6542. var SimRange = { create: create$2 };
  6543. var adt$2 = Adt.generate([
  6544. { before: ['element'] },
  6545. {
  6546. on: [
  6547. 'element',
  6548. 'offset'
  6549. ]
  6550. },
  6551. { after: ['element'] }
  6552. ]);
  6553. var cata$1 = function (subject, onBefore, onOn, onAfter) {
  6554. return subject.fold(onBefore, onOn, onAfter);
  6555. };
  6556. var getStart = function (situ) {
  6557. return situ.fold(identity, identity, identity);
  6558. };
  6559. var before$2 = adt$2.before;
  6560. var on = adt$2.on;
  6561. var after$2 = adt$2.after;
  6562. var Situ = {
  6563. before: before$2,
  6564. on: on,
  6565. after: after$2,
  6566. cata: cata$1,
  6567. getStart: getStart
  6568. };
  6569. var adt$3 = Adt.generate([
  6570. { domRange: ['rng'] },
  6571. {
  6572. relative: [
  6573. 'startSitu',
  6574. 'finishSitu'
  6575. ]
  6576. },
  6577. {
  6578. exact: [
  6579. 'start',
  6580. 'soffset',
  6581. 'finish',
  6582. 'foffset'
  6583. ]
  6584. }
  6585. ]);
  6586. var exactFromRange = function (simRange) {
  6587. return adt$3.exact(simRange.start(), simRange.soffset(), simRange.finish(), simRange.foffset());
  6588. };
  6589. var getStart$1 = function (selection) {
  6590. return selection.match({
  6591. domRange: function (rng) {
  6592. return Element.fromDom(rng.startContainer);
  6593. },
  6594. relative: function (startSitu, _finishSitu) {
  6595. return Situ.getStart(startSitu);
  6596. },
  6597. exact: function (start, _soffset, _finish, _foffset) {
  6598. return start;
  6599. }
  6600. });
  6601. };
  6602. var domRange = adt$3.domRange;
  6603. var relative = adt$3.relative;
  6604. var exact = adt$3.exact;
  6605. var getWin = function (selection) {
  6606. var start = getStart$1(selection);
  6607. return defaultView(start);
  6608. };
  6609. var range$1 = SimRange.create;
  6610. var Selection = {
  6611. domRange: domRange,
  6612. relative: relative,
  6613. exact: exact,
  6614. exactFromRange: exactFromRange,
  6615. getWin: getWin,
  6616. range: range$1
  6617. };
  6618. var selectNodeContents = function (win, element) {
  6619. var rng = win.document.createRange();
  6620. selectNodeContentsUsing(rng, element);
  6621. return rng;
  6622. };
  6623. var selectNodeContentsUsing = function (rng, element) {
  6624. return rng.selectNodeContents(element.dom());
  6625. };
  6626. var setStart = function (rng, situ) {
  6627. situ.fold(function (e) {
  6628. rng.setStartBefore(e.dom());
  6629. }, function (e, o) {
  6630. rng.setStart(e.dom(), o);
  6631. }, function (e) {
  6632. rng.setStartAfter(e.dom());
  6633. });
  6634. };
  6635. var setFinish = function (rng, situ) {
  6636. situ.fold(function (e) {
  6637. rng.setEndBefore(e.dom());
  6638. }, function (e, o) {
  6639. rng.setEnd(e.dom(), o);
  6640. }, function (e) {
  6641. rng.setEndAfter(e.dom());
  6642. });
  6643. };
  6644. var relativeToNative = function (win, startSitu, finishSitu) {
  6645. var range = win.document.createRange();
  6646. setStart(range, startSitu);
  6647. setFinish(range, finishSitu);
  6648. return range;
  6649. };
  6650. var exactToNative = function (win, start, soffset, finish, foffset) {
  6651. var rng = win.document.createRange();
  6652. rng.setStart(start.dom(), soffset);
  6653. rng.setEnd(finish.dom(), foffset);
  6654. return rng;
  6655. };
  6656. var toRect = function (rect) {
  6657. return {
  6658. left: constant(rect.left),
  6659. top: constant(rect.top),
  6660. right: constant(rect.right),
  6661. bottom: constant(rect.bottom),
  6662. width: constant(rect.width),
  6663. height: constant(rect.height)
  6664. };
  6665. };
  6666. var getFirstRect = function (rng) {
  6667. var rects = rng.getClientRects();
  6668. var rect = rects.length > 0 ? rects[0] : rng.getBoundingClientRect();
  6669. return rect.width > 0 || rect.height > 0 ? Option.some(rect).map(toRect) : Option.none();
  6670. };
  6671. var adt$4 = Adt.generate([
  6672. {
  6673. ltr: [
  6674. 'start',
  6675. 'soffset',
  6676. 'finish',
  6677. 'foffset'
  6678. ]
  6679. },
  6680. {
  6681. rtl: [
  6682. 'start',
  6683. 'soffset',
  6684. 'finish',
  6685. 'foffset'
  6686. ]
  6687. }
  6688. ]);
  6689. var fromRange = function (win, type, range) {
  6690. return type(Element.fromDom(range.startContainer), range.startOffset, Element.fromDom(range.endContainer), range.endOffset);
  6691. };
  6692. var getRanges = function (win, selection) {
  6693. return selection.match({
  6694. domRange: function (rng) {
  6695. return {
  6696. ltr: constant(rng),
  6697. rtl: Option.none
  6698. };
  6699. },
  6700. relative: function (startSitu, finishSitu) {
  6701. return {
  6702. ltr: cached(function () {
  6703. return relativeToNative(win, startSitu, finishSitu);
  6704. }),
  6705. rtl: cached(function () {
  6706. return Option.some(relativeToNative(win, finishSitu, startSitu));
  6707. })
  6708. };
  6709. },
  6710. exact: function (start, soffset, finish, foffset) {
  6711. return {
  6712. ltr: cached(function () {
  6713. return exactToNative(win, start, soffset, finish, foffset);
  6714. }),
  6715. rtl: cached(function () {
  6716. return Option.some(exactToNative(win, finish, foffset, start, soffset));
  6717. })
  6718. };
  6719. }
  6720. });
  6721. };
  6722. var doDiagnose = function (win, ranges) {
  6723. var rng = ranges.ltr();
  6724. if (rng.collapsed) {
  6725. var reversed = ranges.rtl().filter(function (rev) {
  6726. return rev.collapsed === false;
  6727. });
  6728. return reversed.map(function (rev) {
  6729. return adt$4.rtl(Element.fromDom(rev.endContainer), rev.endOffset, Element.fromDom(rev.startContainer), rev.startOffset);
  6730. }).getOrThunk(function () {
  6731. return fromRange(win, adt$4.ltr, rng);
  6732. });
  6733. } else {
  6734. return fromRange(win, adt$4.ltr, rng);
  6735. }
  6736. };
  6737. var diagnose = function (win, selection) {
  6738. var ranges = getRanges(win, selection);
  6739. return doDiagnose(win, ranges);
  6740. };
  6741. var asLtrRange = function (win, selection) {
  6742. var diagnosis = diagnose(win, selection);
  6743. return diagnosis.match({
  6744. ltr: function (start, soffset, finish, foffset) {
  6745. var rng = win.document.createRange();
  6746. rng.setStart(start.dom(), soffset);
  6747. rng.setEnd(finish.dom(), foffset);
  6748. return rng;
  6749. },
  6750. rtl: function (start, soffset, finish, foffset) {
  6751. var rng = win.document.createRange();
  6752. rng.setStart(finish.dom(), foffset);
  6753. rng.setEnd(start.dom(), soffset);
  6754. return rng;
  6755. }
  6756. });
  6757. };
  6758. var ltr$2 = adt$4.ltr;
  6759. var rtl$2 = adt$4.rtl;
  6760. var searchForPoint = function (rectForOffset, x, y, maxX, length) {
  6761. if (length === 0) {
  6762. return 0;
  6763. } else if (x === maxX) {
  6764. return length - 1;
  6765. }
  6766. var xDelta = maxX;
  6767. for (var i = 1; i < length; i++) {
  6768. var rect = rectForOffset(i);
  6769. var curDeltaX = Math.abs(x - rect.left);
  6770. if (y <= rect.bottom) {
  6771. if (y < rect.top || curDeltaX > xDelta) {
  6772. return i - 1;
  6773. } else {
  6774. xDelta = curDeltaX;
  6775. }
  6776. }
  6777. }
  6778. return 0;
  6779. };
  6780. var inRect = function (rect, x, y) {
  6781. return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
  6782. };
  6783. var locateOffset = function (doc, textnode, x, y, rect) {
  6784. var rangeForOffset = function (o) {
  6785. var r = doc.dom().createRange();
  6786. r.setStart(textnode.dom(), o);
  6787. r.collapse(true);
  6788. return r;
  6789. };
  6790. var rectForOffset = function (o) {
  6791. var r = rangeForOffset(o);
  6792. return r.getBoundingClientRect();
  6793. };
  6794. var length = get$3(textnode).length;
  6795. var offset = searchForPoint(rectForOffset, x, y, rect.right, length);
  6796. return rangeForOffset(offset);
  6797. };
  6798. var locate = function (doc, node, x, y) {
  6799. var r = doc.dom().createRange();
  6800. r.selectNode(node.dom());
  6801. var rects = r.getClientRects();
  6802. var foundRect = findMap(rects, function (rect) {
  6803. return inRect(rect, x, y) ? Option.some(rect) : Option.none();
  6804. });
  6805. return foundRect.map(function (rect) {
  6806. return locateOffset(doc, node, x, y, rect);
  6807. });
  6808. };
  6809. var searchInChildren = function (doc, node, x, y) {
  6810. var r = doc.dom().createRange();
  6811. var nodes = children(node);
  6812. return findMap(nodes, function (n) {
  6813. r.selectNode(n.dom());
  6814. return inRect(r.getBoundingClientRect(), x, y) ? locateNode(doc, n, x, y) : Option.none();
  6815. });
  6816. };
  6817. var locateNode = function (doc, node, x, y) {
  6818. return isText(node) ? locate(doc, node, x, y) : searchInChildren(doc, node, x, y);
  6819. };
  6820. var locate$1 = function (doc, node, x, y) {
  6821. var r = doc.dom().createRange();
  6822. r.selectNode(node.dom());
  6823. var rect = r.getBoundingClientRect();
  6824. var boundedX = Math.max(rect.left, Math.min(rect.right, x));
  6825. var boundedY = Math.max(rect.top, Math.min(rect.bottom, y));
  6826. return locateNode(doc, node, boundedX, boundedY);
  6827. };
  6828. var COLLAPSE_TO_LEFT = true;
  6829. var COLLAPSE_TO_RIGHT = false;
  6830. var getCollapseDirection = function (rect, x) {
  6831. return x - rect.left < rect.right - x ? COLLAPSE_TO_LEFT : COLLAPSE_TO_RIGHT;
  6832. };
  6833. var createCollapsedNode = function (doc, target, collapseDirection) {
  6834. var r = doc.dom().createRange();
  6835. r.selectNode(target.dom());
  6836. r.collapse(collapseDirection);
  6837. return r;
  6838. };
  6839. var locateInElement = function (doc, node, x) {
  6840. var cursorRange = doc.dom().createRange();
  6841. cursorRange.selectNode(node.dom());
  6842. var rect = cursorRange.getBoundingClientRect();
  6843. var collapseDirection = getCollapseDirection(rect, x);
  6844. var f = collapseDirection === COLLAPSE_TO_LEFT ? first : last$1;
  6845. return f(node).map(function (target) {
  6846. return createCollapsedNode(doc, target, collapseDirection);
  6847. });
  6848. };
  6849. var locateInEmpty = function (doc, node, x) {
  6850. var rect = node.dom().getBoundingClientRect();
  6851. var collapseDirection = getCollapseDirection(rect, x);
  6852. return Option.some(createCollapsedNode(doc, node, collapseDirection));
  6853. };
  6854. var search = function (doc, node, x) {
  6855. var f = children(node).length === 0 ? locateInEmpty : locateInElement;
  6856. return f(doc, node, x);
  6857. };
  6858. var caretPositionFromPoint = function (doc, x, y) {
  6859. return Option.from(doc.dom().caretPositionFromPoint(x, y)).bind(function (pos) {
  6860. if (pos.offsetNode === null) {
  6861. return Option.none();
  6862. }
  6863. var r = doc.dom().createRange();
  6864. r.setStart(pos.offsetNode, pos.offset);
  6865. r.collapse();
  6866. return Option.some(r);
  6867. });
  6868. };
  6869. var caretRangeFromPoint = function (doc, x, y) {
  6870. return Option.from(doc.dom().caretRangeFromPoint(x, y));
  6871. };
  6872. var searchTextNodes = function (doc, node, x, y) {
  6873. var r = doc.dom().createRange();
  6874. r.selectNode(node.dom());
  6875. var rect = r.getBoundingClientRect();
  6876. var boundedX = Math.max(rect.left, Math.min(rect.right, x));
  6877. var boundedY = Math.max(rect.top, Math.min(rect.bottom, y));
  6878. return locate$1(doc, node, boundedX, boundedY);
  6879. };
  6880. var searchFromPoint = function (doc, x, y) {
  6881. return Element.fromPoint(doc, x, y).bind(function (elem) {
  6882. var fallback = function () {
  6883. return search(doc, elem, x);
  6884. };
  6885. return children(elem).length === 0 ? fallback() : searchTextNodes(doc, elem, x, y).orThunk(fallback);
  6886. });
  6887. };
  6888. var availableSearch = document.caretPositionFromPoint ? caretPositionFromPoint : document.caretRangeFromPoint ? caretRangeFromPoint : searchFromPoint;
  6889. var fromPoint$1 = function (win, x, y) {
  6890. var doc = Element.fromDom(win.document);
  6891. return availableSearch(doc, x, y).map(function (rng) {
  6892. return SimRange.create(Element.fromDom(rng.startContainer), rng.startOffset, Element.fromDom(rng.endContainer), rng.endOffset);
  6893. });
  6894. };
  6895. var beforeSpecial = function (element, offset) {
  6896. var name$1 = name(element);
  6897. if ('input' === name$1) {
  6898. return Situ.after(element);
  6899. } else if (!contains([
  6900. 'br',
  6901. 'img'
  6902. ], name$1)) {
  6903. return Situ.on(element, offset);
  6904. } else {
  6905. return offset === 0 ? Situ.before(element) : Situ.after(element);
  6906. }
  6907. };
  6908. var preprocessRelative = function (startSitu, finishSitu) {
  6909. var start = startSitu.fold(Situ.before, beforeSpecial, Situ.after);
  6910. var finish = finishSitu.fold(Situ.before, beforeSpecial, Situ.after);
  6911. return Selection.relative(start, finish);
  6912. };
  6913. var preprocessExact = function (start, soffset, finish, foffset) {
  6914. var startSitu = beforeSpecial(start, soffset);
  6915. var finishSitu = beforeSpecial(finish, foffset);
  6916. return Selection.relative(startSitu, finishSitu);
  6917. };
  6918. var preprocess = function (selection) {
  6919. return selection.match({
  6920. domRange: function (rng) {
  6921. var start = Element.fromDom(rng.startContainer);
  6922. var finish = Element.fromDom(rng.endContainer);
  6923. return preprocessExact(start, rng.startOffset, finish, rng.endOffset);
  6924. },
  6925. relative: preprocessRelative,
  6926. exact: preprocessExact
  6927. });
  6928. };
  6929. var makeRange = function (start, soffset, finish, foffset) {
  6930. var doc = owner(start);
  6931. var rng = doc.dom().createRange();
  6932. rng.setStart(start.dom(), soffset);
  6933. rng.setEnd(finish.dom(), foffset);
  6934. return rng;
  6935. };
  6936. var after$3 = function (start, soffset, finish, foffset) {
  6937. var r = makeRange(start, soffset, finish, foffset);
  6938. var same = eq(start, finish) && soffset === foffset;
  6939. return r.collapsed && !same;
  6940. };
  6941. var doSetNativeRange = function (win, rng) {
  6942. Option.from(win.getSelection()).each(function (selection) {
  6943. selection.removeAllRanges();
  6944. selection.addRange(rng);
  6945. });
  6946. };
  6947. var doSetRange = function (win, start, soffset, finish, foffset) {
  6948. var rng = exactToNative(win, start, soffset, finish, foffset);
  6949. doSetNativeRange(win, rng);
  6950. };
  6951. var setLegacyRtlRange = function (win, selection, start, soffset, finish, foffset) {
  6952. selection.collapse(start.dom(), soffset);
  6953. selection.extend(finish.dom(), foffset);
  6954. };
  6955. var setRangeFromRelative = function (win, relative) {
  6956. return diagnose(win, relative).match({
  6957. ltr: function (start, soffset, finish, foffset) {
  6958. doSetRange(win, start, soffset, finish, foffset);
  6959. },
  6960. rtl: function (start, soffset, finish, foffset) {
  6961. var selection = win.getSelection();
  6962. if (selection.setBaseAndExtent) {
  6963. selection.setBaseAndExtent(start.dom(), soffset, finish.dom(), foffset);
  6964. } else if (selection.extend) {
  6965. try {
  6966. setLegacyRtlRange(win, selection, start, soffset, finish, foffset);
  6967. } catch (e) {
  6968. doSetRange(win, finish, foffset, start, soffset);
  6969. }
  6970. } else {
  6971. doSetRange(win, finish, foffset, start, soffset);
  6972. }
  6973. }
  6974. });
  6975. };
  6976. var setExact = function (win, start, soffset, finish, foffset) {
  6977. var relative = preprocessExact(start, soffset, finish, foffset);
  6978. setRangeFromRelative(win, relative);
  6979. };
  6980. var setRelative = function (win, startSitu, finishSitu) {
  6981. var relative = preprocessRelative(startSitu, finishSitu);
  6982. setRangeFromRelative(win, relative);
  6983. };
  6984. var toNative = function (selection) {
  6985. var win = Selection.getWin(selection).dom();
  6986. var getDomRange = function (start, soffset, finish, foffset) {
  6987. return exactToNative(win, start, soffset, finish, foffset);
  6988. };
  6989. var filtered = preprocess(selection);
  6990. return diagnose(win, filtered).match({
  6991. ltr: getDomRange,
  6992. rtl: getDomRange
  6993. });
  6994. };
  6995. var readRange = function (selection) {
  6996. if (selection.rangeCount > 0) {
  6997. var firstRng = selection.getRangeAt(0);
  6998. var lastRng = selection.getRangeAt(selection.rangeCount - 1);
  6999. return Option.some(SimRange.create(Element.fromDom(firstRng.startContainer), firstRng.startOffset, Element.fromDom(lastRng.endContainer), lastRng.endOffset));
  7000. } else {
  7001. return Option.none();
  7002. }
  7003. };
  7004. var doGetExact = function (selection) {
  7005. var anchor = Element.fromDom(selection.anchorNode);
  7006. var focus = Element.fromDom(selection.focusNode);
  7007. return after$3(anchor, selection.anchorOffset, focus, selection.focusOffset) ? Option.some(SimRange.create(anchor, selection.anchorOffset, focus, selection.focusOffset)) : readRange(selection);
  7008. };
  7009. var setToElement = function (win, element) {
  7010. var rng = selectNodeContents(win, element);
  7011. doSetNativeRange(win, rng);
  7012. };
  7013. var getExact = function (win) {
  7014. return Option.from(win.getSelection()).filter(function (sel) {
  7015. return sel.rangeCount > 0;
  7016. }).bind(doGetExact);
  7017. };
  7018. var get$a = function (win) {
  7019. return getExact(win).map(function (range) {
  7020. return Selection.exact(range.start(), range.soffset(), range.finish(), range.foffset());
  7021. });
  7022. };
  7023. var getFirstRect$1 = function (win, selection) {
  7024. var rng = asLtrRange(win, selection);
  7025. return getFirstRect(rng);
  7026. };
  7027. var getAtPoint = function (win, x, y) {
  7028. return fromPoint$1(win, x, y);
  7029. };
  7030. var clear = function (win) {
  7031. var selection = win.getSelection();
  7032. selection.removeAllRanges();
  7033. };
  7034. var global$3 = tinymce.util.Tools.resolve('tinymce.util.VK');
  7035. var forward = function (editor, isRoot, cell, lazyWire) {
  7036. return go(editor, isRoot, next(cell), lazyWire);
  7037. };
  7038. var backward = function (editor, isRoot, cell, lazyWire) {
  7039. return go(editor, isRoot, prev(cell), lazyWire);
  7040. };
  7041. var getCellFirstCursorPosition = function (editor, cell) {
  7042. var selection = Selection.exact(cell, 0, cell, 0);
  7043. return toNative(selection);
  7044. };
  7045. var getNewRowCursorPosition = function (editor, table) {
  7046. var rows = descendants$1(table, 'tr');
  7047. return last(rows).bind(function (last) {
  7048. return descendant$1(last, 'td,th').map(function (first) {
  7049. return getCellFirstCursorPosition(editor, first);
  7050. });
  7051. });
  7052. };
  7053. var go = function (editor, isRoot, cell, actions, _lazyWire) {
  7054. return cell.fold(Option.none, Option.none, function (current, next) {
  7055. return first(next).map(function (cell) {
  7056. return getCellFirstCursorPosition(editor, cell);
  7057. });
  7058. }, function (current) {
  7059. return table(current, isRoot).bind(function (table) {
  7060. var targets = noMenu(current);
  7061. editor.undoManager.transact(function () {
  7062. actions.insertRowsAfter(table, targets);
  7063. });
  7064. return getNewRowCursorPosition(editor, table);
  7065. });
  7066. });
  7067. };
  7068. var rootElements = [
  7069. 'table',
  7070. 'li',
  7071. 'dl'
  7072. ];
  7073. var handle$1 = function (event, editor, actions, lazyWire) {
  7074. if (event.keyCode === global$3.TAB) {
  7075. var body_1 = getBody$1(editor);
  7076. var isRoot_1 = function (element) {
  7077. var name$1 = name(element);
  7078. return eq(element, body_1) || contains(rootElements, name$1);
  7079. };
  7080. var rng = editor.selection.getRng();
  7081. if (rng.collapsed) {
  7082. var start = Element.fromDom(rng.startContainer);
  7083. cell(start, isRoot_1).each(function (cell) {
  7084. event.preventDefault();
  7085. var navigation = event.shiftKey ? backward : forward;
  7086. var rng = navigation(editor, isRoot_1, cell, actions, lazyWire);
  7087. rng.each(function (range) {
  7088. editor.selection.setRng(range);
  7089. });
  7090. });
  7091. }
  7092. }
  7093. };
  7094. var create$3 = function (selection, kill) {
  7095. return {
  7096. selection: constant(selection),
  7097. kill: constant(kill)
  7098. };
  7099. };
  7100. var Response = { create: create$3 };
  7101. var create$4 = function (start, soffset, finish, foffset) {
  7102. return {
  7103. start: constant(Situ.on(start, soffset)),
  7104. finish: constant(Situ.on(finish, foffset))
  7105. };
  7106. };
  7107. var Situs = { create: create$4 };
  7108. var convertToRange = function (win, selection) {
  7109. var rng = asLtrRange(win, selection);
  7110. return SimRange.create(Element.fromDom(rng.startContainer), rng.startOffset, Element.fromDom(rng.endContainer), rng.endOffset);
  7111. };
  7112. var makeSitus = Situs.create;
  7113. var sync = function (container, isRoot, start, soffset, finish, foffset, selectRange) {
  7114. if (!(eq(start, finish) && soffset === foffset)) {
  7115. return closest$1(start, 'td,th', isRoot).bind(function (s) {
  7116. return closest$1(finish, 'td,th', isRoot).bind(function (f) {
  7117. return detect$5(container, isRoot, s, f, selectRange);
  7118. });
  7119. });
  7120. } else {
  7121. return Option.none();
  7122. }
  7123. };
  7124. var detect$5 = function (container, isRoot, start, finish, selectRange) {
  7125. if (!eq(start, finish)) {
  7126. return identify(start, finish, isRoot).bind(function (cellSel) {
  7127. var boxes = cellSel.boxes.getOr([]);
  7128. if (boxes.length > 0) {
  7129. selectRange(container, boxes, cellSel.start, cellSel.finish);
  7130. return Option.some(Response.create(Option.some(makeSitus(start, 0, start, getEnd(start))), true));
  7131. } else {
  7132. return Option.none();
  7133. }
  7134. });
  7135. } else {
  7136. return Option.none();
  7137. }
  7138. };
  7139. var update = function (rows, columns, container, selected, annotations) {
  7140. var updateSelection = function (newSels) {
  7141. annotations.clearBeforeUpdate(container);
  7142. annotations.selectRange(container, newSels.boxes, newSels.start, newSels.finish);
  7143. return newSels.boxes;
  7144. };
  7145. return shiftSelection(selected, rows, columns, annotations.firstSelectedSelector, annotations.lastSelectedSelector).map(updateSelection);
  7146. };
  7147. var traverse = function (item, mode) {
  7148. return {
  7149. item: constant(item),
  7150. mode: constant(mode)
  7151. };
  7152. };
  7153. var backtrack = function (universe, item, _direction, transition) {
  7154. if (transition === void 0) {
  7155. transition = sidestep;
  7156. }
  7157. return universe.property().parent(item).map(function (p) {
  7158. return traverse(p, transition);
  7159. });
  7160. };
  7161. var sidestep = function (universe, item, direction, transition) {
  7162. if (transition === void 0) {
  7163. transition = advance;
  7164. }
  7165. return direction.sibling(universe, item).map(function (p) {
  7166. return traverse(p, transition);
  7167. });
  7168. };
  7169. var advance = function (universe, item, direction, transition) {
  7170. if (transition === void 0) {
  7171. transition = advance;
  7172. }
  7173. var children = universe.property().children(item);
  7174. var result = direction.first(children);
  7175. return result.map(function (r) {
  7176. return traverse(r, transition);
  7177. });
  7178. };
  7179. var successors = [
  7180. {
  7181. current: backtrack,
  7182. next: sidestep,
  7183. fallback: Option.none()
  7184. },
  7185. {
  7186. current: sidestep,
  7187. next: advance,
  7188. fallback: Option.some(backtrack)
  7189. },
  7190. {
  7191. current: advance,
  7192. next: advance,
  7193. fallback: Option.some(sidestep)
  7194. }
  7195. ];
  7196. var go$1 = function (universe, item, mode, direction, rules) {
  7197. if (rules === void 0) {
  7198. rules = successors;
  7199. }
  7200. var ruleOpt = find(rules, function (succ) {
  7201. return succ.current === mode;
  7202. });
  7203. return ruleOpt.bind(function (rule) {
  7204. return rule.current(universe, item, direction, rule.next).orThunk(function () {
  7205. return rule.fallback.bind(function (fb) {
  7206. return go$1(universe, item, fb, direction);
  7207. });
  7208. });
  7209. });
  7210. };
  7211. var left = function () {
  7212. var sibling = function (universe, item) {
  7213. return universe.query().prevSibling(item);
  7214. };
  7215. var first = function (children) {
  7216. return children.length > 0 ? Option.some(children[children.length - 1]) : Option.none();
  7217. };
  7218. return {
  7219. sibling: sibling,
  7220. first: first
  7221. };
  7222. };
  7223. var right = function () {
  7224. var sibling = function (universe, item) {
  7225. return universe.query().nextSibling(item);
  7226. };
  7227. var first = function (children) {
  7228. return children.length > 0 ? Option.some(children[0]) : Option.none();
  7229. };
  7230. return {
  7231. sibling: sibling,
  7232. first: first
  7233. };
  7234. };
  7235. var Walkers = {
  7236. left: left,
  7237. right: right
  7238. };
  7239. var hone = function (universe, item, predicate, mode, direction, isRoot) {
  7240. var next = go$1(universe, item, mode, direction);
  7241. return next.bind(function (n) {
  7242. if (isRoot(n.item())) {
  7243. return Option.none();
  7244. } else {
  7245. return predicate(n.item()) ? Option.some(n.item()) : hone(universe, n.item(), predicate, n.mode(), direction, isRoot);
  7246. }
  7247. });
  7248. };
  7249. var left$1 = function (universe, item, predicate, isRoot) {
  7250. return hone(universe, item, predicate, sidestep, Walkers.left(), isRoot);
  7251. };
  7252. var right$1 = function (universe, item, predicate, isRoot) {
  7253. return hone(universe, item, predicate, sidestep, Walkers.right(), isRoot);
  7254. };
  7255. var isLeaf = function (universe) {
  7256. return function (element) {
  7257. return universe.property().children(element).length === 0;
  7258. };
  7259. };
  7260. var before$3 = function (universe, item, isRoot) {
  7261. return seekLeft(universe, item, isLeaf(universe), isRoot);
  7262. };
  7263. var after$4 = function (universe, item, isRoot) {
  7264. return seekRight(universe, item, isLeaf(universe), isRoot);
  7265. };
  7266. var seekLeft = left$1;
  7267. var seekRight = right$1;
  7268. var universe$3 = DomUniverse();
  7269. var before$4 = function (element, isRoot) {
  7270. return before$3(universe$3, element, isRoot);
  7271. };
  7272. var after$5 = function (element, isRoot) {
  7273. return after$4(universe$3, element, isRoot);
  7274. };
  7275. var seekLeft$1 = function (element, predicate, isRoot) {
  7276. return seekLeft(universe$3, element, predicate, isRoot);
  7277. };
  7278. var seekRight$1 = function (element, predicate, isRoot) {
  7279. return seekRight(universe$3, element, predicate, isRoot);
  7280. };
  7281. var ancestor$2 = function (scope, predicate, isRoot) {
  7282. return ancestor(scope, predicate, isRoot).isSome();
  7283. };
  7284. var adt$5 = Adt.generate([
  7285. { none: ['message'] },
  7286. { success: [] },
  7287. { failedUp: ['cell'] },
  7288. { failedDown: ['cell'] }
  7289. ]);
  7290. var isOverlapping = function (bridge, before, after) {
  7291. var beforeBounds = bridge.getRect(before);
  7292. var afterBounds = bridge.getRect(after);
  7293. return afterBounds.right > beforeBounds.left && afterBounds.left < beforeBounds.right;
  7294. };
  7295. var isRow = function (elem) {
  7296. return closest$1(elem, 'tr');
  7297. };
  7298. var verify = function (bridge, before, beforeOffset, after, afterOffset, failure, isRoot) {
  7299. return closest$1(after, 'td,th', isRoot).bind(function (afterCell) {
  7300. return closest$1(before, 'td,th', isRoot).map(function (beforeCell) {
  7301. if (!eq(afterCell, beforeCell)) {
  7302. return sharedOne$1(isRow, [
  7303. afterCell,
  7304. beforeCell
  7305. ]).fold(function () {
  7306. return isOverlapping(bridge, beforeCell, afterCell) ? adt$5.success() : failure(beforeCell);
  7307. }, function (_sharedRow) {
  7308. return failure(beforeCell);
  7309. });
  7310. } else {
  7311. return eq(after, afterCell) && getEnd(afterCell) === afterOffset ? failure(beforeCell) : adt$5.none('in same cell');
  7312. }
  7313. });
  7314. }).getOr(adt$5.none('default'));
  7315. };
  7316. var cata$2 = function (subject, onNone, onSuccess, onFailedUp, onFailedDown) {
  7317. return subject.fold(onNone, onSuccess, onFailedUp, onFailedDown);
  7318. };
  7319. var BeforeAfter = __assign(__assign({}, adt$5), {
  7320. verify: verify,
  7321. cata: cata$2
  7322. });
  7323. var inParent = function (parent, children, element, index) {
  7324. return {
  7325. parent: constant(parent),
  7326. children: constant(children),
  7327. element: constant(element),
  7328. index: constant(index)
  7329. };
  7330. };
  7331. var indexInParent = function (element) {
  7332. return parent(element).bind(function (parent) {
  7333. var children$1 = children(parent);
  7334. return indexOf(children$1, element).map(function (index) {
  7335. return inParent(parent, children$1, element, index);
  7336. });
  7337. });
  7338. };
  7339. var indexOf = function (elements, element) {
  7340. return findIndex(elements, curry(eq, element));
  7341. };
  7342. var isBr = function (elem) {
  7343. return name(elem) === 'br';
  7344. };
  7345. var gatherer = function (cand, gather, isRoot) {
  7346. return gather(cand, isRoot).bind(function (target) {
  7347. return isText(target) && get$3(target).trim().length === 0 ? gatherer(target, gather, isRoot) : Option.some(target);
  7348. });
  7349. };
  7350. var handleBr = function (isRoot, element, direction) {
  7351. return direction.traverse(element).orThunk(function () {
  7352. return gatherer(element, direction.gather, isRoot);
  7353. }).map(direction.relative);
  7354. };
  7355. var findBr = function (element, offset) {
  7356. return child(element, offset).filter(isBr).orThunk(function () {
  7357. return child(element, offset - 1).filter(isBr);
  7358. });
  7359. };
  7360. var handleParent = function (isRoot, element, offset, direction) {
  7361. return findBr(element, offset).bind(function (br) {
  7362. return direction.traverse(br).fold(function () {
  7363. return gatherer(br, direction.gather, isRoot).map(direction.relative);
  7364. }, function (adjacent) {
  7365. return indexInParent(adjacent).map(function (info) {
  7366. return Situ.on(info.parent(), info.index());
  7367. });
  7368. });
  7369. });
  7370. };
  7371. var tryBr = function (isRoot, element, offset, direction) {
  7372. var target = isBr(element) ? handleBr(isRoot, element, direction) : handleParent(isRoot, element, offset, direction);
  7373. return target.map(function (tgt) {
  7374. return {
  7375. start: constant(tgt),
  7376. finish: constant(tgt)
  7377. };
  7378. });
  7379. };
  7380. var process = function (analysis) {
  7381. return BeforeAfter.cata(analysis, function (_message) {
  7382. return Option.none();
  7383. }, function () {
  7384. return Option.none();
  7385. }, function (cell) {
  7386. return Option.some(point(cell, 0));
  7387. }, function (cell) {
  7388. return Option.some(point(cell, getEnd(cell)));
  7389. });
  7390. };
  7391. var moveDown = function (caret, amount) {
  7392. return {
  7393. left: caret.left,
  7394. top: caret.top + amount,
  7395. right: caret.right,
  7396. bottom: caret.bottom + amount
  7397. };
  7398. };
  7399. var moveUp = function (caret, amount) {
  7400. return {
  7401. left: caret.left,
  7402. top: caret.top - amount,
  7403. right: caret.right,
  7404. bottom: caret.bottom - amount
  7405. };
  7406. };
  7407. var translate = function (caret, xDelta, yDelta) {
  7408. return {
  7409. left: caret.left + xDelta,
  7410. top: caret.top + yDelta,
  7411. right: caret.right + xDelta,
  7412. bottom: caret.bottom + yDelta
  7413. };
  7414. };
  7415. var getTop$1 = function (caret) {
  7416. return caret.top;
  7417. };
  7418. var getBottom = function (caret) {
  7419. return caret.bottom;
  7420. };
  7421. var getPartialBox = function (bridge, element, offset) {
  7422. if (offset >= 0 && offset < getEnd(element)) {
  7423. return bridge.getRangedRect(element, offset, element, offset + 1);
  7424. } else if (offset > 0) {
  7425. return bridge.getRangedRect(element, offset - 1, element, offset);
  7426. }
  7427. return Option.none();
  7428. };
  7429. var toCaret = function (rect) {
  7430. return {
  7431. left: rect.left,
  7432. top: rect.top,
  7433. right: rect.right,
  7434. bottom: rect.bottom
  7435. };
  7436. };
  7437. var getElemBox = function (bridge, element) {
  7438. return Option.some(bridge.getRect(element));
  7439. };
  7440. var getBoxAt = function (bridge, element, offset) {
  7441. if (isElement(element)) {
  7442. return getElemBox(bridge, element).map(toCaret);
  7443. } else if (isText(element)) {
  7444. return getPartialBox(bridge, element, offset).map(toCaret);
  7445. } else {
  7446. return Option.none();
  7447. }
  7448. };
  7449. var getEntireBox = function (bridge, element) {
  7450. if (isElement(element)) {
  7451. return getElemBox(bridge, element).map(toCaret);
  7452. } else if (isText(element)) {
  7453. return bridge.getRangedRect(element, 0, element, getEnd(element)).map(toCaret);
  7454. } else {
  7455. return Option.none();
  7456. }
  7457. };
  7458. var JUMP_SIZE = 5;
  7459. var NUM_RETRIES = 100;
  7460. var adt$6 = Adt.generate([
  7461. { none: [] },
  7462. { retry: ['caret'] }
  7463. ]);
  7464. var isOutside = function (caret, box) {
  7465. return caret.left < box.left || Math.abs(box.right - caret.left) < 1 || caret.left > box.right;
  7466. };
  7467. var inOutsideBlock = function (bridge, element, caret) {
  7468. return closest(element, isBlock$1).fold(constant(false), function (cell) {
  7469. return getEntireBox(bridge, cell).exists(function (box) {
  7470. return isOutside(caret, box);
  7471. });
  7472. });
  7473. };
  7474. var adjustDown = function (bridge, element, guessBox, original, caret) {
  7475. var lowerCaret = moveDown(caret, JUMP_SIZE);
  7476. if (Math.abs(guessBox.bottom - original.bottom) < 1) {
  7477. return adt$6.retry(lowerCaret);
  7478. } else if (guessBox.top > caret.bottom) {
  7479. return adt$6.retry(lowerCaret);
  7480. } else if (guessBox.top === caret.bottom) {
  7481. return adt$6.retry(moveDown(caret, 1));
  7482. } else {
  7483. return inOutsideBlock(bridge, element, caret) ? adt$6.retry(translate(lowerCaret, JUMP_SIZE, 0)) : adt$6.none();
  7484. }
  7485. };
  7486. var adjustUp = function (bridge, element, guessBox, original, caret) {
  7487. var higherCaret = moveUp(caret, JUMP_SIZE);
  7488. if (Math.abs(guessBox.top - original.top) < 1) {
  7489. return adt$6.retry(higherCaret);
  7490. } else if (guessBox.bottom < caret.top) {
  7491. return adt$6.retry(higherCaret);
  7492. } else if (guessBox.bottom === caret.top) {
  7493. return adt$6.retry(moveUp(caret, 1));
  7494. } else {
  7495. return inOutsideBlock(bridge, element, caret) ? adt$6.retry(translate(higherCaret, JUMP_SIZE, 0)) : adt$6.none();
  7496. }
  7497. };
  7498. var upMovement = {
  7499. point: getTop$1,
  7500. adjuster: adjustUp,
  7501. move: moveUp,
  7502. gather: before$4
  7503. };
  7504. var downMovement = {
  7505. point: getBottom,
  7506. adjuster: adjustDown,
  7507. move: moveDown,
  7508. gather: after$5
  7509. };
  7510. var isAtTable = function (bridge, x, y) {
  7511. return bridge.elementFromPoint(x, y).filter(function (elm) {
  7512. return name(elm) === 'table';
  7513. }).isSome();
  7514. };
  7515. var adjustForTable = function (bridge, movement, original, caret, numRetries) {
  7516. return adjustTil(bridge, movement, original, movement.move(caret, JUMP_SIZE), numRetries);
  7517. };
  7518. var adjustTil = function (bridge, movement, original, caret, numRetries) {
  7519. if (numRetries === 0) {
  7520. return Option.some(caret);
  7521. }
  7522. if (isAtTable(bridge, caret.left, movement.point(caret))) {
  7523. return adjustForTable(bridge, movement, original, caret, numRetries - 1);
  7524. }
  7525. return bridge.situsFromPoint(caret.left, movement.point(caret)).bind(function (guess) {
  7526. return guess.start().fold(Option.none, function (element) {
  7527. return getEntireBox(bridge, element).bind(function (guessBox) {
  7528. return movement.adjuster(bridge, element, guessBox, original, caret).fold(Option.none, function (newCaret) {
  7529. return adjustTil(bridge, movement, original, newCaret, numRetries - 1);
  7530. });
  7531. }).orThunk(function () {
  7532. return Option.some(caret);
  7533. });
  7534. }, Option.none);
  7535. });
  7536. };
  7537. var ieTryDown = function (bridge, caret) {
  7538. return bridge.situsFromPoint(caret.left, caret.bottom + JUMP_SIZE);
  7539. };
  7540. var ieTryUp = function (bridge, caret) {
  7541. return bridge.situsFromPoint(caret.left, caret.top - JUMP_SIZE);
  7542. };
  7543. var checkScroll = function (movement, adjusted, bridge) {
  7544. if (movement.point(adjusted) > bridge.getInnerHeight()) {
  7545. return Option.some(movement.point(adjusted) - bridge.getInnerHeight());
  7546. } else if (movement.point(adjusted) < 0) {
  7547. return Option.some(-movement.point(adjusted));
  7548. } else {
  7549. return Option.none();
  7550. }
  7551. };
  7552. var retry = function (movement, bridge, caret) {
  7553. var moved = movement.move(caret, JUMP_SIZE);
  7554. var adjusted = adjustTil(bridge, movement, caret, moved, NUM_RETRIES).getOr(moved);
  7555. return checkScroll(movement, adjusted, bridge).fold(function () {
  7556. return bridge.situsFromPoint(adjusted.left, movement.point(adjusted));
  7557. }, function (delta) {
  7558. bridge.scrollBy(0, delta);
  7559. return bridge.situsFromPoint(adjusted.left, movement.point(adjusted) - delta);
  7560. });
  7561. };
  7562. var Retries = {
  7563. tryUp: curry(retry, upMovement),
  7564. tryDown: curry(retry, downMovement),
  7565. ieTryUp: ieTryUp,
  7566. ieTryDown: ieTryDown,
  7567. getJumpSize: constant(JUMP_SIZE)
  7568. };
  7569. var MAX_RETRIES = 20;
  7570. var findSpot = function (bridge, isRoot, direction) {
  7571. return bridge.getSelection().bind(function (sel) {
  7572. return tryBr(isRoot, sel.finish(), sel.foffset(), direction).fold(function () {
  7573. return Option.some(point(sel.finish(), sel.foffset()));
  7574. }, function (brNeighbour) {
  7575. var range = bridge.fromSitus(brNeighbour);
  7576. var analysis = BeforeAfter.verify(bridge, sel.finish(), sel.foffset(), range.finish(), range.foffset(), direction.failure, isRoot);
  7577. return process(analysis);
  7578. });
  7579. });
  7580. };
  7581. var scan$1 = function (bridge, isRoot, element, offset, direction, numRetries) {
  7582. if (numRetries === 0) {
  7583. return Option.none();
  7584. }
  7585. return tryCursor(bridge, isRoot, element, offset, direction).bind(function (situs) {
  7586. var range = bridge.fromSitus(situs);
  7587. var analysis = BeforeAfter.verify(bridge, element, offset, range.finish(), range.foffset(), direction.failure, isRoot);
  7588. return BeforeAfter.cata(analysis, function () {
  7589. return Option.none();
  7590. }, function () {
  7591. return Option.some(situs);
  7592. }, function (cell) {
  7593. if (eq(element, cell) && offset === 0) {
  7594. return tryAgain(bridge, element, offset, moveUp, direction);
  7595. } else {
  7596. return scan$1(bridge, isRoot, cell, 0, direction, numRetries - 1);
  7597. }
  7598. }, function (cell) {
  7599. if (eq(element, cell) && offset === getEnd(cell)) {
  7600. return tryAgain(bridge, element, offset, moveDown, direction);
  7601. } else {
  7602. return scan$1(bridge, isRoot, cell, getEnd(cell), direction, numRetries - 1);
  7603. }
  7604. });
  7605. });
  7606. };
  7607. var tryAgain = function (bridge, element, offset, move, direction) {
  7608. return getBoxAt(bridge, element, offset).bind(function (box) {
  7609. return tryAt(bridge, direction, move(box, Retries.getJumpSize()));
  7610. });
  7611. };
  7612. var tryAt = function (bridge, direction, box) {
  7613. var browser = detect$3().browser;
  7614. if (browser.isChrome() || browser.isSafari() || browser.isFirefox() || browser.isEdge()) {
  7615. return direction.otherRetry(bridge, box);
  7616. } else if (browser.isIE()) {
  7617. return direction.ieRetry(bridge, box);
  7618. } else {
  7619. return Option.none();
  7620. }
  7621. };
  7622. var tryCursor = function (bridge, isRoot, element, offset, direction) {
  7623. return getBoxAt(bridge, element, offset).bind(function (box) {
  7624. return tryAt(bridge, direction, box);
  7625. });
  7626. };
  7627. var handle$2 = function (bridge, isRoot, direction) {
  7628. return findSpot(bridge, isRoot, direction).bind(function (spot) {
  7629. return scan$1(bridge, isRoot, spot.element(), spot.offset(), direction, MAX_RETRIES).map(bridge.fromSitus);
  7630. });
  7631. };
  7632. var inSameTable = function (elem, table) {
  7633. return ancestor$2(elem, function (e) {
  7634. return parent(e).exists(function (p) {
  7635. return eq(p, table);
  7636. });
  7637. });
  7638. };
  7639. var simulate = function (bridge, isRoot, direction, initial, anchor) {
  7640. return closest$1(initial, 'td,th', isRoot).bind(function (start) {
  7641. return closest$1(start, 'table', isRoot).bind(function (table) {
  7642. if (!inSameTable(anchor, table)) {
  7643. return Option.none();
  7644. }
  7645. return handle$2(bridge, isRoot, direction).bind(function (range) {
  7646. return closest$1(range.finish(), 'td,th', isRoot).map(function (finish) {
  7647. return {
  7648. start: constant(start),
  7649. finish: constant(finish),
  7650. range: constant(range)
  7651. };
  7652. });
  7653. });
  7654. });
  7655. });
  7656. };
  7657. var navigate = function (bridge, isRoot, direction, initial, anchor, precheck) {
  7658. if (detect$3().browser.isIE()) {
  7659. return Option.none();
  7660. } else {
  7661. return precheck(initial, isRoot).orThunk(function () {
  7662. return simulate(bridge, isRoot, direction, initial, anchor).map(function (info) {
  7663. var range = info.range();
  7664. return Response.create(Option.some(makeSitus(range.start(), range.soffset(), range.finish(), range.foffset())), true);
  7665. });
  7666. });
  7667. }
  7668. };
  7669. var firstUpCheck = function (initial, isRoot) {
  7670. return closest$1(initial, 'tr', isRoot).bind(function (startRow) {
  7671. return closest$1(startRow, 'table', isRoot).bind(function (table) {
  7672. var rows = descendants$1(table, 'tr');
  7673. if (eq(startRow, rows[0])) {
  7674. return seekLeft$1(table, function (element) {
  7675. return last$1(element).isSome();
  7676. }, isRoot).map(function (last) {
  7677. var lastOffset = getEnd(last);
  7678. return Response.create(Option.some(makeSitus(last, lastOffset, last, lastOffset)), true);
  7679. });
  7680. } else {
  7681. return Option.none();
  7682. }
  7683. });
  7684. });
  7685. };
  7686. var lastDownCheck = function (initial, isRoot) {
  7687. return closest$1(initial, 'tr', isRoot).bind(function (startRow) {
  7688. return closest$1(startRow, 'table', isRoot).bind(function (table) {
  7689. var rows = descendants$1(table, 'tr');
  7690. if (eq(startRow, rows[rows.length - 1])) {
  7691. return seekRight$1(table, function (element) {
  7692. return first(element).isSome();
  7693. }, isRoot).map(function (first) {
  7694. return Response.create(Option.some(makeSitus(first, 0, first, 0)), true);
  7695. });
  7696. } else {
  7697. return Option.none();
  7698. }
  7699. });
  7700. });
  7701. };
  7702. var select = function (bridge, container, isRoot, direction, initial, anchor, selectRange) {
  7703. return simulate(bridge, isRoot, direction, initial, anchor).bind(function (info) {
  7704. return detect$5(container, isRoot, info.start(), info.finish(), selectRange);
  7705. });
  7706. };
  7707. var findCell = function (target, isRoot) {
  7708. return closest$1(target, 'td,th', isRoot);
  7709. };
  7710. function MouseSelection (bridge, container, isRoot, annotations) {
  7711. var cursor = Option.none();
  7712. var clearState = function () {
  7713. cursor = Option.none();
  7714. };
  7715. var mousedown = function (event) {
  7716. annotations.clear(container);
  7717. cursor = findCell(event.target(), isRoot);
  7718. };
  7719. var mouseover = function (event) {
  7720. cursor.each(function (start) {
  7721. annotations.clearBeforeUpdate(container);
  7722. findCell(event.target(), isRoot).each(function (finish) {
  7723. identify(start, finish, isRoot).each(function (cellSel) {
  7724. var boxes = cellSel.boxes.getOr([]);
  7725. if (boxes.length > 1 || boxes.length === 1 && !eq(start, finish)) {
  7726. annotations.selectRange(container, boxes, cellSel.start, cellSel.finish);
  7727. bridge.selectContents(finish);
  7728. }
  7729. });
  7730. });
  7731. });
  7732. };
  7733. var mouseup = function (_event) {
  7734. cursor.each(clearState);
  7735. };
  7736. return {
  7737. mousedown: mousedown,
  7738. mouseover: mouseover,
  7739. mouseup: mouseup
  7740. };
  7741. }
  7742. var down = {
  7743. traverse: nextSibling,
  7744. gather: after$5,
  7745. relative: Situ.before,
  7746. otherRetry: Retries.tryDown,
  7747. ieRetry: Retries.ieTryDown,
  7748. failure: BeforeAfter.failedDown
  7749. };
  7750. var up = {
  7751. traverse: prevSibling,
  7752. gather: before$4,
  7753. relative: Situ.before,
  7754. otherRetry: Retries.tryUp,
  7755. ieRetry: Retries.ieTryUp,
  7756. failure: BeforeAfter.failedUp
  7757. };
  7758. var isKey = function (key) {
  7759. return function (keycode) {
  7760. return keycode === key;
  7761. };
  7762. };
  7763. var isUp = isKey(38);
  7764. var isDown = isKey(40);
  7765. var isNavigation = function (keycode) {
  7766. return keycode >= 37 && keycode <= 40;
  7767. };
  7768. var ltr$3 = {
  7769. isBackward: isKey(37),
  7770. isForward: isKey(39)
  7771. };
  7772. var rtl$3 = {
  7773. isBackward: isKey(39),
  7774. isForward: isKey(37)
  7775. };
  7776. var toRaw = function (sr) {
  7777. return {
  7778. left: sr.left(),
  7779. top: sr.top(),
  7780. right: sr.right(),
  7781. bottom: sr.bottom(),
  7782. width: sr.width(),
  7783. height: sr.height()
  7784. };
  7785. };
  7786. var Rect = { toRaw: toRaw };
  7787. var get$b = function (_DOC) {
  7788. var doc = _DOC !== undefined ? _DOC.dom() : domGlobals.document;
  7789. var x = doc.body.scrollLeft || doc.documentElement.scrollLeft;
  7790. var y = doc.body.scrollTop || doc.documentElement.scrollTop;
  7791. return Position(x, y);
  7792. };
  7793. var by = function (x, y, _DOC) {
  7794. var doc = _DOC !== undefined ? _DOC.dom() : domGlobals.document;
  7795. var win = doc.defaultView;
  7796. win.scrollBy(x, y);
  7797. };
  7798. var WindowBridge = function (win) {
  7799. var elementFromPoint = function (x, y) {
  7800. return Element.fromPoint(Element.fromDom(win.document), x, y);
  7801. };
  7802. var getRect = function (element) {
  7803. return element.dom().getBoundingClientRect();
  7804. };
  7805. var getRangedRect = function (start, soffset, finish, foffset) {
  7806. var sel = Selection.exact(start, soffset, finish, foffset);
  7807. return getFirstRect$1(win, sel).map(Rect.toRaw);
  7808. };
  7809. var getSelection = function () {
  7810. return get$a(win).map(function (exactAdt) {
  7811. return convertToRange(win, exactAdt);
  7812. });
  7813. };
  7814. var fromSitus = function (situs) {
  7815. var relative = Selection.relative(situs.start(), situs.finish());
  7816. return convertToRange(win, relative);
  7817. };
  7818. var situsFromPoint = function (x, y) {
  7819. return getAtPoint(win, x, y).map(function (exact) {
  7820. return Situs.create(exact.start(), exact.soffset(), exact.finish(), exact.foffset());
  7821. });
  7822. };
  7823. var clearSelection = function () {
  7824. clear(win);
  7825. };
  7826. var collapseSelection = function (toStart) {
  7827. if (toStart === void 0) {
  7828. toStart = false;
  7829. }
  7830. get$a(win).each(function (sel) {
  7831. return sel.fold(function (rng) {
  7832. return rng.collapse(toStart);
  7833. }, function (startSitu, finishSitu) {
  7834. var situ = toStart ? startSitu : finishSitu;
  7835. setRelative(win, situ, situ);
  7836. }, function (start, soffset, finish, foffset) {
  7837. var node = toStart ? start : finish;
  7838. var offset = toStart ? soffset : foffset;
  7839. setExact(win, node, offset, node, offset);
  7840. });
  7841. });
  7842. };
  7843. var selectContents = function (element) {
  7844. setToElement(win, element);
  7845. };
  7846. var setSelection = function (sel) {
  7847. setExact(win, sel.start(), sel.soffset(), sel.finish(), sel.foffset());
  7848. };
  7849. var setRelativeSelection = function (start, finish) {
  7850. setRelative(win, start, finish);
  7851. };
  7852. var getInnerHeight = function () {
  7853. return win.innerHeight;
  7854. };
  7855. var getScrollY = function () {
  7856. var pos = get$b(Element.fromDom(win.document));
  7857. return pos.top();
  7858. };
  7859. var scrollBy = function (x, y) {
  7860. by(x, y, Element.fromDom(win.document));
  7861. };
  7862. return {
  7863. elementFromPoint: elementFromPoint,
  7864. getRect: getRect,
  7865. getRangedRect: getRangedRect,
  7866. getSelection: getSelection,
  7867. fromSitus: fromSitus,
  7868. situsFromPoint: situsFromPoint,
  7869. clearSelection: clearSelection,
  7870. collapseSelection: collapseSelection,
  7871. setSelection: setSelection,
  7872. setRelativeSelection: setRelativeSelection,
  7873. selectContents: selectContents,
  7874. getInnerHeight: getInnerHeight,
  7875. getScrollY: getScrollY,
  7876. scrollBy: scrollBy
  7877. };
  7878. };
  7879. var rc = function (rows, cols) {
  7880. return {
  7881. rows: rows,
  7882. cols: cols
  7883. };
  7884. };
  7885. var mouse = function (win, container, isRoot, annotations) {
  7886. var bridge = WindowBridge(win);
  7887. var handlers = MouseSelection(bridge, container, isRoot, annotations);
  7888. return {
  7889. mousedown: handlers.mousedown,
  7890. mouseover: handlers.mouseover,
  7891. mouseup: handlers.mouseup
  7892. };
  7893. };
  7894. var keyboard = function (win, container, isRoot, annotations) {
  7895. var bridge = WindowBridge(win);
  7896. var clearToNavigate = function () {
  7897. annotations.clear(container);
  7898. return Option.none();
  7899. };
  7900. var keydown = function (event, start, soffset, finish, foffset, direction) {
  7901. var realEvent = event.raw();
  7902. var keycode = realEvent.which;
  7903. var shiftKey = realEvent.shiftKey === true;
  7904. var handler = retrieve(container, annotations.selectedSelector).fold(function () {
  7905. if (isDown(keycode) && shiftKey) {
  7906. return curry(select, bridge, container, isRoot, down, finish, start, annotations.selectRange);
  7907. } else if (isUp(keycode) && shiftKey) {
  7908. return curry(select, bridge, container, isRoot, up, finish, start, annotations.selectRange);
  7909. } else if (isDown(keycode)) {
  7910. return curry(navigate, bridge, isRoot, down, finish, start, lastDownCheck);
  7911. } else if (isUp(keycode)) {
  7912. return curry(navigate, bridge, isRoot, up, finish, start, firstUpCheck);
  7913. } else {
  7914. return Option.none;
  7915. }
  7916. }, function (selected) {
  7917. var update$1 = function (attempts) {
  7918. return function () {
  7919. var navigation = findMap(attempts, function (delta) {
  7920. return update(delta.rows, delta.cols, container, selected, annotations);
  7921. });
  7922. return navigation.fold(function () {
  7923. return getEdges(container, annotations.firstSelectedSelector, annotations.lastSelectedSelector).map(function (edges) {
  7924. var relative = isDown(keycode) || direction.isForward(keycode) ? Situ.after : Situ.before;
  7925. bridge.setRelativeSelection(Situ.on(edges.first(), 0), relative(edges.table()));
  7926. annotations.clear(container);
  7927. return Response.create(Option.none(), true);
  7928. });
  7929. }, function (_) {
  7930. return Option.some(Response.create(Option.none(), true));
  7931. });
  7932. };
  7933. };
  7934. if (isDown(keycode) && shiftKey) {
  7935. return update$1([rc(+1, 0)]);
  7936. } else if (isUp(keycode) && shiftKey) {
  7937. return update$1([rc(-1, 0)]);
  7938. } else if (direction.isBackward(keycode) && shiftKey) {
  7939. return update$1([
  7940. rc(0, -1),
  7941. rc(-1, 0)
  7942. ]);
  7943. } else if (direction.isForward(keycode) && shiftKey) {
  7944. return update$1([
  7945. rc(0, +1),
  7946. rc(+1, 0)
  7947. ]);
  7948. } else if (isNavigation(keycode) && shiftKey === false) {
  7949. return clearToNavigate;
  7950. } else {
  7951. return Option.none;
  7952. }
  7953. });
  7954. return handler();
  7955. };
  7956. var keyup = function (event, start, soffset, finish, foffset) {
  7957. return retrieve(container, annotations.selectedSelector).fold(function () {
  7958. var realEvent = event.raw();
  7959. var keycode = realEvent.which;
  7960. var shiftKey = realEvent.shiftKey === true;
  7961. if (shiftKey === false) {
  7962. return Option.none();
  7963. }
  7964. if (isNavigation(keycode)) {
  7965. return sync(container, isRoot, start, soffset, finish, foffset, annotations.selectRange);
  7966. } else {
  7967. return Option.none();
  7968. }
  7969. }, Option.none);
  7970. };
  7971. return {
  7972. keydown: keydown,
  7973. keyup: keyup
  7974. };
  7975. };
  7976. var external = function (win, container, isRoot, annotations) {
  7977. var bridge = WindowBridge(win);
  7978. return function (start, finish) {
  7979. annotations.clearBeforeUpdate(container);
  7980. identify(start, finish, isRoot).each(function (cellSel) {
  7981. var boxes = cellSel.boxes.getOr([]);
  7982. annotations.selectRange(container, boxes, cellSel.start, cellSel.finish);
  7983. bridge.selectContents(finish);
  7984. bridge.collapseSelection();
  7985. });
  7986. };
  7987. };
  7988. var remove$7 = function (element, classes) {
  7989. each(classes, function (x) {
  7990. remove$5(element, x);
  7991. });
  7992. };
  7993. var addClass = function (clazz) {
  7994. return function (element) {
  7995. add$2(element, clazz);
  7996. };
  7997. };
  7998. var removeClasses = function (classes) {
  7999. return function (element) {
  8000. remove$7(element, classes);
  8001. };
  8002. };
  8003. var byClass = function (ephemera) {
  8004. var addSelectionClass = addClass(ephemera.selected);
  8005. var removeSelectionClasses = removeClasses([
  8006. ephemera.selected,
  8007. ephemera.lastSelected,
  8008. ephemera.firstSelected
  8009. ]);
  8010. var clear = function (container) {
  8011. var sels = descendants$1(container, ephemera.selectedSelector);
  8012. each(sels, removeSelectionClasses);
  8013. };
  8014. var selectRange = function (container, cells, start, finish) {
  8015. clear(container);
  8016. each(cells, addSelectionClass);
  8017. add$2(start, ephemera.firstSelected);
  8018. add$2(finish, ephemera.lastSelected);
  8019. };
  8020. return {
  8021. clearBeforeUpdate: clear,
  8022. clear: clear,
  8023. selectRange: selectRange,
  8024. selectedSelector: ephemera.selectedSelector,
  8025. firstSelectedSelector: ephemera.firstSelectedSelector,
  8026. lastSelectedSelector: ephemera.lastSelectedSelector
  8027. };
  8028. };
  8029. var byAttr = function (ephemera, onSelection, onClear) {
  8030. var removeSelectionAttributes = function (element) {
  8031. remove(element, ephemera.selected);
  8032. remove(element, ephemera.firstSelected);
  8033. remove(element, ephemera.lastSelected);
  8034. };
  8035. var addSelectionAttribute = function (element) {
  8036. set(element, ephemera.selected, '1');
  8037. };
  8038. var clear = function (container) {
  8039. clearBeforeUpdate(container);
  8040. onClear();
  8041. };
  8042. var clearBeforeUpdate = function (container) {
  8043. var sels = descendants$1(container, ephemera.selectedSelector);
  8044. each(sels, removeSelectionAttributes);
  8045. };
  8046. var selectRange = function (container, cells, start, finish) {
  8047. clear(container);
  8048. each(cells, addSelectionAttribute);
  8049. set(start, ephemera.firstSelected, '1');
  8050. set(finish, ephemera.lastSelected, '1');
  8051. onSelection(cells, start, finish);
  8052. };
  8053. return {
  8054. clearBeforeUpdate: clearBeforeUpdate,
  8055. clear: clear,
  8056. selectRange: selectRange,
  8057. selectedSelector: ephemera.selectedSelector,
  8058. firstSelectedSelector: ephemera.firstSelectedSelector,
  8059. lastSelectedSelector: ephemera.lastSelectedSelector
  8060. };
  8061. };
  8062. var SelectionAnnotation = {
  8063. byClass: byClass,
  8064. byAttr: byAttr
  8065. };
  8066. var getUpOrLeftCells = function (grid, selectedCells, generators) {
  8067. var upGrid = grid.slice(0, selectedCells[selectedCells.length - 1].row() + 1);
  8068. var upDetails = toDetailList(upGrid, generators);
  8069. return bind(upDetails, function (detail) {
  8070. var slicedCells = detail.cells().slice(0, selectedCells[selectedCells.length - 1].column() + 1);
  8071. return map(slicedCells, function (cell) {
  8072. return cell.element();
  8073. });
  8074. });
  8075. };
  8076. var getDownOrRightCells = function (grid, selectedCells, generators) {
  8077. var downGrid = grid.slice(selectedCells[0].row() + selectedCells[0].rowspan() - 1, grid.length);
  8078. var downDetails = toDetailList(downGrid, generators);
  8079. return bind(downDetails, function (detail) {
  8080. var slicedCells = detail.cells().slice(selectedCells[0].column() + selectedCells[0].colspan() - 1, +detail.cells().length);
  8081. return map(slicedCells, function (cell) {
  8082. return cell.element();
  8083. });
  8084. });
  8085. };
  8086. var getOtherCells = function (table, target, generators) {
  8087. var list = fromTable(table);
  8088. var house = Warehouse.generate(list);
  8089. var details = onCells(house, target);
  8090. return details.map(function (selectedCells) {
  8091. var grid = toGrid(house, generators, false);
  8092. var upOrLeftCells = getUpOrLeftCells(grid, selectedCells, generators);
  8093. var downOrRightCells = getDownOrRightCells(grid, selectedCells, generators);
  8094. return {
  8095. upOrLeftCells: upOrLeftCells,
  8096. downOrRightCells: downOrRightCells
  8097. };
  8098. });
  8099. };
  8100. var hasInternalTarget = function (e) {
  8101. return has$2(Element.fromDom(e.target), 'ephox-snooker-resizer-bar') === false;
  8102. };
  8103. function CellSelection (editor, lazyResize, selectionTargets) {
  8104. var handlers = Option.none();
  8105. var cloneFormats = getCloneElements(editor);
  8106. var onSelection = function (cells, start, finish) {
  8107. selectionTargets.targets().each(function (targets) {
  8108. var tableOpt = table(start);
  8109. tableOpt.each(function (table) {
  8110. var doc = Element.fromDom(editor.getDoc());
  8111. var generators = cellOperations(noop, doc, cloneFormats);
  8112. var otherCells = getOtherCells(table, targets, generators);
  8113. fireTableSelectionChange(editor, cells, start, finish, otherCells);
  8114. });
  8115. });
  8116. };
  8117. var onClear = function () {
  8118. fireTableSelectionClear(editor);
  8119. };
  8120. var annotations = SelectionAnnotation.byAttr(Ephemera, onSelection, onClear);
  8121. editor.on('init', function (_e) {
  8122. var win = editor.getWin();
  8123. var body = getBody$1(editor);
  8124. var isRoot = getIsRoot(editor);
  8125. var syncSelection = function () {
  8126. var sel = editor.selection;
  8127. var start = Element.fromDom(sel.getStart());
  8128. var end = Element.fromDom(sel.getEnd());
  8129. var shared = sharedOne$1(table, [
  8130. start,
  8131. end
  8132. ]);
  8133. shared.fold(function () {
  8134. annotations.clear(body);
  8135. }, noop);
  8136. };
  8137. var mouseHandlers = mouse(win, body, isRoot, annotations);
  8138. var keyHandlers = keyboard(win, body, isRoot, annotations);
  8139. var external$1 = external(win, body, isRoot, annotations);
  8140. var hasShiftKey = function (event) {
  8141. return event.raw().shiftKey === true;
  8142. };
  8143. editor.on('TableSelectorChange', function (e) {
  8144. external$1(e.start, e.finish);
  8145. });
  8146. var handleResponse = function (event, response) {
  8147. if (!hasShiftKey(event)) {
  8148. return;
  8149. }
  8150. if (response.kill()) {
  8151. event.kill();
  8152. }
  8153. response.selection().each(function (ns) {
  8154. var relative = Selection.relative(ns.start(), ns.finish());
  8155. var rng = asLtrRange(win, relative);
  8156. editor.selection.setRng(rng);
  8157. });
  8158. };
  8159. var keyup = function (event) {
  8160. var wrappedEvent = fromRawEvent$1(event);
  8161. if (wrappedEvent.raw().shiftKey && isNavigation(wrappedEvent.raw().which)) {
  8162. var rng = editor.selection.getRng();
  8163. var start = Element.fromDom(rng.startContainer);
  8164. var end = Element.fromDom(rng.endContainer);
  8165. keyHandlers.keyup(wrappedEvent, start, rng.startOffset, end, rng.endOffset).each(function (response) {
  8166. handleResponse(wrappedEvent, response);
  8167. });
  8168. }
  8169. };
  8170. var keydown = function (event) {
  8171. var wrappedEvent = fromRawEvent$1(event);
  8172. lazyResize().each(function (resize) {
  8173. resize.hideBars();
  8174. });
  8175. var rng = editor.selection.getRng();
  8176. var startContainer = Element.fromDom(editor.selection.getStart());
  8177. var start = Element.fromDom(rng.startContainer);
  8178. var end = Element.fromDom(rng.endContainer);
  8179. var direction = directionAt(startContainer).isRtl() ? rtl$3 : ltr$3;
  8180. keyHandlers.keydown(wrappedEvent, start, rng.startOffset, end, rng.endOffset, direction).each(function (response) {
  8181. handleResponse(wrappedEvent, response);
  8182. });
  8183. lazyResize().each(function (resize) {
  8184. resize.showBars();
  8185. });
  8186. };
  8187. var isLeftMouse = function (raw) {
  8188. return raw.button === 0;
  8189. };
  8190. var isLeftButtonPressed = function (raw) {
  8191. if (raw.buttons === undefined) {
  8192. return true;
  8193. }
  8194. if (global$2.browser.isEdge() && raw.buttons === 0) {
  8195. return true;
  8196. }
  8197. return (raw.buttons & 1) !== 0;
  8198. };
  8199. var mouseDown = function (e) {
  8200. if (isLeftMouse(e) && hasInternalTarget(e)) {
  8201. mouseHandlers.mousedown(fromRawEvent$1(e));
  8202. }
  8203. };
  8204. var mouseOver = function (e) {
  8205. if (isLeftButtonPressed(e) && hasInternalTarget(e)) {
  8206. mouseHandlers.mouseover(fromRawEvent$1(e));
  8207. }
  8208. };
  8209. var mouseUp = function (e) {
  8210. if (isLeftMouse(e) && hasInternalTarget(e)) {
  8211. mouseHandlers.mouseup(fromRawEvent$1(e));
  8212. }
  8213. };
  8214. var getDoubleTap = function () {
  8215. var lastTarget = Cell(Element.fromDom(body));
  8216. var lastTimeStamp = Cell(0);
  8217. var touchEnd = function (t) {
  8218. var target = Element.fromDom(t.target);
  8219. if (name(target) === 'td' || name(target) === 'th') {
  8220. var lT = lastTarget.get();
  8221. var lTS = lastTimeStamp.get();
  8222. if (eq(lT, target) && t.timeStamp - lTS < 300) {
  8223. t.preventDefault();
  8224. external$1(target, target);
  8225. }
  8226. }
  8227. lastTarget.set(target);
  8228. lastTimeStamp.set(t.timeStamp);
  8229. };
  8230. return { touchEnd: touchEnd };
  8231. };
  8232. var doubleTap = getDoubleTap();
  8233. editor.on('mousedown', mouseDown);
  8234. editor.on('mouseover', mouseOver);
  8235. editor.on('mouseup', mouseUp);
  8236. editor.on('touchend', doubleTap.touchEnd);
  8237. editor.on('keyup', keyup);
  8238. editor.on('keydown', keydown);
  8239. editor.on('NodeChange', syncSelection);
  8240. handlers = Option.some({
  8241. mousedown: mouseDown,
  8242. mouseover: mouseOver,
  8243. mouseup: mouseUp,
  8244. keyup: keyup,
  8245. keydown: keydown
  8246. });
  8247. });
  8248. var destroy = function () {
  8249. handlers.each(function (_handlers) {
  8250. });
  8251. };
  8252. return {
  8253. clear: annotations.clear,
  8254. destroy: destroy
  8255. };
  8256. }
  8257. var Selections = function (editor) {
  8258. var get = function () {
  8259. var body = getBody$1(editor);
  8260. return retrieve$1(body, selectedSelector).fold(function () {
  8261. if (editor.selection.getStart() === undefined) {
  8262. return none$1();
  8263. } else {
  8264. return single(editor.selection);
  8265. }
  8266. }, function (cells) {
  8267. return multiple(cells);
  8268. });
  8269. };
  8270. return { get: get };
  8271. };
  8272. var getSelectionTargets = function (editor, selections) {
  8273. var targets = Cell(Option.none());
  8274. var changeHandlers = Cell([]);
  8275. var findTargets = function () {
  8276. return getSelectionStartCellOrCaption(editor).bind(function (cellOrCaption) {
  8277. var table$1 = table(cellOrCaption);
  8278. return table$1.map(function (table) {
  8279. if (name(cellOrCaption) === 'caption') {
  8280. return notCell(cellOrCaption);
  8281. } else {
  8282. return forMenu(selections, table, cellOrCaption);
  8283. }
  8284. });
  8285. });
  8286. };
  8287. var resetTargets = function () {
  8288. targets.set(cached(findTargets)());
  8289. each(changeHandlers.get(), function (handler) {
  8290. return handler();
  8291. });
  8292. };
  8293. var onSetup = function (api, isDisabled) {
  8294. var handler = function () {
  8295. return targets.get().fold(function () {
  8296. api.setDisabled(true);
  8297. }, function (targets) {
  8298. api.setDisabled(isDisabled(targets));
  8299. });
  8300. };
  8301. handler();
  8302. changeHandlers.set(changeHandlers.get().concat([handler]));
  8303. return function () {
  8304. changeHandlers.set(filter(changeHandlers.get(), function (h) {
  8305. return h !== handler;
  8306. }));
  8307. };
  8308. };
  8309. var onSetupTable = function (api) {
  8310. return onSetup(api, function (_) {
  8311. return false;
  8312. });
  8313. };
  8314. var onSetupCellOrRow = function (api) {
  8315. return onSetup(api, function (targets) {
  8316. return name(targets.element()) === 'caption';
  8317. });
  8318. };
  8319. var onSetupMergeable = function (api) {
  8320. return onSetup(api, function (targets) {
  8321. return targets.mergable().isNone();
  8322. });
  8323. };
  8324. var onSetupUnmergeable = function (api) {
  8325. return onSetup(api, function (targets) {
  8326. return targets.unmergable().isNone();
  8327. });
  8328. };
  8329. editor.on('NodeChange TableSelectorChange', resetTargets);
  8330. return {
  8331. onSetupTable: onSetupTable,
  8332. onSetupCellOrRow: onSetupCellOrRow,
  8333. onSetupMergeable: onSetupMergeable,
  8334. onSetupUnmergeable: onSetupUnmergeable,
  8335. resetTargets: resetTargets,
  8336. targets: function () {
  8337. return targets.get();
  8338. }
  8339. };
  8340. };
  8341. var addButtons = function (editor, selectionTargets) {
  8342. editor.ui.registry.addMenuButton('table', {
  8343. tooltip: 'Table',
  8344. icon: 'table',
  8345. fetch: function (callback) {
  8346. return callback('inserttable | cell row column | advtablesort | tableprops deletetable');
  8347. }
  8348. });
  8349. var cmd = function (command) {
  8350. return function () {
  8351. return editor.execCommand(command);
  8352. };
  8353. };
  8354. editor.ui.registry.addButton('tableprops', {
  8355. tooltip: 'Table properties',
  8356. onAction: cmd('mceTableProps'),
  8357. icon: 'table',
  8358. onSetup: selectionTargets.onSetupTable
  8359. });
  8360. editor.ui.registry.addButton('tabledelete', {
  8361. tooltip: 'Delete table',
  8362. onAction: cmd('mceTableDelete'),
  8363. icon: 'table-delete-table',
  8364. onSetup: selectionTargets.onSetupTable
  8365. });
  8366. editor.ui.registry.addButton('tablecellprops', {
  8367. tooltip: 'Cell properties',
  8368. onAction: cmd('mceTableCellProps'),
  8369. icon: 'table-cell-properties',
  8370. onSetup: selectionTargets.onSetupCellOrRow
  8371. });
  8372. editor.ui.registry.addButton('tablemergecells', {
  8373. tooltip: 'Merge cells',
  8374. onAction: cmd('mceTableMergeCells'),
  8375. icon: 'table-merge-cells',
  8376. onSetup: selectionTargets.onSetupMergeable
  8377. });
  8378. editor.ui.registry.addButton('tablesplitcells', {
  8379. tooltip: 'Split cell',
  8380. onAction: cmd('mceTableSplitCells'),
  8381. icon: 'table-split-cells',
  8382. onSetup: selectionTargets.onSetupUnmergeable
  8383. });
  8384. editor.ui.registry.addButton('tableinsertrowbefore', {
  8385. tooltip: 'Insert row before',
  8386. onAction: cmd('mceTableInsertRowBefore'),
  8387. icon: 'table-insert-row-above',
  8388. onSetup: selectionTargets.onSetupCellOrRow
  8389. });
  8390. editor.ui.registry.addButton('tableinsertrowafter', {
  8391. tooltip: 'Insert row after',
  8392. onAction: cmd('mceTableInsertRowAfter'),
  8393. icon: 'table-insert-row-after',
  8394. onSetup: selectionTargets.onSetupCellOrRow
  8395. });
  8396. editor.ui.registry.addButton('tabledeleterow', {
  8397. tooltip: 'Delete row',
  8398. onAction: cmd('mceTableDeleteRow'),
  8399. icon: 'table-delete-row',
  8400. onSetup: selectionTargets.onSetupCellOrRow
  8401. });
  8402. editor.ui.registry.addButton('tablerowprops', {
  8403. tooltip: 'Row properties',
  8404. onAction: cmd('mceTableRowProps'),
  8405. icon: 'table-row-properties',
  8406. onSetup: selectionTargets.onSetupCellOrRow
  8407. });
  8408. editor.ui.registry.addButton('tableinsertcolbefore', {
  8409. tooltip: 'Insert column before',
  8410. onAction: cmd('mceTableInsertColBefore'),
  8411. icon: 'table-insert-column-before',
  8412. onSetup: selectionTargets.onSetupCellOrRow
  8413. });
  8414. editor.ui.registry.addButton('tableinsertcolafter', {
  8415. tooltip: 'Insert column after',
  8416. onAction: cmd('mceTableInsertColAfter'),
  8417. icon: 'table-insert-column-after',
  8418. onSetup: selectionTargets.onSetupCellOrRow
  8419. });
  8420. editor.ui.registry.addButton('tabledeletecol', {
  8421. tooltip: 'Delete column',
  8422. onAction: cmd('mceTableDeleteCol'),
  8423. icon: 'table-delete-column',
  8424. onSetup: selectionTargets.onSetupCellOrRow
  8425. });
  8426. editor.ui.registry.addButton('tablecutrow', {
  8427. tooltip: 'Cut row',
  8428. onAction: cmd('mceTableCutRow'),
  8429. icon: 'temporary-placeholder',
  8430. onSetup: selectionTargets.onSetupCellOrRow
  8431. });
  8432. editor.ui.registry.addButton('tablecopyrow', {
  8433. tooltip: 'Copy row',
  8434. onAction: cmd('mceTableCopyRow'),
  8435. icon: 'temporary-placeholder',
  8436. onSetup: selectionTargets.onSetupCellOrRow
  8437. });
  8438. editor.ui.registry.addButton('tablepasterowbefore', {
  8439. tooltip: 'Paste row before',
  8440. onAction: cmd('mceTablePasteRowBefore'),
  8441. icon: 'temporary-placeholder',
  8442. onSetup: selectionTargets.onSetupCellOrRow
  8443. });
  8444. editor.ui.registry.addButton('tablepasterowafter', {
  8445. tooltip: 'Paste row after',
  8446. onAction: cmd('mceTablePasteRowAfter'),
  8447. icon: 'temporary-placeholder',
  8448. onSetup: selectionTargets.onSetupCellOrRow
  8449. });
  8450. editor.ui.registry.addButton('tableinsertdialog', {
  8451. tooltip: 'Insert table',
  8452. onAction: cmd('mceInsertTable'),
  8453. icon: 'table'
  8454. });
  8455. };
  8456. var addToolbars = function (editor) {
  8457. var isTable = function (table) {
  8458. return editor.dom.is(table, 'table') && editor.getBody().contains(table);
  8459. };
  8460. var toolbar = getToolbar(editor);
  8461. if (toolbar.length > 0) {
  8462. editor.ui.registry.addContextToolbar('table', {
  8463. predicate: isTable,
  8464. items: toolbar,
  8465. scope: 'node',
  8466. position: 'node'
  8467. });
  8468. }
  8469. };
  8470. var addMenuItems = function (editor, selectionTargets) {
  8471. var cmd = function (command) {
  8472. return function () {
  8473. return editor.execCommand(command);
  8474. };
  8475. };
  8476. var insertTableAction = function (_a) {
  8477. var numRows = _a.numRows, numColumns = _a.numColumns;
  8478. editor.undoManager.transact(function () {
  8479. insert$1(editor, numColumns, numRows);
  8480. });
  8481. editor.addVisual();
  8482. };
  8483. var tableProperties = {
  8484. text: 'Table properties',
  8485. onSetup: selectionTargets.onSetupTable,
  8486. onAction: cmd('mceTableProps')
  8487. };
  8488. var deleteTable = {
  8489. text: 'Delete table',
  8490. icon: 'table-delete-table',
  8491. onSetup: selectionTargets.onSetupTable,
  8492. onAction: cmd('mceTableDelete')
  8493. };
  8494. var rowItems = [
  8495. {
  8496. type: 'menuitem',
  8497. text: 'Insert row before',
  8498. icon: 'table-insert-row-above',
  8499. onAction: cmd('mceTableInsertRowBefore'),
  8500. onSetup: selectionTargets.onSetupCellOrRow
  8501. },
  8502. {
  8503. type: 'menuitem',
  8504. text: 'Insert row after',
  8505. icon: 'table-insert-row-after',
  8506. onAction: cmd('mceTableInsertRowAfter'),
  8507. onSetup: selectionTargets.onSetupCellOrRow
  8508. },
  8509. {
  8510. type: 'menuitem',
  8511. text: 'Delete row',
  8512. icon: 'table-delete-row',
  8513. onAction: cmd('mceTableDeleteRow'),
  8514. onSetup: selectionTargets.onSetupCellOrRow
  8515. },
  8516. {
  8517. type: 'menuitem',
  8518. text: 'Row properties',
  8519. icon: 'table-row-properties',
  8520. onAction: cmd('mceTableRowProps'),
  8521. onSetup: selectionTargets.onSetupCellOrRow
  8522. },
  8523. { type: 'separator' },
  8524. {
  8525. type: 'menuitem',
  8526. text: 'Cut row',
  8527. onAction: cmd('mceTableCutRow'),
  8528. onSetup: selectionTargets.onSetupCellOrRow
  8529. },
  8530. {
  8531. type: 'menuitem',
  8532. text: 'Copy row',
  8533. onAction: cmd('mceTableCopyRow'),
  8534. onSetup: selectionTargets.onSetupCellOrRow
  8535. },
  8536. {
  8537. type: 'menuitem',
  8538. text: 'Paste row before',
  8539. onAction: cmd('mceTablePasteRowBefore'),
  8540. onSetup: selectionTargets.onSetupCellOrRow
  8541. },
  8542. {
  8543. type: 'menuitem',
  8544. text: 'Paste row after',
  8545. onAction: cmd('mceTablePasteRowAfter'),
  8546. onSetup: selectionTargets.onSetupCellOrRow
  8547. }
  8548. ];
  8549. var row = {
  8550. type: 'nestedmenuitem',
  8551. text: 'Row',
  8552. getSubmenuItems: function () {
  8553. return rowItems;
  8554. }
  8555. };
  8556. var columnItems = [
  8557. {
  8558. type: 'menuitem',
  8559. text: 'Insert column before',
  8560. icon: 'table-insert-column-before',
  8561. onAction: cmd('mceTableInsertColBefore'),
  8562. onSetup: selectionTargets.onSetupCellOrRow
  8563. },
  8564. {
  8565. type: 'menuitem',
  8566. text: 'Insert column after',
  8567. icon: 'table-insert-column-after',
  8568. onAction: cmd('mceTableInsertColAfter'),
  8569. onSetup: selectionTargets.onSetupCellOrRow
  8570. },
  8571. {
  8572. type: 'menuitem',
  8573. text: 'Delete column',
  8574. icon: 'table-delete-column',
  8575. onAction: cmd('mceTableDeleteCol'),
  8576. onSetup: selectionTargets.onSetupCellOrRow
  8577. }
  8578. ];
  8579. var column = {
  8580. type: 'nestedmenuitem',
  8581. text: 'Column',
  8582. getSubmenuItems: function () {
  8583. return columnItems;
  8584. }
  8585. };
  8586. var cellItems = [
  8587. {
  8588. type: 'menuitem',
  8589. text: 'Cell properties',
  8590. icon: 'table-cell-properties',
  8591. onAction: cmd('mceTableCellProps'),
  8592. onSetup: selectionTargets.onSetupCellOrRow
  8593. },
  8594. {
  8595. type: 'menuitem',
  8596. text: 'Merge cells',
  8597. icon: 'table-merge-cells',
  8598. onAction: cmd('mceTableMergeCells'),
  8599. onSetup: selectionTargets.onSetupMergeable
  8600. },
  8601. {
  8602. type: 'menuitem',
  8603. text: 'Split cell',
  8604. icon: 'table-split-cells',
  8605. onAction: cmd('mceTableSplitCells'),
  8606. onSetup: selectionTargets.onSetupUnmergeable
  8607. }
  8608. ];
  8609. var cell = {
  8610. type: 'nestedmenuitem',
  8611. text: 'Cell',
  8612. getSubmenuItems: function () {
  8613. return cellItems;
  8614. }
  8615. };
  8616. if (hasTableGrid(editor) === false) {
  8617. editor.ui.registry.addMenuItem('inserttable', {
  8618. text: 'Table',
  8619. icon: 'table',
  8620. onAction: cmd('mceInsertTable')
  8621. });
  8622. } else {
  8623. editor.ui.registry.addNestedMenuItem('inserttable', {
  8624. text: 'Table',
  8625. icon: 'table',
  8626. getSubmenuItems: function () {
  8627. return [{
  8628. type: 'fancymenuitem',
  8629. fancytype: 'inserttable',
  8630. onAction: insertTableAction
  8631. }];
  8632. }
  8633. });
  8634. }
  8635. editor.ui.registry.addMenuItem('inserttabledialog', {
  8636. text: 'Insert table',
  8637. icon: 'table',
  8638. onAction: cmd('mceInsertTable')
  8639. });
  8640. editor.ui.registry.addMenuItem('tableprops', tableProperties);
  8641. editor.ui.registry.addMenuItem('deletetable', deleteTable);
  8642. editor.ui.registry.addNestedMenuItem('row', row);
  8643. editor.ui.registry.addNestedMenuItem('column', column);
  8644. editor.ui.registry.addNestedMenuItem('cell', cell);
  8645. editor.ui.registry.addContextMenu('table', {
  8646. update: function () {
  8647. selectionTargets.resetTargets();
  8648. return selectionTargets.targets().fold(function () {
  8649. return '';
  8650. }, function (targets) {
  8651. if (name(targets.element()) === 'caption') {
  8652. return 'tableprops deletetable';
  8653. } else {
  8654. return 'cell row column | advtablesort | tableprops deletetable';
  8655. }
  8656. });
  8657. }
  8658. });
  8659. };
  8660. var getClipboardRows = function (clipboardRows) {
  8661. return clipboardRows.get().fold(function () {
  8662. return;
  8663. }, function (rows) {
  8664. return map(rows, function (row) {
  8665. return row.dom();
  8666. });
  8667. });
  8668. };
  8669. var setClipboardRows = function (rows, clipboardRows) {
  8670. var sugarRows = map(rows, Element.fromDom);
  8671. clipboardRows.set(Option.from(sugarRows));
  8672. };
  8673. var getApi = function (editor, clipboardRows, resizeHandler, selectionTargets) {
  8674. return {
  8675. insertTable: function (columns, rows) {
  8676. return insert$1(editor, columns, rows);
  8677. },
  8678. setClipboardRows: function (rows) {
  8679. return setClipboardRows(rows, clipboardRows);
  8680. },
  8681. getClipboardRows: function () {
  8682. return getClipboardRows(clipboardRows);
  8683. },
  8684. resizeHandler: resizeHandler,
  8685. selectionTargets: selectionTargets
  8686. };
  8687. };
  8688. function Plugin(editor) {
  8689. var selections = Selections(editor);
  8690. var selectionTargets = getSelectionTargets(editor, selections);
  8691. var resizeHandler = getResizeHandler(editor);
  8692. var cellSelection = CellSelection(editor, resizeHandler.lazyResize, selectionTargets);
  8693. var actions = TableActions(editor, resizeHandler.lazyWire);
  8694. var clipboardRows = Cell(Option.none());
  8695. registerCommands(editor, actions, cellSelection, selections, clipboardRows);
  8696. registerEvents(editor, selections, actions, cellSelection);
  8697. addMenuItems(editor, selectionTargets);
  8698. addButtons(editor, selectionTargets);
  8699. addToolbars(editor);
  8700. editor.on('PreInit', function () {
  8701. editor.serializer.addTempAttr(firstSelected);
  8702. editor.serializer.addTempAttr(lastSelected);
  8703. });
  8704. if (hasTabNavigation(editor)) {
  8705. editor.on('keydown', function (e) {
  8706. handle$1(e, editor, actions, resizeHandler.lazyWire);
  8707. });
  8708. }
  8709. editor.on('remove', function () {
  8710. resizeHandler.destroy();
  8711. cellSelection.destroy();
  8712. });
  8713. return getApi(editor, clipboardRows, resizeHandler, selectionTargets);
  8714. }
  8715. function Plugin$1 () {
  8716. global.add('table', Plugin);
  8717. }
  8718. Plugin$1();
  8719. }(window));