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 single asterisk to italic #92

Merged
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 @@ -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 @@ -312,7 +312,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