Rich Text Editor - AtlasOfLivingAustralia/profile-hub GitHub Wiki
Profiles is using CKEditor, wrapped in an AngularJS directive.
CKEditor (website) supports a lot of different plugins.
The version we are using is based on the Classic version, with a few useful plugins added in order to keep it as small as possible.
- Go to the CKEditor Builder page
- Click the
classicbutton - Add the plugins selected in
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/src/ckeditor.jsfile using the online interface. This contains the built in plugins we are using. It also contains custom plugins. Make sure they are not added. Click Next. - Choose the toolbar items and arrange them. Click next.
- Add/remove the languages using the controls on the UI
- Download the zip file
- Unpack the zip file and replace the contents of
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/ - Asset pipeline requires CKEditor to be in ES5. Therefore we need to transpile the code using babeljs. Install the following dependencies
npm install --save babel-loader @babel/core @babel/preset-env regenerator-runtime. - Modify
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/webpack.config.jsto include the following inmodule.exports.module.rulespath. See Note 1. - Add the following code to
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/webpack.config.jsin pathmodule.export.entry. See Note 2. - Run webpack
npx webpack. Build file should be found inbuilddirectory. - Update
build/ckeditor.jsfile by convertingconstkeyword tovarkeyword. This part of code is inserted as string and escapes the transpiling process. - More information on ES5 conversion can be found here.
- More information to extract css to a file can be found here. Note class
ck-contenthas to be added to apply the style on a page.
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: [ require( '@babel/preset-env' ) ]
}
}
]
}
Note 1
require.resolve( 'regenerator-runtime/runtime.js' )
Note 2
Do not put custom plugins in the thirdparty/ckeditor directory - they will easily be deleted when CKEditor is updated. Put them in javascript/ckeditor/plugins (the javascript directory contains only our custom js).
- Install ckeditor built in plugins from npm. Change directory to
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/. Then,npm install. - Create a new file in
[projects dir]/profile-hub/web-app/javascript/ckeditor/pluginswith the name of your plugin. SeeInsertImage.jsfor example. - Register the plugin in the
[projects dir]/profile-hub/grails-app/assets/thirdparty/ckeditor5/src/ckeditor.jsfile. See note 3 below. - Run webpack
npx webpack. Build file should be found inbuilddirectory. - Update
build/ckeditor.jsfile by convertingconstkeyword tovarkeyword. This part of code is inserted as string and escapes the transpiling process.
import InsertImage from '../../../javascripts/ckeditor/plugins/insertimage.js'
class Editor extends ClassicEditor {}
Editor.builtinPlugins = [
Bold,
...
InsertImage
];
export default Editor;
Note 3
The version we are using is based on the Basic version, with a few useful plugins added in order to keep it as small as possible.
- Go to the CKEditor Builder page
- Click the
upload build-config.jsbutton - Find the
[projects dir]/profile-hub/web-app/thirdparty/ckeditor/build-config.jsfile. This contains the config we are using - Add/remove additional plugins or languages using the controls on the UI
- Download the zip file
- Unpack the zip file and replace the contents of
[projects dir]/profile-hub/web-app/thirdparty/ckeditor/ - Update the config in
profiles.jsas necessary - Remove the directory
[projects dir]/profile-hub/web-app/thirdparty/ckeditor/samples.
Do not put custom plugins in the thirdparty/ckeditor directory - they will easily be deleted when CKEditor is updated. Put them in js/ckeditor (the js directory contains only our custom js).
- Create a new directory in
[projects dir]/profile-hub/web-app/js/ckeditor/pluginswith the name of your plugin. - Add a
plugin.jsfile - Register the plugin in the plugin.js file using
CKEDITOR.plugins.add('alaToolbar', { init: function (editor) {...} - Update
profiles.jsand add a line like thisCKEDITOR.plugins.addExternal('<plugin>', config.contextPath + '/static/js/ckeditor/plugins/<plugin>/');