Lazy-loading TinyMCE in Vue.js
This guide shows how to self-host and lazy-load TinyMCE in a Vue.js application using the TinyMCE Vue.js component.
Prerequisites
This procedure requires:
-
Node.js LTS (recommended).
-
Vue CLI (optional).
Procedure
-
Create a new Vue project named
tinymce-vue-demousing the Create Vue tool.-
From a command line or command prompt, create a Vue.js 3.x project:
tinymce-vue-demo.npm create vue@3 -
If a Vue.js 2.x project is required, instead use:
npm create vue@2 -
Follow the prompts and type
tinymce-vue-demoas the project name.
-
-
Change into the newly created directory.
cd tinymce-vue-demo -
Install the
tinymceandtinymce-vuepackages.-
For Vue.js 3.x users:
npm install tinymce @tinymce/tinymce-vue -
For Vue.js 2.x users:
npm install tinymce @tinymce/tinymce-vue@^3
-
-
Update
App.vuewith the below code snippet. Note:tinymceSrcScriptreferences to the publictinymceusing absolute root path. .For example:<script setup> import Editor from '@tinymce/tinymce-vue'; </script> <template> <main id="sample"> <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" /> <Editor id="<uid>" licenseKey="gpl" // Load tinymce at run-time tinymce-script-src="/tinymce/tinymce.min.js" :init="{ plugins: 'anchor lists advlist autolink charmap code media help', toolbar: 'undo redo | styles | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image', height: 500 }" /> </main> </template> <style scoped> .logo { display: block; margin: 0 auto 2rem; } @media (min-width: 1024px) { #sample { display: flex; flex-direction: column; place-items: center; width: 1000px; } } </style> -
Setup a
postinstallscript to copy TinyMCE topublicdirectory after each packages installation, which makes TinyMCE available to the application as static assets.-
Install a library to help with copying assets.
npm install -D fs-extra -
Add the following snipper to
postinstall.jsfile.import fse from 'fs-extra'; import path from 'path'; const topDir = import.meta.dirname; fse.emptyDirSync(path.join(topDir, 'public', 'tinymce')); fse.copySync(path.join(topDir, 'node_modules', 'tinymce'), path.join(topDir, 'public', 'tinymce'), { overwrite: true }); -
Update
postinstalltarget inpackage.jsonto run thepostinstall.jsscript.// ... other content omitted for brevity ... "scripts": { "postinstall": "node ./postinstall.js" }
-
-
Exclude
public/tinymcefrom Git tracking.# ... other rules omitted for brevity ... /public/tinymce/ -
In this instance, run the
postinstallto copy TinyMCE to thepublicdirectory.npm run postinstall -
To start the development server, run:
npm run dev
Other resources
-
For examples of
tinymce-vuewrapper, see: the tinymce-vue storybook. -
To report an issue, please go to: TinyMCE Vue.js issues.
-
For guides on integrating TinyMCE premium plugins, see: Using premium plugins.
-
For information on customizing:
-
TinyMCE integration, see: Vue.js framework Technical Reference.
-
TinyMCE, see: Basic setup.
-
The Vue.js application, see: Vue.js Documentation.
-