-
Notifications
You must be signed in to change notification settings - Fork 924
Feat: customizable character and space shortcut events #2228
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c723af1
Added character shortcut events class
0315763
Feat: support for character and space shortcut events
34cee79
Chore: added new gif video url
dc9c512
Merge branch 'master' into markdown_shortcuts
CatHood0 a62cd8b
Chore: improve and fix some parts of the new section in the README.md
CatHood0 568ccee
Merge from master
de374a2
Fix: typo in comment into the Shortcut event section
66b8a32
chore: add a message for validating an assert in handleFormatByWrappi…
EchoEllet 2b699d8
chore: fix analysis warning
EchoEllet 205fe5e
Chore: moved shortcut docs to its own file
a137486
Chore: changed editor without shortcut gif url
36a08f8
Chore: added description to asserts
3e9b56c
Chore: removed unnecessary default cases in format functions
e526011
Fix: characterShortcutEvents param in raw configs is showing as it is…
3d04ae6
Chore: dart format
295bd41
Chore: fixed some comments that references double chars instead singl…
9e7f38e
Fix: typo
4402a16
Chore: improved doc comments on format functions
513438c
Chore: dart format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,101 @@ | ||
# Shortcut events | ||
|
||
We will use a simple example to illustrate how to quickly add a `CharacterShortcutEvent` event. | ||
|
||
In this example, text that starts and ends with an asterisk ( * ) character will be rendered in italics for emphasis. So typing `*xxx*` will automatically be converted into _`xxx`_. | ||
|
||
Let's start with a empty document: | ||
|
||
```dart | ||
import 'package:flutter_quill/flutter_quill.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class AsteriskToItalicStyle extends StatelessWidget { | ||
const AsteriskToItalicStyle({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return QuillEditor( | ||
scrollController: <your_scrollController>, | ||
focusNode: <your_focusNode>, | ||
controller: <your_controller>, | ||
configurations: QuillEditorConfigurations( | ||
characterShortcutEvents: [], | ||
), | ||
); | ||
} | ||
} | ||
``` | ||
|
||
At this point, nothing magic will happen after typing `*xxx*`. | ||
|
||
<p align="center"> | ||
<img src="https://github.com/user-attachments/assets/c9ab15ec-2ada-4a84-96e8-55e6145e7925" width="800px" alt="Editor without shortcuts gif"> | ||
</p> | ||
|
||
To implement our shortcut event we will create a `CharacterShortcutEvent` instance to handle an asterisk input. | ||
|
||
We need to define key and character in a `CharacterShortcutEvent` object to customize hotkeys. We recommend using the description of your event as a key. For example, if the asterisk `*` is defined to make text italic, the key can be 'Asterisk to italic'. | ||
|
||
```dart | ||
import 'package:flutter_quill/flutter_quill.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
// [handleFormatByWrappingWithSingleCharacter] is a example function that contains | ||
// the necessary logic to replace asterisk characters and apply correctly the | ||
// style to the text around them | ||
|
||
enum SingleCharacterFormatStyle { | ||
code, | ||
italic, | ||
strikethrough, | ||
} | ||
|
||
CharacterShortcutEvent asteriskToItalicStyleEvent = CharacterShortcutEvent( | ||
key: 'Asterisk to italic', | ||
character: '*', | ||
handler: (QuillController controller) => handleFormatByWrappingWithSingleCharacter( | ||
controller: controller, | ||
character: '*', | ||
formatStyle: SingleCharacterFormatStyle.italic, | ||
), | ||
); | ||
``` | ||
|
||
Now our 'asterisk handler' function is done and the only task left is to inject it into the `QuillEditorConfigurations`. | ||
|
||
```dart | ||
import 'package:flutter_quill/flutter_quill.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class AsteriskToItalicStyle extends StatelessWidget { | ||
const AsteriskToItalicStyle({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return QuillEditor( | ||
scrollController: <your_scrollController>, | ||
focusNode: <your_focusNode>, | ||
controller: <your_controller>, | ||
configurations: QuillEditorConfigurations( | ||
characterShortcutEvents: [ | ||
asteriskToItalicStyleEvent, | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
CharacterShortcutEvent asteriskToItalicStyleEvent = CharacterShortcutEvent( | ||
key: 'Asterisk to italic', | ||
character: '*', | ||
handler: (QuillController controller) => handleFormatByWrappingWithSingleCharacter( | ||
controller: controller, | ||
character: '*', | ||
formatStyle: SingleCharacterFormatStyle.italic, | ||
), | ||
); | ||
``` | ||
<p align="center"> | ||
<img src="https://github.com/user-attachments/assets/35e74cbf-1bd8-462d-bb90-50d712012c90" width="800px" alt="Editor with shortcuts gif"> | ||
</p> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions
45
lib/src/editor/raw_editor/config/events/character_shortcuts_events.dart
This file contains hidden or 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,45 @@ | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../../../../../flutter_quill.dart'; | ||
|
||
typedef CharacterShortcutEventHandler = bool Function( | ||
QuillController controller); | ||
|
||
/// Defines the implementation of shortcut event based on character. | ||
@immutable | ||
class CharacterShortcutEvent extends Equatable { | ||
const CharacterShortcutEvent({ | ||
required this.key, | ||
required this.character, | ||
required this.handler, | ||
}) : assert(character.length == 1 && character != '\n', | ||
'character cannot be major than one char, and it must not be a new line'); | ||
|
||
final String key; | ||
final String character; | ||
final CharacterShortcutEventHandler handler; | ||
|
||
bool execute(QuillController controller) { | ||
return handler(controller); | ||
} | ||
|
||
CharacterShortcutEvent copyWith({ | ||
String? key, | ||
String? character, | ||
CharacterShortcutEventHandler? handler, | ||
}) { | ||
return CharacterShortcutEvent( | ||
key: key ?? this.key, | ||
character: character ?? this.character, | ||
handler: handler ?? this.handler, | ||
); | ||
} | ||
|
||
@override | ||
String toString() => | ||
'CharacterShortcutEvent(key: $key, character: $character, handler: $handler)'; | ||
|
||
@override | ||
List<Object?> get props => [key, character, handler]; | ||
} |
This file contains hidden or 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,9 @@ | ||
// event classes | ||
export 'character_shortcuts_events.dart'; | ||
export 'space_shortcut_events.dart'; | ||
// default implementation of the shortcuts | ||
export 'standard_char_shortcuts/block_shortcut_events_handlers.dart'; | ||
export 'standard_char_shortcuts/double_character_shortcut_events.dart'; | ||
export 'standard_char_shortcuts/single_character_shortcut_events.dart'; | ||
// all available shortcuts | ||
export 'standard_char_shortcuts/standard_shortcut_events.dart'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.