Delete Plugin
- Description
- Features
- Dependency
- Basic Deletion
- Programmatic Selection and Delete
- Disable Plugin
- Listen to Delete Events
- Main Delete Flow
- Block Merging (__moveContentInLeftSibling)
- List Handling (__defineRightLeftBox)
- Empty Block Cleanup (__addBrInEmptyBlock)
- Post-Delete Cleanup (__afterDeleteCommand)
- afterCommand.delete
- 1. Deleting Across Block Boundaries
- 2. Deleting in Lists
- 3. Deleting Next to Tables
- 4. Deleting All Content
- Configuration
- Notes
- Classes
- Interfaces
Provides custom implementation of the delete
command with intelligent content merging, empty block cleanup, and cursor positioning.
Description
This plugin overrides the browser's default delete
command behavior to provide smarter deletion when text is selected. It handles merging blocks, cleaning up empty elements, managing lists and tables, and positioning the cursor correctly after deletion.
Note: This plugin extends the backspace
plugin functionality and requires it as a dependency.
Features
- Smart Block Merging: Merges left and right blocks after deletion
- List Handling: Manages list items and empty list elements
- Table Support: Adds
<br>
to empty table cells after deletion - Cursor Positioning: Places cursor in correct editable position
- Empty Block Cleanup: Removes unnecessary empty blocks and
<br>
tags - Content Preservation: Moves content from right block to left block
- Full Editor Cleanup: Clears editor if all content is deleted (except tables/images)
delete
Deletes selected content with intelligent merging and cleanup.
Parameters:
command
:'delete'
editor.execCommand('delete');
Dependency
This plugin requires the backspace
plugin:
static override requires = ['backspace'];
If backspace
plugin is disabled, this plugin won't work.
Basic Deletion
const editor = Jodit.make('#editor');
editor.value = '<p>Hello</p><p>World</p>';
// Select all text
editor.execCommand('selectall');
// Delete selection
editor.execCommand('delete');
console.log(editor.value); // Empty or default paragraph
Programmatic Selection and Delete
const editor = Jodit.make('#editor');
editor.value = '<p>First paragraph</p><p>Second paragraph</p>';
// Select some text programmatically
const range = editor.s.createRange();
range.selectNodeContents(editor.editor.firstChild);
editor.s.selectRange(range);
// Delete selected content
editor.execCommand('delete');
Disable Plugin
const editor = Jodit.make('#editor', {
disablePlugins: ['delete']
// Falls back to browser default delete behavior
});
Listen to Delete Events
const editor = Jodit.make('#editor');
editor.e.on('afterCommand', (command) => {
if (command === 'delete') {
console.log('Delete command completed');
}
});
editor.e.on('beforeCommand', (command) => {
if (command === 'delete') {
console.log('Delete command starting');
// Can prevent deletion by returning false
}
});
Main Delete Flow
- Check Selection: If selection is collapsed (no text selected), do nothing
- Expand Selection: Call
editor.s.expandSelection()
to include full blocks - Delete Content: Use
range.deleteContents()
to remove selected content - Insert Marker: Place temporary fake text node at deletion point
- Find Siblings: Locate left and right sibling blocks
- Merge Content: Move content from right block into left block
- Position Cursor: Place cursor in correct editable position
- Cleanup: Remove empty blocks and add
<br>
where needed - Remove Marker: Delete fake text node
- Select Range: Set final cursor position
__moveContentInLeftSibling
)
Block Merging (// Example: Deleting across two paragraphs
// Before: <p>Hel|lo</p><p>Wor|ld</p>
// After: <p>Held</p>
1. Define left sibling (handles lists specially)
2. Check if siblings are blocks (not lists/tables)
3. Append fake marker to left block
4. Move all content from right block to left block
5. Remove empty right block
6. Clean up empty list items if needed
__defineRightLeftBox
)
List Handling (When left sibling is a list:
- Find the last list item (
<li>
) - If no leaf exists, create new
<li>
- Use that
<li>
as the merge target - This prevents merging into the
<ul>
/<ol>
directly
__addBrInEmptyBlock
)
Empty Block Cleanup (After deletion:
- If fake marker is alone in a block, add
<br>
for cursor positioning - If right sibling is a table, check first cell
- If cell is empty, add
<br>
to keep it editable
__afterDeleteCommand
)
Post-Delete Cleanup (Fires on afterCommand.delete
event:
- Remove extra
<br>
from current element's first child - Check if editor is now empty (no text, images, tables, etc.)
- If empty, clear
innerHTML
completely - Set cursor in editor root
- Remove the cursor marker node
afterCommand.delete
Fired after delete command completes.
editor.e.on('afterCommand.delete', () => {
console.log('Delete operation finished');
console.log('Editor content:', editor.value);
});
1. Deleting Across Block Boundaries
<!-- Before -->
<p>Hello|</p><p>|World</p>
<!-- After -->
<p>Hello</p>
2. Deleting in Lists
<!-- Before -->
<ul>
<li>Item 1|</li>
<li>|Item 2</li>
</ul>
<!-- After -->
<ul>
<li>Item 1</li>
</ul>
3. Deleting Next to Tables
<!-- Before -->
<p>Text|</p><table><tr><td>|Cell</td></tr></table>
<!-- After -->
<p>Text</p><table><tr><td><br></td></tr></table>
4. Deleting All Content
<!-- Before -->
<p>|All content|</p>
<!-- After -->
<!-- Editor cleared completely -->
Configuration
This plugin has no configuration options. To disable:
const editor = Jodit.make('#editor', {
disablePlugins: ['delete']
});
Notes
- Requires
backspace
plugin as dependency - Only works when selection is NOT collapsed (text is selected)
- Uses fake text node as temporary marker during processing
- Handles block-level merging intelligently
- Preserves table structure and adds
<br>
to empty cells - Clears entire editor if all meaningful content is deleted
- Does NOT trigger on collapsed selection (use backspace plugin for that)
- Prevents default browser delete behavior by returning
false