mxCellEditor

In-place editor for the graph.  To control this editor, use mxGraph.invokesStopCellEditing, mxGraph.enterStopsCellEditing and mxGraph.escapeEnabled.  If mxGraph.enterStopsCellEditing is true then ctrl-enter or shift-enter can be used to create a linefeed.  The F2 and escape keys can always be used to stop editing.

To customize the location of the textbox in the graph, override getEditorBounds as follows:

graph.cellEditor.getEditorBounds = function(state)
{
  var result = mxCellEditor.prototype.getEditorBounds.apply(this, arguments);

  if (this.graph.getModel().isEdge(state.cell))
  {
    result.x = state.getCenterX() - result.width / 2;
    result.y = state.getCenterY() - result.height / 2;
  }

  return result;
};

Note that this hook is only called if autoSize is false.  If autoSize is true, then mxShape.getLabelBounds is used to compute the current bounds of the textbox.

The textarea uses the mxCellEditor CSS class.  You can modify this class in your custom CSS.  Note: You should modify the CSS after loading the client in the page.

Example

To only allow numeric input in the in-place editor, use the following code.

var text = graph.cellEditor.textarea;

mxEvent.addListener(text, 'keydown', function (evt)
{
  if (!(evt.keyCode >= 48 && evt.keyCode <= 57) &&
      !(evt.keyCode >= 96 && evt.keyCode <= 105))
  {
    mxEvent.consume(evt);
  }
});

Placeholder

To implement a placeholder for cells without a label, use the emptyLabelText variable.

Resize in Chrome

Resize of the textarea is disabled by default.  If you want to enable this feature extend init and set this.textarea.style.resize = ‘’.

To start editing on a key press event, the container of the graph should have focus or a focusable parent should be used to add the key press handler as follows.

mxEvent.addListener(graph.container, 'keypress', mxUtils.bind(this, function(evt)
{
  if (!graph.isEditing() && !graph.isSelectionEmpty() && evt.which !== 0 &&
      !mxEvent.isAltDown(evt) && !mxEvent.isControlDown(evt) && !mxEvent.isMetaDown(evt))
  {
    graph.startEditing();

    if (mxClient.IS_FF)
    {
      graph.cellEditor.textarea.value = String.fromCharCode(evt.which);
    }
  }
}));

To allow focus for a DIV, and hence to receive key press events, some browsers require it to have a valid tabindex attribute.  In this case the following code may be used to keep the container focused.

var graphFireMouseEvent = graph.fireMouseEvent;
graph.fireMouseEvent = function(evtName, me, sender)
{
  if (evtName == mxEvent.MOUSE_DOWN)
  {
    this.container.focus();
  }

  graphFireMouseEvent.apply(this, arguments);
};
Summary
mxCellEditorIn-place editor for the graph.
Functions
mxCellEditorConstructs a new in-place editor for the specified graph.
Variables
graphReference to the enclosing mxGraph.
textareaHolds the DIV that is used for text editing.
editingCellReference to the mxCell that is currently being edited.
triggerReference to the event that was used to start editing.
modifiedSpecifies if the label has been modified.
autoSizeSpecifies if the textarea should be resized while the text is being edited.
selectTextSpecifies if the text should be selected when editing starts.
emptyLabelTextText to be displayed for empty labels.
escapeCancelsEditingIf true, pressing the escape key will stop editing and not accept the new value.
textNodeReference to the label DOM node that has been hidden.
zIndexSpecifies the zIndex for the textarea.
minResizeDefines the minimum width and height to be used in resize.
wordWrapPaddingCorrection factor for word wrapping width.
blurEnabledIf focusLost should be called if textarea loses the focus.
initialValueHolds the initial editing value to check if the current value was modified.
alignHolds the current temporary horizontal alignment for the cell style.
Functions
initCreates the textarea and installs the event listeners.
applyValueCalled in stopEditing if cancel is false to invoke mxGraph.labelChanged.
setAlignSets the temporary horizontal alignment for the current editing session.
getInitialValueGets the initial editing value for the given cell.
getCurrentValueReturns the current editing value.
isCancelEditingKeyEventReturns true if escapeCancelsEditing is true and shift, control and meta are not pressed.
installListenersInstalls listeners for focus, change and standard key event handling.
isStopEditingEventReturns true if the given keydown event should stop cell editing.
isEventSourceReturns true if this editor is the source for the given native event.
resizeReturns modified.
focusLostCalled if the textarea has lost focus.
getBackgroundColorReturns the background color for the in-place editor.
isLegacyEditorReturns true if max-width is not supported or if the SVG root element in in the graph does not have CSS position absolute.
startEditingStarts the editor for the given cell.
isSelectTextReturns selectText.
clearSelectionClears the selection.
stopEditingStops the editor and applies the value if cancel is false.
prepareTextareaPrepares the textarea for getting its value in stopEditing.
isHideLabelReturns true if the label should be hidden while the cell is being edited.
getMinimumSizeReturns the minimum width and height for editing the given state.
getEditorBoundsReturns the mxRectangle that defines the bounds of the editor.
getEmptyLabelTextReturns the initial label value to be used of the label of the given cell is empty.
getEditingCellReturns the cell that is currently being edited or null if no cell is being edited.
destroyDestroys the editor and removes all associated resources.

