1. 程式人生 > >vue專案移植tinymce踩坑

vue專案移植tinymce踩坑

前言

最近因業務需求在專案中嵌入了tinymce這個編輯器,用於滿足平臺給使用者編輯各類新聞內容什麼的業務需求,前後也花了不少時間體驗和對比了市面上各類開源編輯器。

各大WYSIWYG編輯器的簡單比較

UEditor: 因為已經不再維護了,需要大量修改原始碼,很多都是專門為jsp等伺服器渲染專案寫的程式碼需要刪除, 然後越刪越害怕越刪越不敢用,依賴jquery,需要專門用js去parse編輯完成的內容,parse完的內容還可能汙染全域性css,相容老瀏覽器還不錯, 但是,我們不怎麼考慮相容IE。所以,告辭。

wangEditor: 中文文件,上手快,依賴jquery,功能少點要花時間去寫外掛,需要單獨為圖片上傳功能寫個介面,老專案忙著上線臨時用過,感覺並不適合當前業務這麼重的編輯功能於是放棄了。

Quill:api友好, 功能少,需要特定的css去解析文字(這點我不大喜歡),ui好看,適合作為論壇回帖功能使用。

CKEditor: CKEditor目前主流的還是4.x的版本,但是文件看著很瞎眼實在是提不起興致去配置,草草用了下就放棄了,5.x版本剛從beta結束,需要指定專門的node以及npm版本,雖然功能強大配置靈活ui漂亮不過目前糟糕的相容性基本是不可能出現在大眾視野了。

KingEditor: 醜,不喜歡,不愛用

Draft-js: 知乎最近剛改的文字編輯器就是在draft的基礎上開發的,依賴react, 棄。

Medium-editor: 雖然看著感覺很酷炫,但是,不適合我們的業務場景啊, api也簡陋可怕。

trix: 嗯,又一個小而美,放棄

Slatereact,放棄

Bootstrap-wysiwygbootstrap, jquery, 放棄

tinymce: 文件好,功能強,bug少,無外部依賴,大家用了都說好,嗯,沒錯就是它了。

編輯器配置方面只要能看得懂英文耍起來還是比較簡單的,適配中碰到的大部分問題都可以通過看文件解決,即便看文件解決不了網上也有大量的文章能告訴你怎麼配置能解決。

當然了,主要是我這裡需要解決一些別人覺得超簡單自己一想都很煩人的需求,比如:

  1. word文件貼上進來要帶格式
  2. 相容移動端
  3. word文件貼上進來要正常顯示並且還要相容移動端
  4. 電腦網頁裡貼上進來內容要正常顯示並且排版還不能亂
  5. 電腦網頁拷過來的內容還要相容到移動端

初始化

因為tinymce的Plugins是按需載入的
為了能先快速上手這個編輯器
就先在vue-cli的index.html中預設塞入一條線上cdn地址

<script src="https://cdn.bootcss.com/tinymce/4.7.4/tinymce.min.js"></script>

記得去下載語言包到本地,
然後就在檔案內引入

import './zh_CN.js'

後面有機會再寫下單獨打包的事項,畢竟這貨體積還不小。

插入vue元件模板

<template>
  <div>
    <textarea :id= "Id"></textarea>
  </div>
</template>

記得一定要在textarea外面包一層div,不然...你自己試試看就知道了。

元件基礎配置

將tinymce通過指定的selector掛載到元件中

<template>
  <div>
    <textarea :id= "Id"></textarea>
  </div>
</template>
<script>
  import './zh_CN.js'
  export default {
    data () {
      const Id = Date.now()
      return {
        Id: Id,
        Editor: null,
        DefaultConfig: {}
      }
    },
    props: {
      value: {
        default: '',
        type: String
      },
      config: {
        type: Object,
        default: () => {
          return {
            theme: 'modern',
            height: 300
          }
        }
      }
    },
    mounted () {
      this.init()
    },
    beforeDestroy () {
      // 銷燬tinymce
      this.$emit('on-destroy')
      window.tinymce.remove(`#${this.Id}`)
    },
    methods: {
       init () {
        const self = this
        this.Editor = window.tinymce.init({
          // 預設配置
          ...this.DefaultConfig,
          
          // prop內傳入的的config
          ...this.config, 
          
          // 掛載的DOM物件
          selector: `#${this.Id}`,
          
          setup: (editor) => {
            // 丟擲 'on-ready' 事件鉤子
            editor.on(
              'init', () => {
                self.loading = false
                self.$emit('on-ready')
                editor.setContent(self.value)
              }
            )
            // 丟擲 'input' 事件鉤子,同步value資料
            editor.on(
              'input change undo redo', () => {
                self.$emit('input', editor.getContent())
              }
            )
          }
        })
      }
    }
  }
