The Equation Editor plugin for CK Editor
fx
` button on the CK Editor toolbar, to launch the CodeCogs Equation Editor.
npm install @codecogs/eqneditor-ckeditor5
Typical CKEditor imports:
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
...
Additional imports for the Equation Editor:
import Eqneditor5 from '@codecogs/eqneditor-ckeditor5/src/eqneditor5';
import Image from '@ckeditor/ckeditor5-image/src/image';
import MediaEmbed from '@ckeditor/ckeditor5-media-embed/src/mediaembed';
CKEditor Instantiation:
ClassicEditor
.create( document.querySelector( '#editor' )) {
plugins: [
Essentials,
Bold,
Italic,
Heading,
/* ... Other required plugins */
Image, /* Need by EqnEditor 5 */
MediaEmbed, /* Need by EqnEditor 5 */
EqnEditor5 /* Core of EqnEditor 5 */
],
toolbar: [
'undo', 'redo',
'|',
'EqnEditor5', /* Added fx button for EqnEditor 5 */
'bold', 'italic',
]
});
It is not possible to integrate the Equation Editor into CKEditor without using NPM. So for more traditional website that do not need or use additional web frameworks or libraries managed by NPM, we build a custom `ckeditor5.js` that contains the plugins required, including the Equation Editor.
Warning:If you using CKEditor from a CDN, as shown in this example below then you need to follow these instructions.
Example of CDN code for CKEditor that must be changed (full code is here):
<script type="importmap">
{
"imports": {
"ckeditor5": "https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.js",
"ckeditor5/": "https://cdn.ckeditor.com/ckeditor5/43.0.0/"
}
}</script>
Follow these steps:
git clone https://github.com/ckeditor/ ckeditor5-build-classic.git
This library was last updated in 2020 so it now requires some changes to work as we need
cd ckeditor5-build-classic
npm install
npm install stylelint@latest postcss-unique-selectors@latest stylehacks@latest sugarss@latest --save-dev
npm install postcss-loader@latest --save-dev
npm audit fix --force
npm install @codecogs/eqneditor-ckeditor5
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
// The editor creator to use.
import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Indent from '@ckeditor/ckeditor5-indent/src/indent';
import IndentBlock from '@ckeditor/ckeditor5-indent/src/indentblock';
import FontSize from '@ckeditor/ckeditor5-font/src/fontsize';
import FontColor from '@ckeditor/ckeditor5-font/src/fontcolor';
import FontBackgroundColor from '@ckeditor/ckeditor5-font/src/fontbackgroundcolor';
import List from '@ckeditor/ckeditor5-list/src/list';
// Following are essential for CodeCogs Equation Editor
import Image from '@ckeditor/ckeditor5-image/src/image';
import MediaEmbed from '@ckeditor/ckeditor5-media-embed/src/mediaembed';
import EqnEditor5 from '@codecogs/eqneditor-ckeditor5/src/eqneditor5';
export default class ClassicEditor extends ClassicEditorBase {}
// Plugins to include in the build.
ClassicEditor.builtinPlugins = [
Essentials,
Bold,
Italic,
Heading,
Indent, IndentBlock,
FontSize, FontColor, FontBackgroundColor,
List,
Image, /* Need by EqnEditor 5 */
MediaEmbed, /* Need by EqnEditor 5 */
EqnEditor5 /* Core of EqnEditor 5 */
];
// Editor configuration.
ClassicEditor.defaultConfig = {
toolbar: {
items: [
'undo', 'redo',
'|',
'EqnEditor5', /* Added fx button for EqnEditor 5 */
'bold', 'italic',
'|',
'numberedList', 'bulletedList', 'outdent', 'indent',
'|',
'heading', 'fontsize', 'fontColor', 'fontBackgroundColor'
]
},
// This value must be kept in sync with the language defined in webpack.config.js.
language: 'en'
};
If you do this right then you should have a smaller CKEditor library that will make your website more responsive.
...
module: {
rules: [
{
test: /\.svg$/,
use: [ 'raw-loader' ]
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
options: {
injectType: 'singletonStyleTag',
attributes: {
'data-cke': true
}
}
},
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: styles.getPostCssConfig({
themeImporter: {
themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
},
minify: true
})
}
}
...
npm run build
cp build/* WEBSITE/public/js/
Example code for a fully working editor:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/js/ckeditor.js"></script>
<link rel="stylesheet" href="https://cdn.ckeditor.com/ckeditor5/43.0.0/ckeditor5.css" />
</head>
<body>
<p>Example CK Editor with Equation Editor:</a></p>
<textarea id="editor5" name="editor5"></textarea>
<script>
ClassicEditor
.create( document.querySelector( '#editor5' ) )
.catch( error => {
console.error( error );
} );
</script>
</body>
</html>
OR change these lines from the CKEditor example (full code is here):
<script type="importmap">
{
"imports": {
"ckeditor5": "/js/ckeditor5.js",
"ckeditor5/": "https://cdn.ckeditor.com/ckeditor5/43.0.0/"
}
}</script>