Vuejs Tinymce directive
by Isaac Ben
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.7.4/tinymce.min.js"></script>
<script src="https://unpkg.com/vue@2.5.9/dist/vue.js"></script>
<div id="tinymce-form">
<textarea
rows="10"
v-tinymce-editor="content"
v-model="content">
</textarea>
<div v-text="content"></div>
</div>
Babel + JSX
//I'm using target instead of selector to save you from
//having to add a unique ID.
//This would be useful incase you need multiple of
//editors in the same page.
Vue.directive('tinymce-editor', {
bind: function (el, binding, vNode) {
Vue.nextTick(() => {
tinymce.init({
target: el,
height: 250,
setup: function (editor) {
editor.on('init', function () {
tinymce.editors[tinymce.editors.findIndex(i => i['targetElm'] === el)].setContent(el.value);
});
editor.on('keyup', function () {
let newValue = tinymce.editors[tinymce.editors.findIndex(i => i['targetElm'] === el)].getContent();
let event = new Event('input', {bubbles: true});
el.value = newValue;
el.dispatchEvent(event);
});
editor.on('change', function () {
let newValue = tinymce.editors[tinymce.editors.findIndex(i => i['targetElm'] === el)].getContent();
let event = new Event('input', {bubbles: true});
el.value = newValue;
el.dispatchEvent(event);
});
}
});
});
},
unbind (el) {
let event = new Event('input', {bubbles: true});
el.value = '';
el.dispatchEvent(event);
tinymce.editors[tinymce.editors.findIndex(i => i['targetElm'] === el)].remove();
},
});
new Vue({
el: '#tinymce-form',
data: {
content: ''
}
})