Skip to content

Commit

Permalink
feat: single asterisk to italic
Browse files Browse the repository at this point in the history
  • Loading branch information
Xazin committed Mar 26, 2023
1 parent 16b64cf commit b6a2803
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ List<int> _findBackquoteIndexes(String text, TextNode textNode) {
/// If the word or phrase you want to denote as code includes one or more
/// backticks, you can escape it by enclosing the word or phrase in double
/// backticks (``).
ShortcutEventHandler backquoteToCodeHandler = (editorState, event) {
ShortcutEventHandler backtickToCodeHandler = (editorState, event) {
final selectionService = editorState.service.selectionService;
final selection = selectionService.currentSelection.value;
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
Expand Down Expand Up @@ -310,6 +310,45 @@ ShortcutEventHandler underscoreToItalicHandler = (editorState, event) {
return KeyEventResult.handled;
};

ShortcutEventHandler asteriskToItalicHandler = (editorState, event) {
final selectionService = editorState.service.selectionService;
final selection = selectionService.currentSelection.value;
final textNodes = selectionService.currentSelectedNodes.whereType<TextNode>();
if (selection == null || !selection.isSingle || textNodes.length != 1) {
return KeyEventResult.ignored;
}

final textNode = textNodes.first;
final text = textNode.toPlainText();
final firstAsterisk = text.indexOf('*');
final lastAsterisk = text.lastIndexOf('*');
if (firstAsterisk == -1 ||
firstAsterisk != lastAsterisk ||
firstAsterisk == selection.start.offset - 1) {
return KeyEventResult.ignored;
}

final transaction = editorState.transaction
..deleteText(textNode, firstAsterisk, 1)
..formatText(
textNode,
firstAsterisk,
selection.end.offset - firstAsterisk - 1,
{
BuiltInAttributeKey.italic: true,
},
)
..afterSelection = Selection.collapsed(
Position(
path: textNode.path,
offset: selection.end.offset - 1,
),
);
editorState.apply(transaction);

return KeyEventResult.handled;
};

ShortcutEventHandler doubleAsteriskToBoldHanlder = (editorState, event) {
final selectionService = editorState.service.selectionService;
final selection = selectionService.currentSelection.value;
Expand Down
17 changes: 11 additions & 6 deletions lib/src/service/shortcut_event/built_in_shortcut_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ List<ShortcutEvent> builtInShortcutEvents = [
handler: tabHandler,
),
ShortcutEvent(
key: 'Backquote to code',
command: 'backquote',
handler: backquoteToCodeHandler,
key: 'Backtick to code',
command: '`',
handler: backtickToCodeHandler,
),
ShortcutEvent(
key: 'Double tilde to strikethrough',
Expand All @@ -304,17 +304,22 @@ List<ShortcutEvent> builtInShortcutEvents = [
),
ShortcutEvent(
key: 'Underscore to italic',
command: 'shift+underscore',
command: '_',
handler: underscoreToItalicHandler,
),
ShortcutEvent(
key: 'Asterisk to italic',
command: '*',
handler: asteriskToItalicHandler,
),
ShortcutEvent(
key: 'Double asterisk to bold',
command: 'shift+digit 8',
command: '*',
handler: doubleAsteriskToBoldHanlder,
),
ShortcutEvent(
key: 'Double underscore to bold',
command: 'shift+underscore',
command: '_',
handler: doubleUnderscoreToBoldHanlder,
),
// https://github.com/flutter/flutter/issues/104944
Expand Down

0 comments on commit b6a2803

Please sign in to comment.