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

feat: support updating character of character shortcut event #187

Merged
merged 2 commits into from
Jun 12, 2023
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 @@ -19,14 +19,35 @@ class CharacterShortcutEvent {
/// Usually, uses the description as the key.
final String key;

/// The character to trigger the shortcut event.
///
/// It mus tbe a single character.
String character;

//// The handler to handle the shortcut event.
final CharacterShortcutEventHandler handler;

void updateCharacter(String newCharacter) {
assert(newCharacter.length == 1);
character = newCharacter;
}

Future<bool> execute(EditorState editorState) async {
return handler(editorState);
}

CharacterShortcutEvent copyWith({
String? key,
String? character,
CharacterShortcutEventHandler? handler,
}) {
return CharacterShortcutEvent(
key: key ?? this.key,
character: character ?? this.character,
handler: handler ?? this.handler,
);
}

@override
String toString() =>
'CharacterShortcutEvent(key: $key, character: $character, handler: $handler)';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
group('character shortcut event', () {
test('update character_shortcut_event\'s character', () async {
final event = CharacterShortcutEvent(
key: 'test',
character: 'a',
handler: (editorState) async => true,
);
expect(event.character, 'a');
event.updateCharacter('b');
expect(event.character, 'b');
});

test('copy character_shortcut_event', () async {
final event = CharacterShortcutEvent(
key: 'test',
character: 'a',
handler: (editorState) async => true,
);
final newEvent = event.copyWith(
character: 'b',
handler: (editorState) async => false,
);
expect(newEvent.character, 'b');
expect(await newEvent.execute(EditorState.blank()), false);
});
});
}