2022-03-20 13:55:19 +01:00
import { App , ItemView , Plugin , PluginSettingTab , Setting , WorkspaceLeaf } 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" ;
2022-03-20 13:55:19 +01:00
const defaultSettings : KeepSettings = {
padding : 5 ,
css : ` /* hide the menu bar and the "Keep" logo and text */
. PvRhvb - qAWA2 , . gb_qc {
display : none ! important ;
}
/* remove the margin around each note so that less horizontal space is taken up */
. kPTQic - nUpftc . ma6Yeb - r8s4j - gkA7Yd . IZ65Hb - n0tgWb {
margin : 0px ! important ;
}
. kPTQic - nUpftc . h1U9Be - xhiy4 {
margin : 16px 0 px 8 px 0 px ! important ;
} `
}
interface KeepSettings {
padding : number ;
css : string ;
}
2022-03-19 20:21:16 +01:00
2022-03-20 13:55:19 +01:00
export default class KeepPlugin extends Plugin {
settings : KeepSettings ;
2022-03-19 20:21:16 +01:00
2022-03-20 00:20:09 +01:00
async onload ( ) : Promise < void > {
2022-03-20 13:55:19 +01:00
await this . loadSettings ( ) ;
2022-03-19 20:21:16 +01:00
2022-03-20 13:55:19 +01:00
this . registerView ( viewName , l = > new KeepView ( l , this . settings ) ) ;
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 13:55:19 +01:00
this . addSettingTab ( new KeepSettingTab ( this . app , this ) ) ;
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-20 13:55:19 +01:00
async loadSettings() {
this . settings = Object . assign ( { } , defaultSettings , await this . loadData ( ) ) ;
}
async saveSettings() {
await this . saveData ( this . settings ) ;
}
2022-03-20 00:20:09 +01:00
}
2022-03-19 20:21:16 +01:00
2022-03-20 13:55:19 +01:00
class KeepView extends ItemView {
2022-03-19 20:21:16 +01:00
2022-03-20 13:55:19 +01:00
private settings : KeepSettings ;
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 13:55:19 +01:00
constructor ( leaf : WorkspaceLeaf , settings : KeepSettings ) {
super ( leaf ) ;
this . settings = settings ;
}
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' ) ;
2022-03-20 13:55:19 +01:00
await this . keep . webContents . insertCSS ( this . settings . css ) ;
2022-03-20 00:44:01 +01:00
this . registerInterval ( window . setInterval ( ( ) = > this . update ( ) , 33.33 ) ) ;
2022-03-20 00:20:09 +01:00
}
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 ( {
2022-03-20 13:55:19 +01:00
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
2022-03-20 00:20:09 +01:00
} ) ;
}
2022-03-19 20:21:16 +01:00
2022-03-20 00:20:09 +01:00
private coveredByElement ( ) : boolean {
2022-03-20 11:57:54 +01:00
let nodes = document . body . querySelectorAll ( ".modal-bg, .menu, .notice" ) ;
2022-03-20 00:20:09 +01:00
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 13:55:19 +01:00
}
class KeepSettingTab extends PluginSettingTab {
2022-03-20 13:57:02 +01:00
plugin : KeepPlugin ;
2022-03-20 13:55:19 +01:00
constructor ( app : App , plugin : KeepPlugin ) {
super ( app , plugin ) ;
2022-03-20 13:57:02 +01:00
this . plugin = plugin ;
2022-03-20 13:55:19 +01:00
}
display ( ) : void {
this . containerEl . empty ( ) ;
this . containerEl . createEl ( 'h2' , { text : 'Obsidian Keep Settings' } ) ;
new Setting ( this . containerEl )
. setName ( 'Padding' )
. setDesc ( 'The padding that should be left around the inside of the Google Keep view, in pixels.' )
2022-03-20 13:57:02 +01:00
. addText ( async t = > {
2022-03-20 13:55:19 +01:00
t . inputEl . type = "number" ;
2022-03-20 13:57:02 +01:00
t . setValue ( String ( this . plugin . settings . padding ) ) ;
t . onChange ( async v = > this . plugin . settings . padding = Number ( v ) ) ;
await this . plugin . saveSettings ( ) ;
2022-03-20 13:55:19 +01:00
} ) ;
new Setting ( this . containerEl )
. 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." )
. addTextArea ( t = > {
t . inputEl . rows = 10 ;
t . inputEl . cols = 50 ;
2022-03-20 13:57:02 +01:00
t . setValue ( this . plugin . settings . css ) ;
t . onChange ( async v = > {
this . plugin . settings . css = v ;
await this . plugin . saveSettings ( ) ;
} ) ;
2022-03-20 13:55:19 +01:00
} ) ;
}
2022-03-20 00:20:09 +01:00
}