dijit/_editor/plugins/EnterKeyHandling (version 1.10)

dijit/_editor/_Plugin

Summary

This plugin tries to make all browsers behave consistently with regard to how ENTER behaves in the editor window. It traps the ENTER key and alters the way DOM is constructed in certain cases to try to commonize the generated DOM and behaviors across browsers.

This plugin has three modes:

In blockNodeForEnter=P, the ENTER key starts a new paragraph, and shift-ENTER starts a new line in the current paragraph. For example, the input:

first paragraph <shift-ENTER>
second line of first paragraph <ENTER>
second paragraph

will generate:

<p>
    first paragraph
    <br/>
    second line of first paragraph
</p>
<p>
    second paragraph
</p>

In BR and DIV mode, the ENTER key conceptually goes to a new line in the current paragraph, and users conceptually create a new paragraph by pressing ENTER twice. For example, if the user enters text into an editor like this:

one <ENTER>
two <ENTER>
three <ENTER>
<ENTER>
four <ENTER>
five <ENTER>
six <ENTER>

It will appear on the screen as two 'paragraphs' of three lines each. Markupwise, this generates:

BR:

one<br/>
two<br/>
three<br/>
<br/>
four<br/>
five<br/>
six<br/>

DIV:

<div>one</div>
<div>two</div>
<div>three</div>
<div>&nbsp;</div>
<div>four</div>
<div>five</div>
<div>six</div>

Usage

var foo = new EnterKeyHandling(args);
dijit/_editor/plugins/EnterKeyHandling
Parameter Type Description
args undefined

This object will be passed to the plugin constructor

See the dijit/_editor/plugins/EnterKeyHandling reference documentation for more information.

Property Summary

  • blockNodeForEnterThis property decides the behavior of Enter key.
  • blockNodesRegex for testing if a given tag is a block level (display:block) tag
  • bogusHtmlContentHTML to stick into a new empty block
  • buttonPointer to dijit/form/Button or other widget (ex: dijit/form/FilteringSelect) that is added to the toolbar to control this plugin.
  • commandString like &quot;insertUnorderedList&quot;, &quot;outdent&quot;, &quot;justifyCenter&quot;, etc. that represents an editor command.
  • disabledFlag to indicate if this plugin has been disabled and should do nothing helps control button state, among other things.
  • editorPoints to the parent editor
  • iconClassPrefixThe CSS class name for the button node is formed from iconClassPrefix and command
  • useDefaultCommandIf true, this plugin executes by calling Editor.execCommand() with the argument specified in command.

Method Summary

  • _adjustNodeAndOffset(node,offset) In the case there are multiple text nodes in a row the offset may not be within the node.
  • _getAttrNames(name) Helper function for get() and set().
  • _initButton() Initialize the button or other widget that will control this plugin.
  • _set(name,value) Helper function to set new value for specified attribute
  • _setDisabledAttr(disabled) Function to set the plugin state and call updateState to make sure the button is updated appropriately.
  • buttonClass() Class of widget (ex: dijit.form.Button or dijit/form/FilteringSelect) that is added to the toolbar to control this plugin.
  • connect(o,f,tf) Deprecated.
  • destroy()
  • get(name) Get a property from a plugin.
  • getLabel(key) Returns the label to use for the button
  • handleEnterKey(e) Handler for enter key events when blockNodeForEnter is DIV or P.
  • own() Track specified handles and remove/destroy them when this instance is destroyed, unless they were already removed/destroyed manually.
  • removeTrailingBr(container) If last child of container is a <br>, then remove it.
  • set(name,value) Set a property on a plugin
  • setEditor(editor)
  • setToolbar(toolbar) Tell the plugin to add it's controller widget (often a button) to the toolbar.
  • updateState() Change state of the plugin to respond to events in the editor.

Event Summary

  • onKeyPressed() Handler for after the user has pressed a key, and the display has been updated.

Properties

blockNodeForEnter

This property decides the behavior of Enter key. It can be either P, DIV, BR, or empty (which means disable this feature). Anything else will trigger errors. The default is 'BR'

See class description for more details.

blockNodes

Regex for testing if a given tag is a block level (display:block) tag

bogusHtmlContent

HTML to stick into a new empty block

button
Defined by: dijit/_editor/_Plugin

Pointer to dijit/form/Button or other widget (ex: dijit/form/FilteringSelect) that is added to the toolbar to control this plugin. If not specified, will be created on initialization according to buttonClass

command
Defined by: dijit/_editor/_Plugin

String like "insertUnorderedList", "outdent", "justifyCenter", etc. that represents an editor command. Passed to editor.execCommand() if useDefaultCommand is true.

disabled
Defined by: dijit/_editor/_Plugin

Flag to indicate if this plugin has been disabled and should do nothing helps control button state, among other things. Set via the setter api.

editor
Defined by: dijit/_editor/_Plugin

Points to the parent editor

iconClassPrefix
Defined by: dijit/_editor/_Plugin

The CSS class name for the button node is formed from iconClassPrefix and command

useDefaultCommand
Defined by: dijit/_editor/_Plugin

If true, this plugin executes by calling Editor.execCommand() with the argument specified in command.

Methods

_adjustNodeAndOffset(node,offset)

In the case there are multiple text nodes in a row the offset may not be within the node. If the offset is larger than the node length, it will attempt to find the next text sibling until it locates the text node in which the offset refers to

Parameter Type Description
node DomNode

The node to check.

offset Int

The position to find within the text node

Returns:object
_getAttrNames(name)

Helper function for get() and set(). Caches attribute name values so we don't do the string ops every time.

Parameter Type Description
name undefined
Returns:undefined | object
_initButton()

Initialize the button or other widget that will control this plugin. This code only works for plugins controlling built-in commands in the editor.

_set(name,value)

Helper function to set new value for specified attribute

Parameter Type Description
name String
value anything
_setDisabledAttr(disabled)

Function to set the plugin state and call updateState to make sure the button is updated appropriately.

Parameter Type Description
disabled undefined
buttonClass()

Class of widget (ex: dijit.form.Button or dijit/form/FilteringSelect) that is added to the toolbar to control this plugin. This is used to instantiate the button, unless button itself is specified directly.

connect(o,f,tf)

Deprecated. Use this.own() with dojo/on or dojo/aspect.instead.

Make a connect.connect() that is automatically disconnected when this plugin is destroyed. Similar to dijit/_Widget.connect().

Parameter Type Description
o undefined
f undefined
tf undefined
destroy()
get(name)

Get a property from a plugin.

Get a named property from a plugin. The property may potentially be retrieved via a getter method. If no getter is defined, this just retrieves the object's property. For example, if the plugin has a properties "foo" and "bar" and a method named "_getFooAttr", calling:

plugin.get("foo");

would be equivalent to writing:

plugin._getFooAttr();

and:

plugin.get("bar");

would be equivalent to writing:

plugin.bar;
Parameter Type Description
name undefined

The property to get.

Returns:undefined
getLabel(key)

Returns the label to use for the button

Parameter Type Description
key String
Returns:undefined
handleEnterKey(e)

Handler for enter key events when blockNodeForEnter is DIV or P.

Manually handle enter key event to make the behavior consistent across all supported browsers. See class description for details.

Parameter Type Description
e undefined
Returns:boolean
own()
Defined by dijit/Destroyable

Track specified handles and remove/destroy them when this instance is destroyed, unless they were already removed/destroyed manually.

Returns:any | undefined

The array of specified handles, so you can do for example:

var handle = this.own(on(...))[0];
removeTrailingBr(container)

If last child of container is a <br>, then remove it.

Parameter Type Description
container undefined
set(name,value)

Set a property on a plugin

Sets named properties on a plugin which may potentially be handled by a setter in the plugin. For example, if the plugin has a properties "foo" and "bar" and a method named "_setFooAttr", calling:

plugin.set("foo", "Howdy!");

would be equivalent to writing:

plugin._setFooAttr("Howdy!");

and:

plugin.set("bar", 3);

would be equivalent to writing:

plugin.bar = 3;

set() may also be called with a hash of name/value pairs, ex:

plugin.set({
    foo: "Howdy",
    bar: 3
})

This is equivalent to calling set(foo, "Howdy") and set(bar, 3)

Parameter Type Description
name attribute

The property to set.

value anything

The value to set in the property.

Returns:function | string

Set a property on a plugin

setEditor(editor)
Parameter Type Description
editor undefined
setToolbar(toolbar)

Tell the plugin to add it's controller widget (often a button) to the toolbar. Does nothing if there is no controller widget.

Parameter Type Description
toolbar dijit/Toolbar
updateState()

Change state of the plugin to respond to events in the editor.

This is called on meaningful events in the editor, such as change of selection or caret position (but not simple typing of alphanumeric keys). It gives the plugin a chance to update the CSS of its button.

For example, the "bold" plugin will highlight/unhighlight the bold button depending on whether the characters next to the caret are bold or not.

Only makes sense when useDefaultCommand is true, as it calls Editor.queryCommandEnabled(command).

Events

onKeyPressed()

Handler for after the user has pressed a key, and the display has been updated. Connected to RichText's onKeyPressed() method.

Error in the documentation? Can’t find what you are looking for? Let us know!