import store from '../store' ; import { tr } from '../translations' ; import LDParse from '../ld_parse' ; function addOneTitlePage ( parent ? ) { let insertionIndex = 1 ; if ( parent ) { insertionIndex = store . state . pages . findIndex ( ( page ) => page . id === parent . pages [ 0 ] ) ; } const page = store . mutations . page . add ( { subtype : 'titlePage' , parent , insertionIndex , parentInsertionIndex : 0 , doNotRenumber : true , } ) ; page . number = 1 ; if ( parent ) { const tmpPage = store . get . page ( parent . pages [ 1 ] ) ; if ( tmpPage == null ) { throw 'Trying to get a Book Page number from a non-existent Page' ; } page . number = tmpPage . number ; } store . mutations . page . renumber ( ) ; const step = store . mutations . step . add ( { dest : page } ) ; if ( store && store . model && store . model . filename ) { step . model . filename = store . model . filename ; } store . mutations . titlePage . addTitleLabel ( page ) ; store . mutations . titlePage . addPageCountLabel ( page ) ; } export interface TitlePageMutationInterface { add ( ) : void ; delete ( ) : void ; addTitleLabel ( page ) : Annotation ; setPageCountLabels ( parent ? ) : void ; addPageCountLabel ( page , parent ? ) : Annotation ; } export const TitlePageMutations = { add ( ) { if ( store . state . books . length > 1 ) { store . state . books . forEach ( addOneTitlePage ) ; } else { addOneTitlePage ( ) ; } } , delete ( ) { store . state . pages . map ( store . get . page ) . filter ( ( page ) : page is Page => page != null && page . subtype === 'titlePage' ) . forEach ( ( page ) => { store . mutations . item . deleteChildList ( { item : page , listType : 'step' } ) ; store . mutations . page . delete ( { page } ) ; } ) ; } , addTitleLabel ( page ) { return store . mutations . annotation . add ( { annotationType : 'label' , properties : { text : store . get . modelName ( true ) , font : 'bold 30pt Helvetica' , meta : { type : 'title-page-model-name' , } , } , parent : page , x : 0 , y : 0 , } ) ; } , setPageCountLabels ( parent ) { const titlePage = store . get . titlePage ( ) ; if ( titlePage ) { const annotations = titlePage . annotations . map ( store . get . annotation ) ; const annotation = annotations . find ( a => { return ( a == null ) ? false : a . meta . type === 'title-page-page-count' ; } ) ; if ( annotation != null ) { const partCount = LDParse . model . get . partCount ( store . model ) ; const pageCount = store . get . pageCount ( ) ; let text ; if ( parent ) { const parentBook = store . get . book ( parent ) ; if ( parentBook == null ) { throw 'Trying to find a nice annotation for a non-existent Book' ; } const bookNumber = parentBook . number ; text = tr ( 'title_page.book_model_info_@mf' , { bookNumber , partCount , pageCount } ) ; } else { text = tr ( 'title_page.model_info_@mf' , { partCount , pageCount } ) ; } store . mutations . annotation . set ( { annotation , newProperties : { text } } ) ; } } } , addPageCountLabel ( page , parent ) { const annotation = store . mutations . annotation . add ( { annotationType : 'label' , properties : { text : '' , font : '16pt Helvetica' , meta : { type : 'title-page-page-count' , } , } , parent : page , x : 0 , y : 0 , } ) ; store . mutations . titlePage . setPageCountLabels ( parent ) ; return annotation ; } , } ;