From 7dd6d4cf5325024526df8e8fd46558818fd43a06 Mon Sep 17 00:00:00 2001 From: taqi be Date: Thu, 16 Jan 2025 14:31:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20md=20=E5=9B=BE=E7=89=87=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/module/eam/device/document.ts | 10 +++++ .../fileDoc/components/markdownDrawer.vue | 37 ++++++++++++++++++- src/pages/fileDoc/utils.ts | 20 ++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/api/module/eam/device/document.ts b/src/api/module/eam/device/document.ts index 33707fd..cee3fc0 100644 --- a/src/api/module/eam/device/document.ts +++ b/src/api/module/eam/device/document.ts @@ -98,3 +98,13 @@ export function updateMarkdown(data: MarkdownVO) { data: data, }) } + +export function createImage(data: any) { + return documentServer({ + url: '/image/upload', + method: 'post', + data, + headers: { 'Content-Type': 'multipart/form-data' }, + timeout: 0, + }) +} diff --git a/src/pages/fileDoc/components/markdownDrawer.vue b/src/pages/fileDoc/components/markdownDrawer.vue index 490d804..2a3ae51 100644 --- a/src/pages/fileDoc/components/markdownDrawer.vue +++ b/src/pages/fileDoc/components/markdownDrawer.vue @@ -11,7 +11,12 @@
{{ data.fileName }}
- +
@@ -27,7 +32,7 @@ import { isResError, useMessage } from '@/hooks/useMessage' import { cloneDeep } from 'lodash' import EDFSButton from '@/components/dashboard/Edfs-button/index.vue' import Vditor from 'vditor' -import { type ContentType } from '@/api/module/eam/device/document' +import { createImage, type ContentType } from '@/api/module/eam/device/document' import 'vditor/dist/index.css' import { vditorToolbar } from '../utils' interface Props { @@ -81,6 +86,34 @@ function initEditor() { after: () => { contentEditor.value?.setValue(props?.data?.content ?? '') }, + upload: { + url: ' ', + accept: 'image/*', + multiple: false, + handler: async files => { + const data = new FormData() + data.append('file', files[0]) + data.append('name', files[0].name) + // 校验文件类型 智能上传图片 + if (!files[0].type.includes('image')) { + message.error('请上传图片') + return null + } + + const res = await createImage(data) + if (isResError(res)) { + message.error('上传失败') + return null + } + const { url } = res.data + if (!url) { + message.success('上传失败,图片路径未找到!') + return null + } + contentEditor.value?.insertValue(`![image](${url})`) + return null + }, + }, }) } diff --git a/src/pages/fileDoc/utils.ts b/src/pages/fileDoc/utils.ts index 430ea9e..5885f26 100644 --- a/src/pages/fileDoc/utils.ts +++ b/src/pages/fileDoc/utils.ts @@ -28,6 +28,7 @@ export const vditorToolbar = [ 'line', 'quote', 'list', + 'upload', 'ordered-list', 'check', 'outdent', @@ -46,3 +47,22 @@ export const vditorToolbar = [ 'redo', 'fullscreen', ] + +// 拼接前缀函数 +function addPrefixToImages(str: string, prefix: string) { + return str.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (match, alt, url) => { + // 检查是否已经有前缀,避免重复拼接 + if (!url.startsWith(prefix)) { + return `![${alt}](${prefix}/${url.replace(/^\//, '')})` + } + return match + }) +} + +// 去除前缀函数 +function removePrefixFromImages(str: string, prefix: string) { + const prefixPattern = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/?`) + return str.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (match, alt, url) => { + return `![${alt}](${url.replace(prefixPattern, '')})` + }) +}