Add ribbonIcon with ExoCommands

Add ExoCommand that opens random old note
This commit is contained in:
kitelev 2024-12-30 20:02:56 +05:00
parent 17a3bcb009
commit 4662be38ac
5 changed files with 64 additions and 20 deletions

7
Commands/ExoCommand.ts Normal file
View File

@ -0,0 +1,7 @@
import {App} from "obsidian";
export default interface ExoCommand {
name: string;
execute(app: App): Promise<void>;
}

10
Commands/ExoCommands.ts Normal file
View File

@ -0,0 +1,10 @@
import OpenRandomNoteExoCommand from "./OpenRandomNoteExoCommand";
import ExoCommand from "./ExoCommand";
export default class ExoCommands {
static all(): ExoCommand[] {
return [
new OpenRandomNoteExoCommand()
];
}
}

View File

@ -0,0 +1,18 @@
import {FuzzySuggestModal} from "obsidian";
import ExoCommand from "./ExoCommand";
import ExoCommands from "./ExoCommands";
export class ExoCommandsModal extends FuzzySuggestModal<ExoCommand> {
getItems(): ExoCommand[] {
return ExoCommands.all();
}
getItemText(cmd: ExoCommand): string {
return cmd.name;
}
async onChooseItem(cmd: ExoCommand, evt: MouseEvent | KeyboardEvent) {
await cmd.execute(this.app);
}
}

View File

@ -0,0 +1,25 @@
import ExoCommand from "./ExoCommand";
import {App, Notice} from "obsidian";
export default class OpenRandomNoteExoCommand implements ExoCommand {
name: string = "Рандомная заметка из прошлого";
async execute(app: App): Promise<void> {
const files = app.vault.getFiles();
const today = new Date();
const lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate()).setHours(0, 0, 0, 0); // Дата месяц назад без времени
// Фильтрация заметок с датой изменения раньше прошлого месяца
const oldNotes = files.filter(file => file.stat.mtime < lastMonth);
if (oldNotes.length > 0) {
// Выбираем случайную заметку
const randomNote = oldNotes[Math.floor(Math.random() * oldNotes.length)];
// Открываем её в активной панели
const leaf = app.workspace.getLeaf(false);
await leaf.openFile(randomNote);
} else {
new Notice("No old notes found.");
}
}
}

24
main.ts
View File

@ -1,26 +1,10 @@
import {Notice, Plugin} from 'obsidian'; import {Plugin} from 'obsidian';
import {ExoCommandsModal} from "./Commands/ExoCommandsModal";
export default class ExoPlugin extends Plugin { export default class ExoPlugin extends Plugin {
async onload() { async onload() {
this.addRibbonIcon('dice', 'Exo', () => { this.addRibbonIcon('star', 'Exocortex Commands List', () => {
this.showNotice(); new ExoCommandsModal(this.app).open();
}); });
this.addCommand({
id: 'exo-first-notice',
name: 'Shows simple Notice',
callback: () => {
this.showNotice()
}
})
}
async showNotice(): Promise<void> {
new Notice('This is Exocortex!');
}
onunload() {
} }
} }