's'
This commit is contained in:
26
lib/content/course-utils.ts
Normal file
26
lib/content/course-utils.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 课程工具 - 获取上一节/下一节
|
||||
*/
|
||||
|
||||
export type LessonRef = { moduleIndex: number; lessonIndex: number };
|
||||
|
||||
export function getPrevNextLesson(
|
||||
modules: readonly { lessons: readonly unknown[] }[],
|
||||
moduleIndex: number,
|
||||
lessonIndex: number
|
||||
): { prev: LessonRef | null; next: LessonRef | null } {
|
||||
const allLessons: LessonRef[] = [];
|
||||
modules.forEach((m, mi) => {
|
||||
m.lessons.forEach((_, li) => {
|
||||
allLessons.push({ moduleIndex: mi, lessonIndex: li });
|
||||
});
|
||||
});
|
||||
|
||||
const idx = allLessons.findIndex((l) => l.moduleIndex === moduleIndex && l.lessonIndex === lessonIndex);
|
||||
if (idx < 0) return { prev: null, next: null };
|
||||
|
||||
return {
|
||||
prev: idx > 0 ? allLessons[idx - 1] : null,
|
||||
next: idx < allLessons.length - 1 ? allLessons[idx + 1] : null,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user