Add keybind support to skip to highlight

This commit is contained in:
Ajay Ramachandran 2021-08-19 01:17:36 -04:00
parent 21f563fdb7
commit db34f1fd5b
3 changed files with 37 additions and 12 deletions

View file

@ -1,5 +1,5 @@
import Config from "./config"; import Config from "./config";
import { SponsorTime, CategorySkipOption, VideoID, SponsorHideType, VideoInfo, StorageChangesObject, CategoryActionType, ChannelIDInfo, ChannelIDStatus, SponsorSourceType, SegmentUUID, Category, SkipToTimeParams } from "./types"; import { SponsorTime, CategorySkipOption, VideoID, SponsorHideType, VideoInfo, StorageChangesObject, CategoryActionType, ChannelIDInfo, ChannelIDStatus, SponsorSourceType, SegmentUUID, Category, SkipToTimeParams, ToggleSkippable } from "./types";
import { ContentContainer } from "./types"; import { ContentContainer } from "./types";
import Utils from "./utils"; import Utils from "./utils";
@ -27,6 +27,7 @@ let sponsorTimes: SponsorTime[] = null;
let sponsorVideoID: VideoID = null; let sponsorVideoID: VideoID = null;
// List of open skip notices // List of open skip notices
const skipNotices: SkipNotice[] = []; const skipNotices: SkipNotice[] = [];
let activeSkipKeybindElement: ToggleSkippable = null;
// JSON video info // JSON video info
let videoInfo: VideoInfo = null; let videoInfo: VideoInfo = null;
@ -1104,12 +1105,18 @@ function skipToTime({v, skipTime, skippingSegments, openNotice, forceAutoSkip, u
&& skippingSegments.length === 1 && skippingSegments.length === 1
&& getCategoryActionType(skippingSegments[0].category) === CategoryActionType.POI) { && getCategoryActionType(skippingSegments[0].category) === CategoryActionType.POI) {
skipButtonControlBar.enable(skippingSegments[0]); skipButtonControlBar.enable(skippingSegments[0]);
activeSkipKeybindElement?.setShowKeybindHint(false);
activeSkipKeybindElement = skipButtonControlBar;
} else { } else {
if (openNotice) { if (openNotice) {
//send out the message saying that a sponsor message was skipped //send out the message saying that a sponsor message was skipped
if (!Config.config.dontShowNotice || !autoSkip) { if (!Config.config.dontShowNotice || !autoSkip) {
skipNotices.forEach((notice) => notice.setShowKeybindHint(false)); const newSkipNotice = new SkipNotice(skippingSegments, autoSkip, skipNoticeContentContainer, unskipTime);
skipNotices.push(new SkipNotice(skippingSegments, autoSkip, skipNoticeContentContainer, unskipTime)); skipNotices.push(newSkipNotice);
activeSkipKeybindElement?.setShowKeybindHint(false);
activeSkipKeybindElement = newSkipNotice;
} }
} }
} }
@ -1702,9 +1709,8 @@ function hotkeyListener(e: KeyboardEvent): void {
switch (key) { switch (key) {
case skipKey: case skipKey:
if (skipNotices.length > 0) { if (activeSkipKeybindElement) {
const latestSkipNotice = skipNotices[skipNotices.length - 1]; activeSkipKeybindElement.toggleSkip.call(activeSkipKeybindElement);
latestSkipNotice.toggleSkip.call(latestSkipNotice);
} }
break; break;
case startSponsorKey: case startSponsorKey:

View file

@ -15,6 +15,8 @@ export class SkipButtonControlBar {
chapterText: HTMLElement; chapterText: HTMLElement;
segment: SponsorTime; segment: SponsorTime;
showKeybindHint = true;
timeout: NodeJS.Timeout; timeout: NodeJS.Timeout;
skip: (segment: SponsorTime) => void; skip: (segment: SponsorTime) => void;
@ -35,7 +37,7 @@ export class SkipButtonControlBar {
this.container.appendChild(this.skipIcon); this.container.appendChild(this.skipIcon);
this.container.appendChild(this.textContainer); this.container.appendChild(this.textContainer);
this.container.addEventListener("click", () => this.onClick()); this.container.addEventListener("click", () => this.toggleSkip());
this.container.addEventListener("mouseenter", () => this.stopTimer()); this.container.addEventListener("mouseenter", () => this.stopTimer());
this.container.addEventListener("mouseleave", () => this.startTimer()); this.container.addEventListener("mouseleave", () => this.startTimer());
} }
@ -51,18 +53,30 @@ export class SkipButtonControlBar {
enable(segment: SponsorTime): void { enable(segment: SponsorTime): void {
this.segment = segment; this.segment = segment;
this.chapterText?.classList?.add("hidden"); this.refreshText();
this.container.classList.remove("hidden");
this.textContainer.innerText = getSkippingText([segment], false);
this.startTimer(); this.startTimer();
} }
refreshText(): void {
if (this.segment) {
this.chapterText?.classList?.add("hidden");
this.container.classList.remove("hidden");
this.textContainer.innerText = getSkippingText([this.segment], false) + (this.showKeybindHint ? " (" + Config.config.skipKeybind + ")" : "");
}
}
setShowKeybindHint(show: boolean): void {
this.showKeybindHint = show;
this.refreshText();
}
stopTimer(): void { stopTimer(): void {
if (this.timeout) clearTimeout(this.timeout); if (this.timeout) clearTimeout(this.timeout);
} }
startTimer() { startTimer(): void {
this.stopTimer(); this.stopTimer();
this.timeout = setTimeout(() => this.disable(), Config.config.skipNoticeDuration * 1000); this.timeout = setTimeout(() => this.disable(), Config.config.skipNoticeDuration * 1000);
} }
@ -72,7 +86,7 @@ export class SkipButtonControlBar {
this.chapterText?.classList?.remove("hidden"); this.chapterText?.classList?.remove("hidden");
} }
onClick(): void { toggleSkip(): void {
this.skip(this.segment); this.skip(this.segment);
this.disable(); this.disable();
} }

View file

@ -195,3 +195,8 @@ export interface SkipToTimeParams {
forceAutoSkip?: boolean, forceAutoSkip?: boolean,
unskipTime?: number unskipTime?: number
} }
export interface ToggleSkippable {
toggleSkip: () => void;
setShowKeybindHint: (show: boolean) => void;
}