Introduce tinymce rich text editor in Vue project

The rich text editor originally used in the project is wangEditor , which is a very lightweight and concise editor

But the company's business upgrade, I want a more comprehensive editor, I have been looking for a long time, the common editors are these:

UEditor : Baidu front-end open source project, powerful, based on jQuery, but no longer maintained, and limited back-end code, it is more difficult to modify

bootstrap-wysiwyg : Tiny, easy to use, small and beautiful, just Bootstrap + jQuery...

kindEditor : powerful functions, concise code, need to configure the background, and it has not been updated for a long time

wangEditor : Lightweight, concise, and easy to use, but after upgrading to 3.x, it is not easy to customize development. But the author is very diligent, and I am a family in a broad sense, make a call

quill : It does not have many functions, but it can be extended by itself. The api is also very easy to understand. If you can understand English...

summernote : I haven't studied it in depth, the UI is pretty, and it's a small and beautiful editor, but I need a big one

 

With such a reference, I finally chose tinymce , an editor that can't even open the official website without a ladder (it's just asking for trouble), mainly because of two points:

1. There are many stars on GitHub, and the functions are also complete;

2. The only editor that can keep most of the formatting after pasting from word;

3. There is no need to find back-end personnel to scan the code to change the interface, and the front-end and back-end are separated;

4. Two good points!

 

1. Resource download

tinymce officially provides a component tinymce-vue for the vue project

npm install @tinymce/tinymce-vue -S

Running this code in the terminal of vscode and webstorm may report an error, it is best to use the command line tool that comes with the operating system

If you have the service of purchasing tinymce, you can refer to the instructions of tinymce-vue and use tinymce directly through api-key

If you don't buy it like me, you still have to download tinymce honestly

npm install tinymce -S

After installation, find the tinymce/skins directory in node_modules, and then copy the skins directory to the static directory

// If it is a typescript project built with vue-cli 3.x, put it in the public directory, and all the static directory related in this article will be handled in this way

tinymce has an English interface by default, so you also need to download a Chinese language pack (remember to take the ladder! Take the ladder! Take the ladder!)

Then put this language package in the static directory. For the sake of clarity, I packaged a tinymce directory

 

 2. Initialization

Include the following files in the page

import tinymce from 'tinymce/tinymce'
import 'tinymce/themes/modern/theme'
import Editor from '@tinymce/tinymce-vue'

tinymce-vue is a component that needs to be registered in components and then used directly

The init here is the tinymce initialization configuration item. Some key APIs will be discussed later. For the complete API, please refer to the official documentation .

The editor needs a skin to work properly, so set a skin_url to point to the previously copied skin file

init: {
  language_url: '/static/tinymce/zh_CN.js',
  language: 'zh_CN',
  skin_url: '/static/tinymce/skins/lightgray',
  height: 300
}

// For the typescript project created by vue-cli 3.x, remove the static in the url, that is, skin_url: '/tinymce/skins/lightgray'

At the same time, it also needs to be initialized once in mounted:

If the above init object is passed in here, it will not take effect, but an error will be reported if no parameters are passed, so an empty object is passed in here

 

 3. Extension plug-ins

After completing the above initialization, the editor can be run normally, but only some basic functions

tinymce adds functionality by adding plugins

For example, to add a function of uploading pictures, you need to use the image plugin, and adding hyperlinks requires the link plugin

At the same time, you also need to introduce these plugins on the page:

After the plugin is added, the corresponding function button will be added to the toolbar by default, and the toolbar can also be customized

 

 

Paste the complete component code:

<template>
  <div class='tinymce'>
    <h1>tinymce</h1>
    <editor id='tinymce' v-model='tinymceHtml' :init='init'></editor>
    <div v-html='tinymceHtml'></div>
  </div>
</template>

<script>
import tinymce from 'tinymce/tinymce'
import 'tinymce/themes/modern/theme'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/plugins/image'
import 'tinymce/plugins/link'
import 'tinymce/plugins/code'
import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/wordcount'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'
export default {
  name: 'tinymce',
  data () {
    return {
      tinymceHtml: ' Please enter the content ' ,
      init: {
        language_url: '/static/tinymce/zh_CN.js',
        language: 'zh_CN',
        skin_url: '/static/tinymce/skins/lightgray',
        height: 300,
        plugins: 'link lists image code table colorpicker textcolor wordcount contextmenu',
        toolbar:
          'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat',
        branding: false
      }
    }
  },
  mounted () {
    tinymce.init({})
  },
  components: {Editor}
}
</script>

 

Fourth, upload pictures

tinymce provides apis such as images_upload_url to allow users to configure relevant parameters for uploading images

But in order to adapt to your own project without bothering the backend, you still have to use images_upload_handler to customize an upload method

This method will provide three parameters: blobInfo, success, failure

where blobinfo is an object containing information about the uploaded file:

Success and failure are functions. When the upload is successful, an image address is passed to success , and an error message is passed to failure when it fails.

 

Post my own upload method, using axios to send the request

handleImgUpload (blobInfo, success, failure) {
  let formdata = new FormData()
  formdata.set('upload_file', blobInfo.blob())
  axios.post('/api/upload', formdata).then(res => {
    success(res.data.data.src)
  }).catch(res => {
    failure('error')
  })
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325219033&siteId=291194637