executeHandleDelete function
Handles the Delete key.
Supported behaviors:
- If there's an active selection: delete the selection
- If cursor is at the end of a container: merge with the next node
- If cursor is on an image: remove the image
- If ctrl is pressed: delete the next word
- Otherwise: delete the next character in the fragment
Implementation
bool executeHandleDelete(FluentDocument document, {bool ctrl = false}) {
if (document.registry.dispatchDelete(document, ctrl: ctrl)) return true;
final cursor = document.cursor;
if (deleteSelectionIfExists(document)) return true;
if (ctrl) {
return deleteWordHelper(document, forward: true);
}
final currentNode = document.nodeById(cursor.anchorId);
if (currentNode is HorizontalRule) {
return removeNodeAndReposition(document, currentNode, forward: true);
}
final currentFrag = resolveFragmentFromCursor(currentNode, cursor.anchorOffset);
if (currentFrag == null) return false;
final container = document.findLogicalContainerCached(cursor.anchorId);
if (container == null) return false;
if (currentFrag is FluentImage) {
return removeNodeAndReposition(document, currentFrag, forward: true);
}
if (cursor.anchorOffset >= currentFrag.text.length) {
return _handleDeleteAtEnd(document, container, currentFrag);
}
FragmentOperations.deleteTextInFragment(currentFrag, cursor.anchorOffset, count: 1);
cursor.moveTo(currentFrag.id, cursor.anchorOffset);
notifyTextMutation(document, container, currentFrag.id, cursor.anchorOffset, -1);
document.updateContent();
return true;
}