mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Scripts: a batch keeps one copy of the style lists for its style objects
While a script runs a `command.batch`, the session kept the style lists of every step that changed them, so each such step also made the next edit copy the whole list. The session now keeps one copy of each open document's lists, as the last step left them, and records the names of the styles a step removed or renamed and the documents a step reverted. The style objects check those names, so a style that a batch removes and makes again, or renames and names back, is still a different style.
This commit is contained in:
1 parent
c7671c615b
commit
997375be0d
5 files changed
+93
-25
No files matched your search
@@ -309,10 +309,9 @@ pub struct Session {
|
||||
pub script_frames: bool,
|
||||
/// Each open document's highest next id a run has seen, by [`DocState::uid`].
|
||||
pub(crate) script_high_water: std::collections::HashMap<u64, u64>,
|
||||
/// The style lists each step of a batch a script runs left in a document whose lists it
|
||||
/// changed: its uid, revert count, and character and paragraph style lists
|
||||
/// ([`script::note_style_lists`]).
|
||||
pub(crate) script_style_trail: Vec<script::StyleLists>,
|
||||
/// The styles the steps of a batch a script runs removed or renamed and the documents they
|
||||
/// reverted ([`script::note_style_lists`]).
|
||||
pub(crate) script_style_trail: script::StyleTrail,
|
||||
/// While a slice of a script runs in this session.
|
||||
pub(crate) script_in_run: bool,
|
||||
/// Write recovery copies of the unsaved documents before a run, and don't start without them.
|
||||
|
||||
@@ -76,17 +76,20 @@ pub(super) struct StyleState {
|
||||
|
||||
/// Marks stale each style object whose style the command just run removed or renamed, so a style
|
||||
/// removed and made again under its name by later commands, or by later steps of the same batch,
|
||||
/// is another style. A document whose style lists did not change is not checked again.
|
||||
/// is another style. A document whose style lists did not change is not checked again. A live
|
||||
/// slot's style was in its document's list when the command started, so a step that removed or
|
||||
/// renamed it is in the trail's removed names.
|
||||
pub(in crate::script) fn after_command(h: &Host) {
|
||||
let trail = std::mem::take(&mut h.session.borrow_mut().script_style_trail);
|
||||
let mut slots = h.dom.text.styles.slots.borrow_mut();
|
||||
if !slots.iter().any(|s| s.live) {
|
||||
return;
|
||||
}
|
||||
for (uid, reverts, c, p) in &trail {
|
||||
for slot in slots.iter_mut().filter(|x| x.live && x.uid == *uid) {
|
||||
let list = if slot.para { p } else { c };
|
||||
slot.live = slot.reverts == *reverts && (slot.name == normal(slot.para) || list.iter().any(|d| d.name == slot.name));
|
||||
for slot in slots.iter_mut().filter(|x| x.live) {
|
||||
if trail.reverted.contains(&slot.uid)
|
||||
|| (!trail.gone.is_empty() && slot.name != normal(slot.para) && trail.gone.contains(&(slot.uid, slot.para, slot.name.clone())))
|
||||
{
|
||||
slot.live = false;
|
||||
}
|
||||
}
|
||||
let s = h.session.borrow();
|
||||
|
||||
@@ -1236,7 +1236,7 @@ fn note_ids(s: &mut Session) {
|
||||
/// it, for a result that cannot cross to the script.
|
||||
fn step(s: &mut Session, host: &Host, id: &str, params: &Value) -> Result<(Value, u64, u64, Option<Savepoint>)> {
|
||||
raise_ids(s);
|
||||
s.script_style_trail.clear();
|
||||
s.script_style_trail = StyleTrail::start(s);
|
||||
let mutating = waits_for_tool_edit(id);
|
||||
// Inside an object-model call's own savepoint ([`dom`]), that savepoint puts a failure back.
|
||||
let save = (mutating && !host.dom.saving()).then(|| Savepoint::take(s, id));
|
||||
@@ -1274,6 +1274,7 @@ fn step(s: &mut Session, host: &Host, id: &str, params: &Value) -> Result<(Value
|
||||
Ok((v, objects, bytes, save))
|
||||
}
|
||||
Err(e) => {
|
||||
s.script_style_trail = StyleTrail::default();
|
||||
if let Some(save) = save
|
||||
&& !save.restore(s)
|
||||
{
|
||||
@@ -1350,6 +1351,8 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
// Style objects follow only a command that is kept.
|
||||
if crossed.is_ok() {
|
||||
dom::after_command(host);
|
||||
} else {
|
||||
session.borrow_mut().script_style_trail = StyleTrail::default();
|
||||
}
|
||||
crossed.map(|data| (data, value))
|
||||
}
|
||||
@@ -1358,23 +1361,63 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
pub(crate) type StyleLists =
|
||||
(u64, u64, vectorcraft_doc::Shared<Vec<vectorcraft_doc::TextStyleDef>>, vectorcraft_doc::Shared<Vec<vectorcraft_doc::TextStyleDef>>);
|
||||
|
||||
/// After a step of a batch a script runs: notes the style lists of each document whose lists or
|
||||
/// revert count the step changed, so the style objects see a style the batch removed and made
|
||||
/// again under its name as another style ([`dom::after_command`]).
|
||||
pub(crate) fn note_style_lists(s: &mut Session) {
|
||||
for st in &s.docs {
|
||||
let last = s.script_style_trail.iter().rev().find(|(uid, ..)| *uid == st.uid);
|
||||
let same = last.is_some_and(|(_, reverts, c, p)| {
|
||||
*reverts == st.reverts
|
||||
&& vectorcraft_doc::Shared::ptr_eq(c, &st.doc.char_styles)
|
||||
&& vectorcraft_doc::Shared::ptr_eq(p, &st.doc.para_styles)
|
||||
});
|
||||
if !same {
|
||||
s.script_style_trail.push((st.uid, st.reverts, st.doc.char_styles.clone(), st.doc.para_styles.clone()));
|
||||
}
|
||||
/// What the steps of a command a script runs did to the documents' style lists
|
||||
/// ([`note_style_lists`]): each open document's lists as the last step left them, the styles a step
|
||||
/// removed or renamed (a document's uid, whether it is a paragraph style, and the name), and the
|
||||
/// documents a step reverted.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct StyleTrail {
|
||||
lists: Vec<StyleLists>,
|
||||
pub(crate) gone: std::collections::HashSet<(u64, bool, String)>,
|
||||
pub(crate) reverted: Vec<u64>,
|
||||
}
|
||||
|
||||
impl StyleTrail {
|
||||
/// A trail that starts from the documents' style lists now.
|
||||
fn start(s: &Session) -> Self {
|
||||
let lists = s.docs.iter().map(|st| (st.uid, st.reverts, st.doc.char_styles.clone(), st.doc.para_styles.clone())).collect();
|
||||
StyleTrail { lists, ..Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
/// After a step of a batch a script runs: notes the styles the step removed or renamed and the
|
||||
/// documents it reverted, so the style objects see a style the batch removed and made again under
|
||||
/// its name as another style ([`dom::after_command`]). The trail keeps one copy of each open
|
||||
/// document's lists, however many steps change them.
|
||||
pub(crate) fn note_style_lists(s: &mut Session) {
|
||||
let trail = &mut s.script_style_trail;
|
||||
trail.lists.retain(|(uid, ..)| s.docs.iter().any(|d| d.uid == *uid));
|
||||
for st in &s.docs {
|
||||
let (c, p) = (&st.doc.char_styles, &st.doc.para_styles);
|
||||
let Some(last) = trail.lists.iter_mut().find(|(uid, ..)| *uid == st.uid) else {
|
||||
trail.lists.push((st.uid, st.reverts, c.clone(), p.clone()));
|
||||
continue;
|
||||
};
|
||||
if last.1 != st.reverts {
|
||||
if !trail.reverted.contains(&st.uid) {
|
||||
trail.reverted.push(st.uid);
|
||||
}
|
||||
} else {
|
||||
for (para, old, new) in [(false, &last.2, c), (true, &last.3, p)] {
|
||||
if vectorcraft_doc::Shared::ptr_eq(old, new) {
|
||||
continue;
|
||||
}
|
||||
let names: std::collections::HashSet<&str> = new.iter().map(|d| d.name.as_str()).collect();
|
||||
for d in old.iter().filter(|d| !names.contains(d.name.as_str())) {
|
||||
trail.gone.insert((st.uid, para, d.name.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
*last = (st.uid, st.reverts, c.clone(), p.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// How many sets of style lists the steps of a batch keep ([`note_style_lists`]).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn retained_style_lists(s: &Session) -> usize {
|
||||
s.script_style_trail.lists.len()
|
||||
}
|
||||
|
||||
/// A bounded snapshot of the active document, or `null` with none open. A snapshot past the limits
|
||||
/// of a crossing throws a `RangeError`.
|
||||
fn vc_document(host: &Host) -> std::result::Result<Data, HostError> {
|
||||
|
||||
@@ -165,7 +165,7 @@ impl Session {
|
||||
script_runs: 0,
|
||||
script_frames: false,
|
||||
script_high_water: Default::default(),
|
||||
script_style_trail: Vec::new(),
|
||||
script_style_trail: Default::default(),
|
||||
script_in_run: false,
|
||||
script_copy_first: cfg!(target_arch = "wasm32"),
|
||||
}
|
||||
|
||||
@@ -174,6 +174,15 @@ fn a_renamed_style_keeps_its_object_and_a_removed_style_is_stale() {
|
||||
),
|
||||
);
|
||||
assert!(e.contains("Stale style"), "{e}");
|
||||
// And a style a batch renames and then names back.
|
||||
let e = failure(
|
||||
&mut s,
|
||||
&format!(
|
||||
"{DOC} var a = cs.getByName('B'); vc.run('command.batch', {{commands: [{{command: 'charStyle.rename', params: {{name: 'B', to: 'C'}}}}, \
|
||||
{{command: 'charStyle.rename', params: {{name: 'C', to: 'B'}}}}]}}); a.name;"
|
||||
),
|
||||
);
|
||||
assert!(e.contains("Stale style"), "{e}");
|
||||
// The attributes object of a removed style throws for its typename too.
|
||||
let e =
|
||||
failure(&mut s, &format!("{DOC} var t = cs.getByName('B').characterAttributes; vc.log(t.typename); cs.getByName('B').remove(); t.typename;"));
|
||||
@@ -191,6 +200,20 @@ fn a_renamed_style_keeps_its_object_and_a_removed_style_is_stale() {
|
||||
assert_eq!(runs(&s, id)[0].2, 40.0);
|
||||
}
|
||||
|
||||
/// The steps of one batch a script runs keep one copy of a document's style lists, however many of
|
||||
/// them change the lists.
|
||||
#[test]
|
||||
fn the_steps_of_a_batch_keep_one_copy_of_the_style_lists() {
|
||||
let (mut s, _) = doc_with("Hello");
|
||||
let commands: Vec<Value> = (0..20).map(|i| json!({"command": "charStyle.new", "params": {"name": format!("S{i}"), "attrs": {}}})).collect();
|
||||
s.script_in_run = true;
|
||||
let r = s.execute("command.batch", &json!({"commands": commands}));
|
||||
s.script_in_run = false;
|
||||
r.unwrap();
|
||||
assert_eq!(char_names(&s).len(), 20);
|
||||
assert_eq!(crate::script::retained_style_lists(&s), 1);
|
||||
}
|
||||
|
||||
/// A removal or rename whose result cannot cross to the script is put back, and the style's object
|
||||
/// still names the style after it.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user