-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from LucasXu0/feat/to_do_list_charcter_shortcut
feat: to do list character shortcut
- Loading branch information
Showing
4 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
lib/src/editor/block_component/todo_list_block_component/todo_list_character_shortcut.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:appflowy_editor/src/editor/block_component/base_component/markdown_format_helper.dart'; | ||
|
||
/// Convert '[] ' to unchecked todo list | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatEmptyBracketsToUncheckedBox = | ||
CharacterShortcutEvent( | ||
key: 'format empty square brackets to unchecked todo list', | ||
character: ' ', | ||
handler: (editorState) async { | ||
return _formatSymbolToUncheckedBox( | ||
editorState: editorState, | ||
symbol: '[]', | ||
); | ||
}, | ||
); | ||
|
||
/// Convert '-[] ' to unchecked todo list | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatHyphenEmptyBracketsToUncheckedBox = | ||
CharacterShortcutEvent( | ||
key: 'format hyphen and empty square brackets to unchecked todo list', | ||
character: ' ', | ||
handler: (editorState) async { | ||
return _formatSymbolToUncheckedBox( | ||
editorState: editorState, | ||
symbol: '-[]', | ||
); | ||
}, | ||
); | ||
|
||
/// Convert '[x] ' to checked todo list | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatFilledBracketsToCheckedBox = | ||
CharacterShortcutEvent( | ||
key: 'format filled square brackets to checked todo list', | ||
character: ' ', | ||
handler: (editorState) async { | ||
return _formatSymbolToCheckedBox( | ||
editorState: editorState, | ||
symbol: '[x]', | ||
); | ||
}, | ||
); | ||
|
||
/// Convert '-[x] ' to checked todo list | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatHyphenFilledBracketsToCheckedBox = | ||
CharacterShortcutEvent( | ||
key: 'format hyphen and filled square brackets to checked todo list', | ||
character: ' ', | ||
handler: (editorState) async { | ||
return _formatSymbolToCheckedBox( | ||
editorState: editorState, | ||
symbol: '-[x]', | ||
); | ||
}, | ||
); | ||
|
||
Future<bool> _formatSymbolToUncheckedBox({ | ||
required EditorState editorState, | ||
required String symbol, | ||
}) async { | ||
assert(symbol == '[]' || symbol == '-[]'); | ||
|
||
return formatMarkdownSymbol( | ||
editorState, | ||
(node) => node.type != 'todo_list', | ||
(text, _) => text == symbol, | ||
(_, node, delta) => Node( | ||
type: 'todo_list', | ||
attributes: { | ||
'checked': false, | ||
'delta': delta.compose(Delta()..delete(symbol.length)).toJson(), | ||
}, | ||
), | ||
); | ||
} | ||
|
||
Future<bool> _formatSymbolToCheckedBox({ | ||
required EditorState editorState, | ||
required String symbol, | ||
}) async { | ||
assert(symbol == '[x]' || symbol == '-[x]'); | ||
|
||
return formatMarkdownSymbol( | ||
editorState, | ||
(node) => node.type != 'todo_list', | ||
(text, _) => text == symbol, | ||
(_, node, delta) => Node( | ||
type: 'todo_list', | ||
attributes: { | ||
'checked': true, | ||
'delta': delta.compose(Delta()..delete(symbol.length)).toJson(), | ||
}, | ||
), | ||
); | ||
} |
107 changes: 107 additions & 0 deletions
107
test/new/block_component/todo_list_block_component/todo_list_character_shortcut_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import '../../util/util.dart'; | ||
import '../test_character_shortcut.dart'; | ||
|
||
void main() async { | ||
group( | ||
'todo_list_character_shortcut.dart', | ||
() { | ||
setUpAll(() { | ||
if (kDebugMode) { | ||
activateLog(); | ||
} | ||
}); | ||
|
||
tearDownAll( | ||
() { | ||
if (kDebugMode) { | ||
deactivateLog(); | ||
} | ||
}, | ||
); | ||
|
||
// Before | ||
// []|Welcome to AppFlowy Editor 🔥! | ||
// After | ||
// [uncheckedbox]Welcome to AppFlowy Editor 🔥! | ||
test('[] to unchecked todo list ', () async { | ||
const text = 'Welcome to AppFlowy Editor 🔥!'; | ||
testFormatCharacterShortcut( | ||
formatEmptyBracketsToUncheckedBox, | ||
'[]', | ||
2, | ||
(result, before, after) { | ||
expect(result, true); | ||
expect(after.delta!.toPlainText(), text); | ||
expect(after.type, 'todo_list'); | ||
expect(after.attributes['checked'], false); | ||
}, | ||
text: text, | ||
); | ||
}); | ||
|
||
// Before | ||
// -[]|Welcome to AppFlowy Editor 🔥! | ||
// After | ||
// [uncheckedbox]Welcome to AppFlowy Editor 🔥! | ||
test('-[] to unchecked todo list ', () async { | ||
const text = 'Welcome to AppFlowy Editor 🔥!'; | ||
testFormatCharacterShortcut( | ||
formatHyphenEmptyBracketsToUncheckedBox, | ||
'-[]', | ||
3, | ||
(result, before, after) { | ||
expect(result, true); | ||
expect(after.delta!.toPlainText(), text); | ||
expect(after.type, 'todo_list'); | ||
expect(after.attributes['checked'], false); | ||
}, | ||
text: text, | ||
); | ||
}); | ||
|
||
// Before | ||
// [x]|Welcome to AppFlowy Editor 🔥! | ||
// After | ||
// [checkedbox]Welcome to AppFlowy Editor 🔥! | ||
test('[x] to checked todo list ', () async { | ||
const text = 'Welcome to AppFlowy Editor 🔥!'; | ||
testFormatCharacterShortcut( | ||
formatFilledBracketsToCheckedBox, | ||
'[x]', | ||
3, | ||
(result, before, after) { | ||
expect(result, true); | ||
expect(after.delta!.toPlainText(), text); | ||
expect(after.type, 'todo_list'); | ||
expect(after.attributes['checked'], true); | ||
}, | ||
text: text, | ||
); | ||
}); | ||
|
||
// Before | ||
// -[x]|Welcome to AppFlowy Editor 🔥! | ||
// After | ||
// [checkedbox]Welcome to AppFlowy Editor 🔥! | ||
test('-[x] to checked todo list ', () async { | ||
const text = 'Welcome to AppFlowy Editor 🔥!'; | ||
testFormatCharacterShortcut( | ||
formatHyphenFilledBracketsToCheckedBox, | ||
'-[x]', | ||
4, | ||
(result, before, after) { | ||
expect(result, true); | ||
expect(after.delta!.toPlainText(), text); | ||
expect(after.type, 'todo_list'); | ||
expect(after.attributes['checked'], true); | ||
}, | ||
text: text, | ||
); | ||
}); | ||
}, | ||
); | ||
} |