10.3. <rich:editor>
The
<rich:editor> component is used for creating a WYSIWYG editor on a page.
<rich:editor> component is based on the CKEditor implementation.
When rendering a
<rich:editor>, a textarea is rendered to the page and once the page is completely loaded (ready state), the textarea is enhanced using a CKEditor script and replaced with a full-featured WYSIWYG editor.
10.3.1. Basic usage
Basic usage requires the
value attribute to point to the expression for the current value of the component.
Example 10.5. Basic usage of <rich:editor>
<rich:editor value="#{backingBean.editedValue}" /> <h:outputText escape="false" value="#{backingBean.editedValue}" />
Note that the editor produces HTML markup and to be able to render it's output, the markup needs to be unescaped (as with
<h:outputText> component in example above).
The dimensions of the editor can be managed using
width and height attributes.
The
readonly attribute can be used to switch the editor into a read-only mode.
The
tabindex attribute specifies the position of the current element in the tabbing order for the current document.
Note
The
ResourceServlet has to be registered for the url-pattern /org.richfaces.resources/* in order to serve the editor resources (JavaScript, CSS, images) correctly. Check the RichFaces Developer's Guide for further details.
Note
The
<rich:editor> requires the <h:body> component to be present in the view and must be an ancestor of the editor in order for the resource dependencies to render correctly.
10.3.2. Styling
There are several options to customize the style of the editor:
style, styleClass: customizes the style of the editor and underlying textareaeditorStyle, editorClass: customizes the style of the CKEditor instancetextareaStyle, textareaClass: customizes the style of the underlying textarea
10.3.3. Editor skins
The
<rich:editor> is skinnable using the JavaScript API of the CKeditor. Refer to the CKeditor documentation on installing skins for details on how to customize the look and feel of the editor component.
10.3.4. Advanced configuration
The basic set of
<rich:editor> attributes allows you to support common use-cases for a WYSIWYG editor. However the underlying CKEditor implementation supports many more configuration options.
Use the
config attribute to define any of these advanced configuration options supported by the CKEditor. This configuration is written in JavaScript object format and its value is interpolated for EL expressions (making configuration dynamic).
There are two ways to define the configuration: the
config attribute or a facet named config. The facet takes precedence over attribute when both are defined.
<rich:editor config="startupFocus: #{userPreferences.startupFocus}" /> <rich:editor> <f:facet name="config"> startupFocus: #{userPreferences.startupFocus} </f:facet> </rich:editor>
In the above samples, the
<rich:editor> is configured to take focus after loading the page as defined by the userPreference bean. Definitions using either attribute or facet are equivalent.
Note
For further configuration options, refer to CKEditor 4 Developer Guide and CKEditor 4 configuration reference.
10.3.5. Toolbar customization
The
<rich:editor> supports a toolbar attribute, which is able to change the configuration of the toolbar's button set. There are two configurations available: basic (default), full (enables all of the features).
It is also possible to define a custom toolbar using the CKEditor toolbar configuration in a
config facet:
<rich:editor toolbar="CustomToolbar"> <f:facet name="config"> toolbar_CustomToolbar: [ { name: 'document', items : [ 'NewPage','Preview' ] }, { name: 'clipboard', items : [ 'Cut','Copy','Paste','-','Undo','Redo' ] }, { name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','Scayt' ] }, { name: 'insert', items : [ 'Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe' ] }, '/', { name: 'styles', items : [ 'Styles','Format' ] }, { name: 'basicstyles', items : [ 'Bold','Italic','Strike','-','RemoveFormat' ] }, { name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote' ] }, { name: 'links', items : [ 'Link','Unlink','Anchor' ] }, { name: 'tools', items : [ 'Maximize' ] } ] </f:facet> </rich:editor>
Note that toolbar name (
CustomToolbar) needs to match the toolbar_<name> configuration option.
10.3.6. Internationalization and localization
The
<rich:editor> comes with a lang attribute which allows you to change the localization of the editor. For language configuration options, refer to http://www.w3.org/TR/html4/struct/dirlang.html.
The
lang attribute influences following settings:
- underlying textarea - specifies the i18n settings for received and submitted content
- editor value - specifies the i18n settings for value edited in WYSIWYG mode
- default settings of localization of editor controls and interface
However the interface first localized using the browser configuration (usually determined by client system settings). To force the editor to use a specific localization for the interface, you use the advanced CKEditor configuration option
language, as in following sample:
<rich:editor lang="fr" config="language: 'fr'" />
The above sample forces the editor to use a french interface, suppressing the browser preferred settings.
10.3.7. Client-side event handlers
The
<rich:editor> component produces set of events for handling component specific interaction.
init- once the editor is initialized and ready to be handle user interactionfocus- once the editor is focusedblur- once the editor is blurredchange- fired on blur event when editor content has been changed after previous focusdirty- fired immediately after editor content has been changed
Events can be handled either by registering a JavaScript event handler or by attaching JSF behavior:
<rich:editor value="#{backingBean.editorValue}"> <a4j:ajax event="change" render="editorOutput" /> <a4j:ajax event="dirty" render="editorOutput"> <a4j:attachQueue requestDelay="1000" /> </a4j:ajax> </rich:editor> <a4j:outputPanel id="editorOutput"> <h:outputText escape="false" value="#{backingBean.editorValue}" /> </a4j:outputPanel>
The example above shows the editor and its output, which is updated every second after each instant change or immediately after user focus leaves the editor area.
10.3.8. JavaScript API
The
<rich:editor> component can be controlled through the JavaScript API. The JavaScript API provides the following functions:
getValue()- Get the current value of the input control.
setValue(newValue)- Set the value of the input control to the
newValuestring passed as a parameter. getEditor()- Returns the CKEditor object instance associated to given
<rich:editor>component. getInput()- Returns the associated textarea.
focus()- Gives focus to this component
blur()- Removes focus from this component
isFocused()- Returns
trueif this component is focused isDirty()- Returns
trueif editor is focused and it was edited from last focus event (reset by blur event, by usingsetValue(newValue)call and when component is re-rendered) isValueChanged()- Returns
trueif the control's value has been changed from the default (reset bysetValue(newValue)call and when component is re-rendered) isReadonly()- Returns
trueif editor content is editable. setReadonly(readonly)- When
readonlyistrue, editor will be switched to editable state. Otherwise, it will be switched to readonly state.
10.3.9. Reference data
component-type:org.richfaces.Editorcomponent-class:org.richfaces.component.UIEditorcomponent-family:org.richfaces.Editorrenderer-type:org.richfaces.EditorRenderer
10.3.10. Style classes and skin parameters
Table 10.3. Style classes (selectors) and corresponding skin parameters
| Class (selector) | Skin Parameters | Mapped CSS properties |
|---|---|---|
| panelBorderColor
|
border-color
|
| editorMainBackgroundColor
|
background-color
|
| panelBorderColor
|
border-color
|
generalBackgroundColor
|
background
| |
| headerBackgroundColor
|
repeat-x
|
headerWeightFont
|
font-weight
| |
headerTextColor
|
color
| |
headerFamilyFont
|
font-family
| |
headerSizeFont
|
font-size
| |
| editorMainTextColor
|
color
|
| additionalBackgroundColor
|
background-color
|
panelBorderColor
|
border-color
| |
| panelBorderColor
|
border-color
|
tabBackgroundColor
|
background-color
| |
| panelBorderColor
|
border-color
|
generalSizeFont
|
font-size
| |
generalFamilyFont
|
font-family
| |
controlTextColor
|
color
| |
controlBackgroundColor
|
background-color
| |
| headerBackgroundColor
|
background-color
|
panelBorderColor
|
border-left-color
|
