2022-03-20 13:55:19 +01:00
import { App , ItemView , Plugin , PluginSettingTab , Setting , WorkspaceLeaf } from 'obsidian' ;
2022-03-22 00:51:28 +01:00
import { remote , webContents } 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 = {
2022-03-22 01:16:51 +01:00
minimumWidth : 370 ,
2022-03-20 13:55:19 +01:00
padding : 5 ,
css : ` /* hide the menu bar and the "Keep" logo and text */
2022-03-22 01:06:16 +01:00
. PvRhvb - qAWA2 , . gb_2d . gb_Zc {
2022-03-20 13:55:19 +01:00
display : none ! important ;
} `
2022-03-22 00:51:28 +01:00
} ;
2022-03-20 13:55:19 +01:00
interface KeepSettings {
2022-03-20 19:32:21 +01:00
minimumWidth : number ;
2022-03-20 13:55:19 +01:00
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-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-22 00:51:28 +01:00
onload ( ) : void {
this . contentEl . empty ( ) ;
this . contentEl . addClass ( "obsidian-keep-view" ) ;
let frame = this . contentEl . createEl ( "iframe" ) ;
frame . setAttribute ( "style" , ` padding: ${ this . settings . padding } px ` ) ;
frame . addClass ( "obsidian-keep-frame" ) ;
frame . onload = ( ) = > {
for ( let other of remote . getCurrentWebContents ( ) . mainFrame . frames ) {
if ( frame . src . contains ( new URL ( other . url ) . host ) ) {
other . executeJavaScript ( `
let style = document . createElement ( "style" ) ;
style . textContent = \ ` ${ this . settings . css } \` ;
document . head . appendChild ( style ) ;
` );
}
2022-03-20 00:20:09 +01:00
}
2022-03-19 20:21:16 +01:00
2022-03-20 19:32:21 +01:00
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 ;
}
}
2022-03-22 00:51:28 +01:00
} ;
frame . src = "https://keep.google.com" ;
2022-03-20 00:20:09 +01:00
}
2022-03-19 20:21:16 +01:00
2022-03-22 00:51:28 +01:00
getViewType ( ) : string {
return viewName ;
}
getDisplayText ( ) : string {
return "Google Keep" ;
2022-03-20 00:20:09 +01:00
}
2022-03-19 20:21:16 +01:00
2022-03-22 00:51:28 +01:00
getIcon ( ) : string {
return "documents" ;
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 )
2022-03-20 19:32:21 +01:00
. setName ( "Minimum View Width" )
2022-03-22 00:51:28 +01:00
. setDesc ( "The width that the Google Keep view should be adjusted to automatically if it is lower. Set to 0 to disable." )
2022-03-20 19:32:21 +01:00
. addText ( t = > {
2022-03-20 13:55:19 +01:00
t . inputEl . type = "number" ;
2022-03-20 19:32:21 +01:00
t . setValue ( String ( this . plugin . settings . minimumWidth ) ) ;
t . onChange ( async v = > {
2022-03-21 19:39:03 +01:00
this . plugin . settings . minimumWidth = v . length ? Number ( v ) : defaultSettings . minimumWidth ;
2022-03-20 19:32:21 +01:00
await this . plugin . saveSettings ( ) ;
} ) ;
2022-03-20 13:55:19 +01:00
} ) ;
2022-03-20 19:32:21 +01:00
new Setting ( this . containerEl )
2022-03-21 00:10:06 +01:00
. setName ( "View Padding" )
2022-03-20 19:32:21 +01:00
. 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 = > {
2022-03-21 19:39:03 +01:00
this . plugin . settings . padding = v . length ? Number ( v ) : defaultSettings . padding ;
2022-03-20 19:32:21 +01:00
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 = > {
2022-03-21 19:39:03 +01:00
this . plugin . settings . css = v . length ? v : defaultSettings.css ;
2022-03-20 13:57:02 +01:00
await this . plugin . saveSettings ( ) ;
} ) ;
2022-03-20 13:55:19 +01:00
} ) ;
}
2022-03-20 00:20:09 +01:00
}