-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Improve workflow for creating new theme, generalize theme-related c…
…ode as a node-require module "themes".
- Loading branch information
1 parent
86623b8
commit b57b0ae
Showing
17 changed files
with
311 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const path = require('path'); | ||
|
||
const defaultTheme = 'Day'; | ||
const builtInThemes = [ | ||
'Day', | ||
'SpringStream', | ||
'Forest', | ||
'Horizon', | ||
'LucasDracula', | ||
'Night' | ||
]; | ||
interface ITheme { | ||
name: string; | ||
translated: string; | ||
monacoTheme: object; | ||
css: string; | ||
} | ||
|
||
const registeredThemes: ITheme[] = []; | ||
localStorage.UItheme = localStorage.UItheme || 'Day'; | ||
|
||
const mod = { | ||
registerTheme(name: string): ITheme { | ||
if (mod.getTheme(name)) { | ||
throw new Error(`A theme called ${name} is already registered.`); | ||
} | ||
const monacoTheme = require(path.join('./data/node_requires/monaco-themes', `${name}.json`)); | ||
(window as any).monaco.editor.defineTheme(name, monacoTheme); | ||
const css = `./data/theme${name}.css`; | ||
const theme = { | ||
name, | ||
get translated() { | ||
return (window as any).languageJSON.menu[`theme${name}`] || name; | ||
}, | ||
monacoTheme, | ||
css | ||
}; | ||
registeredThemes.push(theme); | ||
return theme; | ||
}, | ||
getTheme(name: string): ITheme | void { | ||
return registeredThemes.find(t => t.name === name); | ||
}, | ||
loadBuiltInThemes() { | ||
for (const themeName of builtInThemes) { | ||
if (mod.getTheme(themeName)) { | ||
continue; | ||
} | ||
mod.registerTheme(themeName); | ||
} | ||
}, | ||
async switchToTheme(name: string) { | ||
const fs = require('fs-extra'); | ||
try { | ||
const theme = mod.getTheme(name); | ||
if (!theme) { | ||
throw new Error(`A theme called ${name} either does not exist or is not loaded.`); | ||
} | ||
await fs.lstat(theme.css); | ||
const link = (document.getElementById('themeCSS') as HTMLLinkElement); | ||
// Avoid flickering on startup theme reloading | ||
if (link.href !== theme.css) { | ||
link.href = theme.css; | ||
} | ||
(window as any).monaco.editor.setTheme(theme.name); | ||
(window as any).signals.trigger('UIThemeChanged', name); | ||
localStorage.UItheme = name; | ||
} catch (o_O) { | ||
(window as any).alertify.error(`Could not load theme ${name}. Rolling back to the default ${defaultTheme}.`); | ||
await mod.switchToTheme(defaultTheme); | ||
} | ||
}, | ||
async loadTheme() { | ||
mod.switchToTheme(localStorage.UItheme); | ||
}, | ||
getThemeList(): ITheme[] { | ||
return [...registeredThemes]; | ||
} | ||
}; | ||
|
||
module.exports = mod; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.