import { nanoid } from "nanoid" ; import ExcalidrawElement from "./entities/ExcalidrawElement" ; import Slide from "./entities/Slide" ; import SlideNotFoundAtIndex from "./errors/SlideNotFoundAtIndex" ; import SlideNotFoundWithId from "./errors/SlideNotFoundWithId" ; const SlideOperations = { addEmpty ( slides ) { return [ ... slides , { id : nanoid ( ) , shouldRender : true , shouldRenderWithCommonExcalidrawElements : true , excalidrawElements : [ ] , } , ] ; } , getById ( slides , id ) { const slide = slides . find ( ( slide ) => slide . id === id ) ; if ( ! slide ) { throw new SlideNotFoundWithId ( id ) ; } return slide ; } , getIndexById ( slides , id ) { const slideIndex = slides . findIndex ( ( slide ) => slide . id === id ) ; if ( slideIndex === - 1 ) { throw new SlideNotFoundWithId ( id ) ; } return slideIndex ; } , updateShouldRenderById ( slides , id , shouldRender ) { return SlideOperations . updateById ( slides , id , ( slide ) => ( { ... slide , shouldRender , } ) ) ; } , updateShouldRenderWithCommonExcalidrawElementsById ( slides , id , shouldRenderWithCommonExcalidrawElements ) { return SlideOperations . updateById ( slides , id , ( slide ) => ( { ... slide , shouldRenderWithCommonExcalidrawElements , } ) ) ; } , updateExcalidrawElementsById ( slides , id , excalidrawElements ) { return SlideOperations . updateById ( slides , id , ( slide ) => ( { ... slide , excalidrawElements , } ) ) ; } , updateById ( slides , id , updater ) { SlideOperations . assertSlideExistsWithId ( slides , id ) ; return slides . map ( ( slide ) => slide . id === id ? updater ( slide ) : slide ) ; } , removeById ( slides , id ) { SlideOperations . assertSlideExistsWithId ( slides , id ) ; return slides . filter ( ( slide ) => slide . id !== id ) ; } , moveSlide ( slides , fromIndex , toIndex ) { SlideOperations . assertSlideExistsAtIndex ( slides , fromIndex ) ; SlideOperations . assertSlideExistsAtIndex ( slides , toIndex ) ; const rearrangedSlides = [ ... slides ] ; rearrangedSlides . splice ( toIndex , 0 , rearrangedSlides . splice ( fromIndex , 1 ) [ 0 ] ! ) ; return rearrangedSlides ; } , assertSlideExistsWithId ( slides , id ) { SlideOperations . getById ( slides , id ) ; } , assertSlideExistsAtIndex ( slides , index ) { if ( index < 0 || index > slides . length - 1 ) { throw new SlideNotFoundAtIndex ( index ) ; } } , } ; export default SlideOperations ;