Skip to content

Commit 3389a73

Browse files
committed
feat: customizable code block font size (#56)
1 parent 6632523 commit 3389a73

File tree

6 files changed

+79
-67
lines changed

6 files changed

+79
-67
lines changed

src/components/MarkdownRender/Pre.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useEffect, useRef, useState } from 'react'
2+
import CopyButton from '../CopyButton'
3+
import PropTypes from 'prop-types'
4+
import { changeChildrenFontSize } from '../../utils'
5+
6+
export function Pre({ className, children }) {
7+
const preRef = useRef(null)
8+
const [fontSize, setFontSize] = useState(14)
9+
const sizeList = [10, 12, 14, 16, 18]
10+
11+
useEffect(() => {
12+
changeChildrenFontSize(preRef.current.childNodes[1], fontSize + 'px')
13+
})
14+
15+
return (
16+
<pre className={className} ref={preRef} style={{ position: 'relative' }}>
17+
<span className="code-corner-util gpt-util-group">
18+
<select
19+
className="normal-button"
20+
required
21+
onChange={(e) => {
22+
setFontSize(e.target.value)
23+
}}
24+
>
25+
{Object.values(sizeList).map((size) => {
26+
return (
27+
<option value={size} key={size} selected={size === fontSize}>
28+
{size}px
29+
</option>
30+
)
31+
})}
32+
</select>
33+
<CopyButton contentFn={() => preRef.current.childNodes[1].textContent} size={14} />
34+
</span>
35+
{children}
36+
</pre>
37+
)
38+
}
39+
40+
Pre.propTypes = {
41+
className: PropTypes.string.isRequired,
42+
children: PropTypes.object.isRequired,
43+
}

src/components/MarkdownRender/markdown-without-katex.jsx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,7 @@ import rehypeRaw from 'rehype-raw'
33
import rehypeHighlight from 'rehype-highlight'
44
import remarkGfm from 'remark-gfm'
55
import remarkBreaks from 'remark-breaks'
6-
import CopyButton from '../CopyButton'
7-
import { useRef } from 'react'
8-
import PropTypes from 'prop-types'
9-
10-
function Pre({ className, children }) {
11-
const preRef = useRef(null)
12-
return (
13-
<pre className={className} ref={preRef} style="position: relative;">
14-
<CopyButton
15-
className="code-copy-btn"
16-
contentFn={() => preRef.current.textContent}
17-
size={14}
18-
/>
19-
{children}
20-
</pre>
21-
)
22-
}
23-
24-
Pre.propTypes = {
25-
className: PropTypes.string.isRequired,
26-
children: PropTypes.object.isRequired,
27-
}
6+
import { Pre } from './Pre'
287

298
export function MarkdownRender(props) {
309
const linkProperties = {

src/components/MarkdownRender/markdown.jsx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,7 @@ import rehypeKatex from 'rehype-katex'
66
import remarkMath from 'remark-math'
77
import remarkGfm from 'remark-gfm'
88
import remarkBreaks from 'remark-breaks'
9-
import CopyButton from '../CopyButton'
10-
import { useRef } from 'react'
11-
import PropTypes from 'prop-types'
12-
13-
function Pre({ className, children }) {
14-
const preRef = useRef(null)
15-
return (
16-
<pre className={className} ref={preRef} style="position: relative;">
17-
<CopyButton
18-
className="code-copy-btn"
19-
contentFn={() => preRef.current.textContent}
20-
size={14}
21-
/>
22-
{children}
23-
</pre>
24-
)
25-
}
26-
27-
Pre.propTypes = {
28-
className: PropTypes.string.isRequired,
29-
children: PropTypes.object.isRequired,
30-
}
9+
import { Pre } from './Pre'
3110

3211
export function MarkdownRender(props) {
3312
const linkProperties = {

src/content-script/styles.scss

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,32 @@
112112
animation: chatgptbox-pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
113113
}
114114

115-
.code-copy-btn {
115+
.code-corner-util {
116116
color: inherit;
117117
position: absolute;
118118
right: 10px;
119119
top: 3px;
120+
}
121+
122+
.gpt-util-group {
123+
display: flex;
124+
gap: 15px;
125+
align-items: center;
126+
}
127+
128+
.gpt-util-icon {
129+
display: flex;
130+
cursor: pointer;
131+
align-items: center;
132+
}
133+
134+
.normal-button {
135+
padding: 1px 6px;
136+
border: 1px solid;
137+
border-color: var(--theme-border-color);
138+
background-color: var(--theme-color);
139+
color: var(--font-color);
140+
border-radius: 5px;
120141
cursor: pointer;
121142
}
122143

@@ -127,6 +148,7 @@
127148
word-break: break-word;
128149
pre {
129150
margin-top: 10px;
151+
padding: 0;
130152
}
131153

132154
& > p {
@@ -168,28 +190,6 @@
168190
.gpt-feedback-selected {
169191
color: #f08080;
170192
}
171-
172-
.gpt-util-group {
173-
display: flex;
174-
gap: 15px;
175-
align-items: center;
176-
}
177-
178-
.gpt-util-icon {
179-
display: flex;
180-
cursor: pointer;
181-
align-items: center;
182-
}
183-
184-
.normal-button {
185-
padding: 1px 6px;
186-
border: 1px solid;
187-
border-color: var(--theme-border-color);
188-
background-color: var(--theme-color);
189-
color: var(--font-color);
190-
border-radius: 5px;
191-
cursor: pointer;
192-
}
193193
}
194194

195195
.error {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function changeChildrenFontSize(element, size) {
2+
try {
3+
element.style.fontSize = size
4+
} catch {
5+
/* empty */
6+
}
7+
for (let i = 0; i < element.childNodes.length; i++) {
8+
changeChildrenFontSize(element.childNodes[i], size)
9+
}
10+
}

src/utils/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './change-children-font-size'
12
export * from './create-element-at-position'
23
export * from './crop-text'
34
export * from './ends-with-question-mark'

0 commit comments

Comments
 (0)