Skip to content

Commit 0011e19

Browse files
committedJan 22, 2025
theme; highlight theme
1 parent 38d5613 commit 0011e19

6 files changed

+143
-87
lines changed
 

‎common/themes.ts

+53-34
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
1-
export default themes = [
2-
'cupcake',
3-
'dark',
4-
'light',
5-
'bumblebee',
6-
'emerald',
7-
'corporate',
8-
'synthwave',
9-
'retro',
10-
'cyberpunk',
11-
'valentine',
12-
'halloween',
13-
'garden',
14-
'forest',
15-
'aqua',
16-
'lofi',
17-
'pastel',
18-
'fantasy',
19-
'wireframe',
20-
'black',
21-
'luxury',
22-
'dracula',
23-
'cmyk',
24-
'autumn',
25-
'business',
26-
'acid',
27-
'lemonade',
28-
'night',
29-
'coffee',
30-
'winter',
31-
'dim',
32-
'nord',
33-
'sunset',
34-
]
1+
import { Extension } from '@uiw/react-codemirror'
2+
import { catppuccin } from 'codemirror-theme-catppuccin'
3+
4+
const darkCodeMirrorTheme = catppuccin('macchiato')
5+
const lightCodeMirrorTheme = catppuccin('latte')
6+
7+
export const themeMap = {
8+
light: lightCodeMirrorTheme,
9+
dark: darkCodeMirrorTheme,
10+
acid: lightCodeMirrorTheme,
11+
aqua: darkCodeMirrorTheme,
12+
autumn: lightCodeMirrorTheme,
13+
black: darkCodeMirrorTheme,
14+
bumblebee: lightCodeMirrorTheme,
15+
business: darkCodeMirrorTheme,
16+
cmyk: lightCodeMirrorTheme,
17+
coffee: darkCodeMirrorTheme,
18+
corporate: lightCodeMirrorTheme,
19+
cupcake: lightCodeMirrorTheme,
20+
cyberpunk: lightCodeMirrorTheme,
21+
dim: darkCodeMirrorTheme,
22+
dracula: darkCodeMirrorTheme,
23+
emerald: lightCodeMirrorTheme,
24+
fantasy: lightCodeMirrorTheme,
25+
forest: darkCodeMirrorTheme,
26+
garden: lightCodeMirrorTheme,
27+
halloween: darkCodeMirrorTheme,
28+
lemonade: lightCodeMirrorTheme,
29+
lofi: lightCodeMirrorTheme,
30+
luxury: darkCodeMirrorTheme,
31+
night: darkCodeMirrorTheme,
32+
nord: lightCodeMirrorTheme,
33+
pastel: lightCodeMirrorTheme,
34+
retro: lightCodeMirrorTheme,
35+
sunset: darkCodeMirrorTheme,
36+
synthwave: darkCodeMirrorTheme,
37+
valentine: lightCodeMirrorTheme,
38+
winter: lightCodeMirrorTheme,
39+
wireframe: lightCodeMirrorTheme,
40+
}
41+
42+
export function getCodeMirrorTheme(theme: string | undefined): Extension {
43+
if (!theme || theme === 'system') {
44+
const isDarkMode = window.matchMedia(
45+
'(prefers-color-scheme: dark)',
46+
).matches
47+
return isDarkMode ? darkCodeMirrorTheme : lightCodeMirrorTheme
48+
}
49+
if (theme in themeMap) {
50+
return themeMap[theme as keyof typeof themeMap]
51+
}
52+
return lightCodeMirrorTheme
53+
}

‎components/CollaborativeEditor.css

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.cm-content {
2+
@apply bg-base-100;
3+
/* @apply text-base-content; */
4+
}
5+
.cm-gutters {
6+
@apply bg-base-200 !important;
7+
@apply border-base-200 !important;
8+
}
9+
10+
.cm-lineNumbers {
11+
@apply text-base-content/80;
12+
}
13+
14+
.cm-activeLineGutter {
15+
@apply bg-base-content/5 !important;
16+
@apply text-base-content/80 !important;
17+
}
18+
19+
.cm-activeLine {
20+
@apply bg-base-content/5 !important;
21+
}
22+
23+
/* .cm-focused .cm-cursor {
24+
@apply text-secondary;
25+
}
26+
27+
.CodeMirror-selected {
28+
@apply bg-secondary;
29+
} */

