NodeV0.fromJson constructor

NodeV0.fromJson(
  1. Map<String, Object> json
)

Implementation

factory NodeV0.fromJson(Map<String, Object> json) {
  assert(json['type'] is String);

  final jType = json['type'] as String;
  final jChildren = json['children'] as List?;
  final jAttributes = json['attributes'] != null
      ? Attributes.from(json['attributes'] as Map)
      : Attributes.from({});

  final children = LinkedList<NodeV0>();
  if (jChildren != null) {
    children.addAll(
      jChildren.map(
        (jChild) => NodeV0.fromJson(
          Map<String, Object>.from(jChild),
        ),
      ),
    );
  }

  NodeV0 node;

  if (jType == 'text') {
    final jDelta = json['delta'] as List<dynamic>?;
    final delta = jDelta == null ? Delta() : Delta.fromJson(jDelta);
    node = TextNodeV0(
      children: children,
      attributes: jAttributes,
      delta: delta,
    );
  } else {
    node = NodeV0(
      type: jType,
      children: children,
      attributes: jAttributes,
    );
  }

  for (final child in children) {
    child.parent = node;
  }

  return node;
}