executeHandleInsertText function

void executeHandleInsertText(
  1. String text,
  2. FluentDocument document
)

Inserts text by iterating through grapheme clusters instead of UTF-16 code units. This ensures emoji and other multi-code-unit characters are inserted as single units.

Implementation

void executeHandleInsertText(String text, FluentDocument document) {
  if (text.isEmpty) return;

  int i = 0;
  while (i < text.length) {
    int charCode = text.codeUnitAt(i);

    if (charCode >= 0xD800 && charCode <= 0xDBFF && i + 1 < text.length) {
      int nextCharCode = text.codeUnitAt(i + 1);
      if (nextCharCode >= 0xDC00 && nextCharCode <= 0xDFFF) {
        final emoji = text.substring(i, i + 2);
        executeHandleInsertCharacter(emoji, document);
        i += 2;
        continue;
      }
    }

    executeHandleInsertCharacter(text[i], document);
    i++;
  }
}