insertNewLineInType function

Future<bool> insertNewLineInType(
  1. EditorState editorState,
  2. String type, {
  3. Attributes attributes = const {},
})

Implementation

Future<bool> insertNewLineInType(
  EditorState editorState,
  String type, {
  Attributes attributes = const {},
}) async {
  final selection = editorState.selection;
  if (selection == null || !selection.isCollapsed) {
    return false;
  }

  final node = editorState.getNodeAtPath(selection.end.path);
  final delta = node?.delta;
  if (node?.type != type || delta == null) {
    return false;
  }

  if (selection.startIndex == 0 && delta.isEmpty) {
    // clear the style
    if (node != null && node.path.length > 1) {
      return KeyEventResult.ignored != outdentCommand.execute(editorState);
    }
    return KeyEventResult.ignored !=
        convertToParagraphCommand.execute(editorState);
  }

  await editorState.insertNewLine(
    nodeBuilder: (node) => node.copyWith(
      type: type,
      attributes: {
        ...node.attributes,
        ...attributes,
      },
    ),
  );
  return true;
}