Skip to content

Commit

Permalink
Fix: Allow backspace at start of document to remove block elements an…
Browse files Browse the repository at this point in the history
…d headers (#2198)
  • Loading branch information
agata authored Sep 9, 2024
1 parent 4e5ab58 commit f351a80
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/src/editor/raw_editor/raw_editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ class QuillRawEditorState extends EditorState
return _handleSpaceKey(event);
}

// Handles removing styles when pressing the backspace key at the beginning of the document.
if (event.logicalKey == LogicalKeyboardKey.backspace) {
return _handleBackspaceKey(event);
}

return KeyEventResult.ignored;
}

Expand Down Expand Up @@ -797,6 +802,49 @@ class QuillRawEditorState extends EditorState
return KeyEventResult.handled;
}

KeyEventResult _handleBackspaceKey(KeyEvent event) {
final child =
controller.document.queryChild(controller.selection.baseOffset);
if (child.node == null) {
return KeyEventResult.ignored;
}

if (child.node == null) {
return KeyEventResult.ignored;
}

// Only process when the backspace key is pressed at the beginning of the document.
if (controller.selection.baseOffset != 0) {
return KeyEventResult.ignored;
}

// Blocks and headers are targeted.
final node = child.node!;
final parent = node.parent;
if (parent == null && (parent is Block || parent is Root)) {
return KeyEventResult.ignored;
}

// Remove the parent's style.
// Block attributes are removed.
final style = parent!.style;
if (style.isNotEmpty) {
for (final attr in style.values) {
controller.formatSelection(Attribute.clone(attr, null));
}
}

// Remove the first child's style.
// Header attributes are removed.
final firstChildStyle = parent.first?.style;
if (firstChildStyle != null) {
for (final attr in firstChildStyle.values) {
controller.formatSelection(Attribute.clone(attr, null));
}
}
return KeyEventResult.handled;
}

KeyEventResult _handleTabKey(KeyEvent event) {
final child =
controller.document.queryChild(controller.selection.baseOffset);
Expand Down

0 comments on commit f351a80

Please sign in to comment.