Skip to content

Commit

Permalink
feat: support single asterisk to italic (AppFlowy-IO#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin committed Apr 24, 2023
1 parent 30f231a commit df4c222
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,17 @@ ShortcutEventHandler markdownLinkOrImageHandler = (editorState, event) {
return KeyEventResult.handled;
};

ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {
ShortcutEventHandler singleUnderscoreToItalicHandler = (editorState, event) {
return singleCharacterToItalicHandler(editorState, event, '_');
};

ShortcutEventHandler singleAsteriskToItalicHandler = (editorState, event) {
return singleCharacterToItalicHandler(editorState, event, '*');
};

KeyEventResult Function(EditorState, RawKeyEvent?, String)
singleCharacterToItalicHandler =
(EditorState editorState, RawKeyEvent? event, String character) {
// Obtain the selection and selected nodes of the current document through the 'selectionService'
// to determine whether the selection is collapsed and whether the selected node is a text node.
final selectionService = editorState.service.selectionService;
Expand All @@ -284,24 +294,24 @@ ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {

final textNode = textNodes.first;
final text = textNode.toPlainText();
// Determine if an 'underscore' already exists in the text node and only once.
final firstUnderscore = text.indexOf('_');
final lastUnderscore = text.lastIndexOf('_');
if (firstUnderscore == -1 ||
firstUnderscore != lastUnderscore ||
firstUnderscore == selection.start.offset - 1) {
// Determine if a 'character' already exists in the text node and only once.
final firstCharacter = text.indexOf(character);
final lastCharacter = text.lastIndexOf(character);
if (firstCharacter == -1 ||
firstCharacter != lastCharacter ||
firstCharacter == selection.start.offset - 1) {
return KeyEventResult.ignored;
}

// Delete the previous 'underscore',
// update the style of the text surrounded by the two underscores to 'italic',
// Delete the previous 'character',
// update the style of the text surrounded by the two characters to 'italic',
// and update the cursor position.
final transaction = editorState.transaction
..deleteText(textNode, firstUnderscore, 1)
..deleteText(textNode, firstCharacter, 1)
..formatText(
textNode,
firstUnderscore,
selection.end.offset - firstUnderscore - 1,
firstCharacter,
selection.end.offset - firstCharacter - 1,
{
BuiltInAttributeKey.italic: true,
},
Expand Down
7 changes: 6 additions & 1 deletion lib/src/service/shortcut_event/built_in_shortcut_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,12 @@ List<ShortcutEvent> builtInShortcutEvents = [
ShortcutEvent(
key: 'Underscore to italic',
character: '_',
handler: underscoreToItalicHandler,
handler: singleUnderscoreToItalicHandler,
),
ShortcutEvent(
key: 'Asterisk to italic',
character: '*',
handler: singleAsteriskToItalicHandler,
),
ShortcutEvent(
key: 'Double asterisk to bold',
Expand Down

0 comments on commit df4c222

Please sign in to comment.