Created
April 16, 2017 18:24
-
-
Save codebykyle/4f7f4b935e8119c290f00efc01fb0c2b to your computer and use it in GitHub Desktop.
Basecamp Trix-Editor VueJS 2.0 v-model 2 way binding
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
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