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

@@ -181,10 +181,11 @@ 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");
if (nextChapter && chapter.end > nextChapter.start) throw new Error("Illegal chapter time point");
}
@@ -203,6 +204,7 @@ function artplayerPluginChapter(option = {}) {
end: chapters[i + 1].start,
title: ""
});
$control.innerText = "";
$chapters = chapters.map((chapter)=>{
const $chapter = append($control, html);
const start = clamp(chapter.start, 0, art.duration);