+
+ {
+ currentEditValueRef.current = value
+ }}
+ />
-
)
}
diff --git a/src/components/ui/code-editor/CodeEditor.tsx b/src/components/ui/code-editor/CodeEditor.tsx
new file mode 100644
index 0000000000..d02e26aef5
--- /dev/null
+++ b/src/components/ui/code-editor/CodeEditor.tsx
@@ -0,0 +1,35 @@
+import { useEffect, useState } from 'react'
+import type { FC } from 'react'
+
+import { BaseCodeHighlighter } from '../code-highlighter'
+
+export const CodeEditor: FC<{
+ content: string
+ language: string
+
+ onChange?: (value: string) => void
+}> = ({ content, language, onChange }) => {
+ const [highlighterValue, setHighlighterValue] = useState(content)
+
+ useEffect(() => {
+ setHighlighterValue(content)
+ }, [content])
+
+ return (
+
+
+ )
+}
diff --git a/src/components/ui/code-editor/index.demo.tsx b/src/components/ui/code-editor/index.demo.tsx
new file mode 100644
index 0000000000..0ebf06e425
--- /dev/null
+++ b/src/components/ui/code-editor/index.demo.tsx
@@ -0,0 +1,21 @@
+import type { DocumentComponent } from 'storybook/typings'
+
+import { CodeEditor } from './CodeEditor'
+
+export const CodeEditorDemo: DocumentComponent = () => {
+ return (
+
+ `const a = ${Math.random()};\n`)
+ .join('')}
+ language="javascript"
+ />
+
+ )
+}
+
+CodeEditorDemo.meta = {
+ title: 'CodeEditor',
+}
diff --git a/src/components/ui/code-editor/index.ts b/src/components/ui/code-editor/index.ts
new file mode 100644
index 0000000000..51fade8bb9
--- /dev/null
+++ b/src/components/ui/code-editor/index.ts
@@ -0,0 +1 @@
+export * from './CodeEditor'
diff --git a/src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx b/src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx
index e921fb0123..908f89bb09 100644
--- a/src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx
+++ b/src/components/ui/editor/Milkdown/plugins/CodeBlock.tsx
@@ -13,10 +13,10 @@ import { $view } from '@milkdown/utils'
import { BlockLoading } from '~/components/modules/shared/BlockLoading'
import { StyledButton } from '~/components/ui/button'
+import { CodeEditor } from '~/components/ui/code-editor'
import { HighLighter } from '~/components/ui/code-highlighter'
-import { Input, TextArea } from '~/components/ui/input'
+import { Input } from '~/components/ui/input'
import { useCurrentModal, useModalStack } from '~/components/ui/modal'
-import { useUncontrolledInput } from '~/hooks/common/use-uncontrolled-input'
import { useEditorCtx } from '../ctx'
@@ -49,14 +49,20 @@ const NormalCodeBlock: FC<{
const handleEdit = () => {
const Content: FC
= () => {
- const [, getValue, ref] =
- useUncontrolledInput(content)
-
+ const valueRef = useRef(content)
const nextLangRef = useRef(language)
return (
-
-
+
+
+ {
+ valueRef.current = code
+ }}
+ />
+
@@ -78,7 +84,10 @@ const NormalCodeBlock: FC<{
}}
/>
-
+
valueRef.current}
+ />
)
@@ -204,7 +213,7 @@ const SharedModalAction: FC<{
dismiss()
}
return (
-