From ccec5109290a7c98faa9cfe912c4f333b79f6525 Mon Sep 17 00:00:00 2001 From: Tyler Liu Date: Tue, 15 Oct 2024 20:16:06 -0700 Subject: [PATCH] Editor dark theme and light theme --- package.json | 1 + src/components/editor.tsx | 9 ++++ src/components/modals/preferences.tsx | 62 +++------------------------ src/store.ts | 7 ++- yarn.lock | 16 +++++++ 5 files changed, 38 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index f7a48bb..28cdfc0 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@types/node": "^22.7.5", "@types/react": "^18.3.11", "@types/react-dom": "^18.3.1", + "@uiw/codemirror-theme-github": "^4.23.5", "antd": "^5.21.3", "chart.js": "2.6.0", "debounce": "^2.1.1", diff --git a/src/components/editor.tsx b/src/components/editor.tsx index 72a2e28..0a85190 100644 --- a/src/components/editor.tsx +++ b/src/components/editor.tsx @@ -4,6 +4,7 @@ import { defaultHighlightStyle, syntaxHighlighting, } from '@codemirror/language'; +import { Compartment } from '@codemirror/state'; import { EditorView, highlightActiveLine, @@ -12,6 +13,7 @@ import { scrollPastEnd, ViewUpdate, } from '@codemirror/view'; +import { githubDark, githubLight } from '@uiw/codemirror-theme-github'; import debounce from 'debounce'; import { exclude } from 'manate'; import { auto } from 'manate/react'; @@ -33,8 +35,10 @@ const Editor = auto((props: { store: Store }) => { } }, ); + const theme = new Compartment(); const cm = new EditorView({ extensions: [ + theme.of(githubLight), EditorView.lineWrapping, highlightActiveLine(), lineNumbers(), @@ -58,6 +62,11 @@ const Editor = auto((props: { store: Store }) => { }; store.editor = exclude(cm); + store.editor.toggleDarkTheme = (isDark: boolean) => { + store.editor.dispatch({ + effects: theme.reconfigure(isDark ? githubDark : githubLight), + }); + }; store.editor.scrollDOM.addEventListener('scroll', () => { syncPreview(); diff --git a/src/components/modals/preferences.tsx b/src/components/modals/preferences.tsx index 22c7831..15cd899 100644 --- a/src/components/modals/preferences.tsx +++ b/src/components/modals/preferences.tsx @@ -7,58 +7,6 @@ import React, { useEffect } from 'react'; import iconUrl from '../../icon.svg'; import { Store } from '../../store'; -const themes = [ - '3024-day', - '3024-night', - 'abcdef', - 'ambiance-mobile', - 'ambiance', - 'base16-dark', - 'base16-light', - 'bespin', - 'blackboard', - 'cobalt', - 'colorforth', - 'default', - 'dracula', - 'duotone-dark', - 'duotone-light', - 'eclipse', - 'elegant', - 'erlang-dark', - 'hopscotch', - 'icecoder', - 'isotope', - 'lesser-dark', - 'liquibyte', - 'material', - 'mbo', - 'mdn-like', - 'midnight', - 'monokai', - 'neat', - 'neo', - 'night', - 'panda-syntax', - 'paraiso-dark', - 'paraiso-light', - 'pastel-on-dark', - 'railscasts', - 'rubyblue', - 'seti', - 'solarized', - 'the-matrix', - 'tomorrow-night-bright', - 'tomorrow-night-eighties', - 'ttcn', - 'twilight', - 'vibrant-ink', - 'xq-dark', - 'xq-light', - 'yeti', - 'zenburn', -]; - const PreferencesModal = auto((props: { store: Store }) => { console.log('render preferences modal'); const { store } = props; @@ -69,6 +17,7 @@ const PreferencesModal = auto((props: { store: Store }) => { if (!store.editor) { return; } + store.editor.toggleDarkTheme?.(preferences.darkTheme); mdc.mermaid.gantt.axisFormat(preferences.ganttAxisFormat); }); start(); @@ -125,9 +74,12 @@ const PreferencesModal = auto((props: { store: Store }) => {