setTextDirection command

The setTextDirection command sets the text direction for nodes at the current selection or at a specified position. This is useful for controlling the direction of right-to-left (RTL) languages like Arabic and Hebrew, or for bidirectional text content.

See also: unsetTextDirection

Parameters

direction

The text direction to set. Can be 'ltr' (left-to-right), 'rtl' (right-to-left), or 'auto' (automatically detect based on content).

position

Optional. The position or range where the direction should be applied. If not provided, the command will use the current selection.

Can be:

  • A number representing a specific position
  • An object with from and to properties representing a range

Examples

// Set RTL direction on current selection
editor.commands.setTextDirection('rtl')

// Set LTR direction on current selection
editor.commands.setTextDirection('ltr')

// Set auto direction on current selection
editor.commands.setTextDirection('auto')

// Set direction on a specific position
editor.commands.setTextDirection('rtl', 5)

// Set direction on a specific range
editor.commands.setTextDirection('ltr', { from: 0, to: 10 })

// Chain with other commands
editor.chain().focus().setTextDirection('rtl').run()