‎components/CollaborativeEditor.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import CodeMirror, {
1010
ReactCodeMirrorRef,
1111
ViewUpdate,
1212
} from '@uiw/react-codemirror'
13-
// import { vscodeDark } from '@uiw/codemirror-theme-vscode'
1413
import { insertNewlineAndIndent } from '@codemirror/commands'
15-
import { Button, Select } from 'react-daisyui'
14+
import { Button } from 'react-daisyui'
1615
import { useSettingsStore } from './SettingPanel' // 引入 useSettingsStore
17-
16+
import './CollaborativeEditor.css'
17+
import { useTheme } from 'next-themes'
18+
import { getCodeMirrorTheme } from '@/common/themes'
1819
const BACKEND_URL =
1920
process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:3001'
2021

@@ -31,6 +32,10 @@ export default function CollaborativeEditor({
3132
const [editingUser, setEditingUser] = useState<number | null>(null)
3233
const [isJoined, setIsJoined] = useState(false)
3334
const [error, setError] = useState<string | null>(null)
35+
const [editorHighlightTheme, setEditorHighlightTheme] = useState<
36+
Extension | undefined
37+
>(undefined)
38+
const { theme } = useTheme()
3439
const { settings } = useSettingsStore() // 使用 useSettingsStore 获取语言设置
3540
const [languageExtension, setLanguageExtension] = useState<Extension>(null)
3641
const socketRef = useRef<Socket>(null)
@@ -85,6 +90,10 @@ export default function CollaborativeEditor({
8590
})
8691
}, [settings.language]) // 依赖 settings.language
8792

93+
useEffect(() => {
94+
setEditorHighlightTheme(getCodeMirrorTheme(theme))
95+
}, [theme])
96+
8897
const handleEditorChange = (value: string) => {
8998
if (roomId && !isReadOnly) {
9099
setEditorContent(value)
@@ -159,7 +168,7 @@ export default function CollaborativeEditor({
159168
ref={editorRef}
160169
value={editorContent}
161170
height='50vh'
162-
// theme={vscodeDark}
171+
theme={editorHighlightTheme}
163172
extensions={extensions}
164173
onChange={handleEditorChange}
165174
editable={!isReadOnly}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"@codemirror/lang-php": "^6.0.1",
1313
"@codemirror/language": "^6.10.8",
1414
"@codemirror/legacy-modes": "^6.4.2",
15-
"@uiw/codemirror-theme-vscode": "^4.23.7",
1615
"@uiw/react-codemirror": "^4.23.7",
1716
"clsx": "^2.1.1",
1817
"codemirror-lang-elixir": "^4.0.0",
18+
"codemirror-theme-catppuccin": "^0.3.0",
1919
"next": "15.1.2",
2020
"next-themes": "^0.4.4",
2121
"react": "^19.0.0",

‎pnpm-lock.yaml

+26-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tailwind.config.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,36 @@ export default {
2222
themes: [
2323
'light',
2424
'dark',
25-
'cupcake',
25+
'acid',
26+
'aqua',
27+
'autumn',
28+
'black',
2629
'bumblebee',
27-
'emerald',
30+
'business',
31+
'cmyk',
32+
'coffee',
2833
'corporate',
29-
'synthwave',
30-
'retro',
34+
'cupcake',
3135
'cyberpunk',
32-
'valentine',
33-
'halloween',
34-
'garden',
36+
'dim',
37+
'dracula',
38+
'emerald',
39+
'fantasy',
3540
'forest',
36-
'aqua',
41+
'garden',
42+
'halloween',
43+
'lemonade',
3744
'lofi',
38-
'pastel',
39-
'fantasy',
40-
'wireframe',
41-
'black',
4245
'luxury',
43-
'dracula',
44-
'cmyk',
45-
'autumn',
46-
'business',
47-
'acid',
48-
'lemonade',
4946
'night',
50-
'coffee',
51-
'winter',
52-
'dim',
5347
'nord',
48+
'pastel',
49+
'retro',
5450
'sunset',
51+
'synthwave',
52+
'valentine',
53+
'winter',
54+
'wireframe',
5555
],
5656
},
5757
} satisfies Config

0 commit comments

Comments
 (0)