apply method

  1. @override
Delta? apply(
  1. Delta document,
  2. int index,
  3. Object data
)
override

Applies heuristic rule to an insert operation on a document and returns resulting Delta.

Implementation

@override
Delta? apply(Delta document, int index, Object data) {
  if (data is! String) return null;

  // Special case: code blocks don't need a `space` to get formatted, we can
  // detect when the user types ``` (or ''') and apply the style immediately.
  if (data == '`' || data == "'") {
    final iter = DeltaIterator(document);
    final prefix = _getLinePrefix(iter, index);
    if (prefix == null || prefix.isEmpty) return null;
    final shortcut = '$prefix$data';
    if (shortcut == '```' || shortcut == "'''") {
      return _formatLine(iter, index, prefix, NotusAttribute.code);
    }
  }

  // Standard case: triggered by a space character after the shortcut.
  if (data != ' ') return null;

  final iter = DeltaIterator(document);
  final prefix = _getLinePrefix(iter, index);

  if (prefix == null || prefix.isEmpty) return null;

  final attribute = rules[prefix];
  if (attribute == null) return null;

  return _formatLine(iter, index, prefix, attribute);
}