singerdmx / flutter-quill

Rich text editor for Flutter
https://pub.dev/packages/flutter_quill
MIT License
2.6k stars 840 forks source link

Feat: support for customize `onKey` events #2345

Closed CatHood0 closed 2 weeks ago

CatHood0 commented 3 weeks ago

Description

[!NOTE] This PR has been moved to #2368.

A new parameter was added with the name onKeyPressed, which as its name indicates, calls this function every time a key on the keyboard is pressed.

Why was this done?

This is because previously the user never had the opportunity to prevent a key from executing its default function, because we handled all this internally.

It was decided to pass two parameters in this function, the KeyEvent and a Node that corresponds to the current node that is in selection.

Example

If we want to prevent all Embed Objects from being deleted, we would only have to create a function such that:

QuillEditor(
  controller: _controller,
  config: QuillEditorConfig(
     onKeyPressed: (event, node) {
       if (event.logicalKey == LogicalKeyboardKey.backspace && (node is Line || node is Block)) {
          final iterator = DeltaIterator(_controller.document.toDelta())
               ..skip(_controller.selection.baseOffset - 1);
          final cur = iterator.next();
          if (cur.data is! String && cur.data != null) {
             return KeyEventResult.handled;
          }
       }
       return null;
     },
  ),
)

Related Issues

EchoEllet commented 2 weeks ago

I will solve the conflicts of this PR and the others soon.

EchoEllet commented 2 weeks ago

Closing this PR since I don't have permission to push to the fork or merge changes to the master branch of this repo while fixing the conflicts. Submitted a new PR (#2368).