focus a frame's content when it gets opened

This commit is contained in:
Ell 2022-09-17 18:28:06 +02:00
parent 33f3407afe
commit 4340d08e29
4 changed files with 43 additions and 13 deletions

View file

@ -12,7 +12,7 @@ export class CustomFrame {
this.data = data; this.data = data;
} }
public create(additionalStyle: string = undefined, urlSuffix: string = undefined): any { create(additionalStyle: string = undefined, urlSuffix: string = undefined): any {
let style = `padding: ${this.settings.padding}px;`; let style = `padding: ${this.settings.padding}px;`;
if (additionalStyle) if (additionalStyle)
style += additionalStyle; style += additionalStyle;
@ -44,7 +44,7 @@ export class CustomFrame {
return this.frame; return this.frame;
} }
public refresh(): void { refresh(): void {
if (this.frame instanceof HTMLIFrameElement) { if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.location.reload(); this.frame.contentWindow.location.reload();
} else { } else {
@ -52,7 +52,7 @@ export class CustomFrame {
} }
} }
public return(): void { return(): void {
if (this.frame instanceof HTMLIFrameElement) { if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.open(this.data.url); this.frame.contentWindow.open(this.data.url);
} else { } else {
@ -60,7 +60,7 @@ export class CustomFrame {
} }
} }
public goBack(): void { goBack(): void {
if (this.frame instanceof HTMLIFrameElement) { if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.history.back(); this.frame.contentWindow.history.back();
} else { } else {
@ -68,7 +68,7 @@ export class CustomFrame {
} }
} }
public goForward(): void { goForward(): void {
if (this.frame instanceof HTMLIFrameElement) { if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.history.forward(); this.frame.contentWindow.history.forward();
} else { } else {
@ -76,7 +76,7 @@ export class CustomFrame {
} }
} }
public toggleDevTools(): void { toggleDevTools(): void {
if (!(this.frame instanceof HTMLIFrameElement)) { if (!(this.frame instanceof HTMLIFrameElement)) {
if (!this.frame.isDevToolsOpened()) { if (!this.frame.isDevToolsOpened()) {
this.frame.openDevTools(); this.frame.openDevTools();
@ -86,7 +86,15 @@ export class CustomFrame {
} }
} }
public getCurrentUrl(): string { getCurrentUrl(): string {
return this.frame instanceof HTMLIFrameElement ? this.frame.contentWindow.location.href : this.frame.getURL(); return this.frame instanceof HTMLIFrameElement ? this.frame.contentWindow.location.href : this.frame.getURL();
} }
focus(): void {
if (this.frame instanceof HTMLIFrameElement) {
this.frame.contentWindow.focus();
} else {
this.frame.focus();
}
}
} }

View file

@ -1,4 +1,4 @@
import { Plugin, Platform } from "obsidian"; import { Plugin, Platform, WorkspaceLeaf } from "obsidian";
import { CustomFrame } from "./frame"; import { CustomFrame } from "./frame";
import { CustomFramesSettings, defaultSettings, getIcon, getId } from "./settings"; import { CustomFramesSettings, defaultSettings, getIcon, getId } from "./settings";
import { CustomFramesSettingTab } from "./settings-tab"; import { CustomFramesSettingTab } from "./settings-tab";
@ -81,13 +81,17 @@ export default class CustomFramesPlugin extends Plugin {
} }
private async openLeaf(name: string, center: boolean, split: boolean): Promise<void> { private async openLeaf(name: string, center: boolean, split: boolean): Promise<void> {
let leaf: WorkspaceLeaf;
if (center) { if (center) {
let leaf = this.app.workspace.getLeaf(split); leaf = this.app.workspace.getLeaf(split);
await leaf.setViewState({ type: name, active: true }); await leaf.setViewState({ type: name, active: true });
} else { } else {
if (!this.app.workspace.getLeavesOfType(name).length) if (!this.app.workspace.getLeavesOfType(name).length)
await this.app.workspace.getRightLeaf(false).setViewState({ type: name, active: true }); await this.app.workspace.getRightLeaf(false).setViewState({ type: name, active: true });
this.app.workspace.revealLeaf(this.app.workspace.getLeavesOfType(name)[0]); leaf = this.app.workspace.getLeavesOfType(name)[0];
this.app.workspace.revealLeaf(leaf);
} }
if (leaf.view instanceof CustomFrameView)
leaf.view.focus();
} }
} }

View file

@ -80,6 +80,10 @@ export class CustomFrameView extends ItemView {
getIcon(): string { getIcon(): string {
return getIcon(this.data); return getIcon(this.data);
} }
focus(): void {
this.frame.focus();
}
} }
interface Action { interface Action {

File diff suppressed because one or more lines are too long