Covnerted views/Files.vue to composition api
This commit is contained in:
parent
cdcf05545a
commit
1bca23b3dc
@ -36,7 +36,7 @@ export const useFileStore = defineStore("file", {
|
|||||||
toggleMultiple() {
|
toggleMultiple() {
|
||||||
this.multiple = !this.multiple;
|
this.multiple = !this.multiple;
|
||||||
},
|
},
|
||||||
updateRequest(value: IFile) {
|
updateRequest(value: IFile | null) {
|
||||||
this.oldReq = this.req;
|
this.oldReq = this.req;
|
||||||
this.req = value;
|
this.req = value;
|
||||||
},
|
},
|
||||||
|
|||||||
@ -25,7 +25,7 @@ export const useLayoutStore = defineStore("layout", {
|
|||||||
toggleShell() {
|
toggleShell() {
|
||||||
this.showShell = !this.showShell;
|
this.showShell = !this.showShell;
|
||||||
},
|
},
|
||||||
showHover(value: LayoutValue) {
|
showHover(value: LayoutValue | string) {
|
||||||
if (typeof value !== "object") {
|
if (typeof value !== "object") {
|
||||||
this.show = value;
|
this.show = value;
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -20,7 +20,8 @@ export const useUploadStore = defineStore("upload", {
|
|||||||
sizes: any[],
|
sizes: any[],
|
||||||
progress: any[],
|
progress: any[],
|
||||||
queue: any[],
|
queue: any[],
|
||||||
uploads: uploads
|
uploads: uploads,
|
||||||
|
error: any
|
||||||
} => ({
|
} => ({
|
||||||
id: 0,
|
id: 0,
|
||||||
sizes: [],
|
sizes: [],
|
||||||
|
|||||||
2
frontend/src/types/file.d.ts
vendored
2
frontend/src/types/file.d.ts
vendored
@ -16,7 +16,7 @@ interface IFile {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
type uploadType = "video" | "audio" | "image" | "pdf" | "text" | "blob"
|
type uploadType = "video" | "audio" | "image" | "pdf" | "text" | "blob" | "textImmutable"
|
||||||
|
|
||||||
type req = {
|
type req = {
|
||||||
path: string
|
path: string
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<header-bar v-if="error || req?.type === null" showMenu showLogo />
|
<header-bar v-if="error || fileStore.req?.type === null" showMenu showLogo />
|
||||||
|
|
||||||
<breadcrumbs base="/files" />
|
<breadcrumbs base="/files" />
|
||||||
|
|
||||||
<errors v-if="error" :errorCode="error.status" />
|
<errors v-if="error" :errorCode="error?.status" />
|
||||||
<component v-else-if="currentView" :is="currentView"></component>
|
<component v-else-if="currentView" :is="currentView"></component>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<h2 class="message delayed">
|
<h2 class="message delayed">
|
||||||
@ -19,8 +19,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script setup lang="ts">
|
||||||
import { defineAsyncComponent } from "vue";
|
import { computed, defineAsyncComponent, onBeforeUnmount, onMounted, onUnmounted, ref, watch } from "vue";
|
||||||
import { files as api } from "@/api";
|
import { files as api } from "@/api";
|
||||||
import { mapState, mapActions, mapWritableState } from "pinia";
|
import { mapState, mapActions, mapWritableState } from "pinia";
|
||||||
import { useFileStore } from "@/stores/file";
|
import { useFileStore } from "@/stores/file";
|
||||||
@ -32,127 +32,114 @@ import Breadcrumbs from "@/components/Breadcrumbs.vue";
|
|||||||
import Errors from "@/views/Errors.vue";
|
import Errors from "@/views/Errors.vue";
|
||||||
import Preview from "@/views/files/Preview.vue";
|
import Preview from "@/views/files/Preview.vue";
|
||||||
import FileListing from "@/views/files/FileListing.vue";
|
import FileListing from "@/views/files/FileListing.vue";
|
||||||
|
import { useI18n } from "vue-i18n";
|
||||||
|
import { useRoute, useRouter } from "vue-router";
|
||||||
|
|
||||||
function clean(path) {
|
const layoutStore = useLayoutStore()
|
||||||
|
const fileStore = useFileStore()
|
||||||
|
const uploadStore = useUploadStore()
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
|
||||||
|
|
||||||
|
const { t } = useI18n({});
|
||||||
|
|
||||||
|
const clean = (path: string) => {
|
||||||
return path.endsWith("/") ? path.slice(0, -1) : path;
|
return path.endsWith("/") ? path.slice(0, -1) : path;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
const Editor = defineAsyncComponent(() => import("@/views/files/Editor.vue"));
|
||||||
name: "files",
|
const error = ref<any | null>(null)
|
||||||
components: {
|
const width = computed(() => window.innerWidth)
|
||||||
HeaderBar,
|
|
||||||
Breadcrumbs,
|
|
||||||
Errors,
|
|
||||||
Preview,
|
|
||||||
FileListing,
|
|
||||||
Editor: defineAsyncComponent(() => import("@/views/files/Editor.vue")),
|
|
||||||
},
|
|
||||||
data: function () {
|
|
||||||
return {
|
|
||||||
error: null,
|
|
||||||
width: window.innerWidth,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
inject: ["$showError"],
|
|
||||||
computed: {
|
|
||||||
...mapWritableState(useFileStore, [
|
|
||||||
"req",
|
|
||||||
"reload",
|
|
||||||
"selected",
|
|
||||||
"multiple",
|
|
||||||
"isFiles",
|
|
||||||
]),
|
|
||||||
...mapState(useLayoutStore, ["show", "showShell"]),
|
|
||||||
...mapWritableState(useLayoutStore, ["loading"]),
|
|
||||||
...mapState(useUploadStore, {
|
|
||||||
uploadError: "error",
|
|
||||||
}),
|
|
||||||
currentView() {
|
|
||||||
if (this.req?.type == undefined) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.req.isDir) {
|
const currentView = computed(() => {
|
||||||
return "file-listing";
|
if (fileStore.req?.type == undefined) {
|
||||||
} else if (
|
return null;
|
||||||
this.req.type === "text" ||
|
}
|
||||||
this.req.type === "textImmutable"
|
|
||||||
) {
|
if (fileStore.req.isDir) {
|
||||||
return "editor";
|
return "file-listing";
|
||||||
} else {
|
} else if (
|
||||||
return "preview";
|
fileStore.req.type === "text" ||
|
||||||
}
|
fileStore.req.type === "textImmutable"
|
||||||
},
|
) {
|
||||||
},
|
return "editor";
|
||||||
created() {
|
} else {
|
||||||
this.fetchData();
|
return "preview";
|
||||||
},
|
}
|
||||||
watch: {
|
})
|
||||||
$route: "fetchData",
|
|
||||||
reload: function (value) {
|
// Define hooks
|
||||||
if (value === true) {
|
onMounted(() => {
|
||||||
this.fetchData();
|
fetchData();
|
||||||
}
|
fileStore.isFiles = true;
|
||||||
},
|
window.addEventListener("keydown", keyEvent);
|
||||||
uploadError(newValue, oldValue) {
|
})
|
||||||
newValue && newValue !== oldValue && this.$showError(this.uploadError);
|
|
||||||
},
|
onBeforeUnmount(() => {
|
||||||
},
|
window.removeEventListener("keydown", keyEvent);
|
||||||
mounted() {
|
})
|
||||||
this.isFiles = true;
|
|
||||||
window.addEventListener("keydown", this.keyEvent);
|
onUnmounted(() => {
|
||||||
},
|
fileStore.isFiles = false;
|
||||||
beforeUnmount() {
|
if (layoutStore.showShell) {
|
||||||
window.removeEventListener("keydown", this.keyEvent);
|
layoutStore.toggleShell();
|
||||||
},
|
}
|
||||||
unmounted() {
|
fileStore.updateRequest(null);
|
||||||
this.isFiles = false;
|
})
|
||||||
if (this.showShell) {
|
|
||||||
this.toggleShell();
|
watch(route, () => fetchData())
|
||||||
|
// @ts-ignore
|
||||||
|
watch(fileStore.reload, (val) => {
|
||||||
|
if (val) {
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
watch(uploadStore.error, (newValue, oldValue) => {
|
||||||
|
newValue && newValue !== oldValue && layoutStore.showError();
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// Define functions
|
||||||
|
|
||||||
|
const fetchData = async () => {
|
||||||
|
// Reset view information.
|
||||||
|
fileStore.reload = false;
|
||||||
|
fileStore.selected = [];
|
||||||
|
fileStore.multiple = false;
|
||||||
|
layoutStore.closeHovers();
|
||||||
|
|
||||||
|
// Set loading to true and reset the error.
|
||||||
|
layoutStore.loading = true;
|
||||||
|
error.value = null;
|
||||||
|
|
||||||
|
let url = route.path;
|
||||||
|
if (url === "") url = "/";
|
||||||
|
if (url[0] !== "/") url = "/" + url;
|
||||||
|
try {
|
||||||
|
const res = await api.fetch(url);
|
||||||
|
|
||||||
|
if (
|
||||||
|
clean(res.path) !==
|
||||||
|
clean(`/${[...route.params.path].join("/")}`)
|
||||||
|
) {
|
||||||
|
throw new Error("Data Mismatch!");
|
||||||
}
|
}
|
||||||
this.updateRequest({});
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
...mapActions(useLayoutStore, ["toggleShell", "showHover", "closeHovers"]),
|
|
||||||
...mapActions(useFileStore, ["updateRequest"]),
|
|
||||||
async fetchData() {
|
|
||||||
// Reset view information.
|
|
||||||
this.reload = false;
|
|
||||||
this.selected = [];
|
|
||||||
this.multiple = false;
|
|
||||||
this.closeHovers();
|
|
||||||
|
|
||||||
// Set loading to true and reset the error.
|
fileStore.updateRequest(res);
|
||||||
this.loading = true;
|
document.title = `${res.name} - ${document.title}`;
|
||||||
this.error = null;
|
} catch (e: any) {
|
||||||
|
error.value = e;
|
||||||
let url = this.$route.path;
|
} finally {
|
||||||
if (url === "") url = "/";
|
layoutStore.loading = false;
|
||||||
if (url[0] !== "/") url = "/" + url;
|
}
|
||||||
try {
|
}
|
||||||
const res = await api.fetch(url);
|
const keyEvent = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === "F1") {
|
||||||
if (
|
event.preventDefault();
|
||||||
clean(res.path) !==
|
layoutStore.showHover("help");
|
||||||
clean(`/${[...this.$route.params.path].join("/")}`)
|
}
|
||||||
) {
|
}
|
||||||
throw new Error("Data Mismatch!");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.updateRequest(res);
|
|
||||||
document.title = `${res.name} - ${document.title}`;
|
|
||||||
} catch (e) {
|
|
||||||
this.error = e;
|
|
||||||
} finally {
|
|
||||||
this.loading = false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
keyEvent(event) {
|
|
||||||
if (event.key === "F1") {
|
|
||||||
event.preventDefault();
|
|
||||||
this.showHover("help");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user