singerdmx / flutter-quill

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

Use the concept of '_ controller. onDelete' to set the value of '_ handleDelete'; Deleting UTF-16 emoticons doesn't work properly, what should I do #2331

Open zhaofeixaing opened 1 month ago

zhaofeixaing commented 1 month ago

Is there an existing issue for this?

The question

Use the concept of ' controller. onDelete' to set the value of ' handleDelete'; Deleting UTF-16 emoticons doesn't work properly, what should I do 新标签页 - Awesome Screenshot - Awesome Screenshot.webm 这个是我的代码

 @override
  void initState() {
    super.initState();
    _controller.document = widget.args.document;

    _controller.onDelete = _handleDelete;
    // textEditingValue
  }

  void _handleDelete(int cursorPosition, bool forward) {
    print('Delete at position: $cursorPosition, forward: $forward');
    final doc = _controller.document.toPlainText();
    final characters = doc.characters;

    if (forward) {
      if (cursorPosition < characters.length) {
        final newText = characters.take(cursorPosition).toString() + characters.skip(cursorPosition + 1).toString();
        _controller.replaceText(
          cursorPosition,
          1,
          '',
          TextSelection.collapsed(offset: cursorPosition),
        );
      }
    } else {
      if (cursorPosition > 0) {
        final newText = characters.take(cursorPosition - 1).toString() + characters.skip(cursorPosition).toString();
        _controller.replaceText(
          cursorPosition - 1,
          1,
          '',
          TextSelection.collapsed(offset: cursorPosition - 1),
        );
      }
    }
  }

Running error

════════ Exception caught by painting library ══════════════════════════════════ The following ArgumentError was thrown while building a TextSpan: Invalid argument(s): string is not well-formed UTF-16