Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: creating document from slash menu leaves text behind #980

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final CharacterShortcutEvent slashCommand = CharacterShortcutEvent(
CharacterShortcutEvent customSlashCommand(
List<SelectionMenuItem> items, {
bool shouldInsertSlash = true,
bool deleteKeywordsByDefault = false,
bool singleColumn = true,
SelectionMenuStyle style = SelectionMenuStyle.light,
}) {
Expand All @@ -33,6 +34,7 @@ CharacterShortcutEvent customSlashCommand(
editorState,
items,
shouldInsertSlash: shouldInsertSlash,
deleteKeywordsByDefault: deleteKeywordsByDefault,
singleColumn: singleColumn,
style: style,
),
Expand All @@ -54,6 +56,7 @@ Future<bool> _showSlashMenu(
List<SelectionMenuItem> items, {
bool shouldInsertSlash = true,
bool singleColumn = true,
bool deleteKeywordsByDefault = false,
SelectionMenuStyle style = SelectionMenuStyle.light,
}) async {
if (PlatformExtension.isMobile) {
Expand Down Expand Up @@ -98,6 +101,7 @@ Future<bool> _showSlashMenu(
editorState: editorState,
selectionMenuItems: items,
deleteSlashByDefault: shouldInsertSlash,
deleteKeywordsByDefault: deleteKeywordsByDefault,
singleColumn: singleColumn,
style: style,
);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/editor/selection_menu/selection_menu_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SelectionMenu extends SelectionMenuService {
required this.editorState,
required this.selectionMenuItems,
this.deleteSlashByDefault = true,
this.deleteKeywordsByDefault = false,
this.style = SelectionMenuStyle.light,
this.itemCountFilter = 0,
this.singleColumn = false,
Expand All @@ -29,7 +30,9 @@ class SelectionMenu extends SelectionMenuService {
final EditorState editorState;
final List<SelectionMenuItem> selectionMenuItems;
final bool deleteSlashByDefault;
final bool deleteKeywordsByDefault;
final bool singleColumn;

@override
final SelectionMenuStyle style;

Expand Down Expand Up @@ -110,6 +113,7 @@ class SelectionMenu extends SelectionMenuService {
items: selectionMenuItems
..forEach((element) {
element.deleteSlash = deleteSlashByDefault;
element.deleteKeywords = deleteKeywordsByDefault;
element.onSelected = () {
dismiss();
};
Expand Down
33 changes: 21 additions & 12 deletions lib/src/editor/selection_menu/selection_menu_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
required this.keywords,
required SelectionMenuItemHandler handler,
this.nameBuilder,
this.deleteKeywords = false,
this.deleteSlash = true,
}) : _getName = getName {
this.handler = (editorState, menuService, context) {
if (deleteSlash) {
if (deleteSlash || deleteKeywords) {
_deleteSlash(editorState);
}

Expand Down Expand Up @@ -55,7 +57,8 @@

VoidCallback? onSelected;

bool deleteSlash = true;
bool deleteSlash;
bool deleteKeywords;

void _deleteSlash(EditorState editorState) {
final selection = editorState.selection;
Expand All @@ -68,14 +71,20 @@
return;
}
final end = selection.start.offset;
final lastSlashIndex =
delta.toPlainText().substring(0, end).lastIndexOf('/');
int deletedIndex = 0;

if (deleteKeywords) {
deletedIndex = 0;
} else if (deleteSlash) {
deletedIndex = delta.toPlainText().substring(0, end).lastIndexOf('/');
}

// delete all the texts after '/' along with '/'
final transaction = editorState.transaction
..deleteText(
node,
lastSlashIndex,
end - lastSlashIndex,
deletedIndex,
end - deletedIndex,
);

editorState.apply(transaction);
Expand Down Expand Up @@ -593,12 +602,12 @@
return;
}
widget.onSelectionUpdate();
final transaction = widget.editorState.transaction
..insertText(
node,
selection.end.offset,
text,
);
final transaction = widget.editorState.transaction;
transaction.insertText(

Check warning on line 606 in lib/src/editor/selection_menu/selection_menu_widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/selection_menu/selection_menu_widget.dart#L605-L606

Added lines #L605 - L606 were not covered by tests
node,
selection.end.offset,

Check warning on line 608 in lib/src/editor/selection_menu/selection_menu_widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/selection_menu/selection_menu_widget.dart#L608

Added line #L608 was not covered by tests
text,
);
widget.editorState.apply(transaction);
}
}
Loading