</script>

好了,元件基本的初始化完成,後面正式開始踩坑之旅

API

具體內容看官網的API就行,英語不好的用chrome翻譯下對照著demo也能看個七七八八,當然主要原因還是我比較懶。

我這邊根據自身業務需求在元件的data內寫了個預設配置


DefaultConfig: {
  // GLOBAL
  height: 500,
  theme: 'modern',
  menubar: false,
  toolbar: `styleselect | fontselect | formatselect | fontsizeselect | forecolor backcolor | bold italic underline strikethrough | image  media | table | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | preview removeformat  hr | paste code  link | undo redo | fullscreen `,
  plugins: `
    paste
    importcss
    image
    code
    table
    advlist
    fullscreen
    link
    media
    lists
    textcolor
    colorpicker
    hr
    preview
  `,

  
  // CONFIG

  forced_root_block: 'p',
  force_p_newlines: true,
  importcss_append: true,

 // CONFIG: ContentStyle 這塊很重要, 在最後呈現的頁面也要寫入這個基本樣式保證前後一致, `table`和`img`的問題基本就靠這個來填坑了
  content_style: `
    *                         { padding:0; margin:0; }
    html, body                { height:100%; }
    img                       { max-width:100%; display:block;height:auto; }
    a                         { text-decoration: none; }
    iframe                    { width: 100%; }
    p                         { line-height:1.6; margin: 0px; }
    table                     { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
    .mce-object-iframe        { width:100%; box-sizing:border-box; margin:0; padding:0; }
    ul,ol                     { list-style-position:inside; }
  `,

  insert_button_items: 'image link | inserttable',

  // CONFIG: Paste
  paste_retain_style_properties: 'all',
  paste_word_valid_elements: '*[*]',        // word需要它
  paste_data_images: true,                  // 貼上的同時能把內容裡的圖片自動上傳,非常強力的功能
  paste_convert_word_fake_lists: false,     // 插入word文件需要該屬性
  paste_webkit_styles: 'all',
  paste_merge_formats: true,
  nonbreaking_force_tab: false,
  paste_auto_cleanup_on_paste: false,

  // CONFIG: Font
  fontsize_formats: '10px 11px 12px 14px 16px 18px 20px 24px',

  // CONFIG: StyleSelect
  style_formats: [
    {
      title: '首行縮排',
      block: 'p',
      styles: { 'text-indent': '2em' }
    },
    {
      title: '行高',
      items: [
        {title: '1', styles: { 'line-height': '1' }, inline: 'span'},
        {title: '1.5', styles: { 'line-height': '1.5' }, inline: 'span'},
        {title: '2', styles: { 'line-height': '2' }, inline: 'span'},
        {title: '2.5', styles: { 'line-height': '2.5' }, inline: 'span'},
        {title: '3', styles: { 'line-height': '3' }, inline: 'span'}
      ]
    }
  ],

  // FontSelect
  font_formats: `
    微軟雅黑=微軟雅黑;
    宋體=宋體;
    黑體=黑體;
    仿宋=仿宋;
    楷體=楷體;
    隸書=隸書;
    幼圓=幼圓;
    Andale Mono=andale mono,times;
    Arial=arial, helvetica,
    sans-serif;
    Arial Black=arial black, avant garde;
    Book Antiqua=book antiqua,palatino;
    Comic Sans MS=comic sans ms,sans-serif;
    Courier New=courier new,courier;
    Georgia=georgia,palatino;
    Helvetica=helvetica;
    Impact=impact,chicago;
    Symbol=symbol;
    Tahoma=tahoma,arial,helvetica,sans-serif;
    Terminal=terminal,monaco;
    Times New Roman=times new roman,times;
    Trebuchet MS=trebuchet ms,geneva;
    Verdana=verdana,geneva;
    Webdings=webdings;
    Wingdings=wingdings,zapf dingbats`,

  // Tab
  tabfocus_elements: ':prev,:next',
  object_resizing: true,

  // Image
  imagetools_toolbar: 'rotateleft rotateright | flipv fliph | editimage imageoptions'
}

