add restoreInit

This commit is contained in:
Harvey Zack committed 2024-09-14 16:48:30 +08:00
1 parent 2b11d897ff
commit 36b2c80666
6 files changed
+56 -68

No files matched your search

+2
View File
@@ -6,6 +6,7 @@ import gestureInit from './gestureInit';
import viewInit from './viewInit';
import documentInit from './documentInit';
import updateInit from './updateInit';
import restoreInit from './restoreInit';
export default class Events {
constructor(art) {
@@ -21,6 +22,7 @@ export default class Events {
viewInit(art, this);
documentInit(art, this);
updateInit(art, this);
restoreInit(art, this);
}
proxy(target, name, callback, option = {}) {
@@ -0,0 +1,3 @@
export default function restoreInit(art, events) {
//
}
@@ -2,41 +2,32 @@ import { def } from '../utils';
export default function subtitleOffsetMix(art) {
const { clamp } = art.constructor.utils;
const { notice, template, i18n } = art;
const { notice, i18n } = art;
let offsetCache = 0;
let cuesCache = [];
art.on('subtitleSwitch', () => {
cuesCache = [];
let subtitleOffset = 0;
art.on('subtitleTrackLoad', () => {
subtitleOffset = 0;
art.emit('subtitleOffset', 0);
});
def(art, 'subtitleOffset', {
get() {
return offsetCache;
return subtitleOffset;
},
set(value) {
if (template.$track && template.$track.track) {
const cues = Array.from(template.$track.track.cues);
offsetCache = clamp(value, -5, 5);
for (let index = 0; index < cues.length; index++) {
const cue = cues[index];
if (!cuesCache[index]) {
cuesCache[index] = {
startTime: cue.startTime,
endTime: cue.endTime,
};
}
cue.startTime = clamp(cuesCache[index].startTime + offsetCache, 0, art.duration);
cue.endTime = clamp(cuesCache[index].endTime + offsetCache, 0, art.duration);
}
art.subtitle.update();
notice.show = `${i18n.get('Subtitle Offset')}: ${value}s`;
art.emit('subtitleOffset', value);
} else {
art.emit('subtitleOffset', 0);
const { cues } = art.subtitle;
subtitleOffset = clamp(value, -5, 5);
for (let index = 0; index < cues.length; index++) {
const cue = cues[index];
cue.originalStartTime = cue.originalStartTime ?? cue.startTime;
cue.originalEndTime = cue.originalEndTime ?? cue.endTime;
cue.startTime = clamp(cue.originalStartTime + subtitleOffset, 0, art.duration);
cue.endTime = clamp(cue.originalEndTime + subtitleOffset, 0, art.duration);
}
art.subtitle.update();
notice.show = `${i18n.get('Subtitle Offset')}: ${value}s`;
art.emit('subtitleOffset', value);
},
});
}
+1 -1
View File
@@ -41,6 +41,7 @@ export type Events = {
subtitleLoad: [url: string];
subtitleSwitch: [url: string];
subtitleTrackLoad: [track: HTMLTrackElement, event: Event];
subtitleOffset: [offset: number];
focus: [event: Event];
blur: [event: Event];
dblclick: [event: Event];
@@ -65,7 +66,6 @@ export type Events = {
play: [];
screenshot: [dataUri: string];
seek: [currentTime: number];
subtitleOffset: [offset: number];
restart: [url: string];
muted: [state: boolean];
setBar: [type: Bar, percentage: number, event?: Event];