index.es.js 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891
  1. import { isPlainObject } from 'is-plain-object';
  2. import { produce, createDraft, finishDraft, isDraft } from 'immer';
  3. function _defineProperty(obj, key, value) {
  4. if (key in obj) {
  5. Object.defineProperty(obj, key, {
  6. value: value,
  7. enumerable: true,
  8. configurable: true,
  9. writable: true
  10. });
  11. } else {
  12. obj[key] = value;
  13. }
  14. return obj;
  15. }
  16. var DIRTY_PATHS = new WeakMap();
  17. var DIRTY_PATH_KEYS = new WeakMap();
  18. var FLUSHING = new WeakMap();
  19. var NORMALIZING = new WeakMap();
  20. var PATH_REFS = new WeakMap();
  21. var POINT_REFS = new WeakMap();
  22. var RANGE_REFS = new WeakMap();
  23. 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; }
  24. 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; }
  25. /**
  26. * Create a new Slate `Editor` object.
  27. */
  28. var createEditor = () => {
  29. var editor = {
  30. children: [],
  31. operations: [],
  32. selection: null,
  33. marks: null,
  34. isInline: () => false,
  35. isVoid: () => false,
  36. onChange: () => {},
  37. apply: op => {
  38. for (var ref of Editor.pathRefs(editor)) {
  39. PathRef.transform(ref, op);
  40. }
  41. for (var _ref of Editor.pointRefs(editor)) {
  42. PointRef.transform(_ref, op);
  43. }
  44. for (var _ref2 of Editor.rangeRefs(editor)) {
  45. RangeRef.transform(_ref2, op);
  46. }
  47. var oldDirtyPaths = DIRTY_PATHS.get(editor) || [];
  48. var oldDirtyPathKeys = DIRTY_PATH_KEYS.get(editor) || new Set();
  49. var dirtyPaths;
  50. var dirtyPathKeys;
  51. var add = path => {
  52. if (path) {
  53. var key = path.join(',');
  54. if (!dirtyPathKeys.has(key)) {
  55. dirtyPathKeys.add(key);
  56. dirtyPaths.push(path);
  57. }
  58. }
  59. };
  60. if (Path.operationCanTransformPath(op)) {
  61. dirtyPaths = [];
  62. dirtyPathKeys = new Set();
  63. for (var path of oldDirtyPaths) {
  64. var newPath = Path.transform(path, op);
  65. add(newPath);
  66. }
  67. } else {
  68. dirtyPaths = oldDirtyPaths;
  69. dirtyPathKeys = oldDirtyPathKeys;
  70. }
  71. var newDirtyPaths = getDirtyPaths(op);
  72. for (var _path of newDirtyPaths) {
  73. add(_path);
  74. }
  75. DIRTY_PATHS.set(editor, dirtyPaths);
  76. DIRTY_PATH_KEYS.set(editor, dirtyPathKeys);
  77. Transforms.transform(editor, op);
  78. editor.operations.push(op);
  79. Editor.normalize(editor); // Clear any formats applied to the cursor if the selection changes.
  80. if (op.type === 'set_selection') {
  81. editor.marks = null;
  82. }
  83. if (!FLUSHING.get(editor)) {
  84. FLUSHING.set(editor, true);
  85. Promise.resolve().then(() => {
  86. FLUSHING.set(editor, false);
  87. editor.onChange();
  88. editor.operations = [];
  89. });
  90. }
  91. },
  92. addMark: (key, value) => {
  93. var {
  94. selection
  95. } = editor;
  96. if (selection) {
  97. if (Range.isExpanded(selection)) {
  98. Transforms.setNodes(editor, {
  99. [key]: value
  100. }, {
  101. match: Text.isText,
  102. split: true
  103. });
  104. } else {
  105. var marks = _objectSpread$9(_objectSpread$9({}, Editor.marks(editor) || {}), {}, {
  106. [key]: value
  107. });
  108. editor.marks = marks;
  109. if (!FLUSHING.get(editor)) {
  110. editor.onChange();
  111. }
  112. }
  113. }
  114. },
  115. deleteBackward: unit => {
  116. var {
  117. selection
  118. } = editor;
  119. if (selection && Range.isCollapsed(selection)) {
  120. Transforms.delete(editor, {
  121. unit,
  122. reverse: true
  123. });
  124. }
  125. },
  126. deleteForward: unit => {
  127. var {
  128. selection
  129. } = editor;
  130. if (selection && Range.isCollapsed(selection)) {
  131. Transforms.delete(editor, {
  132. unit
  133. });
  134. }
  135. },
  136. deleteFragment: direction => {
  137. var {
  138. selection
  139. } = editor;
  140. if (selection && Range.isExpanded(selection)) {
  141. Transforms.delete(editor, {
  142. reverse: direction === 'backward'
  143. });
  144. }
  145. },
  146. getFragment: () => {
  147. var {
  148. selection
  149. } = editor;
  150. if (selection) {
  151. return Node.fragment(editor, selection);
  152. }
  153. return [];
  154. },
  155. insertBreak: () => {
  156. Transforms.splitNodes(editor, {
  157. always: true
  158. });
  159. },
  160. insertFragment: fragment => {
  161. Transforms.insertFragment(editor, fragment);
  162. },
  163. insertNode: node => {
  164. Transforms.insertNodes(editor, node);
  165. },
  166. insertText: text => {
  167. var {
  168. selection,
  169. marks
  170. } = editor;
  171. if (selection) {
  172. if (marks) {
  173. var node = _objectSpread$9({
  174. text
  175. }, marks);
  176. Transforms.insertNodes(editor, node);
  177. } else {
  178. Transforms.insertText(editor, text);
  179. }
  180. editor.marks = null;
  181. }
  182. },
  183. normalizeNode: entry => {
  184. var [node, path] = entry; // There are no core normalizations for text nodes.
  185. if (Text.isText(node)) {
  186. return;
  187. } // Ensure that block and inline nodes have at least one text child.
  188. if (Element.isElement(node) && node.children.length === 0) {
  189. var child = {
  190. text: ''
  191. };
  192. Transforms.insertNodes(editor, child, {
  193. at: path.concat(0),
  194. voids: true
  195. });
  196. return;
  197. } // Determine whether the node should have block or inline children.
  198. 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
  199. // index that accounts for any added/removed nodes.
  200. var n = 0;
  201. for (var i = 0; i < node.children.length; i++, n++) {
  202. var currentNode = Node.get(editor, path);
  203. if (Text.isText(currentNode)) continue;
  204. var _child = node.children[i];
  205. var prev = currentNode.children[n - 1];
  206. var isLast = i === node.children.length - 1;
  207. var isInlineOrText = Text.isText(_child) || Element.isElement(_child) && editor.isInline(_child); // Only allow block nodes in the top-level children and parent blocks
  208. // that only contain block nodes. Similarly, only allow inline nodes in
  209. // other inline nodes, or parent blocks that only contain inlines and
  210. // text.
  211. if (isInlineOrText !== shouldHaveInlines) {
  212. Transforms.removeNodes(editor, {
  213. at: path.concat(n),
  214. voids: true
  215. });
  216. n--;
  217. } else if (Element.isElement(_child)) {
  218. // Ensure that inline nodes are surrounded by text nodes.
  219. if (editor.isInline(_child)) {
  220. if (prev == null || !Text.isText(prev)) {
  221. var newChild = {
  222. text: ''
  223. };
  224. Transforms.insertNodes(editor, newChild, {
  225. at: path.concat(n),
  226. voids: true
  227. });
  228. n++;
  229. } else if (isLast) {
  230. var _newChild = {
  231. text: ''
  232. };
  233. Transforms.insertNodes(editor, _newChild, {
  234. at: path.concat(n + 1),
  235. voids: true
  236. });
  237. n++;
  238. }
  239. }
  240. } else {
  241. // Merge adjacent text nodes that are empty or match.
  242. if (prev != null && Text.isText(prev)) {
  243. if (Text.equals(_child, prev, {
  244. loose: true
  245. })) {
  246. Transforms.mergeNodes(editor, {
  247. at: path.concat(n),
  248. voids: true
  249. });
  250. n--;
  251. } else if (prev.text === '') {
  252. Transforms.removeNodes(editor, {
  253. at: path.concat(n - 1),
  254. voids: true
  255. });
  256. n--;
  257. } else if (_child.text === '') {
  258. Transforms.removeNodes(editor, {
  259. at: path.concat(n),
  260. voids: true
  261. });
  262. n--;
  263. }
  264. }
  265. }
  266. }
  267. },
  268. removeMark: key => {
  269. var {
  270. selection
  271. } = editor;
  272. if (selection) {
  273. if (Range.isExpanded(selection)) {
  274. Transforms.unsetNodes(editor, key, {
  275. match: Text.isText,
  276. split: true
  277. });
  278. } else {
  279. var marks = _objectSpread$9({}, Editor.marks(editor) || {});
  280. delete marks[key];
  281. editor.marks = marks;
  282. if (!FLUSHING.get(editor)) {
  283. editor.onChange();
  284. }
  285. }
  286. }
  287. }
  288. };
  289. return editor;
  290. };
  291. /**
  292. * Get the "dirty" paths generated from an operation.
  293. */
  294. var getDirtyPaths = op => {
  295. switch (op.type) {
  296. case 'insert_text':
  297. case 'remove_text':
  298. case 'set_node':
  299. {
  300. var {
  301. path
  302. } = op;
  303. return Path.levels(path);
  304. }
  305. case 'insert_node':
  306. {
  307. var {
  308. node,
  309. path: _path2
  310. } = op;
  311. var levels = Path.levels(_path2);
  312. var descendants = Text.isText(node) ? [] : Array.from(Node.nodes(node), _ref3 => {
  313. var [, p] = _ref3;
  314. return _path2.concat(p);
  315. });
  316. return [...levels, ...descendants];
  317. }
  318. case 'merge_node':
  319. {
  320. var {
  321. path: _path3
  322. } = op;
  323. var ancestors = Path.ancestors(_path3);
  324. var previousPath = Path.previous(_path3);
  325. return [...ancestors, previousPath];
  326. }
  327. case 'move_node':
  328. {
  329. var {
  330. path: _path4,
  331. newPath
  332. } = op;
  333. if (Path.equals(_path4, newPath)) {
  334. return [];
  335. }
  336. var oldAncestors = [];
  337. var newAncestors = [];
  338. for (var ancestor of Path.ancestors(_path4)) {
  339. var p = Path.transform(ancestor, op);
  340. oldAncestors.push(p);
  341. }
  342. for (var _ancestor of Path.ancestors(newPath)) {
  343. var _p = Path.transform(_ancestor, op);
  344. newAncestors.push(_p);
  345. }
  346. var newParent = newAncestors[newAncestors.length - 1];
  347. var newIndex = newPath[newPath.length - 1];
  348. var resultPath = newParent.concat(newIndex);
  349. return [...oldAncestors, ...newAncestors, resultPath];
  350. }
  351. case 'remove_node':
  352. {
  353. var {
  354. path: _path5
  355. } = op;
  356. var _ancestors = Path.ancestors(_path5);
  357. return [..._ancestors];
  358. }
  359. case 'split_node':
  360. {
  361. var {
  362. path: _path6
  363. } = op;
  364. var _levels = Path.levels(_path6);
  365. var nextPath = Path.next(_path6);
  366. return [..._levels, nextPath];
  367. }
  368. default:
  369. {
  370. return [];
  371. }
  372. }
  373. };
  374. function _objectWithoutPropertiesLoose(source, excluded) {
  375. if (source == null) return {};
  376. var target = {};
  377. var sourceKeys = Object.keys(source);
  378. var key, i;
  379. for (i = 0; i < sourceKeys.length; i++) {
  380. key = sourceKeys[i];
  381. if (excluded.indexOf(key) >= 0) continue;
  382. target[key] = source[key];
  383. }
  384. return target;
  385. }
  386. function _objectWithoutProperties(source, excluded) {
  387. if (source == null) return {};
  388. var target = _objectWithoutPropertiesLoose(source, excluded);
  389. var key, i;
  390. if (Object.getOwnPropertySymbols) {
  391. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  392. for (i = 0; i < sourceSymbolKeys.length; i++) {
  393. key = sourceSymbolKeys[i];
  394. if (excluded.indexOf(key) >= 0) continue;
  395. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  396. target[key] = source[key];
  397. }
  398. }
  399. return target;
  400. }
  401. // Character (grapheme cluster) boundaries are determined according to
  402. // the default grapheme cluster boundary specification, extended grapheme clusters variant[1].
  403. //
  404. // References:
  405. //
  406. // [1] https://www.unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table
  407. // [2] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakProperty.txt
  408. // [3] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.html
  409. // [4] https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.txt
  410. /**
  411. * Get the distance to the end of the first character in a string of text.
  412. */
  413. var getCharacterDistance = function getCharacterDistance(str) {
  414. var isRTL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  415. var isLTR = !isRTL;
  416. var codepoints = isRTL ? codepointsIteratorRTL(str) : str;
  417. var left = CodepointType.None;
  418. var right = CodepointType.None;
  419. var distance = 0; // Evaluation of these conditions are deferred.
  420. var gb11 = null; // Is GB11 applicable?
  421. var gb12Or13 = null; // Is GB12 or GB13 applicable?
  422. for (var char of codepoints) {
  423. var code = char.codePointAt(0);
  424. if (!code) break;
  425. var type = getCodepointType(char, code);
  426. [left, right] = isLTR ? [right, type] : [type, left];
  427. if (intersects(left, CodepointType.ZWJ) && intersects(right, CodepointType.ExtPict)) {
  428. if (isLTR) {
  429. gb11 = endsWithEmojiZWJ(str.substring(0, distance));
  430. } else {
  431. gb11 = endsWithEmojiZWJ(str.substring(0, str.length - distance));
  432. }
  433. if (!gb11) break;
  434. }
  435. if (intersects(left, CodepointType.RI) && intersects(right, CodepointType.RI)) {
  436. if (gb12Or13 !== null) {
  437. gb12Or13 = !gb12Or13;
  438. } else {
  439. if (isLTR) {
  440. gb12Or13 = true;
  441. } else {
  442. gb12Or13 = endsWithOddNumberOfRIs(str.substring(0, str.length - distance));
  443. }
  444. }
  445. if (!gb12Or13) break;
  446. }
  447. if (left !== CodepointType.None && right !== CodepointType.None && isBoundaryPair(left, right)) {
  448. break;
  449. }
  450. distance += char.length;
  451. }
  452. return distance || 1;
  453. };
  454. var SPACE = /\s/;
  455. 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]/;
  456. var CHAMELEON = /['\u2018\u2019]/;
  457. /**
  458. * Get the distance to the end of the first word in a string of text.
  459. */
  460. var getWordDistance = function getWordDistance(text) {
  461. var isRTL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  462. var dist = 0;
  463. var started = false;
  464. while (text.length > 0) {
  465. var charDist = getCharacterDistance(text, isRTL);
  466. var [char, remaining] = splitByCharacterDistance(text, charDist, isRTL);
  467. if (isWordCharacter(char, remaining, isRTL)) {
  468. started = true;
  469. dist += charDist;
  470. } else if (!started) {
  471. dist += charDist;
  472. } else {
  473. break;
  474. }
  475. text = remaining;
  476. }
  477. return dist;
  478. };
  479. /**
  480. * Split a string in two parts at a given distance starting from the end when
  481. * `isRTL` is set to `true`.
  482. */
  483. var splitByCharacterDistance = (str, dist, isRTL) => {
  484. if (isRTL) {
  485. var at = str.length - dist;
  486. return [str.slice(at, str.length), str.slice(0, at)];
  487. }
  488. return [str.slice(0, dist), str.slice(dist)];
  489. };
  490. /**
  491. * Check if a character is a word character. The `remaining` argument is used
  492. * because sometimes you must read subsequent characters to truly determine it.
  493. */
  494. var isWordCharacter = function isWordCharacter(char, remaining) {
  495. var isRTL = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  496. if (SPACE.test(char)) {
  497. return false;
  498. } // Chameleons count as word characters as long as they're in a word, so
  499. // recurse to see if the next one is a word character or not.
  500. if (CHAMELEON.test(char)) {
  501. var charDist = getCharacterDistance(remaining, isRTL);
  502. var [nextChar, nextRemaining] = splitByCharacterDistance(remaining, charDist, isRTL);
  503. if (isWordCharacter(nextChar, nextRemaining, isRTL)) {
  504. return true;
  505. }
  506. }
  507. if (PUNCTUATION.test(char)) {
  508. return false;
  509. }
  510. return true;
  511. };
  512. /**
  513. * Iterate on codepoints from right to left.
  514. */
  515. var codepointsIteratorRTL = function* codepointsIteratorRTL(str) {
  516. var end = str.length - 1;
  517. for (var i = 0; i < str.length; i++) {
  518. var char1 = str.charAt(end - i);
  519. if (isLowSurrogate(char1.charCodeAt(0))) {
  520. var char2 = str.charAt(end - i - 1);
  521. if (isHighSurrogate(char2.charCodeAt(0))) {
  522. yield char2 + char1;
  523. i++;
  524. continue;
  525. }
  526. }
  527. yield char1;
  528. }
  529. };
  530. /**
  531. * Is `charCode` a high surrogate.
  532. *
  533. * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
  534. */
  535. var isHighSurrogate = charCode => {
  536. return charCode >= 0xd800 && charCode <= 0xdbff;
  537. };
  538. /**
  539. * Is `charCode` a low surrogate.
  540. *
  541. * https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
  542. */
  543. var isLowSurrogate = charCode => {
  544. return charCode >= 0xdc00 && charCode <= 0xdfff;
  545. };
  546. var CodepointType;
  547. (function (CodepointType) {
  548. CodepointType[CodepointType["None"] = 0] = "None";
  549. CodepointType[CodepointType["Extend"] = 1] = "Extend";
  550. CodepointType[CodepointType["ZWJ"] = 2] = "ZWJ";
  551. CodepointType[CodepointType["RI"] = 4] = "RI";
  552. CodepointType[CodepointType["Prepend"] = 8] = "Prepend";
  553. CodepointType[CodepointType["SpacingMark"] = 16] = "SpacingMark";
  554. CodepointType[CodepointType["L"] = 32] = "L";
  555. CodepointType[CodepointType["V"] = 64] = "V";
  556. CodepointType[CodepointType["T"] = 128] = "T";
  557. CodepointType[CodepointType["LV"] = 256] = "LV";
  558. CodepointType[CodepointType["LVT"] = 512] = "LVT";
  559. CodepointType[CodepointType["ExtPict"] = 1024] = "ExtPict";
  560. CodepointType[CodepointType["Any"] = 2048] = "Any";
  561. })(CodepointType || (CodepointType = {}));
  562. 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])$/;
  563. var rePrepend = /^(?:[\u0600-\u0605\u06DD\u070F\u0890\u0891\u08E2\u0D4E]|\uD804[\uDCBD\uDCCD\uDDC2\uDDC3]|\uD806[\uDD3F\uDD41\uDE3A\uDE84-\uDE89]|\uD807\uDD46)$/;
  564. 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])$/;
  565. var reL = /^[\u1100-\u115F\uA960-\uA97C]$/;
  566. var reV = /^[\u1160-\u11A7\uD7B0-\uD7C6]$/;
  567. var reT = /^[\u11A8-\u11FF\uD7CB-\uD7FB]$/;
  568. 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]$/;
  569. 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]$/;
  570. 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])$/;
  571. var getCodepointType = (char, code) => {
  572. var type = CodepointType.Any;
  573. if (char.search(reExtend) !== -1) {
  574. type |= CodepointType.Extend;
  575. }
  576. if (code === 0x200d) {
  577. type |= CodepointType.ZWJ;
  578. }
  579. if (code >= 0x1f1e6 && code <= 0x1f1ff) {
  580. type |= CodepointType.RI;
  581. }
  582. if (char.search(rePrepend) !== -1) {
  583. type |= CodepointType.Prepend;
  584. }
  585. if (char.search(reSpacingMark) !== -1) {
  586. type |= CodepointType.SpacingMark;
  587. }
  588. if (char.search(reL) !== -1) {
  589. type |= CodepointType.L;
  590. }
  591. if (char.search(reV) !== -1) {
  592. type |= CodepointType.V;
  593. }
  594. if (char.search(reT) !== -1) {
  595. type |= CodepointType.T;
  596. }
  597. if (char.search(reLV) !== -1) {
  598. type |= CodepointType.LV;
  599. }
  600. if (char.search(reLVT) !== -1) {
  601. type |= CodepointType.LVT;
  602. }
  603. if (char.search(reExtPict) !== -1) {
  604. type |= CodepointType.ExtPict;
  605. }
  606. return type;
  607. };
  608. function intersects(x, y) {
  609. return (x & y) !== 0;
  610. }
  611. var NonBoundaryPairs = [// GB6
  612. [CodepointType.L, CodepointType.L | CodepointType.V | CodepointType.LV | CodepointType.LVT], // GB7
  613. [CodepointType.LV | CodepointType.V, CodepointType.V | CodepointType.T], // GB8
  614. [CodepointType.LVT | CodepointType.T, CodepointType.T], // GB9
  615. [CodepointType.Any, CodepointType.Extend | CodepointType.ZWJ], // GB9a
  616. [CodepointType.Any, CodepointType.SpacingMark], // GB9b
  617. [CodepointType.Prepend, CodepointType.Any], // GB11
  618. [CodepointType.ZWJ, CodepointType.ExtPict], // GB12 and GB13
  619. [CodepointType.RI, CodepointType.RI]];
  620. function isBoundaryPair(left, right) {
  621. return NonBoundaryPairs.findIndex(r => intersects(left, r[0]) && intersects(right, r[1])) === -1;
  622. }
  623. 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$/;
  624. var endsWithEmojiZWJ = str => {
  625. return str.search(endingEmojiZWJ) !== -1;
  626. };
  627. var endingRIs = /(?:\uD83C[\uDDE6-\uDDFF])+$/g;
  628. var endsWithOddNumberOfRIs = str => {
  629. var match = str.match(endingRIs);
  630. if (match === null) {
  631. return false;
  632. } else {
  633. // A RI is represented by a surrogate pair.
  634. var numRIs = match[0].length / 2;
  635. return numRIs % 2 === 1;
  636. }
  637. };
  638. /**
  639. * Shared the function with isElementType utility
  640. */
  641. var isElement = value => {
  642. return isPlainObject(value) && Node.isNodeList(value.children) && !Editor.isEditor(value);
  643. };
  644. var Element = {
  645. /**
  646. * Check if a value implements the 'Ancestor' interface.
  647. */
  648. isAncestor(value) {
  649. return isPlainObject(value) && Node.isNodeList(value.children);
  650. },
  651. /**
  652. * Check if a value implements the `Element` interface.
  653. */
  654. isElement,
  655. /**
  656. * Check if a value is an array of `Element` objects.
  657. */
  658. isElementList(value) {
  659. return Array.isArray(value) && value.every(val => Element.isElement(val));
  660. },
  661. /**
  662. * Check if a set of props is a partial of Element.
  663. */
  664. isElementProps(props) {
  665. return props.children !== undefined;
  666. },
  667. /**
  668. * Check if a value implements the `Element` interface and has elementKey with selected value.
  669. * Default it check to `type` key value
  670. */
  671. isElementType: function isElementType(value, elementVal) {
  672. var elementKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'type';
  673. return isElement(value) && value[elementKey] === elementVal;
  674. },
  675. /**
  676. * Check if an element matches set of properties.
  677. *
  678. * Note: this checks custom properties, and it does not ensure that any
  679. * children are equivalent.
  680. */
  681. matches(element, props) {
  682. for (var key in props) {
  683. if (key === 'children') {
  684. continue;
  685. }
  686. if (element[key] !== props[key]) {
  687. return false;
  688. }
  689. }
  690. return true;
  691. }
  692. };
  693. var _excluded$4 = ["text"],
  694. _excluded2$3 = ["text"];
  695. 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; }
  696. 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; }
  697. var IS_EDITOR_CACHE = new WeakMap();
  698. var Editor = {
  699. /**
  700. * Get the ancestor above a location in the document.
  701. */
  702. above(editor) {
  703. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  704. var {
  705. voids = false,
  706. mode = 'lowest',
  707. at = editor.selection,
  708. match
  709. } = options;
  710. if (!at) {
  711. return;
  712. }
  713. var path = Editor.path(editor, at);
  714. var reverse = mode === 'lowest';
  715. for (var [n, p] of Editor.levels(editor, {
  716. at: path,
  717. voids,
  718. match,
  719. reverse
  720. })) {
  721. if (!Text.isText(n) && !Path.equals(path, p)) {
  722. return [n, p];
  723. }
  724. }
  725. },
  726. /**
  727. * Add a custom property to the leaf text nodes in the current selection.
  728. *
  729. * If the selection is currently collapsed, the marks will be added to the
  730. * `editor.marks` property instead, and applied when text is inserted next.
  731. */
  732. addMark(editor, key, value) {
  733. editor.addMark(key, value);
  734. },
  735. /**
  736. * Get the point after a location.
  737. */
  738. after(editor, at) {
  739. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  740. var anchor = Editor.point(editor, at, {
  741. edge: 'end'
  742. });
  743. var focus = Editor.end(editor, []);
  744. var range = {
  745. anchor,
  746. focus
  747. };
  748. var {
  749. distance = 1
  750. } = options;
  751. var d = 0;
  752. var target;
  753. for (var p of Editor.positions(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  754. at: range
  755. }))) {
  756. if (d > distance) {
  757. break;
  758. }
  759. if (d !== 0) {
  760. target = p;
  761. }
  762. d++;
  763. }
  764. return target;
  765. },
  766. /**
  767. * Get the point before a location.
  768. */
  769. before(editor, at) {
  770. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  771. var anchor = Editor.start(editor, []);
  772. var focus = Editor.point(editor, at, {
  773. edge: 'start'
  774. });
  775. var range = {
  776. anchor,
  777. focus
  778. };
  779. var {
  780. distance = 1
  781. } = options;
  782. var d = 0;
  783. var target;
  784. for (var p of Editor.positions(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  785. at: range,
  786. reverse: true
  787. }))) {
  788. if (d > distance) {
  789. break;
  790. }
  791. if (d !== 0) {
  792. target = p;
  793. }
  794. d++;
  795. }
  796. return target;
  797. },
  798. /**
  799. * Delete content in the editor backward from the current selection.
  800. */
  801. deleteBackward(editor) {
  802. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  803. var {
  804. unit = 'character'
  805. } = options;
  806. editor.deleteBackward(unit);
  807. },
  808. /**
  809. * Delete content in the editor forward from the current selection.
  810. */
  811. deleteForward(editor) {
  812. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  813. var {
  814. unit = 'character'
  815. } = options;
  816. editor.deleteForward(unit);
  817. },
  818. /**
  819. * Delete the content in the current selection.
  820. */
  821. deleteFragment(editor) {
  822. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  823. var {
  824. direction = 'forward'
  825. } = options;
  826. editor.deleteFragment(direction);
  827. },
  828. /**
  829. * Get the start and end points of a location.
  830. */
  831. edges(editor, at) {
  832. return [Editor.start(editor, at), Editor.end(editor, at)];
  833. },
  834. /**
  835. * Get the end point of a location.
  836. */
  837. end(editor, at) {
  838. return Editor.point(editor, at, {
  839. edge: 'end'
  840. });
  841. },
  842. /**
  843. * Get the first node at a location.
  844. */
  845. first(editor, at) {
  846. var path = Editor.path(editor, at, {
  847. edge: 'start'
  848. });
  849. return Editor.node(editor, path);
  850. },
  851. /**
  852. * Get the fragment at a location.
  853. */
  854. fragment(editor, at) {
  855. var range = Editor.range(editor, at);
  856. var fragment = Node.fragment(editor, range);
  857. return fragment;
  858. },
  859. /**
  860. * Check if a node has block children.
  861. */
  862. hasBlocks(editor, element) {
  863. return element.children.some(n => Editor.isBlock(editor, n));
  864. },
  865. /**
  866. * Check if a node has inline and text children.
  867. */
  868. hasInlines(editor, element) {
  869. return element.children.some(n => Text.isText(n) || Editor.isInline(editor, n));
  870. },
  871. /**
  872. * Check if a node has text children.
  873. */
  874. hasTexts(editor, element) {
  875. return element.children.every(n => Text.isText(n));
  876. },
  877. /**
  878. * Insert a block break at the current selection.
  879. *
  880. * If the selection is currently expanded, it will be deleted first.
  881. */
  882. insertBreak(editor) {
  883. editor.insertBreak();
  884. },
  885. /**
  886. * Insert a fragment at the current selection.
  887. *
  888. * If the selection is currently expanded, it will be deleted first.
  889. */
  890. insertFragment(editor, fragment) {
  891. editor.insertFragment(fragment);
  892. },
  893. /**
  894. * Insert a node at the current selection.
  895. *
  896. * If the selection is currently expanded, it will be deleted first.
  897. */
  898. insertNode(editor, node) {
  899. editor.insertNode(node);
  900. },
  901. /**
  902. * Insert text at the current selection.
  903. *
  904. * If the selection is currently expanded, it will be deleted first.
  905. */
  906. insertText(editor, text) {
  907. editor.insertText(text);
  908. },
  909. /**
  910. * Check if a value is a block `Element` object.
  911. */
  912. isBlock(editor, value) {
  913. return Element.isElement(value) && !editor.isInline(value);
  914. },
  915. /**
  916. * Check if a value is an `Editor` object.
  917. */
  918. isEditor(value) {
  919. if (!isPlainObject(value)) return false;
  920. var cachedIsEditor = IS_EDITOR_CACHE.get(value);
  921. if (cachedIsEditor !== undefined) {
  922. return cachedIsEditor;
  923. }
  924. 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(value.marks)) && (value.selection === null || Range.isRange(value.selection)) && Node.isNodeList(value.children) && Operation.isOperationList(value.operations);
  925. IS_EDITOR_CACHE.set(value, isEditor);
  926. return isEditor;
  927. },
  928. /**
  929. * Check if a point is the end point of a location.
  930. */
  931. isEnd(editor, point, at) {
  932. var end = Editor.end(editor, at);
  933. return Point.equals(point, end);
  934. },
  935. /**
  936. * Check if a point is an edge of a location.
  937. */
  938. isEdge(editor, point, at) {
  939. return Editor.isStart(editor, point, at) || Editor.isEnd(editor, point, at);
  940. },
  941. /**
  942. * Check if an element is empty, accounting for void nodes.
  943. */
  944. isEmpty(editor, element) {
  945. var {
  946. children
  947. } = element;
  948. var [first] = children;
  949. return children.length === 0 || children.length === 1 && Text.isText(first) && first.text === '' && !editor.isVoid(element);
  950. },
  951. /**
  952. * Check if a value is an inline `Element` object.
  953. */
  954. isInline(editor, value) {
  955. return Element.isElement(value) && editor.isInline(value);
  956. },
  957. /**
  958. * Check if the editor is currently normalizing after each operation.
  959. */
  960. isNormalizing(editor) {
  961. var isNormalizing = NORMALIZING.get(editor);
  962. return isNormalizing === undefined ? true : isNormalizing;
  963. },
  964. /**
  965. * Check if a point is the start point of a location.
  966. */
  967. isStart(editor, point, at) {
  968. // PERF: If the offset isn't `0` we know it's not the start.
  969. if (point.offset !== 0) {
  970. return false;
  971. }
  972. var start = Editor.start(editor, at);
  973. return Point.equals(point, start);
  974. },
  975. /**
  976. * Check if a value is a void `Element` object.
  977. */
  978. isVoid(editor, value) {
  979. return Element.isElement(value) && editor.isVoid(value);
  980. },
  981. /**
  982. * Get the last node at a location.
  983. */
  984. last(editor, at) {
  985. var path = Editor.path(editor, at, {
  986. edge: 'end'
  987. });
  988. return Editor.node(editor, path);
  989. },
  990. /**
  991. * Get the leaf text node at a location.
  992. */
  993. leaf(editor, at) {
  994. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  995. var path = Editor.path(editor, at, options);
  996. var node = Node.leaf(editor, path);
  997. return [node, path];
  998. },
  999. /**
  1000. * Iterate through all of the levels at a location.
  1001. */
  1002. *levels(editor) {
  1003. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1004. var {
  1005. at = editor.selection,
  1006. reverse = false,
  1007. voids = false
  1008. } = options;
  1009. var {
  1010. match
  1011. } = options;
  1012. if (match == null) {
  1013. match = () => true;
  1014. }
  1015. if (!at) {
  1016. return;
  1017. }
  1018. var levels = [];
  1019. var path = Editor.path(editor, at);
  1020. for (var [n, p] of Node.levels(editor, path)) {
  1021. if (!match(n, p)) {
  1022. continue;
  1023. }
  1024. levels.push([n, p]);
  1025. if (!voids && Editor.isVoid(editor, n)) {
  1026. break;
  1027. }
  1028. }
  1029. if (reverse) {
  1030. levels.reverse();
  1031. }
  1032. yield* levels;
  1033. },
  1034. /**
  1035. * Get the marks that would be added to text at the current selection.
  1036. */
  1037. marks(editor) {
  1038. var {
  1039. marks,
  1040. selection
  1041. } = editor;
  1042. if (!selection) {
  1043. return null;
  1044. }
  1045. if (marks) {
  1046. return marks;
  1047. }
  1048. if (Range.isExpanded(selection)) {
  1049. var [match] = Editor.nodes(editor, {
  1050. match: Text.isText
  1051. });
  1052. if (match) {
  1053. var [_node] = match;
  1054. var _rest = _objectWithoutProperties(_node, _excluded$4);
  1055. return _rest;
  1056. } else {
  1057. return {};
  1058. }
  1059. }
  1060. var {
  1061. anchor
  1062. } = selection;
  1063. var {
  1064. path
  1065. } = anchor;
  1066. var [node] = Editor.leaf(editor, path);
  1067. if (anchor.offset === 0) {
  1068. var prev = Editor.previous(editor, {
  1069. at: path,
  1070. match: Text.isText
  1071. });
  1072. var block = Editor.above(editor, {
  1073. match: n => Editor.isBlock(editor, n)
  1074. });
  1075. if (prev && block) {
  1076. var [prevNode, prevPath] = prev;
  1077. var [, blockPath] = block;
  1078. if (Path.isAncestor(blockPath, prevPath)) {
  1079. node = prevNode;
  1080. }
  1081. }
  1082. }
  1083. var rest = _objectWithoutProperties(node, _excluded2$3);
  1084. return rest;
  1085. },
  1086. /**
  1087. * Get the matching node in the branch of the document after a location.
  1088. */
  1089. next(editor) {
  1090. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1091. var {
  1092. mode = 'lowest',
  1093. voids = false
  1094. } = options;
  1095. var {
  1096. match,
  1097. at = editor.selection
  1098. } = options;
  1099. if (!at) {
  1100. return;
  1101. }
  1102. var pointAfterLocation = Editor.after(editor, at, {
  1103. voids
  1104. });
  1105. if (!pointAfterLocation) return;
  1106. var [, to] = Editor.last(editor, []);
  1107. var span = [pointAfterLocation.path, to];
  1108. if (Path.isPath(at) && at.length === 0) {
  1109. throw new Error("Cannot get the next node from the root node!");
  1110. }
  1111. if (match == null) {
  1112. if (Path.isPath(at)) {
  1113. var [parent] = Editor.parent(editor, at);
  1114. match = n => parent.children.includes(n);
  1115. } else {
  1116. match = () => true;
  1117. }
  1118. }
  1119. var [next] = Editor.nodes(editor, {
  1120. at: span,
  1121. match,
  1122. mode,
  1123. voids
  1124. });
  1125. return next;
  1126. },
  1127. /**
  1128. * Get the node at a location.
  1129. */
  1130. node(editor, at) {
  1131. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1132. var path = Editor.path(editor, at, options);
  1133. var node = Node.get(editor, path);
  1134. return [node, path];
  1135. },
  1136. /**
  1137. * Iterate through all of the nodes in the Editor.
  1138. */
  1139. *nodes(editor) {
  1140. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1141. var {
  1142. at = editor.selection,
  1143. mode = 'all',
  1144. universal = false,
  1145. reverse = false,
  1146. voids = false
  1147. } = options;
  1148. var {
  1149. match
  1150. } = options;
  1151. if (!match) {
  1152. match = () => true;
  1153. }
  1154. if (!at) {
  1155. return;
  1156. }
  1157. var from;
  1158. var to;
  1159. if (Span.isSpan(at)) {
  1160. from = at[0];
  1161. to = at[1];
  1162. } else {
  1163. var first = Editor.path(editor, at, {
  1164. edge: 'start'
  1165. });
  1166. var last = Editor.path(editor, at, {
  1167. edge: 'end'
  1168. });
  1169. from = reverse ? last : first;
  1170. to = reverse ? first : last;
  1171. }
  1172. var nodeEntries = Node.nodes(editor, {
  1173. reverse,
  1174. from,
  1175. to,
  1176. pass: _ref => {
  1177. var [n] = _ref;
  1178. return voids ? false : Editor.isVoid(editor, n);
  1179. }
  1180. });
  1181. var matches = [];
  1182. var hit;
  1183. for (var [node, path] of nodeEntries) {
  1184. var isLower = hit && Path.compare(path, hit[1]) === 0; // In highest mode any node lower than the last hit is not a match.
  1185. if (mode === 'highest' && isLower) {
  1186. continue;
  1187. }
  1188. if (!match(node, path)) {
  1189. // If we've arrived at a leaf text node that is not lower than the last
  1190. // hit, then we've found a branch that doesn't include a match, which
  1191. // means the match is not universal.
  1192. if (universal && !isLower && Text.isText(node)) {
  1193. return;
  1194. } else {
  1195. continue;
  1196. }
  1197. } // If there's a match and it's lower than the last, update the hit.
  1198. if (mode === 'lowest' && isLower) {
  1199. hit = [node, path];
  1200. continue;
  1201. } // In lowest mode we emit the last hit, once it's guaranteed lowest.
  1202. var emit = mode === 'lowest' ? hit : [node, path];
  1203. if (emit) {
  1204. if (universal) {
  1205. matches.push(emit);
  1206. } else {
  1207. yield emit;
  1208. }
  1209. }
  1210. hit = [node, path];
  1211. } // Since lowest is always emitting one behind, catch up at the end.
  1212. if (mode === 'lowest' && hit) {
  1213. if (universal) {
  1214. matches.push(hit);
  1215. } else {
  1216. yield hit;
  1217. }
  1218. } // Universal defers to ensure that the match occurs in every branch, so we
  1219. // yield all of the matches after iterating.
  1220. if (universal) {
  1221. yield* matches;
  1222. }
  1223. },
  1224. /**
  1225. * Normalize any dirty objects in the editor.
  1226. */
  1227. normalize(editor) {
  1228. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1229. var {
  1230. force = false
  1231. } = options;
  1232. var getDirtyPaths = editor => {
  1233. return DIRTY_PATHS.get(editor) || [];
  1234. };
  1235. var getDirtyPathKeys = editor => {
  1236. return DIRTY_PATH_KEYS.get(editor) || new Set();
  1237. };
  1238. var popDirtyPath = editor => {
  1239. var path = getDirtyPaths(editor).pop();
  1240. var key = path.join(',');
  1241. getDirtyPathKeys(editor).delete(key);
  1242. return path;
  1243. };
  1244. if (!Editor.isNormalizing(editor)) {
  1245. return;
  1246. }
  1247. if (force) {
  1248. var allPaths = Array.from(Node.nodes(editor), _ref2 => {
  1249. var [, p] = _ref2;
  1250. return p;
  1251. });
  1252. var allPathKeys = new Set(allPaths.map(p => p.join(',')));
  1253. DIRTY_PATHS.set(editor, allPaths);
  1254. DIRTY_PATH_KEYS.set(editor, allPathKeys);
  1255. }
  1256. if (getDirtyPaths(editor).length === 0) {
  1257. return;
  1258. }
  1259. Editor.withoutNormalizing(editor, () => {
  1260. /*
  1261. Fix dirty elements with no children.
  1262. editor.normalizeNode() does fix this, but some normalization fixes also require it to work.
  1263. Running an initial pass avoids the catch-22 race condition.
  1264. */
  1265. for (var dirtyPath of getDirtyPaths(editor)) {
  1266. if (Node.has(editor, dirtyPath)) {
  1267. var entry = Editor.node(editor, dirtyPath);
  1268. var [node, _] = entry;
  1269. /*
  1270. The default normalizer inserts an empty text node in this scenario, but it can be customised.
  1271. So there is some risk here.
  1272. As long as the normalizer only inserts child nodes for this case it is safe to do in any order;
  1273. by definition adding children to an empty node can't cause other paths to change.
  1274. */
  1275. if (Element.isElement(node) && node.children.length === 0) {
  1276. editor.normalizeNode(entry);
  1277. }
  1278. }
  1279. }
  1280. var max = getDirtyPaths(editor).length * 42; // HACK: better way?
  1281. var m = 0;
  1282. while (getDirtyPaths(editor).length !== 0) {
  1283. if (m > max) {
  1284. 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 "));
  1285. }
  1286. var _dirtyPath = popDirtyPath(editor); // If the node doesn't exist in the tree, it does not need to be normalized.
  1287. if (Node.has(editor, _dirtyPath)) {
  1288. var _entry = Editor.node(editor, _dirtyPath);
  1289. editor.normalizeNode(_entry);
  1290. }
  1291. m++;
  1292. }
  1293. });
  1294. },
  1295. /**
  1296. * Get the parent node of a location.
  1297. */
  1298. parent(editor, at) {
  1299. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1300. var path = Editor.path(editor, at, options);
  1301. var parentPath = Path.parent(path);
  1302. var entry = Editor.node(editor, parentPath);
  1303. return entry;
  1304. },
  1305. /**
  1306. * Get the path of a location.
  1307. */
  1308. path(editor, at) {
  1309. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1310. var {
  1311. depth,
  1312. edge
  1313. } = options;
  1314. if (Path.isPath(at)) {
  1315. if (edge === 'start') {
  1316. var [, firstPath] = Node.first(editor, at);
  1317. at = firstPath;
  1318. } else if (edge === 'end') {
  1319. var [, lastPath] = Node.last(editor, at);
  1320. at = lastPath;
  1321. }
  1322. }
  1323. if (Range.isRange(at)) {
  1324. if (edge === 'start') {
  1325. at = Range.start(at);
  1326. } else if (edge === 'end') {
  1327. at = Range.end(at);
  1328. } else {
  1329. at = Path.common(at.anchor.path, at.focus.path);
  1330. }
  1331. }
  1332. if (Point.isPoint(at)) {
  1333. at = at.path;
  1334. }
  1335. if (depth != null) {
  1336. at = at.slice(0, depth);
  1337. }
  1338. return at;
  1339. },
  1340. hasPath(editor, path) {
  1341. return Node.has(editor, path);
  1342. },
  1343. /**
  1344. * Create a mutable ref for a `Path` object, which will stay in sync as new
  1345. * operations are applied to the editor.
  1346. */
  1347. pathRef(editor, path) {
  1348. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1349. var {
  1350. affinity = 'forward'
  1351. } = options;
  1352. var ref = {
  1353. current: path,
  1354. affinity,
  1355. unref() {
  1356. var {
  1357. current
  1358. } = ref;
  1359. var pathRefs = Editor.pathRefs(editor);
  1360. pathRefs.delete(ref);
  1361. ref.current = null;
  1362. return current;
  1363. }
  1364. };
  1365. var refs = Editor.pathRefs(editor);
  1366. refs.add(ref);
  1367. return ref;
  1368. },
  1369. /**
  1370. * Get the set of currently tracked path refs of the editor.
  1371. */
  1372. pathRefs(editor) {
  1373. var refs = PATH_REFS.get(editor);
  1374. if (!refs) {
  1375. refs = new Set();
  1376. PATH_REFS.set(editor, refs);
  1377. }
  1378. return refs;
  1379. },
  1380. /**
  1381. * Get the start or end point of a location.
  1382. */
  1383. point(editor, at) {
  1384. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1385. var {
  1386. edge = 'start'
  1387. } = options;
  1388. if (Path.isPath(at)) {
  1389. var path;
  1390. if (edge === 'end') {
  1391. var [, lastPath] = Node.last(editor, at);
  1392. path = lastPath;
  1393. } else {
  1394. var [, firstPath] = Node.first(editor, at);
  1395. path = firstPath;
  1396. }
  1397. var node = Node.get(editor, path);
  1398. if (!Text.isText(node)) {
  1399. throw new Error("Cannot get the ".concat(edge, " point in the node at path [").concat(at, "] because it has no ").concat(edge, " text node."));
  1400. }
  1401. return {
  1402. path,
  1403. offset: edge === 'end' ? node.text.length : 0
  1404. };
  1405. }
  1406. if (Range.isRange(at)) {
  1407. var [start, end] = Range.edges(at);
  1408. return edge === 'start' ? start : end;
  1409. }
  1410. return at;
  1411. },
  1412. /**
  1413. * Create a mutable ref for a `Point` object, which will stay in sync as new
  1414. * operations are applied to the editor.
  1415. */
  1416. pointRef(editor, point) {
  1417. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1418. var {
  1419. affinity = 'forward'
  1420. } = options;
  1421. var ref = {
  1422. current: point,
  1423. affinity,
  1424. unref() {
  1425. var {
  1426. current
  1427. } = ref;
  1428. var pointRefs = Editor.pointRefs(editor);
  1429. pointRefs.delete(ref);
  1430. ref.current = null;
  1431. return current;
  1432. }
  1433. };
  1434. var refs = Editor.pointRefs(editor);
  1435. refs.add(ref);
  1436. return ref;
  1437. },
  1438. /**
  1439. * Get the set of currently tracked point refs of the editor.
  1440. */
  1441. pointRefs(editor) {
  1442. var refs = POINT_REFS.get(editor);
  1443. if (!refs) {
  1444. refs = new Set();
  1445. POINT_REFS.set(editor, refs);
  1446. }
  1447. return refs;
  1448. },
  1449. /**
  1450. * Return all the positions in `at` range where a `Point` can be placed.
  1451. *
  1452. * By default, moves forward by individual offsets at a time, but
  1453. * the `unit` option can be used to to move by character, word, line, or block.
  1454. *
  1455. * The `reverse` option can be used to change iteration direction.
  1456. *
  1457. * Note: By default void nodes are treated as a single point and iteration
  1458. * will not happen inside their content unless you pass in true for the
  1459. * `voids` option, then iteration will occur.
  1460. */
  1461. *positions(editor) {
  1462. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1463. var {
  1464. at = editor.selection,
  1465. unit = 'offset',
  1466. reverse = false,
  1467. voids = false
  1468. } = options;
  1469. if (!at) {
  1470. return;
  1471. }
  1472. /**
  1473. * Algorithm notes:
  1474. *
  1475. * Each step `distance` is dynamic depending on the underlying text
  1476. * and the `unit` specified. Each step, e.g., a line or word, may
  1477. * span multiple text nodes, so we iterate through the text both on
  1478. * two levels in step-sync:
  1479. *
  1480. * `leafText` stores the text on a text leaf level, and is advanced
  1481. * through using the counters `leafTextOffset` and `leafTextRemaining`.
  1482. *
  1483. * `blockText` stores the text on a block level, and is shortened
  1484. * by `distance` every time it is advanced.
  1485. *
  1486. * We only maintain a window of one blockText and one leafText because
  1487. * a block node always appears before all of its leaf nodes.
  1488. */
  1489. var range = Editor.range(editor, at);
  1490. var [start, end] = Range.edges(range);
  1491. var first = reverse ? end : start;
  1492. var isNewBlock = false;
  1493. var blockText = '';
  1494. var distance = 0; // Distance for leafText to catch up to blockText.
  1495. var leafTextRemaining = 0;
  1496. var leafTextOffset = 0; // Iterate through all nodes in range, grabbing entire textual content
  1497. // of block nodes in blockText, and text nodes in leafText.
  1498. // Exploits the fact that nodes are sequenced in such a way that we first
  1499. // encounter the block node, then all of its text nodes, so when iterating
  1500. // through the blockText and leafText we just need to remember a window of
  1501. // one block node and leaf node, respectively.
  1502. for (var [node, path] of Editor.nodes(editor, {
  1503. at,
  1504. reverse,
  1505. voids
  1506. })) {
  1507. /*
  1508. * ELEMENT NODE - Yield position(s) for voids, collect blockText for blocks
  1509. */
  1510. if (Element.isElement(node)) {
  1511. // Void nodes are a special case, so by default we will always
  1512. // yield their first point. If the `voids` option is set to true,
  1513. // then we will iterate over their content.
  1514. if (!voids && editor.isVoid(node)) {
  1515. yield Editor.start(editor, path);
  1516. continue;
  1517. } // Inline element nodes are ignored as they don't themselves
  1518. // contribute to `blockText` or `leafText` - their parent and
  1519. // children do.
  1520. if (editor.isInline(node)) continue; // Block element node - set `blockText` to its text content.
  1521. if (Editor.hasInlines(editor, node)) {
  1522. // We always exhaust block nodes before encountering a new one:
  1523. // console.assert(blockText === '',
  1524. // `blockText='${blockText}' - `+
  1525. // `not exhausted before new block node`, path)
  1526. // Ensure range considered is capped to `range`, in the
  1527. // start/end edge cases where block extends beyond range.
  1528. // Equivalent to this, but presumably more performant:
  1529. // blockRange = Editor.range(editor, ...Editor.edges(editor, path))
  1530. // blockRange = Range.intersection(range, blockRange) // intersect
  1531. // blockText = Editor.string(editor, blockRange, { voids })
  1532. var e = Path.isAncestor(path, end.path) ? end : Editor.end(editor, path);
  1533. var s = Path.isAncestor(path, start.path) ? start : Editor.start(editor, path);
  1534. blockText = Editor.string(editor, {
  1535. anchor: s,
  1536. focus: e
  1537. }, {
  1538. voids
  1539. });
  1540. isNewBlock = true;
  1541. }
  1542. }
  1543. /*
  1544. * TEXT LEAF NODE - Iterate through text content, yielding
  1545. * positions every `distance` offset according to `unit`.
  1546. */
  1547. if (Text.isText(node)) {
  1548. var isFirst = Path.equals(path, first.path); // Proof that we always exhaust text nodes before encountering a new one:
  1549. // console.assert(leafTextRemaining <= 0,
  1550. // `leafTextRemaining=${leafTextRemaining} - `+
  1551. // `not exhausted before new leaf text node`, path)
  1552. // Reset `leafText` counters for new text node.
  1553. if (isFirst) {
  1554. leafTextRemaining = reverse ? first.offset : node.text.length - first.offset;
  1555. leafTextOffset = first.offset; // Works for reverse too.
  1556. } else {
  1557. leafTextRemaining = node.text.length;
  1558. leafTextOffset = reverse ? leafTextRemaining : 0;
  1559. } // Yield position at the start of node (potentially).
  1560. if (isFirst || isNewBlock || unit === 'offset') {
  1561. yield {
  1562. path,
  1563. offset: leafTextOffset
  1564. };
  1565. isNewBlock = false;
  1566. } // Yield positions every (dynamically calculated) `distance` offset.
  1567. while (true) {
  1568. // If `leafText` has caught up with `blockText` (distance=0),
  1569. // and if blockText is exhausted, break to get another block node,
  1570. // otherwise advance blockText forward by the new `distance`.
  1571. if (distance === 0) {
  1572. if (blockText === '') break;
  1573. distance = calcDistance(blockText, unit, reverse); // Split the string at the previously found distance and use the
  1574. // remaining string for the next iteration.
  1575. blockText = splitByCharacterDistance(blockText, distance, reverse)[1];
  1576. } // Advance `leafText` by the current `distance`.
  1577. leafTextOffset = reverse ? leafTextOffset - distance : leafTextOffset + distance;
  1578. leafTextRemaining = leafTextRemaining - distance; // If `leafText` is exhausted, break to get a new leaf node
  1579. // and set distance to the overflow amount, so we'll (maybe)
  1580. // catch up to blockText in the next leaf text node.
  1581. if (leafTextRemaining < 0) {
  1582. distance = -leafTextRemaining;
  1583. break;
  1584. } // Successfully walked `distance` offsets through `leafText`
  1585. // to catch up with `blockText`, so we can reset `distance`
  1586. // and yield this position in this node.
  1587. distance = 0;
  1588. yield {
  1589. path,
  1590. offset: leafTextOffset
  1591. };
  1592. }
  1593. }
  1594. } // Proof that upon completion, we've exahusted both leaf and block text:
  1595. // console.assert(leafTextRemaining <= 0, "leafText wasn't exhausted")
  1596. // console.assert(blockText === '', "blockText wasn't exhausted")
  1597. // Helper:
  1598. // Return the distance in offsets for a step of size `unit` on given string.
  1599. function calcDistance(text, unit, reverse) {
  1600. if (unit === 'character') {
  1601. return getCharacterDistance(text, reverse);
  1602. } else if (unit === 'word') {
  1603. return getWordDistance(text, reverse);
  1604. } else if (unit === 'line' || unit === 'block') {
  1605. return text.length;
  1606. }
  1607. return 1;
  1608. }
  1609. },
  1610. /**
  1611. * Get the matching node in the branch of the document before a location.
  1612. */
  1613. previous(editor) {
  1614. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1615. var {
  1616. mode = 'lowest',
  1617. voids = false
  1618. } = options;
  1619. var {
  1620. match,
  1621. at = editor.selection
  1622. } = options;
  1623. if (!at) {
  1624. return;
  1625. }
  1626. var pointBeforeLocation = Editor.before(editor, at, {
  1627. voids
  1628. });
  1629. if (!pointBeforeLocation) {
  1630. return;
  1631. }
  1632. var [, to] = Editor.first(editor, []); // The search location is from the start of the document to the path of
  1633. // the point before the location passed in
  1634. var span = [pointBeforeLocation.path, to];
  1635. if (Path.isPath(at) && at.length === 0) {
  1636. throw new Error("Cannot get the previous node from the root node!");
  1637. }
  1638. if (match == null) {
  1639. if (Path.isPath(at)) {
  1640. var [parent] = Editor.parent(editor, at);
  1641. match = n => parent.children.includes(n);
  1642. } else {
  1643. match = () => true;
  1644. }
  1645. }
  1646. var [previous] = Editor.nodes(editor, {
  1647. reverse: true,
  1648. at: span,
  1649. match,
  1650. mode,
  1651. voids
  1652. });
  1653. return previous;
  1654. },
  1655. /**
  1656. * Get a range of a location.
  1657. */
  1658. range(editor, at, to) {
  1659. if (Range.isRange(at) && !to) {
  1660. return at;
  1661. }
  1662. var start = Editor.start(editor, at);
  1663. var end = Editor.end(editor, to || at);
  1664. return {
  1665. anchor: start,
  1666. focus: end
  1667. };
  1668. },
  1669. /**
  1670. * Create a mutable ref for a `Range` object, which will stay in sync as new
  1671. * operations are applied to the editor.
  1672. */
  1673. rangeRef(editor, range) {
  1674. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1675. var {
  1676. affinity = 'forward'
  1677. } = options;
  1678. var ref = {
  1679. current: range,
  1680. affinity,
  1681. unref() {
  1682. var {
  1683. current
  1684. } = ref;
  1685. var rangeRefs = Editor.rangeRefs(editor);
  1686. rangeRefs.delete(ref);
  1687. ref.current = null;
  1688. return current;
  1689. }
  1690. };
  1691. var refs = Editor.rangeRefs(editor);
  1692. refs.add(ref);
  1693. return ref;
  1694. },
  1695. /**
  1696. * Get the set of currently tracked range refs of the editor.
  1697. */
  1698. rangeRefs(editor) {
  1699. var refs = RANGE_REFS.get(editor);
  1700. if (!refs) {
  1701. refs = new Set();
  1702. RANGE_REFS.set(editor, refs);
  1703. }
  1704. return refs;
  1705. },
  1706. /**
  1707. * Remove a custom property from all of the leaf text nodes in the current
  1708. * selection.
  1709. *
  1710. * If the selection is currently collapsed, the removal will be stored on
  1711. * `editor.marks` and applied to the text inserted next.
  1712. */
  1713. removeMark(editor, key) {
  1714. editor.removeMark(key);
  1715. },
  1716. /**
  1717. * Manually set if the editor should currently be normalizing.
  1718. *
  1719. * Note: Using this incorrectly can leave the editor in an invalid state.
  1720. *
  1721. */
  1722. setNormalizing(editor, isNormalizing) {
  1723. NORMALIZING.set(editor, isNormalizing);
  1724. },
  1725. /**
  1726. * Get the start point of a location.
  1727. */
  1728. start(editor, at) {
  1729. return Editor.point(editor, at, {
  1730. edge: 'start'
  1731. });
  1732. },
  1733. /**
  1734. * Get the text string content of a location.
  1735. *
  1736. * Note: by default the text of void nodes is considered to be an empty
  1737. * string, regardless of content, unless you pass in true for the voids option
  1738. */
  1739. string(editor, at) {
  1740. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1741. var {
  1742. voids = false
  1743. } = options;
  1744. var range = Editor.range(editor, at);
  1745. var [start, end] = Range.edges(range);
  1746. var text = '';
  1747. for (var [node, path] of Editor.nodes(editor, {
  1748. at: range,
  1749. match: Text.isText,
  1750. voids
  1751. })) {
  1752. var t = node.text;
  1753. if (Path.equals(path, end.path)) {
  1754. t = t.slice(0, end.offset);
  1755. }
  1756. if (Path.equals(path, start.path)) {
  1757. t = t.slice(start.offset);
  1758. }
  1759. text += t;
  1760. }
  1761. return text;
  1762. },
  1763. /**
  1764. * Convert a range into a non-hanging one.
  1765. */
  1766. unhangRange(editor, range) {
  1767. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1768. var {
  1769. voids = false
  1770. } = options;
  1771. var [start, end] = Range.edges(range); // PERF: exit early if we can guarantee that the range isn't hanging.
  1772. if (start.offset !== 0 || end.offset !== 0 || Range.isCollapsed(range)) {
  1773. return range;
  1774. }
  1775. var endBlock = Editor.above(editor, {
  1776. at: end,
  1777. match: n => Editor.isBlock(editor, n)
  1778. });
  1779. var blockPath = endBlock ? endBlock[1] : [];
  1780. var first = Editor.start(editor, start);
  1781. var before = {
  1782. anchor: first,
  1783. focus: end
  1784. };
  1785. var skip = true;
  1786. for (var [node, path] of Editor.nodes(editor, {
  1787. at: before,
  1788. match: Text.isText,
  1789. reverse: true,
  1790. voids
  1791. })) {
  1792. if (skip) {
  1793. skip = false;
  1794. continue;
  1795. }
  1796. if (node.text !== '' || Path.isBefore(path, blockPath)) {
  1797. end = {
  1798. path,
  1799. offset: node.text.length
  1800. };
  1801. break;
  1802. }
  1803. }
  1804. return {
  1805. anchor: start,
  1806. focus: end
  1807. };
  1808. },
  1809. /**
  1810. * Match a void node in the current branch of the editor.
  1811. */
  1812. void(editor) {
  1813. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1814. return Editor.above(editor, _objectSpread$8(_objectSpread$8({}, options), {}, {
  1815. match: n => Editor.isVoid(editor, n)
  1816. }));
  1817. },
  1818. /**
  1819. * Call a function, deferring normalization until after it completes.
  1820. */
  1821. withoutNormalizing(editor, fn) {
  1822. var value = Editor.isNormalizing(editor);
  1823. Editor.setNormalizing(editor, false);
  1824. try {
  1825. fn();
  1826. } finally {
  1827. Editor.setNormalizing(editor, value);
  1828. }
  1829. Editor.normalize(editor);
  1830. }
  1831. };
  1832. var Location = {
  1833. /**
  1834. * Check if a value implements the `Location` interface.
  1835. */
  1836. isLocation(value) {
  1837. return Path.isPath(value) || Point.isPoint(value) || Range.isRange(value);
  1838. }
  1839. };
  1840. var Span = {
  1841. /**
  1842. * Check if a value implements the `Span` interface.
  1843. */
  1844. isSpan(value) {
  1845. return Array.isArray(value) && value.length === 2 && value.every(Path.isPath);
  1846. }
  1847. };
  1848. var _excluded$3 = ["children"],
  1849. _excluded2$2 = ["text"];
  1850. var IS_NODE_LIST_CACHE = new WeakMap();
  1851. var Node = {
  1852. /**
  1853. * Get the node at a specific path, asserting that it's an ancestor node.
  1854. */
  1855. ancestor(root, path) {
  1856. var node = Node.get(root, path);
  1857. if (Text.isText(node)) {
  1858. throw new Error("Cannot get the ancestor node at path [".concat(path, "] because it refers to a text node instead: ").concat(node));
  1859. }
  1860. return node;
  1861. },
  1862. /**
  1863. * Return a generator of all the ancestor nodes above a specific path.
  1864. *
  1865. * By default the order is bottom-up, from lowest to highest ancestor in
  1866. * the tree, but you can pass the `reverse: true` option to go top-down.
  1867. */
  1868. *ancestors(root, path) {
  1869. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1870. for (var p of Path.ancestors(path, options)) {
  1871. var n = Node.ancestor(root, p);
  1872. var entry = [n, p];
  1873. yield entry;
  1874. }
  1875. },
  1876. /**
  1877. * Get the child of a node at a specific index.
  1878. */
  1879. child(root, index) {
  1880. if (Text.isText(root)) {
  1881. throw new Error("Cannot get the child of a text node: ".concat(JSON.stringify(root)));
  1882. }
  1883. var c = root.children[index];
  1884. if (c == null) {
  1885. throw new Error("Cannot get child at index `".concat(index, "` in node: ").concat(JSON.stringify(root)));
  1886. }
  1887. return c;
  1888. },
  1889. /**
  1890. * Iterate over the children of a node at a specific path.
  1891. */
  1892. *children(root, path) {
  1893. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1894. var {
  1895. reverse = false
  1896. } = options;
  1897. var ancestor = Node.ancestor(root, path);
  1898. var {
  1899. children
  1900. } = ancestor;
  1901. var index = reverse ? children.length - 1 : 0;
  1902. while (reverse ? index >= 0 : index < children.length) {
  1903. var child = Node.child(ancestor, index);
  1904. var childPath = path.concat(index);
  1905. yield [child, childPath];
  1906. index = reverse ? index - 1 : index + 1;
  1907. }
  1908. },
  1909. /**
  1910. * Get an entry for the common ancesetor node of two paths.
  1911. */
  1912. common(root, path, another) {
  1913. var p = Path.common(path, another);
  1914. var n = Node.get(root, p);
  1915. return [n, p];
  1916. },
  1917. /**
  1918. * Get the node at a specific path, asserting that it's a descendant node.
  1919. */
  1920. descendant(root, path) {
  1921. var node = Node.get(root, path);
  1922. if (Editor.isEditor(node)) {
  1923. throw new Error("Cannot get the descendant node at path [".concat(path, "] because it refers to the root editor node instead: ").concat(node));
  1924. }
  1925. return node;
  1926. },
  1927. /**
  1928. * Return a generator of all the descendant node entries inside a root node.
  1929. */
  1930. *descendants(root) {
  1931. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1932. for (var [node, path] of Node.nodes(root, options)) {
  1933. if (path.length !== 0) {
  1934. // NOTE: we have to coerce here because checking the path's length does
  1935. // guarantee that `node` is not a `Editor`, but TypeScript doesn't know.
  1936. yield [node, path];
  1937. }
  1938. }
  1939. },
  1940. /**
  1941. * Return a generator of all the element nodes inside a root node. Each iteration
  1942. * will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the
  1943. * root node is an element it will be included in the iteration as well.
  1944. */
  1945. *elements(root) {
  1946. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1947. for (var [node, path] of Node.nodes(root, options)) {
  1948. if (Element.isElement(node)) {
  1949. yield [node, path];
  1950. }
  1951. }
  1952. },
  1953. /**
  1954. * Extract props from a Node.
  1955. */
  1956. extractProps(node) {
  1957. if (Element.isAncestor(node)) {
  1958. var properties = _objectWithoutProperties(node, _excluded$3);
  1959. return properties;
  1960. } else {
  1961. var properties = _objectWithoutProperties(node, _excluded2$2);
  1962. return properties;
  1963. }
  1964. },
  1965. /**
  1966. * Get the first node entry in a root node from a path.
  1967. */
  1968. first(root, path) {
  1969. var p = path.slice();
  1970. var n = Node.get(root, p);
  1971. while (n) {
  1972. if (Text.isText(n) || n.children.length === 0) {
  1973. break;
  1974. } else {
  1975. n = n.children[0];
  1976. p.push(0);
  1977. }
  1978. }
  1979. return [n, p];
  1980. },
  1981. /**
  1982. * Get the sliced fragment represented by a range inside a root node.
  1983. */
  1984. fragment(root, range) {
  1985. if (Text.isText(root)) {
  1986. throw new Error("Cannot get a fragment starting from a root text node: ".concat(JSON.stringify(root)));
  1987. }
  1988. var newRoot = produce({
  1989. children: root.children
  1990. }, r => {
  1991. var [start, end] = Range.edges(range);
  1992. var nodeEntries = Node.nodes(r, {
  1993. reverse: true,
  1994. pass: _ref => {
  1995. var [, path] = _ref;
  1996. return !Range.includes(range, path);
  1997. }
  1998. });
  1999. for (var [, path] of nodeEntries) {
  2000. if (!Range.includes(range, path)) {
  2001. var parent = Node.parent(r, path);
  2002. var index = path[path.length - 1];
  2003. parent.children.splice(index, 1);
  2004. }
  2005. if (Path.equals(path, end.path)) {
  2006. var leaf = Node.leaf(r, path);
  2007. leaf.text = leaf.text.slice(0, end.offset);
  2008. }
  2009. if (Path.equals(path, start.path)) {
  2010. var _leaf = Node.leaf(r, path);
  2011. _leaf.text = _leaf.text.slice(start.offset);
  2012. }
  2013. }
  2014. if (Editor.isEditor(r)) {
  2015. r.selection = null;
  2016. }
  2017. });
  2018. return newRoot.children;
  2019. },
  2020. /**
  2021. * Get the descendant node referred to by a specific path. If the path is an
  2022. * empty array, it refers to the root node itself.
  2023. */
  2024. get(root, path) {
  2025. var node = root;
  2026. for (var i = 0; i < path.length; i++) {
  2027. var p = path[i];
  2028. if (Text.isText(node) || !node.children[p]) {
  2029. throw new Error("Cannot find a descendant at path [".concat(path, "] in node: ").concat(JSON.stringify(root)));
  2030. }
  2031. node = node.children[p];
  2032. }
  2033. return node;
  2034. },
  2035. /**
  2036. * Check if a descendant node exists at a specific path.
  2037. */
  2038. has(root, path) {
  2039. var node = root;
  2040. for (var i = 0; i < path.length; i++) {
  2041. var p = path[i];
  2042. if (Text.isText(node) || !node.children[p]) {
  2043. return false;
  2044. }
  2045. node = node.children[p];
  2046. }
  2047. return true;
  2048. },
  2049. /**
  2050. * Check if a value implements the `Node` interface.
  2051. */
  2052. isNode(value) {
  2053. return Text.isText(value) || Element.isElement(value) || Editor.isEditor(value);
  2054. },
  2055. /**
  2056. * Check if a value is a list of `Node` objects.
  2057. */
  2058. isNodeList(value) {
  2059. if (!Array.isArray(value)) {
  2060. return false;
  2061. }
  2062. var cachedResult = IS_NODE_LIST_CACHE.get(value);
  2063. if (cachedResult !== undefined) {
  2064. return cachedResult;
  2065. }
  2066. var isNodeList = value.every(val => Node.isNode(val));
  2067. IS_NODE_LIST_CACHE.set(value, isNodeList);
  2068. return isNodeList;
  2069. },
  2070. /**
  2071. * Get the last node entry in a root node from a path.
  2072. */
  2073. last(root, path) {
  2074. var p = path.slice();
  2075. var n = Node.get(root, p);
  2076. while (n) {
  2077. if (Text.isText(n) || n.children.length === 0) {
  2078. break;
  2079. } else {
  2080. var i = n.children.length - 1;
  2081. n = n.children[i];
  2082. p.push(i);
  2083. }
  2084. }
  2085. return [n, p];
  2086. },
  2087. /**
  2088. * Get the node at a specific path, ensuring it's a leaf text node.
  2089. */
  2090. leaf(root, path) {
  2091. var node = Node.get(root, path);
  2092. if (!Text.isText(node)) {
  2093. throw new Error("Cannot get the leaf node at path [".concat(path, "] because it refers to a non-leaf node: ").concat(node));
  2094. }
  2095. return node;
  2096. },
  2097. /**
  2098. * Return a generator of the in a branch of the tree, from a specific path.
  2099. *
  2100. * By default the order is top-down, from lowest to highest node in the tree,
  2101. * but you can pass the `reverse: true` option to go bottom-up.
  2102. */
  2103. *levels(root, path) {
  2104. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2105. for (var p of Path.levels(path, options)) {
  2106. var n = Node.get(root, p);
  2107. yield [n, p];
  2108. }
  2109. },
  2110. /**
  2111. * Check if a node matches a set of props.
  2112. */
  2113. matches(node, props) {
  2114. return Element.isElement(node) && Element.isElementProps(props) && Element.matches(node, props) || Text.isText(node) && Text.isTextProps(props) && Text.matches(node, props);
  2115. },
  2116. /**
  2117. * Return a generator of all the node entries of a root node. Each entry is
  2118. * returned as a `[Node, Path]` tuple, with the path referring to the node's
  2119. * position inside the root node.
  2120. */
  2121. *nodes(root) {
  2122. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2123. var {
  2124. pass,
  2125. reverse = false
  2126. } = options;
  2127. var {
  2128. from = [],
  2129. to
  2130. } = options;
  2131. var visited = new Set();
  2132. var p = [];
  2133. var n = root;
  2134. while (true) {
  2135. if (to && (reverse ? Path.isBefore(p, to) : Path.isAfter(p, to))) {
  2136. break;
  2137. }
  2138. if (!visited.has(n)) {
  2139. yield [n, p];
  2140. } // If we're allowed to go downward and we haven't descended yet, do.
  2141. if (!visited.has(n) && !Text.isText(n) && n.children.length !== 0 && (pass == null || pass([n, p]) === false)) {
  2142. visited.add(n);
  2143. var nextIndex = reverse ? n.children.length - 1 : 0;
  2144. if (Path.isAncestor(p, from)) {
  2145. nextIndex = from[p.length];
  2146. }
  2147. p = p.concat(nextIndex);
  2148. n = Node.get(root, p);
  2149. continue;
  2150. } // If we're at the root and we can't go down, we're done.
  2151. if (p.length === 0) {
  2152. break;
  2153. } // If we're going forward...
  2154. if (!reverse) {
  2155. var newPath = Path.next(p);
  2156. if (Node.has(root, newPath)) {
  2157. p = newPath;
  2158. n = Node.get(root, p);
  2159. continue;
  2160. }
  2161. } // If we're going backward...
  2162. if (reverse && p[p.length - 1] !== 0) {
  2163. var _newPath = Path.previous(p);
  2164. p = _newPath;
  2165. n = Node.get(root, p);
  2166. continue;
  2167. } // Otherwise we're going upward...
  2168. p = Path.parent(p);
  2169. n = Node.get(root, p);
  2170. visited.add(n);
  2171. }
  2172. },
  2173. /**
  2174. * Get the parent of a node at a specific path.
  2175. */
  2176. parent(root, path) {
  2177. var parentPath = Path.parent(path);
  2178. var p = Node.get(root, parentPath);
  2179. if (Text.isText(p)) {
  2180. throw new Error("Cannot get the parent of path [".concat(path, "] because it does not exist in the root."));
  2181. }
  2182. return p;
  2183. },
  2184. /**
  2185. * Get the concatenated text string of a node's content.
  2186. *
  2187. * Note that this will not include spaces or line breaks between block nodes.
  2188. * It is not a user-facing string, but a string for performing offset-related
  2189. * computations for a node.
  2190. */
  2191. string(node) {
  2192. if (Text.isText(node)) {
  2193. return node.text;
  2194. } else {
  2195. return node.children.map(Node.string).join('');
  2196. }
  2197. },
  2198. /**
  2199. * Return a generator of all leaf text nodes in a root node.
  2200. */
  2201. *texts(root) {
  2202. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2203. for (var [node, path] of Node.nodes(root, options)) {
  2204. if (Text.isText(node)) {
  2205. yield [node, path];
  2206. }
  2207. }
  2208. }
  2209. };
  2210. 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; }
  2211. 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; }
  2212. var Operation = {
  2213. /**
  2214. * Check of a value is a `NodeOperation` object.
  2215. */
  2216. isNodeOperation(value) {
  2217. return Operation.isOperation(value) && value.type.endsWith('_node');
  2218. },
  2219. /**
  2220. * Check of a value is an `Operation` object.
  2221. */
  2222. isOperation(value) {
  2223. if (!isPlainObject(value)) {
  2224. return false;
  2225. }
  2226. switch (value.type) {
  2227. case 'insert_node':
  2228. return Path.isPath(value.path) && Node.isNode(value.node);
  2229. case 'insert_text':
  2230. return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
  2231. case 'merge_node':
  2232. return typeof value.position === 'number' && Path.isPath(value.path) && isPlainObject(value.properties);
  2233. case 'move_node':
  2234. return Path.isPath(value.path) && Path.isPath(value.newPath);
  2235. case 'remove_node':
  2236. return Path.isPath(value.path) && Node.isNode(value.node);
  2237. case 'remove_text':
  2238. return typeof value.offset === 'number' && typeof value.text === 'string' && Path.isPath(value.path);
  2239. case 'set_node':
  2240. return Path.isPath(value.path) && isPlainObject(value.properties) && isPlainObject(value.newProperties);
  2241. case 'set_selection':
  2242. return value.properties === null && Range.isRange(value.newProperties) || value.newProperties === null && Range.isRange(value.properties) || isPlainObject(value.properties) && isPlainObject(value.newProperties);
  2243. case 'split_node':
  2244. return Path.isPath(value.path) && typeof value.position === 'number' && isPlainObject(value.properties);
  2245. default:
  2246. return false;
  2247. }
  2248. },
  2249. /**
  2250. * Check if a value is a list of `Operation` objects.
  2251. */
  2252. isOperationList(value) {
  2253. return Array.isArray(value) && value.every(val => Operation.isOperation(val));
  2254. },
  2255. /**
  2256. * Check of a value is a `SelectionOperation` object.
  2257. */
  2258. isSelectionOperation(value) {
  2259. return Operation.isOperation(value) && value.type.endsWith('_selection');
  2260. },
  2261. /**
  2262. * Check of a value is a `TextOperation` object.
  2263. */
  2264. isTextOperation(value) {
  2265. return Operation.isOperation(value) && value.type.endsWith('_text');
  2266. },
  2267. /**
  2268. * Invert an operation, returning a new operation that will exactly undo the
  2269. * original when applied.
  2270. */
  2271. inverse(op) {
  2272. switch (op.type) {
  2273. case 'insert_node':
  2274. {
  2275. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2276. type: 'remove_node'
  2277. });
  2278. }
  2279. case 'insert_text':
  2280. {
  2281. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2282. type: 'remove_text'
  2283. });
  2284. }
  2285. case 'merge_node':
  2286. {
  2287. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2288. type: 'split_node',
  2289. path: Path.previous(op.path)
  2290. });
  2291. }
  2292. case 'move_node':
  2293. {
  2294. var {
  2295. newPath,
  2296. path
  2297. } = op; // PERF: in this case the move operation is a no-op anyways.
  2298. if (Path.equals(newPath, path)) {
  2299. return op;
  2300. } // If the move happens completely within a single parent the path and
  2301. // newPath are stable with respect to each other.
  2302. if (Path.isSibling(path, newPath)) {
  2303. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2304. path: newPath,
  2305. newPath: path
  2306. });
  2307. } // If the move does not happen within a single parent it is possible
  2308. // for the move to impact the true path to the location where the node
  2309. // was removed from and where it was inserted. We have to adjust for this
  2310. // and find the original path. We can accomplish this (only in non-sibling)
  2311. // moves by looking at the impact of the move operation on the node
  2312. // after the original move path.
  2313. var inversePath = Path.transform(path, op);
  2314. var inverseNewPath = Path.transform(Path.next(path), op);
  2315. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2316. path: inversePath,
  2317. newPath: inverseNewPath
  2318. });
  2319. }
  2320. case 'remove_node':
  2321. {
  2322. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2323. type: 'insert_node'
  2324. });
  2325. }
  2326. case 'remove_text':
  2327. {
  2328. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2329. type: 'insert_text'
  2330. });
  2331. }
  2332. case 'set_node':
  2333. {
  2334. var {
  2335. properties,
  2336. newProperties
  2337. } = op;
  2338. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2339. properties: newProperties,
  2340. newProperties: properties
  2341. });
  2342. }
  2343. case 'set_selection':
  2344. {
  2345. var {
  2346. properties: _properties,
  2347. newProperties: _newProperties
  2348. } = op;
  2349. if (_properties == null) {
  2350. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2351. properties: _newProperties,
  2352. newProperties: null
  2353. });
  2354. } else if (_newProperties == null) {
  2355. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2356. properties: null,
  2357. newProperties: _properties
  2358. });
  2359. } else {
  2360. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2361. properties: _newProperties,
  2362. newProperties: _properties
  2363. });
  2364. }
  2365. }
  2366. case 'split_node':
  2367. {
  2368. return _objectSpread$7(_objectSpread$7({}, op), {}, {
  2369. type: 'merge_node',
  2370. path: Path.next(op.path)
  2371. });
  2372. }
  2373. }
  2374. }
  2375. };
  2376. var Path = {
  2377. /**
  2378. * Get a list of ancestor paths for a given path.
  2379. *
  2380. * The paths are sorted from deepest to shallowest ancestor. However, if the
  2381. * `reverse: true` option is passed, they are reversed.
  2382. */
  2383. ancestors(path) {
  2384. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2385. var {
  2386. reverse = false
  2387. } = options;
  2388. var paths = Path.levels(path, options);
  2389. if (reverse) {
  2390. paths = paths.slice(1);
  2391. } else {
  2392. paths = paths.slice(0, -1);
  2393. }
  2394. return paths;
  2395. },
  2396. /**
  2397. * Get the common ancestor path of two paths.
  2398. */
  2399. common(path, another) {
  2400. var common = [];
  2401. for (var i = 0; i < path.length && i < another.length; i++) {
  2402. var av = path[i];
  2403. var bv = another[i];
  2404. if (av !== bv) {
  2405. break;
  2406. }
  2407. common.push(av);
  2408. }
  2409. return common;
  2410. },
  2411. /**
  2412. * Compare a path to another, returning an integer indicating whether the path
  2413. * was before, at, or after the other.
  2414. *
  2415. * Note: Two paths of unequal length can still receive a `0` result if one is
  2416. * directly above or below the other. If you want exact matching, use
  2417. * [[Path.equals]] instead.
  2418. */
  2419. compare(path, another) {
  2420. var min = Math.min(path.length, another.length);
  2421. for (var i = 0; i < min; i++) {
  2422. if (path[i] < another[i]) return -1;
  2423. if (path[i] > another[i]) return 1;
  2424. }
  2425. return 0;
  2426. },
  2427. /**
  2428. * Check if a path ends after one of the indexes in another.
  2429. */
  2430. endsAfter(path, another) {
  2431. var i = path.length - 1;
  2432. var as = path.slice(0, i);
  2433. var bs = another.slice(0, i);
  2434. var av = path[i];
  2435. var bv = another[i];
  2436. return Path.equals(as, bs) && av > bv;
  2437. },
  2438. /**
  2439. * Check if a path ends at one of the indexes in another.
  2440. */
  2441. endsAt(path, another) {
  2442. var i = path.length;
  2443. var as = path.slice(0, i);
  2444. var bs = another.slice(0, i);
  2445. return Path.equals(as, bs);
  2446. },
  2447. /**
  2448. * Check if a path ends before one of the indexes in another.
  2449. */
  2450. endsBefore(path, another) {
  2451. var i = path.length - 1;
  2452. var as = path.slice(0, i);
  2453. var bs = another.slice(0, i);
  2454. var av = path[i];
  2455. var bv = another[i];
  2456. return Path.equals(as, bs) && av < bv;
  2457. },
  2458. /**
  2459. * Check if a path is exactly equal to another.
  2460. */
  2461. equals(path, another) {
  2462. return path.length === another.length && path.every((n, i) => n === another[i]);
  2463. },
  2464. /**
  2465. * Check if the path of previous sibling node exists
  2466. */
  2467. hasPrevious(path) {
  2468. return path[path.length - 1] > 0;
  2469. },
  2470. /**
  2471. * Check if a path is after another.
  2472. */
  2473. isAfter(path, another) {
  2474. return Path.compare(path, another) === 1;
  2475. },
  2476. /**
  2477. * Check if a path is an ancestor of another.
  2478. */
  2479. isAncestor(path, another) {
  2480. return path.length < another.length && Path.compare(path, another) === 0;
  2481. },
  2482. /**
  2483. * Check if a path is before another.
  2484. */
  2485. isBefore(path, another) {
  2486. return Path.compare(path, another) === -1;
  2487. },
  2488. /**
  2489. * Check if a path is a child of another.
  2490. */
  2491. isChild(path, another) {
  2492. return path.length === another.length + 1 && Path.compare(path, another) === 0;
  2493. },
  2494. /**
  2495. * Check if a path is equal to or an ancestor of another.
  2496. */
  2497. isCommon(path, another) {
  2498. return path.length <= another.length && Path.compare(path, another) === 0;
  2499. },
  2500. /**
  2501. * Check if a path is a descendant of another.
  2502. */
  2503. isDescendant(path, another) {
  2504. return path.length > another.length && Path.compare(path, another) === 0;
  2505. },
  2506. /**
  2507. * Check if a path is the parent of another.
  2508. */
  2509. isParent(path, another) {
  2510. return path.length + 1 === another.length && Path.compare(path, another) === 0;
  2511. },
  2512. /**
  2513. * Check is a value implements the `Path` interface.
  2514. */
  2515. isPath(value) {
  2516. return Array.isArray(value) && (value.length === 0 || typeof value[0] === 'number');
  2517. },
  2518. /**
  2519. * Check if a path is a sibling of another.
  2520. */
  2521. isSibling(path, another) {
  2522. if (path.length !== another.length) {
  2523. return false;
  2524. }
  2525. var as = path.slice(0, -1);
  2526. var bs = another.slice(0, -1);
  2527. var al = path[path.length - 1];
  2528. var bl = another[another.length - 1];
  2529. return al !== bl && Path.equals(as, bs);
  2530. },
  2531. /**
  2532. * Get a list of paths at every level down to a path. Note: this is the same
  2533. * as `Path.ancestors`, but including the path itself.
  2534. *
  2535. * The paths are sorted from shallowest to deepest. However, if the `reverse:
  2536. * true` option is passed, they are reversed.
  2537. */
  2538. levels(path) {
  2539. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2540. var {
  2541. reverse = false
  2542. } = options;
  2543. var list = [];
  2544. for (var i = 0; i <= path.length; i++) {
  2545. list.push(path.slice(0, i));
  2546. }
  2547. if (reverse) {
  2548. list.reverse();
  2549. }
  2550. return list;
  2551. },
  2552. /**
  2553. * Given a path, get the path to the next sibling node.
  2554. */
  2555. next(path) {
  2556. if (path.length === 0) {
  2557. throw new Error("Cannot get the next path of a root path [".concat(path, "], because it has no next index."));
  2558. }
  2559. var last = path[path.length - 1];
  2560. return path.slice(0, -1).concat(last + 1);
  2561. },
  2562. /**
  2563. * Returns whether this operation can affect paths or not. Used as an
  2564. * optimization when updating dirty paths during normalization
  2565. *
  2566. * NOTE: This *must* be kept in sync with the implementation of 'transform'
  2567. * below
  2568. */
  2569. operationCanTransformPath(operation) {
  2570. switch (operation.type) {
  2571. case 'insert_node':
  2572. case 'remove_node':
  2573. case 'merge_node':
  2574. case 'split_node':
  2575. case 'move_node':
  2576. return true;
  2577. default:
  2578. return false;
  2579. }
  2580. },
  2581. /**
  2582. * Given a path, return a new path referring to the parent node above it.
  2583. */
  2584. parent(path) {
  2585. if (path.length === 0) {
  2586. throw new Error("Cannot get the parent path of the root path [".concat(path, "]."));
  2587. }
  2588. return path.slice(0, -1);
  2589. },
  2590. /**
  2591. * Given a path, get the path to the previous sibling node.
  2592. */
  2593. previous(path) {
  2594. if (path.length === 0) {
  2595. throw new Error("Cannot get the previous path of a root path [".concat(path, "], because it has no previous index."));
  2596. }
  2597. var last = path[path.length - 1];
  2598. if (last <= 0) {
  2599. throw new Error("Cannot get the previous path of a first child path [".concat(path, "] because it would result in a negative index."));
  2600. }
  2601. return path.slice(0, -1).concat(last - 1);
  2602. },
  2603. /**
  2604. * Get a path relative to an ancestor.
  2605. */
  2606. relative(path, ancestor) {
  2607. if (!Path.isAncestor(ancestor, path) && !Path.equals(path, ancestor)) {
  2608. 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."));
  2609. }
  2610. return path.slice(ancestor.length);
  2611. },
  2612. /**
  2613. * Transform a path by an operation.
  2614. */
  2615. transform(path, operation) {
  2616. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2617. return produce(path, p => {
  2618. var {
  2619. affinity = 'forward'
  2620. } = options; // PERF: Exit early if the operation is guaranteed not to have an effect.
  2621. if (!path || (path === null || path === void 0 ? void 0 : path.length) === 0) {
  2622. return;
  2623. }
  2624. if (p === null) {
  2625. return null;
  2626. }
  2627. switch (operation.type) {
  2628. case 'insert_node':
  2629. {
  2630. var {
  2631. path: op
  2632. } = operation;
  2633. if (Path.equals(op, p) || Path.endsBefore(op, p) || Path.isAncestor(op, p)) {
  2634. p[op.length - 1] += 1;
  2635. }
  2636. break;
  2637. }
  2638. case 'remove_node':
  2639. {
  2640. var {
  2641. path: _op
  2642. } = operation;
  2643. if (Path.equals(_op, p) || Path.isAncestor(_op, p)) {
  2644. return null;
  2645. } else if (Path.endsBefore(_op, p)) {
  2646. p[_op.length - 1] -= 1;
  2647. }
  2648. break;
  2649. }
  2650. case 'merge_node':
  2651. {
  2652. var {
  2653. path: _op2,
  2654. position
  2655. } = operation;
  2656. if (Path.equals(_op2, p) || Path.endsBefore(_op2, p)) {
  2657. p[_op2.length - 1] -= 1;
  2658. } else if (Path.isAncestor(_op2, p)) {
  2659. p[_op2.length - 1] -= 1;
  2660. p[_op2.length] += position;
  2661. }
  2662. break;
  2663. }
  2664. case 'split_node':
  2665. {
  2666. var {
  2667. path: _op3,
  2668. position: _position
  2669. } = operation;
  2670. if (Path.equals(_op3, p)) {
  2671. if (affinity === 'forward') {
  2672. p[p.length - 1] += 1;
  2673. } else if (affinity === 'backward') ; else {
  2674. return null;
  2675. }
  2676. } else if (Path.endsBefore(_op3, p)) {
  2677. p[_op3.length - 1] += 1;
  2678. } else if (Path.isAncestor(_op3, p) && path[_op3.length] >= _position) {
  2679. p[_op3.length - 1] += 1;
  2680. p[_op3.length] -= _position;
  2681. }
  2682. break;
  2683. }
  2684. case 'move_node':
  2685. {
  2686. var {
  2687. path: _op4,
  2688. newPath: onp
  2689. } = operation; // If the old and new path are the same, it's a no-op.
  2690. if (Path.equals(_op4, onp)) {
  2691. return;
  2692. }
  2693. if (Path.isAncestor(_op4, p) || Path.equals(_op4, p)) {
  2694. var copy = onp.slice();
  2695. if (Path.endsBefore(_op4, onp) && _op4.length < onp.length) {
  2696. copy[_op4.length - 1] -= 1;
  2697. }
  2698. return copy.concat(p.slice(_op4.length));
  2699. } else if (Path.isSibling(_op4, onp) && (Path.isAncestor(onp, p) || Path.equals(onp, p))) {
  2700. if (Path.endsBefore(_op4, p)) {
  2701. p[_op4.length - 1] -= 1;
  2702. } else {
  2703. p[_op4.length - 1] += 1;
  2704. }
  2705. } else if (Path.endsBefore(onp, p) || Path.equals(onp, p) || Path.isAncestor(onp, p)) {
  2706. if (Path.endsBefore(_op4, p)) {
  2707. p[_op4.length - 1] -= 1;
  2708. }
  2709. p[onp.length - 1] += 1;
  2710. } else if (Path.endsBefore(_op4, p)) {
  2711. if (Path.equals(onp, p)) {
  2712. p[onp.length - 1] += 1;
  2713. }
  2714. p[_op4.length - 1] -= 1;
  2715. }
  2716. break;
  2717. }
  2718. }
  2719. });
  2720. }
  2721. };
  2722. var PathRef = {
  2723. /**
  2724. * Transform the path ref's current value by an operation.
  2725. */
  2726. transform(ref, op) {
  2727. var {
  2728. current,
  2729. affinity
  2730. } = ref;
  2731. if (current == null) {
  2732. return;
  2733. }
  2734. var path = Path.transform(current, op, {
  2735. affinity
  2736. });
  2737. ref.current = path;
  2738. if (path == null) {
  2739. ref.unref();
  2740. }
  2741. }
  2742. };
  2743. 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; }
  2744. 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; }
  2745. var Point = {
  2746. /**
  2747. * Compare a point to another, returning an integer indicating whether the
  2748. * point was before, at, or after the other.
  2749. */
  2750. compare(point, another) {
  2751. var result = Path.compare(point.path, another.path);
  2752. if (result === 0) {
  2753. if (point.offset < another.offset) return -1;
  2754. if (point.offset > another.offset) return 1;
  2755. return 0;
  2756. }
  2757. return result;
  2758. },
  2759. /**
  2760. * Check if a point is after another.
  2761. */
  2762. isAfter(point, another) {
  2763. return Point.compare(point, another) === 1;
  2764. },
  2765. /**
  2766. * Check if a point is before another.
  2767. */
  2768. isBefore(point, another) {
  2769. return Point.compare(point, another) === -1;
  2770. },
  2771. /**
  2772. * Check if a point is exactly equal to another.
  2773. */
  2774. equals(point, another) {
  2775. // PERF: ensure the offsets are equal first since they are cheaper to check.
  2776. return point.offset === another.offset && Path.equals(point.path, another.path);
  2777. },
  2778. /**
  2779. * Check if a value implements the `Point` interface.
  2780. */
  2781. isPoint(value) {
  2782. return isPlainObject(value) && typeof value.offset === 'number' && Path.isPath(value.path);
  2783. },
  2784. /**
  2785. * Transform a point by an operation.
  2786. */
  2787. transform(point, op) {
  2788. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  2789. return produce(point, p => {
  2790. if (p === null) {
  2791. return null;
  2792. }
  2793. var {
  2794. affinity = 'forward'
  2795. } = options;
  2796. var {
  2797. path,
  2798. offset
  2799. } = p;
  2800. switch (op.type) {
  2801. case 'insert_node':
  2802. case 'move_node':
  2803. {
  2804. p.path = Path.transform(path, op, options);
  2805. break;
  2806. }
  2807. case 'insert_text':
  2808. {
  2809. if (Path.equals(op.path, path) && op.offset <= offset) {
  2810. p.offset += op.text.length;
  2811. }
  2812. break;
  2813. }
  2814. case 'merge_node':
  2815. {
  2816. if (Path.equals(op.path, path)) {
  2817. p.offset += op.position;
  2818. }
  2819. p.path = Path.transform(path, op, options);
  2820. break;
  2821. }
  2822. case 'remove_text':
  2823. {
  2824. if (Path.equals(op.path, path) && op.offset <= offset) {
  2825. p.offset -= Math.min(offset - op.offset, op.text.length);
  2826. }
  2827. break;
  2828. }
  2829. case 'remove_node':
  2830. {
  2831. if (Path.equals(op.path, path) || Path.isAncestor(op.path, path)) {
  2832. return null;
  2833. }
  2834. p.path = Path.transform(path, op, options);
  2835. break;
  2836. }
  2837. case 'split_node':
  2838. {
  2839. if (Path.equals(op.path, path)) {
  2840. if (op.position === offset && affinity == null) {
  2841. return null;
  2842. } else if (op.position < offset || op.position === offset && affinity === 'forward') {
  2843. p.offset -= op.position;
  2844. p.path = Path.transform(path, op, _objectSpread$6(_objectSpread$6({}, options), {}, {
  2845. affinity: 'forward'
  2846. }));
  2847. }
  2848. } else {
  2849. p.path = Path.transform(path, op, options);
  2850. }
  2851. break;
  2852. }
  2853. }
  2854. });
  2855. }
  2856. };
  2857. var PointRef = {
  2858. /**
  2859. * Transform the point ref's current value by an operation.
  2860. */
  2861. transform(ref, op) {
  2862. var {
  2863. current,
  2864. affinity
  2865. } = ref;
  2866. if (current == null) {
  2867. return;
  2868. }
  2869. var point = Point.transform(current, op, {
  2870. affinity
  2871. });
  2872. ref.current = point;
  2873. if (point == null) {
  2874. ref.unref();
  2875. }
  2876. }
  2877. };
  2878. var _excluded$2 = ["anchor", "focus"];
  2879. 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; }
  2880. 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; }
  2881. var Range = {
  2882. /**
  2883. * Get the start and end points of a range, in the order in which they appear
  2884. * in the document.
  2885. */
  2886. edges(range) {
  2887. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2888. var {
  2889. reverse = false
  2890. } = options;
  2891. var {
  2892. anchor,
  2893. focus
  2894. } = range;
  2895. return Range.isBackward(range) === reverse ? [anchor, focus] : [focus, anchor];
  2896. },
  2897. /**
  2898. * Get the end point of a range.
  2899. */
  2900. end(range) {
  2901. var [, end] = Range.edges(range);
  2902. return end;
  2903. },
  2904. /**
  2905. * Check if a range is exactly equal to another.
  2906. */
  2907. equals(range, another) {
  2908. return Point.equals(range.anchor, another.anchor) && Point.equals(range.focus, another.focus);
  2909. },
  2910. /**
  2911. * Check if a range includes a path, a point or part of another range.
  2912. */
  2913. includes(range, target) {
  2914. if (Range.isRange(target)) {
  2915. if (Range.includes(range, target.anchor) || Range.includes(range, target.focus)) {
  2916. return true;
  2917. }
  2918. var [rs, re] = Range.edges(range);
  2919. var [ts, te] = Range.edges(target);
  2920. return Point.isBefore(rs, ts) && Point.isAfter(re, te);
  2921. }
  2922. var [start, end] = Range.edges(range);
  2923. var isAfterStart = false;
  2924. var isBeforeEnd = false;
  2925. if (Point.isPoint(target)) {
  2926. isAfterStart = Point.compare(target, start) >= 0;
  2927. isBeforeEnd = Point.compare(target, end) <= 0;
  2928. } else {
  2929. isAfterStart = Path.compare(target, start.path) >= 0;
  2930. isBeforeEnd = Path.compare(target, end.path) <= 0;
  2931. }
  2932. return isAfterStart && isBeforeEnd;
  2933. },
  2934. /**
  2935. * Get the intersection of a range with another.
  2936. */
  2937. intersection(range, another) {
  2938. var rest = _objectWithoutProperties(range, _excluded$2);
  2939. var [s1, e1] = Range.edges(range);
  2940. var [s2, e2] = Range.edges(another);
  2941. var start = Point.isBefore(s1, s2) ? s2 : s1;
  2942. var end = Point.isBefore(e1, e2) ? e1 : e2;
  2943. if (Point.isBefore(end, start)) {
  2944. return null;
  2945. } else {
  2946. return _objectSpread$5({
  2947. anchor: start,
  2948. focus: end
  2949. }, rest);
  2950. }
  2951. },
  2952. /**
  2953. * Check if a range is backward, meaning that its anchor point appears in the
  2954. * document _after_ its focus point.
  2955. */
  2956. isBackward(range) {
  2957. var {
  2958. anchor,
  2959. focus
  2960. } = range;
  2961. return Point.isAfter(anchor, focus);
  2962. },
  2963. /**
  2964. * Check if a range is collapsed, meaning that both its anchor and focus
  2965. * points refer to the exact same position in the document.
  2966. */
  2967. isCollapsed(range) {
  2968. var {
  2969. anchor,
  2970. focus
  2971. } = range;
  2972. return Point.equals(anchor, focus);
  2973. },
  2974. /**
  2975. * Check if a range is expanded.
  2976. *
  2977. * This is the opposite of [[Range.isCollapsed]] and is provided for legibility.
  2978. */
  2979. isExpanded(range) {
  2980. return !Range.isCollapsed(range);
  2981. },
  2982. /**
  2983. * Check if a range is forward.
  2984. *
  2985. * This is the opposite of [[Range.isBackward]] and is provided for legibility.
  2986. */
  2987. isForward(range) {
  2988. return !Range.isBackward(range);
  2989. },
  2990. /**
  2991. * Check if a value implements the [[Range]] interface.
  2992. */
  2993. isRange(value) {
  2994. return isPlainObject(value) && Point.isPoint(value.anchor) && Point.isPoint(value.focus);
  2995. },
  2996. /**
  2997. * Iterate through all of the point entries in a range.
  2998. */
  2999. *points(range) {
  3000. yield [range.anchor, 'anchor'];
  3001. yield [range.focus, 'focus'];
  3002. },
  3003. /**
  3004. * Get the start point of a range.
  3005. */
  3006. start(range) {
  3007. var [start] = Range.edges(range);
  3008. return start;
  3009. },
  3010. /**
  3011. * Transform a range by an operation.
  3012. */
  3013. transform(range, op) {
  3014. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3015. return produce(range, r => {
  3016. if (r === null) {
  3017. return null;
  3018. }
  3019. var {
  3020. affinity = 'inward'
  3021. } = options;
  3022. var affinityAnchor;
  3023. var affinityFocus;
  3024. if (affinity === 'inward') {
  3025. // If the range is collapsed, make sure to use the same affinity to
  3026. // avoid the two points passing each other and expanding in the opposite
  3027. // direction
  3028. var isCollapsed = Range.isCollapsed(r);
  3029. if (Range.isForward(r)) {
  3030. affinityAnchor = 'forward';
  3031. affinityFocus = isCollapsed ? affinityAnchor : 'backward';
  3032. } else {
  3033. affinityAnchor = 'backward';
  3034. affinityFocus = isCollapsed ? affinityAnchor : 'forward';
  3035. }
  3036. } else if (affinity === 'outward') {
  3037. if (Range.isForward(r)) {
  3038. affinityAnchor = 'backward';
  3039. affinityFocus = 'forward';
  3040. } else {
  3041. affinityAnchor = 'forward';
  3042. affinityFocus = 'backward';
  3043. }
  3044. } else {
  3045. affinityAnchor = affinity;
  3046. affinityFocus = affinity;
  3047. }
  3048. var anchor = Point.transform(r.anchor, op, {
  3049. affinity: affinityAnchor
  3050. });
  3051. var focus = Point.transform(r.focus, op, {
  3052. affinity: affinityFocus
  3053. });
  3054. if (!anchor || !focus) {
  3055. return null;
  3056. }
  3057. r.anchor = anchor;
  3058. r.focus = focus;
  3059. });
  3060. }
  3061. };
  3062. var RangeRef = {
  3063. /**
  3064. * Transform the range ref's current value by an operation.
  3065. */
  3066. transform(ref, op) {
  3067. var {
  3068. current,
  3069. affinity
  3070. } = ref;
  3071. if (current == null) {
  3072. return;
  3073. }
  3074. var path = Range.transform(current, op, {
  3075. affinity
  3076. });
  3077. ref.current = path;
  3078. if (path == null) {
  3079. ref.unref();
  3080. }
  3081. }
  3082. };
  3083. /*
  3084. Custom deep equal comparison for Slate nodes.
  3085. We don't need general purpose deep equality;
  3086. Slate only supports plain values, Arrays, and nested objects.
  3087. Complex values nested inside Arrays are not supported.
  3088. Slate objects are designed to be serialised, so
  3089. missing keys are deliberately normalised to undefined.
  3090. */
  3091. var isDeepEqual = (node, another) => {
  3092. for (var key in node) {
  3093. var a = node[key];
  3094. var b = another[key];
  3095. if (isPlainObject(a) && isPlainObject(b)) {
  3096. if (!isDeepEqual(a, b)) return false;
  3097. } else if (Array.isArray(a) && Array.isArray(b)) {
  3098. if (a.length !== b.length) return false;
  3099. for (var i = 0; i < a.length; i++) {
  3100. if (a[i] !== b[i]) return false;
  3101. }
  3102. } else if (a !== b) {
  3103. return false;
  3104. }
  3105. }
  3106. /*
  3107. Deep object equality is only necessary in one direction; in the reverse direction
  3108. we are only looking for keys that are missing.
  3109. As above, undefined keys are normalised to missing.
  3110. */
  3111. for (var _key in another) {
  3112. if (node[_key] === undefined && another[_key] !== undefined) {
  3113. return false;
  3114. }
  3115. }
  3116. return true;
  3117. };
  3118. var _excluded$1 = ["text"],
  3119. _excluded2$1 = ["anchor", "focus"];
  3120. 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; }
  3121. 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; }
  3122. var Text = {
  3123. /**
  3124. * Check if two text nodes are equal.
  3125. *
  3126. * When loose is set, the text is not compared. This is
  3127. * used to check whether sibling text nodes can be merged.
  3128. */
  3129. equals(text, another) {
  3130. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3131. var {
  3132. loose = false
  3133. } = options;
  3134. function omitText(obj) {
  3135. var rest = _objectWithoutProperties(obj, _excluded$1);
  3136. return rest;
  3137. }
  3138. return isDeepEqual(loose ? omitText(text) : text, loose ? omitText(another) : another);
  3139. },
  3140. /**
  3141. * Check if a value implements the `Text` interface.
  3142. */
  3143. isText(value) {
  3144. return isPlainObject(value) && typeof value.text === 'string';
  3145. },
  3146. /**
  3147. * Check if a value is a list of `Text` objects.
  3148. */
  3149. isTextList(value) {
  3150. return Array.isArray(value) && value.every(val => Text.isText(val));
  3151. },
  3152. /**
  3153. * Check if some props are a partial of Text.
  3154. */
  3155. isTextProps(props) {
  3156. return props.text !== undefined;
  3157. },
  3158. /**
  3159. * Check if an text matches set of properties.
  3160. *
  3161. * Note: this is for matching custom properties, and it does not ensure that
  3162. * the `text` property are two nodes equal.
  3163. */
  3164. matches(text, props) {
  3165. for (var key in props) {
  3166. if (key === 'text') {
  3167. continue;
  3168. }
  3169. if (!text.hasOwnProperty(key) || text[key] !== props[key]) {
  3170. return false;
  3171. }
  3172. }
  3173. return true;
  3174. },
  3175. /**
  3176. * Get the leaves for a text node given decorations.
  3177. */
  3178. decorations(node, decorations) {
  3179. var leaves = [_objectSpread$4({}, node)];
  3180. for (var dec of decorations) {
  3181. var rest = _objectWithoutProperties(dec, _excluded2$1);
  3182. var [start, end] = Range.edges(dec);
  3183. var next = [];
  3184. var o = 0;
  3185. for (var leaf of leaves) {
  3186. var {
  3187. length
  3188. } = leaf.text;
  3189. var offset = o;
  3190. o += length; // If the range encompases the entire leaf, add the range.
  3191. if (start.offset <= offset && end.offset >= o) {
  3192. Object.assign(leaf, rest);
  3193. next.push(leaf);
  3194. continue;
  3195. } // If the range expanded and match the leaf, or starts after, or ends before it, continue.
  3196. if (start.offset !== end.offset && (start.offset === o || end.offset === offset) || start.offset > o || end.offset < offset || end.offset === offset && offset !== 0) {
  3197. next.push(leaf);
  3198. continue;
  3199. } // Otherwise we need to split the leaf, at the start, end, or both,
  3200. // and add the range to the middle intersecting section. Do the end
  3201. // split first since we don't need to update the offset that way.
  3202. var middle = leaf;
  3203. var before = void 0;
  3204. var after = void 0;
  3205. if (end.offset < o) {
  3206. var off = end.offset - offset;
  3207. after = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3208. text: middle.text.slice(off)
  3209. });
  3210. middle = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3211. text: middle.text.slice(0, off)
  3212. });
  3213. }
  3214. if (start.offset > offset) {
  3215. var _off = start.offset - offset;
  3216. before = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3217. text: middle.text.slice(0, _off)
  3218. });
  3219. middle = _objectSpread$4(_objectSpread$4({}, middle), {}, {
  3220. text: middle.text.slice(_off)
  3221. });
  3222. }
  3223. Object.assign(middle, rest);
  3224. if (before) {
  3225. next.push(before);
  3226. }
  3227. next.push(middle);
  3228. if (after) {
  3229. next.push(after);
  3230. }
  3231. }
  3232. leaves = next;
  3233. }
  3234. return leaves;
  3235. }
  3236. };
  3237. 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; }
  3238. 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; }
  3239. var applyToDraft = (editor, selection, op) => {
  3240. switch (op.type) {
  3241. case 'insert_node':
  3242. {
  3243. var {
  3244. path,
  3245. node
  3246. } = op;
  3247. var parent = Node.parent(editor, path);
  3248. var index = path[path.length - 1];
  3249. if (index > parent.children.length) {
  3250. throw new Error("Cannot apply an \"insert_node\" operation at path [".concat(path, "] because the destination is past the end of the node."));
  3251. }
  3252. parent.children.splice(index, 0, node);
  3253. if (selection) {
  3254. for (var [point, key] of Range.points(selection)) {
  3255. selection[key] = Point.transform(point, op);
  3256. }
  3257. }
  3258. break;
  3259. }
  3260. case 'insert_text':
  3261. {
  3262. var {
  3263. path: _path,
  3264. offset,
  3265. text
  3266. } = op;
  3267. if (text.length === 0) break;
  3268. var _node = Node.leaf(editor, _path);
  3269. var before = _node.text.slice(0, offset);
  3270. var after = _node.text.slice(offset);
  3271. _node.text = before + text + after;
  3272. if (selection) {
  3273. for (var [_point, _key] of Range.points(selection)) {
  3274. selection[_key] = Point.transform(_point, op);
  3275. }
  3276. }
  3277. break;
  3278. }
  3279. case 'merge_node':
  3280. {
  3281. var {
  3282. path: _path2
  3283. } = op;
  3284. var _node2 = Node.get(editor, _path2);
  3285. var prevPath = Path.previous(_path2);
  3286. var prev = Node.get(editor, prevPath);
  3287. var _parent = Node.parent(editor, _path2);
  3288. var _index = _path2[_path2.length - 1];
  3289. if (Text.isText(_node2) && Text.isText(prev)) {
  3290. prev.text += _node2.text;
  3291. } else if (!Text.isText(_node2) && !Text.isText(prev)) {
  3292. prev.children.push(..._node2.children);
  3293. } else {
  3294. throw new Error("Cannot apply a \"merge_node\" operation at path [".concat(_path2, "] to nodes of different interfaces: ").concat(_node2, " ").concat(prev));
  3295. }
  3296. _parent.children.splice(_index, 1);
  3297. if (selection) {
  3298. for (var [_point2, _key2] of Range.points(selection)) {
  3299. selection[_key2] = Point.transform(_point2, op);
  3300. }
  3301. }
  3302. break;
  3303. }
  3304. case 'move_node':
  3305. {
  3306. var {
  3307. path: _path3,
  3308. newPath
  3309. } = op;
  3310. if (Path.isAncestor(_path3, newPath)) {
  3311. throw new Error("Cannot move a path [".concat(_path3, "] to new path [").concat(newPath, "] because the destination is inside itself."));
  3312. }
  3313. var _node3 = Node.get(editor, _path3);
  3314. var _parent2 = Node.parent(editor, _path3);
  3315. var _index2 = _path3[_path3.length - 1]; // This is tricky, but since the `path` and `newPath` both refer to
  3316. // the same snapshot in time, there's a mismatch. After either
  3317. // removing the original position, the second step's path can be out
  3318. // of date. So instead of using the `op.newPath` directly, we
  3319. // transform `op.path` to ascertain what the `newPath` would be after
  3320. // the operation was applied.
  3321. _parent2.children.splice(_index2, 1);
  3322. var truePath = Path.transform(_path3, op);
  3323. var newParent = Node.get(editor, Path.parent(truePath));
  3324. var newIndex = truePath[truePath.length - 1];
  3325. newParent.children.splice(newIndex, 0, _node3);
  3326. if (selection) {
  3327. for (var [_point3, _key3] of Range.points(selection)) {
  3328. selection[_key3] = Point.transform(_point3, op);
  3329. }
  3330. }
  3331. break;
  3332. }
  3333. case 'remove_node':
  3334. {
  3335. var {
  3336. path: _path4
  3337. } = op;
  3338. var _index3 = _path4[_path4.length - 1];
  3339. var _parent3 = Node.parent(editor, _path4);
  3340. _parent3.children.splice(_index3, 1); // Transform all of the points in the value, but if the point was in the
  3341. // node that was removed we need to update the range or remove it.
  3342. if (selection) {
  3343. for (var [_point4, _key4] of Range.points(selection)) {
  3344. var result = Point.transform(_point4, op);
  3345. if (selection != null && result != null) {
  3346. selection[_key4] = result;
  3347. } else {
  3348. var _prev = void 0;
  3349. var next = void 0;
  3350. for (var [n, p] of Node.texts(editor)) {
  3351. if (Path.compare(p, _path4) === -1) {
  3352. _prev = [n, p];
  3353. } else {
  3354. next = [n, p];
  3355. break;
  3356. }
  3357. }
  3358. var preferNext = false;
  3359. if (_prev && next) {
  3360. if (Path.equals(next[1], _path4)) {
  3361. preferNext = !Path.hasPrevious(next[1]);
  3362. } else {
  3363. preferNext = Path.common(_prev[1], _path4).length < Path.common(next[1], _path4).length;
  3364. }
  3365. }
  3366. if (_prev && !preferNext) {
  3367. _point4.path = _prev[1];
  3368. _point4.offset = _prev[0].text.length;
  3369. } else if (next) {
  3370. _point4.path = next[1];
  3371. _point4.offset = 0;
  3372. } else {
  3373. selection = null;
  3374. }
  3375. }
  3376. }
  3377. }
  3378. break;
  3379. }
  3380. case 'remove_text':
  3381. {
  3382. var {
  3383. path: _path5,
  3384. offset: _offset,
  3385. text: _text
  3386. } = op;
  3387. if (_text.length === 0) break;
  3388. var _node4 = Node.leaf(editor, _path5);
  3389. var _before = _node4.text.slice(0, _offset);
  3390. var _after = _node4.text.slice(_offset + _text.length);
  3391. _node4.text = _before + _after;
  3392. if (selection) {
  3393. for (var [_point5, _key5] of Range.points(selection)) {
  3394. selection[_key5] = Point.transform(_point5, op);
  3395. }
  3396. }
  3397. break;
  3398. }
  3399. case 'set_node':
  3400. {
  3401. var {
  3402. path: _path6,
  3403. properties,
  3404. newProperties
  3405. } = op;
  3406. if (_path6.length === 0) {
  3407. throw new Error("Cannot set properties on the root node!");
  3408. }
  3409. var _node5 = Node.get(editor, _path6);
  3410. for (var _key6 in newProperties) {
  3411. if (_key6 === 'children' || _key6 === 'text') {
  3412. throw new Error("Cannot set the \"".concat(_key6, "\" property of nodes!"));
  3413. }
  3414. var value = newProperties[_key6];
  3415. if (value == null) {
  3416. delete _node5[_key6];
  3417. } else {
  3418. _node5[_key6] = value;
  3419. }
  3420. } // properties that were previously defined, but are now missing, must be deleted
  3421. for (var _key7 in properties) {
  3422. if (!newProperties.hasOwnProperty(_key7)) {
  3423. delete _node5[_key7];
  3424. }
  3425. }
  3426. break;
  3427. }
  3428. case 'set_selection':
  3429. {
  3430. var {
  3431. newProperties: _newProperties
  3432. } = op;
  3433. if (_newProperties == null) {
  3434. selection = _newProperties;
  3435. } else {
  3436. if (selection == null) {
  3437. if (!Range.isRange(_newProperties)) {
  3438. throw new Error("Cannot apply an incomplete \"set_selection\" operation properties ".concat(JSON.stringify(_newProperties), " when there is no current selection."));
  3439. }
  3440. selection = _objectSpread$3({}, _newProperties);
  3441. }
  3442. for (var _key8 in _newProperties) {
  3443. var _value = _newProperties[_key8];
  3444. if (_value == null) {
  3445. if (_key8 === 'anchor' || _key8 === 'focus') {
  3446. throw new Error("Cannot remove the \"".concat(_key8, "\" selection property"));
  3447. }
  3448. delete selection[_key8];
  3449. } else {
  3450. selection[_key8] = _value;
  3451. }
  3452. }
  3453. }
  3454. break;
  3455. }
  3456. case 'split_node':
  3457. {
  3458. var {
  3459. path: _path7,
  3460. position,
  3461. properties: _properties
  3462. } = op;
  3463. if (_path7.length === 0) {
  3464. throw new Error("Cannot apply a \"split_node\" operation at path [".concat(_path7, "] because the root node cannot be split."));
  3465. }
  3466. var _node6 = Node.get(editor, _path7);
  3467. var _parent4 = Node.parent(editor, _path7);
  3468. var _index4 = _path7[_path7.length - 1];
  3469. var newNode;
  3470. if (Text.isText(_node6)) {
  3471. var _before2 = _node6.text.slice(0, position);
  3472. var _after2 = _node6.text.slice(position);
  3473. _node6.text = _before2;
  3474. newNode = _objectSpread$3(_objectSpread$3({}, _properties), {}, {
  3475. text: _after2
  3476. });
  3477. } else {
  3478. var _before3 = _node6.children.slice(0, position);
  3479. var _after3 = _node6.children.slice(position);
  3480. _node6.children = _before3;
  3481. newNode = _objectSpread$3(_objectSpread$3({}, _properties), {}, {
  3482. children: _after3
  3483. });
  3484. }
  3485. _parent4.children.splice(_index4 + 1, 0, newNode);
  3486. if (selection) {
  3487. for (var [_point6, _key9] of Range.points(selection)) {
  3488. selection[_key9] = Point.transform(_point6, op);
  3489. }
  3490. }
  3491. break;
  3492. }
  3493. }
  3494. return selection;
  3495. };
  3496. var GeneralTransforms = {
  3497. /**
  3498. * Transform the editor by an operation.
  3499. */
  3500. transform(editor, op) {
  3501. editor.children = createDraft(editor.children);
  3502. var selection = editor.selection && createDraft(editor.selection);
  3503. try {
  3504. selection = applyToDraft(editor, selection, op);
  3505. } finally {
  3506. editor.children = finishDraft(editor.children);
  3507. if (selection) {
  3508. editor.selection = isDraft(selection) ? finishDraft(selection) : selection;
  3509. } else {
  3510. editor.selection = null;
  3511. }
  3512. }
  3513. }
  3514. };
  3515. var _excluded = ["text"],
  3516. _excluded2 = ["children"];
  3517. 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; }
  3518. 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; }
  3519. var NodeTransforms = {
  3520. /**
  3521. * Insert nodes at a specific location in the Editor.
  3522. */
  3523. insertNodes(editor, nodes) {
  3524. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3525. Editor.withoutNormalizing(editor, () => {
  3526. var {
  3527. hanging = false,
  3528. voids = false,
  3529. mode = 'lowest'
  3530. } = options;
  3531. var {
  3532. at,
  3533. match,
  3534. select
  3535. } = options;
  3536. if (Node.isNode(nodes)) {
  3537. nodes = [nodes];
  3538. }
  3539. if (nodes.length === 0) {
  3540. return;
  3541. }
  3542. var [node] = nodes; // By default, use the selection as the target location. But if there is
  3543. // no selection, insert at the end of the document since that is such a
  3544. // common use case when inserting from a non-selected state.
  3545. if (!at) {
  3546. if (editor.selection) {
  3547. at = editor.selection;
  3548. } else if (editor.children.length > 0) {
  3549. at = Editor.end(editor, []);
  3550. } else {
  3551. at = [0];
  3552. }
  3553. select = true;
  3554. }
  3555. if (select == null) {
  3556. select = false;
  3557. }
  3558. if (Range.isRange(at)) {
  3559. if (!hanging) {
  3560. at = Editor.unhangRange(editor, at);
  3561. }
  3562. if (Range.isCollapsed(at)) {
  3563. at = at.anchor;
  3564. } else {
  3565. var [, end] = Range.edges(at);
  3566. var pointRef = Editor.pointRef(editor, end);
  3567. Transforms.delete(editor, {
  3568. at
  3569. });
  3570. at = pointRef.unref();
  3571. }
  3572. }
  3573. if (Point.isPoint(at)) {
  3574. if (match == null) {
  3575. if (Text.isText(node)) {
  3576. match = n => Text.isText(n);
  3577. } else if (editor.isInline(node)) {
  3578. match = n => Text.isText(n) || Editor.isInline(editor, n);
  3579. } else {
  3580. match = n => Editor.isBlock(editor, n);
  3581. }
  3582. }
  3583. var [entry] = Editor.nodes(editor, {
  3584. at: at.path,
  3585. match,
  3586. mode,
  3587. voids
  3588. });
  3589. if (entry) {
  3590. var [, _matchPath] = entry;
  3591. var pathRef = Editor.pathRef(editor, _matchPath);
  3592. var isAtEnd = Editor.isEnd(editor, at, _matchPath);
  3593. Transforms.splitNodes(editor, {
  3594. at,
  3595. match,
  3596. mode,
  3597. voids
  3598. });
  3599. var path = pathRef.unref();
  3600. at = isAtEnd ? Path.next(path) : path;
  3601. } else {
  3602. return;
  3603. }
  3604. }
  3605. var parentPath = Path.parent(at);
  3606. var index = at[at.length - 1];
  3607. if (!voids && Editor.void(editor, {
  3608. at: parentPath
  3609. })) {
  3610. return;
  3611. }
  3612. for (var _node of nodes) {
  3613. var _path = parentPath.concat(index);
  3614. index++;
  3615. editor.apply({
  3616. type: 'insert_node',
  3617. path: _path,
  3618. node: _node
  3619. });
  3620. at = Path.next(at);
  3621. }
  3622. at = Path.previous(at);
  3623. if (select) {
  3624. var point = Editor.end(editor, at);
  3625. if (point) {
  3626. Transforms.select(editor, point);
  3627. }
  3628. }
  3629. });
  3630. },
  3631. /**
  3632. * Lift nodes at a specific location upwards in the document tree, splitting
  3633. * their parent in two if necessary.
  3634. */
  3635. liftNodes(editor) {
  3636. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3637. Editor.withoutNormalizing(editor, () => {
  3638. var {
  3639. at = editor.selection,
  3640. mode = 'lowest',
  3641. voids = false
  3642. } = options;
  3643. var {
  3644. match
  3645. } = options;
  3646. if (match == null) {
  3647. match = Path.isPath(at) ? matchPath(editor, at) : n => Editor.isBlock(editor, n);
  3648. }
  3649. if (!at) {
  3650. return;
  3651. }
  3652. var matches = Editor.nodes(editor, {
  3653. at,
  3654. match,
  3655. mode,
  3656. voids
  3657. });
  3658. var pathRefs = Array.from(matches, _ref => {
  3659. var [, p] = _ref;
  3660. return Editor.pathRef(editor, p);
  3661. });
  3662. for (var pathRef of pathRefs) {
  3663. var path = pathRef.unref();
  3664. if (path.length < 2) {
  3665. throw new Error("Cannot lift node at a path [".concat(path, "] because it has a depth of less than `2`."));
  3666. }
  3667. var parentNodeEntry = Editor.node(editor, Path.parent(path));
  3668. var [parent, parentPath] = parentNodeEntry;
  3669. var index = path[path.length - 1];
  3670. var {
  3671. length
  3672. } = parent.children;
  3673. if (length === 1) {
  3674. var toPath = Path.next(parentPath);
  3675. Transforms.moveNodes(editor, {
  3676. at: path,
  3677. to: toPath,
  3678. voids
  3679. });
  3680. Transforms.removeNodes(editor, {
  3681. at: parentPath,
  3682. voids
  3683. });
  3684. } else if (index === 0) {
  3685. Transforms.moveNodes(editor, {
  3686. at: path,
  3687. to: parentPath,
  3688. voids
  3689. });
  3690. } else if (index === length - 1) {
  3691. var _toPath = Path.next(parentPath);
  3692. Transforms.moveNodes(editor, {
  3693. at: path,
  3694. to: _toPath,
  3695. voids
  3696. });
  3697. } else {
  3698. var splitPath = Path.next(path);
  3699. var _toPath2 = Path.next(parentPath);
  3700. Transforms.splitNodes(editor, {
  3701. at: splitPath,
  3702. voids
  3703. });
  3704. Transforms.moveNodes(editor, {
  3705. at: path,
  3706. to: _toPath2,
  3707. voids
  3708. });
  3709. }
  3710. }
  3711. });
  3712. },
  3713. /**
  3714. * Merge a node at a location with the previous node of the same depth,
  3715. * removing any empty containing nodes after the merge if necessary.
  3716. */
  3717. mergeNodes(editor) {
  3718. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3719. Editor.withoutNormalizing(editor, () => {
  3720. var {
  3721. match,
  3722. at = editor.selection
  3723. } = options;
  3724. var {
  3725. hanging = false,
  3726. voids = false,
  3727. mode = 'lowest'
  3728. } = options;
  3729. if (!at) {
  3730. return;
  3731. }
  3732. if (match == null) {
  3733. if (Path.isPath(at)) {
  3734. var [parent] = Editor.parent(editor, at);
  3735. match = n => parent.children.includes(n);
  3736. } else {
  3737. match = n => Editor.isBlock(editor, n);
  3738. }
  3739. }
  3740. if (!hanging && Range.isRange(at)) {
  3741. at = Editor.unhangRange(editor, at);
  3742. }
  3743. if (Range.isRange(at)) {
  3744. if (Range.isCollapsed(at)) {
  3745. at = at.anchor;
  3746. } else {
  3747. var [, end] = Range.edges(at);
  3748. var pointRef = Editor.pointRef(editor, end);
  3749. Transforms.delete(editor, {
  3750. at
  3751. });
  3752. at = pointRef.unref();
  3753. if (options.at == null) {
  3754. Transforms.select(editor, at);
  3755. }
  3756. }
  3757. }
  3758. var [current] = Editor.nodes(editor, {
  3759. at,
  3760. match,
  3761. voids,
  3762. mode
  3763. });
  3764. var prev = Editor.previous(editor, {
  3765. at,
  3766. match,
  3767. voids,
  3768. mode
  3769. });
  3770. if (!current || !prev) {
  3771. return;
  3772. }
  3773. var [node, path] = current;
  3774. var [prevNode, prevPath] = prev;
  3775. if (path.length === 0 || prevPath.length === 0) {
  3776. return;
  3777. }
  3778. var newPath = Path.next(prevPath);
  3779. var commonPath = Path.common(path, prevPath);
  3780. var isPreviousSibling = Path.isSibling(path, prevPath);
  3781. var levels = Array.from(Editor.levels(editor, {
  3782. at: path
  3783. }), _ref2 => {
  3784. var [n] = _ref2;
  3785. return n;
  3786. }).slice(commonPath.length).slice(0, -1); // Determine if the merge will leave an ancestor of the path empty as a
  3787. // result, in which case we'll want to remove it after merging.
  3788. var emptyAncestor = Editor.above(editor, {
  3789. at: path,
  3790. mode: 'highest',
  3791. match: n => levels.includes(n) && hasSingleChildNest(editor, n)
  3792. });
  3793. var emptyRef = emptyAncestor && Editor.pathRef(editor, emptyAncestor[1]);
  3794. var properties;
  3795. var position; // Ensure that the nodes are equivalent, and figure out what the position
  3796. // and extra properties of the merge will be.
  3797. if (Text.isText(node) && Text.isText(prevNode)) {
  3798. var rest = _objectWithoutProperties(node, _excluded);
  3799. position = prevNode.text.length;
  3800. properties = rest;
  3801. } else if (Element.isElement(node) && Element.isElement(prevNode)) {
  3802. var rest = _objectWithoutProperties(node, _excluded2);
  3803. position = prevNode.children.length;
  3804. properties = rest;
  3805. } else {
  3806. 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)));
  3807. } // If the node isn't already the next sibling of the previous node, move
  3808. // it so that it is before merging.
  3809. if (!isPreviousSibling) {
  3810. Transforms.moveNodes(editor, {
  3811. at: path,
  3812. to: newPath,
  3813. voids
  3814. });
  3815. } // If there was going to be an empty ancestor of the node that was merged,
  3816. // we remove it from the tree.
  3817. if (emptyRef) {
  3818. Transforms.removeNodes(editor, {
  3819. at: emptyRef.current,
  3820. voids
  3821. });
  3822. } // If the target node that we're merging with is empty, remove it instead
  3823. // of merging the two. This is a common rich text editor behavior to
  3824. // prevent losing formatting when deleting entire nodes when you have a
  3825. // hanging selection.
  3826. // if prevNode is first child in parent,don't remove it.
  3827. if (Element.isElement(prevNode) && Editor.isEmpty(editor, prevNode) || Text.isText(prevNode) && prevNode.text === '' && prevPath[prevPath.length - 1] !== 0) {
  3828. Transforms.removeNodes(editor, {
  3829. at: prevPath,
  3830. voids
  3831. });
  3832. } else {
  3833. editor.apply({
  3834. type: 'merge_node',
  3835. path: newPath,
  3836. position,
  3837. properties
  3838. });
  3839. }
  3840. if (emptyRef) {
  3841. emptyRef.unref();
  3842. }
  3843. });
  3844. },
  3845. /**
  3846. * Move the nodes at a location to a new location.
  3847. */
  3848. moveNodes(editor, options) {
  3849. Editor.withoutNormalizing(editor, () => {
  3850. var {
  3851. to,
  3852. at = editor.selection,
  3853. mode = 'lowest',
  3854. voids = false
  3855. } = options;
  3856. var {
  3857. match
  3858. } = options;
  3859. if (!at) {
  3860. return;
  3861. }
  3862. if (match == null) {
  3863. match = Path.isPath(at) ? matchPath(editor, at) : n => Editor.isBlock(editor, n);
  3864. }
  3865. var toRef = Editor.pathRef(editor, to);
  3866. var targets = Editor.nodes(editor, {
  3867. at,
  3868. match,
  3869. mode,
  3870. voids
  3871. });
  3872. var pathRefs = Array.from(targets, _ref3 => {
  3873. var [, p] = _ref3;
  3874. return Editor.pathRef(editor, p);
  3875. });
  3876. for (var pathRef of pathRefs) {
  3877. var path = pathRef.unref();
  3878. var newPath = toRef.current;
  3879. if (path.length !== 0) {
  3880. editor.apply({
  3881. type: 'move_node',
  3882. path,
  3883. newPath
  3884. });
  3885. }
  3886. if (toRef.current && Path.isSibling(newPath, path) && Path.isAfter(newPath, path)) {
  3887. // When performing a sibling move to a later index, the path at the destination is shifted
  3888. // to before the insertion point instead of after. To ensure our group of nodes are inserted
  3889. // in the correct order we increment toRef to account for that
  3890. toRef.current = Path.next(toRef.current);
  3891. }
  3892. }
  3893. toRef.unref();
  3894. });
  3895. },
  3896. /**
  3897. * Remove the nodes at a specific location in the document.
  3898. */
  3899. removeNodes(editor) {
  3900. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3901. Editor.withoutNormalizing(editor, () => {
  3902. var {
  3903. hanging = false,
  3904. voids = false,
  3905. mode = 'lowest'
  3906. } = options;
  3907. var {
  3908. at = editor.selection,
  3909. match
  3910. } = options;
  3911. if (!at) {
  3912. return;
  3913. }
  3914. if (match == null) {
  3915. match = Path.isPath(at) ? matchPath(editor, at) : n => Editor.isBlock(editor, n);
  3916. }
  3917. if (!hanging && Range.isRange(at)) {
  3918. at = Editor.unhangRange(editor, at);
  3919. }
  3920. var depths = Editor.nodes(editor, {
  3921. at,
  3922. match,
  3923. mode,
  3924. voids
  3925. });
  3926. var pathRefs = Array.from(depths, _ref4 => {
  3927. var [, p] = _ref4;
  3928. return Editor.pathRef(editor, p);
  3929. });
  3930. for (var pathRef of pathRefs) {
  3931. var path = pathRef.unref();
  3932. if (path) {
  3933. var [node] = Editor.node(editor, path);
  3934. editor.apply({
  3935. type: 'remove_node',
  3936. path,
  3937. node
  3938. });
  3939. }
  3940. }
  3941. });
  3942. },
  3943. /**
  3944. * Set new properties on the nodes at a location.
  3945. */
  3946. setNodes(editor, props) {
  3947. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  3948. Editor.withoutNormalizing(editor, () => {
  3949. var {
  3950. match,
  3951. at = editor.selection
  3952. } = options;
  3953. var {
  3954. hanging = false,
  3955. mode = 'lowest',
  3956. split = false,
  3957. voids = false
  3958. } = options;
  3959. if (!at) {
  3960. return;
  3961. }
  3962. if (match == null) {
  3963. match = Path.isPath(at) ? matchPath(editor, at) : n => Editor.isBlock(editor, n);
  3964. }
  3965. if (!hanging && Range.isRange(at)) {
  3966. at = Editor.unhangRange(editor, at);
  3967. }
  3968. if (split && Range.isRange(at)) {
  3969. if (Range.isCollapsed(at) && Editor.leaf(editor, at.anchor)[0].text.length > 0) {
  3970. // If the range is collapsed in a non-empty node and 'split' is true, there's nothing to
  3971. // set that won't get normalized away
  3972. return;
  3973. }
  3974. var rangeRef = Editor.rangeRef(editor, at, {
  3975. affinity: 'inward'
  3976. });
  3977. var [start, end] = Range.edges(at);
  3978. var splitMode = mode === 'lowest' ? 'lowest' : 'highest';
  3979. var endAtEndOfNode = Editor.isEnd(editor, end, end.path);
  3980. Transforms.splitNodes(editor, {
  3981. at: end,
  3982. match,
  3983. mode: splitMode,
  3984. voids,
  3985. always: !endAtEndOfNode
  3986. });
  3987. var startAtStartOfNode = Editor.isStart(editor, start, start.path);
  3988. Transforms.splitNodes(editor, {
  3989. at: start,
  3990. match,
  3991. mode: splitMode,
  3992. voids,
  3993. always: !startAtStartOfNode
  3994. });
  3995. at = rangeRef.unref();
  3996. if (options.at == null) {
  3997. Transforms.select(editor, at);
  3998. }
  3999. }
  4000. for (var [node, path] of Editor.nodes(editor, {
  4001. at,
  4002. match,
  4003. mode,
  4004. voids
  4005. })) {
  4006. var properties = {};
  4007. var newProperties = {}; // You can't set properties on the editor node.
  4008. if (path.length === 0) {
  4009. continue;
  4010. }
  4011. var hasChanges = false;
  4012. for (var k in props) {
  4013. if (k === 'children' || k === 'text') {
  4014. continue;
  4015. }
  4016. if (props[k] !== node[k]) {
  4017. hasChanges = true; // Omit new properties from the old properties list
  4018. if (node.hasOwnProperty(k)) properties[k] = node[k]; // Omit properties that have been removed from the new properties list
  4019. if (props[k] != null) newProperties[k] = props[k];
  4020. }
  4021. }
  4022. if (hasChanges) {
  4023. editor.apply({
  4024. type: 'set_node',
  4025. path,
  4026. properties,
  4027. newProperties
  4028. });
  4029. }
  4030. }
  4031. });
  4032. },
  4033. /**
  4034. * Split the nodes at a specific location.
  4035. */
  4036. splitNodes(editor) {
  4037. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4038. Editor.withoutNormalizing(editor, () => {
  4039. var {
  4040. mode = 'lowest',
  4041. voids = false
  4042. } = options;
  4043. var {
  4044. match,
  4045. at = editor.selection,
  4046. height = 0,
  4047. always = false
  4048. } = options;
  4049. if (match == null) {
  4050. match = n => Editor.isBlock(editor, n);
  4051. }
  4052. if (Range.isRange(at)) {
  4053. at = deleteRange(editor, at);
  4054. } // If the target is a path, the default height-skipping and position
  4055. // counters need to account for us potentially splitting at a non-leaf.
  4056. if (Path.isPath(at)) {
  4057. var path = at;
  4058. var point = Editor.point(editor, path);
  4059. var [parent] = Editor.parent(editor, path);
  4060. match = n => n === parent;
  4061. height = point.path.length - path.length + 1;
  4062. at = point;
  4063. always = true;
  4064. }
  4065. if (!at) {
  4066. return;
  4067. }
  4068. var beforeRef = Editor.pointRef(editor, at, {
  4069. affinity: 'backward'
  4070. });
  4071. var [highest] = Editor.nodes(editor, {
  4072. at,
  4073. match,
  4074. mode,
  4075. voids
  4076. });
  4077. if (!highest) {
  4078. return;
  4079. }
  4080. var voidMatch = Editor.void(editor, {
  4081. at,
  4082. mode: 'highest'
  4083. });
  4084. var nudge = 0;
  4085. if (!voids && voidMatch) {
  4086. var [voidNode, voidPath] = voidMatch;
  4087. if (Element.isElement(voidNode) && editor.isInline(voidNode)) {
  4088. var after = Editor.after(editor, voidPath);
  4089. if (!after) {
  4090. var text = {
  4091. text: ''
  4092. };
  4093. var afterPath = Path.next(voidPath);
  4094. Transforms.insertNodes(editor, text, {
  4095. at: afterPath,
  4096. voids
  4097. });
  4098. after = Editor.point(editor, afterPath);
  4099. }
  4100. at = after;
  4101. always = true;
  4102. }
  4103. var siblingHeight = at.path.length - voidPath.length;
  4104. height = siblingHeight + 1;
  4105. always = true;
  4106. }
  4107. var afterRef = Editor.pointRef(editor, at);
  4108. var depth = at.path.length - height;
  4109. var [, highestPath] = highest;
  4110. var lowestPath = at.path.slice(0, depth);
  4111. var position = height === 0 ? at.offset : at.path[depth] + nudge;
  4112. for (var [node, _path2] of Editor.levels(editor, {
  4113. at: lowestPath,
  4114. reverse: true,
  4115. voids
  4116. })) {
  4117. var split = false;
  4118. if (_path2.length < highestPath.length || _path2.length === 0 || !voids && Editor.isVoid(editor, node)) {
  4119. break;
  4120. }
  4121. var _point = beforeRef.current;
  4122. var isEnd = Editor.isEnd(editor, _point, _path2);
  4123. if (always || !beforeRef || !Editor.isEdge(editor, _point, _path2)) {
  4124. split = true;
  4125. var properties = Node.extractProps(node);
  4126. editor.apply({
  4127. type: 'split_node',
  4128. path: _path2,
  4129. position,
  4130. properties
  4131. });
  4132. }
  4133. position = _path2[_path2.length - 1] + (split || isEnd ? 1 : 0);
  4134. }
  4135. if (options.at == null) {
  4136. var _point2 = afterRef.current || Editor.end(editor, []);
  4137. Transforms.select(editor, _point2);
  4138. }
  4139. beforeRef.unref();
  4140. afterRef.unref();
  4141. });
  4142. },
  4143. /**
  4144. * Unset properties on the nodes at a location.
  4145. */
  4146. unsetNodes(editor, props) {
  4147. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4148. if (!Array.isArray(props)) {
  4149. props = [props];
  4150. }
  4151. var obj = {};
  4152. for (var key of props) {
  4153. obj[key] = null;
  4154. }
  4155. Transforms.setNodes(editor, obj, options);
  4156. },
  4157. /**
  4158. * Unwrap the nodes at a location from a parent node, splitting the parent if
  4159. * necessary to ensure that only the content in the range is unwrapped.
  4160. */
  4161. unwrapNodes(editor) {
  4162. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4163. Editor.withoutNormalizing(editor, () => {
  4164. var {
  4165. mode = 'lowest',
  4166. split = false,
  4167. voids = false
  4168. } = options;
  4169. var {
  4170. at = editor.selection,
  4171. match
  4172. } = options;
  4173. if (!at) {
  4174. return;
  4175. }
  4176. if (match == null) {
  4177. match = Path.isPath(at) ? matchPath(editor, at) : n => Editor.isBlock(editor, n);
  4178. }
  4179. if (Path.isPath(at)) {
  4180. at = Editor.range(editor, at);
  4181. }
  4182. var rangeRef = Range.isRange(at) ? Editor.rangeRef(editor, at) : null;
  4183. var matches = Editor.nodes(editor, {
  4184. at,
  4185. match,
  4186. mode,
  4187. voids
  4188. });
  4189. var pathRefs = Array.from(matches, _ref5 => {
  4190. var [, p] = _ref5;
  4191. return Editor.pathRef(editor, p);
  4192. } // unwrapNode will call liftNode which does not support splitting the node when nested.
  4193. // If we do not reverse the order and call it from top to the bottom, it will remove all blocks
  4194. // that wrap target node. So we reverse the order.
  4195. ).reverse();
  4196. var _loop = function _loop(pathRef) {
  4197. var path = pathRef.unref();
  4198. var [node] = Editor.node(editor, path);
  4199. var range = Editor.range(editor, path);
  4200. if (split && rangeRef) {
  4201. range = Range.intersection(rangeRef.current, range);
  4202. }
  4203. Transforms.liftNodes(editor, {
  4204. at: range,
  4205. match: n => Element.isAncestor(node) && node.children.includes(n),
  4206. voids
  4207. });
  4208. };
  4209. for (var pathRef of pathRefs) {
  4210. _loop(pathRef);
  4211. }
  4212. if (rangeRef) {
  4213. rangeRef.unref();
  4214. }
  4215. });
  4216. },
  4217. /**
  4218. * Wrap the nodes at a location in a new container node, splitting the edges
  4219. * of the range first to ensure that only the content in the range is wrapped.
  4220. */
  4221. wrapNodes(editor, element) {
  4222. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4223. Editor.withoutNormalizing(editor, () => {
  4224. var {
  4225. mode = 'lowest',
  4226. split = false,
  4227. voids = false
  4228. } = options;
  4229. var {
  4230. match,
  4231. at = editor.selection
  4232. } = options;
  4233. if (!at) {
  4234. return;
  4235. }
  4236. if (match == null) {
  4237. if (Path.isPath(at)) {
  4238. match = matchPath(editor, at);
  4239. } else if (editor.isInline(element)) {
  4240. match = n => Editor.isInline(editor, n) || Text.isText(n);
  4241. } else {
  4242. match = n => Editor.isBlock(editor, n);
  4243. }
  4244. }
  4245. if (split && Range.isRange(at)) {
  4246. var [start, end] = Range.edges(at);
  4247. var rangeRef = Editor.rangeRef(editor, at, {
  4248. affinity: 'inward'
  4249. });
  4250. Transforms.splitNodes(editor, {
  4251. at: end,
  4252. match,
  4253. voids
  4254. });
  4255. Transforms.splitNodes(editor, {
  4256. at: start,
  4257. match,
  4258. voids
  4259. });
  4260. at = rangeRef.unref();
  4261. if (options.at == null) {
  4262. Transforms.select(editor, at);
  4263. }
  4264. }
  4265. var roots = Array.from(Editor.nodes(editor, {
  4266. at,
  4267. match: editor.isInline(element) ? n => Editor.isBlock(editor, n) : n => Editor.isEditor(n),
  4268. mode: 'lowest',
  4269. voids
  4270. }));
  4271. for (var [, rootPath] of roots) {
  4272. var a = Range.isRange(at) ? Range.intersection(at, Editor.range(editor, rootPath)) : at;
  4273. if (!a) {
  4274. continue;
  4275. }
  4276. var matches = Array.from(Editor.nodes(editor, {
  4277. at: a,
  4278. match,
  4279. mode,
  4280. voids
  4281. }));
  4282. if (matches.length > 0) {
  4283. var _ret = function () {
  4284. var [first] = matches;
  4285. var last = matches[matches.length - 1];
  4286. var [, firstPath] = first;
  4287. var [, lastPath] = last;
  4288. if (firstPath.length === 0 && lastPath.length === 0) {
  4289. // if there's no matching parent - usually means the node is an editor - don't do anything
  4290. return "continue";
  4291. }
  4292. var commonPath = Path.equals(firstPath, lastPath) ? Path.parent(firstPath) : Path.common(firstPath, lastPath);
  4293. var range = Editor.range(editor, firstPath, lastPath);
  4294. var commonNodeEntry = Editor.node(editor, commonPath);
  4295. var [commonNode] = commonNodeEntry;
  4296. var depth = commonPath.length + 1;
  4297. var wrapperPath = Path.next(lastPath.slice(0, depth));
  4298. var wrapper = _objectSpread$2(_objectSpread$2({}, element), {}, {
  4299. children: []
  4300. });
  4301. Transforms.insertNodes(editor, wrapper, {
  4302. at: wrapperPath,
  4303. voids
  4304. });
  4305. Transforms.moveNodes(editor, {
  4306. at: range,
  4307. match: n => Element.isAncestor(commonNode) && commonNode.children.includes(n),
  4308. to: wrapperPath.concat(0),
  4309. voids
  4310. });
  4311. }();
  4312. if (_ret === "continue") continue;
  4313. }
  4314. }
  4315. });
  4316. }
  4317. };
  4318. var hasSingleChildNest = (editor, node) => {
  4319. if (Element.isElement(node)) {
  4320. var element = node;
  4321. if (Editor.isVoid(editor, node)) {
  4322. return true;
  4323. } else if (element.children.length === 1) {
  4324. return hasSingleChildNest(editor, element.children[0]);
  4325. } else {
  4326. return false;
  4327. }
  4328. } else if (Editor.isEditor(node)) {
  4329. return false;
  4330. } else {
  4331. return true;
  4332. }
  4333. };
  4334. /**
  4335. * Convert a range into a point by deleting it's content.
  4336. */
  4337. var deleteRange = (editor, range) => {
  4338. if (Range.isCollapsed(range)) {
  4339. return range.anchor;
  4340. } else {
  4341. var [, end] = Range.edges(range);
  4342. var pointRef = Editor.pointRef(editor, end);
  4343. Transforms.delete(editor, {
  4344. at: range
  4345. });
  4346. return pointRef.unref();
  4347. }
  4348. };
  4349. var matchPath = (editor, path) => {
  4350. var [node] = Editor.node(editor, path);
  4351. return n => n === node;
  4352. };
  4353. 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; }
  4354. 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; }
  4355. var SelectionTransforms = {
  4356. /**
  4357. * Collapse the selection.
  4358. */
  4359. collapse(editor) {
  4360. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4361. var {
  4362. edge = 'anchor'
  4363. } = options;
  4364. var {
  4365. selection
  4366. } = editor;
  4367. if (!selection) {
  4368. return;
  4369. } else if (edge === 'anchor') {
  4370. Transforms.select(editor, selection.anchor);
  4371. } else if (edge === 'focus') {
  4372. Transforms.select(editor, selection.focus);
  4373. } else if (edge === 'start') {
  4374. var [start] = Range.edges(selection);
  4375. Transforms.select(editor, start);
  4376. } else if (edge === 'end') {
  4377. var [, end] = Range.edges(selection);
  4378. Transforms.select(editor, end);
  4379. }
  4380. },
  4381. /**
  4382. * Unset the selection.
  4383. */
  4384. deselect(editor) {
  4385. var {
  4386. selection
  4387. } = editor;
  4388. if (selection) {
  4389. editor.apply({
  4390. type: 'set_selection',
  4391. properties: selection,
  4392. newProperties: null
  4393. });
  4394. }
  4395. },
  4396. /**
  4397. * Move the selection's point forward or backward.
  4398. */
  4399. move(editor) {
  4400. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4401. var {
  4402. selection
  4403. } = editor;
  4404. var {
  4405. distance = 1,
  4406. unit = 'character',
  4407. reverse = false
  4408. } = options;
  4409. var {
  4410. edge = null
  4411. } = options;
  4412. if (!selection) {
  4413. return;
  4414. }
  4415. if (edge === 'start') {
  4416. edge = Range.isBackward(selection) ? 'focus' : 'anchor';
  4417. }
  4418. if (edge === 'end') {
  4419. edge = Range.isBackward(selection) ? 'anchor' : 'focus';
  4420. }
  4421. var {
  4422. anchor,
  4423. focus
  4424. } = selection;
  4425. var opts = {
  4426. distance,
  4427. unit
  4428. };
  4429. var props = {};
  4430. if (edge == null || edge === 'anchor') {
  4431. var point = reverse ? Editor.before(editor, anchor, opts) : Editor.after(editor, anchor, opts);
  4432. if (point) {
  4433. props.anchor = point;
  4434. }
  4435. }
  4436. if (edge == null || edge === 'focus') {
  4437. var _point = reverse ? Editor.before(editor, focus, opts) : Editor.after(editor, focus, opts);
  4438. if (_point) {
  4439. props.focus = _point;
  4440. }
  4441. }
  4442. Transforms.setSelection(editor, props);
  4443. },
  4444. /**
  4445. * Set the selection to a new value.
  4446. */
  4447. select(editor, target) {
  4448. var {
  4449. selection
  4450. } = editor;
  4451. target = Editor.range(editor, target);
  4452. if (selection) {
  4453. Transforms.setSelection(editor, target);
  4454. return;
  4455. }
  4456. if (!Range.isRange(target)) {
  4457. 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)));
  4458. }
  4459. editor.apply({
  4460. type: 'set_selection',
  4461. properties: selection,
  4462. newProperties: target
  4463. });
  4464. },
  4465. /**
  4466. * Set new properties on one of the selection's points.
  4467. */
  4468. setPoint(editor, props) {
  4469. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4470. var {
  4471. selection
  4472. } = editor;
  4473. var {
  4474. edge = 'both'
  4475. } = options;
  4476. if (!selection) {
  4477. return;
  4478. }
  4479. if (edge === 'start') {
  4480. edge = Range.isBackward(selection) ? 'focus' : 'anchor';
  4481. }
  4482. if (edge === 'end') {
  4483. edge = Range.isBackward(selection) ? 'anchor' : 'focus';
  4484. }
  4485. var {
  4486. anchor,
  4487. focus
  4488. } = selection;
  4489. var point = edge === 'anchor' ? anchor : focus;
  4490. Transforms.setSelection(editor, {
  4491. [edge === 'anchor' ? 'anchor' : 'focus']: _objectSpread$1(_objectSpread$1({}, point), props)
  4492. });
  4493. },
  4494. /**
  4495. * Set new properties on the selection.
  4496. */
  4497. setSelection(editor, props) {
  4498. var {
  4499. selection
  4500. } = editor;
  4501. var oldProps = {};
  4502. var newProps = {};
  4503. if (!selection) {
  4504. return;
  4505. }
  4506. for (var k in props) {
  4507. 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]) {
  4508. oldProps[k] = selection[k];
  4509. newProps[k] = props[k];
  4510. }
  4511. }
  4512. if (Object.keys(oldProps).length > 0) {
  4513. editor.apply({
  4514. type: 'set_selection',
  4515. properties: oldProps,
  4516. newProperties: newProps
  4517. });
  4518. }
  4519. }
  4520. };
  4521. var TextTransforms = {
  4522. /**
  4523. * Delete content in the editor.
  4524. */
  4525. delete(editor) {
  4526. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  4527. Editor.withoutNormalizing(editor, () => {
  4528. var {
  4529. reverse = false,
  4530. unit = 'character',
  4531. distance = 1,
  4532. voids = false
  4533. } = options;
  4534. var {
  4535. at = editor.selection,
  4536. hanging = false
  4537. } = options;
  4538. if (!at) {
  4539. return;
  4540. }
  4541. if (Range.isRange(at) && Range.isCollapsed(at)) {
  4542. at = at.anchor;
  4543. }
  4544. if (Point.isPoint(at)) {
  4545. var furthestVoid = Editor.void(editor, {
  4546. at,
  4547. mode: 'highest'
  4548. });
  4549. if (!voids && furthestVoid) {
  4550. var [, voidPath] = furthestVoid;
  4551. at = voidPath;
  4552. } else {
  4553. var opts = {
  4554. unit,
  4555. distance
  4556. };
  4557. var target = reverse ? Editor.before(editor, at, opts) || Editor.start(editor, []) : Editor.after(editor, at, opts) || Editor.end(editor, []);
  4558. at = {
  4559. anchor: at,
  4560. focus: target
  4561. };
  4562. hanging = true;
  4563. }
  4564. }
  4565. if (Path.isPath(at)) {
  4566. Transforms.removeNodes(editor, {
  4567. at,
  4568. voids
  4569. });
  4570. return;
  4571. }
  4572. if (Range.isCollapsed(at)) {
  4573. return;
  4574. }
  4575. if (!hanging) {
  4576. var [, _end] = Range.edges(at);
  4577. var endOfDoc = Editor.end(editor, []);
  4578. if (!Point.equals(_end, endOfDoc)) {
  4579. at = Editor.unhangRange(editor, at, {
  4580. voids
  4581. });
  4582. }
  4583. }
  4584. var [start, end] = Range.edges(at);
  4585. var startBlock = Editor.above(editor, {
  4586. match: n => Editor.isBlock(editor, n),
  4587. at: start,
  4588. voids
  4589. });
  4590. var endBlock = Editor.above(editor, {
  4591. match: n => Editor.isBlock(editor, n),
  4592. at: end,
  4593. voids
  4594. });
  4595. var isAcrossBlocks = startBlock && endBlock && !Path.equals(startBlock[1], endBlock[1]);
  4596. var isSingleText = Path.equals(start.path, end.path);
  4597. var startVoid = voids ? null : Editor.void(editor, {
  4598. at: start,
  4599. mode: 'highest'
  4600. });
  4601. var endVoid = voids ? null : Editor.void(editor, {
  4602. at: end,
  4603. mode: 'highest'
  4604. }); // If the start or end points are inside an inline void, nudge them out.
  4605. if (startVoid) {
  4606. var before = Editor.before(editor, start);
  4607. if (before && startBlock && Path.isAncestor(startBlock[1], before.path)) {
  4608. start = before;
  4609. }
  4610. }
  4611. if (endVoid) {
  4612. var after = Editor.after(editor, end);
  4613. if (after && endBlock && Path.isAncestor(endBlock[1], after.path)) {
  4614. end = after;
  4615. }
  4616. } // Get the highest nodes that are completely inside the range, as well as
  4617. // the start and end nodes.
  4618. var matches = [];
  4619. var lastPath;
  4620. for (var entry of Editor.nodes(editor, {
  4621. at,
  4622. voids
  4623. })) {
  4624. var [node, path] = entry;
  4625. if (lastPath && Path.compare(path, lastPath) === 0) {
  4626. continue;
  4627. }
  4628. if (!voids && Editor.isVoid(editor, node) || !Path.isCommon(path, start.path) && !Path.isCommon(path, end.path)) {
  4629. matches.push(entry);
  4630. lastPath = path;
  4631. }
  4632. }
  4633. var pathRefs = Array.from(matches, _ref => {
  4634. var [, p] = _ref;
  4635. return Editor.pathRef(editor, p);
  4636. });
  4637. var startRef = Editor.pointRef(editor, start);
  4638. var endRef = Editor.pointRef(editor, end);
  4639. if (!isSingleText && !startVoid) {
  4640. var _point = startRef.current;
  4641. var [_node] = Editor.leaf(editor, _point);
  4642. var {
  4643. path: _path
  4644. } = _point;
  4645. var {
  4646. offset
  4647. } = start;
  4648. var text = _node.text.slice(offset);
  4649. if (text.length > 0) editor.apply({
  4650. type: 'remove_text',
  4651. path: _path,
  4652. offset,
  4653. text
  4654. });
  4655. }
  4656. for (var pathRef of pathRefs) {
  4657. var _path2 = pathRef.unref();
  4658. Transforms.removeNodes(editor, {
  4659. at: _path2,
  4660. voids
  4661. });
  4662. }
  4663. if (!endVoid) {
  4664. var _point2 = endRef.current;
  4665. var [_node2] = Editor.leaf(editor, _point2);
  4666. var {
  4667. path: _path3
  4668. } = _point2;
  4669. var _offset = isSingleText ? start.offset : 0;
  4670. var _text = _node2.text.slice(_offset, end.offset);
  4671. if (_text.length > 0) editor.apply({
  4672. type: 'remove_text',
  4673. path: _path3,
  4674. offset: _offset,
  4675. text: _text
  4676. });
  4677. }
  4678. if (!isSingleText && isAcrossBlocks && endRef.current && startRef.current) {
  4679. Transforms.mergeNodes(editor, {
  4680. at: endRef.current,
  4681. hanging: true,
  4682. voids
  4683. });
  4684. }
  4685. var point = reverse ? startRef.unref() || endRef.unref() : endRef.unref() || startRef.unref();
  4686. if (options.at == null && point) {
  4687. Transforms.select(editor, point);
  4688. }
  4689. });
  4690. },
  4691. /**
  4692. * Insert a fragment at a specific location in the editor.
  4693. */
  4694. insertFragment(editor, fragment) {
  4695. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4696. Editor.withoutNormalizing(editor, () => {
  4697. var {
  4698. hanging = false,
  4699. voids = false
  4700. } = options;
  4701. var {
  4702. at = editor.selection
  4703. } = options;
  4704. if (!fragment.length) {
  4705. return;
  4706. }
  4707. if (!at) {
  4708. return;
  4709. } else if (Range.isRange(at)) {
  4710. if (!hanging) {
  4711. at = Editor.unhangRange(editor, at);
  4712. }
  4713. if (Range.isCollapsed(at)) {
  4714. at = at.anchor;
  4715. } else {
  4716. var [, end] = Range.edges(at);
  4717. if (!voids && Editor.void(editor, {
  4718. at: end
  4719. })) {
  4720. return;
  4721. }
  4722. var pointRef = Editor.pointRef(editor, end);
  4723. Transforms.delete(editor, {
  4724. at
  4725. });
  4726. at = pointRef.unref();
  4727. }
  4728. } else if (Path.isPath(at)) {
  4729. at = Editor.start(editor, at);
  4730. }
  4731. if (!voids && Editor.void(editor, {
  4732. at
  4733. })) {
  4734. return;
  4735. } // If the insert point is at the edge of an inline node, move it outside
  4736. // instead since it will need to be split otherwise.
  4737. var inlineElementMatch = Editor.above(editor, {
  4738. at,
  4739. match: n => Editor.isInline(editor, n),
  4740. mode: 'highest',
  4741. voids
  4742. });
  4743. if (inlineElementMatch) {
  4744. var [, _inlinePath] = inlineElementMatch;
  4745. if (Editor.isEnd(editor, at, _inlinePath)) {
  4746. var after = Editor.after(editor, _inlinePath);
  4747. at = after;
  4748. } else if (Editor.isStart(editor, at, _inlinePath)) {
  4749. var before = Editor.before(editor, _inlinePath);
  4750. at = before;
  4751. }
  4752. }
  4753. var blockMatch = Editor.above(editor, {
  4754. match: n => Editor.isBlock(editor, n),
  4755. at,
  4756. voids
  4757. });
  4758. var [, blockPath] = blockMatch;
  4759. var isBlockStart = Editor.isStart(editor, at, blockPath);
  4760. var isBlockEnd = Editor.isEnd(editor, at, blockPath);
  4761. var isBlockEmpty = isBlockStart && isBlockEnd;
  4762. var mergeStart = !isBlockStart || isBlockStart && isBlockEnd;
  4763. var mergeEnd = !isBlockEnd;
  4764. var [, firstPath] = Node.first({
  4765. children: fragment
  4766. }, []);
  4767. var [, lastPath] = Node.last({
  4768. children: fragment
  4769. }, []);
  4770. var matches = [];
  4771. var matcher = _ref2 => {
  4772. var [n, p] = _ref2;
  4773. var isRoot = p.length === 0;
  4774. if (isRoot) {
  4775. return false;
  4776. }
  4777. if (isBlockEmpty) {
  4778. return true;
  4779. }
  4780. if (mergeStart && Path.isAncestor(p, firstPath) && Element.isElement(n) && !editor.isVoid(n) && !editor.isInline(n)) {
  4781. return false;
  4782. }
  4783. if (mergeEnd && Path.isAncestor(p, lastPath) && Element.isElement(n) && !editor.isVoid(n) && !editor.isInline(n)) {
  4784. return false;
  4785. }
  4786. return true;
  4787. };
  4788. for (var entry of Node.nodes({
  4789. children: fragment
  4790. }, {
  4791. pass: matcher
  4792. })) {
  4793. if (matcher(entry)) {
  4794. matches.push(entry);
  4795. }
  4796. }
  4797. var starts = [];
  4798. var middles = [];
  4799. var ends = [];
  4800. var starting = true;
  4801. var hasBlocks = false;
  4802. for (var [node] of matches) {
  4803. if (Element.isElement(node) && !editor.isInline(node)) {
  4804. starting = false;
  4805. hasBlocks = true;
  4806. middles.push(node);
  4807. } else if (starting) {
  4808. starts.push(node);
  4809. } else {
  4810. ends.push(node);
  4811. }
  4812. }
  4813. var [inlineMatch] = Editor.nodes(editor, {
  4814. at,
  4815. match: n => Text.isText(n) || Editor.isInline(editor, n),
  4816. mode: 'highest',
  4817. voids
  4818. });
  4819. var [, inlinePath] = inlineMatch;
  4820. var isInlineStart = Editor.isStart(editor, at, inlinePath);
  4821. var isInlineEnd = Editor.isEnd(editor, at, inlinePath);
  4822. var middleRef = Editor.pathRef(editor, isBlockEnd ? Path.next(blockPath) : blockPath);
  4823. var endRef = Editor.pathRef(editor, isInlineEnd ? Path.next(inlinePath) : inlinePath);
  4824. var blockPathRef = Editor.pathRef(editor, blockPath);
  4825. Transforms.splitNodes(editor, {
  4826. at,
  4827. match: n => hasBlocks ? Editor.isBlock(editor, n) : Text.isText(n) || Editor.isInline(editor, n),
  4828. mode: hasBlocks ? 'lowest' : 'highest',
  4829. voids
  4830. });
  4831. var startRef = Editor.pathRef(editor, !isInlineStart || isInlineStart && isInlineEnd ? Path.next(inlinePath) : inlinePath);
  4832. Transforms.insertNodes(editor, starts, {
  4833. at: startRef.current,
  4834. match: n => Text.isText(n) || Editor.isInline(editor, n),
  4835. mode: 'highest',
  4836. voids
  4837. });
  4838. if (isBlockEmpty && middles.length) {
  4839. Transforms.delete(editor, {
  4840. at: blockPathRef.unref(),
  4841. voids
  4842. });
  4843. }
  4844. Transforms.insertNodes(editor, middles, {
  4845. at: middleRef.current,
  4846. match: n => Editor.isBlock(editor, n),
  4847. mode: 'lowest',
  4848. voids
  4849. });
  4850. Transforms.insertNodes(editor, ends, {
  4851. at: endRef.current,
  4852. match: n => Text.isText(n) || Editor.isInline(editor, n),
  4853. mode: 'highest',
  4854. voids
  4855. });
  4856. if (!options.at) {
  4857. var path;
  4858. if (ends.length > 0) {
  4859. path = Path.previous(endRef.current);
  4860. } else if (middles.length > 0) {
  4861. path = Path.previous(middleRef.current);
  4862. } else {
  4863. path = Path.previous(startRef.current);
  4864. }
  4865. var _end2 = Editor.end(editor, path);
  4866. Transforms.select(editor, _end2);
  4867. }
  4868. startRef.unref();
  4869. middleRef.unref();
  4870. endRef.unref();
  4871. });
  4872. },
  4873. /**
  4874. * Insert a string of text in the Editor.
  4875. */
  4876. insertText(editor, text) {
  4877. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  4878. Editor.withoutNormalizing(editor, () => {
  4879. var {
  4880. voids = false
  4881. } = options;
  4882. var {
  4883. at = editor.selection
  4884. } = options;
  4885. if (!at) {
  4886. return;
  4887. }
  4888. if (Path.isPath(at)) {
  4889. at = Editor.range(editor, at);
  4890. }
  4891. if (Range.isRange(at)) {
  4892. if (Range.isCollapsed(at)) {
  4893. at = at.anchor;
  4894. } else {
  4895. var end = Range.end(at);
  4896. if (!voids && Editor.void(editor, {
  4897. at: end
  4898. })) {
  4899. return;
  4900. }
  4901. var start = Range.start(at);
  4902. var pointRef = Editor.pointRef(editor, start);
  4903. Transforms.delete(editor, {
  4904. at,
  4905. voids
  4906. });
  4907. at = pointRef.unref();
  4908. Transforms.setSelection(editor, {
  4909. anchor: at,
  4910. focus: at
  4911. });
  4912. }
  4913. }
  4914. if (!voids && Editor.void(editor, {
  4915. at
  4916. })) {
  4917. return;
  4918. }
  4919. var {
  4920. path,
  4921. offset
  4922. } = at;
  4923. if (text.length > 0) editor.apply({
  4924. type: 'insert_text',
  4925. path,
  4926. offset,
  4927. text
  4928. });
  4929. });
  4930. }
  4931. };
  4932. 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; }
  4933. 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; }
  4934. var Transforms = _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, GeneralTransforms), NodeTransforms), SelectionTransforms), TextTransforms);
  4935. export { Editor, Element, Location, Node, Operation, Path, PathRef, Point, PointRef, Range, RangeRef, Span, Text, Transforms, createEditor };
  4936. //# sourceMappingURL=index.es.js.map