因為本人比較懶,以上配置匯出的程式碼可能會有程式碼注入的風險,建議儲存的時候再前後端都做下注入過濾,不過一般資料安全問題主要還是伺服器那邊的事情?。

後面的圖片上傳可以單獨拆出來做個小配置,直接寫到props裡好了。

  url: {
    default: '',
    type: String
  },
  accept: {
    default: 'image/jpeg, image/png',
    type: String
  },
  maxSize: {
    default: 2097152,
    type: Number
  },
  withCredentials: {
    default: false,
    type: Boolean
  }

然後把這套東西塞到init配置裡


  // 圖片上傳
  images_upload_handler: function (blobInfo, success, failure) {
    if (blobInfo.blob().size > self.maxSize) {
      failure('檔案體積過大')
    }
    
    if (self.accept.indexOf(blobInfo.blob().type) >= 0) {
      uploadPic()
    } else {
      failure('圖片格式錯誤')
    }
    function uploadPic () {
      const xhr = new XMLHttpRequest()
      const formData = new FormData()
      xhr.withCredentials = self.withCredentials
      xhr.open('POST', self.url)
      xhr.onload = function () {

        if (xhr.status !== 200) {
          // 丟擲 'on-upload-fail' 鉤子
          self.$emit('on-upload-fail')
          failure('上傳失敗: ' + xhr.status)
          return
        }

        const json = JSON.parse(xhr.responseText)
        // 丟擲 'on-upload-success' 鉤子
        self.$emit('on-upload-complete' , [
          json, success, failure
        ])
      }
      formData.append('file', blobInfo.blob())
      xhr.send(formData)
    }
  }

至此, 一個元件的封裝基本算是完成了

看下初階成果

<template>
  <div>
    <textarea :id= "Id"></textarea>
  </div>
