-
-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: milkdown code editor node view
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
5 changed files
with
81 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
import { useEffect, useState } from 'react' | ||
import type { FC } from 'react' | ||
import { forwardRef, useEffect, useState } from 'react' | ||
|
||
import { clsxm } from '~/lib/helper' | ||
|
||
import { BaseCodeHighlighter } from '../code-highlighter' | ||
|
||
export const CodeEditor: FC<{ | ||
content: string | ||
language: string | ||
export const CodeEditor = forwardRef< | ||
HTMLTextAreaElement, | ||
{ | ||
content: string | ||
language: string | ||
|
||
onChange?: (value: string) => void | ||
}> = ({ content, language, onChange }) => { | ||
onChange?: (value: string) => void | ||
minHeight?: string | ||
className?: string | ||
} | ||
>(({ content, language, onChange, minHeight, className }, ref) => { | ||
const [highlighterValue, setHighlighterValue] = useState(content) | ||
|
||
useEffect(() => { | ||
setHighlighterValue(content) | ||
}, [content]) | ||
|
||
const sharedStyles = { | ||
minHeight, | ||
} | ||
return ( | ||
<div className="relative"> | ||
<div | ||
className={clsxm( | ||
'relative [&_*]:!font-mono [&_*]:!text-base [&_*]:!leading-[1.5]', | ||
className, | ||
)} | ||
contentEditable={false} | ||
> | ||
<textarea | ||
className="absolute h-full w-full resize-none overflow-hidden bg-transparent p-0 !font-mono text-transparent caret-accent *:leading-4" | ||
contentEditable={false} | ||
ref={ref} | ||
className="absolute h-full w-full resize-none overflow-hidden bg-transparent p-0 text-transparent caret-accent" | ||
style={sharedStyles} | ||
value={highlighterValue} | ||
onChange={(e) => { | ||
setHighlighterValue(e.target.value) | ||
onChange?.(e.target.value) | ||
}} | ||
/> | ||
<BaseCodeHighlighter | ||
className="code-wrap pointer-events-none relative z-[1] !m-0 !p-0 *:!font-mono *:!leading-4" | ||
className="code-wrap pointer-events-none relative z-[1] !m-0 !p-0" | ||
style={sharedStyles} | ||
lang={language} | ||
content={highlighterValue} | ||
/> | ||
</div> | ||
) | ||
} | ||
}) | ||
|
||
CodeEditor.displayName = 'CodeEditor' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters