mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Scripts: a style change that is put back keeps its style object
A removal or rename whose result cannot reach the script is put back, and style objects now follow a command only once it is kept. A failed atomic object-model call puts style objects back with the documents.
This commit is contained in:
1 parent
69448d0c23
commit
a2063cea75
4 files changed
+79
-9
No files matched your search
@@ -113,15 +113,22 @@ fn atomically<T>(h: &Host, f: impl FnOnce(&Host) -> std::result::Result<T, HostE
|
||||
return f(h);
|
||||
}
|
||||
let save = Savepoint::take(&mut h.session.borrow_mut(), "edit.duplicate");
|
||||
let styles = text::styles::mark(h);
|
||||
h.dom.saving.set(true);
|
||||
let r = f(h);
|
||||
h.dom.saving.set(false);
|
||||
let mut s = h.session.borrow_mut();
|
||||
if r.is_ok() {
|
||||
save.keep(&mut s);
|
||||
} else if !save.restore(&mut s) {
|
||||
return Err(HostError::Abort(HISTORY_LOST.to_string()));
|
||||
{
|
||||
let mut s = h.session.borrow_mut();
|
||||
if r.is_ok() {
|
||||
save.keep(&mut s);
|
||||
return r;
|
||||
}
|
||||
if !save.restore(&mut s) {
|
||||
return Err(HostError::Abort(HISTORY_LOST.to_string()));
|
||||
}
|
||||
}
|
||||
// Style objects are put back with the documents.
|
||||
text::styles::put_back(h, styles);
|
||||
r
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,41 @@ pub(in crate::script) fn after_command(h: &Host) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The style objects' state when a savepoint was taken: each slot's name and whether it was
|
||||
/// live, and the style lists last checked.
|
||||
pub(in crate::script) struct Mark {
|
||||
slots: Vec<(String, bool)>,
|
||||
seen: Vec<Seen>,
|
||||
}
|
||||
|
||||
/// The style objects' state now, for [`put_back`] when the savepoint taken with it is restored.
|
||||
pub(in crate::script) fn mark(h: &Host) -> Mark {
|
||||
let styles = &h.dom.text.styles;
|
||||
let slots = styles.slots.borrow().iter().map(|s| (s.name.clone(), s.live)).collect();
|
||||
Mark { slots, seen: styles.seen.borrow().clone() }
|
||||
}
|
||||
|
||||
/// Puts the style objects back as they were at `mark`, after the documents were put back to the
|
||||
/// savepoint taken with it. A slot added since then stays live only while its document has the
|
||||
/// style and no older live slot names it.
|
||||
pub(in crate::script) fn put_back(h: &Host, mark: Mark) {
|
||||
let styles = &h.dom.text.styles;
|
||||
let mut slots = styles.slots.borrow_mut();
|
||||
let kept = mark.slots.len();
|
||||
for (slot, (name, live)) in slots.iter_mut().zip(mark.slots) {
|
||||
slot.name = name;
|
||||
slot.live = live;
|
||||
}
|
||||
let s = h.session.borrow();
|
||||
for i in kept..slots.len() {
|
||||
let (older, newer) = slots.split_at_mut(i);
|
||||
let Some(slot) = newer.first_mut() else { break };
|
||||
let named = older.iter().any(|o| o.live && o.uid == slot.uid && o.para == slot.para && o.name == slot.name);
|
||||
slot.live = !named && s.documents().iter().any(|st| st.uid == slot.uid && has_style(st, slot.para, &slot.name));
|
||||
}
|
||||
*styles.seen.borrow_mut() = mark.seen;
|
||||
}
|
||||
|
||||
fn normal(para: bool) -> &'static str {
|
||||
if para { NORMAL_PARA } else { NORMAL_CHAR }
|
||||
}
|
||||
|
||||
@@ -1317,9 +1317,6 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
let mut s = session.borrow_mut();
|
||||
step(&mut s, host, &id, ¶ms)
|
||||
};
|
||||
if r.is_ok() {
|
||||
dom::after_command(host);
|
||||
}
|
||||
// Only commands that exist are timed, so the table holds at most one row per command.
|
||||
if let (Some(started), Some(spec)) = (started, crate::find_command(&id)) {
|
||||
let nanos = started.elapsed().as_nanos();
|
||||
@@ -1349,6 +1346,10 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
return Err(HostError::Abort(HISTORY_LOST.to_string()));
|
||||
}
|
||||
}
|
||||
// Style objects follow only a command that is kept.
|
||||
if crossed.is_ok() {
|
||||
dom::after_command(host);
|
||||
}
|
||||
crossed.map(|data| (data, value))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{TextObject, TextStyleDef};
|
||||
|
||||
use crate::cmd::textstyles::{NORMAL_CHAR, NORMAL_PARA};
|
||||
use crate::script::TEST_RESULT_BYTES;
|
||||
use crate::*;
|
||||
|
||||
fn run(s: &mut Session, source: &str) -> Result<Value> {
|
||||
@@ -177,6 +178,29 @@ fn a_renamed_style_keeps_its_object_and_a_removed_style_is_stale() {
|
||||
assert_eq!(runs(&s, id)[0].2, 40.0);
|
||||
}
|
||||
|
||||
/// 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]
|
||||
fn a_style_change_that_is_put_back_keeps_the_style_object() {
|
||||
let (mut s, _) = doc_with("Hello");
|
||||
s.execute("charStyle.new", &json!({"name": "B", "attrs": {}})).unwrap();
|
||||
TEST_RESULT_BYTES.with(|b| b.set(Some(1)));
|
||||
let r = run(
|
||||
&mut s,
|
||||
&format!(
|
||||
"{DOC} var a = cs.getByName('B');\
|
||||
try {{ a.remove(); }} catch (e) {{ vc.log(e.name); }}\
|
||||
try {{ vc.run('charStyle.delete', {{name: 'B'}}); }} catch (e) {{ vc.log(e.name); }}\
|
||||
try {{ a.name = 'C'; }} catch (e) {{ vc.log(e.name); }}\
|
||||
vc.log([a.name, a === cs.getByName('B'), cs.length]);"
|
||||
),
|
||||
);
|
||||
TEST_RESULT_BYTES.with(|b| b.set(None));
|
||||
let r = r.unwrap();
|
||||
assert_eq!(r["log"], json!(["RangeError", "RangeError", "RangeError", ["B", true, 2]]), "{r}");
|
||||
assert!(char_names(&s).contains(&"B".to_string()), "{:?}", char_names(&s));
|
||||
}
|
||||
|
||||
/// A character style styles exactly the range; without clearing overrides the range keeps its
|
||||
/// own underline, and with them it goes back to the Normal style first.
|
||||
#[test]
|
||||
@@ -239,7 +263,10 @@ fn style_attributes_redefine_the_text_in_the_style() {
|
||||
assert_eq!(out, [json!([true, true, true, "CharacterAttributes"]), json!([24, true, true])]);
|
||||
assert_eq!(runs(&s, id), [("ab ".into(), Some("X".into()), 24.0, true), ("cd".into(), Some("X".into()), 9.0, true)]);
|
||||
// Turning auto leading off keeps the leading auto leading gave the style's size.
|
||||
let out = log(&mut s, &format!("{DOC} var a = cs.getByName('X').characterAttributes; a.autoLeading = false; vc.log([a.autoLeading, Math.round(a.leading * 10)]);"));
|
||||
let out = log(
|
||||
&mut s,
|
||||
&format!("{DOC} var a = cs.getByName('X').characterAttributes; a.autoLeading = false; vc.log([a.autoLeading, Math.round(a.leading * 10)]);"),
|
||||
);
|
||||
assert_eq!(out, [json!([false, 288])]);
|
||||
let e = failure(&mut s, &format!("{DOC} cs.getByName('X').characterAttributes.size = 0;"));
|
||||
assert!(e.contains("0.1 to 1296"), "{e}");
|
||||
|
||||
Reference in new issue
Block a user