handleShortcut method

Future<void> handleShortcut(
  1. InputShortcut? shortcut
)

Implementation

Future<void> handleShortcut(InputShortcut? shortcut) async {
  final selection = widget.controller.selection;
  final plainText = getTextEditingValue().text;
  if (shortcut == InputShortcut.COPY) {
    if (!selection.isCollapsed) {
      await Clipboard.setData(
          ClipboardData(text: selection.textInside(plainText)));
    }
    return;
  }
  if (shortcut == InputShortcut.UNDO) {
    if (widget.controller.hasUndo) {
      widget.controller.undo();
    }
    return;
  }
  if (shortcut == InputShortcut.REDO) {
    if (widget.controller.hasRedo) {
      widget.controller.redo();
    }
    return;
  }
  if (shortcut == InputShortcut.CUT && !widget.readOnly) {
    if (!selection.isCollapsed) {
      final data = selection.textInside(plainText);
      await Clipboard.setData(ClipboardData(text: data));

      widget.controller.replaceText(
        selection.start,
        data.length,
        '',
        TextSelection.collapsed(offset: selection.start),
      );

      setTextEditingValue(TextEditingValue(
        text:
            selection.textBefore(plainText) + selection.textAfter(plainText),
        selection: TextSelection.collapsed(offset: selection.start),
      ));
    }
    return;
  }
  if (shortcut == InputShortcut.PASTE && !widget.readOnly) {
    final data = await Clipboard.getData(Clipboard.kTextPlain);
    if (data != null) {
      widget.controller.replaceText(
        selection.start,
        selection.end - selection.start,
        data.text,
        TextSelection.collapsed(offset: selection.start + data.text!.length),
      );
    }
    return;
  }
  if (shortcut == InputShortcut.SELECT_ALL &&
      widget.enableInteractiveSelection) {
    widget.controller.updateSelection(
        selection.copyWith(
          baseOffset: 0,
          extentOffset: getTextEditingValue().text.length,
        ),
        ChangeSource.REMOTE);
    return;
  }
}