From 47311c1e4213a569a03bf95a8b5d4ba4659cd5e8 Mon Sep 17 00:00:00 2001 From: Mathias Mogensen Date: Sun, 23 Apr 2023 17:52:55 +0200 Subject: [PATCH] feat: support single asterisk to italic Closes: #1059 --- .../markdown_syntax_to_styled_text.dart | 34 ++++++++++++------- .../built_in_shortcut_events.dart | 7 +++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text.dart b/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text.dart index 8532fde24..850f918ea 100644 --- a/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text.dart +++ b/lib/src/service/internal_key_event_handlers/markdown_syntax_to_styled_text.dart @@ -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; @@ -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, }, diff --git a/lib/src/service/shortcut_event/built_in_shortcut_events.dart b/lib/src/service/shortcut_event/built_in_shortcut_events.dart index b9f52a19a..f659942bc 100644 --- a/lib/src/service/shortcut_event/built_in_shortcut_events.dart +++ b/lib/src/service/shortcut_event/built_in_shortcut_events.dart @@ -312,7 +312,12 @@ List 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',