mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-08 12:37:05 +03:00
Use formatted time inputs for segment editing
Additionally, removes unused util functions and saves the edits on changes to update the preview immediately
This commit is contained in:
@@ -372,12 +372,8 @@ input::-webkit-inner-spin-button {
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
|
||||
.sponsorTimeEditMinutes {
|
||||
width: 30px;
|
||||
}
|
||||
|
||||
.sponsorTimeEditSeconds {
|
||||
width: 60px;
|
||||
.sponsorTimeEditInput {
|
||||
width: 90px;
|
||||
}
|
||||
|
||||
.sponsorNowButton {
|
||||
|
||||
@@ -20,7 +20,7 @@ export interface SponsorTimeEditProps {
|
||||
|
||||
export interface SponsorTimeEditState {
|
||||
editing: boolean;
|
||||
sponsorTimeEdits: string[][];
|
||||
sponsorTimeEdits: [string, string];
|
||||
}
|
||||
|
||||
class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, SponsorTimeEditState> {
|
||||
@@ -40,7 +40,7 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
|
||||
this.state = {
|
||||
editing: false,
|
||||
sponsorTimeEdits: [[null, null], [null, null]]
|
||||
sponsorTimeEdits: [null, null]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -96,29 +96,18 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
{chrome.i18n.getMessage("bracketNow")}
|
||||
</span>
|
||||
|
||||
<input id={"submittingTimeMinutes0" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditMinutes"
|
||||
<input id={"submittingTime0" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditInput"
|
||||
ref={oldYouTubeDarkStyles}
|
||||
type="number"
|
||||
value={this.state.sponsorTimeEdits[0][0]}
|
||||
type="text"
|
||||
value={this.state.sponsorTimeEdits[0]}
|
||||
onChange={(e) => {
|
||||
let sponsorTimeEdits = this.state.sponsorTimeEdits;
|
||||
sponsorTimeEdits[0][0] = e.target.value;
|
||||
sponsorTimeEdits[0] = e.target.value;
|
||||
|
||||
this.setState({sponsorTimeEdits});
|
||||
}}>
|
||||
</input>
|
||||
|
||||
<input id={"submittingTimeSeconds0" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditSeconds"
|
||||
ref={oldYouTubeDarkStyles}
|
||||
type="number"
|
||||
value={this.state.sponsorTimeEdits[0][1]}
|
||||
onChange={(e) => {
|
||||
let sponsorTimeEdits = this.state.sponsorTimeEdits;
|
||||
sponsorTimeEdits[0][1] = e.target.value;
|
||||
|
||||
this.setState({sponsorTimeEdits});
|
||||
this.saveEditTimes();
|
||||
}}>
|
||||
</input>
|
||||
|
||||
@@ -126,29 +115,18 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
{" " + chrome.i18n.getMessage("to") + " "}
|
||||
</span>
|
||||
|
||||
<input id={"submittingTimeMinutes1" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditMinutes"
|
||||
<input id={"submittingTime1" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditInput"
|
||||
ref={oldYouTubeDarkStyles}
|
||||
type="text"
|
||||
value={this.state.sponsorTimeEdits[1][0]}
|
||||
value={this.state.sponsorTimeEdits[1]}
|
||||
onChange={(e) => {
|
||||
let sponsorTimeEdits = this.state.sponsorTimeEdits;
|
||||
sponsorTimeEdits[1][0] = e.target.value;
|
||||
sponsorTimeEdits[1] = e.target.value;
|
||||
|
||||
this.setState({sponsorTimeEdits});
|
||||
}}>
|
||||
</input>
|
||||
|
||||
<input id={"submittingTimeSeconds1" + this.idSuffix}
|
||||
className="sponsorTimeEdit sponsorTimeEditSeconds"
|
||||
ref={oldYouTubeDarkStyles}
|
||||
type="text"
|
||||
value={this.state.sponsorTimeEdits[1][1]}
|
||||
onChange={(e) => {
|
||||
let sponsorTimeEdits = this.state.sponsorTimeEdits;
|
||||
sponsorTimeEdits[1][1] = e.target.value;
|
||||
|
||||
this.setState({sponsorTimeEdits});
|
||||
this.saveEditTimes();
|
||||
}}>
|
||||
</input>
|
||||
|
||||
@@ -318,19 +296,23 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns an array in the sponsorTimeEdits form (minutes and seconds) from a normal seconds sponsor time */
|
||||
getFormattedSponsorTimesEdits(sponsorTime: SponsorTime): string[][] {
|
||||
return [[String(utils.getFormattedMinutes(sponsorTime.segment[0])), String(utils.getFormattedSeconds(sponsorTime.segment[0]))],
|
||||
[String(utils.getFormattedMinutes(sponsorTime.segment[1])), String(utils.getFormattedSeconds(sponsorTime.segment[1]))]];
|
||||
/** Returns an array in the sponsorTimeEdits form (formatted time string) from a normal seconds sponsor time */
|
||||
getFormattedSponsorTimesEdits(sponsorTime: SponsorTime): [string, string] {
|
||||
return [utils.getFormattedTime(sponsorTime.segment[0], true),
|
||||
utils.getFormattedTime(sponsorTime.segment[1], true)];
|
||||
}
|
||||
|
||||
saveEditTimes() {
|
||||
let sponsorTimesSubmitting = this.props.contentContainer().sponsorTimesSubmitting;
|
||||
|
||||
if (this.state.editing) {
|
||||
sponsorTimesSubmitting[this.props.index].segment =
|
||||
[utils.getRawSeconds(parseFloat(this.state.sponsorTimeEdits[0][0]), parseFloat(this.state.sponsorTimeEdits[0][1])),
|
||||
utils.getRawSeconds(parseFloat(this.state.sponsorTimeEdits[1][0]), parseFloat(this.state.sponsorTimeEdits[1][1]))];
|
||||
const startTime = utils.getFormattedTimeToSeconds(this.state.sponsorTimeEdits[0]);
|
||||
const endTime = utils.getFormattedTimeToSeconds(this.state.sponsorTimeEdits[1]);
|
||||
|
||||
// Change segment time only if the format was correct
|
||||
if (startTime !== null && endTime !== null) {
|
||||
sponsorTimesSubmitting[this.props.index].segment = [startTime, endTime];
|
||||
}
|
||||
}
|
||||
|
||||
sponsorTimesSubmitting[this.props.index].category = this.categoryOptionRef.current.value;
|
||||
@@ -346,11 +328,6 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
|
||||
let skipTime = sponsorTimes[index].segment[0];
|
||||
|
||||
if (this.state.editing) {
|
||||
// Save edits before previewing
|
||||
this.saveEditTimes();
|
||||
}
|
||||
|
||||
this.props.contentContainer().previewTime(skipTime - 2);
|
||||
}
|
||||
|
||||
@@ -360,11 +337,6 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
|
||||
|
||||
let skipTime = sponsorTimes[index].segment[0];
|
||||
|
||||
if (this.state.editing) {
|
||||
// Save edits before inspecting
|
||||
this.saveEditTimes();
|
||||
}
|
||||
|
||||
this.props.contentContainer().previewTime(skipTime + 0.000001, false);
|
||||
}
|
||||
|
||||
|
||||
26
src/utils.ts
26
src/utils.ts
@@ -323,14 +323,6 @@ class Utils {
|
||||
});
|
||||
}
|
||||
|
||||
getFormattedMinutes(seconds: number) {
|
||||
return Math.floor(seconds / 60);
|
||||
}
|
||||
|
||||
getFormattedSeconds(seconds: number) {
|
||||
return seconds % 60;
|
||||
}
|
||||
|
||||
getFormattedTime(seconds: number, precise?: boolean): string {
|
||||
let hours = Math.floor(seconds / 60 / 60);
|
||||
let minutes = Math.floor(seconds / 60) % 60;
|
||||
@@ -356,12 +348,22 @@ class Utils {
|
||||
return formatted;
|
||||
}
|
||||
|
||||
shortCategoryName(categoryName: string): string {
|
||||
return chrome.i18n.getMessage("category_" + categoryName + "_short") || chrome.i18n.getMessage("category_" + categoryName);
|
||||
getFormattedTimeToSeconds(formatted: string): number | null {
|
||||
const fragments = /^(?:(?:(\d+):)?(\d+):)?(\d*(?:\.\d+)?)$/.exec(formatted);
|
||||
|
||||
if (fragments === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const hours = fragments[1] ? parseInt(fragments[1]) : 0;
|
||||
const minutes = fragments[2] ? parseInt(fragments[2] || '0') : 0;
|
||||
const seconds = fragments[3] ? parseFloat(fragments[3]) : 0;
|
||||
|
||||
return hours * 3600 + minutes * 60 + seconds;
|
||||
}
|
||||
|
||||
getRawSeconds(minutes: number, seconds: number): number {
|
||||
return minutes * 60 + seconds;
|
||||
shortCategoryName(categoryName: string): string {
|
||||
return chrome.i18n.getMessage("category_" + categoryName + "_short") || chrome.i18n.getMessage("category_" + categoryName);
|
||||
}
|
||||
|
||||
isContentScript(): boolean {
|
||||
|
||||
Reference in New Issue
Block a user