优化了链接

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, FileImageOutlined,
FilePdfOutlined, FilePdfOutlined,
FileTextOutlined, FileTextOutlined,
UndoOutlined,
} from '@ant-design/icons' } from '@ant-design/icons'
import { Editor } from '@bytemd/react' import { Editor } from '@bytemd/react'
import gfm from '@bytemd/plugin-gfm' import gfm from '@bytemd/plugin-gfm'
@ -78,13 +79,19 @@ function DocumentEditor() {
return return
} }
//
const fileName = linkTarget.split('/').pop()
const linkText = `[${fileName}](${linkTarget})`
if (editorCtxRef.current && editorCtxRef.current.editor) { if (editorCtxRef.current && editorCtxRef.current.editor) {
editorCtxRef.current.editor.replaceSelection(linkText) const editor = editorCtxRef.current.editor
editorCtxRef.current.editor.focus() //
const selection = editor.getSelection()
//
const fileName = linkTarget.split('/').pop()
// 使
const linkTitle = selection || fileName
const linkText = `[${linkTitle}](${linkTarget})`
editor.replaceSelection(linkText)
editor.focus()
} }
setLinkModalVisible(false) 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 () => { const handleSaveFile = async () => {
if (!selectedFile) { if (!selectedFile) {
Toast.warning('提示', '请先选择文件') Toast.warning('提示', '请先选择文件')
@ -915,12 +944,11 @@ function DocumentEditor() {
保存 保存
</Button> </Button>
<Button <Button
danger icon={<UndoOutlined />}
icon={<DeleteOutlined />} onClick={handleReset}
onClick={handleDelete} disabled={!selectedFile || loading}
disabled={!selectedFile}
> >
删除 重置
</Button> </Button>
</Space> </Space>
</div> </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)) { if (parentPath && !openKeys.includes(parentPath)) {
// //
const pathParts = parentPath.split('/') const pathParts = parentPath.split('/')
@ -265,6 +274,7 @@ function DocumentPage() {
} }
setOpenKeys([...new Set([...openKeys, ...allParentPaths])]) setOpenKeys([...new Set([...openKeys, ...allParentPaths])])
} }
}
// //
setSelectedFile(targetPath) setSelectedFile(targetPath)
@ -590,15 +600,20 @@ function DocumentPage() {
remarkPlugins={[remarkGfm]} remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw, rehypeSlug, rehypeHighlight]} rehypePlugins={[rehypeRaw, rehypeSlug, rehypeHighlight]}
components={{ components={{
a: ({ node, href, children, ...props }) => ( a: ({ node, href, children, ...props }) => {
const isExternal = href && (href.startsWith('http') || href.startsWith('//'));
return (
<a <a
href={href} href={href}
onClick={(e) => handleMarkdownLink(e, href)} onClick={(e) => handleMarkdownLink(e, href)}
target={isExternal ? '_blank' : undefined}
rel={isExternal ? 'noopener noreferrer' : undefined}
{...props} {...props}
> >
{children} {children}
</a> </a>
), );
},
}} }}
> >
{markdownContent} {markdownContent}