Skip to content

Commit

Permalink
feat: port global commands
Browse files Browse the repository at this point in the history
  • Loading branch information
srg-kostyrko committed Aug 23, 2024
1 parent 1e03f14 commit 523dc4e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const CALENDAR_VIEW_TYPE = "journal-calendar";

export const FRONTMATTER_DATE_FORMAT = "YYYY-MM-DD";
export const FRONTMATTER_ID_KEY = "journal";
export const FRONTMATTER_START_DATE_KEY = "journal-start-date";
Expand Down
6 changes: 5 additions & 1 deletion src/journals/journals-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export class JournalsIndex extends Component {
this.#setupListeners();
}

getPathComputed(path: string) {
getForPath(path: string): JournalMetadata | null {
return this.#pathIndex.value.get(path) ?? null;
}

getForPathComputed(path: string) {
let cmp = this.#pathComputeds.get(path);
if (cmp) return cmp;
cmp = computed(() => this.#pathIndex.value.get(path) ?? null);
Expand Down
97 changes: 95 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type App, Plugin } from "obsidian";
import { calendarSettings$, journals$, pluginSettings$ } from "./stores/settings.store";
import { type App, Notice, Plugin } from "obsidian";
import { calendarSettings$, calendarViewSettings$, journals$, pluginSettings$ } from "./stores/settings.store";
import { watch, type WatchStopHandle } from "vue";
import { debounce } from "perfect-debounce";
import { initCalendarCustomization, updateLocale } from "./calendar";
Expand All @@ -10,6 +10,7 @@ import type { JournalSettings } from "./types/settings.types";
import { defaultJournalSettings } from "./defaults";
import { prepareJournalDefaultsBasedOnType } from "./journals/journal-defaults";
import { JournalsIndex } from "./journals/journals-index";
import { CALENDAR_VIEW_TYPE } from "./constants";

export default class JournalPlugin extends Plugin {
#stopHandles: WatchStopHandle[] = [];
Expand Down Expand Up @@ -37,6 +38,20 @@ export default class JournalPlugin extends Plugin {
delete pluginSettings$.value.journals[id];
}

placeCalendarView(moving = false) {
if (this.app.workspace.getLeavesOfType(CALENDAR_VIEW_TYPE).length > 0) {
if (!moving) return;
this.app.workspace.getLeavesOfType(CALENDAR_VIEW_TYPE).forEach((leaf) => {
leaf.detach();
});
}
if (calendarViewSettings$.value.leaf === "left") {
this.app.workspace.getLeftLeaf(false)?.setViewState({ type: CALENDAR_VIEW_TYPE });
} else {
this.app.workspace.getRightLeaf(false)?.setViewState({ type: CALENDAR_VIEW_TYPE });
}
}

async onload(): Promise<void> {
app$.value = this.app;
plugin$.value = this;
Expand All @@ -50,6 +65,8 @@ export default class JournalPlugin extends Plugin {
this.addChild(this.#index);
this.index.reindex();

this.#configureCommands();

this.addSettingTab(new JournalSettingTab(this.app, this));
}
onunload(): void {
Expand Down Expand Up @@ -98,4 +115,80 @@ export default class JournalPlugin extends Plugin {
}),
);
}

#configureCommands(): void {
this.addCommand({
id: "open-next",
name: "Open next note",
editorCallback: async (editor, ctx) => {
const file = ctx.file;
if (!file) return;
const metadata = this.#index.getForPath(file.path);
if (!metadata) {
new Notice("This note is not connected to any journal.");
return;
}
const journal = this.#journals.get(metadata.id);
if (!journal) {
new Notice("Unknown journal id.");
return;
}
const nextNote = await journal.next(metadata, true);
if (!nextNote) {
new Notice("There is no next note after this one.");
return;
}
await journal.open(nextNote);
},
});
this.addCommand({
id: "open-prev",
name: "Open previous note",
editorCallback: async (editor, ctx) => {
const file = ctx.file;
if (!file) return;
const metadata = this.#index.getForPath(file.path);
if (!metadata) {
new Notice("This note is not connected to any journal.");
return;
}
const journal = this.#journals.get(metadata.id);
if (!journal) {
new Notice("Unknown journal id.");
return;
}
const prevNote = await journal.previous(metadata, true);
if (!prevNote) {
new Notice("There is no previous note before this one.");
return;
}
await journal.open(prevNote);
},
});

this.addCommand({
id: "connect-note",
name: "Connect note to a journal",
editorCallback: async (editor, ctx) => {
const file = ctx.file;
if (file) {
// TODO
// new ConnectNoteModal(this.app, this, file).open();
}
},
});

this.addCommand({
id: "open-calendar",
name: "Open calendar",
callback: () => {
let [leaf] = this.app.workspace.getLeavesOfType(CALENDAR_VIEW_TYPE);
if (!leaf) {
this.placeCalendarView();
leaf = this.app.workspace.getLeavesOfType(CALENDAR_VIEW_TYPE)[0];
}
this.app.workspace.revealLeaf(leaf);
},
});
}
}

0 comments on commit 523dc4e

Please sign in to comment.