Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/API/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ export class API implements IAPI {
* Gets the current time in the given moment format.
* @param format Moment format.
* @param linkify Linking to the podcast so PodNotes can open it at this time later.
* @param offsetSeconds Optional offset to subtract from the current playback time.
* @returns
*/
getPodcastTimeFormatted(format: string, linkify = false): string {
getPodcastTimeFormatted(
format: string,
linkify = false,
offsetSeconds = 0,
): string {
if (!this.podcast) {
throw new Error("No podcast loaded");
}

const time = formatSeconds(this.currentTime, format);
const adjustedTime = Math.max(0, this.currentTime - offsetSeconds);
const time = formatSeconds(adjustedTime, format);

if (!linkify) return time;

Expand All @@ -63,7 +69,7 @@ export class API implements IAPI {
const url = encodePodnotesURI(
this.podcast.title,
feedUrl,
this.currentTime
adjustedTime,
);

return `[${time}](${url.href})`;
Expand Down
6 changes: 5 additions & 1 deletion src/API/IAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export interface IAPI {
readonly length: number;
currentTime: number;

getPodcastTimeFormatted(format: string, linkify?: boolean): string;
getPodcastTimeFormatted(
format: string,
linkify?: boolean,
offsetSeconds?: number,
): string;

start(): void;
stop(): void;
Expand Down
6 changes: 4 additions & 2 deletions src/TemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ export function NoteTemplateEngine(template: string, episode: Episode) {

export function TimestampTemplateEngine(template: string) {
const [replacer, addTag] = useTemplateEngine();
const { api, settings } = get(plugin);
const timestampOffset = settings.timestamp.offset ?? 0;

addTag("time", (format?: string) =>
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss"),
api.getPodcastTimeFormatted(format ?? "HH:mm:ss", false, timestampOffset),
);
addTag("linktime", (format?: string) =>
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss", true),
api.getPodcastTimeFormatted(format ?? "HH:mm:ss", true, timestampOffset),
);

return replacer(template);
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const DEFAULT_SETTINGS: IPodNotesSettings = {

timestamp: {
template: "- {{time}} ",
offset: 0,
},

note: {
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,13 @@ export default class PodNotes extends Plugin implements IPodNotes {
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
const loadedData = await this.loadData();

this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedData);
this.settings.timestamp = {
...DEFAULT_SETTINGS.timestamp,
...(loadedData?.timestamp ?? {}),
};
}

async saveSettings() {
Expand Down
1 change: 1 addition & 0 deletions src/types/IPodNotesSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IPodNotesSettings {

timestamp: {
template: string;
offset: number;
};

note: {
Expand Down
20 changes: 20 additions & 0 deletions src/ui/settings/PodNotesSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ export class PodNotesSettingsTab extends PluginSettingTab {
);
};

new Setting(container)
.setName("Timestamp offset (s)")
.setDesc(
"Subtract this many seconds when capturing a timestamp to compensate for reaction time.",
)
.addText((textComponent) => {
textComponent.inputEl.type = "number";
textComponent
.setValue(`${this.plugin.settings.timestamp.offset}`)
.onChange((value) => {
const parsedValue = Number.parseInt(value, 10);
this.plugin.settings.timestamp.offset = Number.isNaN(parsedValue)
? 0
: Math.max(0, parsedValue);
this.plugin.saveSettings();
updateTimestampDemo(this.plugin.settings.timestamp.template);
})
.setPlaceholder("e.g. 5");
});

updateTimestampDemo(this.plugin.settings.timestamp.template);

const randomEpisode = getRandomEpisode();
Expand Down