Skip to content

Commit ff4137c

Browse files
committed
fix: code editor
Signed-off-by: Innei <[email protected]>
1 parent 06db5eb commit ff4137c

File tree

5 files changed

+89
-34
lines changed

5 files changed

+89
-34
lines changed

src/components/modules/dashboard/writing/MetaKeyValueEditSection.tsx

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { Label } from '@radix-ui/react-label'
2-
import { useMemo, useRef, useState } from 'react'
2+
import { useMemo, useRef } from 'react'
33
import type { FC } from 'react'
44

55
import { StyledButton } from '~/components/ui/button'
6-
import {
7-
BaseCodeHighlighter,
8-
HighLighter,
9-
} from '~/components/ui/code-highlighter'
6+
import { CodeEditor } from '~/components/ui/code-editor'
7+
import { HighLighter } from '~/components/ui/code-highlighter'
108
import { useModalStack } from '~/components/ui/modal'
119
import { useEventCallback } from '~/hooks/common/use-event-callback'
1210
import { toast } from '~/lib/toast'
@@ -80,7 +78,6 @@ const EditorModal: FC<{
8078
onChange: (value: object) => void
8179
}> = ({ value, onChange, dismiss }) => {
8280
const currentEditValueRef = useRef(value)
83-
const [highlighterValue, setHighlighterValue] = useState(value)
8481

8582
const handleSave = () => {
8683
if (!isValidJSONString(currentEditValueRef.current)) {
@@ -93,27 +90,19 @@ const EditorModal: FC<{
9390
}
9491

9592
return (
96-
<>
97-
<div className="relative flex w-full flex-grow flex-col lg:w-[600px]">
98-
<div className="relative w-full overflow-auto pb-[60px]">
99-
<textarea
100-
className="absolute h-full w-full resize-none bg-transparent p-0 !font-mono text-transparent caret-accent *:leading-4"
101-
defaultValue={value}
102-
onChange={(e) => {
103-
currentEditValueRef.current = e.target.value
104-
setHighlighterValue(e.target.value)
105-
}}
106-
/>
107-
<BaseCodeHighlighter
108-
className="code-wrap pointer-events-none relative z-[1] !m-0 overflow-auto !p-0 *:!font-mono *:!leading-4"
109-
lang="json"
110-
content={highlighterValue}
111-
/>
112-
</div>
93+
<div className="relative flex w-full flex-grow flex-col lg:w-[600px]">
94+
<div className="relative max-h-[450px] w-full overflow-auto">
95+
<CodeEditor
96+
content={value}
97+
language="json"
98+
onChange={(value) => {
99+
currentEditValueRef.current = value
100+
}}
101+
/>
113102
</div>
114-
<div className="absolute bottom-4 right-6 flex flex-shrink-0 justify-end">
103+
<div className="flex flex-shrink-0 justify-end p-2">
115104
<StyledButton onClick={handleSave}>保存</StyledButton>
116105
</div>
117-
</>
106+
</div>
118107
)
119108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useEffect, useState } from 'react'
2+
import type { FC } from 'react'
3+
4+
import { BaseCodeHighlighter } from '../code-highlighter'
5+
6+
export const CodeEditor: FC<{
7+
content: string
8+
language: string
9+
10+
onChange?: (value: string) => void
11+
}> = ({ content, language, onChange }) => {
12+
const [highlighterValue, setHighlighterValue] = useState(content)
13+
14+
useEffect(() => {
15+
setHighlighterValue(content)
16+
}, [content])
17+
18+
return (
19+
<div className="relative">
20+
<textarea
21+
className="absolute h-full w-full resize-none overflow-hidden bg-transparent p-0 !font-mono text-transparent caret-accent *:leading-4"
22+
value={highlighterValue}
23+
onChange={(e) => {
24+
setHighlighterValue(e.target.value)
25+
onChange?.(e.target.value)
26+
}}
27+
/>
28+
<BaseCodeHighlighter
29+
className="code-wrap pointer-events-none relative z-[1] !m-0 !p-0 *:!font-mono *:!leading-4"
30+
lang={language}
31+
content={highlighterValue}
32+
/>
33+
</div>
34+
)
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { DocumentComponent } from 'storybook/typings'
2+
3+
import { CodeEditor } from './CodeEditor'
4+
5+
export const CodeEditorDemo: DocumentComponent = () => {
6+
return (
7+
<div className="h-[300px] overflow-auto border p-4">
8+
<CodeEditor
9+
content={Array(100)
10+
.fill(null)
11+
.map(() => `const a = ${Math.random()};\n`)
12+
.join('')}
13+
language="javascript"
14+
/>
15+
</div>
16+
)
17+
}
18+
19+
CodeEditorDemo.meta = {
20+
title: 'CodeEditor',
21+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './CodeEditor'

src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx

+18-9
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import { $view } from '@milkdown/utils'
1313

1414
import { BlockLoading } from '~/components/modules/shared/BlockLoading'
1515
import { StyledButton } from '~/components/ui/button'
16+
import { CodeEditor } from '~/components/ui/code-editor'
1617
import { HighLighter } from '~/components/ui/code-highlighter'
17-
import { Input, TextArea } from '~/components/ui/input'
18+
import { Input } from '~/components/ui/input'
1819
import { useCurrentModal, useModalStack } from '~/components/ui/modal'
19-
import { useUncontrolledInput } from '~/hooks/common/use-uncontrolled-input'
2020

2121
import { useEditorCtx } from '../ctx'
2222

@@ -49,14 +49,20 @@ const NormalCodeBlock: FC<{
4949

5050
const handleEdit = () => {
5151
const Content: FC<ModalContentPropsInternal> = () => {
52-
const [, getValue, ref] =
53-
useUncontrolledInput<HTMLTextAreaElement>(content)
54-
52+
const valueRef = useRef<string | undefined>(content)
5553
const nextLangRef = useRef(language)
5654

5755
return (
58-
<div className="flex h-[450px] max-h-[80vh] w-[60ch] max-w-full flex-col">
59-
<TextArea defaultValue={content} className="flex-grow" ref={ref} />
56+
<div className="flex w-[60ch] max-w-full flex-col overflow-auto">
57+
<div className="max-h-[400px] overflow-auto">
58+
<CodeEditor
59+
content={content}
60+
language={language}
61+
onChange={(code) => {
62+
valueRef.current = code
63+
}}
64+
/>
65+
</div>
6066

6167
<div className="relative">
6268
<div className="absolute left-0 top-0">
@@ -78,7 +84,10 @@ const NormalCodeBlock: FC<{
7884
}}
7985
/>
8086
</div>
81-
<SharedModalAction nodeCtx={nodeCtx} getValue={getValue} />
87+
<SharedModalAction
88+
nodeCtx={nodeCtx}
89+
getValue={() => valueRef.current}
90+
/>
8291
</div>
8392
</div>
8493
)
@@ -204,7 +213,7 @@ const SharedModalAction: FC<{
204213
dismiss()
205214
}
206215
return (
207-
<div className="mt-4 flex justify-end space-x-2">
216+
<div className="mt-4 flex justify-end space-x-2 p-2">
208217
<StyledButton variant="secondary" onClick={deleteNode}>
209218
删除
210219
</StyledButton>

0 commit comments

Comments
 (0)