ObsidianCustomFrames/src/view.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-04-15 13:22:09 +02:00
import { ItemView, WorkspaceLeaf, Menu } from "obsidian";
2022-04-13 21:21:48 +02:00
import { CustomFrame } from "./frame";
2022-04-15 13:22:09 +02:00
import { CustomFrameSettings, CustomFramesSettings, getIcon } from "./settings";
2022-03-29 13:18:25 +02:00
export class CustomFrameView extends ItemView {
private static readonly actions: Action[] = [
{
name: "Return to original page",
icon: "home",
2022-04-13 21:21:48 +02:00
action: v => v.frame.return()
2022-03-29 16:04:55 +02:00
}, {
2022-03-29 15:53:18 +02:00
name: "Open dev tools",
icon: "binary",
2022-04-13 21:21:48 +02:00
action: v => v.frame.toggleDevTools()
2022-03-29 16:04:55 +02:00
}, {
2022-03-29 15:53:18 +02:00
name: "Copy link",
icon: "link",
action: v => navigator.clipboard.writeText(v.frame.getCurrentUrl())
}, {
name: "Open in browser",
icon: "globe",
action: v => open(v.frame.getCurrentUrl())
}, {
name: "Refresh",
icon: "refresh-cw",
2022-04-13 21:21:48 +02:00
action: v => v.frame.refresh()
}, {
name: "Go back",
icon: "arrow-left",
2022-04-13 21:21:48 +02:00
action: v => v.frame.goBack()
}, {
name: "Go forward",
icon: "arrow-right",
2022-04-13 21:21:48 +02:00
action: v => v.frame.goForward()
}
];
2022-04-13 21:21:48 +02:00
private readonly data: CustomFrameSettings;
2022-03-29 13:18:25 +02:00
private readonly name: string;
2022-04-13 21:21:48 +02:00
private frame: CustomFrame;
2022-03-29 13:18:25 +02:00
2022-04-13 21:21:48 +02:00
constructor(leaf: WorkspaceLeaf, settings: CustomFramesSettings, data: CustomFrameSettings, name: string) {
2022-03-29 13:18:25 +02:00
super(leaf);
this.data = data;
this.name = name;
this.frame = new CustomFrame(settings, data);
this.navigation = data.openInCenter;
2022-03-29 13:18:25 +02:00
for (let action of CustomFrameView.actions)
this.addAction(action.icon, action.name, () => action.action(this));
2022-03-29 13:18:25 +02:00
}
onload(): void {
this.contentEl.empty();
this.contentEl.addClass("custom-frames-view");
this.frame.create(this.contentEl);
2022-03-29 13:18:25 +02:00
}
2022-08-21 18:22:15 +02:00
onPaneMenu(menu: Menu, source: string): void {
super.onPaneMenu(menu, source);
for (let action of CustomFrameView.actions) {
menu.addItem(i => {
i.setTitle(action.name);
i.setIcon(action.icon);
i.onClick(() => action.action(this));
});
}
2022-03-29 13:18:25 +02:00
}
getViewType(): string {
return this.name;
}
getDisplayText(): string {
return this.data.displayName;
}
getIcon(): string {
2022-04-15 13:22:09 +02:00
return getIcon(this.data);
2022-03-29 13:18:25 +02:00
}
focus(): void {
this.frame.focus();
}
}
interface Action {
name: string;
icon: string;
action: (view: CustomFrameView) => any;
2022-08-21 18:22:15 +02:00
}