diff --git a/README.md b/README.md index 3d5d682..efc7857 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ todo: 标签页,miaoDirectory切换动画 [finish] todo: 重构virtualPage,所有page以插件形式使用 [finish] -todo: miaoDirectory插件化,为后续插件功能开发做准备 [finish] +todo: miaoDirectory插件化,为后续插件功能开发做准备 [finish] todo: miaoPopupInput, 弹出一个输入框 [finish] todo: miaoDirectory的重命名功能 [finish] todo: miaoDirectory的剪切功能 [finish] todo: miaoDirectory的上传按钮 [finish] +todo: miaoDirectory的新建文件/文件夹功能 [finish] todo: VirtualDirectory更新时更新文件信息 -todo: miaoDirectory的新建文件/文件夹功能 todo: miaoDirectory的复制文件/文件夹功能 todo: miaoDirectory的多选功能 \ No newline at end of file diff --git a/src/class/VirtualDirectory.ts b/src/class/VirtualDirectory.ts index 34cb700..c5ac422 100644 --- a/src/class/VirtualDirectory.ts +++ b/src/class/VirtualDirectory.ts @@ -71,11 +71,11 @@ class VirtualDirectory { * 获取从根目录开始的按顺序的文件夹 * @returns {VirtualDirectory[]} */ - get directoryArray(): VirtualDirectory[] { + get getParents(): VirtualDirectory[] { if (!this.parent) { return [this]; } - return [...this.parent.directoryArray, this]; + return [...this.parent.getParents, this]; } /** @@ -83,11 +83,11 @@ class VirtualDirectory { * @param {number} layer 需要跳转的层数 * @returns {VirtualDirectory} */ - getParents(layer: number): VirtualDirectory { + getParent(layer: number): VirtualDirectory { if (layer <= 0 || !this.parent) { return this; } - return this.parent.getParents(layer - 1); + return this.parent.getParent(layer - 1); } updateContent(content: (file | directory)[]) { diff --git a/src/components/miaoContextMenu.vue b/src/components/miaoContextMenu.vue index 802fc4b..d4f339b 100644 --- a/src/components/miaoContextMenu.vue +++ b/src/components/miaoContextMenu.vue @@ -59,7 +59,7 @@ const handleContextMenu = (e: MouseEvent) => { const handleTouchStart = (e: TouchEvent) => { if(props.touchTimeOut === undefined) { - return + return } timer_touch ?? clearTimeout(timer_touch) timer_touch = setTimeout(() => { diff --git a/src/config.ts b/src/config.ts index 5100b80..725703d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,15 +1,16 @@ const baseUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : ''); export default { - baseUrl, - uploadSizeLimit: 1024 * 1024 * 100, - api: { - get: '/get', - upload: '/upload', - delete: '/delete', - query: '/query', - rename: '/rename', - cut: '/cut' - }, - enableUpload: true, -}; + baseUrl, + uploadSizeLimit: 1024 * 1024 * 100, + api: { + get: '/get', + upload: '/upload', + delete: '/delete', + query: '/query', + rename: '/rename', + mkdir: '/mkdir', + cut: '/cut' + }, + enableUpload: true +} diff --git a/src/hooks/useMiaoFetchApi.ts b/src/hooks/useMiaoFetchApi.ts index 5f7b786..f1a5bd4 100644 --- a/src/hooks/useMiaoFetchApi.ts +++ b/src/hooks/useMiaoFetchApi.ts @@ -1,199 +1,266 @@ -import VirtualDirectory, { VirtualFile } from "@/class/VirtualDirectory"; -import { getOption, getResponse, miaoFetchConfig, deleteOption, uploadOption, queryOption, renameOption, cutOption } from "@/types/fetch"; -import Config from "@/config"; -import axios, { AxiosRequestConfig } from "axios"; +import VirtualDirectory, { VirtualFile } from '@/class/VirtualDirectory' +import { + getOption, + getResponse, + miaoFetchConfig, + deleteOption, + uploadOption, + queryOption, + renameOption, + cutOption, + mkdirOption +} from '@/types/fetch' +import Config from '@/config' +import axios, { AxiosRequestConfig } from 'axios' export const useMiaoFetch = (option?: miaoFetchConfig) => { - const retry = option?.retry ?? 0; - const controller = new AbortController(); - let flag = true; - return { - miaoFetch: async (config: AxiosRequestConfig) => { - let counter = 0; - let response: any; - let errList = []; - while (flag && counter <= retry) { - try { - response = await axios({ - ...config, - signal: controller.signal - }); - return response; - } catch (_e) { - errList.push(_e); - counter += 1; - } - } - throw errList; - }, - /** - * 强制终止miaoFetch - */ - abortMiaoFetch: () => { - flag = false; - controller.abort(); - } - }; -}; + const retry = option?.retry ?? 0 + const controller = new AbortController() + let flag = true + return { + miaoFetch: async (config: AxiosRequestConfig) => { + let counter = 0 + let response: any + let errList = [] + while (flag && counter <= retry) { + try { + response = await axios({ + ...config, + signal: controller.signal + }) + return response + } catch (_e) { + errList.push(_e) + counter += 1 + } + } + throw errList + }, + /** + * 强制终止miaoFetch + */ + abortMiaoFetch: () => { + flag = false + controller.abort() + } + } +} const miaoFetchApi = { - /** - * 获取对应文件夹下的数据 - */ - async get(vDirectory: VirtualDirectory, option?: getOption): Promise { - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.get}${vDirectory.path}`; - const retry = option?.retry ?? 0; + /** + * 获取对应文件夹下的数据 + */ + async get( + vDirectory: VirtualDirectory, + option?: getOption + ): Promise { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.get}${vDirectory.path}` + const retry = option?.retry ?? 0 - const { miaoFetch } = useMiaoFetch({ - retry - }); - const response = await miaoFetch({ - url - }); - return response.data; - }, + const { miaoFetch } = useMiaoFetch({ + retry + }) + const response = await miaoFetch({ + url + }) + return response.data + }, - /** - * 上传单个文件到对应文件夹, 后续要将data类型改成buffer以支持分片上传 - */ - upload(data: File, targetVDir: VirtualDirectory, option?: uploadOption) { - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.upload}${targetVDir.path}`; - const retry = option?.retry ?? 0; - const { miaoFetch, abortMiaoFetch } = useMiaoFetch({ - retry - }); - const sizeTotal = data.size; + /** + * 上传单个文件到对应文件夹, 后续要将data类型改成buffer以支持分片上传 + * 正常上传文件请使用uploadQueue + */ + upload(data: File, targetVDir: VirtualDirectory, option?: uploadOption) { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.upload}${targetVDir.path}` + const retry = option?.retry ?? 0 + const { miaoFetch, abortMiaoFetch } = useMiaoFetch({ + retry + }) + const sizeTotal = data.size - const opType = option?.type ?? "write"; + const opType = option?.type ?? 'write' - const formData = new FormData(); + const formData = new FormData() - formData.append("file", data); - // 必须这样传,不然中文乱码 - formData.append("fileName", data.name); - formData.append("oprateType", opType); + formData.append('file', data) + // 必须这样传,不然中文乱码 + formData.append('fileName', data.name) + formData.append('operateType', opType) - return { - abort: () => abortMiaoFetch(), - response: new Promise<{ - message: "success" | "failed"; - error?: string; - }>(async resolve => { - const response = await miaoFetch({ - url, - method: "post", - data: formData, - onUploadProgress(progressEvent) { - option && option.onProcess && option.onProcess(progressEvent.loaded / sizeTotal); - } - }); - resolve(response.data); - }) - }; - }, + return { + abort: () => abortMiaoFetch(), + response: new Promise<{ + message: 'success' | 'failed' + error?: string + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'post', + data: formData, + onUploadProgress(progressEvent) { + option && + option.onProcess && + option.onProcess(progressEvent.loaded / sizeTotal) + } + }) + resolve(response.data) + }) + } + }, - /** - * 删除对应的文件或文件夹,若为文件夹会递归删除 - */ - delete(target: VirtualFile | VirtualDirectory, option?: deleteOption) { - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.delete}${target.path}`; - const retry = option?.retry ?? 0; - const { miaoFetch } = useMiaoFetch({ - retry - }); + /** + * 在目标文件夹下创建新的文件夹 + */ + mkdir( + targetVDir: VirtualDirectory, + newFolderName: string, + option?: mkdirOption + ) { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.mkdir}${targetVDir.path}` + const { miaoFetch } = useMiaoFetch({ + retry: option?.retry ?? 0 + }) - return { - response: new Promise<{ - eventId: number; - }>(async resolve => { - const response = await miaoFetch({ - url, - method: "post" - }); - resolve(response.data); - }) - }; - }, - /** - * 重命名对应的文件或文件夹 - */ - rename(target: VirtualFile | VirtualDirectory, newName: string, option?: renameOption) { - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.rename}${target.path}`; - const retry = option?.retry ?? 0; - const { miaoFetch } = useMiaoFetch({ - retry - }); + return new Promise<{ + message: 'success' | 'failed' + error?: string + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'post', + data: { + folderName: newFolderName + } + }) + resolve(response.data) + }) + }, + /** + * 在目标文件夹下创建新的空文件,实际上是通过upload空文件实现 + */ + mkFile( + targetVDir: VirtualDirectory, + newFileName: string, + option?: uploadOption + ) { + const emptyBlob = new Blob([]) + const emptyFile = new File([emptyBlob], newFileName, { + type: 'text/plain' + }) + return this.upload(emptyFile, targetVDir, option) + }, - return { - response: new Promise<{ - eventId: number; - }>(async resolve => { - const response = await miaoFetch({ - url, - method: "post", - data: { newName } - }); - resolve(response.data); - }) - }; - }, - /** - * 剪切文件或文件夹 - */ - cut(target: VirtualFile | VirtualDirectory, to: VirtualDirectory, option?: cutOption) { - const newPath = to.path - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.cut}${target.path}`; - const retry = option?.retry ?? 0; - const { miaoFetch } = useMiaoFetch({ - retry - }); - return { - response: new Promise<{ - eventId: number; - }>(async resolve => { - const response = await miaoFetch({ - url, - method: "post", - data: { newPath } - }); - resolve(response.data); - }) - }; - }, + /** + * 删除对应的文件或文件夹,若为文件夹会递归删除 + */ + delete(target: VirtualFile | VirtualDirectory, option?: deleteOption) { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.delete}${target.path}` + const retry = option?.retry ?? 0 + const { miaoFetch } = useMiaoFetch({ + retry + }) - /** - * 查询服务端操作事件的进展 - */ - query(eventId: number, option?: queryOption) { - const { baseUrl, api } = Config; - const url = `${baseUrl}${api.query}`; - const retry = option?.retry ?? 0; - const { miaoFetch } = useMiaoFetch({ - retry - }); - return { - response: new Promise<{ - status: "process" | "success" | "failed"; - error?: unknown; - }>(async resolve => { - const response = await miaoFetch({ - url, - method: "get", - params: { - id: eventId - } - }); - resolve(response.data); - }) - }; - } -}; + return { + response: new Promise<{ + eventId: number + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'post' + }) + resolve(response.data) + }) + } + }, + /** + * 重命名对应的文件或文件夹 + */ + rename( + target: VirtualFile | VirtualDirectory, + newName: string, + option?: renameOption + ) { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.rename}${target.path}` + const retry = option?.retry ?? 0 + const { miaoFetch } = useMiaoFetch({ + retry + }) + + return { + response: new Promise<{ + eventId: number + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'post', + data: { newName } + }) + resolve(response.data) + }) + } + }, + /** + * 剪切文件或文件夹 + */ + cut( + target: VirtualFile | VirtualDirectory, + to: VirtualDirectory, + option?: cutOption + ) { + const newPath = to.path + const { baseUrl, api } = Config + const url = `${baseUrl}${api.cut}${target.path}` + const retry = option?.retry ?? 0 + const { miaoFetch } = useMiaoFetch({ + retry + }) + return { + response: new Promise<{ + eventId: number + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'post', + data: { newPath } + }) + resolve(response.data) + }) + } + }, + + /** + * 查询服务端操作事件的进展 + */ + query(eventId: number, option?: queryOption) { + const { baseUrl, api } = Config + const url = `${baseUrl}${api.query}` + const retry = option?.retry ?? 0 + const { miaoFetch } = useMiaoFetch({ + retry + }) + return { + response: new Promise<{ + status: 'process' | 'success' | 'failed' + error?: unknown + }>(async (resolve) => { + const response = await miaoFetch({ + url, + method: 'get', + params: { + id: eventId + } + }) + resolve(response.data) + }) + } + } +} export default function useMiaoFetchApi() { - return miaoFetchApi; + return miaoFetchApi } diff --git a/src/types/fetch.ts b/src/types/fetch.ts index 9a21dae..9fc1508 100644 --- a/src/types/fetch.ts +++ b/src/types/fetch.ts @@ -24,5 +24,6 @@ export type uploadOption = { export type deleteOption = {} & miaoFetchConfig; export type renameOption = {} & miaoFetchConfig; -export type cutOption = {} & miaoFetchConfig; +export type cutOption = {} & miaoFetchConfig +export type mkdirOption = {} & miaoFetchConfig export type queryOption = {} & miaoFetchConfig; diff --git a/src/views/miaoDirectory.vue b/src/views/miaoDirectory.vue index 7a72ddf..311ad86 100644 --- a/src/views/miaoDirectory.vue +++ b/src/views/miaoDirectory.vue @@ -1,12 +1,18 @@