【问题标题】:TinyMCE and Vuejs as a componentTinyMCE 和 Vuejs 作为组件
【发布时间】:2016-08-28 20:56:35
【问题描述】:

我正在尝试为 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 插件已经被一个一个加载了,需要很多时间!

提前致谢!

【问题讨论】:

    标签: javascript tinymce vue.js vue-component


    【解决方案1】:

    您可以使用getInstanceById,而不是使用activeEditor

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

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

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

    还可以查看这个答案,它使用 Vue 指令自动管理它:VueJS and tinyMCE, custom directives。这使您可以简单地使用&lt;textarea v-editor :body="body"&gt;&lt;/textarea&gt; 制作一个 tinyMCE 编辑器。您需要对其进行调整,但指令是解决此问题的方法。

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

    【讨论】:

      【解决方案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);
              }
          }
      
      };
      

      【讨论】:

        猜你喜欢
        • 2021-08-05
        • 2017-10-16
        • 2016-04-04
        • 2018-08-25
        • 2017-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-02
        相关资源
        最近更新 更多