Functions

mxCellEditor

function mxCellEditor(graph)

Constructs a new in-place editor for the specified graph.

Parameters

graphReference to the enclosing mxGraph.

Variables

graph

mxCellEditor.prototype.graph

Reference to the enclosing mxGraph.

textarea

mxCellEditor.prototype.textarea

Holds the DIV that is used for text editing.  Note that this may be null before the first edit.  Instantiated in init.

editingCell

mxCellEditor.prototype.editingCell

Reference to the mxCell that is currently being edited.

trigger

mxCellEditor.prototype.trigger

Reference to the event that was used to start editing.

modified

mxCellEditor.prototype.modified

Specifies if the label has been modified.

autoSize

mxCellEditor.prototype.autoSize

Specifies if the textarea should be resized while the text is being edited.  Default is true.

selectText

mxCellEditor.prototype.selectText

Specifies if the text should be selected when editing starts.  Default is true.

emptyLabelText

mxCellEditor.prototype.emptyLabelText

Text to be displayed for empty labels.  Default is ‘’ or ‘<br>’ in Firefox as a workaround for the missing cursor bug for empty content editable.  This can be set to eg.  “[Type Here]” to easier visualize editing of empty labels.  The value is only displayed before the first keystroke and is never used as the actual editing value.

escapeCancelsEditing

mxCellEditor.prototype.escapeCancelsEditing

If true, pressing the escape key will stop editing and not accept the new value.  Change this to false to accept the new value on escape, and cancel editing on Shift+Escape instead.  Default is true.

textNode

mxCellEditor.prototype.textNode

Reference to the label DOM node that has been hidden.

zIndex

mxCellEditor.prototype.zIndex

Specifies the zIndex for the textarea.  Default is 5.

minResize

mxCellEditor.prototype.minResize

Defines the minimum width and height to be used in resize.  Default is 0x20px.

wordWrapPadding

mxCellEditor.prototype.wordWrapPadding

Correction factor for word wrapping width.  Default is 2 in quirks, 0 in IE 11 and 1 in all other browsers and modes.

blurEnabled

mxCellEditor.prototype.blurEnabled

If focusLost should be called if textarea loses the focus.  Default is false.

initialValue

mxCellEditor.prototype.initialValue

Holds the initial editing value to check if the current value was modified.

align

mxCellEditor.prototype.align

Holds the current temporary horizontal alignment for the cell style.  If this is modified then the current text alignment is changed and the cell style is updated when the value is applied.

Functions

init

mxCellEditor.prototype.init = function ()

Creates the textarea and installs the event listeners.  The key handler updates the modified state.

applyValue

mxCellEditor.prototype.applyValue = function(state,
value)

Called in stopEditing if cancel is false to invoke mxGraph.labelChanged.

setAlign

mxCellEditor.prototype.setAlign = function (align)

Sets the temporary horizontal alignment for the current editing session.

getInitialValue

mxCellEditor.prototype.getInitialValue = function(state,
trigger)

Gets the initial editing value for the given cell.

getCurrentValue

mxCellEditor.prototype.getCurrentValue = function(state)

Returns the current editing value.

isCancelEditingKeyEvent

mxCellEditor.prototype.isCancelEditingKeyEvent = function(evt)

Returns true if escapeCancelsEditing is true and shift, control and meta are not pressed.

installListeners

mxCellEditor.prototype.installListeners = function(elt)

Installs listeners for focus, change and standard key event handling.

isStopEditingEvent

mxCellEditor.prototype.isStopEditingEvent = function(evt)

Returns true if the given keydown event should stop cell editing.  This returns true if F2 is pressed of if mxGraph.enterStopsCellEditing is true and enter is pressed without control or shift.

isEventSource

mxCellEditor.prototype.isEventSource = function(evt)

Returns true if this editor is the source for the given native event.

resize

mxCellEditor.prototype.resize = function()

Returns modified.

focusLost

mxCellEditor.prototype.focusLost = function()

Called if the textarea has lost focus.

getBackgroundColor

mxCellEditor.prototype.getBackgroundColor = function(state)

Returns the background color for the in-place editor.  This implementation always returns null.

isLegacyEditor

mxCellEditor.prototype.isLegacyEditor = function()

Returns true if max-width is not supported or if the SVG root element in in the graph does not have CSS position absolute.  In these cases the text editor must use CSS position absolute to avoid an offset but it will have a less accurate line wrapping width during the text editing preview.  This implementation returns true for IE8- and quirks mode or if the CSS position of the SVG element is not absolute.

startEditing

mxCellEditor.prototype.startEditing = function(cell,
trigger)

Starts the editor for the given cell.

Parameters

cellmxCell to start editing.
triggerOptional mouse event that triggered the editor.

isSelectText

mxCellEditor.prototype.isSelectText = function()

Returns selectText.

clearSelection

mxCellEditor.prototype.clearSelection = function()

Clears the selection.

stopEditing

mxCellEditor.prototype.stopEditing = function(cancel)

Stops the editor and applies the value if cancel is false.

prepareTextarea

mxCellEditor.prototype.prepareTextarea = function()

Prepares the textarea for getting its value in stopEditing.  This implementation removes the extra trailing linefeed in Firefox.

isHideLabel

mxCellEditor.prototype.isHideLabel = function(state)

Returns true if the label should be hidden while the cell is being edited.

getMinimumSize

mxCellEditor.prototype.getMinimumSize = function(state)

Returns the minimum width and height for editing the given state.

getEditorBounds

mxCellEditor.prototype.getEditorBounds = function(state)

Returns the mxRectangle that defines the bounds of the editor.

getEmptyLabelText

mxCellEditor.prototype.getEmptyLabelText = function (cell)

Returns the initial label value to be used of the label of the given cell is empty.  This label is displayed and cleared on the first keystroke.  This implementation returns emptyLabelText.

Parameters

cellmxCell for which a text for an empty editing box should be returned.

getEditingCell

mxCellEditor.prototype.getEditingCell = function ()

Returns the cell that is currently being edited or null if no cell is being edited.

destroy

mxCellEditor.prototype.destroy = function ()

Destroys the editor and removes all associated resources.

function mxCellEditor(graph)
Constructs a new in-place editor for the specified graph.
mxCellEditor.prototype.graph
Reference to the enclosing mxGraph.
Extends mxEventSource to implement a graph component for the browser.
mxCellEditor.prototype.textarea
Holds the DIV that is used for text editing.
mxCellEditor.prototype.editingCell
Reference to the mxCell that is currently being edited.
Cells are the elements of the graph model.
mxCellEditor.prototype.trigger
Reference to the event that was used to start editing.
mxCellEditor.prototype.modified
Specifies if the label has been modified.
mxCellEditor.prototype.autoSize
Specifies if the textarea should be resized while the text is being edited.
mxCellEditor.prototype.selectText
Specifies if the text should be selected when editing starts.
mxCellEditor.prototype.emptyLabelText
Text to be displayed for empty labels.
mxCellEditor.prototype.escapeCancelsEditing
If true, pressing the escape key will stop editing and not accept the new value.
mxCellEditor.prototype.textNode
Reference to the label DOM node that has been hidden.
mxCellEditor.prototype.zIndex
Specifies the zIndex for the textarea.
mxCellEditor.prototype.minResize
Defines the minimum width and height to be used in resize.
mxCellEditor.prototype.resize = function()
Returns modified.
mxCellEditor.prototype.wordWrapPadding
Correction factor for word wrapping width.
mxCellEditor.prototype.blurEnabled
If focusLost should be called if textarea loses the focus.
mxCellEditor.prototype.focusLost = function()
Called if the textarea has lost focus.
mxCellEditor.prototype.initialValue
Holds the initial editing value to check if the current value was modified.
mxCellEditor.prototype.align
Holds the current temporary horizontal alignment for the cell style.
mxCellEditor.prototype.init = function ()
Creates the textarea and installs the event listeners.
mxCellEditor.prototype.applyValue = function(state,
value)
Called in stopEditing if cancel is false to invoke mxGraph.labelChanged.
mxCellEditor.prototype.stopEditing = function(cancel)
Stops the editor and applies the value if cancel is false.
mxGraph.prototype.labelChanged = function(cell,
value,
evt)
Sets the label of the specified cell to the given value using cellLabelChanged and fires mxEvent.LABEL_CHANGED while the transaction is in progress.
mxCellEditor.prototype.setAlign = function (align)
Sets the temporary horizontal alignment for the current editing session.
mxCellEditor.prototype.getInitialValue = function(state,
trigger)
Gets the initial editing value for the given cell.
mxCellEditor.prototype.getCurrentValue = function(state)
Returns the current editing value.
mxCellEditor.prototype.isCancelEditingKeyEvent = function(evt)
Returns true if escapeCancelsEditing is true and shift, control and meta are not pressed.
mxCellEditor.prototype.installListeners = function(elt)
Installs listeners for focus, change and standard key event handling.
mxCellEditor.prototype.isStopEditingEvent = function(evt)
Returns true if the given keydown event should stop cell editing.
mxCellEditor.prototype.isEventSource = function(evt)
Returns true if this editor is the source for the given native event.
mxCellEditor.prototype.getBackgroundColor = function(state)
Returns the background color for the in-place editor.
mxCellEditor.prototype.isLegacyEditor = function()
Returns true if max-width is not supported or if the SVG root element in in the graph does not have CSS position absolute.
mxCellEditor.prototype.startEditing = function(cell,
trigger)
Starts the editor for the given cell.
mxCellEditor.prototype.isSelectText = function()
Returns selectText.
mxCellEditor.prototype.clearSelection = function()
Clears the selection.
mxCellEditor.prototype.prepareTextarea = function()
Prepares the textarea for getting its value in stopEditing.
mxCellEditor.prototype.isHideLabel = function(state)
Returns true if the label should be hidden while the cell is being edited.
mxCellEditor.prototype.getMinimumSize = function(state)
Returns the minimum width and height for editing the given state.
mxCellEditor.prototype.getEditorBounds = function(state)
Returns the mxRectangle that defines the bounds of the editor.
Extends mxPoint to implement a 2-dimensional rectangle with double precision coordinates.
mxCellEditor.prototype.getEmptyLabelText = function (cell)
Returns the initial label value to be used of the label of the given cell is empty.
mxCellEditor.prototype.getEditingCell = function ()
Returns the cell that is currently being edited or null if no cell is being edited.
mxCellEditor.prototype.destroy = function ()
Destroys the editor and removes all associated resources.
mxGraph.prototype.invokesStopCellEditing
If true, when editing is to be stopped by way of selection changing, data in diagram changing or other means stopCellEditing is invoked, and changes are saved.
mxGraph.prototype.enterStopsCellEditing
If true, pressing the enter key without pressing control or shift will stop editing and accept the new value.
mxGraph.prototype.escapeEnabled
Specifies if mxKeyHandler should invoke escape when the escape key is pressed.
mxShape.prototype.getLabelBounds = function(rect)
Returns the mxRectangle for the label bounds of this shape, based on the given scaled and translated bounds of the shape.
Close