index.js 227 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var isPlainObject = require('is-plain-object');
  4. var immer = require('immer');
  5. function unwrapExports (x) {
  6. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  7. }
  8. function createCommonjsModule(fn, module) {
  9. return module = { exports: {} }, fn(module, module.exports), module.exports;
  10. }
  11. var arrayLikeToArray = createCommonjsModule(function (module) {
  12. function _arrayLikeToArray(arr, len) {
  13. if (len == null || len > arr.length) len = arr.length;
  14. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  15. arr2[i] = arr[i];
  16. }
  17. return arr2;
  18. }
  19. module.exports = _arrayLikeToArray;
  20. module.exports["default"] = module.exports, module.exports.__esModule = true;
  21. });
  22. unwrapExports(arrayLikeToArray);
  23. var arrayWithoutHoles = createCommonjsModule(function (module) {
  24. function _arrayWithoutHoles(arr) {
  25. if (Array.isArray(arr)) return arrayLikeToArray(arr);
  26. }
  27. module.exports = _arrayWithoutHoles;
  28. module.exports["default"] = module.exports, module.exports.__esModule = true;
  29. });
  30. unwrapExports(arrayWithoutHoles);
  31. var iterableToArray = createCommonjsModule(function (module) {
  32. function _iterableToArray(iter) {
  33. if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
  34. }
  35. module.exports = _iterableToArray;
  36. module.exports["default"] = module.exports, module.exports.__esModule = true;
  37. });
  38. unwrapExports(iterableToArray);
  39. var unsupportedIterableToArray = createCommonjsModule(function (module) {
  40. function _unsupportedIterableToArray(o, minLen) {
  41. if (!o) return;
  42. if (typeof o === "string") return arrayLikeToArray(o, minLen);
  43. var n = Object.prototype.toString.call(o).slice(8, -1);
  44. if (n === "Object" && o.constructor) n = o.constructor.name;
  45. if (n === "Map" || n === "Set") return Array.from(o);
  46. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return arrayLikeToArray(o, minLen);
  47. }
  48. module.exports = _unsupportedIterableToArray;
  49. module.exports["default"] = module.exports, module.exports.__esModule = true;
  50. });
  51. unwrapExports(unsupportedIterableToArray);
  52. var nonIterableSpread = createCommonjsModule(function (module) {
  53. function _nonIterableSpread() {
  54. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  55. }
  56. module.exports = _nonIterableSpread;
  57. module.exports["default"] = module.exports, module.exports.__esModule = true;
  58. });
  59. unwrapExports(nonIterableSpread);
  60. var toConsumableArray = createCommonjsModule(function (module) {
  61. function _toConsumableArray(arr) {
  62. return arrayWithoutHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableSpread();
  63. }
  64. module.exports = _toConsumableArray;
  65. module.exports["default"] = module.exports, module.exports.__esModule = true;
  66. });
  67. var _toConsumableArray = unwrapExports(toConsumableArray);
  68. var arrayWithHoles = createCommonjsModule(function (module) {
  69. function _arrayWithHoles(arr) {
  70. if (Array.isArray(arr)) return arr;
  71. }
  72. module.exports = _arrayWithHoles;
  73. module.exports["default"] = module.exports, module.exports.__esModule = true;
  74. });
  75. unwrapExports(arrayWithHoles);
  76. var iterableToArrayLimit = createCommonjsModule(function (module) {
  77. function _iterableToArrayLimit(arr, i) {
  78. var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
  79. if (_i == null) return;
  80. var _arr = [];
  81. var _n = true;
  82. var _d = false;
  83. var _s, _e;
  84. try {
  85. for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
  86. _arr.push(_s.value);
  87. if (i && _arr.length === i) break;
  88. }
  89. } catch (err) {
  90. _d = true;
  91. _e = err;
  92. } finally {
  93. try {
  94. if (!_n && _i["return"] != null) _i["return"]();
  95. } finally {
  96. if (_d) throw _e;
  97. }
  98. }
  99. return _arr;
  100. }
  101. module.exports = _iterableToArrayLimit;
  102. module.exports["default"] = module.exports, module.exports.__esModule = true;
  103. });
  104. unwrapExports(iterableToArrayLimit);
  105. var nonIterableRest = createCommonjsModule(function (module) {
  106. function _nonIterableRest() {
  107. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  108. }
  109. module.exports = _nonIterableRest;
  110. module.exports["default"] = module.exports, module.exports.__esModule = true;
  111. });
  112. unwrapExports(nonIterableRest);
  113. var slicedToArray = createCommonjsModule(function (module) {
  114. function _slicedToArray(arr, i) {
  115. return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || unsupportedIterableToArray(arr, i) || nonIterableRest();
  116. }
  117. module.exports = _slicedToArray;
  118. module.exports["default"] = module.exports, module.exports.__esModule = true;
  119. });
  120. var _slicedToArray = unwrapExports(slicedToArray);
  121. var defineProperty = createCommonjsModule(function (module) {
  122. function _defineProperty(obj, key, value) {
  123. if (key in obj) {
  124. Object.defineProperty(obj, key, {
  125. value: value,
  126. enumerable: true,
  127. configurable: true,
  128. writable: true
  129. });
  130. } else {
  131. obj[key] = value;
  132. }
  133. return obj;
  134. }
  135. module.exports = _defineProperty;
  136. module.exports["default"] = module.exports, module.exports.__esModule = true;
  137. });
  138. var _defineProperty = unwrapExports(defineProperty);
  139. var DIRTY_PATHS = new WeakMap();
  140. var DIRTY_PATH_KEYS = new WeakMap();
  141. var FLUSHING = new WeakMap();
  142. var NORMALIZING = new WeakMap();
  143. var PATH_REFS = new WeakMap();
  144. var POINT_REFS = new WeakMap();
  145. var RANGE_REFS = new WeakMap();
  146. function ownKeys$9(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  147. function _objectSpread$9(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$9(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$9(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  148. function _createForOfIteratorHelper$7(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$7(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  149. function _unsupportedIterableToArray$7(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$7(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$7(o, minLen); }
  150. function _arrayLikeToArray$7(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  151. /**
  152. * Create a new Slate `Editor` object.
  153. */
  154. var createEditor = function createEditor() {
  155. var editor = {
  156. children: [],
  157. operations: [],
  158. selection: null,
  159. marks: null,
  160. isInline: function isInline() {
  161. return false;
  162. },
  163. isVoid: function isVoid() {
  164. return false;
  165. },
  166. onChange: function onChange() {},
  167. apply: function apply(op) {
  168. var _iterator = _createForOfIteratorHelper$7(Editor.pathRefs(editor)),
  169. _step;
  170. try {
  171. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  172. var ref = _step.value;
  173. PathRef.transform(ref, op);
  174. }
  175. } catch (err) {
  176. _iterator.e(err);
  177. } finally {
  178. _iterator.f();
  179. }
  180. var _iterator2 = _createForOfIteratorHelper$7(Editor.pointRefs(editor)),
  181. _step2;
  182. try {
  183. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  184. var _ref = _step2.value;
  185. PointRef.transform(_ref, op);
  186. }
  187. } catch (err) {
  188. _iterator2.e(err);
  189. } finally {
  190. _iterator2.f();
  191. }
  192. var _iterator3 = _createForOfIteratorHelper$7(Editor.rangeRefs(editor)),
  193. _step3;
  194. try {
  195. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  196. var _ref2 = _step3.value;
  197. RangeRef.transform(_ref2, op);
  198. }
  199. } catch (err) {
  200. _iterator3.e(err);
  201. } finally {
  202. _iterator3.f();
  203. }
  204. var oldDirtyPaths = DIRTY_PATHS.get(editor) || [];
  205. var oldDirtyPathKeys = DIRTY_PATH_KEYS.get(editor) || new Set();
  206. var dirtyPaths;
  207. var dirtyPathKeys;
  208. var add = function add(path) {
  209. if (path) {
  210. var key = path.join(',');
  211. if (!dirtyPathKeys.has(key)) {
  212. dirtyPathKeys.add(key);
  213. dirtyPaths.push(path);
  214. }
  215. }
  216. };
  217. if (Path.operationCanTransformPath(op)) {
  218. dirtyPaths = [];
  219. dirtyPathKeys = new Set();
  220. var _iterator4 = _createForOfIteratorHelper$7(oldDirtyPaths),
  221. _step4;
  222. try {
  223. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  224. var path = _step4.value;
  225. var newPath = Path.transform(path, op);
  226. add(newPath);
  227. }
  228. } catch (err) {
  229. _iterator4.e(err);
  230. } finally {
  231. _iterator4.f();
  232. }
  233. } else {
  234. dirtyPaths = oldDirtyPaths;
  235. dirtyPathKeys = oldDirtyPathKeys;
  236. }
  237. var newDirtyPaths = getDirtyPaths(op);
  238. var _iterator5 = _createForOfIteratorHelper$7(newDirtyPaths),
  239. _step5;
  240. try {
  241. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  242. var _path = _step5.value;
  243. add(_path);
  244. }
  245. } catch (err) {
  246. _iterator5.e(err);
  247. } finally {
  248. _iterator5.f();
  249. }
  250. DIRTY_PATHS.set(editor, dirtyPaths);
  251. DIRTY_PATH_KEYS.set(editor, dirtyPathKeys);
  252. Transforms.transform(editor, op);
  253. editor.operations.push(op);
  254. Editor.normalize(editor); // Clear any formats applied to the cursor if the selection changes.
  255. if (op.type === 'set_selection') {
  256. editor.marks = null;
  257. }
  258. if (!FLUSHING.get(editor)) {
  259. FLUSHING.set(editor, true);
  260. Promise.resolve().then(function () {
  261. FLUSHING.set(editor, false);
  262. editor.onChange();
  263. editor.operations = [];
  264. });
  265. }
  266. },
  267. addMark: function addMark(key, value) {
  268. var selection = editor.selection;
  269. if (selection) {
  270. if (Range.isExpanded(selection)) {
  271. Transforms.setNodes(editor, _defineProperty({}, key, value), {
  272. match: Text.isText,
  273. split: true
  274. });
  275. } else {
  276. var marks = _objectSpread$9(_objectSpread$9({}, Editor.marks(editor) || {}), {}, _defineProperty({}, key, value));
  277. editor.marks = marks;
  278. if (!FLUSHING.get(editor)) {
  279. editor.onChange();
  280. }
  281. }
  282. }
  283. },
  284. deleteBackward: function deleteBackward(unit) {
  285. var selection = editor.selection;
  286. if (selection && Range.isCollapsed(selection)) {
  287. Transforms["delete"](editor, {
  288. unit: unit,
  289. reverse: true
  290. });
  291. }
  292. },
  293. deleteForward: function deleteForward(unit) {
  294. var selection = editor.selection;
  295. if (selection && Range.isCollapsed(selection)) {
  296. Transforms["delete"](editor, {
  297. unit: unit
  298. });
  299. }
  300. },
  301. deleteFragment: function deleteFragment(direction) {
  302. var selection = editor.selection;
  303. if (selection && Range.isExpanded(selection)) {
  304. Transforms["delete"](editor, {
  305. reverse: direction === 'backward'
  306. });
  307. }
  308. },
  309. getFragment: function getFragment() {
  310. var selection = editor.selection;
  311. if (selection) {
  312. return Node.fragment(editor, selection);
  313. }
  314. return [];
  315. },
  316. insertBreak: function insertBreak() {
  317. Transforms.splitNodes(editor, {
  318. always: true
  319. });
  320. },
  321. insertFragment: function insertFragment(fragment) {
  322. Transforms.insertFragment(editor, fragment);
  323. },
  324. insertNode: function insertNode(node) {
  325. Transforms.insertNodes(editor, node);
  326. },
  327. insertText: function insertText(text) {
  328. var selection = editor.selection,
  329. marks = editor.marks;
  330. if (selection) {
  331. if (marks) {
  332. var node = _objectSpread$9({
  333. text: text
  334. }, marks);
  335. Transforms.insertNodes(editor, node);
  336. } else {
  337. Transforms.insertText(editor, text);
  338. }
  339. editor.marks = null;
  340. }
  341. },
  342. normalizeNode: function normalizeNode(entry) {
  343. var _entry = _slicedToArray(entry, 2),
  344. node = _entry[0],
  345. path = _entry[1]; // There are no core normalizations for text nodes.
  346. if (Text.isText(node)) {
  347. return;
  348. } // Ensure that block and inline nodes have at least one text child.
  349. if (Element.isElement(node) && node.children.length === 0) {
  350. var child = {
  351. text: ''
  352. };
  353. Transforms.insertNodes(editor, child, {
  354. at: path.concat(0),
  355. voids: true
  356. });
  357. return;
  358. } // Determine whether the node should have block or inline children.
  359. var shouldHaveInlines = Editor.isEditor(node) ? false : Element.isElement(node) && (editor.isInline(node) || node.children.length === 0 || Text.isText(node.children[0]) || editor.isInline(node.children[0])); // Since we'll be applying operations while iterating, keep track of an
  360. // index that accounts for any added/removed nodes.
  361. var n = 0;
  362. for (var i = 0; i < node.children.length; i++, n++) {
  363. var currentNode = Node.get(editor, path);
  364. if (Text.isText(currentNode)) continue;
  365. var _child = node.children[i];
  366. var prev = currentNode.children[n - 1];
  367. var isLast = i === node.children.length - 1;
  368. var isInlineOrText = Text.isText(_child) || Element.isElement(_child) && editor.isInline(_child); // Only allow block nodes in the top-level children and parent blocks
  369. // that only contain block nodes. Similarly, only allow inline nodes in
  370. // other inline nodes, or parent blocks that only contain inlines and
  371. // text.
  372. if (isInlineOrText !== shouldHaveInlines) {
  373. Transforms.removeNodes(editor, {
  374. at: path.concat(n),
  375. voids: true
  376. });
  377. n--;
  378. } else if (Element.isElement(_child)) {
  379. // Ensure that inline nodes are surrounded by text nodes.
  380. if (editor.isInline(_child)) {
  381. if (prev == null || !Text.isText(prev)) {
  382. var newChild = {
  383. text: ''
  384. };
  385. Transforms.insertNodes(editor, newChild, {
  386. at: path.concat(n),
  387. voids: true
  388. });
  389. n++;
  390. } else if (isLast) {
  391. var _newChild = {
  392. text: ''
  393. };
  394. Transforms.insertNodes(editor, _newChild, {
  395. at: path.concat(n + 1),
  396. voids: true
  397. });
  398. n++;
  399. }
  400. }
  401. } else {
  402. // Merge adjacent text nodes that are empty or match.
  403. if (prev != null && Text.isText(prev)) {
  404. if (Text.equals(_child, prev, {
  405. loose: true
  406. })) {
  407. Transforms.mergeNodes(editor, {
  408. at: path.concat(n),
  409. voids: true
  410. });
  411. n--;
  412. } else if (prev.text === '') {
  413. Transforms.removeNodes(editor, {
  414. at: path.concat(n - 1),
  415. voids: true
  416. });
  417. n--;
  418. } else if (_child.text === '') {
  419. Transforms.removeNodes(editor, {
  420. at: path.concat(n),
  421. voids: true
  422. });
  423. n--;
  424. }
  425. }
  426. }
  427. }
  428. },
  429. removeMark: function removeMark(key) {
  430. var selection = editor.selection;
  431. if (selection) {
  432. if (Range.isExpanded(selection)) {
  433. Transforms.unsetNodes(editor, key, {
  434. match: Text.isText,
  435. split: true
  436. });
  437. } else {
  438. var marks = _objectSpread$9({}, Editor.marks(editor) || {});
  439. delete marks[key];
  440. editor.marks = marks;
  441. if (!FLUSHING.get(editor)) {
  442. editor.onChange();
  443. }
  444. }
  445. }
  446. }
  447. };
  448. return editor;
  449. };
  450. /**
  451. * Get the "dirty" paths generated from an operation.
  452. */
  453. var getDirtyPaths = function getDirtyPaths(op) {
  454. switch (op.type) {
  455. case 'insert_text':
  456. case 'remove_text':
  457. case 'set_node':
  458. {
  459. var path = op.path;
  460. return Path.levels(path);
  461. }
  462. case 'insert_node':
  463. {
  464. var node = op.node,
  465. _path2 = op.path;
  466. var levels = Path.levels(_path2);
  467. var descendants = Text.isText(node) ? [] : Array.from(Node.nodes(node), function (_ref3) {
  468. var _ref4 = _slicedToArray(_ref3, 2),
  469. p = _ref4[1];
  470. return _path2.concat(p);
  471. });
  472. return [].concat(_toConsumableArray(levels), _toConsumableArray(descendants));
  473. }
  474. case 'merge_node':
  475. {
  476. var _path3 = op.path;
  477. var ancestors = Path.ancestors(_path3);
  478. var previousPath = Path.previous(_path3);
  479. return [].concat(_toConsumableArray(ancestors), [previousPath]);
  480. }
  481. case 'move_node':
  482. {
  483. var _path4 = op.path,
  484. newPath = op.newPath;
  485. if (Path.equals(_path4, newPath)) {
  486. return [];
  487. }
  488. var oldAncestors = [];
  489. var newAncestors = [];
  490. var _iterator6 = _createForOfIteratorHelper$7(Path.ancestors(_path4)),
  491. _step6;
  492. try {
  493. for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
  494. var ancestor = _step6.value;
  495. var p = Path.transform(ancestor, op);
  496. oldAncestors.push(p);
  497. }
  498. } catch (err) {
  499. _iterator6.e(err);
  500. } finally {
  501. _iterator6.f();
  502. }
  503. var _iterator7 = _createForOfIteratorHelper$7(Path.ancestors(newPath)),
  504. _step7;
  505. try {
  506. for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
  507. var _ancestor = _step7.value;
  508. var _p = Path.transform(_ancestor, op);
  509. newAncestors.push(_p);
  510. }
  511. } catch (err) {
  512. _iterator7.e(err);
  513. } finally {
  514. _iterator7.f();
  515. }
  516. var newParent = newAncestors[newAncestors.length - 1];
  517. var newIndex = newPath[newPath.length - 1];
  518. var resultPath = newParent.concat(newIndex);
  519. return [].concat(oldAncestors, newAncestors, [resultPath]);
  520. }
  521. case 'remove_node':
  522. {
  523. var _path5 = op.path;
  524. var _ancestors = Path.ancestors(_path5);
  525. return _toConsumableArray(_ancestors);
  526. }
  527. case 'split_node':
  528. {
  529. var _path6 = op.path;
  530. var _levels = Path.levels(_path6);
  531. var nextPath = Path.next(_path6);
  532. return [].concat(_toConsumableArray(_levels), [nextPath]);
  533. }
  534. default:
  535. {
  536. return [];
  537. }
  538. }
  539. };
  540. var objectWithoutPropertiesLoose = createCommonjsModule(function (module) {
  541. function _objectWithoutPropertiesLoose(source, excluded) {
  542. if (source == null) return {};
  543. var target = {};
  544. var sourceKeys = Object.keys(source);
  545. var key, i;
  546. for (i = 0; i < sourceKeys.length; i++) {
  547. key = sourceKeys[i];
  548. if (excluded.indexOf(key) >= 0) continue;
  549. target[key] = source[key];
  550. }
  551. return target;
  552. }
  553. module.exports = _objectWithoutPropertiesLoose;
  554. module.exports["default"] = module.exports, module.exports.__esModule = true;
  555. });
  556. unwrapExports(objectWithoutPropertiesLoose);
  557. var objectWithoutProperties = createCommonjsModule(function (module) {
  558. function _objectWithoutProperties(source, excluded) {
  559. if (source == null) return {};
  560. var target = objectWithoutPropertiesLoose(source, excluded);
  561. var key, i;
  562. if (Object.getOwnPropertySymbols) {
  563. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  564. for (i = 0; i < sourceSymbolKeys.length; i++) {
  565. key = sourceSymbolKeys[i];
  566. if (excluded.indexOf(key) >= 0) continue;
  567. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  568. target[key] = source[key];
  569. }
  570. }
  571. return target;
  572. }
  573. module.exports = _objectWithoutProperties;
  574. module.exports["default"] = module.exports, module.exports.__esModule = true;
  575. });
  576. var _objectWithoutProperties = unwrapExports(objectWithoutProperties);
  577. function _createForOfIteratorHelper$6(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$6(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  578. function _unsupportedIterableToArray$6(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$6(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$6(o, minLen); }
  579. function _arrayLikeToArray$6(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  580. // Character (grapheme cluster) boundaries are determined according to
  581. // the default grapheme cluster boundary specification, extended grapheme clusters variant[1].
  582. //
  583. // References:
  584. //
  585. // [1] https://www.unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table
  586. // [2] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakProperty.txt
  587. // [3] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.html
  588. // [4] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.txt
  589. /**
  590. * Get the distance to the end of the first character in a string of text.
  591. */
  592. var getCharacterDistance = function getCharacterDistance(str) {
  593. var isRTL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  594. var isLTR = !isRTL;
  595. var codepoints = isRTL ? codepointsIteratorRTL(str) : str;
  596. var left = CodepointType.None;
  597. var right = CodepointType.None;
  598. var distance = 0; // Evaluation of these conditions are deferred.
  599. var gb11 = null; // Is GB11 applicable?
  600. var gb12Or13 = null; // Is GB12 or GB13 applicable?
  601. var _iterator = _createForOfIteratorHelper$6(codepoints),
  602. _step;
  603. try {
  604. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  605. var _char = _step.value;
  606. var code = _char.codePointAt(0);
  607. if (!code) break;
  608. var type = getCodepointType(_char, code);
  609. var _ref = isLTR ? [right, type] : [type, left];
  610. var _ref2 = _slicedToArray(_ref, 2);
  611. left = _ref2[0];
  612. right = _ref2[1];
  613. if (intersects(left, CodepointType.ZWJ) && intersects(right, CodepointType.ExtPict)) {
  614. if (isLTR) {
  615. gb11 = endsWithEmojiZWJ(str.substring(0, distance));
  616. } else {
  617. gb11 = endsWithEmojiZWJ(str.substring(0, str.length - distance));
  618. }
  619. if (!gb11) break;
  620. }
  621. if (intersects(left, CodepointType.RI) && intersects(right, CodepointType.RI)) {
  622. if (gb12Or13 !== null) {
  623. gb12Or13 = !gb12Or13;
  624. } else {
  625. if (isLTR) {
  626. gb12Or13 = true;
  627. } else {
  628. gb12Or13 = endsWithOddNumberOfRIs(str.substring(0, str.length - distance));
  629. }
  630. }
  631. if (!gb12Or13) break;
  632. }
  633. if (left !== CodepointType.None && right !== CodepointType.None && isBoundaryPair(left, right)) {
  634. break;
  635. }
  636. distance += _char.length;
  637. }
  638. } catch (err) {
  639. _iterator.e(err);
  640. } finally {
  641. _iterator.f();
  642. }
  643. return distance || 1;
  644. };
  645. var SPACE = /\s/;
  646. var PUNCTUATION = /[\u0021-\u0023\u0025-\u002A\u002C-\u002F\u003A\u003B\u003F\u0040\u005B-\u005D\u005F\u007B\u007D\u00A1\u00A7\u00AB\u00B6\u00B7\u00BB\u00BF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E3B\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]/;
  647. var CHAMELEON = /['\u2018\u2019]/;
  648. /**
  649. * Get the distance to the end of the first word in a string of text.
  650. */
  651. var getWordDistance = function getWordDistance(text) {
  652. var isRTL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  653. var dist = 0;
  654. var started = false;
  655. while (text.length > 0) {
  656. var charDist = getCharacterDistance(text, isRTL);
  657. var _splitByCharacterDist = splitByCharacterDistance(text, charDist, isRTL),
  658. _splitByCharacterDist2 = _slicedToArray(_splitByCharacterDist, 2),
  659. _char2 = _splitByCharacterDist2[0],
  660. remaining = _splitByCharacterDist2[1];
  661. if (isWordCharacter(_char2, remaining, isRTL)) {
  662. started = true;
  663. dist += charDist;
  664. } else if (!started) {
  665. dist += charDist;
  666. } else {
  667. break;
  668. }
  669. text = remaining;
  670. }
  671. return dist;
  672. };
  673. /**
  674. * Split a string in two parts at a given distance starting from the end when
  675. * `isRTL` is set to `true`.
  676. */
  677. var splitByCharacterDistance = function splitByCharacterDistance(str, dist, isRTL) {
  678. if (isRTL) {
  679. var at = str.length - dist;
  680. return [str.slice(at, str.length), str.slice(0, at)];
  681. }
  682. return [str.slice(0, dist), str.slice(dist)];
  683. };
  684. /**
  685. * Check if a character is a word character. The `remaining` argument is used
  686. * because sometimes you must read subsequent characters to truly determine it.
  687. */
  688. var isWordCharacter = function isWordCharacter(_char3, remaining) {
  689. var isRTL = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  690. if (SPACE.test(_char3)) {
  691. return false;
  692. } // Chameleons count as word characters as long as they're in a word, so
  693. // recurse to see if the next one is a word character or not.
  694. if (CHAMELEON.test(_char3)) {
  695. var charDist = getCharacterDistance(remaining, isRTL);
  696. var _splitByCharacterDist3 = splitByCharacterDistance(remaining, charDist, isRTL),
  697. _splitByCharacterDist4 = _slicedToArray(_splitByCharacterDist3, 2),
  698. nextChar = _splitByCharacterDist4[0],
  699. nextRemaining = _splitByCharacterDist4[1];
  700. if (isWordCharacter(nextChar, nextRemaining, isRTL)) {
  701. return true;
  702. }
  703. }
  704. if (PUNCTUATION.test(_char3)) {
  705. return false;
  706. }
  707. return true;
  708. };
  709. /**
  710. * Iterate on codepoints from right to left.
  711. */
  712. var codepointsIteratorRTL = function* codepointsIteratorRTL(str) {
  713. var end = str.length - 1;
  714. for (var i = 0; i < str.length; i++) {
  715. var char1 = str.charAt(end - i);
  716. if (isLowSurrogate(char1.charCodeAt(0))) {
  717. var char2 = str.charAt(end - i - 1);
  718. if (isHighSurrogate(char2.charCodeAt(0))) {
  719. yield char2 + char1;
  720. i++;
  721. continue;
  722. }
  723. }
  724. yield char1;
  725. }
  726. };
  727. /**
  728. * Is `charCode` a high surrogate.
  729. *
  730. * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
  731. */
  732. var isHighSurrogate = function isHighSurrogate(charCode) {
  733. return charCode >= 0xd800 && charCode <= 0xdbff;
  734. };
  735. /**
  736. * Is `charCode` a low surrogate.
  737. *
  738. * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
  739. */
  740. var isLowSurrogate = function isLowSurrogate(charCode) {
  741. return charCode >= 0xdc00 && charCode <= 0xdfff;
  742. };
  743. var CodepointType;
  744. (function (CodepointType) {
  745. CodepointType[CodepointType["None"] = 0] = "None";
  746. CodepointType[CodepointType["Extend"] = 1] = "Extend";
  747. CodepointType[CodepointType["ZWJ"] = 2] = "ZWJ";
  748. CodepointType[CodepointType["RI"] = 4] = "RI";
  749. CodepointType[CodepointType["Prepend"] = 8] = "Prepend";
  750. CodepointType[CodepointType["SpacingMark"] = 16] = "SpacingMark";
  751. CodepointType[CodepointType["L"] = 32] = "L";
  752. CodepointType[CodepointType["V"] = 64] = "V";
  753. CodepointType[CodepointType["T"] = 128] = "T";
  754. CodepointType[CodepointType["LV"] = 256] = "LV";
  755. CodepointType[CodepointType["LVT"] = 512] = "LVT";
  756. CodepointType[CodepointType["ExtPict"] = 1024] = "ExtPict";
  757. CodepointType[CodepointType["Any"] = 2048] = "Any";
  758. })(CodepointType || (CodepointType = {}));
  759. var reExtend = /^(?:[\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u07FD\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08D3-\u08E1\u08E3-\u0902\u093A\u093C\u0941-\u0948\u094D\u0951-\u0957\u0962\u0963\u0981\u09BC\u09BE\u09C1-\u09C4\u09CD\u09D7\u09E2\u09E3\u09FE\u0A01\u0A02\u0A3C\u0A41\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81\u0A82\u0ABC\u0AC1-\u0AC5\u0AC7\u0AC8\u0ACD\u0AE2\u0AE3\u0AFA-\u0AFF\u0B01\u0B3C\u0B3E\u0B3F\u0B41-\u0B44\u0B4D\u0B55-\u0B57\u0B62\u0B63\u0B82\u0BBE\u0BC0\u0BCD\u0BD7\u0C00\u0C04\u0C3E-\u0C40\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C81\u0CBC\u0CBF\u0CC2\u0CC6\u0CCC\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D00\u0D01\u0D3B\u0D3C\u0D3E\u0D41-\u0D44\u0D4D\u0D57\u0D62\u0D63\u0D81\u0DCA\u0DCF\u0DD2-\u0DD4\u0DD6\u0DDF\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F71-\u0F7E\u0F80-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102D-\u1030\u1032-\u1037\u1039\u103A\u103D\u103E\u1058\u1059\u105E-\u1060\u1071-\u1074\u1082\u1085\u1086\u108D\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4\u17B5\u17B7-\u17BD\u17C6\u17C9-\u17D3\u17DD\u180B-\u180D\u1885\u1886\u18A9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193B\u1A17\u1A18\u1A1B\u1A56\u1A58-\u1A5E\u1A60\u1A62\u1A65-\u1A6C\u1A73-\u1A7C\u1A7F\u1AB0-\u1AC0\u1B00-\u1B03\u1B34-\u1B3A\u1B3C\u1B42\u1B6B-\u1B73\u1B80\u1B81\u1BA2-\u1BA5\u1BA8\u1BA9\u1BAB-\u1BAD\u1BE6\u1BE8\u1BE9\u1BED\u1BEF-\u1BF1\u1C2C-\u1C33\u1C36\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE0\u1CE2-\u1CE8\u1CED\u1CF4\u1CF8\u1CF9\u1DC0-\u1DF9\u1DFB-\u1DFF\u200C\u20D0-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA825\uA826\uA82C\uA8C4\uA8C5\uA8E0-\uA8F1\uA8FF\uA926-\uA92D\uA947-\uA951\uA980-\uA982\uA9B3\uA9B6-\uA9B9\uA9BC\uA9BD\uA9E5\uAA29-\uAA2E\uAA31\uAA32\uAA35\uAA36\uAA43\uAA4C\uAA7C\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEC\uAAED\uAAF6\uABE5\uABE8\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE2F\uFF9E\uFF9F]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD803[\uDD24-\uDD27\uDEAB\uDEAC\uDF46-\uDF50]|\uD804[\uDC01\uDC38-\uDC46\uDC7F-\uDC81\uDCB3-\uDCB6\uDCB9\uDCBA\uDD00-\uDD02\uDD27-\uDD2B\uDD2D-\uDD34\uDD73\uDD80\uDD81\uDDB6-\uDDBE\uDDC9-\uDDCC\uDDCF\uDE2F-\uDE31\uDE34\uDE36\uDE37\uDE3E\uDEDF\uDEE3-\uDEEA\uDF00\uDF01\uDF3B\uDF3C\uDF3E\uDF40\uDF57\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC38-\uDC3F\uDC42-\uDC44\uDC46\uDC5E\uDCB0\uDCB3-\uDCB8\uDCBA\uDCBD\uDCBF\uDCC0\uDCC2\uDCC3\uDDAF\uDDB2-\uDDB5\uDDBC\uDDBD\uDDBF\uDDC0\uDDDC\uDDDD\uDE33-\uDE3A\uDE3D\uDE3F\uDE40\uDEAB\uDEAD\uDEB0-\uDEB5\uDEB7\uDF1D-\uDF1F\uDF22-\uDF25\uDF27-\uDF2B]|\uD806[\uDC2F-\uDC37\uDC39\uDC3A\uDD30\uDD3B\uDD3C\uDD3E\uDD43\uDDD4-\uDDD7\uDDDA\uDDDB\uDDE0\uDE01-\uDE0A\uDE33-\uDE38\uDE3B-\uDE3E\uDE47\uDE51-\uDE56\uDE59-\uDE5B\uDE8A-\uDE96\uDE98\uDE99]|\uD807[\uDC30-\uDC36\uDC38-\uDC3D\uDC3F\uDC92-\uDCA7\uDCAA-\uDCB0\uDCB2\uDCB3\uDCB5\uDCB6\uDD31-\uDD36\uDD3A\uDD3C\uDD3D\uDD3F-\uDD45\uDD47\uDD90\uDD91\uDD95\uDD97\uDEF3\uDEF4]|\uD81A[\uDEF0-\uDEF4\uDF30-\uDF36]|\uD81B[\uDF4F\uDF8F-\uDF92\uDFE4]|\uD82F[\uDC9D\uDC9E]|\uD834[\uDD65\uDD67-\uDD69\uDD6E-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD838[\uDC00-\uDC06\uDC08-\uDC18\uDC1B-\uDC21\uDC23\uDC24\uDC26-\uDC2A\uDD30-\uDD36\uDEEC-\uDEEF]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A]|\uD83C[\uDFFB-\uDFFF]|\uDB40[\uDC20-\uDC7F\uDD00-\uDDEF])$/;
  760. var rePrepend = /^(?:[\u0600-\u0605\u06DD\u070F\u0890\u0891\u08E2\u0D4E]|\uD804[\uDCBD\uDCCD\uDDC2\uDDC3]|\uD806[\uDD3F\uDD41\uDE3A\uDE84-\uDE89]|\uD807\uDD46)$/;
  761. var reSpacingMark = /^(?:[\u0903\u093B\u093E-\u0940\u0949-\u094C\u094E\u094F\u0982\u0983\u09BF\u09C0\u09C7\u09C8\u09CB\u09CC\u0A03\u0A3E-\u0A40\u0A83\u0ABE-\u0AC0\u0AC9\u0ACB\u0ACC\u0B02\u0B03\u0B40\u0B47\u0B48\u0B4B\u0B4C\u0BBF\u0BC1\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCC\u0C01-\u0C03\u0C41-\u0C44\u0C82\u0C83\u0CBE\u0CC0\u0CC1\u0CC3\u0CC4\u0CC7\u0CC8\u0CCA\u0CCB\u0D02\u0D03\u0D3F\u0D40\u0D46-\u0D48\u0D4A-\u0D4C\u0D82\u0D83\u0DD0\u0DD1\u0DD8-\u0DDE\u0DF2\u0DF3\u0E33\u0EB3\u0F3E\u0F3F\u0F7F\u1031\u103B\u103C\u1056\u1057\u1084\u1715\u1734\u17B6\u17BE-\u17C5\u17C7\u17C8\u1923-\u1926\u1929-\u192B\u1930\u1931\u1933-\u1938\u1A19\u1A1A\u1A55\u1A57\u1A6D-\u1A72\u1B04\u1B3B\u1B3D-\u1B41\u1B43\u1B44\u1B82\u1BA1\u1BA6\u1BA7\u1BAA\u1BE7\u1BEA-\u1BEC\u1BEE\u1BF2\u1BF3\u1C24-\u1C2B\u1C34\u1C35\u1CE1\u1CF7\uA823\uA824\uA827\uA880\uA881\uA8B4-\uA8C3\uA952\uA953\uA983\uA9B4\uA9B5\uA9BA\uA9BB\uA9BE-\uA9C0\uAA2F\uAA30\uAA33\uAA34\uAA4D\uAAEB\uAAEE\uAAEF\uAAF5\uABE3\uABE4\uABE6\uABE7\uABE9\uABEA\uABEC]|\uD804[\uDC00\uDC02\uDC82\uDCB0-\uDCB2\uDCB7\uDCB8\uDD2C\uDD45\uDD46\uDD82\uDDB3-\uDDB5\uDDBF\uDDC0\uDDCE\uDE2C-\uDE2E\uDE32\uDE33\uDE35\uDEE0-\uDEE2\uDF02\uDF03\uDF3F\uDF41-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF62\uDF63]|\uD805[\uDC35-\uDC37\uDC40\uDC41\uDC45\uDCB1\uDCB2\uDCB9\uDCBB\uDCBC\uDCBE\uDCC1\uDDB0\uDDB1\uDDB8-\uDDBB\uDDBE\uDE30-\uDE32\uDE3B\uDE3C\uDE3E\uDEAC\uDEAE\uDEAF\uDEB6\uDF26]|\uD806[\uDC2C-\uDC2E\uDC38\uDD31-\uDD35\uDD37\uDD38\uDD3D\uDD40\uDD42\uDDD1-\uDDD3\uDDDC-\uDDDF\uDDE4\uDE39\uDE57\uDE58\uDE97]|\uD807[\uDC2F\uDC3E\uDCA9\uDCB1\uDCB4\uDD8A-\uDD8E\uDD93\uDD94\uDD96\uDEF5\uDEF6]|\uD81B[\uDF51-\uDF87\uDFF0\uDFF1]|\uD834[\uDD66\uDD6D])$/;
  762. var reL = /^[\u1100-\u115F\uA960-\uA97C]$/;
  763. var reV = /^[\u1160-\u11A7\uD7B0-\uD7C6]$/;
  764. var reT = /^[\u11A8-\u11FF\uD7CB-\uD7FB]$/;
  765. var reLV = /^[\uAC00\uAC1C\uAC38\uAC54\uAC70\uAC8C\uACA8\uACC4\uACE0\uACFC\uAD18\uAD34\uAD50\uAD6C\uAD88\uADA4\uADC0\uADDC\uADF8\uAE14\uAE30\uAE4C\uAE68\uAE84\uAEA0\uAEBC\uAED8\uAEF4\uAF10\uAF2C\uAF48\uAF64\uAF80\uAF9C\uAFB8\uAFD4\uAFF0\uB00C\uB028\uB044\uB060\uB07C\uB098\uB0B4\uB0D0\uB0EC\uB108\uB124\uB140\uB15C\uB178\uB194\uB1B0\uB1CC\uB1E8\uB204\uB220\uB23C\uB258\uB274\uB290\uB2AC\uB2C8\uB2E4\uB300\uB31C\uB338\uB354\uB370\uB38C\uB3A8\uB3C4\uB3E0\uB3FC\uB418\uB434\uB450\uB46C\uB488\uB4A4\uB4C0\uB4DC\uB4F8\uB514\uB530\uB54C\uB568\uB584\uB5A0\uB5BC\uB5D8\uB5F4\uB610\uB62C\uB648\uB664\uB680\uB69C\uB6B8\uB6D4\uB6F0\uB70C\uB728\uB744\uB760\uB77C\uB798\uB7B4\uB7D0\uB7EC\uB808\uB824\uB840\uB85C\uB878\uB894\uB8B0\uB8CC\uB8E8\uB904\uB920\uB93C\uB958\uB974\uB990\uB9AC\uB9C8\uB9E4\uBA00\uBA1C\uBA38\uBA54\uBA70\uBA8C\uBAA8\uBAC4\uBAE0\uBAFC\uBB18\uBB34\uBB50\uBB6C\uBB88\uBBA4\uBBC0\uBBDC\uBBF8\uBC14\uBC30\uBC4C\uBC68\uBC84\uBCA0\uBCBC\uBCD8\uBCF4\uBD10\uBD2C\uBD48\uBD64\uBD80\uBD9C\uBDB8\uBDD4\uBDF0\uBE0C\uBE28\uBE44\uBE60\uBE7C\uBE98\uBEB4\uBED0\uBEEC\uBF08\uBF24\uBF40\uBF5C\uBF78\uBF94\uBFB0\uBFCC\uBFE8\uC004\uC020\uC03C\uC058\uC074\uC090\uC0AC\uC0C8\uC0E4\uC100\uC11C\uC138\uC154\uC170\uC18C\uC1A8\uC1C4\uC1E0\uC1FC\uC218\uC234\uC250\uC26C\uC288\uC2A4\uC2C0\uC2DC\uC2F8\uC314\uC330\uC34C\uC368\uC384\uC3A0\uC3BC\uC3D8\uC3F4\uC410\uC42C\uC448\uC464\uC480\uC49C\uC4B8\uC4D4\uC4F0\uC50C\uC528\uC544\uC560\uC57C\uC598\uC5B4\uC5D0\uC5EC\uC608\uC624\uC640\uC65C\uC678\uC694\uC6B0\uC6CC\uC6E8\uC704\uC720\uC73C\uC758\uC774\uC790\uC7AC\uC7C8\uC7E4\uC800\uC81C\uC838\uC854\uC870\uC88C\uC8A8\uC8C4\uC8E0\uC8FC\uC918\uC934\uC950\uC96C\uC988\uC9A4\uC9C0\uC9DC\uC9F8\uCA14\uCA30\uCA4C\uCA68\uCA84\uCAA0\uCABC\uCAD8\uCAF4\uCB10\uCB2C\uCB48\uCB64\uCB80\uCB9C\uCBB8\uCBD4\uCBF0\uCC0C\uCC28\uCC44\uCC60\uCC7C\uCC98\uCCB4\uCCD0\uCCEC\uCD08\uCD24\uCD40\uCD5C\uCD78\uCD94\uCDB0\uCDCC\uCDE8\uCE04\uCE20\uCE3C\uCE58\uCE74\uCE90\uCEAC\uCEC8\uCEE4\uCF00\uCF1C\uCF38\uCF54\uCF70\uCF8C\uCFA8\uCFC4\uCFE0\uCFFC\uD018\uD034\uD050\uD06C\uD088\uD0A4\uD0C0\uD0DC\uD0F8\uD114\uD130\uD14C\uD168\uD184\uD1A0\uD1BC\uD1D8\uD1F4\uD210\uD22C\uD248\uD264\uD280\uD29C\uD2B8\uD2D4\uD2F0\uD30C\uD328\uD344\uD360\uD37C\uD398\uD3B4\uD3D0\uD3EC\uD408\uD424\uD440\uD45C\uD478\uD494\uD4B0\uD4CC\uD4E8\uD504\uD520\uD53C\uD558\uD574\uD590\uD5AC\uD5C8\uD5E4\uD600\uD61C\uD638\uD654\uD670\uD68C\uD6A8\uD6C4\uD6E0\uD6FC\uD718\uD734\uD750\uD76C\uD788]$/;
  766. var reLVT = /^[\uAC01-\uAC1B\uAC1D-\uAC37\uAC39-\uAC53\uAC55-\uAC6F\uAC71-\uAC8B\uAC8D-\uACA7\uACA9-\uACC3\uACC5-\uACDF\uACE1-\uACFB\uACFD-\uAD17\uAD19-\uAD33\uAD35-\uAD4F\uAD51-\uAD6B\uAD6D-\uAD87\uAD89-\uADA3\uADA5-\uADBF\uADC1-\uADDB\uADDD-\uADF7\uADF9-\uAE13\uAE15-\uAE2F\uAE31-\uAE4B\uAE4D-\uAE67\uAE69-\uAE83\uAE85-\uAE9F\uAEA1-\uAEBB\uAEBD-\uAED7\uAED9-\uAEF3\uAEF5-\uAF0F\uAF11-\uAF2B\uAF2D-\uAF47\uAF49-\uAF63\uAF65-\uAF7F\uAF81-\uAF9B\uAF9D-\uAFB7\uAFB9-\uAFD3\uAFD5-\uAFEF\uAFF1-\uB00B\uB00D-\uB027\uB029-\uB043\uB045-\uB05F\uB061-\uB07B\uB07D-\uB097\uB099-\uB0B3\uB0B5-\uB0CF\uB0D1-\uB0EB\uB0ED-\uB107\uB109-\uB123\uB125-\uB13F\uB141-\uB15B\uB15D-\uB177\uB179-\uB193\uB195-\uB1AF\uB1B1-\uB1CB\uB1CD-\uB1E7\uB1E9-\uB203\uB205-\uB21F\uB221-\uB23B\uB23D-\uB257\uB259-\uB273\uB275-\uB28F\uB291-\uB2AB\uB2AD-\uB2C7\uB2C9-\uB2E3\uB2E5-\uB2FF\uB301-\uB31B\uB31D-\uB337\uB339-\uB353\uB355-\uB36F\uB371-\uB38B\uB38D-\uB3A7\uB3A9-\uB3C3\uB3C5-\uB3DF\uB3E1-\uB3FB\uB3FD-\uB417\uB419-\uB433\uB435-\uB44F\uB451-\uB46B\uB46D-\uB487\uB489-\uB4A3\uB4A5-\uB4BF\uB4C1-\uB4DB\uB4DD-\uB4F7\uB4F9-\uB513\uB515-\uB52F\uB531-\uB54B\uB54D-\uB567\uB569-\uB583\uB585-\uB59F\uB5A1-\uB5BB\uB5BD-\uB5D7\uB5D9-\uB5F3\uB5F5-\uB60F\uB611-\uB62B\uB62D-\uB647\uB649-\uB663\uB665-\uB67F\uB681-\uB69B\uB69D-\uB6B7\uB6B9-\uB6D3\uB6D5-\uB6EF\uB6F1-\uB70B\uB70D-\uB727\uB729-\uB743\uB745-\uB75F\uB761-\uB77B\uB77D-\uB797\uB799-\uB7B3\uB7B5-\uB7CF\uB7D1-\uB7EB\uB7ED-\uB807\uB809-\uB823\uB825-\uB83F\uB841-\uB85B\uB85D-\uB877\uB879-\uB893\uB895-\uB8AF\uB8B1-\uB8CB\uB8CD-\uB8E7\uB8E9-\uB903\uB905-\uB91F\uB921-\uB93B\uB93D-\uB957\uB959-\uB973\uB975-\uB98F\uB991-\uB9AB\uB9AD-\uB9C7\uB9C9-\uB9E3\uB9E5-\uB9FF\uBA01-\uBA1B\uBA1D-\uBA37\uBA39-\uBA53\uBA55-\uBA6F\uBA71-\uBA8B\uBA8D-\uBAA7\uBAA9-\uBAC3\uBAC5-\uBADF\uBAE1-\uBAFB\uBAFD-\uBB17\uBB19-\uBB33\uBB35-\uBB4F\uBB51-\uBB6B\uBB6D-\uBB87\uBB89-\uBBA3\uBBA5-\uBBBF\uBBC1-\uBBDB\uBBDD-\uBBF7\uBBF9-\uBC13\uBC15-\uBC2F\uBC31-\uBC4B\uBC4D-\uBC67\uBC69-\uBC83\uBC85-\uBC9F\uBCA1-\uBCBB\uBCBD-\uBCD7\uBCD9-\uBCF3\uBCF5-\uBD0F\uBD11-\uBD2B\uBD2D-\uBD47\uBD49-\uBD63\uBD65-\uBD7F\uBD81-\uBD9B\uBD9D-\uBDB7\uBDB9-\uBDD3\uBDD5-\uBDEF\uBDF1-\uBE0B\uBE0D-\uBE27\uBE29-\uBE43\uBE45-\uBE5F\uBE61-\uBE7B\uBE7D-\uBE97\uBE99-\uBEB3\uBEB5-\uBECF\uBED1-\uBEEB\uBEED-\uBF07\uBF09-\uBF23\uBF25-\uBF3F\uBF41-\uBF5B\uBF5D-\uBF77\uBF79-\uBF93\uBF95-\uBFAF\uBFB1-\uBFCB\uBFCD-\uBFE7\uBFE9-\uC003\uC005-\uC01F\uC021-\uC03B\uC03D-\uC057\uC059-\uC073\uC075-\uC08F\uC091-\uC0AB\uC0AD-\uC0C7\uC0C9-\uC0E3\uC0E5-\uC0FF\uC101-\uC11B\uC11D-\uC137\uC139-\uC153\uC155-\uC16F\uC171-\uC18B\uC18D-\uC1A7\uC1A9-\uC1C3\uC1C5-\uC1DF\uC1E1-\uC1FB\uC1FD-\uC217\uC219-\uC233\uC235-\uC24F\uC251-\uC26B\uC26D-\uC287\uC289-\uC2A3\uC2A5-\uC2BF\uC2C1-\uC2DB\uC2DD-\uC2F7\uC2F9-\uC313\uC315-\uC32F\uC331-\uC34B\uC34D-\uC367\uC369-\uC383\uC385-\uC39F\uC3A1-\uC3BB\uC3BD-\uC3D7\uC3D9-\uC3F3\uC3F5-\uC40F\uC411-\uC42B\uC42D-\uC447\uC449-\uC463\uC465-\uC47F\uC481-\uC49B\uC49D-\uC4B7\uC4B9-\uC4D3\uC4D5-\uC4EF\uC4F1-\uC50B\uC50D-\uC527\uC529-\uC543\uC545-\uC55F\uC561-\uC57B\uC57D-\uC597\uC599-\uC5B3\uC5B5-\uC5CF\uC5D1-\uC5EB\uC5ED-\uC607\uC609-\uC623\uC625-\uC63F\uC641-\uC65B\uC65D-\uC677\uC679-\uC693\uC695-\uC6AF\uC6B1-\uC6CB\uC6CD-\uC6E7\uC6E9-\uC703\uC705-\uC71F\uC721-\uC73B\uC73D-\uC757\uC759-\uC773\uC775-\uC78F\uC791-\uC7AB\uC7AD-\uC7C7\uC7C9-\uC7E3\uC7E5-\uC7FF\uC801-\uC81B\uC81D-\uC837\uC839-\uC853\uC855-\uC86F\uC871-\uC88B\uC88D-\uC8A7\uC8A9-\uC8C3\uC8C5-\uC8DF\uC8E1-\uC8FB\uC8FD-\uC917\uC919-\uC933\uC935-\uC94F\uC951-\uC96B\uC96D-\uC987\uC989-\uC9A3\uC9A5-\uC9BF\uC9C1-\uC9DB\uC9DD-\uC9F7\uC9F9-\uCA13\uCA15-\uCA2F\uCA31-\uCA4B\uCA4D-\uCA67\uCA69-\uCA83\uCA85-\uCA9F\uCAA1-\uCABB\uCABD-\uCAD7\uCAD9-\uCAF3\uCAF5-\uCB0F\uCB11-\uCB2B\uCB2D-\uCB47\uCB49-\uCB63\uCB65-\uCB7F\uCB81-\uCB9B\uCB9D-\uCBB7\uCBB9-\uCBD3\uCBD5-\uCBEF\uCBF1-\uCC0B\uCC0D-\uCC27\uCC29-\uCC43\uCC45-\uCC5F\uCC61-\uCC7B\uCC7D-\uCC97\uCC99-\uCCB3\uCCB5-\uCCCF\uCCD1-\uCCEB\uCCED-\uCD07\uCD09-\uCD23\uCD25-\uCD3F\uCD41-\uCD5B\uCD5D-\uCD77\uCD79-\uCD93\uCD95-\uCDAF\uCDB1-\uCDCB\uCDCD-\uCDE7\uCDE9-\uCE03\uCE05-\uCE1F\uCE21-\uCE3B\uCE3D-\uCE57\uCE59-\uCE73\uCE75-\uCE8F\uCE91-\uCEAB\uCEAD-\uCEC7\uCEC9-\uCEE3\uCEE5-\uCEFF\uCF01-\uCF1B\uCF1D-\uCF37\uCF39-\uCF53\uCF55-\uCF6F\uCF71-\uCF8B\uCF8D-\uCFA7\uCFA9-\uCFC3\uCFC5-\uCFDF\uCFE1-\uCFFB\uCFFD-\uD017\uD019-\uD033\uD035-\uD04F\uD051-\uD06B\uD06D-\uD087\uD089-\uD0A3\uD0A5-\uD0BF\uD0C1-\uD0DB\uD0DD-\uD0F7\uD0F9-\uD113\uD115-\uD12F\uD131-\uD14B\uD14D-\uD167\uD169-\uD183\uD185-\uD19F\uD1A1-\uD1BB\uD1BD-\uD1D7\uD1D9-\uD1F3\uD1F5-\uD20F\uD211-\uD22B\uD22D-\uD247\uD249-\uD263\uD265-\uD27F\uD281-\uD29B\uD29D-\uD2B7\uD2B9-\uD2D3\uD2D5-\uD2EF\uD2F1-\uD30B\uD30D-\uD327\uD329-\uD343\uD345-\uD35F\uD361-\uD37B\uD37D-\uD397\uD399-\uD3B3\uD3B5-\uD3CF\uD3D1-\uD3EB\uD3ED-\uD407\uD409-\uD423\uD425-\uD43F\uD441-\uD45B\uD45D-\uD477\uD479-\uD493\uD495-\uD4AF\uD4B1-\uD4CB\uD4CD-\uD4E7\uD4E9-\uD503\uD505-\uD51F\uD521-\uD53B\uD53D-\uD557\uD559-\uD573\uD575-\uD58F\uD591-\uD5AB\uD5AD-\uD5C7\uD5C9-\uD5E3\uD5E5-\uD5FF\uD601-\uD61B\uD61D-\uD637\uD639-\uD653\uD655-\uD66F\uD671-\uD68B\uD68D-\uD6A7\uD6A9-\uD6C3\uD6C5-\uD6DF\uD6E1-\uD6FB\uD6FD-\uD717\uD719-\uD733\uD735-\uD74F\uD751-\uD76B\uD76D-\uD787\uD789-\uD7A3]$/;
  767. var reExtPict = /^(?:[\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u2388\u23CF\u23E9-\u23F3\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE\u2600-\u2605\u2607-\u2612\u2614-\u2685\u2690-\u2705\u2708-\u2712\u2714\u2716\u271D\u2721\u2728\u2733\u2734\u2744\u2747\u274C\u274E\u2753-\u2755\u2757\u2763-\u2767\u2795-\u2797\u27A1\u27B0\u27BF\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55\u3030\u303D\u3297\u3299]|\uD83C[\uDC00-\uDCFF\uDD0D-\uDD0F\uDD2F\uDD6C-\uDD71\uDD7E\uDD7F\uDD8E\uDD91-\uDD9A\uDDAD-\uDDE5\uDE01-\uDE0F\uDE1A\uDE2F\uDE32-\uDE3A\uDE3C-\uDE3F\uDE49-\uDFFA]|\uD83D[\uDC00-\uDD3D\uDD46-\uDE4F\uDE80-\uDEFF\uDF74-\uDF7F\uDFD5-\uDFFF]|\uD83E[\uDC0C-\uDC0F\uDC48-\uDC4F\uDC5A-\uDC5F\uDC88-\uDC8F\uDCAE-\uDCFF\uDD0C-\uDD3A\uDD3C-\uDD45\uDD47-\uDEFF]|\uD83F[\uDC00-\uDFFD])$/;
  768. var getCodepointType = function getCodepointType(_char4, code) {
  769. var type = CodepointType.Any;
  770. if (_char4.search(reExtend) !== -1) {
  771. type |= CodepointType.Extend;
  772. }
  773. if (code === 0x200d) {
  774. type |= CodepointType.ZWJ;
  775. }
  776. if (code >= 0x1f1e6 && code <= 0x1f1ff) {
  777. type |= CodepointType.RI;
  778. }
  779. if (_char4.search(rePrepend) !== -1) {
  780. type |= CodepointType.Prepend;
  781. }
  782. if (_char4.search(reSpacingMark) !== -1) {
  783. type |= CodepointType.SpacingMark;
  784. }
  785. if (_char4.search(reL) !== -1) {
  786. type |= CodepointType.L;
  787. }
  788. if (_char4.search(reV) !== -1) {
  789. type |= CodepointType.V;
  790. }
  791. if (_char4.search(reT) !== -1) {
  792. type |= CodepointType.T;
  793. }
  794. if (_char4.search(reLV) !== -1) {
  795. type |= CodepointType.LV;
  796. }
  797. if (_char4.search(reLVT) !== -1) {
  798. type |= CodepointType.LVT;
  799. }
  800. if (_char4.search(reExtPict) !== -1) {
  801. type |= CodepointType.ExtPict;
  802. }
  803. return type;
  804. };
  805. function intersects(x, y) {
  806. return (x & y) !== 0;
  807. }
  808. var NonBoundaryPairs = [// GB6
  809. [CodepointType.L, CodepointType.L | CodepointType.V | CodepointType.LV | CodepointType.LVT], // GB7
  810. [CodepointType.LV | CodepointType.V, CodepointType.V | CodepointType.T], // GB8
  811. [CodepointType.LVT | CodepointType.T, CodepointType.T], // GB9
  812. [CodepointType.Any, CodepointType.Extend | CodepointType.ZWJ], // GB9a
  813. [CodepointType.Any, CodepointType.SpacingMark], // GB9b
  814. [CodepointType.Prepend, CodepointType.Any], // GB11
  815. [CodepointType.ZWJ, CodepointType.ExtPict], // GB12 and GB13
  816. [CodepointType.RI, CodepointType.RI]];
  817. function isBoundaryPair(left, right) {
  818. return NonBoundaryPairs.findIndex(function (r) {
  819. return intersects(left, r[0]) && intersects(right, r[1]);
  820. }) === -1;
  821. }
  822. var endingEmojiZWJ = /(?:[\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u2388\u23CF\u23E9-\u23F3\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE\u2600-\u2605\u2607-\u2612\u2614-\u2685\u2690-\u2705\u2708-\u2712\u2714\u2716\u271D\u2721\u2728\u2733\u2734\u2744\u2747\u274C\u274E\u2753-\u2755\u2757\u2763-\u2767\u2795-\u2797\u27A1\u27B0\u27BF\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55\u3030\u303D\u3297\u3299]|\uD83C[\uDC00-\uDCFF\uDD0D-\uDD0F\uDD2F\uDD6C-\uDD71\uDD7E\uDD7F\uDD8E\uDD91-\uDD9A\uDDAD-\uDDE5\uDE01-\uDE0F\uDE1A\uDE2F\uDE32-\uDE3A\uDE3C-\uDE3F\uDE49-\uDFFA]|\uD83D[\uDC00-\uDD3D\uDD46-\uDE4F\uDE80-\uDEFF\uDF74-\uDF7F\uDFD5-\uDFFF]|\uD83E[\uDC0C-\uDC0F\uDC48-\uDC4F\uDC5A-\uDC5F\uDC88-\uDC8F\uDCAE-\uDCFF\uDD0C-\uDD3A\uDD3C-\uDD45\uDD47-\uDEFF]|\uD83F[\uDC00-\uDFFD])(?:[\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u07FD\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08D3-\u08E1\u08E3-\u0902\u093A\u093C\u0941-\u0948\u094D\u0951-\u0957\u0962\u0963\u0981\u09BC\u09BE\u09C1-\u09C4\u09CD\u09D7\u09E2\u09E3\u09FE\u0A01\u0A02\u0A3C\u0A41\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81\u0A82\u0ABC\u0AC1-\u0AC5\u0AC7\u0AC8\u0ACD\u0AE2\u0AE3\u0AFA-\u0AFF\u0B01\u0B3C\u0B3E\u0B3F\u0B41-\u0B44\u0B4D\u0B55-\u0B57\u0B62\u0B63\u0B82\u0BBE\u0BC0\u0BCD\u0BD7\u0C00\u0C04\u0C3E-\u0C40\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C81\u0CBC\u0CBF\u0CC2\u0CC6\u0CCC\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D00\u0D01\u0D3B\u0D3C\u0D3E\u0D41-\u0D44\u0D4D\u0D57\u0D62\u0D63\u0D81\u0DCA\u0DCF\u0DD2-\u0DD4\u0DD6\u0DDF\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F71-\u0F7E\u0F80-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102D-\u1030\u1032-\u1037\u1039\u103A\u103D\u103E\u1058\u1059\u105E-\u1060\u1071-\u1074\u1082\u1085\u1086\u108D\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4\u17B5\u17B7-\u17BD\u17C6\u17C9-\u17D3\u17DD\u180B-\u180D\u1885\u1886\u18A9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193B\u1A17\u1A18\u1A1B\u1A56\u1A58-\u1A5E\u1A60\u1A62\u1A65-\u1A6C\u1A73-\u1A7C\u1A7F\u1AB0-\u1AC0\u1B00-\u1B03\u1B34-\u1B3A\u1B3C\u1B42\u1B6B-\u1B73\u1B80\u1B81\u1BA2-\u1BA5\u1BA8\u1BA9\u1BAB-\u1BAD\u1BE6\u1BE8\u1BE9\u1BED\u1BEF-\u1BF1\u1C2C-\u1C33\u1C36\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE0\u1CE2-\u1CE8\u1CED\u1CF4\u1CF8\u1CF9\u1DC0-\u1DF9\u1DFB-\u1DFF\u200C\u20D0-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA674-\uA67D\uA69E\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA825\uA826\uA82C\uA8C4\uA8C5\uA8E0-\uA8F1\uA8FF\uA926-\uA92D\uA947-\uA951\uA980-\uA982\uA9B3\uA9B6-\uA9B9\uA9BC\uA9BD\uA9E5\uAA29-\uAA2E\uAA31\uAA32\uAA35\uAA36\uAA43\uAA4C\uAA7C\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEC\uAAED\uAAF6\uABE5\uABE8\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE2F\uFF9E\uFF9F]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD803[\uDD24-\uDD27\uDEAB\uDEAC\uDF46-\uDF50]|\uD804[\uDC01\uDC38-\uDC46\uDC7F-\uDC81\uDCB3-\uDCB6\uDCB9\uDCBA\uDD00-\uDD02\uDD27-\uDD2B\uDD2D-\uDD34\uDD73\uDD80\uDD81\uDDB6-\uDDBE\uDDC9-\uDDCC\uDDCF\uDE2F-\uDE31\uDE34\uDE36\uDE37\uDE3E\uDEDF\uDEE3-\uDEEA\uDF00\uDF01\uDF3B\uDF3C\uDF3E\uDF40\uDF57\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC38-\uDC3F\uDC42-\uDC44\uDC46\uDC5E\uDCB0\uDCB3-\uDCB8\uDCBA\uDCBD\uDCBF\uDCC0\uDCC2\uDCC3\uDDAF\uDDB2-\uDDB5\uDDBC\uDDBD\uDDBF\uDDC0\uDDDC\uDDDD\uDE33-\uDE3A\uDE3D\uDE3F\uDE40\uDEAB\uDEAD\uDEB0-\uDEB5\uDEB7\uDF1D-\uDF1F\uDF22-\uDF25\uDF27-\uDF2B]|\uD806[\uDC2F-\uDC37\uDC39\uDC3A\uDD30\uDD3B\uDD3C\uDD3E\uDD43\uDDD4-\uDDD7\uDDDA\uDDDB\uDDE0\uDE01-\uDE0A\uDE33-\uDE38\uDE3B-\uDE3E\uDE47\uDE51-\uDE56\uDE59-\uDE5B\uDE8A-\uDE96\uDE98\uDE99]|\uD807[\uDC30-\uDC36\uDC38-\uDC3D\uDC3F\uDC92-\uDCA7\uDCAA-\uDCB0\uDCB2\uDCB3\uDCB5\uDCB6\uDD31-\uDD36\uDD3A\uDD3C\uDD3D\uDD3F-\uDD45\uDD47\uDD90\uDD91\uDD95\uDD97\uDEF3\uDEF4]|\uD81A[\uDEF0-\uDEF4\uDF30-\uDF36]|\uD81B[\uDF4F\uDF8F-\uDF92\uDFE4]|\uD82F[\uDC9D\uDC9E]|\uD834[\uDD65\uDD67-\uDD69\uDD6E-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD838[\uDC00-\uDC06\uDC08-\uDC18\uDC1B-\uDC21\uDC23\uDC24\uDC26-\uDC2A\uDD30-\uDD36\uDEEC-\uDEEF]|\uD83A[\uDCD0-\uDCD6\uDD44-\uDD4A]|\uD83C[\uDFFB-\uDFFF]|\uDB40[\uDC20-\uDC7F\uDD00-\uDDEF])*\u200D$/;
  823. var endsWithEmojiZWJ = function endsWithEmojiZWJ(str) {
  824. return str.search(endingEmojiZWJ) !== -1;
  825. };
  826. var endingRIs = /(?:\uD83C[\uDDE6-\uDDFF])+$/g;
  827. var endsWithOddNumberOfRIs = function endsWithOddNumberOfRIs(str) {
  828. var match = str.match(endingRIs);
  829. if (match === null) {
  830. return false;
  831. } else {
  832. // A RI is represented by a surrogate pair.
  833. var numRIs = match[0].length / 2;
  834. return numRIs % 2 === 1;
  835. }
  836. };
  837. /**
  838. * Shared the function with isElementType utility
  839. */
  840. var isElement = function isElement(value) {
  841. return isPlainObject.isPlainObject(value) && Node.isNodeList(value.children) && !Editor.isEditor(value);
  842. };
  843. var Element = {
  844. /**
  845. * Check if a value implements the 'Ancestor' interface.
  846. */
  847. isAncestor: function isAncestor(value) {
  848. return isPlainObject.isPlainObject(value) && Node.isNodeList(value.children);
  849. },
  850. /**
  851. * Check if a value implements the `Element` interface.
  852. */
  853. isElement: isElement,
  854. /**
  855. * Check if a value is an array of `Element` objects.
  856. */
  857. isElementList: function isElementList(value) {
  858. return Array.isArray(value) && value.every(function (val) {
  859. return Element.isElement(val);
  860. });
  861. },
  862. /**
  863. * Check if a set of props is a partial of Element.
  864. */
  865. isElementProps: function isElementProps(props) {
  866. return props.children !== undefined;
  867. },
  868. /**
  869. * Check if a value implements the `Element` interface and has elementKey with selected value.
  870. * Default it check to `type` key value
  871. */
  872. isElementType: function isElementType(value, elementVal) {
  873. var elementKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'type';
  874. return isElement(value) && value[elementKey] === elementVal;
  875. },
  876. /**
  877. * Check if an element matches set of properties.
  878. *
  879. * Note: this checks custom properties, and it does not ensure that any
  880. * children are equivalent.
  881. */
  882. matches: function matches(element, props) {
  883. for (var key in props) {
  884. if (key === 'children') {
  885. continue;
  886. }
  887. if (element[key] !== props[key]) {
  888. return false;
  889. }
  890. }
  891. return true;
  892. }
  893. };
  894. var _excluded$4 = ["text"],
  895. _excluded2$3 = ["text"];
  896. function ownKeys$8(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  897. function _objectSpread$8(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$8(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$8(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  898. function _createForOfIteratorHelper$5(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$5(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  899. function _unsupportedIterableToArray$5(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$5(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$5(o, minLen); }
  900. function _arrayLikeToArray$5(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  901. var IS_EDITOR_CACHE = new WeakMap();
  902. var Editor = {
  903. /**
  904. * Get the ancestor above a location in the document.
  905. */
  906. above: function above(editor) {
  907. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  908. var _options$voids = options.voids,
  909. voids = _options$voids === void 0 ? false : _options$voids,
  910. _options$mode = options.mode,
  911. mode = _options$mode === void 0 ? 'lowest' : _options$mode,
  912. _options$at = options.at,
  913. at = _options$at === void 0 ? editor.selection : _options$at,
  914. match = options.match;
  915. if (!at) {
  916. return;
  917. }
  918. var path = Editor.path(editor, at);
  919. var reverse = mode === 'lowest';
  920. var _iterator = _createForOfIteratorHelper$5(Editor.levels(editor, {
  921. at: path,
  922. voids: voids,
  923. match: match,
  924. reverse: reverse
  925. })),
  926. _step;
  927. try {
  928. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  929. var _step$value = _slicedToArray(_step.value, 2),
  930. n = _step$value[0],
  931. p = _step$value[1];
  932. if (!Text.isText(n) && !Path.equals(path, p)) {
  933. return [n, p];
  934. }
  935. }
  936. } catch (err) {
  937. _iterator.e(err);
  938. } finally {
  939. _iterator.f();
  940. }
  941. },
  942. /**
  943. * Add a custom property to the leaf text nodes in the current selection.
  944. *
  945. * If the selection is currently collapsed, the marks will be added to the
  946. * `editor.marks` property instead, and applied when text is inserted next.
  947. */
  948. addMark: function addMark(editor, key, value) {
  949. editor.addMark(key, value);
  950. },
  951. /**
  952. * Get the point after a location.
  953. */
  954. after: function after(editor, at) {
  955. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  956. var anchor = Editor.point(editor, at, {
  957. edge: 'end'
  958. });
  959. var focus = Editor.end(editor, []);
  960. var range = {
  961. anchor: anchor,
  962. focus: focus
  963. };
  964. var _options$distance = options.distance,
  965. distance = _options$distance === void 0 ? 1 : _options$distance;
  966. var d = 0;
  967. var target;
  968. var _iterator2 = _createForOfIteratorHelper$5(Editor.positions(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  969. at: range
  970. }))),
  971. _step2;
  972. try {
  973. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  974. var p = _step2.value;
  975. if (d > distance) {
  976. break;
  977. }
  978. if (d !== 0) {
  979. target = p;
  980. }
  981. d++;
  982. }
  983. } catch (err) {
  984. _iterator2.e(err);
  985. } finally {
  986. _iterator2.f();
  987. }
  988. return target;
  989. },
  990. /**
  991. * Get the point before a location.
  992. */
  993. before: function before(editor, at) {
  994. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  995. var anchor = Editor.start(editor, []);
  996. var focus = Editor.point(editor, at, {
  997. edge: 'start'
  998. });
  999. var range = {
  1000. anchor: anchor,
  1001. focus: focus
  1002. };
  1003. var _options$distance2 = options.distance,
  1004. distance = _options$distance2 === void 0 ? 1 : _options$distance2;
  1005. var d = 0;
  1006. var target;
  1007. var _iterator3 = _createForOfIteratorHelper$5(Editor.positions(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  1008. at: range,
  1009. reverse: true
  1010. }))),
  1011. _step3;
  1012. try {
  1013. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  1014. var p = _step3.value;
  1015. if (d > distance) {
  1016. break;
  1017. }
  1018. if (d !== 0) {
  1019. target = p;
  1020. }
  1021. d++;
  1022. }
  1023. } catch (err) {
  1024. _iterator3.e(err);
  1025. } finally {
  1026. _iterator3.f();
  1027. }
  1028. return target;
  1029. },
  1030. /**
  1031. * Delete content in the editor backward from the current selection.
  1032. */
  1033. deleteBackward: function deleteBackward(editor) {
  1034. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1035. var _options$unit = options.unit,
  1036. unit = _options$unit === void 0 ? 'character' : _options$unit;
  1037. editor.deleteBackward(unit);
  1038. },
  1039. /**
  1040. * Delete content in the editor forward from the current selection.
  1041. */
  1042. deleteForward: function deleteForward(editor) {
  1043. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1044. var _options$unit2 = options.unit,
  1045. unit = _options$unit2 === void 0 ? 'character' : _options$unit2;
  1046. editor.deleteForward(unit);
  1047. },
  1048. /**
  1049. * Delete the content in the current selection.
  1050. */
  1051. deleteFragment: function deleteFragment(editor) {
  1052. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1053. var _options$direction = options.direction,
  1054. direction = _options$direction === void 0 ? 'forward' : _options$direction;
  1055. editor.deleteFragment(direction);
  1056. },
  1057. /**
  1058. * Get the start and end points of a location.
  1059. */
  1060. edges: function edges(editor, at) {
  1061. return [Editor.start(editor, at), Editor.end(editor, at)];
  1062. },
  1063. /**
  1064. * Get the end point of a location.
  1065. */
  1066. end: function end(editor, at) {
  1067. return Editor.point(editor, at, {
  1068. edge: 'end'
  1069. });
  1070. },
  1071. /**
  1072. * Get the first node at a location.
  1073. */
  1074. first: function first(editor, at) {
  1075. var path = Editor.path(editor, at, {
  1076. edge: 'start'
  1077. });
  1078. return Editor.node(editor, path);
  1079. },
  1080. /**
  1081. * Get the fragment at a location.
  1082. */
  1083. fragment: function fragment(editor, at) {
  1084. var range = Editor.range(editor, at);
  1085. var fragment = Node.fragment(editor, range);
  1086. return fragment;
  1087. },
  1088. /**
  1089. * Check if a node has block children.
  1090. */
  1091. hasBlocks: function hasBlocks(editor, element) {
  1092. return element.children.some(function (n) {
  1093. return Editor.isBlock(editor, n);
  1094. });
  1095. },
  1096. /**
  1097. * Check if a node has inline and text children.
  1098. */
  1099. hasInlines: function hasInlines(editor, element) {
  1100. return element.children.some(function (n) {
  1101. return Text.isText(n) || Editor.isInline(editor, n);
  1102. });
  1103. },
  1104. /**
  1105. * Check if a node has text children.
  1106. */
  1107. hasTexts: function hasTexts(editor, element) {
  1108. return element.children.every(function (n) {
  1109. return Text.isText(n);
  1110. });
  1111. },
  1112. /**
  1113. * Insert a block break at the current selection.
  1114. *
  1115. * If the selection is currently expanded, it will be deleted first.
  1116. */
  1117. insertBreak: function insertBreak(editor) {
  1118. editor.insertBreak();
  1119. },
  1120. /**
  1121. * Insert a fragment at the current selection.
  1122. *
  1123. * If the selection is currently expanded, it will be deleted first.
  1124. */
  1125. insertFragment: function insertFragment(editor, fragment) {
  1126. editor.insertFragment(fragment);
  1127. },
  1128. /**
  1129. * Insert a node at the current selection.
  1130. *
  1131. * If the selection is currently expanded, it will be deleted first.
  1132. */
  1133. insertNode: function insertNode(editor, node) {
  1134. editor.insertNode(node);
  1135. },
  1136. /**
  1137. * Insert text at the current selection.
  1138. *
  1139. * If the selection is currently expanded, it will be deleted first.
  1140. */
  1141. insertText: function insertText(editor, text) {
  1142. editor.insertText(text);
  1143. },
  1144. /**
  1145. * Check if a value is a block `Element` object.
  1146. */
  1147. isBlock: function isBlock(editor, value) {
  1148. return Element.isElement(value) && !editor.isInline(value);
  1149. },
  1150. /**
  1151. * Check if a value is an `Editor` object.
  1152. */
  1153. isEditor: function isEditor(value) {
  1154. if (!isPlainObject.isPlainObject(value)) return false;
  1155. var cachedIsEditor = IS_EDITOR_CACHE.get(value);
  1156. if (cachedIsEditor !== undefined) {
  1157. return cachedIsEditor;
  1158. }
  1159. var isEditor = typeof value.addMark === 'function' && typeof value.apply === 'function' && typeof value.deleteBackward === 'function' && typeof value.deleteForward === 'function' && typeof value.deleteFragment === 'function' && typeof value.insertBreak === 'function' && typeof value.insertFragment === 'function' && typeof value.insertNode === 'function' && typeof value.insertText === 'function' && typeof value.isInline === 'function' && typeof value.isVoid === 'function' && typeof value.normalizeNode === 'function' && typeof value.onChange === 'function' && typeof value.removeMark === 'function' && (value.marks === null || isPlainObject.isPlainObject(value.marks)) && (value.selection === null || Range.isRange(value.selection)) && Node.isNodeList(value.children) && Operation.isOperationList(value.operations);
  1160. IS_EDITOR_CACHE.set(value, isEditor);
  1161. return isEditor;
  1162. },
  1163. /**
  1164. * Check if a point is the end point of a location.
  1165. */
  1166. isEnd: function isEnd(editor, point, at) {
  1167. var end = Editor.end(editor, at);
  1168. return Point.equals(point, end);
  1169. },
  1170. /**
  1171. * Check if a point is an edge of a location.
  1172. */
  1173. isEdge: function isEdge(editor, point, at) {
  1174. return Editor.isStart(editor, point, at) || Editor.isEnd(editor, point, at);
  1175. },
  1176. /**
  1177. * Check if an element is empty, accounting for void nodes.
  1178. */
  1179. isEmpty: function isEmpty(editor, element) {
  1180. var children = element.children;
  1181. var _children = _slicedToArray(children, 1),
  1182. first = _children[0];
  1183. return children.length === 0 || children.length === 1 && Text.isText(first) && first.text === '' && !editor.isVoid(element);
  1184. },
  1185. /**
  1186. * Check if a value is an inline `Element` object.
  1187. */
  1188. isInline: function isInline(editor, value) {
  1189. return Element.isElement(value) && editor.isInline(value);
  1190. },
  1191. /**
  1192. * Check if the editor is currently normalizing after each operation.
  1193. */
  1194. isNormalizing: function isNormalizing(editor) {
  1195. var isNormalizing = NORMALIZING.get(editor);
  1196. return isNormalizing === undefined ? true : isNormalizing;
  1197. },
  1198. /**
  1199. * Check if a point is the start point of a location.
  1200. */
  1201. isStart: function isStart(editor, point, at) {
  1202. // PERF: If the offset isn't `0` we know it's not the start.
  1203. if (point.offset !== 0) {
  1204. return false;
  1205. }
  1206. var start = Editor.start(editor, at);
  1207. return Point.equals(point, start);
  1208. },
  1209. /**
  1210. * Check if a value is a void `Element` object.
  1211. */
  1212. isVoid: function isVoid(editor, value) {
  1213. return Element.isElement(value) && editor.isVoid(value);
  1214. },
  1215. /**
  1216. * Get the last node at a location.
  1217. */
  1218. last: function last(editor, at) {
  1219. var path = Editor.path(editor, at, {
  1220. edge: 'end'
  1221. });
  1222. return Editor.node(editor, path);
  1223. },
  1224. /**
  1225. * Get the leaf text node at a location.
  1226. */
  1227. leaf: function leaf(editor, at) {
  1228. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1229. var path = Editor.path(editor, at, options);
  1230. var node = Node.leaf(editor, path);
  1231. return [node, path];
  1232. },
  1233. /**
  1234. * Iterate through all of the levels at a location.
  1235. */
  1236. levels: function* levels(editor) {
  1237. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1238. var _options$at2 = options.at,
  1239. at = _options$at2 === void 0 ? editor.selection : _options$at2,
  1240. _options$reverse = options.reverse,
  1241. reverse = _options$reverse === void 0 ? false : _options$reverse,
  1242. _options$voids2 = options.voids,
  1243. voids = _options$voids2 === void 0 ? false : _options$voids2;
  1244. var match = options.match;
  1245. if (match == null) {
  1246. match = function match() {
  1247. return true;
  1248. };
  1249. }
  1250. if (!at) {
  1251. return;
  1252. }
  1253. var levels = [];
  1254. var path = Editor.path(editor, at);
  1255. var _iterator4 = _createForOfIteratorHelper$5(Node.levels(editor, path)),
  1256. _step4;
  1257. try {
  1258. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  1259. var _step4$value = _slicedToArray(_step4.value, 2),
  1260. n = _step4$value[0],
  1261. p = _step4$value[1];
  1262. if (!match(n, p)) {
  1263. continue;
  1264. }
  1265. levels.push([n, p]);
  1266. if (!voids && Editor.isVoid(editor, n)) {
  1267. break;
  1268. }
  1269. }
  1270. } catch (err) {
  1271. _iterator4.e(err);
  1272. } finally {
  1273. _iterator4.f();
  1274. }
  1275. if (reverse) {
  1276. levels.reverse();
  1277. }
  1278. yield* levels;
  1279. },
  1280. /**
  1281. * Get the marks that would be added to text at the current selection.
  1282. */
  1283. marks: function marks(editor) {
  1284. var marks = editor.marks,
  1285. selection = editor.selection;
  1286. if (!selection) {
  1287. return null;
  1288. }
  1289. if (marks) {
  1290. return marks;
  1291. }
  1292. if (Range.isExpanded(selection)) {
  1293. var _Editor$nodes = Editor.nodes(editor, {
  1294. match: Text.isText
  1295. }),
  1296. _Editor$nodes2 = _slicedToArray(_Editor$nodes, 1),
  1297. match = _Editor$nodes2[0];
  1298. if (match) {
  1299. var _match = _slicedToArray(match, 1),
  1300. _node = _match[0];
  1301. _node.text;
  1302. var _rest = _objectWithoutProperties(_node, _excluded$4);
  1303. return _rest;
  1304. } else {
  1305. return {};
  1306. }
  1307. }
  1308. var anchor = selection.anchor;
  1309. var path = anchor.path;
  1310. var _Editor$leaf = Editor.leaf(editor, path),
  1311. _Editor$leaf2 = _slicedToArray(_Editor$leaf, 1),
  1312. node = _Editor$leaf2[0];
  1313. if (anchor.offset === 0) {
  1314. var prev = Editor.previous(editor, {
  1315. at: path,
  1316. match: Text.isText
  1317. });
  1318. var block = Editor.above(editor, {
  1319. match: function match(n) {
  1320. return Editor.isBlock(editor, n);
  1321. }
  1322. });
  1323. if (prev && block) {
  1324. var _prev = _slicedToArray(prev, 2),
  1325. prevNode = _prev[0],
  1326. prevPath = _prev[1];
  1327. var _block = _slicedToArray(block, 2),
  1328. blockPath = _block[1];
  1329. if (Path.isAncestor(blockPath, prevPath)) {
  1330. node = prevNode;
  1331. }
  1332. }
  1333. }
  1334. var _node2 = node;
  1335. _node2.text;
  1336. var rest = _objectWithoutProperties(_node2, _excluded2$3);
  1337. return rest;
  1338. },
  1339. /**
  1340. * Get the matching node in the branch of the document after a location.
  1341. */
  1342. next: function next(editor) {
  1343. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1344. var _options$mode2 = options.mode,
  1345. mode = _options$mode2 === void 0 ? 'lowest' : _options$mode2,
  1346. _options$voids3 = options.voids,
  1347. voids = _options$voids3 === void 0 ? false : _options$voids3;
  1348. var match = options.match,
  1349. _options$at3 = options.at,
  1350. at = _options$at3 === void 0 ? editor.selection : _options$at3;
  1351. if (!at) {
  1352. return;
  1353. }
  1354. var pointAfterLocation = Editor.after(editor, at, {
  1355. voids: voids
  1356. });
  1357. if (!pointAfterLocation) return;
  1358. var _Editor$last = Editor.last(editor, []),
  1359. _Editor$last2 = _slicedToArray(_Editor$last, 2),
  1360. to = _Editor$last2[1];
  1361. var span = [pointAfterLocation.path, to];
  1362. if (Path.isPath(at) && at.length === 0) {
  1363. throw new Error("Cannot get the next node from the root node!");
  1364. }
  1365. if (match == null) {
  1366. if (Path.isPath(at)) {
  1367. var _Editor$parent = Editor.parent(editor, at),
  1368. _Editor$parent2 = _slicedToArray(_Editor$parent, 1),
  1369. parent = _Editor$parent2[0];
  1370. match = function match(n) {
  1371. return parent.children.includes(n);
  1372. };
  1373. } else {
  1374. match = function match() {
  1375. return true;
  1376. };
  1377. }
  1378. }
  1379. var _Editor$nodes3 = Editor.nodes(editor, {
  1380. at: span,
  1381. match: match,
  1382. mode: mode,
  1383. voids: voids
  1384. }),
  1385. _Editor$nodes4 = _slicedToArray(_Editor$nodes3, 1),
  1386. next = _Editor$nodes4[0];
  1387. return next;
  1388. },
  1389. /**
  1390. * Get the node at a location.
  1391. */
  1392. node: function node(editor, at) {
  1393. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1394. var path = Editor.path(editor, at, options);
  1395. var node = Node.get(editor, path);
  1396. return [node, path];
  1397. },
  1398. /**
  1399. * Iterate through all of the nodes in the Editor.
  1400. */
  1401. nodes: function* nodes(editor) {
  1402. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1403. var _options$at4 = options.at,
  1404. at = _options$at4 === void 0 ? editor.selection : _options$at4,
  1405. _options$mode3 = options.mode,
  1406. mode = _options$mode3 === void 0 ? 'all' : _options$mode3,
  1407. _options$universal = options.universal,
  1408. universal = _options$universal === void 0 ? false : _options$universal,
  1409. _options$reverse2 = options.reverse,
  1410. reverse = _options$reverse2 === void 0 ? false : _options$reverse2,
  1411. _options$voids4 = options.voids,
  1412. voids = _options$voids4 === void 0 ? false : _options$voids4;
  1413. var match = options.match;
  1414. if (!match) {
  1415. match = function match() {
  1416. return true;
  1417. };
  1418. }
  1419. if (!at) {
  1420. return;
  1421. }
  1422. var from;
  1423. var to;
  1424. if (Span.isSpan(at)) {
  1425. from = at[0];
  1426. to = at[1];
  1427. } else {
  1428. var first = Editor.path(editor, at, {
  1429. edge: 'start'
  1430. });
  1431. var last = Editor.path(editor, at, {
  1432. edge: 'end'
  1433. });
  1434. from = reverse ? last : first;
  1435. to = reverse ? first : last;
  1436. }
  1437. var nodeEntries = Node.nodes(editor, {
  1438. reverse: reverse,
  1439. from: from,
  1440. to: to,
  1441. pass: function pass(_ref) {
  1442. var _ref2 = _slicedToArray(_ref, 1),
  1443. n = _ref2[0];
  1444. return voids ? false : Editor.isVoid(editor, n);
  1445. }
  1446. });
  1447. var matches = [];
  1448. var hit;
  1449. var _iterator5 = _createForOfIteratorHelper$5(nodeEntries),
  1450. _step5;
  1451. try {
  1452. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  1453. var _step5$value = _slicedToArray(_step5.value, 2),
  1454. node = _step5$value[0],
  1455. path = _step5$value[1];
  1456. var isLower = hit && Path.compare(path, hit[1]) === 0; // In highest mode any node lower than the last hit is not a match.
  1457. if (mode === 'highest' && isLower) {
  1458. continue;
  1459. }
  1460. if (!match(node, path)) {
  1461. // If we've arrived at a leaf text node that is not lower than the last
  1462. // hit, then we've found a branch that doesn't include a match, which
  1463. // means the match is not universal.
  1464. if (universal && !isLower && Text.isText(node)) {
  1465. return;
  1466. } else {
  1467. continue;
  1468. }
  1469. } // If there's a match and it's lower than the last, update the hit.
  1470. if (mode === 'lowest' && isLower) {
  1471. hit = [node, path];
  1472. continue;
  1473. } // In lowest mode we emit the last hit, once it's guaranteed lowest.
  1474. var emit = mode === 'lowest' ? hit : [node, path];
  1475. if (emit) {
  1476. if (universal) {
  1477. matches.push(emit);
  1478. } else {
  1479. yield emit;
  1480. }
  1481. }
  1482. hit = [node, path];
  1483. } // Since lowest is always emitting one behind, catch up at the end.
  1484. } catch (err) {
  1485. _iterator5.e(err);
  1486. } finally {
  1487. _iterator5.f();
  1488. }
  1489. if (mode === 'lowest' && hit) {
  1490. if (universal) {
  1491. matches.push(hit);
  1492. } else {
  1493. yield hit;
  1494. }
  1495. } // Universal defers to ensure that the match occurs in every branch, so we
  1496. // yield all of the matches after iterating.
  1497. if (universal) {
  1498. yield* matches;
  1499. }
  1500. },
  1501. /**
  1502. * Normalize any dirty objects in the editor.
  1503. */
  1504. normalize: function normalize(editor) {
  1505. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1506. var _options$force = options.force,
  1507. force = _options$force === void 0 ? false : _options$force;
  1508. var getDirtyPaths = function getDirtyPaths(editor) {
  1509. return DIRTY_PATHS.get(editor) || [];
  1510. };
  1511. var getDirtyPathKeys = function getDirtyPathKeys(editor) {
  1512. return DIRTY_PATH_KEYS.get(editor) || new Set();
  1513. };
  1514. var popDirtyPath = function popDirtyPath(editor) {
  1515. var path = getDirtyPaths(editor).pop();
  1516. var key = path.join(',');
  1517. getDirtyPathKeys(editor)["delete"](key);
  1518. return path;
  1519. };
  1520. if (!Editor.isNormalizing(editor)) {
  1521. return;
  1522. }
  1523. if (force) {
  1524. var allPaths = Array.from(Node.nodes(editor), function (_ref3) {
  1525. var _ref4 = _slicedToArray(_ref3, 2),
  1526. p = _ref4[1];
  1527. return p;
  1528. });
  1529. var allPathKeys = new Set(allPaths.map(function (p) {
  1530. return p.join(',');
  1531. }));
  1532. DIRTY_PATHS.set(editor, allPaths);
  1533. DIRTY_PATH_KEYS.set(editor, allPathKeys);
  1534. }
  1535. if (getDirtyPaths(editor).length === 0) {
  1536. return;
  1537. }
  1538. Editor.withoutNormalizing(editor, function () {
  1539. /*
  1540. Fix dirty elements with no children.
  1541. editor.normalizeNode() does fix this, but some normalization fixes also require it to work.
  1542. Running an initial pass avoids the catch-22 race condition.
  1543. */
  1544. var _iterator6 = _createForOfIteratorHelper$5(getDirtyPaths(editor)),
  1545. _step6;
  1546. try {
  1547. for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
  1548. var _dirtyPath = _step6.value;
  1549. if (Node.has(editor, _dirtyPath)) {
  1550. var _entry = Editor.node(editor, _dirtyPath);
  1551. var _entry2 = _slicedToArray(_entry, 2),
  1552. node = _entry2[0],
  1553. _ = _entry2[1];
  1554. /*
  1555. The default normalizer inserts an empty text node in this scenario, but it can be customised.
  1556. So there is some risk here.
  1557. As long as the normalizer only inserts child nodes for this case it is safe to do in any order;
  1558. by definition adding children to an empty node can't cause other paths to change.
  1559. */
  1560. if (Element.isElement(node) && node.children.length === 0) {
  1561. editor.normalizeNode(_entry);
  1562. }
  1563. }
  1564. }
  1565. } catch (err) {
  1566. _iterator6.e(err);
  1567. } finally {
  1568. _iterator6.f();
  1569. }
  1570. var max = getDirtyPaths(editor).length * 42; // HACK: better way?
  1571. var m = 0;
  1572. while (getDirtyPaths(editor).length !== 0) {
  1573. if (m > max) {
  1574. throw new Error("\n Could not completely normalize the editor after ".concat(max, " iterations! This is usually due to incorrect normalization logic that leaves a node in an invalid state.\n "));
  1575. }
  1576. var dirtyPath = popDirtyPath(editor); // If the node doesn't exist in the tree, it does not need to be normalized.
  1577. if (Node.has(editor, dirtyPath)) {
  1578. var entry = Editor.node(editor, dirtyPath);
  1579. editor.normalizeNode(entry);
  1580. }
  1581. m++;
  1582. }
  1583. });
  1584. },
  1585. /**
  1586. * Get the parent node of a location.
  1587. */
  1588. parent: function parent(editor, at) {
  1589. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1590. var path = Editor.path(editor, at, options);
  1591. var parentPath = Path.parent(path);
  1592. var entry = Editor.node(editor, parentPath);
  1593. return entry;
  1594. },
  1595. /**
  1596. * Get the path of a location.
  1597. */
  1598. path: function path(editor, at) {
  1599. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1600. var depth = options.depth,
  1601. edge = options.edge;
  1602. if (Path.isPath(at)) {
  1603. if (edge === 'start') {
  1604. var _Node$first = Node.first(editor, at),
  1605. _Node$first2 = _slicedToArray(_Node$first, 2),
  1606. firstPath = _Node$first2[1];
  1607. at = firstPath;
  1608. } else if (edge === 'end') {
  1609. var _Node$last = Node.last(editor, at),
  1610. _Node$last2 = _slicedToArray(_Node$last, 2),
  1611. lastPath = _Node$last2[1];
  1612. at = lastPath;
  1613. }
  1614. }
  1615. if (Range.isRange(at)) {
  1616. if (edge === 'start') {
  1617. at = Range.start(at);
  1618. } else if (edge === 'end') {
  1619. at = Range.end(at);
  1620. } else {
  1621. at = Path.common(at.anchor.path, at.focus.path);
  1622. }
  1623. }
  1624. if (Point.isPoint(at)) {
  1625. at = at.path;
  1626. }
  1627. if (depth != null) {
  1628. at = at.slice(0, depth);
  1629. }
  1630. return at;
  1631. },
  1632. hasPath: function hasPath(editor, path) {
  1633. return Node.has(editor, path);
  1634. },
  1635. /**
  1636. * Create a mutable ref for a `Path` object, which will stay in sync as new
  1637. * operations are applied to the editor.
  1638. */
  1639. pathRef: function pathRef(editor, path) {
  1640. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1641. var _options$affinity = options.affinity,
  1642. affinity = _options$affinity === void 0 ? 'forward' : _options$affinity;
  1643. var ref = {
  1644. current: path,
  1645. affinity: affinity,
  1646. unref: function unref() {
  1647. var current = ref.current;
  1648. var pathRefs = Editor.pathRefs(editor);
  1649. pathRefs["delete"](ref);
  1650. ref.current = null;
  1651. return current;
  1652. }
  1653. };
  1654. var refs = Editor.pathRefs(editor);
  1655. refs.add(ref);
  1656. return ref;
  1657. },
  1658. /**
  1659. * Get the set of currently tracked path refs of the editor.
  1660. */
  1661. pathRefs: function pathRefs(editor) {
  1662. var refs = PATH_REFS.get(editor);
  1663. if (!refs) {
  1664. refs = new Set();
  1665. PATH_REFS.set(editor, refs);
  1666. }
  1667. return refs;
  1668. },
  1669. /**
  1670. * Get the start or end point of a location.
  1671. */
  1672. point: function point(editor, at) {
  1673. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1674. var _options$edge = options.edge,
  1675. edge = _options$edge === void 0 ? 'start' : _options$edge;
  1676. if (Path.isPath(at)) {
  1677. var path;
  1678. if (edge === 'end') {
  1679. var _Node$last3 = Node.last(editor, at),
  1680. _Node$last4 = _slicedToArray(_Node$last3, 2),
  1681. lastPath = _Node$last4[1];
  1682. path = lastPath;
  1683. } else {
  1684. var _Node$first3 = Node.first(editor, at),
  1685. _Node$first4 = _slicedToArray(_Node$first3, 2),
  1686. firstPath = _Node$first4[1];
  1687. path = firstPath;
  1688. }
  1689. var node = Node.get(editor, path);
  1690. if (!Text.isText(node)) {
  1691. throw new Error("Cannot get the ".concat(edge, " point in the node at path [").concat(at, "] because it has no ").concat(edge, " text node."));
  1692. }
  1693. return {
  1694. path: path,
  1695. offset: edge === 'end' ? node.text.length : 0
  1696. };
  1697. }
  1698. if (Range.isRange(at)) {
  1699. var _Range$edges = Range.edges(at),
  1700. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  1701. start = _Range$edges2[0],
  1702. end = _Range$edges2[1];
  1703. return edge === 'start' ? start : end;
  1704. }
  1705. return at;
  1706. },
  1707. /**
  1708. * Create a mutable ref for a `Point` object, which will stay in sync as new
  1709. * operations are applied to the editor.
  1710. */
  1711. pointRef: function pointRef(editor, point) {
  1712. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1713. var _options$affinity2 = options.affinity,
  1714. affinity = _options$affinity2 === void 0 ? 'forward' : _options$affinity2;
  1715. var ref = {
  1716. current: point,
  1717. affinity: affinity,
  1718. unref: function unref() {
  1719. var current = ref.current;
  1720. var pointRefs = Editor.pointRefs(editor);
  1721. pointRefs["delete"](ref);
  1722. ref.current = null;
  1723. return current;
  1724. }
  1725. };
  1726. var refs = Editor.pointRefs(editor);
  1727. refs.add(ref);
  1728. return ref;
  1729. },
  1730. /**
  1731. * Get the set of currently tracked point refs of the editor.
  1732. */
  1733. pointRefs: function pointRefs(editor) {
  1734. var refs = POINT_REFS.get(editor);
  1735. if (!refs) {
  1736. refs = new Set();
  1737. POINT_REFS.set(editor, refs);
  1738. }
  1739. return refs;
  1740. },
  1741. /**
  1742. * Return all the positions in `at` range where a `Point` can be placed.
  1743. *
  1744. * By default, moves forward by individual offsets at a time, but
  1745. * the `unit` option can be used to to move by character, word, line, or block.
  1746. *
  1747. * The `reverse` option can be used to change iteration direction.
  1748. *
  1749. * Note: By default void nodes are treated as a single point and iteration
  1750. * will not happen inside their content unless you pass in true for the
  1751. * `voids` option, then iteration will occur.
  1752. */
  1753. positions: function* positions(editor) {
  1754. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1755. var _options$at5 = options.at,
  1756. at = _options$at5 === void 0 ? editor.selection : _options$at5,
  1757. _options$unit3 = options.unit,
  1758. unit = _options$unit3 === void 0 ? 'offset' : _options$unit3,
  1759. _options$reverse3 = options.reverse,
  1760. reverse = _options$reverse3 === void 0 ? false : _options$reverse3,
  1761. _options$voids5 = options.voids,
  1762. voids = _options$voids5 === void 0 ? false : _options$voids5;
  1763. if (!at) {
  1764. return;
  1765. }
  1766. /**
  1767. * Algorithm notes:
  1768. *
  1769. * Each step `distance` is dynamic depending on the underlying text
  1770. * and the `unit` specified. Each step, e.g., a line or word, may
  1771. * span multiple text nodes, so we iterate through the text both on
  1772. * two levels in step-sync:
  1773. *
  1774. * `leafText` stores the text on a text leaf level, and is advanced
  1775. * through using the counters `leafTextOffset` and `leafTextRemaining`.
  1776. *
  1777. * `blockText` stores the text on a block level, and is shortened
  1778. * by `distance` every time it is advanced.
  1779. *
  1780. * We only maintain a window of one blockText and one leafText because
  1781. * a block node always appears before all of its leaf nodes.
  1782. */
  1783. var range = Editor.range(editor, at);
  1784. var _Range$edges3 = Range.edges(range),
  1785. _Range$edges4 = _slicedToArray(_Range$edges3, 2),
  1786. start = _Range$edges4[0],
  1787. end = _Range$edges4[1];
  1788. var first = reverse ? end : start;
  1789. var isNewBlock = false;
  1790. var blockText = '';
  1791. var distance = 0; // Distance for leafText to catch up to blockText.
  1792. var leafTextRemaining = 0;
  1793. var leafTextOffset = 0; // Iterate through all nodes in range, grabbing entire textual content
  1794. // of block nodes in blockText, and text nodes in leafText.
  1795. // Exploits the fact that nodes are sequenced in such a way that we first
  1796. // encounter the block node, then all of its text nodes, so when iterating
  1797. // through the blockText and leafText we just need to remember a window of
  1798. // one block node and leaf node, respectively.
  1799. var _iterator7 = _createForOfIteratorHelper$5(Editor.nodes(editor, {
  1800. at: at,
  1801. reverse: reverse,
  1802. voids: voids
  1803. })),
  1804. _step7;
  1805. try {
  1806. for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
  1807. var _step7$value = _slicedToArray(_step7.value, 2),
  1808. node = _step7$value[0],
  1809. path = _step7$value[1];
  1810. /*
  1811. * ELEMENT NODE - Yield position(s) for voids, collect blockText for blocks
  1812. */
  1813. if (Element.isElement(node)) {
  1814. // Void nodes are a special case, so by default we will always
  1815. // yield their first point. If the `voids` option is set to true,
  1816. // then we will iterate over their content.
  1817. if (!voids && editor.isVoid(node)) {
  1818. yield Editor.start(editor, path);
  1819. continue;
  1820. } // Inline element nodes are ignored as they don't themselves
  1821. // contribute to `blockText` or `leafText` - their parent and
  1822. // children do.
  1823. if (editor.isInline(node)) continue; // Block element node - set `blockText` to its text content.
  1824. if (Editor.hasInlines(editor, node)) {
  1825. // We always exhaust block nodes before encountering a new one:
  1826. // console.assert(blockText === '',
  1827. // `blockText='${blockText}' - `+
  1828. // `not exhausted before new block node`, path)
  1829. // Ensure range considered is capped to `range`, in the
  1830. // start/end edge cases where block extends beyond range.
  1831. // Equivalent to this, but presumably more performant:
  1832. // blockRange = Editor.range(editor, ...Editor.edges(editor, path))
  1833. // blockRange = Range.intersection(range, blockRange) // intersect
  1834. // blockText = Editor.string(editor, blockRange, { voids })
  1835. var e = Path.isAncestor(path, end.path) ? end : Editor.end(editor, path);
  1836. var s = Path.isAncestor(path, start.path) ? start : Editor.start(editor, path);
  1837. blockText = Editor.string(editor, {
  1838. anchor: s,
  1839. focus: e
  1840. }, {
  1841. voids: voids
  1842. });
  1843. isNewBlock = true;
  1844. }
  1845. }
  1846. /*
  1847. * TEXT LEAF NODE - Iterate through text content, yielding
  1848. * positions every `distance` offset according to `unit`.
  1849. */
  1850. if (Text.isText(node)) {
  1851. var isFirst = Path.equals(path, first.path); // Proof that we always exhaust text nodes before encountering a new one:
  1852. // console.assert(leafTextRemaining <= 0,
  1853. // `leafTextRemaining=${leafTextRemaining} - `+
  1854. // `not exhausted before new leaf text node`, path)
  1855. // Reset `leafText` counters for new text node.
  1856. if (isFirst) {
  1857. leafTextRemaining = reverse ? first.offset : node.text.length - first.offset;
  1858. leafTextOffset = first.offset; // Works for reverse too.
  1859. } else {
  1860. leafTextRemaining = node.text.length;
  1861. leafTextOffset = reverse ? leafTextRemaining : 0;
  1862. } // Yield position at the start of node (potentially).
  1863. if (isFirst || isNewBlock || unit === 'offset') {
  1864. yield {
  1865. path: path,
  1866. offset: leafTextOffset
  1867. };
  1868. isNewBlock = false;
  1869. } // Yield positions every (dynamically calculated) `distance` offset.
  1870. while (true) {
  1871. // If `leafText` has caught up with `blockText` (distance=0),
  1872. // and if blockText is exhausted, break to get another block node,
  1873. // otherwise advance blockText forward by the new `distance`.
  1874. if (distance === 0) {
  1875. if (blockText === '') break;
  1876. distance = calcDistance(blockText, unit, reverse); // Split the string at the previously found distance and use the
  1877. // remaining string for the next iteration.
  1878. blockText = splitByCharacterDistance(blockText, distance, reverse)[1];
  1879. } // Advance `leafText` by the current `distance`.
  1880. leafTextOffset = reverse ? leafTextOffset - distance : leafTextOffset + distance;
  1881. leafTextRemaining = leafTextRemaining - distance; // If `leafText` is exhausted, break to get a new leaf node
  1882. // and set distance to the overflow amount, so we'll (maybe)
  1883. // catch up to blockText in the next leaf text node.
  1884. if (leafTextRemaining < 0) {
  1885. distance = -leafTextRemaining;
  1886. break;
  1887. } // Successfully walked `distance` offsets through `leafText`
  1888. // to catch up with `blockText`, so we can reset `distance`
  1889. // and yield this position in this node.
  1890. distance = 0;
  1891. yield {
  1892. path: path,
  1893. offset: leafTextOffset
  1894. };
  1895. }
  1896. }
  1897. } // Proof that upon completion, we've exahusted both leaf and block text:
  1898. // console.assert(leafTextRemaining <= 0, "leafText wasn't exhausted")
  1899. // console.assert(blockText === '', "blockText wasn't exhausted")
  1900. // Helper:
  1901. // Return the distance in offsets for a step of size `unit` on given string.
  1902. } catch (err) {
  1903. _iterator7.e(err);
  1904. } finally {
  1905. _iterator7.f();
  1906. }
  1907. function calcDistance(text, unit, reverse) {
  1908. if (unit === 'character') {
  1909. return getCharacterDistance(text, reverse);
  1910. } else if (unit === 'word') {
  1911. return getWordDistance(text, reverse);
  1912. } else if (unit === 'line' || unit === 'block') {
  1913. return text.length;
  1914. }
  1915. return 1;
  1916. }
  1917. },
  1918. /**
  1919. * Get the matching node in the branch of the document before a location.
  1920. */
  1921. previous: function previous(editor) {
  1922. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1923. var _options$mode4 = options.mode,
  1924. mode = _options$mode4 === void 0 ? 'lowest' : _options$mode4,
  1925. _options$voids6 = options.voids,
  1926. voids = _options$voids6 === void 0 ? false : _options$voids6;
  1927. var match = options.match,
  1928. _options$at6 = options.at,
  1929. at = _options$at6 === void 0 ? editor.selection : _options$at6;
  1930. if (!at) {
  1931. return;
  1932. }
  1933. var pointBeforeLocation = Editor.before(editor, at, {
  1934. voids: voids
  1935. });
  1936. if (!pointBeforeLocation) {
  1937. return;
  1938. }
  1939. var _Editor$first = Editor.first(editor, []),
  1940. _Editor$first2 = _slicedToArray(_Editor$first, 2),
  1941. to = _Editor$first2[1]; // The search location is from the start of the document to the path of
  1942. // the point before the location passed in
  1943. var span = [pointBeforeLocation.path, to];
  1944. if (Path.isPath(at) && at.length === 0) {
  1945. throw new Error("Cannot get the previous node from the root node!");
  1946. }
  1947. if (match == null) {
  1948. if (Path.isPath(at)) {
  1949. var _Editor$parent3 = Editor.parent(editor, at),
  1950. _Editor$parent4 = _slicedToArray(_Editor$parent3, 1),
  1951. parent = _Editor$parent4[0];
  1952. match = function match(n) {
  1953. return parent.children.includes(n);
  1954. };
  1955. } else {
  1956. match = function match() {
  1957. return true;
  1958. };
  1959. }
  1960. }
  1961. var _Editor$nodes5 = Editor.nodes(editor, {
  1962. reverse: true,
  1963. at: span,
  1964. match: match,
  1965. mode: mode,
  1966. voids: voids
  1967. }),
  1968. _Editor$nodes6 = _slicedToArray(_Editor$nodes5, 1),
  1969. previous = _Editor$nodes6[0];
  1970. return previous;
  1971. },
  1972. /**
  1973. * Get a range of a location.
  1974. */
  1975. range: function range(editor, at, to) {
  1976. if (Range.isRange(at) && !to) {
  1977. return at;
  1978. }
  1979. var start = Editor.start(editor, at);
  1980. var end = Editor.end(editor, to || at);
  1981. return {
  1982. anchor: start,
  1983. focus: end
  1984. };
  1985. },
  1986. /**
  1987. * Create a mutable ref for a `Range` object, which will stay in sync as new
  1988. * operations are applied to the editor.
  1989. */
  1990. rangeRef: function rangeRef(editor, range) {
  1991. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1992. var _options$affinity3 = options.affinity,
  1993. affinity = _options$affinity3 === void 0 ? 'forward' : _options$affinity3;
  1994. var ref = {
  1995. current: range,
  1996. affinity: affinity,
  1997. unref: function unref() {
  1998. var current = ref.current;
  1999. var rangeRefs = Editor.rangeRefs(editor);
  2000. rangeRefs["delete"](ref);
  2001. ref.current = null;
  2002. return current;
  2003. }
  2004. };
  2005. var refs = Editor.rangeRefs(editor);
  2006. refs.add(ref);
  2007. return ref;
  2008. },
  2009. /**
  2010. * Get the set of currently tracked range refs of the editor.
  2011. */
  2012. rangeRefs: function rangeRefs(editor) {
  2013. var refs = RANGE_REFS.get(editor);
  2014. if (!refs) {
  2015. refs = new Set();
  2016. RANGE_REFS.set(editor, refs);
  2017. }
  2018. return refs;
  2019. },
  2020. /**
  2021. * Remove a custom property from all of the leaf text nodes in the current
  2022. * selection.
  2023. *
  2024. * If the selection is currently collapsed, the removal will be stored on
  2025. * `editor.marks` and applied to the text inserted next.
  2026. */
  2027. removeMark: function removeMark(editor, key) {
  2028. editor.removeMark(key);
  2029. },
  2030. /**
  2031. * Manually set if the editor should currently be normalizing.
  2032. *
  2033. * Note: Using this incorrectly can leave the editor in an invalid state.
  2034. *
  2035. */
  2036. setNormalizing: function setNormalizing(editor, isNormalizing) {
  2037. NORMALIZING.set(editor, isNormalizing);
  2038. },
  2039. /**
  2040. * Get the start point of a location.
  2041. */
  2042. start: function start(editor, at) {
  2043. return Editor.point(editor, at, {
  2044. edge: 'start'
  2045. });
  2046. },
  2047. /**
  2048. * Get the text string content of a location.
  2049. *
  2050. * Note: by default the text of void nodes is considered to be an empty
  2051. * string, regardless of content, unless you pass in true for the voids option
  2052. */
  2053. string: function string(editor, at) {
  2054. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2055. var _options$voids7 = options.voids,
  2056. voids = _options$voids7 === void 0 ? false : _options$voids7;
  2057. var range = Editor.range(editor, at);
  2058. var _Range$edges5 = Range.edges(range),
  2059. _Range$edges6 = _slicedToArray(_Range$edges5, 2),
  2060. start = _Range$edges6[0],
  2061. end = _Range$edges6[1];
  2062. var text = '';
  2063. var _iterator8 = _createForOfIteratorHelper$5(Editor.nodes(editor, {
  2064. at: range,
  2065. match: Text.isText,
  2066. voids: voids
  2067. })),
  2068. _step8;
  2069. try {
  2070. for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
  2071. var _step8$value = _slicedToArray(_step8.value, 2),
  2072. node = _step8$value[0],
  2073. path = _step8$value[1];
  2074. var t = node.text;
  2075. if (Path.equals(path, end.path)) {
  2076. t = t.slice(0, end.offset);
  2077. }
  2078. if (Path.equals(path, start.path)) {
  2079. t = t.slice(start.offset);
  2080. }
  2081. text += t;
  2082. }
  2083. } catch (err) {
  2084. _iterator8.e(err);
  2085. } finally {
  2086. _iterator8.f();
  2087. }
  2088. return text;
  2089. },
  2090. /**
  2091. * Convert a range into a non-hanging one.
  2092. */
  2093. unhangRange: function unhangRange(editor, range) {
  2094. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2095. var _options$voids8 = options.voids,
  2096. voids = _options$voids8 === void 0 ? false : _options$voids8;
  2097. var _Range$edges7 = Range.edges(range),
  2098. _Range$edges8 = _slicedToArray(_Range$edges7, 2),
  2099. start = _Range$edges8[0],
  2100. end = _Range$edges8[1]; // PERF: exit early if we can guarantee that the range isn't hanging.
  2101. if (start.offset !== 0 || end.offset !== 0 || Range.isCollapsed(range)) {
  2102. return range;
  2103. }
  2104. var endBlock = Editor.above(editor, {
  2105. at: end,
  2106. match: function match(n) {
  2107. return Editor.isBlock(editor, n);
  2108. }
  2109. });
  2110. var blockPath = endBlock ? endBlock[1] : [];
  2111. var first = Editor.start(editor, start);
  2112. var before = {
  2113. anchor: first,
  2114. focus: end
  2115. };
  2116. var skip = true;
  2117. var _iterator9 = _createForOfIteratorHelper$5(Editor.nodes(editor, {
  2118. at: before,
  2119. match: Text.isText,
  2120. reverse: true,
  2121. voids: voids
  2122. })),
  2123. _step9;
  2124. try {
  2125. for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
  2126. var _step9$value = _slicedToArray(_step9.value, 2),
  2127. node = _step9$value[0],
  2128. path = _step9$value[1];
  2129. if (skip) {
  2130. skip = false;
  2131. continue;
  2132. }
  2133. if (node.text !== '' || Path.isBefore(path, blockPath)) {
  2134. end = {
  2135. path: path,
  2136. offset: node.text.length
  2137. };
  2138. break;
  2139. }
  2140. }
  2141. } catch (err) {
  2142. _iterator9.e(err);
  2143. } finally {
  2144. _iterator9.f();
  2145. }
  2146. return {
  2147. anchor: start,
  2148. focus: end
  2149. };
  2150. },
  2151. /**
  2152. * Match a void node in the current branch of the editor.
  2153. */
  2154. "void": function _void(editor) {
  2155. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2156. return Editor.above(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  2157. match: function match(n) {
  2158. return Editor.isVoid(editor, n);
  2159. }
  2160. }));
  2161. },
  2162. /**
  2163. * Call a function, deferring normalization until after it completes.
  2164. */
  2165. withoutNormalizing: function withoutNormalizing(editor, fn) {
  2166. var value = Editor.isNormalizing(editor);
  2167. Editor.setNormalizing(editor, false);
  2168. try {
  2169. fn();
  2170. } finally {
  2171. Editor.setNormalizing(editor, value);
  2172. }
  2173. Editor.normalize(editor);
  2174. }
  2175. };
  2176. var Location = {
  2177. /**
  2178. * Check if a value implements the `Location` interface.
  2179. */
  2180. isLocation: function isLocation(value) {
  2181. return Path.isPath(value) || Point.isPoint(value) || Range.isRange(value);
  2182. }
  2183. };
  2184. var Span = {
  2185. /**
  2186. * Check if a value implements the `Span` interface.
  2187. */
  2188. isSpan: function isSpan(value) {
  2189. return Array.isArray(value) && value.length === 2 && value.every(Path.isPath);
  2190. }
  2191. };
  2192. var _excluded$3 = ["children"],
  2193. _excluded2$2 = ["text"];
  2194. function _createForOfIteratorHelper$4(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$4(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  2195. function _unsupportedIterableToArray$4(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$4(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$4(o, minLen); }
  2196. function _arrayLikeToArray$4(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  2197. var IS_NODE_LIST_CACHE = new WeakMap();
  2198. var Node = {
  2199. /**
  2200. * Get the node at a specific path, asserting that it's an ancestor node.
  2201. */
  2202. ancestor: function ancestor(root, path) {
  2203. var node = Node.get(root, path);
  2204. if (Text.isText(node)) {
  2205. throw new Error("Cannot get the ancestor node at path [".concat(path, "] because it refers to a text node instead: ").concat(node));
  2206. }
  2207. return node;
  2208. },
  2209. /**
  2210. * Return a generator of all the ancestor nodes above a specific path.
  2211. *
  2212. * By default the order is bottom-up, from lowest to highest ancestor in
  2213. * the tree, but you can pass the `reverse: true` option to go top-down.
  2214. */
  2215. ancestors: function* ancestors(root, path) {
  2216. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2217. var _iterator = _createForOfIteratorHelper$4(Path.ancestors(path, options)),
  2218. _step;
  2219. try {
  2220. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  2221. var p = _step.value;
  2222. var n = Node.ancestor(root, p);
  2223. var entry = [n, p];
  2224. yield entry;
  2225. }
  2226. } catch (err) {
  2227. _iterator.e(err);
  2228. } finally {
  2229. _iterator.f();
  2230. }
  2231. },
  2232. /**
  2233. * Get the child of a node at a specific index.
  2234. */
  2235. child: function child(root, index) {
  2236. if (Text.isText(root)) {
  2237. throw new Error("Cannot get the child of a text node: ".concat(JSON.stringify(root)));
  2238. }
  2239. var c = root.children[index];
  2240. if (c == null) {
  2241. throw new Error("Cannot get child at index `".concat(index, "` in node: ").concat(JSON.stringify(root)));
  2242. }
  2243. return c;
  2244. },
  2245. /**
  2246. * Iterate over the children of a node at a specific path.
  2247. */
  2248. children: function* children(root, path) {
  2249. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2250. var _options$reverse = options.reverse,
  2251. reverse = _options$reverse === void 0 ? false : _options$reverse;
  2252. var ancestor = Node.ancestor(root, path);
  2253. var children = ancestor.children;
  2254. var index = reverse ? children.length - 1 : 0;
  2255. while (reverse ? index >= 0 : index < children.length) {
  2256. var child = Node.child(ancestor, index);
  2257. var childPath = path.concat(index);
  2258. yield [child, childPath];
  2259. index = reverse ? index - 1 : index + 1;
  2260. }
  2261. },
  2262. /**
  2263. * Get an entry for the common ancesetor node of two paths.
  2264. */
  2265. common: function common(root, path, another) {
  2266. var p = Path.common(path, another);
  2267. var n = Node.get(root, p);
  2268. return [n, p];
  2269. },
  2270. /**
  2271. * Get the node at a specific path, asserting that it's a descendant node.
  2272. */
  2273. descendant: function descendant(root, path) {
  2274. var node = Node.get(root, path);
  2275. if (Editor.isEditor(node)) {
  2276. throw new Error("Cannot get the descendant node at path [".concat(path, "] because it refers to the root editor node instead: ").concat(node));
  2277. }
  2278. return node;
  2279. },
  2280. /**
  2281. * Return a generator of all the descendant node entries inside a root node.
  2282. */
  2283. descendants: function* descendants(root) {
  2284. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2285. var _iterator2 = _createForOfIteratorHelper$4(Node.nodes(root, options)),
  2286. _step2;
  2287. try {
  2288. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  2289. var _step2$value = _slicedToArray(_step2.value, 2),
  2290. node = _step2$value[0],
  2291. path = _step2$value[1];
  2292. if (path.length !== 0) {
  2293. // NOTE: we have to coerce here because checking the path's length does
  2294. // guarantee that `node` is not a `Editor`, but TypeScript doesn't know.
  2295. yield [node, path];
  2296. }
  2297. }
  2298. } catch (err) {
  2299. _iterator2.e(err);
  2300. } finally {
  2301. _iterator2.f();
  2302. }
  2303. },
  2304. /**
  2305. * Return a generator of all the element nodes inside a root node. Each iteration
  2306. * will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the
  2307. * root node is an element it will be included in the iteration as well.
  2308. */
  2309. elements: function* elements(root) {
  2310. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2311. var _iterator3 = _createForOfIteratorHelper$4(Node.nodes(root, options)),
  2312. _step3;
  2313. try {
  2314. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  2315. var _step3$value = _slicedToArray(_step3.value, 2),
  2316. node = _step3$value[0],
  2317. path = _step3$value[1];
  2318. if (Element.isElement(node)) {
  2319. yield [node, path];
  2320. }
  2321. }
  2322. } catch (err) {
  2323. _iterator3.e(err);
  2324. } finally {
  2325. _iterator3.f();
  2326. }
  2327. },
  2328. /**
  2329. * Extract props from a Node.
  2330. */
  2331. extractProps: function extractProps(node) {
  2332. if (Element.isAncestor(node)) {
  2333. node.children;
  2334. var properties = _objectWithoutProperties(node, _excluded$3);
  2335. return properties;
  2336. } else {
  2337. node.text;
  2338. var _properties = _objectWithoutProperties(node, _excluded2$2);
  2339. return _properties;
  2340. }
  2341. },
  2342. /**
  2343. * Get the first node entry in a root node from a path.
  2344. */
  2345. first: function first(root, path) {
  2346. var p = path.slice();
  2347. var n = Node.get(root, p);
  2348. while (n) {
  2349. if (Text.isText(n) || n.children.length === 0) {
  2350. break;
  2351. } else {
  2352. n = n.children[0];
  2353. p.push(0);
  2354. }
  2355. }
  2356. return [n, p];
  2357. },
  2358. /**
  2359. * Get the sliced fragment represented by a range inside a root node.
  2360. */
  2361. fragment: function fragment(root, range) {
  2362. if (Text.isText(root)) {
  2363. throw new Error("Cannot get a fragment starting from a root text node: ".concat(JSON.stringify(root)));
  2364. }
  2365. var newRoot = immer.produce({
  2366. children: root.children
  2367. }, function (r) {
  2368. var _Range$edges = Range.edges(range),
  2369. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  2370. start = _Range$edges2[0],
  2371. end = _Range$edges2[1];
  2372. var nodeEntries = Node.nodes(r, {
  2373. reverse: true,
  2374. pass: function pass(_ref) {
  2375. var _ref2 = _slicedToArray(_ref, 2),
  2376. path = _ref2[1];
  2377. return !Range.includes(range, path);
  2378. }
  2379. });
  2380. var _iterator4 = _createForOfIteratorHelper$4(nodeEntries),
  2381. _step4;
  2382. try {
  2383. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  2384. var _step4$value = _slicedToArray(_step4.value, 2),
  2385. path = _step4$value[1];
  2386. if (!Range.includes(range, path)) {
  2387. var parent = Node.parent(r, path);
  2388. var index = path[path.length - 1];
  2389. parent.children.splice(index, 1);
  2390. }
  2391. if (Path.equals(path, end.path)) {
  2392. var leaf = Node.leaf(r, path);
  2393. leaf.text = leaf.text.slice(0, end.offset);
  2394. }
  2395. if (Path.equals(path, start.path)) {
  2396. var _leaf = Node.leaf(r, path);
  2397. _leaf.text = _leaf.text.slice(start.offset);
  2398. }
  2399. }
  2400. } catch (err) {
  2401. _iterator4.e(err);
  2402. } finally {
  2403. _iterator4.f();
  2404. }
  2405. if (Editor.isEditor(r)) {
  2406. r.selection = null;
  2407. }
  2408. });
  2409. return newRoot.children;
  2410. },
  2411. /**
  2412. * Get the descendant node referred to by a specific path. If the path is an
  2413. * empty array, it refers to the root node itself.
  2414. */
  2415. get: function get(root, path) {
  2416. var node = root;
  2417. for (var i = 0; i < path.length; i++) {
  2418. var p = path[i];
  2419. if (Text.isText(node) || !node.children[p]) {
  2420. throw new Error("Cannot find a descendant at path [".concat(path, "] in node: ").concat(JSON.stringify(root)));
  2421. }
  2422. node = node.children[p];
  2423. }
  2424. return node;
  2425. },
  2426. /**
  2427. * Check if a descendant node exists at a specific path.
  2428. */
  2429. has: function has(root, path) {
  2430. var node = root;
  2431. for (var i = 0; i < path.length; i++) {
  2432. var p = path[i];
  2433. if (Text.isText(node) || !node.children[p]) {
  2434. return false;
  2435. }
  2436. node = node.children[p];
  2437. }
  2438. return true;
  2439. },
  2440. /**
  2441. * Check if a value implements the `Node` interface.
  2442. */
  2443. isNode: function isNode(value) {
  2444. return Text.isText(value) || Element.isElement(value) || Editor.isEditor(value);
  2445. },
  2446. /**
  2447. * Check if a value is a list of `Node` objects.
  2448. */
  2449. isNodeList: function isNodeList(value) {
  2450. if (!Array.isArray(value)) {
  2451. return false;
  2452. }
  2453. var cachedResult = IS_NODE_LIST_CACHE.get(value);
  2454. if (cachedResult !== undefined) {
  2455. return cachedResult;
  2456. }
  2457. var isNodeList = value.every(function (val) {
  2458. return Node.isNode(val);
  2459. });
  2460. IS_NODE_LIST_CACHE.set(value, isNodeList);
  2461. return isNodeList;
  2462. },
  2463. /**
  2464. * Get the last node entry in a root node from a path.
  2465. */
  2466. last: function last(root, path) {
  2467. var p = path.slice();
  2468. var n = Node.get(root, p);
  2469. while (n) {
  2470. if (Text.isText(n) || n.children.length === 0) {
  2471. break;
  2472. } else {
  2473. var i = n.children.length - 1;
  2474. n = n.children[i];
  2475. p.push(i);
  2476. }
  2477. }
  2478. return [n, p];
  2479. },
  2480. /**
  2481. * Get the node at a specific path, ensuring it's a leaf text node.
  2482. */
  2483. leaf: function leaf(root, path) {
  2484. var node = Node.get(root, path);
  2485. if (!Text.isText(node)) {
  2486. throw new Error("Cannot get the leaf node at path [".concat(path, "] because it refers to a non-leaf node: ").concat(node));
  2487. }
  2488. return node;
  2489. },
  2490. /**
  2491. * Return a generator of the in a branch of the tree, from a specific path.
  2492. *
  2493. * By default the order is top-down, from lowest to highest node in the tree,
  2494. * but you can pass the `reverse: true` option to go bottom-up.
  2495. */
  2496. levels: function* levels(root, path) {
  2497. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2498. var _iterator5 = _createForOfIteratorHelper$4(Path.levels(path, options)),
  2499. _step5;
  2500. try {
  2501. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  2502. var p = _step5.value;
  2503. var n = Node.get(root, p);
  2504. yield [n, p];
  2505. }
  2506. } catch (err) {
  2507. _iterator5.e(err);
  2508. } finally {
  2509. _iterator5.f();
  2510. }
  2511. },
  2512. /**
  2513. * Check if a node matches a set of props.
  2514. */
  2515. matches: function matches(node, props) {
  2516. return Element.isElement(node) && Element.isElementProps(props) && Element.matches(node, props) || Text.isText(node) && Text.isTextProps(props) && Text.matches(node, props);
  2517. },
  2518. /**
  2519. * Return a generator of all the node entries of a root node. Each entry is
  2520. * returned as a `[Node, Path]` tuple, with the path referring to the node's
  2521. * position inside the root node.
  2522. */
  2523. nodes: function* nodes(root) {
  2524. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2525. var pass = options.pass,
  2526. _options$reverse2 = options.reverse,
  2527. reverse = _options$reverse2 === void 0 ? false : _options$reverse2;
  2528. var _options$from = options.from,
  2529. from = _options$from === void 0 ? [] : _options$from,
  2530. to = options.to;
  2531. var visited = new Set();
  2532. var p = [];
  2533. var n = root;
  2534. while (true) {
  2535. if (to && (reverse ? Path.isBefore(p, to) : Path.isAfter(p, to))) {
  2536. break;
  2537. }
  2538. if (!visited.has(n)) {
  2539. yield [n, p];
  2540. } // If we're allowed to go downward and we haven't descended yet, do.
  2541. if (!visited.has(n) && !Text.isText(n) && n.children.length !== 0 && (pass == null || pass([n, p]) === false)) {
  2542. visited.add(n);
  2543. var nextIndex = reverse ? n.children.length - 1 : 0;
  2544. if (Path.isAncestor(p, from)) {
  2545. nextIndex = from[p.length];
  2546. }
  2547. p = p.concat(nextIndex);
  2548. n = Node.get(root, p);
  2549. continue;
  2550. } // If we're at the root and we can't go down, we're done.
  2551. if (p.length === 0) {
  2552. break;
  2553. } // If we're going forward...
  2554. if (!reverse) {
  2555. var newPath = Path.next(p);
  2556. if (Node.has(root, newPath)) {
  2557. p = newPath;
  2558. n = Node.get(root, p);
  2559. continue;
  2560. }
  2561. } // If we're going backward...
  2562. if (reverse && p[p.length - 1] !== 0) {
  2563. var _newPath = Path.previous(p);
  2564. p = _newPath;
  2565. n = Node.get(root, p);
  2566. continue;
  2567. } // Otherwise we're going upward...
  2568. p = Path.parent(p);
  2569. n = Node.get(root, p);
  2570. visited.add(n);
  2571. }
  2572. },
  2573. /**
  2574. * Get the parent of a node at a specific path.
  2575. */
  2576. parent: function parent(root, path) {
  2577. var parentPath = Path.parent(path);
  2578. var p = Node.get(root, parentPath);
  2579. if (Text.isText(p)) {
  2580. throw new Error("Cannot get the parent of path [".concat(path, "] because it does not exist in the root."));
  2581. }
  2582. return p;
  2583. },
  2584. /**
  2585. * Get the concatenated text string of a node's content.
  2586. *
  2587. * Note that this will not include spaces or line breaks between block nodes.
  2588. * It is not a user-facing string, but a string for performing offset-related
  2589. * computations for a node.
  2590. */
  2591. string: function string(node) {
  2592. if (Text.isText(node)) {
  2593. return node.text;
  2594. } else {
  2595. return node.children.map(Node.string).join('');
  2596. }
  2597. },
  2598. /**
  2599. * Return a generator of all leaf text nodes in a root node.
  2600. */
  2601. texts: function* texts(root) {
  2602. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2603. var _iterator6 = _createForOfIteratorHelper$4(Node.nodes(root, options)),
  2604. _step6;
  2605. try {
  2606. for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
  2607. var _step6$value = _slicedToArray(_step6.value, 2),
  2608. node = _step6$value[0],
  2609. path = _step6$value[1];
  2610. if (Text.isText(node)) {
  2611. yield [node, path];
  2612. }
  2613. }
  2614. } catch (err) {
  2615. _iterator6.e(err);
  2616. } finally {
  2617. _iterator6.f();
  2618. }
  2619. }
  2620. };
  2621. function ownKeys$7(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  2622. function _objectSpread$7(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$7(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$7(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  2623. var Operation = {
  2624. /**
  2625. * Check of a value is a `NodeOperation` object.
  2626. */
  2627. isNodeOperation: function isNodeOperation(value) {
  2628. return Operation.isOperation(value) && value.type.endsWith('_node');
  2629. },
  2630. /**
  2631. * Check of a value is an `Operation` object.
  2632. */
  2633. isOperation: function isOperation(value) {
  2634. if (!isPlainObject.isPlainObject(value)) {
  2635. return false;
  2636. }
  2637. switch (value.type) {
  2638. case 'insert_node':
  2639. return Path.isPath(value.path) && Node.isNode(value.node);
  2640. case 'insert_text':
  2641. return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
  2642. case 'merge_node':
  2643. return typeof value.position === 'number' && Path.isPath(value.path) && isPlainObject.isPlainObject(value.properties);
  2644. case 'move_node':
  2645. return Path.isPath(value.path) && Path.isPath(value.newPath);
  2646. case 'remove_node':
  2647. return Path.isPath(value.path) && Node.isNode(value.node);
  2648. case 'remove_text':
  2649. return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
  2650. case 'set_node':
  2651. return Path.isPath(value.path) && isPlainObject.isPlainObject(value.properties) && isPlainObject.isPlainObject(value.newProperties);
  2652. case 'set_selection':
  2653. return value.properties === null && Range.isRange(value.newProperties) || value.newProperties === null && Range.isRange(value.properties) || isPlainObject.isPlainObject(value.properties) && isPlainObject.isPlainObject(value.newProperties);
  2654. case 'split_node':
  2655. return Path.isPath(value.path) && typeof value.position === 'number' && isPlainObject.isPlainObject(value.properties);
  2656. default:
  2657. return false;
  2658. }
  2659. },
  2660. /**
  2661. * Check if a value is a list of `Operation` objects.
  2662. */
  2663. isOperationList: function isOperationList(value) {
  2664. return Array.isArray(value) && value.every(function (val) {
  2665. return Operation.isOperation(val);
  2666. });
  2667. },
  2668. /**
  2669. * Check of a value is a `SelectionOperation` object.
  2670. */
  2671. isSelectionOperation: function isSelectionOperation(value) {
  2672. return Operation.isOperation(value) && value.type.endsWith('_selection');
  2673. },
  2674. /**
  2675. * Check of a value is a `TextOperation` object.
  2676. */
  2677. isTextOperation: function isTextOperation(value) {
  2678. return Operation.isOperation(value) && value.type.endsWith('_text');
  2679. },
  2680. /**
  2681. * Invert an operation, returning a new operation that will exactly undo the
  2682. * original when applied.
  2683. */
  2684. inverse: function inverse(op) {
  2685. switch (op.type) {
  2686. case 'insert_node':
  2687. {
  2688. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2689. type: 'remove_node'
  2690. });
  2691. }
  2692. case 'insert_text':
  2693. {
  2694. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2695. type: 'remove_text'
  2696. });
  2697. }
  2698. case 'merge_node':
  2699. {
  2700. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2701. type: 'split_node',
  2702. path: Path.previous(op.path)
  2703. });
  2704. }
  2705. case 'move_node':
  2706. {
  2707. var newPath = op.newPath,
  2708. path = op.path; // PERF: in this case the move operation is a no-op anyways.
  2709. if (Path.equals(newPath, path)) {
  2710. return op;
  2711. } // If the move happens completely within a single parent the path and
  2712. // newPath are stable with respect to each other.
  2713. if (Path.isSibling(path, newPath)) {
  2714. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2715. path: newPath,
  2716. newPath: path
  2717. });
  2718. } // If the move does not happen within a single parent it is possible
  2719. // for the move to impact the true path to the location where the node
  2720. // was removed from and where it was inserted. We have to adjust for this
  2721. // and find the original path. We can accomplish this (only in non-sibling)
  2722. // moves by looking at the impact of the move operation on the node
  2723. // after the original move path.
  2724. var inversePath = Path.transform(path, op);
  2725. var inverseNewPath = Path.transform(Path.next(path), op);
  2726. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2727. path: inversePath,
  2728. newPath: inverseNewPath
  2729. });
  2730. }
  2731. case 'remove_node':
  2732. {
  2733. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2734. type: 'insert_node'
  2735. });
  2736. }
  2737. case 'remove_text':
  2738. {
  2739. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2740. type: 'insert_text'
  2741. });
  2742. }
  2743. case 'set_node':
  2744. {
  2745. var properties = op.properties,
  2746. newProperties = op.newProperties;
  2747. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2748. properties: newProperties,
  2749. newProperties: properties
  2750. });
  2751. }
  2752. case 'set_selection':
  2753. {
  2754. var _properties = op.properties,
  2755. _newProperties = op.newProperties;
  2756. if (_properties == null) {
  2757. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2758. properties: _newProperties,
  2759. newProperties: null
  2760. });
  2761. } else if (_newProperties == null) {
  2762. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2763. properties: null,
  2764. newProperties: _properties
  2765. });
  2766. } else {
  2767. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2768. properties: _newProperties,
  2769. newProperties: _properties
  2770. });
  2771. }
  2772. }
  2773. case 'split_node':
  2774. {
  2775. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2776. type: 'merge_node',
  2777. path: Path.next(op.path)
  2778. });
  2779. }
  2780. }
  2781. }
  2782. };
  2783. var Path = {
  2784. /**
  2785. * Get a list of ancestor paths for a given path.
  2786. *
  2787. * The paths are sorted from deepest to shallowest ancestor. However, if the
  2788. * `reverse: true` option is passed, they are reversed.
  2789. */
  2790. ancestors: function ancestors(path) {
  2791. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2792. var _options$reverse = options.reverse,
  2793. reverse = _options$reverse === void 0 ? false : _options$reverse;
  2794. var paths = Path.levels(path, options);
  2795. if (reverse) {
  2796. paths = paths.slice(1);
  2797. } else {
  2798. paths = paths.slice(0, -1);
  2799. }
  2800. return paths;
  2801. },
  2802. /**
  2803. * Get the common ancestor path of two paths.
  2804. */
  2805. common: function common(path, another) {
  2806. var common = [];
  2807. for (var i = 0; i < path.length && i < another.length; i++) {
  2808. var av = path[i];
  2809. var bv = another[i];
  2810. if (av !== bv) {
  2811. break;
  2812. }
  2813. common.push(av);
  2814. }
  2815. return common;
  2816. },
  2817. /**
  2818. * Compare a path to another, returning an integer indicating whether the path
  2819. * was before, at, or after the other.
  2820. *
  2821. * Note: Two paths of unequal length can still receive a `0` result if one is
  2822. * directly above or below the other. If you want exact matching, use
  2823. * [[Path.equals]] instead.
  2824. */
  2825. compare: function compare(path, another) {
  2826. var min = Math.min(path.length, another.length);
  2827. for (var i = 0; i < min; i++) {
  2828. if (path[i] < another[i]) return -1;
  2829. if (path[i] > another[i]) return 1;
  2830. }
  2831. return 0;
  2832. },
  2833. /**
  2834. * Check if a path ends after one of the indexes in another.
  2835. */
  2836. endsAfter: function endsAfter(path, another) {
  2837. var i = path.length - 1;
  2838. var as = path.slice(0, i);
  2839. var bs = another.slice(0, i);
  2840. var av = path[i];
  2841. var bv = another[i];
  2842. return Path.equals(as, bs) && av > bv;
  2843. },
  2844. /**
  2845. * Check if a path ends at one of the indexes in another.
  2846. */
  2847. endsAt: function endsAt(path, another) {
  2848. var i = path.length;
  2849. var as = path.slice(0, i);
  2850. var bs = another.slice(0, i);
  2851. return Path.equals(as, bs);
  2852. },
  2853. /**
  2854. * Check if a path ends before one of the indexes in another.
  2855. */
  2856. endsBefore: function endsBefore(path, another) {
  2857. var i = path.length - 1;
  2858. var as = path.slice(0, i);
  2859. var bs = another.slice(0, i);
  2860. var av = path[i];
  2861. var bv = another[i];
  2862. return Path.equals(as, bs) && av < bv;
  2863. },
  2864. /**
  2865. * Check if a path is exactly equal to another.
  2866. */
  2867. equals: function equals(path, another) {
  2868. return path.length === another.length && path.every(function (n, i) {
  2869. return n === another[i];
  2870. });
  2871. },
  2872. /**
  2873. * Check if the path of previous sibling node exists
  2874. */
  2875. hasPrevious: function hasPrevious(path) {
  2876. return path[path.length - 1] > 0;
  2877. },
  2878. /**
  2879. * Check if a path is after another.
  2880. */
  2881. isAfter: function isAfter(path, another) {
  2882. return Path.compare(path, another) === 1;
  2883. },
  2884. /**
  2885. * Check if a path is an ancestor of another.
  2886. */
  2887. isAncestor: function isAncestor(path, another) {
  2888. return path.length < another.length && Path.compare(path, another) === 0;
  2889. },
  2890. /**
  2891. * Check if a path is before another.
  2892. */
  2893. isBefore: function isBefore(path, another) {
  2894. return Path.compare(path, another) === -1;
  2895. },
  2896. /**
  2897. * Check if a path is a child of another.
  2898. */
  2899. isChild: function isChild(path, another) {
  2900. return path.length === another.length + 1 && Path.compare(path, another) === 0;
  2901. },
  2902. /**
  2903. * Check if a path is equal to or an ancestor of another.
  2904. */
  2905. isCommon: function isCommon(path, another) {
  2906. return path.length <= another.length && Path.compare(path, another) === 0;
  2907. },
  2908. /**
  2909. * Check if a path is a descendant of another.
  2910. */
  2911. isDescendant: function isDescendant(path, another) {
  2912. return path.length > another.length && Path.compare(path, another) === 0;
  2913. },
  2914. /**
  2915. * Check if a path is the parent of another.
  2916. */
  2917. isParent: function isParent(path, another) {
  2918. return path.length + 1 === another.length && Path.compare(path, another) === 0;
  2919. },
  2920. /**
  2921. * Check is a value implements the `Path` interface.
  2922. */
  2923. isPath: function isPath(value) {
  2924. return Array.isArray(value) && (value.length === 0 || typeof value[0] === 'number');
  2925. },
  2926. /**
  2927. * Check if a path is a sibling of another.
  2928. */
  2929. isSibling: function isSibling(path, another) {
  2930. if (path.length !== another.length) {
  2931. return false;
  2932. }
  2933. var as = path.slice(0, -1);
  2934. var bs = another.slice(0, -1);
  2935. var al = path[path.length - 1];
  2936. var bl = another[another.length - 1];
  2937. return al !== bl && Path.equals(as, bs);
  2938. },
  2939. /**
  2940. * Get a list of paths at every level down to a path. Note: this is the same
  2941. * as `Path.ancestors`, but including the path itself.
  2942. *
  2943. * The paths are sorted from shallowest to deepest. However, if the `reverse:
  2944. * true` option is passed, they are reversed.
  2945. */
  2946. levels: function levels(path) {
  2947. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2948. var _options$reverse2 = options.reverse,
  2949. reverse = _options$reverse2 === void 0 ? false : _options$reverse2;
  2950. var list = [];
  2951. for (var i = 0; i <= path.length; i++) {
  2952. list.push(path.slice(0, i));
  2953. }
  2954. if (reverse) {
  2955. list.reverse();
  2956. }
  2957. return list;
  2958. },
  2959. /**
  2960. * Given a path, get the path to the next sibling node.
  2961. */
  2962. next: function next(path) {
  2963. if (path.length === 0) {
  2964. throw new Error("Cannot get the next path of a root path [".concat(path, "], because it has no next index."));
  2965. }
  2966. var last = path[path.length - 1];
  2967. return path.slice(0, -1).concat(last + 1);
  2968. },
  2969. /**
  2970. * Returns whether this operation can affect paths or not. Used as an
  2971. * optimization when updating dirty paths during normalization
  2972. *
  2973. * NOTE: This *must* be kept in sync with the implementation of 'transform'
  2974. * below
  2975. */
  2976. operationCanTransformPath: function operationCanTransformPath(operation) {
  2977. switch (operation.type) {
  2978. case 'insert_node':
  2979. case 'remove_node':
  2980. case 'merge_node':
  2981. case 'split_node':
  2982. case 'move_node':
  2983. return true;
  2984. default:
  2985. return false;
  2986. }
  2987. },
  2988. /**
  2989. * Given a path, return a new path referring to the parent node above it.
  2990. */
  2991. parent: function parent(path) {
  2992. if (path.length === 0) {
  2993. throw new Error("Cannot get the parent path of the root path [".concat(path, "]."));
  2994. }
  2995. return path.slice(0, -1);
  2996. },
  2997. /**
  2998. * Given a path, get the path to the previous sibling node.
  2999. */
  3000. previous: function previous(path) {
  3001. if (path.length === 0) {
  3002. throw new Error("Cannot get the previous path of a root path [".concat(path, "], because it has no previous index."));
  3003. }
  3004. var last = path[path.length - 1];
  3005. if (last <= 0) {
  3006. throw new Error("Cannot get the previous path of a first child path [".concat(path, "] because it would result in a negative index."));
  3007. }
  3008. return path.slice(0, -1).concat(last - 1);
  3009. },
  3010. /**
  3011. * Get a path relative to an ancestor.
  3012. */
  3013. relative: function relative(path, ancestor) {
  3014. if (!Path.isAncestor(ancestor, path) && !Path.equals(path, ancestor)) {
  3015. throw new Error("Cannot get the relative path of [".concat(path, "] inside ancestor [").concat(ancestor, "], because it is not above or equal to the path."));
  3016. }
  3017. return path.slice(ancestor.length);
  3018. },
  3019. /**
  3020. * Transform a path by an operation.
  3021. */
  3022. transform: function transform(path, operation) {
  3023. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3024. return immer.produce(path, function (p) {
  3025. var _options$affinity = options.affinity,
  3026. affinity = _options$affinity === void 0 ? 'forward' : _options$affinity; // PERF: Exit early if the operation is guaranteed not to have an effect.
  3027. if (!path || (path === null || path === void 0 ? void 0 : path.length) === 0) {
  3028. return;
  3029. }
  3030. if (p === null) {
  3031. return null;
  3032. }
  3033. switch (operation.type) {
  3034. case 'insert_node':
  3035. {
  3036. var op = operation.path;
  3037. if (Path.equals(op, p) || Path.endsBefore(op, p) || Path.isAncestor(op, p)) {
  3038. p[op.length - 1] += 1;
  3039. }
  3040. break;
  3041. }
  3042. case 'remove_node':
  3043. {
  3044. var _op = operation.path;
  3045. if (Path.equals(_op, p) || Path.isAncestor(_op, p)) {
  3046. return null;
  3047. } else if (Path.endsBefore(_op, p)) {
  3048. p[_op.length - 1] -= 1;
  3049. }
  3050. break;
  3051. }
  3052. case 'merge_node':
  3053. {
  3054. var _op2 = operation.path,
  3055. position = operation.position;
  3056. if (Path.equals(_op2, p) || Path.endsBefore(_op2, p)) {
  3057. p[_op2.length - 1] -= 1;
  3058. } else if (Path.isAncestor(_op2, p)) {
  3059. p[_op2.length - 1] -= 1;
  3060. p[_op2.length] += position;
  3061. }
  3062. break;
  3063. }
  3064. case 'split_node':
  3065. {
  3066. var _op3 = operation.path,
  3067. _position = operation.position;
  3068. if (Path.equals(_op3, p)) {
  3069. if (affinity === 'forward') {
  3070. p[p.length - 1] += 1;
  3071. } else if (affinity === 'backward') ; else {
  3072. return null;
  3073. }
  3074. } else if (Path.endsBefore(_op3, p)) {
  3075. p[_op3.length - 1] += 1;
  3076. } else if (Path.isAncestor(_op3, p) && path[_op3.length] >= _position) {
  3077. p[_op3.length - 1] += 1;
  3078. p[_op3.length] -= _position;
  3079. }
  3080. break;
  3081. }
  3082. case 'move_node':
  3083. {
  3084. var _op4 = operation.path,
  3085. onp = operation.newPath; // If the old and new path are the same, it's a no-op.
  3086. if (Path.equals(_op4, onp)) {
  3087. return;
  3088. }
  3089. if (Path.isAncestor(_op4, p) || Path.equals(_op4, p)) {
  3090. var copy = onp.slice();
  3091. if (Path.endsBefore(_op4, onp) && _op4.length < onp.length) {
  3092. copy[_op4.length - 1] -= 1;
  3093. }
  3094. return copy.concat(p.slice(_op4.length));
  3095. } else if (Path.isSibling(_op4, onp) && (Path.isAncestor(onp, p) || Path.equals(onp, p))) {
  3096. if (Path.endsBefore(_op4, p)) {
  3097. p[_op4.length - 1] -= 1;
  3098. } else {
  3099. p[_op4.length - 1] += 1;
  3100. }
  3101. } else if (Path.endsBefore(onp, p) || Path.equals(onp, p) || Path.isAncestor(onp, p)) {
  3102. if (Path.endsBefore(_op4, p)) {
  3103. p[_op4.length - 1] -= 1;
  3104. }
  3105. p[onp.length - 1] += 1;
  3106. } else if (Path.endsBefore(_op4, p)) {
  3107. if (Path.equals(onp, p)) {
  3108. p[onp.length - 1] += 1;
  3109. }
  3110. p[_op4.length - 1] -= 1;
  3111. }
  3112. break;
  3113. }
  3114. }
  3115. });
  3116. }
  3117. };
  3118. var PathRef = {
  3119. /**
  3120. * Transform the path ref's current value by an operation.
  3121. */
  3122. transform: function transform(ref, op) {
  3123. var current = ref.current,
  3124. affinity = ref.affinity;
  3125. if (current == null) {
  3126. return;
  3127. }
  3128. var path = Path.transform(current, op, {
  3129. affinity: affinity
  3130. });
  3131. ref.current = path;
  3132. if (path == null) {
  3133. ref.unref();
  3134. }
  3135. }
  3136. };
  3137. function ownKeys$6(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  3138. function _objectSpread$6(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$6(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$6(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3139. var Point = {
  3140. /**
  3141. * Compare a point to another, returning an integer indicating whether the
  3142. * point was before, at, or after the other.
  3143. */
  3144. compare: function compare(point, another) {
  3145. var result = Path.compare(point.path, another.path);
  3146. if (result === 0) {
  3147. if (point.offset < another.offset) return -1;
  3148. if (point.offset > another.offset) return 1;
  3149. return 0;
  3150. }
  3151. return result;
  3152. },
  3153. /**
  3154. * Check if a point is after another.
  3155. */
  3156. isAfter: function isAfter(point, another) {
  3157. return Point.compare(point, another) === 1;
  3158. },
  3159. /**
  3160. * Check if a point is before another.
  3161. */
  3162. isBefore: function isBefore(point, another) {
  3163. return Point.compare(point, another) === -1;
  3164. },
  3165. /**
  3166. * Check if a point is exactly equal to another.
  3167. */
  3168. equals: function equals(point, another) {
  3169. // PERF: ensure the offsets are equal first since they are cheaper to check.
  3170. return point.offset === another.offset && Path.equals(point.path, another.path);
  3171. },
  3172. /**
  3173. * Check if a value implements the `Point` interface.
  3174. */
  3175. isPoint: function isPoint(value) {
  3176. return isPlainObject.isPlainObject(value) && typeof value.offset === 'number' && Path.isPath(value.path);
  3177. },
  3178. /**
  3179. * Transform a point by an operation.
  3180. */
  3181. transform: function transform(point, op) {
  3182. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3183. return immer.produce(point, function (p) {
  3184. if (p === null) {
  3185. return null;
  3186. }
  3187. var _options$affinity = options.affinity,
  3188. affinity = _options$affinity === void 0 ? 'forward' : _options$affinity;
  3189. var path = p.path,
  3190. offset = p.offset;
  3191. switch (op.type) {
  3192. case 'insert_node':
  3193. case 'move_node':
  3194. {
  3195. p.path = Path.transform(path, op, options);
  3196. break;
  3197. }
  3198. case 'insert_text':
  3199. {
  3200. if (Path.equals(op.path, path) && op.offset <= offset) {
  3201. p.offset += op.text.length;
  3202. }
  3203. break;
  3204. }
  3205. case 'merge_node':
  3206. {
  3207. if (Path.equals(op.path, path)) {
  3208. p.offset += op.position;
  3209. }
  3210. p.path = Path.transform(path, op, options);
  3211. break;
  3212. }
  3213. case 'remove_text':
  3214. {
  3215. if (Path.equals(op.path, path) && op.offset <= offset) {
  3216. p.offset -= Math.min(offset - op.offset, op.text.length);
  3217. }
  3218. break;
  3219. }
  3220. case 'remove_node':
  3221. {
  3222. if (Path.equals(op.path, path) || Path.isAncestor(op.path, path)) {
  3223. return null;
  3224. }
  3225. p.path = Path.transform(path, op, options);
  3226. break;
  3227. }
  3228. case 'split_node':
  3229. {
  3230. if (Path.equals(op.path, path)) {
  3231. if (op.position === offset && affinity == null) {
  3232. return null;
  3233. } else if (op.position < offset || op.position === offset && affinity === 'forward') {
  3234. p.offset -= op.position;
  3235. p.path = Path.transform(path, op, _objectSpread$6(_objectSpread$6({}, options), {}, {
  3236. affinity: 'forward'
  3237. }));
  3238. }
  3239. } else {
  3240. p.path = Path.transform(path, op, options);
  3241. }
  3242. break;
  3243. }
  3244. }
  3245. });
  3246. }
  3247. };
  3248. var PointRef = {
  3249. /**
  3250. * Transform the point ref's current value by an operation.
  3251. */
  3252. transform: function transform(ref, op) {
  3253. var current = ref.current,
  3254. affinity = ref.affinity;
  3255. if (current == null) {
  3256. return;
  3257. }
  3258. var point = Point.transform(current, op, {
  3259. affinity: affinity
  3260. });
  3261. ref.current = point;
  3262. if (point == null) {
  3263. ref.unref();
  3264. }
  3265. }
  3266. };
  3267. var _excluded$2 = ["anchor", "focus"];
  3268. function ownKeys$5(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  3269. function _objectSpread$5(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$5(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$5(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3270. var Range = {
  3271. /**
  3272. * Get the start and end points of a range, in the order in which they appear
  3273. * in the document.
  3274. */
  3275. edges: function edges(range) {
  3276. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3277. var _options$reverse = options.reverse,
  3278. reverse = _options$reverse === void 0 ? false : _options$reverse;
  3279. var anchor = range.anchor,
  3280. focus = range.focus;
  3281. return Range.isBackward(range) === reverse ? [anchor, focus] : [focus, anchor];
  3282. },
  3283. /**
  3284. * Get the end point of a range.
  3285. */
  3286. end: function end(range) {
  3287. var _Range$edges = Range.edges(range),
  3288. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  3289. end = _Range$edges2[1];
  3290. return end;
  3291. },
  3292. /**
  3293. * Check if a range is exactly equal to another.
  3294. */
  3295. equals: function equals(range, another) {
  3296. return Point.equals(range.anchor, another.anchor) && Point.equals(range.focus, another.focus);
  3297. },
  3298. /**
  3299. * Check if a range includes a path, a point or part of another range.
  3300. */
  3301. includes: function includes(range, target) {
  3302. if (Range.isRange(target)) {
  3303. if (Range.includes(range, target.anchor) || Range.includes(range, target.focus)) {
  3304. return true;
  3305. }
  3306. var _Range$edges3 = Range.edges(range),
  3307. _Range$edges4 = _slicedToArray(_Range$edges3, 2),
  3308. rs = _Range$edges4[0],
  3309. re = _Range$edges4[1];
  3310. var _Range$edges5 = Range.edges(target),
  3311. _Range$edges6 = _slicedToArray(_Range$edges5, 2),
  3312. ts = _Range$edges6[0],
  3313. te = _Range$edges6[1];
  3314. return Point.isBefore(rs, ts) && Point.isAfter(re, te);
  3315. }
  3316. var _Range$edges7 = Range.edges(range),
  3317. _Range$edges8 = _slicedToArray(_Range$edges7, 2),
  3318. start = _Range$edges8[0],
  3319. end = _Range$edges8[1];
  3320. var isAfterStart = false;
  3321. var isBeforeEnd = false;
  3322. if (Point.isPoint(target)) {
  3323. isAfterStart = Point.compare(target, start) >= 0;
  3324. isBeforeEnd = Point.compare(target, end) <= 0;
  3325. } else {
  3326. isAfterStart = Path.compare(target, start.path) >= 0;
  3327. isBeforeEnd = Path.compare(target, end.path) <= 0;
  3328. }
  3329. return isAfterStart && isBeforeEnd;
  3330. },
  3331. /**
  3332. * Get the intersection of a range with another.
  3333. */
  3334. intersection: function intersection(range, another) {
  3335. range.anchor;
  3336. range.focus;
  3337. var rest = _objectWithoutProperties(range, _excluded$2);
  3338. var _Range$edges9 = Range.edges(range),
  3339. _Range$edges10 = _slicedToArray(_Range$edges9, 2),
  3340. s1 = _Range$edges10[0],
  3341. e1 = _Range$edges10[1];
  3342. var _Range$edges11 = Range.edges(another),
  3343. _Range$edges12 = _slicedToArray(_Range$edges11, 2),
  3344. s2 = _Range$edges12[0],
  3345. e2 = _Range$edges12[1];
  3346. var start = Point.isBefore(s1, s2) ? s2 : s1;
  3347. var end = Point.isBefore(e1, e2) ? e1 : e2;
  3348. if (Point.isBefore(end, start)) {
  3349. return null;
  3350. } else {
  3351. return _objectSpread$5({
  3352. anchor: start,
  3353. focus: end
  3354. }, rest);
  3355. }
  3356. },
  3357. /**
  3358. * Check if a range is backward, meaning that its anchor point appears in the
  3359. * document _after_ its focus point.
  3360. */
  3361. isBackward: function isBackward(range) {
  3362. var anchor = range.anchor,
  3363. focus = range.focus;
  3364. return Point.isAfter(anchor, focus);
  3365. },
  3366. /**
  3367. * Check if a range is collapsed, meaning that both its anchor and focus
  3368. * points refer to the exact same position in the document.
  3369. */
  3370. isCollapsed: function isCollapsed(range) {
  3371. var anchor = range.anchor,
  3372. focus = range.focus;
  3373. return Point.equals(anchor, focus);
  3374. },
  3375. /**
  3376. * Check if a range is expanded.
  3377. *
  3378. * This is the opposite of [[Range.isCollapsed]] and is provided for legibility.
  3379. */
  3380. isExpanded: function isExpanded(range) {
  3381. return !Range.isCollapsed(range);
  3382. },
  3383. /**
  3384. * Check if a range is forward.
  3385. *
  3386. * This is the opposite of [[Range.isBackward]] and is provided for legibility.
  3387. */
  3388. isForward: function isForward(range) {
  3389. return !Range.isBackward(range);
  3390. },
  3391. /**
  3392. * Check if a value implements the [[Range]] interface.
  3393. */
  3394. isRange: function isRange(value) {
  3395. return isPlainObject.isPlainObject(value) && Point.isPoint(value.anchor) && Point.isPoint(value.focus);
  3396. },
  3397. /**
  3398. * Iterate through all of the point entries in a range.
  3399. */
  3400. points: function* points(range) {
  3401. yield [range.anchor, 'anchor'];
  3402. yield [range.focus, 'focus'];
  3403. },
  3404. /**
  3405. * Get the start point of a range.
  3406. */
  3407. start: function start(range) {
  3408. var _Range$edges13 = Range.edges(range),
  3409. _Range$edges14 = _slicedToArray(_Range$edges13, 1),
  3410. start = _Range$edges14[0];
  3411. return start;
  3412. },
  3413. /**
  3414. * Transform a range by an operation.
  3415. */
  3416. transform: function transform(range, op) {
  3417. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3418. return immer.produce(range, function (r) {
  3419. if (r === null) {
  3420. return null;
  3421. }
  3422. var _options$affinity = options.affinity,
  3423. affinity = _options$affinity === void 0 ? 'inward' : _options$affinity;
  3424. var affinityAnchor;
  3425. var affinityFocus;
  3426. if (affinity === 'inward') {
  3427. // If the range is collapsed, make sure to use the same affinity to
  3428. // avoid the two points passing each other and expanding in the opposite
  3429. // direction
  3430. var isCollapsed = Range.isCollapsed(r);
  3431. if (Range.isForward(r)) {
  3432. affinityAnchor = 'forward';
  3433. affinityFocus = isCollapsed ? affinityAnchor : 'backward';
  3434. } else {
  3435. affinityAnchor = 'backward';
  3436. affinityFocus = isCollapsed ? affinityAnchor : 'forward';
  3437. }
  3438. } else if (affinity === 'outward') {
  3439. if (Range.isForward(r)) {
  3440. affinityAnchor = 'backward';
  3441. affinityFocus = 'forward';
  3442. } else {
  3443. affinityAnchor = 'forward';
  3444. affinityFocus = 'backward';
  3445. }
  3446. } else {
  3447. affinityAnchor = affinity;
  3448. affinityFocus = affinity;
  3449. }
  3450. var anchor = Point.transform(r.anchor, op, {
  3451. affinity: affinityAnchor
  3452. });
  3453. var focus = Point.transform(r.focus, op, {
  3454. affinity: affinityFocus
  3455. });
  3456. if (!anchor || !focus) {
  3457. return null;
  3458. }
  3459. r.anchor = anchor;
  3460. r.focus = focus;
  3461. });
  3462. }
  3463. };
  3464. var RangeRef = {
  3465. /**
  3466. * Transform the range ref's current value by an operation.
  3467. */
  3468. transform: function transform(ref, op) {
  3469. var current = ref.current,
  3470. affinity = ref.affinity;
  3471. if (current == null) {
  3472. return;
  3473. }
  3474. var path = Range.transform(current, op, {
  3475. affinity: affinity
  3476. });
  3477. ref.current = path;
  3478. if (path == null) {
  3479. ref.unref();
  3480. }
  3481. }
  3482. };
  3483. /*
  3484. Custom deep equal comparison for Slate nodes.
  3485. We don't need general purpose deep equality;
  3486. Slate only supports plain values, Arrays, and nested objects.
  3487. Complex values nested inside Arrays are not supported.
  3488. Slate objects are designed to be serialised, so
  3489. missing keys are deliberately normalised to undefined.
  3490. */
  3491. var isDeepEqual = function isDeepEqual(node, another) {
  3492. for (var key in node) {
  3493. var a = node[key];
  3494. var b = another[key];
  3495. if (isPlainObject.isPlainObject(a) && isPlainObject.isPlainObject(b)) {
  3496. if (!isDeepEqual(a, b)) return false;
  3497. } else if (Array.isArray(a) && Array.isArray(b)) {
  3498. if (a.length !== b.length) return false;
  3499. for (var i = 0; i < a.length; i++) {
  3500. if (a[i] !== b[i]) return false;
  3501. }
  3502. } else if (a !== b) {
  3503. return false;
  3504. }
  3505. }
  3506. /*
  3507. Deep object equality is only necessary in one direction; in the reverse direction
  3508. we are only looking for keys that are missing.
  3509. As above, undefined keys are normalised to missing.
  3510. */
  3511. for (var _key in another) {
  3512. if (node[_key] === undefined && another[_key] !== undefined) {
  3513. return false;
  3514. }
  3515. }
  3516. return true;
  3517. };
  3518. var _excluded$1 = ["text"],
  3519. _excluded2$1 = ["anchor", "focus"];
  3520. function _createForOfIteratorHelper$3(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$3(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  3521. function _unsupportedIterableToArray$3(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$3(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$3(o, minLen); }
  3522. function _arrayLikeToArray$3(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  3523. function ownKeys$4(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  3524. function _objectSpread$4(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$4(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$4(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3525. var Text = {
  3526. /**
  3527. * Check if two text nodes are equal.
  3528. *
  3529. * When loose is set, the text is not compared. This is
  3530. * used to check whether sibling text nodes can be merged.
  3531. */
  3532. equals: function equals(text, another) {
  3533. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3534. var _options$loose = options.loose,
  3535. loose = _options$loose === void 0 ? false : _options$loose;
  3536. function omitText(obj) {
  3537. obj.text;
  3538. var rest = _objectWithoutProperties(obj, _excluded$1);
  3539. return rest;
  3540. }
  3541. return isDeepEqual(loose ? omitText(text) : text, loose ? omitText(another) : another);
  3542. },
  3543. /**
  3544. * Check if a value implements the `Text` interface.
  3545. */
  3546. isText: function isText(value) {
  3547. return isPlainObject.isPlainObject(value) && typeof value.text === 'string';
  3548. },
  3549. /**
  3550. * Check if a value is a list of `Text` objects.
  3551. */
  3552. isTextList: function isTextList(value) {
  3553. return Array.isArray(value) && value.every(function (val) {
  3554. return Text.isText(val);
  3555. });
  3556. },
  3557. /**
  3558. * Check if some props are a partial of Text.
  3559. */
  3560. isTextProps: function isTextProps(props) {
  3561. return props.text !== undefined;
  3562. },
  3563. /**
  3564. * Check if an text matches set of properties.
  3565. *
  3566. * Note: this is for matching custom properties, and it does not ensure that
  3567. * the `text` property are two nodes equal.
  3568. */
  3569. matches: function matches(text, props) {
  3570. for (var key in props) {
  3571. if (key === 'text') {
  3572. continue;
  3573. }
  3574. if (!text.hasOwnProperty(key) || text[key] !== props[key]) {
  3575. return false;
  3576. }
  3577. }
  3578. return true;
  3579. },
  3580. /**
  3581. * Get the leaves for a text node given decorations.
  3582. */
  3583. decorations: function decorations(node, _decorations) {
  3584. var leaves = [_objectSpread$4({}, node)];
  3585. var _iterator = _createForOfIteratorHelper$3(_decorations),
  3586. _step;
  3587. try {
  3588. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  3589. var dec = _step.value;
  3590. var anchor = dec.anchor,
  3591. focus = dec.focus,
  3592. rest = _objectWithoutProperties(dec, _excluded2$1);
  3593. var _Range$edges = Range.edges(dec),
  3594. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  3595. start = _Range$edges2[0],
  3596. end = _Range$edges2[1];
  3597. var next = [];
  3598. var o = 0;
  3599. var _iterator2 = _createForOfIteratorHelper$3(leaves),
  3600. _step2;
  3601. try {
  3602. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  3603. var leaf = _step2.value;
  3604. var length = leaf.text.length;
  3605. var offset = o;
  3606. o += length; // If the range encompases the entire leaf, add the range.
  3607. if (start.offset <= offset && end.offset >= o) {
  3608. Object.assign(leaf, rest);
  3609. next.push(leaf);
  3610. continue;
  3611. } // If the range expanded and match the leaf, or starts after, or ends before it, continue.
  3612. if (start.offset !== end.offset && (start.offset === o || end.offset === offset) || start.offset > o || end.offset < offset || end.offset === offset && offset !== 0) {
  3613. next.push(leaf);
  3614. continue;
  3615. } // Otherwise we need to split the leaf, at the start, end, or both,
  3616. // and add the range to the middle intersecting section. Do the end
  3617. // split first since we don't need to update the offset that way.
  3618. var middle = leaf;
  3619. var before = void 0;
  3620. var after = void 0;
  3621. if (end.offset < o) {
  3622. var off = end.offset - offset;
  3623. after = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3624. text: middle.text.slice(off)
  3625. });
  3626. middle = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3627. text: middle.text.slice(0, off)
  3628. });
  3629. }
  3630. if (start.offset > offset) {
  3631. var _off = start.offset - offset;
  3632. before = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3633. text: middle.text.slice(0, _off)
  3634. });
  3635. middle = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3636. text: middle.text.slice(_off)
  3637. });
  3638. }
  3639. Object.assign(middle, rest);
  3640. if (before) {
  3641. next.push(before);
  3642. }
  3643. next.push(middle);
  3644. if (after) {
  3645. next.push(after);
  3646. }
  3647. }
  3648. } catch (err) {
  3649. _iterator2.e(err);
  3650. } finally {
  3651. _iterator2.f();
  3652. }
  3653. leaves = next;
  3654. }
  3655. } catch (err) {
  3656. _iterator.e(err);
  3657. } finally {
  3658. _iterator.f();
  3659. }
  3660. return leaves;
  3661. }
  3662. };
  3663. function ownKeys$3(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  3664. function _objectSpread$3(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$3(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$3(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3665. function _createForOfIteratorHelper$2(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$2(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  3666. function _unsupportedIterableToArray$2(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$2(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$2(o, minLen); }
  3667. function _arrayLikeToArray$2(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  3668. var applyToDraft = function applyToDraft(editor, selection, op) {
  3669. switch (op.type) {
  3670. case 'insert_node':
  3671. {
  3672. var path = op.path,
  3673. node = op.node;
  3674. var parent = Node.parent(editor, path);
  3675. var index = path[path.length - 1];
  3676. if (index > parent.children.length) {
  3677. throw new Error("Cannot apply an \"insert_node\" operation at path [".concat(path, "] because the destination is past the end of the node."));
  3678. }
  3679. parent.children.splice(index, 0, node);
  3680. if (selection) {
  3681. var _iterator = _createForOfIteratorHelper$2(Range.points(selection)),
  3682. _step;
  3683. try {
  3684. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  3685. var _step$value = _slicedToArray(_step.value, 2),
  3686. point = _step$value[0],
  3687. key = _step$value[1];
  3688. selection[key] = Point.transform(point, op);
  3689. }
  3690. } catch (err) {
  3691. _iterator.e(err);
  3692. } finally {
  3693. _iterator.f();
  3694. }
  3695. }
  3696. break;
  3697. }
  3698. case 'insert_text':
  3699. {
  3700. var _path = op.path,
  3701. offset = op.offset,
  3702. text = op.text;
  3703. if (text.length === 0) break;
  3704. var _node = Node.leaf(editor, _path);
  3705. var before = _node.text.slice(0, offset);
  3706. var after = _node.text.slice(offset);
  3707. _node.text = before + text + after;
  3708. if (selection) {
  3709. var _iterator2 = _createForOfIteratorHelper$2(Range.points(selection)),
  3710. _step2;
  3711. try {
  3712. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  3713. var _step2$value = _slicedToArray(_step2.value, 2),
  3714. _point = _step2$value[0],
  3715. _key = _step2$value[1];
  3716. selection[_key] = Point.transform(_point, op);
  3717. }
  3718. } catch (err) {
  3719. _iterator2.e(err);
  3720. } finally {
  3721. _iterator2.f();
  3722. }
  3723. }
  3724. break;
  3725. }
  3726. case 'merge_node':
  3727. {
  3728. var _path2 = op.path;
  3729. var _node2 = Node.get(editor, _path2);
  3730. var prevPath = Path.previous(_path2);
  3731. var prev = Node.get(editor, prevPath);
  3732. var _parent = Node.parent(editor, _path2);
  3733. var _index = _path2[_path2.length - 1];
  3734. if (Text.isText(_node2) && Text.isText(prev)) {
  3735. prev.text += _node2.text;
  3736. } else if (!Text.isText(_node2) && !Text.isText(prev)) {
  3737. var _prev$children;
  3738. (_prev$children = prev.children).push.apply(_prev$children, _toConsumableArray(_node2.children));
  3739. } else {
  3740. throw new Error("Cannot apply a \"merge_node\" operation at path [".concat(_path2, "] to nodes of different interfaces: ").concat(_node2, " ").concat(prev));
  3741. }
  3742. _parent.children.splice(_index, 1);
  3743. if (selection) {
  3744. var _iterator3 = _createForOfIteratorHelper$2(Range.points(selection)),
  3745. _step3;
  3746. try {
  3747. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  3748. var _step3$value = _slicedToArray(_step3.value, 2),
  3749. _point2 = _step3$value[0],
  3750. _key2 = _step3$value[1];
  3751. selection[_key2] = Point.transform(_point2, op);
  3752. }
  3753. } catch (err) {
  3754. _iterator3.e(err);
  3755. } finally {
  3756. _iterator3.f();
  3757. }
  3758. }
  3759. break;
  3760. }
  3761. case 'move_node':
  3762. {
  3763. var _path3 = op.path,
  3764. newPath = op.newPath;
  3765. if (Path.isAncestor(_path3, newPath)) {
  3766. throw new Error("Cannot move a path [".concat(_path3, "] to new path [").concat(newPath, "] because the destination is inside itself."));
  3767. }
  3768. var _node3 = Node.get(editor, _path3);
  3769. var _parent2 = Node.parent(editor, _path3);
  3770. var _index2 = _path3[_path3.length - 1]; // This is tricky, but since the `path` and `newPath` both refer to
  3771. // the same snapshot in time, there's a mismatch. After either
  3772. // removing the original position, the second step's path can be out
  3773. // of date. So instead of using the `op.newPath` directly, we
  3774. // transform `op.path` to ascertain what the `newPath` would be after
  3775. // the operation was applied.
  3776. _parent2.children.splice(_index2, 1);
  3777. var truePath = Path.transform(_path3, op);
  3778. var newParent = Node.get(editor, Path.parent(truePath));
  3779. var newIndex = truePath[truePath.length - 1];
  3780. newParent.children.splice(newIndex, 0, _node3);
  3781. if (selection) {
  3782. var _iterator4 = _createForOfIteratorHelper$2(Range.points(selection)),
  3783. _step4;
  3784. try {
  3785. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  3786. var _step4$value = _slicedToArray(_step4.value, 2),
  3787. _point3 = _step4$value[0],
  3788. _key3 = _step4$value[1];
  3789. selection[_key3] = Point.transform(_point3, op);
  3790. }
  3791. } catch (err) {
  3792. _iterator4.e(err);
  3793. } finally {
  3794. _iterator4.f();
  3795. }
  3796. }
  3797. break;
  3798. }
  3799. case 'remove_node':
  3800. {
  3801. var _path4 = op.path;
  3802. var _index3 = _path4[_path4.length - 1];
  3803. var _parent3 = Node.parent(editor, _path4);
  3804. _parent3.children.splice(_index3, 1); // Transform all of the points in the value, but if the point was in the
  3805. // node that was removed we need to update the range or remove it.
  3806. if (selection) {
  3807. var _iterator5 = _createForOfIteratorHelper$2(Range.points(selection)),
  3808. _step5;
  3809. try {
  3810. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  3811. var _step5$value = _slicedToArray(_step5.value, 2),
  3812. _point4 = _step5$value[0],
  3813. _key4 = _step5$value[1];
  3814. var result = Point.transform(_point4, op);
  3815. if (selection != null && result != null) {
  3816. selection[_key4] = result;
  3817. } else {
  3818. var _prev = void 0;
  3819. var next = void 0;
  3820. var _iterator6 = _createForOfIteratorHelper$2(Node.texts(editor)),
  3821. _step6;
  3822. try {
  3823. for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
  3824. var _step6$value = _slicedToArray(_step6.value, 2),
  3825. n = _step6$value[0],
  3826. p = _step6$value[1];
  3827. if (Path.compare(p, _path4) === -1) {
  3828. _prev = [n, p];
  3829. } else {
  3830. next = [n, p];
  3831. break;
  3832. }
  3833. }
  3834. } catch (err) {
  3835. _iterator6.e(err);
  3836. } finally {
  3837. _iterator6.f();
  3838. }
  3839. var preferNext = false;
  3840. if (_prev && next) {
  3841. if (Path.equals(next[1], _path4)) {
  3842. preferNext = !Path.hasPrevious(next[1]);
  3843. } else {
  3844. preferNext = Path.common(_prev[1], _path4).length < Path.common(next[1], _path4).length;
  3845. }
  3846. }
  3847. if (_prev && !preferNext) {
  3848. _point4.path = _prev[1];
  3849. _point4.offset = _prev[0].text.length;
  3850. } else if (next) {
  3851. _point4.path = next[1];
  3852. _point4.offset = 0;
  3853. } else {
  3854. selection = null;
  3855. }
  3856. }
  3857. }
  3858. } catch (err) {
  3859. _iterator5.e(err);
  3860. } finally {
  3861. _iterator5.f();
  3862. }
  3863. }
  3864. break;
  3865. }
  3866. case 'remove_text':
  3867. {
  3868. var _path5 = op.path,
  3869. _offset = op.offset,
  3870. _text = op.text;
  3871. if (_text.length === 0) break;
  3872. var _node4 = Node.leaf(editor, _path5);
  3873. var _before = _node4.text.slice(0, _offset);
  3874. var _after = _node4.text.slice(_offset + _text.length);
  3875. _node4.text = _before + _after;
  3876. if (selection) {
  3877. var _iterator7 = _createForOfIteratorHelper$2(Range.points(selection)),
  3878. _step7;
  3879. try {
  3880. for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
  3881. var _step7$value = _slicedToArray(_step7.value, 2),
  3882. _point5 = _step7$value[0],
  3883. _key5 = _step7$value[1];
  3884. selection[_key5] = Point.transform(_point5, op);
  3885. }
  3886. } catch (err) {
  3887. _iterator7.e(err);
  3888. } finally {
  3889. _iterator7.f();
  3890. }
  3891. }
  3892. break;
  3893. }
  3894. case 'set_node':
  3895. {
  3896. var _path6 = op.path,
  3897. properties = op.properties,
  3898. newProperties = op.newProperties;
  3899. if (_path6.length === 0) {
  3900. throw new Error("Cannot set properties on the root node!");
  3901. }
  3902. var _node5 = Node.get(editor, _path6);
  3903. for (var _key6 in newProperties) {
  3904. if (_key6 === 'children' || _key6 === 'text') {
  3905. throw new Error("Cannot set the \"".concat(_key6, "\" property of nodes!"));
  3906. }
  3907. var value = newProperties[_key6];
  3908. if (value == null) {
  3909. delete _node5[_key6];
  3910. } else {
  3911. _node5[_key6] = value;
  3912. }
  3913. } // properties that were previously defined, but are now missing, must be deleted
  3914. for (var _key7 in properties) {
  3915. if (!newProperties.hasOwnProperty(_key7)) {
  3916. delete _node5[_key7];
  3917. }
  3918. }
  3919. break;
  3920. }
  3921. case 'set_selection':
  3922. {
  3923. var _newProperties = op.newProperties;
  3924. if (_newProperties == null) {
  3925. selection = _newProperties;
  3926. } else {
  3927. if (selection == null) {
  3928. if (!Range.isRange(_newProperties)) {
  3929. throw new Error("Cannot apply an incomplete \"set_selection\" operation properties ".concat(JSON.stringify(_newProperties), " when there is no current selection."));
  3930. }
  3931. selection = _objectSpread$3({}, _newProperties);
  3932. }
  3933. for (var _key8 in _newProperties) {
  3934. var _value = _newProperties[_key8];
  3935. if (_value == null) {
  3936. if (_key8 === 'anchor' || _key8 === 'focus') {
  3937. throw new Error("Cannot remove the \"".concat(_key8, "\" selection property"));
  3938. }
  3939. delete selection[_key8];
  3940. } else {
  3941. selection[_key8] = _value;
  3942. }
  3943. }
  3944. }
  3945. break;
  3946. }
  3947. case 'split_node':
  3948. {
  3949. var _path7 = op.path,
  3950. position = op.position,
  3951. _properties = op.properties;
  3952. if (_path7.length === 0) {
  3953. throw new Error("Cannot apply a \"split_node\" operation at path [".concat(_path7, "] because the root node cannot be split."));
  3954. }
  3955. var _node6 = Node.get(editor, _path7);
  3956. var _parent4 = Node.parent(editor, _path7);
  3957. var _index4 = _path7[_path7.length - 1];
  3958. var newNode;
  3959. if (Text.isText(_node6)) {
  3960. var _before2 = _node6.text.slice(0, position);
  3961. var _after2 = _node6.text.slice(position);
  3962. _node6.text = _before2;
  3963. newNode = _objectSpread$3(_objectSpread$3({}, _properties), {}, {
  3964. text: _after2
  3965. });
  3966. } else {
  3967. var _before3 = _node6.children.slice(0, position);
  3968. var _after3 = _node6.children.slice(position);
  3969. _node6.children = _before3;
  3970. newNode = _objectSpread$3(_objectSpread$3({}, _properties), {}, {
  3971. children: _after3
  3972. });
  3973. }
  3974. _parent4.children.splice(_index4 + 1, 0, newNode);
  3975. if (selection) {
  3976. var _iterator8 = _createForOfIteratorHelper$2(Range.points(selection)),
  3977. _step8;
  3978. try {
  3979. for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
  3980. var _step8$value = _slicedToArray(_step8.value, 2),
  3981. _point6 = _step8$value[0],
  3982. _key9 = _step8$value[1];
  3983. selection[_key9] = Point.transform(_point6, op);
  3984. }
  3985. } catch (err) {
  3986. _iterator8.e(err);
  3987. } finally {
  3988. _iterator8.f();
  3989. }
  3990. }
  3991. break;
  3992. }
  3993. }
  3994. return selection;
  3995. };
  3996. var GeneralTransforms = {
  3997. /**
  3998. * Transform the editor by an operation.
  3999. */
  4000. transform: function transform(editor, op) {
  4001. editor.children = immer.createDraft(editor.children);
  4002. var selection = editor.selection && immer.createDraft(editor.selection);
  4003. try {
  4004. selection = applyToDraft(editor, selection, op);
  4005. } finally {
  4006. editor.children = immer.finishDraft(editor.children);
  4007. if (selection) {
  4008. editor.selection = immer.isDraft(selection) ? immer.finishDraft(selection) : selection;
  4009. } else {
  4010. editor.selection = null;
  4011. }
  4012. }
  4013. }
  4014. };
  4015. var _excluded = ["text"],
  4016. _excluded2 = ["children"];
  4017. function ownKeys$2(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  4018. function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$2(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$2(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  4019. function _createForOfIteratorHelper$1(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray$1(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  4020. function _unsupportedIterableToArray$1(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray$1(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray$1(o, minLen); }
  4021. function _arrayLikeToArray$1(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  4022. var NodeTransforms = {
  4023. /**
  4024. * Insert nodes at a specific location in the Editor.
  4025. */
  4026. insertNodes: function insertNodes(editor, nodes) {
  4027. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4028. Editor.withoutNormalizing(editor, function () {
  4029. var _options$hanging = options.hanging,
  4030. hanging = _options$hanging === void 0 ? false : _options$hanging,
  4031. _options$voids = options.voids,
  4032. voids = _options$voids === void 0 ? false : _options$voids,
  4033. _options$mode = options.mode,
  4034. mode = _options$mode === void 0 ? 'lowest' : _options$mode;
  4035. var at = options.at,
  4036. match = options.match,
  4037. select = options.select;
  4038. if (Node.isNode(nodes)) {
  4039. nodes = [nodes];
  4040. }
  4041. if (nodes.length === 0) {
  4042. return;
  4043. }
  4044. var _nodes = nodes,
  4045. _nodes2 = _slicedToArray(_nodes, 1),
  4046. node = _nodes2[0]; // By default, use the selection as the target location. But if there is
  4047. // no selection, insert at the end of the document since that is such a
  4048. // common use case when inserting from a non-selected state.
  4049. if (!at) {
  4050. if (editor.selection) {
  4051. at = editor.selection;
  4052. } else if (editor.children.length > 0) {
  4053. at = Editor.end(editor, []);
  4054. } else {
  4055. at = [0];
  4056. }
  4057. select = true;
  4058. }
  4059. if (select == null) {
  4060. select = false;
  4061. }
  4062. if (Range.isRange(at)) {
  4063. if (!hanging) {
  4064. at = Editor.unhangRange(editor, at);
  4065. }
  4066. if (Range.isCollapsed(at)) {
  4067. at = at.anchor;
  4068. } else {
  4069. var _Range$edges = Range.edges(at),
  4070. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  4071. end = _Range$edges2[1];
  4072. var pointRef = Editor.pointRef(editor, end);
  4073. Transforms["delete"](editor, {
  4074. at: at
  4075. });
  4076. at = pointRef.unref();
  4077. }
  4078. }
  4079. if (Point.isPoint(at)) {
  4080. if (match == null) {
  4081. if (Text.isText(node)) {
  4082. match = function match(n) {
  4083. return Text.isText(n);
  4084. };
  4085. } else if (editor.isInline(node)) {
  4086. match = function match(n) {
  4087. return Text.isText(n) || Editor.isInline(editor, n);
  4088. };
  4089. } else {
  4090. match = function match(n) {
  4091. return Editor.isBlock(editor, n);
  4092. };
  4093. }
  4094. }
  4095. var _Editor$nodes = Editor.nodes(editor, {
  4096. at: at.path,
  4097. match: match,
  4098. mode: mode,
  4099. voids: voids
  4100. }),
  4101. _Editor$nodes2 = _slicedToArray(_Editor$nodes, 1),
  4102. entry = _Editor$nodes2[0];
  4103. if (entry) {
  4104. var _entry = _slicedToArray(entry, 2),
  4105. _matchPath = _entry[1];
  4106. var pathRef = Editor.pathRef(editor, _matchPath);
  4107. var isAtEnd = Editor.isEnd(editor, at, _matchPath);
  4108. Transforms.splitNodes(editor, {
  4109. at: at,
  4110. match: match,
  4111. mode: mode,
  4112. voids: voids
  4113. });
  4114. var path = pathRef.unref();
  4115. at = isAtEnd ? Path.next(path) : path;
  4116. } else {
  4117. return;
  4118. }
  4119. }
  4120. var parentPath = Path.parent(at);
  4121. var index = at[at.length - 1];
  4122. if (!voids && Editor["void"](editor, {
  4123. at: parentPath
  4124. })) {
  4125. return;
  4126. }
  4127. var _iterator = _createForOfIteratorHelper$1(nodes),
  4128. _step;
  4129. try {
  4130. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  4131. var _node = _step.value;
  4132. var _path = parentPath.concat(index);
  4133. index++;
  4134. editor.apply({
  4135. type: 'insert_node',
  4136. path: _path,
  4137. node: _node
  4138. });
  4139. at = Path.next(at);
  4140. }
  4141. } catch (err) {
  4142. _iterator.e(err);
  4143. } finally {
  4144. _iterator.f();
  4145. }
  4146. at = Path.previous(at);
  4147. if (select) {
  4148. var point = Editor.end(editor, at);
  4149. if (point) {
  4150. Transforms.select(editor, point);
  4151. }
  4152. }
  4153. });
  4154. },
  4155. /**
  4156. * Lift nodes at a specific location upwards in the document tree, splitting
  4157. * their parent in two if necessary.
  4158. */
  4159. liftNodes: function liftNodes(editor) {
  4160. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4161. Editor.withoutNormalizing(editor, function () {
  4162. var _options$at = options.at,
  4163. at = _options$at === void 0 ? editor.selection : _options$at,
  4164. _options$mode2 = options.mode,
  4165. mode = _options$mode2 === void 0 ? 'lowest' : _options$mode2,
  4166. _options$voids2 = options.voids,
  4167. voids = _options$voids2 === void 0 ? false : _options$voids2;
  4168. var match = options.match;
  4169. if (match == null) {
  4170. match = Path.isPath(at) ? matchPath(editor, at) : function (n) {
  4171. return Editor.isBlock(editor, n);
  4172. };
  4173. }
  4174. if (!at) {
  4175. return;
  4176. }
  4177. var matches = Editor.nodes(editor, {
  4178. at: at,
  4179. match: match,
  4180. mode: mode,
  4181. voids: voids
  4182. });
  4183. var pathRefs = Array.from(matches, function (_ref) {
  4184. var _ref2 = _slicedToArray(_ref, 2),
  4185. p = _ref2[1];
  4186. return Editor.pathRef(editor, p);
  4187. });
  4188. for (var _i = 0, _pathRefs = pathRefs; _i < _pathRefs.length; _i++) {
  4189. var pathRef = _pathRefs[_i];
  4190. var path = pathRef.unref();
  4191. if (path.length < 2) {
  4192. throw new Error("Cannot lift node at a path [".concat(path, "] because it has a depth of less than `2`."));
  4193. }
  4194. var parentNodeEntry = Editor.node(editor, Path.parent(path));
  4195. var _parentNodeEntry = _slicedToArray(parentNodeEntry, 2),
  4196. parent = _parentNodeEntry[0],
  4197. parentPath = _parentNodeEntry[1];
  4198. var index = path[path.length - 1];
  4199. var length = parent.children.length;
  4200. if (length === 1) {
  4201. var toPath = Path.next(parentPath);
  4202. Transforms.moveNodes(editor, {
  4203. at: path,
  4204. to: toPath,
  4205. voids: voids
  4206. });
  4207. Transforms.removeNodes(editor, {
  4208. at: parentPath,
  4209. voids: voids
  4210. });
  4211. } else if (index === 0) {
  4212. Transforms.moveNodes(editor, {
  4213. at: path,
  4214. to: parentPath,
  4215. voids: voids
  4216. });
  4217. } else if (index === length - 1) {
  4218. var _toPath = Path.next(parentPath);
  4219. Transforms.moveNodes(editor, {
  4220. at: path,
  4221. to: _toPath,
  4222. voids: voids
  4223. });
  4224. } else {
  4225. var splitPath = Path.next(path);
  4226. var _toPath2 = Path.next(parentPath);
  4227. Transforms.splitNodes(editor, {
  4228. at: splitPath,
  4229. voids: voids
  4230. });
  4231. Transforms.moveNodes(editor, {
  4232. at: path,
  4233. to: _toPath2,
  4234. voids: voids
  4235. });
  4236. }
  4237. }
  4238. });
  4239. },
  4240. /**
  4241. * Merge a node at a location with the previous node of the same depth,
  4242. * removing any empty containing nodes after the merge if necessary.
  4243. */
  4244. mergeNodes: function mergeNodes(editor) {
  4245. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4246. Editor.withoutNormalizing(editor, function () {
  4247. var match = options.match,
  4248. _options$at2 = options.at,
  4249. at = _options$at2 === void 0 ? editor.selection : _options$at2;
  4250. var _options$hanging2 = options.hanging,
  4251. hanging = _options$hanging2 === void 0 ? false : _options$hanging2,
  4252. _options$voids3 = options.voids,
  4253. voids = _options$voids3 === void 0 ? false : _options$voids3,
  4254. _options$mode3 = options.mode,
  4255. mode = _options$mode3 === void 0 ? 'lowest' : _options$mode3;
  4256. if (!at) {
  4257. return;
  4258. }
  4259. if (match == null) {
  4260. if (Path.isPath(at)) {
  4261. var _Editor$parent = Editor.parent(editor, at),
  4262. _Editor$parent2 = _slicedToArray(_Editor$parent, 1),
  4263. parent = _Editor$parent2[0];
  4264. match = function match(n) {
  4265. return parent.children.includes(n);
  4266. };
  4267. } else {
  4268. match = function match(n) {
  4269. return Editor.isBlock(editor, n);
  4270. };
  4271. }
  4272. }
  4273. if (!hanging && Range.isRange(at)) {
  4274. at = Editor.unhangRange(editor, at);
  4275. }
  4276. if (Range.isRange(at)) {
  4277. if (Range.isCollapsed(at)) {
  4278. at = at.anchor;
  4279. } else {
  4280. var _Range$edges3 = Range.edges(at),
  4281. _Range$edges4 = _slicedToArray(_Range$edges3, 2),
  4282. end = _Range$edges4[1];
  4283. var pointRef = Editor.pointRef(editor, end);
  4284. Transforms["delete"](editor, {
  4285. at: at
  4286. });
  4287. at = pointRef.unref();
  4288. if (options.at == null) {
  4289. Transforms.select(editor, at);
  4290. }
  4291. }
  4292. }
  4293. var _Editor$nodes3 = Editor.nodes(editor, {
  4294. at: at,
  4295. match: match,
  4296. voids: voids,
  4297. mode: mode
  4298. }),
  4299. _Editor$nodes4 = _slicedToArray(_Editor$nodes3, 1),
  4300. current = _Editor$nodes4[0];
  4301. var prev = Editor.previous(editor, {
  4302. at: at,
  4303. match: match,
  4304. voids: voids,
  4305. mode: mode
  4306. });
  4307. if (!current || !prev) {
  4308. return;
  4309. }
  4310. var _current = _slicedToArray(current, 2),
  4311. node = _current[0],
  4312. path = _current[1];
  4313. var _prev = _slicedToArray(prev, 2),
  4314. prevNode = _prev[0],
  4315. prevPath = _prev[1];
  4316. if (path.length === 0 || prevPath.length === 0) {
  4317. return;
  4318. }
  4319. var newPath = Path.next(prevPath);
  4320. var commonPath = Path.common(path, prevPath);
  4321. var isPreviousSibling = Path.isSibling(path, prevPath);
  4322. var levels = Array.from(Editor.levels(editor, {
  4323. at: path
  4324. }), function (_ref3) {
  4325. var _ref4 = _slicedToArray(_ref3, 1),
  4326. n = _ref4[0];
  4327. return n;
  4328. }).slice(commonPath.length).slice(0, -1); // Determine if the merge will leave an ancestor of the path empty as a
  4329. // result, in which case we'll want to remove it after merging.
  4330. var emptyAncestor = Editor.above(editor, {
  4331. at: path,
  4332. mode: 'highest',
  4333. match: function match(n) {
  4334. return levels.includes(n) && hasSingleChildNest(editor, n);
  4335. }
  4336. });
  4337. var emptyRef = emptyAncestor && Editor.pathRef(editor, emptyAncestor[1]);
  4338. var properties;
  4339. var position; // Ensure that the nodes are equivalent, and figure out what the position
  4340. // and extra properties of the merge will be.
  4341. if (Text.isText(node) && Text.isText(prevNode)) {
  4342. node.text;
  4343. var rest = _objectWithoutProperties(node, _excluded);
  4344. position = prevNode.text.length;
  4345. properties = rest;
  4346. } else if (Element.isElement(node) && Element.isElement(prevNode)) {
  4347. node.children;
  4348. var _rest = _objectWithoutProperties(node, _excluded2);
  4349. position = prevNode.children.length;
  4350. properties = _rest;
  4351. } else {
  4352. throw new Error("Cannot merge the node at path [".concat(path, "] with the previous sibling because it is not the same kind: ").concat(JSON.stringify(node), " ").concat(JSON.stringify(prevNode)));
  4353. } // If the node isn't already the next sibling of the previous node, move
  4354. // it so that it is before merging.
  4355. if (!isPreviousSibling) {
  4356. Transforms.moveNodes(editor, {
  4357. at: path,
  4358. to: newPath,
  4359. voids: voids
  4360. });
  4361. } // If there was going to be an empty ancestor of the node that was merged,
  4362. // we remove it from the tree.
  4363. if (emptyRef) {
  4364. Transforms.removeNodes(editor, {
  4365. at: emptyRef.current,
  4366. voids: voids
  4367. });
  4368. } // If the target node that we're merging with is empty, remove it instead
  4369. // of merging the two. This is a common rich text editor behavior to
  4370. // prevent losing formatting when deleting entire nodes when you have a
  4371. // hanging selection.
  4372. // if prevNode is first child in parent,don't remove it.
  4373. if (Element.isElement(prevNode) && Editor.isEmpty(editor, prevNode) || Text.isText(prevNode) && prevNode.text === '' && prevPath[prevPath.length - 1] !== 0) {
  4374. Transforms.removeNodes(editor, {
  4375. at: prevPath,
  4376. voids: voids
  4377. });
  4378. } else {
  4379. editor.apply({
  4380. type: 'merge_node',
  4381. path: newPath,
  4382. position: position,
  4383. properties: properties
  4384. });
  4385. }
  4386. if (emptyRef) {
  4387. emptyRef.unref();
  4388. }
  4389. });
  4390. },
  4391. /**
  4392. * Move the nodes at a location to a new location.
  4393. */
  4394. moveNodes: function moveNodes(editor, options) {
  4395. Editor.withoutNormalizing(editor, function () {
  4396. var to = options.to,
  4397. _options$at3 = options.at,
  4398. at = _options$at3 === void 0 ? editor.selection : _options$at3,
  4399. _options$mode4 = options.mode,
  4400. mode = _options$mode4 === void 0 ? 'lowest' : _options$mode4,
  4401. _options$voids4 = options.voids,
  4402. voids = _options$voids4 === void 0 ? false : _options$voids4;
  4403. var match = options.match;
  4404. if (!at) {
  4405. return;
  4406. }
  4407. if (match == null) {
  4408. match = Path.isPath(at) ? matchPath(editor, at) : function (n) {
  4409. return Editor.isBlock(editor, n);
  4410. };
  4411. }
  4412. var toRef = Editor.pathRef(editor, to);
  4413. var targets = Editor.nodes(editor, {
  4414. at: at,
  4415. match: match,
  4416. mode: mode,
  4417. voids: voids
  4418. });
  4419. var pathRefs = Array.from(targets, function (_ref5) {
  4420. var _ref6 = _slicedToArray(_ref5, 2),
  4421. p = _ref6[1];
  4422. return Editor.pathRef(editor, p);
  4423. });
  4424. for (var _i2 = 0, _pathRefs2 = pathRefs; _i2 < _pathRefs2.length; _i2++) {
  4425. var pathRef = _pathRefs2[_i2];
  4426. var path = pathRef.unref();
  4427. var newPath = toRef.current;
  4428. if (path.length !== 0) {
  4429. editor.apply({
  4430. type: 'move_node',
  4431. path: path,
  4432. newPath: newPath
  4433. });
  4434. }
  4435. if (toRef.current && Path.isSibling(newPath, path) && Path.isAfter(newPath, path)) {
  4436. // When performing a sibling move to a later index, the path at the destination is shifted
  4437. // to before the insertion point instead of after. To ensure our group of nodes are inserted
  4438. // in the correct order we increment toRef to account for that
  4439. toRef.current = Path.next(toRef.current);
  4440. }
  4441. }
  4442. toRef.unref();
  4443. });
  4444. },
  4445. /**
  4446. * Remove the nodes at a specific location in the document.
  4447. */
  4448. removeNodes: function removeNodes(editor) {
  4449. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4450. Editor.withoutNormalizing(editor, function () {
  4451. var _options$hanging3 = options.hanging,
  4452. hanging = _options$hanging3 === void 0 ? false : _options$hanging3,
  4453. _options$voids5 = options.voids,
  4454. voids = _options$voids5 === void 0 ? false : _options$voids5,
  4455. _options$mode5 = options.mode,
  4456. mode = _options$mode5 === void 0 ? 'lowest' : _options$mode5;
  4457. var _options$at4 = options.at,
  4458. at = _options$at4 === void 0 ? editor.selection : _options$at4,
  4459. match = options.match;
  4460. if (!at) {
  4461. return;
  4462. }
  4463. if (match == null) {
  4464. match = Path.isPath(at) ? matchPath(editor, at) : function (n) {
  4465. return Editor.isBlock(editor, n);
  4466. };
  4467. }
  4468. if (!hanging && Range.isRange(at)) {
  4469. at = Editor.unhangRange(editor, at);
  4470. }
  4471. var depths = Editor.nodes(editor, {
  4472. at: at,
  4473. match: match,
  4474. mode: mode,
  4475. voids: voids
  4476. });
  4477. var pathRefs = Array.from(depths, function (_ref7) {
  4478. var _ref8 = _slicedToArray(_ref7, 2),
  4479. p = _ref8[1];
  4480. return Editor.pathRef(editor, p);
  4481. });
  4482. for (var _i3 = 0, _pathRefs3 = pathRefs; _i3 < _pathRefs3.length; _i3++) {
  4483. var pathRef = _pathRefs3[_i3];
  4484. var path = pathRef.unref();
  4485. if (path) {
  4486. var _Editor$node = Editor.node(editor, path),
  4487. _Editor$node2 = _slicedToArray(_Editor$node, 1),
  4488. node = _Editor$node2[0];
  4489. editor.apply({
  4490. type: 'remove_node',
  4491. path: path,
  4492. node: node
  4493. });
  4494. }
  4495. }
  4496. });
  4497. },
  4498. /**
  4499. * Set new properties on the nodes at a location.
  4500. */
  4501. setNodes: function setNodes(editor, props) {
  4502. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4503. Editor.withoutNormalizing(editor, function () {
  4504. var match = options.match,
  4505. _options$at5 = options.at,
  4506. at = _options$at5 === void 0 ? editor.selection : _options$at5;
  4507. var _options$hanging4 = options.hanging,
  4508. hanging = _options$hanging4 === void 0 ? false : _options$hanging4,
  4509. _options$mode6 = options.mode,
  4510. mode = _options$mode6 === void 0 ? 'lowest' : _options$mode6,
  4511. _options$split = options.split,
  4512. split = _options$split === void 0 ? false : _options$split,
  4513. _options$voids6 = options.voids,
  4514. voids = _options$voids6 === void 0 ? false : _options$voids6;
  4515. if (!at) {
  4516. return;
  4517. }
  4518. if (match == null) {
  4519. match = Path.isPath(at) ? matchPath(editor, at) : function (n) {
  4520. return Editor.isBlock(editor, n);
  4521. };
  4522. }
  4523. if (!hanging && Range.isRange(at)) {
  4524. at = Editor.unhangRange(editor, at);
  4525. }
  4526. if (split && Range.isRange(at)) {
  4527. if (Range.isCollapsed(at) && Editor.leaf(editor, at.anchor)[0].text.length > 0) {
  4528. // If the range is collapsed in a non-empty node and 'split' is true, there's nothing to
  4529. // set that won't get normalized away
  4530. return;
  4531. }
  4532. var rangeRef = Editor.rangeRef(editor, at, {
  4533. affinity: 'inward'
  4534. });
  4535. var _Range$edges5 = Range.edges(at),
  4536. _Range$edges6 = _slicedToArray(_Range$edges5, 2),
  4537. start = _Range$edges6[0],
  4538. end = _Range$edges6[1];
  4539. var splitMode = mode === 'lowest' ? 'lowest' : 'highest';
  4540. var endAtEndOfNode = Editor.isEnd(editor, end, end.path);
  4541. Transforms.splitNodes(editor, {
  4542. at: end,
  4543. match: match,
  4544. mode: splitMode,
  4545. voids: voids,
  4546. always: !endAtEndOfNode
  4547. });
  4548. var startAtStartOfNode = Editor.isStart(editor, start, start.path);
  4549. Transforms.splitNodes(editor, {
  4550. at: start,
  4551. match: match,
  4552. mode: splitMode,
  4553. voids: voids,
  4554. always: !startAtStartOfNode
  4555. });
  4556. at = rangeRef.unref();
  4557. if (options.at == null) {
  4558. Transforms.select(editor, at);
  4559. }
  4560. }
  4561. var _iterator2 = _createForOfIteratorHelper$1(Editor.nodes(editor, {
  4562. at: at,
  4563. match: match,
  4564. mode: mode,
  4565. voids: voids
  4566. })),
  4567. _step2;
  4568. try {
  4569. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  4570. var _step2$value = _slicedToArray(_step2.value, 2),
  4571. node = _step2$value[0],
  4572. path = _step2$value[1];
  4573. var properties = {};
  4574. var newProperties = {}; // You can't set properties on the editor node.
  4575. if (path.length === 0) {
  4576. continue;
  4577. }
  4578. var hasChanges = false;
  4579. for (var k in props) {
  4580. if (k === 'children' || k === 'text') {
  4581. continue;
  4582. }
  4583. if (props[k] !== node[k]) {
  4584. hasChanges = true; // Omit new properties from the old properties list
  4585. if (node.hasOwnProperty(k)) properties[k] = node[k]; // Omit properties that have been removed from the new properties list
  4586. if (props[k] != null) newProperties[k] = props[k];
  4587. }
  4588. }
  4589. if (hasChanges) {
  4590. editor.apply({
  4591. type: 'set_node',
  4592. path: path,
  4593. properties: properties,
  4594. newProperties: newProperties
  4595. });
  4596. }
  4597. }
  4598. } catch (err) {
  4599. _iterator2.e(err);
  4600. } finally {
  4601. _iterator2.f();
  4602. }
  4603. });
  4604. },
  4605. /**
  4606. * Split the nodes at a specific location.
  4607. */
  4608. splitNodes: function splitNodes(editor) {
  4609. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4610. Editor.withoutNormalizing(editor, function () {
  4611. var _options$mode7 = options.mode,
  4612. mode = _options$mode7 === void 0 ? 'lowest' : _options$mode7,
  4613. _options$voids7 = options.voids,
  4614. voids = _options$voids7 === void 0 ? false : _options$voids7;
  4615. var match = options.match,
  4616. _options$at6 = options.at,
  4617. at = _options$at6 === void 0 ? editor.selection : _options$at6,
  4618. _options$height = options.height,
  4619. height = _options$height === void 0 ? 0 : _options$height,
  4620. _options$always = options.always,
  4621. always = _options$always === void 0 ? false : _options$always;
  4622. if (match == null) {
  4623. match = function match(n) {
  4624. return Editor.isBlock(editor, n);
  4625. };
  4626. }
  4627. if (Range.isRange(at)) {
  4628. at = deleteRange(editor, at);
  4629. } // If the target is a path, the default height-skipping and position
  4630. // counters need to account for us potentially splitting at a non-leaf.
  4631. if (Path.isPath(at)) {
  4632. var path = at;
  4633. var point = Editor.point(editor, path);
  4634. var _Editor$parent3 = Editor.parent(editor, path),
  4635. _Editor$parent4 = _slicedToArray(_Editor$parent3, 1),
  4636. parent = _Editor$parent4[0];
  4637. match = function match(n) {
  4638. return n === parent;
  4639. };
  4640. height = point.path.length - path.length + 1;
  4641. at = point;
  4642. always = true;
  4643. }
  4644. if (!at) {
  4645. return;
  4646. }
  4647. var beforeRef = Editor.pointRef(editor, at, {
  4648. affinity: 'backward'
  4649. });
  4650. var _Editor$nodes5 = Editor.nodes(editor, {
  4651. at: at,
  4652. match: match,
  4653. mode: mode,
  4654. voids: voids
  4655. }),
  4656. _Editor$nodes6 = _slicedToArray(_Editor$nodes5, 1),
  4657. highest = _Editor$nodes6[0];
  4658. if (!highest) {
  4659. return;
  4660. }
  4661. var voidMatch = Editor["void"](editor, {
  4662. at: at,
  4663. mode: 'highest'
  4664. });
  4665. var nudge = 0;
  4666. if (!voids && voidMatch) {
  4667. var _voidMatch = _slicedToArray(voidMatch, 2),
  4668. voidNode = _voidMatch[0],
  4669. voidPath = _voidMatch[1];
  4670. if (Element.isElement(voidNode) && editor.isInline(voidNode)) {
  4671. var after = Editor.after(editor, voidPath);
  4672. if (!after) {
  4673. var text = {
  4674. text: ''
  4675. };
  4676. var afterPath = Path.next(voidPath);
  4677. Transforms.insertNodes(editor, text, {
  4678. at: afterPath,
  4679. voids: voids
  4680. });
  4681. after = Editor.point(editor, afterPath);
  4682. }
  4683. at = after;
  4684. always = true;
  4685. }
  4686. var siblingHeight = at.path.length - voidPath.length;
  4687. height = siblingHeight + 1;
  4688. always = true;
  4689. }
  4690. var afterRef = Editor.pointRef(editor, at);
  4691. var depth = at.path.length - height;
  4692. var _highest = _slicedToArray(highest, 2),
  4693. highestPath = _highest[1];
  4694. var lowestPath = at.path.slice(0, depth);
  4695. var position = height === 0 ? at.offset : at.path[depth] + nudge;
  4696. var _iterator3 = _createForOfIteratorHelper$1(Editor.levels(editor, {
  4697. at: lowestPath,
  4698. reverse: true,
  4699. voids: voids
  4700. })),
  4701. _step3;
  4702. try {
  4703. for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
  4704. var _step3$value = _slicedToArray(_step3.value, 2),
  4705. node = _step3$value[0],
  4706. _path2 = _step3$value[1];
  4707. var split = false;
  4708. if (_path2.length < highestPath.length || _path2.length === 0 || !voids && Editor.isVoid(editor, node)) {
  4709. break;
  4710. }
  4711. var _point2 = beforeRef.current;
  4712. var isEnd = Editor.isEnd(editor, _point2, _path2);
  4713. if (always || !beforeRef || !Editor.isEdge(editor, _point2, _path2)) {
  4714. split = true;
  4715. var properties = Node.extractProps(node);
  4716. editor.apply({
  4717. type: 'split_node',
  4718. path: _path2,
  4719. position: position,
  4720. properties: properties
  4721. });
  4722. }
  4723. position = _path2[_path2.length - 1] + (split || isEnd ? 1 : 0);
  4724. }
  4725. } catch (err) {
  4726. _iterator3.e(err);
  4727. } finally {
  4728. _iterator3.f();
  4729. }
  4730. if (options.at == null) {
  4731. var _point = afterRef.current || Editor.end(editor, []);
  4732. Transforms.select(editor, _point);
  4733. }
  4734. beforeRef.unref();
  4735. afterRef.unref();
  4736. });
  4737. },
  4738. /**
  4739. * Unset properties on the nodes at a location.
  4740. */
  4741. unsetNodes: function unsetNodes(editor, props) {
  4742. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4743. if (!Array.isArray(props)) {
  4744. props = [props];
  4745. }
  4746. var obj = {};
  4747. var _iterator4 = _createForOfIteratorHelper$1(props),
  4748. _step4;
  4749. try {
  4750. for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
  4751. var key = _step4.value;
  4752. obj[key] = null;
  4753. }
  4754. } catch (err) {
  4755. _iterator4.e(err);
  4756. } finally {
  4757. _iterator4.f();
  4758. }
  4759. Transforms.setNodes(editor, obj, options);
  4760. },
  4761. /**
  4762. * Unwrap the nodes at a location from a parent node, splitting the parent if
  4763. * necessary to ensure that only the content in the range is unwrapped.
  4764. */
  4765. unwrapNodes: function unwrapNodes(editor) {
  4766. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4767. Editor.withoutNormalizing(editor, function () {
  4768. var _options$mode8 = options.mode,
  4769. mode = _options$mode8 === void 0 ? 'lowest' : _options$mode8,
  4770. _options$split2 = options.split,
  4771. split = _options$split2 === void 0 ? false : _options$split2,
  4772. _options$voids8 = options.voids,
  4773. voids = _options$voids8 === void 0 ? false : _options$voids8;
  4774. var _options$at7 = options.at,
  4775. at = _options$at7 === void 0 ? editor.selection : _options$at7,
  4776. match = options.match;
  4777. if (!at) {
  4778. return;
  4779. }
  4780. if (match == null) {
  4781. match = Path.isPath(at) ? matchPath(editor, at) : function (n) {
  4782. return Editor.isBlock(editor, n);
  4783. };
  4784. }
  4785. if (Path.isPath(at)) {
  4786. at = Editor.range(editor, at);
  4787. }
  4788. var rangeRef = Range.isRange(at) ? Editor.rangeRef(editor, at) : null;
  4789. var matches = Editor.nodes(editor, {
  4790. at: at,
  4791. match: match,
  4792. mode: mode,
  4793. voids: voids
  4794. });
  4795. var pathRefs = Array.from(matches, function (_ref9) {
  4796. var _ref10 = _slicedToArray(_ref9, 2),
  4797. p = _ref10[1];
  4798. return Editor.pathRef(editor, p);
  4799. } // unwrapNode will call liftNode which does not support splitting the node when nested.
  4800. // If we do not reverse the order and call it from top to the bottom, it will remove all blocks
  4801. // that wrap target node. So we reverse the order.
  4802. ).reverse();
  4803. var _iterator5 = _createForOfIteratorHelper$1(pathRefs),
  4804. _step5;
  4805. try {
  4806. var _loop = function _loop() {
  4807. var pathRef = _step5.value;
  4808. var path = pathRef.unref();
  4809. var _Editor$node3 = Editor.node(editor, path),
  4810. _Editor$node4 = _slicedToArray(_Editor$node3, 1),
  4811. node = _Editor$node4[0];
  4812. var range = Editor.range(editor, path);
  4813. if (split && rangeRef) {
  4814. range = Range.intersection(rangeRef.current, range);
  4815. }
  4816. Transforms.liftNodes(editor, {
  4817. at: range,
  4818. match: function match(n) {
  4819. return Element.isAncestor(node) && node.children.includes(n);
  4820. },
  4821. voids: voids
  4822. });
  4823. };
  4824. for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
  4825. _loop();
  4826. }
  4827. } catch (err) {
  4828. _iterator5.e(err);
  4829. } finally {
  4830. _iterator5.f();
  4831. }
  4832. if (rangeRef) {
  4833. rangeRef.unref();
  4834. }
  4835. });
  4836. },
  4837. /**
  4838. * Wrap the nodes at a location in a new container node, splitting the edges
  4839. * of the range first to ensure that only the content in the range is wrapped.
  4840. */
  4841. wrapNodes: function wrapNodes(editor, element) {
  4842. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4843. Editor.withoutNormalizing(editor, function () {
  4844. var _options$mode9 = options.mode,
  4845. mode = _options$mode9 === void 0 ? 'lowest' : _options$mode9,
  4846. _options$split3 = options.split,
  4847. split = _options$split3 === void 0 ? false : _options$split3,
  4848. _options$voids9 = options.voids,
  4849. voids = _options$voids9 === void 0 ? false : _options$voids9;
  4850. var match = options.match,
  4851. _options$at8 = options.at,
  4852. at = _options$at8 === void 0 ? editor.selection : _options$at8;
  4853. if (!at) {
  4854. return;
  4855. }
  4856. if (match == null) {
  4857. if (Path.isPath(at)) {
  4858. match = matchPath(editor, at);
  4859. } else if (editor.isInline(element)) {
  4860. match = function match(n) {
  4861. return Editor.isInline(editor, n) || Text.isText(n);
  4862. };
  4863. } else {
  4864. match = function match(n) {
  4865. return Editor.isBlock(editor, n);
  4866. };
  4867. }
  4868. }
  4869. if (split && Range.isRange(at)) {
  4870. var _Range$edges7 = Range.edges(at),
  4871. _Range$edges8 = _slicedToArray(_Range$edges7, 2),
  4872. start = _Range$edges8[0],
  4873. end = _Range$edges8[1];
  4874. var rangeRef = Editor.rangeRef(editor, at, {
  4875. affinity: 'inward'
  4876. });
  4877. Transforms.splitNodes(editor, {
  4878. at: end,
  4879. match: match,
  4880. voids: voids
  4881. });
  4882. Transforms.splitNodes(editor, {
  4883. at: start,
  4884. match: match,
  4885. voids: voids
  4886. });
  4887. at = rangeRef.unref();
  4888. if (options.at == null) {
  4889. Transforms.select(editor, at);
  4890. }
  4891. }
  4892. var roots = Array.from(Editor.nodes(editor, {
  4893. at: at,
  4894. match: editor.isInline(element) ? function (n) {
  4895. return Editor.isBlock(editor, n);
  4896. } : function (n) {
  4897. return Editor.isEditor(n);
  4898. },
  4899. mode: 'lowest',
  4900. voids: voids
  4901. }));
  4902. for (var _i4 = 0, _roots = roots; _i4 < _roots.length; _i4++) {
  4903. var _roots$_i = _slicedToArray(_roots[_i4], 2),
  4904. rootPath = _roots$_i[1];
  4905. var a = Range.isRange(at) ? Range.intersection(at, Editor.range(editor, rootPath)) : at;
  4906. if (!a) {
  4907. continue;
  4908. }
  4909. var matches = Array.from(Editor.nodes(editor, {
  4910. at: a,
  4911. match: match,
  4912. mode: mode,
  4913. voids: voids
  4914. }));
  4915. if (matches.length > 0) {
  4916. var _ret = function () {
  4917. var _matches = _slicedToArray(matches, 1),
  4918. first = _matches[0];
  4919. var last = matches[matches.length - 1];
  4920. var _first = _slicedToArray(first, 2),
  4921. firstPath = _first[1];
  4922. var _last = _slicedToArray(last, 2),
  4923. lastPath = _last[1];
  4924. if (firstPath.length === 0 && lastPath.length === 0) {
  4925. // if there's no matching parent - usually means the node is an editor - don't do anything
  4926. return "continue";
  4927. }
  4928. var commonPath = Path.equals(firstPath, lastPath) ? Path.parent(firstPath) : Path.common(firstPath, lastPath);
  4929. var range = Editor.range(editor, firstPath, lastPath);
  4930. var commonNodeEntry = Editor.node(editor, commonPath);
  4931. var _commonNodeEntry = _slicedToArray(commonNodeEntry, 1),
  4932. commonNode = _commonNodeEntry[0];
  4933. var depth = commonPath.length + 1;
  4934. var wrapperPath = Path.next(lastPath.slice(0, depth));
  4935. var wrapper = _objectSpread$2(_objectSpread$2({}, element), {}, {
  4936. children: []
  4937. });
  4938. Transforms.insertNodes(editor, wrapper, {
  4939. at: wrapperPath,
  4940. voids: voids
  4941. });
  4942. Transforms.moveNodes(editor, {
  4943. at: range,
  4944. match: function match(n) {
  4945. return Element.isAncestor(commonNode) && commonNode.children.includes(n);
  4946. },
  4947. to: wrapperPath.concat(0),
  4948. voids: voids
  4949. });
  4950. }();
  4951. if (_ret === "continue") continue;
  4952. }
  4953. }
  4954. });
  4955. }
  4956. };
  4957. var hasSingleChildNest = function hasSingleChildNest(editor, node) {
  4958. if (Element.isElement(node)) {
  4959. var element = node;
  4960. if (Editor.isVoid(editor, node)) {
  4961. return true;
  4962. } else if (element.children.length === 1) {
  4963. return hasSingleChildNest(editor, element.children[0]);
  4964. } else {
  4965. return false;
  4966. }
  4967. } else if (Editor.isEditor(node)) {
  4968. return false;
  4969. } else {
  4970. return true;
  4971. }
  4972. };
  4973. /**
  4974. * Convert a range into a point by deleting it's content.
  4975. */
  4976. var deleteRange = function deleteRange(editor, range) {
  4977. if (Range.isCollapsed(range)) {
  4978. return range.anchor;
  4979. } else {
  4980. var _Range$edges9 = Range.edges(range),
  4981. _Range$edges10 = _slicedToArray(_Range$edges9, 2),
  4982. end = _Range$edges10[1];
  4983. var pointRef = Editor.pointRef(editor, end);
  4984. Transforms["delete"](editor, {
  4985. at: range
  4986. });
  4987. return pointRef.unref();
  4988. }
  4989. };
  4990. var matchPath = function matchPath(editor, path) {
  4991. var _Editor$node5 = Editor.node(editor, path),
  4992. _Editor$node6 = _slicedToArray(_Editor$node5, 1),
  4993. node = _Editor$node6[0];
  4994. return function (n) {
  4995. return n === node;
  4996. };
  4997. };
  4998. function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  4999. function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  5000. var SelectionTransforms = {
  5001. /**
  5002. * Collapse the selection.
  5003. */
  5004. collapse: function collapse(editor) {
  5005. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5006. var _options$edge = options.edge,
  5007. edge = _options$edge === void 0 ? 'anchor' : _options$edge;
  5008. var selection = editor.selection;
  5009. if (!selection) {
  5010. return;
  5011. } else if (edge === 'anchor') {
  5012. Transforms.select(editor, selection.anchor);
  5013. } else if (edge === 'focus') {
  5014. Transforms.select(editor, selection.focus);
  5015. } else if (edge === 'start') {
  5016. var _Range$edges = Range.edges(selection),
  5017. _Range$edges2 = _slicedToArray(_Range$edges, 1),
  5018. start = _Range$edges2[0];
  5019. Transforms.select(editor, start);
  5020. } else if (edge === 'end') {
  5021. var _Range$edges3 = Range.edges(selection),
  5022. _Range$edges4 = _slicedToArray(_Range$edges3, 2),
  5023. end = _Range$edges4[1];
  5024. Transforms.select(editor, end);
  5025. }
  5026. },
  5027. /**
  5028. * Unset the selection.
  5029. */
  5030. deselect: function deselect(editor) {
  5031. var selection = editor.selection;
  5032. if (selection) {
  5033. editor.apply({
  5034. type: 'set_selection',
  5035. properties: selection,
  5036. newProperties: null
  5037. });
  5038. }
  5039. },
  5040. /**
  5041. * Move the selection's point forward or backward.
  5042. */
  5043. move: function move(editor) {
  5044. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5045. var selection = editor.selection;
  5046. var _options$distance = options.distance,
  5047. distance = _options$distance === void 0 ? 1 : _options$distance,
  5048. _options$unit = options.unit,
  5049. unit = _options$unit === void 0 ? 'character' : _options$unit,
  5050. _options$reverse = options.reverse,
  5051. reverse = _options$reverse === void 0 ? false : _options$reverse;
  5052. var _options$edge2 = options.edge,
  5053. edge = _options$edge2 === void 0 ? null : _options$edge2;
  5054. if (!selection) {
  5055. return;
  5056. }
  5057. if (edge === 'start') {
  5058. edge = Range.isBackward(selection) ? 'focus' : 'anchor';
  5059. }
  5060. if (edge === 'end') {
  5061. edge = Range.isBackward(selection) ? 'anchor' : 'focus';
  5062. }
  5063. var anchor = selection.anchor,
  5064. focus = selection.focus;
  5065. var opts = {
  5066. distance: distance,
  5067. unit: unit
  5068. };
  5069. var props = {};
  5070. if (edge == null || edge === 'anchor') {
  5071. var point = reverse ? Editor.before(editor, anchor, opts) : Editor.after(editor, anchor, opts);
  5072. if (point) {
  5073. props.anchor = point;
  5074. }
  5075. }
  5076. if (edge == null || edge === 'focus') {
  5077. var _point = reverse ? Editor.before(editor, focus, opts) : Editor.after(editor, focus, opts);
  5078. if (_point) {
  5079. props.focus = _point;
  5080. }
  5081. }
  5082. Transforms.setSelection(editor, props);
  5083. },
  5084. /**
  5085. * Set the selection to a new value.
  5086. */
  5087. select: function select(editor, target) {
  5088. var selection = editor.selection;
  5089. target = Editor.range(editor, target);
  5090. if (selection) {
  5091. Transforms.setSelection(editor, target);
  5092. return;
  5093. }
  5094. if (!Range.isRange(target)) {
  5095. throw new Error("When setting the selection and the current selection is `null` you must provide at least an `anchor` and `focus`, but you passed: ".concat(JSON.stringify(target)));
  5096. }
  5097. editor.apply({
  5098. type: 'set_selection',
  5099. properties: selection,
  5100. newProperties: target
  5101. });
  5102. },
  5103. /**
  5104. * Set new properties on one of the selection's points.
  5105. */
  5106. setPoint: function setPoint(editor, props) {
  5107. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  5108. var selection = editor.selection;
  5109. var _options$edge3 = options.edge,
  5110. edge = _options$edge3 === void 0 ? 'both' : _options$edge3;
  5111. if (!selection) {
  5112. return;
  5113. }
  5114. if (edge === 'start') {
  5115. edge = Range.isBackward(selection) ? 'focus' : 'anchor';
  5116. }
  5117. if (edge === 'end') {
  5118. edge = Range.isBackward(selection) ? 'anchor' : 'focus';
  5119. }
  5120. var anchor = selection.anchor,
  5121. focus = selection.focus;
  5122. var point = edge === 'anchor' ? anchor : focus;
  5123. Transforms.setSelection(editor, _defineProperty({}, edge === 'anchor' ? 'anchor' : 'focus', _objectSpread$1(_objectSpread$1({}, point), props)));
  5124. },
  5125. /**
  5126. * Set new properties on the selection.
  5127. */
  5128. setSelection: function setSelection(editor, props) {
  5129. var selection = editor.selection;
  5130. var oldProps = {};
  5131. var newProps = {};
  5132. if (!selection) {
  5133. return;
  5134. }
  5135. for (var k in props) {
  5136. if (k === 'anchor' && props.anchor != null && !Point.equals(props.anchor, selection.anchor) || k === 'focus' && props.focus != null && !Point.equals(props.focus, selection.focus) || k !== 'anchor' && k !== 'focus' && props[k] !== selection[k]) {
  5137. oldProps[k] = selection[k];
  5138. newProps[k] = props[k];
  5139. }
  5140. }
  5141. if (Object.keys(oldProps).length > 0) {
  5142. editor.apply({
  5143. type: 'set_selection',
  5144. properties: oldProps,
  5145. newProperties: newProps
  5146. });
  5147. }
  5148. }
  5149. };
  5150. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  5151. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5152. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  5153. var TextTransforms = {
  5154. /**
  5155. * Delete content in the editor.
  5156. */
  5157. "delete": function _delete(editor) {
  5158. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5159. Editor.withoutNormalizing(editor, function () {
  5160. var _options$reverse = options.reverse,
  5161. reverse = _options$reverse === void 0 ? false : _options$reverse,
  5162. _options$unit = options.unit,
  5163. unit = _options$unit === void 0 ? 'character' : _options$unit,
  5164. _options$distance = options.distance,
  5165. distance = _options$distance === void 0 ? 1 : _options$distance,
  5166. _options$voids = options.voids,
  5167. voids = _options$voids === void 0 ? false : _options$voids;
  5168. var _options$at = options.at,
  5169. at = _options$at === void 0 ? editor.selection : _options$at,
  5170. _options$hanging = options.hanging,
  5171. hanging = _options$hanging === void 0 ? false : _options$hanging;
  5172. if (!at) {
  5173. return;
  5174. }
  5175. if (Range.isRange(at) && Range.isCollapsed(at)) {
  5176. at = at.anchor;
  5177. }
  5178. if (Point.isPoint(at)) {
  5179. var furthestVoid = Editor["void"](editor, {
  5180. at: at,
  5181. mode: 'highest'
  5182. });
  5183. if (!voids && furthestVoid) {
  5184. var _furthestVoid = _slicedToArray(furthestVoid, 2),
  5185. voidPath = _furthestVoid[1];
  5186. at = voidPath;
  5187. } else {
  5188. var opts = {
  5189. unit: unit,
  5190. distance: distance
  5191. };
  5192. var target = reverse ? Editor.before(editor, at, opts) || Editor.start(editor, []) : Editor.after(editor, at, opts) || Editor.end(editor, []);
  5193. at = {
  5194. anchor: at,
  5195. focus: target
  5196. };
  5197. hanging = true;
  5198. }
  5199. }
  5200. if (Path.isPath(at)) {
  5201. Transforms.removeNodes(editor, {
  5202. at: at,
  5203. voids: voids
  5204. });
  5205. return;
  5206. }
  5207. if (Range.isCollapsed(at)) {
  5208. return;
  5209. }
  5210. if (!hanging) {
  5211. var _Range$edges = Range.edges(at),
  5212. _Range$edges2 = _slicedToArray(_Range$edges, 2),
  5213. _end = _Range$edges2[1];
  5214. var endOfDoc = Editor.end(editor, []);
  5215. if (!Point.equals(_end, endOfDoc)) {
  5216. at = Editor.unhangRange(editor, at, {
  5217. voids: voids
  5218. });
  5219. }
  5220. }
  5221. var _Range$edges3 = Range.edges(at),
  5222. _Range$edges4 = _slicedToArray(_Range$edges3, 2),
  5223. start = _Range$edges4[0],
  5224. end = _Range$edges4[1];
  5225. var startBlock = Editor.above(editor, {
  5226. match: function match(n) {
  5227. return Editor.isBlock(editor, n);
  5228. },
  5229. at: start,
  5230. voids: voids
  5231. });
  5232. var endBlock = Editor.above(editor, {
  5233. match: function match(n) {
  5234. return Editor.isBlock(editor, n);
  5235. },
  5236. at: end,
  5237. voids: voids
  5238. });
  5239. var isAcrossBlocks = startBlock && endBlock && !Path.equals(startBlock[1], endBlock[1]);
  5240. var isSingleText = Path.equals(start.path, end.path);
  5241. var startVoid = voids ? null : Editor["void"](editor, {
  5242. at: start,
  5243. mode: 'highest'
  5244. });
  5245. var endVoid = voids ? null : Editor["void"](editor, {
  5246. at: end,
  5247. mode: 'highest'
  5248. }); // If the start or end points are inside an inline void, nudge them out.
  5249. if (startVoid) {
  5250. var before = Editor.before(editor, start);
  5251. if (before && startBlock && Path.isAncestor(startBlock[1], before.path)) {
  5252. start = before;
  5253. }
  5254. }
  5255. if (endVoid) {
  5256. var after = Editor.after(editor, end);
  5257. if (after && endBlock && Path.isAncestor(endBlock[1], after.path)) {
  5258. end = after;
  5259. }
  5260. } // Get the highest nodes that are completely inside the range, as well as
  5261. // the start and end nodes.
  5262. var matches = [];
  5263. var lastPath;
  5264. var _iterator = _createForOfIteratorHelper(Editor.nodes(editor, {
  5265. at: at,
  5266. voids: voids
  5267. })),
  5268. _step;
  5269. try {
  5270. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  5271. var entry = _step.value;
  5272. var _entry = _slicedToArray(entry, 2),
  5273. _node2 = _entry[0],
  5274. _path3 = _entry[1];
  5275. if (lastPath && Path.compare(_path3, lastPath) === 0) {
  5276. continue;
  5277. }
  5278. if (!voids && Editor.isVoid(editor, _node2) || !Path.isCommon(_path3, start.path) && !Path.isCommon(_path3, end.path)) {
  5279. matches.push(entry);
  5280. lastPath = _path3;
  5281. }
  5282. }
  5283. } catch (err) {
  5284. _iterator.e(err);
  5285. } finally {
  5286. _iterator.f();
  5287. }
  5288. var pathRefs = Array.from(matches, function (_ref) {
  5289. var _ref2 = _slicedToArray(_ref, 2),
  5290. p = _ref2[1];
  5291. return Editor.pathRef(editor, p);
  5292. });
  5293. var startRef = Editor.pointRef(editor, start);
  5294. var endRef = Editor.pointRef(editor, end);
  5295. if (!isSingleText && !startVoid) {
  5296. var _point = startRef.current;
  5297. var _Editor$leaf = Editor.leaf(editor, _point),
  5298. _Editor$leaf2 = _slicedToArray(_Editor$leaf, 1),
  5299. node = _Editor$leaf2[0];
  5300. var path = _point.path;
  5301. var _start = start,
  5302. offset = _start.offset;
  5303. var text = node.text.slice(offset);
  5304. if (text.length > 0) editor.apply({
  5305. type: 'remove_text',
  5306. path: path,
  5307. offset: offset,
  5308. text: text
  5309. });
  5310. }
  5311. for (var _i = 0, _pathRefs = pathRefs; _i < _pathRefs.length; _i++) {
  5312. var pathRef = _pathRefs[_i];
  5313. var _path = pathRef.unref();
  5314. Transforms.removeNodes(editor, {
  5315. at: _path,
  5316. voids: voids
  5317. });
  5318. }
  5319. if (!endVoid) {
  5320. var _point2 = endRef.current;
  5321. var _Editor$leaf3 = Editor.leaf(editor, _point2),
  5322. _Editor$leaf4 = _slicedToArray(_Editor$leaf3, 1),
  5323. _node = _Editor$leaf4[0];
  5324. var _path2 = _point2.path;
  5325. var _offset = isSingleText ? start.offset : 0;
  5326. var _text = _node.text.slice(_offset, end.offset);
  5327. if (_text.length > 0) editor.apply({
  5328. type: 'remove_text',
  5329. path: _path2,
  5330. offset: _offset,
  5331. text: _text
  5332. });
  5333. }
  5334. if (!isSingleText && isAcrossBlocks && endRef.current && startRef.current) {
  5335. Transforms.mergeNodes(editor, {
  5336. at: endRef.current,
  5337. hanging: true,
  5338. voids: voids
  5339. });
  5340. }
  5341. var point = reverse ? startRef.unref() || endRef.unref() : endRef.unref() || startRef.unref();
  5342. if (options.at == null && point) {
  5343. Transforms.select(editor, point);
  5344. }
  5345. });
  5346. },
  5347. /**
  5348. * Insert a fragment at a specific location in the editor.
  5349. */
  5350. insertFragment: function insertFragment(editor, fragment) {
  5351. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  5352. Editor.withoutNormalizing(editor, function () {
  5353. var _options$hanging2 = options.hanging,
  5354. hanging = _options$hanging2 === void 0 ? false : _options$hanging2,
  5355. _options$voids2 = options.voids,
  5356. voids = _options$voids2 === void 0 ? false : _options$voids2;
  5357. var _options$at2 = options.at,
  5358. at = _options$at2 === void 0 ? editor.selection : _options$at2;
  5359. if (!fragment.length) {
  5360. return;
  5361. }
  5362. if (!at) {
  5363. return;
  5364. } else if (Range.isRange(at)) {
  5365. if (!hanging) {
  5366. at = Editor.unhangRange(editor, at);
  5367. }
  5368. if (Range.isCollapsed(at)) {
  5369. at = at.anchor;
  5370. } else {
  5371. var _Range$edges5 = Range.edges(at),
  5372. _Range$edges6 = _slicedToArray(_Range$edges5, 2),
  5373. end = _Range$edges6[1];
  5374. if (!voids && Editor["void"](editor, {
  5375. at: end
  5376. })) {
  5377. return;
  5378. }
  5379. var pointRef = Editor.pointRef(editor, end);
  5380. Transforms["delete"](editor, {
  5381. at: at
  5382. });
  5383. at = pointRef.unref();
  5384. }
  5385. } else if (Path.isPath(at)) {
  5386. at = Editor.start(editor, at);
  5387. }
  5388. if (!voids && Editor["void"](editor, {
  5389. at: at
  5390. })) {
  5391. return;
  5392. } // If the insert point is at the edge of an inline node, move it outside
  5393. // instead since it will need to be split otherwise.
  5394. var inlineElementMatch = Editor.above(editor, {
  5395. at: at,
  5396. match: function match(n) {
  5397. return Editor.isInline(editor, n);
  5398. },
  5399. mode: 'highest',
  5400. voids: voids
  5401. });
  5402. if (inlineElementMatch) {
  5403. var _inlineElementMatch = _slicedToArray(inlineElementMatch, 2),
  5404. _inlinePath = _inlineElementMatch[1];
  5405. if (Editor.isEnd(editor, at, _inlinePath)) {
  5406. var after = Editor.after(editor, _inlinePath);
  5407. at = after;
  5408. } else if (Editor.isStart(editor, at, _inlinePath)) {
  5409. var before = Editor.before(editor, _inlinePath);
  5410. at = before;
  5411. }
  5412. }
  5413. var blockMatch = Editor.above(editor, {
  5414. match: function match(n) {
  5415. return Editor.isBlock(editor, n);
  5416. },
  5417. at: at,
  5418. voids: voids
  5419. });
  5420. var _blockMatch = _slicedToArray(blockMatch, 2),
  5421. blockPath = _blockMatch[1];
  5422. var isBlockStart = Editor.isStart(editor, at, blockPath);
  5423. var isBlockEnd = Editor.isEnd(editor, at, blockPath);
  5424. var isBlockEmpty = isBlockStart && isBlockEnd;
  5425. var mergeStart = !isBlockStart || isBlockStart && isBlockEnd;
  5426. var mergeEnd = !isBlockEnd;
  5427. var _Node$first = Node.first({
  5428. children: fragment
  5429. }, []),
  5430. _Node$first2 = _slicedToArray(_Node$first, 2),
  5431. firstPath = _Node$first2[1];
  5432. var _Node$last = Node.last({
  5433. children: fragment
  5434. }, []),
  5435. _Node$last2 = _slicedToArray(_Node$last, 2),
  5436. lastPath = _Node$last2[1];
  5437. var matches = [];
  5438. var matcher = function matcher(_ref3) {
  5439. var _ref4 = _slicedToArray(_ref3, 2),
  5440. n = _ref4[0],
  5441. p = _ref4[1];
  5442. var isRoot = p.length === 0;
  5443. if (isRoot) {
  5444. return false;
  5445. }
  5446. if (isBlockEmpty) {
  5447. return true;
  5448. }
  5449. if (mergeStart && Path.isAncestor(p, firstPath) && Element.isElement(n) && !editor.isVoid(n) && !editor.isInline(n)) {
  5450. return false;
  5451. }
  5452. if (mergeEnd && Path.isAncestor(p, lastPath) && Element.isElement(n) && !editor.isVoid(n) && !editor.isInline(n)) {
  5453. return false;
  5454. }
  5455. return true;
  5456. };
  5457. var _iterator2 = _createForOfIteratorHelper(Node.nodes({
  5458. children: fragment
  5459. }, {
  5460. pass: matcher
  5461. })),
  5462. _step2;
  5463. try {
  5464. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  5465. var entry = _step2.value;
  5466. if (matcher(entry)) {
  5467. matches.push(entry);
  5468. }
  5469. }
  5470. } catch (err) {
  5471. _iterator2.e(err);
  5472. } finally {
  5473. _iterator2.f();
  5474. }
  5475. var starts = [];
  5476. var middles = [];
  5477. var ends = [];
  5478. var starting = true;
  5479. var hasBlocks = false;
  5480. for (var _i2 = 0, _matches = matches; _i2 < _matches.length; _i2++) {
  5481. var _matches$_i = _slicedToArray(_matches[_i2], 1),
  5482. node = _matches$_i[0];
  5483. if (Element.isElement(node) && !editor.isInline(node)) {
  5484. starting = false;
  5485. hasBlocks = true;
  5486. middles.push(node);
  5487. } else if (starting) {
  5488. starts.push(node);
  5489. } else {
  5490. ends.push(node);
  5491. }
  5492. }
  5493. var _Editor$nodes = Editor.nodes(editor, {
  5494. at: at,
  5495. match: function match(n) {
  5496. return Text.isText(n) || Editor.isInline(editor, n);
  5497. },
  5498. mode: 'highest',
  5499. voids: voids
  5500. }),
  5501. _Editor$nodes2 = _slicedToArray(_Editor$nodes, 1),
  5502. inlineMatch = _Editor$nodes2[0];
  5503. var _inlineMatch = _slicedToArray(inlineMatch, 2),
  5504. inlinePath = _inlineMatch[1];
  5505. var isInlineStart = Editor.isStart(editor, at, inlinePath);
  5506. var isInlineEnd = Editor.isEnd(editor, at, inlinePath);
  5507. var middleRef = Editor.pathRef(editor, isBlockEnd ? Path.next(blockPath) : blockPath);
  5508. var endRef = Editor.pathRef(editor, isInlineEnd ? Path.next(inlinePath) : inlinePath);
  5509. var blockPathRef = Editor.pathRef(editor, blockPath);
  5510. Transforms.splitNodes(editor, {
  5511. at: at,
  5512. match: function match(n) {
  5513. return hasBlocks ? Editor.isBlock(editor, n) : Text.isText(n) || Editor.isInline(editor, n);
  5514. },
  5515. mode: hasBlocks ? 'lowest' : 'highest',
  5516. voids: voids
  5517. });
  5518. var startRef = Editor.pathRef(editor, !isInlineStart || isInlineStart && isInlineEnd ? Path.next(inlinePath) : inlinePath);
  5519. Transforms.insertNodes(editor, starts, {
  5520. at: startRef.current,
  5521. match: function match(n) {
  5522. return Text.isText(n) || Editor.isInline(editor, n);
  5523. },
  5524. mode: 'highest',
  5525. voids: voids
  5526. });
  5527. if (isBlockEmpty && middles.length) {
  5528. Transforms["delete"](editor, {
  5529. at: blockPathRef.unref(),
  5530. voids: voids
  5531. });
  5532. }
  5533. Transforms.insertNodes(editor, middles, {
  5534. at: middleRef.current,
  5535. match: function match(n) {
  5536. return Editor.isBlock(editor, n);
  5537. },
  5538. mode: 'lowest',
  5539. voids: voids
  5540. });
  5541. Transforms.insertNodes(editor, ends, {
  5542. at: endRef.current,
  5543. match: function match(n) {
  5544. return Text.isText(n) || Editor.isInline(editor, n);
  5545. },
  5546. mode: 'highest',
  5547. voids: voids
  5548. });
  5549. if (!options.at) {
  5550. var path;
  5551. if (ends.length > 0) {
  5552. path = Path.previous(endRef.current);
  5553. } else if (middles.length > 0) {
  5554. path = Path.previous(middleRef.current);
  5555. } else {
  5556. path = Path.previous(startRef.current);
  5557. }
  5558. var _end2 = Editor.end(editor, path);
  5559. Transforms.select(editor, _end2);
  5560. }
  5561. startRef.unref();
  5562. middleRef.unref();
  5563. endRef.unref();
  5564. });
  5565. },
  5566. /**
  5567. * Insert a string of text in the Editor.
  5568. */
  5569. insertText: function insertText(editor, text) {
  5570. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  5571. Editor.withoutNormalizing(editor, function () {
  5572. var _options$voids3 = options.voids,
  5573. voids = _options$voids3 === void 0 ? false : _options$voids3;
  5574. var _options$at3 = options.at,
  5575. at = _options$at3 === void 0 ? editor.selection : _options$at3;
  5576. if (!at) {
  5577. return;
  5578. }
  5579. if (Path.isPath(at)) {
  5580. at = Editor.range(editor, at);
  5581. }
  5582. if (Range.isRange(at)) {
  5583. if (Range.isCollapsed(at)) {
  5584. at = at.anchor;
  5585. } else {
  5586. var end = Range.end(at);
  5587. if (!voids && Editor["void"](editor, {
  5588. at: end
  5589. })) {
  5590. return;
  5591. }
  5592. var start = Range.start(at);
  5593. var pointRef = Editor.pointRef(editor, start);
  5594. Transforms["delete"](editor, {
  5595. at: at,
  5596. voids: voids
  5597. });
  5598. at = pointRef.unref();
  5599. Transforms.setSelection(editor, {
  5600. anchor: at,
  5601. focus: at
  5602. });
  5603. }
  5604. }
  5605. if (!voids && Editor["void"](editor, {
  5606. at: at
  5607. })) {
  5608. return;
  5609. }
  5610. var _at = at,
  5611. path = _at.path,
  5612. offset = _at.offset;
  5613. if (text.length > 0) editor.apply({
  5614. type: 'insert_text',
  5615. path: path,
  5616. offset: offset,
  5617. text: text
  5618. });
  5619. });
  5620. }
  5621. };
  5622. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  5623. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  5624. var Transforms = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, GeneralTransforms), NodeTransforms), SelectionTransforms), TextTransforms);
  5625. exports.Editor = Editor;
  5626. exports.Element = Element;
  5627. exports.Location = Location;
  5628. exports.Node = Node;
  5629. exports.Operation = Operation;
  5630. exports.Path = Path;
  5631. exports.PathRef = PathRef;
  5632. exports.Point = Point;
  5633. exports.PointRef = PointRef;
  5634. exports.Range = Range;
  5635. exports.RangeRef = RangeRef;
  5636. exports.Span = Span;
  5637. exports.Text = Text;
  5638. exports.Transforms = Transforms;
  5639. exports.createEditor = createEditor;
  5640. //# sourceMappingURL=index.js.map