Search code examples
vue.jsmedium-editor

Medium Editor not pre-filling value in VueJS directive


I am attempting to integrate the medium editor with a VueJs directive.

It is working well but the initial value prop for the field is not initially populating the medium editor. As soon as I update the text in the medium editor the link is established and the html is populated in the textarea.

Directive:

Vue.directive('wysiwyg', {
  bind () {
    let editor = new MediumEditor(this.el)
  }
})

Element:

<textarea v-wysiwyg name="{{ name }}" class="wysiwyg">{{ value }}</textarea>

It appears that the bind function runs before the value is populated in the textarea.

Anyone had any experience integrating these two?


Solution

  • Yes, a bind on the container will happen before the binding of its contents. You can wait for it by using nextTick.

    Vue.directive('wysiwyg', {
      bind() {
        Vue.nextTick(() => new MediumEditor(this.el));
      }
    });
    
    new Vue({
      el: 'body',
      data: {
        value: 'Initial text'
      }
    });
    <script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/medium-editor/5.22.0/js/medium-editor.min.js"></script>
    <link href="//cdnjs.cloudflare.com/ajax/libs/medium-editor/5.22.0/css/medium-editor.min.css" rel="stylesheet" />
    <textarea v-wysiwyg name="{{ name }}" class="wysiwyg">{{ value }}</textarea>