mirror of
https://github.com/devonthesofa/obsidian-note-status.git
synced 2026-07-22 05:45:04 +00:00
92 lines
2.1 KiB
TypeScript
92 lines
2.1 KiB
TypeScript
import { App, Editor, MarkdownView, Menu } from "obsidian";
|
|
import { NoteStatusSettings } from "../../models/types";
|
|
import { StatusService } from "services/status-service";
|
|
import { StatusDropdown } from "components/status-dropdown";
|
|
|
|
export class EditorIntegration {
|
|
private app: App;
|
|
private settings: NoteStatusSettings;
|
|
private statusService: StatusService;
|
|
private statusDropdown: StatusDropdown;
|
|
private editorMenuRef: (
|
|
menu: Menu,
|
|
editor: Editor,
|
|
view: MarkdownView,
|
|
) => void;
|
|
|
|
constructor(
|
|
app: App,
|
|
settings: NoteStatusSettings,
|
|
statusService: StatusService,
|
|
statusDropdown: StatusDropdown,
|
|
) {
|
|
this.app = app;
|
|
this.settings = settings;
|
|
this.statusService = statusService;
|
|
this.statusDropdown = statusDropdown;
|
|
}
|
|
|
|
public updateSettings(settings: NoteStatusSettings): void {
|
|
this.settings = settings;
|
|
}
|
|
|
|
public registerEditorMenus(): void {
|
|
this.editorMenuRef = (
|
|
menu: Menu,
|
|
editor: Editor,
|
|
view: MarkdownView,
|
|
) => {
|
|
if (view.file) {
|
|
this.addStatusMenuItems(menu, editor, view);
|
|
}
|
|
};
|
|
|
|
this.app.workspace.on("editor-menu", this.editorMenuRef);
|
|
}
|
|
|
|
private addStatusMenuItems(
|
|
menu: Menu,
|
|
editor: Editor,
|
|
view: MarkdownView,
|
|
): void {
|
|
menu.addItem((item) =>
|
|
item
|
|
.setTitle("Change note status")
|
|
.setIcon("tag")
|
|
.onClick(() => {
|
|
if (view.file) {
|
|
this.statusDropdown.openStatusDropdown({
|
|
files: [view.file],
|
|
editor,
|
|
view,
|
|
});
|
|
}
|
|
}),
|
|
);
|
|
|
|
// Only show insert metadata if it doesn't exist
|
|
if (view.file) {
|
|
const statuses = this.statusService.getFileStatuses(view.file);
|
|
if (statuses.length === 1 && statuses[0] === "unknown") {
|
|
menu.addItem((item) =>
|
|
item
|
|
.setTitle("Insert status metadata")
|
|
.setIcon("plus-circle")
|
|
.onClick(() => {
|
|
this.insertStatusMetadata(editor);
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
public insertStatusMetadata(editor: Editor): void {
|
|
this.statusService.insertStatusMetadataInEditor(editor);
|
|
}
|
|
|
|
public unload(): void {
|
|
if (this.editorMenuRef) {
|
|
this.app.workspace.off("editor-menu", this.editorMenuRef);
|
|
}
|
|
}
|
|
}
|