Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0734bef

Browse files
Optimize getRecordCardCovers
1 parent 72e1478 commit 0734bef

File tree

1 file changed

+43
-50
lines changed
  • packages/gitbook/src/components/DocumentView/Table

1 file changed

+43
-50
lines changed

‎packages/gitbook/src/components/DocumentView/Table/utils.ts

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,60 +45,53 @@ export function getRecordCardCovers(
4545
objectFit?: string;
4646
};
4747
} {
48+
const lightValue = view.coverDefinition
49+
? (getRecordValue(record, view.coverDefinition) as CoverValue | string[])
50+
: null;
51+
52+
const darkValue = view.coverDefinitionDark
53+
? (getRecordValue(record, view.coverDefinitionDark) as CoverValue)
54+
: null;
55+
4856
return {
49-
light: (() => {
50-
if (!view.coverDefinition) {
51-
return { contentRef: null };
52-
}
53-
54-
const value = getRecordValue(record, view.coverDefinition) as CoverValue | string[];
55-
56-
if (Array.isArray(value)) {
57-
if (value.length === 0) {
58-
return { contentRef: null };
59-
}
60-
61-
if (typeof value[0] === 'string') {
62-
return { contentRef: { kind: 'file', file: value[0] } };
63-
}
64-
}
65-
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 };
76-
})(),
77-
dark: (() => {
78-
if (!view.coverDefinitionDark) {
79-
return { contentRef: null };
80-
}
81-
82-
const value = getRecordValue(record, view.coverDefinitionDark) as CoverValue;
83-
84-
if (!value) {
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-
};
94-
}
95-
96-
// It's a direct ContentRef
97-
return { contentRef: value as ContentRefFile | ContentRefURL };
98-
})(),
57+
light: processCoverValue(lightValue),
58+
dark: processCoverValue(darkValue),
9959
};
10060
}
10161

62+
/**
63+
* Process a cover value and return the content ref and object fit.
64+
*/
65+
function processCoverValue(value: CoverValue | string[] | null | undefined): {
66+
contentRef: ContentRefFile | ContentRefURL | null;
67+
objectFit?: string;
68+
} {
69+
if (!value) {
70+
return { contentRef: null };
71+
}
72+
73+
if (Array.isArray(value)) {
74+
if (value.length === 0) {
75+
return { contentRef: null };
76+
}
77+
78+
if (typeof value[0] === 'string') {
79+
return { contentRef: { kind: 'file', file: value[0] } };
80+
}
81+
}
82+
83+
// Check if it's the new schema with objectFit
84+
if (value && typeof value === 'object' && 'ref' in value && 'objectFit' in value) {
85+
return {
86+
contentRef: value.ref,
87+
objectFit: value.objectFit,
88+
};
89+
}
90+
91+
// It's a direct ContentRef
92+
return { contentRef: value as ContentRefFile | ContentRefURL };
93+
}
94+
10295
/**
10396
* Get the text alignment for a column.
10497
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /