mirror of
https://github.com/typst/typst
synced 2025-05-17 02:25:27 +08:00
Clean up list styling (#4324)
This commit is contained in:
parent
3257efd03a
commit
a9b3273a2b
@ -385,6 +385,19 @@ impl Content {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Style this content with a full style map in-place.
|
||||||
|
pub fn style_in_place(&mut self, styles: Styles) {
|
||||||
|
if styles.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(style_elem) = self.to_packed_mut::<StyledElem>() {
|
||||||
|
style_elem.styles.apply(styles);
|
||||||
|
} else {
|
||||||
|
*self = StyledElem::new(std::mem::take(self), styles).into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Queries the content tree for all elements that match the given selector.
|
/// Queries the content tree for all elements that match the given selector.
|
||||||
///
|
///
|
||||||
/// Elements produced in `show` rules will not be included in the results.
|
/// Elements produced in `show` rules will not be included in the results.
|
||||||
|
@ -7,7 +7,7 @@ use crate::diag::{bail, SourceResult};
|
|||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
cast, elem, scope, Array, Content, Context, NativeElement, Packed, Show, Smart,
|
cast, elem, scope, Array, Content, Context, NativeElement, Packed, Show, Smart,
|
||||||
StyleChain,
|
StyleChain, Styles,
|
||||||
};
|
};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
Alignment, Axes, BlockElem, Cell, CellGrid, Em, Fragment, GridLayouter, HAlignment,
|
Alignment, Axes, BlockElem, Cell, CellGrid, Em, Fragment, GridLayouter, HAlignment,
|
||||||
@ -316,6 +316,14 @@ pub struct EnumItem {
|
|||||||
pub body: Content,
|
pub body: Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Packed<EnumItem> {
|
||||||
|
/// Apply styles to this enum item.
|
||||||
|
pub fn styled(mut self, styles: Styles) -> Self {
|
||||||
|
self.body.style_in_place(styles);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cast! {
|
cast! {
|
||||||
EnumItem,
|
EnumItem,
|
||||||
array: Array => {
|
array: Array => {
|
||||||
|
@ -4,7 +4,7 @@ use crate::diag::{bail, SourceResult};
|
|||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
cast, elem, scope, Array, Content, Context, Depth, Func, NativeElement, Packed, Show,
|
cast, elem, scope, Array, Content, Context, Depth, Func, NativeElement, Packed, Show,
|
||||||
Smart, StyleChain, Value,
|
Smart, StyleChain, Styles, Value,
|
||||||
};
|
};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
Axes, BlockElem, Cell, CellGrid, Em, Fragment, GridLayouter, HAlignment, Length,
|
Axes, BlockElem, Cell, CellGrid, Em, Fragment, GridLayouter, HAlignment, Length,
|
||||||
@ -206,6 +206,14 @@ pub struct ListItem {
|
|||||||
pub body: Content,
|
pub body: Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Packed<ListItem> {
|
||||||
|
/// Apply styles to this list item.
|
||||||
|
pub fn styled(mut self, styles: Styles) -> Self {
|
||||||
|
self.body.style_in_place(styles);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cast! {
|
cast! {
|
||||||
ListItem,
|
ListItem,
|
||||||
v: Content => v.unpack::<Self>().unwrap_or_else(Self::new)
|
v: Content => v.unpack::<Self>().unwrap_or_else(Self::new)
|
||||||
|
@ -2,6 +2,7 @@ use crate::diag::{bail, SourceResult};
|
|||||||
use crate::engine::Engine;
|
use crate::engine::Engine;
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
cast, elem, scope, Array, Content, NativeElement, Packed, Show, Smart, StyleChain,
|
cast, elem, scope, Array, Content, NativeElement, Packed, Show, Smart, StyleChain,
|
||||||
|
Styles,
|
||||||
};
|
};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
BlockElem, Dir, Em, HElem, Length, Sides, Spacing, StackChild, StackElem, VElem,
|
BlockElem, Dir, Em, HElem, Length, Sides, Spacing, StackChild, StackElem, VElem,
|
||||||
@ -168,6 +169,15 @@ pub struct TermItem {
|
|||||||
pub description: Content,
|
pub description: Content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Packed<TermItem> {
|
||||||
|
/// Apply styles to this term item.
|
||||||
|
pub fn styled(mut self, styles: Styles) -> Self {
|
||||||
|
self.term.style_in_place(styles.clone());
|
||||||
|
self.description.style_in_place(styles);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cast! {
|
cast! {
|
||||||
TermItem,
|
TermItem,
|
||||||
array: Array => {
|
array: Array => {
|
||||||
|
@ -508,50 +508,26 @@ impl<'a> ListBuilder<'a> {
|
|||||||
let mut items = items.into_iter().peekable();
|
let mut items = items.into_iter().peekable();
|
||||||
let (first, _) = items.peek().unwrap();
|
let (first, _) = items.peek().unwrap();
|
||||||
let output = if first.is::<ListItem>() {
|
let output = if first.is::<ListItem>() {
|
||||||
ListElem::new(
|
let children = items
|
||||||
items
|
.map(|(item, local)| {
|
||||||
.map(|(item, local)| {
|
item.into_packed::<ListItem>().unwrap().styled(local)
|
||||||
let mut item = item.to_packed::<ListItem>().unwrap().clone();
|
})
|
||||||
let body = item.body().clone().styled_with_map(local);
|
.collect();
|
||||||
item.push_body(body);
|
ListElem::new(children).with_tight(self.tight).pack().spanned(span)
|
||||||
item
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.with_tight(self.tight)
|
|
||||||
.pack()
|
|
||||||
.spanned(span)
|
|
||||||
} else if first.is::<EnumItem>() {
|
} else if first.is::<EnumItem>() {
|
||||||
EnumElem::new(
|
let children = items
|
||||||
items
|
.map(|(item, local)| {
|
||||||
.map(|(item, local)| {
|
item.into_packed::<EnumItem>().unwrap().styled(local)
|
||||||
let mut item = item.to_packed::<EnumItem>().unwrap().clone();
|
})
|
||||||
let body = item.body().clone().styled_with_map(local);
|
.collect();
|
||||||
item.push_body(body);
|
EnumElem::new(children).with_tight(self.tight).pack().spanned(span)
|
||||||
item
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.with_tight(self.tight)
|
|
||||||
.pack()
|
|
||||||
.spanned(span)
|
|
||||||
} else if first.is::<TermItem>() {
|
} else if first.is::<TermItem>() {
|
||||||
TermsElem::new(
|
let children = items
|
||||||
items
|
.map(|(item, local)| {
|
||||||
.map(|(item, local)| {
|
item.into_packed::<TermItem>().unwrap().styled(local)
|
||||||
let mut item = item.to_packed::<TermItem>().unwrap().clone();
|
})
|
||||||
let term = item.term().clone().styled_with_map(local.clone());
|
.collect();
|
||||||
let description =
|
TermsElem::new(children).with_tight(self.tight).pack().spanned(span)
|
||||||
item.description().clone().styled_with_map(local);
|
|
||||||
item.push_term(term);
|
|
||||||
item.push_description(description);
|
|
||||||
item
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
)
|
|
||||||
.with_tight(self.tight)
|
|
||||||
.pack()
|
|
||||||
.spanned(span)
|
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user