mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-18 19:01:20 -08:00
Optimize setBar event and update chapter display in artplayer-plugin-chapter
This commit is contained in:
1 parent
94e34320e0
commit
2e72e0cec2
4 files changed
+69
-61
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import style from 'bundle-text:./style.less';
|
||||
|
||||
export default function artplayerPluginChapter({ chapters = [] }) {
|
||||
export default function artplayerPluginChapter(option = {}) {
|
||||
return (art) => {
|
||||
const { $player } = art.template;
|
||||
const { setStyle, append, clamp, query, isMobile, addClass, removeClass } = art.constructor.utils;
|
||||
@@ -35,6 +35,36 @@ export default function artplayerPluginChapter({ chapters = [] }) {
|
||||
}
|
||||
}
|
||||
|
||||
function update(chapters = []) {
|
||||
$control.innerText = '';
|
||||
|
||||
$chapters = chapters.map((chapter) => {
|
||||
const $chapter = append($control, html);
|
||||
const start = clamp(chapter.start, 0, art.duration);
|
||||
const end = clamp(chapter.end, 0, art.duration);
|
||||
const duration = end - start;
|
||||
const percentage = duration / art.duration;
|
||||
$chapter.dataset.start = start;
|
||||
$chapter.dataset.end = end;
|
||||
$chapter.dataset.duration = duration;
|
||||
$chapter.dataset.title = chapter.title.trim();
|
||||
$chapter.style.width = `${percentage * 100}%`;
|
||||
|
||||
return {
|
||||
$chapter,
|
||||
$hover: query('.art-progress-hover', $chapter),
|
||||
$loaded: query('.art-progress-loaded', $chapter),
|
||||
$played: query('.art-progress-played', $chapter),
|
||||
};
|
||||
});
|
||||
|
||||
if ($chapters.length) {
|
||||
addClass($player, 'artplayer-plugin-chapter');
|
||||
} else {
|
||||
removeClass($player, 'artplayer-plugin-chapter');
|
||||
}
|
||||
}
|
||||
|
||||
art.on('setBar', (type, percentage, event) => {
|
||||
if (!$chapters.length) return;
|
||||
|
||||
@@ -97,38 +127,11 @@ export default function artplayerPluginChapter({ chapters = [] }) {
|
||||
});
|
||||
}
|
||||
|
||||
art.on('video:loadedmetadata', () => {
|
||||
$control.innerText = '';
|
||||
|
||||
$chapters = chapters.map((chapter) => {
|
||||
const $chapter = append($control, html);
|
||||
const start = clamp(chapter.start, 0, art.duration);
|
||||
const end = clamp(chapter.end, 0, art.duration);
|
||||
const duration = end - start;
|
||||
const percentage = duration / art.duration;
|
||||
$chapter.dataset.start = start;
|
||||
$chapter.dataset.end = end;
|
||||
$chapter.dataset.duration = duration;
|
||||
$chapter.dataset.title = chapter.title.trim();
|
||||
$chapter.style.width = `${percentage * 100}%`;
|
||||
|
||||
return {
|
||||
$chapter,
|
||||
$hover: query('.art-progress-hover', $chapter),
|
||||
$loaded: query('.art-progress-loaded', $chapter),
|
||||
$played: query('.art-progress-played', $chapter),
|
||||
};
|
||||
});
|
||||
|
||||
if ($chapters.length) {
|
||||
addClass($player, 'artplayer-plugin-chapter');
|
||||
} else {
|
||||
removeClass($player, 'artplayer-plugin-chapter');
|
||||
}
|
||||
});
|
||||
art.once('video:loadedmetadata', () => update(option.chapters));
|
||||
|
||||
return {
|
||||
name: 'artplayerPluginChapter',
|
||||
update: (chapters) => update(chapters),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,16 +3,19 @@ import type Artplayer from 'artplayer';
|
||||
export = artplayerPluginChapter;
|
||||
export as namespace artplayerPluginChapter;
|
||||
|
||||
type Chapters = {
|
||||
start: number;
|
||||
end: number;
|
||||
title: string;
|
||||
}[];
|
||||
|
||||
type Option = {
|
||||
chapters?: {
|
||||
start: number;
|
||||
end: number;
|
||||
title: string;
|
||||
}[];
|
||||
chapters?: Chapters;
|
||||
};
|
||||
|
||||
type Result = {
|
||||
name: 'artplayerPluginChapter';
|
||||
update: (chapters: Chapters) => void;
|
||||
};
|
||||
|
||||
declare const artplayerPluginChapter: (option: Option) => (art: Artplayer) => Result;
|
||||
Reference in new issue
Block a user