Skip to content
Draft
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
37 changes: 37 additions & 0 deletions src/CommandSuggestModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { App, SuggestModal } from "obsidian";

interface CommandOption {
id: string;
name: string;
}

export class CommandSuggestModal extends SuggestModal<CommandOption> {
private onChoose: (command: CommandOption) => void;

constructor(app: App, onChoose: (command: CommandOption) => void) {
super(app);
this.onChoose = onChoose;
}

getSuggestions(query: string): CommandOption[] {
const commands = Object.values((this.app as any).commands.commands).map(
(cmd: any) => ({ id: cmd.id, name: cmd.name })
);
return commands.filter(
(cmd) =>
cmd.name.toLowerCase().includes(query.toLowerCase()) ||
cmd.id.toLowerCase().includes(query.toLowerCase())
);
}

renderSuggestion(command: CommandOption, el: HTMLElement) {
el.createEl("div", { text: `${command.name} (${command.id})` });
}

onChooseSuggestion(
command: CommandOption,
evt: MouseEvent | KeyboardEvent
) {
this.onChoose(command);
}
}
19 changes: 19 additions & 0 deletions src/settings-tab.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { App, PluginSettingTab, Setting } from "obsidian";
import SimpleTimeTrackerPlugin from "./main";
import { defaultSettings } from "./settings";
import { CommandSuggestModal } from "./CommandSuggestModal";

export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {

Expand Down Expand Up @@ -84,6 +85,24 @@ export class SimpleTimeTrackerSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});

new Setting(this.containerEl)
.setName("Tracker End Command")
.setDesc("Select the command to execute when a tracker is ended.")
.addButton((button) => {
button.setButtonText(
this.plugin.settings.trackerEndCommand
? this.plugin.settings.trackerEndCommand
: "Select Command"
);
button.onClick(() => {
new CommandSuggestModal(this.app, (command) => {
this.plugin.settings.trackerEndCommand = command.id;
button.setButtonText(`${command.name}`);
this.plugin.saveSettings();
}).open();
});
});

this.containerEl.createEl("hr");
this.containerEl.createEl("p", { text: "Need help using the plugin? Feel free to join the Discord server!" });
Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const defaultSettings: SimpleTimeTrackerSettings = {
reverseSegmentOrder: false,
timestampDurations: false,
showToday: false,
trackerEndCommand: "",
};

export interface SimpleTimeTrackerSettings {
Expand All @@ -17,4 +18,5 @@ export interface SimpleTimeTrackerSettings {
reverseSegmentOrder: boolean;
timestampDurations: boolean;
showToday: boolean;
trackerEndCommand: string;
}
6 changes: 6 additions & 0 deletions src/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export function displayTracker(tracker: Tracker, element: HTMLElement, getFile:
startNewEntry(tracker, newSegmentNameBox.getValue());
}
await saveTracker(tracker, getFile(), getSectionInfo());
if (running) {
const commandId = settings.trackerEndCommand;
if (commandId) {
(app as any).commands.executeCommandById(commandId);
}
}
});
btn.buttonEl.addClass("simple-time-tracker-btn");
let newSegmentNameBox = new TextComponent(element)
Expand Down