markdown: Add debounce 500ms to update markdown to avoid render slow. (#953)

Ref #933
This commit is contained in:
Jason Lee 2025-06-12 19:15:23 +08:00 committed by GitHub
parent e29b51cc83
commit 1032881103
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,5 @@
use std::time::Instant;
use gpui::{
div, prelude::FluentBuilder as _, AnyElement, App, Element, ElementId, IntoElement,
ParentElement, SharedString, Styled, Window,
@ -57,6 +59,7 @@ pub struct MarkdownState {
raw: SharedString,
root: Option<Result<element::Node, SharedString>>,
style: TextViewStyle,
_last_parsed: Option<Instant>,
}
impl MarkdownState {
@ -67,8 +70,18 @@ impl MarkdownState {
return;
}
if let Some(last_parsed) = self._last_parsed {
if last_parsed.elapsed().as_millis() < 500 {
return;
}
}
self.raw = new_text;
// NOTE: About 100ms
// let measure = crate::Measure::new("parse_markdown");
self.root = Some(parse_markdown(&self.raw, &style, cx));
// measure.end();
self._last_parsed = Some(Instant::now());
self.style = style.clone();
}
}