2018-03-20 10:12:48 -04:00
|
|
|
<script>
|
2018-07-10 09:56:50 -04:00
|
|
|
import ItemButton from './button.vue';
|
2020-03-11 17:09:19 -04:00
|
|
|
import { isTextFile } from '~/ide/utils';
|
2018-07-10 09:56:50 -04:00
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
ItemButton,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
path: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: '',
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-07-10 09:56:50 -04:00
|
|
|
showLabel: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: true,
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-07-10 09:56:50 -04:00
|
|
|
buttonCssClasses: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: null,
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-07-10 09:56:50 -04:00
|
|
|
},
|
|
|
|
methods: {
|
2018-10-22 04:28:36 -04:00
|
|
|
createFile(target, file) {
|
2018-07-10 09:56:50 -04:00
|
|
|
const { name } = file;
|
2020-02-03 13:08:46 -05:00
|
|
|
const encodedContent = target.result.split('base64,')[1];
|
2018-10-22 04:28:36 -04:00
|
|
|
const rawContent = encodedContent ? atob(encodedContent) : '';
|
2020-03-11 17:09:19 -04:00
|
|
|
const isText = isTextFile(rawContent, file.type, name);
|
2018-03-20 10:12:48 -04:00
|
|
|
|
2020-02-03 13:08:46 -05:00
|
|
|
const emitCreateEvent = content =>
|
|
|
|
this.$emit('create', {
|
|
|
|
name: `${this.path ? `${this.path}/` : ''}${name}`,
|
|
|
|
type: 'blob',
|
|
|
|
content,
|
|
|
|
binary: !isText,
|
|
|
|
rawPath: !isText ? target.result : '',
|
|
|
|
});
|
2018-03-20 10:12:48 -04:00
|
|
|
|
2020-02-03 13:08:46 -05:00
|
|
|
if (isText) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.addEventListener('load', e => emitCreateEvent(e.target.result), { once: true });
|
|
|
|
reader.readAsText(file);
|
|
|
|
} else {
|
|
|
|
emitCreateEvent(encodedContent);
|
|
|
|
}
|
2018-07-10 09:56:50 -04:00
|
|
|
},
|
|
|
|
readFile(file) {
|
|
|
|
const reader = new FileReader();
|
2018-03-20 10:12:48 -04:00
|
|
|
|
2018-10-22 04:28:36 -04:00
|
|
|
reader.addEventListener('load', e => this.createFile(e.target, file), { once: true });
|
|
|
|
reader.readAsDataURL(file);
|
2018-07-10 09:56:50 -04:00
|
|
|
},
|
|
|
|
openFile() {
|
|
|
|
Array.from(this.$refs.fileUpload.files).forEach(file => this.readFile(file));
|
2018-03-20 10:12:48 -04:00
|
|
|
},
|
2018-07-10 09:56:50 -04:00
|
|
|
startFileUpload() {
|
|
|
|
this.$refs.fileUpload.click();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-03-20 10:12:48 -04:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
2018-07-10 09:56:50 -04:00
|
|
|
<item-button
|
|
|
|
:class="buttonCssClasses"
|
|
|
|
:show-label="showLabel"
|
|
|
|
:icon-classes="showLabel ? 'mr-2' : ''"
|
|
|
|
:label="__('Upload file')"
|
|
|
|
class="d-flex"
|
|
|
|
icon="upload"
|
|
|
|
@click="startFileUpload"
|
|
|
|
/>
|
2018-03-20 10:12:48 -04:00
|
|
|
<input
|
|
|
|
id="file-upload"
|
2018-06-11 05:49:47 -04:00
|
|
|
ref="fileUpload"
|
2018-03-20 10:12:48 -04:00
|
|
|
type="file"
|
|
|
|
class="hidden"
|
2018-08-29 11:05:30 -04:00
|
|
|
multiple
|
|
|
|
@change="openFile"
|
2018-03-20 10:12:48 -04:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</template>
|