Archived
1
0

feat: add setBodyBackgroundToThemeBackgroundColor

This refactors some logic in src/browser/pages/vscode.ts
related to setting the background color of the body
to the editor background theme color.
This commit is contained in:
Joe Previte
2021-07-07 16:28:05 -07:00
parent 197bccc7cd
commit 66dc4cc0dc
2 changed files with 178 additions and 5 deletions

View File

@ -95,8 +95,64 @@ try {
console.error(error)
}
try {
document.body.style.background = JSON.parse(localStorage.getItem("colorThemeData")!).colorMap["editor.background"]
} catch (error) {
// Oh well.
export function setBodyBackgroundToThemeBackgroundColor(document: Document, localStorage: Storage) {
const errorMsgPrefix = "[vscode]"
if (!document) {
throw new Error(`${errorMsgPrefix} Could not set body background to theme background color. Document is undefined.`)
}
if (!localStorage) {
throw new Error(
`${errorMsgPrefix} Could not set body background to theme background color. localStorage is undefined.`,
)
}
const colorThemeData = localStorage.getItem("colorThemeData")
if (!colorThemeData) {
throw new Error(
`${errorMsgPrefix} Could not set body background to theme background color. Could not find colorThemeData in localStorage.`,
)
}
let _colorThemeData
try {
// We wrap this JSON.parse logic in a try/catch
// because it can throw if the JSON is invalid.
// and instead of throwing a random error
// we can throw our own error, which will be more helpful
// to the end user.
_colorThemeData = JSON.parse(colorThemeData)
} catch {
throw new Error(
`${errorMsgPrefix} Could not set body background to theme background color. Could not parse colorThemeData from localStorage.`,
)
}
const hasColorMapProperty = Object.prototype.hasOwnProperty.call(_colorThemeData, "colorMap")
if (!hasColorMapProperty) {
throw new Error(
`${errorMsgPrefix} Could not set body background to theme background color. colorThemeData is missing colorMap.`,
)
}
const editorBgColor = _colorThemeData.colorMap["editor.background"]
if (!editorBgColor) {
throw new Error(
`${errorMsgPrefix} Could not set body background to theme background color. colorThemeData.colorMap["editor.background"] is undefined.`,
)
}
document.body.style.background = editorBgColor
return null
}
try {
setBodyBackgroundToThemeBackgroundColor(document, localStorage)
} catch (error) {
console.error("Something went wrong setting the body background to the theme background color.")
console.error(error)
}