Indent Plugin
Provides indent and outdent functionality for the Jodit editor. This plugin adds toolbar buttons and keyboard shortcuts to increase or decrease the indentation of block elements.
Features
- Indent (increase indentation) button
- Outdent (decrease indentation) button
- Keyboard shortcuts (Ctrl/Cmd+] and Ctrl/Cmd+[)
- RTL (right-to-left) text direction support
- Smart handling of BR mode and block mode
- Automatic block wrapping when needed
- Prevents negative indentation
- Table cell padding support
- Preserves selection across multiple blocks
indentMargin
Type: number
Default: 10
The number of pixels to use for indenting the current line or block.
Example:
const editor = Jodit.make('#editor', {
indentMargin: 30
});
Basic Usage
const editor = Jodit.make('#editor');
// Use indent/outdent buttons in toolbar or keyboard shortcuts
Custom Indent Margin
const editor = Jodit.make('#editor', {
indentMargin: 20
});
Programmatic Usage
const editor = Jodit.make('#editor');
// Indent current block
editor.execCommand('indent');
// Outdent current block
editor.execCommand('outdent');
Keyboard Shortcuts
- Indent:
Ctrl+]
(Windows/Linux) orCmd+]
(Mac) - Outdent:
Ctrl+[
(Windows/Linux) orCmd+[
(Mac)
With BR Mode
const editor = Jodit.make('#editor', {
enter: 'br',
indentMargin: 15
});
Plugin Initialization
- Button Registration: Registers
indent
andoutdent
buttons inindent
toolbar group - Command Registration: Registers
indent
andoutdent
commands with hotkeys - Icon Loading: Loads SVG icons for both buttons
Indent/Outdent Command Execution
When indent or outdent command is executed:
- Mode Detection: Checks if editor is in BR mode (
enter: 'br'
) - Selection Handling:
- If collapsed selection in BR mode: Wraps content in block
- Otherwise: Processes each selection range
- Block Discovery: Finds nearest block element containing the cursor
- Block Wrapping: If no block exists, wraps inline content in block
- Indentation Application: Adjusts margin/padding value
- Duplicate Prevention: Tracks processed elements to avoid duplicate processing
Direction and Property Selection
The plugin automatically selects the correct CSS property based on:
- Text Direction:
marginLeft
for LTR,marginRight
for RTL - Element Type:
paddingLeft
/paddingRight
for table cells,marginLeft
/marginRight
for other blocks
Selection logic:
`${Dom.isCell(box) ? 'padding' : 'margin'}${direction === 'rtl' ? 'Right' : 'Left'}`
Indentation Calculation
For each block element:
- Current Value: Reads existing margin/padding value
- Calculate New Value:
- Indent:
currentValue + indentMargin
- Outdent:
currentValue - indentMargin
- Indent:
- Apply Value: Sets new value as inline style
- Cleanup: Removes empty
style
attribute if value becomes 0
BR Mode Handling
In BR mode with collapsed selection:
- Inline Wrap: Uses
Dom.wrapNextInline()
to wrap content - Block Creation: Creates block using
enterBlock
configuration - Apply Indent: Applies indentation to created block
Block Mode Handling
In block mode (enter: 'p'
or enter: 'div'
):
- Find/Create Block: Finds existing block or wraps content
- Apply Indent: Applies indentation to block
- Multiple Selections: Processes each selection independently
Outdent Button State
The outdent button is automatically disabled when:
- Current block has no indentation
- Indentation value is 0 or negative
- No current block element exists
indent
Increases indentation of current block element(s).
Hotkeys: Ctrl+]
, Cmd+]
Example:
editor.execCommand('indent');
outdent
Decreases indentation of current block element(s).
Hotkeys: Ctrl+[
, Cmd+[
Example:
editor.execCommand('outdent');
Edge Cases
- No Block Element: Automatically wraps inline content in appropriate block before indenting
- BR Mode: Uses
enterBlock
for wrapping instead ofenter
- Multiple Selections: Each selection range is processed independently
- Duplicate Processing: Tracks processed elements to prevent double-processing
- Table Cells: Uses
padding
instead ofmargin
for table cell indentation - RTL Direction: Automatically uses
marginRight
/paddingRight
for right-to-left text - Negative Values: Prevents negative indentation by setting minimum value to 0
- Empty Style: Removes
style
attribute if it becomes empty after setting margin to 0 - Selection Preservation: Saves and restores selection for each processed range
Notes
- Indentation is applied as inline styles on block elements
- The plugin works with both LTR and RTL text directions
- Table cells receive padding instead of margin for proper rendering
- Multiple selected blocks are all indented/outdented together
- The plugin automatically creates block wrappers when needed
- Outdent button shows disabled state when no indentation exists
- Keyboard shortcuts work in both Windows/Linux and Mac
- The plugin respects
enter
andenterBlock
configuration options - Selection is preserved across indent/outdent operations
- Both buttons appear in the
indent
toolbar group
indent
indent(editor
): void
Indents the line containing the selection or insertion point.
Parameters
Name | Type |
---|---|
editor |
IJodit |
Returns
void
Defined in
jodit/src/plugins/indent/indent.ts:55