@@ -6,6 +6,17 @@ import type {
6
6
DocumentTableRecord ,
7
7
DocumentTableViewCards ,
8
8
} from '@gitbook/api' ;
9
+
10
+ /**
11
+ * Cover value can be either a direct ContentRef or an object with objectFit and ref
12
+ */
13
+ export type CoverValue =
14
+ | ContentRefFile
15
+ | ContentRefURL
16
+ | {
17
+ objectFit : string ;
18
+ ref : ContentRefFile | ContentRefURL ;
19
+ } ;
9
20
import assertNever from 'assert-never' ;
10
21
11
22
/**
@@ -21,51 +32,69 @@ export function getRecordValue<T extends number | string | boolean | string[] |
21
32
22
33
/**
23
34
* Get the covers for a record card.
24
- * Returns both the light and dark covers.
35
+ * Returns both the light and dark covers with their content refs and optional object fit .
25
36
* The light cover is a string or a content ref (image or files column type).
26
37
* The dark cover is a content ref (image column type).
27
38
*/
28
39
export function getRecordCardCovers (
29
40
record : DocumentTableRecord ,
30
41
view : DocumentTableViewCards
31
- ) : { [ key in 'light' | 'dark' ] : ContentRefFile | ContentRefURL | null } {
42
+ ) : {
43
+ [ key in 'light' | 'dark' ] : {
44
+ contentRef : ContentRefFile | ContentRefURL | null ;
45
+ objectFit ?: string ;
46
+ } ;
47
+ } {
32
48
return {
33
49
light : ( ( ) => {
34
50
if ( ! view . coverDefinition ) {
35
- return null ;
51
+ return { contentRef : null } ;
36
52
}
37
53
38
- const value = getRecordValue ( record , view . coverDefinition ) as
39
- | ContentRefFile
40
- | ContentRefURL
41
- | string [ ] ;
54
+ const value = getRecordValue ( record , view . coverDefinition ) as CoverValue | string [ ] ;
42
55
43
56
if ( Array . isArray ( value ) ) {
44
57
if ( value . length === 0 ) {
45
- return null ;
58
+ return { contentRef : null } ;
46
59
}
47
60
48
61
if ( typeof value [ 0 ] === 'string' ) {
49
- return { kind : 'file' , file : value [ 0 ] } ;
62
+ return { contentRef : { kind : 'file' , file : value [ 0 ] } } ;
50
63
}
51
64
}
52
65
53
- return value as ContentRefFile | ContentRefURL ;
66
+ // Check if it's the new schema with objectFit
67
+ if ( value && typeof value === 'object' && 'ref' in value && 'objectFit' in value ) {
68
+ return {
69
+ contentRef : value . ref ,
70
+ objectFit : value . objectFit ,
71
+ } ;
72
+ }
73
+
74
+ // It's a direct ContentRef
75
+ return { contentRef : value as ContentRefFile | ContentRefURL } ;
54
76
} ) ( ) ,
55
77
dark : ( ( ) => {
56
78
if ( ! view . coverDefinitionDark ) {
57
- return null ;
79
+ return { contentRef : null } ;
58
80
}
59
81
60
- const value = getRecordValue ( record , view . coverDefinitionDark ) as
61
- | ContentRefFile
62
- | ContentRefURL ;
82
+ const value = getRecordValue ( record , view . coverDefinitionDark ) as CoverValue ;
63
83
64
84
if ( ! value ) {
65
- return null ;
85
+ return { contentRef : null } ;
86
+ }
87
+
88
+ // Check if it's the new schema with objectFit
89
+ if ( typeof value === 'object' && 'ref' in value && 'objectFit' in value ) {
90
+ return {
91
+ contentRef : value . ref ,
92
+ objectFit : value . objectFit ,
93
+ } ;
66
94
}
67
95
68
- return value ;
96
+ // It's a direct ContentRef
97
+ return { contentRef : value as ContentRefFile | ContentRefURL } ;
69
98
} ) ( ) ,
70
99
} ;
71
100
}
0 commit comments