Created
May 12, 2020 13:58
-
-
Save insidert/b15e594975b1ce999139d4cd428522c6 to your computer and use it in GitHub Desktop.
This is an example of Vue component with Quill editor. It can show the editor update the details and read the details from the database with Read-Only option.
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> | |
<section> | |
<div id="editor-container" style="font-family: inherit; font-size: inherit;"> | |
</div> | |
<button class="btn btn-secondary mt-3" v-on:click.prevent="updateSyllabus" v-if="canEdit">Update Syllabus</button> | |
<p v-if="syllabusUpdated" class="mt-3"><i class="fas fa-check text-success"></i> Syllabus updated. Redirecting back to course...</p> | |
</section> | |
</template> | |
<script> | |
export default { | |
data() { | |
return { | |
quill: '', | |
syllabusUpdated: false, | |
} | |
}, | |
props: ["url", "redirectUrl", "canEdit", "syllabus"], | |
mounted() { | |
let options; | |
if (this.canEdit) { | |
options = { | |
placeholder: 'Add detailed syllabus here...', | |
theme: 'snow', | |
modules: { | |
toolbar: [ | |
['bold', 'italic'], | |
[{ list: 'ordered' }, { list: 'bullet' }], | |
['link'], | |
] | |
} | |
}; | |
} else { | |
options = { | |
readOnly: true | |
}; | |
} | |
this.quill = new Quill('#editor-container', options); | |
if (this.syllabus != "") { | |
let editedSyllabus = JSON.parse(this.syllabus); | |
editedSyllabus.ops.forEach(item => { | |
if (typeof item.insert != "string") { | |
item.insert = "\n"; | |
} | |
}); | |
this.quill.setContents(editedSyllabus); | |
} | |
}, | |
methods: { | |
updateSyllabus() { | |
console.log(this.quill.getContents()); | |
axios.post(this.url, { | |
syllabus: this.quill.getContents() | |
}) | |
.then(response => { | |
console.log(response); | |
if (response.data.status == 'ok') { | |
this.syllabusUpdated = true; | |
window.setTimeout(() => location.href = this.redirectUrl, 2000); | |
} | |
}) | |
.catch(errors => { | |
alert('Something went wrong.'); | |
}); | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment