|
1 | | -import { useMemo, Fragment } from 'react'; |
| 1 | +import { useMemo, useEffect, useState, Fragment } from 'react'; |
2 | 2 | import { useSelector } from 'react-redux'; |
3 | 3 | import Loader from '@uiw/react-loader'; |
4 | 4 | import MarkdownPreview from '@uiw/react-markdown-preview'; |
5 | 5 | import rehypeVideo from 'rehype-video'; |
6 | | -import CodeMirror from '@uiw/react-codemirror'; |
| 6 | +import CodeMirror, { ReactCodeMirrorProps } from '@uiw/react-codemirror'; |
7 | 7 | import { Extension } from '@codemirror/state'; |
8 | 8 | import { StreamLanguage } from '@codemirror/language'; |
9 | 9 | import { stylus } from '@codemirror/legacy-modes/mode/stylus'; |
@@ -41,6 +41,14 @@ const langs: Record<string, any> = { |
41 | 41 | }; |
42 | 42 |
|
43 | 43 | export default function DirectoryTrees() { |
| 44 | + const dark = document.documentElement.getAttribute('data-color-mode'); |
| 45 | + const [theme, setTheme] = useState<ReactCodeMirrorProps['theme']>(dark === 'dark' ? 'dark' : 'light'); |
| 46 | + useEffect(() => { |
| 47 | + setTheme(document.documentElement.getAttribute('data-color-mode') === 'dark' ? 'dark' : 'light'); |
| 48 | + document.addEventListener('colorschemechange', (e) => { |
| 49 | + setTheme(e.detail.colorScheme as ReactCodeMirrorProps['theme']); |
| 50 | + }); |
| 51 | + }, []); |
44 | 52 | const { loading, content, extname } = useSelector(({ global, loading }: RootState) => ({ |
45 | 53 | loading: loading.effects.global.getFileContent, |
46 | 54 | content: global.content, |
@@ -85,10 +93,11 @@ export default function DirectoryTrees() { |
85 | 93 | if (langs[extname]) { |
86 | 94 | extensions.push(langs[extname]()); |
87 | 95 | } |
88 | | - return <CodeMirror extensions={[...extensions]} editable={false} height="100%" value={content} />; |
| 96 | + return <CodeMirror theme={theme} extensions={[...extensions]} editable={false} height="100%" value={content} />; |
89 | 97 | } |
90 | 98 | return <pre style={{ padding: 25 }}>{content}</pre>; |
91 | | - }, [extname, content, filePath]); |
| 99 | + }, [extname, content, filePath, theme]); |
| 100 | + |
92 | 101 |
|
93 | 102 | return ( |
94 | 103 | <Fragment> |
|
0 commit comments