-
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 branch 'mobile' into feat/to_do_list_charcter_shortcut
- Loading branch information
Showing
26 changed files
with
737 additions
and
82 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
4 changes: 4 additions & 0 deletions
4
...itor_component/service/shortcuts/character_shortcut_events/character_shortcut_events.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,4 @@ | ||
export 'insert_newline.dart'; | ||
export 'slash_command.dart'; | ||
export 'format_by_wrapping_with_single_char/format_by_wrapping_with_single_char.dart'; | ||
export 'format_by_wrapping_with_double_char/format_by_wrapping_with_double_char.dart'; |
42 changes: 42 additions & 0 deletions
42
.../shortcuts/character_shortcut_events/format_by_wrapping_with_double_char/format_bold.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,42 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
|
||
const _asterisk = '*'; | ||
const _underscore = '_'; | ||
|
||
/// format the text surrounded by double asterisks to bold | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatDoubleAsterisksToBold = CharacterShortcutEvent( | ||
key: 'format the text surrounded by double asterisks to bold', | ||
character: _asterisk, | ||
handler: (editorState) async { | ||
return handleFormatByWrappingWithDoubleChar( | ||
editorState: editorState, | ||
char: _asterisk, | ||
formatStyle: DoubleCharacterFormatStyle.bold, | ||
); | ||
}, | ||
); | ||
|
||
/// format the text surrounded by double underscores to bold | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - mobile | ||
/// - web | ||
/// | ||
CharacterShortcutEvent formatDoubleUnderscoresToBold = CharacterShortcutEvent( | ||
key: 'format the text surrounded by double underscores to bold', | ||
character: _underscore, | ||
handler: (editorState) async { | ||
return handleFormatByWrappingWithDoubleChar( | ||
editorState: editorState, | ||
char: _underscore, | ||
formatStyle: DoubleCharacterFormatStyle.bold, | ||
); | ||
}, | ||
); |
6 changes: 6 additions & 0 deletions
6
...rtcut_events/format_by_wrapping_with_double_char/format_by_wrapping_with_double_char.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,6 @@ | ||
// Include all the shortcut(formatting) events triggered by wrapping text with double characters. | ||
// 1. double asterisk to bold -> **abc** | ||
// 2. double underscore to bold -> __abc__ | ||
|
||
export 'format_bold.dart'; | ||
export 'handle_format_by_wrapping_with_double_char.dart'; |
103 changes: 103 additions & 0 deletions
103
...vents/format_by_wrapping_with_double_char/handle_format_by_wrapping_with_double_char.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,103 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
|
||
// We currently have only one format style is triggered by double characters. | ||
// **abc** or __abc__ -> bold abc | ||
// If we have more in the future, we should add them in this enum and update the [style] variable in [handleDoubleCharactersFormat]. | ||
enum DoubleCharacterFormatStyle { | ||
bold, | ||
} | ||
|
||
bool handleFormatByWrappingWithDoubleChar({ | ||
// for demonstration purpose, the following comments use * to represent the character from the parameter [char]. | ||
required EditorState editorState, | ||
required String char, | ||
required DoubleCharacterFormatStyle formatStyle, | ||
}) { | ||
assert(char.length == 1); | ||
final selection = editorState.selection; | ||
// if the selection is not collapsed or the cursor is at the first three index range, we don't need to format it. | ||
// we should return false to let the IME handle it. | ||
if (selection == null || !selection.isCollapsed || selection.end.offset < 4) { | ||
return false; | ||
} | ||
|
||
final path = selection.end.path; | ||
final node = editorState.getNodeAtPath(path); | ||
final delta = node?.delta; | ||
// if the node doesn't contain the delta(which means it isn't a text), | ||
// we don't need to format it. | ||
if (node == null || delta == null) { | ||
return false; | ||
} | ||
|
||
final plainText = delta.toPlainText(); | ||
|
||
// The plainText should have at least 4 characters,like **a*. | ||
// The last char in the plainText should be *[char]. Otherwise, we don't need to format it. | ||
if (plainText.length < 4 || plainText[selection.end.offset - 1] != char) { | ||
return false; | ||
} | ||
|
||
// find all the index of *[char] | ||
final charIndexList = <int>[]; | ||
for (var i = 0; i < plainText.length; i++) { | ||
if (plainText[i] == char) { | ||
charIndexList.add(i); | ||
} | ||
} | ||
|
||
if (charIndexList.length < 3) { | ||
return false; | ||
} | ||
|
||
// for example: **abc* -> [0, 1, 5] | ||
// thirdLastCharIndex = 0, secondLastCharIndex = 1, lastCharIndex = 5 | ||
// make sure the third *[char] and second *[char] are connected | ||
// make sure the second *[char] and last *[char] are split by at least one character | ||
final thirdLastCharIndex = charIndexList[charIndexList.length - 3]; | ||
final secondLastCharIndex = charIndexList[charIndexList.length - 2]; | ||
final lastCharIndex = charIndexList[charIndexList.length - 1]; | ||
if (secondLastCharIndex != thirdLastCharIndex + 1 || | ||
lastCharIndex == secondLastCharIndex + 1) { | ||
return false; | ||
} | ||
|
||
// if all the conditions are met, we should format the text. | ||
// 1. delete all the *[char] | ||
// 2. update the style of the text surrounded by the double *[char] to [formatStyle] | ||
// 3. update the cursor position. | ||
final deletion = editorState.transaction | ||
..deleteText(node, lastCharIndex, 1) | ||
..deleteText(node, thirdLastCharIndex, 2); | ||
editorState.apply(deletion); | ||
|
||
// To minimize errors, retrieve the format style from an enum that is specific to double characters. | ||
final String style; | ||
|
||
switch (formatStyle) { | ||
case DoubleCharacterFormatStyle.bold: | ||
style = 'bold'; | ||
break; | ||
default: | ||
style = ''; | ||
assert(false, 'Invalid format style'); | ||
} | ||
|
||
final format = editorState.transaction | ||
..formatText( | ||
node, | ||
thirdLastCharIndex, | ||
selection.end.offset - thirdLastCharIndex - 3, | ||
{ | ||
style: true, | ||
}, | ||
) | ||
..afterSelection = Selection.collapsed( | ||
Position( | ||
path: path, | ||
offset: selection.end.offset - 3, | ||
), | ||
); | ||
editorState.apply(format); | ||
return true; | ||
} |
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
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
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
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
Oops, something went wrong.