Skip to content

Commit

Permalink
text_view: Fix table alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Feb 26, 2025
1 parent 6a125b3 commit 95fcdbc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/story/examples/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ Here is a link to [Google](https://www.google.com), and another to [Rust](https:

### Table

| Header 1 | Header 2 | Header 3 | Header 4 |
| -------- | -------- | ------------------------------------ | -------- |
| Cell 0 | Cell 1 | This is a long cell with line break. | Cell 3 |
| Row 2 | Row 2 | Row 2<br>[Link](https://github.com) | Row 2 |
| Row 3 | **Bold** | Row 3 | Row 3 |
| Header 1 | Centered | Header 3 | Align Right |
| -------- | :------: | ------------------------------------ | ----------: |
| Cell 0 | Cell 1 | This is a long cell with line break. | Cell 3 |
| Row 2 | Row 2 | Row 2<br>[Link](https://github.com) | Row 2 |
| Row 3 | **Bold** | Row 3 | Row 3 |

#### Lists

Expand Down
35 changes: 35 additions & 0 deletions crates/ui/src/text/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gpui::{
IntoElement, Length, ObjectFit, ParentElement, RenderOnce, SharedString, SharedUri, Styled,
StyledImage as _, StyledText, Window,
};
use markdown::mdast;

use crate::{h_flex, v_flex, ActiveTheme as _, Icon, IconName};

Expand Down Expand Up @@ -99,6 +100,32 @@ impl From<String> for Paragraph {
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Table {
pub children: Vec<TableRow>,
pub column_aligns: Vec<TableColumnAlign>,
}

impl Table {
pub(crate) fn column_align(&self, index: usize) -> TableColumnAlign {
self.column_aligns.get(index).copied().unwrap_or_default()
}
}

#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub enum TableColumnAlign {
#[default]
Left,
Center,
Right,
}

impl From<mdast::AlignKind> for TableColumnAlign {
fn from(value: mdast::AlignKind) -> Self {
match value {
mdast::AlignKind::None => TableColumnAlign::Left,
mdast::AlignKind::Left => TableColumnAlign::Left,
mdast::AlignKind::Center => TableColumnAlign::Center,
mdast::AlignKind::Right => TableColumnAlign::Right,
}
}
}

#[derive(Debug, Default, Clone, PartialEq)]
Expand Down Expand Up @@ -461,6 +488,7 @@ impl Node {
.children({
let mut cells = Vec::with_capacity(row.children.len());
for (ix, cell) in row.children.iter().enumerate() {
let align = table.column_align(ix);
let len = col_lens
.get(ix)
.copied()
Expand All @@ -470,6 +498,13 @@ impl Node {
cells.push(
div()
.id("cell")
.flex()
.when(align == TableColumnAlign::Center, |this| {
this.justify_center()
})
.when(align == TableColumnAlign::Right, |this| {
this.justify_end()
})
.w(Length::Definite(relative(len as f32)))
.px_2()
.py_1()
Expand Down
6 changes: 6 additions & 0 deletions crates/ui/src/text/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ impl From<mdast::Node> for element::Node {
Node::ThematicBreak(_) => element::Node::Divider,
Node::Table(val) => {
let mut table = Table::default();
table.column_aligns = val
.align
.clone()
.into_iter()
.map(|align| align.into())
.collect();
val.children.iter().for_each(|c| {
if let Node::TableRow(row) = c {
parse_table_row(&mut table, row);
Expand Down

0 comments on commit 95fcdbc

Please sign in to comment.