Skip to content

Commit f550f8d

Browse files
committed
Handles code theme and change default thumbnail img
1 parent 2ffb5fa commit f550f8d

File tree

9 files changed

+95
-36
lines changed

9 files changed

+95
-36
lines changed

src/components/base/hooks/useToggleTheme.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { useDispatch, useSelector } from 'react-redux';
1+
import { useDispatch } from 'react-redux';
2+
import { useTheme } from '../../../lib/hooks/useTheme';
23
import storage from '../../../lib/storage';
3-
import { RootState } from '../../../modules';
44
import darkMode from '../../../modules/darkMode';
55

66
export function useToggleTheme() {
77
const dispatch = useDispatch();
8-
const darkModeState = useSelector((state: RootState) => state.darkMode);
9-
const theme = (() => {
10-
if (darkModeState.systemTheme === 'not-ready') return 'light';
11-
if (darkModeState.theme !== 'default') return darkModeState.theme;
12-
return darkModeState.systemTheme;
13-
})();
8+
const theme = useTheme();
149

1510
const saveToStorage = (value: 'light' | 'dark') => {
1611
storage.setItem('theme', value); // For CSR

src/components/common/MarkdownEditor.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { themedPalette } from '../../lib/styles/themes';
88

99
import 'codemirror/mode/markdown/markdown';
1010
import 'codemirror/addon/display/placeholder';
11+
import { useTheme } from '../../lib/hooks/useTheme';
1112

1213
const MarkdownEditorBlock = styled.div`
1314
.CodeMirror {
@@ -69,12 +70,14 @@ const MarkdownEditor = ({
6970
[onChangeMarkdown],
7071
);
7172

73+
const theme = useTheme();
74+
7275
// initialize editor
7376
useEffect(() => {
7477
if (!textArea.current) return;
7578
const cm = CodeMirror.fromTextArea(textArea.current, {
7679
mode: 'markdown',
77-
theme: 'one-dark',
80+
theme: `one-${theme}`,
7881
placeholder: '당신은 어떤 사람인가요? 당신에 대해서 알려주세요.',
7982
lineWrapping: true,
8083
});

src/components/common/MarkdownRender.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ export interface MarkdownRenderProps {
3131
}
3232

3333
const MarkdownRenderBlock = styled.div`
34-
&.atom-one-dark {
35-
${prismThemes['atom-one-dark']}
36-
}
37-
&.atom-one-light {
38-
${prismThemes['atom-one-light']}
34+
&.atom-one {
35+
${prismThemes['atom-one']}
3936
}
4037
&.github {
4138
${prismThemes['github']}
@@ -203,7 +200,7 @@ type RenderedElement =
203200

204201
const MarkdownRender: React.FC<MarkdownRenderProps> = ({
205202
markdown,
206-
codeTheme = 'atom-one-dark',
203+
codeTheme = 'atom-one',
207204
onConvertFinish,
208205
editing,
209206
}) => {

src/components/write/WriteMarkdownEditor.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface MarkdownEditorProps {
3333
onUpload: () => void;
3434
lastUploadedImage: string | null;
3535
initialBody: string;
36+
theme: 'light' | 'dark';
3637
}
3738

3839
type MarkdownEditorState = {
@@ -124,7 +125,7 @@ export default class WriteMarkdownEditor extends React.Component<
124125
if (!this.editorElement.current) return;
125126
this.codemirror = CodeMirror.fromTextArea(this.editorElement.current, {
126127
mode: 'markdown',
127-
theme: 'one-dark',
128+
theme: `one-${this.props.theme}`,
128129
placeholder: '당신의 이야기를 적어보세요...',
129130
// viewportMargin: Infinity,
130131
lineWrapping: true,

src/containers/write/MarkdownEditorContainer.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import embedPlugin from '../../lib/remark/embedPlugin';
4141
import { Helmet } from 'react-helmet-async';
4242
import { toast } from 'react-toastify';
4343
import { usePrevious } from 'react-use';
44+
import { useTheme } from '../../lib/hooks/useTheme';
4445

4546
export type MarkdownEditorContainerProps = {};
4647

@@ -59,9 +60,8 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
5960
tags,
6061
} = useSelector((state: RootState) => state.write);
6162
const [writePost] = useMutation<WritePostResponse>(WRITE_POST);
62-
const [createPostHistory] = useMutation<CreatePostHistoryResponse>(
63-
CREATE_POST_HISTORY,
64-
);
63+
const [createPostHistory] =
64+
useMutation<CreatePostHistoryResponse>(CREATE_POST_HISTORY);
6565
const [editPost] = useMutation<EditPostResult>(EDIT_POST);
6666

6767
const [lastSavedData, setLastSavedData] = useState({
@@ -256,6 +256,8 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
256256

257257
useSaveHotKey(() => onTempSave(true));
258258

259+
const theme = useTheme();
260+
259261
return (
260262
<>
261263
<Helmet>
@@ -279,6 +281,7 @@ const MarkdownEditorContainer: React.FC<MarkdownEditorContainerProps> = () => {
279281
edit={!!postId && !isTemp}
280282
/>
281283
}
284+
theme={theme}
282285
/>
283286
<DragDropUpload onUpload={onDragDropUpload} />
284287
<PasteUpload onUpload={onDragDropUpload} />

src/lib/hooks/useTheme.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useSelector } from 'react-redux';
2+
import { RootState } from '../../modules';
3+
4+
export function useTheme() {
5+
const darkModeState = useSelector((state: RootState) => state.darkMode);
6+
const theme = (() => {
7+
if (darkModeState.systemTheme === 'not-ready') return 'light';
8+
if (darkModeState.theme !== 'default') return darkModeState.theme;
9+
return darkModeState.systemTheme;
10+
})();
11+
12+
return theme;
13+
}

src/lib/styles/prismThemes.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { css } from 'styled-components';
22

3+
import { themedPalette } from './themes';
4+
35
const prismThemes = {
4-
'atom-one-dark': css`
6+
'atom-one': css`
57
pre {
6-
background: #313440;
8+
background: ${themedPalette.prism_bg};
79
}
810
/**
911
* prism.js default theme for JavaScript, CSS and HTML
@@ -12,7 +14,7 @@ const prismThemes = {
1214
*/
1315
code[class*='language-'],
1416
pre[class*='language-'] {
15-
color: #e0e6f1;
17+
color: ${themedPalette.prism_default_text};
1618
background: none;
1719
text-align: left;
1820
white-space: pre;
@@ -34,15 +36,15 @@ const prismThemes = {
3436
code[class*='language-']::-moz-selection,
3537
code[class*='language-'] ::-moz-selection {
3638
text-shadow: none;
37-
background: #383e49;
39+
background: ${themedPalette.prism_selection_bg};
3840
}
3941
4042
pre[class*='language-']::selection,
4143
pre[class*='language-'] ::selection,
4244
code[class*='language-']::selection,
4345
code[class*='language-'] ::selection {
4446
text-shadow: none;
45-
background: #9aa2b1;
47+
background: ${themedPalette.prism_selection_bg};
4648
}
4749
4850
@media print {
@@ -60,7 +62,7 @@ const prismThemes = {
6062
6163
:not(pre) > code[class*='language-'],
6264
pre[class*='language-'] {
63-
background: #282c34;
65+
background: ${themedPalette.prism_code_block_bg};
6466
}
6567
6668
/* Inline code */
@@ -74,16 +76,16 @@ const prismThemes = {
7476
.token.prolog,
7577
.token.doctype,
7678
.token.cdata {
77-
color: #5c6370;
79+
color: ${themedPalette.prism_code_1};
7880
}
7981
8082
.token.punctuation {
81-
color: #abb2bf;
83+
color: ${themedPalette.prism_code_2};
8284
}
8385
8486
.token.selector,
8587
.token.tag {
86-
color: #e06c75;
88+
color: ${themedPalette.prism_code_3};
8789
}
8890
8991
.token.property,
@@ -93,38 +95,38 @@ const prismThemes = {
9395
.token.symbol,
9496
.token.attr-name,
9597
.token.deleted {
96-
color: #d19a66;
98+
color: ${themedPalette.prism_code_4};
9799
}
98100
99101
.token.string,
100102
.token.char,
101103
.token.attr-value,
102104
.token.builtin,
103105
.token.inserted {
104-
color: #98c379;
106+
color: ${themedPalette.prism_code_6};
105107
}
106108
107109
.token.operator,
108110
.token.entity,
109111
.token.url,
110112
.language-css .token.string,
111113
.style .token.string {
112-
color: #56b6c2;
114+
color: ${themedPalette.prism_code_5};
113115
}
114116
115117
.token.atrule,
116118
.token.keyword {
117-
color: #c678dd;
119+
color: ${themedPalette.prism_code_7};
118120
}
119121
120122
.token.function {
121-
color: #61afef;
123+
color: ${themedPalette.prism_code_8};
122124
}
123125
124126
.token.regex,
125127
.token.important,
126128
.token.variable {
127-
color: #c678dd;
129+
color: ${themedPalette.prism_code_9};
128130
}
129131
130132
.token.important,
@@ -174,7 +176,7 @@ const prismThemes = {
174176
175177
.line-numbers-rows > span:before {
176178
content: counter(linenumber);
177-
color: #5c6370;
179+
color: ${themedPalette.prism_line_number};
178180
display: block;
179181
padding-right: 0.8em;
180182
text-align: right;

src/lib/styles/themes.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ type ThemeVariables = {
2828
slight_layer: string;
2929
opaque_layer: string;
3030
editor_footer: string;
31+
32+
prism_bg: string;
33+
prism_default_text: string;
34+
prism_selection_bg: string;
35+
prism_code_block_bg: string;
36+
prism_code_1: string;
37+
prism_code_2: string;
38+
prism_code_3: string;
39+
prism_code_4: string;
40+
prism_code_5: string;
41+
prism_code_6: string;
42+
prism_code_7: string;
43+
prism_code_8: string;
44+
prism_code_9: string;
45+
prism_line_number: string;
3146
};
3247

3348
type Theme = 'light' | 'dark';
@@ -65,6 +80,21 @@ const themeVariableSets: Record<Theme, ThemeVariables> = {
6580
slight_layer: 'rgba(0,0,0,0.05)',
6681
opaque_layer: 'rgba(249,249,249,0.85)',
6782
editor_footer: '#FFFFFF',
83+
84+
prism_bg: '#fbfcfd',
85+
prism_default_text: '#24292e',
86+
prism_selection_bg: 'rgba(0,0,0,0.15)',
87+
prism_code_block_bg: '#fbfcfd',
88+
prism_code_1: '#969896',
89+
prism_code_2: '#24292e',
90+
prism_code_3: '#a626a4',
91+
prism_code_4: '#63a35c',
92+
prism_code_5: '#0184bc',
93+
prism_code_6: '#50a14f',
94+
prism_code_7: '#a626a4',
95+
prism_code_8: '#005cc5',
96+
prism_code_9: '#a626a4',
97+
prism_line_number: '#585c63',
6898
},
6999
dark: {
70100
bg_page1: '#121212',
@@ -96,10 +126,25 @@ const themeVariableSets: Record<Theme, ThemeVariables> = {
96126
slight_layer: 'rgba(255,255,255,0.1)',
97127
opaque_layer: 'rgba(0, 0, 0, 0.85)',
98128
editor_footer: '#2E2E2E',
129+
130+
prism_bg: '#313440',
131+
prism_default_text: '#e0e6f1',
132+
prism_selection_bg: '#383e49',
133+
prism_code_block_bg: '#282c34',
134+
prism_code_1: '#5c6370',
135+
prism_code_2: '#abb2bf',
136+
prism_code_3: '#e06c75',
137+
prism_code_4: '#d19a66',
138+
prism_code_5: '#98c379',
139+
prism_code_6: '#56b6c2',
140+
prism_code_7: '#c678dd',
141+
prism_code_8: '#61afef',
142+
prism_code_9: '#c678dd',
143+
prism_line_number: '#5c6370',
99144
},
100145
};
101146

102-
const buildCssVariables = (variables: ThemeVariables) => {
147+
const buildCssVariables = <T>(variables: ThemeVariables) => {
103148
const keys = Object.keys(variables) as (keyof ThemeVariables)[];
104149
return keys.reduce(
105150
(acc, key) =>

src/static/images/user-thumbnail.png

-644 Bytes
Loading

0 commit comments

Comments
 (0)