history.test.tsx 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771
  1. import * as StaticScene from "../renderer/staticScene";
  2. import {
  3. GlobalTestState,
  4. act,
  5. assertSelectedElements,
  6. render,
  7. togglePopover,
  8. } from "./test-utils";
  9. import { Excalidraw } from "../index";
  10. import { Keyboard, Pointer, UI } from "./helpers/ui";
  11. import { API } from "./helpers/api";
  12. import { getDefaultAppState } from "../appState";
  13. import { fireEvent, queryByTestId, waitFor } from "@testing-library/react";
  14. import { createUndoAction, createRedoAction } from "../actions/actionHistory";
  15. import { actionToggleViewMode } from "../actions/actionToggleViewMode";
  16. import { EXPORT_DATA_TYPES, MIME_TYPES } from "../constants";
  17. import type { AppState, ExcalidrawImperativeAPI } from "../types";
  18. import { arrayToMap, resolvablePromise } from "../utils";
  19. import {
  20. COLOR_PALETTE,
  21. DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX,
  22. DEFAULT_ELEMENT_STROKE_COLOR_INDEX,
  23. } from "../colors";
  24. import { KEYS } from "../keys";
  25. import { newElementWith } from "../element/mutateElement";
  26. import type {
  27. ExcalidrawFrameElement,
  28. ExcalidrawGenericElement,
  29. ExcalidrawLinearElement,
  30. ExcalidrawTextElement,
  31. FractionalIndex,
  32. SceneElementsMap,
  33. } from "../element/types";
  34. import {
  35. actionSendBackward,
  36. actionBringForward,
  37. actionSendToBack,
  38. } from "../actions";
  39. import { vi } from "vitest";
  40. import { queryByText } from "@testing-library/react";
  41. import { HistoryEntry } from "../history";
  42. import { AppStateChange, ElementsChange } from "../change";
  43. import { Snapshot, StoreAction } from "../store";
  44. const { h } = window;
  45. const mouse = new Pointer("mouse");
  46. const checkpoint = (name: string) => {
  47. expect(renderStaticScene.mock.calls.length).toMatchSnapshot(
  48. `[${name}] number of renders`,
  49. );
  50. // `scrolledOutside` does not appear to be stable between test runs
  51. // `selectedLinearElemnt` includes `startBindingElement` containing seed and versionNonce
  52. const {
  53. name: _,
  54. scrolledOutside,
  55. selectedLinearElement,
  56. ...strippedAppState
  57. } = h.state;
  58. expect(strippedAppState).toMatchSnapshot(`[${name}] appState`);
  59. expect(h.history).toMatchSnapshot(`[${name}] history`);
  60. expect(h.elements.length).toMatchSnapshot(`[${name}] number of elements`);
  61. h.elements
  62. .map(({ seed, versionNonce, ...strippedElement }) => strippedElement)
  63. .forEach((element, i) =>
  64. expect(element).toMatchSnapshot(`[${name}] element ${i}`),
  65. );
  66. };
  67. const renderStaticScene = vi.spyOn(StaticScene, "renderStaticScene");
  68. const transparent = COLOR_PALETTE.transparent;
  69. const black = COLOR_PALETTE.black;
  70. const red = COLOR_PALETTE.red[DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX];
  71. const blue = COLOR_PALETTE.blue[DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX];
  72. const yellow = COLOR_PALETTE.yellow[DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX];
  73. const violet = COLOR_PALETTE.violet[DEFAULT_ELEMENT_BACKGROUND_COLOR_INDEX];
  74. describe("history", () => {
  75. beforeEach(() => {
  76. renderStaticScene.mockClear();
  77. });
  78. afterEach(() => {
  79. checkpoint("end of test");
  80. });
  81. describe("singleplayer undo/redo", () => {
  82. it("should not collapse when applying corrupted history entry", async () => {
  83. await render(<Excalidraw handleKeyboardGlobally={true} />);
  84. const rect = API.createElement({ type: "rectangle" });
  85. h.elements = [rect];
  86. const corrupedEntry = HistoryEntry.create(
  87. AppStateChange.empty(),
  88. ElementsChange.empty(),
  89. );
  90. vi.spyOn(corrupedEntry, "applyTo").mockImplementation(() => {
  91. throw new Error("Oh no, I am corrupted!");
  92. });
  93. (h.history as any).undoStack.push(corrupedEntry);
  94. const appState = getDefaultAppState() as AppState;
  95. try {
  96. // due to this we unfortunately we couldn't do simple .toThrow()
  97. act(
  98. () =>
  99. h.history.undo(
  100. arrayToMap(h.elements) as SceneElementsMap,
  101. appState,
  102. Snapshot.empty(),
  103. ) as any,
  104. );
  105. } catch (e) {
  106. expect(e).toBeInstanceOf(Error);
  107. }
  108. // we popped the entry, even though it is corrupted, so the user could perform subsequent undo/redo and would not be stuck on this entry forever
  109. expect(API.getUndoStack().length).toBe(0);
  110. // we pushed the entr, as we don't want just lose it and throw it away - it might be perfectly valid on subsequent redo
  111. expect(API.getRedoStack().length).toBe(1);
  112. expect(h.elements).toEqual([
  113. expect.objectContaining({ id: rect.id, isDeleted: false }), // no changes detected
  114. ]);
  115. try {
  116. // due to this we unfortunately we couldn't do simple .toThrow()
  117. act(
  118. () =>
  119. h.history.redo(
  120. arrayToMap(h.elements) as SceneElementsMap,
  121. appState,
  122. Snapshot.empty(),
  123. ) as any,
  124. );
  125. } catch (e) {
  126. expect(e).toBeInstanceOf(Error);
  127. }
  128. expect(API.getUndoStack().length).toBe(1); // vice versa for redo
  129. expect(API.getRedoStack().length).toBe(0); // vice versa for undo
  130. expect(h.elements).toEqual([
  131. expect.objectContaining({ id: rect.id, isDeleted: false }),
  132. ]);
  133. });
  134. it("should not end up with history entry when there are no appstate changes", async () => {
  135. await render(<Excalidraw handleKeyboardGlobally={true} />);
  136. const rect1 = API.createElement({ type: "rectangle", groupIds: ["A"] });
  137. const rect2 = API.createElement({ type: "rectangle", groupIds: ["A"] });
  138. h.elements = [rect1, rect2];
  139. mouse.select(rect1);
  140. assertSelectedElements([rect1, rect2]);
  141. expect(h.state.selectedGroupIds).toEqual({ A: true });
  142. expect(API.getUndoStack().length).toBe(1);
  143. expect(API.getRedoStack().length).toBe(0);
  144. mouse.select(rect2);
  145. assertSelectedElements([rect1, rect2]);
  146. expect(h.state.selectedGroupIds).toEqual({ A: true });
  147. expect(API.getUndoStack().length).toBe(1); // no new entry was created
  148. expect(API.getRedoStack().length).toBe(0);
  149. });
  150. it("should not end up with history entry when there are no elements changes", async () => {
  151. const excalidrawAPIPromise = resolvablePromise<ExcalidrawImperativeAPI>();
  152. await render(
  153. <Excalidraw
  154. excalidrawAPI={(api) => excalidrawAPIPromise.resolve(api as any)}
  155. handleKeyboardGlobally={true}
  156. />,
  157. );
  158. const excalidrawAPI = await excalidrawAPIPromise;
  159. const rect1 = API.createElement({ type: "rectangle" });
  160. const rect2 = API.createElement({ type: "rectangle" });
  161. excalidrawAPI.updateScene({
  162. elements: [rect1, rect2],
  163. storeAction: StoreAction.CAPTURE,
  164. });
  165. expect(API.getUndoStack().length).toBe(1);
  166. expect(API.getRedoStack().length).toBe(0);
  167. expect(h.elements).toEqual([
  168. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  169. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  170. ]);
  171. excalidrawAPI.updateScene({
  172. elements: [rect1, rect2],
  173. storeAction: StoreAction.CAPTURE, // even though the flag is on, same elements are passed, nothing to commit
  174. });
  175. expect(API.getUndoStack().length).toBe(1);
  176. expect(API.getRedoStack().length).toBe(0);
  177. expect(h.elements).toEqual([
  178. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  179. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  180. ]);
  181. });
  182. it("should not clear the redo stack on standalone appstate change", async () => {
  183. await render(<Excalidraw handleKeyboardGlobally={true} />);
  184. const rect1 = UI.createElement("rectangle", { x: 10 });
  185. const rect2 = UI.createElement("rectangle", { x: 20 });
  186. expect(API.getUndoStack().length).toBe(2);
  187. expect(API.getRedoStack().length).toBe(0);
  188. assertSelectedElements(rect2);
  189. expect(h.elements).toEqual([
  190. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  191. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  192. ]);
  193. Keyboard.undo();
  194. expect(API.getUndoStack().length).toBe(1);
  195. expect(API.getRedoStack().length).toBe(1);
  196. assertSelectedElements(rect1);
  197. expect(h.elements).toEqual([
  198. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  199. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  200. ]);
  201. mouse.clickAt(-10, -10);
  202. expect(API.getUndoStack().length).toBe(2);
  203. expect(API.getRedoStack().length).toBe(1); // we still have a possibility to redo!
  204. expect(API.getSelectedElements().length).toBe(0);
  205. expect(h.elements).toEqual([
  206. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  207. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  208. ]);
  209. mouse.downAt(0, 0);
  210. mouse.moveTo(50, 50);
  211. mouse.upAt(50, 50);
  212. expect(API.getUndoStack().length).toBe(3);
  213. expect(API.getRedoStack().length).toBe(1); // even after re-select!
  214. assertSelectedElements(rect1);
  215. expect(h.elements).toEqual([
  216. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  217. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  218. ]);
  219. Keyboard.redo();
  220. expect(API.getUndoStack().length).toBe(4);
  221. expect(API.getRedoStack().length).toBe(0);
  222. assertSelectedElements(rect2);
  223. expect(h.elements).toEqual([
  224. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  225. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  226. ]);
  227. });
  228. it("should not override appstate changes when redo stack is not cleared", async () => {
  229. await render(<Excalidraw handleKeyboardGlobally={true} />);
  230. const rect = UI.createElement("rectangle", { x: 10 });
  231. togglePopover("Background");
  232. UI.clickOnTestId("color-red");
  233. UI.clickOnTestId("color-blue");
  234. expect(API.getUndoStack().length).toBe(3);
  235. expect(API.getRedoStack().length).toBe(0);
  236. assertSelectedElements(rect);
  237. expect(h.elements).toEqual([
  238. expect.objectContaining({ id: rect.id, backgroundColor: blue }),
  239. ]);
  240. Keyboard.undo();
  241. expect(API.getUndoStack().length).toBe(2);
  242. expect(API.getRedoStack().length).toBe(1);
  243. assertSelectedElements(rect);
  244. expect(h.elements).toEqual([
  245. expect.objectContaining({ id: rect.id, backgroundColor: red }),
  246. ]);
  247. Keyboard.undo();
  248. expect(API.getUndoStack().length).toBe(1);
  249. expect(API.getRedoStack().length).toBe(2);
  250. assertSelectedElements(rect);
  251. expect(h.elements).toEqual([
  252. expect.objectContaining({ id: rect.id, backgroundColor: transparent }),
  253. ]);
  254. mouse.clickAt(-10, -10);
  255. expect(API.getUndoStack().length).toBe(2); // pushed appstate change,
  256. expect(API.getRedoStack().length).toBe(2); // redo stack is not cleared
  257. expect(API.getSelectedElements().length).toBe(0);
  258. expect(h.elements).toEqual([
  259. expect.objectContaining({ id: rect.id, backgroundColor: transparent }),
  260. ]);
  261. Keyboard.redo();
  262. expect(API.getUndoStack().length).toBe(3);
  263. expect(API.getRedoStack().length).toBe(1);
  264. expect(API.getSelectedElements().length).toBe(0); // previously the item was selected, not it is not
  265. expect(h.elements).toEqual([
  266. expect.objectContaining({ id: rect.id, backgroundColor: red }),
  267. ]);
  268. Keyboard.redo();
  269. expect(API.getUndoStack().length).toBe(4);
  270. expect(API.getRedoStack().length).toBe(0);
  271. expect(API.getSelectedElements().length).toBe(0); // previously the item was selected, not it is not
  272. expect(h.elements).toEqual([
  273. expect.objectContaining({ id: rect.id, backgroundColor: blue }),
  274. ]);
  275. Keyboard.undo();
  276. expect(API.getUndoStack().length).toBe(3);
  277. expect(API.getRedoStack().length).toBe(1);
  278. expect(API.getSelectedElements().length).toBe(0); // previously the item was selected, not it is not
  279. expect(h.elements).toEqual([
  280. expect.objectContaining({ id: rect.id, backgroundColor: red }),
  281. ]);
  282. Keyboard.undo();
  283. expect(API.getUndoStack().length).toBe(2);
  284. expect(API.getRedoStack().length).toBe(2);
  285. expect(API.getSelectedElements().length).toBe(0);
  286. expect(h.elements).toEqual([
  287. expect.objectContaining({ id: rect.id, backgroundColor: transparent }),
  288. ]);
  289. Keyboard.undo();
  290. expect(API.getUndoStack().length).toBe(1);
  291. expect(API.getRedoStack().length).toBe(3);
  292. assertSelectedElements(rect); // get's reselected with out pushed entry!
  293. expect(h.elements).toEqual([
  294. expect.objectContaining({ id: rect.id, backgroundColor: transparent }),
  295. ]);
  296. });
  297. it("should clear the redo stack on elements change", async () => {
  298. await render(<Excalidraw handleKeyboardGlobally={true} />);
  299. const rect1 = UI.createElement("rectangle", { x: 10 });
  300. expect(API.getUndoStack().length).toBe(1);
  301. expect(API.getRedoStack().length).toBe(0);
  302. assertSelectedElements(rect1);
  303. expect(h.elements).toEqual([
  304. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  305. ]);
  306. Keyboard.undo();
  307. expect(API.getUndoStack().length).toBe(0);
  308. expect(API.getRedoStack().length).toBe(1);
  309. expect(API.getSelectedElements()).toEqual([]);
  310. expect(h.elements).toEqual([
  311. expect.objectContaining({ id: rect1.id, isDeleted: true }),
  312. ]);
  313. const rect2 = UI.createElement("rectangle", { x: 20 });
  314. assertSelectedElements(rect2);
  315. expect(API.getUndoStack().length).toBe(1);
  316. expect(API.getRedoStack().length).toBe(0); // redo stack got cleared
  317. expect(API.getSnapshot()).toEqual([
  318. expect.objectContaining({ id: rect1.id, isDeleted: true }),
  319. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  320. ]);
  321. expect(h.elements).toEqual([
  322. expect.objectContaining({ id: rect1.id, isDeleted: true }),
  323. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  324. ]);
  325. });
  326. it("should iterate through the history when selection changes do not produce visible change", async () => {
  327. await render(<Excalidraw handleKeyboardGlobally={true} />);
  328. const rect = UI.createElement("rectangle", { x: 10 });
  329. mouse.clickAt(-10, -10);
  330. expect(API.getUndoStack().length).toBe(2);
  331. expect(API.getRedoStack().length).toBe(0);
  332. expect(API.getSelectedElements().length).toBe(0);
  333. Keyboard.undo();
  334. expect(API.getUndoStack().length).toBe(1);
  335. expect(API.getRedoStack().length).toBe(1);
  336. assertSelectedElements(rect);
  337. mouse.clickAt(-10, -10);
  338. expect(API.getUndoStack().length).toBe(2);
  339. expect(API.getRedoStack().length).toBe(1);
  340. expect(API.getSelectedElements().length).toBe(0);
  341. Keyboard.undo();
  342. expect(API.getUndoStack().length).toBe(1);
  343. expect(API.getRedoStack().length).toBe(2); // now we have two same redos
  344. assertSelectedElements(rect);
  345. Keyboard.redo();
  346. expect(API.getUndoStack().length).toBe(2);
  347. expect(API.getRedoStack().length).toBe(1); // didn't iterate through completely, as first redo already results in a visible change
  348. expect(API.getSelectedElements().length).toBe(0);
  349. Keyboard.redo(); // acceptable empty redo
  350. expect(API.getUndoStack().length).toBe(3);
  351. expect(API.getRedoStack().length).toBe(0);
  352. expect(API.getSelectedElements().length).toBe(0);
  353. Keyboard.undo();
  354. expect(API.getUndoStack().length).toBe(2);
  355. expect(API.getRedoStack().length).toBe(1);
  356. assertSelectedElements(rect);
  357. Keyboard.undo();
  358. expect(API.getUndoStack().length).toBe(0); // now we iterated through the same undos!
  359. expect(API.getRedoStack().length).toBe(3);
  360. expect(API.getSelectedElements().length).toBe(0);
  361. expect(h.elements).toEqual([
  362. expect.objectContaining({ id: rect.id, isDeleted: true }),
  363. ]);
  364. });
  365. it("should end up with no history entry after initializing scene", async () => {
  366. await render(
  367. <Excalidraw
  368. initialData={{
  369. elements: [API.createElement({ type: "rectangle", id: "A" })],
  370. appState: {
  371. zenModeEnabled: true,
  372. },
  373. }}
  374. />,
  375. );
  376. await waitFor(() => {
  377. expect(h.state.zenModeEnabled).toBe(true);
  378. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  379. expect(h.history.isUndoStackEmpty).toBeTruthy();
  380. });
  381. const undoAction = createUndoAction(h.history, h.store);
  382. const redoAction = createRedoAction(h.history, h.store);
  383. // noop
  384. act(() => h.app.actionManager.executeAction(undoAction));
  385. expect(h.elements).toEqual([
  386. expect.objectContaining({ id: "A", isDeleted: false }),
  387. ]);
  388. const rectangle = UI.createElement("rectangle");
  389. expect(h.elements).toEqual([
  390. expect.objectContaining({ id: "A" }),
  391. expect.objectContaining({ id: rectangle.id }),
  392. ]);
  393. act(() => h.app.actionManager.executeAction(undoAction));
  394. expect(h.elements).toEqual([
  395. expect.objectContaining({ id: "A", isDeleted: false }),
  396. expect.objectContaining({ id: rectangle.id, isDeleted: true }),
  397. ]);
  398. // noop
  399. act(() => h.app.actionManager.executeAction(undoAction));
  400. expect(h.elements).toEqual([
  401. expect.objectContaining({ id: "A", isDeleted: false }),
  402. expect.objectContaining({ id: rectangle.id, isDeleted: true }),
  403. ]);
  404. expect(API.getUndoStack().length).toBe(0);
  405. act(() => h.app.actionManager.executeAction(redoAction));
  406. expect(h.elements).toEqual([
  407. expect.objectContaining({ id: "A", isDeleted: false }),
  408. expect.objectContaining({ id: rectangle.id, isDeleted: false }),
  409. ]);
  410. expect(API.getUndoStack().length).toBe(1);
  411. });
  412. it("should create new history entry on scene import via drag&drop", async () => {
  413. await render(
  414. <Excalidraw
  415. initialData={{
  416. elements: [API.createElement({ type: "rectangle", id: "A" })],
  417. appState: {
  418. viewBackgroundColor: "#FFF",
  419. },
  420. }}
  421. />,
  422. );
  423. await waitFor(() => expect(h.state.viewBackgroundColor).toBe("#FFF"));
  424. await waitFor(() =>
  425. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]),
  426. );
  427. API.drop(
  428. new Blob(
  429. [
  430. JSON.stringify({
  431. type: EXPORT_DATA_TYPES.excalidraw,
  432. appState: {
  433. ...getDefaultAppState(),
  434. viewBackgroundColor: "#000",
  435. },
  436. elements: [API.createElement({ type: "rectangle", id: "B" })],
  437. }),
  438. ],
  439. { type: MIME_TYPES.json },
  440. ),
  441. );
  442. await waitFor(() => expect(API.getUndoStack().length).toBe(1));
  443. expect(h.state.viewBackgroundColor).toBe("#000");
  444. expect(API.getSnapshot()).toEqual([
  445. expect.objectContaining({ id: "A", isDeleted: true }),
  446. expect.objectContaining({ id: "B", isDeleted: false }),
  447. ]);
  448. expect(h.elements).toEqual([
  449. expect.objectContaining({ id: "B", isDeleted: false }),
  450. ]);
  451. const undoAction = createUndoAction(h.history, h.store);
  452. const redoAction = createRedoAction(h.history, h.store);
  453. act(() => h.app.actionManager.executeAction(undoAction));
  454. expect(API.getSnapshot()).toEqual([
  455. expect.objectContaining({ id: "A", isDeleted: false }),
  456. expect.objectContaining({ id: "B", isDeleted: true }),
  457. ]);
  458. expect(h.elements).toEqual([
  459. expect.objectContaining({ id: "A", isDeleted: false }),
  460. expect.objectContaining({ id: "B", isDeleted: true }),
  461. ]);
  462. expect(h.state.viewBackgroundColor).toBe("#FFF");
  463. act(() => h.app.actionManager.executeAction(redoAction));
  464. expect(h.state.viewBackgroundColor).toBe("#000");
  465. expect(API.getSnapshot()).toEqual([
  466. expect.objectContaining({ id: "A", isDeleted: true }),
  467. expect.objectContaining({ id: "B", isDeleted: false }),
  468. ]);
  469. expect(h.elements).toEqual([
  470. expect.objectContaining({ id: "A", isDeleted: true }),
  471. expect.objectContaining({ id: "B", isDeleted: false }),
  472. ]);
  473. });
  474. it("should support appstate name or viewBackgroundColor change", async () => {
  475. const excalidrawAPIPromise = resolvablePromise<ExcalidrawImperativeAPI>();
  476. await render(
  477. <Excalidraw
  478. excalidrawAPI={(api) => excalidrawAPIPromise.resolve(api as any)}
  479. handleKeyboardGlobally={true}
  480. initialData={{
  481. appState: {
  482. name: "Old name",
  483. viewBackgroundColor: "#FFF",
  484. },
  485. }}
  486. />,
  487. );
  488. const excalidrawAPI = await excalidrawAPIPromise;
  489. excalidrawAPI.updateScene({
  490. appState: {
  491. name: "New name",
  492. },
  493. storeAction: StoreAction.CAPTURE,
  494. });
  495. expect(API.getUndoStack().length).toBe(1);
  496. expect(API.getRedoStack().length).toBe(0);
  497. expect(h.state.name).toBe("New name");
  498. excalidrawAPI.updateScene({
  499. appState: {
  500. viewBackgroundColor: "#000",
  501. },
  502. storeAction: StoreAction.CAPTURE,
  503. });
  504. expect(API.getUndoStack().length).toBe(2);
  505. expect(API.getRedoStack().length).toBe(0);
  506. expect(h.state.name).toBe("New name");
  507. expect(h.state.viewBackgroundColor).toBe("#000");
  508. // just to double check that same change is not recorded
  509. excalidrawAPI.updateScene({
  510. appState: {
  511. name: "New name",
  512. viewBackgroundColor: "#000",
  513. },
  514. storeAction: StoreAction.CAPTURE,
  515. });
  516. expect(API.getUndoStack().length).toBe(2);
  517. expect(API.getRedoStack().length).toBe(0);
  518. expect(h.state.name).toBe("New name");
  519. expect(h.state.viewBackgroundColor).toBe("#000");
  520. Keyboard.undo();
  521. expect(API.getUndoStack().length).toBe(1);
  522. expect(API.getRedoStack().length).toBe(1);
  523. expect(h.state.name).toBe("New name");
  524. expect(h.state.viewBackgroundColor).toBe("#FFF");
  525. Keyboard.undo();
  526. expect(API.getUndoStack().length).toBe(0);
  527. expect(API.getRedoStack().length).toBe(2);
  528. expect(h.state.name).toBe("Old name");
  529. expect(h.state.viewBackgroundColor).toBe("#FFF");
  530. Keyboard.redo();
  531. expect(API.getUndoStack().length).toBe(1);
  532. expect(API.getRedoStack().length).toBe(1);
  533. expect(h.state.name).toBe("New name");
  534. expect(h.state.viewBackgroundColor).toBe("#FFF");
  535. Keyboard.redo();
  536. expect(API.getUndoStack().length).toBe(2);
  537. expect(API.getRedoStack().length).toBe(0);
  538. expect(h.state.name).toBe("New name");
  539. expect(h.state.viewBackgroundColor).toBe("#000");
  540. });
  541. it("should support element creation, deletion and appstate element selection change", async () => {
  542. await render(<Excalidraw handleKeyboardGlobally={true} />);
  543. const rect1 = UI.createElement("rectangle", { x: 10 });
  544. const rect2 = UI.createElement("rectangle", { x: 20, y: 20 });
  545. const rect3 = UI.createElement("rectangle", { x: 40, y: 40 });
  546. mouse.select([rect2, rect3]);
  547. Keyboard.keyDown(KEYS.DELETE);
  548. expect(API.getUndoStack().length).toBe(6);
  549. Keyboard.undo();
  550. assertSelectedElements(rect2, rect3);
  551. expect(h.elements).toEqual([
  552. expect.objectContaining({ id: rect1.id }),
  553. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  554. expect.objectContaining({ id: rect3.id, isDeleted: false }),
  555. ]);
  556. Keyboard.undo();
  557. assertSelectedElements(rect2);
  558. Keyboard.undo();
  559. assertSelectedElements(rect3);
  560. Keyboard.undo();
  561. assertSelectedElements(rect2);
  562. expect(h.elements).toEqual([
  563. expect.objectContaining({ id: rect1.id }),
  564. expect.objectContaining({ id: rect2.id }),
  565. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  566. ]);
  567. Keyboard.undo();
  568. assertSelectedElements(rect1);
  569. expect(h.elements).toEqual([
  570. expect.objectContaining({ id: rect1.id }),
  571. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  572. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  573. ]);
  574. Keyboard.undo();
  575. assertSelectedElements();
  576. expect(h.elements).toEqual([
  577. expect.objectContaining({ id: rect1.id, isDeleted: true }),
  578. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  579. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  580. ]);
  581. // no-op
  582. Keyboard.undo();
  583. assertSelectedElements();
  584. expect(h.elements).toEqual([
  585. expect.objectContaining({ id: rect1.id, isDeleted: true }),
  586. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  587. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  588. ]);
  589. Keyboard.redo();
  590. assertSelectedElements(rect1);
  591. expect(h.elements).toEqual([
  592. expect.objectContaining({ id: rect1.id }),
  593. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  594. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  595. ]);
  596. Keyboard.redo();
  597. assertSelectedElements(rect2);
  598. expect(h.elements).toEqual([
  599. expect.objectContaining({ id: rect1.id }),
  600. expect.objectContaining({ id: rect2.id }),
  601. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  602. ]);
  603. Keyboard.redo();
  604. assertSelectedElements(rect3);
  605. Keyboard.redo();
  606. assertSelectedElements(rect2);
  607. Keyboard.redo();
  608. assertSelectedElements(rect2, rect3);
  609. expect(h.elements).toEqual([
  610. expect.objectContaining({ id: rect1.id }),
  611. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  612. expect.objectContaining({ id: rect3.id, isDeleted: false }),
  613. ]);
  614. Keyboard.redo();
  615. expect(API.getUndoStack().length).toBe(6);
  616. expect(API.getRedoStack().length).toBe(0);
  617. assertSelectedElements();
  618. expect(h.elements).toEqual([
  619. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  620. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  621. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  622. ]);
  623. // no-op
  624. Keyboard.redo();
  625. expect(API.getUndoStack().length).toBe(6);
  626. expect(API.getRedoStack().length).toBe(0);
  627. assertSelectedElements();
  628. expect(h.elements).toEqual([
  629. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  630. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  631. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  632. ]);
  633. });
  634. it("should support linear element creation and points manipulation through the editor", async () => {
  635. await render(<Excalidraw handleKeyboardGlobally={true} />);
  636. // create three point arrow
  637. UI.clickTool("arrow");
  638. mouse.click(0, 0);
  639. mouse.click(10, 10);
  640. mouse.click(10, -10);
  641. // actionFinalize
  642. Keyboard.keyPress(KEYS.ENTER);
  643. // open editor
  644. Keyboard.withModifierKeys({ ctrl: true }, () => {
  645. Keyboard.keyPress(KEYS.ENTER);
  646. });
  647. // move point
  648. mouse.downAt(20, 0);
  649. mouse.moveTo(20, 20);
  650. mouse.up();
  651. // leave editor
  652. Keyboard.keyPress(KEYS.ESCAPE);
  653. expect(API.getUndoStack().length).toBe(6);
  654. expect(API.getRedoStack().length).toBe(0);
  655. expect(assertSelectedElements(h.elements[0]));
  656. expect(h.state.editingLinearElement).toBeNull();
  657. expect(h.state.selectedLinearElement).toBeNull();
  658. expect(h.elements).toEqual([
  659. expect.objectContaining({
  660. isDeleted: false,
  661. points: [
  662. [0, 0],
  663. [10, 10],
  664. [20, 20],
  665. ],
  666. }),
  667. ]);
  668. Keyboard.undo();
  669. expect(API.getUndoStack().length).toBe(5);
  670. expect(API.getRedoStack().length).toBe(1);
  671. expect(assertSelectedElements(h.elements[0]));
  672. expect(h.state.editingLinearElement?.elementId).toBe(h.elements[0].id);
  673. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  674. expect(h.elements).toEqual([
  675. expect.objectContaining({
  676. isDeleted: false,
  677. points: [
  678. [0, 0],
  679. [10, 10],
  680. [20, 20],
  681. ],
  682. }),
  683. ]);
  684. // making sure clicking on points in the editor does not generate new history entries!
  685. mouse.clickAt(0, 0);
  686. mouse.clickAt(10, 10);
  687. mouse.clickAt(20, 20);
  688. expect(API.getUndoStack().length).toBe(5);
  689. expect(API.getRedoStack().length).toBe(1);
  690. Keyboard.undo();
  691. expect(API.getUndoStack().length).toBe(4);
  692. expect(API.getRedoStack().length).toBe(2);
  693. expect(assertSelectedElements(h.elements[0]));
  694. expect(h.state.editingLinearElement?.elementId).toBe(h.elements[0].id);
  695. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  696. expect(h.elements).toEqual([
  697. expect.objectContaining({
  698. isDeleted: false,
  699. points: [
  700. [0, 0],
  701. [10, 10],
  702. [20, 0],
  703. ],
  704. }),
  705. ]);
  706. Keyboard.undo();
  707. expect(API.getUndoStack().length).toBe(3);
  708. expect(API.getRedoStack().length).toBe(3);
  709. expect(assertSelectedElements(h.elements[0]));
  710. expect(h.state.editingLinearElement).toBeNull(); // undo `open editor`
  711. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  712. expect(h.elements).toEqual([
  713. expect.objectContaining({
  714. isDeleted: false,
  715. points: [
  716. [0, 0],
  717. [10, 10],
  718. [20, 0],
  719. ],
  720. }),
  721. ]);
  722. Keyboard.undo();
  723. expect(API.getUndoStack().length).toBe(2);
  724. expect(API.getRedoStack().length).toBe(4);
  725. expect(assertSelectedElements(h.elements[0]));
  726. expect(h.state.editingLinearElement).toBeNull();
  727. expect(h.state.selectedLinearElement).toBeNull(); // undo `actionFinalize`
  728. expect(h.elements).toEqual([
  729. expect.objectContaining({
  730. isDeleted: false,
  731. points: [
  732. [0, 0],
  733. [10, 10],
  734. [20, 0],
  735. ],
  736. }),
  737. ]);
  738. Keyboard.undo();
  739. expect(API.getUndoStack().length).toBe(1);
  740. expect(API.getRedoStack().length).toBe(5);
  741. expect(assertSelectedElements(h.elements[0]));
  742. expect(h.state.editingLinearElement).toBeNull();
  743. expect(h.state.selectedLinearElement).toBeNull();
  744. expect(h.elements).toEqual([
  745. expect.objectContaining({
  746. isDeleted: false,
  747. points: [
  748. [0, 0],
  749. [10, 10],
  750. ],
  751. }),
  752. ]);
  753. Keyboard.undo();
  754. expect(API.getUndoStack().length).toBe(0);
  755. expect(API.getRedoStack().length).toBe(6);
  756. expect(API.getSelectedElements().length).toBe(0);
  757. expect(h.state.editingLinearElement).toBeNull();
  758. expect(h.state.selectedLinearElement).toBeNull();
  759. expect(h.elements).toEqual([
  760. expect.objectContaining({
  761. isDeleted: true,
  762. points: [
  763. [0, 0],
  764. [10, 10],
  765. ],
  766. }),
  767. ]);
  768. Keyboard.redo();
  769. expect(API.getUndoStack().length).toBe(1);
  770. expect(API.getRedoStack().length).toBe(5);
  771. expect(assertSelectedElements(h.elements[0]));
  772. expect(h.state.editingLinearElement).toBeNull();
  773. expect(h.state.selectedLinearElement).toBeNull();
  774. expect(h.elements).toEqual([
  775. expect.objectContaining({
  776. isDeleted: false,
  777. points: [
  778. [0, 0],
  779. [10, 10],
  780. ],
  781. }),
  782. ]);
  783. Keyboard.redo();
  784. expect(API.getUndoStack().length).toBe(2);
  785. expect(API.getRedoStack().length).toBe(4);
  786. expect(assertSelectedElements(h.elements[0]));
  787. expect(h.state.editingLinearElement).toBeNull();
  788. expect(h.state.selectedLinearElement).toBeNull(); // undo `actionFinalize`
  789. expect(h.elements).toEqual([
  790. expect.objectContaining({
  791. isDeleted: false,
  792. points: [
  793. [0, 0],
  794. [10, 10],
  795. [20, 0],
  796. ],
  797. }),
  798. ]);
  799. Keyboard.redo();
  800. expect(API.getUndoStack().length).toBe(3);
  801. expect(API.getRedoStack().length).toBe(3);
  802. expect(assertSelectedElements(h.elements[0]));
  803. expect(h.state.editingLinearElement).toBeNull(); // undo `open editor`
  804. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  805. expect(h.elements).toEqual([
  806. expect.objectContaining({
  807. isDeleted: false,
  808. points: [
  809. [0, 0],
  810. [10, 10],
  811. [20, 0],
  812. ],
  813. }),
  814. ]);
  815. Keyboard.redo();
  816. expect(API.getUndoStack().length).toBe(4);
  817. expect(API.getRedoStack().length).toBe(2);
  818. expect(assertSelectedElements(h.elements[0]));
  819. expect(h.state.editingLinearElement?.elementId).toBe(h.elements[0].id);
  820. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  821. expect(h.elements).toEqual([
  822. expect.objectContaining({
  823. isDeleted: false,
  824. points: [
  825. [0, 0],
  826. [10, 10],
  827. [20, 0],
  828. ],
  829. }),
  830. ]);
  831. Keyboard.redo();
  832. expect(API.getUndoStack().length).toBe(5);
  833. expect(API.getRedoStack().length).toBe(1);
  834. expect(assertSelectedElements(h.elements[0]));
  835. expect(h.state.editingLinearElement?.elementId).toBe(h.elements[0].id);
  836. expect(h.state.selectedLinearElement?.elementId).toBe(h.elements[0].id);
  837. expect(h.elements).toEqual([
  838. expect.objectContaining({
  839. isDeleted: false,
  840. points: [
  841. [0, 0],
  842. [10, 10],
  843. [20, 20],
  844. ],
  845. }),
  846. ]);
  847. Keyboard.redo();
  848. expect(API.getUndoStack().length).toBe(6);
  849. expect(API.getRedoStack().length).toBe(0);
  850. expect(assertSelectedElements(h.elements[0]));
  851. expect(h.state.editingLinearElement).toBeNull();
  852. expect(h.state.selectedLinearElement).toBeNull();
  853. expect(h.elements).toEqual([
  854. expect.objectContaining({
  855. isDeleted: false,
  856. points: [
  857. [0, 0],
  858. [10, 10],
  859. [20, 20],
  860. ],
  861. }),
  862. ]);
  863. });
  864. it("should create entry when selecting freedraw", async () => {
  865. await render(<Excalidraw handleKeyboardGlobally={true} />);
  866. UI.clickTool("rectangle");
  867. mouse.down(-10, -10);
  868. mouse.up(10, 10);
  869. UI.clickTool("freedraw");
  870. mouse.down(40, -20);
  871. mouse.up(50, 10);
  872. const rectangle = h.elements[0];
  873. const freedraw1 = h.elements[1];
  874. expect(API.getUndoStack().length).toBe(3);
  875. expect(API.getRedoStack().length).toBe(0);
  876. expect(API.getSelectedElements().length).toBe(0);
  877. expect(h.elements).toEqual([
  878. expect.objectContaining({ id: rectangle.id }),
  879. expect.objectContaining({ id: freedraw1.id, strokeColor: black }),
  880. ]);
  881. Keyboard.undo();
  882. expect(API.getUndoStack().length).toBe(2);
  883. expect(API.getRedoStack().length).toBe(1);
  884. expect(API.getSelectedElements().length).toBe(0);
  885. expect(h.elements).toEqual([
  886. expect.objectContaining({ id: rectangle.id }),
  887. expect.objectContaining({
  888. id: freedraw1.id,
  889. strokeColor: black,
  890. isDeleted: true,
  891. }),
  892. ]);
  893. togglePopover("Stroke");
  894. UI.clickOnTestId("color-red");
  895. mouse.down(40, -20);
  896. mouse.up(50, 10);
  897. const freedraw2 = h.elements[2];
  898. expect(API.getUndoStack().length).toBe(3);
  899. expect(API.getRedoStack().length).toBe(0);
  900. expect(h.elements).toEqual([
  901. expect.objectContaining({ id: rectangle.id }),
  902. expect.objectContaining({
  903. id: freedraw1.id,
  904. strokeColor: black,
  905. isDeleted: true,
  906. }),
  907. expect.objectContaining({
  908. id: freedraw2.id,
  909. strokeColor: COLOR_PALETTE.red[DEFAULT_ELEMENT_STROKE_COLOR_INDEX],
  910. }),
  911. ]);
  912. // ensure we don't end up with duplicated entries
  913. UI.clickTool("freedraw");
  914. expect(API.getUndoStack().length).toBe(3);
  915. expect(API.getRedoStack().length).toBe(0);
  916. });
  917. it("should support duplication of groups, appstate group selection and editing group", async () => {
  918. await render(<Excalidraw handleKeyboardGlobally={true} />);
  919. const rect1 = API.createElement({
  920. type: "rectangle",
  921. groupIds: ["A"],
  922. x: 0,
  923. });
  924. const rect2 = API.createElement({
  925. type: "rectangle",
  926. groupIds: ["A"],
  927. x: 100,
  928. });
  929. h.elements = [rect1, rect2];
  930. mouse.select(rect1);
  931. assertSelectedElements([rect1, rect2]);
  932. expect(API.getUndoStack().length).toBe(1);
  933. expect(API.getRedoStack().length).toBe(0);
  934. expect(h.state.editingGroupId).toBeNull();
  935. expect(h.state.selectedGroupIds).toEqual({ A: true });
  936. // inside the editing group
  937. mouse.doubleClickOn(rect2);
  938. assertSelectedElements([rect2]);
  939. expect(API.getUndoStack().length).toBe(2);
  940. expect(API.getRedoStack().length).toBe(0);
  941. expect(h.state.editingGroupId).toBe("A");
  942. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  943. mouse.clickOn(rect1);
  944. assertSelectedElements([rect1]);
  945. expect(API.getUndoStack().length).toBe(3);
  946. expect(API.getRedoStack().length).toBe(0);
  947. expect(h.state.editingGroupId).toBe("A");
  948. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  949. Keyboard.undo();
  950. assertSelectedElements([rect2]);
  951. expect(API.getUndoStack().length).toBe(2);
  952. expect(API.getRedoStack().length).toBe(1);
  953. expect(h.state.editingGroupId).toBe("A");
  954. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  955. Keyboard.undo();
  956. assertSelectedElements([rect1, rect2]);
  957. expect(API.getUndoStack().length).toBe(1);
  958. expect(API.getRedoStack().length).toBe(2);
  959. expect(h.state.editingGroupId).toBeNull();
  960. expect(h.state.selectedGroupIds).toEqual({ A: true });
  961. Keyboard.redo();
  962. assertSelectedElements([rect2]);
  963. expect(API.getUndoStack().length).toBe(2);
  964. expect(API.getRedoStack().length).toBe(1);
  965. expect(h.state.editingGroupId).toBe("A");
  966. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  967. Keyboard.redo();
  968. assertSelectedElements([rect1]);
  969. expect(API.getUndoStack().length).toBe(3);
  970. expect(API.getRedoStack().length).toBe(0);
  971. expect(h.state.editingGroupId).toBe("A");
  972. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  973. Keyboard.undo();
  974. assertSelectedElements([rect2]);
  975. expect(API.getUndoStack().length).toBe(2);
  976. expect(API.getRedoStack().length).toBe(1);
  977. expect(h.state.editingGroupId).toBe("A");
  978. expect(h.state.selectedGroupIds).not.toEqual({ A: true });
  979. Keyboard.undo();
  980. assertSelectedElements([rect1, rect2]);
  981. expect(API.getUndoStack().length).toBe(1);
  982. expect(API.getRedoStack().length).toBe(2);
  983. expect(h.state.editingGroupId).toBeNull();
  984. expect(h.state.selectedGroupIds).toEqual({ A: true });
  985. // outside the editing group, testing duplication
  986. Keyboard.withModifierKeys({ ctrl: true }, () => {
  987. Keyboard.keyPress("d");
  988. });
  989. assertSelectedElements([h.elements[2], h.elements[3]]);
  990. expect(API.getUndoStack().length).toBe(2);
  991. expect(API.getRedoStack().length).toBe(0);
  992. expect(h.elements.length).toBe(4);
  993. expect(h.state.editingGroupId).toBeNull();
  994. expect(h.state.selectedGroupIds).not.toEqual(
  995. expect.objectContaining({ A: true }),
  996. );
  997. Keyboard.undo();
  998. expect(API.getUndoStack().length).toBe(1);
  999. expect(API.getRedoStack().length).toBe(1);
  1000. expect(h.elements.length).toBe(4);
  1001. expect(h.elements).toEqual([
  1002. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  1003. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  1004. expect.objectContaining({ id: `${rect1.id}_copy`, isDeleted: true }),
  1005. expect.objectContaining({ id: `${rect2.id}_copy`, isDeleted: true }),
  1006. ]);
  1007. expect(h.state.editingGroupId).toBeNull();
  1008. expect(h.state.selectedGroupIds).toEqual({ A: true });
  1009. Keyboard.redo();
  1010. expect(API.getUndoStack().length).toBe(2);
  1011. expect(API.getRedoStack().length).toBe(0);
  1012. expect(h.elements.length).toBe(4);
  1013. expect(h.elements).toEqual([
  1014. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  1015. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  1016. expect.objectContaining({ id: `${rect1.id}_copy`, isDeleted: false }),
  1017. expect.objectContaining({ id: `${rect2.id}_copy`, isDeleted: false }),
  1018. ]);
  1019. expect(h.state.editingGroupId).toBeNull();
  1020. expect(h.state.selectedGroupIds).not.toEqual(
  1021. expect.objectContaining({ A: true }),
  1022. );
  1023. // undo again, and duplicate once more
  1024. Keyboard.withModifierKeys({ ctrl: true }, () => {
  1025. Keyboard.keyPress("z");
  1026. Keyboard.keyPress("d");
  1027. });
  1028. expect(API.getUndoStack().length).toBe(2);
  1029. expect(API.getRedoStack().length).toBe(0);
  1030. expect(h.elements.length).toBe(6);
  1031. expect(h.elements).toEqual(
  1032. expect.arrayContaining([
  1033. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  1034. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  1035. expect.objectContaining({ id: `${rect1.id}_copy`, isDeleted: true }),
  1036. expect.objectContaining({ id: `${rect2.id}_copy`, isDeleted: true }),
  1037. expect.objectContaining({
  1038. id: `${rect1.id}_copy_copy`,
  1039. isDeleted: false,
  1040. }),
  1041. expect.objectContaining({
  1042. id: `${rect2.id}_copy_copy`,
  1043. isDeleted: false,
  1044. }),
  1045. ]),
  1046. );
  1047. expect(h.state.editingGroupId).toBeNull();
  1048. expect(h.state.selectedGroupIds).not.toEqual(
  1049. expect.objectContaining({ A: true }),
  1050. );
  1051. });
  1052. it("should support changes in elements' order", async () => {
  1053. await render(<Excalidraw handleKeyboardGlobally={true} />);
  1054. const rect1 = UI.createElement("rectangle", { x: 10 });
  1055. const rect2 = UI.createElement("rectangle", { x: 20, y: 20 });
  1056. const rect3 = UI.createElement("rectangle", { x: 40, y: 40 });
  1057. act(() => h.app.actionManager.executeAction(actionSendBackward));
  1058. expect(API.getUndoStack().length).toBe(4);
  1059. expect(API.getRedoStack().length).toBe(0);
  1060. assertSelectedElements(rect3);
  1061. Keyboard.undo();
  1062. expect(API.getUndoStack().length).toBe(3);
  1063. expect(API.getRedoStack().length).toBe(1);
  1064. assertSelectedElements(rect3);
  1065. expect(h.elements).toEqual([
  1066. expect.objectContaining({ id: rect1.id }),
  1067. expect.objectContaining({ id: rect2.id }),
  1068. expect.objectContaining({ id: rect3.id }),
  1069. ]);
  1070. Keyboard.redo();
  1071. expect(API.getUndoStack().length).toBe(4);
  1072. expect(API.getRedoStack().length).toBe(0);
  1073. assertSelectedElements(rect3);
  1074. expect(h.elements).toEqual([
  1075. expect.objectContaining({ id: rect1.id }),
  1076. expect.objectContaining({ id: rect3.id }),
  1077. expect.objectContaining({ id: rect2.id }),
  1078. ]);
  1079. mouse.select([rect1, rect3]);
  1080. expect(API.getUndoStack().length).toBe(6);
  1081. expect(API.getRedoStack().length).toBe(0);
  1082. assertSelectedElements([rect1, rect3]);
  1083. act(() => h.app.actionManager.executeAction(actionBringForward));
  1084. expect(API.getUndoStack().length).toBe(7);
  1085. expect(API.getRedoStack().length).toBe(0);
  1086. assertSelectedElements([rect1, rect3]);
  1087. Keyboard.undo();
  1088. expect(API.getUndoStack().length).toBe(6);
  1089. expect(API.getRedoStack().length).toBe(1);
  1090. assertSelectedElements([rect1, rect3]);
  1091. expect(h.elements).toEqual([
  1092. expect.objectContaining({ id: rect1.id }),
  1093. expect.objectContaining({ id: rect3.id }),
  1094. expect.objectContaining({ id: rect2.id }),
  1095. ]);
  1096. Keyboard.redo();
  1097. expect(API.getUndoStack().length).toBe(7);
  1098. expect(API.getRedoStack().length).toBe(0);
  1099. assertSelectedElements([rect1, rect3]);
  1100. expect(h.elements).toEqual([
  1101. expect.objectContaining({ id: rect2.id }),
  1102. expect.objectContaining({ id: rect1.id }),
  1103. expect.objectContaining({ id: rect3.id }),
  1104. ]);
  1105. });
  1106. describe("should support bidirectional bindings", async () => {
  1107. let excalidrawAPI: ExcalidrawImperativeAPI;
  1108. let rect1: ExcalidrawGenericElement;
  1109. let rect2: ExcalidrawGenericElement;
  1110. let text: ExcalidrawTextElement;
  1111. let arrow: ExcalidrawLinearElement;
  1112. const rect1Props = {
  1113. type: "rectangle",
  1114. height: 100,
  1115. width: 100,
  1116. x: -100,
  1117. y: -50,
  1118. } as const;
  1119. const rect2Props = {
  1120. type: "rectangle",
  1121. height: 100,
  1122. width: 100,
  1123. x: 100,
  1124. y: -50,
  1125. } as const;
  1126. const textProps = {
  1127. type: "text",
  1128. x: -200,
  1129. text: "ola",
  1130. } as const;
  1131. beforeEach(async () => {
  1132. const excalidrawAPIPromise =
  1133. resolvablePromise<ExcalidrawImperativeAPI>();
  1134. await render(
  1135. <Excalidraw
  1136. excalidrawAPI={(api) => excalidrawAPIPromise.resolve(api as any)}
  1137. handleKeyboardGlobally={true}
  1138. />,
  1139. );
  1140. excalidrawAPI = await excalidrawAPIPromise;
  1141. rect1 = API.createElement({ ...rect1Props });
  1142. text = API.createElement({ ...textProps });
  1143. rect2 = API.createElement({ ...rect2Props });
  1144. excalidrawAPI.updateScene({
  1145. elements: [rect1, text, rect2],
  1146. storeAction: StoreAction.CAPTURE,
  1147. });
  1148. // bind text1 to rect1
  1149. mouse.select([rect1, text]);
  1150. fireEvent.contextMenu(GlobalTestState.interactiveCanvas);
  1151. fireEvent.click(
  1152. queryByText(
  1153. document.querySelector(".context-menu") as HTMLElement,
  1154. "Bind text to the container",
  1155. )!,
  1156. );
  1157. expect(API.getUndoStack().length).toBe(4);
  1158. expect(text.containerId).toBe(rect1.id);
  1159. expect(rect1.boundElements).toStrictEqual([
  1160. { id: text.id, type: "text" },
  1161. ]);
  1162. // bind arrow to rect1 and rect2
  1163. UI.clickTool("arrow");
  1164. mouse.down(0, 0);
  1165. mouse.up(100, 0);
  1166. arrow = h.elements[3] as ExcalidrawLinearElement;
  1167. expect(API.getUndoStack().length).toBe(5);
  1168. expect(arrow.startBinding).toEqual({
  1169. elementId: rect1.id,
  1170. focus: expect.toBeNonNaNNumber(),
  1171. gap: expect.toBeNonNaNNumber(),
  1172. });
  1173. expect(arrow.endBinding).toEqual({
  1174. elementId: rect2.id,
  1175. focus: expect.toBeNonNaNNumber(),
  1176. gap: expect.toBeNonNaNNumber(),
  1177. });
  1178. expect(rect1.boundElements).toStrictEqual([
  1179. { id: text.id, type: "text" },
  1180. { id: arrow.id, type: "arrow" },
  1181. ]);
  1182. expect(rect2.boundElements).toStrictEqual([
  1183. { id: arrow.id, type: "arrow" },
  1184. ]);
  1185. });
  1186. it("should unbind arrow from non deleted bindable elements on undo and rebind on redo", async () => {
  1187. Keyboard.undo();
  1188. expect(API.getUndoStack().length).toBe(4);
  1189. expect(API.getRedoStack().length).toBe(1);
  1190. expect(arrow.startBinding).toEqual({
  1191. elementId: rect1.id,
  1192. focus: expect.toBeNonNaNNumber(),
  1193. gap: expect.toBeNonNaNNumber(),
  1194. });
  1195. expect(arrow.endBinding).toEqual({
  1196. elementId: rect2.id,
  1197. focus: expect.toBeNonNaNNumber(),
  1198. gap: expect.toBeNonNaNNumber(),
  1199. });
  1200. expect(h.elements).toEqual([
  1201. expect.objectContaining({
  1202. id: rect1.id,
  1203. boundElements: [{ id: text.id, type: "text" }],
  1204. }),
  1205. expect.objectContaining({ id: text.id }),
  1206. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  1207. expect.objectContaining({ id: arrow.id, isDeleted: true }),
  1208. ]);
  1209. Keyboard.redo();
  1210. expect(API.getUndoStack().length).toBe(5);
  1211. expect(API.getRedoStack().length).toBe(0);
  1212. expect(arrow.startBinding).toEqual({
  1213. elementId: rect1.id,
  1214. focus: expect.toBeNonNaNNumber(),
  1215. gap: expect.toBeNonNaNNumber(),
  1216. });
  1217. expect(arrow.endBinding).toEqual({
  1218. elementId: rect2.id,
  1219. focus: expect.toBeNonNaNNumber(),
  1220. gap: expect.toBeNonNaNNumber(),
  1221. });
  1222. expect(h.elements).toEqual([
  1223. expect.objectContaining({
  1224. id: rect1.id,
  1225. boundElements: [
  1226. { id: text.id, type: "text" },
  1227. { id: arrow.id, type: "arrow" },
  1228. ],
  1229. }),
  1230. expect.objectContaining({ id: text.id }),
  1231. expect.objectContaining({
  1232. id: rect2.id,
  1233. boundElements: [{ id: arrow.id, type: "arrow" }],
  1234. }),
  1235. expect.objectContaining({ id: arrow.id, isDeleted: false }),
  1236. ]);
  1237. });
  1238. it("should unbind arrow from non deleted bindable elements on deletion and rebind on undo", async () => {
  1239. Keyboard.keyDown(KEYS.DELETE);
  1240. expect(API.getUndoStack().length).toBe(6);
  1241. expect(API.getRedoStack().length).toBe(0);
  1242. expect(arrow.startBinding).toEqual({
  1243. elementId: rect1.id,
  1244. focus: expect.toBeNonNaNNumber(),
  1245. gap: expect.toBeNonNaNNumber(),
  1246. });
  1247. expect(arrow.endBinding).toEqual({
  1248. elementId: rect2.id,
  1249. focus: expect.toBeNonNaNNumber(),
  1250. gap: expect.toBeNonNaNNumber(),
  1251. });
  1252. expect(h.elements).toEqual([
  1253. expect.objectContaining({
  1254. id: rect1.id,
  1255. boundElements: [{ id: text.id, type: "text" }],
  1256. }),
  1257. expect.objectContaining({ id: text.id }),
  1258. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  1259. expect.objectContaining({ id: arrow.id, isDeleted: true }),
  1260. ]);
  1261. Keyboard.undo();
  1262. expect(API.getUndoStack().length).toBe(5);
  1263. expect(API.getRedoStack().length).toBe(1);
  1264. expect(arrow.startBinding).toEqual({
  1265. elementId: rect1.id,
  1266. focus: expect.toBeNonNaNNumber(),
  1267. gap: expect.toBeNonNaNNumber(),
  1268. });
  1269. expect(arrow.endBinding).toEqual({
  1270. elementId: rect2.id,
  1271. focus: expect.toBeNonNaNNumber(),
  1272. gap: expect.toBeNonNaNNumber(),
  1273. });
  1274. expect(h.elements).toEqual([
  1275. expect.objectContaining({
  1276. id: rect1.id,
  1277. boundElements: [
  1278. { id: text.id, type: "text" },
  1279. { id: arrow.id, type: "arrow" },
  1280. ],
  1281. }),
  1282. expect.objectContaining({ id: text.id }),
  1283. expect.objectContaining({
  1284. id: rect2.id,
  1285. boundElements: [{ id: arrow.id, type: "arrow" }],
  1286. }),
  1287. expect.objectContaining({ id: arrow.id, isDeleted: false }),
  1288. ]);
  1289. });
  1290. it("should unbind everything from non deleted elements when iterating through the whole undo stack and vice versa rebind everything on redo", async () => {
  1291. Keyboard.undo();
  1292. Keyboard.undo();
  1293. Keyboard.undo();
  1294. Keyboard.undo();
  1295. Keyboard.undo();
  1296. expect(API.getUndoStack().length).toBe(0);
  1297. expect(API.getRedoStack().length).toBe(5);
  1298. expect(h.elements).toEqual([
  1299. expect.objectContaining({
  1300. id: rect1.id,
  1301. boundElements: [],
  1302. isDeleted: true,
  1303. }),
  1304. expect.objectContaining({
  1305. id: text.id,
  1306. containerId: null,
  1307. isDeleted: true,
  1308. }),
  1309. expect.objectContaining({
  1310. id: rect2.id,
  1311. boundElements: [],
  1312. isDeleted: true,
  1313. }),
  1314. expect.objectContaining({
  1315. id: arrow.id,
  1316. startBinding: {
  1317. elementId: rect1.id,
  1318. focus: expect.toBeNonNaNNumber(),
  1319. gap: expect.toBeNonNaNNumber(),
  1320. },
  1321. endBinding: {
  1322. elementId: rect2.id,
  1323. focus: expect.toBeNonNaNNumber(),
  1324. gap: expect.toBeNonNaNNumber(),
  1325. },
  1326. isDeleted: true,
  1327. }),
  1328. ]);
  1329. Keyboard.redo();
  1330. Keyboard.redo();
  1331. Keyboard.redo();
  1332. Keyboard.redo();
  1333. Keyboard.redo();
  1334. expect(API.getUndoStack().length).toBe(5);
  1335. expect(API.getRedoStack().length).toBe(0);
  1336. expect(h.elements).toEqual([
  1337. expect.objectContaining({
  1338. id: rect1.id,
  1339. boundElements: [
  1340. { id: text.id, type: "text" },
  1341. { id: arrow.id, type: "arrow" },
  1342. ],
  1343. isDeleted: false,
  1344. }),
  1345. expect.objectContaining({
  1346. id: text.id,
  1347. containerId: rect1.id,
  1348. isDeleted: false,
  1349. }),
  1350. expect.objectContaining({
  1351. id: rect2.id,
  1352. boundElements: [{ id: arrow.id, type: "arrow" }],
  1353. isDeleted: false,
  1354. }),
  1355. expect.objectContaining({
  1356. id: arrow.id,
  1357. startBinding: {
  1358. elementId: rect1.id,
  1359. focus: expect.toBeNonNaNNumber(),
  1360. gap: expect.toBeNonNaNNumber(),
  1361. },
  1362. endBinding: {
  1363. elementId: rect2.id,
  1364. focus: expect.toBeNonNaNNumber(),
  1365. gap: expect.toBeNonNaNNumber(),
  1366. },
  1367. isDeleted: false,
  1368. }),
  1369. ]);
  1370. });
  1371. it("should unbind rectangle from arrow on deletion and rebind on undo", async () => {
  1372. mouse.select(rect1);
  1373. Keyboard.keyPress(KEYS.DELETE);
  1374. expect(API.getUndoStack().length).toBe(7);
  1375. expect(API.getRedoStack().length).toBe(0);
  1376. expect(h.elements).toEqual([
  1377. expect.objectContaining({
  1378. id: rect1.id,
  1379. boundElements: [
  1380. { id: text.id, type: "text" },
  1381. { id: arrow.id, type: "arrow" },
  1382. ],
  1383. isDeleted: true,
  1384. }),
  1385. expect.objectContaining({
  1386. id: text.id,
  1387. containerId: rect1.id,
  1388. isDeleted: true,
  1389. }),
  1390. expect.objectContaining({
  1391. id: rect2.id,
  1392. boundElements: [{ id: arrow.id, type: "arrow" }],
  1393. isDeleted: false,
  1394. }),
  1395. expect.objectContaining({
  1396. id: arrow.id,
  1397. startBinding: null,
  1398. endBinding: {
  1399. elementId: rect2.id,
  1400. focus: expect.toBeNonNaNNumber(),
  1401. gap: expect.toBeNonNaNNumber(),
  1402. },
  1403. isDeleted: false,
  1404. }),
  1405. ]);
  1406. Keyboard.undo();
  1407. expect(API.getUndoStack().length).toBe(6);
  1408. expect(API.getRedoStack().length).toBe(1);
  1409. expect(h.elements).toEqual([
  1410. expect.objectContaining({
  1411. id: rect1.id,
  1412. boundElements: [
  1413. { id: arrow.id, type: "arrow" },
  1414. { id: text.id, type: "text" }, // order has now changed!
  1415. ],
  1416. isDeleted: false,
  1417. }),
  1418. expect.objectContaining({
  1419. id: text.id,
  1420. containerId: rect1.id,
  1421. isDeleted: false,
  1422. }),
  1423. expect.objectContaining({
  1424. id: rect2.id,
  1425. boundElements: [{ id: arrow.id, type: "arrow" }],
  1426. isDeleted: false,
  1427. }),
  1428. expect.objectContaining({
  1429. id: arrow.id,
  1430. startBinding: {
  1431. elementId: rect1.id,
  1432. focus: expect.toBeNonNaNNumber(),
  1433. gap: expect.toBeNonNaNNumber(),
  1434. },
  1435. endBinding: {
  1436. elementId: rect2.id,
  1437. focus: expect.toBeNonNaNNumber(),
  1438. gap: expect.toBeNonNaNNumber(),
  1439. },
  1440. isDeleted: false,
  1441. }),
  1442. ]);
  1443. });
  1444. it("should unbind rectangles from arrow on deletion and rebind on undo", async () => {
  1445. mouse.select([rect1, rect2]);
  1446. Keyboard.keyPress(KEYS.DELETE);
  1447. expect(API.getUndoStack().length).toBe(8);
  1448. expect(API.getRedoStack().length).toBe(0);
  1449. expect(h.elements).toEqual([
  1450. expect.objectContaining({
  1451. id: rect1.id,
  1452. boundElements: [
  1453. { id: text.id, type: "text" },
  1454. { id: arrow.id, type: "arrow" },
  1455. ],
  1456. isDeleted: true,
  1457. }),
  1458. expect.objectContaining({
  1459. id: text.id,
  1460. containerId: rect1.id,
  1461. isDeleted: true,
  1462. }),
  1463. expect.objectContaining({
  1464. id: rect2.id,
  1465. boundElements: [{ id: arrow.id, type: "arrow" }],
  1466. isDeleted: true,
  1467. }),
  1468. expect.objectContaining({
  1469. id: arrow.id,
  1470. startBinding: null,
  1471. endBinding: null,
  1472. isDeleted: false,
  1473. }),
  1474. ]);
  1475. Keyboard.undo();
  1476. expect(API.getUndoStack().length).toBe(7);
  1477. expect(API.getRedoStack().length).toBe(1);
  1478. expect(h.elements).toEqual([
  1479. expect.objectContaining({
  1480. id: rect1.id,
  1481. boundElements: [
  1482. { id: arrow.id, type: "arrow" },
  1483. { id: text.id, type: "text" }, // order has now changed!
  1484. ],
  1485. isDeleted: false,
  1486. }),
  1487. expect.objectContaining({
  1488. id: text.id,
  1489. containerId: rect1.id,
  1490. isDeleted: false,
  1491. }),
  1492. expect.objectContaining({
  1493. id: rect2.id,
  1494. boundElements: [{ id: arrow.id, type: "arrow" }],
  1495. isDeleted: false,
  1496. }),
  1497. expect.objectContaining({
  1498. id: arrow.id,
  1499. startBinding: {
  1500. elementId: rect1.id,
  1501. focus: expect.toBeNonNaNNumber(),
  1502. gap: expect.toBeNonNaNNumber(),
  1503. },
  1504. endBinding: {
  1505. elementId: rect2.id,
  1506. focus: expect.toBeNonNaNNumber(),
  1507. gap: expect.toBeNonNaNNumber(),
  1508. },
  1509. isDeleted: false,
  1510. }),
  1511. ]);
  1512. });
  1513. });
  1514. it("should disable undo/redo buttons when stacks empty", async () => {
  1515. const { container } = await render(
  1516. <Excalidraw
  1517. initialData={{
  1518. elements: [API.createElement({ type: "rectangle", id: "A" })],
  1519. }}
  1520. />,
  1521. );
  1522. const undoAction = createUndoAction(h.history, h.store);
  1523. const redoAction = createRedoAction(h.history, h.store);
  1524. await waitFor(() => {
  1525. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  1526. expect(h.history.isUndoStackEmpty).toBeTruthy();
  1527. expect(h.history.isRedoStackEmpty).toBeTruthy();
  1528. });
  1529. const undoButton = queryByTestId(container, "button-undo");
  1530. const redoButton = queryByTestId(container, "button-redo");
  1531. expect(undoButton).toBeDisabled();
  1532. expect(redoButton).toBeDisabled();
  1533. const rectangle = UI.createElement("rectangle");
  1534. expect(h.elements).toEqual([
  1535. expect.objectContaining({ id: "A" }),
  1536. expect.objectContaining({ id: rectangle.id }),
  1537. ]);
  1538. expect(h.history.isUndoStackEmpty).toBeFalsy();
  1539. expect(h.history.isRedoStackEmpty).toBeTruthy();
  1540. expect(undoButton).not.toBeDisabled();
  1541. expect(redoButton).toBeDisabled();
  1542. act(() => h.app.actionManager.executeAction(undoAction));
  1543. expect(h.history.isUndoStackEmpty).toBeTruthy();
  1544. expect(h.history.isRedoStackEmpty).toBeFalsy();
  1545. expect(undoButton).toBeDisabled();
  1546. expect(redoButton).not.toBeDisabled();
  1547. act(() => h.app.actionManager.executeAction(redoAction));
  1548. expect(h.history.isUndoStackEmpty).toBeFalsy();
  1549. expect(h.history.isRedoStackEmpty).toBeTruthy();
  1550. expect(undoButton).not.toBeDisabled();
  1551. expect(redoButton).toBeDisabled();
  1552. });
  1553. it("remounting undo/redo buttons should initialize undo/redo state correctly", async () => {
  1554. const { container } = await render(
  1555. <Excalidraw
  1556. initialData={{
  1557. elements: [API.createElement({ type: "rectangle", id: "A" })],
  1558. }}
  1559. />,
  1560. );
  1561. const undoAction = createUndoAction(h.history, h.store);
  1562. await waitFor(() => {
  1563. expect(h.elements).toEqual([expect.objectContaining({ id: "A" })]);
  1564. expect(h.history.isUndoStackEmpty).toBeTruthy();
  1565. expect(h.history.isRedoStackEmpty).toBeTruthy();
  1566. });
  1567. expect(queryByTestId(container, "button-undo")).toBeDisabled();
  1568. expect(queryByTestId(container, "button-redo")).toBeDisabled();
  1569. // testing undo button
  1570. // -----------------------------------------------------------------------
  1571. const rectangle = UI.createElement("rectangle");
  1572. expect(h.elements).toEqual([
  1573. expect.objectContaining({ id: "A" }),
  1574. expect.objectContaining({ id: rectangle.id }),
  1575. ]);
  1576. expect(h.history.isUndoStackEmpty).toBeFalsy();
  1577. expect(h.history.isRedoStackEmpty).toBeTruthy();
  1578. expect(queryByTestId(container, "button-undo")).not.toBeDisabled();
  1579. expect(queryByTestId(container, "button-redo")).toBeDisabled();
  1580. act(() => h.app.actionManager.executeAction(actionToggleViewMode));
  1581. expect(h.state.viewModeEnabled).toBe(true);
  1582. expect(queryByTestId(container, "button-undo")).toBeNull();
  1583. expect(queryByTestId(container, "button-redo")).toBeNull();
  1584. act(() => h.app.actionManager.executeAction(actionToggleViewMode));
  1585. expect(h.state.viewModeEnabled).toBe(false);
  1586. await waitFor(() => {
  1587. expect(queryByTestId(container, "button-undo")).not.toBeDisabled();
  1588. expect(queryByTestId(container, "button-redo")).toBeDisabled();
  1589. });
  1590. // testing redo button
  1591. // -----------------------------------------------------------------------
  1592. act(() => h.app.actionManager.executeAction(undoAction));
  1593. expect(h.history.isUndoStackEmpty).toBeTruthy();
  1594. expect(h.history.isRedoStackEmpty).toBeFalsy();
  1595. expect(queryByTestId(container, "button-undo")).toBeDisabled();
  1596. expect(queryByTestId(container, "button-redo")).not.toBeDisabled();
  1597. act(() => h.app.actionManager.executeAction(actionToggleViewMode));
  1598. expect(h.state.viewModeEnabled).toBe(true);
  1599. expect(queryByTestId(container, "button-undo")).toBeNull();
  1600. expect(queryByTestId(container, "button-redo")).toBeNull();
  1601. act(() => h.app.actionManager.executeAction(actionToggleViewMode));
  1602. expect(h.state.viewModeEnabled).toBe(false);
  1603. expect(h.history.isUndoStackEmpty).toBeTruthy();
  1604. expect(h.history.isRedoStackEmpty).toBeFalsy();
  1605. expect(queryByTestId(container, "button-undo")).toBeDisabled();
  1606. expect(queryByTestId(container, "button-redo")).not.toBeDisabled();
  1607. });
  1608. });
  1609. describe("multiplayer undo/redo", () => {
  1610. let excalidrawAPI: ExcalidrawImperativeAPI;
  1611. // Util to check that we end up in the same state after series of undo / redo
  1612. function runTwice(callback: () => void) {
  1613. for (let i = 0; i < 2; i++) {
  1614. callback();
  1615. }
  1616. }
  1617. beforeEach(async () => {
  1618. const excalidrawAPIPromise = resolvablePromise<ExcalidrawImperativeAPI>();
  1619. await render(
  1620. <Excalidraw
  1621. excalidrawAPI={(api) => excalidrawAPIPromise.resolve(api as any)}
  1622. handleKeyboardGlobally={true}
  1623. isCollaborating={true}
  1624. />,
  1625. );
  1626. excalidrawAPI = await excalidrawAPIPromise;
  1627. });
  1628. it("should not override remote changes on different elements", async () => {
  1629. UI.createElement("rectangle", { x: 10 });
  1630. togglePopover("Background");
  1631. UI.clickOnTestId("color-red");
  1632. expect(API.getUndoStack().length).toBe(2);
  1633. expect(h.elements).toEqual([
  1634. expect.objectContaining({ backgroundColor: red }),
  1635. ]);
  1636. // Simulate remote update
  1637. excalidrawAPI.updateScene({
  1638. elements: [
  1639. ...h.elements,
  1640. API.createElement({
  1641. type: "rectangle",
  1642. strokeColor: blue,
  1643. }),
  1644. ],
  1645. storeAction: StoreAction.UPDATE,
  1646. });
  1647. Keyboard.undo();
  1648. expect(h.elements).toEqual([
  1649. expect.objectContaining({ backgroundColor: transparent }),
  1650. expect.objectContaining({ strokeColor: blue }),
  1651. ]);
  1652. Keyboard.redo();
  1653. expect(h.elements).toEqual([
  1654. expect.objectContaining({ backgroundColor: red }),
  1655. expect.objectContaining({ strokeColor: blue }),
  1656. ]);
  1657. Keyboard.undo();
  1658. expect(API.getUndoStack().length).toBe(1);
  1659. expect(API.getUndoStack().length).toBe(1);
  1660. expect(h.elements).toEqual([
  1661. expect.objectContaining({ backgroundColor: transparent }),
  1662. expect.objectContaining({ strokeColor: blue }),
  1663. ]);
  1664. });
  1665. it("should not override remote changes on different properties", async () => {
  1666. UI.createElement("rectangle", { x: 10 });
  1667. togglePopover("Background");
  1668. UI.clickOnTestId("color-red");
  1669. expect(API.getUndoStack().length).toBe(2);
  1670. // Simulate remote update
  1671. excalidrawAPI.updateScene({
  1672. elements: [
  1673. newElementWith(h.elements[0], {
  1674. strokeColor: yellow,
  1675. }),
  1676. ],
  1677. storeAction: StoreAction.UPDATE,
  1678. });
  1679. Keyboard.undo();
  1680. expect(h.elements).toEqual([
  1681. expect.objectContaining({
  1682. backgroundColor: transparent,
  1683. strokeColor: yellow,
  1684. }),
  1685. ]);
  1686. Keyboard.redo();
  1687. expect(h.elements).toEqual([
  1688. expect.objectContaining({
  1689. backgroundColor: red,
  1690. strokeColor: yellow,
  1691. }),
  1692. ]);
  1693. });
  1694. // https://www.figma.com/blog/how-figmas-multiplayer-technology-works/#implementing-undo
  1695. // This is due to the fact that deltas are updated in `applyLatestChanges`.
  1696. it("should update history entries after remote changes on the same properties", async () => {
  1697. UI.createElement("rectangle", { x: 10 });
  1698. togglePopover("Background");
  1699. UI.clickOnTestId("color-red");
  1700. UI.clickOnTestId("color-blue");
  1701. // At this point we have all the history entries created, no new entries will be created, only existing entries will get inversed and updated
  1702. expect(API.getUndoStack().length).toBe(3);
  1703. Keyboard.undo();
  1704. expect(h.elements).toEqual([
  1705. expect.objectContaining({ backgroundColor: red }),
  1706. ]);
  1707. Keyboard.redo();
  1708. expect(h.elements).toEqual([
  1709. expect.objectContaining({ backgroundColor: blue }),
  1710. ]);
  1711. // Simulate remote update
  1712. excalidrawAPI.updateScene({
  1713. elements: [
  1714. newElementWith(h.elements[0], {
  1715. backgroundColor: yellow,
  1716. }),
  1717. ],
  1718. storeAction: StoreAction.UPDATE,
  1719. });
  1720. // At this point our entry gets updated from `red` -> `blue` into `red` -> `yellow`
  1721. Keyboard.undo();
  1722. expect(h.elements).toEqual([
  1723. expect.objectContaining({ backgroundColor: red }),
  1724. ]);
  1725. // Simulate remote update
  1726. excalidrawAPI.updateScene({
  1727. elements: [
  1728. newElementWith(h.elements[0], {
  1729. backgroundColor: violet,
  1730. }),
  1731. ],
  1732. storeAction: StoreAction.UPDATE,
  1733. });
  1734. // At this point our (inversed) entry gets updated from `red` -> `yellow` into `violet` -> `yellow`
  1735. Keyboard.redo();
  1736. expect(h.elements).toEqual([
  1737. expect.objectContaining({ backgroundColor: yellow }),
  1738. ]);
  1739. Keyboard.undo();
  1740. expect(h.elements).toEqual([
  1741. expect.objectContaining({ backgroundColor: violet }),
  1742. ]);
  1743. Keyboard.undo();
  1744. expect(h.elements).toEqual([
  1745. expect.objectContaining({ backgroundColor: transparent }),
  1746. ]);
  1747. });
  1748. // TODO: #7348 ideally we should not override, but since the order of groupIds matters, right now we cannot ensure that with postprocssed groupIds the order will be consistent after series or undos/redos, we don't postprocess them at all
  1749. // in other words, if we would postprocess groupIds, the groupIds order on "redo" below would be ["B", "A"] instead of ["A", "B"]
  1750. it("should override remotely added groups on undo, but restore them on redo", async () => {
  1751. const rect1 = API.createElement({ type: "rectangle" });
  1752. const rect2 = API.createElement({ type: "rectangle" });
  1753. // Initialize scene
  1754. excalidrawAPI.updateScene({
  1755. elements: [rect1, rect2],
  1756. storeAction: StoreAction.UPDATE,
  1757. });
  1758. // Simulate local update
  1759. excalidrawAPI.updateScene({
  1760. elements: [
  1761. newElementWith(h.elements[0], { groupIds: ["A"] }),
  1762. newElementWith(h.elements[1], { groupIds: ["A"] }),
  1763. ],
  1764. storeAction: StoreAction.CAPTURE,
  1765. });
  1766. const rect3 = API.createElement({ type: "rectangle", groupIds: ["B"] });
  1767. const rect4 = API.createElement({ type: "rectangle", groupIds: ["B"] });
  1768. // Simulate remote update
  1769. excalidrawAPI.updateScene({
  1770. elements: [
  1771. newElementWith(h.elements[0], { groupIds: ["A", "B"] }),
  1772. newElementWith(h.elements[1], { groupIds: ["A", "B"] }),
  1773. rect3,
  1774. rect4,
  1775. ],
  1776. storeAction: StoreAction.UPDATE,
  1777. });
  1778. Keyboard.undo();
  1779. expect(API.getUndoStack().length).toBe(0);
  1780. expect(API.getRedoStack().length).toBe(1);
  1781. expect(h.elements).toEqual([
  1782. expect.objectContaining({ id: rect1.id, groupIds: [] }),
  1783. expect.objectContaining({ id: rect2.id, groupIds: [] }),
  1784. expect.objectContaining({ id: rect3.id, groupIds: ["B"] }),
  1785. expect.objectContaining({ id: rect4.id, groupIds: ["B"] }),
  1786. ]);
  1787. Keyboard.redo();
  1788. expect(API.getUndoStack().length).toBe(1);
  1789. expect(API.getRedoStack().length).toBe(0);
  1790. expect(h.elements).toEqual([
  1791. expect.objectContaining({ id: rect1.id, groupIds: ["A", "B"] }),
  1792. expect.objectContaining({ id: rect2.id, groupIds: ["A", "B"] }),
  1793. expect.objectContaining({ id: rect3.id, groupIds: ["B"] }),
  1794. expect.objectContaining({ id: rect4.id, groupIds: ["B"] }),
  1795. ]);
  1796. });
  1797. it("should override remotely added points on undo, but restore them on redo", async () => {
  1798. UI.clickTool("arrow");
  1799. mouse.click(0, 0);
  1800. mouse.click(10, 10);
  1801. mouse.click(20, 20);
  1802. // actionFinalize
  1803. Keyboard.keyPress(KEYS.ENTER);
  1804. // Simulate remote update
  1805. excalidrawAPI.updateScene({
  1806. elements: [
  1807. newElementWith(h.elements[0] as ExcalidrawLinearElement, {
  1808. points: [
  1809. [0, 0],
  1810. [5, 5],
  1811. [10, 10],
  1812. [15, 15],
  1813. [20, 20],
  1814. ],
  1815. }),
  1816. ],
  1817. storeAction: StoreAction.UPDATE,
  1818. });
  1819. Keyboard.undo(); // undo `actionFinalize`
  1820. Keyboard.undo();
  1821. expect(API.getUndoStack().length).toBe(1);
  1822. expect(API.getRedoStack().length).toBe(2);
  1823. expect(h.elements).toEqual([
  1824. expect.objectContaining({
  1825. points: [
  1826. [0, 0],
  1827. // overriding all the remote points as they are not being postprocessed (as we cannot ensure the order consistency similar to groupIds)
  1828. // but in this case it might not make even sense to combine the points, as in some cases the linear element might lead unexpected results
  1829. [10, 10],
  1830. ],
  1831. }),
  1832. ]);
  1833. Keyboard.undo();
  1834. expect(API.getUndoStack().length).toBe(0);
  1835. expect(API.getRedoStack().length).toBe(3);
  1836. expect(h.elements).toEqual([
  1837. expect.objectContaining({
  1838. isDeleted: true,
  1839. points: [
  1840. [0, 0],
  1841. [10, 10],
  1842. ],
  1843. }),
  1844. ]);
  1845. Keyboard.redo();
  1846. expect(API.getUndoStack().length).toBe(1);
  1847. expect(API.getRedoStack().length).toBe(2);
  1848. expect(h.elements).toEqual([
  1849. expect.objectContaining({
  1850. isDeleted: false,
  1851. points: [
  1852. [0, 0],
  1853. [10, 10],
  1854. ],
  1855. }),
  1856. ]);
  1857. Keyboard.redo();
  1858. expect(API.getUndoStack().length).toBe(2);
  1859. expect(API.getRedoStack().length).toBe(1);
  1860. expect(h.elements).toEqual([
  1861. expect.objectContaining({
  1862. points: [
  1863. [0, 0],
  1864. [5, 5],
  1865. [10, 10],
  1866. [15, 15],
  1867. [20, 20],
  1868. ],
  1869. }),
  1870. ]);
  1871. Keyboard.redo(); // redo `actionFinalize`
  1872. expect(API.getUndoStack().length).toBe(3);
  1873. expect(API.getRedoStack().length).toBe(0);
  1874. expect(h.elements).toEqual([
  1875. expect.objectContaining({
  1876. points: [
  1877. [0, 0],
  1878. [5, 5],
  1879. [10, 10],
  1880. [15, 15],
  1881. [20, 20],
  1882. ],
  1883. }),
  1884. ]);
  1885. });
  1886. it("should redistribute deltas when element gets removed locally but is restored remotely", async () => {
  1887. UI.createElement("rectangle", { x: 10 });
  1888. Keyboard.keyDown(KEYS.DELETE);
  1889. expect(API.getUndoStack().length).toBe(2);
  1890. expect(h.elements).toEqual([
  1891. expect.objectContaining({
  1892. backgroundColor: transparent,
  1893. isDeleted: true,
  1894. }),
  1895. ]);
  1896. // Simulate remote update & restore
  1897. excalidrawAPI.updateScene({
  1898. elements: [
  1899. newElementWith(h.elements[0], {
  1900. backgroundColor: yellow,
  1901. isDeleted: false, // undeletion might happen due to concurrency between clients
  1902. }),
  1903. ],
  1904. storeAction: StoreAction.UPDATE,
  1905. });
  1906. expect(API.getSelectedElements()).toEqual([]);
  1907. expect(h.elements).toEqual([
  1908. expect.objectContaining({
  1909. backgroundColor: yellow,
  1910. isDeleted: false,
  1911. }),
  1912. ]);
  1913. // inserted.isDeleted: true is updated with the latest changes to false
  1914. // deleted.isDeleted and inserted.isDeleted are the same and therefore removed delta becomes an updated delta
  1915. Keyboard.undo();
  1916. expect(assertSelectedElements(h.elements[0]));
  1917. expect(API.getUndoStack().length).toBe(1);
  1918. expect(API.getRedoStack().length).toBe(1);
  1919. expect(h.elements).toEqual([
  1920. expect.objectContaining({
  1921. backgroundColor: yellow,
  1922. isDeleted: false,
  1923. }),
  1924. ]);
  1925. Keyboard.undo();
  1926. expect(API.getSelectedElements()).toEqual([]);
  1927. expect(API.getUndoStack().length).toBe(0);
  1928. expect(API.getRedoStack().length).toBe(2);
  1929. expect(h.elements).toEqual([
  1930. expect.objectContaining({
  1931. backgroundColor: yellow,
  1932. isDeleted: true,
  1933. }),
  1934. ]);
  1935. Keyboard.redo();
  1936. expect(assertSelectedElements(h.elements[0]));
  1937. expect(API.getUndoStack().length).toBe(1);
  1938. expect(API.getRedoStack().length).toBe(1);
  1939. expect(h.elements).toEqual([
  1940. expect.objectContaining({
  1941. backgroundColor: yellow,
  1942. isDeleted: false,
  1943. }),
  1944. ]);
  1945. Keyboard.redo();
  1946. expect(assertSelectedElements([]));
  1947. expect(API.getUndoStack().length).toBe(2);
  1948. expect(API.getRedoStack().length).toBe(0);
  1949. expect(h.elements).toEqual([
  1950. expect.objectContaining({
  1951. backgroundColor: yellow,
  1952. isDeleted: false, // isDeleted got updated
  1953. }),
  1954. ]);
  1955. });
  1956. it("should iterate through the history when when element change relates to remotely deleted element", async () => {
  1957. UI.createElement("rectangle", { x: 10 });
  1958. togglePopover("Background");
  1959. UI.clickOnTestId("color-red");
  1960. expect(API.getUndoStack().length).toBe(2);
  1961. expect(h.elements).toEqual([
  1962. expect.objectContaining({ backgroundColor: red }),
  1963. ]);
  1964. // Simulate remote update & deletion
  1965. excalidrawAPI.updateScene({
  1966. elements: [
  1967. newElementWith(h.elements[0], {
  1968. backgroundColor: yellow,
  1969. isDeleted: true,
  1970. }),
  1971. ],
  1972. storeAction: StoreAction.UPDATE,
  1973. });
  1974. expect(h.elements).toEqual([
  1975. expect.objectContaining({
  1976. backgroundColor: yellow,
  1977. isDeleted: true,
  1978. }),
  1979. ]);
  1980. // Will iterate through undo stack since applying the change
  1981. // results in no visible change on a deleted element
  1982. Keyboard.undo();
  1983. expect(API.getUndoStack().length).toBe(0);
  1984. expect(API.getRedoStack().length).toBe(2);
  1985. expect(h.elements).toEqual([
  1986. expect.objectContaining({
  1987. backgroundColor: transparent,
  1988. isDeleted: true,
  1989. }),
  1990. ]);
  1991. // We reached the bottom, again we iterate through invisible changes and reach the top
  1992. Keyboard.redo();
  1993. assertSelectedElements();
  1994. expect(API.getUndoStack().length).toBe(2);
  1995. expect(API.getRedoStack().length).toBe(0);
  1996. expect(h.elements).toEqual([
  1997. expect.objectContaining({
  1998. backgroundColor: yellow, // the color still get's updated
  1999. isDeleted: true, // but the element remains deleted
  2000. }),
  2001. ]);
  2002. });
  2003. it("should iterate through the history when element changes relate only to remotely deleted elements", async () => {
  2004. const rect1 = UI.createElement("rectangle", { x: 10 });
  2005. const rect2 = UI.createElement("rectangle", { x: 20 });
  2006. togglePopover("Background");
  2007. UI.clickOnTestId("color-red");
  2008. const rect3 = UI.createElement("rectangle", { x: 30, y: 30 });
  2009. // move rect3
  2010. mouse.downAt(35, 35);
  2011. mouse.moveTo(55, 55);
  2012. mouse.upAt(55, 55);
  2013. expect(API.getUndoStack().length).toBe(5);
  2014. // Simulate remote update
  2015. excalidrawAPI.updateScene({
  2016. elements: [
  2017. h.elements[0],
  2018. newElementWith(h.elements[1], {
  2019. isDeleted: true,
  2020. }),
  2021. newElementWith(h.elements[2], {
  2022. isDeleted: true,
  2023. }),
  2024. ],
  2025. storeAction: StoreAction.UPDATE,
  2026. });
  2027. Keyboard.undo();
  2028. expect(API.getUndoStack().length).toBe(1);
  2029. expect(API.getRedoStack().length).toBe(4);
  2030. expect(API.getSelectedElements()).toEqual([
  2031. expect.objectContaining({ id: rect1.id }),
  2032. ]);
  2033. expect(h.elements).toEqual([
  2034. expect.objectContaining({
  2035. id: rect1.id,
  2036. isDeleted: false,
  2037. }),
  2038. expect.objectContaining({
  2039. id: rect2.id,
  2040. isDeleted: true,
  2041. backgroundColor: transparent,
  2042. }),
  2043. expect.objectContaining({
  2044. id: rect3.id,
  2045. isDeleted: true,
  2046. x: 30,
  2047. y: 30,
  2048. }),
  2049. ]);
  2050. Keyboard.redo();
  2051. expect(API.getUndoStack().length).toBe(5);
  2052. expect(API.getRedoStack().length).toBe(0);
  2053. expect(API.getSelectedElements()).toEqual([]);
  2054. expect(h.elements).toEqual([
  2055. expect.objectContaining({
  2056. id: rect1.id,
  2057. isDeleted: false,
  2058. }),
  2059. expect.objectContaining({
  2060. id: rect2.id,
  2061. isDeleted: true,
  2062. backgroundColor: red,
  2063. }),
  2064. expect.objectContaining({
  2065. id: rect3.id,
  2066. isDeleted: true,
  2067. x: 50,
  2068. y: 50,
  2069. }),
  2070. ]);
  2071. });
  2072. it("should iterate through the history when selected elements relate only to remotely deleted elements", async () => {
  2073. const rect1 = API.createElement({ type: "rectangle", x: 10, y: 10 });
  2074. const rect2 = API.createElement({ type: "rectangle", x: 20, y: 20 });
  2075. const rect3 = API.createElement({ type: "rectangle", x: 30, y: 30 });
  2076. h.elements = [rect1, rect2, rect3];
  2077. mouse.select(rect1);
  2078. mouse.select([rect2, rect3]);
  2079. expect(API.getUndoStack().length).toBe(3);
  2080. expect(API.getSelectedElements()).toEqual([
  2081. expect.objectContaining({ id: rect2.id }),
  2082. expect.objectContaining({ id: rect3.id }),
  2083. ]);
  2084. // Simulate remote update
  2085. excalidrawAPI.updateScene({
  2086. elements: [
  2087. h.elements[0],
  2088. newElementWith(h.elements[1], {
  2089. isDeleted: true,
  2090. }),
  2091. newElementWith(h.elements[2], {
  2092. isDeleted: true,
  2093. }),
  2094. ],
  2095. storeAction: StoreAction.UPDATE,
  2096. });
  2097. Keyboard.undo();
  2098. expect(API.getUndoStack().length).toBe(1);
  2099. expect(API.getRedoStack().length).toBe(2);
  2100. expect(API.getSelectedElements()).toEqual([
  2101. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  2102. ]);
  2103. Keyboard.redo();
  2104. expect(API.getUndoStack().length).toBe(3);
  2105. expect(API.getRedoStack().length).toBe(0);
  2106. // do not expect any selectedElementIds, as all relate to deleted elements
  2107. expect(API.getSelectedElements()).toEqual([]);
  2108. expect(h.elements).toEqual([
  2109. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  2110. expect.objectContaining({ id: rect2.id, isDeleted: true }),
  2111. expect.objectContaining({ id: rect3.id, isDeleted: true }),
  2112. ]);
  2113. Keyboard.undo();
  2114. expect(API.getUndoStack().length).toBe(1);
  2115. expect(API.getRedoStack().length).toBe(2);
  2116. expect(API.getSelectedElements()).toEqual([
  2117. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  2118. ]);
  2119. // Simulate remote update
  2120. excalidrawAPI.updateScene({
  2121. elements: [
  2122. h.elements[0],
  2123. newElementWith(h.elements[1], {
  2124. isDeleted: false,
  2125. }),
  2126. newElementWith(h.elements[2], {
  2127. isDeleted: false,
  2128. }),
  2129. ],
  2130. storeAction: StoreAction.UPDATE,
  2131. });
  2132. Keyboard.redo();
  2133. expect(API.getUndoStack().length).toBe(2);
  2134. expect(API.getRedoStack().length).toBe(1);
  2135. expect(API.getSelectedElements()).toEqual([
  2136. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  2137. ]);
  2138. Keyboard.redo();
  2139. expect(API.getUndoStack().length).toBe(3);
  2140. expect(API.getRedoStack().length).toBe(0);
  2141. // now we again expect these as selected, as they got restored remotely
  2142. expect(API.getSelectedElements()).toEqual([
  2143. expect.objectContaining({ id: rect2.id }),
  2144. expect.objectContaining({ id: rect3.id }),
  2145. ]);
  2146. expect(h.elements).toEqual([
  2147. expect.objectContaining({ id: rect1.id, isDeleted: false }),
  2148. expect.objectContaining({ id: rect2.id, isDeleted: false }),
  2149. expect.objectContaining({ id: rect3.id, isDeleted: false }),
  2150. ]);
  2151. });
  2152. it("should iterate through the history when selected groups contain only remotely deleted elements", async () => {
  2153. const rect1 = API.createElement({
  2154. type: "rectangle",
  2155. groupIds: ["A"],
  2156. });
  2157. const rect2 = API.createElement({
  2158. type: "rectangle",
  2159. groupIds: ["A"],
  2160. });
  2161. const rect3 = API.createElement({
  2162. type: "rectangle",
  2163. groupIds: ["B"],
  2164. });
  2165. const rect4 = API.createElement({
  2166. type: "rectangle",
  2167. groupIds: ["B"],
  2168. });
  2169. // Simulate remote update
  2170. excalidrawAPI.updateScene({
  2171. elements: [rect1, rect2],
  2172. storeAction: StoreAction.UPDATE,
  2173. });
  2174. Keyboard.withModifierKeys({ ctrl: true }, () => {
  2175. Keyboard.keyPress(KEYS.A);
  2176. });
  2177. // Simulate remote update
  2178. excalidrawAPI.updateScene({
  2179. elements: [h.elements[0], h.elements[1], rect3, rect4],
  2180. storeAction: StoreAction.UPDATE,
  2181. });
  2182. Keyboard.withModifierKeys({ ctrl: true }, () => {
  2183. Keyboard.keyPress(KEYS.A);
  2184. });
  2185. expect(API.getUndoStack().length).toBe(2);
  2186. expect(API.getRedoStack().length).toBe(0);
  2187. expect(h.state.selectedGroupIds).toEqual({ A: true, B: true });
  2188. // Simulate remote update
  2189. excalidrawAPI.updateScene({
  2190. elements: [
  2191. newElementWith(h.elements[0], {
  2192. isDeleted: true,
  2193. }),
  2194. newElementWith(h.elements[1], {
  2195. isDeleted: true,
  2196. }),
  2197. ],
  2198. storeAction: StoreAction.UPDATE,
  2199. });
  2200. Keyboard.undo();
  2201. expect(API.getUndoStack().length).toBe(0);
  2202. expect(API.getRedoStack().length).toBe(2); // iterated two steps back!
  2203. expect(h.state.selectedGroupIds).toEqual({});
  2204. Keyboard.redo();
  2205. expect(API.getUndoStack().length).toBe(2); // iterated two steps forward!
  2206. expect(API.getRedoStack().length).toBe(0);
  2207. expect(h.state.selectedGroupIds).toEqual({});
  2208. Keyboard.undo();
  2209. // Simulate remote update
  2210. excalidrawAPI.updateScene({
  2211. elements: [
  2212. newElementWith(h.elements[0], {
  2213. isDeleted: false,
  2214. }),
  2215. newElementWith(h.elements[1], {
  2216. isDeleted: false,
  2217. }),
  2218. ],
  2219. storeAction: StoreAction.UPDATE,
  2220. });
  2221. Keyboard.redo();
  2222. expect(API.getUndoStack().length).toBe(1);
  2223. expect(API.getRedoStack().length).toBe(1);
  2224. expect(h.state.selectedGroupIds).toEqual({ A: true });
  2225. // Simulate remote update
  2226. excalidrawAPI.updateScene({
  2227. elements: [h.elements[0], h.elements[1], rect3, rect4],
  2228. storeAction: StoreAction.UPDATE,
  2229. });
  2230. Keyboard.redo();
  2231. expect(API.getUndoStack().length).toBe(2);
  2232. expect(API.getRedoStack().length).toBe(0);
  2233. expect(h.state.selectedGroupIds).toEqual({ A: true, B: true });
  2234. });
  2235. it("should iterate through the history when editing group contains only remotely deleted elements", async () => {
  2236. const rect1 = API.createElement({
  2237. type: "rectangle",
  2238. groupIds: ["A"],
  2239. x: 0,
  2240. });
  2241. const rect2 = API.createElement({
  2242. type: "rectangle",
  2243. groupIds: ["A"],
  2244. x: 100,
  2245. });
  2246. h.elements = [rect1, rect2];
  2247. mouse.select(rect1);
  2248. // inside the editing group
  2249. mouse.doubleClickOn(rect2);
  2250. expect(API.getUndoStack().length).toBe(2);
  2251. expect(API.getRedoStack().length).toBe(0);
  2252. expect(h.state.editingGroupId).toBe("A");
  2253. mouse.clickAt(-10, -10);
  2254. expect(API.getSelectedElements().length).toBe(0);
  2255. expect(API.getUndoStack().length).toBe(3);
  2256. expect(API.getRedoStack().length).toBe(0);
  2257. expect(h.state.editingGroupId).toBeNull();
  2258. // Simulate remote update
  2259. excalidrawAPI.updateScene({
  2260. elements: [
  2261. newElementWith(h.elements[0], {
  2262. isDeleted: true,
  2263. }),
  2264. newElementWith(h.elements[1], {
  2265. isDeleted: true,
  2266. }),
  2267. ],
  2268. storeAction: StoreAction.UPDATE,
  2269. });
  2270. Keyboard.undo();
  2271. expect(API.getUndoStack().length).toBe(0);
  2272. expect(API.getRedoStack().length).toBe(3);
  2273. expect(h.state.editingGroupId).toBeNull();
  2274. Keyboard.redo();
  2275. expect(API.getUndoStack().length).toBe(3);
  2276. expect(API.getRedoStack().length).toBe(0);
  2277. expect(h.state.editingGroupId).toBeNull();
  2278. // Simulate remote update
  2279. excalidrawAPI.updateScene({
  2280. elements: [
  2281. newElementWith(h.elements[0], {
  2282. isDeleted: false,
  2283. }),
  2284. h.elements[1],
  2285. ],
  2286. storeAction: StoreAction.UPDATE,
  2287. });
  2288. Keyboard.undo();
  2289. expect(API.getUndoStack().length).toBe(2);
  2290. expect(API.getRedoStack().length).toBe(1);
  2291. expect(h.state.editingGroupId).toBe("A");
  2292. Keyboard.redo();
  2293. expect(API.getUndoStack().length).toBe(3);
  2294. expect(API.getRedoStack().length).toBe(0);
  2295. expect(h.state.editingGroupId).toBeNull();
  2296. });
  2297. it("should iterate through the history when selected or editing linear element was remotely deleted", async () => {
  2298. // create three point arrow
  2299. UI.clickTool("arrow");
  2300. mouse.click(0, 0);
  2301. mouse.click(10, 10);
  2302. // actionFinalize
  2303. Keyboard.keyPress(KEYS.ENTER);
  2304. // open editor
  2305. Keyboard.withModifierKeys({ ctrl: true }, () => {
  2306. Keyboard.keyPress(KEYS.ENTER);
  2307. });
  2308. // leave editor
  2309. Keyboard.keyPress(KEYS.ESCAPE);
  2310. expect(API.getUndoStack().length).toBe(4);
  2311. expect(API.getRedoStack().length).toBe(0);
  2312. expect(h.state.editingLinearElement).toBeNull();
  2313. expect(h.state.selectedLinearElement).toBeNull();
  2314. // Simulate remote update
  2315. excalidrawAPI.updateScene({
  2316. elements: [
  2317. newElementWith(h.elements[0], {
  2318. isDeleted: true,
  2319. }),
  2320. ],
  2321. storeAction: StoreAction.UPDATE,
  2322. });
  2323. Keyboard.undo();
  2324. expect(API.getUndoStack().length).toBe(0);
  2325. expect(API.getRedoStack().length).toBe(4);
  2326. expect(h.state.editingLinearElement).toBeNull();
  2327. expect(h.state.selectedLinearElement).toBeNull();
  2328. Keyboard.redo();
  2329. expect(API.getUndoStack().length).toBe(4);
  2330. expect(API.getRedoStack().length).toBe(0);
  2331. expect(h.state.editingLinearElement).toBeNull();
  2332. expect(h.state.selectedLinearElement).toBeNull();
  2333. });
  2334. it("should iterate through the history when z-index changes do not produce visible change and we synced changed indices", async () => {
  2335. const rect1 = API.createElement({ type: "rectangle", x: 10, y: 10 }); // a "a0"
  2336. const rect2 = API.createElement({ type: "rectangle", x: 20, y: 20 }); // b "a1"
  2337. const rect3 = API.createElement({ type: "rectangle", x: 30, y: 30 }); // c "a2"
  2338. h.elements = [rect1, rect2, rect3];
  2339. mouse.select(rect2);
  2340. act(() => h.app.actionManager.executeAction(actionSendToBack));
  2341. expect(API.getUndoStack().length).toBe(2);
  2342. expect(API.getRedoStack().length).toBe(0);
  2343. assertSelectedElements([rect2]);
  2344. expect(h.elements).toEqual([
  2345. expect.objectContaining({ id: rect2.id }), // b "Zz"
  2346. expect.objectContaining({ id: rect1.id }), // a "a0"
  2347. expect.objectContaining({ id: rect3.id }), // c "a2"
  2348. ]);
  2349. // Simulate remote update
  2350. excalidrawAPI.updateScene({
  2351. elements: [
  2352. newElementWith(h.elements[2], { index: "Zy" as FractionalIndex }),
  2353. h.elements[0],
  2354. h.elements[1],
  2355. ],
  2356. storeAction: StoreAction.UPDATE,
  2357. });
  2358. expect(API.getUndoStack().length).toBe(2);
  2359. expect(API.getRedoStack().length).toBe(0);
  2360. assertSelectedElements([rect2]);
  2361. expect(h.elements).toEqual([
  2362. expect.objectContaining({ id: rect3.id }), // c "Zy"
  2363. expect.objectContaining({ id: rect2.id }), // b "Zz"
  2364. expect.objectContaining({ id: rect1.id }), // a "a0"
  2365. ]);
  2366. Keyboard.undo();
  2367. expect(API.getUndoStack().length).toBe(1);
  2368. expect(API.getRedoStack().length).toBe(1);
  2369. assertSelectedElements([rect2]);
  2370. expect(h.elements).toEqual([
  2371. expect.objectContaining({ id: rect3.id }), // c "Zy"
  2372. expect.objectContaining({ id: rect1.id }), // a "a0"
  2373. expect.objectContaining({ id: rect2.id }), // b "a1"
  2374. ]);
  2375. Keyboard.redo();
  2376. expect(API.getUndoStack().length).toBe(2);
  2377. expect(API.getRedoStack().length).toBe(0);
  2378. assertSelectedElements([rect2]);
  2379. expect(h.elements).toEqual([
  2380. expect.objectContaining({ id: rect3.id }), // c "Zy"
  2381. expect.objectContaining({ id: rect2.id }), // b "Zz"
  2382. expect.objectContaining({ id: rect1.id }), // a "a0"
  2383. ]);
  2384. // Simulate remote update
  2385. excalidrawAPI.updateScene({
  2386. elements: [
  2387. newElementWith(h.elements[2], { index: "Zx" as FractionalIndex }),
  2388. h.elements[0],
  2389. h.elements[1],
  2390. ],
  2391. storeAction: StoreAction.UPDATE,
  2392. });
  2393. expect(API.getUndoStack().length).toBe(2);
  2394. expect(API.getRedoStack().length).toBe(0);
  2395. assertSelectedElements([rect2]);
  2396. expect(h.elements).toEqual([
  2397. expect.objectContaining({ id: rect1.id }), // a "Zx"
  2398. expect.objectContaining({ id: rect3.id }), // c "Zy"
  2399. expect.objectContaining({ id: rect2.id }), // b "Zz"
  2400. ]);
  2401. Keyboard.undo();
  2402. // We iterated two steps as there was no change in order!
  2403. expect(API.getUndoStack().length).toBe(0);
  2404. expect(API.getRedoStack().length).toBe(2);
  2405. expect(API.getSelectedElements().length).toBe(0);
  2406. expect(h.elements).toEqual([
  2407. expect.objectContaining({ id: rect1.id }), // a "Zx"
  2408. expect.objectContaining({ id: rect3.id }), // c "Zy"
  2409. expect.objectContaining({ id: rect2.id }), // b "a1"
  2410. ]);
  2411. });
  2412. it("should iterate through the history when z-index changes do not produce visible change and we synced all indices", async () => {
  2413. const rect1 = API.createElement({ type: "rectangle", x: 10, y: 10 });
  2414. const rect2 = API.createElement({ type: "rectangle", x: 20, y: 20 });
  2415. const rect3 = API.createElement({ type: "rectangle", x: 30, y: 30 });
  2416. h.elements = [rect1, rect2, rect3];
  2417. mouse.select(rect2);
  2418. act(() => h.app.actionManager.executeAction(actionSendToBack));
  2419. expect(API.getUndoStack().length).toBe(2);
  2420. expect(API.getRedoStack().length).toBe(0);
  2421. assertSelectedElements([rect2]);
  2422. expect(h.elements).toEqual([
  2423. expect.objectContaining({ id: rect2.id }),
  2424. expect.objectContaining({ id: rect1.id }),
  2425. expect.objectContaining({ id: rect3.id }),
  2426. ]);
  2427. // Simulate remote update (fixes all invalid z-indices)
  2428. excalidrawAPI.updateScene({
  2429. elements: [
  2430. h.elements[2], // rect3
  2431. h.elements[0], // rect2
  2432. h.elements[1], // rect1
  2433. ],
  2434. storeAction: StoreAction.UPDATE,
  2435. });
  2436. Keyboard.undo();
  2437. expect(API.getUndoStack().length).toBe(1);
  2438. expect(API.getRedoStack().length).toBe(1);
  2439. assertSelectedElements([rect2]);
  2440. expect(h.elements).toEqual([
  2441. expect.objectContaining({ id: rect2.id }),
  2442. expect.objectContaining({ id: rect3.id }),
  2443. expect.objectContaining({ id: rect1.id }),
  2444. ]);
  2445. Keyboard.redo();
  2446. expect(API.getUndoStack().length).toBe(2);
  2447. expect(API.getRedoStack().length).toBe(0);
  2448. assertSelectedElements([rect2]);
  2449. expect(h.elements).toEqual([
  2450. expect.objectContaining({ id: rect3.id }),
  2451. expect.objectContaining({ id: rect2.id }),
  2452. expect.objectContaining({ id: rect1.id }),
  2453. ]);
  2454. // Simulate remote update
  2455. excalidrawAPI.updateScene({
  2456. elements: [
  2457. h.elements[1], // rect2
  2458. h.elements[0], // rect3
  2459. h.elements[2], // rect1
  2460. ],
  2461. storeAction: StoreAction.UPDATE,
  2462. });
  2463. Keyboard.undo();
  2464. expect(API.getUndoStack().length).toBe(0);
  2465. expect(API.getRedoStack().length).toBe(2); // now we iterated two steps back!
  2466. assertSelectedElements([]);
  2467. expect(h.elements).toEqual([
  2468. expect.objectContaining({ id: rect2.id }),
  2469. expect.objectContaining({ id: rect3.id }),
  2470. expect.objectContaining({ id: rect1.id }),
  2471. ]);
  2472. });
  2473. it("should not let remote changes to interfere with in progress freedraw", async () => {
  2474. UI.clickTool("freedraw");
  2475. mouse.down(10, 10);
  2476. mouse.moveTo(30, 30);
  2477. const rectProps = {
  2478. type: "rectangle",
  2479. strokeColor: blue,
  2480. } as const;
  2481. // Simulate remote update
  2482. const rect = API.createElement({ ...rectProps });
  2483. // Simulate remote update
  2484. excalidrawAPI.updateScene({
  2485. elements: [...h.elements, rect],
  2486. storeAction: StoreAction.UPDATE,
  2487. });
  2488. mouse.moveTo(60, 60);
  2489. mouse.up();
  2490. Keyboard.undo();
  2491. expect(API.getUndoStack().length).toBe(0);
  2492. expect(API.getRedoStack().length).toBe(1);
  2493. expect(h.elements).toEqual([
  2494. expect.objectContaining({
  2495. id: h.elements[0].id,
  2496. type: "freedraw",
  2497. isDeleted: true,
  2498. }),
  2499. expect.objectContaining(rectProps),
  2500. ]);
  2501. Keyboard.redo();
  2502. expect(API.getUndoStack().length).toBe(1);
  2503. expect(API.getRedoStack().length).toBe(0);
  2504. expect(h.elements).toEqual([
  2505. expect.objectContaining({
  2506. id: h.elements[0].id,
  2507. type: "freedraw",
  2508. isDeleted: false,
  2509. }),
  2510. expect.objectContaining(rectProps),
  2511. ]);
  2512. });
  2513. it("should not let remote changes to interfere with in progress resizing", async () => {
  2514. const props1 = { x: 10, y: 10, width: 10, height: 10 };
  2515. const rect1 = UI.createElement("rectangle", { ...props1 });
  2516. mouse.downAt(20, 20);
  2517. mouse.moveTo(40, 40);
  2518. assertSelectedElements(rect1);
  2519. expect(API.getUndoStack().length).toBe(1);
  2520. const rect3Props = {
  2521. type: "rectangle",
  2522. strokeColor: blue,
  2523. } as const;
  2524. const rect3 = API.createElement({ ...rect3Props });
  2525. // // Simulate remote update
  2526. excalidrawAPI.updateScene({
  2527. elements: [...h.elements, rect3],
  2528. storeAction: StoreAction.UPDATE,
  2529. });
  2530. mouse.moveTo(100, 100);
  2531. mouse.up();
  2532. expect(API.getUndoStack().length).toBe(2);
  2533. expect(API.getRedoStack().length).toBe(0);
  2534. assertSelectedElements(rect1);
  2535. expect(h.elements).toEqual([
  2536. expect.objectContaining({
  2537. id: rect1.id,
  2538. ...props1,
  2539. isDeleted: false,
  2540. width: 90,
  2541. height: 90,
  2542. }),
  2543. expect.objectContaining(rect3Props),
  2544. ]);
  2545. Keyboard.undo();
  2546. assertSelectedElements(rect1);
  2547. expect(h.elements).toEqual([
  2548. expect.objectContaining({
  2549. id: rect1.id,
  2550. ...props1,
  2551. isDeleted: false,
  2552. }),
  2553. expect.objectContaining(rect3Props),
  2554. ]);
  2555. Keyboard.undo();
  2556. expect(API.getSelectedElements()).toEqual([]);
  2557. expect(h.elements).toEqual([
  2558. expect.objectContaining({
  2559. id: rect1.id,
  2560. ...props1,
  2561. isDeleted: true,
  2562. }),
  2563. expect.objectContaining(rect3Props),
  2564. ]);
  2565. Keyboard.redo();
  2566. assertSelectedElements(rect1);
  2567. expect(h.elements).toEqual([
  2568. expect.objectContaining({
  2569. id: rect1.id,
  2570. ...props1,
  2571. isDeleted: false,
  2572. }),
  2573. expect.objectContaining(rect3Props),
  2574. ]);
  2575. Keyboard.redo();
  2576. expect(API.getUndoStack().length).toBe(2);
  2577. expect(API.getRedoStack().length).toBe(0);
  2578. assertSelectedElements(rect1);
  2579. expect(h.elements).toEqual([
  2580. expect.objectContaining({
  2581. id: rect1.id,
  2582. ...props1,
  2583. isDeleted: false,
  2584. width: 90,
  2585. height: 90,
  2586. }),
  2587. expect.objectContaining(rect3Props),
  2588. ]);
  2589. });
  2590. it("should not let remote changes to interfere with in progress dragging", async () => {
  2591. const rect1 = UI.createElement("rectangle", { x: 10, y: 10 });
  2592. const rect2 = UI.createElement("rectangle", { x: 30, y: 30 });
  2593. mouse.select([rect1, rect2]);
  2594. mouse.downAt(20, 20);
  2595. mouse.moveTo(50, 50);
  2596. assertSelectedElements(rect1, rect2);
  2597. expect(API.getUndoStack().length).toBe(4);
  2598. const rect3Props = {
  2599. type: "rectangle",
  2600. strokeColor: blue,
  2601. } as const;
  2602. const rect3 = API.createElement({ ...rect3Props });
  2603. // Simulate remote update
  2604. excalidrawAPI.updateScene({
  2605. elements: [...h.elements, rect3],
  2606. storeAction: StoreAction.UPDATE,
  2607. });
  2608. mouse.moveTo(100, 100);
  2609. mouse.up();
  2610. expect(API.getUndoStack().length).toBe(5);
  2611. expect(API.getRedoStack().length).toBe(0);
  2612. assertSelectedElements(rect1, rect2);
  2613. expect(h.elements).toEqual([
  2614. expect.objectContaining({
  2615. id: rect1.id,
  2616. x: 90,
  2617. y: 90,
  2618. isDeleted: false,
  2619. }),
  2620. expect.objectContaining({
  2621. id: rect2.id,
  2622. x: 110,
  2623. y: 110,
  2624. isDeleted: false,
  2625. }),
  2626. expect.objectContaining(rect3Props),
  2627. ]);
  2628. Keyboard.undo();
  2629. assertSelectedElements(rect1, rect2);
  2630. expect(h.elements).toEqual([
  2631. expect.objectContaining({
  2632. id: rect1.id,
  2633. x: 10,
  2634. y: 10,
  2635. isDeleted: false,
  2636. }),
  2637. expect.objectContaining({
  2638. id: rect2.id,
  2639. x: 30,
  2640. y: 30,
  2641. isDeleted: false,
  2642. }),
  2643. expect.objectContaining(rect3Props),
  2644. ]);
  2645. Keyboard.undo();
  2646. assertSelectedElements(rect1);
  2647. Keyboard.undo();
  2648. assertSelectedElements(rect2);
  2649. Keyboard.undo();
  2650. assertSelectedElements(rect1);
  2651. expect(h.elements).toEqual([
  2652. expect.objectContaining({
  2653. id: rect1.id,
  2654. x: 10,
  2655. y: 10,
  2656. isDeleted: false,
  2657. }),
  2658. expect.objectContaining({
  2659. id: rect2.id,
  2660. x: 30,
  2661. y: 30,
  2662. isDeleted: true,
  2663. }),
  2664. expect.objectContaining(rect3Props),
  2665. ]);
  2666. Keyboard.undo();
  2667. assertSelectedElements();
  2668. expect(h.elements).toEqual([
  2669. expect.objectContaining({
  2670. id: rect1.id,
  2671. x: 10,
  2672. y: 10,
  2673. isDeleted: true,
  2674. }),
  2675. expect.objectContaining({
  2676. id: rect2.id,
  2677. x: 30,
  2678. y: 30,
  2679. isDeleted: true,
  2680. }),
  2681. expect.objectContaining(rect3Props),
  2682. ]);
  2683. Keyboard.redo();
  2684. assertSelectedElements(rect1);
  2685. expect(h.elements).toEqual([
  2686. expect.objectContaining({
  2687. id: rect1.id,
  2688. x: 10,
  2689. y: 10,
  2690. isDeleted: false,
  2691. }),
  2692. expect.objectContaining({
  2693. id: rect2.id,
  2694. x: 30,
  2695. y: 30,
  2696. isDeleted: true,
  2697. }),
  2698. expect.objectContaining(rect3Props),
  2699. ]);
  2700. Keyboard.redo();
  2701. assertSelectedElements(rect2);
  2702. Keyboard.redo();
  2703. assertSelectedElements(rect1);
  2704. Keyboard.redo();
  2705. assertSelectedElements(rect1, rect2);
  2706. expect(h.elements).toEqual([
  2707. expect.objectContaining({
  2708. id: rect1.id,
  2709. x: 10,
  2710. y: 10,
  2711. isDeleted: false,
  2712. }),
  2713. expect.objectContaining({
  2714. id: rect2.id,
  2715. x: 30,
  2716. y: 30,
  2717. isDeleted: false,
  2718. }),
  2719. expect.objectContaining(rect3Props),
  2720. ]);
  2721. Keyboard.redo();
  2722. expect(API.getUndoStack().length).toBe(5);
  2723. expect(API.getRedoStack().length).toBe(0);
  2724. assertSelectedElements(rect1, rect2);
  2725. expect(h.elements).toEqual([
  2726. expect.objectContaining({
  2727. id: rect1.id,
  2728. x: 90,
  2729. y: 90,
  2730. isDeleted: false,
  2731. }),
  2732. expect.objectContaining({
  2733. id: rect2.id,
  2734. x: 110,
  2735. y: 110,
  2736. isDeleted: false,
  2737. }),
  2738. expect.objectContaining(rect3Props),
  2739. ]);
  2740. });
  2741. describe("conflicts in bound text elements and their containers", () => {
  2742. let container: ExcalidrawGenericElement;
  2743. let text: ExcalidrawTextElement;
  2744. const containerProps = {
  2745. type: "rectangle",
  2746. width: 100,
  2747. x: 10,
  2748. y: 10,
  2749. angle: 0,
  2750. } as const;
  2751. const textProps = {
  2752. type: "text",
  2753. text: "que pasa",
  2754. x: 15,
  2755. y: 15,
  2756. angle: 0,
  2757. } as const;
  2758. beforeEach(() => {
  2759. container = API.createElement({ ...containerProps });
  2760. text = API.createElement({ ...textProps });
  2761. });
  2762. it("should rebind bindings when both are updated through the history and there no conflicting updates in the meantime", async () => {
  2763. // Initialize the scene
  2764. excalidrawAPI.updateScene({
  2765. elements: [container, text],
  2766. storeAction: StoreAction.UPDATE,
  2767. });
  2768. // Simulate local update
  2769. excalidrawAPI.updateScene({
  2770. elements: [
  2771. newElementWith(h.elements[0], {
  2772. boundElements: [{ id: text.id, type: "text" }],
  2773. }),
  2774. newElementWith(h.elements[1] as ExcalidrawTextElement, {
  2775. containerId: container.id,
  2776. }),
  2777. ],
  2778. storeAction: StoreAction.CAPTURE,
  2779. });
  2780. Keyboard.undo();
  2781. expect(API.getUndoStack().length).toBe(0);
  2782. expect(API.getRedoStack().length).toBe(1);
  2783. expect(h.elements).toEqual([
  2784. expect.objectContaining({
  2785. id: container.id,
  2786. boundElements: [],
  2787. isDeleted: false,
  2788. }),
  2789. expect.objectContaining({
  2790. id: text.id,
  2791. containerId: null,
  2792. isDeleted: false,
  2793. }),
  2794. ]);
  2795. // Simulate remote update
  2796. excalidrawAPI.updateScene({
  2797. elements: [
  2798. newElementWith(h.elements[0], {
  2799. // no conflicting updates
  2800. x: h.elements[1].x + 20,
  2801. }),
  2802. newElementWith(h.elements[1] as ExcalidrawTextElement, {
  2803. // no conflicting updates
  2804. x: h.elements[1].x + 10,
  2805. }),
  2806. ],
  2807. storeAction: StoreAction.UPDATE,
  2808. });
  2809. runTwice(() => {
  2810. Keyboard.redo();
  2811. expect(API.getUndoStack().length).toBe(1);
  2812. expect(API.getRedoStack().length).toBe(0);
  2813. expect(h.elements).toEqual([
  2814. expect.objectContaining({
  2815. id: container.id,
  2816. boundElements: [{ id: text.id, type: "text" }],
  2817. isDeleted: false,
  2818. }),
  2819. expect.objectContaining({
  2820. id: text.id,
  2821. containerId: container.id,
  2822. isDeleted: false,
  2823. }),
  2824. ]);
  2825. Keyboard.undo();
  2826. expect(API.getUndoStack().length).toBe(0);
  2827. expect(API.getRedoStack().length).toBe(1);
  2828. expect(h.elements).toEqual([
  2829. expect.objectContaining({
  2830. id: container.id,
  2831. boundElements: [],
  2832. isDeleted: false,
  2833. }),
  2834. expect.objectContaining({
  2835. id: text.id,
  2836. containerId: null,
  2837. isDeleted: false,
  2838. }),
  2839. ]);
  2840. });
  2841. });
  2842. // TODO: #7348 we do rebind now, when we have bi-directional binding in history, to eliminate potential data-integrity issues, but we should consider not rebinding in the future
  2843. it("should rebind bindings when both are updated through the history and the container got bound to a different text in the meantime", async () => {
  2844. // Initialize the scene
  2845. excalidrawAPI.updateScene({
  2846. elements: [container, text],
  2847. storeAction: StoreAction.UPDATE,
  2848. });
  2849. // Simulate local update
  2850. excalidrawAPI.updateScene({
  2851. elements: [
  2852. newElementWith(h.elements[0], {
  2853. boundElements: [{ id: text.id, type: "text" }],
  2854. }),
  2855. newElementWith(h.elements[1] as ExcalidrawTextElement, {
  2856. containerId: container.id,
  2857. }),
  2858. ],
  2859. storeAction: StoreAction.CAPTURE,
  2860. });
  2861. Keyboard.undo();
  2862. expect(API.getUndoStack().length).toBe(0);
  2863. expect(API.getRedoStack().length).toBe(1);
  2864. expect(h.elements).toEqual([
  2865. expect.objectContaining({
  2866. id: container.id,
  2867. boundElements: [],
  2868. isDeleted: false,
  2869. }),
  2870. expect.objectContaining({
  2871. id: text.id,
  2872. containerId: null,
  2873. isDeleted: false,
  2874. }),
  2875. ]);
  2876. const remoteText = API.createElement({
  2877. type: "text",
  2878. text: "ola",
  2879. containerId: container.id,
  2880. });
  2881. // Simulate remote update
  2882. excalidrawAPI.updateScene({
  2883. elements: [
  2884. newElementWith(h.elements[0], {
  2885. boundElements: [{ id: remoteText.id, type: "text" }],
  2886. }),
  2887. remoteText,
  2888. h.elements[1],
  2889. ],
  2890. storeAction: StoreAction.UPDATE,
  2891. });
  2892. runTwice(() => {
  2893. Keyboard.redo();
  2894. expect(API.getUndoStack().length).toBe(1);
  2895. expect(API.getRedoStack().length).toBe(0);
  2896. expect(h.elements).toEqual([
  2897. expect.objectContaining({
  2898. id: container.id,
  2899. // last added was `text.id`, removing `remoteText.id`
  2900. boundElements: [{ id: text.id, type: "text" }],
  2901. isDeleted: false,
  2902. }),
  2903. expect.objectContaining({
  2904. id: remoteText.id,
  2905. // unbound as `remoteText.id` was removed
  2906. containerId: null,
  2907. isDeleted: false,
  2908. }),
  2909. expect.objectContaining({
  2910. id: text.id,
  2911. // rebound!
  2912. containerId: container.id,
  2913. isDeleted: false,
  2914. }),
  2915. ]);
  2916. Keyboard.undo();
  2917. expect(API.getUndoStack().length).toBe(0);
  2918. expect(API.getRedoStack().length).toBe(1);
  2919. expect(h.elements).toEqual([
  2920. expect.objectContaining({
  2921. id: container.id,
  2922. boundElements: [{ id: remoteText.id, type: "text" }],
  2923. isDeleted: false,
  2924. }),
  2925. expect.objectContaining({
  2926. id: remoteText.id,
  2927. containerId: container.id,
  2928. isDeleted: false,
  2929. }),
  2930. expect.objectContaining({
  2931. id: text.id,
  2932. containerId: null,
  2933. isDeleted: false,
  2934. }),
  2935. ]);
  2936. });
  2937. });
  2938. // TODO: #7348 we do rebind now, when we have bi-directional binding in history, to eliminate potential data-integrity issues, but we should consider not rebinding in the future
  2939. it("should rebind bindings when both are updated through the history and the text got bound to a different container in the meantime", async () => {
  2940. // Initialize the scene
  2941. excalidrawAPI.updateScene({
  2942. elements: [container, text],
  2943. storeAction: StoreAction.UPDATE,
  2944. });
  2945. // Simulate local update
  2946. excalidrawAPI.updateScene({
  2947. elements: [
  2948. newElementWith(h.elements[0], {
  2949. boundElements: [{ id: text.id, type: "text" }],
  2950. }),
  2951. newElementWith(h.elements[1] as ExcalidrawTextElement, {
  2952. containerId: container.id,
  2953. }),
  2954. ],
  2955. storeAction: StoreAction.CAPTURE,
  2956. });
  2957. Keyboard.undo();
  2958. expect(API.getUndoStack().length).toBe(0);
  2959. expect(API.getRedoStack().length).toBe(1);
  2960. expect(h.elements).toEqual([
  2961. expect.objectContaining({
  2962. id: container.id,
  2963. boundElements: [],
  2964. isDeleted: false,
  2965. }),
  2966. expect.objectContaining({
  2967. id: text.id,
  2968. containerId: null,
  2969. isDeleted: false,
  2970. }),
  2971. ]);
  2972. const remoteContainer = API.createElement({
  2973. type: "rectangle",
  2974. width: 50,
  2975. x: 100,
  2976. boundElements: [{ id: text.id, type: "text" }],
  2977. });
  2978. // Simulate remote update
  2979. excalidrawAPI.updateScene({
  2980. elements: [
  2981. h.elements[0],
  2982. newElementWith(remoteContainer, {
  2983. boundElements: [{ id: text.id, type: "text" }],
  2984. }),
  2985. newElementWith(h.elements[1] as ExcalidrawTextElement, {
  2986. containerId: remoteContainer.id,
  2987. }),
  2988. ],
  2989. storeAction: StoreAction.UPDATE,
  2990. });
  2991. runTwice(() => {
  2992. Keyboard.redo();
  2993. expect(API.getUndoStack().length).toBe(1);
  2994. expect(API.getRedoStack().length).toBe(0);
  2995. expect(h.elements).toEqual([
  2996. expect.objectContaining({
  2997. id: container.id,
  2998. // rebound the text as we captured the full bidirectional binding in history!
  2999. boundElements: [{ id: text.id, type: "text" }],
  3000. isDeleted: false,
  3001. }),
  3002. expect.objectContaining({
  3003. id: remoteContainer.id,
  3004. // previous binding got unbound, as text is no longer bound to this element
  3005. boundElements: [],
  3006. isDeleted: false,
  3007. }),
  3008. expect.objectContaining({
  3009. id: text.id,
  3010. // rebound!
  3011. containerId: container.id,
  3012. isDeleted: false,
  3013. }),
  3014. ]);
  3015. Keyboard.undo();
  3016. expect(API.getUndoStack().length).toBe(0);
  3017. expect(API.getRedoStack().length).toBe(1);
  3018. expect(h.elements).toEqual([
  3019. expect.objectContaining({
  3020. id: container.id,
  3021. // deleted binding (already during applyDelta)
  3022. boundElements: [],
  3023. isDeleted: false,
  3024. }),
  3025. expect.objectContaining({
  3026. id: remoteContainer.id,
  3027. // #2 due to restored binding in #1, we could rebind the remote container!
  3028. boundElements: [{ id: text.id, type: "text" }],
  3029. isDeleted: false,
  3030. }),
  3031. expect.objectContaining({
  3032. id: text.id,
  3033. // #1 due to applying latest changes to the history entries, we could restore this binding
  3034. containerId: remoteContainer.id,
  3035. isDeleted: false,
  3036. }),
  3037. ]);
  3038. });
  3039. });
  3040. it("should rebind remotely added bound text when it's container is added through the history", async () => {
  3041. // Simulate local update
  3042. excalidrawAPI.updateScene({
  3043. elements: [container],
  3044. storeAction: StoreAction.CAPTURE,
  3045. });
  3046. // Simulate remote update
  3047. excalidrawAPI.updateScene({
  3048. elements: [
  3049. newElementWith(h.elements[0], {
  3050. boundElements: [{ id: text.id, type: "text" }],
  3051. }),
  3052. newElementWith(text, { containerId: container.id }),
  3053. ],
  3054. storeAction: StoreAction.UPDATE,
  3055. });
  3056. runTwice(() => {
  3057. Keyboard.undo();
  3058. expect(API.getUndoStack().length).toBe(0);
  3059. expect(API.getRedoStack().length).toBe(1);
  3060. expect(h.elements).toEqual([
  3061. expect.objectContaining({
  3062. ...containerProps,
  3063. id: container.id,
  3064. // binding from deleted to non deleted is correct!
  3065. // so that we could restore the bindings on history actions (subsequent redo in this case)
  3066. boundElements: [{ id: text.id, type: "text" }],
  3067. isDeleted: true,
  3068. }),
  3069. expect.objectContaining({
  3070. ...textProps,
  3071. id: text.id,
  3072. // we trigger unbind - binding from non deleted to deleted cannot exist!
  3073. containerId: null,
  3074. isDeleted: false,
  3075. }),
  3076. ]);
  3077. Keyboard.redo();
  3078. expect(API.getUndoStack().length).toBe(1);
  3079. expect(API.getRedoStack().length).toBe(0);
  3080. expect(h.elements).toEqual([
  3081. expect.objectContaining({
  3082. ...containerProps,
  3083. id: container.id,
  3084. boundElements: [{ id: text.id, type: "text" }],
  3085. isDeleted: false,
  3086. }),
  3087. expect.objectContaining({
  3088. ...textProps,
  3089. id: text.id,
  3090. // we triggered rebind!
  3091. containerId: container.id,
  3092. isDeleted: false,
  3093. }),
  3094. ]);
  3095. });
  3096. });
  3097. it("should rebind remotely added container when it's bound text is added through the history", async () => {
  3098. // Simulate local update
  3099. excalidrawAPI.updateScene({
  3100. elements: [text],
  3101. storeAction: StoreAction.CAPTURE,
  3102. });
  3103. // Simulate remote update
  3104. excalidrawAPI.updateScene({
  3105. elements: [
  3106. newElementWith(container, {
  3107. boundElements: [{ id: text.id, type: "text" }],
  3108. }),
  3109. newElementWith(text, { containerId: container.id }),
  3110. ],
  3111. storeAction: StoreAction.UPDATE,
  3112. });
  3113. runTwice(() => {
  3114. Keyboard.undo();
  3115. expect(API.getUndoStack().length).toBe(0);
  3116. expect(API.getRedoStack().length).toBe(1);
  3117. expect(h.elements).toEqual([
  3118. expect.objectContaining({
  3119. ...containerProps,
  3120. id: container.id,
  3121. // we trigged unbind - bindings from non deleted to deleted cannot exist!
  3122. boundElements: [],
  3123. isDeleted: false,
  3124. }),
  3125. expect.objectContaining({
  3126. ...textProps,
  3127. // binding from deleted to non deleted is correct, so that we could restore the bindings on history actions
  3128. containerId: container.id,
  3129. id: text.id,
  3130. isDeleted: true,
  3131. }),
  3132. ]);
  3133. Keyboard.redo();
  3134. expect(API.getUndoStack().length).toBe(1);
  3135. expect(API.getRedoStack().length).toBe(0);
  3136. expect(h.elements).toEqual([
  3137. expect.objectContaining({
  3138. ...containerProps,
  3139. id: container.id,
  3140. // we triggered rebind!
  3141. boundElements: [{ id: text.id, type: "text" }],
  3142. isDeleted: false,
  3143. }),
  3144. expect.objectContaining({
  3145. ...textProps,
  3146. containerId: container.id,
  3147. id: text.id,
  3148. isDeleted: false,
  3149. }),
  3150. ]);
  3151. });
  3152. });
  3153. it("should preserve latest remotely added binding and unbind previous one when the container is added through the history", async () => {
  3154. // Simulate local update
  3155. excalidrawAPI.updateScene({
  3156. elements: [container],
  3157. storeAction: StoreAction.CAPTURE,
  3158. });
  3159. // Simulate remote update
  3160. excalidrawAPI.updateScene({
  3161. elements: [
  3162. newElementWith(h.elements[0], {
  3163. boundElements: [{ id: text.id, type: "text" }],
  3164. }),
  3165. newElementWith(text, {
  3166. containerId: container.id,
  3167. }),
  3168. ],
  3169. storeAction: StoreAction.UPDATE,
  3170. });
  3171. Keyboard.undo();
  3172. expect(API.getUndoStack().length).toBe(0);
  3173. expect(API.getRedoStack().length).toBe(1);
  3174. expect(h.elements).toEqual([
  3175. expect.objectContaining({
  3176. id: container.id,
  3177. boundElements: [{ id: text.id, type: "text" }],
  3178. isDeleted: true,
  3179. }),
  3180. expect.objectContaining({
  3181. id: text.id,
  3182. // unbound!
  3183. containerId: null,
  3184. isDeleted: false,
  3185. }),
  3186. ]);
  3187. const remoteText = API.createElement({
  3188. type: "text",
  3189. text: "ola",
  3190. containerId: container.id,
  3191. });
  3192. // Simulate remote update
  3193. excalidrawAPI.updateScene({
  3194. elements: [
  3195. newElementWith(h.elements[0], {
  3196. boundElements: [{ id: remoteText.id, type: "text" }],
  3197. isDeleted: false, // purposefully undeleting, mimicing concurrenct update
  3198. }),
  3199. h.elements[1],
  3200. // rebinding the container with a new text element!
  3201. remoteText,
  3202. ],
  3203. storeAction: StoreAction.UPDATE,
  3204. });
  3205. runTwice(() => {
  3206. Keyboard.redo();
  3207. expect(API.getUndoStack().length).toBe(1);
  3208. expect(API.getRedoStack().length).toBe(0);
  3209. expect(h.elements).toEqual([
  3210. expect.objectContaining({
  3211. id: container.id,
  3212. // previously bound text is preserved
  3213. // text bindings are not duplicated
  3214. boundElements: [{ id: remoteText.id, type: "text" }],
  3215. isDeleted: false,
  3216. }),
  3217. expect.objectContaining({
  3218. id: text.id,
  3219. // unbound
  3220. containerId: null,
  3221. isDeleted: false,
  3222. }),
  3223. expect.objectContaining({
  3224. id: remoteText.id,
  3225. // preserved existing binding!
  3226. containerId: container.id,
  3227. isDeleted: false,
  3228. }),
  3229. ]);
  3230. Keyboard.undo();
  3231. expect(API.getUndoStack().length).toBe(0);
  3232. expect(API.getRedoStack().length).toBe(1);
  3233. expect(h.elements).toEqual([
  3234. expect.objectContaining({
  3235. id: container.id,
  3236. boundElements: [{ id: remoteText.id, type: "text" }],
  3237. isDeleted: false, // isDeleted got remotely updated to false
  3238. }),
  3239. expect.objectContaining({
  3240. id: text.id,
  3241. containerId: null,
  3242. isDeleted: false,
  3243. }),
  3244. expect.objectContaining({
  3245. id: remoteText.id,
  3246. // unbound
  3247. containerId: container.id,
  3248. isDeleted: false,
  3249. }),
  3250. ]);
  3251. });
  3252. });
  3253. it("should preserve latest remotely added binding and unbind previous one when the text is added through history", async () => {
  3254. // Simulate local update
  3255. excalidrawAPI.updateScene({
  3256. elements: [text],
  3257. storeAction: StoreAction.CAPTURE,
  3258. });
  3259. // Simulate remote update
  3260. excalidrawAPI.updateScene({
  3261. elements: [
  3262. newElementWith(container, {
  3263. boundElements: [{ id: text.id, type: "text" }],
  3264. }),
  3265. newElementWith(h.elements[0] as ExcalidrawTextElement, {
  3266. containerId: container.id,
  3267. }),
  3268. ],
  3269. storeAction: StoreAction.UPDATE,
  3270. });
  3271. Keyboard.undo();
  3272. expect(API.getUndoStack().length).toBe(0);
  3273. expect(API.getRedoStack().length).toBe(1);
  3274. expect(h.elements).toEqual([
  3275. expect.objectContaining({
  3276. id: container.id,
  3277. // unbind affected bindable element
  3278. boundElements: [],
  3279. isDeleted: false,
  3280. }),
  3281. expect.objectContaining({
  3282. id: text.id,
  3283. containerId: container.id,
  3284. isDeleted: true,
  3285. }),
  3286. ]);
  3287. const remoteText = API.createElement({
  3288. type: "text",
  3289. text: "ola",
  3290. containerId: container.id,
  3291. });
  3292. // Simulate remote update
  3293. excalidrawAPI.updateScene({
  3294. elements: [
  3295. newElementWith(h.elements[0], {
  3296. boundElements: [{ id: remoteText.id, type: "text" }],
  3297. }),
  3298. h.elements[1],
  3299. newElementWith(remoteText as ExcalidrawTextElement, {
  3300. containerId: container.id,
  3301. }),
  3302. ],
  3303. storeAction: StoreAction.UPDATE,
  3304. });
  3305. runTwice(() => {
  3306. Keyboard.redo();
  3307. expect(API.getUndoStack().length).toBe(1);
  3308. expect(API.getRedoStack().length).toBe(0);
  3309. expect(h.elements).toEqual([
  3310. expect.objectContaining({
  3311. id: container.id,
  3312. // previously bound text is preserved
  3313. // text bindings are not duplicated
  3314. boundElements: [{ id: remoteText.id, type: "text" }],
  3315. isDeleted: false,
  3316. }),
  3317. expect.objectContaining({
  3318. id: text.id,
  3319. // unbound from container!
  3320. containerId: null,
  3321. isDeleted: false,
  3322. }),
  3323. expect.objectContaining({
  3324. id: remoteText.id,
  3325. // preserved existing binding!
  3326. containerId: container.id,
  3327. isDeleted: false,
  3328. }),
  3329. ]);
  3330. Keyboard.undo();
  3331. expect(API.getUndoStack().length).toBe(0);
  3332. expect(API.getRedoStack().length).toBe(1);
  3333. expect(h.elements).toEqual([
  3334. expect.objectContaining({
  3335. id: container.id,
  3336. boundElements: [{ id: remoteText.id, type: "text" }],
  3337. isDeleted: false,
  3338. }),
  3339. expect.objectContaining({
  3340. id: text.id,
  3341. containerId: container.id,
  3342. isDeleted: true,
  3343. }),
  3344. expect.objectContaining({
  3345. id: remoteText.id,
  3346. containerId: container.id,
  3347. isDeleted: false,
  3348. }),
  3349. ]);
  3350. });
  3351. });
  3352. it("should unbind remotely deleted bound text from container when the container is added through the history", async () => {
  3353. // Simulate local update
  3354. excalidrawAPI.updateScene({
  3355. elements: [container],
  3356. storeAction: StoreAction.CAPTURE,
  3357. });
  3358. // Simulate remote update
  3359. excalidrawAPI.updateScene({
  3360. elements: [
  3361. newElementWith(h.elements[0], {
  3362. boundElements: [{ id: text.id, type: "text" }],
  3363. }),
  3364. newElementWith(text, {
  3365. containerId: container.id,
  3366. isDeleted: true,
  3367. }),
  3368. ],
  3369. storeAction: StoreAction.UPDATE,
  3370. });
  3371. runTwice(() => {
  3372. Keyboard.undo();
  3373. expect(API.getUndoStack().length).toBe(0);
  3374. expect(API.getRedoStack().length).toBe(1);
  3375. expect(h.elements).toEqual([
  3376. expect.objectContaining({
  3377. id: container.id,
  3378. boundElements: [{ id: text.id, type: "text" }],
  3379. isDeleted: true,
  3380. }),
  3381. expect.objectContaining({
  3382. id: text.id,
  3383. containerId: container.id,
  3384. isDeleted: true,
  3385. }),
  3386. ]);
  3387. Keyboard.redo();
  3388. expect(API.getUndoStack().length).toBe(1);
  3389. expect(API.getRedoStack().length).toBe(0);
  3390. expect(h.elements).toEqual([
  3391. expect.objectContaining({
  3392. id: container.id,
  3393. // unbound!
  3394. boundElements: [],
  3395. isDeleted: false,
  3396. }),
  3397. expect.objectContaining({
  3398. id: text.id,
  3399. containerId: container.id,
  3400. isDeleted: true,
  3401. }),
  3402. ]);
  3403. });
  3404. });
  3405. it("should unbind remotely deleted container from bound text when the text is added through the history", async () => {
  3406. // Simulate local update
  3407. excalidrawAPI.updateScene({
  3408. elements: [text],
  3409. storeAction: StoreAction.CAPTURE,
  3410. });
  3411. // Simulate remote update
  3412. excalidrawAPI.updateScene({
  3413. elements: [
  3414. newElementWith(container, {
  3415. boundElements: [{ id: text.id, type: "text" }],
  3416. isDeleted: true,
  3417. }),
  3418. newElementWith(h.elements[0] as ExcalidrawTextElement, {
  3419. containerId: container.id,
  3420. }),
  3421. ],
  3422. storeAction: StoreAction.UPDATE,
  3423. });
  3424. runTwice(() => {
  3425. Keyboard.undo();
  3426. expect(API.getUndoStack().length).toBe(0);
  3427. expect(API.getRedoStack().length).toBe(1);
  3428. expect(h.elements).toEqual([
  3429. expect.objectContaining({
  3430. id: container.id,
  3431. boundElements: [{ id: text.id, type: "text" }],
  3432. isDeleted: true,
  3433. }),
  3434. expect.objectContaining({
  3435. id: text.id,
  3436. containerId: container.id,
  3437. isDeleted: true,
  3438. }),
  3439. ]);
  3440. Keyboard.redo();
  3441. expect(API.getUndoStack().length).toBe(1);
  3442. expect(API.getRedoStack().length).toBe(0);
  3443. expect(h.elements).toEqual([
  3444. expect.objectContaining({
  3445. id: container.id,
  3446. boundElements: [{ id: text.id, type: "text" }],
  3447. isDeleted: true,
  3448. }),
  3449. expect.objectContaining({
  3450. id: text.id,
  3451. // unbound!
  3452. containerId: null,
  3453. isDeleted: false,
  3454. }),
  3455. ]);
  3456. });
  3457. });
  3458. it("should redraw remotely added bound text when it's container is updated through the history", async () => {
  3459. // Initialize the scene
  3460. excalidrawAPI.updateScene({
  3461. elements: [container],
  3462. storeAction: StoreAction.UPDATE,
  3463. });
  3464. // Simulate local update
  3465. excalidrawAPI.updateScene({
  3466. elements: [
  3467. newElementWith(h.elements[0], {
  3468. x: 200,
  3469. y: 200,
  3470. angle: 90,
  3471. }),
  3472. ],
  3473. storeAction: StoreAction.CAPTURE,
  3474. });
  3475. Keyboard.undo();
  3476. // Simulate remote update
  3477. excalidrawAPI.updateScene({
  3478. elements: [
  3479. newElementWith(h.elements[0], {
  3480. boundElements: [{ id: text.id, type: "text" }],
  3481. }),
  3482. newElementWith(text, { containerId: container.id }),
  3483. ],
  3484. storeAction: StoreAction.UPDATE,
  3485. });
  3486. expect(h.elements).toEqual([
  3487. expect.objectContaining({
  3488. ...containerProps,
  3489. id: container.id,
  3490. boundElements: [{ id: text.id, type: "text" }],
  3491. isDeleted: false,
  3492. }),
  3493. expect.objectContaining({
  3494. ...textProps,
  3495. id: text.id,
  3496. containerId: container.id,
  3497. isDeleted: false,
  3498. }),
  3499. ]);
  3500. Keyboard.redo();
  3501. expect(API.getUndoStack().length).toBe(1);
  3502. expect(API.getRedoStack().length).toBe(0);
  3503. expect(h.elements).toEqual([
  3504. expect.objectContaining({
  3505. ...containerProps,
  3506. x: 200,
  3507. y: 200,
  3508. angle: 90,
  3509. id: container.id,
  3510. boundElements: [{ id: text.id, type: "text" }],
  3511. isDeleted: false,
  3512. }),
  3513. expect.objectContaining({
  3514. ...textProps,
  3515. // text element got redrawn!
  3516. x: 205,
  3517. y: 205,
  3518. angle: 90,
  3519. id: text.id,
  3520. containerId: container.id,
  3521. isDeleted: false,
  3522. }),
  3523. ]);
  3524. Keyboard.undo();
  3525. expect(API.getUndoStack().length).toBe(0);
  3526. expect(API.getRedoStack().length).toBe(1);
  3527. // both elements got redrawn!
  3528. expect(h.elements).toEqual([
  3529. expect.objectContaining({
  3530. ...containerProps,
  3531. id: container.id,
  3532. boundElements: [{ id: text.id, type: "text" }],
  3533. isDeleted: false,
  3534. }),
  3535. expect.objectContaining({
  3536. ...textProps,
  3537. id: text.id,
  3538. containerId: container.id,
  3539. isDeleted: false,
  3540. }),
  3541. ]);
  3542. Keyboard.redo();
  3543. expect(API.getUndoStack().length).toBe(1);
  3544. expect(API.getRedoStack().length).toBe(0);
  3545. // both elements got redrawn!
  3546. expect(h.elements).toEqual([
  3547. expect.objectContaining({
  3548. ...containerProps,
  3549. x: 200,
  3550. y: 200,
  3551. angle: 90,
  3552. id: container.id,
  3553. boundElements: [{ id: text.id, type: "text" }],
  3554. isDeleted: false,
  3555. }),
  3556. expect.objectContaining({
  3557. ...textProps,
  3558. x: 205,
  3559. y: 205,
  3560. angle: 90,
  3561. id: text.id,
  3562. containerId: container.id,
  3563. isDeleted: false,
  3564. }),
  3565. ]);
  3566. });
  3567. // TODO: #7348 this leads to empty undo/redo and could be confusing - instead we might consider redrawing container based on the text dimensions
  3568. it("should redraw bound text to match container dimensions when the bound text is updated through the history", async () => {
  3569. // Initialize the scene
  3570. excalidrawAPI.updateScene({
  3571. elements: [text],
  3572. storeAction: StoreAction.UPDATE,
  3573. });
  3574. // Simulate local update
  3575. excalidrawAPI.updateScene({
  3576. elements: [
  3577. newElementWith(h.elements[0], {
  3578. x: 205,
  3579. y: 205,
  3580. angle: 90,
  3581. }),
  3582. ],
  3583. storeAction: StoreAction.CAPTURE,
  3584. });
  3585. Keyboard.undo();
  3586. // Simulate remote update
  3587. excalidrawAPI.updateScene({
  3588. elements: [
  3589. newElementWith(container, {
  3590. boundElements: [{ id: text.id, type: "text" }],
  3591. }),
  3592. newElementWith(h.elements[0] as ExcalidrawTextElement, {
  3593. containerId: container.id,
  3594. }),
  3595. ],
  3596. storeAction: StoreAction.UPDATE,
  3597. });
  3598. expect(API.getUndoStack().length).toBe(0);
  3599. expect(API.getRedoStack().length).toBe(1);
  3600. expect(h.elements).toEqual([
  3601. expect.objectContaining({
  3602. ...containerProps,
  3603. id: container.id,
  3604. boundElements: [{ id: text.id, type: "text" }],
  3605. isDeleted: false,
  3606. }),
  3607. expect.objectContaining({
  3608. ...textProps,
  3609. id: text.id,
  3610. containerId: container.id,
  3611. isDeleted: false,
  3612. }),
  3613. ]);
  3614. Keyboard.redo();
  3615. expect(API.getUndoStack().length).toBe(1);
  3616. expect(API.getRedoStack().length).toBe(0);
  3617. expect(h.elements).toEqual([
  3618. expect.objectContaining({
  3619. ...containerProps,
  3620. id: container.id,
  3621. boundElements: [{ id: text.id, type: "text" }],
  3622. isDeleted: false,
  3623. }),
  3624. expect.objectContaining({
  3625. ...textProps,
  3626. // bound text got redrawn, as redraw is triggered based on container positon!
  3627. id: text.id,
  3628. containerId: container.id,
  3629. isDeleted: false,
  3630. }),
  3631. ]);
  3632. Keyboard.undo();
  3633. expect(API.getUndoStack().length).toBe(0);
  3634. expect(API.getRedoStack().length).toBe(1);
  3635. // both elements got redrawn!
  3636. expect(h.elements).toEqual([
  3637. expect.objectContaining({
  3638. ...containerProps,
  3639. id: container.id,
  3640. boundElements: [{ id: text.id, type: "text" }],
  3641. isDeleted: false,
  3642. }),
  3643. expect.objectContaining({
  3644. ...textProps,
  3645. id: text.id,
  3646. containerId: container.id,
  3647. isDeleted: false,
  3648. }),
  3649. ]);
  3650. });
  3651. });
  3652. describe("conflicts in arrows and their bindable elements", () => {
  3653. let rect1: ExcalidrawGenericElement;
  3654. let rect2: ExcalidrawGenericElement;
  3655. const rect1Props = {
  3656. type: "rectangle",
  3657. height: 100,
  3658. width: 100,
  3659. x: -100,
  3660. y: -50,
  3661. } as const;
  3662. const rect2Props = {
  3663. type: "rectangle",
  3664. height: 100,
  3665. width: 100,
  3666. x: 100,
  3667. y: -50,
  3668. } as const;
  3669. function roundToNearestHundred(number: number) {
  3670. return Math.round(number / 100) * 100;
  3671. }
  3672. beforeEach(() => {
  3673. rect1 = API.createElement({ ...rect1Props });
  3674. rect2 = API.createElement({ ...rect2Props });
  3675. // Simulate local update
  3676. excalidrawAPI.updateScene({
  3677. elements: [rect1, rect2],
  3678. storeAction: StoreAction.CAPTURE,
  3679. });
  3680. mouse.reset();
  3681. });
  3682. it("should rebind bindings when both are updated through the history and there are no conflicting updates in the meantime", async () => {
  3683. // create arrow without bindings
  3684. Keyboard.withModifierKeys({ ctrl: true }, () => {
  3685. UI.clickTool("arrow");
  3686. mouse.down(0, 0);
  3687. mouse.up(100, 0);
  3688. });
  3689. const arrowId = h.elements[2].id;
  3690. // create start binding
  3691. mouse.downAt(0, 0);
  3692. mouse.moveTo(0, 1);
  3693. mouse.moveTo(0, 0);
  3694. mouse.up();
  3695. // create end binding
  3696. mouse.downAt(100, 0);
  3697. mouse.moveTo(100, 1);
  3698. mouse.moveTo(100, 0);
  3699. mouse.up();
  3700. expect(h.elements).toEqual([
  3701. expect.objectContaining({
  3702. id: rect1.id,
  3703. boundElements: [{ id: arrowId, type: "arrow" }],
  3704. }),
  3705. expect.objectContaining({
  3706. id: rect2.id,
  3707. boundElements: [{ id: arrowId, type: "arrow" }],
  3708. }),
  3709. expect.objectContaining({
  3710. id: arrowId,
  3711. startBinding: {
  3712. elementId: rect1.id,
  3713. focus: expect.toBeNonNaNNumber(),
  3714. gap: expect.toBeNonNaNNumber(),
  3715. },
  3716. endBinding: {
  3717. elementId: rect2.id,
  3718. focus: expect.toBeNonNaNNumber(),
  3719. gap: expect.toBeNonNaNNumber(),
  3720. },
  3721. }),
  3722. ]);
  3723. Keyboard.undo(); // undo start binding
  3724. Keyboard.undo(); // undo end binding
  3725. expect(API.getUndoStack().length).toBe(2);
  3726. expect(API.getRedoStack().length).toBe(2);
  3727. expect(h.elements).toEqual([
  3728. expect.objectContaining({
  3729. id: rect1.id,
  3730. boundElements: [],
  3731. }),
  3732. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  3733. expect.objectContaining({
  3734. id: arrowId,
  3735. startBinding: null,
  3736. endBinding: null,
  3737. }),
  3738. ]);
  3739. // Simulate remote update
  3740. excalidrawAPI.updateScene({
  3741. elements: [
  3742. newElementWith(h.elements[0], {
  3743. // no conflicting updates
  3744. x: h.elements[1].x + 50,
  3745. }),
  3746. newElementWith(h.elements[1], {
  3747. // no conflicting updates
  3748. x: h.elements[1].x + 50,
  3749. }),
  3750. newElementWith(h.elements[2], {
  3751. // no conflicting updates
  3752. x: h.elements[1].x + 50,
  3753. }),
  3754. ],
  3755. storeAction: StoreAction.UPDATE,
  3756. });
  3757. runTwice(() => {
  3758. Keyboard.redo();
  3759. Keyboard.redo();
  3760. expect(API.getUndoStack().length).toBe(4);
  3761. expect(API.getRedoStack().length).toBe(0);
  3762. expect(h.elements).toEqual([
  3763. expect.objectContaining({
  3764. id: rect1.id,
  3765. boundElements: [{ id: arrowId, type: "arrow" }],
  3766. }),
  3767. expect.objectContaining({
  3768. id: rect2.id,
  3769. boundElements: [{ id: arrowId, type: "arrow" }],
  3770. }),
  3771. expect.objectContaining({
  3772. id: arrowId,
  3773. startBinding: {
  3774. elementId: rect1.id,
  3775. focus: expect.toBeNonNaNNumber(),
  3776. gap: expect.toBeNonNaNNumber(),
  3777. },
  3778. endBinding: {
  3779. elementId: rect2.id,
  3780. focus: expect.toBeNonNaNNumber(),
  3781. gap: expect.toBeNonNaNNumber(),
  3782. },
  3783. }),
  3784. ]);
  3785. Keyboard.undo();
  3786. Keyboard.undo();
  3787. expect(API.getUndoStack().length).toBe(2);
  3788. expect(API.getRedoStack().length).toBe(2);
  3789. expect(h.elements).toEqual([
  3790. expect.objectContaining({
  3791. id: rect1.id,
  3792. boundElements: [],
  3793. }),
  3794. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  3795. expect.objectContaining({
  3796. id: arrowId,
  3797. startBinding: null,
  3798. endBinding: null,
  3799. }),
  3800. ]);
  3801. });
  3802. });
  3803. it("should rebind bindings when both are updated through the history and the arrow got bound to a different element in the meantime", async () => {
  3804. // create arrow without bindings
  3805. Keyboard.withModifierKeys({ ctrl: true }, () => {
  3806. UI.clickTool("arrow");
  3807. mouse.down(0, 0);
  3808. mouse.up(100, 0);
  3809. });
  3810. const arrowId = h.elements[2].id;
  3811. // create start binding
  3812. mouse.downAt(0, 0);
  3813. mouse.moveTo(0, 1);
  3814. mouse.upAt(0, 0);
  3815. // create end binding
  3816. mouse.downAt(100, 0);
  3817. mouse.moveTo(100, 1);
  3818. mouse.upAt(100, 0);
  3819. expect(h.elements).toEqual([
  3820. expect.objectContaining({
  3821. id: rect1.id,
  3822. boundElements: [{ id: arrowId, type: "arrow" }],
  3823. }),
  3824. expect.objectContaining({
  3825. id: rect2.id,
  3826. boundElements: [{ id: arrowId, type: "arrow" }],
  3827. }),
  3828. expect.objectContaining({
  3829. id: arrowId,
  3830. startBinding: {
  3831. elementId: rect1.id,
  3832. focus: expect.toBeNonNaNNumber(),
  3833. gap: expect.toBeNonNaNNumber(),
  3834. },
  3835. endBinding: {
  3836. elementId: rect2.id,
  3837. focus: expect.toBeNonNaNNumber(),
  3838. gap: expect.toBeNonNaNNumber(),
  3839. },
  3840. }),
  3841. ]);
  3842. Keyboard.undo();
  3843. Keyboard.undo();
  3844. expect(API.getUndoStack().length).toBe(2);
  3845. expect(API.getRedoStack().length).toBe(2);
  3846. expect(h.elements).toEqual([
  3847. expect.objectContaining({
  3848. id: rect1.id,
  3849. boundElements: [],
  3850. }),
  3851. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  3852. expect.objectContaining({
  3853. id: arrowId,
  3854. startBinding: null,
  3855. endBinding: null,
  3856. }),
  3857. ]);
  3858. const remoteContainer = API.createElement({
  3859. type: "rectangle",
  3860. width: 50,
  3861. x: 100,
  3862. boundElements: [{ id: arrowId, type: "arrow" }],
  3863. });
  3864. // Simulate remote update
  3865. excalidrawAPI.updateScene({
  3866. elements: [
  3867. h.elements[0],
  3868. newElementWith(h.elements[1], { boundElements: [] }),
  3869. newElementWith(h.elements[2] as ExcalidrawLinearElement, {
  3870. endBinding: { elementId: remoteContainer.id, gap: 1, focus: 0 },
  3871. }),
  3872. remoteContainer,
  3873. ],
  3874. storeAction: StoreAction.UPDATE,
  3875. });
  3876. runTwice(() => {
  3877. Keyboard.redo();
  3878. Keyboard.redo();
  3879. expect(API.getUndoStack().length).toBe(4);
  3880. expect(API.getRedoStack().length).toBe(0);
  3881. expect(h.elements).toEqual([
  3882. expect.objectContaining({
  3883. id: rect1.id,
  3884. boundElements: [{ id: arrowId, type: "arrow" }],
  3885. }),
  3886. expect.objectContaining({
  3887. id: rect2.id,
  3888. boundElements: [{ id: arrowId, type: "arrow" }],
  3889. }),
  3890. expect.objectContaining({
  3891. id: arrowId,
  3892. startBinding: {
  3893. elementId: rect1.id,
  3894. focus: expect.toBeNonNaNNumber(),
  3895. gap: expect.toBeNonNaNNumber(),
  3896. },
  3897. // rebound with previous rectangle
  3898. endBinding: {
  3899. elementId: rect2.id,
  3900. focus: expect.toBeNonNaNNumber(),
  3901. gap: expect.toBeNonNaNNumber(),
  3902. },
  3903. }),
  3904. expect.objectContaining({
  3905. id: remoteContainer.id,
  3906. boundElements: [],
  3907. }),
  3908. ]);
  3909. Keyboard.undo();
  3910. Keyboard.undo();
  3911. expect(API.getUndoStack().length).toBe(2);
  3912. expect(API.getRedoStack().length).toBe(2);
  3913. expect(h.elements).toEqual([
  3914. expect.objectContaining({
  3915. id: rect1.id,
  3916. boundElements: [],
  3917. }),
  3918. expect.objectContaining({
  3919. id: rect2.id,
  3920. boundElements: [],
  3921. }),
  3922. expect.objectContaining({
  3923. id: arrowId,
  3924. startBinding: null,
  3925. endBinding: {
  3926. // now we are back in the previous state!
  3927. elementId: remoteContainer.id,
  3928. focus: expect.toBeNonNaNNumber(),
  3929. gap: expect.toBeNonNaNNumber(),
  3930. },
  3931. }),
  3932. expect.objectContaining({
  3933. id: remoteContainer.id,
  3934. // leaving as bound until we can rebind arrows!
  3935. boundElements: [{ id: arrowId, type: "arrow" }],
  3936. }),
  3937. ]);
  3938. });
  3939. });
  3940. it("should rebind remotely added arrow when it's bindable elements are added through the history", async () => {
  3941. const arrow = API.createElement({
  3942. type: "arrow",
  3943. startBinding: { elementId: rect1.id, gap: 1, focus: 0 },
  3944. endBinding: { elementId: rect2.id, gap: 1, focus: 0 },
  3945. });
  3946. // Simulate remote update
  3947. excalidrawAPI.updateScene({
  3948. elements: [
  3949. arrow,
  3950. newElementWith(h.elements[0], {
  3951. boundElements: [{ id: arrow.id, type: "arrow" }],
  3952. }),
  3953. newElementWith(h.elements[1], {
  3954. boundElements: [{ id: arrow.id, type: "arrow" }],
  3955. }),
  3956. ],
  3957. storeAction: StoreAction.UPDATE,
  3958. });
  3959. runTwice(() => {
  3960. Keyboard.undo();
  3961. expect(API.getUndoStack().length).toBe(0);
  3962. expect(API.getRedoStack().length).toBe(1);
  3963. expect(h.elements).toEqual([
  3964. expect.objectContaining({
  3965. id: arrow.id,
  3966. startBinding: null,
  3967. endBinding: null,
  3968. }),
  3969. expect.objectContaining({
  3970. id: rect1.id,
  3971. boundElements: [{ id: arrow.id, type: "arrow" }],
  3972. isDeleted: true,
  3973. }),
  3974. expect.objectContaining({
  3975. id: rect2.id,
  3976. boundElements: [{ id: arrow.id, type: "arrow" }],
  3977. isDeleted: true,
  3978. }),
  3979. ]);
  3980. Keyboard.redo();
  3981. expect(API.getUndoStack().length).toBe(1);
  3982. expect(API.getRedoStack().length).toBe(0);
  3983. expect(h.elements).toEqual([
  3984. expect.objectContaining({
  3985. id: arrow.id,
  3986. startBinding: {
  3987. // now we are back in the previous state!
  3988. elementId: rect1.id,
  3989. focus: expect.toBeNonNaNNumber(),
  3990. gap: expect.toBeNonNaNNumber(),
  3991. },
  3992. endBinding: {
  3993. // now we are back in the previous state!
  3994. elementId: rect2.id,
  3995. focus: expect.toBeNonNaNNumber(),
  3996. gap: expect.toBeNonNaNNumber(),
  3997. },
  3998. }),
  3999. expect.objectContaining({
  4000. id: rect1.id,
  4001. boundElements: [{ id: arrow.id, type: "arrow" }],
  4002. isDeleted: false,
  4003. }),
  4004. expect.objectContaining({
  4005. id: rect2.id,
  4006. boundElements: [{ id: arrow.id, type: "arrow" }],
  4007. isDeleted: false,
  4008. }),
  4009. ]);
  4010. });
  4011. });
  4012. it("should rebind remotely added bindable elements when it's arrow is added through the history", async () => {
  4013. Keyboard.undo();
  4014. const arrow = API.createElement({
  4015. type: "arrow",
  4016. });
  4017. // Simulate local update
  4018. excalidrawAPI.updateScene({
  4019. elements: [arrow],
  4020. storeAction: StoreAction.CAPTURE,
  4021. });
  4022. // Simulate remote update
  4023. excalidrawAPI.updateScene({
  4024. elements: [
  4025. newElementWith(h.elements[0] as ExcalidrawLinearElement, {
  4026. startBinding: { elementId: rect1.id, gap: 1, focus: 0 },
  4027. endBinding: { elementId: rect2.id, gap: 1, focus: 0 },
  4028. }),
  4029. newElementWith(rect1, {
  4030. boundElements: [{ id: arrow.id, type: "arrow" }],
  4031. }),
  4032. newElementWith(rect2, {
  4033. boundElements: [{ id: arrow.id, type: "arrow" }],
  4034. }),
  4035. ],
  4036. storeAction: StoreAction.UPDATE,
  4037. });
  4038. runTwice(() => {
  4039. Keyboard.undo();
  4040. expect(API.getUndoStack().length).toBe(0);
  4041. expect(API.getRedoStack().length).toBe(1);
  4042. expect(h.elements).toEqual([
  4043. expect.objectContaining({
  4044. id: arrow.id,
  4045. startBinding: {
  4046. elementId: rect1.id,
  4047. focus: expect.toBeNonNaNNumber(),
  4048. gap: expect.toBeNonNaNNumber(),
  4049. },
  4050. endBinding: {
  4051. elementId: rect2.id,
  4052. focus: expect.toBeNonNaNNumber(),
  4053. gap: expect.toBeNonNaNNumber(),
  4054. },
  4055. isDeleted: true,
  4056. }),
  4057. expect.objectContaining({
  4058. id: rect1.id,
  4059. boundElements: [],
  4060. isDeleted: false,
  4061. }),
  4062. expect.objectContaining({
  4063. id: rect2.id,
  4064. boundElements: [],
  4065. isDeleted: false,
  4066. }),
  4067. ]);
  4068. Keyboard.redo();
  4069. expect(API.getUndoStack().length).toBe(1);
  4070. expect(API.getRedoStack().length).toBe(0);
  4071. expect(h.elements).toEqual([
  4072. expect.objectContaining({
  4073. id: arrow.id,
  4074. startBinding: {
  4075. elementId: rect1.id,
  4076. focus: expect.toBeNonNaNNumber(),
  4077. gap: expect.toBeNonNaNNumber(),
  4078. },
  4079. endBinding: {
  4080. elementId: rect2.id,
  4081. focus: expect.toBeNonNaNNumber(),
  4082. gap: expect.toBeNonNaNNumber(),
  4083. },
  4084. isDeleted: false,
  4085. }),
  4086. expect.objectContaining({
  4087. id: rect1.id,
  4088. boundElements: [{ id: arrow.id, type: "arrow" }],
  4089. isDeleted: false,
  4090. }),
  4091. expect.objectContaining({
  4092. id: rect2.id,
  4093. boundElements: [{ id: arrow.id, type: "arrow" }],
  4094. isDeleted: false,
  4095. }),
  4096. ]);
  4097. });
  4098. });
  4099. it("should unbind remotely deleted bindable elements from arrow when the arrow is added through the history", async () => {});
  4100. it("should update bound element points when rectangle was remotely moved and arrow is added back through the history", async () => {
  4101. // bind arrow to rect1 and rect2
  4102. UI.clickTool("arrow");
  4103. mouse.down(0, 0);
  4104. mouse.up(100, 0);
  4105. const arrowId = h.elements[2].id;
  4106. Keyboard.undo();
  4107. expect(API.getUndoStack().length).toBe(1);
  4108. expect(API.getRedoStack().length).toBe(1);
  4109. expect(h.elements).toEqual([
  4110. expect.objectContaining({
  4111. id: rect1.id,
  4112. boundElements: [],
  4113. }),
  4114. expect.objectContaining({ id: rect2.id, boundElements: [] }),
  4115. expect.objectContaining({
  4116. id: arrowId,
  4117. points: [
  4118. [0, 0],
  4119. [100, 0],
  4120. ],
  4121. startBinding: {
  4122. elementId: rect1.id,
  4123. focus: expect.toBeNonNaNNumber(),
  4124. gap: expect.toBeNonNaNNumber(),
  4125. },
  4126. endBinding: {
  4127. elementId: rect2.id,
  4128. focus: expect.toBeNonNaNNumber(),
  4129. gap: expect.toBeNonNaNNumber(),
  4130. },
  4131. isDeleted: true,
  4132. }),
  4133. ]);
  4134. // Simulate remote update
  4135. excalidrawAPI.updateScene({
  4136. elements: [
  4137. h.elements[0],
  4138. newElementWith(h.elements[1], { x: 500, y: -500 }),
  4139. h.elements[2],
  4140. ],
  4141. storeAction: StoreAction.UPDATE,
  4142. });
  4143. Keyboard.redo();
  4144. expect(API.getUndoStack().length).toBe(2);
  4145. expect(API.getRedoStack().length).toBe(0);
  4146. {
  4147. // no need to be strict about points, hence the rounding
  4148. const points = (h.elements[2] as ExcalidrawLinearElement).points[1];
  4149. expect([
  4150. roundToNearestHundred(points[0]),
  4151. roundToNearestHundred(points[1]),
  4152. ]).toEqual([500, -400]);
  4153. }
  4154. expect(h.elements).toEqual([
  4155. expect.objectContaining({
  4156. id: rect1.id,
  4157. boundElements: [{ id: arrowId, type: "arrow" }],
  4158. }),
  4159. expect.objectContaining({
  4160. id: rect2.id,
  4161. boundElements: [{ id: arrowId, type: "arrow" }],
  4162. }),
  4163. expect.objectContaining({
  4164. id: arrowId,
  4165. startBinding: {
  4166. elementId: rect1.id,
  4167. focus: expect.toBeNonNaNNumber(),
  4168. gap: expect.toBeNonNaNNumber(),
  4169. },
  4170. endBinding: {
  4171. elementId: rect2.id,
  4172. focus: expect.toBeNonNaNNumber(),
  4173. gap: expect.toBeNonNaNNumber(),
  4174. },
  4175. isDeleted: false,
  4176. }),
  4177. ]);
  4178. });
  4179. });
  4180. describe("conflicts in frames and their children", () => {
  4181. let frame: ExcalidrawFrameElement;
  4182. let rect: ExcalidrawGenericElement;
  4183. const frameProps = {
  4184. type: "frame",
  4185. x: 0,
  4186. width: 500,
  4187. } as const;
  4188. const rectProps = {
  4189. type: "rectangle",
  4190. width: 100,
  4191. x: 10,
  4192. y: 10,
  4193. angle: 0,
  4194. } as const;
  4195. beforeEach(() => {
  4196. frame = API.createElement({ ...frameProps });
  4197. rect = API.createElement({ ...rectProps });
  4198. });
  4199. it("should not rebind frame child with frame when frame was remotely deleted and frame child is added back through the history ", async () => {
  4200. // Initialize the scene
  4201. excalidrawAPI.updateScene({
  4202. elements: [frame],
  4203. storeAction: StoreAction.UPDATE,
  4204. });
  4205. // Simulate local update
  4206. excalidrawAPI.updateScene({
  4207. elements: [rect, h.elements[0]],
  4208. storeAction: StoreAction.CAPTURE,
  4209. });
  4210. // Simulate local update
  4211. excalidrawAPI.updateScene({
  4212. elements: [
  4213. newElementWith(h.elements[0], {
  4214. frameId: frame.id,
  4215. }),
  4216. h.elements[1],
  4217. ],
  4218. storeAction: StoreAction.CAPTURE,
  4219. });
  4220. Keyboard.undo();
  4221. expect(API.getUndoStack().length).toBe(1);
  4222. expect(API.getRedoStack().length).toBe(1);
  4223. expect(h.elements).toEqual([
  4224. expect.objectContaining({
  4225. id: rect.id,
  4226. frameId: null,
  4227. isDeleted: false,
  4228. }),
  4229. expect.objectContaining({
  4230. id: frame.id,
  4231. isDeleted: false,
  4232. }),
  4233. ]);
  4234. Keyboard.redo();
  4235. expect(API.getUndoStack().length).toBe(2);
  4236. expect(API.getRedoStack().length).toBe(0);
  4237. expect(h.elements).toEqual([
  4238. expect.objectContaining({
  4239. id: rect.id,
  4240. frameId: frame.id, // double check that the element is rebound
  4241. isDeleted: false,
  4242. }),
  4243. expect.objectContaining({
  4244. id: frame.id,
  4245. isDeleted: false,
  4246. }),
  4247. ]);
  4248. Keyboard.undo();
  4249. Keyboard.undo();
  4250. // Simulate remote update
  4251. excalidrawAPI.updateScene({
  4252. elements: [
  4253. h.elements[0],
  4254. newElementWith(h.elements[1], {
  4255. isDeleted: true,
  4256. }),
  4257. ],
  4258. storeAction: StoreAction.UPDATE,
  4259. });
  4260. Keyboard.redo();
  4261. Keyboard.redo();
  4262. expect(API.getUndoStack().length).toBe(2);
  4263. expect(API.getRedoStack().length).toBe(0);
  4264. expect(h.elements).toEqual([
  4265. expect.objectContaining({
  4266. id: rect.id,
  4267. frameId: null, // element is not unbound from
  4268. isDeleted: false,
  4269. }),
  4270. expect.objectContaining({
  4271. id: frame.id,
  4272. isDeleted: true,
  4273. }),
  4274. ]);
  4275. });
  4276. });
  4277. });
  4278. });