Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE and Vuejs as a component

I am trying to make a Vue Component for TinyMCE but I am facing some problems that I can not solve! Can anybody help me? Or maybe advise a better way to walk?

There is my Component

import Vue from 'vue'
import _ from 'lodash'

export
default {

  props: {
    model: {
      default () {
        return null
      }
    },
    showLeadInfo: {
      default () {
        return false
      }
    }
  },

  data() {
    return {
      id: 'editor_' + _.random(10000, 99999)
    }
  },

  watch: {
    model() {
      if (this.model == null)
        tinyMCE.activeEditor.setContent('');
    }
  },

  ready() {
    var vm = this;
    tinyMCE.baseURL = "/vendor/tinymce/";
    tinymce.init({
      selector: "#" + vm.id,
      theme: "modern",
      height: 200,
      plugins: [
        "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
        "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
        "save table contextmenu directionality emoticons template paste textcolor"
      ],
      toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons",
      style_formats: [{
        title: 'Bold text',
        inline: 'b'
      }, {
        title: 'Red text',
        inline: 'span',
        styles: {
          color: '#ff0000'
        }
      }, {
        title: 'Red header',
        block: 'h1',
        styles: {
          color: '#ff0000'
        }
      }, {
        title: 'Example 1',
        inline: 'span',
        classes: 'example1'
      }, {
        title: 'Example 2',
        inline: 'span',
        classes: 'example2'
      }, {
        title: 'Table styles'
      }, {
        title: 'Table row 1',
        selector: 'tr',
        classes: 'tablerow1'
      }],
      setup: function(editor) {
        editor.on('keyup', function(e) {
          vm.model = editor.getContent();
        });
      }
    });

  },

  events: {
    updateTinyValue(value) {
      tinyMCE.activeEditor.setContent(value);
    }
  }

}

HTML

<textarea :id="id" v-model="model" v-el:editor></textarea>

PS: It is made up with Vueify so there is a template and a script tag wrapping that code.

My biggest problem is when I try to instantiate multiple editors I cant clear the right component because of this code tinyMCE.activeEditor.setContent(value)... I've have tried tinyMCE.get('#' + this.id).setContent() but it does not work!

Somebody have any clue?

Other thing is about ja TinyMCE Plugins... I am using Bower + Gulp to manage my assets! I would like to put all the plugins on my gulpfile (I am using Laravel 5)... Right now the TinyMCE plugins are been loaded one by one and it takes a lot of time!

Thanks in advance!

like image 939
Gustavo Bissolli Avatar asked May 03 '16 17:05

Gustavo Bissolli


People also ask

Can I use TinyMCE without API key?

You can use TinyMCE without an API key. However there will be warning messages in the text editor area.

Is TinyMCE free for commercial use?

Is TinyMCE free? Yes. The TinyMCE core editor is free to use for commercial and noncommercial purposes.

Is TinyMCE an API?

The TinyMCE API is available publicly (versus privately, which is beyond the scope of this post) and you can use it to form functions that help customers and clients.


1 Answers

Instead of using activeEditor you can use getInstanceById:

tinyMCE.getInstanceById(this.id).setContent(value);

Looking at the docs that might be the old version of tinyMCE, might also try:

tinymce.editors[this.id].setContent(value);

Also check out this answer, which uses a Vue directive to manage this automatically: VueJS and tinyMCE, custom directives . This allows you to make a tinyMCE editor simply with <textarea v-editor :body="body"></textarea>. You'll need to adapt it a but but directives are the way to go on this.

Another example directive: https://jsfiddle.net/edwindeveloper90/edjc82z0/

like image 199
Jeff Avatar answered Oct 10 '22 22:10

Jeff