mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 22:53:21 -08:00
feat: update subtitle handling to support multiple active cues and improve rendering logic
This commit is contained in:
1 parent
3d33458efc
commit
8ea85476e0
5 files changed
+64
-29
No files matched your search
@@ -46,9 +46,9 @@ export default class Subtitle extends Component {
|
||||
return this.art.template.$video?.textTracks?.[0];
|
||||
}
|
||||
|
||||
get activeCue() {
|
||||
if (!this.textTrack) return null;
|
||||
return this.textTrack.activeCues[0];
|
||||
get activeCues() {
|
||||
if (!this.textTrack) return [];
|
||||
return Array.from(this.textTrack.activeCues);
|
||||
}
|
||||
|
||||
get cues() {
|
||||
@@ -65,20 +65,29 @@ export default class Subtitle extends Component {
|
||||
}
|
||||
|
||||
update() {
|
||||
const { $subtitle } = this.art.template;
|
||||
const {
|
||||
option: { subtitle },
|
||||
template: { $subtitle },
|
||||
} = this.art;
|
||||
|
||||
$subtitle.innerHTML = '';
|
||||
if (this.activeCue) {
|
||||
this.art.emit('subtitleBeforeUpdate', this.activeCue);
|
||||
if (this.art.option.subtitle.escape) {
|
||||
$subtitle.innerHTML = this.activeCue.text
|
||||
if (!this.activeCues.length) return;
|
||||
|
||||
this.art.emit('subtitleBeforeUpdate', this.activeCues);
|
||||
$subtitle.innerHTML = this.activeCues
|
||||
.map((cue, index) =>
|
||||
cue.text
|
||||
.split(/\r?\n/)
|
||||
.map((item) => `<div class="art-subtitle-line">${escape(item)}</div>`)
|
||||
.join('');
|
||||
} else {
|
||||
$subtitle.innerHTML = this.activeCue.text;
|
||||
}
|
||||
this.art.emit('subtitleAfterUpdate', this.activeCue);
|
||||
}
|
||||
.map(
|
||||
(item) =>
|
||||
`<div class="art-subtitle-line" data-group="${index}">
|
||||
${subtitle.escape ? escape(item) : item}
|
||||
</div>`,
|
||||
)
|
||||
.join(''),
|
||||
)
|
||||
.join('');
|
||||
this.art.emit('subtitleAfterUpdate', this.activeCues);
|
||||
}
|
||||
|
||||
async switch(url, newOption = {}) {
|
||||
|
||||
+2
-2
@@ -153,8 +153,8 @@ declare class Artplayer extends Player {
|
||||
get url(): string;
|
||||
set url(url: string);
|
||||
get textTrack(): TextTrack;
|
||||
get activeCue(): VTTCue;
|
||||
get cues(): TextTrackCueList;
|
||||
get activeCues(): VTTCue[];
|
||||
get cues(): VTTCue[];
|
||||
style(name: string | Partial<CSSStyleDeclaration>, value?: string): void;
|
||||
switch(url: string, option?: Subtitle): Promise<string>;
|
||||
} & Component;
|
||||
|
||||
Reference in new issue
Block a user