Files
management/main.ts
2026-04-30 20:10:14 +02:00

84 lines
2.6 KiB
TypeScript

import { MarkdownView, Platform, Plugin, WorkspaceLeaf } from "obsidian";
import {
VIEW_TYPE_PROJECTS,
VIEW_TYPE_OVERVIEW,
VIEW_TYPE_DETAILS,
RIBBON_ICON,
} from "./src/const";
import { ProjectsView } from "./src/views/ProjectsView";
import { OverviewView } from "./src/views/OverviewView";
import { DetailsView } from "./src/views/DetailsView";
import { consumeMobileReturn } from "./src/ui";
const SWIPE_THRESHOLD = 80;
const SWIPE_MAX_VERTICAL = 50;
export default class ProjektkontextPlugin extends Plugin {
async onload(): Promise<void> {
this.registerView(VIEW_TYPE_PROJECTS, (leaf) => new ProjectsView(leaf));
this.registerView(VIEW_TYPE_OVERVIEW, (leaf) => new OverviewView(leaf));
this.registerView(VIEW_TYPE_DETAILS, (leaf) => new DetailsView(leaf));
this.addRibbonIcon(RIBBON_ICON, "Projekte", () => {
void this.activateProjectsView();
});
this.addCommand({
id: "open-projects",
name: "Projekte öffnen",
callback: () => void this.activateProjectsView(),
});
if (Platform.isMobile) this.installMobileSwipe();
}
async onunload(): Promise<void> {}
async activateProjectsView(): Promise<void> {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = workspace.getLeavesOfType(VIEW_TYPE_PROJECTS)[0] ?? null;
if (!leaf) {
leaf = workspace.getLeaf(false);
await leaf.setViewState({ type: VIEW_TYPE_PROJECTS, active: true });
}
workspace.revealLeaf(leaf);
}
private installMobileSwipe(): void {
let startX = 0;
let startY = 0;
let active = false;
this.registerDomEvent(document, "touchstart", (ev: TouchEvent) => {
if (ev.touches.length !== 1) return;
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
startX = ev.touches[0].clientX;
startY = ev.touches[0].clientY;
active = true;
});
this.registerDomEvent(document, "touchend", (ev: TouchEvent) => {
if (!active) return;
active = false;
const t = ev.changedTouches[0];
const dx = t.clientX - startX;
const dy = Math.abs(t.clientY - startY);
if (dx < SWIPE_THRESHOLD || dy > SWIPE_MAX_VERTICAL) return;
const target = consumeMobileReturn();
if (!target) return;
void this.restoreView(target.type, target.state);
});
}
private async restoreView(type: string, state: unknown): Promise<void> {
const leaf = this.app.workspace.getLeaf(false);
await leaf.setViewState({
type,
active: true,
state: (state as Record<string, unknown>) ?? {},
});
this.app.workspace.revealLeaf(leaf);
}
}