Clean up list styling (#4324)

This commit is contained in:
Laurenz 2024-06-03 12:04:07 +02:00 committed by GitHub
parent 3257efd03a
commit a9b3273a2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 44 deletions

View File

@ -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.

View File

@ -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 => {

View File

@ -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)

View File

@ -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 => {

View File

@ -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)| {
let mut item = item.to_packed::<ListItem>().unwrap().clone(); item.into_packed::<ListItem>().unwrap().styled(local)
let body = item.body().clone().styled_with_map(local);
item.push_body(body);
item
}) })
.collect(), .collect();
) ListElem::new(children).with_tight(self.tight).pack().spanned(span)
.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)| {
let mut item = item.to_packed::<EnumItem>().unwrap().clone(); item.into_packed::<EnumItem>().unwrap().styled(local)
let body = item.body().clone().styled_with_map(local);
item.push_body(body);
item
}) })
.collect(), .collect();
) EnumElem::new(children).with_tight(self.tight).pack().spanned(span)
.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)| {
let mut item = item.to_packed::<TermItem>().unwrap().clone(); item.into_packed::<TermItem>().unwrap().styled(local)
let term = item.term().clone().styled_with_map(local.clone());
let description =
item.description().clone().styled_with_map(local);
item.push_term(term);
item.push_description(description);
item
}) })
.collect(), .collect();
) TermsElem::new(children).with_tight(self.tight).pack().spanned(span)
.with_tight(self.tight)
.pack()
.spanned(span)
} else { } else {
unreachable!() unreachable!()
}; };