(key: string, value: string)
| 197 | |
| 198 | // Export a utility function that components can use to apply a theme |
| 199 | export const applyEditorTheme = (key: string, value: string) => { |
| 200 | localStorage.setItem("editorTheme", key); |
| 201 | |
| 202 | if (key in DEFAULT_THEMES) { |
| 203 | const themeConfig = DEFAULT_THEMES[key as keyof typeof DEFAULT_THEMES]; |
| 204 | setCSSVariables(themeConfig.variables); |
| 205 | setCSSVariables({ "--status-bar-text": "#fff" }); |
| 206 | |
| 207 | // Set document classes |
| 208 | if (key === "vs-dark") { |
| 209 | document.documentElement.classList.add("dark"); |
| 210 | document.documentElement.classList.remove("light"); |
| 211 | } else { |
| 212 | document.documentElement.classList.remove("dark"); |
| 213 | document.documentElement.classList.add("light"); |
| 214 | } |
| 215 | } else { |
| 216 | try { |
| 217 | // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 218 | const themeData = require(`monaco-themes/themes/${value}.json`); |
| 219 | |
| 220 | // Set document classes |
| 221 | if (themeData.base === "vs-dark") { |
| 222 | document.documentElement.classList.add("dark"); |
| 223 | document.documentElement.classList.remove("light"); |
| 224 | } else { |
| 225 | document.documentElement.classList.remove("dark"); |
| 226 | document.documentElement.classList.add("light"); |
| 227 | } |
| 228 | |
| 229 | setCSSVariables({ |
| 230 | "--toolbar-bg-primary": themeData.colors[ |
| 231 | "editor.selectionBackground" |
| 232 | ].slice(0, 7), |
| 233 | "--toolbar-bg-secondary": themeData.colors[ |
| 234 | "editor.selectionBackground" |
| 235 | ].slice(0, 7), |
| 236 | "--toolbar-foreground": themeData.colors["editor.foreground"].slice( |
| 237 | 0, |
| 238 | 7 |
| 239 | ), |
| 240 | "--toolbar-accent": themeData.colors["editorCursor.foreground"].slice( |
| 241 | 0, |
| 242 | 7 |
| 243 | ), |
| 244 | "--panel-text-accent": themeData.colors["editor.background"].slice( |
| 245 | 0, |
| 246 | 7 |
| 247 | ), |
| 248 | "--panel-background": themeData.colors["editor.background"].slice(0, 7), |
| 249 | "--status-bar-text": themeData.base === "vs-dark" ? "dark" : "light", |
| 250 | }); |
| 251 | } catch (error) { |
| 252 | console.error("Failed to load theme:", error); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (globalMonaco) { |
no test coverage detected