update
This commit is contained in:
93
main.ts
93
main.ts
@@ -8,10 +8,8 @@ import {
|
||||
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;
|
||||
import { parseProjectFilePath } from "./src/fs";
|
||||
import { BreadcrumbSegment, injectMobileBreadcrumb } from "./src/ui";
|
||||
|
||||
export default class ProjektkontextPlugin extends Plugin {
|
||||
async onload(): Promise<void> {
|
||||
@@ -29,7 +27,16 @@ export default class ProjektkontextPlugin extends Plugin {
|
||||
callback: () => void this.activateProjectsView(),
|
||||
});
|
||||
|
||||
if (Platform.isMobile) this.installMobileSwipe();
|
||||
if (Platform.isMobile) {
|
||||
const reattach = () => this.reattachMobileBreadcrumbs();
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("file-open", () => requestAnimationFrame(reattach)),
|
||||
);
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("active-leaf-change", () => requestAnimationFrame(reattach)),
|
||||
);
|
||||
this.registerEvent(this.app.workspace.on("layout-change", reattach));
|
||||
}
|
||||
}
|
||||
|
||||
async onunload(): Promise<void> {}
|
||||
@@ -44,40 +51,50 @@ export default class ProjektkontextPlugin extends Plugin {
|
||||
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 reattachMobileBreadcrumbs(): void {
|
||||
this.app.workspace.iterateRootLeaves((leaf) => this.applyMobileBreadcrumb(leaf));
|
||||
}
|
||||
|
||||
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);
|
||||
private applyMobileBreadcrumb(leaf: WorkspaceLeaf): void {
|
||||
const view = leaf.view;
|
||||
if (!(view instanceof MarkdownView)) return;
|
||||
const file = view.file;
|
||||
if (!file) {
|
||||
injectMobileBreadcrumb(view, []);
|
||||
return;
|
||||
}
|
||||
const loc = parseProjectFilePath(file.path);
|
||||
if (!loc) {
|
||||
injectMobileBreadcrumb(view, []);
|
||||
return;
|
||||
}
|
||||
const segments: BreadcrumbSegment[] = [
|
||||
{
|
||||
label: "Projekte",
|
||||
onClick: () => void leaf.setViewState({ type: VIEW_TYPE_PROJECTS, active: true }),
|
||||
},
|
||||
{
|
||||
label: loc.project,
|
||||
onClick: () =>
|
||||
void leaf.setViewState({
|
||||
type: VIEW_TYPE_OVERVIEW,
|
||||
active: true,
|
||||
state: { project: loc.project },
|
||||
}),
|
||||
},
|
||||
];
|
||||
if (loc.area) {
|
||||
segments.push({
|
||||
label: loc.area,
|
||||
onClick: () =>
|
||||
void leaf.setViewState({
|
||||
type: VIEW_TYPE_DETAILS,
|
||||
active: true,
|
||||
state: { project: loc.project, area: loc.area },
|
||||
}),
|
||||
});
|
||||
}
|
||||
segments.push({ label: file.basename });
|
||||
injectMobileBreadcrumb(view, segments);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user