diff --git a/crates/typst-library/src/meta/counter.rs b/crates/typst-library/src/meta/counter.rs index 9a223b328..ca4f3bc5d 100644 --- a/crates/typst-library/src/meta/counter.rs +++ b/crates/typst-library/src/meta/counter.rs @@ -385,7 +385,7 @@ impl Counter { /// Produce content that performs a state update. pub fn update(self, update: CounterUpdate) -> Content { - UpdateElem::new(self, update).pack() + UpdateElem::new(self.0, update).pack() } /// Produce the whole sequence of counter states. @@ -461,7 +461,7 @@ impl Counter { /// The selector relevant for this counter's updates. fn selector(&self) -> Selector { let mut selector = - Selector::Elem(UpdateElem::func(), Some(dict! { "counter" => self.clone() })); + Selector::Elem(UpdateElem::func(), Some(dict! { "key" => self.0.clone() })); if let CounterKey::Selector(key) = &self.0 { selector = Selector::Or(eco_vec![selector, key.clone()]); @@ -502,8 +502,13 @@ pub enum CounterKey { cast! { CounterKey, + self => match self { + Self::Page => PageElem::func().into_value(), + Self::Selector(v) => v.into_value(), + Self::Str(v) => v.into_value(), + }, v: Str => Self::Str(v), - label: Label => Self::Selector(Selector::Label(label)), + v: Label => Self::Selector(Selector::Label(v)), v: ElemFunc => { if v == PageElem::func() { Self::Page @@ -511,7 +516,7 @@ cast! { Self::Selector(LocatableSelector::from_value(v.into_value())?.0) } }, - selector: LocatableSelector => Self::Selector(selector.0), + v: LocatableSelector => Self::Selector(v.0), } impl Debug for CounterKey { @@ -666,9 +671,9 @@ impl Show for DisplayElem { /// Category: special #[element(Locatable, Show)] struct UpdateElem { - /// The counter. + /// The key that identifies the counter. #[required] - counter: Counter, + key: CounterKey, /// The update to perform on the counter. #[required] diff --git a/crates/typst-library/src/meta/state.rs b/crates/typst-library/src/meta/state.rs index aee53a29a..5b3049728 100644 --- a/crates/typst-library/src/meta/state.rs +++ b/crates/typst-library/src/meta/state.rs @@ -297,7 +297,7 @@ impl State { /// Produce content that performs a state update. pub fn update(self, update: StateUpdate) -> Content { - UpdateElem::new(self, update).pack() + UpdateElem::new(self.key, update).pack() } /// Produce the whole sequence of states. @@ -349,7 +349,7 @@ impl State { /// The selector for this state's updates. fn selector(&self) -> Selector { - Selector::Elem(UpdateElem::func(), Some(dict! { "state" => self.clone() })) + Selector::Elem(UpdateElem::func(), Some(dict! { "key" => self.key.clone() })) } } @@ -423,9 +423,9 @@ impl Show for DisplayElem { /// Category: special #[element(Locatable, Show)] struct UpdateElem { - /// The state. + /// The key that identifies the state. #[required] - state: State, + key: Str, /// The update to perform on the state. #[required] diff --git a/tests/ref/meta/state.png b/tests/ref/meta/state.png index ca77fdc7e..d179c3c5f 100644 Binary files a/tests/ref/meta/state.png and b/tests/ref/meta/state.png differ diff --git a/tests/typ/meta/state.typ b/tests/typ/meta/state.typ index a7c3d26a7..8f460ce10 100644 --- a/tests/typ/meta/state.typ +++ b/tests/typ/meta/state.typ @@ -15,6 +15,15 @@ Was: #locate(location => { s.at(it.location()) }). +--- +// Try same key with different initial value. +#state("key", 2).display() +#state("key").update(x => x + 1) +#state("key", 2).display() +#state("key", 3).display() +#state("key").update(x => x + 1) +#state("key", 2).display() + --- #set page(width: 200pt) #set text(8pt)