mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Reorder blocks
This commit is contained in:
parent
029b87b0a9
commit
3cc026cf39
@ -28,7 +28,7 @@ use crate::util::EcoString;
|
|||||||
/// 1. A `Styled` node attaches a style map to other content. For example, a
|
/// 1. A `Styled` node attaches a style map to other content. For example, a
|
||||||
/// single bold word could be represented as a `Styled(Text("Hello"),
|
/// single bold word could be represented as a `Styled(Text("Hello"),
|
||||||
/// [TextNode::STRONG: true])` node.
|
/// [TextNode::STRONG: true])` node.
|
||||||
|
///
|
||||||
/// 2. A `Sequence` node content combines other arbitrary content and is the
|
/// 2. A `Sequence` node content combines other arbitrary content and is the
|
||||||
/// representation of a "flow" of other nodes. So, when you write `[Hi] +
|
/// representation of a "flow" of other nodes. So, when you write `[Hi] +
|
||||||
/// [you]` in Typst, this type's [`Add`] implementation is invoked and the
|
/// [you]` in Typst, this type's [`Add`] implementation is invoked and the
|
||||||
@ -175,12 +175,86 @@ impl Content {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Layout for Content {
|
||||||
|
fn layout(
|
||||||
|
&self,
|
||||||
|
ctx: &mut Context,
|
||||||
|
regions: &Regions,
|
||||||
|
styles: StyleChain,
|
||||||
|
) -> TypResult<Vec<Arc<Frame>>> {
|
||||||
|
let sya = Arena::new();
|
||||||
|
let tpa = Arena::new();
|
||||||
|
|
||||||
|
let mut builder = Builder::new(&sya, &tpa, false);
|
||||||
|
builder.process(ctx, self, styles)?;
|
||||||
|
builder.finish(ctx, styles)?;
|
||||||
|
|
||||||
|
let (flow, shared) = builder.flow.finish();
|
||||||
|
FlowNode(flow).layout(ctx, regions, shared)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pack(self) -> LayoutNode {
|
||||||
|
match self {
|
||||||
|
Content::Block(node) => node,
|
||||||
|
other => LayoutNode::new(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for Content {
|
impl Default for Content {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for Content {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Space => f.pad("Space"),
|
||||||
|
Self::Linebreak => f.pad("Linebreak"),
|
||||||
|
Self::Horizontal(kind) => write!(f, "Horizontal({kind:?})"),
|
||||||
|
Self::Text(text) => write!(f, "Text({text:?})"),
|
||||||
|
Self::Inline(node) => {
|
||||||
|
f.write_str("Inline(")?;
|
||||||
|
node.fmt(f)?;
|
||||||
|
f.write_str(")")
|
||||||
|
}
|
||||||
|
Self::Parbreak => f.pad("Parbreak"),
|
||||||
|
Self::Colbreak => f.pad("Colbreak"),
|
||||||
|
Self::Vertical(kind) => write!(f, "Vertical({kind:?})"),
|
||||||
|
Self::Block(node) => {
|
||||||
|
f.write_str("Block(")?;
|
||||||
|
node.fmt(f)?;
|
||||||
|
f.write_str(")")
|
||||||
|
}
|
||||||
|
Self::List(item) => {
|
||||||
|
f.write_str("- ")?;
|
||||||
|
item.body.fmt(f)
|
||||||
|
}
|
||||||
|
Self::Enum(item) => {
|
||||||
|
if let Some(number) = item.number {
|
||||||
|
write!(f, "{}", number)?;
|
||||||
|
}
|
||||||
|
f.write_str(". ")?;
|
||||||
|
item.body.fmt(f)
|
||||||
|
}
|
||||||
|
Self::Pagebreak => f.pad("Pagebreak"),
|
||||||
|
Self::Page(page) => page.fmt(f),
|
||||||
|
Self::Show(node) => {
|
||||||
|
f.write_str("Show(")?;
|
||||||
|
node.fmt(f)?;
|
||||||
|
f.write_str(")")
|
||||||
|
}
|
||||||
|
Self::Styled(styled) => {
|
||||||
|
let (sub, map) = styled.as_ref();
|
||||||
|
map.fmt(f)?;
|
||||||
|
sub.fmt(f)
|
||||||
|
}
|
||||||
|
Self::Sequence(seq) => f.debug_list().entries(seq.iter()).finish(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Add for Content {
|
impl Add for Content {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
@ -219,32 +293,6 @@ impl Sum for Content {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Layout for Content {
|
|
||||||
fn layout(
|
|
||||||
&self,
|
|
||||||
ctx: &mut Context,
|
|
||||||
regions: &Regions,
|
|
||||||
styles: StyleChain,
|
|
||||||
) -> TypResult<Vec<Arc<Frame>>> {
|
|
||||||
let sya = Arena::new();
|
|
||||||
let tpa = Arena::new();
|
|
||||||
|
|
||||||
let mut builder = Builder::new(&sya, &tpa, false);
|
|
||||||
builder.process(ctx, self, styles)?;
|
|
||||||
builder.finish(ctx, styles)?;
|
|
||||||
|
|
||||||
let (flow, shared) = builder.flow.finish();
|
|
||||||
FlowNode(flow).layout(ctx, regions, shared)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pack(self) -> LayoutNode {
|
|
||||||
match self {
|
|
||||||
Content::Block(node) => node,
|
|
||||||
other => LayoutNode::new(other),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Builds a flow or page nodes from content.
|
/// Builds a flow or page nodes from content.
|
||||||
struct Builder<'a> {
|
struct Builder<'a> {
|
||||||
/// An arena where intermediate style chains are stored.
|
/// An arena where intermediate style chains are stored.
|
||||||
@ -263,6 +311,15 @@ struct Builder<'a> {
|
|||||||
keep_next: bool,
|
keep_next: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Builds an unordered or ordered list from items.
|
||||||
|
struct ListBuilder<'a> {
|
||||||
|
styles: StyleChain<'a>,
|
||||||
|
kind: ListKind,
|
||||||
|
items: Vec<ListItem>,
|
||||||
|
wide: bool,
|
||||||
|
staged: Vec<(&'a Content, StyleChain<'a>)>,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Builder<'a> {
|
impl<'a> Builder<'a> {
|
||||||
/// Prepare the builder.
|
/// Prepare the builder.
|
||||||
fn new(sya: &'a Arena<StyleChain<'a>>, tpa: &'a Arena<Content>, top: bool) -> Self {
|
fn new(sya: &'a Arena<StyleChain<'a>>, tpa: &'a Arena<Content>, top: bool) -> Self {
|
||||||
@ -513,60 +570,3 @@ impl<'a> Builder<'a> {
|
|||||||
self.finish_page(ctx, true, false, styles)
|
self.finish_page(ctx, true, false, styles)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds an unordered or ordered list from items.
|
|
||||||
struct ListBuilder<'a> {
|
|
||||||
styles: StyleChain<'a>,
|
|
||||||
kind: ListKind,
|
|
||||||
items: Vec<ListItem>,
|
|
||||||
wide: bool,
|
|
||||||
staged: Vec<(&'a Content, StyleChain<'a>)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Content {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Space => f.pad("Space"),
|
|
||||||
Self::Linebreak => f.pad("Linebreak"),
|
|
||||||
Self::Horizontal(kind) => write!(f, "Horizontal({kind:?})"),
|
|
||||||
Self::Text(text) => write!(f, "Text({text:?})"),
|
|
||||||
Self::Inline(node) => {
|
|
||||||
f.write_str("Inline(")?;
|
|
||||||
node.fmt(f)?;
|
|
||||||
f.write_str(")")
|
|
||||||
}
|
|
||||||
Self::Parbreak => f.pad("Parbreak"),
|
|
||||||
Self::Colbreak => f.pad("Colbreak"),
|
|
||||||
Self::Vertical(kind) => write!(f, "Vertical({kind:?})"),
|
|
||||||
Self::Block(node) => {
|
|
||||||
f.write_str("Block(")?;
|
|
||||||
node.fmt(f)?;
|
|
||||||
f.write_str(")")
|
|
||||||
}
|
|
||||||
Self::List(item) => {
|
|
||||||
f.write_str("- ")?;
|
|
||||||
item.body.fmt(f)
|
|
||||||
}
|
|
||||||
Self::Enum(item) => {
|
|
||||||
if let Some(number) = item.number {
|
|
||||||
write!(f, "{}", number)?;
|
|
||||||
}
|
|
||||||
f.write_str(". ")?;
|
|
||||||
item.body.fmt(f)
|
|
||||||
}
|
|
||||||
Self::Pagebreak => f.pad("Pagebreak"),
|
|
||||||
Self::Page(page) => page.fmt(f),
|
|
||||||
Self::Show(node) => {
|
|
||||||
f.write_str("Show(")?;
|
|
||||||
node.fmt(f)?;
|
|
||||||
f.write_str(")")
|
|
||||||
}
|
|
||||||
Self::Styled(styled) => {
|
|
||||||
let (sub, map) = styled.as_ref();
|
|
||||||
map.fmt(f)?;
|
|
||||||
sub.fmt(f)
|
|
||||||
}
|
|
||||||
Self::Sequence(seq) => f.debug_list().entries(seq.iter()).finish(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user