Files
cloudron-box/dashboard/src/components/FileViewer.vue

104 lines
3.0 KiB
Vue
Raw Normal View History

2023-02-26 23:34:31 +01:00
<template>
<div class="viewer">
2024-06-09 15:53:17 +02:00
<TextViewer ref="textEditor"
2023-04-12 15:59:48 +02:00
v-show="active === 'textEditor'"
:save-handler="saveHandler"
@close="onClose"
:tr="$t"
2023-04-12 15:59:48 +02:00
/>
2024-07-22 18:00:33 +02:00
<ImageViewer ref="imageViewer" v-show="active === 'imageViewer'" @close="onClose" :navigation-handler="imageViewerNavigationHandler"/>
2023-04-01 11:33:22 +02:00
</div>
2023-02-26 23:34:31 +01:00
</template>
<script>
2024-06-09 15:53:17 +02:00
import { TextViewer, ImageViewer } from 'pankow-viewers';
2023-02-26 23:34:31 +01:00
import { createDirectoryModel } from '../models/DirectoryModel.js';
2023-04-01 10:41:35 +02:00
import { sanitize } from 'pankow/utils';
2023-02-26 23:34:31 +01:00
const API_ORIGIN = import.meta.env.VITE_API_ORIGIN ? import.meta.env.VITE_API_ORIGIN : window.location.origin;
2023-02-26 23:34:31 +01:00
export default {
name: 'FileViewer',
2023-02-26 23:34:31 +01:00
components: {
2023-04-01 11:33:22 +02:00
ImageViewer,
2024-06-09 15:53:17 +02:00
TextViewer
2023-02-26 23:34:31 +01:00
},
data() {
return {
2023-04-11 12:14:47 +02:00
resourceId: '',
resourceType: '',
2023-04-01 11:33:22 +02:00
item: null,
active: ''
2023-02-26 23:34:31 +01:00
};
},
methods: {
onClose() {
location.replace('#/home' + location.hash.slice('#/viewer'.length, location.hash.lastIndexOf('/')+1));
2023-04-12 15:59:48 +02:00
},
2024-07-22 18:00:33 +02:00
imageViewerNavigationHandler() {
// nothing to do
},
2023-04-12 15:59:48 +02:00
async saveHandler(item, content) {
await this.directoryModel.save(this.filePath, content);
2023-02-26 23:34:31 +01:00
}
},
async mounted() {
2023-04-11 12:14:47 +02:00
this.resourceId = this.$route.params.resourceId;
this.resourceType = this.$route.params.type;
2023-02-26 23:34:31 +01:00
2023-04-12 15:59:48 +02:00
this.filePath = this.$route.params.filePath.join('/');
2023-02-26 23:34:31 +01:00
const fileName = this.$route.params.filePath[this.$route.params.filePath.length-1];
2023-04-12 15:59:48 +02:00
const parentDirectoryPath = sanitize(this.filePath.split('/').slice(0, -1).join('/'));
2023-02-26 23:34:31 +01:00
this.directoryModel = createDirectoryModel(API_ORIGIN, localStorage.token, (this.resourceType === 'volume' ? 'volumes/' : 'apps/') + this.resourceId);
2023-02-26 23:34:31 +01:00
const files = await this.directoryModel.listFiles(parentDirectoryPath);
this.item = files.find(i => i.fileName === fileName);
if (!this.item) {
2023-04-12 15:59:48 +02:00
console.log('File not found', this.filePath);
2023-02-26 23:34:31 +01:00
return;
}
2023-04-01 11:33:22 +02:00
if (this.$refs.imageViewer.canHandle(this.item)) {
// collect other files in directory for prev/next action
const otherSupportedEntries = files.filter((item) => this.$refs.imageViewer.canHandle(item)).map((item) => {
2023-09-20 09:49:25 +02:00
item.resourcePath = `/viewer/${this.resourceType}/${this.resourceId}${item.folderPath}/${item.fileName}`;
item.fullFileUrl = this.directoryModel.getFileUrl(`${item.folderPath}/${item.fileName}`);
return item;
});
this.$refs.imageViewer.open(this.item, otherSupportedEntries);
2023-04-01 11:33:22 +02:00
this.active = 'imageViewer';
} else if (this.$refs.textEditor.canHandle(this.item)) {
2023-04-12 15:59:48 +02:00
const content = await this.directoryModel.getFile(this.filePath);
2023-02-26 23:34:31 +01:00
this.$refs.textEditor.open(this.item, content);
2023-04-01 11:33:22 +02:00
this.active = 'textEditor';
2023-02-26 23:34:31 +01:00
} else {
2023-04-12 15:59:48 +02:00
console.warn(`no editor or viewer found for ${this.item.mimeType}`, this.item);
2023-04-01 11:33:22 +02:00
this.active = '';
window.location.replace(this.directoryModel.getFileUrl(this.filePath));
2023-02-26 23:34:31 +01:00
}
}
};
</script>
<style scoped>
2023-04-01 11:33:22 +02:00
.viewer{
2023-02-26 23:34:31 +01:00
flex-grow: 1;
overflow: hidden;
height: 100%;
display: flex;
}
.main-view-col {
overflow: auto;
flex-grow: 1;
}
</style>