From d885beb56d743d36676de003502a14708481894c Mon Sep 17 00:00:00 2001 From: Timi Date: Fri, 11 Jul 2025 20:07:37 +0800 Subject: [PATCH] support preview img,audio,video for FileDetail.vue --- src/api/RepositoryAPI.ts | 19 +- src/components/FileViewer.vue | 362 ++++++++++++++++++++++++++++ src/types/Repository.ts | 29 ++- src/views/repository/FileDetail.vue | 241 ++---------------- 4 files changed, 427 insertions(+), 224 deletions(-) create mode 100644 src/components/FileViewer.vue diff --git a/src/api/RepositoryAPI.ts b/src/api/RepositoryAPI.ts index 5994651..65ea7de 100644 --- a/src/api/RepositoryAPI.ts +++ b/src/api/RepositoryAPI.ts @@ -21,14 +21,26 @@ async function listFile(name: string, branch: string, path: string): Promise { +function fileRawURL(name: string, branch: string, path: string): string { + return `${API_HOST}${BASE_URI}/${name}:${branch}/file/raw${path}`; +} + +async function fileRawByURL(rawURL: string): Promise { return axios({ - url: `${BASE_URI}/${name}:${branch}/file/raw${path}`, + url: rawURL, method: "GET", responseType: "arraybuffer" }); } +async function fileRaw(name: string, branch: string, path: string): Promise { + return fileRawByURL(fileRawURL(name, branch, path)); +} + +async function fileMimeType(name: string, branch: string, path: string): Promise { + return axios.get(`${BASE_URI}/${name}:${branch}/file/mime${path}`); +} + function downloadArchive(name: string, branch: string) { window.open(`${API_HOST}${BASE_URI}/${name}:${branch}/archive`); } @@ -43,6 +55,9 @@ export default { pagePush, listFile, fileRaw, + fileRawURL, + fileRawByURL, + fileMimeType, downloadArchive, pageLog }; diff --git a/src/components/FileViewer.vue b/src/components/FileViewer.vue new file mode 100644 index 0000000..ef1746e --- /dev/null +++ b/src/components/FileViewer.vue @@ -0,0 +1,362 @@ + + + + + diff --git a/src/types/Repository.ts b/src/types/Repository.ts index 07054f7..6577e18 100644 --- a/src/types/Repository.ts +++ b/src/types/Repository.ts @@ -1,4 +1,4 @@ -import { Model, Page, UserView } from "timi-web"; +import { Model, Page, PrismjsType, UserView } from "timi-web"; export type Repository = { id: number; @@ -61,3 +61,30 @@ export type File = { children: boolean; } + +export enum FileViewerType { + + NOT_SUPPORT = "NOT_SUPPORT", + AUDIO = "AUDIO", + VIDEO = "VIDEO", + IMAGE = "IMAGE", + TEXT = "TEXT", + CODE = "CODE", + MARKDOWN = "MARKDOWN", +} + +export type OpenFile = { + + path: string; + name: string; + raw?: ArrayBuffer; + rawURL: string; + rawFile: File; + data?: string; + removable: boolean; + isPreview: boolean; + activatedAt?: number; + mimeType?: string; + viewerType?: FileViewerType; + prismjsType?: PrismjsType; +} diff --git a/src/views/repository/FileDetail.vue b/src/views/repository/FileDetail.vue index c0e60cc..2cbba16 100644 --- a/src/views/repository/FileDetail.vue +++ b/src/views/repository/FileDetail.vue @@ -59,61 +59,24 @@ 复制 下载源码 - - - - - -
- - -
-
-
不支持预览
-
+