mirror of
https://github.com/typst/typst
synced 2025-06-28 08:12:53 +08:00
List above & below
This commit is contained in:
parent
49c0bac44d
commit
ecd2bca606
@ -43,10 +43,7 @@ impl ShowNode {
|
|||||||
|
|
||||||
impl Show for ShowNode {
|
impl Show for ShowNode {
|
||||||
fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
|
fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
|
||||||
ctx.query((self, styles), |ctx, (node, styles)| {
|
self.0.show(ctx, styles)
|
||||||
node.0.show(ctx, styles)
|
|
||||||
})
|
|
||||||
.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pack(self) -> ShowNode {
|
fn pack(self) -> ShowNode {
|
||||||
|
@ -33,9 +33,13 @@ impl<const L: ListKind> ListNode<L> {
|
|||||||
/// The spacing between the list items of a non-wide list.
|
/// The spacing between the list items of a non-wide list.
|
||||||
pub const SPACING: Linear = Linear::zero();
|
pub const SPACING: Linear = Linear::zero();
|
||||||
/// The indentation of each item's label.
|
/// The indentation of each item's label.
|
||||||
pub const LABEL_INDENT: Linear = Relative::new(0.0).into();
|
pub const INDENT: Linear = Relative::new(0.0).into();
|
||||||
/// The space between the label and the body of each item.
|
/// The space between the label and the body of each item.
|
||||||
pub const BODY_INDENT: Linear = Relative::new(0.5).into();
|
pub const BODY_INDENT: Linear = Relative::new(0.5).into();
|
||||||
|
/// The extra padding above the list.
|
||||||
|
pub const ABOVE: Length = Length::zero();
|
||||||
|
/// The extra padding below the list.
|
||||||
|
pub const BELOW: Length = Length::zero();
|
||||||
|
|
||||||
fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> {
|
fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> {
|
||||||
Ok(Template::show(Self {
|
Ok(Template::show(Self {
|
||||||
@ -52,54 +56,69 @@ impl<const L: ListKind> ListNode<L> {
|
|||||||
|
|
||||||
impl<const L: ListKind> Show for ListNode<L> {
|
impl<const L: ListKind> Show for ListNode<L> {
|
||||||
fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
|
fn show(&self, ctx: &mut Context, styles: StyleChain) -> TypResult<Template> {
|
||||||
if let Some(template) = styles.show(
|
let template = if let Some(template) = styles.show(
|
||||||
self,
|
self,
|
||||||
ctx,
|
ctx,
|
||||||
self.items.iter().map(|item| Value::Template((*item.body).clone())),
|
self.items.iter().map(|item| Value::Template((*item.body).clone())),
|
||||||
)? {
|
)? {
|
||||||
return Ok(template);
|
template
|
||||||
}
|
} else {
|
||||||
|
let mut children = vec![];
|
||||||
|
let mut number = self.start;
|
||||||
|
|
||||||
let mut children = vec![];
|
let label = styles.get_ref(Self::LABEL);
|
||||||
let mut number = self.start;
|
|
||||||
|
|
||||||
let label = styles.get_ref(Self::LABEL);
|
for item in &self.items {
|
||||||
|
number = item.number.unwrap_or(number);
|
||||||
|
if L == UNORDERED {
|
||||||
|
number = 1;
|
||||||
|
}
|
||||||
|
|
||||||
for item in &self.items {
|
children.push(LayoutNode::default());
|
||||||
number = item.number.unwrap_or(number);
|
children.push(label.resolve(ctx, L, number)?.pack());
|
||||||
if L == UNORDERED {
|
children.push(LayoutNode::default());
|
||||||
number = 1;
|
children.push((*item.body).clone().pack());
|
||||||
|
number += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
children.push(LayoutNode::default());
|
let em = styles.get(TextNode::SIZE).abs;
|
||||||
children.push(label.resolve(ctx, L, number)?.pack());
|
let leading = styles.get(ParNode::LEADING);
|
||||||
children.push(LayoutNode::default());
|
let spacing = if self.wide {
|
||||||
children.push((*item.body).clone().pack());
|
styles.get(ParNode::SPACING)
|
||||||
number += 1;
|
} else {
|
||||||
}
|
styles.get(Self::SPACING)
|
||||||
|
};
|
||||||
|
|
||||||
let em = styles.get(TextNode::SIZE).abs;
|
let gutter = (leading + spacing).resolve(em);
|
||||||
let leading = styles.get(ParNode::LEADING);
|
let indent = styles.get(Self::INDENT).resolve(em);
|
||||||
let spacing = if self.wide {
|
let body_indent = styles.get(Self::BODY_INDENT).resolve(em);
|
||||||
styles.get(ParNode::SPACING)
|
|
||||||
} else {
|
Template::block(GridNode {
|
||||||
styles.get(Self::SPACING)
|
tracks: Spec::with_x(vec![
|
||||||
|
TrackSizing::Linear(indent.into()),
|
||||||
|
TrackSizing::Auto,
|
||||||
|
TrackSizing::Linear(body_indent.into()),
|
||||||
|
TrackSizing::Auto,
|
||||||
|
]),
|
||||||
|
gutter: Spec::with_y(vec![TrackSizing::Linear(gutter.into())]),
|
||||||
|
children,
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
let gutter = (leading + spacing).resolve(em);
|
let mut seq = vec![];
|
||||||
let label_indent = styles.get(Self::LABEL_INDENT).resolve(em);
|
let above = styles.get(Self::ABOVE);
|
||||||
let body_indent = styles.get(Self::BODY_INDENT).resolve(em);
|
if !above.is_zero() {
|
||||||
|
seq.push(Template::Vertical(above.into()));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Template::block(GridNode {
|
seq.push(template);
|
||||||
tracks: Spec::with_x(vec![
|
|
||||||
TrackSizing::Linear(label_indent.into()),
|
let below = styles.get(Self::BELOW);
|
||||||
TrackSizing::Auto,
|
if !below.is_zero() {
|
||||||
TrackSizing::Linear(body_indent.into()),
|
seq.push(Template::Vertical(below.into()));
|
||||||
TrackSizing::Auto,
|
}
|
||||||
]),
|
|
||||||
gutter: Spec::with_y(vec![TrackSizing::Linear(gutter.into())]),
|
Ok(Template::sequence(seq))
|
||||||
children,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,4 +30,4 @@ A #rect(fill: yellow, padding: 5pt, rect()) B
|
|||||||
|
|
||||||
---
|
---
|
||||||
// The inner list should not be indented extra.
|
// The inner list should not be indented extra.
|
||||||
[#set text(100%);#list(label-indent: 20pt, list[A])]
|
[#set text(100%);#list(indent: 20pt, list[A])]
|
||||||
|
@ -15,7 +15,7 @@ Hello *{x}*
|
|||||||
]
|
]
|
||||||
|
|
||||||
- Fruit
|
- Fruit
|
||||||
[#set list(label-indent: 10pt)
|
[#set list(indent: 10pt)
|
||||||
#fruit]
|
#fruit]
|
||||||
- No more fruit
|
- No more fruit
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user