Skip to content

Commit

Permalink
refactor: remove unnecessary command handler and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayaprakash-dev committed Jan 9, 2024
1 parent c2341e7 commit 2856714
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ final List<CommandShortcutEvent> standardCommandShortcutEvents = [
convertToParagraphCommand,
...tableCommands,
backspaceCommand,
deleteLeftCharacterCommand,
deleteLeftWordCommand,
deleteLeftSentenceCommand,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,7 @@ import 'package:flutter/material.dart';
///
final CommandShortcutEvent backspaceCommand = CommandShortcutEvent(
key: 'backspace',
command: 'backspace',
handler: _backspaceCommandHandler,
);

/// Windows / Linux / macOS : shift + backspace
/// Enables the deletion of a character to the left while holding the shift key
///
/// - support
/// - desktop
/// - web
///
final CommandShortcutEvent deleteLeftCharacterCommand = CommandShortcutEvent(
key: 'delete the character on the left',
command: 'shift+backspace',
command: 'backspace, shift+backspace',
handler: _backspaceCommandHandler,
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,271 +1,14 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../../infra/testable_editor.dart';
import '../../../util/util.dart';

// single | means the cursor
// double | means the selection
void main() async {
group('deleteLeftCharacterCommand - unit test', () {
group('deleteLeftCharacterCommand - collapsed selection', () {
const text = 'Welcome to AppFlowy Editor 🔥!';
// Before
// Welcome| to AppFlowy Editor 🔥!
// After
// | to AppFlowy Editor 🔥!
test('delete in collapsed selection when the index > 0', () async {
final document = Document.blank().addParagraph(
initialText: text,
);
final editorState = EditorState(document: document);

const index = 'Welcome'.length;
// Welcome| to AppFlowy Editor 🔥!
final selection = Selection.collapsed(
Position(path: [0], offset: index),
);
editorState.selection = selection;
for (var i = 0; i < index; i++) {
final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);
}

final after = editorState.getNodeAtPath([0])!;
expect(after.delta!.toPlainText(), text.substring(index));
});

// Before
// |Welcome to AppFlowy Editor 🔥!
// After
// |Welcome to AppFlowy Editor 🔥!
test(
'Delete the collapsed selection when the index is 0 and there is no previous node that contains a delta',
() async {
final document = Document.blank().addParagraph(
initialText: text,
);
final editorState = EditorState(document: document);

// |Welcome to AppFlowy Editor 🔥!
final selection = Selection.collapsed(
Position(path: [0], offset: 0),
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.ignored);

final after = editorState.getNodeAtPath([0])!;
expect(after.delta!.toPlainText(), text);
expect(editorState.selection, selection);
});

// Before
// Welcome to AppFlowy Editor 🔥!
// |Welcome to AppFlowy Editor 🔥!
// After
// Welcome to AppFlowy Editor 🔥!|Welcome to AppFlowy Editor 🔥!
test('''Delete the collapsed selection when the index is 0
and there is a previous node that contains a delta
and the previous node is in the same level with the current node''',
() async {
final document = Document.blank().addParagraphs(
2,
initialText: text,
);
final editorState = EditorState(document: document);

// Welcome to AppFlowy Editor 🔥!
// |Welcome to AppFlowy Editor 🔥!
final selection = Selection.collapsed(
Position(path: [1], offset: 0),
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);

// the second node should be deleted.
expect(editorState.getNodeAtPath([1]), null);

// the first node should be combined with the second node.
final after = editorState.getNodeAtPath([0])!;
expect(after.delta!.toPlainText(), text * 2);
expect(
editorState.selection,
Selection.collapsed(
Position(path: [0], offset: text.length),
),
);
});

// Before
// Welcome to AppFlowy Editor 🔥!
// |Welcome to AppFlowy Editor 🔥!
// After
// Welcome to AppFlowy Editor 🔥!
// |Welcome to AppFlowy Editor 🔥!
test('''Delete the collapsed selection when the index is 0
and there is a previous node that contains a delta
and the previous node is the parent of the current node''', () async {
final document = Document.blank().addParagraph(
initialText: text,
decorator: (index, node) => node.addParagraph(
initialText: text,
),
);
final editorState = EditorState(document: document);

// Welcome to AppFlowy Editor 🔥!
// |Welcome to AppFlowy Editor 🔥!
final selection = Selection.collapsed(
Position(path: [0, 0], offset: 0),
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);

// the second node should be moved to the same level as it's parent.
expect(editorState.getNodeAtPath([0, 1]), null);
final after = editorState.getNodeAtPath([1])!;
expect(after.delta!.toPlainText(), text);
expect(
editorState.selection,
Selection.collapsed(
Position(path: [1], offset: 0),
),
);
});
});

group('deleteLeftCharacterCommand - not collapsed selection', () {
const text = 'Welcome to AppFlowy Editor 🔥!';

// Before
// |Welcome to AppFlowy |Editor 🔥!
// After
// |Editor 🔥!
test('Delete in the not collapsed selection that is single', () async {
final document = Document.blank().addParagraph(
initialText: text,
);
final editorState = EditorState(document: document);

// |Welcome to AppFlowy |Editor 🔥!
const deleteText = 'Welcome to AppFlowy ';
final selection = Selection.single(
path: [0],
startOffset: 0,
endOffset: deleteText.length,
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);

final after = editorState.getNodeAtPath([0])!;
expect(
after.delta!.toPlainText(),
text.substring(deleteText.length),
);
expect(
editorState.selection,
selection.collapse(atStart: true),
);
});

// Before
// Welcome| to AppFlowy Editor 🔥!
// Welcome| to AppFlowy Editor 🔥!
// After
// Welcome| to AppFlowy Editor 🔥!
test('Delete in the not collapsed selection that is not single',
() async {
final document = Document.blank().addParagraphs(
2,
initialText: text,
);
final editorState = EditorState(document: document);

const index = 'Welcome'.length;
// Welcome| to AppFlowy Editor 🔥!
// Welcome| to AppFlowy Editor 🔥!
final selection = Selection(
start: Position(path: [0], offset: index),
end: Position(path: [1], offset: index),
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);

final after = editorState.getNodeAtPath([0])!;
expect(after.delta!.toPlainText(), text);
expect(editorState.getNodeAtPath([1]), null);
});

// Before
// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
// After
// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
test(
'Delete in the not collapsed selection that is not single and not flatted',
() async {
final document = Document.blank()
.addParagraph(
initialText: text,
) // Welcome to AppFlowy Editor 🔥!
.addParagraph(
initialText: text,
decorator: (index, node) => node.addParagraph(
initialText: text,
decorator: (index, node) => node.addParagraph(
initialText: text,
),
),
);
assert(document.nodeAtPath([1, 0, 0]) != null, true);
final editorState = EditorState(document: document);

// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
const index = 'Welcome'.length;
final selection = Selection(
start: Position(path: [0], offset: index),
end: Position(path: [1, 0], offset: index),
);
editorState.selection = selection;

final result = deleteLeftCharacterCommand.execute(editorState);
expect(result, KeyEventResult.handled);

// Welcome| to AppFlowy Editor 🔥!
// Welcome to AppFlowy Editor 🔥!
expect(
editorState.selection,
selection.collapse(atStart: true),
);

// the [1] node should be deleted.
expect(editorState.getNodeAtPath([1]), null);

expect(editorState.getNodeAtPath([0])?.delta?.toPlainText(), text);
expect(editorState.getNodeAtPath([0, 0])?.delta?.toPlainText(), text);
});
});
});

group('deleteLeftCharacterCommand - widget test', () {
group('deletion of a character while holding down shift key - widget test',
() {
const text = 'Welcome to AppFlowy Editor 🔥!';
const List<LogicalKeyboardKey> keys = [
LogicalKeyboardKey.shift,
Expand Down

0 comments on commit 2856714

Please sign in to comment.