@@ -11,7 +11,7 @@ import { actionToKey } from '@/src/composables/useKeyboardShortcuts';
1111import { useSegmentGroupStore } from '@/src/store/segmentGroups' ;
1212import { AnnotationToolStore } from '@/src/store/tools/useAnnotationTool' ;
1313import useLoadDataStore from '@/src/store/load-data' ;
14- import type { Layout , LayoutItem } from '@/src/types/layout' ;
14+ import type { LayoutItem , LayoutDirection } from '@/src/types/layout' ;
1515import type { ViewInfoInit } from '@/src/types/views' ;
1616
1717// --------------------------------------------------------------------------
@@ -34,8 +34,12 @@ const view2D = z.object({
3434const view3D = z . object ( {
3535 type : z . literal ( '3D' ) ,
3636 name : z . string ( ) . optional ( ) ,
37- viewDirection : z . enum ( [ 'Left' , 'Right' , 'Posterior' , 'Anterior' , 'Superior' , 'Inferior' ] ) . optional ( ) ,
38- viewUp : z . enum ( [ 'Left' , 'Right' , 'Posterior' , 'Anterior' , 'Superior' , 'Inferior' ] ) . optional ( ) ,
37+ viewDirection : z
38+ . enum ( [ 'Left' , 'Right' , 'Posterior' , 'Anterior' , 'Superior' , 'Inferior' ] )
39+ . optional ( ) ,
40+ viewUp : z
41+ . enum ( [ 'Left' , 'Right' , 'Posterior' , 'Anterior' , 'Superior' , 'Inferior' ] )
42+ . optional ( ) ,
3943} ) ;
4044
4145const viewOblique = z . object ( {
@@ -48,16 +52,18 @@ const viewSpec = z.union([viewString, view2D, view3D, viewOblique]);
4852// --------------------------------------------------------------------------
4953// Layout Specifications
5054
51- type LayoutConfigItem = z . infer < typeof viewSpec > | {
52- direction : 'H' | 'V' ;
53- items : LayoutConfigItem [ ] ;
54- } ;
55+ type LayoutConfigItem =
56+ | z . infer < typeof viewSpec >
57+ | {
58+ direction : LayoutDirection ;
59+ items : LayoutConfigItem [ ] ;
60+ } ;
5561
5662const layoutConfigItem : z . ZodType < LayoutConfigItem > = z . lazy ( ( ) =>
5763 z . union ( [
5864 viewSpec ,
5965 z . object ( {
60- direction : z . enum ( [ 'H ' , 'V ' ] ) ,
66+ direction : z . enum ( [ 'row ' , 'column ' ] ) ,
6167 items : z . array ( layoutConfigItem ) ,
6268 } ) ,
6369 ] )
@@ -66,7 +72,7 @@ const layoutConfigItem: z.ZodType<LayoutConfigItem> = z.lazy(() =>
6672const layoutConfig = z . union ( [
6773 z . array ( z . array ( viewString ) ) ,
6874 z . object ( {
69- direction : z . enum ( [ 'H ' , 'V ' ] ) ,
75+ direction : z . enum ( [ 'row ' , 'column ' ] ) ,
7076 items : z . array ( layoutConfigItem ) ,
7177 } ) ,
7278 z . object ( {
@@ -147,24 +153,48 @@ export const readConfigFile = async (configFile: File) => {
147153// --------------------------------------------------------------------------
148154// Layout Parsing
149155
150- const stringToViewInfoInit = ( str : z . infer < typeof viewString > ) : ViewInfoInit => {
156+ const stringToViewInfoInit = (
157+ str : z . infer < typeof viewString >
158+ ) : ViewInfoInit => {
151159 switch ( str ) {
152160 case 'axial' :
153- return { name : 'Axial' , type : '2D' , dataID : null , options : { orientation : 'Axial' } } ;
161+ return {
162+ name : 'Axial' ,
163+ type : '2D' ,
164+ dataID : null ,
165+ options : { orientation : 'Axial' } ,
166+ } ;
154167 case 'coronal' :
155- return { name : 'Coronal' , type : '2D' , dataID : null , options : { orientation : 'Coronal' } } ;
168+ return {
169+ name : 'Coronal' ,
170+ type : '2D' ,
171+ dataID : null ,
172+ options : { orientation : 'Coronal' } ,
173+ } ;
156174 case 'sagittal' :
157- return { name : 'Sagittal' , type : '2D' , dataID : null , options : { orientation : 'Sagittal' } } ;
175+ return {
176+ name : 'Sagittal' ,
177+ type : '2D' ,
178+ dataID : null ,
179+ options : { orientation : 'Sagittal' } ,
180+ } ;
158181 case 'volume' :
159- return { name : 'Volume' , type : '3D' , dataID : null , options : { viewDirection : 'Posterior' , viewUp : 'Superior' } } ;
182+ return {
183+ name : 'Volume' ,
184+ type : '3D' ,
185+ dataID : null ,
186+ options : { viewDirection : 'Posterior' , viewUp : 'Superior' } ,
187+ } ;
160188 case 'oblique' :
161189 return { name : 'Oblique' , type : 'Oblique' , dataID : null , options : { } } ;
162190 default :
163191 throw new Error ( `Unknown view string: ${ str } ` ) ;
164192 }
165193} ;
166194
167- const viewSpecToViewInfoInit = ( spec : z . infer < typeof viewSpec > ) : ViewInfoInit => {
195+ const viewSpecToViewInfoInit = (
196+ spec : z . infer < typeof viewSpec >
197+ ) : ViewInfoInit => {
168198 if ( typeof spec === 'string' ) {
169199 return stringToViewInfoInit ( spec ) ;
170200 }
@@ -202,20 +232,20 @@ const viewSpecToViewInfoInit = (spec: z.infer<typeof viewSpec>): ViewInfoInit =>
202232 throw new Error ( `Unknown view spec type` ) ;
203233} ;
204234
205- const parseGridLayout = ( grid : z . infer < typeof viewString > [ ] [ ] ) : { layout : Layout ; views : ViewInfoInit [ ] } => {
235+ const parseGridLayout = ( grid : z . infer < typeof viewString > [ ] [ ] ) => {
206236 const views : ViewInfoInit [ ] = [ ] ;
207237 let slotIndex = 0 ;
208238
209- const items : LayoutItem [ ] = grid . map ( ( row ) => {
210- const rowItems : LayoutItem [ ] = row . map ( ( ) => {
239+ const items = grid . map ( ( row ) => {
240+ const rowItems = row . map ( ( ) => {
211241 const currentSlot = slotIndex ;
212242 slotIndex += 1 ;
213243 return { type : 'slot' as const , slotIndex : currentSlot } ;
214244 } ) ;
215245
216246 return {
217247 type : 'layout' as const ,
218- direction : 'V ' as const ,
248+ direction : 'row ' as const ,
219249 items : rowItems ,
220250 } ;
221251 } ) ;
@@ -226,51 +256,46 @@ const parseGridLayout = (grid: z.infer<typeof viewString>[][]): { layout: Layout
226256
227257 return {
228258 layout : {
229- direction : 'H' ,
259+ direction : 'column' as const ,
230260 items,
231261 } ,
232262 views,
233263 } ;
234264} ;
235265
236- const parseNestedLayout = (
237- layoutItem : LayoutConfigItem
238- ) : { layout : Layout ; views : ViewInfoInit [ ] } => {
266+ const parseNestedLayout = ( layoutItem : LayoutConfigItem ) => {
239267 const views : ViewInfoInit [ ] = [ ] ;
240268 let slotIndex = 0 ;
241269
270+ const isViewSpec = (
271+ item : LayoutConfigItem
272+ ) : item is z . infer < typeof viewSpec > =>
273+ typeof item === 'string' || 'type' in item ;
274+
242275 const processItem = ( item : LayoutConfigItem ) : LayoutItem => {
243- if ( typeof item === 'string' || 'type' in item ) {
244- const viewInfo = viewSpecToViewInfoInit ( item as z . infer < typeof viewSpec > ) ;
276+ if ( isViewSpec ( item ) ) {
277+ const viewInfo = viewSpecToViewInfoInit ( item ) ;
245278 views . push ( viewInfo ) ;
246279 const currentSlot = slotIndex ;
247280 slotIndex += 1 ;
248- return { type : 'slot' , slotIndex : currentSlot } ;
281+ return { type : 'slot' as const , slotIndex : currentSlot } ;
249282 }
250283
251284 return {
252- type : 'layout' ,
285+ type : 'layout' as const ,
253286 direction : item . direction ,
254287 items : item . items . map ( processItem ) ,
255288 } ;
256289 } ;
257290
258- if ( typeof layoutItem === 'string' || 'type' in layoutItem ) {
259- const viewInfo = viewSpecToViewInfoInit ( layoutItem as z . infer < typeof viewSpec > ) ;
260- views . push ( viewInfo ) ;
261- return {
262- layout : {
263- direction : 'H' ,
264- items : [ { type : 'slot' , slotIndex : 0 } ] ,
265- } ,
266- views,
267- } ;
268- }
291+ const rootLayout = isViewSpec ( layoutItem )
292+ ? { direction : 'column' as const , items : [ layoutItem ] }
293+ : layoutItem ;
269294
270295 return {
271296 layout : {
272- direction : layoutItem . direction ,
273- items : layoutItem . items . map ( processItem ) ,
297+ direction : rootLayout . direction ,
298+ items : rootLayout . items . map ( processItem ) ,
274299 } ,
275300 views,
276301 } ;
0 commit comments