-
-
Save tbreuss/c3765c7e5c824da37356c617830402bf to your computer and use it in GitHub Desktop.
Markdown Text Area Upload VueJS 2 Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<textarea | |
:id="id" | |
:name="name" | |
:value="value" | |
:placeholder="placeholder" | |
:rows="rows" | |
:cols="cols" | |
class="form-control" | |
@dragover.prevent | |
@dragenter.prevent | |
@drop.prevent="drop" | |
@input="update" | |
></textarea> | |
</template> | |
<script> | |
export default { | |
props: { | |
id: String, | |
name: String, | |
value: String, | |
placeholder: String, | |
rows: { | |
type: [Number, String], | |
default: 10, | |
}, | |
cols: { | |
type: [Number, String], | |
default: 30, | |
}, | |
url: String, | |
}, | |
methods: { | |
drop(event) { | |
let file = event.dataTransfer.files[0]; | |
let placeholder = '![Uploading ' + file.name + ']\n'; | |
event.target.value = event.target.value + placeholder; | |
let form = new FormData(); | |
form.append('file', file); | |
axios.post(this.url, form).then(({data}) => { | |
event.target.value = event.target.value.replace(placeholder, `\n`); | |
}).catch(() => { | |
event.target.value = event.target.value.replace(placeholder, '![Failed to upload ' + file.name + ']\n'); | |
}); | |
}, | |
update(event) { | |
this.$emit('input', event.target.value); | |
}, | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment