Skip to content

ckeditor5-elements / ElementInlineCommand

Class: ElementInlineCommand ​

Toggles an inline element with text on the selection, e.g. a bold: formatting is an element, as the rest of the content, with its name and attributes, in the tree of elements. It works as CKEditor's bold command does for its mark on the text.

ts
editor.execute('bold'); // The selection.
editor.commands.get('bold').executeFor(element); // An element, e.g. of a context menu.
  • On selected text, it wraps the text with the element, in each block, and merges it with the elements next to it. When all the text is in the element already, it unwraps the elements: the whole ones, not only the selected part.
  • On the cursor, it inserts an empty element with the cursor in it. In the element, it moves the cursor out of it, at its start or end, or it unwraps it.
  • On an element (selected, or given), it unwraps the element it is in, or wraps it.

It is "on" (isOn) when the target is in the element: for selected text, when all of it is.

Extends ​

Constructors ​

Constructor ​

ts
new ElementInlineCommand(editor, elementName): ElementInlineCommand;

Parameters ​

ParameterType
editorEditor
elementNamestring

Returns ​

ElementInlineCommand

Inherited from ​

ElementCommand.constructor

Properties ​

elementAttributes ​

ts
elementAttributes: Record<string, unknown> = {};

The attributes the element must have to be the element of the command. The new elements get them.

Inherited from ​

ElementCommand.elementAttributes


elementName ​

ts
readonly elementName: string;

The name of the model element of the command.

Inherited from ​

ElementCommand.elementName


icon ​

ts
icon: string | undefined;

Observable

The icon (SVG string) of the command.

Inherited from ​

ElementCommand.icon


isForceDisabled ​

ts
readonly isForceDisabled: boolean;

Observable

Whether the command is disabled with forceDisabled() (and not enabled back with clearForceDisabled() for every id), so it is disabled for every target.

Inherited from ​

ElementCommand.isForceDisabled


isOn ​

ts
isOn: boolean;

Observable

Whether the target is in the element, or is one, for the commands that toggle it.

Inherited from ​

ElementCommand.isOn


isToggleable ​

ts
isToggleable: boolean = true;

Whether the command can be "on" (see _isOnFor), e.g. because it toggles something. Its buttons then tell assistive technology whether it is on: toolbar buttons with aria-pressed, menu items with aria-checked. Defaults to false; the commands of one element that toggle it set it, e.g. ElementInlineCommand.

Overrides ​

ElementCommand.isToggleable


title ​

ts
title: string;

Observable

The title of the command.

Inherited from ​

ElementCommand.title


tooltip ​

ts
tooltip: string | boolean;

Observable

The tooltip of the command. true uses the title.

Inherited from ​

ElementCommand.tooltip

Methods ​

_canHold() ​

ts
protected _canHold(
   position, 
   items, 
   name?
): boolean;

Whether an element (by default, the one of the command) can be at a position, and hold the items there: the check for wrapping them, or for renaming their parent.

Parameters ​

ParameterType
positionModelPosition
itemsIterable<ModelItem>
name?string

Returns ​

boolean

Inherited from ​

ElementCommand._canHold


_canUnwrap() ​

ts
protected _canUnwrap(element): boolean;

Whether the children of an element can be in its place: the check for unwrapping it.

Parameters ​

ParameterType
elementModelElement

Returns ​

boolean

Inherited from ​

ElementCommand._canUnwrap


_createElement() ​

ts
protected _createElement(writer): ModelElement;

Creates a new element of the command, with the elementAttributes.

Parameters ​

ParameterType
writerModelWriter

Returns ​

ModelElement

Inherited from ​

ElementCommand._createElement


_findElement() ​

ts
protected _findElement(elementOrPosition?): 
  | ModelElement
  | null;

Returns the closest element of the command that the target is, or is in, in its line of text.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

| ModelElement | null

Overrides ​

ElementCommand._findElement


_getAncestors() ​

ts
protected _getAncestors(elementOrPosition?): ModelElement[];

Returns the elements the target is, or is in (by default, the selection), the inner first: an element and its ancestors, or the parent of a position and its ancestors. Not the root.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ModelElement[]

Inherited from ​

ElementCommand._getAncestors


_getBlocks() ​

ts
protected _getBlocks(elementOrPosition?): ModelElement[];

Returns the blocks of the target, by default of the selection: the selected blocks, or the block-level element the target is, or is in (e.g. the paragraph with the cursor, a selected horizontal line, or a block quote).

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ModelElement[]

Inherited from ​

ElementCommand._getBlocks


_getElement() ​

ts
protected _getElement(elementOrPosition?): 
  | ModelElement
  | null;

Resolves the target (see _getElementOrPosition) to an element: a position becomes its parent element. Never returns a root.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

| ModelElement | null

Inherited from ​

ElementCommand._getElement


_getElementOrPosition() ​

ts
protected _getElementOrPosition(elementOrPosition?): ElementOrPosition | null;

Returns the given position or element, or resolves it from the current selection when absent. Returns null for a target that is not in the document (any more), e.g. a removed element, so commands ignore such targets.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ElementOrPosition | null

Inherited from ​

ElementCommand._getElementOrPosition


_getPosition() ​

ts
protected _getPosition(elementOrPosition?): 
  | ModelPosition
  | null;

Resolves the target (see _getElementOrPosition) to a position: an element becomes the position before it.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

| ModelPosition | null

Inherited from ​

ElementCommand._getPosition


_getSelectedRanges() ​

ts
protected _getSelectedRanges(elementOrPosition?): 
  | ModelRange[]
  | null;

Returns the ranges of the selected content, when the command is for the selection, and the selection is content, not a cursor nor an element.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

| ModelRange[] | null

Inherited from ​

ElementCommand._getSelectedRanges


_getStateFor() ​

ts
protected _getStateFor(elementOrPosition?): ElementOrPositionState;

Enabled when the target can be edited, and is in the element, or the element can be there. "On" when the target is in the element: for selected text, when all of it is. Both from one look at the selected text.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ElementOrPositionState

Overrides ​

ElementCommand._getStateFor


_isEnabledFor() ​

ts
protected _isEnabledFor(elementOrPosition?): boolean;

Whether the command is enabled for the target. By default: when there is a target and it can be edited.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

boolean

Inherited from ​

ElementCommand._isEnabledFor


_isOnFor() ​

ts
protected _isOnFor(elementOrPosition?): boolean;

Whether the command is "on" for the target. By default: never. Commands that override it should set isToggleable.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

boolean

Inherited from ​

ElementCommand._isOnFor


_keepSelection() ​

ts
protected _keepSelection(
   writer, 
   change, 
   ranges?
): void;

Changes the content of ranges, by default the ones of the selection, and selects the same content then: the ends of the ranges stick to the content, so they follow it into new elements and out of them, which the selection does not.

Parameters ​

ParameterType
writerModelWriter
change() => void
ranges?Iterable<ModelRange>

Returns ​

void

Inherited from ​

ElementCommand._keepSelection


_requireElement() ​

ts
protected _requireElement(elementOrPosition?): ModelElement;

Returns the element of the target (see _getElement), in execute() of a command that is enabled only for a target with an element: execute() runs only then, so there is one. Throws when there is none.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ModelElement

Inherited from ​

ElementCommand._requireElement


clearForceDisabled() ​

ts
clearForceDisabled(id): void;

Clears the disabling done by forceDisabled with this id. When no id is left, the command is refreshed, so isEnabled follows the selection again.

Parameters ​

ParameterType
idstring

Returns ​

void

Inherited from ​

ElementCommand.clearForceDisabled


execute() ​

ts
execute(elementOrPosition?): void;

Puts the target in the element, or out of it: by default, the selection, also selected text.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

void

Overrides ​

ElementCommand.execute


executeFor() ​

ts
executeFor(elementOrPosition, ...args): unknown;

Executes the command for the target, even when the command is disabled for the selection (execute() checks isEnabled, which follows the selection). Does nothing when the command is disabled for the target.

The target is the first argument of execute(); the other arguments are passed after it.

Parameters ​

ParameterType
elementOrPositionElementOrPosition
...argsunknown[]

Returns ​

unknown

Inherited from ​

ElementCommand.executeFor


forceDisabled() ​

ts
forceDisabled(id): void;

Disables the command for every target until it is enabled back with clearForceDisabled for every id it was disabled with. See isForceDisabled.

Parameters ​

ParameterType
idstring

Returns ​

void

Inherited from ​

ElementCommand.forceDisabled


getState() ​

ts
getState(elementOrPosition?): ElementOrPositionState;

Returns whether the command would be enabled and "on" for the target, without changing isEnabled. Commands that change the data are disabled in read-only mode, and all commands are disabled when isForceDisabled.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

ElementOrPositionState

Inherited from ​

ElementCommand.getState


isElement() ​

ts
isElement(node): node is ModelElement;

Whether a node is an element of the command: it is named elementName, and has the elementAttributes.

Parameters ​

ParameterType
node| ModelDocumentFragment | ModelItem | null

Returns ​

node is ModelElement

Inherited from ​

ElementCommand.isElement


refresh() ​

ts
refresh(elementOrPosition?): void;

Sets isEnabled for the target (by default, the selection). See _getStateFor.

Parameters ​

ParameterType
elementOrPosition?ElementOrPosition

Returns ​

void

Inherited from ​

ElementCommand.refresh