2023-07-06 14:47:50 +00:00
import { readYamlPage } from "$sb/lib/yaml_page.ts" ;
2023-05-23 18:53:53 +00:00
import { YAML } from "$sb/plugos-syscall/mod.ts" ;
2022-10-10 12:50:21 +00:00
2023-07-06 14:47:50 +00:00
import { editor , space } from "$sb/silverbullet-syscall/mod.ts" ;
2022-07-15 09:17:02 +00:00
2022-07-18 08:51:28 +00:00
/ * *
* Convenience function to read a specific set of settings from the ` SETTINGS ` page as well as default values
* in case they are not specified .
* Example : ` await readSettings({showPreview: false}) ` will return an object like ` {showPreview: false} ` ( or ` true ` )
* in case this setting is specifically set in the ` SETTINGS ` page .
*
* @param settings object with settings to fetch and their default values
* @returns an object with the same shape as ` settings ` but with non - default values override based on ` SETTINGS `
* /
2022-07-23 17:18:03 +00:00
const SETTINGS_PAGE = "SETTINGS" ;
2022-07-15 09:17:02 +00:00
export async function readSettings < T extends object > ( settings : T ) : Promise < T > {
try {
2022-10-14 13:11:33 +00:00
const allSettings = ( await readYamlPage ( SETTINGS_PAGE , [ "yaml" ] ) ) || { } ;
2022-07-15 09:17:02 +00:00
// TODO: I'm sure there's a better way to type this than "any"
2022-10-14 13:11:33 +00:00
const collectedSettings : any = { } ;
2022-10-25 16:50:07 +00:00
for ( const [ key , defaultVal ] of Object . entries ( settings ) ) {
2022-07-23 17:18:03 +00:00
if ( key in allSettings ) {
2022-07-15 09:17:02 +00:00
collectedSettings [ key ] = allSettings [ key ] ;
} else {
collectedSettings [ key ] = defaultVal ;
}
}
return collectedSettings as T ;
} catch ( e : any ) {
2023-05-23 18:53:53 +00:00
if ( e . message === "Not found" ) {
2022-07-15 09:17:02 +00:00
// No settings yet, return default values for all
return settings ;
}
throw e ;
}
}
2022-07-23 17:18:03 +00:00
2022-12-16 15:35:23 +00:00
export async function readSetting (
key : string ,
defaultValue? : any ,
) : Promise < any > {
try {
const allSettings = ( await readYamlPage ( SETTINGS_PAGE , [ "yaml" ] ) ) || { } ;
const val = allSettings [ key ] ;
return val === undefined ? defaultValue : val ;
} catch ( e : any ) {
2023-05-23 18:53:53 +00:00
if ( e . message === "Not found" ) {
2022-12-16 15:35:23 +00:00
// No settings yet, return default values for all
return defaultValue ;
}
throw e ;
}
}
2022-07-23 17:18:03 +00:00
/ * *
* Convenience function to write a specific set of settings from the ` SETTINGS ` page .
* If the SETTiNGS page doesn ' t exist it will create it .
2022-09-13 06:41:01 +00:00
* @param settings
2022-07-23 17:18:03 +00:00
* /
export async function writeSettings < T extends object > ( settings : T ) {
let readSettings = { } ;
try {
readSettings = ( await readYamlPage ( SETTINGS_PAGE , [ "yaml" ] ) ) || { } ;
2022-10-14 13:11:33 +00:00
} catch {
2023-07-06 14:47:50 +00:00
await editor . flashNotification ( "Creating a new SETTINGS page..." , "info" ) ;
2022-07-23 17:18:03 +00:00
}
2022-10-14 13:11:33 +00:00
const writeSettings : any = { . . . readSettings , . . . settings } ;
2022-09-13 06:41:01 +00:00
// const doc = new YAML.Document();
// doc.contents = writeSettings;
2022-10-10 12:50:21 +00:00
const contents =
2023-05-23 18:53:53 +00:00
` This page contains settings for configuring SilverBullet and its Plugs. \ nAny changes outside of the yaml block will be overwritten. \ n \` \` \` yaml \ n ${ await YAML
. stringify (
2022-10-10 12:50:21 +00:00
writeSettings ,
2023-05-23 18:53:53 +00:00
) } \ n \ ` \` \` ` ; // might need \r\n for windows?
2022-10-14 13:11:33 +00:00
await space . writePage ( SETTINGS_PAGE , contents ) ;
2022-09-13 06:41:01 +00:00
}