Improved behavior of next chapter keybind with overlap

This commit is contained in:
Ajay 2022-09-16 01:38:37 -04:00
parent 6571bba218
commit 23e0666569
2 changed files with 27 additions and 8 deletions

View file

@ -2237,7 +2237,7 @@ function updateActiveSegment(currentTime: number): void {
}
function nextChapter(): void {
const chapters = previewBar.chapterGroups?.filter((time) => [ActionType.Chapter, null].includes(time.actionType));
const chapters = previewBar.unfilteredChapterGroups?.filter((time) => [ActionType.Chapter, null].includes(time.actionType));
if (!chapters || chapters.length <= 0) return;
lastNextChapterKeybind.time = video.currentTime;
@ -2258,7 +2258,7 @@ function previousChapter(): void {
return;
}
const chapters = previewBar.chapterGroups?.filter((time) => [ActionType.Chapter, null].includes(time.actionType));
const chapters = previewBar.unfilteredChapterGroups?.filter((time) => [ActionType.Chapter, null].includes(time.actionType));
if (!chapters || chapters.length <= 0) {
video.currentTime = 0;
return;

View file

@ -55,6 +55,7 @@ class PreviewBar {
originalChapterBar: HTMLElement;
originalChapterBarBlocks: NodeListOf<HTMLElement>;
chapterMargin: number;
unfilteredChapterGroups: ChapterGroup[];
chapterGroups: ChapterGroup[];
constructor(parent: HTMLElement, onMobileYouTube: boolean, onInvidious: boolean, chapterVote: ChapterVote, test=false) {
@ -311,13 +312,19 @@ class PreviewBar {
}
// Merge overlapping chapters
this.unfilteredChapterGroups = this.createChapterRenderGroups(segments);
const filteredSegments = segments?.filter((segment) => this.chapterFilter(segment));
this.chapterGroups = this.createChapterRenderGroups(filteredSegments).filter((segment) => this.chapterGroupFilter(segment));
// Fix missing sections due to filtered segments
for (let i = 1; i < this.chapterGroups.length; i++) {
if (this.chapterGroups[i].segment[0] !== this.chapterGroups[i - 1].segment[1]) {
this.chapterGroups[i - 1].segment[1] = this.chapterGroups[i].segment[0]
if (filteredSegments && filteredSegments.length !== segments.length) {
this.chapterGroups = this.createChapterRenderGroups(filteredSegments).filter((segment) => this.chapterGroupFilter(segment));
// Fix missing sections due to filtered segments
for (let i = 1; i < this.chapterGroups.length; i++) {
if (this.chapterGroups[i].segment[0] !== this.chapterGroups[i - 1].segment[1]) {
this.chapterGroups[i - 1].segment[1] = this.chapterGroups[i].segment[0]
}
}
} else {
this.chapterGroups = this.unfilteredChapterGroups;
}
if (segments.every((segments) => segments.source === SponsorSourceType.YouTube)
@ -407,11 +414,13 @@ class PreviewBar {
latestValidChapter = result[result.length - 1];
}
const priorityActionType = this.getActionTypePrioritized([segment.actionType, latestChapter?.actionType]);
// Split the latest chapter if smaller
result.push({
segment: [segment.segment[0], segment.segment[1]],
originalDuration: segmentDuration,
actionType: segment.actionType
actionType: priorityActionType
});
if (latestValidChapter?.segment[1] > segment.segment[1]) {
result.push({
@ -478,6 +487,16 @@ class PreviewBar {
return result;
}
private getActionTypePrioritized(actionTypes: ActionType[]): ActionType {
if (actionTypes.includes(ActionType.Skip)) {
return ActionType.Skip;
} else if (actionTypes.includes(ActionType.Mute)) {
return ActionType.Mute;
} else {
return actionTypes.find(a => a) ?? actionTypes[0];
}
}
private setupChapterSection(section: HTMLElement, startTime: number, endTime: number, addMargin: boolean): void {
const sizePercent = this.intervalToPercentage(startTime, endTime);
if (addMargin) {