ObsidianCustomFrames/src/view.ts

90 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);
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");
2022-04-13 21:21:48 +02:00
this.contentEl.appendChild(this.frame.create());
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);
if (source == "tab-header") {
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
}
}
interface Action {
name: string;
icon: string;
action: (view: CustomFrameView) => any;
2022-08-21 18:22:15 +02:00
}