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: enter to outdent checkbox/bullet lists #84

Merged
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 @@ -77,15 +77,27 @@ ShortcutEventHandler enterWithoutShiftInTextNodesHandler =
// insert a empty text node before.
if (selection.isCollapsed && selection.start.offset == 0) {
if (textNode.toPlainText().isEmpty && textNode.subtype != null) {
final path =
textNode.path.length > 1 ? [++textNode.path.first] : textNode.path;

final afterSelection = Selection.collapsed(
Position(path: textNode.path, offset: 0),
Position(path: path, offset: 0),
);
final transaction = editorState.transaction
..updateNode(textNode, {
BuiltInAttributeKey.subtype: null,
textNode.subtype!: null,
})
..afterSelection = afterSelection;

final transaction = editorState.transaction;
if (textNode.path.length > 1) {
transaction
..deleteNode(textNode)
..insertNode(path, textNode)
..afterSelection = afterSelection;
} else {
transaction
..updateNode(textNode, {
BuiltInAttributeKey.subtype: null,
textNode.subtype!: null,
})
..afterSelection = afterSelection;
}
editorState.apply(transaction);

final nextNode = textNode.next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,23 @@ void main() async {
testWidgets('Presses enter key in bulleted list', (tester) async {
await _testStyleNeedToBeCopy(tester, BuiltInAttributeKey.bulletedList);
});

testWidgets('Presses enter key in numbered list', (tester) async {
await _testStyleNeedToBeCopy(tester, BuiltInAttributeKey.numberList);
});

testWidgets('Presses enter key in checkbox styled text', (tester) async {
await _testStyleNeedToBeCopy(tester, BuiltInAttributeKey.checkbox);
});

testWidgets('Presses enter key in checkbox list indented', (tester) async {
await _testListOutdent(tester, BuiltInAttributeKey.checkbox);
});

testWidgets('Presses enter key in bulleted list indented', (tester) async {
await _testListOutdent(tester, BuiltInAttributeKey.bulletedList);
});

testWidgets('Presses enter key in quoted text', (tester) async {
await _testStyleNeedToBeCopy(tester, BuiltInAttributeKey.quote);
});
Expand Down Expand Up @@ -203,6 +214,50 @@ Future<void> _testStyleNeedToBeCopy(WidgetTester tester, String style) async {
}
}

Future<void> _testListOutdent(WidgetTester tester, String style) async {
const text = 'Welcome to Appflowy 😁';
final Attributes attributes = {
BuiltInAttributeKey.subtype: style,
style: true,
};

final editor = tester.editor
..insertTextNode(text)
..insertTextNode(text, attributes: attributes)
..insertTextNode('', attributes: attributes);

await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [2], startOffset: 0),
);
await editor.pressLogicKey(
key: LogicalKeyboardKey.tab,
);
expect(
editor.documentSelection,
Selection.single(path: [1, 0], startOffset: 0),
);

await editor.pressLogicKey(
key: LogicalKeyboardKey.enter,
);
expect(
editor.documentSelection,
Selection.single(path: [2], startOffset: 0),
);
expect(editor.nodeAtPath([2])?.subtype, style);

await editor.pressLogicKey(
key: LogicalKeyboardKey.enter,
);
expect(
editor.documentSelection,
Selection.single(path: [2], startOffset: 0),
);

expect(editor.nodeAtPath([2])?.subtype, null);
}

Future<void> _testMultipleSelection(
WidgetTester tester,
bool isBackwardSelection,
Expand Down