Use TinyMCE
Feb 22, 2017 · 1 分钟阅读

export default {
  init(options = {}) {
    this.cfgs = $.extend({
      selector: 'textarea',
      menubar: false,
      language: 'zh_CN',
      plugins: 'autosave',
      toolbar: 'alignleft aligncenter alignright myimage mysave',
      height: 360,
      autosave_ask_before_unload: false,
      autosave_retention: '1440m',
      content_css: '/assets/plugins/tinymce/content.css',
      statusbar: false,
      setup: (editor) => {
        this.setup(editor);
      }
    }, options);

    tinymce.init(this.cfgs);
  },

  setup(editor) {
    if (this.cfgs.toolbar.indexOf('myimage') !== -1) {
      this.initModal();
      editor.addButton('myimage', {
        title: '插入图片',
        icon: 'image',
        classes: 'widget btn upload-image',
        onclick() {
          $('#editor-image-modal').modal({
            backdrop: 'static'
          });
        }
      });
    }

    if (this.cfgs.toolbar.indexOf('mysave') !== -1) {
      editor.addButton('mysave', {
        title: '保存草稿',
        icon: 'fa fa-floppy-o',
        classes: 'widget btn save-draft',
        onclick() {
          if ($.trim($(tinymce.activeEditor.getContent()).text()) !== '') {
            editor.plugins.autosave.storeDraft();
            console.log('草稿已保存');
          }
        }
      });
    }
  },

  initModal() {
    const title = '插入图片';
    const accept = '.gif, .jpg, .jpeg, .png';
    const msg = '支持jpg、png和gif格式的图片,最大5MB';
    const tpl = `
            <div id="editor-image-modal" class="modal editor-modal center-container fade">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4>${title}</h4>
                  </div>
                  <div class="modal-body">
                    <div class="btns">
                      <input name="file" type="file" accept="${accept}" multiple />
                      <button type="button" disabled class="btn btn-info btn-upload disabled">开始上传</button>
                    </div>
                    <div class="msg">${msg}</div>
                    <div class="template-upload"></div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" disabled class="btn btn-primary btn-confirm disabled">${title}</button>
                  </div>
                </div>
              </div>
            </div>
          `;

    $(tpl).appendTo('body');
  }
};

see more: tinymce docs

← Previous Post Next Post →

Ryun的博客
与其感慨路难行,不如马上出发。

atom css design git html javascript jekyll laravel life mac mobile optimization sublime tool vscode vue
mac

About

你好,❤朋友

这里是 Ryun 的博客 📝

📝记录了我学习 🔎的过程

作为一名前端攻城狮 🦁

希望能和大家一起 🔎

共同进步 🏃

🦁的特长:

  • HTML5
  • CSS3
  • JavaScript
  • Vue.js
  • 微信小程序
  • 熟练配合 Node.js、PHP 和 Laravel
  • 熟练使用各种开发和设计工具

🦁喜欢 🆒🆒的东西

对提升效率和美感的事物有兴趣 😋

欢迎 👏交流


see this hugo-theme-dream-ink