ObsidianCustomFrames/src/view.ts

152 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-03-29 13:18:25 +02:00
import { ItemView, WorkspaceLeaf, Platform, Menu } from "obsidian";
import { CustomFrame, CustomFramesSettings } from "./settings";
export class CustomFrameView extends ItemView {
private static readonly actions: Action[] = [
{
name: "Return to original page",
icon: "home",
action: v => v.return()
2022-03-29 16:04:55 +02:00
}, {
2022-03-29 15:53:18 +02:00
name: "Open dev tools",
icon: "binary",
action: v => v.toggleDevTools()
2022-03-29 16:04:55 +02:00
}, {
2022-03-29 15:53:18 +02:00
name: "Copy link",
icon: "link",
action: v => v.copyLink()
}, {
name: "Refresh",
icon: "refresh-cw",
action: v => v.refresh()
}, {
name: "Go back",
icon: "arrow-left",
action: v => v.goBack()
}, {
name: "Go forward",
icon: "arrow-right",
action: v => v.goForward()
}
];
2022-03-29 13:18:25 +02:00
private readonly settings: CustomFramesSettings;
private readonly data: CustomFrame;
private readonly name: string;
private frame: HTMLIFrameElement | any;
constructor(leaf: WorkspaceLeaf, settings: CustomFramesSettings, data: CustomFrame, name: string) {
super(leaf);
this.settings = settings;
this.data = data;
this.name = name;
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");
let style = `padding: ${this.settings.padding}px;`;
2022-03-29 13:18:25 +02:00
if (Platform.isDesktopApp) {
this.frame = document.createElement("webview");
this.frame.setAttribute("allowpopups", "");
this.frame.addEventListener("dom-ready", () => {
this.frame.setZoomFactor(this.data.zoomLevel);
2022-03-29 13:18:25 +02:00
this.frame.insertCSS(this.data.customCss);
});
}
else {
this.frame = document.createElement("iframe");
this.frame.setAttribute("sandbox", "allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts allow-top-navigation-by-user-activation");
this.frame.setAttribute("allow", "encrypted-media; fullscreen; oversized-images; picture-in-picture; sync-xhr; geolocation;");
style += `transform: scale(${this.data.zoomLevel}); transform-origin: 0 0;`;
2022-03-29 13:18:25 +02:00
}
this.frame.addClass("custom-frames-frame");
this.frame.setAttribute("style", style);
2022-03-29 13:18:25 +02:00
this.frame.setAttribute("src", this.data.url);
this.contentEl.appendChild(this.frame);
}
onHeaderMenu(menu: Menu): void {
super.onHeaderMenu(menu);
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 {
return this.data.icon ? `lucide-${this.data.icon}` : "documents";
}
private refresh(): void {
if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.location.reload();
} else {
this.frame.reload();
}
}
private return(): void {
if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.open(this.data.url);
} else {
this.frame.loadURL(this.data.url);
}
}
private goBack(): void {
if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.history.back();
}
else {
this.frame.goBack();
}
}
private goForward(): void {
2022-03-29 13:18:25 +02:00
if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.history.forward();
}
else {
this.frame.goForward();
}
}
private toggleDevTools(): void {
if (!(this.frame instanceof HTMLIFrameElement)) {
if (!this.frame.isDevToolsOpened()) {
this.frame.openDevTools();
}
else {
this.frame.closeDevTools();
}
}
}
2022-03-29 15:53:18 +02:00
private copyLink(): void {
let link = this.frame instanceof HTMLIFrameElement ? this.frame.contentWindow.location.href : this.frame.getURL();
navigator.clipboard.writeText(link);
}
}
interface Action {
name: string;
icon: string;
action: (view: CustomFrameView) => any;
2022-03-29 13:18:25 +02:00
}