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

A brief analysis of kubernetes controllers and labels

Table of contents 01 Common controllers in k8s RC...

Thirty HTML coding guidelines for beginners

1. Always close HTML tags In the source code of p...

Vue3.0 project construction and usage process

Table of contents 1. Project construction 2: Dire...

Several ways to manually implement HMR in webpack

Table of contents 1. Introduction 2. GitHub 3. Ba...

Example code for realizing charging effect of B station with css+svg

difficulty Two mask creation of svg graphics Firs...

Summary of essential Docker commands for developers

Table of contents Introduction to Docker Docker e...

Are you still Select *?

There are many reasons why an application is as s...

Tutorial on deploying nginx+uwsgi in Django project under Centos8

1. Virtual environment virtualenv installation 1....

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and...

Workerman writes the example code of mysql connection pool

First of all, you need to understand why you use ...

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...