diff options
Diffstat (limited to 'src/webview/darkmode.ts')
-rw-r--r-- | src/webview/darkmode.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/webview/darkmode.ts b/src/webview/darkmode.ts new file mode 100644 index 000000000..e06c22f11 --- /dev/null +++ b/src/webview/darkmode.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | /* eslint no-bitwise: ["error", { "int32Hint": true }] */ | ||
2 | |||
3 | import { join } from 'path'; | ||
4 | import { pathExistsSync, readFileSync } from 'fs-extra'; | ||
5 | |||
6 | const debug = require('debug')('Ferdi:DarkMode'); | ||
7 | |||
8 | const chars = [...'abcdefghijklmnopqrstuvwxyz']; | ||
9 | |||
10 | const ID = [...Array(20)].map(() => chars[Math.random() * chars.length | 0]).join(''); | ||
11 | |||
12 | export function injectDarkModeStyle(recipePath: string) { | ||
13 | const darkModeStyle = join(recipePath, 'darkmode.css'); | ||
14 | if (pathExistsSync(darkModeStyle)) { | ||
15 | const data = readFileSync(darkModeStyle); | ||
16 | const styles = document.createElement('style'); | ||
17 | styles.id = ID; | ||
18 | styles.innerHTML = data.toString(); | ||
19 | |||
20 | document.querySelector('head')?.appendChild(styles); | ||
21 | |||
22 | debug('Injected Dark Mode style with ID', ID); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | export function removeDarkModeStyle() { | ||
27 | const style = document.querySelector(`#${ID}`); | ||
28 | |||
29 | if (style) { | ||
30 | style.remove(); | ||
31 | |||
32 | debug('Removed Dark Mode Style with ID', ID); | ||
33 | } | ||
34 | } | ||
35 | |||
36 | export function isDarkModeStyleInjected() { | ||
37 | return !!document.querySelector(`#${ID}`); | ||
38 | } | ||