14-day Cloud trial
Start today. For free.

One editor. 50+ features. Zero constraints. After your trial, retain the advanced features.

Try Professional Plan for FREE
PricingContact Us
Log InGet Started Free

How to enrich a Vue textarea with TinyMCE

August 23rd, 2023

5 min read

The Vue logo displayed on a large background inside a text editor with TinyMCE

Written by

Ben Long

Category

How-to Use TinyMCE

Vue is a front end development framework with a low barrier to entry. It also integrates easily with other libraries and existing projects, which are some of the reasons why so many developers select the Vue framework. 

When your website requires an enriched Vue textarea, you can easily get bogged down trying to find the right library or project that can quickly integrate with Vue. When you spend a lot of time researching the best ways to get an enriched Vue textarea, or WYSIWYG, you risk pushing out project timelines. 

Deadlines don’t wait, so to make sure you have what you need to get started straight away, we’ve put together this article.

TinyMCE provides fast integration and flexibility, and there’s a specific integration of its WYSIWYG editor for the Vue framework. This article helps you integrate the TinyMCE WYSIWYG editor into your Vue project, and create an enriched Vue textarea for your end users.

Let’s get started…

NOTE: Just in case you don’t already know how to get started with a Vue project, we’ll start there. But you can skip ahead to the part about adding TinyMCE if you’re already well acquainted with Vue.

Start with a simple Vue project

The Vue CLI (command line interface) provides the ability to scaffold new projects and prototype new ideas. We’ll use it to set up a basic project to which we’ll add TinyMCE.

If you haven’t already, install node.js as well on your development workstation.

  1. Install the Vue CLI

npm install -g @vue/cli
  1. Use the Vue create command for a basic Vue project.

vue create tinymce-vue-demo
  1. Press Enter when prompted (shown below), to select the default preset.

The results of running Vue cli commands

  1. Choose your preferred package manager from Yarn or NPM.

Vue will then create the project, and install any CLI plugins.

Test your Vue project

  1. Change into your new project directory the newly created directory:

cd tinymce-vue-demo
  1. Test the project with the following npm command. This will build the project in development mode:

npm run serve
  1. Open http://localhost:8080/ in your browser:

The demo Vue app open in the browser

How to set up a an enriched Vue textarea

Now that you’ve got a simple Vue project running, it’s time to enrich the Vue textarea with TinyMCE.

The official TinyMCE Vue package provides a thin wrapper around TinyMCE to make the editor easier to add to your Vue applications.

  1. Install the TinyMCE vue integration to enrich the Vue textarea. This example uses npm, and is set for For Vue.js 3.x users:

npm install --save "@tinymce/tinymce-vue@^5"
  1. From within your project directory, open the src/App.vue file

  2. Overwrite the default <template> and <script> content with the following TinyMCE template to enrich the Vue demo app with TinyMCE (the <style> content is not changed in this demo):

<script setup>

import Editor from '@tinymce/tinymce-vue'

</script>

<template>

TinyMCE running in a Vue app in the browser

Vue JS textarea input binding

To try out input binding with an enriched Vue textarea, navigate to the TinyMCE Vue integration repository on GitHub. There are several demos available for you to try out, including a controlled component.

NOTE: You can try out the enriched Vue demos by cloning the repository, and then running yarn install followed by yarn run within the cloned folder. Check on the TinyMCE Vue integration documentation for more information.

The demo Controlled.vue file shows how an input binding is possible:

  1. Configure two editor components
  2. Link the contents of one of the editors to the other with a ref value.

For more on input binding, and watching for changes in your enriched Vue textarea, a library that can support you is Vuex – it’s designed for making sure states can only mutate in a predictable fashion.

Another solution, noted in this Stack Overflow discussion, is that using an event handler, any changes in the WYSIWYG are monitored, and when the model changes, the imputed change is caught and registered.

How to limit characters in Vue textarea

Limiting characters is possible using TinyMCE with some additional configuration to your TinyMCE init script. As an example, the following demo modifies the Refreshable.vue file from the Vue integration demo collection:

  1. Clone the tinymce/tinymce-vue GitHub repository:

git clone https://github.com/tinymce/tinymce-vue.git
  1. Change into the tinymce-vue repository, and install the required packages using yarn:

yarn install
  1. Create a branch to preserve the original demo by running the following git command:

git checkout -b <change-to-your-branchname>
  1. Open the Refreshable.vue file in your rich text editor of choice.

  2. Locate the setup TinyMCE option within the return value of the Editor component set up in the definedComponent section.

  3. Change the setup content to limit the characters in the Vue textarea with the following example:

setup: (editor: any) => {
  editor.on("keypress", () => {
    const max = 2;
    const numChars = tinymce.activeEditor.plugins.wordcount.body.getCharacterCount();
    if (numChars > max) {
      alert("Only maximum " + max + " characters allowed.");
    }
  });
};
  1. Start the demo server by using the yarn command:

yarn demo
  1. Open the local host address in your browser, and navigate to the “Rerender” page, which is now replaced with a Vue textarea character limited demo:

TinyMCE demo running in the browser

The following article explains more on how to set up character limits.

How to set up automatic resizing of textarea in Vue

You can set TinyMCE to automatically adjust its size when content is entered, using the Autoresize plugin. Configure the plugin in your TinyMCE plugins list, and add some of the other related options, such as setting a maximum height and width for the Vue textarea. The documentation for the Autoresize plugin shows the configuration and options.

How to add autocomplete on inputs to the textbox

Using the Autocomplete API with TinyMCE, it’s possible to include autocomplete functionality within your enriched Vue textarea. Configure the API method to change the direction of the autocomplete menu, or include icons if needed – the following article explains how you can configure autocomplete for inputs in the textarea.

What to look at next for your Vue app

For more information, you can put a Vue application built for production to the test with a how-to guide written by Marty Friedel, one of the devs in the TinyMCE community. Also, refer to the documentation for comprehensive information about the configuration options available.

Interested in how to get rich text editing opportunities for the textarea of a different framework? We have guides to help you with this: learn how to configure and work with enriched

Contact us now for more information about our WYSIWYG HTML editor and how it’s helping other developers with their apps.

Get your FREE TinyMCE API key today for a 14 day free trial of TinyMCE's premium plugins!

VueTinyMCEConfigurationJavascript
byBen Long

Computer scientist, storyteller, teacher, and an advocate of TinyMCE. Reminisces about programming on the MicroBee. Writes picture books for kids. Also the wearer of rad shoes. “Science isn’t finished until you share the story.”

Related Articles

  • How-to Use TinyMCEApr 16th, 2024

    How to enable a Bootstrap WYSIWYG editor: a step-by-step guide

Join 100,000+ developers who get regular tips & updates from the Tiny team.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Tiny logo

Stay Connected

SOC2 compliance badge

Products

TinyMCEDriveMoxieManager
© Copyright 2024 Tiny Technologies Inc.

TinyMCE® and Tiny® are registered trademarks of Tiny Technologies, Inc.