DocumentJsonCodec constructor

DocumentJsonCodec({
  1. Iterable<TextAttributeCodec<TextAttribute>> attributes = const [],
  2. Iterable<LineModifierCodec<LineModifier>> lineModifiers = const [],
  3. Iterable<ParagraphEmbedCodec<ParagraphEmbed>> embeds = const [],
})

Create a codec to convert a document to or from JSON with codecs for attributes, line modifiers and embeds that are supported for conversion.

Implementation

factory DocumentJsonCodec({
  Iterable<TextAttributeCodec> attributes = const [],
  Iterable<LineModifierCodec> lineModifiers = const [],
  Iterable<ParagraphEmbedCodec> embeds = const [],
}) {
  final decoder = _JsonDecoder(
    attributeDecoders:
        {for (final ac in attributes) ac.typeStr: ac.decode}.build(),
    lineModifierDecoders:
        {for (final lc in lineModifiers) lc.typeStr: lc.decode}.build(),
    embedDecoders: {for (final lc in embeds) lc.typeStr: lc.decode}.build(),
  );
  final encoder = _JsonEncoder(
    attributes: {for (final attr in attributes) attr.type: attr}.build(),
    lineModifiers: {for (final lc in lineModifiers) lc.type: lc}.build(),
    embeds: {for (final embed in embeds) embed.type: embed}.build(),
  );
  return DocumentJsonCodec._(decoder, encoder);
}