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

75
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,10 +110,10 @@ class KeepView extends ItemView {
} else if (!this.visible && !covered) { } else if (!this.visible && !covered) {
this.show(); this.show();
} }
if (this.visible)
this.resizeIfNecessary();
} }
if (this.visible)
this.resizeIfNecessary();
} }
hide() { hide() {
@ -124,31 +127,38 @@ 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;
if (this.settings.minimumWidth) {
let parent = this.contentEl.closest<HTMLElement>(".workspace-split.mod-horizontal");
if (parent) {
let minWidth = `${this.settings.minimumWidth + 2 * this.settings.padding}px`;
if (parent.style.width < minWidth)
parent.style.width = minWidth;
}
}
} }
} }
protected async onOpen(): Promise<void> {
this.show();
this.resizeIfNecessary();
this.open = true;
}
protected async onClose(): Promise<void> {
this.hide();
this.open = false;
}
private resizeIfNecessary(): void { private resizeIfNecessary(): void {
let rect = this.contentEl.getBoundingClientRect(); 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) 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;
this.keep.setBounds({
x: Math.floor(rect.x) + this.settings.padding, if (rect.width <= 0 || rect.height <= 0) {
y: Math.floor(rect.top) + this.settings.padding, this.hide();
width: Math.floor(rect.width) - 2 * this.settings.padding, this.open = false;
height: Math.floor(rect.height) - 2 * this.settings.padding } else {
}); this.show();
this.open = true;
this.keep.setBounds({
x: Math.floor(rect.x) + this.settings.padding,
y: Math.floor(rect.top) + this.settings.padding,
width: Math.floor(rect.width) - 2 * this.settings.padding,
height: Math.floor(rect.height) - 2 * this.settings.padding
});
}
} }
private coveredByElement(): boolean { private coveredByElement(): boolean {
@ -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 => {
await this.plugin.saveSettings(); this.plugin.settings.minimumWidth = Number(v);
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