2022-03-20 00:20:09 +01:00
|
|
|
import { ItemView, Plugin } from 'obsidian';
|
2022-03-20 00:31:54 +01:00
|
|
|
import { BrowserView, remote } from 'electron';
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
const viewName: string = "keep";
|
|
|
|
const padding: number = 5;
|
2022-03-19 20:21:16 +01:00
|
|
|
|
|
|
|
export default class MyPlugin extends Plugin {
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
async onload(): Promise<void> {
|
|
|
|
console.log('Loading obsidian-keep');
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
this.registerView(viewName, l => new KeepView(l));
|
2022-03-19 20:21:16 +01:00
|
|
|
this.addCommand({
|
2022-03-20 00:20:09 +01:00
|
|
|
id: "open-keep",
|
|
|
|
name: "Open Keep",
|
2022-03-19 20:21:16 +01:00
|
|
|
checkCallback: (checking: boolean) => {
|
2022-03-20 00:20:09 +01:00
|
|
|
if (checking)
|
|
|
|
return !this.app.workspace.getLeavesOfType(viewName).length;
|
|
|
|
this.openKeep();
|
|
|
|
},
|
2022-03-19 20:21:16 +01:00
|
|
|
});
|
2022-03-20 00:20:09 +01:00
|
|
|
this.app.workspace.onLayoutReady(() => this.openKeep());
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
private openKeep(): void {
|
|
|
|
if (!this.app.workspace.getLeavesOfType(viewName).length)
|
|
|
|
this.app.workspace.getRightLeaf(false).setViewState({ type: viewName });
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
export class KeepView extends ItemView {
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
private keep: BrowserView;
|
|
|
|
private visible: boolean;
|
|
|
|
private open: boolean;
|
|
|
|
private size: DOMRect;
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
async onload(): Promise<void> {
|
|
|
|
this.keep = new remote.BrowserView();
|
|
|
|
await this.keep.webContents.loadURL('https://keep.google.com');
|
|
|
|
this.registerInterval(window.setInterval(() => this.update(), 16.67));
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
onunload(): void {
|
|
|
|
this.hide();
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
onResize(): void {
|
|
|
|
this.resizeIfNecessary();
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
getViewType(): string {
|
|
|
|
return viewName;
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
getDisplayText(): string {
|
|
|
|
return "Google Keep";
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
getIcon(): string {
|
|
|
|
return "documents";
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
update() {
|
|
|
|
if (this.open) {
|
|
|
|
let covered = this.coveredByElement();
|
|
|
|
if (this.visible && covered) {
|
|
|
|
this.hide();
|
|
|
|
} else if (!this.visible && !covered) {
|
|
|
|
this.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.visible)
|
|
|
|
this.resizeIfNecessary();
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
hide() {
|
|
|
|
if (this.visible) {
|
|
|
|
remote.BrowserWindow.getFocusedWindow().removeBrowserView(this.keep);
|
|
|
|
this.visible = false;
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
show() {
|
|
|
|
if (!this.visible) {
|
|
|
|
remote.BrowserWindow.getFocusedWindow().addBrowserView(this.keep);
|
|
|
|
this.visible = true;
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
protected async onOpen(): Promise<void> {
|
|
|
|
this.show();
|
|
|
|
this.resizeIfNecessary();
|
|
|
|
this.open = true;
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
protected async onClose(): Promise<void> {
|
|
|
|
this.hide();
|
|
|
|
this.open = false;
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
private resizeIfNecessary(): void {
|
|
|
|
let rect = this.contentEl.getBoundingClientRect();
|
|
|
|
if (this.size && rect.x == this.size.x && rect.y == this.size.y && rect.width == this.size.width && rect.height == this.size.height)
|
|
|
|
return;
|
|
|
|
this.size = rect;
|
|
|
|
this.keep.setBounds({
|
|
|
|
x: Math.floor(rect.x) + padding,
|
|
|
|
y: Math.floor(rect.top) + padding,
|
|
|
|
width: Math.floor(rect.width) - 2 * padding,
|
|
|
|
height: Math.floor(rect.height) - 2 * padding
|
|
|
|
});
|
|
|
|
}
|
2022-03-19 20:21:16 +01:00
|
|
|
|
2022-03-20 00:20:09 +01:00
|
|
|
private coveredByElement(): boolean {
|
|
|
|
let nodes = document.body.querySelectorAll(".modal-bg, .menu");
|
|
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
|
|
let rect = nodes[i].getBoundingClientRect();
|
|
|
|
if (rect.left < this.size.right && this.size.left < rect.right && rect.top < this.size.bottom && this.size.top < rect.bottom)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-03-19 20:21:16 +01:00
|
|
|
}
|
2022-03-20 00:20:09 +01:00
|
|
|
}
|