优化了链接

main
mula.liu 2026-01-06 15:50:05 +08:00
parent 2af47195c1
commit a6e2e95cc3
2 changed files with 78 additions and 35 deletions

View File

@ -17,6 +17,7 @@ import {
FileImageOutlined,
FilePdfOutlined,
FileTextOutlined,
UndoOutlined,
} from '@ant-design/icons'
import { Editor } from '@bytemd/react'
import gfm from '@bytemd/plugin-gfm'
@ -78,13 +79,19 @@ function DocumentEditor() {
return
}
//
const fileName = linkTarget.split('/').pop()
const linkText = `[${fileName}](${linkTarget})`
if (editorCtxRef.current && editorCtxRef.current.editor) {
editorCtxRef.current.editor.replaceSelection(linkText)
editorCtxRef.current.editor.focus()
const editor = editorCtxRef.current.editor
//
const selection = editor.getSelection()
//
const fileName = linkTarget.split('/').pop()
// 使
const linkTitle = selection || fileName
const linkText = `[${linkTitle}](${linkTarget})`
editor.replaceSelection(linkText)
editor.focus()
}
setLinkModalVisible(false)
@ -197,6 +204,28 @@ function DocumentEditor() {
}
}
//
const handleReset = () => {
if (!selectedFile) return
Modal.confirm({
title: '确认重置',
content: '确定要重置当前修改吗?所有未保存的更改都将丢失。',
onOk: async () => {
setLoading(true)
try {
const res = await getFileContent(projectId, selectedFile)
setFileContent(res.data.content)
Toast.success('重置成功', '已恢复至最后保存的版本')
} catch (error) {
Toast.error('重置失败', '无法重新加载文件内容')
} finally {
setLoading(false)
}
},
})
}
const handleSaveFile = async () => {
if (!selectedFile) {
Toast.warning('提示', '请先选择文件')
@ -915,12 +944,11 @@ function DocumentEditor() {
保存
</Button>
<Button
danger
icon={<DeleteOutlined />}
onClick={handleDelete}
disabled={!selectedFile}
icon={<UndoOutlined />}
onClick={handleReset}
disabled={!selectedFile || loading}
>
删除
重置
</Button>
</Space>
</div>

View File

@ -249,11 +249,20 @@ function DocumentPage() {
// 使
}
//
const targetPath = resolveRelativePath(selectedFile, decodedHref)
//
let targetPath
if (decodedHref.startsWith('.') || decodedHref.startsWith('..')) {
//
targetPath = resolveRelativePath(selectedFile, decodedHref)
} else {
//
targetPath = decodedHref.startsWith('/') ? decodedHref.substring(1) : decodedHref
}
//
const parentPath = targetPath.substring(0, targetPath.lastIndexOf('/'))
const lastSlashIndex = targetPath.lastIndexOf('/')
if (lastSlashIndex !== -1) {
const parentPath = targetPath.substring(0, lastSlashIndex)
if (parentPath && !openKeys.includes(parentPath)) {
//
const pathParts = parentPath.split('/')
@ -265,6 +274,7 @@ function DocumentPage() {
}
setOpenKeys([...new Set([...openKeys, ...allParentPaths])])
}
}
//
setSelectedFile(targetPath)
@ -590,15 +600,20 @@ function DocumentPage() {
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw, rehypeSlug, rehypeHighlight]}
components={{
a: ({ node, href, children, ...props }) => (
a: ({ node, href, children, ...props }) => {
const isExternal = href && (href.startsWith('http') || href.startsWith('//'));
return (
<a
href={href}
onClick={(e) => handleMarkdownLink(e, href)}
target={isExternal ? '_blank' : undefined}
rel={isExternal ? 'noopener noreferrer' : undefined}
{...props}
>
{children}
</a>
),
);
},
}}
>
{markdownContent}