import { assert } from "../util" ; import { View , ViewType } from "../components/editor/View" ; import { File } from "../models" ; export default class Group { currentView : View ; views : View [ ] ; preview : View ; constructor ( view , views = [ ] ) { this . currentView = view ; this . views = views ; } open ( view , shouldPreview = true ) { const views = this . views ; const index = views . indexOf ( view ) ; if ( index >= 0 ) { this . currentView = view ; if ( ! shouldPreview && this . preview === view ) { this . preview = null ; } return ; } if ( shouldPreview ) { if ( this . preview ) { const previewIndex = views . indexOf ( this . preview ) ; assert ( previewIndex >= 0 ) ; this . currentView = this . preview = views [ previewIndex ] = view ; } else { views . push ( view ) ; this . currentView = this . preview = view ; } } else { views . push ( view ) ; this . currentView = view ; this . preview = null ; } } openFile ( file , type = ViewType . Editor , preview = true ) { const index = this . views . findIndex ( view => view . file === file && view . type === type ) ; const view = ( index >= 0 ) ? this . views [ index ] : new View ( file , type ) ; this . open ( view , preview ) ; } close ( view ) { const i = this . views . indexOf ( view ) ; if ( i < 0 ) { return ; } this . views . splice ( i , 1 ) ; const numViews = this . views . length ; this . currentView = numViews ? this . views [ Math . min ( numViews - 1 , i ) ] : null ; if ( view === this . preview ) { this . preview = null ; } } }