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 {
|
|
|
|
|
2022-03-29 15:15:34 +02:00
|
|
|
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()
|
2022-03-29 15:15:34 +02:00
|
|
|
}, {
|
|
|
|
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;
|
|
|
|
|
2022-03-29 15:15:34 +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");
|
|
|
|
|
|
|
|
if (Platform.isDesktopApp) {
|
|
|
|
this.frame = document.createElement("webview");
|
|
|
|
this.frame.setAttribute("allowpopups", "");
|
|
|
|
this.frame.addEventListener("dom-ready", () => {
|
|
|
|
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;");
|
|
|
|
}
|
|
|
|
this.frame.addClass("custom-frames-frame");
|
|
|
|
this.frame.setAttribute("style", `padding: ${this.settings.padding}px`);
|
|
|
|
this.frame.setAttribute("src", this.data.url);
|
|
|
|
this.contentEl.appendChild(this.frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
onHeaderMenu(menu: Menu): void {
|
|
|
|
super.onHeaderMenu(menu);
|
2022-03-29 15:15:34 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 15:15:34 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 15:15:34 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-03-29 15:15:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Action {
|
|
|
|
name: string;
|
|
|
|
icon: string;
|
|
|
|
action: (view: CustomFrameView) => any;
|
2022-03-29 13:18:25 +02:00
|
|
|
}
|