Handle document upload in image form

This commit is contained in:
Ramon Wenger 2019-05-15 15:14:32 +02:00
parent 1417f8fc2d
commit caa6736330
4 changed files with 90 additions and 27 deletions

View File

@ -39,6 +39,8 @@
v-on:video-change-url="changeVideoUrl" v-on:video-change-url="changeVideoUrl"
@switch-to-document="switchToDocument"
v-on:assignment-change-title="changeAssignmentTitle" v-on:assignment-change-title="changeAssignmentTitle"
v-on:assignment-change-assignment="changeAssignmentAssignment" v-on:assignment-change-assignment="changeAssignmentAssignment"
> >
@ -200,10 +202,10 @@
this.localContentBlock.title = title; this.localContentBlock.title = title;
this.error = false; this.error = false;
}, },
changeType(index, type) { changeType(index, type, value) {
let el = { let el = {
type: type, type: type,
value: {} value: Object.assign({}, value)
}; };
switch (type) { switch (type) {
case 'text_block': case 'text_block':
@ -234,9 +236,9 @@
case 'document_block': case 'document_block':
el = { el = {
...el, ...el,
value: { value: Object.assign({
url: '' url: ''
} }, value)
}; };
break; break;
case 'image_url_block': case 'image_url_block':
@ -262,6 +264,9 @@
}, },
setContentBlockType(checked, localContentBlock) { setContentBlockType(checked, localContentBlock) {
this.localContentBlock.isAssignment = checked; this.localContentBlock.isAssignment = checked;
},
switchToDocument(index, value) {
this.changeType(index, 'document_block', value)
} }
} }
} }

View File

@ -2,7 +2,7 @@
<div class="document-form" ref="documentform"> <div class="document-form" ref="documentform">
<div v-if="!value.url" ref="uploadcare-panel"></div> <div v-if="!value.url" ref="uploadcare-panel"></div>
<div v-if="value.url"> <div v-if="value.url">
<a :href="previewUrl" target="_blank">{{previewUrl}}</a> <a :href="previewUrl" class="document-form__link" target="_blank">{{previewLink}}</a>
</div> </div>
</div> </div>
</template> </template>
@ -18,12 +18,20 @@
this.$emit('link-change-url', url, this.index) this.$emit('link-change-url', url, this.index)
}); });
}, },
computed: { computed: {
previewUrl: function () { previewUrl() {
if (this.value && this.value.url) { if (this.value && this.value.url) {
return this.value.url; return this.value.url;
} }
return null; return null;
},
previewLink() {
if (this.value && this.value.url) {
const parts = this.value.url.split('/');
return parts[parts.length - 1]
}
return '';
} }
} }
} }
@ -32,7 +40,12 @@
<style scoped lang="scss"> <style scoped lang="scss">
@import "@/styles/_variables.scss"; @import "@/styles/_variables.scss";
.image-form { .document-form {
&__link {
text-decoration: underline;
}
&__file-input { &__file-input {
width: 0.1px; width: 0.1px;
height: 0.1px; height: 0.1px;

View File

@ -1,8 +1,13 @@
<template> <template>
<div class="image-form"> <div class="image-form">
<div v-if="hadError" class="image-form__error">
Ups, das scheint kein Bild zu sein. Bitte versuche es nochmal mit einer anderen Datei, oder lade die Datei als <a
class="image-form__link" @click="switchToDocument">Dokument</a> hoch.
</div>
<div v-if="!value.url" ref="uploadcare-panel"></div> <div v-if="!value.url" ref="uploadcare-panel"></div>
<div v-if="value.url"> <div v-if="value.url && !hadError">
<img :src="previewUrl"> <img :src="previewUrl" @error="error">
</div> </div>
</div> </div>
</template> </template>
@ -11,40 +16,81 @@
import uploadcare from 'uploadcare-widget'; import uploadcare from 'uploadcare-widget';
export default { export default {
props: ['value', 'index'], props: ['value', 'index'],
mounted() { mounted() {
const uploadcarePanel = uploadcare.openPanel(this.$refs['uploadcare-panel'], null, { this.mountUploadcare();
tabs: ['file'], },
}); data() {
return {
uploadcarePanel.done(panelResult => { hadError: false,
panelResult.done(fileInfo => { uploadcarePanel: null,
this.$emit('link-change-url', fileInfo.cdnUrl, this.index) url: '',
}); filename: ''
}
panelResult.progress(p => {
});
panelResult.fail(uploadResult => {
});
});
}, },
computed: { computed: {
previewUrl: function() { previewUrl: function () {
if (this.value && this.value.url) { if (this.value && this.value.url) {
return this.value.url + '-/preview/200x200/'; return this.value.url + '-/preview/200x200/';
} }
return null; return null;
} }
},
methods: {
error(e) {
this.hadError = true;
delete this.value.url;
setTimeout(() => {
this.mountUploadcare();
}, 0);
},
mountUploadcare() {
this.uploadcarePanel = uploadcare.openPanel(this.$refs['uploadcare-panel'], null, {
tabs: ['file'],
});
this.uploadcarePanel.done(panelResult => {
panelResult.done(fileInfo => {
this.hadError = false;
this.url = fileInfo.cdnUrl;
this.filename = fileInfo.name;
this.$emit('link-change-url', fileInfo.cdnUrl, this.index)
});
panelResult.progress(p => {
});
panelResult.fail(uploadResult => {
});
});
},
switchToDocument() {
this.$emit('switch-to-document', this.index, {
url: `${this.url}${this.filename}`
});
}
} }
} }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
@import "@/styles/_variables.scss"; @import "@/styles/_variables.scss";
@import "@/styles/_mixins.scss";
.image-form { .image-form {
&__error {
@include regular-text;
margin-bottom: $medium-spacing;
line-height: 1.5;
}
&__link {
text-decoration: underline;
@include regular-text;
cursor: pointer;
}
&__file-input { &__file-input {
width: 0.1px; width: 0.1px;
height: 0.1px; height: 0.1px;

View File

@ -96,7 +96,6 @@
if (module) { if (module) {
const data = store.readQuery({query: ME_QUERY}); const data = store.readQuery({query: ME_QUERY});
if (data) { if (data) {
console.log(data);
data.me.lastModule = module; data.me.lastModule = module;
store.writeQuery({query: ME_QUERY, data}); store.writeQuery({query: ME_QUERY, data});
} }