mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
Identify state by key only
This commit is contained in:
parent
fa9e2c6237
commit
fc90b72355
@ -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]
|
||||
|
@ -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]
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 52 KiB |
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user