| Authors: | Becky Gibson
Bill Keese Nikolai Onken Marcus Reimann Jared Jurkiewicz |
|---|---|
| Developers: | Liu Cougar, Bill Keese, Douglas Hays, Becky Gibson, Jared Jurkiewicz |
| since: | V1.0 |
Contents
Dijit's Rich Text editor, dijit/Editor, is a text box on steroids, designed to look and work like a word processor. The editor features a toolbar, HTML output, and a plugin architecture that supports new commands, new buttons and other new features.
require(["dojo/parser", "dijit/Editor", "dojo/domReady!"], function(parser){
parser.parse();
myEditor.onLoadDeferred.then(function(){
myEditor.set("value", "<b>This is new content.</b>");
});
});
<div data-dojo-type="dijit/Editor" id="myEditor">
<p>This is the initial content.</p>
</div>
require(["dojo/parser", "dijit/Editor"]);
<div data-dojo-type="dijit/Editor" id="editor1" data-dojo-props="onChange:function(){console.log('editor1 onChange handler: ' + arguments[0])}">
<p>This instance is created from a div directly with default toolbar and plugins</p>
</div>
Of course, the editor can be created programmatically in addition to declaratively:
require(["dijit/Editor", "dijit/_editor/plugins/AlwaysShowToolbar", "dojo/dom", "dojo/query", "dojo/domReady!"],
function(Editor, AlwaysShowToolbar, dom, query){
this.createEditor = function(){
var myEditor = new Editor({
height: '',
extraPlugins: [AlwaysShowToolbar]
}, dom.byId('programmatic2'));
myEditor.startup();
query('#create2').orphan();
}
});
<div id="programmatic2">This div will become an auto-expanding editor.</div>
<button id="create2" onclick="createEditor();">
create expanding editor
</button>
Of course the toolbar can be reordered and customized to suit your layout needs.
require(["dojo/parser", "dijit/Editor"]);
<div data-dojo-type="dijit/Editor" id="editor1" data-dojo-props="onChange:function(){console.log('editor1 onChange handler: ' + arguments[0])},
plugins:['cut','copy','paste','|','bold','italic','underline','strikethrough','subscript','superscript','|', 'indent', 'outdent', 'justifyLeft', 'justifyCenter', 'justifyRight']">
<p>This instance is created with a subset of functions enabled in the order we want</p>
</div>
A plugin (a.k.a. extension) is something that adds a function to the editor, or changes it's behavior. Dojo includes a number of editor plugins, and developers can write additional plugins on their own.
Most plugins have an associated toolbar button(s), such as the FontChoice plugin (which has a drop down list for fonts), but some plugins (like AlwaysShowToolbar) just affect the Editor's behavior without changing the toolbar.
The "plugins" parameter controls which plugins are available, and also controls which builtin editor commands are available. It can also be used to re-arrange the default ordering of the buttons.
The basic plugins which are enabled by default are: undo, redo, cut, copy, paste, bold, italic, underline, strikethrough, insertOrderedList, insertUnorderedList, indent, outdent, justifyLeft, justifyRight, justifyCenter, justifyFull, dijit._editor.plugins.EnterKeyHandling
If you want to just add plugins above and beyond the standard configuration, then you should use the "extraPlugins" parameter.
Both the "plugins" parameter and the "extraPlugins" parameter are arrays, where each element in the array can be a simple string or an object (if you need to set options on a plugin).
This example adds the text color, background color, and font selection plugins to the editor by setting extraPlugins. (Technically, the FontChoice plugin provides two commands, foreground-color and highlight-color.)
require([
"dojo/parser",
"dijit/Editor",
"dijit/_editor/plugins/FontChoice", // 'fontName','fontSize','formatBlock'
"dijit/_editor/plugins/TextColor"
]);
<div data-dojo-type="dijit/Editor" id="editor2"
data-dojo-props="extraPlugins:['foreColor','hiliteColor',{name:'dijit/_editor/plugins/FontChoice', command:'fontName', generic:true}],
onChange:function(){console.log('editor2 onChange handler: ' + arguments[0])}">
<p>This instance is created with additional toolbar/ plugins</p>
</div>
This example starts from scratch, thus removing some items from the toolbar (as compared to the default), like underline, and adding other features, namely the LinkDialog:
require(["dojo/parser", "dijit/Editor", "dijit/_editor/plugins/LinkDialog"]);
<div data-dojo-type="dijit/Editor" id="editor3"
data-dojo-props="plugins:['bold','italic','|','createLink'],
onChange:function(){console.log('editor3 onChange handler: ' + arguments[0])}">
<p>This instance is created with customized toolbar/ plugins</p>
</div>
This is a list of the default commands (plugins) supported by the editor as built-in capabilities. They can be specified in the plugins parameter (in addition to actual editor plugins in the editor/plugins directory or other places):
| Command/Plugin | Description |
| undo | Undo the last operation on the editor contents. |
| redo | Redo the last operation that was 'undone' on the editor contents |
| cut | Remove the currently selected text and put it on the clipboard. Please note that some browsers, such as FireFox, do not allow direct access to the clipboard by default (for security purposes). The editor, therefore, cannot use its own events to access and past content there. In those cases, the editor will warn you it cannot and tell you what native hotkey sequence to use to perform the operation. |
| copy | Copy the currently selected text and put it on the clipboard. Please note that some browsers, such as FireFox, do not allow direct access to the clipboard by default (for security purposes). The editor, therefore, cannot use its own events to access and past content there. In those cases, the editor will warn you it cannot and tell you what native hotkey sequence to use to perform the operation. |
| paste | Paste content currently in the clipboard to the editor. Please note that some browsers, such as FireFox, do not allow direct access to the clipboard by default (for security purposes). The editor, therefore, cannot use its own events to access and past content there. In those cases, the editor will warn you it cannot and tell you what native hotkey sequence to use to perform the operation. |
| selectAll | Select all the content in the editor. |
| bold | Bold the currently selected text. |
| italic | Italic the currently selected text. |
| underline | Underline the currently selected text. |
| strikethrough | Strike through the currently selected text. |
| subscript | Make the currently selected text subscript. |
| superscript | Make the currently selected text superscript. |
| removeFormat | Remove formatting on current block. |
| insertOrderedList | Insert an ordered list (1, 2, 3, etc.) |
| insertUnorderedList | Insert an unordered list (bullets) |
| insertHorizontalRule | Insert a horizontal line. |
| indent | Indent the current text block or list item |
| outdent | 'Unindent' the current text block or list item. |
| justifyLeft | Justify the current text block/selected text to the left. |
| justifyRight | Justify the current text block/selected text to the right. |
| justifyCenter | Center the current text block/selected text. |
| justifyFull | Full-justify the current text block/selected text. |
| createLink | Create a hyperlink. Works best when using the dijit._editor.plugins.LinkDialog <dijit/_editor/plugins/LinkDialog> plugin. |
| unlink | Unlink the current hyperlink under the cursor/selected text. |
| delete | Delete the currently selected text. |
There are several additional editor plugins provided by dijit and are listed on the page: dijit._editor.plugins. These plugins add very useful functionality above and beyond the basics of editor, such as setting text color or printing. Please note that several of the plugins actually provide multiple capabilities.
DojoX (Dojo eXtensions) contains even more plugins for improving the capabilities of dijit.Editor. These are functions that were deemed 'less common' requirements and were therefore put in the extensions namespace. Please refer to the dojox.editor.plugins page for more information about them.
Typically an editor has a constant height, and if there's a lot of content it gets a scrollbar. This is in addition to the main scrollbar for the page.
Editor also has a mode like dijit.form.Textarea where the more a user types, the more the text box expands.
However, that's a bit tricky because if implemented naively the toolbar would eventually scroll off the top of the page.
The AlwaysShowToolbar plugin prevents that. It's used along with setting height="" parameter setting.
require(["dojo/parser", "dijit/Editor", "dijit/_editor/plugins/AlwaysShowToolbar"]);
<div data-dojo-type="dijit/Editor" id="editor5"
data-dojo-props="extraPlugins:['dijit/_editor/plugins/AlwaysShowToolbar']" height="100px">
<p>
This editor is created from a div with AlwaysShowToolbar plugin (do not forget to set height="").
</p>
</div>
| Action | Key |
|---|---|
| Move focus to the next widget in the tab order. | Tab (must press tab twice in some situations - see Known Issues below) |
| Move focus to the prior widget in the tab order (the editor toolbar) | Shift+Tab (must press shift-tab twice in some situations - see Known Issues below) |
| Action | Key |
|---|---|
| Move focus to the next enabled button in the toolbar. | Arrow right in left to right locales, arrow left in right to left locales |
| Move focus to the previous widget in the toolbar | Arrow left in left to right locales; arrow right in right to left locales. |
The arrow keys will not work within any optional drop down lists such as ComboBox or FilteringSelect in the editor toolbar until the drop down list of choices has been activated. Use the backspace or escape key to clear the current selection in the textbox associated with the drop down. When the list of choices is not activated, the arrow keys will move between toolbar buttons rather than within the combobox or select.
In order for the screen reader to announce a label for the editor, the developer must include a label element that is associated with the editor using the for attribute. When the editor is created, Dojo will create a title element for the HTML document within the editor that contains the label text. The screen reader will announce that title when the editor component gets focus.