修复点击 markdown 编辑按钮跳转到顶部的 bug

This commit is contained in:
tianyaxiang 2025-02-24 18:31:33 +08:00
parent 52dedb0ed6
commit 66e916517e
2 changed files with 10 additions and 5 deletions

View File

@ -194,6 +194,7 @@ export default function WechatEditor() {
const start = textarea.selectionStart const start = textarea.selectionStart
const end = textarea.selectionEnd const end = textarea.selectionEnd
const selectedText = value.substring(start, end) const selectedText = value.substring(start, end)
const scrollTop = textarea.scrollTop
let newText = '' let newText = ''
let newCursorPos = 0 let newCursorPos = 0
@ -216,6 +217,7 @@ export default function WechatEditor() {
requestAnimationFrame(() => { requestAnimationFrame(() => {
textarea.focus() textarea.focus()
textarea.scrollTop = scrollTop
textarea.setSelectionRange(newCursorPos, newCursorPos) textarea.setSelectionRange(newCursorPos, newCursorPos)
}) })
}, [value, handleEditorChange]) }, [value, handleEditorChange])

View File

@ -152,11 +152,14 @@ export function MarkdownToolbar({ onInsert }: MarkdownToolbarProps) {
variant="ghost" variant="ghost"
size="sm" size="sm"
className="h-8 w-8 p-0" className="h-8 w-8 p-0"
onClick={() => onInsert(buttonTool.text, { onClick={(e) => {
wrap: buttonTool.wrap, e.preventDefault()
placeholder: buttonTool.placeholder, onInsert(buttonTool.text, {
suffix: buttonTool.suffix wrap: buttonTool.wrap,
})} placeholder: buttonTool.placeholder,
suffix: buttonTool.suffix
})
}}
> >
{buttonTool.icon} {buttonTool.icon}
</Button> </Button>