Practice of using Tinymce rich text to customize toolbar buttons in Vue

Practice of using Tinymce rich text to customize toolbar buttons in Vue

There are many rich text editors, the popular ones are UEditor, kindeditor, CKEditor and so on. But today we are going to implement the plugin development of tniyMCE.

Install tinymce, tinymce ts, tinymce-vue declaration files

npm install tinymce -S
npm install @types/tinymce -S
npm install @tinymce/tinymce-vue -S

Package components

<template>
    <div>
        <editor :id="id" v-model="content" :init="init"></editor>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';

import tinymce from 'tinymce';
import Editor from '@tinymce/tinymce-vue';

import 'tinymce/themes/silver/theme';
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';
import 'tinymce/plugins/media';
import 'tinymce/plugins/fullscreen';
import 'tinymce/plugins/preview';
import 'tinymce/plugins/pagebreak';
import 'tinymce/plugins/insertdatetime';
import 'tinymce/plugins/hr';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/codesample';
import 'tinymce/icons/default/icons';

console.log(tinymce);

@Component({ name: 'TinymceEditer', components: { Editor } })
export default class extends Vue {
    //Set prop id
    @Prop({ default: 'vue-tinymce-' + +new Date() }) id!: string;
 
 //Default height @Prop({ default: 300 }) height!: number;

    @Prop({ default: '' })
    private value!: string;

    private content: string = '';

    @Watch('value')
    private onChangeValue(newVal: string) {
        this.content = newVal;
    }

    @Watch('content')
    private onChangeContent(newVal: string) {
        this.$emit('input', newVal);
    }

    //Rich text box init configuration private get init() {
        return {
            selector: '#' + this.id, //Rich text editor id
            language: 'zh_CN', //languagelanguage_url: '/tinymce/zh_CN.js', //language packageskin_url: '/tinymce/skins/ui/oxide', //The editor needs a skin to work properly, so set a skin_url to point to the skin file copied beforemenubar: false, //menu barplugins:
                'link lists image code table colorpicker textcolor wordcount contextmenu media table fullscreen preview pagebreak insertdatetime hr paste codesample emoticons', //plugin toolbar:
                : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
            //Instantiate execution init_instance_callback: (editor: any) => {
                this.content && editor.setContent(this.content);

                //this.hasInit = true
                editor.on('NodeChange Change KeyUp SetContent', () => {
                    //this.hasChange = true
                    this.content = editor.getContent();
                });
            },

            //Video settings callback video_template_callback: (data: any) => {
                return `<video width="
                    ${data.width} " height="${data.height}"
                    ${data.poster ? 'poster="' + data.poster + '"' : ''}
                    controls="controls">
                    <source src="${data.source}"/>
                </video>`;
            },

            //Paste image callback images_upload_handler: (blobInfo: any, success: Function, failure: Function) => {
                this.handleImgUpload(blobInfo, success, failure);
            },
        };
    }

    private mounted() {
        this.content = this.value;
    }

    //Upload pictures private handleImgUpload(blobInfo: any, success: Function, failure: Function) {
        this.$emit('upload', blobInfo, success, failure);
    }
}
</script>

<style lang="scss">
.tox-tinymce-aux {
    z-index: 3000 !important;
}
</style>

Component Usage

<template>
   <tinymce v-model="content" />
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Tinymce from '@/components/tinymce/tinymce.vue';

@Component({
    components:
        Tinymce,
    },
})
export default class extends Vue {
    private content: string = '';
}
</script>

<style lang="scss" scoped></style>

Vue uses Tinymce rich text editor to customize toolbar buttons

insert image description here

Here I added a shrink button

 init: {
        language: "zh_CN",
        skin_url: "/tinymce/skins/ui/oxide",
        height: "100%",
        fontsize_formats: "8pt 10pt 12pt 14pt 16pt 18pt 24pt 36pt",
        font_formats:
          "Microsoft YaHei=Microsoft YaHei;Founder FangSong_GBK=Founder FangSong_GBK;Songti=simsun,serif;FangSongti=FangSong,serif;Boldti=SimHei;Times New Roman=Times New Roman;",
        plugins: {
	      type: [String, Array],
	      default: "code lists image media table wordcount indent2em"
	    ,
        toolbar:
	      type: [String, Array],
	      default:
	        "code | lineheight | undo redo | fontsizeselect | fontselect | formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | myCustomToolbarButton | bullist numlist outdent indent indent2em | lists image media table | removeformat"
	    },
        branding: false,
        menubar: false,
        setup: editor => {
          let _this = this;
          editor.ui.registry.addButton("myCustomToolbarButton", {
            text: "Shrink",
            onAction: function() {
              _this.show= !_this.show;
            }
          });
        }
      },

Key Code

Arrow functions are used here => to operate properties and events in Vue

 setup: editor => {
    let _this = this;
    editor.ui.registry.addButton("myCustomToolbarButton", {
      text: "Shrink",
      onAction: function() {
        _this.show= !_this.show;
      }
    });
  }

This is the end of this article about the practice of using Tinymce rich text to customize toolbar buttons in Vue. For more relevant Vue Tinymce custom toolbar content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue uses the rich text editor Vue-Quill-Editor (including custom image upload service, clear copy and paste style, etc.)
  • Vue+Element UI+vue-quill-editor Rich text editor and customized image insertion

<<:  Discussion on the Issues of Image Button Submission and Form Repeated Submission

>>:  Solution to the problem that the vertical centering of flex inside button is not centered

Recommend

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

How to view available network interfaces in Linux

Preface The most common task after we install a L...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two...

Difference between src and href attributes

There is a difference between src and href, and t...

About Nginx gzip configuration

The principle of nginx to achieve resource compre...

JavaScript quickly implements calendar effects

This article example shares the specific code of ...

Detailed explanation of common commands in Docker repository

Log in docker login Complete the registration and...

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...

Javascript implements simple navigation bar

This article shares the specific code of Javascri...

A simple LED digital clock implementation method in CSS3

This should be something that many people have do...