Skip to content

Instantly share code, notes, and snippets.

@codebykyle
Created April 16, 2017 18:24
Show Gist options
  • Save codebykyle/4f7f4b935e8119c290f00efc01fb0c2b to your computer and use it in GitHub Desktop.
Save codebykyle/4f7f4b935e8119c290f00efc01fb0c2b to your computer and use it in GitHub Desktop.
Basecamp Trix-Editor VueJS 2.0 v-model 2 way binding
let _ = require('lodash');
Vue.component('wysiwyg', {
props: ['value'],
template: '<div></div>',
data() {
return {
trix: null,
id: ''
}
},
mounted() {
this.id = _.sampleSize('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 5).join('');
this.trix = $(`<trix-editor input="${this.id}"></trix-editor>`);
let self = this;
this.trix.on('trix-change', (e) => {
self.$emit('input', e.currentTarget.innerHTML)
});
this.$watch('value', function(value) {
value = value === undefined ? '' : value;
if (self.trix[0].innerHTML !== value) {
self.trix[0].editor.loadHTML(value);
}
});
this.trix.insertAfter(this.$el);
},
});
//Usage:
<wysiwyg v-model="createBlogForm.title" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment