6

我正在尝试为 TinyMCE 制作一个 Vue 组件,但我遇到了一些我无法解决的问题!有谁能够帮我?或者也许建议一种更好的步行方式

有我的组件

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:它是由 Vueify 组成的,因此有一个模板和一个包装该代码的脚本标签。

我最大的问题是当我尝试实例化多个编辑器时,由于这段代码,我无法清除正确的组件tinyMCE.activeEditor.setContent(value)......我已经尝试过tinyMCE.get('#' + this.id).setContent(),但它不起作用!

有人有任何线索吗?

另一件事是关于 ja TinyMCE 插件......我正在使用 Bower + Gulp 来管理我的资产!我想把所有的插件都放在我的 gulpfile 中(我使用的是 Laravel 5)...... 现在 TinyMCE 插件已经被一个一个加载了,这需要很多时间!

提前致谢!

4

2 回答 2

6

而不是使用activeEditor你可以使用getInstanceById

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

查看可能是 tinyMCE 旧版本的文档,也可以尝试:

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

另请查看此答案,它使用 Vue 指令自动管理此问题:VueJS 和 tinyMCE,自定义指令。这使您可以简单地使用<textarea v-editor :body="body"></textarea>. 您需要对其进行调整,但指令是解决此问题的方法。

另一个示例指令:https ://jsfiddle.net/edwindeveloper90/edjc82z0/

于 2016-05-03T20:52:28.840 回答
2

您可以使用vue-easy-tinymce作为组件,这是一个简单而强大的包,可以在Vue.js项目中轻松使用tinymce 。


或者简单地说:

var yourComponent = {

    props: {
        id: {type: String, default: 'editor'},
        value: {default: ''}
    },

    data: function () {
        return {
            objTinymce: null
        }
    },

    template: '<textarea :id="id" :value="value"></textarea>',

    mounted: function () {

        var component = this;
        var initialOptions = {
            target: this.$el,
            init_instance_callback: function (editor) {
                editor.on('Change KeyUp Undo Redo', function (e) {
                    component.value = editor.getContent();
                });
                component.objTinymce = editor;
            }
        };
        tinymce.init(initialOptions);
    },

    watch: {
        value: function (newValue, oldValue) {
            var component = this;
            if (component.value !== this.objTinymce.getContent())
                this.objTinymce.setContent(component.value);
            else
                component.$emit('input', newValue);
        }
    }

};
于 2018-01-19T16:09:06.913 回答