chore: Refactor artplayer-plugin-chapter to sort chapters before processing

This commit is contained in:
Harvey Zack committed 2024-06-03 01:10:29 +08:00
1 parent 5edf5af672
commit d8ff61acf7
3 files changed
+15 -3

No files matched your search

+11 -1
View File
@@ -44,12 +44,21 @@ export default function artplayerPluginChapter(option = {}) {
function update(chapters = []) {
if (!chapters.length) return;
if (!art.duration) return;
$control.innerText = '';
chapters = chapters.sort((a, b) => a.start - b.start);
for (let i = 0; i < chapters.length; i++) {
const chapter = chapters[i];
const nextChapter = chapters[i + 1];
if (
typeof chapter.start !== 'number' ||
typeof chapter.end !== 'number' ||
typeof chapter.title !== 'string'
) {
throw new Error('Illegal chapter data type');
}
if (chapter.start < 0 || chapter.end > art.duration || chapter.start >= chapter.end) {
throw new Error('Illegal chapter time point');
}
@@ -77,6 +86,7 @@ export default function artplayerPluginChapter(option = {}) {
}
}
$control.innerText = '';
$chapters = chapters.map((chapter) => {
const $chapter = append($control, html);
const start = clamp(chapter.start, 0, art.duration);