</template>
<script>
  import './zh_CN.js'
  export default {
    data () {
      const Id = Date.now()
      return {
        Id: Id,
        Editor: null,
        DefaultConfig: {
          // GLOBAL
          height: 500,
          theme: 'modern',
          menubar: false,
          toolbar: `styleselect | fontselect | formatselect | fontsizeselect | forecolor backcolor | bold italic underline strikethrough | image  media | table | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | preview removeformat  hr | paste code  link | undo redo | fullscreen `,
          plugins: `
            paste
            importcss
            image
            code
            table
            advlist
            fullscreen
            link
            media
            lists
            textcolor
            colorpicker
            hr
            preview
          `,

          
          // CONFIG

          forced_root_block: 'p',
          force_p_newlines: true,
          importcss_append: true,

        // CONFIG: ContentStyle 這塊很重要, 在最後呈現的頁面也要寫入這個基本樣式保證前後一致, `table`和`img`的問題基本就靠這個來填坑了
          content_style: `
            *                         { padding:0; margin:0; }
            html, body                { height:100%; }
            img                       { max-width:100%; display:block;height:auto; }
            a                         { text-decoration: none; }
            iframe                    { width: 100%; }
            p                         { line-height:1.6; margin: 0px; }
            table                     { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
            .mce-object-iframe        { width:100%; box-sizing:border-box; margin:0; padding:0; }
            ul,ol                     { list-style-position:inside; }
          `,

          insert_button_items: 'image link | inserttable',

          // CONFIG: Paste
          paste_retain_style_properties: 'all',
          paste_word_valid_elements: '*[*]',        // word需要它
          paste_data_images: true,                  // 貼上的同時能把內容裡的圖片自動上傳,非常強力的功能
          paste_convert_word_fake_lists: false,     // 插入word文件需要該屬性
          paste_webkit_styles: 'all',
          paste_merge_formats: true,
          nonbreaking_force_tab: false,
          paste_auto_cleanup_on_paste: false,

          // CONFIG: Font
          fontsize_formats: '10px 11px 12px 14px 16px 18px 20px 24px',

          // CONFIG: StyleSelect
          style_formats: [
            {
              title: '首行縮排',
              block: 'p',
              styles: { 'text-indent': '2em' }
            },
            {
              title: '行高',
              items: [
                {title: '1', styles: { 'line-height': '1' }, inline: 'span'},
                {title: '1.5', styles: { 'line-height': '1.5' }, inline: 'span'},
                {title: '2', styles: { 'line-height': '2' }, inline: 'span'},
                {title: '2.5', styles: { 'line-height': '2.5' }, inline: 'span'},
                {title: '3', styles: { 'line-height': '3' }, inline: 'span'}
              ]
            }
          ],

          // FontSelect
          font_formats: `
            微軟雅黑=微軟雅黑;
            宋體=宋體;
            黑體=黑體;
            仿宋=仿宋;
            楷體=楷體;
            隸書=隸書;
            幼圓=幼圓;
            Andale Mono=andale mono,times;
            Arial=arial, helvetica,
            sans-serif;
            Arial Black=arial black, avant garde;
            Book Antiqua=book antiqua,palatino;
            Comic Sans MS=comic sans ms,sans-serif;
            Courier New=courier new,courier;
            Georgia=georgia,palatino;
            Helvetica=helvetica;
            Impact=impact,chicago;
            Symbol=symbol;
            Tahoma=tahoma,arial,helvetica,sans-serif;
            Terminal=terminal,monaco;
            Times New Roman=times new roman,times;
            Trebuchet MS=trebuchet ms,geneva;
            Verdana=verdana,geneva;
            Webdings=webdings;
            Wingdings=wingdings,zapf dingbats`,

          // Tab
          tabfocus_elements: ':prev,:next',
          object_resizing: true,

          // Image
          imagetools_toolbar: 'rotateleft rotateright | flipv fliph | editimage imageoptions'
        }
      }
    },
    props: {
      value: {
        default: '',
        type: String
      },
      config: {
        type: Object,
        default: () => {
          return {
            theme: 'modern',
            height: 300
          }
        }
      },
      url: {
        default: '',
        type: String
      },
      accept: {
        default: 'image/jpeg, image/png',
        type: String
      },
      maxSize: {
        default: 2097152,
        type: Number
      },
      withCredentials: {
        default: false,
        type: Boolean
      }
    },
    mounted () {
      this.init()
    },
    beforeDestroy () {
      // 銷燬tinymce
      this.$emit('on-destroy')
      window.tinymce.remove(`$#{this.Id}`)
    },
    methods: {
       init () {
        const self = this
        
        this.Editor = window.tinymce.init({
          // 預設配置
          ...this.DefaultConfig,
          
          // 圖片上傳
          images_upload_handler: function (blobInfo, success, failure) {
            if (blobInfo.blob().size > self.maxSize) {
              failure('檔案體積過大')
            }
            
            if (self.accept.indexOf(blobInfo.blob().type) > 0) {
              uploadPic()
            } else {
              failure('圖片格式錯誤')
            }
            function uploadPic () {
              const xhr = new XMLHttpRequest()
              const formData = new FormData()
              xhr.withCredentials = self.withCredentials
              xhr.open('POST', self.url)
              xhr.onload = function () {

                if (xhr.status !== 200) {
                  // 丟擲 'on-upload-fail' 鉤子
                  self.$emit('on-upload-fail')
                  failure('上傳失敗: ' + xhr.status)
                  return
                }

                const json = JSON.parse(xhr.responseText)
                // 丟擲 'on-upload-complete' 鉤子
                self.$emit('on-upload-complete' , [
                  json, success, failure
                ])
              }
              formData.append('file', blobInfo.blob())
              xhr.send(formData)
            }
          },

          // prop內傳入的的config
          ...this.config, 
          
          // 掛載的DOM物件
          selector: `#${this.Id}`,
          setup: (editor) => {
            // 丟擲 'on-ready' 事件鉤子
            editor.on(
              'init', () => {
                self.loading = false
                self.$emit('on-ready')
                editor.setContent(self.value)
              }
            )
            // 丟擲 'input' 事件鉤子,同步value資料
            editor.on(
              'input change undo redo', () => {
                self.$emit('input', editor.getContent())
              }
            )
          }
        })
      }
    }
  }
</script>

直接引入元件呼叫就行了

<template>
  <mce-editor 
    :config           = "Config"
     v-model          = "Value"
    :url              = "Url"
    :max-size         = "MaxSize"
    :accept           = "Accept"
    :with-credentials = false
    @on-ready         = "onEditorReady"
    @on-destroy       = "onEditorDestroy"
    @on-upload-success= "onEditorUploadComplete"
    @on-upload-fail   = "onEditorUploadFail"
  ></mce-editor>
