Skip to content

Commit

Permalink
content: combine multiple images in order
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpengi committed Dec 18, 2023
1 parent e8ae428 commit 35b5808
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ class CodeBlockSpanNode extends InlineContentNode {
}
}

class ImageNodes extends BlockContentNode {
const ImageNodes(this.images, {super.debugHtmlNode});

final List<ImageNode> images;

@override
List<DiagnosticsNode> debugDescribeChildren() {
return images.map((node) => node.toDiagnosticsNode()).toList();
}
}

class ImageNode extends BlockContentNode {
const ImageNode({super.debugHtmlNode, required this.srcUrl});

Expand Down Expand Up @@ -892,13 +903,24 @@ class _ZulipContentParser {

List<BlockContentNode> parseBlockContentList(dom.NodeList nodes) {
assert(_debugParserContext == _ParserContext.block);
final acceptedNodes = nodes.where((node) {
// We get a bunch of newline Text nodes between paragraphs.
// A browser seems to ignore these; let's do the same.
if (node is dom.Text && (node.text == '\n')) return false;
return true;
});
return acceptedNodes.map(parseBlockContent).toList(growable: false);
final List<BlockContentNode> blocks = [];
final List<ImageNode> imageNodes = [];
for (final node in nodes) {
if (node is dom.Text && (node.text == '\n')) continue;

final block = parseBlockContent(node);
if (block is! ImageNode) {
if (imageNodes.isNotEmpty) {
blocks.add(ImageNodes(imageNodes));
imageNodes.clear();
}
blocks.add(block);
} else {
imageNodes.add(block);
}
}
if (imageNodes.isNotEmpty) blocks.add(ImageNodes(imageNodes));
return blocks;
}

ZulipContent parse(String html) {
Expand Down
15 changes: 15 additions & 0 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class BlockContentList extends StatelessWidget {
return ListNodeWidget(node: node);
} else if (node is CodeBlockNode) {
return CodeBlock(node: node);
} else if (node is ImageNodes) {
return MessageImages(node: node);
} else if (node is ImageNode) {
return MessageImage(node: node);
} else if (node is UnimplementedBlockContentNode) {
Expand Down Expand Up @@ -249,6 +251,19 @@ class ListItemWidget extends StatelessWidget {
}
}

class MessageImages extends StatelessWidget {
const MessageImages({super.key, required this.node});

final ImageNodes node;

@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: node.images.map((imageNode) => MessageImage(node: imageNode)).toList());
}
}

class MessageImage extends StatelessWidget {
const MessageImage({super.key, required this.node});

Expand Down

0 comments on commit 35b5808

Please sign in to comment.