@@ -103,21 +103,48 @@ export function setBodyBackgroundToThemeBackgroundColor(document: Document, loca
103
103
}
104
104
105
105
if ( ! localStorage ) {
106
- throw new Error ( `${ errorMsgPrefix } Could not set body background to theme background color. localStorage is undefined.` )
106
+ throw new Error (
107
+ `${ errorMsgPrefix } Could not set body background to theme background color. localStorage is undefined.` ,
108
+ )
107
109
}
108
110
109
111
const colorThemeData = localStorage . getItem ( "colorThemeData" )
110
112
111
113
if ( ! colorThemeData ) {
112
- throw new Error ( `${ errorMsgPrefix } Could not set body background to theme background color. Could not find colorThemeData in localStorage.` )
114
+ throw new Error (
115
+ `${ errorMsgPrefix } Could not set body background to theme background color. Could not find colorThemeData in localStorage.` ,
116
+ )
117
+ }
118
+
119
+ let _colorThemeData
120
+ try {
121
+ // We wrap this JSON.parse logic in a try/catch
122
+ // because it can throw if the JSON is invalid.
123
+ // and instead of throwing a random error
124
+ // we can throw our own error, which will be more helpful
125
+ // to the end user.
126
+ _colorThemeData = JSON . parse ( colorThemeData )
127
+ } catch {
128
+ throw new Error (
129
+ `${ errorMsgPrefix } Could not set body background to theme background color. Could not parse colorThemeData from localStorage.` ,
130
+ )
131
+ }
132
+
133
+ if ( ! _colorThemeData . hasOwnProperty ( "colorMap" ) ) {
134
+ throw new Error (
135
+ `${ errorMsgPrefix } Could not set body background to theme background color. colorThemeData is missing colorMap.` ,
136
+ )
137
+ }
138
+
139
+ const editorBgColor = _colorThemeData . colorMap [ "editor.background" ]
140
+
141
+ if ( ! editorBgColor ) {
142
+ throw new Error (
143
+ `${ errorMsgPrefix } Could not set body background to theme background color. colorThemeData.colorMap["editor.background"] is undefined.` ,
144
+ )
113
145
}
114
146
115
- // TODO parse colorThemeData
116
- // which may need try/catch
117
- // or not since we are catching when we call the function
118
- // TODO get editor.background
119
- // TODO set it to style.background
120
- // also read about when to set background vs backgroundColor
147
+ document . body . style . background = editorBgColor
121
148
122
149
return null
123
150
}
0 commit comments