Skip to content

Commit

Permalink
chore: Small refactoring. #275
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Oct 2, 2023
1 parent 69672c1 commit 61c7394
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 85 deletions.
12 changes: 6 additions & 6 deletions lib/presentation/widgets/editor/emoji_picker_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const emojiPickerWidgetKey = Key('emojiPickerWidgetKey');
/// Shows an emoji picker when [offstageEmojiPicker] is `false`.
class OffstageEmojiPicker extends StatefulWidget {
/// `QuillController` controller that is passed so the controller document is changed when emojis are inserted.
final QuillController? quillController;
final QuillController? editorController;

/// Determines if the emoji picker is offstage or not.
final bool offstageEmojiPicker;

const OffstageEmojiPicker({required this.offstageEmojiPicker, this.quillController, super.key});
const OffstageEmojiPicker({required this.offstageEmojiPicker, this.editorController, super.key});

@override
State<OffstageEmojiPicker> createState() => _OffstageEmojiPickerState();
Expand Down Expand Up @@ -47,13 +47,13 @@ class _OffstageEmojiPickerState extends State<OffstageEmojiPicker> {
child: EmojiPicker(
key: emojiPickerWidgetKey,
onEmojiSelected: (category, emoji) {
if (widget.quillController != null) {
if (widget.editorController != null) {
// Get pointer selection and insert emoji there
final selection = widget.quillController?.selection;
widget.quillController?.document.insert(selection!.end, emoji.emoji);
final selection = widget.editorController?.selection;
widget.editorController?.document.insert(selection!.end, emoji.emoji);

// Update the pointer after the emoji we've just inserted
widget.quillController?.updateSelection(TextSelection.collapsed(offset: selection!.end + emoji.emoji.length), ChangeSource.REMOTE);
widget.editorController?.updateSelection(TextSelection.collapsed(offset: selection!.end + emoji.emoji.length), ChangeSource.REMOTE);
}
},
config: _buildEmojiPickerConfig(context),
Expand Down
151 changes: 72 additions & 79 deletions lib/presentation/widgets/editor/todo_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class DeltaTodoEditor extends StatefulWidget {
}

class DeltaTodoEditorState extends State<DeltaTodoEditor> {

/// Focus node used to obtain keyboard focus and events
final FocusNode _focusNode = FocusNode();

Expand All @@ -63,83 +62,6 @@ class DeltaTodoEditorState extends State<DeltaTodoEditor> {

@override
Widget build(BuildContext context) {
/// Returning scaffold with editor as body
return _buildEditor(context);
}

/// Callback called whenever the person taps on the text.
/// It will select nothing, then the word if another tap is detected
/// and then the whole text if another tap is detected (triple).
bool _onTripleClickSelection() {
final controller = widget.editorController;

// If nothing is selected, selection type is `none`
if (controller.selection.isCollapsed) {
_selectionType = _SelectionType.none;
}

// If nothing is selected, selection type becomes `word
if (_selectionType == _SelectionType.none) {
_selectionType = _SelectionType.word;
return false;
}

// If the word is selected, select all text
if (_selectionType == _SelectionType.word) {
final child = controller.document.queryChild(
controller.selection.baseOffset,
);
final offset = child.node?.documentOffset ?? 0;
final length = child.node?.length ?? 0;

final selection = TextSelection(
baseOffset: offset,
extentOffset: offset + length,
);

// Select all text and make next selection to `none`
controller.updateSelection(selection, ChangeSource.REMOTE);

_selectionType = _SelectionType.none;

return true;
}

return false;
}

/// Callback called whenever the person taps on the emoji button in the toolbar.
/// It shows/hides the emoji picker and focus/unfocusses the keyboard accordingly.
void _onEmojiButtonPressed(BuildContext context) {
final isEmojiPickerShown = !_offstageEmojiPickerOffstage;

// If emoji picker is being shown, we show the keyboard and hide the emoji picker.
if (isEmojiPickerShown) {
_focusNode.requestFocus();
setState(() {
_offstageEmojiPickerOffstage = true;
});
}

// Otherwise, we do the inverse.
else {
// Unfocusing when the person clicks away. This is to hide the keyboard.
// See https://flutterigniter.com/dismiss-keyboard-form-lose-focus/
// and https://www.youtube.com/watch?v=MKrEJtheGPk&t=40s&ab_channel=HeyFlutter%E2%80%A4com.
final currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
SystemChannels.textInput.invokeMethod('TextInput.hide');
//currentFocus.unfocus();
}

setState(() {
_offstageEmojiPickerOffstage = false;
});
}
}

/// Build the `flutter-quill` editor to be shown on screen.
Widget _buildEditor(BuildContext context) {
// Default editor (for mobile devices)
Widget quillEditor = QuillEditor(
controller: widget.editorController,
Expand Down Expand Up @@ -365,13 +287,84 @@ class DeltaTodoEditorState extends State<DeltaTodoEditor> {
Container(child: toolbar),
OffstageEmojiPicker(
offstageEmojiPicker: _offstageEmojiPickerOffstage,
quillController: widget.editorController,
editorController: widget.editorController,
),
],
),
);
}

/// Callback called whenever the person taps on the text.
/// It will select nothing, then the word if another tap is detected
/// and then the whole text if another tap is detected (triple).
bool _onTripleClickSelection() {
final controller = widget.editorController;

// If nothing is selected, selection type is `none`
if (controller.selection.isCollapsed) {
_selectionType = _SelectionType.none;
}

// If nothing is selected, selection type becomes `word
if (_selectionType == _SelectionType.none) {
_selectionType = _SelectionType.word;
return false;
}

// If the word is selected, select all text
if (_selectionType == _SelectionType.word) {
final child = controller.document.queryChild(
controller.selection.baseOffset,
);
final offset = child.node?.documentOffset ?? 0;
final length = child.node?.length ?? 0;

final selection = TextSelection(
baseOffset: offset,
extentOffset: offset + length,
);

// Select all text and make next selection to `none`
controller.updateSelection(selection, ChangeSource.REMOTE);

_selectionType = _SelectionType.none;

return true;
}

return false;
}

/// Callback called whenever the person taps on the emoji button in the toolbar.
/// It shows/hides the emoji picker and focus/unfocusses the keyboard accordingly.
void _onEmojiButtonPressed(BuildContext context) {
final isEmojiPickerShown = !_offstageEmojiPickerOffstage;

// If emoji picker is being shown, we show the keyboard and hide the emoji picker.
if (isEmojiPickerShown) {
_focusNode.requestFocus();
setState(() {
_offstageEmojiPickerOffstage = true;
});
}

// Otherwise, we do the inverse.
else {
// Unfocusing when the person clicks away. This is to hide the keyboard.
// See https://flutterigniter.com/dismiss-keyboard-form-lose-focus/
// and https://www.youtube.com/watch?v=MKrEJtheGPk&t=40s&ab_channel=HeyFlutter%E2%80%A4com.
final currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
SystemChannels.textInput.invokeMethod('TextInput.hide');
//currentFocus.unfocus();
}

setState(() {
_offstageEmojiPickerOffstage = false;
});
}
}

/// Renders the image picked by imagePicker from local file storage
/// You can also upload the picked image to any server (eg : AWS s3
/// or Firebase) and then return the uploaded image URL.
Expand Down

0 comments on commit 61c7394

Please sign in to comment.