markdown preview: Break up list items into individual blocks (#10852)

Fixes a panic related to rendering checkboxes, see #10824.

Currently we are rendering a list into a single block, meaning the whole
block has to be rendered when it is visible on screen. This would lead
to performance problems when a single list block contained a lot of
items (especially if it contained checkboxes). This PR splits up list
items into separate blocks, meaning only the actual visible list items
on screen get rendered, instead of the whole list.
A nice side effect of the refactoring is, that you can actually click on
individual list items now:


https://github.com/zed-industries/zed/assets/53836821/5ef4200c-bd85-4e96-a8bf-e0c8b452f762

Release Notes:

- Improved rendering performance of list elements inside the markdown
preview

---------

Co-authored-by: Remco <djsmits12@gmail.com>
This commit is contained in:
Bennet Bo Fenner 2024-04-26 21:34:45 +02:00 committed by GitHub
parent 664f779eb4
commit 9329ef1d78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 286 additions and 279 deletions

View file

@ -15,6 +15,7 @@ use ui::prelude::*;
use workspace::item::{Item, ItemHandle, TabContentParams};
use workspace::{Pane, Workspace};
use crate::markdown_elements::ParsedMarkdownElement;
use crate::OpenPreviewToTheSide;
use crate::{
markdown_elements::ParsedMarkdown,
@ -180,9 +181,14 @@ impl MarkdownPreviewView {
let block = contents.children.get(ix).unwrap();
let rendered_block = render_markdown_block(block, &mut render_cx);
let should_apply_padding = Self::should_apply_padding_between(
block,
contents.children.get(ix + 1),
);
div()
.id(ix)
.pb_3()
.when(should_apply_padding, |this| this.pb_3())
.group("markdown-block")
.on_click(cx.listener(move |this, event: &ClickEvent, cx| {
if event.down.click_count == 2 {
@ -404,7 +410,7 @@ impl MarkdownPreviewView {
let Range { start, end } = block.source_range();
// Check if the cursor is between the last block and the current block
if last_end > cursor && cursor < start {
if last_end <= cursor && cursor < start {
block_index = Some(i.saturating_sub(1));
break;
}
@ -423,6 +429,13 @@ impl MarkdownPreviewView {
block_index.unwrap_or_default()
}
fn should_apply_padding_between(
current_block: &ParsedMarkdownElement,
next_block: Option<&ParsedMarkdownElement>,
) -> bool {
!(current_block.is_list_item() && next_block.map(|b| b.is_list_item()).unwrap_or(false))
}
}
impl FocusableView for MarkdownPreviewView {