index.js 353 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012
  1. import { Text, RangeSet, MapMode, RangeValue, Facet, StateEffect, ChangeSet, findClusterBreak, EditorSelection, EditorState, findColumn, CharCategory, Prec, Transaction, codePointAt, codePointSize, combineConfig, StateField, RangeSetBuilder, countColumn } from '@codemirror/state';
  2. import { StyleModule } from 'style-mod';
  3. import { keyName, base, shift } from 'w3c-keyname';
  4. function getSelection(root) {
  5. let target;
  6. // Browsers differ on whether shadow roots have a getSelection
  7. // method. If it exists, use that, otherwise, call it on the
  8. // document.
  9. if (root.nodeType == 11) { // Shadow root
  10. target = root.getSelection ? root : root.ownerDocument;
  11. }
  12. else {
  13. target = root;
  14. }
  15. return target.getSelection();
  16. }
  17. function contains(dom, node) {
  18. return node ? dom == node || dom.contains(node.nodeType != 1 ? node.parentNode : node) : false;
  19. }
  20. function deepActiveElement() {
  21. let elt = document.activeElement;
  22. while (elt && elt.shadowRoot)
  23. elt = elt.shadowRoot.activeElement;
  24. return elt;
  25. }
  26. function hasSelection(dom, selection) {
  27. if (!selection.anchorNode)
  28. return false;
  29. try {
  30. // Firefox will raise 'permission denied' errors when accessing
  31. // properties of `sel.anchorNode` when it's in a generated CSS
  32. // element.
  33. return contains(dom, selection.anchorNode);
  34. }
  35. catch (_) {
  36. return false;
  37. }
  38. }
  39. function clientRectsFor(dom) {
  40. if (dom.nodeType == 3)
  41. return textRange(dom, 0, dom.nodeValue.length).getClientRects();
  42. else if (dom.nodeType == 1)
  43. return dom.getClientRects();
  44. else
  45. return [];
  46. }
  47. // Scans forward and backward through DOM positions equivalent to the
  48. // given one to see if the two are in the same place (i.e. after a
  49. // text node vs at the end of that text node)
  50. function isEquivalentPosition(node, off, targetNode, targetOff) {
  51. return targetNode ? (scanFor(node, off, targetNode, targetOff, -1) ||
  52. scanFor(node, off, targetNode, targetOff, 1)) : false;
  53. }
  54. function domIndex(node) {
  55. for (var index = 0;; index++) {
  56. node = node.previousSibling;
  57. if (!node)
  58. return index;
  59. }
  60. }
  61. function scanFor(node, off, targetNode, targetOff, dir) {
  62. for (;;) {
  63. if (node == targetNode && off == targetOff)
  64. return true;
  65. if (off == (dir < 0 ? 0 : maxOffset(node))) {
  66. if (node.nodeName == "DIV")
  67. return false;
  68. let parent = node.parentNode;
  69. if (!parent || parent.nodeType != 1)
  70. return false;
  71. off = domIndex(node) + (dir < 0 ? 0 : 1);
  72. node = parent;
  73. }
  74. else if (node.nodeType == 1) {
  75. node = node.childNodes[off + (dir < 0 ? -1 : 0)];
  76. if (node.nodeType == 1 && node.contentEditable == "false")
  77. return false;
  78. off = dir < 0 ? maxOffset(node) : 0;
  79. }
  80. else {
  81. return false;
  82. }
  83. }
  84. }
  85. function maxOffset(node) {
  86. return node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length;
  87. }
  88. const Rect0 = { left: 0, right: 0, top: 0, bottom: 0 };
  89. function flattenRect(rect, left) {
  90. let x = left ? rect.left : rect.right;
  91. return { left: x, right: x, top: rect.top, bottom: rect.bottom };
  92. }
  93. function windowRect(win) {
  94. return { left: 0, right: win.innerWidth,
  95. top: 0, bottom: win.innerHeight };
  96. }
  97. function scrollRectIntoView(dom, rect, side, x, y, xMargin, yMargin, ltr) {
  98. let doc = dom.ownerDocument, win = doc.defaultView;
  99. for (let cur = dom; cur;) {
  100. if (cur.nodeType == 1) { // Element
  101. let bounding, top = cur == doc.body;
  102. if (top) {
  103. bounding = windowRect(win);
  104. }
  105. else {
  106. if (cur.scrollHeight <= cur.clientHeight && cur.scrollWidth <= cur.clientWidth) {
  107. cur = cur.parentNode;
  108. continue;
  109. }
  110. let rect = cur.getBoundingClientRect();
  111. // Make sure scrollbar width isn't included in the rectangle
  112. bounding = { left: rect.left, right: rect.left + cur.clientWidth,
  113. top: rect.top, bottom: rect.top + cur.clientHeight };
  114. }
  115. let moveX = 0, moveY = 0;
  116. if (y == "nearest") {
  117. if (rect.top < bounding.top) {
  118. moveY = -(bounding.top - rect.top + yMargin);
  119. if (side > 0 && rect.bottom > bounding.bottom + moveY)
  120. moveY = rect.bottom - bounding.bottom + moveY + yMargin;
  121. }
  122. else if (rect.bottom > bounding.bottom) {
  123. moveY = rect.bottom - bounding.bottom + yMargin;
  124. if (side < 0 && (rect.top - moveY) < bounding.top)
  125. moveY = -(bounding.top + moveY - rect.top + yMargin);
  126. }
  127. }
  128. else {
  129. let rectHeight = rect.bottom - rect.top, boundingHeight = bounding.bottom - bounding.top;
  130. let targetTop = y == "center" && rectHeight <= boundingHeight ? rect.top + rectHeight / 2 - boundingHeight / 2 :
  131. y == "start" || y == "center" && side < 0 ? rect.top - yMargin :
  132. rect.bottom - boundingHeight + yMargin;
  133. moveY = targetTop - bounding.top;
  134. }
  135. if (x == "nearest") {
  136. if (rect.left < bounding.left) {
  137. moveX = -(bounding.left - rect.left + xMargin);
  138. if (side > 0 && rect.right > bounding.right + moveX)
  139. moveX = rect.right - bounding.right + moveX + xMargin;
  140. }
  141. else if (rect.right > bounding.right) {
  142. moveX = rect.right - bounding.right + xMargin;
  143. if (side < 0 && rect.left < bounding.left + moveX)
  144. moveX = -(bounding.left + moveX - rect.left + xMargin);
  145. }
  146. }
  147. else {
  148. let targetLeft = x == "center" ? rect.left + (rect.right - rect.left) / 2 - (bounding.right - bounding.left) / 2 :
  149. (x == "start") == ltr ? rect.left - xMargin :
  150. rect.right - (bounding.right - bounding.left) + xMargin;
  151. moveX = targetLeft - bounding.left;
  152. }
  153. if (moveX || moveY) {
  154. if (top) {
  155. win.scrollBy(moveX, moveY);
  156. }
  157. else {
  158. if (moveY) {
  159. let start = cur.scrollTop;
  160. cur.scrollTop += moveY;
  161. moveY = cur.scrollTop - start;
  162. }
  163. if (moveX) {
  164. let start = cur.scrollLeft;
  165. cur.scrollLeft += moveX;
  166. moveX = cur.scrollLeft - start;
  167. }
  168. rect = { left: rect.left - moveX, top: rect.top - moveY,
  169. right: rect.right - moveX, bottom: rect.bottom - moveY };
  170. }
  171. }
  172. if (top)
  173. break;
  174. cur = cur.assignedSlot || cur.parentNode;
  175. x = y = "nearest";
  176. }
  177. else if (cur.nodeType == 11) { // A shadow root
  178. cur = cur.host;
  179. }
  180. else {
  181. break;
  182. }
  183. }
  184. }
  185. class DOMSelectionState {
  186. constructor() {
  187. this.anchorNode = null;
  188. this.anchorOffset = 0;
  189. this.focusNode = null;
  190. this.focusOffset = 0;
  191. }
  192. eq(domSel) {
  193. return this.anchorNode == domSel.anchorNode && this.anchorOffset == domSel.anchorOffset &&
  194. this.focusNode == domSel.focusNode && this.focusOffset == domSel.focusOffset;
  195. }
  196. setRange(range) {
  197. this.set(range.anchorNode, range.anchorOffset, range.focusNode, range.focusOffset);
  198. }
  199. set(anchorNode, anchorOffset, focusNode, focusOffset) {
  200. this.anchorNode = anchorNode;
  201. this.anchorOffset = anchorOffset;
  202. this.focusNode = focusNode;
  203. this.focusOffset = focusOffset;
  204. }
  205. }
  206. let preventScrollSupported = null;
  207. // Feature-detects support for .focus({preventScroll: true}), and uses
  208. // a fallback kludge when not supported.
  209. function focusPreventScroll(dom) {
  210. if (dom.setActive)
  211. return dom.setActive(); // in IE
  212. if (preventScrollSupported)
  213. return dom.focus(preventScrollSupported);
  214. let stack = [];
  215. for (let cur = dom; cur; cur = cur.parentNode) {
  216. stack.push(cur, cur.scrollTop, cur.scrollLeft);
  217. if (cur == cur.ownerDocument)
  218. break;
  219. }
  220. dom.focus(preventScrollSupported == null ? {
  221. get preventScroll() {
  222. preventScrollSupported = { preventScroll: true };
  223. return true;
  224. }
  225. } : undefined);
  226. if (!preventScrollSupported) {
  227. preventScrollSupported = false;
  228. for (let i = 0; i < stack.length;) {
  229. let elt = stack[i++], top = stack[i++], left = stack[i++];
  230. if (elt.scrollTop != top)
  231. elt.scrollTop = top;
  232. if (elt.scrollLeft != left)
  233. elt.scrollLeft = left;
  234. }
  235. }
  236. }
  237. let scratchRange;
  238. function textRange(node, from, to = from) {
  239. let range = scratchRange || (scratchRange = document.createRange());
  240. range.setEnd(node, to);
  241. range.setStart(node, from);
  242. return range;
  243. }
  244. function dispatchKey(elt, name, code) {
  245. let options = { key: name, code: name, keyCode: code, which: code, cancelable: true };
  246. let down = new KeyboardEvent("keydown", options);
  247. down.synthetic = true;
  248. elt.dispatchEvent(down);
  249. let up = new KeyboardEvent("keyup", options);
  250. up.synthetic = true;
  251. elt.dispatchEvent(up);
  252. return down.defaultPrevented || up.defaultPrevented;
  253. }
  254. function getRoot(node) {
  255. while (node) {
  256. if (node && (node.nodeType == 9 || node.nodeType == 11 && node.host))
  257. return node;
  258. node = node.assignedSlot || node.parentNode;
  259. }
  260. return null;
  261. }
  262. function clearAttributes(node) {
  263. while (node.attributes.length)
  264. node.removeAttributeNode(node.attributes[0]);
  265. }
  266. class DOMPos {
  267. constructor(node, offset, precise = true) {
  268. this.node = node;
  269. this.offset = offset;
  270. this.precise = precise;
  271. }
  272. static before(dom, precise) { return new DOMPos(dom.parentNode, domIndex(dom), precise); }
  273. static after(dom, precise) { return new DOMPos(dom.parentNode, domIndex(dom) + 1, precise); }
  274. }
  275. const noChildren = [];
  276. class ContentView {
  277. constructor() {
  278. this.parent = null;
  279. this.dom = null;
  280. this.dirty = 2 /* Node */;
  281. }
  282. get editorView() {
  283. if (!this.parent)
  284. throw new Error("Accessing view in orphan content view");
  285. return this.parent.editorView;
  286. }
  287. get overrideDOMText() { return null; }
  288. get posAtStart() {
  289. return this.parent ? this.parent.posBefore(this) : 0;
  290. }
  291. get posAtEnd() {
  292. return this.posAtStart + this.length;
  293. }
  294. posBefore(view) {
  295. let pos = this.posAtStart;
  296. for (let child of this.children) {
  297. if (child == view)
  298. return pos;
  299. pos += child.length + child.breakAfter;
  300. }
  301. throw new RangeError("Invalid child in posBefore");
  302. }
  303. posAfter(view) {
  304. return this.posBefore(view) + view.length;
  305. }
  306. // Will return a rectangle directly before (when side < 0), after
  307. // (side > 0) or directly on (when the browser supports it) the
  308. // given position.
  309. coordsAt(_pos, _side) { return null; }
  310. sync(track) {
  311. if (this.dirty & 2 /* Node */) {
  312. let parent = this.dom;
  313. let prev = null, next;
  314. for (let child of this.children) {
  315. if (child.dirty) {
  316. if (!child.dom && (next = prev ? prev.nextSibling : parent.firstChild)) {
  317. let contentView = ContentView.get(next);
  318. if (!contentView || !contentView.parent && contentView.constructor == child.constructor)
  319. child.reuseDOM(next);
  320. }
  321. child.sync(track);
  322. child.dirty = 0 /* Not */;
  323. }
  324. next = prev ? prev.nextSibling : parent.firstChild;
  325. if (track && !track.written && track.node == parent && next != child.dom)
  326. track.written = true;
  327. if (child.dom.parentNode == parent) {
  328. while (next && next != child.dom)
  329. next = rm$1(next);
  330. }
  331. else {
  332. parent.insertBefore(child.dom, next);
  333. }
  334. prev = child.dom;
  335. }
  336. next = prev ? prev.nextSibling : parent.firstChild;
  337. if (next && track && track.node == parent)
  338. track.written = true;
  339. while (next)
  340. next = rm$1(next);
  341. }
  342. else if (this.dirty & 1 /* Child */) {
  343. for (let child of this.children)
  344. if (child.dirty) {
  345. child.sync(track);
  346. child.dirty = 0 /* Not */;
  347. }
  348. }
  349. }
  350. reuseDOM(_dom) { }
  351. localPosFromDOM(node, offset) {
  352. let after;
  353. if (node == this.dom) {
  354. after = this.dom.childNodes[offset];
  355. }
  356. else {
  357. let bias = maxOffset(node) == 0 ? 0 : offset == 0 ? -1 : 1;
  358. for (;;) {
  359. let parent = node.parentNode;
  360. if (parent == this.dom)
  361. break;
  362. if (bias == 0 && parent.firstChild != parent.lastChild) {
  363. if (node == parent.firstChild)
  364. bias = -1;
  365. else
  366. bias = 1;
  367. }
  368. node = parent;
  369. }
  370. if (bias < 0)
  371. after = node;
  372. else
  373. after = node.nextSibling;
  374. }
  375. if (after == this.dom.firstChild)
  376. return 0;
  377. while (after && !ContentView.get(after))
  378. after = after.nextSibling;
  379. if (!after)
  380. return this.length;
  381. for (let i = 0, pos = 0;; i++) {
  382. let child = this.children[i];
  383. if (child.dom == after)
  384. return pos;
  385. pos += child.length + child.breakAfter;
  386. }
  387. }
  388. domBoundsAround(from, to, offset = 0) {
  389. let fromI = -1, fromStart = -1, toI = -1, toEnd = -1;
  390. for (let i = 0, pos = offset, prevEnd = offset; i < this.children.length; i++) {
  391. let child = this.children[i], end = pos + child.length;
  392. if (pos < from && end > to)
  393. return child.domBoundsAround(from, to, pos);
  394. if (end >= from && fromI == -1) {
  395. fromI = i;
  396. fromStart = pos;
  397. }
  398. if (pos > to && child.dom.parentNode == this.dom) {
  399. toI = i;
  400. toEnd = prevEnd;
  401. break;
  402. }
  403. prevEnd = end;
  404. pos = end + child.breakAfter;
  405. }
  406. return { from: fromStart, to: toEnd < 0 ? offset + this.length : toEnd,
  407. startDOM: (fromI ? this.children[fromI - 1].dom.nextSibling : null) || this.dom.firstChild,
  408. endDOM: toI < this.children.length && toI >= 0 ? this.children[toI].dom : null };
  409. }
  410. markDirty(andParent = false) {
  411. this.dirty |= 2 /* Node */;
  412. this.markParentsDirty(andParent);
  413. }
  414. markParentsDirty(childList) {
  415. for (let parent = this.parent; parent; parent = parent.parent) {
  416. if (childList)
  417. parent.dirty |= 2 /* Node */;
  418. if (parent.dirty & 1 /* Child */)
  419. return;
  420. parent.dirty |= 1 /* Child */;
  421. childList = false;
  422. }
  423. }
  424. setParent(parent) {
  425. if (this.parent != parent) {
  426. this.parent = parent;
  427. if (this.dirty)
  428. this.markParentsDirty(true);
  429. }
  430. }
  431. setDOM(dom) {
  432. if (this.dom)
  433. this.dom.cmView = null;
  434. this.dom = dom;
  435. dom.cmView = this;
  436. }
  437. get rootView() {
  438. for (let v = this;;) {
  439. let parent = v.parent;
  440. if (!parent)
  441. return v;
  442. v = parent;
  443. }
  444. }
  445. replaceChildren(from, to, children = noChildren) {
  446. this.markDirty();
  447. for (let i = from; i < to; i++) {
  448. let child = this.children[i];
  449. if (child.parent == this)
  450. child.destroy();
  451. }
  452. this.children.splice(from, to - from, ...children);
  453. for (let i = 0; i < children.length; i++)
  454. children[i].setParent(this);
  455. }
  456. ignoreMutation(_rec) { return false; }
  457. ignoreEvent(_event) { return false; }
  458. childCursor(pos = this.length) {
  459. return new ChildCursor(this.children, pos, this.children.length);
  460. }
  461. childPos(pos, bias = 1) {
  462. return this.childCursor().findPos(pos, bias);
  463. }
  464. toString() {
  465. let name = this.constructor.name.replace("View", "");
  466. return name + (this.children.length ? "(" + this.children.join() + ")" :
  467. this.length ? "[" + (name == "Text" ? this.text : this.length) + "]" : "") +
  468. (this.breakAfter ? "#" : "");
  469. }
  470. static get(node) { return node.cmView; }
  471. get isEditable() { return true; }
  472. merge(from, to, source, hasStart, openStart, openEnd) {
  473. return false;
  474. }
  475. become(other) { return false; }
  476. // When this is a zero-length view with a side, this should return a
  477. // number <= 0 to indicate it is before its position, or a
  478. // number > 0 when after its position.
  479. getSide() { return 0; }
  480. destroy() {
  481. this.parent = null;
  482. }
  483. }
  484. ContentView.prototype.breakAfter = 0;
  485. // Remove a DOM node and return its next sibling.
  486. function rm$1(dom) {
  487. let next = dom.nextSibling;
  488. dom.parentNode.removeChild(dom);
  489. return next;
  490. }
  491. class ChildCursor {
  492. constructor(children, pos, i) {
  493. this.children = children;
  494. this.pos = pos;
  495. this.i = i;
  496. this.off = 0;
  497. }
  498. findPos(pos, bias = 1) {
  499. for (;;) {
  500. if (pos > this.pos || pos == this.pos &&
  501. (bias > 0 || this.i == 0 || this.children[this.i - 1].breakAfter)) {
  502. this.off = pos - this.pos;
  503. return this;
  504. }
  505. let next = this.children[--this.i];
  506. this.pos -= next.length + next.breakAfter;
  507. }
  508. }
  509. }
  510. function replaceRange(parent, fromI, fromOff, toI, toOff, insert, breakAtStart, openStart, openEnd) {
  511. let { children } = parent;
  512. let before = children.length ? children[fromI] : null;
  513. let last = insert.length ? insert[insert.length - 1] : null;
  514. let breakAtEnd = last ? last.breakAfter : breakAtStart;
  515. // Change within a single child
  516. if (fromI == toI && before && !breakAtStart && !breakAtEnd && insert.length < 2 &&
  517. before.merge(fromOff, toOff, insert.length ? last : null, fromOff == 0, openStart, openEnd))
  518. return;
  519. if (toI < children.length) {
  520. let after = children[toI];
  521. // Make sure the end of the child after the update is preserved in `after`
  522. if (after && toOff < after.length) {
  523. // If we're splitting a child, separate part of it to avoid that
  524. // being mangled when updating the child before the update.
  525. if (fromI == toI) {
  526. after = after.split(toOff);
  527. toOff = 0;
  528. }
  529. // If the element after the replacement should be merged with
  530. // the last replacing element, update `content`
  531. if (!breakAtEnd && last && after.merge(0, toOff, last, true, 0, openEnd)) {
  532. insert[insert.length - 1] = after;
  533. }
  534. else {
  535. // Remove the start of the after element, if necessary, and
  536. // add it to `content`.
  537. if (toOff)
  538. after.merge(0, toOff, null, false, 0, openEnd);
  539. insert.push(after);
  540. }
  541. }
  542. else if (after === null || after === void 0 ? void 0 : after.breakAfter) {
  543. // The element at `toI` is entirely covered by this range.
  544. // Preserve its line break, if any.
  545. if (last)
  546. last.breakAfter = 1;
  547. else
  548. breakAtStart = 1;
  549. }
  550. // Since we've handled the next element from the current elements
  551. // now, make sure `toI` points after that.
  552. toI++;
  553. }
  554. if (before) {
  555. before.breakAfter = breakAtStart;
  556. if (fromOff > 0) {
  557. if (!breakAtStart && insert.length && before.merge(fromOff, before.length, insert[0], false, openStart, 0)) {
  558. before.breakAfter = insert.shift().breakAfter;
  559. }
  560. else if (fromOff < before.length || before.children.length && before.children[before.children.length - 1].length == 0) {
  561. before.merge(fromOff, before.length, null, false, openStart, 0);
  562. }
  563. fromI++;
  564. }
  565. }
  566. // Try to merge widgets on the boundaries of the replacement
  567. while (fromI < toI && insert.length) {
  568. if (children[toI - 1].become(insert[insert.length - 1])) {
  569. toI--;
  570. insert.pop();
  571. openEnd = insert.length ? 0 : openStart;
  572. }
  573. else if (children[fromI].become(insert[0])) {
  574. fromI++;
  575. insert.shift();
  576. openStart = insert.length ? 0 : openEnd;
  577. }
  578. else {
  579. break;
  580. }
  581. }
  582. if (!insert.length && fromI && toI < children.length && !children[fromI - 1].breakAfter &&
  583. children[toI].merge(0, 0, children[fromI - 1], false, openStart, openEnd))
  584. fromI--;
  585. if (fromI < toI || insert.length)
  586. parent.replaceChildren(fromI, toI, insert);
  587. }
  588. function mergeChildrenInto(parent, from, to, insert, openStart, openEnd) {
  589. let cur = parent.childCursor();
  590. let { i: toI, off: toOff } = cur.findPos(to, 1);
  591. let { i: fromI, off: fromOff } = cur.findPos(from, -1);
  592. let dLen = from - to;
  593. for (let view of insert)
  594. dLen += view.length;
  595. parent.length += dLen;
  596. replaceRange(parent, fromI, fromOff, toI, toOff, insert, 0, openStart, openEnd);
  597. }
  598. let nav = typeof navigator != "undefined" ? navigator : { userAgent: "", vendor: "", platform: "" };
  599. let doc = typeof document != "undefined" ? document : { documentElement: { style: {} } };
  600. const ie_edge = /*@__PURE__*//Edge\/(\d+)/.exec(nav.userAgent);
  601. const ie_upto10 = /*@__PURE__*//MSIE \d/.test(nav.userAgent);
  602. const ie_11up = /*@__PURE__*//Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(nav.userAgent);
  603. const ie = !!(ie_upto10 || ie_11up || ie_edge);
  604. const gecko = !ie && /*@__PURE__*//gecko\/(\d+)/i.test(nav.userAgent);
  605. const chrome = !ie && /*@__PURE__*//Chrome\/(\d+)/.exec(nav.userAgent);
  606. const webkit = "webkitFontSmoothing" in doc.documentElement.style;
  607. const safari = !ie && /*@__PURE__*//Apple Computer/.test(nav.vendor);
  608. const ios = safari && (/*@__PURE__*//Mobile\/\w+/.test(nav.userAgent) || nav.maxTouchPoints > 2);
  609. var browser = {
  610. mac: ios || /*@__PURE__*//Mac/.test(nav.platform),
  611. windows: /*@__PURE__*//Win/.test(nav.platform),
  612. linux: /*@__PURE__*//Linux|X11/.test(nav.platform),
  613. ie,
  614. ie_version: ie_upto10 ? doc.documentMode || 6 : ie_11up ? +ie_11up[1] : ie_edge ? +ie_edge[1] : 0,
  615. gecko,
  616. gecko_version: gecko ? +(/*@__PURE__*//Firefox\/(\d+)/.exec(nav.userAgent) || [0, 0])[1] : 0,
  617. chrome: !!chrome,
  618. chrome_version: chrome ? +chrome[1] : 0,
  619. ios,
  620. android: /*@__PURE__*//Android\b/.test(nav.userAgent),
  621. webkit,
  622. safari,
  623. webkit_version: webkit ? +(/*@__PURE__*//\bAppleWebKit\/(\d+)/.exec(navigator.userAgent) || [0, 0])[1] : 0,
  624. tabSize: doc.documentElement.style.tabSize != null ? "tab-size" : "-moz-tab-size"
  625. };
  626. const MaxJoinLen = 256;
  627. class TextView extends ContentView {
  628. constructor(text) {
  629. super();
  630. this.text = text;
  631. }
  632. get length() { return this.text.length; }
  633. createDOM(textDOM) {
  634. this.setDOM(textDOM || document.createTextNode(this.text));
  635. }
  636. sync(track) {
  637. if (!this.dom)
  638. this.createDOM();
  639. if (this.dom.nodeValue != this.text) {
  640. if (track && track.node == this.dom)
  641. track.written = true;
  642. this.dom.nodeValue = this.text;
  643. }
  644. }
  645. reuseDOM(dom) {
  646. if (dom.nodeType == 3)
  647. this.createDOM(dom);
  648. }
  649. merge(from, to, source) {
  650. if (source && (!(source instanceof TextView) || this.length - (to - from) + source.length > MaxJoinLen))
  651. return false;
  652. this.text = this.text.slice(0, from) + (source ? source.text : "") + this.text.slice(to);
  653. this.markDirty();
  654. return true;
  655. }
  656. split(from) {
  657. let result = new TextView(this.text.slice(from));
  658. this.text = this.text.slice(0, from);
  659. this.markDirty();
  660. return result;
  661. }
  662. localPosFromDOM(node, offset) {
  663. return node == this.dom ? offset : offset ? this.text.length : 0;
  664. }
  665. domAtPos(pos) { return new DOMPos(this.dom, pos); }
  666. domBoundsAround(_from, _to, offset) {
  667. return { from: offset, to: offset + this.length, startDOM: this.dom, endDOM: this.dom.nextSibling };
  668. }
  669. coordsAt(pos, side) {
  670. return textCoords(this.dom, pos, side);
  671. }
  672. }
  673. class MarkView extends ContentView {
  674. constructor(mark, children = [], length = 0) {
  675. super();
  676. this.mark = mark;
  677. this.children = children;
  678. this.length = length;
  679. for (let ch of children)
  680. ch.setParent(this);
  681. }
  682. setAttrs(dom) {
  683. clearAttributes(dom);
  684. if (this.mark.class)
  685. dom.className = this.mark.class;
  686. if (this.mark.attrs)
  687. for (let name in this.mark.attrs)
  688. dom.setAttribute(name, this.mark.attrs[name]);
  689. return dom;
  690. }
  691. reuseDOM(node) {
  692. if (node.nodeName == this.mark.tagName.toUpperCase()) {
  693. this.setDOM(node);
  694. this.dirty |= 4 /* Attrs */ | 2 /* Node */;
  695. }
  696. }
  697. sync(track) {
  698. if (!this.dom)
  699. this.setDOM(this.setAttrs(document.createElement(this.mark.tagName)));
  700. else if (this.dirty & 4 /* Attrs */)
  701. this.setAttrs(this.dom);
  702. super.sync(track);
  703. }
  704. merge(from, to, source, _hasStart, openStart, openEnd) {
  705. if (source && (!(source instanceof MarkView && source.mark.eq(this.mark)) ||
  706. (from && openStart <= 0) || (to < this.length && openEnd <= 0)))
  707. return false;
  708. mergeChildrenInto(this, from, to, source ? source.children : [], openStart - 1, openEnd - 1);
  709. this.markDirty();
  710. return true;
  711. }
  712. split(from) {
  713. let result = [], off = 0, detachFrom = -1, i = 0;
  714. for (let elt of this.children) {
  715. let end = off + elt.length;
  716. if (end > from)
  717. result.push(off < from ? elt.split(from - off) : elt);
  718. if (detachFrom < 0 && off >= from)
  719. detachFrom = i;
  720. off = end;
  721. i++;
  722. }
  723. let length = this.length - from;
  724. this.length = from;
  725. if (detachFrom > -1) {
  726. this.children.length = detachFrom;
  727. this.markDirty();
  728. }
  729. return new MarkView(this.mark, result, length);
  730. }
  731. domAtPos(pos) {
  732. return inlineDOMAtPos(this.dom, this.children, pos);
  733. }
  734. coordsAt(pos, side) {
  735. return coordsInChildren(this, pos, side);
  736. }
  737. }
  738. function textCoords(text, pos, side) {
  739. let length = text.nodeValue.length;
  740. if (pos > length)
  741. pos = length;
  742. let from = pos, to = pos, flatten = 0;
  743. if (pos == 0 && side < 0 || pos == length && side >= 0) {
  744. if (!(browser.chrome || browser.gecko)) { // These browsers reliably return valid rectangles for empty ranges
  745. if (pos) {
  746. from--;
  747. flatten = 1;
  748. } // FIXME this is wrong in RTL text
  749. else if (to < length) {
  750. to++;
  751. flatten = -1;
  752. }
  753. }
  754. }
  755. else {
  756. if (side < 0)
  757. from--;
  758. else if (to < length)
  759. to++;
  760. }
  761. let rects = textRange(text, from, to).getClientRects();
  762. if (!rects.length)
  763. return Rect0;
  764. let rect = rects[(flatten ? flatten < 0 : side >= 0) ? 0 : rects.length - 1];
  765. if (browser.safari && !flatten && rect.width == 0)
  766. rect = Array.prototype.find.call(rects, r => r.width) || rect;
  767. return flatten ? flattenRect(rect, flatten < 0) : rect || null;
  768. }
  769. // Also used for collapsed ranges that don't have a placeholder widget!
  770. class WidgetView extends ContentView {
  771. constructor(widget, length, side) {
  772. super();
  773. this.widget = widget;
  774. this.length = length;
  775. this.side = side;
  776. this.prevWidget = null;
  777. }
  778. static create(widget, length, side) {
  779. return new (widget.customView || WidgetView)(widget, length, side);
  780. }
  781. split(from) {
  782. let result = WidgetView.create(this.widget, this.length - from, this.side);
  783. this.length -= from;
  784. return result;
  785. }
  786. sync() {
  787. if (!this.dom || !this.widget.updateDOM(this.dom)) {
  788. if (this.dom && this.prevWidget)
  789. this.prevWidget.destroy(this.dom);
  790. this.prevWidget = null;
  791. this.setDOM(this.widget.toDOM(this.editorView));
  792. this.dom.contentEditable = "false";
  793. }
  794. }
  795. getSide() { return this.side; }
  796. merge(from, to, source, hasStart, openStart, openEnd) {
  797. if (source && (!(source instanceof WidgetView) || !this.widget.compare(source.widget) ||
  798. from > 0 && openStart <= 0 || to < this.length && openEnd <= 0))
  799. return false;
  800. this.length = from + (source ? source.length : 0) + (this.length - to);
  801. return true;
  802. }
  803. become(other) {
  804. if (other.length == this.length && other instanceof WidgetView && other.side == this.side) {
  805. if (this.widget.constructor == other.widget.constructor) {
  806. if (!this.widget.eq(other.widget))
  807. this.markDirty(true);
  808. if (this.dom && !this.prevWidget)
  809. this.prevWidget = this.widget;
  810. this.widget = other.widget;
  811. return true;
  812. }
  813. }
  814. return false;
  815. }
  816. ignoreMutation() { return true; }
  817. ignoreEvent(event) { return this.widget.ignoreEvent(event); }
  818. get overrideDOMText() {
  819. if (this.length == 0)
  820. return Text.empty;
  821. let top = this;
  822. while (top.parent)
  823. top = top.parent;
  824. let view = top.editorView, text = view && view.state.doc, start = this.posAtStart;
  825. return text ? text.slice(start, start + this.length) : Text.empty;
  826. }
  827. domAtPos(pos) {
  828. return pos == 0 ? DOMPos.before(this.dom) : DOMPos.after(this.dom, pos == this.length);
  829. }
  830. domBoundsAround() { return null; }
  831. coordsAt(pos, side) {
  832. let rects = this.dom.getClientRects(), rect = null;
  833. if (!rects.length)
  834. return Rect0;
  835. for (let i = pos > 0 ? rects.length - 1 : 0;; i += (pos > 0 ? -1 : 1)) {
  836. rect = rects[i];
  837. if (pos > 0 ? i == 0 : i == rects.length - 1 || rect.top < rect.bottom)
  838. break;
  839. }
  840. return (pos == 0 && side > 0 || pos == this.length && side <= 0) ? rect : flattenRect(rect, pos == 0);
  841. }
  842. get isEditable() { return false; }
  843. destroy() {
  844. super.destroy();
  845. if (this.dom)
  846. this.widget.destroy(this.dom);
  847. }
  848. }
  849. class CompositionView extends WidgetView {
  850. domAtPos(pos) {
  851. let { topView, text } = this.widget;
  852. if (!topView)
  853. return new DOMPos(text, Math.min(pos, text.nodeValue.length));
  854. return scanCompositionTree(pos, 0, topView, text, (v, p) => v.domAtPos(p), p => new DOMPos(text, Math.min(p, text.nodeValue.length)));
  855. }
  856. sync() { this.setDOM(this.widget.toDOM()); }
  857. localPosFromDOM(node, offset) {
  858. let { topView, text } = this.widget;
  859. if (!topView)
  860. return Math.min(offset, this.length);
  861. return posFromDOMInCompositionTree(node, offset, topView, text);
  862. }
  863. ignoreMutation() { return false; }
  864. get overrideDOMText() { return null; }
  865. coordsAt(pos, side) {
  866. let { topView, text } = this.widget;
  867. if (!topView)
  868. return textCoords(text, pos, side);
  869. return scanCompositionTree(pos, side, topView, text, (v, pos, side) => v.coordsAt(pos, side), (pos, side) => textCoords(text, pos, side));
  870. }
  871. destroy() {
  872. var _a;
  873. super.destroy();
  874. (_a = this.widget.topView) === null || _a === void 0 ? void 0 : _a.destroy();
  875. }
  876. get isEditable() { return true; }
  877. }
  878. // Uses the old structure of a chunk of content view frozen for
  879. // composition to try and find a reasonable DOM location for the given
  880. // offset.
  881. function scanCompositionTree(pos, side, view, text, enterView, fromText) {
  882. if (view instanceof MarkView) {
  883. for (let child of view.children) {
  884. let hasComp = contains(child.dom, text);
  885. let len = hasComp ? text.nodeValue.length : child.length;
  886. if (pos < len || pos == len && child.getSide() <= 0)
  887. return hasComp ? scanCompositionTree(pos, side, child, text, enterView, fromText) : enterView(child, pos, side);
  888. pos -= len;
  889. }
  890. return enterView(view, view.length, -1);
  891. }
  892. else if (view.dom == text) {
  893. return fromText(pos, side);
  894. }
  895. else {
  896. return enterView(view, pos, side);
  897. }
  898. }
  899. function posFromDOMInCompositionTree(node, offset, view, text) {
  900. if (view instanceof MarkView) {
  901. for (let child of view.children) {
  902. let pos = 0, hasComp = contains(child.dom, text);
  903. if (contains(child.dom, node))
  904. return pos + (hasComp ? posFromDOMInCompositionTree(node, offset, child, text) : child.localPosFromDOM(node, offset));
  905. pos += hasComp ? text.nodeValue.length : child.length;
  906. }
  907. }
  908. else if (view.dom == text) {
  909. return Math.min(offset, text.nodeValue.length);
  910. }
  911. return view.localPosFromDOM(node, offset);
  912. }
  913. // These are drawn around uneditable widgets to avoid a number of
  914. // browser bugs that show up when the cursor is directly next to
  915. // uneditable inline content.
  916. class WidgetBufferView extends ContentView {
  917. constructor(side) {
  918. super();
  919. this.side = side;
  920. }
  921. get length() { return 0; }
  922. merge() { return false; }
  923. become(other) {
  924. return other instanceof WidgetBufferView && other.side == this.side;
  925. }
  926. split() { return new WidgetBufferView(this.side); }
  927. sync() {
  928. if (!this.dom) {
  929. let dom = document.createElement("img");
  930. dom.className = "cm-widgetBuffer";
  931. dom.setAttribute("aria-hidden", "true");
  932. this.setDOM(dom);
  933. }
  934. }
  935. getSide() { return this.side; }
  936. domAtPos(pos) { return DOMPos.before(this.dom); }
  937. localPosFromDOM() { return 0; }
  938. domBoundsAround() { return null; }
  939. coordsAt(pos) {
  940. let imgRect = this.dom.getBoundingClientRect();
  941. // Since the <img> height doesn't correspond to text height, try
  942. // to borrow the height from some sibling node.
  943. let siblingRect = inlineSiblingRect(this, this.side > 0 ? -1 : 1);
  944. return siblingRect && siblingRect.top < imgRect.bottom && siblingRect.bottom > imgRect.top
  945. ? { left: imgRect.left, right: imgRect.right, top: siblingRect.top, bottom: siblingRect.bottom } : imgRect;
  946. }
  947. get overrideDOMText() {
  948. return Text.empty;
  949. }
  950. }
  951. TextView.prototype.children = WidgetView.prototype.children = WidgetBufferView.prototype.children = noChildren;
  952. function inlineSiblingRect(view, side) {
  953. let parent = view.parent, index = parent ? parent.children.indexOf(view) : -1;
  954. while (parent && index >= 0) {
  955. if (side < 0 ? index > 0 : index < parent.children.length) {
  956. let next = parent.children[index + side];
  957. if (next instanceof TextView) {
  958. let nextRect = next.coordsAt(side < 0 ? next.length : 0, side);
  959. if (nextRect)
  960. return nextRect;
  961. }
  962. index += side;
  963. }
  964. else if (parent instanceof MarkView && parent.parent) {
  965. index = parent.parent.children.indexOf(parent) + (side < 0 ? 0 : 1);
  966. parent = parent.parent;
  967. }
  968. else {
  969. let last = parent.dom.lastChild;
  970. if (last && last.nodeName == "BR")
  971. return last.getClientRects()[0];
  972. break;
  973. }
  974. }
  975. return undefined;
  976. }
  977. function inlineDOMAtPos(dom, children, pos) {
  978. let i = 0;
  979. for (let off = 0; i < children.length; i++) {
  980. let child = children[i], end = off + child.length;
  981. if (end == off && child.getSide() <= 0)
  982. continue;
  983. if (pos > off && pos < end && child.dom.parentNode == dom)
  984. return child.domAtPos(pos - off);
  985. if (pos <= off)
  986. break;
  987. off = end;
  988. }
  989. for (; i > 0; i--) {
  990. let before = children[i - 1].dom;
  991. if (before.parentNode == dom)
  992. return DOMPos.after(before);
  993. }
  994. return new DOMPos(dom, 0);
  995. }
  996. // Assumes `view`, if a mark view, has precisely 1 child.
  997. function joinInlineInto(parent, view, open) {
  998. let last, { children } = parent;
  999. if (open > 0 && view instanceof MarkView && children.length &&
  1000. (last = children[children.length - 1]) instanceof MarkView && last.mark.eq(view.mark)) {
  1001. joinInlineInto(last, view.children[0], open - 1);
  1002. }
  1003. else {
  1004. children.push(view);
  1005. view.setParent(parent);
  1006. }
  1007. parent.length += view.length;
  1008. }
  1009. function coordsInChildren(view, pos, side) {
  1010. for (let off = 0, i = 0; i < view.children.length; i++) {
  1011. let child = view.children[i], end = off + child.length, next;
  1012. if ((side <= 0 || end == view.length || child.getSide() > 0 ? end >= pos : end > pos) &&
  1013. (pos < end || i + 1 == view.children.length || (next = view.children[i + 1]).length || next.getSide() > 0)) {
  1014. let flatten = 0;
  1015. if (end == off) {
  1016. if (child.getSide() <= 0)
  1017. continue;
  1018. flatten = side = -child.getSide();
  1019. }
  1020. let rect = child.coordsAt(Math.max(0, pos - off), side);
  1021. return flatten && rect ? flattenRect(rect, side < 0) : rect;
  1022. }
  1023. off = end;
  1024. }
  1025. let last = view.dom.lastChild;
  1026. if (!last)
  1027. return view.dom.getBoundingClientRect();
  1028. let rects = clientRectsFor(last);
  1029. return rects[rects.length - 1] || null;
  1030. }
  1031. function combineAttrs(source, target) {
  1032. for (let name in source) {
  1033. if (name == "class" && target.class)
  1034. target.class += " " + source.class;
  1035. else if (name == "style" && target.style)
  1036. target.style += ";" + source.style;
  1037. else
  1038. target[name] = source[name];
  1039. }
  1040. return target;
  1041. }
  1042. function attrsEq(a, b) {
  1043. if (a == b)
  1044. return true;
  1045. if (!a || !b)
  1046. return false;
  1047. let keysA = Object.keys(a), keysB = Object.keys(b);
  1048. if (keysA.length != keysB.length)
  1049. return false;
  1050. for (let key of keysA) {
  1051. if (keysB.indexOf(key) == -1 || a[key] !== b[key])
  1052. return false;
  1053. }
  1054. return true;
  1055. }
  1056. function updateAttrs(dom, prev, attrs) {
  1057. let changed = null;
  1058. if (prev)
  1059. for (let name in prev)
  1060. if (!(attrs && name in attrs))
  1061. dom.removeAttribute(changed = name);
  1062. if (attrs)
  1063. for (let name in attrs)
  1064. if (!(prev && prev[name] == attrs[name]))
  1065. dom.setAttribute(changed = name, attrs[name]);
  1066. return !!changed;
  1067. }
  1068. /**
  1069. Widgets added to the content are described by subclasses of this
  1070. class. Using a description object like that makes it possible to
  1071. delay creating of the DOM structure for a widget until it is
  1072. needed, and to avoid redrawing widgets even if the decorations
  1073. that define them are recreated.
  1074. */
  1075. class WidgetType {
  1076. /**
  1077. Compare this instance to another instance of the same type.
  1078. (TypeScript can't express this, but only instances of the same
  1079. specific class will be passed to this method.) This is used to
  1080. avoid redrawing widgets when they are replaced by a new
  1081. decoration of the same type. The default implementation just
  1082. returns `false`, which will cause new instances of the widget to
  1083. always be redrawn.
  1084. */
  1085. eq(widget) { return false; }
  1086. /**
  1087. Update a DOM element created by a widget of the same type (but
  1088. different, non-`eq` content) to reflect this widget. May return
  1089. true to indicate that it could update, false to indicate it
  1090. couldn't (in which case the widget will be redrawn). The default
  1091. implementation just returns false.
  1092. */
  1093. updateDOM(dom) { return false; }
  1094. /**
  1095. @internal
  1096. */
  1097. compare(other) {
  1098. return this == other || this.constructor == other.constructor && this.eq(other);
  1099. }
  1100. /**
  1101. The estimated height this widget will have, to be used when
  1102. estimating the height of content that hasn't been drawn. May
  1103. return -1 to indicate you don't know. The default implementation
  1104. returns -1.
  1105. */
  1106. get estimatedHeight() { return -1; }
  1107. /**
  1108. Can be used to configure which kinds of events inside the widget
  1109. should be ignored by the editor. The default is to ignore all
  1110. events.
  1111. */
  1112. ignoreEvent(event) { return true; }
  1113. /**
  1114. @internal
  1115. */
  1116. get customView() { return null; }
  1117. /**
  1118. This is called when the an instance of the widget is removed
  1119. from the editor view.
  1120. */
  1121. destroy(dom) { }
  1122. }
  1123. /**
  1124. The different types of blocks that can occur in an editor view.
  1125. */
  1126. var BlockType = /*@__PURE__*/(function (BlockType) {
  1127. /**
  1128. A line of text.
  1129. */
  1130. BlockType[BlockType["Text"] = 0] = "Text";
  1131. /**
  1132. A block widget associated with the position after it.
  1133. */
  1134. BlockType[BlockType["WidgetBefore"] = 1] = "WidgetBefore";
  1135. /**
  1136. A block widget associated with the position before it.
  1137. */
  1138. BlockType[BlockType["WidgetAfter"] = 2] = "WidgetAfter";
  1139. /**
  1140. A block widget [replacing](https://codemirror.net/6/docs/ref/#view.Decoration^replace) a range of content.
  1141. */
  1142. BlockType[BlockType["WidgetRange"] = 3] = "WidgetRange";
  1143. return BlockType})(BlockType || (BlockType = {}));
  1144. /**
  1145. A decoration provides information on how to draw or style a piece
  1146. of content. You'll usually use it wrapped in a
  1147. [`Range`](https://codemirror.net/6/docs/ref/#state.Range), which adds a start and end position.
  1148. @nonabstract
  1149. */
  1150. class Decoration extends RangeValue {
  1151. constructor(
  1152. /**
  1153. @internal
  1154. */
  1155. startSide,
  1156. /**
  1157. @internal
  1158. */
  1159. endSide,
  1160. /**
  1161. @internal
  1162. */
  1163. widget,
  1164. /**
  1165. The config object used to create this decoration. You can
  1166. include additional properties in there to store metadata about
  1167. your decoration.
  1168. */
  1169. spec) {
  1170. super();
  1171. this.startSide = startSide;
  1172. this.endSide = endSide;
  1173. this.widget = widget;
  1174. this.spec = spec;
  1175. }
  1176. /**
  1177. @internal
  1178. */
  1179. get heightRelevant() { return false; }
  1180. /**
  1181. Create a mark decoration, which influences the styling of the
  1182. content in its range. Nested mark decorations will cause nested
  1183. DOM elements to be created. Nesting order is determined by
  1184. precedence of the [facet](https://codemirror.net/6/docs/ref/#view.EditorView^decorations), with
  1185. the higher-precedence decorations creating the inner DOM nodes.
  1186. Such elements are split on line boundaries and on the boundaries
  1187. of lower-precedence decorations.
  1188. */
  1189. static mark(spec) {
  1190. return new MarkDecoration(spec);
  1191. }
  1192. /**
  1193. Create a widget decoration, which displays a DOM element at the
  1194. given position.
  1195. */
  1196. static widget(spec) {
  1197. let side = spec.side || 0, block = !!spec.block;
  1198. side += block ? (side > 0 ? 300000000 /* BlockAfter */ : -400000000 /* BlockBefore */) : (side > 0 ? 100000000 /* InlineAfter */ : -100000000 /* InlineBefore */);
  1199. return new PointDecoration(spec, side, side, block, spec.widget || null, false);
  1200. }
  1201. /**
  1202. Create a replace decoration which replaces the given range with
  1203. a widget, or simply hides it.
  1204. */
  1205. static replace(spec) {
  1206. let block = !!spec.block, startSide, endSide;
  1207. if (spec.isBlockGap) {
  1208. startSide = -500000000 /* GapStart */;
  1209. endSide = 400000000 /* GapEnd */;
  1210. }
  1211. else {
  1212. let { start, end } = getInclusive(spec, block);
  1213. startSide = (start ? (block ? -300000000 /* BlockIncStart */ : -1 /* InlineIncStart */) : 500000000 /* NonIncStart */) - 1;
  1214. endSide = (end ? (block ? 200000000 /* BlockIncEnd */ : 1 /* InlineIncEnd */) : -600000000 /* NonIncEnd */) + 1;
  1215. }
  1216. return new PointDecoration(spec, startSide, endSide, block, spec.widget || null, true);
  1217. }
  1218. /**
  1219. Create a line decoration, which can add DOM attributes to the
  1220. line starting at the given position.
  1221. */
  1222. static line(spec) {
  1223. return new LineDecoration(spec);
  1224. }
  1225. /**
  1226. Build a [`DecorationSet`](https://codemirror.net/6/docs/ref/#view.DecorationSet) from the given
  1227. decorated range or ranges. If the ranges aren't already sorted,
  1228. pass `true` for `sort` to make the library sort them for you.
  1229. */
  1230. static set(of, sort = false) {
  1231. return RangeSet.of(of, sort);
  1232. }
  1233. /**
  1234. @internal
  1235. */
  1236. hasHeight() { return this.widget ? this.widget.estimatedHeight > -1 : false; }
  1237. }
  1238. /**
  1239. The empty set of decorations.
  1240. */
  1241. Decoration.none = RangeSet.empty;
  1242. class MarkDecoration extends Decoration {
  1243. constructor(spec) {
  1244. let { start, end } = getInclusive(spec);
  1245. super(start ? -1 /* InlineIncStart */ : 500000000 /* NonIncStart */, end ? 1 /* InlineIncEnd */ : -600000000 /* NonIncEnd */, null, spec);
  1246. this.tagName = spec.tagName || "span";
  1247. this.class = spec.class || "";
  1248. this.attrs = spec.attributes || null;
  1249. }
  1250. eq(other) {
  1251. return this == other ||
  1252. other instanceof MarkDecoration &&
  1253. this.tagName == other.tagName &&
  1254. this.class == other.class &&
  1255. attrsEq(this.attrs, other.attrs);
  1256. }
  1257. range(from, to = from) {
  1258. if (from >= to)
  1259. throw new RangeError("Mark decorations may not be empty");
  1260. return super.range(from, to);
  1261. }
  1262. }
  1263. MarkDecoration.prototype.point = false;
  1264. class LineDecoration extends Decoration {
  1265. constructor(spec) {
  1266. super(-200000000 /* Line */, -200000000 /* Line */, null, spec);
  1267. }
  1268. eq(other) {
  1269. return other instanceof LineDecoration && attrsEq(this.spec.attributes, other.spec.attributes);
  1270. }
  1271. range(from, to = from) {
  1272. if (to != from)
  1273. throw new RangeError("Line decoration ranges must be zero-length");
  1274. return super.range(from, to);
  1275. }
  1276. }
  1277. LineDecoration.prototype.mapMode = MapMode.TrackBefore;
  1278. LineDecoration.prototype.point = true;
  1279. class PointDecoration extends Decoration {
  1280. constructor(spec, startSide, endSide, block, widget, isReplace) {
  1281. super(startSide, endSide, widget, spec);
  1282. this.block = block;
  1283. this.isReplace = isReplace;
  1284. this.mapMode = !block ? MapMode.TrackDel : startSide <= 0 ? MapMode.TrackBefore : MapMode.TrackAfter;
  1285. }
  1286. // Only relevant when this.block == true
  1287. get type() {
  1288. return this.startSide < this.endSide ? BlockType.WidgetRange
  1289. : this.startSide <= 0 ? BlockType.WidgetBefore : BlockType.WidgetAfter;
  1290. }
  1291. get heightRelevant() { return this.block || !!this.widget && this.widget.estimatedHeight >= 5; }
  1292. eq(other) {
  1293. return other instanceof PointDecoration &&
  1294. widgetsEq(this.widget, other.widget) &&
  1295. this.block == other.block &&
  1296. this.startSide == other.startSide && this.endSide == other.endSide;
  1297. }
  1298. range(from, to = from) {
  1299. if (this.isReplace && (from > to || (from == to && this.startSide > 0 && this.endSide <= 0)))
  1300. throw new RangeError("Invalid range for replacement decoration");
  1301. if (!this.isReplace && to != from)
  1302. throw new RangeError("Widget decorations can only have zero-length ranges");
  1303. return super.range(from, to);
  1304. }
  1305. }
  1306. PointDecoration.prototype.point = true;
  1307. function getInclusive(spec, block = false) {
  1308. let { inclusiveStart: start, inclusiveEnd: end } = spec;
  1309. if (start == null)
  1310. start = spec.inclusive;
  1311. if (end == null)
  1312. end = spec.inclusive;
  1313. return { start: start !== null && start !== void 0 ? start : block, end: end !== null && end !== void 0 ? end : block };
  1314. }
  1315. function widgetsEq(a, b) {
  1316. return a == b || !!(a && b && a.compare(b));
  1317. }
  1318. function addRange(from, to, ranges, margin = 0) {
  1319. let last = ranges.length - 1;
  1320. if (last >= 0 && ranges[last] + margin >= from)
  1321. ranges[last] = Math.max(ranges[last], to);
  1322. else
  1323. ranges.push(from, to);
  1324. }
  1325. class LineView extends ContentView {
  1326. constructor() {
  1327. super(...arguments);
  1328. this.children = [];
  1329. this.length = 0;
  1330. this.prevAttrs = undefined;
  1331. this.attrs = null;
  1332. this.breakAfter = 0;
  1333. }
  1334. // Consumes source
  1335. merge(from, to, source, hasStart, openStart, openEnd) {
  1336. if (source) {
  1337. if (!(source instanceof LineView))
  1338. return false;
  1339. if (!this.dom)
  1340. source.transferDOM(this); // Reuse source.dom when appropriate
  1341. }
  1342. if (hasStart)
  1343. this.setDeco(source ? source.attrs : null);
  1344. mergeChildrenInto(this, from, to, source ? source.children : [], openStart, openEnd);
  1345. return true;
  1346. }
  1347. split(at) {
  1348. let end = new LineView;
  1349. end.breakAfter = this.breakAfter;
  1350. if (this.length == 0)
  1351. return end;
  1352. let { i, off } = this.childPos(at);
  1353. if (off) {
  1354. end.append(this.children[i].split(off), 0);
  1355. this.children[i].merge(off, this.children[i].length, null, false, 0, 0);
  1356. i++;
  1357. }
  1358. for (let j = i; j < this.children.length; j++)
  1359. end.append(this.children[j], 0);
  1360. while (i > 0 && this.children[i - 1].length == 0)
  1361. this.children[--i].destroy();
  1362. this.children.length = i;
  1363. this.markDirty();
  1364. this.length = at;
  1365. return end;
  1366. }
  1367. transferDOM(other) {
  1368. if (!this.dom)
  1369. return;
  1370. this.markDirty();
  1371. other.setDOM(this.dom);
  1372. other.prevAttrs = this.prevAttrs === undefined ? this.attrs : this.prevAttrs;
  1373. this.prevAttrs = undefined;
  1374. this.dom = null;
  1375. }
  1376. setDeco(attrs) {
  1377. if (!attrsEq(this.attrs, attrs)) {
  1378. if (this.dom) {
  1379. this.prevAttrs = this.attrs;
  1380. this.markDirty();
  1381. }
  1382. this.attrs = attrs;
  1383. }
  1384. }
  1385. append(child, openStart) {
  1386. joinInlineInto(this, child, openStart);
  1387. }
  1388. // Only called when building a line view in ContentBuilder
  1389. addLineDeco(deco) {
  1390. let attrs = deco.spec.attributes, cls = deco.spec.class;
  1391. if (attrs)
  1392. this.attrs = combineAttrs(attrs, this.attrs || {});
  1393. if (cls)
  1394. this.attrs = combineAttrs({ class: cls }, this.attrs || {});
  1395. }
  1396. domAtPos(pos) {
  1397. return inlineDOMAtPos(this.dom, this.children, pos);
  1398. }
  1399. reuseDOM(node) {
  1400. if (node.nodeName == "DIV") {
  1401. this.setDOM(node);
  1402. this.dirty |= 4 /* Attrs */ | 2 /* Node */;
  1403. }
  1404. }
  1405. sync(track) {
  1406. var _a;
  1407. if (!this.dom) {
  1408. this.setDOM(document.createElement("div"));
  1409. this.dom.className = "cm-line";
  1410. this.prevAttrs = this.attrs ? null : undefined;
  1411. }
  1412. else if (this.dirty & 4 /* Attrs */) {
  1413. clearAttributes(this.dom);
  1414. this.dom.className = "cm-line";
  1415. this.prevAttrs = this.attrs ? null : undefined;
  1416. }
  1417. if (this.prevAttrs !== undefined) {
  1418. updateAttrs(this.dom, this.prevAttrs, this.attrs);
  1419. this.dom.classList.add("cm-line");
  1420. this.prevAttrs = undefined;
  1421. }
  1422. super.sync(track);
  1423. let last = this.dom.lastChild;
  1424. while (last && ContentView.get(last) instanceof MarkView)
  1425. last = last.lastChild;
  1426. if (!last || !this.length ||
  1427. last.nodeName != "BR" && ((_a = ContentView.get(last)) === null || _a === void 0 ? void 0 : _a.isEditable) == false &&
  1428. (!browser.ios || !this.children.some(ch => ch instanceof TextView))) {
  1429. let hack = document.createElement("BR");
  1430. hack.cmIgnore = true;
  1431. this.dom.appendChild(hack);
  1432. }
  1433. }
  1434. measureTextSize() {
  1435. if (this.children.length == 0 || this.length > 20)
  1436. return null;
  1437. let totalWidth = 0;
  1438. for (let child of this.children) {
  1439. if (!(child instanceof TextView))
  1440. return null;
  1441. let rects = clientRectsFor(child.dom);
  1442. if (rects.length != 1)
  1443. return null;
  1444. totalWidth += rects[0].width;
  1445. }
  1446. return { lineHeight: this.dom.getBoundingClientRect().height,
  1447. charWidth: totalWidth / this.length };
  1448. }
  1449. coordsAt(pos, side) {
  1450. return coordsInChildren(this, pos, side);
  1451. }
  1452. become(_other) { return false; }
  1453. get type() { return BlockType.Text; }
  1454. static find(docView, pos) {
  1455. for (let i = 0, off = 0; i < docView.children.length; i++) {
  1456. let block = docView.children[i], end = off + block.length;
  1457. if (end >= pos) {
  1458. if (block instanceof LineView)
  1459. return block;
  1460. if (end > pos)
  1461. break;
  1462. }
  1463. off = end + block.breakAfter;
  1464. }
  1465. return null;
  1466. }
  1467. }
  1468. class BlockWidgetView extends ContentView {
  1469. constructor(widget, length, type) {
  1470. super();
  1471. this.widget = widget;
  1472. this.length = length;
  1473. this.type = type;
  1474. this.breakAfter = 0;
  1475. this.prevWidget = null;
  1476. }
  1477. merge(from, to, source, _takeDeco, openStart, openEnd) {
  1478. if (source && (!(source instanceof BlockWidgetView) || !this.widget.compare(source.widget) ||
  1479. from > 0 && openStart <= 0 || to < this.length && openEnd <= 0))
  1480. return false;
  1481. this.length = from + (source ? source.length : 0) + (this.length - to);
  1482. return true;
  1483. }
  1484. domAtPos(pos) {
  1485. return pos == 0 ? DOMPos.before(this.dom) : DOMPos.after(this.dom, pos == this.length);
  1486. }
  1487. split(at) {
  1488. let len = this.length - at;
  1489. this.length = at;
  1490. let end = new BlockWidgetView(this.widget, len, this.type);
  1491. end.breakAfter = this.breakAfter;
  1492. return end;
  1493. }
  1494. get children() { return noChildren; }
  1495. sync() {
  1496. if (!this.dom || !this.widget.updateDOM(this.dom)) {
  1497. if (this.dom && this.prevWidget)
  1498. this.prevWidget.destroy(this.dom);
  1499. this.prevWidget = null;
  1500. this.setDOM(this.widget.toDOM(this.editorView));
  1501. this.dom.contentEditable = "false";
  1502. }
  1503. }
  1504. get overrideDOMText() {
  1505. return this.parent ? this.parent.view.state.doc.slice(this.posAtStart, this.posAtEnd) : Text.empty;
  1506. }
  1507. domBoundsAround() { return null; }
  1508. become(other) {
  1509. if (other instanceof BlockWidgetView && other.type == this.type &&
  1510. other.widget.constructor == this.widget.constructor) {
  1511. if (!other.widget.eq(this.widget))
  1512. this.markDirty(true);
  1513. if (this.dom && !this.prevWidget)
  1514. this.prevWidget = this.widget;
  1515. this.widget = other.widget;
  1516. this.length = other.length;
  1517. this.breakAfter = other.breakAfter;
  1518. return true;
  1519. }
  1520. return false;
  1521. }
  1522. ignoreMutation() { return true; }
  1523. ignoreEvent(event) { return this.widget.ignoreEvent(event); }
  1524. destroy() {
  1525. super.destroy();
  1526. if (this.dom)
  1527. this.widget.destroy(this.dom);
  1528. }
  1529. }
  1530. class ContentBuilder {
  1531. constructor(doc, pos, end, disallowBlockEffectsFor) {
  1532. this.doc = doc;
  1533. this.pos = pos;
  1534. this.end = end;
  1535. this.disallowBlockEffectsFor = disallowBlockEffectsFor;
  1536. this.content = [];
  1537. this.curLine = null;
  1538. this.breakAtStart = 0;
  1539. this.pendingBuffer = 0 /* No */;
  1540. // Set to false directly after a widget that covers the position after it
  1541. this.atCursorPos = true;
  1542. this.openStart = -1;
  1543. this.openEnd = -1;
  1544. this.text = "";
  1545. this.textOff = 0;
  1546. this.cursor = doc.iter();
  1547. this.skip = pos;
  1548. }
  1549. posCovered() {
  1550. if (this.content.length == 0)
  1551. return !this.breakAtStart && this.doc.lineAt(this.pos).from != this.pos;
  1552. let last = this.content[this.content.length - 1];
  1553. return !last.breakAfter && !(last instanceof BlockWidgetView && last.type == BlockType.WidgetBefore);
  1554. }
  1555. getLine() {
  1556. if (!this.curLine) {
  1557. this.content.push(this.curLine = new LineView);
  1558. this.atCursorPos = true;
  1559. }
  1560. return this.curLine;
  1561. }
  1562. flushBuffer(active) {
  1563. if (this.pendingBuffer) {
  1564. this.curLine.append(wrapMarks(new WidgetBufferView(-1), active), active.length);
  1565. this.pendingBuffer = 0 /* No */;
  1566. }
  1567. }
  1568. addBlockWidget(view) {
  1569. this.flushBuffer([]);
  1570. this.curLine = null;
  1571. this.content.push(view);
  1572. }
  1573. finish(openEnd) {
  1574. if (!openEnd)
  1575. this.flushBuffer([]);
  1576. else
  1577. this.pendingBuffer = 0 /* No */;
  1578. if (!this.posCovered())
  1579. this.getLine();
  1580. }
  1581. buildText(length, active, openStart) {
  1582. while (length > 0) {
  1583. if (this.textOff == this.text.length) {
  1584. let { value, lineBreak, done } = this.cursor.next(this.skip);
  1585. this.skip = 0;
  1586. if (done)
  1587. throw new Error("Ran out of text content when drawing inline views");
  1588. if (lineBreak) {
  1589. if (!this.posCovered())
  1590. this.getLine();
  1591. if (this.content.length)
  1592. this.content[this.content.length - 1].breakAfter = 1;
  1593. else
  1594. this.breakAtStart = 1;
  1595. this.flushBuffer([]);
  1596. this.curLine = null;
  1597. length--;
  1598. continue;
  1599. }
  1600. else {
  1601. this.text = value;
  1602. this.textOff = 0;
  1603. }
  1604. }
  1605. let take = Math.min(this.text.length - this.textOff, length, 512 /* Chunk */);
  1606. this.flushBuffer(active.slice(0, openStart));
  1607. this.getLine().append(wrapMarks(new TextView(this.text.slice(this.textOff, this.textOff + take)), active), openStart);
  1608. this.atCursorPos = true;
  1609. this.textOff += take;
  1610. length -= take;
  1611. openStart = 0;
  1612. }
  1613. }
  1614. span(from, to, active, openStart) {
  1615. this.buildText(to - from, active, openStart);
  1616. this.pos = to;
  1617. if (this.openStart < 0)
  1618. this.openStart = openStart;
  1619. }
  1620. point(from, to, deco, active, openStart, index) {
  1621. if (this.disallowBlockEffectsFor[index] && deco instanceof PointDecoration) {
  1622. if (deco.block)
  1623. throw new RangeError("Block decorations may not be specified via plugins");
  1624. if (to > this.doc.lineAt(this.pos).to)
  1625. throw new RangeError("Decorations that replace line breaks may not be specified via plugins");
  1626. }
  1627. let len = to - from;
  1628. if (deco instanceof PointDecoration) {
  1629. if (deco.block) {
  1630. let { type } = deco;
  1631. if (type == BlockType.WidgetAfter && !this.posCovered())
  1632. this.getLine();
  1633. this.addBlockWidget(new BlockWidgetView(deco.widget || new NullWidget("div"), len, type));
  1634. }
  1635. else {
  1636. let view = WidgetView.create(deco.widget || new NullWidget("span"), len, deco.startSide);
  1637. let cursorBefore = this.atCursorPos && !view.isEditable && openStart <= active.length && (from < to || deco.startSide > 0);
  1638. let cursorAfter = !view.isEditable && (from < to || deco.startSide <= 0);
  1639. let line = this.getLine();
  1640. if (this.pendingBuffer == 2 /* IfCursor */ && !cursorBefore)
  1641. this.pendingBuffer = 0 /* No */;
  1642. this.flushBuffer(active);
  1643. if (cursorBefore) {
  1644. line.append(wrapMarks(new WidgetBufferView(1), active), openStart);
  1645. openStart = active.length + Math.max(0, openStart - active.length);
  1646. }
  1647. line.append(wrapMarks(view, active), openStart);
  1648. this.atCursorPos = cursorAfter;
  1649. this.pendingBuffer = !cursorAfter ? 0 /* No */ : from < to ? 1 /* Yes */ : 2 /* IfCursor */;
  1650. }
  1651. }
  1652. else if (this.doc.lineAt(this.pos).from == this.pos) { // Line decoration
  1653. this.getLine().addLineDeco(deco);
  1654. }
  1655. if (len) {
  1656. // Advance the iterator past the replaced content
  1657. if (this.textOff + len <= this.text.length) {
  1658. this.textOff += len;
  1659. }
  1660. else {
  1661. this.skip += len - (this.text.length - this.textOff);
  1662. this.text = "";
  1663. this.textOff = 0;
  1664. }
  1665. this.pos = to;
  1666. }
  1667. if (this.openStart < 0)
  1668. this.openStart = openStart;
  1669. }
  1670. static build(text, from, to, decorations, dynamicDecorationMap) {
  1671. let builder = new ContentBuilder(text, from, to, dynamicDecorationMap);
  1672. builder.openEnd = RangeSet.spans(decorations, from, to, builder);
  1673. if (builder.openStart < 0)
  1674. builder.openStart = builder.openEnd;
  1675. builder.finish(builder.openEnd);
  1676. return builder;
  1677. }
  1678. }
  1679. function wrapMarks(view, active) {
  1680. for (let mark of active)
  1681. view = new MarkView(mark, [view], view.length);
  1682. return view;
  1683. }
  1684. class NullWidget extends WidgetType {
  1685. constructor(tag) {
  1686. super();
  1687. this.tag = tag;
  1688. }
  1689. eq(other) { return other.tag == this.tag; }
  1690. toDOM() { return document.createElement(this.tag); }
  1691. updateDOM(elt) { return elt.nodeName.toLowerCase() == this.tag; }
  1692. }
  1693. const clickAddsSelectionRange = /*@__PURE__*/Facet.define();
  1694. const dragMovesSelection$1 = /*@__PURE__*/Facet.define();
  1695. const mouseSelectionStyle = /*@__PURE__*/Facet.define();
  1696. const exceptionSink = /*@__PURE__*/Facet.define();
  1697. const updateListener = /*@__PURE__*/Facet.define();
  1698. const inputHandler = /*@__PURE__*/Facet.define();
  1699. const perLineTextDirection = /*@__PURE__*/Facet.define({
  1700. combine: values => values.some(x => x)
  1701. });
  1702. class ScrollTarget {
  1703. constructor(range, y = "nearest", x = "nearest", yMargin = 5, xMargin = 5) {
  1704. this.range = range;
  1705. this.y = y;
  1706. this.x = x;
  1707. this.yMargin = yMargin;
  1708. this.xMargin = xMargin;
  1709. }
  1710. map(changes) {
  1711. return changes.empty ? this : new ScrollTarget(this.range.map(changes), this.y, this.x, this.yMargin, this.xMargin);
  1712. }
  1713. }
  1714. const scrollIntoView = /*@__PURE__*/StateEffect.define({ map: (t, ch) => t.map(ch) });
  1715. /**
  1716. Log or report an unhandled exception in client code. Should
  1717. probably only be used by extension code that allows client code to
  1718. provide functions, and calls those functions in a context where an
  1719. exception can't be propagated to calling code in a reasonable way
  1720. (for example when in an event handler).
  1721. Either calls a handler registered with
  1722. [`EditorView.exceptionSink`](https://codemirror.net/6/docs/ref/#view.EditorView^exceptionSink),
  1723. `window.onerror`, if defined, or `console.error` (in which case
  1724. it'll pass `context`, when given, as first argument).
  1725. */
  1726. function logException(state, exception, context) {
  1727. let handler = state.facet(exceptionSink);
  1728. if (handler.length)
  1729. handler[0](exception);
  1730. else if (window.onerror)
  1731. window.onerror(String(exception), context, undefined, undefined, exception);
  1732. else if (context)
  1733. console.error(context + ":", exception);
  1734. else
  1735. console.error(exception);
  1736. }
  1737. const editable = /*@__PURE__*/Facet.define({ combine: values => values.length ? values[0] : true });
  1738. let nextPluginID = 0;
  1739. const viewPlugin = /*@__PURE__*/Facet.define();
  1740. /**
  1741. View plugins associate stateful values with a view. They can
  1742. influence the way the content is drawn, and are notified of things
  1743. that happen in the view.
  1744. */
  1745. class ViewPlugin {
  1746. constructor(
  1747. /**
  1748. @internal
  1749. */
  1750. id,
  1751. /**
  1752. @internal
  1753. */
  1754. create,
  1755. /**
  1756. @internal
  1757. */
  1758. domEventHandlers, buildExtensions) {
  1759. this.id = id;
  1760. this.create = create;
  1761. this.domEventHandlers = domEventHandlers;
  1762. this.extension = buildExtensions(this);
  1763. }
  1764. /**
  1765. Define a plugin from a constructor function that creates the
  1766. plugin's value, given an editor view.
  1767. */
  1768. static define(create, spec) {
  1769. const { eventHandlers, provide, decorations: deco } = spec || {};
  1770. return new ViewPlugin(nextPluginID++, create, eventHandlers, plugin => {
  1771. let ext = [viewPlugin.of(plugin)];
  1772. if (deco)
  1773. ext.push(decorations.of(view => {
  1774. let pluginInst = view.plugin(plugin);
  1775. return pluginInst ? deco(pluginInst) : Decoration.none;
  1776. }));
  1777. if (provide)
  1778. ext.push(provide(plugin));
  1779. return ext;
  1780. });
  1781. }
  1782. /**
  1783. Create a plugin for a class whose constructor takes a single
  1784. editor view as argument.
  1785. */
  1786. static fromClass(cls, spec) {
  1787. return ViewPlugin.define(view => new cls(view), spec);
  1788. }
  1789. }
  1790. class PluginInstance {
  1791. constructor(spec) {
  1792. this.spec = spec;
  1793. // When starting an update, all plugins have this field set to the
  1794. // update object, indicating they need to be updated. When finished
  1795. // updating, it is set to `false`. Retrieving a plugin that needs to
  1796. // be updated with `view.plugin` forces an eager update.
  1797. this.mustUpdate = null;
  1798. // This is null when the plugin is initially created, but
  1799. // initialized on the first update.
  1800. this.value = null;
  1801. }
  1802. update(view) {
  1803. if (!this.value) {
  1804. if (this.spec) {
  1805. try {
  1806. this.value = this.spec.create(view);
  1807. }
  1808. catch (e) {
  1809. logException(view.state, e, "CodeMirror plugin crashed");
  1810. this.deactivate();
  1811. }
  1812. }
  1813. }
  1814. else if (this.mustUpdate) {
  1815. let update = this.mustUpdate;
  1816. this.mustUpdate = null;
  1817. if (this.value.update) {
  1818. try {
  1819. this.value.update(update);
  1820. }
  1821. catch (e) {
  1822. logException(update.state, e, "CodeMirror plugin crashed");
  1823. if (this.value.destroy)
  1824. try {
  1825. this.value.destroy();
  1826. }
  1827. catch (_) { }
  1828. this.deactivate();
  1829. }
  1830. }
  1831. }
  1832. return this;
  1833. }
  1834. destroy(view) {
  1835. var _a;
  1836. if ((_a = this.value) === null || _a === void 0 ? void 0 : _a.destroy) {
  1837. try {
  1838. this.value.destroy();
  1839. }
  1840. catch (e) {
  1841. logException(view.state, e, "CodeMirror plugin crashed");
  1842. }
  1843. }
  1844. }
  1845. deactivate() {
  1846. this.spec = this.value = null;
  1847. }
  1848. }
  1849. const editorAttributes = /*@__PURE__*/Facet.define();
  1850. const contentAttributes = /*@__PURE__*/Facet.define();
  1851. // Provide decorations
  1852. const decorations = /*@__PURE__*/Facet.define();
  1853. const atomicRanges = /*@__PURE__*/Facet.define();
  1854. const scrollMargins = /*@__PURE__*/Facet.define();
  1855. const styleModule = /*@__PURE__*/Facet.define();
  1856. class ChangedRange {
  1857. constructor(fromA, toA, fromB, toB) {
  1858. this.fromA = fromA;
  1859. this.toA = toA;
  1860. this.fromB = fromB;
  1861. this.toB = toB;
  1862. }
  1863. join(other) {
  1864. return new ChangedRange(Math.min(this.fromA, other.fromA), Math.max(this.toA, other.toA), Math.min(this.fromB, other.fromB), Math.max(this.toB, other.toB));
  1865. }
  1866. addToSet(set) {
  1867. let i = set.length, me = this;
  1868. for (; i > 0; i--) {
  1869. let range = set[i - 1];
  1870. if (range.fromA > me.toA)
  1871. continue;
  1872. if (range.toA < me.fromA)
  1873. break;
  1874. me = me.join(range);
  1875. set.splice(i - 1, 1);
  1876. }
  1877. set.splice(i, 0, me);
  1878. return set;
  1879. }
  1880. static extendWithRanges(diff, ranges) {
  1881. if (ranges.length == 0)
  1882. return diff;
  1883. let result = [];
  1884. for (let dI = 0, rI = 0, posA = 0, posB = 0;; dI++) {
  1885. let next = dI == diff.length ? null : diff[dI], off = posA - posB;
  1886. let end = next ? next.fromB : 1e9;
  1887. while (rI < ranges.length && ranges[rI] < end) {
  1888. let from = ranges[rI], to = ranges[rI + 1];
  1889. let fromB = Math.max(posB, from), toB = Math.min(end, to);
  1890. if (fromB <= toB)
  1891. new ChangedRange(fromB + off, toB + off, fromB, toB).addToSet(result);
  1892. if (to > end)
  1893. break;
  1894. else
  1895. rI += 2;
  1896. }
  1897. if (!next)
  1898. return result;
  1899. new ChangedRange(next.fromA, next.toA, next.fromB, next.toB).addToSet(result);
  1900. posA = next.toA;
  1901. posB = next.toB;
  1902. }
  1903. }
  1904. }
  1905. /**
  1906. View [plugins](https://codemirror.net/6/docs/ref/#view.ViewPlugin) are given instances of this
  1907. class, which describe what happened, whenever the view is updated.
  1908. */
  1909. class ViewUpdate {
  1910. constructor(
  1911. /**
  1912. The editor view that the update is associated with.
  1913. */
  1914. view,
  1915. /**
  1916. The new editor state.
  1917. */
  1918. state,
  1919. /**
  1920. The transactions involved in the update. May be empty.
  1921. */
  1922. transactions) {
  1923. this.view = view;
  1924. this.state = state;
  1925. this.transactions = transactions;
  1926. /**
  1927. @internal
  1928. */
  1929. this.flags = 0;
  1930. this.startState = view.state;
  1931. this.changes = ChangeSet.empty(this.startState.doc.length);
  1932. for (let tr of transactions)
  1933. this.changes = this.changes.compose(tr.changes);
  1934. let changedRanges = [];
  1935. this.changes.iterChangedRanges((fromA, toA, fromB, toB) => changedRanges.push(new ChangedRange(fromA, toA, fromB, toB)));
  1936. this.changedRanges = changedRanges;
  1937. let focus = view.hasFocus;
  1938. if (focus != view.inputState.notifiedFocused) {
  1939. view.inputState.notifiedFocused = focus;
  1940. this.flags |= 1 /* Focus */;
  1941. }
  1942. }
  1943. /**
  1944. @internal
  1945. */
  1946. static create(view, state, transactions) {
  1947. return new ViewUpdate(view, state, transactions);
  1948. }
  1949. /**
  1950. Tells you whether the [viewport](https://codemirror.net/6/docs/ref/#view.EditorView.viewport) or
  1951. [visible ranges](https://codemirror.net/6/docs/ref/#view.EditorView.visibleRanges) changed in this
  1952. update.
  1953. */
  1954. get viewportChanged() {
  1955. return (this.flags & 4 /* Viewport */) > 0;
  1956. }
  1957. /**
  1958. Indicates whether the height of a block element in the editor
  1959. changed in this update.
  1960. */
  1961. get heightChanged() {
  1962. return (this.flags & 2 /* Height */) > 0;
  1963. }
  1964. /**
  1965. Returns true when the document was modified or the size of the
  1966. editor, or elements within the editor, changed.
  1967. */
  1968. get geometryChanged() {
  1969. return this.docChanged || (this.flags & (8 /* Geometry */ | 2 /* Height */)) > 0;
  1970. }
  1971. /**
  1972. True when this update indicates a focus change.
  1973. */
  1974. get focusChanged() {
  1975. return (this.flags & 1 /* Focus */) > 0;
  1976. }
  1977. /**
  1978. Whether the document changed in this update.
  1979. */
  1980. get docChanged() {
  1981. return !this.changes.empty;
  1982. }
  1983. /**
  1984. Whether the selection was explicitly set in this update.
  1985. */
  1986. get selectionSet() {
  1987. return this.transactions.some(tr => tr.selection);
  1988. }
  1989. /**
  1990. @internal
  1991. */
  1992. get empty() { return this.flags == 0 && this.transactions.length == 0; }
  1993. }
  1994. /**
  1995. Used to indicate [text direction](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection).
  1996. */
  1997. var Direction = /*@__PURE__*/(function (Direction) {
  1998. // (These are chosen to match the base levels, in bidi algorithm
  1999. // terms, of spans in that direction.)
  2000. /**
  2001. Left-to-right.
  2002. */
  2003. Direction[Direction["LTR"] = 0] = "LTR";
  2004. /**
  2005. Right-to-left.
  2006. */
  2007. Direction[Direction["RTL"] = 1] = "RTL";
  2008. return Direction})(Direction || (Direction = {}));
  2009. const LTR = Direction.LTR, RTL = Direction.RTL;
  2010. // Decode a string with each type encoded as log2(type)
  2011. function dec(str) {
  2012. let result = [];
  2013. for (let i = 0; i < str.length; i++)
  2014. result.push(1 << +str[i]);
  2015. return result;
  2016. }
  2017. // Character types for codepoints 0 to 0xf8
  2018. const LowTypes = /*@__PURE__*/dec("88888888888888888888888888888888888666888888787833333333337888888000000000000000000000000008888880000000000000000000000000088888888888888888888888888888888888887866668888088888663380888308888800000000000000000000000800000000000000000000000000000008");
  2019. // Character types for codepoints 0x600 to 0x6f9
  2020. const ArabicTypes = /*@__PURE__*/dec("4444448826627288999999999992222222222222222222222222222222222222222222222229999999999999999999994444444444644222822222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222999999949999999229989999223333333333");
  2021. const Brackets = /*@__PURE__*/Object.create(null), BracketStack = [];
  2022. // There's a lot more in
  2023. // https://www.unicode.org/Public/UCD/latest/ucd/BidiBrackets.txt,
  2024. // which are left out to keep code size down.
  2025. for (let p of ["()", "[]", "{}"]) {
  2026. let l = /*@__PURE__*/p.charCodeAt(0), r = /*@__PURE__*/p.charCodeAt(1);
  2027. Brackets[l] = r;
  2028. Brackets[r] = -l;
  2029. }
  2030. function charType(ch) {
  2031. return ch <= 0xf7 ? LowTypes[ch] :
  2032. 0x590 <= ch && ch <= 0x5f4 ? 2 /* R */ :
  2033. 0x600 <= ch && ch <= 0x6f9 ? ArabicTypes[ch - 0x600] :
  2034. 0x6ee <= ch && ch <= 0x8ac ? 4 /* AL */ :
  2035. 0x2000 <= ch && ch <= 0x200b ? 256 /* NI */ :
  2036. ch == 0x200c ? 256 /* NI */ : 1 /* L */;
  2037. }
  2038. const BidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;
  2039. /**
  2040. Represents a contiguous range of text that has a single direction
  2041. (as in left-to-right or right-to-left).
  2042. */
  2043. class BidiSpan {
  2044. /**
  2045. @internal
  2046. */
  2047. constructor(
  2048. /**
  2049. The start of the span (relative to the start of the line).
  2050. */
  2051. from,
  2052. /**
  2053. The end of the span.
  2054. */
  2055. to,
  2056. /**
  2057. The ["bidi
  2058. level"](https://unicode.org/reports/tr9/#Basic_Display_Algorithm)
  2059. of the span (in this context, 0 means
  2060. left-to-right, 1 means right-to-left, 2 means left-to-right
  2061. number inside right-to-left text).
  2062. */
  2063. level) {
  2064. this.from = from;
  2065. this.to = to;
  2066. this.level = level;
  2067. }
  2068. /**
  2069. The direction of this span.
  2070. */
  2071. get dir() { return this.level % 2 ? RTL : LTR; }
  2072. /**
  2073. @internal
  2074. */
  2075. side(end, dir) { return (this.dir == dir) == end ? this.to : this.from; }
  2076. /**
  2077. @internal
  2078. */
  2079. static find(order, index, level, assoc) {
  2080. let maybe = -1;
  2081. for (let i = 0; i < order.length; i++) {
  2082. let span = order[i];
  2083. if (span.from <= index && span.to >= index) {
  2084. if (span.level == level)
  2085. return i;
  2086. // When multiple spans match, if assoc != 0, take the one that
  2087. // covers that side, otherwise take the one with the minimum
  2088. // level.
  2089. if (maybe < 0 || (assoc != 0 ? (assoc < 0 ? span.from < index : span.to > index) : order[maybe].level > span.level))
  2090. maybe = i;
  2091. }
  2092. }
  2093. if (maybe < 0)
  2094. throw new RangeError("Index out of range");
  2095. return maybe;
  2096. }
  2097. }
  2098. // Reused array of character types
  2099. const types = [];
  2100. function computeOrder(line, direction) {
  2101. let len = line.length, outerType = direction == LTR ? 1 /* L */ : 2 /* R */, oppositeType = direction == LTR ? 2 /* R */ : 1 /* L */;
  2102. if (!line || outerType == 1 /* L */ && !BidiRE.test(line))
  2103. return trivialOrder(len);
  2104. // W1. Examine each non-spacing mark (NSM) in the level run, and
  2105. // change the type of the NSM to the type of the previous
  2106. // character. If the NSM is at the start of the level run, it will
  2107. // get the type of sor.
  2108. // W2. Search backwards from each instance of a European number
  2109. // until the first strong type (R, L, AL, or sor) is found. If an
  2110. // AL is found, change the type of the European number to Arabic
  2111. // number.
  2112. // W3. Change all ALs to R.
  2113. // (Left after this: L, R, EN, AN, ET, CS, NI)
  2114. for (let i = 0, prev = outerType, prevStrong = outerType; i < len; i++) {
  2115. let type = charType(line.charCodeAt(i));
  2116. if (type == 512 /* NSM */)
  2117. type = prev;
  2118. else if (type == 8 /* EN */ && prevStrong == 4 /* AL */)
  2119. type = 16 /* AN */;
  2120. types[i] = type == 4 /* AL */ ? 2 /* R */ : type;
  2121. if (type & 7 /* Strong */)
  2122. prevStrong = type;
  2123. prev = type;
  2124. }
  2125. // W5. A sequence of European terminators adjacent to European
  2126. // numbers changes to all European numbers.
  2127. // W6. Otherwise, separators and terminators change to Other
  2128. // Neutral.
  2129. // W7. Search backwards from each instance of a European number
  2130. // until the first strong type (R, L, or sor) is found. If an L is
  2131. // found, then change the type of the European number to L.
  2132. // (Left after this: L, R, EN+AN, NI)
  2133. for (let i = 0, prev = outerType, prevStrong = outerType; i < len; i++) {
  2134. let type = types[i];
  2135. if (type == 128 /* CS */) {
  2136. if (i < len - 1 && prev == types[i + 1] && (prev & 24 /* Num */))
  2137. type = types[i] = prev;
  2138. else
  2139. types[i] = 256 /* NI */;
  2140. }
  2141. else if (type == 64 /* ET */) {
  2142. let end = i + 1;
  2143. while (end < len && types[end] == 64 /* ET */)
  2144. end++;
  2145. let replace = (i && prev == 8 /* EN */) || (end < len && types[end] == 8 /* EN */) ? (prevStrong == 1 /* L */ ? 1 /* L */ : 8 /* EN */) : 256 /* NI */;
  2146. for (let j = i; j < end; j++)
  2147. types[j] = replace;
  2148. i = end - 1;
  2149. }
  2150. else if (type == 8 /* EN */ && prevStrong == 1 /* L */) {
  2151. types[i] = 1 /* L */;
  2152. }
  2153. prev = type;
  2154. if (type & 7 /* Strong */)
  2155. prevStrong = type;
  2156. }
  2157. // N0. Process bracket pairs in an isolating run sequence
  2158. // sequentially in the logical order of the text positions of the
  2159. // opening paired brackets using the logic given below. Within this
  2160. // scope, bidirectional types EN and AN are treated as R.
  2161. for (let i = 0, sI = 0, context = 0, ch, br, type; i < len; i++) {
  2162. // Keeps [startIndex, type, strongSeen] triples for each open
  2163. // bracket on BracketStack.
  2164. if (br = Brackets[ch = line.charCodeAt(i)]) {
  2165. if (br < 0) { // Closing bracket
  2166. for (let sJ = sI - 3; sJ >= 0; sJ -= 3) {
  2167. if (BracketStack[sJ + 1] == -br) {
  2168. let flags = BracketStack[sJ + 2];
  2169. let type = (flags & 2 /* EmbedInside */) ? outerType :
  2170. !(flags & 4 /* OppositeInside */) ? 0 :
  2171. (flags & 1 /* OppositeBefore */) ? oppositeType : outerType;
  2172. if (type)
  2173. types[i] = types[BracketStack[sJ]] = type;
  2174. sI = sJ;
  2175. break;
  2176. }
  2177. }
  2178. }
  2179. else if (BracketStack.length == 189 /* MaxDepth */) {
  2180. break;
  2181. }
  2182. else {
  2183. BracketStack[sI++] = i;
  2184. BracketStack[sI++] = ch;
  2185. BracketStack[sI++] = context;
  2186. }
  2187. }
  2188. else if ((type = types[i]) == 2 /* R */ || type == 1 /* L */) {
  2189. let embed = type == outerType;
  2190. context = embed ? 0 : 1 /* OppositeBefore */;
  2191. for (let sJ = sI - 3; sJ >= 0; sJ -= 3) {
  2192. let cur = BracketStack[sJ + 2];
  2193. if (cur & 2 /* EmbedInside */)
  2194. break;
  2195. if (embed) {
  2196. BracketStack[sJ + 2] |= 2 /* EmbedInside */;
  2197. }
  2198. else {
  2199. if (cur & 4 /* OppositeInside */)
  2200. break;
  2201. BracketStack[sJ + 2] |= 4 /* OppositeInside */;
  2202. }
  2203. }
  2204. }
  2205. }
  2206. // N1. A sequence of neutrals takes the direction of the
  2207. // surrounding strong text if the text on both sides has the same
  2208. // direction. European and Arabic numbers act as if they were R in
  2209. // terms of their influence on neutrals. Start-of-level-run (sor)
  2210. // and end-of-level-run (eor) are used at level run boundaries.
  2211. // N2. Any remaining neutrals take the embedding direction.
  2212. // (Left after this: L, R, EN+AN)
  2213. for (let i = 0; i < len; i++) {
  2214. if (types[i] == 256 /* NI */) {
  2215. let end = i + 1;
  2216. while (end < len && types[end] == 256 /* NI */)
  2217. end++;
  2218. let beforeL = (i ? types[i - 1] : outerType) == 1 /* L */;
  2219. let afterL = (end < len ? types[end] : outerType) == 1 /* L */;
  2220. let replace = beforeL == afterL ? (beforeL ? 1 /* L */ : 2 /* R */) : outerType;
  2221. for (let j = i; j < end; j++)
  2222. types[j] = replace;
  2223. i = end - 1;
  2224. }
  2225. }
  2226. // Here we depart from the documented algorithm, in order to avoid
  2227. // building up an actual levels array. Since there are only three
  2228. // levels (0, 1, 2) in an implementation that doesn't take
  2229. // explicit embedding into account, we can build up the order on
  2230. // the fly, without following the level-based algorithm.
  2231. let order = [];
  2232. if (outerType == 1 /* L */) {
  2233. for (let i = 0; i < len;) {
  2234. let start = i, rtl = types[i++] != 1 /* L */;
  2235. while (i < len && rtl == (types[i] != 1 /* L */))
  2236. i++;
  2237. if (rtl) {
  2238. for (let j = i; j > start;) {
  2239. let end = j, l = types[--j] != 2 /* R */;
  2240. while (j > start && l == (types[j - 1] != 2 /* R */))
  2241. j--;
  2242. order.push(new BidiSpan(j, end, l ? 2 : 1));
  2243. }
  2244. }
  2245. else {
  2246. order.push(new BidiSpan(start, i, 0));
  2247. }
  2248. }
  2249. }
  2250. else {
  2251. for (let i = 0; i < len;) {
  2252. let start = i, rtl = types[i++] == 2 /* R */;
  2253. while (i < len && rtl == (types[i] == 2 /* R */))
  2254. i++;
  2255. order.push(new BidiSpan(start, i, rtl ? 1 : 2));
  2256. }
  2257. }
  2258. return order;
  2259. }
  2260. function trivialOrder(length) {
  2261. return [new BidiSpan(0, length, 0)];
  2262. }
  2263. let movedOver = "";
  2264. function moveVisually(line, order, dir, start, forward) {
  2265. var _a;
  2266. let startIndex = start.head - line.from, spanI = -1;
  2267. if (startIndex == 0) {
  2268. if (!forward || !line.length)
  2269. return null;
  2270. if (order[0].level != dir) {
  2271. startIndex = order[0].side(false, dir);
  2272. spanI = 0;
  2273. }
  2274. }
  2275. else if (startIndex == line.length) {
  2276. if (forward)
  2277. return null;
  2278. let last = order[order.length - 1];
  2279. if (last.level != dir) {
  2280. startIndex = last.side(true, dir);
  2281. spanI = order.length - 1;
  2282. }
  2283. }
  2284. if (spanI < 0)
  2285. spanI = BidiSpan.find(order, startIndex, (_a = start.bidiLevel) !== null && _a !== void 0 ? _a : -1, start.assoc);
  2286. let span = order[spanI];
  2287. // End of span. (But not end of line--that was checked for above.)
  2288. if (startIndex == span.side(forward, dir)) {
  2289. span = order[spanI += forward ? 1 : -1];
  2290. startIndex = span.side(!forward, dir);
  2291. }
  2292. let indexForward = forward == (span.dir == dir);
  2293. let nextIndex = findClusterBreak(line.text, startIndex, indexForward);
  2294. movedOver = line.text.slice(Math.min(startIndex, nextIndex), Math.max(startIndex, nextIndex));
  2295. if (nextIndex != span.side(forward, dir))
  2296. return EditorSelection.cursor(nextIndex + line.from, indexForward ? -1 : 1, span.level);
  2297. let nextSpan = spanI == (forward ? order.length - 1 : 0) ? null : order[spanI + (forward ? 1 : -1)];
  2298. if (!nextSpan && span.level != dir)
  2299. return EditorSelection.cursor(forward ? line.to : line.from, forward ? -1 : 1, dir);
  2300. if (nextSpan && nextSpan.level < span.level)
  2301. return EditorSelection.cursor(nextSpan.side(!forward, dir) + line.from, forward ? 1 : -1, nextSpan.level);
  2302. return EditorSelection.cursor(nextIndex + line.from, forward ? -1 : 1, span.level);
  2303. }
  2304. const LineBreakPlaceholder = "\uffff";
  2305. class DOMReader {
  2306. constructor(points, state) {
  2307. this.points = points;
  2308. this.text = "";
  2309. this.lineSeparator = state.facet(EditorState.lineSeparator);
  2310. }
  2311. append(text) {
  2312. this.text += text;
  2313. }
  2314. lineBreak() {
  2315. this.text += LineBreakPlaceholder;
  2316. }
  2317. readRange(start, end) {
  2318. if (!start)
  2319. return this;
  2320. let parent = start.parentNode;
  2321. for (let cur = start;;) {
  2322. this.findPointBefore(parent, cur);
  2323. this.readNode(cur);
  2324. let next = cur.nextSibling;
  2325. if (next == end)
  2326. break;
  2327. let view = ContentView.get(cur), nextView = ContentView.get(next);
  2328. if (view && nextView ? view.breakAfter :
  2329. (view ? view.breakAfter : isBlockElement(cur)) ||
  2330. (isBlockElement(next) && (cur.nodeName != "BR" || cur.cmIgnore)))
  2331. this.lineBreak();
  2332. cur = next;
  2333. }
  2334. this.findPointBefore(parent, end);
  2335. return this;
  2336. }
  2337. readTextNode(node) {
  2338. let text = node.nodeValue;
  2339. for (let point of this.points)
  2340. if (point.node == node)
  2341. point.pos = this.text.length + Math.min(point.offset, text.length);
  2342. for (let off = 0, re = this.lineSeparator ? null : /\r\n?|\n/g;;) {
  2343. let nextBreak = -1, breakSize = 1, m;
  2344. if (this.lineSeparator) {
  2345. nextBreak = text.indexOf(this.lineSeparator, off);
  2346. breakSize = this.lineSeparator.length;
  2347. }
  2348. else if (m = re.exec(text)) {
  2349. nextBreak = m.index;
  2350. breakSize = m[0].length;
  2351. }
  2352. this.append(text.slice(off, nextBreak < 0 ? text.length : nextBreak));
  2353. if (nextBreak < 0)
  2354. break;
  2355. this.lineBreak();
  2356. if (breakSize > 1)
  2357. for (let point of this.points)
  2358. if (point.node == node && point.pos > this.text.length)
  2359. point.pos -= breakSize - 1;
  2360. off = nextBreak + breakSize;
  2361. }
  2362. }
  2363. readNode(node) {
  2364. if (node.cmIgnore)
  2365. return;
  2366. let view = ContentView.get(node);
  2367. let fromView = view && view.overrideDOMText;
  2368. if (fromView != null) {
  2369. this.findPointInside(node, fromView.length);
  2370. for (let i = fromView.iter(); !i.next().done;) {
  2371. if (i.lineBreak)
  2372. this.lineBreak();
  2373. else
  2374. this.append(i.value);
  2375. }
  2376. }
  2377. else if (node.nodeType == 3) {
  2378. this.readTextNode(node);
  2379. }
  2380. else if (node.nodeName == "BR") {
  2381. if (node.nextSibling)
  2382. this.lineBreak();
  2383. }
  2384. else if (node.nodeType == 1) {
  2385. this.readRange(node.firstChild, null);
  2386. }
  2387. }
  2388. findPointBefore(node, next) {
  2389. for (let point of this.points)
  2390. if (point.node == node && node.childNodes[point.offset] == next)
  2391. point.pos = this.text.length;
  2392. }
  2393. findPointInside(node, maxLen) {
  2394. for (let point of this.points)
  2395. if (node.nodeType == 3 ? point.node == node : node.contains(point.node))
  2396. point.pos = this.text.length + Math.min(maxLen, point.offset);
  2397. }
  2398. }
  2399. function isBlockElement(node) {
  2400. return node.nodeType == 1 && /^(DIV|P|LI|UL|OL|BLOCKQUOTE|DD|DT|H\d|SECTION|PRE)$/.test(node.nodeName);
  2401. }
  2402. class DOMPoint {
  2403. constructor(node, offset) {
  2404. this.node = node;
  2405. this.offset = offset;
  2406. this.pos = -1;
  2407. }
  2408. }
  2409. class DocView extends ContentView {
  2410. constructor(view) {
  2411. super();
  2412. this.view = view;
  2413. this.compositionDeco = Decoration.none;
  2414. this.decorations = [];
  2415. this.dynamicDecorationMap = [];
  2416. // Track a minimum width for the editor. When measuring sizes in
  2417. // measureVisibleLineHeights, this is updated to point at the width
  2418. // of a given element and its extent in the document. When a change
  2419. // happens in that range, these are reset. That way, once we've seen
  2420. // a line/element of a given length, we keep the editor wide enough
  2421. // to fit at least that element, until it is changed, at which point
  2422. // we forget it again.
  2423. this.minWidth = 0;
  2424. this.minWidthFrom = 0;
  2425. this.minWidthTo = 0;
  2426. // Track whether the DOM selection was set in a lossy way, so that
  2427. // we don't mess it up when reading it back it
  2428. this.impreciseAnchor = null;
  2429. this.impreciseHead = null;
  2430. this.forceSelection = false;
  2431. // Used by the resize observer to ignore resizes that we caused
  2432. // ourselves
  2433. this.lastUpdate = Date.now();
  2434. this.setDOM(view.contentDOM);
  2435. this.children = [new LineView];
  2436. this.children[0].setParent(this);
  2437. this.updateDeco();
  2438. this.updateInner([new ChangedRange(0, 0, 0, view.state.doc.length)], 0);
  2439. }
  2440. get root() { return this.view.root; }
  2441. get editorView() { return this.view; }
  2442. get length() { return this.view.state.doc.length; }
  2443. // Update the document view to a given state. scrollIntoView can be
  2444. // used as a hint to compute a new viewport that includes that
  2445. // position, if we know the editor is going to scroll that position
  2446. // into view.
  2447. update(update) {
  2448. let changedRanges = update.changedRanges;
  2449. if (this.minWidth > 0 && changedRanges.length) {
  2450. if (!changedRanges.every(({ fromA, toA }) => toA < this.minWidthFrom || fromA > this.minWidthTo)) {
  2451. this.minWidth = this.minWidthFrom = this.minWidthTo = 0;
  2452. }
  2453. else {
  2454. this.minWidthFrom = update.changes.mapPos(this.minWidthFrom, 1);
  2455. this.minWidthTo = update.changes.mapPos(this.minWidthTo, 1);
  2456. }
  2457. }
  2458. if (this.view.inputState.composing < 0)
  2459. this.compositionDeco = Decoration.none;
  2460. else if (update.transactions.length || this.dirty)
  2461. this.compositionDeco = computeCompositionDeco(this.view, update.changes);
  2462. // When the DOM nodes around the selection are moved to another
  2463. // parent, Chrome sometimes reports a different selection through
  2464. // getSelection than the one that it actually shows to the user.
  2465. // This forces a selection update when lines are joined to work
  2466. // around that. Issue #54
  2467. if ((browser.ie || browser.chrome) && !this.compositionDeco.size && update &&
  2468. update.state.doc.lines != update.startState.doc.lines)
  2469. this.forceSelection = true;
  2470. let prevDeco = this.decorations, deco = this.updateDeco();
  2471. let decoDiff = findChangedDeco(prevDeco, deco, update.changes);
  2472. changedRanges = ChangedRange.extendWithRanges(changedRanges, decoDiff);
  2473. if (this.dirty == 0 /* Not */ && changedRanges.length == 0) {
  2474. return false;
  2475. }
  2476. else {
  2477. this.updateInner(changedRanges, update.startState.doc.length);
  2478. if (update.transactions.length)
  2479. this.lastUpdate = Date.now();
  2480. return true;
  2481. }
  2482. }
  2483. // Used by update and the constructor do perform the actual DOM
  2484. // update
  2485. updateInner(changes, oldLength) {
  2486. this.view.viewState.mustMeasureContent = true;
  2487. this.updateChildren(changes, oldLength);
  2488. let { observer } = this.view;
  2489. observer.ignore(() => {
  2490. // Lock the height during redrawing, since Chrome sometimes
  2491. // messes with the scroll position during DOM mutation (though
  2492. // no relayout is triggered and I cannot imagine how it can
  2493. // recompute the scroll position without a layout)
  2494. this.dom.style.height = this.view.viewState.contentHeight + "px";
  2495. this.dom.style.flexBasis = this.minWidth ? this.minWidth + "px" : "";
  2496. // Chrome will sometimes, when DOM mutations occur directly
  2497. // around the selection, get confused and report a different
  2498. // selection from the one it displays (issue #218). This tries
  2499. // to detect that situation.
  2500. let track = browser.chrome || browser.ios ? { node: observer.selectionRange.focusNode, written: false } : undefined;
  2501. this.sync(track);
  2502. this.dirty = 0 /* Not */;
  2503. if (track && (track.written || observer.selectionRange.focusNode != track.node))
  2504. this.forceSelection = true;
  2505. this.dom.style.height = "";
  2506. });
  2507. let gaps = [];
  2508. if (this.view.viewport.from || this.view.viewport.to < this.view.state.doc.length)
  2509. for (let child of this.children)
  2510. if (child instanceof BlockWidgetView && child.widget instanceof BlockGapWidget)
  2511. gaps.push(child.dom);
  2512. observer.updateGaps(gaps);
  2513. }
  2514. updateChildren(changes, oldLength) {
  2515. let cursor = this.childCursor(oldLength);
  2516. for (let i = changes.length - 1;; i--) {
  2517. let next = i >= 0 ? changes[i] : null;
  2518. if (!next)
  2519. break;
  2520. let { fromA, toA, fromB, toB } = next;
  2521. let { content, breakAtStart, openStart, openEnd } = ContentBuilder.build(this.view.state.doc, fromB, toB, this.decorations, this.dynamicDecorationMap);
  2522. let { i: toI, off: toOff } = cursor.findPos(toA, 1);
  2523. let { i: fromI, off: fromOff } = cursor.findPos(fromA, -1);
  2524. replaceRange(this, fromI, fromOff, toI, toOff, content, breakAtStart, openStart, openEnd);
  2525. }
  2526. }
  2527. // Sync the DOM selection to this.state.selection
  2528. updateSelection(mustRead = false, fromPointer = false) {
  2529. if (mustRead || !this.view.observer.selectionRange.focusNode)
  2530. this.view.observer.readSelectionRange();
  2531. if (!(fromPointer || this.mayControlSelection()) ||
  2532. browser.ios && this.view.inputState.rapidCompositionStart)
  2533. return;
  2534. let force = this.forceSelection;
  2535. this.forceSelection = false;
  2536. let main = this.view.state.selection.main;
  2537. // FIXME need to handle the case where the selection falls inside a block range
  2538. let anchor = this.domAtPos(main.anchor);
  2539. let head = main.empty ? anchor : this.domAtPos(main.head);
  2540. // Always reset on Firefox when next to an uneditable node to
  2541. // avoid invisible cursor bugs (#111)
  2542. if (browser.gecko && main.empty && betweenUneditable(anchor)) {
  2543. let dummy = document.createTextNode("");
  2544. this.view.observer.ignore(() => anchor.node.insertBefore(dummy, anchor.node.childNodes[anchor.offset] || null));
  2545. anchor = head = new DOMPos(dummy, 0);
  2546. force = true;
  2547. }
  2548. let domSel = this.view.observer.selectionRange;
  2549. // If the selection is already here, or in an equivalent position, don't touch it
  2550. if (force || !domSel.focusNode ||
  2551. !isEquivalentPosition(anchor.node, anchor.offset, domSel.anchorNode, domSel.anchorOffset) ||
  2552. !isEquivalentPosition(head.node, head.offset, domSel.focusNode, domSel.focusOffset)) {
  2553. this.view.observer.ignore(() => {
  2554. // Chrome Android will hide the virtual keyboard when tapping
  2555. // inside an uneditable node, and not bring it back when we
  2556. // move the cursor to its proper position. This tries to
  2557. // restore the keyboard by cycling focus.
  2558. if (browser.android && browser.chrome && this.dom.contains(domSel.focusNode) &&
  2559. inUneditable(domSel.focusNode, this.dom)) {
  2560. this.dom.blur();
  2561. this.dom.focus({ preventScroll: true });
  2562. }
  2563. let rawSel = getSelection(this.root);
  2564. if (!rawSel) ;
  2565. else if (main.empty) {
  2566. // Work around https://bugzilla.mozilla.org/show_bug.cgi?id=1612076
  2567. if (browser.gecko) {
  2568. let nextTo = nextToUneditable(anchor.node, anchor.offset);
  2569. if (nextTo && nextTo != (1 /* Before */ | 2 /* After */)) {
  2570. let text = nearbyTextNode(anchor.node, anchor.offset, nextTo == 1 /* Before */ ? 1 : -1);
  2571. if (text)
  2572. anchor = new DOMPos(text, nextTo == 1 /* Before */ ? 0 : text.nodeValue.length);
  2573. }
  2574. }
  2575. rawSel.collapse(anchor.node, anchor.offset);
  2576. if (main.bidiLevel != null && domSel.cursorBidiLevel != null)
  2577. domSel.cursorBidiLevel = main.bidiLevel;
  2578. }
  2579. else if (rawSel.extend) {
  2580. // Selection.extend can be used to create an 'inverted' selection
  2581. // (one where the focus is before the anchor), but not all
  2582. // browsers support it yet.
  2583. rawSel.collapse(anchor.node, anchor.offset);
  2584. rawSel.extend(head.node, head.offset);
  2585. }
  2586. else {
  2587. // Primitive (IE) way
  2588. let range = document.createRange();
  2589. if (main.anchor > main.head)
  2590. [anchor, head] = [head, anchor];
  2591. range.setEnd(head.node, head.offset);
  2592. range.setStart(anchor.node, anchor.offset);
  2593. rawSel.removeAllRanges();
  2594. rawSel.addRange(range);
  2595. }
  2596. });
  2597. this.view.observer.setSelectionRange(anchor, head);
  2598. }
  2599. this.impreciseAnchor = anchor.precise ? null : new DOMPos(domSel.anchorNode, domSel.anchorOffset);
  2600. this.impreciseHead = head.precise ? null : new DOMPos(domSel.focusNode, domSel.focusOffset);
  2601. }
  2602. enforceCursorAssoc() {
  2603. if (this.compositionDeco.size)
  2604. return;
  2605. let cursor = this.view.state.selection.main;
  2606. let sel = getSelection(this.root);
  2607. if (!sel || !cursor.empty || !cursor.assoc || !sel.modify)
  2608. return;
  2609. let line = LineView.find(this, cursor.head);
  2610. if (!line)
  2611. return;
  2612. let lineStart = line.posAtStart;
  2613. if (cursor.head == lineStart || cursor.head == lineStart + line.length)
  2614. return;
  2615. let before = this.coordsAt(cursor.head, -1), after = this.coordsAt(cursor.head, 1);
  2616. if (!before || !after || before.bottom > after.top)
  2617. return;
  2618. let dom = this.domAtPos(cursor.head + cursor.assoc);
  2619. sel.collapse(dom.node, dom.offset);
  2620. sel.modify("move", cursor.assoc < 0 ? "forward" : "backward", "lineboundary");
  2621. }
  2622. mayControlSelection() {
  2623. let active = this.root.activeElement;
  2624. return active == this.dom ||
  2625. hasSelection(this.dom, this.view.observer.selectionRange) && !(active && this.dom.contains(active));
  2626. }
  2627. nearest(dom) {
  2628. for (let cur = dom; cur;) {
  2629. let domView = ContentView.get(cur);
  2630. if (domView && domView.rootView == this)
  2631. return domView;
  2632. cur = cur.parentNode;
  2633. }
  2634. return null;
  2635. }
  2636. posFromDOM(node, offset) {
  2637. let view = this.nearest(node);
  2638. if (!view)
  2639. throw new RangeError("Trying to find position for a DOM position outside of the document");
  2640. return view.localPosFromDOM(node, offset) + view.posAtStart;
  2641. }
  2642. domAtPos(pos) {
  2643. let { i, off } = this.childCursor().findPos(pos, -1);
  2644. for (; i < this.children.length - 1;) {
  2645. let child = this.children[i];
  2646. if (off < child.length || child instanceof LineView)
  2647. break;
  2648. i++;
  2649. off = 0;
  2650. }
  2651. return this.children[i].domAtPos(off);
  2652. }
  2653. coordsAt(pos, side) {
  2654. for (let off = this.length, i = this.children.length - 1;; i--) {
  2655. let child = this.children[i], start = off - child.breakAfter - child.length;
  2656. if (pos > start ||
  2657. (pos == start && child.type != BlockType.WidgetBefore && child.type != BlockType.WidgetAfter &&
  2658. (!i || side == 2 || this.children[i - 1].breakAfter ||
  2659. (this.children[i - 1].type == BlockType.WidgetBefore && side > -2))))
  2660. return child.coordsAt(pos - start, side);
  2661. off = start;
  2662. }
  2663. }
  2664. measureVisibleLineHeights(viewport) {
  2665. let result = [], { from, to } = viewport;
  2666. let contentWidth = this.view.contentDOM.clientWidth;
  2667. let isWider = contentWidth > Math.max(this.view.scrollDOM.clientWidth, this.minWidth) + 1;
  2668. let widest = -1, ltr = this.view.textDirection == Direction.LTR;
  2669. for (let pos = 0, i = 0; i < this.children.length; i++) {
  2670. let child = this.children[i], end = pos + child.length;
  2671. if (end > to)
  2672. break;
  2673. if (pos >= from) {
  2674. let childRect = child.dom.getBoundingClientRect();
  2675. result.push(childRect.height);
  2676. if (isWider) {
  2677. let last = child.dom.lastChild;
  2678. let rects = last ? clientRectsFor(last) : [];
  2679. if (rects.length) {
  2680. let rect = rects[rects.length - 1];
  2681. let width = ltr ? rect.right - childRect.left : childRect.right - rect.left;
  2682. if (width > widest) {
  2683. widest = width;
  2684. this.minWidth = contentWidth;
  2685. this.minWidthFrom = pos;
  2686. this.minWidthTo = end;
  2687. }
  2688. }
  2689. }
  2690. }
  2691. pos = end + child.breakAfter;
  2692. }
  2693. return result;
  2694. }
  2695. textDirectionAt(pos) {
  2696. let { i } = this.childPos(pos, 1);
  2697. return getComputedStyle(this.children[i].dom).direction == "rtl" ? Direction.RTL : Direction.LTR;
  2698. }
  2699. measureTextSize() {
  2700. for (let child of this.children) {
  2701. if (child instanceof LineView) {
  2702. let measure = child.measureTextSize();
  2703. if (measure)
  2704. return measure;
  2705. }
  2706. }
  2707. // If no workable line exists, force a layout of a measurable element
  2708. let dummy = document.createElement("div"), lineHeight, charWidth;
  2709. dummy.className = "cm-line";
  2710. dummy.style.width = "99999px";
  2711. dummy.textContent = "abc def ghi jkl mno pqr stu";
  2712. this.view.observer.ignore(() => {
  2713. this.dom.appendChild(dummy);
  2714. let rect = clientRectsFor(dummy.firstChild)[0];
  2715. lineHeight = dummy.getBoundingClientRect().height;
  2716. charWidth = rect ? rect.width / 27 : 7;
  2717. dummy.remove();
  2718. });
  2719. return { lineHeight, charWidth };
  2720. }
  2721. childCursor(pos = this.length) {
  2722. // Move back to start of last element when possible, so that
  2723. // `ChildCursor.findPos` doesn't have to deal with the edge case
  2724. // of being after the last element.
  2725. let i = this.children.length;
  2726. if (i)
  2727. pos -= this.children[--i].length;
  2728. return new ChildCursor(this.children, pos, i);
  2729. }
  2730. computeBlockGapDeco() {
  2731. let deco = [], vs = this.view.viewState;
  2732. for (let pos = 0, i = 0;; i++) {
  2733. let next = i == vs.viewports.length ? null : vs.viewports[i];
  2734. let end = next ? next.from - 1 : this.length;
  2735. if (end > pos) {
  2736. let height = vs.lineBlockAt(end).bottom - vs.lineBlockAt(pos).top;
  2737. deco.push(Decoration.replace({
  2738. widget: new BlockGapWidget(height),
  2739. block: true,
  2740. inclusive: true,
  2741. isBlockGap: true,
  2742. }).range(pos, end));
  2743. }
  2744. if (!next)
  2745. break;
  2746. pos = next.to + 1;
  2747. }
  2748. return Decoration.set(deco);
  2749. }
  2750. updateDeco() {
  2751. let allDeco = this.view.state.facet(decorations).map((d, i) => {
  2752. let dynamic = this.dynamicDecorationMap[i] = typeof d == "function";
  2753. return dynamic ? d(this.view) : d;
  2754. });
  2755. for (let i = allDeco.length; i < allDeco.length + 3; i++)
  2756. this.dynamicDecorationMap[i] = false;
  2757. return this.decorations = [
  2758. ...allDeco,
  2759. this.compositionDeco,
  2760. this.computeBlockGapDeco(),
  2761. this.view.viewState.lineGapDeco
  2762. ];
  2763. }
  2764. scrollIntoView(target) {
  2765. let { range } = target;
  2766. let rect = this.coordsAt(range.head, range.empty ? range.assoc : range.head > range.anchor ? -1 : 1), other;
  2767. if (!rect)
  2768. return;
  2769. if (!range.empty && (other = this.coordsAt(range.anchor, range.anchor > range.head ? -1 : 1)))
  2770. rect = { left: Math.min(rect.left, other.left), top: Math.min(rect.top, other.top),
  2771. right: Math.max(rect.right, other.right), bottom: Math.max(rect.bottom, other.bottom) };
  2772. let mLeft = 0, mRight = 0, mTop = 0, mBottom = 0;
  2773. for (let margins of this.view.state.facet(scrollMargins).map(f => f(this.view)))
  2774. if (margins) {
  2775. let { left, right, top, bottom } = margins;
  2776. if (left != null)
  2777. mLeft = Math.max(mLeft, left);
  2778. if (right != null)
  2779. mRight = Math.max(mRight, right);
  2780. if (top != null)
  2781. mTop = Math.max(mTop, top);
  2782. if (bottom != null)
  2783. mBottom = Math.max(mBottom, bottom);
  2784. }
  2785. let targetRect = {
  2786. left: rect.left - mLeft, top: rect.top - mTop,
  2787. right: rect.right + mRight, bottom: rect.bottom + mBottom
  2788. };
  2789. scrollRectIntoView(this.view.scrollDOM, targetRect, range.head < range.anchor ? -1 : 1, target.x, target.y, target.xMargin, target.yMargin, this.view.textDirection == Direction.LTR);
  2790. }
  2791. }
  2792. function betweenUneditable(pos) {
  2793. return pos.node.nodeType == 1 && pos.node.firstChild &&
  2794. (pos.offset == 0 || pos.node.childNodes[pos.offset - 1].contentEditable == "false") &&
  2795. (pos.offset == pos.node.childNodes.length || pos.node.childNodes[pos.offset].contentEditable == "false");
  2796. }
  2797. class BlockGapWidget extends WidgetType {
  2798. constructor(height) {
  2799. super();
  2800. this.height = height;
  2801. }
  2802. toDOM() {
  2803. let elt = document.createElement("div");
  2804. this.updateDOM(elt);
  2805. return elt;
  2806. }
  2807. eq(other) { return other.height == this.height; }
  2808. updateDOM(elt) {
  2809. elt.style.height = this.height + "px";
  2810. return true;
  2811. }
  2812. get estimatedHeight() { return this.height; }
  2813. }
  2814. function compositionSurroundingNode(view) {
  2815. let sel = view.observer.selectionRange;
  2816. let textNode = sel.focusNode && nearbyTextNode(sel.focusNode, sel.focusOffset, 0);
  2817. if (!textNode)
  2818. return null;
  2819. let cView = view.docView.nearest(textNode);
  2820. if (!cView)
  2821. return null;
  2822. if (cView instanceof LineView) {
  2823. let topNode = textNode;
  2824. while (topNode.parentNode != cView.dom)
  2825. topNode = topNode.parentNode;
  2826. let prev = topNode.previousSibling;
  2827. while (prev && !ContentView.get(prev))
  2828. prev = prev.previousSibling;
  2829. let pos = prev ? ContentView.get(prev).posAtEnd : cView.posAtStart;
  2830. return { from: pos, to: pos, node: topNode, text: textNode };
  2831. }
  2832. else {
  2833. for (;;) {
  2834. let { parent } = cView;
  2835. if (!parent)
  2836. return null;
  2837. if (parent instanceof LineView)
  2838. break;
  2839. cView = parent;
  2840. }
  2841. let from = cView.posAtStart;
  2842. return { from, to: from + cView.length, node: cView.dom, text: textNode };
  2843. }
  2844. }
  2845. function computeCompositionDeco(view, changes) {
  2846. let surrounding = compositionSurroundingNode(view);
  2847. if (!surrounding)
  2848. return Decoration.none;
  2849. let { from, to, node, text: textNode } = surrounding;
  2850. let newFrom = changes.mapPos(from, 1), newTo = Math.max(newFrom, changes.mapPos(to, -1));
  2851. let { state } = view, text = node.nodeType == 3 ? node.nodeValue :
  2852. new DOMReader([], state).readRange(node.firstChild, null).text;
  2853. if (newTo - newFrom < text.length) {
  2854. if (state.doc.sliceString(newFrom, Math.min(state.doc.length, newFrom + text.length), LineBreakPlaceholder) == text)
  2855. newTo = newFrom + text.length;
  2856. else if (state.doc.sliceString(Math.max(0, newTo - text.length), newTo, LineBreakPlaceholder) == text)
  2857. newFrom = newTo - text.length;
  2858. else
  2859. return Decoration.none;
  2860. }
  2861. else if (state.doc.sliceString(newFrom, newTo, LineBreakPlaceholder) != text) {
  2862. return Decoration.none;
  2863. }
  2864. let topView = ContentView.get(node);
  2865. if (topView instanceof CompositionView)
  2866. topView = topView.widget.topView;
  2867. else if (topView)
  2868. topView.parent = null;
  2869. return Decoration.set(Decoration.replace({ widget: new CompositionWidget(node, textNode, topView), inclusive: true })
  2870. .range(newFrom, newTo));
  2871. }
  2872. class CompositionWidget extends WidgetType {
  2873. constructor(top, text, topView) {
  2874. super();
  2875. this.top = top;
  2876. this.text = text;
  2877. this.topView = topView;
  2878. }
  2879. eq(other) { return this.top == other.top && this.text == other.text; }
  2880. toDOM() { return this.top; }
  2881. ignoreEvent() { return false; }
  2882. get customView() { return CompositionView; }
  2883. }
  2884. function nearbyTextNode(node, offset, side) {
  2885. for (;;) {
  2886. if (node.nodeType == 3)
  2887. return node;
  2888. if (node.nodeType == 1 && offset > 0 && side <= 0) {
  2889. node = node.childNodes[offset - 1];
  2890. offset = maxOffset(node);
  2891. }
  2892. else if (node.nodeType == 1 && offset < node.childNodes.length && side >= 0) {
  2893. node = node.childNodes[offset];
  2894. offset = 0;
  2895. }
  2896. else {
  2897. return null;
  2898. }
  2899. }
  2900. }
  2901. function nextToUneditable(node, offset) {
  2902. if (node.nodeType != 1)
  2903. return 0;
  2904. return (offset && node.childNodes[offset - 1].contentEditable == "false" ? 1 /* Before */ : 0) |
  2905. (offset < node.childNodes.length && node.childNodes[offset].contentEditable == "false" ? 2 /* After */ : 0);
  2906. }
  2907. class DecorationComparator$1 {
  2908. constructor() {
  2909. this.changes = [];
  2910. }
  2911. compareRange(from, to) { addRange(from, to, this.changes); }
  2912. comparePoint(from, to) { addRange(from, to, this.changes); }
  2913. }
  2914. function findChangedDeco(a, b, diff) {
  2915. let comp = new DecorationComparator$1;
  2916. RangeSet.compare(a, b, diff, comp);
  2917. return comp.changes;
  2918. }
  2919. function inUneditable(node, inside) {
  2920. for (let cur = node; cur && cur != inside; cur = cur.assignedSlot || cur.parentNode) {
  2921. if (cur.nodeType == 1 && cur.contentEditable == 'false') {
  2922. return true;
  2923. }
  2924. }
  2925. return false;
  2926. }
  2927. function groupAt(state, pos, bias = 1) {
  2928. let categorize = state.charCategorizer(pos);
  2929. let line = state.doc.lineAt(pos), linePos = pos - line.from;
  2930. if (line.length == 0)
  2931. return EditorSelection.cursor(pos);
  2932. if (linePos == 0)
  2933. bias = 1;
  2934. else if (linePos == line.length)
  2935. bias = -1;
  2936. let from = linePos, to = linePos;
  2937. if (bias < 0)
  2938. from = findClusterBreak(line.text, linePos, false);
  2939. else
  2940. to = findClusterBreak(line.text, linePos);
  2941. let cat = categorize(line.text.slice(from, to));
  2942. while (from > 0) {
  2943. let prev = findClusterBreak(line.text, from, false);
  2944. if (categorize(line.text.slice(prev, from)) != cat)
  2945. break;
  2946. from = prev;
  2947. }
  2948. while (to < line.length) {
  2949. let next = findClusterBreak(line.text, to);
  2950. if (categorize(line.text.slice(to, next)) != cat)
  2951. break;
  2952. to = next;
  2953. }
  2954. return EditorSelection.range(from + line.from, to + line.from);
  2955. }
  2956. // Search the DOM for the {node, offset} position closest to the given
  2957. // coordinates. Very inefficient and crude, but can usually be avoided
  2958. // by calling caret(Position|Range)FromPoint instead.
  2959. function getdx(x, rect) {
  2960. return rect.left > x ? rect.left - x : Math.max(0, x - rect.right);
  2961. }
  2962. function getdy(y, rect) {
  2963. return rect.top > y ? rect.top - y : Math.max(0, y - rect.bottom);
  2964. }
  2965. function yOverlap(a, b) {
  2966. return a.top < b.bottom - 1 && a.bottom > b.top + 1;
  2967. }
  2968. function upTop(rect, top) {
  2969. return top < rect.top ? { top, left: rect.left, right: rect.right, bottom: rect.bottom } : rect;
  2970. }
  2971. function upBot(rect, bottom) {
  2972. return bottom > rect.bottom ? { top: rect.top, left: rect.left, right: rect.right, bottom } : rect;
  2973. }
  2974. function domPosAtCoords(parent, x, y) {
  2975. let closest, closestRect, closestX, closestY;
  2976. let above, below, aboveRect, belowRect;
  2977. for (let child = parent.firstChild; child; child = child.nextSibling) {
  2978. let rects = clientRectsFor(child);
  2979. for (let i = 0; i < rects.length; i++) {
  2980. let rect = rects[i];
  2981. if (closestRect && yOverlap(closestRect, rect))
  2982. rect = upTop(upBot(rect, closestRect.bottom), closestRect.top);
  2983. let dx = getdx(x, rect), dy = getdy(y, rect);
  2984. if (dx == 0 && dy == 0)
  2985. return child.nodeType == 3 ? domPosInText(child, x, y) : domPosAtCoords(child, x, y);
  2986. if (!closest || closestY > dy || closestY == dy && closestX > dx) {
  2987. closest = child;
  2988. closestRect = rect;
  2989. closestX = dx;
  2990. closestY = dy;
  2991. }
  2992. if (dx == 0) {
  2993. if (y > rect.bottom && (!aboveRect || aboveRect.bottom < rect.bottom)) {
  2994. above = child;
  2995. aboveRect = rect;
  2996. }
  2997. else if (y < rect.top && (!belowRect || belowRect.top > rect.top)) {
  2998. below = child;
  2999. belowRect = rect;
  3000. }
  3001. }
  3002. else if (aboveRect && yOverlap(aboveRect, rect)) {
  3003. aboveRect = upBot(aboveRect, rect.bottom);
  3004. }
  3005. else if (belowRect && yOverlap(belowRect, rect)) {
  3006. belowRect = upTop(belowRect, rect.top);
  3007. }
  3008. }
  3009. }
  3010. if (aboveRect && aboveRect.bottom >= y) {
  3011. closest = above;
  3012. closestRect = aboveRect;
  3013. }
  3014. else if (belowRect && belowRect.top <= y) {
  3015. closest = below;
  3016. closestRect = belowRect;
  3017. }
  3018. if (!closest)
  3019. return { node: parent, offset: 0 };
  3020. let clipX = Math.max(closestRect.left, Math.min(closestRect.right, x));
  3021. if (closest.nodeType == 3)
  3022. return domPosInText(closest, clipX, y);
  3023. if (!closestX && closest.contentEditable == "true")
  3024. return domPosAtCoords(closest, clipX, y);
  3025. let offset = Array.prototype.indexOf.call(parent.childNodes, closest) +
  3026. (x >= (closestRect.left + closestRect.right) / 2 ? 1 : 0);
  3027. return { node: parent, offset };
  3028. }
  3029. function domPosInText(node, x, y) {
  3030. let len = node.nodeValue.length;
  3031. let closestOffset = -1, closestDY = 1e9, generalSide = 0;
  3032. for (let i = 0; i < len; i++) {
  3033. let rects = textRange(node, i, i + 1).getClientRects();
  3034. for (let j = 0; j < rects.length; j++) {
  3035. let rect = rects[j];
  3036. if (rect.top == rect.bottom)
  3037. continue;
  3038. if (!generalSide)
  3039. generalSide = x - rect.left;
  3040. let dy = (rect.top > y ? rect.top - y : y - rect.bottom) - 1;
  3041. if (rect.left - 1 <= x && rect.right + 1 >= x && dy < closestDY) {
  3042. let right = x >= (rect.left + rect.right) / 2, after = right;
  3043. if (browser.chrome || browser.gecko) {
  3044. // Check for RTL on browsers that support getting client
  3045. // rects for empty ranges.
  3046. let rectBefore = textRange(node, i).getBoundingClientRect();
  3047. if (rectBefore.left == rect.right)
  3048. after = !right;
  3049. }
  3050. if (dy <= 0)
  3051. return { node, offset: i + (after ? 1 : 0) };
  3052. closestOffset = i + (after ? 1 : 0);
  3053. closestDY = dy;
  3054. }
  3055. }
  3056. }
  3057. return { node, offset: closestOffset > -1 ? closestOffset : generalSide > 0 ? node.nodeValue.length : 0 };
  3058. }
  3059. function posAtCoords(view, { x, y }, precise, bias = -1) {
  3060. var _a;
  3061. let content = view.contentDOM.getBoundingClientRect(), docTop = content.top + view.viewState.paddingTop;
  3062. let block, { docHeight } = view.viewState;
  3063. let yOffset = y - docTop;
  3064. if (yOffset < 0)
  3065. return 0;
  3066. if (yOffset > docHeight)
  3067. return view.state.doc.length;
  3068. // Scan for a text block near the queried y position
  3069. for (let halfLine = view.defaultLineHeight / 2, bounced = false;;) {
  3070. block = view.elementAtHeight(yOffset);
  3071. if (block.type == BlockType.Text)
  3072. break;
  3073. for (;;) {
  3074. // Move the y position out of this block
  3075. yOffset = bias > 0 ? block.bottom + halfLine : block.top - halfLine;
  3076. if (yOffset >= 0 && yOffset <= docHeight)
  3077. break;
  3078. // If the document consists entirely of replaced widgets, we
  3079. // won't find a text block, so return 0
  3080. if (bounced)
  3081. return precise ? null : 0;
  3082. bounced = true;
  3083. bias = -bias;
  3084. }
  3085. }
  3086. y = docTop + yOffset;
  3087. let lineStart = block.from;
  3088. // If this is outside of the rendered viewport, we can't determine a position
  3089. if (lineStart < view.viewport.from)
  3090. return view.viewport.from == 0 ? 0 : precise ? null : posAtCoordsImprecise(view, content, block, x, y);
  3091. if (lineStart > view.viewport.to)
  3092. return view.viewport.to == view.state.doc.length ? view.state.doc.length :
  3093. precise ? null : posAtCoordsImprecise(view, content, block, x, y);
  3094. // Prefer ShadowRootOrDocument.elementFromPoint if present, fall back to document if not
  3095. let doc = view.dom.ownerDocument;
  3096. let root = view.root.elementFromPoint ? view.root : doc;
  3097. let element = root.elementFromPoint(x, y);
  3098. if (element && !view.contentDOM.contains(element))
  3099. element = null;
  3100. // If the element is unexpected, clip x at the sides of the content area and try again
  3101. if (!element) {
  3102. x = Math.max(content.left + 1, Math.min(content.right - 1, x));
  3103. element = root.elementFromPoint(x, y);
  3104. if (element && !view.contentDOM.contains(element))
  3105. element = null;
  3106. }
  3107. // There's visible editor content under the point, so we can try
  3108. // using caret(Position|Range)FromPoint as a shortcut
  3109. let node, offset = -1;
  3110. if (element && ((_a = view.docView.nearest(element)) === null || _a === void 0 ? void 0 : _a.isEditable) != false) {
  3111. if (doc.caretPositionFromPoint) {
  3112. let pos = doc.caretPositionFromPoint(x, y);
  3113. if (pos)
  3114. ({ offsetNode: node, offset } = pos);
  3115. }
  3116. else if (doc.caretRangeFromPoint) {
  3117. let range = doc.caretRangeFromPoint(x, y);
  3118. if (range) {
  3119. ({ startContainer: node, startOffset: offset } = range);
  3120. if (browser.safari && isSuspiciousSafariCaretResult(node, offset, x) ||
  3121. browser.chrome && isSuspiciousChromeCaretResult(node, offset, x))
  3122. node = undefined;
  3123. }
  3124. }
  3125. }
  3126. // No luck, do our own (potentially expensive) search
  3127. if (!node || !view.docView.dom.contains(node)) {
  3128. let line = LineView.find(view.docView, lineStart);
  3129. if (!line)
  3130. return yOffset > block.top + block.height / 2 ? block.to : block.from;
  3131. ({ node, offset } = domPosAtCoords(line.dom, x, y));
  3132. }
  3133. return view.docView.posFromDOM(node, offset);
  3134. }
  3135. function posAtCoordsImprecise(view, contentRect, block, x, y) {
  3136. let into = Math.round((x - contentRect.left) * view.defaultCharacterWidth);
  3137. if (view.lineWrapping && block.height > view.defaultLineHeight * 1.5) {
  3138. let line = Math.floor((y - block.top) / view.defaultLineHeight);
  3139. into += line * view.viewState.heightOracle.lineLength;
  3140. }
  3141. let content = view.state.sliceDoc(block.from, block.to);
  3142. return block.from + findColumn(content, into, view.state.tabSize);
  3143. }
  3144. // In case of a high line height, Safari's caretRangeFromPoint treats
  3145. // the space between lines as belonging to the last character of the
  3146. // line before. This is used to detect such a result so that it can be
  3147. // ignored (issue #401).
  3148. function isSuspiciousSafariCaretResult(node, offset, x) {
  3149. let len;
  3150. if (node.nodeType != 3 || offset != (len = node.nodeValue.length))
  3151. return false;
  3152. for (let next = node.nextSibling; next; next = next.nextSibling)
  3153. if (next.nodeType != 1 || next.nodeName != "BR")
  3154. return false;
  3155. return textRange(node, len - 1, len).getBoundingClientRect().left > x;
  3156. }
  3157. // Chrome will move positions between lines to the start of the next line
  3158. function isSuspiciousChromeCaretResult(node, offset, x) {
  3159. if (offset != 0)
  3160. return false;
  3161. for (let cur = node;;) {
  3162. let parent = cur.parentNode;
  3163. if (!parent || parent.nodeType != 1 || parent.firstChild != cur)
  3164. return false;
  3165. if (parent.classList.contains("cm-line"))
  3166. break;
  3167. cur = parent;
  3168. }
  3169. let rect = node.nodeType == 1 ? node.getBoundingClientRect()
  3170. : textRange(node, 0, Math.max(node.nodeValue.length, 1)).getBoundingClientRect();
  3171. return x - rect.left > 5;
  3172. }
  3173. function moveToLineBoundary(view, start, forward, includeWrap) {
  3174. let line = view.state.doc.lineAt(start.head);
  3175. let coords = !includeWrap || !view.lineWrapping ? null
  3176. : view.coordsAtPos(start.assoc < 0 && start.head > line.from ? start.head - 1 : start.head);
  3177. if (coords) {
  3178. let editorRect = view.dom.getBoundingClientRect();
  3179. let direction = view.textDirectionAt(line.from);
  3180. let pos = view.posAtCoords({ x: forward == (direction == Direction.LTR) ? editorRect.right - 1 : editorRect.left + 1,
  3181. y: (coords.top + coords.bottom) / 2 });
  3182. if (pos != null)
  3183. return EditorSelection.cursor(pos, forward ? -1 : 1);
  3184. }
  3185. let lineView = LineView.find(view.docView, start.head);
  3186. let end = lineView ? (forward ? lineView.posAtEnd : lineView.posAtStart) : (forward ? line.to : line.from);
  3187. return EditorSelection.cursor(end, forward ? -1 : 1);
  3188. }
  3189. function moveByChar(view, start, forward, by) {
  3190. let line = view.state.doc.lineAt(start.head), spans = view.bidiSpans(line);
  3191. let direction = view.textDirectionAt(line.from);
  3192. for (let cur = start, check = null;;) {
  3193. let next = moveVisually(line, spans, direction, cur, forward), char = movedOver;
  3194. if (!next) {
  3195. if (line.number == (forward ? view.state.doc.lines : 1))
  3196. return cur;
  3197. char = "\n";
  3198. line = view.state.doc.line(line.number + (forward ? 1 : -1));
  3199. spans = view.bidiSpans(line);
  3200. next = EditorSelection.cursor(forward ? line.from : line.to);
  3201. }
  3202. if (!check) {
  3203. if (!by)
  3204. return next;
  3205. check = by(char);
  3206. }
  3207. else if (!check(char)) {
  3208. return cur;
  3209. }
  3210. cur = next;
  3211. }
  3212. }
  3213. function byGroup(view, pos, start) {
  3214. let categorize = view.state.charCategorizer(pos);
  3215. let cat = categorize(start);
  3216. return (next) => {
  3217. let nextCat = categorize(next);
  3218. if (cat == CharCategory.Space)
  3219. cat = nextCat;
  3220. return cat == nextCat;
  3221. };
  3222. }
  3223. function moveVertically(view, start, forward, distance) {
  3224. let startPos = start.head, dir = forward ? 1 : -1;
  3225. if (startPos == (forward ? view.state.doc.length : 0))
  3226. return EditorSelection.cursor(startPos, start.assoc);
  3227. let goal = start.goalColumn, startY;
  3228. let rect = view.contentDOM.getBoundingClientRect();
  3229. let startCoords = view.coordsAtPos(startPos), docTop = view.documentTop;
  3230. if (startCoords) {
  3231. if (goal == null)
  3232. goal = startCoords.left - rect.left;
  3233. startY = dir < 0 ? startCoords.top : startCoords.bottom;
  3234. }
  3235. else {
  3236. let line = view.viewState.lineBlockAt(startPos);
  3237. if (goal == null)
  3238. goal = Math.min(rect.right - rect.left, view.defaultCharacterWidth * (startPos - line.from));
  3239. startY = (dir < 0 ? line.top : line.bottom) + docTop;
  3240. }
  3241. let resolvedGoal = rect.left + goal;
  3242. let dist = distance !== null && distance !== void 0 ? distance : (view.defaultLineHeight >> 1);
  3243. for (let extra = 0;; extra += 10) {
  3244. let curY = startY + (dist + extra) * dir;
  3245. let pos = posAtCoords(view, { x: resolvedGoal, y: curY }, false, dir);
  3246. if (curY < rect.top || curY > rect.bottom || (dir < 0 ? pos < startPos : pos > startPos))
  3247. return EditorSelection.cursor(pos, start.assoc, undefined, goal);
  3248. }
  3249. }
  3250. function skipAtoms(view, oldPos, pos) {
  3251. let atoms = view.state.facet(atomicRanges).map(f => f(view));
  3252. for (;;) {
  3253. let moved = false;
  3254. for (let set of atoms) {
  3255. set.between(pos.from - 1, pos.from + 1, (from, to, value) => {
  3256. if (pos.from > from && pos.from < to) {
  3257. pos = oldPos.from > pos.from ? EditorSelection.cursor(from, 1) : EditorSelection.cursor(to, -1);
  3258. moved = true;
  3259. }
  3260. });
  3261. }
  3262. if (!moved)
  3263. return pos;
  3264. }
  3265. }
  3266. // This will also be where dragging info and such goes
  3267. class InputState {
  3268. constructor(view) {
  3269. this.lastKeyCode = 0;
  3270. this.lastKeyTime = 0;
  3271. this.chromeScrollHack = -1;
  3272. // On iOS, some keys need to have their default behavior happen
  3273. // (after which we retroactively handle them and reset the DOM) to
  3274. // avoid messing up the virtual keyboard state.
  3275. this.pendingIOSKey = undefined;
  3276. this.lastSelectionOrigin = null;
  3277. this.lastSelectionTime = 0;
  3278. this.lastEscPress = 0;
  3279. this.lastContextMenu = 0;
  3280. this.scrollHandlers = [];
  3281. this.registeredEvents = [];
  3282. this.customHandlers = [];
  3283. // -1 means not in a composition. Otherwise, this counts the number
  3284. // of changes made during the composition. The count is used to
  3285. // avoid treating the start state of the composition, before any
  3286. // changes have been made, as part of the composition.
  3287. this.composing = -1;
  3288. // Tracks whether the next change should be marked as starting the
  3289. // composition (null means no composition, true means next is the
  3290. // first, false means first has already been marked for this
  3291. // composition)
  3292. this.compositionFirstChange = null;
  3293. this.compositionEndedAt = 0;
  3294. this.rapidCompositionStart = false;
  3295. this.mouseSelection = null;
  3296. for (let type in handlers) {
  3297. let handler = handlers[type];
  3298. view.contentDOM.addEventListener(type, (event) => {
  3299. if (!eventBelongsToEditor(view, event) || this.ignoreDuringComposition(event))
  3300. return;
  3301. if (type == "keydown" && this.keydown(view, event))
  3302. return;
  3303. if (this.mustFlushObserver(event))
  3304. view.observer.forceFlush();
  3305. if (this.runCustomHandlers(type, view, event))
  3306. event.preventDefault();
  3307. else
  3308. handler(view, event);
  3309. });
  3310. this.registeredEvents.push(type);
  3311. }
  3312. if (browser.chrome && browser.chrome_version >= 102) {
  3313. // On Chrome 102, viewport updates somehow stop wheel-based
  3314. // scrolling. Turning off pointer events during the scroll seems
  3315. // to avoid the issue.
  3316. view.scrollDOM.addEventListener("wheel", () => {
  3317. if (this.chromeScrollHack < 0)
  3318. view.contentDOM.style.pointerEvents = "none";
  3319. else
  3320. window.clearTimeout(this.chromeScrollHack);
  3321. this.chromeScrollHack = setTimeout(() => {
  3322. this.chromeScrollHack = -1;
  3323. view.contentDOM.style.pointerEvents = "";
  3324. }, 100);
  3325. }, { passive: true });
  3326. }
  3327. this.notifiedFocused = view.hasFocus;
  3328. // On Safari adding an input event handler somehow prevents an
  3329. // issue where the composition vanishes when you press enter.
  3330. if (browser.safari)
  3331. view.contentDOM.addEventListener("input", () => null);
  3332. }
  3333. setSelectionOrigin(origin) {
  3334. this.lastSelectionOrigin = origin;
  3335. this.lastSelectionTime = Date.now();
  3336. }
  3337. ensureHandlers(view, plugins) {
  3338. var _a;
  3339. let handlers;
  3340. this.customHandlers = [];
  3341. for (let plugin of plugins)
  3342. if (handlers = (_a = plugin.update(view).spec) === null || _a === void 0 ? void 0 : _a.domEventHandlers) {
  3343. this.customHandlers.push({ plugin: plugin.value, handlers });
  3344. for (let type in handlers)
  3345. if (this.registeredEvents.indexOf(type) < 0 && type != "scroll") {
  3346. this.registeredEvents.push(type);
  3347. view.contentDOM.addEventListener(type, (event) => {
  3348. if (!eventBelongsToEditor(view, event))
  3349. return;
  3350. if (this.runCustomHandlers(type, view, event))
  3351. event.preventDefault();
  3352. });
  3353. }
  3354. }
  3355. }
  3356. runCustomHandlers(type, view, event) {
  3357. for (let set of this.customHandlers) {
  3358. let handler = set.handlers[type];
  3359. if (handler) {
  3360. try {
  3361. if (handler.call(set.plugin, event, view) || event.defaultPrevented)
  3362. return true;
  3363. }
  3364. catch (e) {
  3365. logException(view.state, e);
  3366. }
  3367. }
  3368. }
  3369. return false;
  3370. }
  3371. runScrollHandlers(view, event) {
  3372. for (let set of this.customHandlers) {
  3373. let handler = set.handlers.scroll;
  3374. if (handler) {
  3375. try {
  3376. handler.call(set.plugin, event, view);
  3377. }
  3378. catch (e) {
  3379. logException(view.state, e);
  3380. }
  3381. }
  3382. }
  3383. }
  3384. keydown(view, event) {
  3385. // Must always run, even if a custom handler handled the event
  3386. this.lastKeyCode = event.keyCode;
  3387. this.lastKeyTime = Date.now();
  3388. if (event.keyCode == 9 && Date.now() < this.lastEscPress + 2000)
  3389. return true;
  3390. // Chrome for Android usually doesn't fire proper key events, but
  3391. // occasionally does, usually surrounded by a bunch of complicated
  3392. // composition changes. When an enter or backspace key event is
  3393. // seen, hold off on handling DOM events for a bit, and then
  3394. // dispatch it.
  3395. if (browser.android && browser.chrome && !event.synthetic &&
  3396. (event.keyCode == 13 || event.keyCode == 8)) {
  3397. view.observer.delayAndroidKey(event.key, event.keyCode);
  3398. return true;
  3399. }
  3400. // Prevent the default behavior of Enter on iOS makes the
  3401. // virtual keyboard get stuck in the wrong (lowercase)
  3402. // state. So we let it go through, and then, in
  3403. // applyDOMChange, notify key handlers of it and reset to
  3404. // the state they produce.
  3405. let pending;
  3406. if (browser.ios && (pending = PendingKeys.find(key => key.keyCode == event.keyCode)) &&
  3407. !(event.ctrlKey || event.altKey || event.metaKey) && !event.synthetic) {
  3408. this.pendingIOSKey = pending;
  3409. setTimeout(() => this.flushIOSKey(view), 250);
  3410. return true;
  3411. }
  3412. return false;
  3413. }
  3414. flushIOSKey(view) {
  3415. let key = this.pendingIOSKey;
  3416. if (!key)
  3417. return false;
  3418. this.pendingIOSKey = undefined;
  3419. return dispatchKey(view.contentDOM, key.key, key.keyCode);
  3420. }
  3421. ignoreDuringComposition(event) {
  3422. if (!/^key/.test(event.type))
  3423. return false;
  3424. if (this.composing > 0)
  3425. return true;
  3426. // See https://www.stum.de/2016/06/24/handling-ime-events-in-javascript/.
  3427. // On some input method editors (IMEs), the Enter key is used to
  3428. // confirm character selection. On Safari, when Enter is pressed,
  3429. // compositionend and keydown events are sometimes emitted in the
  3430. // wrong order. The key event should still be ignored, even when
  3431. // it happens after the compositionend event.
  3432. if (browser.safari && Date.now() - this.compositionEndedAt < 100) {
  3433. this.compositionEndedAt = 0;
  3434. return true;
  3435. }
  3436. return false;
  3437. }
  3438. mustFlushObserver(event) {
  3439. return (event.type == "keydown" && event.keyCode != 229) ||
  3440. event.type == "compositionend" && !browser.ios;
  3441. }
  3442. startMouseSelection(mouseSelection) {
  3443. if (this.mouseSelection)
  3444. this.mouseSelection.destroy();
  3445. this.mouseSelection = mouseSelection;
  3446. }
  3447. update(update) {
  3448. if (this.mouseSelection)
  3449. this.mouseSelection.update(update);
  3450. if (update.transactions.length)
  3451. this.lastKeyCode = this.lastSelectionTime = 0;
  3452. }
  3453. destroy() {
  3454. if (this.mouseSelection)
  3455. this.mouseSelection.destroy();
  3456. }
  3457. }
  3458. const PendingKeys = [
  3459. { key: "Backspace", keyCode: 8, inputType: "deleteContentBackward" },
  3460. { key: "Enter", keyCode: 13, inputType: "insertParagraph" },
  3461. { key: "Delete", keyCode: 46, inputType: "deleteContentForward" }
  3462. ];
  3463. // Key codes for modifier keys
  3464. const modifierCodes = [16, 17, 18, 20, 91, 92, 224, 225];
  3465. class MouseSelection {
  3466. constructor(view, startEvent, style, mustSelect) {
  3467. this.view = view;
  3468. this.style = style;
  3469. this.mustSelect = mustSelect;
  3470. this.lastEvent = startEvent;
  3471. let doc = view.contentDOM.ownerDocument;
  3472. doc.addEventListener("mousemove", this.move = this.move.bind(this));
  3473. doc.addEventListener("mouseup", this.up = this.up.bind(this));
  3474. this.extend = startEvent.shiftKey;
  3475. this.multiple = view.state.facet(EditorState.allowMultipleSelections) && addsSelectionRange(view, startEvent);
  3476. this.dragMove = dragMovesSelection(view, startEvent);
  3477. this.dragging = isInPrimarySelection(view, startEvent) && getClickType(startEvent) == 1 ? null : false;
  3478. // When clicking outside of the selection, immediately apply the
  3479. // effect of starting the selection
  3480. if (this.dragging === false) {
  3481. startEvent.preventDefault();
  3482. this.select(startEvent);
  3483. }
  3484. }
  3485. move(event) {
  3486. if (event.buttons == 0)
  3487. return this.destroy();
  3488. if (this.dragging !== false)
  3489. return;
  3490. this.select(this.lastEvent = event);
  3491. }
  3492. up(event) {
  3493. if (this.dragging == null)
  3494. this.select(this.lastEvent);
  3495. if (!this.dragging)
  3496. event.preventDefault();
  3497. this.destroy();
  3498. }
  3499. destroy() {
  3500. let doc = this.view.contentDOM.ownerDocument;
  3501. doc.removeEventListener("mousemove", this.move);
  3502. doc.removeEventListener("mouseup", this.up);
  3503. this.view.inputState.mouseSelection = null;
  3504. }
  3505. select(event) {
  3506. let selection = this.style.get(event, this.extend, this.multiple);
  3507. if (this.mustSelect || !selection.eq(this.view.state.selection) ||
  3508. selection.main.assoc != this.view.state.selection.main.assoc)
  3509. this.view.dispatch({
  3510. selection,
  3511. userEvent: "select.pointer",
  3512. scrollIntoView: true
  3513. });
  3514. this.mustSelect = false;
  3515. }
  3516. update(update) {
  3517. if (update.docChanged && this.dragging)
  3518. this.dragging = this.dragging.map(update.changes);
  3519. if (this.style.update(update))
  3520. setTimeout(() => this.select(this.lastEvent), 20);
  3521. }
  3522. }
  3523. function addsSelectionRange(view, event) {
  3524. let facet = view.state.facet(clickAddsSelectionRange);
  3525. return facet.length ? facet[0](event) : browser.mac ? event.metaKey : event.ctrlKey;
  3526. }
  3527. function dragMovesSelection(view, event) {
  3528. let facet = view.state.facet(dragMovesSelection$1);
  3529. return facet.length ? facet[0](event) : browser.mac ? !event.altKey : !event.ctrlKey;
  3530. }
  3531. function isInPrimarySelection(view, event) {
  3532. let { main } = view.state.selection;
  3533. if (main.empty)
  3534. return false;
  3535. // On boundary clicks, check whether the coordinates are inside the
  3536. // selection's client rectangles
  3537. let sel = getSelection(view.root);
  3538. if (!sel || sel.rangeCount == 0)
  3539. return true;
  3540. let rects = sel.getRangeAt(0).getClientRects();
  3541. for (let i = 0; i < rects.length; i++) {
  3542. let rect = rects[i];
  3543. if (rect.left <= event.clientX && rect.right >= event.clientX &&
  3544. rect.top <= event.clientY && rect.bottom >= event.clientY)
  3545. return true;
  3546. }
  3547. return false;
  3548. }
  3549. function eventBelongsToEditor(view, event) {
  3550. if (!event.bubbles)
  3551. return true;
  3552. if (event.defaultPrevented)
  3553. return false;
  3554. for (let node = event.target, cView; node != view.contentDOM; node = node.parentNode)
  3555. if (!node || node.nodeType == 11 || ((cView = ContentView.get(node)) && cView.ignoreEvent(event)))
  3556. return false;
  3557. return true;
  3558. }
  3559. const handlers = /*@__PURE__*/Object.create(null);
  3560. // This is very crude, but unfortunately both these browsers _pretend_
  3561. // that they have a clipboard API—all the objects and methods are
  3562. // there, they just don't work, and they are hard to test.
  3563. const brokenClipboardAPI = (browser.ie && browser.ie_version < 15) ||
  3564. (browser.ios && browser.webkit_version < 604);
  3565. function capturePaste(view) {
  3566. let parent = view.dom.parentNode;
  3567. if (!parent)
  3568. return;
  3569. let target = parent.appendChild(document.createElement("textarea"));
  3570. target.style.cssText = "position: fixed; left: -10000px; top: 10px";
  3571. target.focus();
  3572. setTimeout(() => {
  3573. view.focus();
  3574. target.remove();
  3575. doPaste(view, target.value);
  3576. }, 50);
  3577. }
  3578. function doPaste(view, input) {
  3579. let { state } = view, changes, i = 1, text = state.toText(input);
  3580. let byLine = text.lines == state.selection.ranges.length;
  3581. let linewise = lastLinewiseCopy != null && state.selection.ranges.every(r => r.empty) && lastLinewiseCopy == text.toString();
  3582. if (linewise) {
  3583. let lastLine = -1;
  3584. changes = state.changeByRange(range => {
  3585. let line = state.doc.lineAt(range.from);
  3586. if (line.from == lastLine)
  3587. return { range };
  3588. lastLine = line.from;
  3589. let insert = state.toText((byLine ? text.line(i++).text : input) + state.lineBreak);
  3590. return { changes: { from: line.from, insert },
  3591. range: EditorSelection.cursor(range.from + insert.length) };
  3592. });
  3593. }
  3594. else if (byLine) {
  3595. changes = state.changeByRange(range => {
  3596. let line = text.line(i++);
  3597. return { changes: { from: range.from, to: range.to, insert: line.text },
  3598. range: EditorSelection.cursor(range.from + line.length) };
  3599. });
  3600. }
  3601. else {
  3602. changes = state.replaceSelection(text);
  3603. }
  3604. view.dispatch(changes, {
  3605. userEvent: "input.paste",
  3606. scrollIntoView: true
  3607. });
  3608. }
  3609. handlers.keydown = (view, event) => {
  3610. view.inputState.setSelectionOrigin("select");
  3611. if (event.keyCode == 27)
  3612. view.inputState.lastEscPress = Date.now();
  3613. else if (modifierCodes.indexOf(event.keyCode) < 0)
  3614. view.inputState.lastEscPress = 0;
  3615. };
  3616. let lastTouch = 0;
  3617. handlers.touchstart = (view, e) => {
  3618. lastTouch = Date.now();
  3619. view.inputState.setSelectionOrigin("select.pointer");
  3620. };
  3621. handlers.touchmove = view => {
  3622. view.inputState.setSelectionOrigin("select.pointer");
  3623. };
  3624. handlers.mousedown = (view, event) => {
  3625. view.observer.flush();
  3626. if (lastTouch > Date.now() - 2000 && getClickType(event) == 1)
  3627. return; // Ignore touch interaction
  3628. let style = null;
  3629. for (let makeStyle of view.state.facet(mouseSelectionStyle)) {
  3630. style = makeStyle(view, event);
  3631. if (style)
  3632. break;
  3633. }
  3634. if (!style && event.button == 0)
  3635. style = basicMouseSelection(view, event);
  3636. if (style) {
  3637. let mustFocus = view.root.activeElement != view.contentDOM;
  3638. if (mustFocus)
  3639. view.observer.ignore(() => focusPreventScroll(view.contentDOM));
  3640. view.inputState.startMouseSelection(new MouseSelection(view, event, style, mustFocus));
  3641. }
  3642. };
  3643. function rangeForClick(view, pos, bias, type) {
  3644. if (type == 1) { // Single click
  3645. return EditorSelection.cursor(pos, bias);
  3646. }
  3647. else if (type == 2) { // Double click
  3648. return groupAt(view.state, pos, bias);
  3649. }
  3650. else { // Triple click
  3651. let visual = LineView.find(view.docView, pos), line = view.state.doc.lineAt(visual ? visual.posAtEnd : pos);
  3652. let from = visual ? visual.posAtStart : line.from, to = visual ? visual.posAtEnd : line.to;
  3653. if (to < view.state.doc.length && to == line.to)
  3654. to++;
  3655. return EditorSelection.range(from, to);
  3656. }
  3657. }
  3658. let insideY = (y, rect) => y >= rect.top && y <= rect.bottom;
  3659. let inside = (x, y, rect) => insideY(y, rect) && x >= rect.left && x <= rect.right;
  3660. // Try to determine, for the given coordinates, associated with the
  3661. // given position, whether they are related to the element before or
  3662. // the element after the position.
  3663. function findPositionSide(view, pos, x, y) {
  3664. let line = LineView.find(view.docView, pos);
  3665. if (!line)
  3666. return 1;
  3667. let off = pos - line.posAtStart;
  3668. // Line boundaries point into the line
  3669. if (off == 0)
  3670. return 1;
  3671. if (off == line.length)
  3672. return -1;
  3673. // Positions on top of an element point at that element
  3674. let before = line.coordsAt(off, -1);
  3675. if (before && inside(x, y, before))
  3676. return -1;
  3677. let after = line.coordsAt(off, 1);
  3678. if (after && inside(x, y, after))
  3679. return 1;
  3680. // This is probably a line wrap point. Pick before if the point is
  3681. // beside it.
  3682. return before && insideY(y, before) ? -1 : 1;
  3683. }
  3684. function queryPos(view, event) {
  3685. let pos = view.posAtCoords({ x: event.clientX, y: event.clientY }, false);
  3686. return { pos, bias: findPositionSide(view, pos, event.clientX, event.clientY) };
  3687. }
  3688. const BadMouseDetail = browser.ie && browser.ie_version <= 11;
  3689. let lastMouseDown = null, lastMouseDownCount = 0, lastMouseDownTime = 0;
  3690. function getClickType(event) {
  3691. if (!BadMouseDetail)
  3692. return event.detail;
  3693. let last = lastMouseDown, lastTime = lastMouseDownTime;
  3694. lastMouseDown = event;
  3695. lastMouseDownTime = Date.now();
  3696. return lastMouseDownCount = !last || (lastTime > Date.now() - 400 && Math.abs(last.clientX - event.clientX) < 2 &&
  3697. Math.abs(last.clientY - event.clientY) < 2) ? (lastMouseDownCount + 1) % 3 : 1;
  3698. }
  3699. function basicMouseSelection(view, event) {
  3700. let start = queryPos(view, event), type = getClickType(event);
  3701. let startSel = view.state.selection;
  3702. let last = start, lastEvent = event;
  3703. return {
  3704. update(update) {
  3705. if (update.docChanged) {
  3706. if (start)
  3707. start.pos = update.changes.mapPos(start.pos);
  3708. startSel = startSel.map(update.changes);
  3709. lastEvent = null;
  3710. }
  3711. },
  3712. get(event, extend, multiple) {
  3713. let cur;
  3714. if (lastEvent && event.clientX == lastEvent.clientX && event.clientY == lastEvent.clientY)
  3715. cur = last;
  3716. else {
  3717. cur = last = queryPos(view, event);
  3718. lastEvent = event;
  3719. }
  3720. if (!cur || !start)
  3721. return startSel;
  3722. let range = rangeForClick(view, cur.pos, cur.bias, type);
  3723. if (start.pos != cur.pos && !extend) {
  3724. let startRange = rangeForClick(view, start.pos, start.bias, type);
  3725. let from = Math.min(startRange.from, range.from), to = Math.max(startRange.to, range.to);
  3726. range = from < range.from ? EditorSelection.range(from, to) : EditorSelection.range(to, from);
  3727. }
  3728. if (extend)
  3729. return startSel.replaceRange(startSel.main.extend(range.from, range.to));
  3730. else if (multiple && startSel.ranges.length > 1 && startSel.ranges.some(r => r.eq(range)))
  3731. return removeRange(startSel, range);
  3732. else if (multiple)
  3733. return startSel.addRange(range);
  3734. else
  3735. return EditorSelection.create([range]);
  3736. }
  3737. };
  3738. }
  3739. function removeRange(sel, range) {
  3740. for (let i = 0;; i++) {
  3741. if (sel.ranges[i].eq(range))
  3742. return EditorSelection.create(sel.ranges.slice(0, i).concat(sel.ranges.slice(i + 1)), sel.mainIndex == i ? 0 : sel.mainIndex - (sel.mainIndex > i ? 1 : 0));
  3743. }
  3744. }
  3745. handlers.dragstart = (view, event) => {
  3746. let { selection: { main } } = view.state;
  3747. let { mouseSelection } = view.inputState;
  3748. if (mouseSelection)
  3749. mouseSelection.dragging = main;
  3750. if (event.dataTransfer) {
  3751. event.dataTransfer.setData("Text", view.state.sliceDoc(main.from, main.to));
  3752. event.dataTransfer.effectAllowed = "copyMove";
  3753. }
  3754. };
  3755. function dropText(view, event, text, direct) {
  3756. if (!text)
  3757. return;
  3758. let dropPos = view.posAtCoords({ x: event.clientX, y: event.clientY }, false);
  3759. event.preventDefault();
  3760. let { mouseSelection } = view.inputState;
  3761. let del = direct && mouseSelection && mouseSelection.dragging && mouseSelection.dragMove ?
  3762. { from: mouseSelection.dragging.from, to: mouseSelection.dragging.to } : null;
  3763. let ins = { from: dropPos, insert: text };
  3764. let changes = view.state.changes(del ? [del, ins] : ins);
  3765. view.focus();
  3766. view.dispatch({
  3767. changes,
  3768. selection: { anchor: changes.mapPos(dropPos, -1), head: changes.mapPos(dropPos, 1) },
  3769. userEvent: del ? "move.drop" : "input.drop"
  3770. });
  3771. }
  3772. handlers.drop = (view, event) => {
  3773. if (!event.dataTransfer)
  3774. return;
  3775. if (view.state.readOnly)
  3776. return event.preventDefault();
  3777. let files = event.dataTransfer.files;
  3778. if (files && files.length) { // For a file drop, read the file's text.
  3779. event.preventDefault();
  3780. let text = Array(files.length), read = 0;
  3781. let finishFile = () => {
  3782. if (++read == files.length)
  3783. dropText(view, event, text.filter(s => s != null).join(view.state.lineBreak), false);
  3784. };
  3785. for (let i = 0; i < files.length; i++) {
  3786. let reader = new FileReader;
  3787. reader.onerror = finishFile;
  3788. reader.onload = () => {
  3789. if (!/[\x00-\x08\x0e-\x1f]{2}/.test(reader.result))
  3790. text[i] = reader.result;
  3791. finishFile();
  3792. };
  3793. reader.readAsText(files[i]);
  3794. }
  3795. }
  3796. else {
  3797. dropText(view, event, event.dataTransfer.getData("Text"), true);
  3798. }
  3799. };
  3800. handlers.paste = (view, event) => {
  3801. if (view.state.readOnly)
  3802. return event.preventDefault();
  3803. view.observer.flush();
  3804. let data = brokenClipboardAPI ? null : event.clipboardData;
  3805. if (data) {
  3806. doPaste(view, data.getData("text/plain"));
  3807. event.preventDefault();
  3808. }
  3809. else {
  3810. capturePaste(view);
  3811. }
  3812. };
  3813. function captureCopy(view, text) {
  3814. // The extra wrapper is somehow necessary on IE/Edge to prevent the
  3815. // content from being mangled when it is put onto the clipboard
  3816. let parent = view.dom.parentNode;
  3817. if (!parent)
  3818. return;
  3819. let target = parent.appendChild(document.createElement("textarea"));
  3820. target.style.cssText = "position: fixed; left: -10000px; top: 10px";
  3821. target.value = text;
  3822. target.focus();
  3823. target.selectionEnd = text.length;
  3824. target.selectionStart = 0;
  3825. setTimeout(() => {
  3826. target.remove();
  3827. view.focus();
  3828. }, 50);
  3829. }
  3830. function copiedRange(state) {
  3831. let content = [], ranges = [], linewise = false;
  3832. for (let range of state.selection.ranges)
  3833. if (!range.empty) {
  3834. content.push(state.sliceDoc(range.from, range.to));
  3835. ranges.push(range);
  3836. }
  3837. if (!content.length) {
  3838. // Nothing selected, do a line-wise copy
  3839. let upto = -1;
  3840. for (let { from } of state.selection.ranges) {
  3841. let line = state.doc.lineAt(from);
  3842. if (line.number > upto) {
  3843. content.push(line.text);
  3844. ranges.push({ from: line.from, to: Math.min(state.doc.length, line.to + 1) });
  3845. }
  3846. upto = line.number;
  3847. }
  3848. linewise = true;
  3849. }
  3850. return { text: content.join(state.lineBreak), ranges, linewise };
  3851. }
  3852. let lastLinewiseCopy = null;
  3853. handlers.copy = handlers.cut = (view, event) => {
  3854. let { text, ranges, linewise } = copiedRange(view.state);
  3855. if (!text && !linewise)
  3856. return;
  3857. lastLinewiseCopy = linewise ? text : null;
  3858. let data = brokenClipboardAPI ? null : event.clipboardData;
  3859. if (data) {
  3860. event.preventDefault();
  3861. data.clearData();
  3862. data.setData("text/plain", text);
  3863. }
  3864. else {
  3865. captureCopy(view, text);
  3866. }
  3867. if (event.type == "cut" && !view.state.readOnly)
  3868. view.dispatch({
  3869. changes: ranges,
  3870. scrollIntoView: true,
  3871. userEvent: "delete.cut"
  3872. });
  3873. };
  3874. function updateForFocusChange(view) {
  3875. setTimeout(() => {
  3876. if (view.hasFocus != view.inputState.notifiedFocused)
  3877. view.update([]);
  3878. }, 10);
  3879. }
  3880. handlers.focus = updateForFocusChange;
  3881. handlers.blur = view => {
  3882. view.observer.clearSelectionRange();
  3883. updateForFocusChange(view);
  3884. };
  3885. function forceClearComposition(view, rapid) {
  3886. if (view.docView.compositionDeco.size) {
  3887. view.inputState.rapidCompositionStart = rapid;
  3888. try {
  3889. view.update([]);
  3890. }
  3891. finally {
  3892. view.inputState.rapidCompositionStart = false;
  3893. }
  3894. }
  3895. }
  3896. handlers.compositionstart = handlers.compositionupdate = view => {
  3897. if (view.inputState.compositionFirstChange == null)
  3898. view.inputState.compositionFirstChange = true;
  3899. if (view.inputState.composing < 0) {
  3900. // FIXME possibly set a timeout to clear it again on Android
  3901. view.inputState.composing = 0;
  3902. if (view.docView.compositionDeco.size) {
  3903. view.observer.flush();
  3904. forceClearComposition(view, true);
  3905. }
  3906. }
  3907. };
  3908. handlers.compositionend = view => {
  3909. view.inputState.composing = -1;
  3910. view.inputState.compositionEndedAt = Date.now();
  3911. view.inputState.compositionFirstChange = null;
  3912. setTimeout(() => {
  3913. if (view.inputState.composing < 0)
  3914. forceClearComposition(view, false);
  3915. }, 50);
  3916. };
  3917. handlers.contextmenu = view => {
  3918. view.inputState.lastContextMenu = Date.now();
  3919. };
  3920. handlers.beforeinput = (view, event) => {
  3921. var _a;
  3922. // Because Chrome Android doesn't fire useful key events, use
  3923. // beforeinput to detect backspace (and possibly enter and delete,
  3924. // but those usually don't even seem to fire beforeinput events at
  3925. // the moment) and fake a key event for it.
  3926. //
  3927. // (preventDefault on beforeinput, though supported in the spec,
  3928. // seems to do nothing at all on Chrome).
  3929. let pending;
  3930. if (browser.chrome && browser.android && (pending = PendingKeys.find(key => key.inputType == event.inputType))) {
  3931. view.observer.delayAndroidKey(pending.key, pending.keyCode);
  3932. if (pending.key == "Backspace" || pending.key == "Delete") {
  3933. let startViewHeight = ((_a = window.visualViewport) === null || _a === void 0 ? void 0 : _a.height) || 0;
  3934. setTimeout(() => {
  3935. var _a;
  3936. // Backspacing near uneditable nodes on Chrome Android sometimes
  3937. // closes the virtual keyboard. This tries to crudely detect
  3938. // that and refocus to get it back.
  3939. if ((((_a = window.visualViewport) === null || _a === void 0 ? void 0 : _a.height) || 0) > startViewHeight + 10 && view.hasFocus) {
  3940. view.contentDOM.blur();
  3941. view.focus();
  3942. }
  3943. }, 100);
  3944. }
  3945. }
  3946. };
  3947. const wrappingWhiteSpace = ["pre-wrap", "normal", "pre-line", "break-spaces"];
  3948. class HeightOracle {
  3949. constructor() {
  3950. this.doc = Text.empty;
  3951. this.lineWrapping = false;
  3952. this.heightSamples = {};
  3953. this.lineHeight = 14;
  3954. this.charWidth = 7;
  3955. this.lineLength = 30;
  3956. // Used to track, during updateHeight, if any actual heights changed
  3957. this.heightChanged = false;
  3958. }
  3959. heightForGap(from, to) {
  3960. let lines = this.doc.lineAt(to).number - this.doc.lineAt(from).number + 1;
  3961. if (this.lineWrapping)
  3962. lines += Math.ceil(((to - from) - (lines * this.lineLength * 0.5)) / this.lineLength);
  3963. return this.lineHeight * lines;
  3964. }
  3965. heightForLine(length) {
  3966. if (!this.lineWrapping)
  3967. return this.lineHeight;
  3968. let lines = 1 + Math.max(0, Math.ceil((length - this.lineLength) / (this.lineLength - 5)));
  3969. return lines * this.lineHeight;
  3970. }
  3971. setDoc(doc) { this.doc = doc; return this; }
  3972. mustRefreshForWrapping(whiteSpace) {
  3973. return (wrappingWhiteSpace.indexOf(whiteSpace) > -1) != this.lineWrapping;
  3974. }
  3975. mustRefreshForHeights(lineHeights) {
  3976. let newHeight = false;
  3977. for (let i = 0; i < lineHeights.length; i++) {
  3978. let h = lineHeights[i];
  3979. if (h < 0) {
  3980. i++;
  3981. }
  3982. else if (!this.heightSamples[Math.floor(h * 10)]) { // Round to .1 pixels
  3983. newHeight = true;
  3984. this.heightSamples[Math.floor(h * 10)] = true;
  3985. }
  3986. }
  3987. return newHeight;
  3988. }
  3989. refresh(whiteSpace, lineHeight, charWidth, lineLength, knownHeights) {
  3990. let lineWrapping = wrappingWhiteSpace.indexOf(whiteSpace) > -1;
  3991. let changed = Math.round(lineHeight) != Math.round(this.lineHeight) || this.lineWrapping != lineWrapping;
  3992. this.lineWrapping = lineWrapping;
  3993. this.lineHeight = lineHeight;
  3994. this.charWidth = charWidth;
  3995. this.lineLength = lineLength;
  3996. if (changed) {
  3997. this.heightSamples = {};
  3998. for (let i = 0; i < knownHeights.length; i++) {
  3999. let h = knownHeights[i];
  4000. if (h < 0)
  4001. i++;
  4002. else
  4003. this.heightSamples[Math.floor(h * 10)] = true;
  4004. }
  4005. }
  4006. return changed;
  4007. }
  4008. }
  4009. // This object is used by `updateHeight` to make DOM measurements
  4010. // arrive at the right nides. The `heights` array is a sequence of
  4011. // block heights, starting from position `from`.
  4012. class MeasuredHeights {
  4013. constructor(from, heights) {
  4014. this.from = from;
  4015. this.heights = heights;
  4016. this.index = 0;
  4017. }
  4018. get more() { return this.index < this.heights.length; }
  4019. }
  4020. /**
  4021. Record used to represent information about a block-level element
  4022. in the editor view.
  4023. */
  4024. class BlockInfo {
  4025. /**
  4026. @internal
  4027. */
  4028. constructor(
  4029. /**
  4030. The start of the element in the document.
  4031. */
  4032. from,
  4033. /**
  4034. The length of the element.
  4035. */
  4036. length,
  4037. /**
  4038. The top position of the element (relative to the top of the
  4039. document).
  4040. */
  4041. top,
  4042. /**
  4043. Its height.
  4044. */
  4045. height,
  4046. /**
  4047. The type of element this is. When querying lines, this may be
  4048. an array of all the blocks that make up the line.
  4049. */
  4050. type) {
  4051. this.from = from;
  4052. this.length = length;
  4053. this.top = top;
  4054. this.height = height;
  4055. this.type = type;
  4056. }
  4057. /**
  4058. The end of the element as a document position.
  4059. */
  4060. get to() { return this.from + this.length; }
  4061. /**
  4062. The bottom position of the element.
  4063. */
  4064. get bottom() { return this.top + this.height; }
  4065. /**
  4066. @internal
  4067. */
  4068. join(other) {
  4069. let detail = (Array.isArray(this.type) ? this.type : [this])
  4070. .concat(Array.isArray(other.type) ? other.type : [other]);
  4071. return new BlockInfo(this.from, this.length + other.length, this.top, this.height + other.height, detail);
  4072. }
  4073. }
  4074. var QueryType = /*@__PURE__*/(function (QueryType) {
  4075. QueryType[QueryType["ByPos"] = 0] = "ByPos";
  4076. QueryType[QueryType["ByHeight"] = 1] = "ByHeight";
  4077. QueryType[QueryType["ByPosNoHeight"] = 2] = "ByPosNoHeight";
  4078. return QueryType})(QueryType || (QueryType = {}));
  4079. const Epsilon = 1e-3;
  4080. class HeightMap {
  4081. constructor(length, // The number of characters covered
  4082. height, // Height of this part of the document
  4083. flags = 2 /* Outdated */) {
  4084. this.length = length;
  4085. this.height = height;
  4086. this.flags = flags;
  4087. }
  4088. get outdated() { return (this.flags & 2 /* Outdated */) > 0; }
  4089. set outdated(value) { this.flags = (value ? 2 /* Outdated */ : 0) | (this.flags & ~2 /* Outdated */); }
  4090. setHeight(oracle, height) {
  4091. if (this.height != height) {
  4092. if (Math.abs(this.height - height) > Epsilon)
  4093. oracle.heightChanged = true;
  4094. this.height = height;
  4095. }
  4096. }
  4097. // Base case is to replace a leaf node, which simply builds a tree
  4098. // from the new nodes and returns that (HeightMapBranch and
  4099. // HeightMapGap override this to actually use from/to)
  4100. replace(_from, _to, nodes) {
  4101. return HeightMap.of(nodes);
  4102. }
  4103. // Again, these are base cases, and are overridden for branch and gap nodes.
  4104. decomposeLeft(_to, result) { result.push(this); }
  4105. decomposeRight(_from, result) { result.push(this); }
  4106. applyChanges(decorations, oldDoc, oracle, changes) {
  4107. let me = this;
  4108. for (let i = changes.length - 1; i >= 0; i--) {
  4109. let { fromA, toA, fromB, toB } = changes[i];
  4110. let start = me.lineAt(fromA, QueryType.ByPosNoHeight, oldDoc, 0, 0);
  4111. let end = start.to >= toA ? start : me.lineAt(toA, QueryType.ByPosNoHeight, oldDoc, 0, 0);
  4112. toB += end.to - toA;
  4113. toA = end.to;
  4114. while (i > 0 && start.from <= changes[i - 1].toA) {
  4115. fromA = changes[i - 1].fromA;
  4116. fromB = changes[i - 1].fromB;
  4117. i--;
  4118. if (fromA < start.from)
  4119. start = me.lineAt(fromA, QueryType.ByPosNoHeight, oldDoc, 0, 0);
  4120. }
  4121. fromB += start.from - fromA;
  4122. fromA = start.from;
  4123. let nodes = NodeBuilder.build(oracle, decorations, fromB, toB);
  4124. me = me.replace(fromA, toA, nodes);
  4125. }
  4126. return me.updateHeight(oracle, 0);
  4127. }
  4128. static empty() { return new HeightMapText(0, 0); }
  4129. // nodes uses null values to indicate the position of line breaks.
  4130. // There are never line breaks at the start or end of the array, or
  4131. // two line breaks next to each other, and the array isn't allowed
  4132. // to be empty (same restrictions as return value from the builder).
  4133. static of(nodes) {
  4134. if (nodes.length == 1)
  4135. return nodes[0];
  4136. let i = 0, j = nodes.length, before = 0, after = 0;
  4137. for (;;) {
  4138. if (i == j) {
  4139. if (before > after * 2) {
  4140. let split = nodes[i - 1];
  4141. if (split.break)
  4142. nodes.splice(--i, 1, split.left, null, split.right);
  4143. else
  4144. nodes.splice(--i, 1, split.left, split.right);
  4145. j += 1 + split.break;
  4146. before -= split.size;
  4147. }
  4148. else if (after > before * 2) {
  4149. let split = nodes[j];
  4150. if (split.break)
  4151. nodes.splice(j, 1, split.left, null, split.right);
  4152. else
  4153. nodes.splice(j, 1, split.left, split.right);
  4154. j += 2 + split.break;
  4155. after -= split.size;
  4156. }
  4157. else {
  4158. break;
  4159. }
  4160. }
  4161. else if (before < after) {
  4162. let next = nodes[i++];
  4163. if (next)
  4164. before += next.size;
  4165. }
  4166. else {
  4167. let next = nodes[--j];
  4168. if (next)
  4169. after += next.size;
  4170. }
  4171. }
  4172. let brk = 0;
  4173. if (nodes[i - 1] == null) {
  4174. brk = 1;
  4175. i--;
  4176. }
  4177. else if (nodes[i] == null) {
  4178. brk = 1;
  4179. j++;
  4180. }
  4181. return new HeightMapBranch(HeightMap.of(nodes.slice(0, i)), brk, HeightMap.of(nodes.slice(j)));
  4182. }
  4183. }
  4184. HeightMap.prototype.size = 1;
  4185. class HeightMapBlock extends HeightMap {
  4186. constructor(length, height, type) {
  4187. super(length, height);
  4188. this.type = type;
  4189. }
  4190. blockAt(_height, _doc, top, offset) {
  4191. return new BlockInfo(offset, this.length, top, this.height, this.type);
  4192. }
  4193. lineAt(_value, _type, doc, top, offset) {
  4194. return this.blockAt(0, doc, top, offset);
  4195. }
  4196. forEachLine(from, to, doc, top, offset, f) {
  4197. if (from <= offset + this.length && to >= offset)
  4198. f(this.blockAt(0, doc, top, offset));
  4199. }
  4200. updateHeight(oracle, offset = 0, _force = false, measured) {
  4201. if (measured && measured.from <= offset && measured.more)
  4202. this.setHeight(oracle, measured.heights[measured.index++]);
  4203. this.outdated = false;
  4204. return this;
  4205. }
  4206. toString() { return `block(${this.length})`; }
  4207. }
  4208. class HeightMapText extends HeightMapBlock {
  4209. constructor(length, height) {
  4210. super(length, height, BlockType.Text);
  4211. this.collapsed = 0; // Amount of collapsed content in the line
  4212. this.widgetHeight = 0; // Maximum inline widget height
  4213. }
  4214. replace(_from, _to, nodes) {
  4215. let node = nodes[0];
  4216. if (nodes.length == 1 && (node instanceof HeightMapText || node instanceof HeightMapGap && (node.flags & 4 /* SingleLine */)) &&
  4217. Math.abs(this.length - node.length) < 10) {
  4218. if (node instanceof HeightMapGap)
  4219. node = new HeightMapText(node.length, this.height);
  4220. else
  4221. node.height = this.height;
  4222. if (!this.outdated)
  4223. node.outdated = false;
  4224. return node;
  4225. }
  4226. else {
  4227. return HeightMap.of(nodes);
  4228. }
  4229. }
  4230. updateHeight(oracle, offset = 0, force = false, measured) {
  4231. if (measured && measured.from <= offset && measured.more)
  4232. this.setHeight(oracle, measured.heights[measured.index++]);
  4233. else if (force || this.outdated)
  4234. this.setHeight(oracle, Math.max(this.widgetHeight, oracle.heightForLine(this.length - this.collapsed)));
  4235. this.outdated = false;
  4236. return this;
  4237. }
  4238. toString() {
  4239. return `line(${this.length}${this.collapsed ? -this.collapsed : ""}${this.widgetHeight ? ":" + this.widgetHeight : ""})`;
  4240. }
  4241. }
  4242. class HeightMapGap extends HeightMap {
  4243. constructor(length) { super(length, 0); }
  4244. lines(doc, offset) {
  4245. let firstLine = doc.lineAt(offset).number, lastLine = doc.lineAt(offset + this.length).number;
  4246. return { firstLine, lastLine, lineHeight: this.height / (lastLine - firstLine + 1) };
  4247. }
  4248. blockAt(height, doc, top, offset) {
  4249. let { firstLine, lastLine, lineHeight } = this.lines(doc, offset);
  4250. let line = Math.max(0, Math.min(lastLine - firstLine, Math.floor((height - top) / lineHeight)));
  4251. let { from, length } = doc.line(firstLine + line);
  4252. return new BlockInfo(from, length, top + lineHeight * line, lineHeight, BlockType.Text);
  4253. }
  4254. lineAt(value, type, doc, top, offset) {
  4255. if (type == QueryType.ByHeight)
  4256. return this.blockAt(value, doc, top, offset);
  4257. if (type == QueryType.ByPosNoHeight) {
  4258. let { from, to } = doc.lineAt(value);
  4259. return new BlockInfo(from, to - from, 0, 0, BlockType.Text);
  4260. }
  4261. let { firstLine, lineHeight } = this.lines(doc, offset);
  4262. let { from, length, number } = doc.lineAt(value);
  4263. return new BlockInfo(from, length, top + lineHeight * (number - firstLine), lineHeight, BlockType.Text);
  4264. }
  4265. forEachLine(from, to, doc, top, offset, f) {
  4266. let { firstLine, lineHeight } = this.lines(doc, offset);
  4267. for (let pos = Math.max(from, offset), end = Math.min(offset + this.length, to); pos <= end;) {
  4268. let line = doc.lineAt(pos);
  4269. if (pos == from)
  4270. top += lineHeight * (line.number - firstLine);
  4271. f(new BlockInfo(line.from, line.length, top, lineHeight, BlockType.Text));
  4272. top += lineHeight;
  4273. pos = line.to + 1;
  4274. }
  4275. }
  4276. replace(from, to, nodes) {
  4277. let after = this.length - to;
  4278. if (after > 0) {
  4279. let last = nodes[nodes.length - 1];
  4280. if (last instanceof HeightMapGap)
  4281. nodes[nodes.length - 1] = new HeightMapGap(last.length + after);
  4282. else
  4283. nodes.push(null, new HeightMapGap(after - 1));
  4284. }
  4285. if (from > 0) {
  4286. let first = nodes[0];
  4287. if (first instanceof HeightMapGap)
  4288. nodes[0] = new HeightMapGap(from + first.length);
  4289. else
  4290. nodes.unshift(new HeightMapGap(from - 1), null);
  4291. }
  4292. return HeightMap.of(nodes);
  4293. }
  4294. decomposeLeft(to, result) {
  4295. result.push(new HeightMapGap(to - 1), null);
  4296. }
  4297. decomposeRight(from, result) {
  4298. result.push(null, new HeightMapGap(this.length - from - 1));
  4299. }
  4300. updateHeight(oracle, offset = 0, force = false, measured) {
  4301. let end = offset + this.length;
  4302. if (measured && measured.from <= offset + this.length && measured.more) {
  4303. // Fill in part of this gap with measured lines. We know there
  4304. // can't be widgets or collapsed ranges in those lines, because
  4305. // they would already have been added to the heightmap (gaps
  4306. // only contain plain text).
  4307. let nodes = [], pos = Math.max(offset, measured.from), singleHeight = -1;
  4308. let wasChanged = oracle.heightChanged;
  4309. if (measured.from > offset)
  4310. nodes.push(new HeightMapGap(measured.from - offset - 1).updateHeight(oracle, offset));
  4311. while (pos <= end && measured.more) {
  4312. let len = oracle.doc.lineAt(pos).length;
  4313. if (nodes.length)
  4314. nodes.push(null);
  4315. let height = measured.heights[measured.index++];
  4316. if (singleHeight == -1)
  4317. singleHeight = height;
  4318. else if (Math.abs(height - singleHeight) >= Epsilon)
  4319. singleHeight = -2;
  4320. let line = new HeightMapText(len, height);
  4321. line.outdated = false;
  4322. nodes.push(line);
  4323. pos += len + 1;
  4324. }
  4325. if (pos <= end)
  4326. nodes.push(null, new HeightMapGap(end - pos).updateHeight(oracle, pos));
  4327. let result = HeightMap.of(nodes);
  4328. oracle.heightChanged = wasChanged || singleHeight < 0 || Math.abs(result.height - this.height) >= Epsilon ||
  4329. Math.abs(singleHeight - this.lines(oracle.doc, offset).lineHeight) >= Epsilon;
  4330. return result;
  4331. }
  4332. else if (force || this.outdated) {
  4333. this.setHeight(oracle, oracle.heightForGap(offset, offset + this.length));
  4334. this.outdated = false;
  4335. }
  4336. return this;
  4337. }
  4338. toString() { return `gap(${this.length})`; }
  4339. }
  4340. class HeightMapBranch extends HeightMap {
  4341. constructor(left, brk, right) {
  4342. super(left.length + brk + right.length, left.height + right.height, brk | (left.outdated || right.outdated ? 2 /* Outdated */ : 0));
  4343. this.left = left;
  4344. this.right = right;
  4345. this.size = left.size + right.size;
  4346. }
  4347. get break() { return this.flags & 1 /* Break */; }
  4348. blockAt(height, doc, top, offset) {
  4349. let mid = top + this.left.height;
  4350. return height < mid ? this.left.blockAt(height, doc, top, offset)
  4351. : this.right.blockAt(height, doc, mid, offset + this.left.length + this.break);
  4352. }
  4353. lineAt(value, type, doc, top, offset) {
  4354. let rightTop = top + this.left.height, rightOffset = offset + this.left.length + this.break;
  4355. let left = type == QueryType.ByHeight ? value < rightTop : value < rightOffset;
  4356. let base = left ? this.left.lineAt(value, type, doc, top, offset)
  4357. : this.right.lineAt(value, type, doc, rightTop, rightOffset);
  4358. if (this.break || (left ? base.to < rightOffset : base.from > rightOffset))
  4359. return base;
  4360. let subQuery = type == QueryType.ByPosNoHeight ? QueryType.ByPosNoHeight : QueryType.ByPos;
  4361. if (left)
  4362. return base.join(this.right.lineAt(rightOffset, subQuery, doc, rightTop, rightOffset));
  4363. else
  4364. return this.left.lineAt(rightOffset, subQuery, doc, top, offset).join(base);
  4365. }
  4366. forEachLine(from, to, doc, top, offset, f) {
  4367. let rightTop = top + this.left.height, rightOffset = offset + this.left.length + this.break;
  4368. if (this.break) {
  4369. if (from < rightOffset)
  4370. this.left.forEachLine(from, to, doc, top, offset, f);
  4371. if (to >= rightOffset)
  4372. this.right.forEachLine(from, to, doc, rightTop, rightOffset, f);
  4373. }
  4374. else {
  4375. let mid = this.lineAt(rightOffset, QueryType.ByPos, doc, top, offset);
  4376. if (from < mid.from)
  4377. this.left.forEachLine(from, mid.from - 1, doc, top, offset, f);
  4378. if (mid.to >= from && mid.from <= to)
  4379. f(mid);
  4380. if (to > mid.to)
  4381. this.right.forEachLine(mid.to + 1, to, doc, rightTop, rightOffset, f);
  4382. }
  4383. }
  4384. replace(from, to, nodes) {
  4385. let rightStart = this.left.length + this.break;
  4386. if (to < rightStart)
  4387. return this.balanced(this.left.replace(from, to, nodes), this.right);
  4388. if (from > this.left.length)
  4389. return this.balanced(this.left, this.right.replace(from - rightStart, to - rightStart, nodes));
  4390. let result = [];
  4391. if (from > 0)
  4392. this.decomposeLeft(from, result);
  4393. let left = result.length;
  4394. for (let node of nodes)
  4395. result.push(node);
  4396. if (from > 0)
  4397. mergeGaps(result, left - 1);
  4398. if (to < this.length) {
  4399. let right = result.length;
  4400. this.decomposeRight(to, result);
  4401. mergeGaps(result, right);
  4402. }
  4403. return HeightMap.of(result);
  4404. }
  4405. decomposeLeft(to, result) {
  4406. let left = this.left.length;
  4407. if (to <= left)
  4408. return this.left.decomposeLeft(to, result);
  4409. result.push(this.left);
  4410. if (this.break) {
  4411. left++;
  4412. if (to >= left)
  4413. result.push(null);
  4414. }
  4415. if (to > left)
  4416. this.right.decomposeLeft(to - left, result);
  4417. }
  4418. decomposeRight(from, result) {
  4419. let left = this.left.length, right = left + this.break;
  4420. if (from >= right)
  4421. return this.right.decomposeRight(from - right, result);
  4422. if (from < left)
  4423. this.left.decomposeRight(from, result);
  4424. if (this.break && from < right)
  4425. result.push(null);
  4426. result.push(this.right);
  4427. }
  4428. balanced(left, right) {
  4429. if (left.size > 2 * right.size || right.size > 2 * left.size)
  4430. return HeightMap.of(this.break ? [left, null, right] : [left, right]);
  4431. this.left = left;
  4432. this.right = right;
  4433. this.height = left.height + right.height;
  4434. this.outdated = left.outdated || right.outdated;
  4435. this.size = left.size + right.size;
  4436. this.length = left.length + this.break + right.length;
  4437. return this;
  4438. }
  4439. updateHeight(oracle, offset = 0, force = false, measured) {
  4440. let { left, right } = this, rightStart = offset + left.length + this.break, rebalance = null;
  4441. if (measured && measured.from <= offset + left.length && measured.more)
  4442. rebalance = left = left.updateHeight(oracle, offset, force, measured);
  4443. else
  4444. left.updateHeight(oracle, offset, force);
  4445. if (measured && measured.from <= rightStart + right.length && measured.more)
  4446. rebalance = right = right.updateHeight(oracle, rightStart, force, measured);
  4447. else
  4448. right.updateHeight(oracle, rightStart, force);
  4449. if (rebalance)
  4450. return this.balanced(left, right);
  4451. this.height = this.left.height + this.right.height;
  4452. this.outdated = false;
  4453. return this;
  4454. }
  4455. toString() { return this.left + (this.break ? " " : "-") + this.right; }
  4456. }
  4457. function mergeGaps(nodes, around) {
  4458. let before, after;
  4459. if (nodes[around] == null &&
  4460. (before = nodes[around - 1]) instanceof HeightMapGap &&
  4461. (after = nodes[around + 1]) instanceof HeightMapGap)
  4462. nodes.splice(around - 1, 3, new HeightMapGap(before.length + 1 + after.length));
  4463. }
  4464. const relevantWidgetHeight = 5;
  4465. class NodeBuilder {
  4466. constructor(pos, oracle) {
  4467. this.pos = pos;
  4468. this.oracle = oracle;
  4469. this.nodes = [];
  4470. this.lineStart = -1;
  4471. this.lineEnd = -1;
  4472. this.covering = null;
  4473. this.writtenTo = pos;
  4474. }
  4475. get isCovered() {
  4476. return this.covering && this.nodes[this.nodes.length - 1] == this.covering;
  4477. }
  4478. span(_from, to) {
  4479. if (this.lineStart > -1) {
  4480. let end = Math.min(to, this.lineEnd), last = this.nodes[this.nodes.length - 1];
  4481. if (last instanceof HeightMapText)
  4482. last.length += end - this.pos;
  4483. else if (end > this.pos || !this.isCovered)
  4484. this.nodes.push(new HeightMapText(end - this.pos, -1));
  4485. this.writtenTo = end;
  4486. if (to > end) {
  4487. this.nodes.push(null);
  4488. this.writtenTo++;
  4489. this.lineStart = -1;
  4490. }
  4491. }
  4492. this.pos = to;
  4493. }
  4494. point(from, to, deco) {
  4495. if (from < to || deco.heightRelevant) {
  4496. let height = deco.widget ? deco.widget.estimatedHeight : 0;
  4497. if (height < 0)
  4498. height = this.oracle.lineHeight;
  4499. let len = to - from;
  4500. if (deco.block) {
  4501. this.addBlock(new HeightMapBlock(len, height, deco.type));
  4502. }
  4503. else if (len || height >= relevantWidgetHeight) {
  4504. this.addLineDeco(height, len);
  4505. }
  4506. }
  4507. else if (to > from) {
  4508. this.span(from, to);
  4509. }
  4510. if (this.lineEnd > -1 && this.lineEnd < this.pos)
  4511. this.lineEnd = this.oracle.doc.lineAt(this.pos).to;
  4512. }
  4513. enterLine() {
  4514. if (this.lineStart > -1)
  4515. return;
  4516. let { from, to } = this.oracle.doc.lineAt(this.pos);
  4517. this.lineStart = from;
  4518. this.lineEnd = to;
  4519. if (this.writtenTo < from) {
  4520. if (this.writtenTo < from - 1 || this.nodes[this.nodes.length - 1] == null)
  4521. this.nodes.push(this.blankContent(this.writtenTo, from - 1));
  4522. this.nodes.push(null);
  4523. }
  4524. if (this.pos > from)
  4525. this.nodes.push(new HeightMapText(this.pos - from, -1));
  4526. this.writtenTo = this.pos;
  4527. }
  4528. blankContent(from, to) {
  4529. let gap = new HeightMapGap(to - from);
  4530. if (this.oracle.doc.lineAt(from).to == to)
  4531. gap.flags |= 4 /* SingleLine */;
  4532. return gap;
  4533. }
  4534. ensureLine() {
  4535. this.enterLine();
  4536. let last = this.nodes.length ? this.nodes[this.nodes.length - 1] : null;
  4537. if (last instanceof HeightMapText)
  4538. return last;
  4539. let line = new HeightMapText(0, -1);
  4540. this.nodes.push(line);
  4541. return line;
  4542. }
  4543. addBlock(block) {
  4544. this.enterLine();
  4545. if (block.type == BlockType.WidgetAfter && !this.isCovered)
  4546. this.ensureLine();
  4547. this.nodes.push(block);
  4548. this.writtenTo = this.pos = this.pos + block.length;
  4549. if (block.type != BlockType.WidgetBefore)
  4550. this.covering = block;
  4551. }
  4552. addLineDeco(height, length) {
  4553. let line = this.ensureLine();
  4554. line.length += length;
  4555. line.collapsed += length;
  4556. line.widgetHeight = Math.max(line.widgetHeight, height);
  4557. this.writtenTo = this.pos = this.pos + length;
  4558. }
  4559. finish(from) {
  4560. let last = this.nodes.length == 0 ? null : this.nodes[this.nodes.length - 1];
  4561. if (this.lineStart > -1 && !(last instanceof HeightMapText) && !this.isCovered)
  4562. this.nodes.push(new HeightMapText(0, -1));
  4563. else if (this.writtenTo < this.pos || last == null)
  4564. this.nodes.push(this.blankContent(this.writtenTo, this.pos));
  4565. let pos = from;
  4566. for (let node of this.nodes) {
  4567. if (node instanceof HeightMapText)
  4568. node.updateHeight(this.oracle, pos);
  4569. pos += node ? node.length : 1;
  4570. }
  4571. return this.nodes;
  4572. }
  4573. // Always called with a region that on both sides either stretches
  4574. // to a line break or the end of the document.
  4575. // The returned array uses null to indicate line breaks, but never
  4576. // starts or ends in a line break, or has multiple line breaks next
  4577. // to each other.
  4578. static build(oracle, decorations, from, to) {
  4579. let builder = new NodeBuilder(from, oracle);
  4580. RangeSet.spans(decorations, from, to, builder, 0);
  4581. return builder.finish(from);
  4582. }
  4583. }
  4584. function heightRelevantDecoChanges(a, b, diff) {
  4585. let comp = new DecorationComparator;
  4586. RangeSet.compare(a, b, diff, comp, 0);
  4587. return comp.changes;
  4588. }
  4589. class DecorationComparator {
  4590. constructor() {
  4591. this.changes = [];
  4592. }
  4593. compareRange() { }
  4594. comparePoint(from, to, a, b) {
  4595. if (from < to || a && a.heightRelevant || b && b.heightRelevant)
  4596. addRange(from, to, this.changes, 5);
  4597. }
  4598. }
  4599. function visiblePixelRange(dom, paddingTop) {
  4600. let rect = dom.getBoundingClientRect();
  4601. let left = Math.max(0, rect.left), right = Math.min(innerWidth, rect.right);
  4602. let top = Math.max(0, rect.top), bottom = Math.min(innerHeight, rect.bottom);
  4603. let body = dom.ownerDocument.body;
  4604. for (let parent = dom.parentNode; parent && parent != body;) {
  4605. if (parent.nodeType == 1) {
  4606. let elt = parent;
  4607. let style = window.getComputedStyle(elt);
  4608. if ((elt.scrollHeight > elt.clientHeight || elt.scrollWidth > elt.clientWidth) &&
  4609. style.overflow != "visible") {
  4610. let parentRect = elt.getBoundingClientRect();
  4611. left = Math.max(left, parentRect.left);
  4612. right = Math.min(right, parentRect.right);
  4613. top = Math.max(top, parentRect.top);
  4614. bottom = Math.min(bottom, parentRect.bottom);
  4615. }
  4616. parent = style.position == "absolute" || style.position == "fixed" ? elt.offsetParent : elt.parentNode;
  4617. }
  4618. else if (parent.nodeType == 11) { // Shadow root
  4619. parent = parent.host;
  4620. }
  4621. else {
  4622. break;
  4623. }
  4624. }
  4625. return { left: left - rect.left, right: Math.max(left, right) - rect.left,
  4626. top: top - (rect.top + paddingTop), bottom: Math.max(top, bottom) - (rect.top + paddingTop) };
  4627. }
  4628. function fullPixelRange(dom, paddingTop) {
  4629. let rect = dom.getBoundingClientRect();
  4630. return { left: 0, right: rect.right - rect.left,
  4631. top: paddingTop, bottom: rect.bottom - (rect.top + paddingTop) };
  4632. }
  4633. // Line gaps are placeholder widgets used to hide pieces of overlong
  4634. // lines within the viewport, as a kludge to keep the editor
  4635. // responsive when a ridiculously long line is loaded into it.
  4636. class LineGap {
  4637. constructor(from, to, size) {
  4638. this.from = from;
  4639. this.to = to;
  4640. this.size = size;
  4641. }
  4642. static same(a, b) {
  4643. if (a.length != b.length)
  4644. return false;
  4645. for (let i = 0; i < a.length; i++) {
  4646. let gA = a[i], gB = b[i];
  4647. if (gA.from != gB.from || gA.to != gB.to || gA.size != gB.size)
  4648. return false;
  4649. }
  4650. return true;
  4651. }
  4652. draw(wrapping) {
  4653. return Decoration.replace({ widget: new LineGapWidget(this.size, wrapping) }).range(this.from, this.to);
  4654. }
  4655. }
  4656. class LineGapWidget extends WidgetType {
  4657. constructor(size, vertical) {
  4658. super();
  4659. this.size = size;
  4660. this.vertical = vertical;
  4661. }
  4662. eq(other) { return other.size == this.size && other.vertical == this.vertical; }
  4663. toDOM() {
  4664. let elt = document.createElement("div");
  4665. if (this.vertical) {
  4666. elt.style.height = this.size + "px";
  4667. }
  4668. else {
  4669. elt.style.width = this.size + "px";
  4670. elt.style.height = "2px";
  4671. elt.style.display = "inline-block";
  4672. }
  4673. return elt;
  4674. }
  4675. get estimatedHeight() { return this.vertical ? this.size : -1; }
  4676. }
  4677. class ViewState {
  4678. constructor(state) {
  4679. this.state = state;
  4680. // These are contentDOM-local coordinates
  4681. this.pixelViewport = { left: 0, right: window.innerWidth, top: 0, bottom: 0 };
  4682. this.inView = true;
  4683. this.paddingTop = 0;
  4684. this.paddingBottom = 0;
  4685. this.contentDOMWidth = 0;
  4686. this.contentDOMHeight = 0;
  4687. this.editorHeight = 0;
  4688. this.editorWidth = 0;
  4689. this.heightOracle = new HeightOracle;
  4690. // See VP.MaxDOMHeight
  4691. this.scaler = IdScaler;
  4692. this.scrollTarget = null;
  4693. // Briefly set to true when printing, to disable viewport limiting
  4694. this.printing = false;
  4695. // Flag set when editor content was redrawn, so that the next
  4696. // measure stage knows it must read DOM layout
  4697. this.mustMeasureContent = true;
  4698. this.defaultTextDirection = Direction.RTL;
  4699. this.visibleRanges = [];
  4700. // Cursor 'assoc' is only significant when the cursor is on a line
  4701. // wrap point, where it must stick to the character that it is
  4702. // associated with. Since browsers don't provide a reasonable
  4703. // interface to set or query this, when a selection is set that
  4704. // might cause this to be significant, this flag is set. The next
  4705. // measure phase will check whether the cursor is on a line-wrapping
  4706. // boundary and, if so, reset it to make sure it is positioned in
  4707. // the right place.
  4708. this.mustEnforceCursorAssoc = false;
  4709. this.stateDeco = state.facet(decorations).filter(d => typeof d != "function");
  4710. this.heightMap = HeightMap.empty().applyChanges(this.stateDeco, Text.empty, this.heightOracle.setDoc(state.doc), [new ChangedRange(0, 0, 0, state.doc.length)]);
  4711. this.viewport = this.getViewport(0, null);
  4712. this.updateViewportLines();
  4713. this.updateForViewport();
  4714. this.lineGaps = this.ensureLineGaps([]);
  4715. this.lineGapDeco = Decoration.set(this.lineGaps.map(gap => gap.draw(false)));
  4716. this.computeVisibleRanges();
  4717. }
  4718. updateForViewport() {
  4719. let viewports = [this.viewport], { main } = this.state.selection;
  4720. for (let i = 0; i <= 1; i++) {
  4721. let pos = i ? main.head : main.anchor;
  4722. if (!viewports.some(({ from, to }) => pos >= from && pos <= to)) {
  4723. let { from, to } = this.lineBlockAt(pos);
  4724. viewports.push(new Viewport(from, to));
  4725. }
  4726. }
  4727. this.viewports = viewports.sort((a, b) => a.from - b.from);
  4728. this.scaler = this.heightMap.height <= 7000000 /* MaxDOMHeight */ ? IdScaler :
  4729. new BigScaler(this.heightOracle.doc, this.heightMap, this.viewports);
  4730. }
  4731. updateViewportLines() {
  4732. this.viewportLines = [];
  4733. this.heightMap.forEachLine(this.viewport.from, this.viewport.to, this.state.doc, 0, 0, block => {
  4734. this.viewportLines.push(this.scaler.scale == 1 ? block : scaleBlock(block, this.scaler));
  4735. });
  4736. }
  4737. update(update, scrollTarget = null) {
  4738. this.state = update.state;
  4739. let prevDeco = this.stateDeco;
  4740. this.stateDeco = this.state.facet(decorations).filter(d => typeof d != "function");
  4741. let contentChanges = update.changedRanges;
  4742. let heightChanges = ChangedRange.extendWithRanges(contentChanges, heightRelevantDecoChanges(prevDeco, this.stateDeco, update ? update.changes : ChangeSet.empty(this.state.doc.length)));
  4743. let prevHeight = this.heightMap.height;
  4744. this.heightMap = this.heightMap.applyChanges(this.stateDeco, update.startState.doc, this.heightOracle.setDoc(this.state.doc), heightChanges);
  4745. if (this.heightMap.height != prevHeight)
  4746. update.flags |= 2 /* Height */;
  4747. let viewport = heightChanges.length ? this.mapViewport(this.viewport, update.changes) : this.viewport;
  4748. if (scrollTarget && (scrollTarget.range.head < viewport.from || scrollTarget.range.head > viewport.to) ||
  4749. !this.viewportIsAppropriate(viewport))
  4750. viewport = this.getViewport(0, scrollTarget);
  4751. let updateLines = !update.changes.empty || (update.flags & 2 /* Height */) ||
  4752. viewport.from != this.viewport.from || viewport.to != this.viewport.to;
  4753. this.viewport = viewport;
  4754. this.updateForViewport();
  4755. if (updateLines)
  4756. this.updateViewportLines();
  4757. if (this.lineGaps.length || this.viewport.to - this.viewport.from > 4000 /* DoubleMargin */)
  4758. this.updateLineGaps(this.ensureLineGaps(this.mapLineGaps(this.lineGaps, update.changes)));
  4759. update.flags |= this.computeVisibleRanges();
  4760. if (scrollTarget)
  4761. this.scrollTarget = scrollTarget;
  4762. if (!this.mustEnforceCursorAssoc && update.selectionSet && update.view.lineWrapping &&
  4763. update.state.selection.main.empty && update.state.selection.main.assoc)
  4764. this.mustEnforceCursorAssoc = true;
  4765. }
  4766. measure(view) {
  4767. let dom = view.contentDOM, style = window.getComputedStyle(dom);
  4768. let oracle = this.heightOracle;
  4769. let whiteSpace = style.whiteSpace;
  4770. this.defaultTextDirection = style.direction == "rtl" ? Direction.RTL : Direction.LTR;
  4771. let refresh = this.heightOracle.mustRefreshForWrapping(whiteSpace);
  4772. let measureContent = refresh || this.mustMeasureContent || this.contentDOMHeight != dom.clientHeight;
  4773. this.contentDOMHeight = dom.clientHeight;
  4774. this.mustMeasureContent = false;
  4775. let result = 0, bias = 0;
  4776. // Vertical padding
  4777. let paddingTop = parseInt(style.paddingTop) || 0, paddingBottom = parseInt(style.paddingBottom) || 0;
  4778. if (this.paddingTop != paddingTop || this.paddingBottom != paddingBottom) {
  4779. this.paddingTop = paddingTop;
  4780. this.paddingBottom = paddingBottom;
  4781. result |= 8 /* Geometry */ | 2 /* Height */;
  4782. }
  4783. if (this.editorWidth != view.scrollDOM.clientWidth) {
  4784. if (oracle.lineWrapping)
  4785. measureContent = true;
  4786. this.editorWidth = view.scrollDOM.clientWidth;
  4787. result |= 8 /* Geometry */;
  4788. }
  4789. // Pixel viewport
  4790. let pixelViewport = (this.printing ? fullPixelRange : visiblePixelRange)(dom, this.paddingTop);
  4791. let dTop = pixelViewport.top - this.pixelViewport.top, dBottom = pixelViewport.bottom - this.pixelViewport.bottom;
  4792. this.pixelViewport = pixelViewport;
  4793. let inView = this.pixelViewport.bottom > this.pixelViewport.top && this.pixelViewport.right > this.pixelViewport.left;
  4794. if (inView != this.inView) {
  4795. this.inView = inView;
  4796. if (inView)
  4797. measureContent = true;
  4798. }
  4799. if (!this.inView)
  4800. return 0;
  4801. let contentWidth = dom.clientWidth;
  4802. if (this.contentDOMWidth != contentWidth || this.editorHeight != view.scrollDOM.clientHeight) {
  4803. this.contentDOMWidth = contentWidth;
  4804. this.editorHeight = view.scrollDOM.clientHeight;
  4805. result |= 8 /* Geometry */;
  4806. }
  4807. if (measureContent) {
  4808. let lineHeights = view.docView.measureVisibleLineHeights(this.viewport);
  4809. if (oracle.mustRefreshForHeights(lineHeights))
  4810. refresh = true;
  4811. if (refresh || oracle.lineWrapping && Math.abs(contentWidth - this.contentDOMWidth) > oracle.charWidth) {
  4812. let { lineHeight, charWidth } = view.docView.measureTextSize();
  4813. refresh = oracle.refresh(whiteSpace, lineHeight, charWidth, contentWidth / charWidth, lineHeights);
  4814. if (refresh) {
  4815. view.docView.minWidth = 0;
  4816. result |= 8 /* Geometry */;
  4817. }
  4818. }
  4819. if (dTop > 0 && dBottom > 0)
  4820. bias = Math.max(dTop, dBottom);
  4821. else if (dTop < 0 && dBottom < 0)
  4822. bias = Math.min(dTop, dBottom);
  4823. oracle.heightChanged = false;
  4824. for (let vp of this.viewports) {
  4825. let heights = vp.from == this.viewport.from ? lineHeights : view.docView.measureVisibleLineHeights(vp);
  4826. this.heightMap = this.heightMap.updateHeight(oracle, 0, refresh, new MeasuredHeights(vp.from, heights));
  4827. }
  4828. if (oracle.heightChanged)
  4829. result |= 2 /* Height */;
  4830. }
  4831. let viewportChange = !this.viewportIsAppropriate(this.viewport, bias) ||
  4832. this.scrollTarget && (this.scrollTarget.range.head < this.viewport.from || this.scrollTarget.range.head > this.viewport.to);
  4833. if (viewportChange)
  4834. this.viewport = this.getViewport(bias, this.scrollTarget);
  4835. this.updateForViewport();
  4836. if ((result & 2 /* Height */) || viewportChange)
  4837. this.updateViewportLines();
  4838. if (this.lineGaps.length || this.viewport.to - this.viewport.from > 4000 /* DoubleMargin */)
  4839. this.updateLineGaps(this.ensureLineGaps(refresh ? [] : this.lineGaps));
  4840. result |= this.computeVisibleRanges();
  4841. if (this.mustEnforceCursorAssoc) {
  4842. this.mustEnforceCursorAssoc = false;
  4843. // This is done in the read stage, because moving the selection
  4844. // to a line end is going to trigger a layout anyway, so it
  4845. // can't be a pure write. It should be rare that it does any
  4846. // writing.
  4847. view.docView.enforceCursorAssoc();
  4848. }
  4849. return result;
  4850. }
  4851. get visibleTop() { return this.scaler.fromDOM(this.pixelViewport.top); }
  4852. get visibleBottom() { return this.scaler.fromDOM(this.pixelViewport.bottom); }
  4853. getViewport(bias, scrollTarget) {
  4854. // This will divide VP.Margin between the top and the
  4855. // bottom, depending on the bias (the change in viewport position
  4856. // since the last update). It'll hold a number between 0 and 1
  4857. let marginTop = 0.5 - Math.max(-0.5, Math.min(0.5, bias / 1000 /* Margin */ / 2));
  4858. let map = this.heightMap, doc = this.state.doc, { visibleTop, visibleBottom } = this;
  4859. let viewport = new Viewport(map.lineAt(visibleTop - marginTop * 1000 /* Margin */, QueryType.ByHeight, doc, 0, 0).from, map.lineAt(visibleBottom + (1 - marginTop) * 1000 /* Margin */, QueryType.ByHeight, doc, 0, 0).to);
  4860. // If scrollTarget is given, make sure the viewport includes that position
  4861. if (scrollTarget) {
  4862. let { head } = scrollTarget.range;
  4863. if (head < viewport.from || head > viewport.to) {
  4864. let viewHeight = Math.min(this.editorHeight, this.pixelViewport.bottom - this.pixelViewport.top);
  4865. let block = map.lineAt(head, QueryType.ByPos, doc, 0, 0), topPos;
  4866. if (scrollTarget.y == "center")
  4867. topPos = (block.top + block.bottom) / 2 - viewHeight / 2;
  4868. else if (scrollTarget.y == "start" || scrollTarget.y == "nearest" && head < viewport.from)
  4869. topPos = block.top;
  4870. else
  4871. topPos = block.bottom - viewHeight;
  4872. viewport = new Viewport(map.lineAt(topPos - 1000 /* Margin */ / 2, QueryType.ByHeight, doc, 0, 0).from, map.lineAt(topPos + viewHeight + 1000 /* Margin */ / 2, QueryType.ByHeight, doc, 0, 0).to);
  4873. }
  4874. }
  4875. return viewport;
  4876. }
  4877. mapViewport(viewport, changes) {
  4878. let from = changes.mapPos(viewport.from, -1), to = changes.mapPos(viewport.to, 1);
  4879. return new Viewport(this.heightMap.lineAt(from, QueryType.ByPos, this.state.doc, 0, 0).from, this.heightMap.lineAt(to, QueryType.ByPos, this.state.doc, 0, 0).to);
  4880. }
  4881. // Checks if a given viewport covers the visible part of the
  4882. // document and not too much beyond that.
  4883. viewportIsAppropriate({ from, to }, bias = 0) {
  4884. if (!this.inView)
  4885. return true;
  4886. let { top } = this.heightMap.lineAt(from, QueryType.ByPos, this.state.doc, 0, 0);
  4887. let { bottom } = this.heightMap.lineAt(to, QueryType.ByPos, this.state.doc, 0, 0);
  4888. let { visibleTop, visibleBottom } = this;
  4889. return (from == 0 || top <= visibleTop - Math.max(10 /* MinCoverMargin */, Math.min(-bias, 250 /* MaxCoverMargin */))) &&
  4890. (to == this.state.doc.length ||
  4891. bottom >= visibleBottom + Math.max(10 /* MinCoverMargin */, Math.min(bias, 250 /* MaxCoverMargin */))) &&
  4892. (top > visibleTop - 2 * 1000 /* Margin */ && bottom < visibleBottom + 2 * 1000 /* Margin */);
  4893. }
  4894. mapLineGaps(gaps, changes) {
  4895. if (!gaps.length || changes.empty)
  4896. return gaps;
  4897. let mapped = [];
  4898. for (let gap of gaps)
  4899. if (!changes.touchesRange(gap.from, gap.to))
  4900. mapped.push(new LineGap(changes.mapPos(gap.from), changes.mapPos(gap.to), gap.size));
  4901. return mapped;
  4902. }
  4903. // Computes positions in the viewport where the start or end of a
  4904. // line should be hidden, trying to reuse existing line gaps when
  4905. // appropriate to avoid unneccesary redraws.
  4906. // Uses crude character-counting for the positioning and sizing,
  4907. // since actual DOM coordinates aren't always available and
  4908. // predictable. Relies on generous margins (see LG.Margin) to hide
  4909. // the artifacts this might produce from the user.
  4910. ensureLineGaps(current) {
  4911. let gaps = [];
  4912. // This won't work at all in predominantly right-to-left text.
  4913. if (this.defaultTextDirection != Direction.LTR)
  4914. return gaps;
  4915. for (let line of this.viewportLines) {
  4916. if (line.length < 4000 /* DoubleMargin */)
  4917. continue;
  4918. let structure = lineStructure(line.from, line.to, this.stateDeco);
  4919. if (structure.total < 4000 /* DoubleMargin */)
  4920. continue;
  4921. let viewFrom, viewTo;
  4922. if (this.heightOracle.lineWrapping) {
  4923. let marginHeight = (2000 /* Margin */ / this.heightOracle.lineLength) * this.heightOracle.lineHeight;
  4924. viewFrom = findPosition(structure, (this.visibleTop - line.top - marginHeight) / line.height);
  4925. viewTo = findPosition(structure, (this.visibleBottom - line.top + marginHeight) / line.height);
  4926. }
  4927. else {
  4928. let totalWidth = structure.total * this.heightOracle.charWidth;
  4929. let marginWidth = 2000 /* Margin */ * this.heightOracle.charWidth;
  4930. viewFrom = findPosition(structure, (this.pixelViewport.left - marginWidth) / totalWidth);
  4931. viewTo = findPosition(structure, (this.pixelViewport.right + marginWidth) / totalWidth);
  4932. }
  4933. let outside = [];
  4934. if (viewFrom > line.from)
  4935. outside.push({ from: line.from, to: viewFrom });
  4936. if (viewTo < line.to)
  4937. outside.push({ from: viewTo, to: line.to });
  4938. let sel = this.state.selection.main;
  4939. // Make sure the gaps don't cover a selection end
  4940. if (sel.from >= line.from && sel.from <= line.to)
  4941. cutRange(outside, sel.from - 10 /* SelectionMargin */, sel.from + 10 /* SelectionMargin */);
  4942. if (!sel.empty && sel.to >= line.from && sel.to <= line.to)
  4943. cutRange(outside, sel.to - 10 /* SelectionMargin */, sel.to + 10 /* SelectionMargin */);
  4944. for (let { from, to } of outside)
  4945. if (to - from > 1000 /* HalfMargin */) {
  4946. gaps.push(find(current, gap => gap.from >= line.from && gap.to <= line.to &&
  4947. Math.abs(gap.from - from) < 1000 /* HalfMargin */ && Math.abs(gap.to - to) < 1000 /* HalfMargin */) ||
  4948. new LineGap(from, to, this.gapSize(line, from, to, structure)));
  4949. }
  4950. }
  4951. return gaps;
  4952. }
  4953. gapSize(line, from, to, structure) {
  4954. let fraction = findFraction(structure, to) - findFraction(structure, from);
  4955. if (this.heightOracle.lineWrapping) {
  4956. return line.height * fraction;
  4957. }
  4958. else {
  4959. return structure.total * this.heightOracle.charWidth * fraction;
  4960. }
  4961. }
  4962. updateLineGaps(gaps) {
  4963. if (!LineGap.same(gaps, this.lineGaps)) {
  4964. this.lineGaps = gaps;
  4965. this.lineGapDeco = Decoration.set(gaps.map(gap => gap.draw(this.heightOracle.lineWrapping)));
  4966. }
  4967. }
  4968. computeVisibleRanges() {
  4969. let deco = this.stateDeco;
  4970. if (this.lineGaps.length)
  4971. deco = deco.concat(this.lineGapDeco);
  4972. let ranges = [];
  4973. RangeSet.spans(deco, this.viewport.from, this.viewport.to, {
  4974. span(from, to) { ranges.push({ from, to }); },
  4975. point() { }
  4976. }, 20);
  4977. let changed = ranges.length != this.visibleRanges.length ||
  4978. this.visibleRanges.some((r, i) => r.from != ranges[i].from || r.to != ranges[i].to);
  4979. this.visibleRanges = ranges;
  4980. return changed ? 4 /* Viewport */ : 0;
  4981. }
  4982. lineBlockAt(pos) {
  4983. return (pos >= this.viewport.from && pos <= this.viewport.to && this.viewportLines.find(b => b.from <= pos && b.to >= pos)) ||
  4984. scaleBlock(this.heightMap.lineAt(pos, QueryType.ByPos, this.state.doc, 0, 0), this.scaler);
  4985. }
  4986. lineBlockAtHeight(height) {
  4987. return scaleBlock(this.heightMap.lineAt(this.scaler.fromDOM(height), QueryType.ByHeight, this.state.doc, 0, 0), this.scaler);
  4988. }
  4989. elementAtHeight(height) {
  4990. return scaleBlock(this.heightMap.blockAt(this.scaler.fromDOM(height), this.state.doc, 0, 0), this.scaler);
  4991. }
  4992. get docHeight() {
  4993. return this.scaler.toDOM(this.heightMap.height);
  4994. }
  4995. get contentHeight() {
  4996. return this.docHeight + this.paddingTop + this.paddingBottom;
  4997. }
  4998. }
  4999. class Viewport {
  5000. constructor(from, to) {
  5001. this.from = from;
  5002. this.to = to;
  5003. }
  5004. }
  5005. function lineStructure(from, to, stateDeco) {
  5006. let ranges = [], pos = from, total = 0;
  5007. RangeSet.spans(stateDeco, from, to, {
  5008. span() { },
  5009. point(from, to) {
  5010. if (from > pos) {
  5011. ranges.push({ from: pos, to: from });
  5012. total += from - pos;
  5013. }
  5014. pos = to;
  5015. }
  5016. }, 20); // We're only interested in collapsed ranges of a significant size
  5017. if (pos < to) {
  5018. ranges.push({ from: pos, to });
  5019. total += to - pos;
  5020. }
  5021. return { total, ranges };
  5022. }
  5023. function findPosition({ total, ranges }, ratio) {
  5024. if (ratio <= 0)
  5025. return ranges[0].from;
  5026. if (ratio >= 1)
  5027. return ranges[ranges.length - 1].to;
  5028. let dist = Math.floor(total * ratio);
  5029. for (let i = 0;; i++) {
  5030. let { from, to } = ranges[i], size = to - from;
  5031. if (dist <= size)
  5032. return from + dist;
  5033. dist -= size;
  5034. }
  5035. }
  5036. function findFraction(structure, pos) {
  5037. let counted = 0;
  5038. for (let { from, to } of structure.ranges) {
  5039. if (pos <= to) {
  5040. counted += pos - from;
  5041. break;
  5042. }
  5043. counted += to - from;
  5044. }
  5045. return counted / structure.total;
  5046. }
  5047. function cutRange(ranges, from, to) {
  5048. for (let i = 0; i < ranges.length; i++) {
  5049. let r = ranges[i];
  5050. if (r.from < to && r.to > from) {
  5051. let pieces = [];
  5052. if (r.from < from)
  5053. pieces.push({ from: r.from, to: from });
  5054. if (r.to > to)
  5055. pieces.push({ from: to, to: r.to });
  5056. ranges.splice(i, 1, ...pieces);
  5057. i += pieces.length - 1;
  5058. }
  5059. }
  5060. }
  5061. function find(array, f) {
  5062. for (let val of array)
  5063. if (f(val))
  5064. return val;
  5065. return undefined;
  5066. }
  5067. // Don't scale when the document height is within the range of what
  5068. // the DOM can handle.
  5069. const IdScaler = {
  5070. toDOM(n) { return n; },
  5071. fromDOM(n) { return n; },
  5072. scale: 1
  5073. };
  5074. // When the height is too big (> VP.MaxDOMHeight), scale down the
  5075. // regions outside the viewports so that the total height is
  5076. // VP.MaxDOMHeight.
  5077. class BigScaler {
  5078. constructor(doc, heightMap, viewports) {
  5079. let vpHeight = 0, base = 0, domBase = 0;
  5080. this.viewports = viewports.map(({ from, to }) => {
  5081. let top = heightMap.lineAt(from, QueryType.ByPos, doc, 0, 0).top;
  5082. let bottom = heightMap.lineAt(to, QueryType.ByPos, doc, 0, 0).bottom;
  5083. vpHeight += bottom - top;
  5084. return { from, to, top, bottom, domTop: 0, domBottom: 0 };
  5085. });
  5086. this.scale = (7000000 /* MaxDOMHeight */ - vpHeight) / (heightMap.height - vpHeight);
  5087. for (let obj of this.viewports) {
  5088. obj.domTop = domBase + (obj.top - base) * this.scale;
  5089. domBase = obj.domBottom = obj.domTop + (obj.bottom - obj.top);
  5090. base = obj.bottom;
  5091. }
  5092. }
  5093. toDOM(n) {
  5094. for (let i = 0, base = 0, domBase = 0;; i++) {
  5095. let vp = i < this.viewports.length ? this.viewports[i] : null;
  5096. if (!vp || n < vp.top)
  5097. return domBase + (n - base) * this.scale;
  5098. if (n <= vp.bottom)
  5099. return vp.domTop + (n - vp.top);
  5100. base = vp.bottom;
  5101. domBase = vp.domBottom;
  5102. }
  5103. }
  5104. fromDOM(n) {
  5105. for (let i = 0, base = 0, domBase = 0;; i++) {
  5106. let vp = i < this.viewports.length ? this.viewports[i] : null;
  5107. if (!vp || n < vp.domTop)
  5108. return base + (n - domBase) / this.scale;
  5109. if (n <= vp.domBottom)
  5110. return vp.top + (n - vp.domTop);
  5111. base = vp.bottom;
  5112. domBase = vp.domBottom;
  5113. }
  5114. }
  5115. }
  5116. function scaleBlock(block, scaler) {
  5117. if (scaler.scale == 1)
  5118. return block;
  5119. let bTop = scaler.toDOM(block.top), bBottom = scaler.toDOM(block.bottom);
  5120. return new BlockInfo(block.from, block.length, bTop, bBottom - bTop, Array.isArray(block.type) ? block.type.map(b => scaleBlock(b, scaler)) : block.type);
  5121. }
  5122. const theme = /*@__PURE__*/Facet.define({ combine: strs => strs.join(" ") });
  5123. const darkTheme = /*@__PURE__*/Facet.define({ combine: values => values.indexOf(true) > -1 });
  5124. const baseThemeID = /*@__PURE__*/StyleModule.newName(), baseLightID = /*@__PURE__*/StyleModule.newName(), baseDarkID = /*@__PURE__*/StyleModule.newName();
  5125. const lightDarkIDs = { "&light": "." + baseLightID, "&dark": "." + baseDarkID };
  5126. function buildTheme(main, spec, scopes) {
  5127. return new StyleModule(spec, {
  5128. finish(sel) {
  5129. return /&/.test(sel) ? sel.replace(/&\w*/, m => {
  5130. if (m == "&")
  5131. return main;
  5132. if (!scopes || !scopes[m])
  5133. throw new RangeError(`Unsupported selector: ${m}`);
  5134. return scopes[m];
  5135. }) : main + " " + sel;
  5136. }
  5137. });
  5138. }
  5139. const baseTheme$1 = /*@__PURE__*/buildTheme("." + baseThemeID, {
  5140. "&.cm-editor": {
  5141. position: "relative !important",
  5142. boxSizing: "border-box",
  5143. "&.cm-focused": {
  5144. // Provide a simple default outline to make sure a focused
  5145. // editor is visually distinct. Can't leave the default behavior
  5146. // because that will apply to the content element, which is
  5147. // inside the scrollable container and doesn't include the
  5148. // gutters. We also can't use an 'auto' outline, since those
  5149. // are, for some reason, drawn behind the element content, which
  5150. // will cause things like the active line background to cover
  5151. // the outline (#297).
  5152. outline: "1px dotted #212121"
  5153. },
  5154. display: "flex !important",
  5155. flexDirection: "column"
  5156. },
  5157. ".cm-scroller": {
  5158. display: "flex !important",
  5159. alignItems: "flex-start !important",
  5160. fontFamily: "monospace",
  5161. lineHeight: 1.4,
  5162. height: "100%",
  5163. overflowX: "auto",
  5164. position: "relative",
  5165. zIndex: 0
  5166. },
  5167. ".cm-content": {
  5168. margin: 0,
  5169. flexGrow: 2,
  5170. flexShrink: 0,
  5171. minHeight: "100%",
  5172. display: "block",
  5173. whiteSpace: "pre",
  5174. wordWrap: "normal",
  5175. boxSizing: "border-box",
  5176. padding: "4px 0",
  5177. outline: "none",
  5178. "&[contenteditable=true]": {
  5179. WebkitUserModify: "read-write-plaintext-only",
  5180. }
  5181. },
  5182. ".cm-lineWrapping": {
  5183. whiteSpace_fallback: "pre-wrap",
  5184. whiteSpace: "break-spaces",
  5185. wordBreak: "break-word",
  5186. overflowWrap: "anywhere",
  5187. flexShrink: 1
  5188. },
  5189. "&light .cm-content": { caretColor: "black" },
  5190. "&dark .cm-content": { caretColor: "white" },
  5191. ".cm-line": {
  5192. display: "block",
  5193. padding: "0 2px 0 4px"
  5194. },
  5195. ".cm-selectionLayer": {
  5196. zIndex: -1,
  5197. contain: "size style"
  5198. },
  5199. ".cm-selectionBackground": {
  5200. position: "absolute",
  5201. },
  5202. "&light .cm-selectionBackground": {
  5203. background: "#d9d9d9"
  5204. },
  5205. "&dark .cm-selectionBackground": {
  5206. background: "#222"
  5207. },
  5208. "&light.cm-focused .cm-selectionBackground": {
  5209. background: "#d7d4f0"
  5210. },
  5211. "&dark.cm-focused .cm-selectionBackground": {
  5212. background: "#233"
  5213. },
  5214. ".cm-cursorLayer": {
  5215. zIndex: 100,
  5216. contain: "size style",
  5217. pointerEvents: "none"
  5218. },
  5219. "&.cm-focused .cm-cursorLayer": {
  5220. animation: "steps(1) cm-blink 1.2s infinite"
  5221. },
  5222. // Two animations defined so that we can switch between them to
  5223. // restart the animation without forcing another style
  5224. // recomputation.
  5225. "@keyframes cm-blink": { "0%": {}, "50%": { opacity: 0 }, "100%": {} },
  5226. "@keyframes cm-blink2": { "0%": {}, "50%": { opacity: 0 }, "100%": {} },
  5227. ".cm-cursor, .cm-dropCursor": {
  5228. position: "absolute",
  5229. borderLeft: "1.2px solid black",
  5230. marginLeft: "-0.6px",
  5231. pointerEvents: "none",
  5232. },
  5233. ".cm-cursor": {
  5234. display: "none"
  5235. },
  5236. "&dark .cm-cursor": {
  5237. borderLeftColor: "#444"
  5238. },
  5239. "&.cm-focused .cm-cursor": {
  5240. display: "block"
  5241. },
  5242. "&light .cm-activeLine": { backgroundColor: "#f3f9ff" },
  5243. "&dark .cm-activeLine": { backgroundColor: "#223039" },
  5244. "&light .cm-specialChar": { color: "red" },
  5245. "&dark .cm-specialChar": { color: "#f78" },
  5246. ".cm-gutters": {
  5247. display: "flex",
  5248. height: "100%",
  5249. boxSizing: "border-box",
  5250. left: 0,
  5251. zIndex: 200
  5252. },
  5253. "&light .cm-gutters": {
  5254. backgroundColor: "#f5f5f5",
  5255. color: "#6c6c6c",
  5256. borderRight: "1px solid #ddd"
  5257. },
  5258. "&dark .cm-gutters": {
  5259. backgroundColor: "#333338",
  5260. color: "#ccc"
  5261. },
  5262. ".cm-gutter": {
  5263. display: "flex !important",
  5264. flexDirection: "column",
  5265. flexShrink: 0,
  5266. boxSizing: "border-box",
  5267. minHeight: "100%",
  5268. overflow: "hidden"
  5269. },
  5270. ".cm-gutterElement": {
  5271. boxSizing: "border-box"
  5272. },
  5273. ".cm-lineNumbers .cm-gutterElement": {
  5274. padding: "0 3px 0 5px",
  5275. minWidth: "20px",
  5276. textAlign: "right",
  5277. whiteSpace: "nowrap"
  5278. },
  5279. "&light .cm-activeLineGutter": {
  5280. backgroundColor: "#e2f2ff"
  5281. },
  5282. "&dark .cm-activeLineGutter": {
  5283. backgroundColor: "#222227"
  5284. },
  5285. ".cm-panels": {
  5286. boxSizing: "border-box",
  5287. position: "sticky",
  5288. left: 0,
  5289. right: 0
  5290. },
  5291. "&light .cm-panels": {
  5292. backgroundColor: "#f5f5f5",
  5293. color: "black"
  5294. },
  5295. "&light .cm-panels-top": {
  5296. borderBottom: "1px solid #ddd"
  5297. },
  5298. "&light .cm-panels-bottom": {
  5299. borderTop: "1px solid #ddd"
  5300. },
  5301. "&dark .cm-panels": {
  5302. backgroundColor: "#333338",
  5303. color: "white"
  5304. },
  5305. ".cm-tab": {
  5306. display: "inline-block",
  5307. overflow: "hidden",
  5308. verticalAlign: "bottom"
  5309. },
  5310. ".cm-widgetBuffer": {
  5311. verticalAlign: "text-top",
  5312. height: "1em",
  5313. display: "inline"
  5314. },
  5315. ".cm-placeholder": {
  5316. color: "#888",
  5317. display: "inline-block",
  5318. verticalAlign: "top",
  5319. },
  5320. ".cm-button": {
  5321. verticalAlign: "middle",
  5322. color: "inherit",
  5323. fontSize: "70%",
  5324. padding: ".2em 1em",
  5325. borderRadius: "1px"
  5326. },
  5327. "&light .cm-button": {
  5328. backgroundImage: "linear-gradient(#eff1f5, #d9d9df)",
  5329. border: "1px solid #888",
  5330. "&:active": {
  5331. backgroundImage: "linear-gradient(#b4b4b4, #d0d3d6)"
  5332. }
  5333. },
  5334. "&dark .cm-button": {
  5335. backgroundImage: "linear-gradient(#393939, #111)",
  5336. border: "1px solid #888",
  5337. "&:active": {
  5338. backgroundImage: "linear-gradient(#111, #333)"
  5339. }
  5340. },
  5341. ".cm-textfield": {
  5342. verticalAlign: "middle",
  5343. color: "inherit",
  5344. fontSize: "70%",
  5345. border: "1px solid silver",
  5346. padding: ".2em .5em"
  5347. },
  5348. "&light .cm-textfield": {
  5349. backgroundColor: "white"
  5350. },
  5351. "&dark .cm-textfield": {
  5352. border: "1px solid #555",
  5353. backgroundColor: "inherit"
  5354. }
  5355. }, lightDarkIDs);
  5356. const observeOptions = {
  5357. childList: true,
  5358. characterData: true,
  5359. subtree: true,
  5360. attributes: true,
  5361. characterDataOldValue: true
  5362. };
  5363. // IE11 has very broken mutation observers, so we also listen to
  5364. // DOMCharacterDataModified there
  5365. const useCharData = browser.ie && browser.ie_version <= 11;
  5366. class DOMObserver {
  5367. constructor(view, onChange, onScrollChanged) {
  5368. this.view = view;
  5369. this.onChange = onChange;
  5370. this.onScrollChanged = onScrollChanged;
  5371. this.active = false;
  5372. // The known selection. Kept in our own object, as opposed to just
  5373. // directly accessing the selection because:
  5374. // - Safari doesn't report the right selection in shadow DOM
  5375. // - Reading from the selection forces a DOM layout
  5376. // - This way, we can ignore selectionchange events if we have
  5377. // already seen the 'new' selection
  5378. this.selectionRange = new DOMSelectionState;
  5379. // Set when a selection change is detected, cleared on flush
  5380. this.selectionChanged = false;
  5381. this.delayedFlush = -1;
  5382. this.resizeTimeout = -1;
  5383. this.queue = [];
  5384. this.delayedAndroidKey = null;
  5385. this.scrollTargets = [];
  5386. this.intersection = null;
  5387. this.resize = null;
  5388. this.intersecting = false;
  5389. this.gapIntersection = null;
  5390. this.gaps = [];
  5391. // Timeout for scheduling check of the parents that need scroll handlers
  5392. this.parentCheck = -1;
  5393. this.dom = view.contentDOM;
  5394. this.observer = new MutationObserver(mutations => {
  5395. for (let mut of mutations)
  5396. this.queue.push(mut);
  5397. // IE11 will sometimes (on typing over a selection or
  5398. // backspacing out a single character text node) call the
  5399. // observer callback before actually updating the DOM.
  5400. //
  5401. // Unrelatedly, iOS Safari will, when ending a composition,
  5402. // sometimes first clear it, deliver the mutations, and then
  5403. // reinsert the finished text. CodeMirror's handling of the
  5404. // deletion will prevent the reinsertion from happening,
  5405. // breaking composition.
  5406. if ((browser.ie && browser.ie_version <= 11 || browser.ios && view.composing) &&
  5407. mutations.some(m => m.type == "childList" && m.removedNodes.length ||
  5408. m.type == "characterData" && m.oldValue.length > m.target.nodeValue.length))
  5409. this.flushSoon();
  5410. else
  5411. this.flush();
  5412. });
  5413. if (useCharData)
  5414. this.onCharData = (event) => {
  5415. this.queue.push({ target: event.target,
  5416. type: "characterData",
  5417. oldValue: event.prevValue });
  5418. this.flushSoon();
  5419. };
  5420. this.onSelectionChange = this.onSelectionChange.bind(this);
  5421. window.addEventListener("resize", this.onResize = this.onResize.bind(this));
  5422. if (typeof ResizeObserver == "function") {
  5423. this.resize = new ResizeObserver(() => {
  5424. if (this.view.docView.lastUpdate < Date.now() - 75)
  5425. this.onResize();
  5426. });
  5427. this.resize.observe(view.scrollDOM);
  5428. }
  5429. window.addEventListener("beforeprint", this.onPrint = this.onPrint.bind(this));
  5430. this.start();
  5431. window.addEventListener("scroll", this.onScroll = this.onScroll.bind(this));
  5432. if (typeof IntersectionObserver == "function") {
  5433. this.intersection = new IntersectionObserver(entries => {
  5434. if (this.parentCheck < 0)
  5435. this.parentCheck = setTimeout(this.listenForScroll.bind(this), 1000);
  5436. if (entries.length > 0 && (entries[entries.length - 1].intersectionRatio > 0) != this.intersecting) {
  5437. this.intersecting = !this.intersecting;
  5438. if (this.intersecting != this.view.inView)
  5439. this.onScrollChanged(document.createEvent("Event"));
  5440. }
  5441. }, {});
  5442. this.intersection.observe(this.dom);
  5443. this.gapIntersection = new IntersectionObserver(entries => {
  5444. if (entries.length > 0 && entries[entries.length - 1].intersectionRatio > 0)
  5445. this.onScrollChanged(document.createEvent("Event"));
  5446. }, {});
  5447. }
  5448. this.listenForScroll();
  5449. this.readSelectionRange();
  5450. this.dom.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
  5451. }
  5452. onScroll(e) {
  5453. if (this.intersecting)
  5454. this.flush(false);
  5455. this.onScrollChanged(e);
  5456. }
  5457. onResize() {
  5458. if (this.resizeTimeout < 0)
  5459. this.resizeTimeout = setTimeout(() => {
  5460. this.resizeTimeout = -1;
  5461. this.view.requestMeasure();
  5462. }, 50);
  5463. }
  5464. onPrint() {
  5465. this.view.viewState.printing = true;
  5466. this.view.measure();
  5467. setTimeout(() => {
  5468. this.view.viewState.printing = false;
  5469. this.view.requestMeasure();
  5470. }, 500);
  5471. }
  5472. updateGaps(gaps) {
  5473. if (this.gapIntersection && (gaps.length != this.gaps.length || this.gaps.some((g, i) => g != gaps[i]))) {
  5474. this.gapIntersection.disconnect();
  5475. for (let gap of gaps)
  5476. this.gapIntersection.observe(gap);
  5477. this.gaps = gaps;
  5478. }
  5479. }
  5480. onSelectionChange(event) {
  5481. if (!this.readSelectionRange() || this.delayedAndroidKey)
  5482. return;
  5483. let { view } = this, sel = this.selectionRange;
  5484. if (view.state.facet(editable) ? view.root.activeElement != this.dom : !hasSelection(view.dom, sel))
  5485. return;
  5486. let context = sel.anchorNode && view.docView.nearest(sel.anchorNode);
  5487. if (context && context.ignoreEvent(event))
  5488. return;
  5489. // Deletions on IE11 fire their events in the wrong order, giving
  5490. // us a selection change event before the DOM changes are
  5491. // reported.
  5492. // Chrome Android has a similar issue when backspacing out a
  5493. // selection (#645).
  5494. if ((browser.ie && browser.ie_version <= 11 || browser.android && browser.chrome) && !view.state.selection.main.empty &&
  5495. // (Selection.isCollapsed isn't reliable on IE)
  5496. sel.focusNode && isEquivalentPosition(sel.focusNode, sel.focusOffset, sel.anchorNode, sel.anchorOffset))
  5497. this.flushSoon();
  5498. else
  5499. this.flush(false);
  5500. }
  5501. readSelectionRange() {
  5502. let { root } = this.view;
  5503. // The Selection object is broken in shadow roots in Safari. See
  5504. // https://github.com/codemirror/dev/issues/414
  5505. let range = browser.safari && root.nodeType == 11 && deepActiveElement() == this.view.contentDOM &&
  5506. safariSelectionRangeHack(this.view) || getSelection(root);
  5507. if (!range || this.selectionRange.eq(range))
  5508. return false;
  5509. this.selectionRange.setRange(range);
  5510. return this.selectionChanged = true;
  5511. }
  5512. setSelectionRange(anchor, head) {
  5513. this.selectionRange.set(anchor.node, anchor.offset, head.node, head.offset);
  5514. this.selectionChanged = false;
  5515. }
  5516. clearSelectionRange() {
  5517. this.selectionRange.set(null, 0, null, 0);
  5518. }
  5519. listenForScroll() {
  5520. this.parentCheck = -1;
  5521. let i = 0, changed = null;
  5522. for (let dom = this.dom; dom;) {
  5523. if (dom.nodeType == 1) {
  5524. if (!changed && i < this.scrollTargets.length && this.scrollTargets[i] == dom)
  5525. i++;
  5526. else if (!changed)
  5527. changed = this.scrollTargets.slice(0, i);
  5528. if (changed)
  5529. changed.push(dom);
  5530. dom = dom.assignedSlot || dom.parentNode;
  5531. }
  5532. else if (dom.nodeType == 11) { // Shadow root
  5533. dom = dom.host;
  5534. }
  5535. else {
  5536. break;
  5537. }
  5538. }
  5539. if (i < this.scrollTargets.length && !changed)
  5540. changed = this.scrollTargets.slice(0, i);
  5541. if (changed) {
  5542. for (let dom of this.scrollTargets)
  5543. dom.removeEventListener("scroll", this.onScroll);
  5544. for (let dom of this.scrollTargets = changed)
  5545. dom.addEventListener("scroll", this.onScroll);
  5546. }
  5547. }
  5548. ignore(f) {
  5549. if (!this.active)
  5550. return f();
  5551. try {
  5552. this.stop();
  5553. return f();
  5554. }
  5555. finally {
  5556. this.start();
  5557. this.clear();
  5558. }
  5559. }
  5560. start() {
  5561. if (this.active)
  5562. return;
  5563. this.observer.observe(this.dom, observeOptions);
  5564. if (useCharData)
  5565. this.dom.addEventListener("DOMCharacterDataModified", this.onCharData);
  5566. this.active = true;
  5567. }
  5568. stop() {
  5569. if (!this.active)
  5570. return;
  5571. this.active = false;
  5572. this.observer.disconnect();
  5573. if (useCharData)
  5574. this.dom.removeEventListener("DOMCharacterDataModified", this.onCharData);
  5575. }
  5576. // Throw away any pending changes
  5577. clear() {
  5578. this.processRecords();
  5579. this.queue.length = 0;
  5580. this.selectionChanged = false;
  5581. }
  5582. // Chrome Android, especially in combination with GBoard, not only
  5583. // doesn't reliably fire regular key events, but also often
  5584. // surrounds the effect of enter or backspace with a bunch of
  5585. // composition events that, when interrupted, cause text duplication
  5586. // or other kinds of corruption. This hack makes the editor back off
  5587. // from handling DOM changes for a moment when such a key is
  5588. // detected (via beforeinput or keydown), and then tries to flush
  5589. // them or, if that has no effect, dispatches the given key.
  5590. delayAndroidKey(key, keyCode) {
  5591. if (!this.delayedAndroidKey)
  5592. requestAnimationFrame(() => {
  5593. let key = this.delayedAndroidKey;
  5594. this.delayedAndroidKey = null;
  5595. this.delayedFlush = -1;
  5596. if (!this.flush())
  5597. dispatchKey(this.view.contentDOM, key.key, key.keyCode);
  5598. });
  5599. // Since backspace beforeinput is sometimes signalled spuriously,
  5600. // Enter always takes precedence.
  5601. if (!this.delayedAndroidKey || key == "Enter")
  5602. this.delayedAndroidKey = { key, keyCode };
  5603. }
  5604. flushSoon() {
  5605. if (this.delayedFlush < 0)
  5606. this.delayedFlush = window.setTimeout(() => { this.delayedFlush = -1; this.flush(); }, 20);
  5607. }
  5608. forceFlush() {
  5609. if (this.delayedFlush >= 0) {
  5610. window.clearTimeout(this.delayedFlush);
  5611. this.delayedFlush = -1;
  5612. this.flush();
  5613. }
  5614. }
  5615. processRecords() {
  5616. let records = this.queue;
  5617. for (let mut of this.observer.takeRecords())
  5618. records.push(mut);
  5619. if (records.length)
  5620. this.queue = [];
  5621. let from = -1, to = -1, typeOver = false;
  5622. for (let record of records) {
  5623. let range = this.readMutation(record);
  5624. if (!range)
  5625. continue;
  5626. if (range.typeOver)
  5627. typeOver = true;
  5628. if (from == -1) {
  5629. ({ from, to } = range);
  5630. }
  5631. else {
  5632. from = Math.min(range.from, from);
  5633. to = Math.max(range.to, to);
  5634. }
  5635. }
  5636. return { from, to, typeOver };
  5637. }
  5638. // Apply pending changes, if any
  5639. flush(readSelection = true) {
  5640. // Completely hold off flushing when pending keys are set—the code
  5641. // managing those will make sure processRecords is called and the
  5642. // view is resynchronized after
  5643. if (this.delayedFlush >= 0 || this.delayedAndroidKey)
  5644. return;
  5645. if (readSelection)
  5646. this.readSelectionRange();
  5647. let { from, to, typeOver } = this.processRecords();
  5648. let newSel = this.selectionChanged && hasSelection(this.dom, this.selectionRange);
  5649. if (from < 0 && !newSel)
  5650. return;
  5651. this.selectionChanged = false;
  5652. let startState = this.view.state;
  5653. let handled = this.onChange(from, to, typeOver);
  5654. // The view wasn't updated
  5655. if (this.view.state == startState)
  5656. this.view.update([]);
  5657. return handled;
  5658. }
  5659. readMutation(rec) {
  5660. let cView = this.view.docView.nearest(rec.target);
  5661. if (!cView || cView.ignoreMutation(rec))
  5662. return null;
  5663. cView.markDirty(rec.type == "attributes");
  5664. if (rec.type == "attributes")
  5665. cView.dirty |= 4 /* Attrs */;
  5666. if (rec.type == "childList") {
  5667. let childBefore = findChild(cView, rec.previousSibling || rec.target.previousSibling, -1);
  5668. let childAfter = findChild(cView, rec.nextSibling || rec.target.nextSibling, 1);
  5669. return { from: childBefore ? cView.posAfter(childBefore) : cView.posAtStart,
  5670. to: childAfter ? cView.posBefore(childAfter) : cView.posAtEnd, typeOver: false };
  5671. }
  5672. else if (rec.type == "characterData") {
  5673. return { from: cView.posAtStart, to: cView.posAtEnd, typeOver: rec.target.nodeValue == rec.oldValue };
  5674. }
  5675. else {
  5676. return null;
  5677. }
  5678. }
  5679. destroy() {
  5680. var _a, _b, _c;
  5681. this.stop();
  5682. (_a = this.intersection) === null || _a === void 0 ? void 0 : _a.disconnect();
  5683. (_b = this.gapIntersection) === null || _b === void 0 ? void 0 : _b.disconnect();
  5684. (_c = this.resize) === null || _c === void 0 ? void 0 : _c.disconnect();
  5685. for (let dom of this.scrollTargets)
  5686. dom.removeEventListener("scroll", this.onScroll);
  5687. window.removeEventListener("scroll", this.onScroll);
  5688. window.removeEventListener("resize", this.onResize);
  5689. window.removeEventListener("beforeprint", this.onPrint);
  5690. this.dom.ownerDocument.removeEventListener("selectionchange", this.onSelectionChange);
  5691. clearTimeout(this.parentCheck);
  5692. clearTimeout(this.resizeTimeout);
  5693. }
  5694. }
  5695. function findChild(cView, dom, dir) {
  5696. while (dom) {
  5697. let curView = ContentView.get(dom);
  5698. if (curView && curView.parent == cView)
  5699. return curView;
  5700. let parent = dom.parentNode;
  5701. dom = parent != cView.dom ? parent : dir > 0 ? dom.nextSibling : dom.previousSibling;
  5702. }
  5703. return null;
  5704. }
  5705. // Used to work around a Safari Selection/shadow DOM bug (#414)
  5706. function safariSelectionRangeHack(view) {
  5707. let found = null;
  5708. // Because Safari (at least in 2018-2021) doesn't provide regular
  5709. // access to the selection inside a shadowroot, we have to perform a
  5710. // ridiculous hack to get at it—using `execCommand` to trigger a
  5711. // `beforeInput` event so that we can read the target range from the
  5712. // event.
  5713. function read(event) {
  5714. event.preventDefault();
  5715. event.stopImmediatePropagation();
  5716. found = event.getTargetRanges()[0];
  5717. }
  5718. view.contentDOM.addEventListener("beforeinput", read, true);
  5719. document.execCommand("indent");
  5720. view.contentDOM.removeEventListener("beforeinput", read, true);
  5721. if (!found)
  5722. return null;
  5723. let anchorNode = found.startContainer, anchorOffset = found.startOffset;
  5724. let focusNode = found.endContainer, focusOffset = found.endOffset;
  5725. let curAnchor = view.docView.domAtPos(view.state.selection.main.anchor);
  5726. // Since such a range doesn't distinguish between anchor and head,
  5727. // use a heuristic that flips it around if its end matches the
  5728. // current anchor.
  5729. if (isEquivalentPosition(curAnchor.node, curAnchor.offset, focusNode, focusOffset))
  5730. [anchorNode, anchorOffset, focusNode, focusOffset] = [focusNode, focusOffset, anchorNode, anchorOffset];
  5731. return { anchorNode, anchorOffset, focusNode, focusOffset };
  5732. }
  5733. function applyDOMChange(view, start, end, typeOver) {
  5734. let change, newSel;
  5735. let sel = view.state.selection.main;
  5736. if (start > -1) {
  5737. let bounds = view.docView.domBoundsAround(start, end, 0);
  5738. if (!bounds || view.state.readOnly)
  5739. return false;
  5740. let { from, to } = bounds;
  5741. let selPoints = view.docView.impreciseHead || view.docView.impreciseAnchor ? [] : selectionPoints(view);
  5742. let reader = new DOMReader(selPoints, view.state);
  5743. reader.readRange(bounds.startDOM, bounds.endDOM);
  5744. let preferredPos = sel.from, preferredSide = null;
  5745. // Prefer anchoring to end when Backspace is pressed (or, on
  5746. // Android, when something was deleted)
  5747. if (view.inputState.lastKeyCode === 8 && view.inputState.lastKeyTime > Date.now() - 100 ||
  5748. browser.android && reader.text.length < to - from) {
  5749. preferredPos = sel.to;
  5750. preferredSide = "end";
  5751. }
  5752. let diff = findDiff(view.state.doc.sliceString(from, to, LineBreakPlaceholder), reader.text, preferredPos - from, preferredSide);
  5753. if (diff) {
  5754. // Chrome inserts two newlines when pressing shift-enter at the
  5755. // end of a line. This drops one of those.
  5756. if (browser.chrome && view.inputState.lastKeyCode == 13 &&
  5757. diff.toB == diff.from + 2 && reader.text.slice(diff.from, diff.toB) == LineBreakPlaceholder + LineBreakPlaceholder)
  5758. diff.toB--;
  5759. change = { from: from + diff.from, to: from + diff.toA,
  5760. insert: Text.of(reader.text.slice(diff.from, diff.toB).split(LineBreakPlaceholder)) };
  5761. }
  5762. newSel = selectionFromPoints(selPoints, from);
  5763. }
  5764. else if (view.hasFocus || !view.state.facet(editable)) {
  5765. let domSel = view.observer.selectionRange;
  5766. let { impreciseHead: iHead, impreciseAnchor: iAnchor } = view.docView;
  5767. let head = iHead && iHead.node == domSel.focusNode && iHead.offset == domSel.focusOffset ||
  5768. !contains(view.contentDOM, domSel.focusNode)
  5769. ? view.state.selection.main.head
  5770. : view.docView.posFromDOM(domSel.focusNode, domSel.focusOffset);
  5771. let anchor = iAnchor && iAnchor.node == domSel.anchorNode && iAnchor.offset == domSel.anchorOffset ||
  5772. !contains(view.contentDOM, domSel.anchorNode)
  5773. ? view.state.selection.main.anchor
  5774. : view.docView.posFromDOM(domSel.anchorNode, domSel.anchorOffset);
  5775. if (head != sel.head || anchor != sel.anchor)
  5776. newSel = EditorSelection.single(anchor, head);
  5777. }
  5778. if (!change && !newSel)
  5779. return false;
  5780. // Heuristic to notice typing over a selected character
  5781. if (!change && typeOver && !sel.empty && newSel && newSel.main.empty)
  5782. change = { from: sel.from, to: sel.to, insert: view.state.doc.slice(sel.from, sel.to) };
  5783. // If the change is inside the selection and covers most of it,
  5784. // assume it is a selection replace (with identical characters at
  5785. // the start/end not included in the diff)
  5786. else if (change && change.from >= sel.from && change.to <= sel.to &&
  5787. (change.from != sel.from || change.to != sel.to) &&
  5788. (sel.to - sel.from) - (change.to - change.from) <= 4)
  5789. change = {
  5790. from: sel.from, to: sel.to,
  5791. insert: view.state.doc.slice(sel.from, change.from).append(change.insert).append(view.state.doc.slice(change.to, sel.to))
  5792. };
  5793. // Detect insert-period-on-double-space Mac behavior, and transform
  5794. // it into a regular space insert.
  5795. else if ((browser.mac || browser.android) && change && change.from == change.to && change.from == sel.head - 1 &&
  5796. change.insert.toString() == ".")
  5797. change = { from: sel.from, to: sel.to, insert: Text.of([" "]) };
  5798. if (change) {
  5799. let startState = view.state;
  5800. if (browser.ios && view.inputState.flushIOSKey(view))
  5801. return true;
  5802. // Android browsers don't fire reasonable key events for enter,
  5803. // backspace, or delete. So this detects changes that look like
  5804. // they're caused by those keys, and reinterprets them as key
  5805. // events. (Some of these keys are also handled by beforeinput
  5806. // events and the pendingAndroidKey mechanism, but that's not
  5807. // reliable in all situations.)
  5808. if (browser.android &&
  5809. ((change.from == sel.from && change.to == sel.to &&
  5810. change.insert.length == 1 && change.insert.lines == 2 &&
  5811. dispatchKey(view.contentDOM, "Enter", 13)) ||
  5812. (change.from == sel.from - 1 && change.to == sel.to && change.insert.length == 0 &&
  5813. dispatchKey(view.contentDOM, "Backspace", 8)) ||
  5814. (change.from == sel.from && change.to == sel.to + 1 && change.insert.length == 0 &&
  5815. dispatchKey(view.contentDOM, "Delete", 46))))
  5816. return true;
  5817. let text = change.insert.toString();
  5818. if (view.state.facet(inputHandler).some(h => h(view, change.from, change.to, text)))
  5819. return true;
  5820. if (view.inputState.composing >= 0)
  5821. view.inputState.composing++;
  5822. let tr;
  5823. if (change.from >= sel.from && change.to <= sel.to && change.to - change.from >= (sel.to - sel.from) / 3 &&
  5824. (!newSel || newSel.main.empty && newSel.main.from == change.from + change.insert.length) &&
  5825. view.inputState.composing < 0) {
  5826. let before = sel.from < change.from ? startState.sliceDoc(sel.from, change.from) : "";
  5827. let after = sel.to > change.to ? startState.sliceDoc(change.to, sel.to) : "";
  5828. tr = startState.replaceSelection(view.state.toText(before + change.insert.sliceString(0, undefined, view.state.lineBreak) + after));
  5829. }
  5830. else {
  5831. let changes = startState.changes(change);
  5832. let mainSel = newSel && !startState.selection.main.eq(newSel.main) && newSel.main.to <= changes.newLength
  5833. ? newSel.main : undefined;
  5834. // Try to apply a composition change to all cursors
  5835. if (startState.selection.ranges.length > 1 && view.inputState.composing >= 0 &&
  5836. change.to <= sel.to && change.to >= sel.to - 10) {
  5837. let replaced = view.state.sliceDoc(change.from, change.to);
  5838. let compositionRange = compositionSurroundingNode(view) || view.state.doc.lineAt(sel.head);
  5839. let offset = sel.to - change.to, size = sel.to - sel.from;
  5840. tr = startState.changeByRange(range => {
  5841. if (range.from == sel.from && range.to == sel.to)
  5842. return { changes, range: mainSel || range.map(changes) };
  5843. let to = range.to - offset, from = to - replaced.length;
  5844. if (range.to - range.from != size || view.state.sliceDoc(from, to) != replaced ||
  5845. // Unfortunately, there's no way to make multiple
  5846. // changes in the same node work without aborting
  5847. // composition, so cursors in the composition range are
  5848. // ignored.
  5849. compositionRange && range.to >= compositionRange.from && range.from <= compositionRange.to)
  5850. return { range };
  5851. let rangeChanges = startState.changes({ from, to, insert: change.insert }), selOff = range.to - sel.to;
  5852. return {
  5853. changes: rangeChanges,
  5854. range: !mainSel ? range.map(rangeChanges) :
  5855. EditorSelection.range(Math.max(0, mainSel.anchor + selOff), Math.max(0, mainSel.head + selOff))
  5856. };
  5857. });
  5858. }
  5859. else {
  5860. tr = {
  5861. changes,
  5862. selection: mainSel && startState.selection.replaceRange(mainSel)
  5863. };
  5864. }
  5865. }
  5866. let userEvent = "input.type";
  5867. if (view.composing) {
  5868. userEvent += ".compose";
  5869. if (view.inputState.compositionFirstChange) {
  5870. userEvent += ".start";
  5871. view.inputState.compositionFirstChange = false;
  5872. }
  5873. }
  5874. view.dispatch(tr, { scrollIntoView: true, userEvent });
  5875. return true;
  5876. }
  5877. else if (newSel && !newSel.main.eq(sel)) {
  5878. let scrollIntoView = false, userEvent = "select";
  5879. if (view.inputState.lastSelectionTime > Date.now() - 50) {
  5880. if (view.inputState.lastSelectionOrigin == "select")
  5881. scrollIntoView = true;
  5882. userEvent = view.inputState.lastSelectionOrigin;
  5883. }
  5884. view.dispatch({ selection: newSel, scrollIntoView, userEvent });
  5885. return true;
  5886. }
  5887. else {
  5888. return false;
  5889. }
  5890. }
  5891. function findDiff(a, b, preferredPos, preferredSide) {
  5892. let minLen = Math.min(a.length, b.length);
  5893. let from = 0;
  5894. while (from < minLen && a.charCodeAt(from) == b.charCodeAt(from))
  5895. from++;
  5896. if (from == minLen && a.length == b.length)
  5897. return null;
  5898. let toA = a.length, toB = b.length;
  5899. while (toA > 0 && toB > 0 && a.charCodeAt(toA - 1) == b.charCodeAt(toB - 1)) {
  5900. toA--;
  5901. toB--;
  5902. }
  5903. if (preferredSide == "end") {
  5904. let adjust = Math.max(0, from - Math.min(toA, toB));
  5905. preferredPos -= toA + adjust - from;
  5906. }
  5907. if (toA < from && a.length < b.length) {
  5908. let move = preferredPos <= from && preferredPos >= toA ? from - preferredPos : 0;
  5909. from -= move;
  5910. toB = from + (toB - toA);
  5911. toA = from;
  5912. }
  5913. else if (toB < from) {
  5914. let move = preferredPos <= from && preferredPos >= toB ? from - preferredPos : 0;
  5915. from -= move;
  5916. toA = from + (toA - toB);
  5917. toB = from;
  5918. }
  5919. return { from, toA, toB };
  5920. }
  5921. function selectionPoints(view) {
  5922. let result = [];
  5923. if (view.root.activeElement != view.contentDOM)
  5924. return result;
  5925. let { anchorNode, anchorOffset, focusNode, focusOffset } = view.observer.selectionRange;
  5926. if (anchorNode) {
  5927. result.push(new DOMPoint(anchorNode, anchorOffset));
  5928. if (focusNode != anchorNode || focusOffset != anchorOffset)
  5929. result.push(new DOMPoint(focusNode, focusOffset));
  5930. }
  5931. return result;
  5932. }
  5933. function selectionFromPoints(points, base) {
  5934. if (points.length == 0)
  5935. return null;
  5936. let anchor = points[0].pos, head = points.length == 2 ? points[1].pos : anchor;
  5937. return anchor > -1 && head > -1 ? EditorSelection.single(anchor + base, head + base) : null;
  5938. }
  5939. // The editor's update state machine looks something like this:
  5940. //
  5941. // Idle → Updating ⇆ Idle (unchecked) → Measuring → Idle
  5942. // ↑ ↓
  5943. // Updating (measure)
  5944. //
  5945. // The difference between 'Idle' and 'Idle (unchecked)' lies in
  5946. // whether a layout check has been scheduled. A regular update through
  5947. // the `update` method updates the DOM in a write-only fashion, and
  5948. // relies on a check (scheduled with `requestAnimationFrame`) to make
  5949. // sure everything is where it should be and the viewport covers the
  5950. // visible code. That check continues to measure and then optionally
  5951. // update until it reaches a coherent state.
  5952. /**
  5953. An editor view represents the editor's user interface. It holds
  5954. the editable DOM surface, and possibly other elements such as the
  5955. line number gutter. It handles events and dispatches state
  5956. transactions for editing actions.
  5957. */
  5958. class EditorView {
  5959. /**
  5960. Construct a new view. You'll want to either provide a `parent`
  5961. option, or put `view.dom` into your document after creating a
  5962. view, so that the user can see the editor.
  5963. */
  5964. constructor(config = {}) {
  5965. this.plugins = [];
  5966. this.pluginMap = new Map;
  5967. this.editorAttrs = {};
  5968. this.contentAttrs = {};
  5969. this.bidiCache = [];
  5970. this.destroyed = false;
  5971. /**
  5972. @internal
  5973. */
  5974. this.updateState = 2 /* Updating */;
  5975. /**
  5976. @internal
  5977. */
  5978. this.measureScheduled = -1;
  5979. /**
  5980. @internal
  5981. */
  5982. this.measureRequests = [];
  5983. this.contentDOM = document.createElement("div");
  5984. this.scrollDOM = document.createElement("div");
  5985. this.scrollDOM.tabIndex = -1;
  5986. this.scrollDOM.className = "cm-scroller";
  5987. this.scrollDOM.appendChild(this.contentDOM);
  5988. this.announceDOM = document.createElement("div");
  5989. this.announceDOM.style.cssText = "position: absolute; top: -10000px";
  5990. this.announceDOM.setAttribute("aria-live", "polite");
  5991. this.dom = document.createElement("div");
  5992. this.dom.appendChild(this.announceDOM);
  5993. this.dom.appendChild(this.scrollDOM);
  5994. this._dispatch = config.dispatch || ((tr) => this.update([tr]));
  5995. this.dispatch = this.dispatch.bind(this);
  5996. this.root = (config.root || getRoot(config.parent) || document);
  5997. this.viewState = new ViewState(config.state || EditorState.create(config));
  5998. this.plugins = this.state.facet(viewPlugin).map(spec => new PluginInstance(spec));
  5999. for (let plugin of this.plugins)
  6000. plugin.update(this);
  6001. this.observer = new DOMObserver(this, (from, to, typeOver) => {
  6002. return applyDOMChange(this, from, to, typeOver);
  6003. }, event => {
  6004. this.inputState.runScrollHandlers(this, event);
  6005. if (this.observer.intersecting)
  6006. this.measure();
  6007. });
  6008. this.inputState = new InputState(this);
  6009. this.inputState.ensureHandlers(this, this.plugins);
  6010. this.docView = new DocView(this);
  6011. this.mountStyles();
  6012. this.updateAttrs();
  6013. this.updateState = 0 /* Idle */;
  6014. this.requestMeasure();
  6015. if (config.parent)
  6016. config.parent.appendChild(this.dom);
  6017. }
  6018. /**
  6019. The current editor state.
  6020. */
  6021. get state() { return this.viewState.state; }
  6022. /**
  6023. To be able to display large documents without consuming too much
  6024. memory or overloading the browser, CodeMirror only draws the
  6025. code that is visible (plus a margin around it) to the DOM. This
  6026. property tells you the extent of the current drawn viewport, in
  6027. document positions.
  6028. */
  6029. get viewport() { return this.viewState.viewport; }
  6030. /**
  6031. When there are, for example, large collapsed ranges in the
  6032. viewport, its size can be a lot bigger than the actual visible
  6033. content. Thus, if you are doing something like styling the
  6034. content in the viewport, it is preferable to only do so for
  6035. these ranges, which are the subset of the viewport that is
  6036. actually drawn.
  6037. */
  6038. get visibleRanges() { return this.viewState.visibleRanges; }
  6039. /**
  6040. Returns false when the editor is entirely scrolled out of view
  6041. or otherwise hidden.
  6042. */
  6043. get inView() { return this.viewState.inView; }
  6044. /**
  6045. Indicates whether the user is currently composing text via
  6046. [IME](https://en.wikipedia.org/wiki/Input_method), and at least
  6047. one change has been made in the current composition.
  6048. */
  6049. get composing() { return this.inputState.composing > 0; }
  6050. /**
  6051. Indicates whether the user is currently in composing state. Note
  6052. that on some platforms, like Android, this will be the case a
  6053. lot, since just putting the cursor on a word starts a
  6054. composition there.
  6055. */
  6056. get compositionStarted() { return this.inputState.composing >= 0; }
  6057. dispatch(...input) {
  6058. this._dispatch(input.length == 1 && input[0] instanceof Transaction ? input[0]
  6059. : this.state.update(...input));
  6060. }
  6061. /**
  6062. Update the view for the given array of transactions. This will
  6063. update the visible document and selection to match the state
  6064. produced by the transactions, and notify view plugins of the
  6065. change. You should usually call
  6066. [`dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch) instead, which uses this
  6067. as a primitive.
  6068. */
  6069. update(transactions) {
  6070. if (this.updateState != 0 /* Idle */)
  6071. throw new Error("Calls to EditorView.update are not allowed while an update is in progress");
  6072. let redrawn = false, attrsChanged = false, update;
  6073. let state = this.state;
  6074. for (let tr of transactions) {
  6075. if (tr.startState != state)
  6076. throw new RangeError("Trying to update state with a transaction that doesn't start from the previous state.");
  6077. state = tr.state;
  6078. }
  6079. if (this.destroyed) {
  6080. this.viewState.state = state;
  6081. return;
  6082. }
  6083. this.observer.clear();
  6084. // When the phrases change, redraw the editor
  6085. if (state.facet(EditorState.phrases) != this.state.facet(EditorState.phrases))
  6086. return this.setState(state);
  6087. update = ViewUpdate.create(this, state, transactions);
  6088. let scrollTarget = this.viewState.scrollTarget;
  6089. try {
  6090. this.updateState = 2 /* Updating */;
  6091. for (let tr of transactions) {
  6092. if (scrollTarget)
  6093. scrollTarget = scrollTarget.map(tr.changes);
  6094. if (tr.scrollIntoView) {
  6095. let { main } = tr.state.selection;
  6096. scrollTarget = new ScrollTarget(main.empty ? main : EditorSelection.cursor(main.head, main.head > main.anchor ? -1 : 1));
  6097. }
  6098. for (let e of tr.effects)
  6099. if (e.is(scrollIntoView))
  6100. scrollTarget = e.value;
  6101. }
  6102. this.viewState.update(update, scrollTarget);
  6103. this.bidiCache = CachedOrder.update(this.bidiCache, update.changes);
  6104. if (!update.empty) {
  6105. this.updatePlugins(update);
  6106. this.inputState.update(update);
  6107. }
  6108. redrawn = this.docView.update(update);
  6109. if (this.state.facet(styleModule) != this.styleModules)
  6110. this.mountStyles();
  6111. attrsChanged = this.updateAttrs();
  6112. this.showAnnouncements(transactions);
  6113. this.docView.updateSelection(redrawn, transactions.some(tr => tr.isUserEvent("select.pointer")));
  6114. }
  6115. finally {
  6116. this.updateState = 0 /* Idle */;
  6117. }
  6118. if (update.startState.facet(theme) != update.state.facet(theme))
  6119. this.viewState.mustMeasureContent = true;
  6120. if (redrawn || attrsChanged || scrollTarget || this.viewState.mustEnforceCursorAssoc || this.viewState.mustMeasureContent)
  6121. this.requestMeasure();
  6122. if (!update.empty)
  6123. for (let listener of this.state.facet(updateListener))
  6124. listener(update);
  6125. }
  6126. /**
  6127. Reset the view to the given state. (This will cause the entire
  6128. document to be redrawn and all view plugins to be reinitialized,
  6129. so you should probably only use it when the new state isn't
  6130. derived from the old state. Otherwise, use
  6131. [`dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch) instead.)
  6132. */
  6133. setState(newState) {
  6134. if (this.updateState != 0 /* Idle */)
  6135. throw new Error("Calls to EditorView.setState are not allowed while an update is in progress");
  6136. if (this.destroyed) {
  6137. this.viewState.state = newState;
  6138. return;
  6139. }
  6140. this.updateState = 2 /* Updating */;
  6141. let hadFocus = this.hasFocus;
  6142. try {
  6143. for (let plugin of this.plugins)
  6144. plugin.destroy(this);
  6145. this.viewState = new ViewState(newState);
  6146. this.plugins = newState.facet(viewPlugin).map(spec => new PluginInstance(spec));
  6147. this.pluginMap.clear();
  6148. for (let plugin of this.plugins)
  6149. plugin.update(this);
  6150. this.docView = new DocView(this);
  6151. this.inputState.ensureHandlers(this, this.plugins);
  6152. this.mountStyles();
  6153. this.updateAttrs();
  6154. this.bidiCache = [];
  6155. }
  6156. finally {
  6157. this.updateState = 0 /* Idle */;
  6158. }
  6159. if (hadFocus)
  6160. this.focus();
  6161. this.requestMeasure();
  6162. }
  6163. updatePlugins(update) {
  6164. let prevSpecs = update.startState.facet(viewPlugin), specs = update.state.facet(viewPlugin);
  6165. if (prevSpecs != specs) {
  6166. let newPlugins = [];
  6167. for (let spec of specs) {
  6168. let found = prevSpecs.indexOf(spec);
  6169. if (found < 0) {
  6170. newPlugins.push(new PluginInstance(spec));
  6171. }
  6172. else {
  6173. let plugin = this.plugins[found];
  6174. plugin.mustUpdate = update;
  6175. newPlugins.push(plugin);
  6176. }
  6177. }
  6178. for (let plugin of this.plugins)
  6179. if (plugin.mustUpdate != update)
  6180. plugin.destroy(this);
  6181. this.plugins = newPlugins;
  6182. this.pluginMap.clear();
  6183. this.inputState.ensureHandlers(this, this.plugins);
  6184. }
  6185. else {
  6186. for (let p of this.plugins)
  6187. p.mustUpdate = update;
  6188. }
  6189. for (let i = 0; i < this.plugins.length; i++)
  6190. this.plugins[i].update(this);
  6191. }
  6192. /**
  6193. @internal
  6194. */
  6195. measure(flush = true) {
  6196. if (this.destroyed)
  6197. return;
  6198. if (this.measureScheduled > -1)
  6199. cancelAnimationFrame(this.measureScheduled);
  6200. this.measureScheduled = 0; // Prevent requestMeasure calls from scheduling another animation frame
  6201. if (flush)
  6202. this.observer.flush();
  6203. let updated = null;
  6204. try {
  6205. for (let i = 0;; i++) {
  6206. this.updateState = 1 /* Measuring */;
  6207. let oldViewport = this.viewport;
  6208. let changed = this.viewState.measure(this);
  6209. if (!changed && !this.measureRequests.length && this.viewState.scrollTarget == null)
  6210. break;
  6211. if (i > 5) {
  6212. console.warn(this.measureRequests.length
  6213. ? "Measure loop restarted more than 5 times"
  6214. : "Viewport failed to stabilize");
  6215. break;
  6216. }
  6217. let measuring = [];
  6218. // Only run measure requests in this cycle when the viewport didn't change
  6219. if (!(changed & 4 /* Viewport */))
  6220. [this.measureRequests, measuring] = [measuring, this.measureRequests];
  6221. let measured = measuring.map(m => {
  6222. try {
  6223. return m.read(this);
  6224. }
  6225. catch (e) {
  6226. logException(this.state, e);
  6227. return BadMeasure;
  6228. }
  6229. });
  6230. let update = ViewUpdate.create(this, this.state, []), redrawn = false, scrolled = false;
  6231. update.flags |= changed;
  6232. if (!updated)
  6233. updated = update;
  6234. else
  6235. updated.flags |= changed;
  6236. this.updateState = 2 /* Updating */;
  6237. if (!update.empty) {
  6238. this.updatePlugins(update);
  6239. this.inputState.update(update);
  6240. this.updateAttrs();
  6241. redrawn = this.docView.update(update);
  6242. }
  6243. for (let i = 0; i < measuring.length; i++)
  6244. if (measured[i] != BadMeasure) {
  6245. try {
  6246. let m = measuring[i];
  6247. if (m.write)
  6248. m.write(measured[i], this);
  6249. }
  6250. catch (e) {
  6251. logException(this.state, e);
  6252. }
  6253. }
  6254. if (this.viewState.scrollTarget) {
  6255. this.docView.scrollIntoView(this.viewState.scrollTarget);
  6256. this.viewState.scrollTarget = null;
  6257. scrolled = true;
  6258. }
  6259. if (redrawn)
  6260. this.docView.updateSelection(true);
  6261. if (this.viewport.from == oldViewport.from && this.viewport.to == oldViewport.to &&
  6262. !scrolled && this.measureRequests.length == 0)
  6263. break;
  6264. }
  6265. }
  6266. finally {
  6267. this.updateState = 0 /* Idle */;
  6268. this.measureScheduled = -1;
  6269. }
  6270. if (updated && !updated.empty)
  6271. for (let listener of this.state.facet(updateListener))
  6272. listener(updated);
  6273. }
  6274. /**
  6275. Get the CSS classes for the currently active editor themes.
  6276. */
  6277. get themeClasses() {
  6278. return baseThemeID + " " +
  6279. (this.state.facet(darkTheme) ? baseDarkID : baseLightID) + " " +
  6280. this.state.facet(theme);
  6281. }
  6282. updateAttrs() {
  6283. let editorAttrs = attrsFromFacet(this, editorAttributes, {
  6284. class: "cm-editor" + (this.hasFocus ? " cm-focused " : " ") + this.themeClasses
  6285. });
  6286. let contentAttrs = {
  6287. spellcheck: "false",
  6288. autocorrect: "off",
  6289. autocapitalize: "off",
  6290. translate: "no",
  6291. contenteditable: !this.state.facet(editable) ? "false" : "true",
  6292. class: "cm-content",
  6293. style: `${browser.tabSize}: ${this.state.tabSize}`,
  6294. role: "textbox",
  6295. "aria-multiline": "true"
  6296. };
  6297. if (this.state.readOnly)
  6298. contentAttrs["aria-readonly"] = "true";
  6299. attrsFromFacet(this, contentAttributes, contentAttrs);
  6300. let changed = this.observer.ignore(() => {
  6301. let changedContent = updateAttrs(this.contentDOM, this.contentAttrs, contentAttrs);
  6302. let changedEditor = updateAttrs(this.dom, this.editorAttrs, editorAttrs);
  6303. return changedContent || changedEditor;
  6304. });
  6305. this.editorAttrs = editorAttrs;
  6306. this.contentAttrs = contentAttrs;
  6307. return changed;
  6308. }
  6309. showAnnouncements(trs) {
  6310. let first = true;
  6311. for (let tr of trs)
  6312. for (let effect of tr.effects)
  6313. if (effect.is(EditorView.announce)) {
  6314. if (first)
  6315. this.announceDOM.textContent = "";
  6316. first = false;
  6317. let div = this.announceDOM.appendChild(document.createElement("div"));
  6318. div.textContent = effect.value;
  6319. }
  6320. }
  6321. mountStyles() {
  6322. this.styleModules = this.state.facet(styleModule);
  6323. StyleModule.mount(this.root, this.styleModules.concat(baseTheme$1).reverse());
  6324. }
  6325. readMeasured() {
  6326. if (this.updateState == 2 /* Updating */)
  6327. throw new Error("Reading the editor layout isn't allowed during an update");
  6328. if (this.updateState == 0 /* Idle */ && this.measureScheduled > -1)
  6329. this.measure(false);
  6330. }
  6331. /**
  6332. Schedule a layout measurement, optionally providing callbacks to
  6333. do custom DOM measuring followed by a DOM write phase. Using
  6334. this is preferable reading DOM layout directly from, for
  6335. example, an event handler, because it'll make sure measuring and
  6336. drawing done by other components is synchronized, avoiding
  6337. unnecessary DOM layout computations.
  6338. */
  6339. requestMeasure(request) {
  6340. if (this.measureScheduled < 0)
  6341. this.measureScheduled = requestAnimationFrame(() => this.measure());
  6342. if (request) {
  6343. if (request.key != null)
  6344. for (let i = 0; i < this.measureRequests.length; i++) {
  6345. if (this.measureRequests[i].key === request.key) {
  6346. this.measureRequests[i] = request;
  6347. return;
  6348. }
  6349. }
  6350. this.measureRequests.push(request);
  6351. }
  6352. }
  6353. /**
  6354. Get the value of a specific plugin, if present. Note that
  6355. plugins that crash can be dropped from a view, so even when you
  6356. know you registered a given plugin, it is recommended to check
  6357. the return value of this method.
  6358. */
  6359. plugin(plugin) {
  6360. let known = this.pluginMap.get(plugin);
  6361. if (known === undefined || known && known.spec != plugin)
  6362. this.pluginMap.set(plugin, known = this.plugins.find(p => p.spec == plugin) || null);
  6363. return known && known.update(this).value;
  6364. }
  6365. /**
  6366. The top position of the document, in screen coordinates. This
  6367. may be negative when the editor is scrolled down. Points
  6368. directly to the top of the first line, not above the padding.
  6369. */
  6370. get documentTop() {
  6371. return this.contentDOM.getBoundingClientRect().top + this.viewState.paddingTop;
  6372. }
  6373. /**
  6374. Reports the padding above and below the document.
  6375. */
  6376. get documentPadding() {
  6377. return { top: this.viewState.paddingTop, bottom: this.viewState.paddingBottom };
  6378. }
  6379. /**
  6380. Find the text line or block widget at the given vertical
  6381. position (which is interpreted as relative to the [top of the
  6382. document](https://codemirror.net/6/docs/ref/#view.EditorView.documentTop)
  6383. */
  6384. elementAtHeight(height) {
  6385. this.readMeasured();
  6386. return this.viewState.elementAtHeight(height);
  6387. }
  6388. /**
  6389. Find the line block (see
  6390. [`lineBlockAt`](https://codemirror.net/6/docs/ref/#view.EditorView.lineBlockAt) at the given
  6391. height.
  6392. */
  6393. lineBlockAtHeight(height) {
  6394. this.readMeasured();
  6395. return this.viewState.lineBlockAtHeight(height);
  6396. }
  6397. /**
  6398. Get the extent and vertical position of all [line
  6399. blocks](https://codemirror.net/6/docs/ref/#view.EditorView.lineBlockAt) in the viewport. Positions
  6400. are relative to the [top of the
  6401. document](https://codemirror.net/6/docs/ref/#view.EditorView.documentTop);
  6402. */
  6403. get viewportLineBlocks() {
  6404. return this.viewState.viewportLines;
  6405. }
  6406. /**
  6407. Find the line block around the given document position. A line
  6408. block is a range delimited on both sides by either a
  6409. non-[hidden](https://codemirror.net/6/docs/ref/#view.Decoration^replace) line breaks, or the
  6410. start/end of the document. It will usually just hold a line of
  6411. text, but may be broken into multiple textblocks by block
  6412. widgets.
  6413. */
  6414. lineBlockAt(pos) {
  6415. return this.viewState.lineBlockAt(pos);
  6416. }
  6417. /**
  6418. The editor's total content height.
  6419. */
  6420. get contentHeight() {
  6421. return this.viewState.contentHeight;
  6422. }
  6423. /**
  6424. Move a cursor position by [grapheme
  6425. cluster](https://codemirror.net/6/docs/ref/#state.findClusterBreak). `forward` determines whether
  6426. the motion is away from the line start, or towards it. In
  6427. bidirectional text, the line is traversed in visual order, using
  6428. the editor's [text direction](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection).
  6429. When the start position was the last one on the line, the
  6430. returned position will be across the line break. If there is no
  6431. further line, the original position is returned.
  6432. By default, this method moves over a single cluster. The
  6433. optional `by` argument can be used to move across more. It will
  6434. be called with the first cluster as argument, and should return
  6435. a predicate that determines, for each subsequent cluster,
  6436. whether it should also be moved over.
  6437. */
  6438. moveByChar(start, forward, by) {
  6439. return skipAtoms(this, start, moveByChar(this, start, forward, by));
  6440. }
  6441. /**
  6442. Move a cursor position across the next group of either
  6443. [letters](https://codemirror.net/6/docs/ref/#state.EditorState.charCategorizer) or non-letter
  6444. non-whitespace characters.
  6445. */
  6446. moveByGroup(start, forward) {
  6447. return skipAtoms(this, start, moveByChar(this, start, forward, initial => byGroup(this, start.head, initial)));
  6448. }
  6449. /**
  6450. Move to the next line boundary in the given direction. If
  6451. `includeWrap` is true, line wrapping is on, and there is a
  6452. further wrap point on the current line, the wrap point will be
  6453. returned. Otherwise this function will return the start or end
  6454. of the line.
  6455. */
  6456. moveToLineBoundary(start, forward, includeWrap = true) {
  6457. return moveToLineBoundary(this, start, forward, includeWrap);
  6458. }
  6459. /**
  6460. Move a cursor position vertically. When `distance` isn't given,
  6461. it defaults to moving to the next line (including wrapped
  6462. lines). Otherwise, `distance` should provide a positive distance
  6463. in pixels.
  6464. When `start` has a
  6465. [`goalColumn`](https://codemirror.net/6/docs/ref/#state.SelectionRange.goalColumn), the vertical
  6466. motion will use that as a target horizontal position. Otherwise,
  6467. the cursor's own horizontal position is used. The returned
  6468. cursor will have its goal column set to whichever column was
  6469. used.
  6470. */
  6471. moveVertically(start, forward, distance) {
  6472. return skipAtoms(this, start, moveVertically(this, start, forward, distance));
  6473. }
  6474. /**
  6475. Find the DOM parent node and offset (child offset if `node` is
  6476. an element, character offset when it is a text node) at the
  6477. given document position.
  6478. Note that for positions that aren't currently in
  6479. `visibleRanges`, the resulting DOM position isn't necessarily
  6480. meaningful (it may just point before or after a placeholder
  6481. element).
  6482. */
  6483. domAtPos(pos) {
  6484. return this.docView.domAtPos(pos);
  6485. }
  6486. /**
  6487. Find the document position at the given DOM node. Can be useful
  6488. for associating positions with DOM events. Will raise an error
  6489. when `node` isn't part of the editor content.
  6490. */
  6491. posAtDOM(node, offset = 0) {
  6492. return this.docView.posFromDOM(node, offset);
  6493. }
  6494. posAtCoords(coords, precise = true) {
  6495. this.readMeasured();
  6496. return posAtCoords(this, coords, precise);
  6497. }
  6498. /**
  6499. Get the screen coordinates at the given document position.
  6500. `side` determines whether the coordinates are based on the
  6501. element before (-1) or after (1) the position (if no element is
  6502. available on the given side, the method will transparently use
  6503. another strategy to get reasonable coordinates).
  6504. */
  6505. coordsAtPos(pos, side = 1) {
  6506. this.readMeasured();
  6507. let rect = this.docView.coordsAt(pos, side);
  6508. if (!rect || rect.left == rect.right)
  6509. return rect;
  6510. let line = this.state.doc.lineAt(pos), order = this.bidiSpans(line);
  6511. let span = order[BidiSpan.find(order, pos - line.from, -1, side)];
  6512. return flattenRect(rect, (span.dir == Direction.LTR) == (side > 0));
  6513. }
  6514. /**
  6515. The default width of a character in the editor. May not
  6516. accurately reflect the width of all characters (given variable
  6517. width fonts or styling of invididual ranges).
  6518. */
  6519. get defaultCharacterWidth() { return this.viewState.heightOracle.charWidth; }
  6520. /**
  6521. The default height of a line in the editor. May not be accurate
  6522. for all lines.
  6523. */
  6524. get defaultLineHeight() { return this.viewState.heightOracle.lineHeight; }
  6525. /**
  6526. The text direction
  6527. ([`direction`](https://developer.mozilla.org/en-US/docs/Web/CSS/direction)
  6528. CSS property) of the editor's content element.
  6529. */
  6530. get textDirection() { return this.viewState.defaultTextDirection; }
  6531. /**
  6532. Find the text direction of the block at the given position, as
  6533. assigned by CSS. If
  6534. [`perLineTextDirection`](https://codemirror.net/6/docs/ref/#view.EditorView^perLineTextDirection)
  6535. isn't enabled, or the given position is outside of the viewport,
  6536. this will always return the same as
  6537. [`textDirection`](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection). Note that
  6538. this may trigger a DOM layout.
  6539. */
  6540. textDirectionAt(pos) {
  6541. let perLine = this.state.facet(perLineTextDirection);
  6542. if (!perLine || pos < this.viewport.from || pos > this.viewport.to)
  6543. return this.textDirection;
  6544. this.readMeasured();
  6545. return this.docView.textDirectionAt(pos);
  6546. }
  6547. /**
  6548. Whether this editor [wraps lines](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping)
  6549. (as determined by the
  6550. [`white-space`](https://developer.mozilla.org/en-US/docs/Web/CSS/white-space)
  6551. CSS property of its content element).
  6552. */
  6553. get lineWrapping() { return this.viewState.heightOracle.lineWrapping; }
  6554. /**
  6555. Returns the bidirectional text structure of the given line
  6556. (which should be in the current document) as an array of span
  6557. objects. The order of these spans matches the [text
  6558. direction](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection)—if that is
  6559. left-to-right, the leftmost spans come first, otherwise the
  6560. rightmost spans come first.
  6561. */
  6562. bidiSpans(line) {
  6563. if (line.length > MaxBidiLine)
  6564. return trivialOrder(line.length);
  6565. let dir = this.textDirectionAt(line.from);
  6566. for (let entry of this.bidiCache)
  6567. if (entry.from == line.from && entry.dir == dir)
  6568. return entry.order;
  6569. let order = computeOrder(line.text, dir);
  6570. this.bidiCache.push(new CachedOrder(line.from, line.to, dir, order));
  6571. return order;
  6572. }
  6573. /**
  6574. Check whether the editor has focus.
  6575. */
  6576. get hasFocus() {
  6577. var _a;
  6578. // Safari return false for hasFocus when the context menu is open
  6579. // or closing, which leads us to ignore selection changes from the
  6580. // context menu because it looks like the editor isn't focused.
  6581. // This kludges around that.
  6582. return (document.hasFocus() || browser.safari && ((_a = this.inputState) === null || _a === void 0 ? void 0 : _a.lastContextMenu) > Date.now() - 3e4) &&
  6583. this.root.activeElement == this.contentDOM;
  6584. }
  6585. /**
  6586. Put focus on the editor.
  6587. */
  6588. focus() {
  6589. this.observer.ignore(() => {
  6590. focusPreventScroll(this.contentDOM);
  6591. this.docView.updateSelection();
  6592. });
  6593. }
  6594. /**
  6595. Clean up this editor view, removing its element from the
  6596. document, unregistering event handlers, and notifying
  6597. plugins. The view instance can no longer be used after
  6598. calling this.
  6599. */
  6600. destroy() {
  6601. for (let plugin of this.plugins)
  6602. plugin.destroy(this);
  6603. this.plugins = [];
  6604. this.inputState.destroy();
  6605. this.dom.remove();
  6606. this.observer.destroy();
  6607. if (this.measureScheduled > -1)
  6608. cancelAnimationFrame(this.measureScheduled);
  6609. this.destroyed = true;
  6610. }
  6611. /**
  6612. Returns an effect that can be
  6613. [added](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) to a transaction to
  6614. cause it to scroll the given position or range into view.
  6615. */
  6616. static scrollIntoView(pos, options = {}) {
  6617. return scrollIntoView.of(new ScrollTarget(typeof pos == "number" ? EditorSelection.cursor(pos) : pos, options.y, options.x, options.yMargin, options.xMargin));
  6618. }
  6619. /**
  6620. Returns an extension that can be used to add DOM event handlers.
  6621. The value should be an object mapping event names to handler
  6622. functions. For any given event, such functions are ordered by
  6623. extension precedence, and the first handler to return true will
  6624. be assumed to have handled that event, and no other handlers or
  6625. built-in behavior will be activated for it. These are registered
  6626. on the [content element](https://codemirror.net/6/docs/ref/#view.EditorView.contentDOM), except
  6627. for `scroll` handlers, which will be called any time the
  6628. editor's [scroll element](https://codemirror.net/6/docs/ref/#view.EditorView.scrollDOM) or one of
  6629. its parent nodes is scrolled.
  6630. */
  6631. static domEventHandlers(handlers) {
  6632. return ViewPlugin.define(() => ({}), { eventHandlers: handlers });
  6633. }
  6634. /**
  6635. Create a theme extension. The first argument can be a
  6636. [`style-mod`](https://github.com/marijnh/style-mod#documentation)
  6637. style spec providing the styles for the theme. These will be
  6638. prefixed with a generated class for the style.
  6639. Because the selectors will be prefixed with a scope class, rule
  6640. that directly match the editor's [wrapper
  6641. element](https://codemirror.net/6/docs/ref/#view.EditorView.dom)—to which the scope class will be
  6642. added—need to be explicitly differentiated by adding an `&` to
  6643. the selector for that element—for example
  6644. `&.cm-focused`.
  6645. When `dark` is set to true, the theme will be marked as dark,
  6646. which will cause the `&dark` rules from [base
  6647. themes](https://codemirror.net/6/docs/ref/#view.EditorView^baseTheme) to be used (as opposed to
  6648. `&light` when a light theme is active).
  6649. */
  6650. static theme(spec, options) {
  6651. let prefix = StyleModule.newName();
  6652. let result = [theme.of(prefix), styleModule.of(buildTheme(`.${prefix}`, spec))];
  6653. if (options && options.dark)
  6654. result.push(darkTheme.of(true));
  6655. return result;
  6656. }
  6657. /**
  6658. Create an extension that adds styles to the base theme. Like
  6659. with [`theme`](https://codemirror.net/6/docs/ref/#view.EditorView^theme), use `&` to indicate the
  6660. place of the editor wrapper element when directly targeting
  6661. that. You can also use `&dark` or `&light` instead to only
  6662. target editors with a dark or light theme.
  6663. */
  6664. static baseTheme(spec) {
  6665. return Prec.lowest(styleModule.of(buildTheme("." + baseThemeID, spec, lightDarkIDs)));
  6666. }
  6667. /**
  6668. Retrieve an editor view instance from the view's DOM
  6669. representation.
  6670. */
  6671. static findFromDOM(dom) {
  6672. var _a;
  6673. let content = dom.querySelector(".cm-content");
  6674. let cView = content && ContentView.get(content) || ContentView.get(dom);
  6675. return ((_a = cView === null || cView === void 0 ? void 0 : cView.rootView) === null || _a === void 0 ? void 0 : _a.view) || null;
  6676. }
  6677. }
  6678. /**
  6679. Facet to add a [style
  6680. module](https://github.com/marijnh/style-mod#documentation) to
  6681. an editor view. The view will ensure that the module is
  6682. mounted in its [document
  6683. root](https://codemirror.net/6/docs/ref/#view.EditorView.constructor^config.root).
  6684. */
  6685. EditorView.styleModule = styleModule;
  6686. /**
  6687. An input handler can override the way changes to the editable
  6688. DOM content are handled. Handlers are passed the document
  6689. positions between which the change was found, and the new
  6690. content. When one returns true, no further input handlers are
  6691. called and the default behavior is prevented.
  6692. */
  6693. EditorView.inputHandler = inputHandler;
  6694. /**
  6695. By default, the editor assumes all its content has the same
  6696. [text direction](https://codemirror.net/6/docs/ref/#view.Direction). Configure this with a `true`
  6697. value to make it read the text direction of every (rendered)
  6698. line separately.
  6699. */
  6700. EditorView.perLineTextDirection = perLineTextDirection;
  6701. /**
  6702. Allows you to provide a function that should be called when the
  6703. library catches an exception from an extension (mostly from view
  6704. plugins, but may be used by other extensions to route exceptions
  6705. from user-code-provided callbacks). This is mostly useful for
  6706. debugging and logging. See [`logException`](https://codemirror.net/6/docs/ref/#view.logException).
  6707. */
  6708. EditorView.exceptionSink = exceptionSink;
  6709. /**
  6710. A facet that can be used to register a function to be called
  6711. every time the view updates.
  6712. */
  6713. EditorView.updateListener = updateListener;
  6714. /**
  6715. Facet that controls whether the editor content DOM is editable.
  6716. When its highest-precedence value is `false`, the element will
  6717. not have its `contenteditable` attribute set. (Note that this
  6718. doesn't affect API calls that change the editor content, even
  6719. when those are bound to keys or buttons. See the
  6720. [`readOnly`](https://codemirror.net/6/docs/ref/#state.EditorState.readOnly) facet for that.)
  6721. */
  6722. EditorView.editable = editable;
  6723. /**
  6724. Allows you to influence the way mouse selection happens. The
  6725. functions in this facet will be called for a `mousedown` event
  6726. on the editor, and can return an object that overrides the way a
  6727. selection is computed from that mouse click or drag.
  6728. */
  6729. EditorView.mouseSelectionStyle = mouseSelectionStyle;
  6730. /**
  6731. Facet used to configure whether a given selection drag event
  6732. should move or copy the selection. The given predicate will be
  6733. called with the `mousedown` event, and can return `true` when
  6734. the drag should move the content.
  6735. */
  6736. EditorView.dragMovesSelection = dragMovesSelection$1;
  6737. /**
  6738. Facet used to configure whether a given selecting click adds a
  6739. new range to the existing selection or replaces it entirely. The
  6740. default behavior is to check `event.metaKey` on macOS, and
  6741. `event.ctrlKey` elsewhere.
  6742. */
  6743. EditorView.clickAddsSelectionRange = clickAddsSelectionRange;
  6744. /**
  6745. A facet that determines which [decorations](https://codemirror.net/6/docs/ref/#view.Decoration)
  6746. are shown in the view. Decorations can be provided in two
  6747. ways—directly, or via a function that takes an editor view.
  6748. Only decoration sets provided directly are allowed to influence
  6749. the editor's vertical layout structure. The ones provided as
  6750. functions are called _after_ the new viewport has been computed,
  6751. and thus **must not** introduce block widgets or replacing
  6752. decorations that cover line breaks.
  6753. */
  6754. EditorView.decorations = decorations;
  6755. /**
  6756. Used to provide ranges that should be treated as atoms as far as
  6757. cursor motion is concerned. This causes methods like
  6758. [`moveByChar`](https://codemirror.net/6/docs/ref/#view.EditorView.moveByChar) and
  6759. [`moveVertically`](https://codemirror.net/6/docs/ref/#view.EditorView.moveVertically) (and the
  6760. commands built on top of them) to skip across such regions when
  6761. a selection endpoint would enter them. This does _not_ prevent
  6762. direct programmatic [selection
  6763. updates](https://codemirror.net/6/docs/ref/#state.TransactionSpec.selection) from moving into such
  6764. regions.
  6765. */
  6766. EditorView.atomicRanges = atomicRanges;
  6767. /**
  6768. Facet that allows extensions to provide additional scroll
  6769. margins (space around the sides of the scrolling element that
  6770. should be considered invisible). This can be useful when the
  6771. plugin introduces elements that cover part of that element (for
  6772. example a horizontally fixed gutter).
  6773. */
  6774. EditorView.scrollMargins = scrollMargins;
  6775. /**
  6776. This facet records whether a dark theme is active. The extension
  6777. returned by [`theme`](https://codemirror.net/6/docs/ref/#view.EditorView^theme) automatically
  6778. includes an instance of this when the `dark` option is set to
  6779. true.
  6780. */
  6781. EditorView.darkTheme = darkTheme;
  6782. /**
  6783. Facet that provides additional DOM attributes for the editor's
  6784. editable DOM element.
  6785. */
  6786. EditorView.contentAttributes = contentAttributes;
  6787. /**
  6788. Facet that provides DOM attributes for the editor's outer
  6789. element.
  6790. */
  6791. EditorView.editorAttributes = editorAttributes;
  6792. /**
  6793. An extension that enables line wrapping in the editor (by
  6794. setting CSS `white-space` to `pre-wrap` in the content).
  6795. */
  6796. EditorView.lineWrapping = /*@__PURE__*/EditorView.contentAttributes.of({ "class": "cm-lineWrapping" });
  6797. /**
  6798. State effect used to include screen reader announcements in a
  6799. transaction. These will be added to the DOM in a visually hidden
  6800. element with `aria-live="polite"` set, and should be used to
  6801. describe effects that are visually obvious but may not be
  6802. noticed by screen reader users (such as moving to the next
  6803. search match).
  6804. */
  6805. EditorView.announce = /*@__PURE__*/StateEffect.define();
  6806. // Maximum line length for which we compute accurate bidi info
  6807. const MaxBidiLine = 4096;
  6808. const BadMeasure = {};
  6809. class CachedOrder {
  6810. constructor(from, to, dir, order) {
  6811. this.from = from;
  6812. this.to = to;
  6813. this.dir = dir;
  6814. this.order = order;
  6815. }
  6816. static update(cache, changes) {
  6817. if (changes.empty)
  6818. return cache;
  6819. let result = [], lastDir = cache.length ? cache[cache.length - 1].dir : Direction.LTR;
  6820. for (let i = Math.max(0, cache.length - 10); i < cache.length; i++) {
  6821. let entry = cache[i];
  6822. if (entry.dir == lastDir && !changes.touchesRange(entry.from, entry.to))
  6823. result.push(new CachedOrder(changes.mapPos(entry.from, 1), changes.mapPos(entry.to, -1), entry.dir, entry.order));
  6824. }
  6825. return result;
  6826. }
  6827. }
  6828. function attrsFromFacet(view, facet, base) {
  6829. for (let sources = view.state.facet(facet), i = sources.length - 1; i >= 0; i--) {
  6830. let source = sources[i], value = typeof source == "function" ? source(view) : source;
  6831. if (value)
  6832. combineAttrs(value, base);
  6833. }
  6834. return base;
  6835. }
  6836. const currentPlatform = browser.mac ? "mac" : browser.windows ? "win" : browser.linux ? "linux" : "key";
  6837. function normalizeKeyName(name, platform) {
  6838. const parts = name.split(/-(?!$)/);
  6839. let result = parts[parts.length - 1];
  6840. if (result == "Space")
  6841. result = " ";
  6842. let alt, ctrl, shift, meta;
  6843. for (let i = 0; i < parts.length - 1; ++i) {
  6844. const mod = parts[i];
  6845. if (/^(cmd|meta|m)$/i.test(mod))
  6846. meta = true;
  6847. else if (/^a(lt)?$/i.test(mod))
  6848. alt = true;
  6849. else if (/^(c|ctrl|control)$/i.test(mod))
  6850. ctrl = true;
  6851. else if (/^s(hift)?$/i.test(mod))
  6852. shift = true;
  6853. else if (/^mod$/i.test(mod)) {
  6854. if (platform == "mac")
  6855. meta = true;
  6856. else
  6857. ctrl = true;
  6858. }
  6859. else
  6860. throw new Error("Unrecognized modifier name: " + mod);
  6861. }
  6862. if (alt)
  6863. result = "Alt-" + result;
  6864. if (ctrl)
  6865. result = "Ctrl-" + result;
  6866. if (meta)
  6867. result = "Meta-" + result;
  6868. if (shift)
  6869. result = "Shift-" + result;
  6870. return result;
  6871. }
  6872. function modifiers(name, event, shift) {
  6873. if (event.altKey)
  6874. name = "Alt-" + name;
  6875. if (event.ctrlKey)
  6876. name = "Ctrl-" + name;
  6877. if (event.metaKey)
  6878. name = "Meta-" + name;
  6879. if (shift !== false && event.shiftKey)
  6880. name = "Shift-" + name;
  6881. return name;
  6882. }
  6883. const handleKeyEvents = /*@__PURE__*/Prec.default(/*@__PURE__*/EditorView.domEventHandlers({
  6884. keydown(event, view) {
  6885. return runHandlers(getKeymap(view.state), event, view, "editor");
  6886. }
  6887. }));
  6888. /**
  6889. Facet used for registering keymaps.
  6890. You can add multiple keymaps to an editor. Their priorities
  6891. determine their precedence (the ones specified early or with high
  6892. priority get checked first). When a handler has returned `true`
  6893. for a given key, no further handlers are called.
  6894. */
  6895. const keymap = /*@__PURE__*/Facet.define({ enables: handleKeyEvents });
  6896. const Keymaps = /*@__PURE__*/new WeakMap();
  6897. // This is hidden behind an indirection, rather than directly computed
  6898. // by the facet, to keep internal types out of the facet's type.
  6899. function getKeymap(state) {
  6900. let bindings = state.facet(keymap);
  6901. let map = Keymaps.get(bindings);
  6902. if (!map)
  6903. Keymaps.set(bindings, map = buildKeymap(bindings.reduce((a, b) => a.concat(b), [])));
  6904. return map;
  6905. }
  6906. /**
  6907. Run the key handlers registered for a given scope. The event
  6908. object should be a `"keydown"` event. Returns true if any of the
  6909. handlers handled it.
  6910. */
  6911. function runScopeHandlers(view, event, scope) {
  6912. return runHandlers(getKeymap(view.state), event, view, scope);
  6913. }
  6914. let storedPrefix = null;
  6915. const PrefixTimeout = 4000;
  6916. function buildKeymap(bindings, platform = currentPlatform) {
  6917. let bound = Object.create(null);
  6918. let isPrefix = Object.create(null);
  6919. let checkPrefix = (name, is) => {
  6920. let current = isPrefix[name];
  6921. if (current == null)
  6922. isPrefix[name] = is;
  6923. else if (current != is)
  6924. throw new Error("Key binding " + name + " is used both as a regular binding and as a multi-stroke prefix");
  6925. };
  6926. let add = (scope, key, command, preventDefault) => {
  6927. let scopeObj = bound[scope] || (bound[scope] = Object.create(null));
  6928. let parts = key.split(/ (?!$)/).map(k => normalizeKeyName(k, platform));
  6929. for (let i = 1; i < parts.length; i++) {
  6930. let prefix = parts.slice(0, i).join(" ");
  6931. checkPrefix(prefix, true);
  6932. if (!scopeObj[prefix])
  6933. scopeObj[prefix] = {
  6934. preventDefault: true,
  6935. commands: [(view) => {
  6936. let ourObj = storedPrefix = { view, prefix, scope };
  6937. setTimeout(() => { if (storedPrefix == ourObj)
  6938. storedPrefix = null; }, PrefixTimeout);
  6939. return true;
  6940. }]
  6941. };
  6942. }
  6943. let full = parts.join(" ");
  6944. checkPrefix(full, false);
  6945. let binding = scopeObj[full] || (scopeObj[full] = { preventDefault: false, commands: [] });
  6946. binding.commands.push(command);
  6947. if (preventDefault)
  6948. binding.preventDefault = true;
  6949. };
  6950. for (let b of bindings) {
  6951. let name = b[platform] || b.key;
  6952. if (!name)
  6953. continue;
  6954. for (let scope of b.scope ? b.scope.split(" ") : ["editor"]) {
  6955. add(scope, name, b.run, b.preventDefault);
  6956. if (b.shift)
  6957. add(scope, "Shift-" + name, b.shift, b.preventDefault);
  6958. }
  6959. }
  6960. return bound;
  6961. }
  6962. function runHandlers(map, event, view, scope) {
  6963. let name = keyName(event);
  6964. let charCode = codePointAt(name, 0), isChar = codePointSize(charCode) == name.length && name != " ";
  6965. let prefix = "", fallthrough = false;
  6966. if (storedPrefix && storedPrefix.view == view && storedPrefix.scope == scope) {
  6967. prefix = storedPrefix.prefix + " ";
  6968. if (fallthrough = modifierCodes.indexOf(event.keyCode) < 0)
  6969. storedPrefix = null;
  6970. }
  6971. let runFor = (binding) => {
  6972. if (binding) {
  6973. for (let cmd of binding.commands)
  6974. if (cmd(view))
  6975. return true;
  6976. if (binding.preventDefault)
  6977. fallthrough = true;
  6978. }
  6979. return false;
  6980. };
  6981. let scopeObj = map[scope], baseName;
  6982. if (scopeObj) {
  6983. if (runFor(scopeObj[prefix + modifiers(name, event, !isChar)]))
  6984. return true;
  6985. if (isChar && (event.shiftKey || event.altKey || event.metaKey || charCode > 127) &&
  6986. (baseName = base[event.keyCode]) && baseName != name) {
  6987. if (runFor(scopeObj[prefix + modifiers(baseName, event, true)]))
  6988. return true;
  6989. else if (event.shiftKey && shift[event.keyCode] != baseName &&
  6990. runFor(scopeObj[prefix + modifiers(shift[event.keyCode], event, false)]))
  6991. return true;
  6992. }
  6993. else if (isChar && event.shiftKey) {
  6994. if (runFor(scopeObj[prefix + modifiers(name, event, true)]))
  6995. return true;
  6996. }
  6997. }
  6998. return fallthrough;
  6999. }
  7000. const CanHidePrimary = !browser.ios; // FIXME test IE
  7001. const selectionConfig = /*@__PURE__*/Facet.define({
  7002. combine(configs) {
  7003. return combineConfig(configs, {
  7004. cursorBlinkRate: 1200,
  7005. drawRangeCursor: true
  7006. }, {
  7007. cursorBlinkRate: (a, b) => Math.min(a, b),
  7008. drawRangeCursor: (a, b) => a || b
  7009. });
  7010. }
  7011. });
  7012. /**
  7013. Returns an extension that hides the browser's native selection and
  7014. cursor, replacing the selection with a background behind the text
  7015. (with the `cm-selectionBackground` class), and the
  7016. cursors with elements overlaid over the code (using
  7017. `cm-cursor-primary` and `cm-cursor-secondary`).
  7018. This allows the editor to display secondary selection ranges, and
  7019. tends to produce a type of selection more in line with that users
  7020. expect in a text editor (the native selection styling will often
  7021. leave gaps between lines and won't fill the horizontal space after
  7022. a line when the selection continues past it).
  7023. It does have a performance cost, in that it requires an extra DOM
  7024. layout cycle for many updates (the selection is drawn based on DOM
  7025. layout information that's only available after laying out the
  7026. content).
  7027. */
  7028. function drawSelection(config = {}) {
  7029. return [
  7030. selectionConfig.of(config),
  7031. drawSelectionPlugin,
  7032. hideNativeSelection
  7033. ];
  7034. }
  7035. class Piece {
  7036. constructor(left, top, width, height, className) {
  7037. this.left = left;
  7038. this.top = top;
  7039. this.width = width;
  7040. this.height = height;
  7041. this.className = className;
  7042. }
  7043. draw() {
  7044. let elt = document.createElement("div");
  7045. elt.className = this.className;
  7046. this.adjust(elt);
  7047. return elt;
  7048. }
  7049. adjust(elt) {
  7050. elt.style.left = this.left + "px";
  7051. elt.style.top = this.top + "px";
  7052. if (this.width >= 0)
  7053. elt.style.width = this.width + "px";
  7054. elt.style.height = this.height + "px";
  7055. }
  7056. eq(p) {
  7057. return this.left == p.left && this.top == p.top && this.width == p.width && this.height == p.height &&
  7058. this.className == p.className;
  7059. }
  7060. }
  7061. const drawSelectionPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
  7062. constructor(view) {
  7063. this.view = view;
  7064. this.rangePieces = [];
  7065. this.cursors = [];
  7066. this.measureReq = { read: this.readPos.bind(this), write: this.drawSel.bind(this) };
  7067. this.selectionLayer = view.scrollDOM.appendChild(document.createElement("div"));
  7068. this.selectionLayer.className = "cm-selectionLayer";
  7069. this.selectionLayer.setAttribute("aria-hidden", "true");
  7070. this.cursorLayer = view.scrollDOM.appendChild(document.createElement("div"));
  7071. this.cursorLayer.className = "cm-cursorLayer";
  7072. this.cursorLayer.setAttribute("aria-hidden", "true");
  7073. view.requestMeasure(this.measureReq);
  7074. this.setBlinkRate();
  7075. }
  7076. setBlinkRate() {
  7077. this.cursorLayer.style.animationDuration = this.view.state.facet(selectionConfig).cursorBlinkRate + "ms";
  7078. }
  7079. update(update) {
  7080. let confChanged = update.startState.facet(selectionConfig) != update.state.facet(selectionConfig);
  7081. if (confChanged || update.selectionSet || update.geometryChanged || update.viewportChanged)
  7082. this.view.requestMeasure(this.measureReq);
  7083. if (update.transactions.some(tr => tr.scrollIntoView))
  7084. this.cursorLayer.style.animationName = this.cursorLayer.style.animationName == "cm-blink" ? "cm-blink2" : "cm-blink";
  7085. if (confChanged)
  7086. this.setBlinkRate();
  7087. }
  7088. readPos() {
  7089. let { state } = this.view, conf = state.facet(selectionConfig);
  7090. let rangePieces = state.selection.ranges.map(r => r.empty ? [] : measureRange(this.view, r)).reduce((a, b) => a.concat(b));
  7091. let cursors = [];
  7092. for (let r of state.selection.ranges) {
  7093. let prim = r == state.selection.main;
  7094. if (r.empty ? !prim || CanHidePrimary : conf.drawRangeCursor) {
  7095. let piece = measureCursor(this.view, r, prim);
  7096. if (piece)
  7097. cursors.push(piece);
  7098. }
  7099. }
  7100. return { rangePieces, cursors };
  7101. }
  7102. drawSel({ rangePieces, cursors }) {
  7103. if (rangePieces.length != this.rangePieces.length || rangePieces.some((p, i) => !p.eq(this.rangePieces[i]))) {
  7104. this.selectionLayer.textContent = "";
  7105. for (let p of rangePieces)
  7106. this.selectionLayer.appendChild(p.draw());
  7107. this.rangePieces = rangePieces;
  7108. }
  7109. if (cursors.length != this.cursors.length || cursors.some((c, i) => !c.eq(this.cursors[i]))) {
  7110. let oldCursors = this.cursorLayer.children;
  7111. if (oldCursors.length !== cursors.length) {
  7112. this.cursorLayer.textContent = "";
  7113. for (const c of cursors)
  7114. this.cursorLayer.appendChild(c.draw());
  7115. }
  7116. else {
  7117. cursors.forEach((c, idx) => c.adjust(oldCursors[idx]));
  7118. }
  7119. this.cursors = cursors;
  7120. }
  7121. }
  7122. destroy() {
  7123. this.selectionLayer.remove();
  7124. this.cursorLayer.remove();
  7125. }
  7126. });
  7127. const themeSpec = {
  7128. ".cm-line": {
  7129. "& ::selection": { backgroundColor: "transparent !important" },
  7130. "&::selection": { backgroundColor: "transparent !important" }
  7131. }
  7132. };
  7133. if (CanHidePrimary)
  7134. themeSpec[".cm-line"].caretColor = "transparent !important";
  7135. const hideNativeSelection = /*@__PURE__*/Prec.highest(/*@__PURE__*/EditorView.theme(themeSpec));
  7136. function getBase(view) {
  7137. let rect = view.scrollDOM.getBoundingClientRect();
  7138. let left = view.textDirection == Direction.LTR ? rect.left : rect.right - view.scrollDOM.clientWidth;
  7139. return { left: left - view.scrollDOM.scrollLeft, top: rect.top - view.scrollDOM.scrollTop };
  7140. }
  7141. function wrappedLine(view, pos, inside) {
  7142. let range = EditorSelection.cursor(pos);
  7143. return { from: Math.max(inside.from, view.moveToLineBoundary(range, false, true).from),
  7144. to: Math.min(inside.to, view.moveToLineBoundary(range, true, true).from),
  7145. type: BlockType.Text };
  7146. }
  7147. function blockAt(view, pos) {
  7148. let line = view.lineBlockAt(pos);
  7149. if (Array.isArray(line.type))
  7150. for (let l of line.type) {
  7151. if (l.to > pos || l.to == pos && (l.to == line.to || l.type == BlockType.Text))
  7152. return l;
  7153. }
  7154. return line;
  7155. }
  7156. function measureRange(view, range) {
  7157. if (range.to <= view.viewport.from || range.from >= view.viewport.to)
  7158. return [];
  7159. let from = Math.max(range.from, view.viewport.from), to = Math.min(range.to, view.viewport.to);
  7160. let ltr = view.textDirection == Direction.LTR;
  7161. let content = view.contentDOM, contentRect = content.getBoundingClientRect(), base = getBase(view);
  7162. let lineStyle = window.getComputedStyle(content.firstChild);
  7163. let leftSide = contentRect.left + parseInt(lineStyle.paddingLeft) + Math.min(0, parseInt(lineStyle.textIndent));
  7164. let rightSide = contentRect.right - parseInt(lineStyle.paddingRight);
  7165. let startBlock = blockAt(view, from), endBlock = blockAt(view, to);
  7166. let visualStart = startBlock.type == BlockType.Text ? startBlock : null;
  7167. let visualEnd = endBlock.type == BlockType.Text ? endBlock : null;
  7168. if (view.lineWrapping) {
  7169. if (visualStart)
  7170. visualStart = wrappedLine(view, from, visualStart);
  7171. if (visualEnd)
  7172. visualEnd = wrappedLine(view, to, visualEnd);
  7173. }
  7174. if (visualStart && visualEnd && visualStart.from == visualEnd.from) {
  7175. return pieces(drawForLine(range.from, range.to, visualStart));
  7176. }
  7177. else {
  7178. let top = visualStart ? drawForLine(range.from, null, visualStart) : drawForWidget(startBlock, false);
  7179. let bottom = visualEnd ? drawForLine(null, range.to, visualEnd) : drawForWidget(endBlock, true);
  7180. let between = [];
  7181. if ((visualStart || startBlock).to < (visualEnd || endBlock).from - 1)
  7182. between.push(piece(leftSide, top.bottom, rightSide, bottom.top));
  7183. else if (top.bottom < bottom.top && view.elementAtHeight((top.bottom + bottom.top) / 2).type == BlockType.Text)
  7184. top.bottom = bottom.top = (top.bottom + bottom.top) / 2;
  7185. return pieces(top).concat(between).concat(pieces(bottom));
  7186. }
  7187. function piece(left, top, right, bottom) {
  7188. return new Piece(left - base.left, top - base.top - 0.01 /* Epsilon */, right - left, bottom - top + 0.01 /* Epsilon */, "cm-selectionBackground");
  7189. }
  7190. function pieces({ top, bottom, horizontal }) {
  7191. let pieces = [];
  7192. for (let i = 0; i < horizontal.length; i += 2)
  7193. pieces.push(piece(horizontal[i], top, horizontal[i + 1], bottom));
  7194. return pieces;
  7195. }
  7196. // Gets passed from/to in line-local positions
  7197. function drawForLine(from, to, line) {
  7198. let top = 1e9, bottom = -1e9, horizontal = [];
  7199. function addSpan(from, fromOpen, to, toOpen, dir) {
  7200. // Passing 2/-2 is a kludge to force the view to return
  7201. // coordinates on the proper side of block widgets, since
  7202. // normalizing the side there, though appropriate for most
  7203. // coordsAtPos queries, would break selection drawing.
  7204. let fromCoords = view.coordsAtPos(from, (from == line.to ? -2 : 2));
  7205. let toCoords = view.coordsAtPos(to, (to == line.from ? 2 : -2));
  7206. top = Math.min(fromCoords.top, toCoords.top, top);
  7207. bottom = Math.max(fromCoords.bottom, toCoords.bottom, bottom);
  7208. if (dir == Direction.LTR)
  7209. horizontal.push(ltr && fromOpen ? leftSide : fromCoords.left, ltr && toOpen ? rightSide : toCoords.right);
  7210. else
  7211. horizontal.push(!ltr && toOpen ? leftSide : toCoords.left, !ltr && fromOpen ? rightSide : fromCoords.right);
  7212. }
  7213. let start = from !== null && from !== void 0 ? from : line.from, end = to !== null && to !== void 0 ? to : line.to;
  7214. // Split the range by visible range and document line
  7215. for (let r of view.visibleRanges)
  7216. if (r.to > start && r.from < end) {
  7217. for (let pos = Math.max(r.from, start), endPos = Math.min(r.to, end);;) {
  7218. let docLine = view.state.doc.lineAt(pos);
  7219. for (let span of view.bidiSpans(docLine)) {
  7220. let spanFrom = span.from + docLine.from, spanTo = span.to + docLine.from;
  7221. if (spanFrom >= endPos)
  7222. break;
  7223. if (spanTo > pos)
  7224. addSpan(Math.max(spanFrom, pos), from == null && spanFrom <= start, Math.min(spanTo, endPos), to == null && spanTo >= end, span.dir);
  7225. }
  7226. pos = docLine.to + 1;
  7227. if (pos >= endPos)
  7228. break;
  7229. }
  7230. }
  7231. if (horizontal.length == 0)
  7232. addSpan(start, from == null, end, to == null, view.textDirection);
  7233. return { top, bottom, horizontal };
  7234. }
  7235. function drawForWidget(block, top) {
  7236. let y = contentRect.top + (top ? block.top : block.bottom);
  7237. return { top: y, bottom: y, horizontal: [] };
  7238. }
  7239. }
  7240. function measureCursor(view, cursor, primary) {
  7241. let pos = view.coordsAtPos(cursor.head, cursor.assoc || 1);
  7242. if (!pos)
  7243. return null;
  7244. let base = getBase(view);
  7245. return new Piece(pos.left - base.left, pos.top - base.top, -1, pos.bottom - pos.top, primary ? "cm-cursor cm-cursor-primary" : "cm-cursor cm-cursor-secondary");
  7246. }
  7247. const setDropCursorPos = /*@__PURE__*/StateEffect.define({
  7248. map(pos, mapping) { return pos == null ? null : mapping.mapPos(pos); }
  7249. });
  7250. const dropCursorPos = /*@__PURE__*/StateField.define({
  7251. create() { return null; },
  7252. update(pos, tr) {
  7253. if (pos != null)
  7254. pos = tr.changes.mapPos(pos);
  7255. return tr.effects.reduce((pos, e) => e.is(setDropCursorPos) ? e.value : pos, pos);
  7256. }
  7257. });
  7258. const drawDropCursor = /*@__PURE__*/ViewPlugin.fromClass(class {
  7259. constructor(view) {
  7260. this.view = view;
  7261. this.cursor = null;
  7262. this.measureReq = { read: this.readPos.bind(this), write: this.drawCursor.bind(this) };
  7263. }
  7264. update(update) {
  7265. var _a;
  7266. let cursorPos = update.state.field(dropCursorPos);
  7267. if (cursorPos == null) {
  7268. if (this.cursor != null) {
  7269. (_a = this.cursor) === null || _a === void 0 ? void 0 : _a.remove();
  7270. this.cursor = null;
  7271. }
  7272. }
  7273. else {
  7274. if (!this.cursor) {
  7275. this.cursor = this.view.scrollDOM.appendChild(document.createElement("div"));
  7276. this.cursor.className = "cm-dropCursor";
  7277. }
  7278. if (update.startState.field(dropCursorPos) != cursorPos || update.docChanged || update.geometryChanged)
  7279. this.view.requestMeasure(this.measureReq);
  7280. }
  7281. }
  7282. readPos() {
  7283. let pos = this.view.state.field(dropCursorPos);
  7284. let rect = pos != null && this.view.coordsAtPos(pos);
  7285. if (!rect)
  7286. return null;
  7287. let outer = this.view.scrollDOM.getBoundingClientRect();
  7288. return {
  7289. left: rect.left - outer.left + this.view.scrollDOM.scrollLeft,
  7290. top: rect.top - outer.top + this.view.scrollDOM.scrollTop,
  7291. height: rect.bottom - rect.top
  7292. };
  7293. }
  7294. drawCursor(pos) {
  7295. if (this.cursor) {
  7296. if (pos) {
  7297. this.cursor.style.left = pos.left + "px";
  7298. this.cursor.style.top = pos.top + "px";
  7299. this.cursor.style.height = pos.height + "px";
  7300. }
  7301. else {
  7302. this.cursor.style.left = "-100000px";
  7303. }
  7304. }
  7305. }
  7306. destroy() {
  7307. if (this.cursor)
  7308. this.cursor.remove();
  7309. }
  7310. setDropPos(pos) {
  7311. if (this.view.state.field(dropCursorPos) != pos)
  7312. this.view.dispatch({ effects: setDropCursorPos.of(pos) });
  7313. }
  7314. }, {
  7315. eventHandlers: {
  7316. dragover(event) {
  7317. this.setDropPos(this.view.posAtCoords({ x: event.clientX, y: event.clientY }));
  7318. },
  7319. dragleave(event) {
  7320. if (event.target == this.view.contentDOM || !this.view.contentDOM.contains(event.relatedTarget))
  7321. this.setDropPos(null);
  7322. },
  7323. dragend() {
  7324. this.setDropPos(null);
  7325. },
  7326. drop() {
  7327. this.setDropPos(null);
  7328. }
  7329. }
  7330. });
  7331. /**
  7332. Draws a cursor at the current drop position when something is
  7333. dragged over the editor.
  7334. */
  7335. function dropCursor() {
  7336. return [dropCursorPos, drawDropCursor];
  7337. }
  7338. function iterMatches(doc, re, from, to, f) {
  7339. re.lastIndex = 0;
  7340. for (let cursor = doc.iterRange(from, to), pos = from, m; !cursor.next().done; pos += cursor.value.length) {
  7341. if (!cursor.lineBreak)
  7342. while (m = re.exec(cursor.value))
  7343. f(pos + m.index, pos + m.index + m[0].length, m);
  7344. }
  7345. }
  7346. function matchRanges(view, maxLength) {
  7347. let visible = view.visibleRanges;
  7348. if (visible.length == 1 && visible[0].from == view.viewport.from &&
  7349. visible[0].to == view.viewport.to)
  7350. return visible;
  7351. let result = [];
  7352. for (let { from, to } of visible) {
  7353. from = Math.max(view.state.doc.lineAt(from).from, from - maxLength);
  7354. to = Math.min(view.state.doc.lineAt(to).to, to + maxLength);
  7355. if (result.length && result[result.length - 1].to >= from)
  7356. result[result.length - 1].to = to;
  7357. else
  7358. result.push({ from, to });
  7359. }
  7360. return result;
  7361. }
  7362. /**
  7363. Helper class used to make it easier to maintain decorations on
  7364. visible code that matches a given regular expression. To be used
  7365. in a [view plugin](https://codemirror.net/6/docs/ref/#view.ViewPlugin). Instances of this object
  7366. represent a matching configuration.
  7367. */
  7368. class MatchDecorator {
  7369. /**
  7370. Create a decorator.
  7371. */
  7372. constructor(config) {
  7373. let { regexp, decoration, boundary, maxLength = 1000 } = config;
  7374. if (!regexp.global)
  7375. throw new RangeError("The regular expression given to MatchDecorator should have its 'g' flag set");
  7376. this.regexp = regexp;
  7377. this.getDeco = typeof decoration == "function" ? decoration : () => decoration;
  7378. this.boundary = boundary;
  7379. this.maxLength = maxLength;
  7380. }
  7381. /**
  7382. Compute the full set of decorations for matches in the given
  7383. view's viewport. You'll want to call this when initializing your
  7384. plugin.
  7385. */
  7386. createDeco(view) {
  7387. let build = new RangeSetBuilder();
  7388. for (let { from, to } of matchRanges(view, this.maxLength))
  7389. iterMatches(view.state.doc, this.regexp, from, to, (a, b, m) => build.add(a, b, this.getDeco(m, view, a)));
  7390. return build.finish();
  7391. }
  7392. /**
  7393. Update a set of decorations for a view update. `deco` _must_ be
  7394. the set of decorations produced by _this_ `MatchDecorator` for
  7395. the view state before the update.
  7396. */
  7397. updateDeco(update, deco) {
  7398. let changeFrom = 1e9, changeTo = -1;
  7399. if (update.docChanged)
  7400. update.changes.iterChanges((_f, _t, from, to) => {
  7401. if (to > update.view.viewport.from && from < update.view.viewport.to) {
  7402. changeFrom = Math.min(from, changeFrom);
  7403. changeTo = Math.max(to, changeTo);
  7404. }
  7405. });
  7406. if (update.viewportChanged || changeTo - changeFrom > 1000)
  7407. return this.createDeco(update.view);
  7408. if (changeTo > -1)
  7409. return this.updateRange(update.view, deco.map(update.changes), changeFrom, changeTo);
  7410. return deco;
  7411. }
  7412. updateRange(view, deco, updateFrom, updateTo) {
  7413. for (let r of view.visibleRanges) {
  7414. let from = Math.max(r.from, updateFrom), to = Math.min(r.to, updateTo);
  7415. if (to > from) {
  7416. let fromLine = view.state.doc.lineAt(from), toLine = fromLine.to < to ? view.state.doc.lineAt(to) : fromLine;
  7417. let start = Math.max(r.from, fromLine.from), end = Math.min(r.to, toLine.to);
  7418. if (this.boundary) {
  7419. for (; from > fromLine.from; from--)
  7420. if (this.boundary.test(fromLine.text[from - 1 - fromLine.from])) {
  7421. start = from;
  7422. break;
  7423. }
  7424. for (; to < toLine.to; to++)
  7425. if (this.boundary.test(toLine.text[to - toLine.from])) {
  7426. end = to;
  7427. break;
  7428. }
  7429. }
  7430. let ranges = [], m;
  7431. if (fromLine == toLine) {
  7432. this.regexp.lastIndex = start - fromLine.from;
  7433. while ((m = this.regexp.exec(fromLine.text)) && m.index < end - fromLine.from) {
  7434. let pos = m.index + fromLine.from;
  7435. ranges.push(this.getDeco(m, view, pos).range(pos, pos + m[0].length));
  7436. }
  7437. }
  7438. else {
  7439. iterMatches(view.state.doc, this.regexp, start, end, (from, to, m) => ranges.push(this.getDeco(m, view, from).range(from, to)));
  7440. }
  7441. deco = deco.update({ filterFrom: start, filterTo: end, filter: (from, to) => from < start || to > end, add: ranges });
  7442. }
  7443. }
  7444. return deco;
  7445. }
  7446. }
  7447. const UnicodeRegexpSupport = /x/.unicode != null ? "gu" : "g";
  7448. const Specials = /*@__PURE__*/new RegExp("[\u0000-\u0008\u000a-\u001f\u007f-\u009f\u00ad\u061c\u200b\u200e\u200f\u2028\u2029\u202d\u202e\ufeff\ufff9-\ufffc]", UnicodeRegexpSupport);
  7449. const Names = {
  7450. 0: "null",
  7451. 7: "bell",
  7452. 8: "backspace",
  7453. 10: "newline",
  7454. 11: "vertical tab",
  7455. 13: "carriage return",
  7456. 27: "escape",
  7457. 8203: "zero width space",
  7458. 8204: "zero width non-joiner",
  7459. 8205: "zero width joiner",
  7460. 8206: "left-to-right mark",
  7461. 8207: "right-to-left mark",
  7462. 8232: "line separator",
  7463. 8237: "left-to-right override",
  7464. 8238: "right-to-left override",
  7465. 8233: "paragraph separator",
  7466. 65279: "zero width no-break space",
  7467. 65532: "object replacement"
  7468. };
  7469. let _supportsTabSize = null;
  7470. function supportsTabSize() {
  7471. var _a;
  7472. if (_supportsTabSize == null && typeof document != "undefined" && document.body) {
  7473. let styles = document.body.style;
  7474. _supportsTabSize = ((_a = styles.tabSize) !== null && _a !== void 0 ? _a : styles.MozTabSize) != null;
  7475. }
  7476. return _supportsTabSize || false;
  7477. }
  7478. const specialCharConfig = /*@__PURE__*/Facet.define({
  7479. combine(configs) {
  7480. let config = combineConfig(configs, {
  7481. render: null,
  7482. specialChars: Specials,
  7483. addSpecialChars: null
  7484. });
  7485. if (config.replaceTabs = !supportsTabSize())
  7486. config.specialChars = new RegExp("\t|" + config.specialChars.source, UnicodeRegexpSupport);
  7487. if (config.addSpecialChars)
  7488. config.specialChars = new RegExp(config.specialChars.source + "|" + config.addSpecialChars.source, UnicodeRegexpSupport);
  7489. return config;
  7490. }
  7491. });
  7492. /**
  7493. Returns an extension that installs highlighting of special
  7494. characters.
  7495. */
  7496. function highlightSpecialChars(
  7497. /**
  7498. Configuration options.
  7499. */
  7500. config = {}) {
  7501. return [specialCharConfig.of(config), specialCharPlugin()];
  7502. }
  7503. let _plugin = null;
  7504. function specialCharPlugin() {
  7505. return _plugin || (_plugin = ViewPlugin.fromClass(class {
  7506. constructor(view) {
  7507. this.view = view;
  7508. this.decorations = Decoration.none;
  7509. this.decorationCache = Object.create(null);
  7510. this.decorator = this.makeDecorator(view.state.facet(specialCharConfig));
  7511. this.decorations = this.decorator.createDeco(view);
  7512. }
  7513. makeDecorator(conf) {
  7514. return new MatchDecorator({
  7515. regexp: conf.specialChars,
  7516. decoration: (m, view, pos) => {
  7517. let { doc } = view.state;
  7518. let code = codePointAt(m[0], 0);
  7519. if (code == 9) {
  7520. let line = doc.lineAt(pos);
  7521. let size = view.state.tabSize, col = countColumn(line.text, size, pos - line.from);
  7522. return Decoration.replace({ widget: new TabWidget((size - (col % size)) * this.view.defaultCharacterWidth) });
  7523. }
  7524. return this.decorationCache[code] ||
  7525. (this.decorationCache[code] = Decoration.replace({ widget: new SpecialCharWidget(conf, code) }));
  7526. },
  7527. boundary: conf.replaceTabs ? undefined : /[^]/
  7528. });
  7529. }
  7530. update(update) {
  7531. let conf = update.state.facet(specialCharConfig);
  7532. if (update.startState.facet(specialCharConfig) != conf) {
  7533. this.decorator = this.makeDecorator(conf);
  7534. this.decorations = this.decorator.createDeco(update.view);
  7535. }
  7536. else {
  7537. this.decorations = this.decorator.updateDeco(update, this.decorations);
  7538. }
  7539. }
  7540. }, {
  7541. decorations: v => v.decorations
  7542. }));
  7543. }
  7544. const DefaultPlaceholder = "\u2022";
  7545. // Assigns placeholder characters from the Control Pictures block to
  7546. // ASCII control characters
  7547. function placeholder$1(code) {
  7548. if (code >= 32)
  7549. return DefaultPlaceholder;
  7550. if (code == 10)
  7551. return "\u2424";
  7552. return String.fromCharCode(9216 + code);
  7553. }
  7554. class SpecialCharWidget extends WidgetType {
  7555. constructor(options, code) {
  7556. super();
  7557. this.options = options;
  7558. this.code = code;
  7559. }
  7560. eq(other) { return other.code == this.code; }
  7561. toDOM(view) {
  7562. let ph = placeholder$1(this.code);
  7563. let desc = view.state.phrase("Control character") + " " + (Names[this.code] || "0x" + this.code.toString(16));
  7564. let custom = this.options.render && this.options.render(this.code, desc, ph);
  7565. if (custom)
  7566. return custom;
  7567. let span = document.createElement("span");
  7568. span.textContent = ph;
  7569. span.title = desc;
  7570. span.setAttribute("aria-label", desc);
  7571. span.className = "cm-specialChar";
  7572. return span;
  7573. }
  7574. ignoreEvent() { return false; }
  7575. }
  7576. class TabWidget extends WidgetType {
  7577. constructor(width) {
  7578. super();
  7579. this.width = width;
  7580. }
  7581. eq(other) { return other.width == this.width; }
  7582. toDOM() {
  7583. let span = document.createElement("span");
  7584. span.textContent = "\t";
  7585. span.className = "cm-tab";
  7586. span.style.width = this.width + "px";
  7587. return span;
  7588. }
  7589. ignoreEvent() { return false; }
  7590. }
  7591. const plugin = /*@__PURE__*/ViewPlugin.fromClass(class {
  7592. constructor() {
  7593. this.height = 1000;
  7594. this.attrs = { style: "padding-bottom: 1000px" };
  7595. }
  7596. update(update) {
  7597. let height = update.view.viewState.editorHeight - update.view.defaultLineHeight;
  7598. if (height != this.height) {
  7599. this.height = height;
  7600. this.attrs = { style: `padding-bottom: ${height}px` };
  7601. }
  7602. }
  7603. });
  7604. /**
  7605. Returns an extension that makes sure the content has a bottom
  7606. margin equivalent to the height of the editor, minus one line
  7607. height, so that every line in the document can be scrolled to the
  7608. top of the editor.
  7609. This is only meaningful when the editor is scrollable, and should
  7610. not be enabled in editors that take the size of their content.
  7611. */
  7612. function scrollPastEnd() {
  7613. return [plugin, contentAttributes.of(view => { var _a; return ((_a = view.plugin(plugin)) === null || _a === void 0 ? void 0 : _a.attrs) || null; })];
  7614. }
  7615. /**
  7616. Mark lines that have a cursor on them with the `"cm-activeLine"`
  7617. DOM class.
  7618. */
  7619. function highlightActiveLine() {
  7620. return activeLineHighlighter;
  7621. }
  7622. const lineDeco = /*@__PURE__*/Decoration.line({ class: "cm-activeLine" });
  7623. const activeLineHighlighter = /*@__PURE__*/ViewPlugin.fromClass(class {
  7624. constructor(view) {
  7625. this.decorations = this.getDeco(view);
  7626. }
  7627. update(update) {
  7628. if (update.docChanged || update.selectionSet)
  7629. this.decorations = this.getDeco(update.view);
  7630. }
  7631. getDeco(view) {
  7632. let lastLineStart = -1, deco = [];
  7633. for (let r of view.state.selection.ranges) {
  7634. if (!r.empty)
  7635. return Decoration.none;
  7636. let line = view.lineBlockAt(r.head);
  7637. if (line.from > lastLineStart) {
  7638. deco.push(lineDeco.range(line.from));
  7639. lastLineStart = line.from;
  7640. }
  7641. }
  7642. return Decoration.set(deco);
  7643. }
  7644. }, {
  7645. decorations: v => v.decorations
  7646. });
  7647. class Placeholder extends WidgetType {
  7648. constructor(content) {
  7649. super();
  7650. this.content = content;
  7651. }
  7652. toDOM() {
  7653. let wrap = document.createElement("span");
  7654. wrap.className = "cm-placeholder";
  7655. wrap.style.pointerEvents = "none";
  7656. wrap.appendChild(typeof this.content == "string" ? document.createTextNode(this.content) : this.content);
  7657. if (typeof this.content == "string")
  7658. wrap.setAttribute("aria-label", "placeholder " + this.content);
  7659. else
  7660. wrap.setAttribute("aria-hidden", "true");
  7661. return wrap;
  7662. }
  7663. ignoreEvent() { return false; }
  7664. }
  7665. /**
  7666. Extension that enables a placeholder—a piece of example content
  7667. to show when the editor is empty.
  7668. */
  7669. function placeholder(content) {
  7670. return ViewPlugin.fromClass(class {
  7671. constructor(view) {
  7672. this.view = view;
  7673. this.placeholder = Decoration.set([Decoration.widget({ widget: new Placeholder(content), side: 1 }).range(0)]);
  7674. }
  7675. get decorations() { return this.view.state.doc.length ? Decoration.none : this.placeholder; }
  7676. }, { decorations: v => v.decorations });
  7677. }
  7678. // Don't compute precise column positions for line offsets above this
  7679. // (since it could get expensive). Assume offset==column for them.
  7680. const MaxOff = 2000;
  7681. function rectangleFor(state, a, b) {
  7682. let startLine = Math.min(a.line, b.line), endLine = Math.max(a.line, b.line);
  7683. let ranges = [];
  7684. if (a.off > MaxOff || b.off > MaxOff || a.col < 0 || b.col < 0) {
  7685. let startOff = Math.min(a.off, b.off), endOff = Math.max(a.off, b.off);
  7686. for (let i = startLine; i <= endLine; i++) {
  7687. let line = state.doc.line(i);
  7688. if (line.length <= endOff)
  7689. ranges.push(EditorSelection.range(line.from + startOff, line.to + endOff));
  7690. }
  7691. }
  7692. else {
  7693. let startCol = Math.min(a.col, b.col), endCol = Math.max(a.col, b.col);
  7694. for (let i = startLine; i <= endLine; i++) {
  7695. let line = state.doc.line(i);
  7696. let start = findColumn(line.text, startCol, state.tabSize, true);
  7697. if (start > -1) {
  7698. let end = findColumn(line.text, endCol, state.tabSize);
  7699. ranges.push(EditorSelection.range(line.from + start, line.from + end));
  7700. }
  7701. }
  7702. }
  7703. return ranges;
  7704. }
  7705. function absoluteColumn(view, x) {
  7706. let ref = view.coordsAtPos(view.viewport.from);
  7707. return ref ? Math.round(Math.abs((ref.left - x) / view.defaultCharacterWidth)) : -1;
  7708. }
  7709. function getPos(view, event) {
  7710. let offset = view.posAtCoords({ x: event.clientX, y: event.clientY }, false);
  7711. let line = view.state.doc.lineAt(offset), off = offset - line.from;
  7712. let col = off > MaxOff ? -1
  7713. : off == line.length ? absoluteColumn(view, event.clientX)
  7714. : countColumn(line.text, view.state.tabSize, offset - line.from);
  7715. return { line: line.number, col, off };
  7716. }
  7717. function rectangleSelectionStyle(view, event) {
  7718. let start = getPos(view, event), startSel = view.state.selection;
  7719. if (!start)
  7720. return null;
  7721. return {
  7722. update(update) {
  7723. if (update.docChanged) {
  7724. let newStart = update.changes.mapPos(update.startState.doc.line(start.line).from);
  7725. let newLine = update.state.doc.lineAt(newStart);
  7726. start = { line: newLine.number, col: start.col, off: Math.min(start.off, newLine.length) };
  7727. startSel = startSel.map(update.changes);
  7728. }
  7729. },
  7730. get(event, _extend, multiple) {
  7731. let cur = getPos(view, event);
  7732. if (!cur)
  7733. return startSel;
  7734. let ranges = rectangleFor(view.state, start, cur);
  7735. if (!ranges.length)
  7736. return startSel;
  7737. if (multiple)
  7738. return EditorSelection.create(ranges.concat(startSel.ranges));
  7739. else
  7740. return EditorSelection.create(ranges);
  7741. }
  7742. };
  7743. }
  7744. /**
  7745. Create an extension that enables rectangular selections. By
  7746. default, it will react to left mouse drag with the Alt key held
  7747. down. When such a selection occurs, the text within the rectangle
  7748. that was dragged over will be selected, as one selection
  7749. [range](https://codemirror.net/6/docs/ref/#state.SelectionRange) per line.
  7750. */
  7751. function rectangularSelection(options) {
  7752. let filter = (options === null || options === void 0 ? void 0 : options.eventFilter) || (e => e.altKey && e.button == 0);
  7753. return EditorView.mouseSelectionStyle.of((view, event) => filter(event) ? rectangleSelectionStyle(view, event) : null);
  7754. }
  7755. const keys = {
  7756. Alt: [18, e => e.altKey],
  7757. Control: [17, e => e.ctrlKey],
  7758. Shift: [16, e => e.shiftKey],
  7759. Meta: [91, e => e.metaKey]
  7760. };
  7761. const showCrosshair = { style: "cursor: crosshair" };
  7762. /**
  7763. Returns an extension that turns the pointer cursor into a
  7764. crosshair when a given modifier key, defaulting to Alt, is held
  7765. down. Can serve as a visual hint that rectangular selection is
  7766. going to happen when paired with
  7767. [`rectangularSelection`](https://codemirror.net/6/docs/ref/#view.rectangularSelection).
  7768. */
  7769. function crosshairCursor(options = {}) {
  7770. let [code, getter] = keys[options.key || "Alt"];
  7771. let plugin = ViewPlugin.fromClass(class {
  7772. constructor(view) {
  7773. this.view = view;
  7774. this.isDown = false;
  7775. }
  7776. set(isDown) {
  7777. if (this.isDown != isDown) {
  7778. this.isDown = isDown;
  7779. this.view.update([]);
  7780. }
  7781. }
  7782. }, {
  7783. eventHandlers: {
  7784. keydown(e) {
  7785. this.set(e.keyCode == code || getter(e));
  7786. },
  7787. keyup(e) {
  7788. if (e.keyCode == code || !getter(e))
  7789. this.set(false);
  7790. }
  7791. }
  7792. });
  7793. return [
  7794. plugin,
  7795. EditorView.contentAttributes.of(view => { var _a; return ((_a = view.plugin(plugin)) === null || _a === void 0 ? void 0 : _a.isDown) ? showCrosshair : null; })
  7796. ];
  7797. }
  7798. const Outside = "-10000px";
  7799. class TooltipViewManager {
  7800. constructor(view, facet, createTooltipView) {
  7801. this.facet = facet;
  7802. this.createTooltipView = createTooltipView;
  7803. this.input = view.state.facet(facet);
  7804. this.tooltips = this.input.filter(t => t);
  7805. this.tooltipViews = this.tooltips.map(createTooltipView);
  7806. }
  7807. update(update) {
  7808. let input = update.state.facet(this.facet);
  7809. let tooltips = input.filter(x => x);
  7810. if (input === this.input) {
  7811. for (let t of this.tooltipViews)
  7812. if (t.update)
  7813. t.update(update);
  7814. return false;
  7815. }
  7816. let tooltipViews = [];
  7817. for (let i = 0; i < tooltips.length; i++) {
  7818. let tip = tooltips[i], known = -1;
  7819. if (!tip)
  7820. continue;
  7821. for (let i = 0; i < this.tooltips.length; i++) {
  7822. let other = this.tooltips[i];
  7823. if (other && other.create == tip.create)
  7824. known = i;
  7825. }
  7826. if (known < 0) {
  7827. tooltipViews[i] = this.createTooltipView(tip);
  7828. }
  7829. else {
  7830. let tooltipView = tooltipViews[i] = this.tooltipViews[known];
  7831. if (tooltipView.update)
  7832. tooltipView.update(update);
  7833. }
  7834. }
  7835. for (let t of this.tooltipViews)
  7836. if (tooltipViews.indexOf(t) < 0)
  7837. t.dom.remove();
  7838. this.input = input;
  7839. this.tooltips = tooltips;
  7840. this.tooltipViews = tooltipViews;
  7841. return true;
  7842. }
  7843. }
  7844. /**
  7845. Creates an extension that configures tooltip behavior.
  7846. */
  7847. function tooltips(config = {}) {
  7848. return tooltipConfig.of(config);
  7849. }
  7850. function windowSpace() {
  7851. return { top: 0, left: 0, bottom: innerHeight, right: innerWidth };
  7852. }
  7853. const tooltipConfig = /*@__PURE__*/Facet.define({
  7854. combine: values => {
  7855. var _a, _b, _c;
  7856. return ({
  7857. position: browser.ios ? "absolute" : ((_a = values.find(conf => conf.position)) === null || _a === void 0 ? void 0 : _a.position) || "fixed",
  7858. parent: ((_b = values.find(conf => conf.parent)) === null || _b === void 0 ? void 0 : _b.parent) || null,
  7859. tooltipSpace: ((_c = values.find(conf => conf.tooltipSpace)) === null || _c === void 0 ? void 0 : _c.tooltipSpace) || windowSpace,
  7860. });
  7861. }
  7862. });
  7863. const tooltipPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
  7864. constructor(view) {
  7865. var _a;
  7866. this.view = view;
  7867. this.inView = true;
  7868. this.lastTransaction = 0;
  7869. this.measureTimeout = -1;
  7870. let config = view.state.facet(tooltipConfig);
  7871. this.position = config.position;
  7872. this.parent = config.parent;
  7873. this.classes = view.themeClasses;
  7874. this.createContainer();
  7875. this.measureReq = { read: this.readMeasure.bind(this), write: this.writeMeasure.bind(this), key: this };
  7876. this.manager = new TooltipViewManager(view, showTooltip, t => this.createTooltip(t));
  7877. this.intersectionObserver = typeof IntersectionObserver == "function" ? new IntersectionObserver(entries => {
  7878. if (Date.now() > this.lastTransaction - 50 &&
  7879. entries.length > 0 && entries[entries.length - 1].intersectionRatio < 1)
  7880. this.measureSoon();
  7881. }, { threshold: [1] }) : null;
  7882. this.observeIntersection();
  7883. (_a = view.dom.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.addEventListener("resize", this.measureSoon = this.measureSoon.bind(this));
  7884. this.maybeMeasure();
  7885. }
  7886. createContainer() {
  7887. if (this.parent) {
  7888. this.container = document.createElement("div");
  7889. this.container.style.position = "relative";
  7890. this.container.className = this.view.themeClasses;
  7891. this.parent.appendChild(this.container);
  7892. }
  7893. else {
  7894. this.container = this.view.dom;
  7895. }
  7896. }
  7897. observeIntersection() {
  7898. if (this.intersectionObserver) {
  7899. this.intersectionObserver.disconnect();
  7900. for (let tooltip of this.manager.tooltipViews)
  7901. this.intersectionObserver.observe(tooltip.dom);
  7902. }
  7903. }
  7904. measureSoon() {
  7905. if (this.measureTimeout < 0)
  7906. this.measureTimeout = setTimeout(() => {
  7907. this.measureTimeout = -1;
  7908. this.maybeMeasure();
  7909. }, 50);
  7910. }
  7911. update(update) {
  7912. if (update.transactions.length)
  7913. this.lastTransaction = Date.now();
  7914. let updated = this.manager.update(update);
  7915. if (updated)
  7916. this.observeIntersection();
  7917. let shouldMeasure = updated || update.geometryChanged;
  7918. let newConfig = update.state.facet(tooltipConfig);
  7919. if (newConfig.position != this.position) {
  7920. this.position = newConfig.position;
  7921. for (let t of this.manager.tooltipViews)
  7922. t.dom.style.position = this.position;
  7923. shouldMeasure = true;
  7924. }
  7925. if (newConfig.parent != this.parent) {
  7926. if (this.parent)
  7927. this.container.remove();
  7928. this.parent = newConfig.parent;
  7929. this.createContainer();
  7930. for (let t of this.manager.tooltipViews)
  7931. this.container.appendChild(t.dom);
  7932. shouldMeasure = true;
  7933. }
  7934. else if (this.parent && this.view.themeClasses != this.classes) {
  7935. this.classes = this.container.className = this.view.themeClasses;
  7936. }
  7937. if (shouldMeasure)
  7938. this.maybeMeasure();
  7939. }
  7940. createTooltip(tooltip) {
  7941. let tooltipView = tooltip.create(this.view);
  7942. tooltipView.dom.classList.add("cm-tooltip");
  7943. if (tooltip.arrow && !tooltipView.dom.querySelector(".cm-tooltip > .cm-tooltip-arrow")) {
  7944. let arrow = document.createElement("div");
  7945. arrow.className = "cm-tooltip-arrow";
  7946. tooltipView.dom.appendChild(arrow);
  7947. }
  7948. tooltipView.dom.style.position = this.position;
  7949. tooltipView.dom.style.top = Outside;
  7950. this.container.appendChild(tooltipView.dom);
  7951. if (tooltipView.mount)
  7952. tooltipView.mount(this.view);
  7953. return tooltipView;
  7954. }
  7955. destroy() {
  7956. var _a, _b;
  7957. (_a = this.view.dom.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.removeEventListener("resize", this.measureSoon);
  7958. for (let { dom } of this.manager.tooltipViews)
  7959. dom.remove();
  7960. (_b = this.intersectionObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
  7961. clearTimeout(this.measureTimeout);
  7962. }
  7963. readMeasure() {
  7964. let editor = this.view.dom.getBoundingClientRect();
  7965. return {
  7966. editor,
  7967. parent: this.parent ? this.container.getBoundingClientRect() : editor,
  7968. pos: this.manager.tooltips.map((t, i) => {
  7969. let tv = this.manager.tooltipViews[i];
  7970. return tv.getCoords ? tv.getCoords(t.pos) : this.view.coordsAtPos(t.pos);
  7971. }),
  7972. size: this.manager.tooltipViews.map(({ dom }) => dom.getBoundingClientRect()),
  7973. space: this.view.state.facet(tooltipConfig).tooltipSpace(this.view),
  7974. };
  7975. }
  7976. writeMeasure(measured) {
  7977. let { editor, space } = measured;
  7978. let others = [];
  7979. for (let i = 0; i < this.manager.tooltips.length; i++) {
  7980. let tooltip = this.manager.tooltips[i], tView = this.manager.tooltipViews[i], { dom } = tView;
  7981. let pos = measured.pos[i], size = measured.size[i];
  7982. // Hide tooltips that are outside of the editor.
  7983. if (!pos || pos.bottom <= Math.max(editor.top, space.top) ||
  7984. pos.top >= Math.min(editor.bottom, space.bottom) ||
  7985. pos.right < Math.max(editor.left, space.left) - .1 ||
  7986. pos.left > Math.min(editor.right, space.right) + .1) {
  7987. dom.style.top = Outside;
  7988. continue;
  7989. }
  7990. let arrow = tooltip.arrow ? tView.dom.querySelector(".cm-tooltip-arrow") : null;
  7991. let arrowHeight = arrow ? 7 /* Size */ : 0;
  7992. let width = size.right - size.left, height = size.bottom - size.top;
  7993. let offset = tView.offset || noOffset, ltr = this.view.textDirection == Direction.LTR;
  7994. let left = size.width > space.right - space.left ? (ltr ? space.left : space.right - size.width)
  7995. : ltr ? Math.min(pos.left - (arrow ? 14 /* Offset */ : 0) + offset.x, space.right - width)
  7996. : Math.max(space.left, pos.left - width + (arrow ? 14 /* Offset */ : 0) - offset.x);
  7997. let above = !!tooltip.above;
  7998. if (!tooltip.strictSide && (above
  7999. ? pos.top - (size.bottom - size.top) - offset.y < space.top
  8000. : pos.bottom + (size.bottom - size.top) + offset.y > space.bottom) &&
  8001. above == (space.bottom - pos.bottom > pos.top - space.top))
  8002. above = !above;
  8003. let top = above ? pos.top - height - arrowHeight - offset.y : pos.bottom + arrowHeight + offset.y;
  8004. let right = left + width;
  8005. if (tView.overlap !== true)
  8006. for (let r of others)
  8007. if (r.left < right && r.right > left && r.top < top + height && r.bottom > top)
  8008. top = above ? r.top - height - 2 - arrowHeight : r.bottom + arrowHeight + 2;
  8009. if (this.position == "absolute") {
  8010. dom.style.top = (top - measured.parent.top) + "px";
  8011. dom.style.left = (left - measured.parent.left) + "px";
  8012. }
  8013. else {
  8014. dom.style.top = top + "px";
  8015. dom.style.left = left + "px";
  8016. }
  8017. if (arrow)
  8018. arrow.style.left = `${pos.left + (ltr ? offset.x : -offset.x) - (left + 14 /* Offset */ - 7 /* Size */)}px`;
  8019. if (tView.overlap !== true)
  8020. others.push({ left, top, right, bottom: top + height });
  8021. dom.classList.toggle("cm-tooltip-above", above);
  8022. dom.classList.toggle("cm-tooltip-below", !above);
  8023. if (tView.positioned)
  8024. tView.positioned();
  8025. }
  8026. }
  8027. maybeMeasure() {
  8028. if (this.manager.tooltips.length) {
  8029. if (this.view.inView)
  8030. this.view.requestMeasure(this.measureReq);
  8031. if (this.inView != this.view.inView) {
  8032. this.inView = this.view.inView;
  8033. if (!this.inView)
  8034. for (let tv of this.manager.tooltipViews)
  8035. tv.dom.style.top = Outside;
  8036. }
  8037. }
  8038. }
  8039. }, {
  8040. eventHandlers: {
  8041. scroll() { this.maybeMeasure(); }
  8042. }
  8043. });
  8044. const baseTheme = /*@__PURE__*/EditorView.baseTheme({
  8045. ".cm-tooltip": {
  8046. zIndex: 100
  8047. },
  8048. "&light .cm-tooltip": {
  8049. border: "1px solid #bbb",
  8050. backgroundColor: "#f5f5f5"
  8051. },
  8052. "&light .cm-tooltip-section:not(:first-child)": {
  8053. borderTop: "1px solid #bbb",
  8054. },
  8055. "&dark .cm-tooltip": {
  8056. backgroundColor: "#333338",
  8057. color: "white"
  8058. },
  8059. ".cm-tooltip-arrow": {
  8060. height: `${7 /* Size */}px`,
  8061. width: `${7 /* Size */ * 2}px`,
  8062. position: "absolute",
  8063. zIndex: -1,
  8064. overflow: "hidden",
  8065. "&:before, &:after": {
  8066. content: "''",
  8067. position: "absolute",
  8068. width: 0,
  8069. height: 0,
  8070. borderLeft: `${7 /* Size */}px solid transparent`,
  8071. borderRight: `${7 /* Size */}px solid transparent`,
  8072. },
  8073. ".cm-tooltip-above &": {
  8074. bottom: `-${7 /* Size */}px`,
  8075. "&:before": {
  8076. borderTop: `${7 /* Size */}px solid #bbb`,
  8077. },
  8078. "&:after": {
  8079. borderTop: `${7 /* Size */}px solid #f5f5f5`,
  8080. bottom: "1px"
  8081. }
  8082. },
  8083. ".cm-tooltip-below &": {
  8084. top: `-${7 /* Size */}px`,
  8085. "&:before": {
  8086. borderBottom: `${7 /* Size */}px solid #bbb`,
  8087. },
  8088. "&:after": {
  8089. borderBottom: `${7 /* Size */}px solid #f5f5f5`,
  8090. top: "1px"
  8091. }
  8092. },
  8093. },
  8094. "&dark .cm-tooltip .cm-tooltip-arrow": {
  8095. "&:before": {
  8096. borderTopColor: "#333338",
  8097. borderBottomColor: "#333338"
  8098. },
  8099. "&:after": {
  8100. borderTopColor: "transparent",
  8101. borderBottomColor: "transparent"
  8102. }
  8103. }
  8104. });
  8105. const noOffset = { x: 0, y: 0 };
  8106. /**
  8107. Facet to which an extension can add a value to show a tooltip.
  8108. */
  8109. const showTooltip = /*@__PURE__*/Facet.define({
  8110. enables: [tooltipPlugin, baseTheme]
  8111. });
  8112. const showHoverTooltip = /*@__PURE__*/Facet.define();
  8113. class HoverTooltipHost {
  8114. constructor(view) {
  8115. this.view = view;
  8116. this.mounted = false;
  8117. this.dom = document.createElement("div");
  8118. this.dom.classList.add("cm-tooltip-hover");
  8119. this.manager = new TooltipViewManager(view, showHoverTooltip, t => this.createHostedView(t));
  8120. }
  8121. // Needs to be static so that host tooltip instances always match
  8122. static create(view) {
  8123. return new HoverTooltipHost(view);
  8124. }
  8125. createHostedView(tooltip) {
  8126. let hostedView = tooltip.create(this.view);
  8127. hostedView.dom.classList.add("cm-tooltip-section");
  8128. this.dom.appendChild(hostedView.dom);
  8129. if (this.mounted && hostedView.mount)
  8130. hostedView.mount(this.view);
  8131. return hostedView;
  8132. }
  8133. mount(view) {
  8134. for (let hostedView of this.manager.tooltipViews) {
  8135. if (hostedView.mount)
  8136. hostedView.mount(view);
  8137. }
  8138. this.mounted = true;
  8139. }
  8140. positioned() {
  8141. for (let hostedView of this.manager.tooltipViews) {
  8142. if (hostedView.positioned)
  8143. hostedView.positioned();
  8144. }
  8145. }
  8146. update(update) {
  8147. this.manager.update(update);
  8148. }
  8149. }
  8150. const showHoverTooltipHost = /*@__PURE__*/showTooltip.compute([showHoverTooltip], state => {
  8151. let tooltips = state.facet(showHoverTooltip).filter(t => t);
  8152. if (tooltips.length === 0)
  8153. return null;
  8154. return {
  8155. pos: Math.min(...tooltips.map(t => t.pos)),
  8156. end: Math.max(...tooltips.filter(t => t.end != null).map(t => t.end)),
  8157. create: HoverTooltipHost.create,
  8158. above: tooltips[0].above,
  8159. arrow: tooltips.some(t => t.arrow),
  8160. };
  8161. });
  8162. class HoverPlugin {
  8163. constructor(view, source, field, setHover, hoverTime) {
  8164. this.view = view;
  8165. this.source = source;
  8166. this.field = field;
  8167. this.setHover = setHover;
  8168. this.hoverTime = hoverTime;
  8169. this.hoverTimeout = -1;
  8170. this.restartTimeout = -1;
  8171. this.pending = null;
  8172. this.lastMove = { x: 0, y: 0, target: view.dom, time: 0 };
  8173. this.checkHover = this.checkHover.bind(this);
  8174. view.dom.addEventListener("mouseleave", this.mouseleave = this.mouseleave.bind(this));
  8175. view.dom.addEventListener("mousemove", this.mousemove = this.mousemove.bind(this));
  8176. }
  8177. update() {
  8178. if (this.pending) {
  8179. this.pending = null;
  8180. clearTimeout(this.restartTimeout);
  8181. this.restartTimeout = setTimeout(() => this.startHover(), 20);
  8182. }
  8183. }
  8184. get active() {
  8185. return this.view.state.field(this.field);
  8186. }
  8187. checkHover() {
  8188. this.hoverTimeout = -1;
  8189. if (this.active)
  8190. return;
  8191. let hovered = Date.now() - this.lastMove.time;
  8192. if (hovered < this.hoverTime)
  8193. this.hoverTimeout = setTimeout(this.checkHover, this.hoverTime - hovered);
  8194. else
  8195. this.startHover();
  8196. }
  8197. startHover() {
  8198. clearTimeout(this.restartTimeout);
  8199. let { lastMove } = this;
  8200. let pos = this.view.contentDOM.contains(lastMove.target) ? this.view.posAtCoords(lastMove) : null;
  8201. if (pos == null)
  8202. return;
  8203. let posCoords = this.view.coordsAtPos(pos);
  8204. if (posCoords == null || lastMove.y < posCoords.top || lastMove.y > posCoords.bottom ||
  8205. lastMove.x < posCoords.left - this.view.defaultCharacterWidth ||
  8206. lastMove.x > posCoords.right + this.view.defaultCharacterWidth)
  8207. return;
  8208. let bidi = this.view.bidiSpans(this.view.state.doc.lineAt(pos)).find(s => s.from <= pos && s.to >= pos);
  8209. let rtl = bidi && bidi.dir == Direction.RTL ? -1 : 1;
  8210. let open = this.source(this.view, pos, (lastMove.x < posCoords.left ? -rtl : rtl));
  8211. if (open === null || open === void 0 ? void 0 : open.then) {
  8212. let pending = this.pending = { pos };
  8213. open.then(result => {
  8214. if (this.pending == pending) {
  8215. this.pending = null;
  8216. if (result)
  8217. this.view.dispatch({ effects: this.setHover.of(result) });
  8218. }
  8219. }, e => logException(this.view.state, e, "hover tooltip"));
  8220. }
  8221. else if (open) {
  8222. this.view.dispatch({ effects: this.setHover.of(open) });
  8223. }
  8224. }
  8225. mousemove(event) {
  8226. var _a;
  8227. this.lastMove = { x: event.clientX, y: event.clientY, target: event.target, time: Date.now() };
  8228. if (this.hoverTimeout < 0)
  8229. this.hoverTimeout = setTimeout(this.checkHover, this.hoverTime);
  8230. let tooltip = this.active;
  8231. if (tooltip && !isInTooltip(this.lastMove.target) || this.pending) {
  8232. let { pos } = tooltip || this.pending, end = (_a = tooltip === null || tooltip === void 0 ? void 0 : tooltip.end) !== null && _a !== void 0 ? _a : pos;
  8233. if ((pos == end ? this.view.posAtCoords(this.lastMove) != pos
  8234. : !isOverRange(this.view, pos, end, event.clientX, event.clientY, 6 /* MaxDist */))) {
  8235. this.view.dispatch({ effects: this.setHover.of(null) });
  8236. this.pending = null;
  8237. }
  8238. }
  8239. }
  8240. mouseleave() {
  8241. clearTimeout(this.hoverTimeout);
  8242. this.hoverTimeout = -1;
  8243. if (this.active)
  8244. this.view.dispatch({ effects: this.setHover.of(null) });
  8245. }
  8246. destroy() {
  8247. clearTimeout(this.hoverTimeout);
  8248. this.view.dom.removeEventListener("mouseleave", this.mouseleave);
  8249. this.view.dom.removeEventListener("mousemove", this.mousemove);
  8250. }
  8251. }
  8252. function isInTooltip(elt) {
  8253. for (let cur = elt; cur; cur = cur.parentNode)
  8254. if (cur.nodeType == 1 && cur.classList.contains("cm-tooltip"))
  8255. return true;
  8256. return false;
  8257. }
  8258. function isOverRange(view, from, to, x, y, margin) {
  8259. let range = document.createRange();
  8260. let fromDOM = view.domAtPos(from), toDOM = view.domAtPos(to);
  8261. range.setEnd(toDOM.node, toDOM.offset);
  8262. range.setStart(fromDOM.node, fromDOM.offset);
  8263. let rects = range.getClientRects();
  8264. range.detach();
  8265. for (let i = 0; i < rects.length; i++) {
  8266. let rect = rects[i];
  8267. let dist = Math.max(rect.top - y, y - rect.bottom, rect.left - x, x - rect.right);
  8268. if (dist <= margin)
  8269. return true;
  8270. }
  8271. return false;
  8272. }
  8273. /**
  8274. Set up a hover tooltip, which shows up when the pointer hovers
  8275. over ranges of text. The callback is called when the mouse hovers
  8276. over the document text. It should, if there is a tooltip
  8277. associated with position `pos`, return the tooltip description
  8278. (either directly or in a promise). The `side` argument indicates
  8279. on which side of the position the pointer is—it will be -1 if the
  8280. pointer is before the position, 1 if after the position.
  8281. Note that all hover tooltips are hosted within a single tooltip
  8282. container element. This allows multiple tooltips over the same
  8283. range to be "merged" together without overlapping.
  8284. */
  8285. function hoverTooltip(source, options = {}) {
  8286. let setHover = StateEffect.define();
  8287. let hoverState = StateField.define({
  8288. create() { return null; },
  8289. update(value, tr) {
  8290. if (value && (options.hideOnChange && (tr.docChanged || tr.selection) ||
  8291. options.hideOn && options.hideOn(tr, value)))
  8292. return null;
  8293. if (value && tr.docChanged) {
  8294. let newPos = tr.changes.mapPos(value.pos, -1, MapMode.TrackDel);
  8295. if (newPos == null)
  8296. return null;
  8297. let copy = Object.assign(Object.create(null), value);
  8298. copy.pos = newPos;
  8299. if (value.end != null)
  8300. copy.end = tr.changes.mapPos(value.end);
  8301. value = copy;
  8302. }
  8303. for (let effect of tr.effects) {
  8304. if (effect.is(setHover))
  8305. value = effect.value;
  8306. if (effect.is(closeHoverTooltipEffect))
  8307. value = null;
  8308. }
  8309. return value;
  8310. },
  8311. provide: f => showHoverTooltip.from(f)
  8312. });
  8313. return [
  8314. hoverState,
  8315. ViewPlugin.define(view => new HoverPlugin(view, source, hoverState, setHover, options.hoverTime || 300 /* Time */)),
  8316. showHoverTooltipHost
  8317. ];
  8318. }
  8319. /**
  8320. Get the active tooltip view for a given tooltip, if available.
  8321. */
  8322. function getTooltip(view, tooltip) {
  8323. let plugin = view.plugin(tooltipPlugin);
  8324. if (!plugin)
  8325. return null;
  8326. let found = plugin.manager.tooltips.indexOf(tooltip);
  8327. return found < 0 ? null : plugin.manager.tooltipViews[found];
  8328. }
  8329. /**
  8330. Returns true if any hover tooltips are currently active.
  8331. */
  8332. function hasHoverTooltips(state) {
  8333. return state.facet(showHoverTooltip).some(x => x);
  8334. }
  8335. const closeHoverTooltipEffect = /*@__PURE__*/StateEffect.define();
  8336. /**
  8337. Transaction effect that closes all hover tooltips.
  8338. */
  8339. const closeHoverTooltips = /*@__PURE__*/closeHoverTooltipEffect.of(null);
  8340. /**
  8341. Tell the tooltip extension to recompute the position of the active
  8342. tooltips. This can be useful when something happens (such as a
  8343. re-positioning or CSS change affecting the editor) that could
  8344. invalidate the existing tooltip positions.
  8345. */
  8346. function repositionTooltips(view) {
  8347. var _a;
  8348. (_a = view.plugin(tooltipPlugin)) === null || _a === void 0 ? void 0 : _a.maybeMeasure();
  8349. }
  8350. const panelConfig = /*@__PURE__*/Facet.define({
  8351. combine(configs) {
  8352. let topContainer, bottomContainer;
  8353. for (let c of configs) {
  8354. topContainer = topContainer || c.topContainer;
  8355. bottomContainer = bottomContainer || c.bottomContainer;
  8356. }
  8357. return { topContainer, bottomContainer };
  8358. }
  8359. });
  8360. /**
  8361. Configures the panel-managing extension.
  8362. */
  8363. function panels(config) {
  8364. return config ? [panelConfig.of(config)] : [];
  8365. }
  8366. /**
  8367. Get the active panel created by the given constructor, if any.
  8368. This can be useful when you need access to your panels' DOM
  8369. structure.
  8370. */
  8371. function getPanel(view, panel) {
  8372. let plugin = view.plugin(panelPlugin);
  8373. let index = plugin ? plugin.specs.indexOf(panel) : -1;
  8374. return index > -1 ? plugin.panels[index] : null;
  8375. }
  8376. const panelPlugin = /*@__PURE__*/ViewPlugin.fromClass(class {
  8377. constructor(view) {
  8378. this.input = view.state.facet(showPanel);
  8379. this.specs = this.input.filter(s => s);
  8380. this.panels = this.specs.map(spec => spec(view));
  8381. let conf = view.state.facet(panelConfig);
  8382. this.top = new PanelGroup(view, true, conf.topContainer);
  8383. this.bottom = new PanelGroup(view, false, conf.bottomContainer);
  8384. this.top.sync(this.panels.filter(p => p.top));
  8385. this.bottom.sync(this.panels.filter(p => !p.top));
  8386. for (let p of this.panels) {
  8387. p.dom.classList.add("cm-panel");
  8388. if (p.mount)
  8389. p.mount();
  8390. }
  8391. }
  8392. update(update) {
  8393. let conf = update.state.facet(panelConfig);
  8394. if (this.top.container != conf.topContainer) {
  8395. this.top.sync([]);
  8396. this.top = new PanelGroup(update.view, true, conf.topContainer);
  8397. }
  8398. if (this.bottom.container != conf.bottomContainer) {
  8399. this.bottom.sync([]);
  8400. this.bottom = new PanelGroup(update.view, false, conf.bottomContainer);
  8401. }
  8402. this.top.syncClasses();
  8403. this.bottom.syncClasses();
  8404. let input = update.state.facet(showPanel);
  8405. if (input != this.input) {
  8406. let specs = input.filter(x => x);
  8407. let panels = [], top = [], bottom = [], mount = [];
  8408. for (let spec of specs) {
  8409. let known = this.specs.indexOf(spec), panel;
  8410. if (known < 0) {
  8411. panel = spec(update.view);
  8412. mount.push(panel);
  8413. }
  8414. else {
  8415. panel = this.panels[known];
  8416. if (panel.update)
  8417. panel.update(update);
  8418. }
  8419. panels.push(panel);
  8420. (panel.top ? top : bottom).push(panel);
  8421. }
  8422. this.specs = specs;
  8423. this.panels = panels;
  8424. this.top.sync(top);
  8425. this.bottom.sync(bottom);
  8426. for (let p of mount) {
  8427. p.dom.classList.add("cm-panel");
  8428. if (p.mount)
  8429. p.mount();
  8430. }
  8431. }
  8432. else {
  8433. for (let p of this.panels)
  8434. if (p.update)
  8435. p.update(update);
  8436. }
  8437. }
  8438. destroy() {
  8439. this.top.sync([]);
  8440. this.bottom.sync([]);
  8441. }
  8442. }, {
  8443. provide: plugin => EditorView.scrollMargins.of(view => {
  8444. let value = view.plugin(plugin);
  8445. return value && { top: value.top.scrollMargin(), bottom: value.bottom.scrollMargin() };
  8446. })
  8447. });
  8448. class PanelGroup {
  8449. constructor(view, top, container) {
  8450. this.view = view;
  8451. this.top = top;
  8452. this.container = container;
  8453. this.dom = undefined;
  8454. this.classes = "";
  8455. this.panels = [];
  8456. this.syncClasses();
  8457. }
  8458. sync(panels) {
  8459. for (let p of this.panels)
  8460. if (p.destroy && panels.indexOf(p) < 0)
  8461. p.destroy();
  8462. this.panels = panels;
  8463. this.syncDOM();
  8464. }
  8465. syncDOM() {
  8466. if (this.panels.length == 0) {
  8467. if (this.dom) {
  8468. this.dom.remove();
  8469. this.dom = undefined;
  8470. }
  8471. return;
  8472. }
  8473. if (!this.dom) {
  8474. this.dom = document.createElement("div");
  8475. this.dom.className = this.top ? "cm-panels cm-panels-top" : "cm-panels cm-panels-bottom";
  8476. this.dom.style[this.top ? "top" : "bottom"] = "0";
  8477. let parent = this.container || this.view.dom;
  8478. parent.insertBefore(this.dom, this.top ? parent.firstChild : null);
  8479. }
  8480. let curDOM = this.dom.firstChild;
  8481. for (let panel of this.panels) {
  8482. if (panel.dom.parentNode == this.dom) {
  8483. while (curDOM != panel.dom)
  8484. curDOM = rm(curDOM);
  8485. curDOM = curDOM.nextSibling;
  8486. }
  8487. else {
  8488. this.dom.insertBefore(panel.dom, curDOM);
  8489. }
  8490. }
  8491. while (curDOM)
  8492. curDOM = rm(curDOM);
  8493. }
  8494. scrollMargin() {
  8495. return !this.dom || this.container ? 0
  8496. : Math.max(0, this.top ?
  8497. this.dom.getBoundingClientRect().bottom - Math.max(0, this.view.scrollDOM.getBoundingClientRect().top) :
  8498. Math.min(innerHeight, this.view.scrollDOM.getBoundingClientRect().bottom) - this.dom.getBoundingClientRect().top);
  8499. }
  8500. syncClasses() {
  8501. if (!this.container || this.classes == this.view.themeClasses)
  8502. return;
  8503. for (let cls of this.classes.split(" "))
  8504. if (cls)
  8505. this.container.classList.remove(cls);
  8506. for (let cls of (this.classes = this.view.themeClasses).split(" "))
  8507. if (cls)
  8508. this.container.classList.add(cls);
  8509. }
  8510. }
  8511. function rm(node) {
  8512. let next = node.nextSibling;
  8513. node.remove();
  8514. return next;
  8515. }
  8516. /**
  8517. Opening a panel is done by providing a constructor function for
  8518. the panel through this facet. (The panel is closed again when its
  8519. constructor is no longer provided.) Values of `null` are ignored.
  8520. */
  8521. const showPanel = /*@__PURE__*/Facet.define({
  8522. enables: panelPlugin
  8523. });
  8524. /**
  8525. A gutter marker represents a bit of information attached to a line
  8526. in a specific gutter. Your own custom markers have to extend this
  8527. class.
  8528. */
  8529. class GutterMarker extends RangeValue {
  8530. /**
  8531. @internal
  8532. */
  8533. compare(other) {
  8534. return this == other || this.constructor == other.constructor && this.eq(other);
  8535. }
  8536. /**
  8537. Compare this marker to another marker of the same type.
  8538. */
  8539. eq(other) { return false; }
  8540. /**
  8541. Called if the marker has a `toDOM` method and its representation
  8542. was removed from a gutter.
  8543. */
  8544. destroy(dom) { }
  8545. }
  8546. GutterMarker.prototype.elementClass = "";
  8547. GutterMarker.prototype.toDOM = undefined;
  8548. GutterMarker.prototype.mapMode = MapMode.TrackBefore;
  8549. GutterMarker.prototype.startSide = GutterMarker.prototype.endSide = -1;
  8550. GutterMarker.prototype.point = true;
  8551. /**
  8552. Facet used to add a class to all gutter elements for a given line.
  8553. Markers given to this facet should _only_ define an
  8554. [`elementclass`](https://codemirror.net/6/docs/ref/#view.GutterMarker.elementClass), not a
  8555. [`toDOM`](https://codemirror.net/6/docs/ref/#view.GutterMarker.toDOM) (or the marker will appear
  8556. in all gutters for the line).
  8557. */
  8558. const gutterLineClass = /*@__PURE__*/Facet.define();
  8559. const defaults = {
  8560. class: "",
  8561. renderEmptyElements: false,
  8562. elementStyle: "",
  8563. markers: () => RangeSet.empty,
  8564. lineMarker: () => null,
  8565. lineMarkerChange: null,
  8566. initialSpacer: null,
  8567. updateSpacer: null,
  8568. domEventHandlers: {}
  8569. };
  8570. const activeGutters = /*@__PURE__*/Facet.define();
  8571. /**
  8572. Define an editor gutter. The order in which the gutters appear is
  8573. determined by their extension priority.
  8574. */
  8575. function gutter(config) {
  8576. return [gutters(), activeGutters.of(Object.assign(Object.assign({}, defaults), config))];
  8577. }
  8578. const unfixGutters = /*@__PURE__*/Facet.define({
  8579. combine: values => values.some(x => x)
  8580. });
  8581. /**
  8582. The gutter-drawing plugin is automatically enabled when you add a
  8583. gutter, but you can use this function to explicitly configure it.
  8584. Unless `fixed` is explicitly set to `false`, the gutters are
  8585. fixed, meaning they don't scroll along with the content
  8586. horizontally (except on Internet Explorer, which doesn't support
  8587. CSS [`position:
  8588. sticky`](https://developer.mozilla.org/en-US/docs/Web/CSS/position#sticky)).
  8589. */
  8590. function gutters(config) {
  8591. let result = [
  8592. gutterView,
  8593. ];
  8594. if (config && config.fixed === false)
  8595. result.push(unfixGutters.of(true));
  8596. return result;
  8597. }
  8598. const gutterView = /*@__PURE__*/ViewPlugin.fromClass(class {
  8599. constructor(view) {
  8600. this.view = view;
  8601. this.prevViewport = view.viewport;
  8602. this.dom = document.createElement("div");
  8603. this.dom.className = "cm-gutters";
  8604. this.dom.setAttribute("aria-hidden", "true");
  8605. this.dom.style.minHeight = this.view.contentHeight + "px";
  8606. this.gutters = view.state.facet(activeGutters).map(conf => new SingleGutterView(view, conf));
  8607. for (let gutter of this.gutters)
  8608. this.dom.appendChild(gutter.dom);
  8609. this.fixed = !view.state.facet(unfixGutters);
  8610. if (this.fixed) {
  8611. // FIXME IE11 fallback, which doesn't support position: sticky,
  8612. // by using position: relative + event handlers that realign the
  8613. // gutter (or just force fixed=false on IE11?)
  8614. this.dom.style.position = "sticky";
  8615. }
  8616. this.syncGutters(false);
  8617. view.scrollDOM.insertBefore(this.dom, view.contentDOM);
  8618. }
  8619. update(update) {
  8620. if (this.updateGutters(update)) {
  8621. // Detach during sync when the viewport changed significantly
  8622. // (such as during scrolling), since for large updates that is
  8623. // faster.
  8624. let vpA = this.prevViewport, vpB = update.view.viewport;
  8625. let vpOverlap = Math.min(vpA.to, vpB.to) - Math.max(vpA.from, vpB.from);
  8626. this.syncGutters(vpOverlap < (vpB.to - vpB.from) * 0.8);
  8627. }
  8628. if (update.geometryChanged)
  8629. this.dom.style.minHeight = this.view.contentHeight + "px";
  8630. if (this.view.state.facet(unfixGutters) != !this.fixed) {
  8631. this.fixed = !this.fixed;
  8632. this.dom.style.position = this.fixed ? "sticky" : "";
  8633. }
  8634. this.prevViewport = update.view.viewport;
  8635. }
  8636. syncGutters(detach) {
  8637. let after = this.dom.nextSibling;
  8638. if (detach)
  8639. this.dom.remove();
  8640. let lineClasses = RangeSet.iter(this.view.state.facet(gutterLineClass), this.view.viewport.from);
  8641. let classSet = [];
  8642. let contexts = this.gutters.map(gutter => new UpdateContext(gutter, this.view.viewport, -this.view.documentPadding.top));
  8643. for (let line of this.view.viewportLineBlocks) {
  8644. let text;
  8645. if (Array.isArray(line.type)) {
  8646. for (let b of line.type)
  8647. if (b.type == BlockType.Text) {
  8648. text = b;
  8649. break;
  8650. }
  8651. }
  8652. else {
  8653. text = line.type == BlockType.Text ? line : undefined;
  8654. }
  8655. if (!text)
  8656. continue;
  8657. if (classSet.length)
  8658. classSet = [];
  8659. advanceCursor(lineClasses, classSet, line.from);
  8660. for (let cx of contexts)
  8661. cx.line(this.view, text, classSet);
  8662. }
  8663. for (let cx of contexts)
  8664. cx.finish();
  8665. if (detach)
  8666. this.view.scrollDOM.insertBefore(this.dom, after);
  8667. }
  8668. updateGutters(update) {
  8669. let prev = update.startState.facet(activeGutters), cur = update.state.facet(activeGutters);
  8670. let change = update.docChanged || update.heightChanged || update.viewportChanged ||
  8671. !RangeSet.eq(update.startState.facet(gutterLineClass), update.state.facet(gutterLineClass), update.view.viewport.from, update.view.viewport.to);
  8672. if (prev == cur) {
  8673. for (let gutter of this.gutters)
  8674. if (gutter.update(update))
  8675. change = true;
  8676. }
  8677. else {
  8678. change = true;
  8679. let gutters = [];
  8680. for (let conf of cur) {
  8681. let known = prev.indexOf(conf);
  8682. if (known < 0) {
  8683. gutters.push(new SingleGutterView(this.view, conf));
  8684. }
  8685. else {
  8686. this.gutters[known].update(update);
  8687. gutters.push(this.gutters[known]);
  8688. }
  8689. }
  8690. for (let g of this.gutters) {
  8691. g.dom.remove();
  8692. if (gutters.indexOf(g) < 0)
  8693. g.destroy();
  8694. }
  8695. for (let g of gutters)
  8696. this.dom.appendChild(g.dom);
  8697. this.gutters = gutters;
  8698. }
  8699. return change;
  8700. }
  8701. destroy() {
  8702. for (let view of this.gutters)
  8703. view.destroy();
  8704. this.dom.remove();
  8705. }
  8706. }, {
  8707. provide: plugin => EditorView.scrollMargins.of(view => {
  8708. let value = view.plugin(plugin);
  8709. if (!value || value.gutters.length == 0 || !value.fixed)
  8710. return null;
  8711. return view.textDirection == Direction.LTR ? { left: value.dom.offsetWidth } : { right: value.dom.offsetWidth };
  8712. })
  8713. });
  8714. function asArray(val) { return (Array.isArray(val) ? val : [val]); }
  8715. function advanceCursor(cursor, collect, pos) {
  8716. while (cursor.value && cursor.from <= pos) {
  8717. if (cursor.from == pos)
  8718. collect.push(cursor.value);
  8719. cursor.next();
  8720. }
  8721. }
  8722. class UpdateContext {
  8723. constructor(gutter, viewport, height) {
  8724. this.gutter = gutter;
  8725. this.height = height;
  8726. this.localMarkers = [];
  8727. this.i = 0;
  8728. this.cursor = RangeSet.iter(gutter.markers, viewport.from);
  8729. }
  8730. line(view, line, extraMarkers) {
  8731. if (this.localMarkers.length)
  8732. this.localMarkers = [];
  8733. advanceCursor(this.cursor, this.localMarkers, line.from);
  8734. let localMarkers = extraMarkers.length ? this.localMarkers.concat(extraMarkers) : this.localMarkers;
  8735. let forLine = this.gutter.config.lineMarker(view, line, localMarkers);
  8736. if (forLine)
  8737. localMarkers.unshift(forLine);
  8738. let gutter = this.gutter;
  8739. if (localMarkers.length == 0 && !gutter.config.renderEmptyElements)
  8740. return;
  8741. let above = line.top - this.height;
  8742. if (this.i == gutter.elements.length) {
  8743. let newElt = new GutterElement(view, line.height, above, localMarkers);
  8744. gutter.elements.push(newElt);
  8745. gutter.dom.appendChild(newElt.dom);
  8746. }
  8747. else {
  8748. gutter.elements[this.i].update(view, line.height, above, localMarkers);
  8749. }
  8750. this.height = line.bottom;
  8751. this.i++;
  8752. }
  8753. finish() {
  8754. let gutter = this.gutter;
  8755. while (gutter.elements.length > this.i) {
  8756. let last = gutter.elements.pop();
  8757. gutter.dom.removeChild(last.dom);
  8758. last.destroy();
  8759. }
  8760. }
  8761. }
  8762. class SingleGutterView {
  8763. constructor(view, config) {
  8764. this.view = view;
  8765. this.config = config;
  8766. this.elements = [];
  8767. this.spacer = null;
  8768. this.dom = document.createElement("div");
  8769. this.dom.className = "cm-gutter" + (this.config.class ? " " + this.config.class : "");
  8770. for (let prop in config.domEventHandlers) {
  8771. this.dom.addEventListener(prop, (event) => {
  8772. let line = view.lineBlockAtHeight(event.clientY - view.documentTop);
  8773. if (config.domEventHandlers[prop](view, line, event))
  8774. event.preventDefault();
  8775. });
  8776. }
  8777. this.markers = asArray(config.markers(view));
  8778. if (config.initialSpacer) {
  8779. this.spacer = new GutterElement(view, 0, 0, [config.initialSpacer(view)]);
  8780. this.dom.appendChild(this.spacer.dom);
  8781. this.spacer.dom.style.cssText += "visibility: hidden; pointer-events: none";
  8782. }
  8783. }
  8784. update(update) {
  8785. let prevMarkers = this.markers;
  8786. this.markers = asArray(this.config.markers(update.view));
  8787. if (this.spacer && this.config.updateSpacer) {
  8788. let updated = this.config.updateSpacer(this.spacer.markers[0], update);
  8789. if (updated != this.spacer.markers[0])
  8790. this.spacer.update(update.view, 0, 0, [updated]);
  8791. }
  8792. let vp = update.view.viewport;
  8793. return !RangeSet.eq(this.markers, prevMarkers, vp.from, vp.to) ||
  8794. (this.config.lineMarkerChange ? this.config.lineMarkerChange(update) : false);
  8795. }
  8796. destroy() {
  8797. for (let elt of this.elements)
  8798. elt.destroy();
  8799. }
  8800. }
  8801. class GutterElement {
  8802. constructor(view, height, above, markers) {
  8803. this.height = -1;
  8804. this.above = 0;
  8805. this.markers = [];
  8806. this.dom = document.createElement("div");
  8807. this.dom.className = "cm-gutterElement";
  8808. this.update(view, height, above, markers);
  8809. }
  8810. update(view, height, above, markers) {
  8811. if (this.height != height)
  8812. this.dom.style.height = (this.height = height) + "px";
  8813. if (this.above != above)
  8814. this.dom.style.marginTop = (this.above = above) ? above + "px" : "";
  8815. if (!sameMarkers(this.markers, markers))
  8816. this.setMarkers(view, markers);
  8817. }
  8818. setMarkers(view, markers) {
  8819. let cls = "cm-gutterElement", domPos = this.dom.firstChild;
  8820. for (let iNew = 0, iOld = 0;;) {
  8821. let skipTo = iOld, marker = iNew < markers.length ? markers[iNew++] : null, matched = false;
  8822. if (marker) {
  8823. let c = marker.elementClass;
  8824. if (c)
  8825. cls += " " + c;
  8826. for (let i = iOld; i < this.markers.length; i++)
  8827. if (this.markers[i].compare(marker)) {
  8828. skipTo = i;
  8829. matched = true;
  8830. break;
  8831. }
  8832. }
  8833. else {
  8834. skipTo = this.markers.length;
  8835. }
  8836. while (iOld < skipTo) {
  8837. let next = this.markers[iOld++];
  8838. if (next.toDOM) {
  8839. next.destroy(domPos);
  8840. let after = domPos.nextSibling;
  8841. domPos.remove();
  8842. domPos = after;
  8843. }
  8844. }
  8845. if (!marker)
  8846. break;
  8847. if (marker.toDOM) {
  8848. if (matched)
  8849. domPos = domPos.nextSibling;
  8850. else
  8851. this.dom.insertBefore(marker.toDOM(view), domPos);
  8852. }
  8853. if (matched)
  8854. iOld++;
  8855. }
  8856. this.dom.className = cls;
  8857. this.markers = markers;
  8858. }
  8859. destroy() {
  8860. this.setMarkers(null, []); // First argument not used unless creating markers
  8861. }
  8862. }
  8863. function sameMarkers(a, b) {
  8864. if (a.length != b.length)
  8865. return false;
  8866. for (let i = 0; i < a.length; i++)
  8867. if (!a[i].compare(b[i]))
  8868. return false;
  8869. return true;
  8870. }
  8871. /**
  8872. Facet used to provide markers to the line number gutter.
  8873. */
  8874. const lineNumberMarkers = /*@__PURE__*/Facet.define();
  8875. const lineNumberConfig = /*@__PURE__*/Facet.define({
  8876. combine(values) {
  8877. return combineConfig(values, { formatNumber: String, domEventHandlers: {} }, {
  8878. domEventHandlers(a, b) {
  8879. let result = Object.assign({}, a);
  8880. for (let event in b) {
  8881. let exists = result[event], add = b[event];
  8882. result[event] = exists ? (view, line, event) => exists(view, line, event) || add(view, line, event) : add;
  8883. }
  8884. return result;
  8885. }
  8886. });
  8887. }
  8888. });
  8889. class NumberMarker extends GutterMarker {
  8890. constructor(number) {
  8891. super();
  8892. this.number = number;
  8893. }
  8894. eq(other) { return this.number == other.number; }
  8895. toDOM() { return document.createTextNode(this.number); }
  8896. }
  8897. function formatNumber(view, number) {
  8898. return view.state.facet(lineNumberConfig).formatNumber(number, view.state);
  8899. }
  8900. const lineNumberGutter = /*@__PURE__*/activeGutters.compute([lineNumberConfig], state => ({
  8901. class: "cm-lineNumbers",
  8902. renderEmptyElements: false,
  8903. markers(view) { return view.state.facet(lineNumberMarkers); },
  8904. lineMarker(view, line, others) {
  8905. if (others.some(m => m.toDOM))
  8906. return null;
  8907. return new NumberMarker(formatNumber(view, view.state.doc.lineAt(line.from).number));
  8908. },
  8909. lineMarkerChange: update => update.startState.facet(lineNumberConfig) != update.state.facet(lineNumberConfig),
  8910. initialSpacer(view) {
  8911. return new NumberMarker(formatNumber(view, maxLineNumber(view.state.doc.lines)));
  8912. },
  8913. updateSpacer(spacer, update) {
  8914. let max = formatNumber(update.view, maxLineNumber(update.view.state.doc.lines));
  8915. return max == spacer.number ? spacer : new NumberMarker(max);
  8916. },
  8917. domEventHandlers: state.facet(lineNumberConfig).domEventHandlers
  8918. }));
  8919. /**
  8920. Create a line number gutter extension.
  8921. */
  8922. function lineNumbers(config = {}) {
  8923. return [
  8924. lineNumberConfig.of(config),
  8925. gutters(),
  8926. lineNumberGutter
  8927. ];
  8928. }
  8929. function maxLineNumber(lines) {
  8930. let last = 9;
  8931. while (last < lines)
  8932. last = last * 10 + 9;
  8933. return last;
  8934. }
  8935. const activeLineGutterMarker = /*@__PURE__*/new class extends GutterMarker {
  8936. constructor() {
  8937. super(...arguments);
  8938. this.elementClass = "cm-activeLineGutter";
  8939. }
  8940. };
  8941. const activeLineGutterHighlighter = /*@__PURE__*/gutterLineClass.compute(["selection"], state => {
  8942. let marks = [], last = -1;
  8943. for (let range of state.selection.ranges)
  8944. if (range.empty) {
  8945. let linePos = state.doc.lineAt(range.head).from;
  8946. if (linePos > last) {
  8947. last = linePos;
  8948. marks.push(activeLineGutterMarker.range(linePos));
  8949. }
  8950. }
  8951. return RangeSet.of(marks);
  8952. });
  8953. /**
  8954. Returns an extension that adds a `cm-activeLineGutter` class to
  8955. all gutter elements on the [active
  8956. line](https://codemirror.net/6/docs/ref/#view.highlightActiveLine).
  8957. */
  8958. function highlightActiveLineGutter() {
  8959. return activeLineGutterHighlighter;
  8960. }
  8961. /**
  8962. @internal
  8963. */
  8964. const __test = { HeightMap, HeightOracle, MeasuredHeights, QueryType, ChangedRange, computeOrder, moveVisually };
  8965. export { BidiSpan, BlockInfo, BlockType, Decoration, Direction, EditorView, GutterMarker, MatchDecorator, ViewPlugin, ViewUpdate, WidgetType, __test, closeHoverTooltips, crosshairCursor, drawSelection, dropCursor, getPanel, getTooltip, gutter, gutterLineClass, gutters, hasHoverTooltips, highlightActiveLine, highlightActiveLineGutter, highlightSpecialChars, hoverTooltip, keymap, lineNumberMarkers, lineNumbers, logException, panels, placeholder, rectangularSelection, repositionTooltips, runScopeHandlers, scrollPastEnd, showPanel, showTooltip, tooltips };