formatImage function

void formatImage({
  1. required TextEditingController controller,
  2. required TextSelection selection,
})

Implementation

void formatImage({
  required TextEditingController controller,
  required TextSelection selection,
}) {
  String altPlaceholder = 'Alt text';
  String linkPlaceholder = '/link/to/picture.jpg';

  var beforeText = controller.text.substring(0, selection.start);
  var afterText =
      controller.text.substring(selection.end, controller.text.length);
  controller.text = '$beforeText![$altPlaceholder]($linkPlaceholder)$afterText';

  controller.selection = TextSelection(
      baseOffset: selection.start + 4 + altPlaceholder.length,
      extentOffset:
          selection.start + altPlaceholder.length + 4 + linkPlaceholder.length);
}