doSerialization method

  1. @override
String doSerialization(
  1. Document document,
  2. ImageNode node, {
  3. NodeSelection? selection,
})
override

Implementation

@override
String doSerialization(
  Document document,
  ImageNode node, {
  NodeSelection? selection,
}) {
  if (selection != null) {
    if (selection is! UpstreamDownstreamNodeSelection) {
      // We don't know how to handle this selection type.
      return '';
    }
    if (selection.isCollapsed) {
      // This selection doesn't include the image - it's a collapsed selection
      // either on the upstream or downstream edge.
      return '';
    }
  }

  if (!useSizeNotation || (node.expectedBitmapSize?.width == null && node.expectedBitmapSize?.height == null)) {
    // We don't want to use size notation or the image doesn't have
    // size information. Use the regular syntax.
    return '![${node.altText}](${node.imageUrl})';
  }

  StringBuffer sizeNotation = StringBuffer();
  sizeNotation.write(' =');

  if (node.expectedBitmapSize?.width != null) {
    sizeNotation.write(node.expectedBitmapSize!.width!.toInt());
  }

  sizeNotation.write('x');

  if (node.expectedBitmapSize?.height != null) {
    sizeNotation.write(node.expectedBitmapSize!.height!.toInt());
  }

  return '![${node.altText}](${node.imageUrl}${sizeNotation.toString()})';
}