added automatic width calculation

This commit is contained in:
Ell 2022-03-20 19:32:21 +01:00
parent 57e0cb6a33
commit 578c7fd73b
2 changed files with 51 additions and 1300 deletions

55
main.ts
View file

@ -3,6 +3,7 @@ import { BrowserView, remote } from 'electron';
const viewName: string = "keep"; const viewName: string = "keep";
const defaultSettings: KeepSettings = { const defaultSettings: KeepSettings = {
minimumWidth: 356,
padding: 5, padding: 5,
css: `/* hide the menu bar and the "Keep" logo and text */ css: `/* hide the menu bar and the "Keep" logo and text */
.PvRhvb-qAWA2, .gb_qc { .PvRhvb-qAWA2, .gb_qc {
@ -19,6 +20,7 @@ const defaultSettings: KeepSettings = {
} }
interface KeepSettings { interface KeepSettings {
minimumWidth: number;
padding: number; padding: number;
css: string; css: string;
} }
@ -77,6 +79,7 @@ class KeepView extends ItemView {
await this.keep.webContents.loadURL('https://keep.google.com'); await this.keep.webContents.loadURL('https://keep.google.com');
await this.keep.webContents.insertCSS(this.settings.css); await this.keep.webContents.insertCSS(this.settings.css);
this.registerInterval(window.setInterval(() => this.update(), 33.33)); this.registerInterval(window.setInterval(() => this.update(), 33.33));
this.resizeIfNecessary();
} }
onunload(): void { onunload(): void {
@ -107,11 +110,11 @@ class KeepView extends ItemView {
} else if (!this.visible && !covered) { } else if (!this.visible && !covered) {
this.show(); this.show();
} }
}
if (this.visible) if (this.visible)
this.resizeIfNecessary(); this.resizeIfNecessary();
} }
}
hide() { hide() {
if (this.visible) { if (this.visible) {
@ -124,18 +127,16 @@ class KeepView extends ItemView {
if (!this.visible) { if (!this.visible) {
remote.BrowserWindow.getFocusedWindow().addBrowserView(this.keep); remote.BrowserWindow.getFocusedWindow().addBrowserView(this.keep);
this.visible = true; this.visible = true;
}
}
protected async onOpen(): Promise<void> { if (this.settings.minimumWidth) {
this.show(); let parent = this.contentEl.closest<HTMLElement>(".workspace-split.mod-horizontal");
this.resizeIfNecessary(); if (parent) {
this.open = true; let minWidth = `${this.settings.minimumWidth + 2 * this.settings.padding}px`;
if (parent.style.width < minWidth)
parent.style.width = minWidth;
}
}
} }
protected async onClose(): Promise<void> {
this.hide();
this.open = false;
} }
private resizeIfNecessary(): void { private resizeIfNecessary(): void {
@ -143,6 +144,14 @@ class KeepView extends ItemView {
if (this.size && rect.x == this.size.x && rect.y == this.size.y && rect.width == this.size.width && rect.height == this.size.height) if (this.size && rect.x == this.size.x && rect.y == this.size.y && rect.width == this.size.width && rect.height == this.size.height)
return; return;
this.size = rect; this.size = rect;
if (rect.width <= 0 || rect.height <= 0) {
this.hide();
this.open = false;
} else {
this.show();
this.open = true;
this.keep.setBounds({ this.keep.setBounds({
x: Math.floor(rect.x) + this.settings.padding, x: Math.floor(rect.x) + this.settings.padding,
y: Math.floor(rect.top) + this.settings.padding, y: Math.floor(rect.top) + this.settings.padding,
@ -150,6 +159,7 @@ class KeepView extends ItemView {
height: Math.floor(rect.height) - 2 * this.settings.padding height: Math.floor(rect.height) - 2 * this.settings.padding
}); });
} }
}
private coveredByElement(): boolean { private coveredByElement(): boolean {
let nodes = document.body.querySelectorAll(".modal-bg, .menu, .notice"); let nodes = document.body.querySelectorAll(".modal-bg, .menu, .notice");
@ -176,15 +186,28 @@ class KeepSettingTab extends PluginSettingTab {
this.containerEl.createEl('h2', { text: 'Obsidian Keep Settings' }); this.containerEl.createEl('h2', { text: 'Obsidian Keep Settings' });
new Setting(this.containerEl) new Setting(this.containerEl)
.setName('Padding') .setName("Minimum View Width")
.setDesc('The padding that should be left around the inside of the Google Keep view, in pixels.') .setDesc("The minimum width that the Google Keep view should be adjusted to automatically when it is opened. Set to 0 to disable.")
.addText(async t => { .addText(t => {
t.inputEl.type = "number"; t.inputEl.type = "number";
t.setValue(String(this.plugin.settings.padding)); t.setValue(String(this.plugin.settings.minimumWidth));
t.onChange(async v => this.plugin.settings.padding = Number(v)); t.onChange(async v => {
this.plugin.settings.minimumWidth = Number(v);
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}); });
});
new Setting(this.containerEl)
.setName("Padding")
.setDesc("The padding that should be left around the inside of the Google Keep view, in pixels.")
.addText(t => {
t.inputEl.type = "number";
t.setValue(String(this.plugin.settings.padding));
t.onChange(async v => {
this.plugin.settings.padding = Number(v);
await this.plugin.saveSettings();
});
});
new Setting(this.containerEl) new Setting(this.containerEl)
.setName("Additional CSS") .setName("Additional CSS")
.setDesc("A snippet of additional CSS that should be applied to the Google Keep embed. By default, this hides a lot of unnecessary information to make the embed take up less horizontal space.") .setDesc("A snippet of additional CSS that should be applied to the Google Keep embed. By default, this hides a lot of unnecessary information to make the embed take up less horizontal space.")

1276
package-lock.json generated

File diff suppressed because it is too large Load diff