Vue.js 2.x rich text editor component
Important: This guide is about the CKEditor 5 integration with Vue.js 2.x. To learn more about the integration with Vue.js 3+, check out the “Rich text editor component for Vue.js 3+” guide.
CKEditor 5 consists of the ready-to-use editor builds and the CKEditor 5 Framework upon which the builds are based.
The easiest way to use CKEditor 5 in your Vue.js application is by choosing one of the rich text editor builds and simply passing it to the configuration of the Vue.js component. Read more about this solution in the Quick start section.
Additionally, you can integrate CKEditor 5 from source which is a much more flexible and powerful solution, but requires some additional configuration.
The watchdog feature is available for the React and Angular integrations, but is not supported in Vue yet.
# Quick start
Install the CKEditor 5 WYSIWYG editor component for Vue.js and the editor build of your choice.
Assuming that you picked @ckeditor/ckeditor5-build-classic
:
You now need to enable the CKEditor component in your application. There are 2 ways to do so:
Optionally, you can use the component locally.
# Direct script include
This is the quickest way to start using CKEditor in your project. Assuming Vue is installed, include the <script>
tags for the WYSIWYG editor component and the build:
Enable the component in your application by using the Vue.use()
method:
Instead of calling Vue.use()
, you can always use the component locally.
Use the <ckeditor>
component in your template:
- The
editor
directive specifies the editor build. - The
v-model
directive enables an out–of–the–box two–way data binding. - The
config
directive helps you pass the configuration to the editor instance.
Voilà! You should see CKEditor 5 running in your Vue.js app.
See the list of supported directives and events that will help you configure the component.
# Using ES6 modules
The editor component comes as a UMD module, which makes it possible to use in various environments, for instance, applications generated by Vue CLI 3, built using webpack, etc.
To create an editor instance, you must first import the editor build and the component modules into the root file of your application (e.g. main.js
when generated by Vue CLI). Then, enable the component using the Vue.use()
method:
Instead of calling Vue.use()
, you can always use the component locally.
The following example showcases a single–file component of the application. Use the <ckeditor>
component in your template:
- The
editor
directive specifies the editor build (the editor constructor). - The
v-model
directive enables an out–of–the–box two–way data binding. - The
config
directive helps you pass the configuration to the editor instance.
See the list of supported directives and events that will help you configure the component.
# Using the component locally
If you do not want the CKEditor component to be enabled globally, you can skip the Vue.use( CKEditor )
part entirely. Instead, configure it in the components
property of your view.
Make sure CKEditor
and ClassicEditor
are accessible depending on the integration scenario: as direct script includes or ES6 module imports.
# Integrating a custom build from the online builder
This guide assumes that you have created a zip archive with the editor built using the CKEditor 5 online builder.
Unpack it into you application main directory. The directory with the editor build cannot be placed inside the src/
directory as Node will return an error. Because of that, we recommend placing the directory next to the src/
and node_modules/
folders:
Then, add the package located in the ckeditor5
directory as a dependency of your project:
Finally, import the build in your application:
# Using CKEditor from source
Integrating the rich text editor from source allows you to use the full power of CKEditor 5 Framework.
This guide assumes that you are using Vue CLI 3+ as your boilerplate and your application has been created using the vue create
command.
Learn more about building CKEditor from source in the Integrating the editor from the source guide.
# Configuring vue.config.js
To build CKEditor with your application, certain changes must be made to the default project configuration.
First, install the necessary dependencies:
Edit the vue.config.js
file and use the following configuration. If the file is not present, create it in the root of the application (i.e. next to package.json
):
By default, the Vue CLI uses file-loader
for all SVG files. The file-loader
copies the file to the output directory and resolves imports into URLs. The CKEditor’s UI components use SVG source directly so the theme icons must be loaded using raw-loader
. If your project uses different approach then CKEditor’s UI library you must create different webpack loader rules for your project SVG files and CKEditor’s ones.
# Using the editor from source
Having configured vue.config.js
, you can choose the building blocks of your editor. Install the packages necessary for your integration, but please remember that all packages (excluding @ckeditor/ckeditor5-dev-*
and @ckeditor/ckeditor5-vue2
) must have the same version as the base editor package.
You can use more packages, depending on which features are needed in your application.
Instead of calling Vue.use()
, you can always use the component locally.
Now all you need to do is specify the list of rich text editor options (including plugins) in the editorConfig
data property:
# Using the Document editor build
If you use the Document editor in your application, you need to manually add the editor toolbar to the DOM.
Since accessing the editor toolbar is not possible until after the editor instance is ready, put your toolbar insertion code in a method executed upon the ready
event of the component, like in the following example:
# Localization
CKEditor 5 supports multiple UI languages, and so does the official Vue.js component. Follow the instructions below to translate CKEditor 5 in your Vue.js application.
# Predefined builds
When using one of the official editor builds, you need to import the translations first.
- When using a direct script include:
- When using ES6 modules:
Then, configure the language of the editor in the component:
For more information, please refer to the Setting the UI language guide.
# CKEditor 5 built from source
Using the editor built from source requires you to modify the webpack configuration. Pass the language
(also additionalLanguages
) to the constructor of CKEditorWebpackPlugin
to localize your editor:
After building the application, CKEditor 5 will run with the UI translated to the specified language.
For more information, please refer to the “Setting UI language” guide.
# Component directives
# editor
This directive specifies the editor to be used by the component. It must directly reference the editor constructor to be used in the template.
To use more than one rich text editor build in your application, you will need to configure it from source or use a “super build”.
# tag-name
By default, the editor component creates a <div>
container which is used as an element passed to the editor (e.g. ClassicEditor#element
). The element can be configured, so for example to create a <textarea>
, use the following directive:
# v-model
A standard directive for form inputs in Vue. Unlike value
, it creates a two–way data binding, which:
- sets the initial editor content,
- automatically updates the state of the application as the editor content changes (e.g. as the user types),
- can be used to set the editor content when necessary.
In the above example, the editorData
property will be updated automatically as the user types and changes the content. It can also be used to change (as in emptyEditor()
) or set the initial content of the editor.
If you only want to execute an action when the editor data changes, use the input
event.
# value
Allows a one–way data binding that sets the content of the editor. Unlike v-model
, the value will not be updated when the content of the editor changes.
To execute an action when the editor data changes, use the input
event.
# config
Specifies the configuration of the editor.
# disabled
This directive controls the isReadOnly
property of the editor.
It sets the initial read–only state of the editor and changes it during its lifecycle.
# Component events
# ready
Corresponds to the ready
editor event.
# focus
Corresponds to the focus
editor event.
# blur
Corresponds to the blur
editor event.
# input
Corresponds to the change:data
editor event. See the v-model
directive to learn more.
# destroy
Corresponds to the destroy
editor event.
Note: Because the destruction of the editor is promise–driven, this event can be fired before the actual promise resolves.
# Contributing and reporting issues
The source code of this component is available on GitHub in https://github.com/ckeditor/ckeditor5-vue2.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.