</template>

但是作為一名優秀的程式設計師,這怎麼可能夠嘛。
下面說下打包的事情

塞入webpack

為了加快頁面載入速度就要首先解決載入檔案過多的問題,而大部分時間使用者並不需要每次開啟頁面都先載入一遍editor的核心檔案,而editor本身也要按需載入內容,一開始想把每個plugin都搞成獨立元件模組按需載入,但是這就要涉及到修改編輯器本身原始碼,或者說對window.tinymce刪掉點特性,這些都太麻煩也都有風險,對後面的程式碼維護影響也大,索性就都先留著。
後面邊做邊改吧

還是以vue-cli為例
把官網下載的包塞到stataic資料夾中
然後刪掉index.html模版中的cdn程式碼吧不需要了
當然這裡有倆選擇
要麼做成一個非同步元件,單獨打包,按需載入
要麼直接引入到main.js中將包打成為一個巨無霸
所以我選擇前者,

首先老規矩 引入編輯器主體

import '../../static/tinymce/tinymce.min.js'

然後重新整理下頁面,不出意外應該是報這麼個錯Uncaught SyntaxError: Unexpected token <
眼尖的朋友應該知道是怎麼回事了theme.js:1
在預設配置下, tinymce載入的theme的路徑居然是這個
Request URL:http://localhost:8080/themes/modern/theme.js
然後我跑去官網搜了下api 只搜到一個叫document_base_url的api,但是根據多年程式設計師的直覺經驗告訴我 不是這貨(嗯,我在這裡卡住了),網上翻了下各地文獻,都沒有啊,
那怎麼辦呢
於是我就跑去看原始碼...但是4萬行...算了...
然後我就在控臺列印了下tinymce物件,然後發現了一個叫baseURLstring物件,嗯,有希望了。
在原始碼裡搜了下baseURL
蹦出來這段程式碼 .... 算了有很多段...
大致思想就是通過當前URI拆出來個baseURL,改掉就行了

window.tinymce.baseURL = '/static/tinymce'

如果需要載入的地址是另一個比如自己公司的cdn的路徑,那改成全路徑就行了

window.tinymce.baseURL = 'http://cdn.xxx.com/static/tinymce'

貌似路徑的問題解決了

但是新的問題又出現了,
外掛下過來都是帶min的,但預設載入的外掛都是不帶min的,一定是我原始碼沒看仔細,
然後我又搜了一下程式碼

if (!baseURL &amp;&amp; document.currentScript) {
  src = document.currentScript.src;
  if (src.indexOf('.min') != -1) {
    suffix = '.min';
  }

  baseURL = src.substring(0, src.lastIndexOf('/'));
}

希望就在眼前,貌似是業務我載入的方式是直接匯入到模組的,於是一個叫suffix的預設值為空了,於是我去又加了行程式碼:

window.tinymce.suffix = '.min'

成功!
你看嘛,超級簡單的是不是,根本不用改原始碼,網上說的動不動就去改原始碼什麼的不要信啊不要信,大部分面向物件的事情改個預設值就行了。

對了,還記得前面的語言包嘛,
下過來塞到/static/tinymce/langs資料夾裡
然後刪掉

import './zh_CN.js'

這行程式碼
DefaultConfig中放入一個新配置項

language: 'zh_CN'

好了,後面就是模組打包的事情了,

打包

前面打的包有一個問題是預設配置是載入tinyMce本體,那麼就會造成這個包大概有500k的體積,如果這個元件不做非同步載入的處理,那麼對於某些業務來說就是災難。雖然這麼做開啟只用載入一個檔案,業務比較穩定。
但我覺得這樣不優雅所以最後還是把它單獨拎出來了。
同理,根據這個庫本身的特性,我們完全可以把這麼多個必須的plugin按需要直接統一打成一個包,直接載入。這樣,我們就又多了一個幾百k的plugins包。
然後把plugins包和tinyMce主體包在不阻塞頁面載入的情況下,做個懶載入提前快取好檔案方便後面使用,而元件本身在掛載前做個監聽window.tinymce全域性變數的方法,然後cdn控制下檔案的過期時間即可。
這樣,在保證了靈活度的前提下也保證了業務載入的速度。

完,感謝閱讀。

原文地址:https://segmentfault.com/a/1190000012791569