add code preview

This commit is contained in:
2026-08-11 10:56:39 +07:00
parent 91a76a6f52
commit aabccdf099
5 changed files with 629 additions and 57 deletions
+20 -8
View File
@@ -18,7 +18,7 @@ use signed_state::{GitStore, RepoStore};
mod browser;
mod helpers;
use browser::{FileContent, MAX_PREVIEW_BYTES, MarkdownView};
use browser::{CodeView, FileContent, MAX_PREVIEW_BYTES, MarkdownView};
use helpers::{build_tree_items, is_markdown_path};
/// Detail view of a repository: header, stats, a file explorer with README
@@ -34,6 +34,8 @@ pub struct RepoDetailView {
worktree: Option<PathBuf>,
/// Markdown document currently in the preview pane (README or a file).
md: Option<MarkdownView>,
/// Code file currently in the preview pane.
code: Option<CodeView>,
readme_name: Option<SharedString>,
/// Currently previewed file (relative path) and its contents.
selected_file: Option<SharedString>,
@@ -63,6 +65,7 @@ impl RepoDetailView {
tree_state,
worktree: None,
md: None,
code: None,
readme_name: None,
selected_file: None,
files: HashMap::new(),
@@ -150,9 +153,11 @@ impl RepoDetailView {
Component::ParentDir | Component::RootDir | Component::Prefix(_)
)
});
let Some(worktree) = self.worktree.clone() else {
return;
};
if unsafe_path {
return;
}
@@ -186,13 +191,19 @@ impl RepoDetailView {
this.loading_files.remove(&path);
match content {
Ok(kind) => {
if let FileContent::Text(text) = &kind
&& is_markdown_path(&path)
{
let same = this.md.as_ref().map(|md| md.path.as_deref())
== Some(Some(path.as_str()));
if !same {
this.set_markdown(Some(path.clone().into()), text, cx);
if let FileContent::Text(text) = &kind {
if is_markdown_path(&path) {
let same = this.md.as_ref().map(|md| md.path.as_deref())
== Some(Some(path.as_str()));
if !same {
this.set_markdown(Some(path.clone().into()), text, cx);
}
} else {
let same = this.code.as_ref().map(|code| code.path.as_str())
== Some(path.as_str());
if !same {
this.set_code(path.clone().into(), text, cx);
}
}
}
this.files.insert(path, kind);
@@ -280,6 +291,7 @@ impl Render for RepoDetailView {
.pt_2()
.pb_4()
.w_full()
.items_start()
.justify_between()
.border_b_1()
.border_color(cx.theme().border)