Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/src/editor/command/text_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ extension TextTransforms on EditorState {
);
}

static const whiteSpaceCharacter = " ";
// A function to check if the character before the selection is a white space.
// Returs true if the character is a white space, false otherwise.
bool iswhiteSpaceBeforeSelected(Selection selection) {
if (selection.end.offset == 0) {
return false;
}
return getTextInSelection(
selection.copyWith(
start: selection.end.copyWith(offset: selection.end.offset - 1),
end: selection.end.copyWith(offset: selection.end.offset),
),
).first == whiteSpaceCharacter;
}

/// Toggles the given attribute on or off for the selected text.
///
/// If the [Selection] is not passed in, use the current selection.
Expand All @@ -186,6 +201,29 @@ extension TextTransforms on EditorState {
return;
}
final nodes = getNodesInSelection(selection);
// If the selection is collapsed, insert a white space character at the current selection. and format it.
if (selection.length == 0) {
if (selection.end.offset == 0) {
insertTextAtCurrentSelection(whiteSpaceCharacter);
selection = selection.copyWith(
start: selection.end.copyWith(offset: selection.end.offset),
end: selection.end.copyWith(offset: selection.end.offset + 1),
);
} else {
if (iswhiteSpaceBeforeSelected(selection)) {
selection = selection.copyWith(
start: selection.end.copyWith(offset: selection.end.offset - 1),
end: selection.end.copyWith(offset: selection.end.offset),
);
} else {
insertTextAtCurrentSelection(whiteSpaceCharacter);
selection = selection.copyWith(
start: selection.end.copyWith(offset: selection.end.offset),
end: selection.end.copyWith(offset: selection.end.offset + 1),
);
}
}
}
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) => attributes[key] == true,
Expand Down