Skip to content

Text Direction Plugin

The TextDirectionPlugin provides comprehensive text direction support — block-level direction control, inline bidi isolation via <bdi> marks, smart middleware for direction inheritance and auto-detection, and keyboard shortcuts for both block and inline operations.

import { TextDirectionPlugin } from '@notectl/core/plugins/text-direction';
new TextDirectionPlugin()
new TextDirectionPlugin({ directableTypes: ['paragraph', 'heading'] })
interface TextDirectionConfig {
/** Block types that support direction. Default: paragraph, heading, title, subtitle, table_cell, blockquote, list_item */
readonly directableTypes: readonly string[];
/** Custom locale for toolbar labels and announcements. */
readonly locale?: TextDirectionLocale;
}
new TextDirectionPlugin({
directableTypes: ['paragraph', 'heading', 'blockquote'],
})

The plugin patches existing NodeSpecs for directable block types to add a dir attribute. This controls the overall text direction of the entire block.

AttributeTypeDefaultRenders As
dir'ltr' | 'rtl' | 'auto''auto'dir="rtl" on the block element
CommandDescriptionReturns
setDirectionLTRSet block direction to left-to-rightboolean
setDirectionRTLSet block direction to right-to-leftboolean
setDirectionAutoSet block direction to automaticboolean
toggleDirectionCycle direction: auto → rtl → ltr → autoboolean
editor.executeCommand('setDirectionRTL');
editor.executeCommand('toggleDirection');
ShortcutAction
Ctrl+Shift+D / Cmd+Shift+DToggle block direction (cycles auto → rtl → ltr → auto)

Windows/Linux only:

ShortcutAction
Ctrl + Left Shift keySet direction to LTR
Ctrl + Right Shift keySet direction to RTL

The plugin renders a Text Direction toolbar dropdown (block group) with LTR, RTL, and Auto options. The icon updates to reflect the current block’s direction.

The plugin registers a bdi mark for mixed-direction content within a single block (e.g., an English phrase inside Arabic text). The mark wraps selected text in a <bdi> element with an explicit dir attribute.

CommandDescriptionReturns
toggleBidiLTRApply inline LTR isolation to selectionboolean
toggleBidiRTLApply inline RTL isolation to selectionboolean
toggleBidiAutoApply inline auto isolation to selectionboolean
removeBidiRemove inline direction isolationboolean
toggleBidiIsolationToggle bidi: applies opposite of block direction, or removes if activeboolean
editor.executeCommand('toggleBidiLTR');
editor.executeCommand('removeBidi');
ShortcutAction
Ctrl+Shift+B / Cmd+Shift+BToggle inline bidi isolation

The plugin renders a second toolbar dropdown — Inline Direction (format group) — with LTR, RTL, Auto, and Remove options for bidi isolation. Only enabled when text is selected.

TypeHTML TagAttributesDescription
bdi<bdi>dirInline bidi isolation element

The plugin registers three transaction middleware handlers that work automatically:

When other plugins change a block’s type (e.g., paragraph → heading), the dir attribute is preserved so the user’s direction choice survives block type transformations.

When text is inserted or deleted in a block with dir="auto", the plugin automatically detects the text direction based on the first strong directional character using Unicode script detection. Supports 20+ RTL scripts including Arabic, Hebrew, Syriac, Thaana, N’Ko, and more.

When a new block is created (e.g., pressing Enter), the plugin inherits the dir attribute from the nearest sibling block. This provides a seamless writing experience in RTL documents.

  • Screen reader announcements for all direction changes via context.announce()
  • Keyboard-accessible shortcuts with platform-aware mappings (Mac vs. Windows/Linux)
  • ARIA-friendly toolbar dropdowns
  • Uses semantic HTML dir attribute and <bdi> element for proper assistive technology support

The plugin ships with translations for 9 languages:

LanguageLoader
EnglishBuilt-in (TEXT_DIRECTION_LOCALE_EN)
ArabicloadTextDirectionLocale('ar')
GermanloadTextDirectionLocale('de')
SpanishloadTextDirectionLocale('es')
FrenchloadTextDirectionLocale('fr')
HindiloadTextDirectionLocale('hi')
PortugueseloadTextDirectionLocale('pt')
RussianloadTextDirectionLocale('ru')
ChineseloadTextDirectionLocale('zh')

Custom locales can be provided via the locale config option. See the Internationalization guide for details.