Follow these steps to create your button for AlloyEditor:
-
Create a
.jsx
file in your OSGi bundle’sresources\META-INF\resources\js
folder. This file defines your button’s configuration. -
Inside the JSX file, define the React variables your buttons require (React, ReactDOM). The log text button only uses AlloyEditor’s React:
(function() { 'use strict'; var React = AlloyEditor.React;
-
Create your button’s class:
var LogSelectedTextButton = React.createClass( { //button configuration goes here } );
-
Inside the
React.createClass()
method’s configuration object, specify the mixins your button requires. These provide additional functionality, making it easy to add features to your button, such as binding a shortcut key to your button. The example below uses theButtonStateClasses
andButtonKeystroke
mixins:mixins: [AlloyEditor.ButtonStateClasses, AlloyEditor.ButtonKeystroke],
-
Pass validating props for your button. These are defined for each instance of the button. At the very least, the
editor
must be defined. The example below sets up properties for the editor, label, and tabIndex:propTypes: { /** * The editor instance where the component is being used. * * @instance * @memberof LogSelectedTextButton * @property {Object} editor */ editor: React.PropTypes.object.isRequired, /** * The label that should be used for accessibility purposes. * * @instance * @memberof LogSelectedTextButton * @property {String} label */ label: React.PropTypes.string, /** * The tabIndex of the button in its toolbar current state. A value other than -1 * means that the button has focus and is the active element. * * @instance * @memberof LogSelectedTextButton * @property {Number} tabIndex */ tabIndex: React.PropTypes.number },
-
Define the static properties for your button. You must at least provide the
key
. Thekey
defines the button’s name to specify in the AlloyEditor’s configuration. Themy-log-text-button
module’s static properties are shown below:statics: { /** * The name which is used as an alias of the button in the configuration. * * @default myTestButton * @memberof LogSelectedTextButton * @property {String} key * @static */ key: 'logSelectedText' },
-
Optionally define any default properties your button has for each instance using the
getDefaultProps
property. The example below uses theButtonKeystroke
mixin’s requiredcommand
andkeystroke
properties to set the shortcut keys for the button’slogText()
function:getDefaultProps: function() { return { command: 'logText', keystroke: { fn: 'logText', keys: CKEDITOR.CTRL + CKEDITOR.SHIFT + 89 /*Y*/ } }; },
-
Define the HTML markup to render for your button. The example below uses the
getStateClasses()
method to retrieve the state class information provided by theButtonStateClasses
mixin and add it to the currentcssClass
value. It also uses Liferay Util’sgetLexiconIconTpl()
method to retrieve a Lexicon icon to use for the button. See Lexicon’s Design Site for a full list of the available icons.render: function() { var cssClass = 'ae-button ' + this.getStateClasses(); var svg = Liferay.Util.getLexiconIconTpl('desktop'); return ( <button className={cssClass} onClick={this._logText} title="Log the selected text in the console" dangerouslySetInnerHTML={{__html: svg}} /> ); },
-
Define your button’s main action. Retrieving the
nativeEditor
, as shown in the example below, gives you access to the full API of CKEditor. From there, you can use any of the availableCKEditor.editor
methods to interact with the editor’s content. The example below chains the editor’sgetSelection()
andgetSelectedText()
methods to retrieve the user’s highlighted text, and then it logs it to the browser’s console:/** * @protected * @method _logText */ _logText: function() { var editor = this.props.editor.get('nativeEditor'); var selectedText = editor.getSelection().getSelectedText(); console.log("Your selected text is " + selectedText); }
-
Finally, add the button to the list of available buttons:
AlloyEditor.Buttons[LogSelectedTextButton.key] = AlloyEditor.LogSelectedTextButton = LogSelectedTextButton;
Now you know how to create a button for AlloyEditor!
Related Topics
Adding New Behavior to an Editor