singerdmx / flutter-quill

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

Supporting 8 digit hex color codes #2327

Open ambrose-gladwell-sonocent opened 1 month ago

ambrose-gladwell-sonocent commented 1 month ago

Is there an existing issue for this?

Flutter Quill version

10.8.4

Steps to reproduce

  1. Create a delta which inserts a background or text color with an 8 digit hexadecimal value which also holds the alpha channel.
  2. Run the app which loads in this delta to an editor.

Expected results

8 digit hex color codes should be supported and displayed correctly. Quill JS supports this so it would be nice if this library did also

Actual results

8 digit hex color code is not recognised mathced in the regex used in the hexToColor method in colour_button.dart instead it return Colors.black.

Additional Context

Potential fixed hexToColor code which support 3, 6 and 8 digit hex colour codes:

Color hexToColor(String? hexString) {
  if (hexString == null) {
    return Colors.black;
  }

  final hexRegex = RegExp(r'^[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8}$');

  hexString = hexString.replaceAll('#', '');
  if (!hexRegex.hasMatch(hexString)) {
    return Colors.black;
  }

  final buffer = StringBuffer();

  if (hexString.length == 3) {
    hexString = hexString.split('').map((char) => char * 2).join('');
  }

  if (hexString.length == 6) {
    buffer.write('ff');
  }

  buffer.write(hexString);

  return Color(int.tryParse(buffer.toString(), radix: 16) ?? 0xFF000000);
}