mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 20:43:18 -05:00
Scripts: visibility variables on embedded images, data sets kept after a no-op import
- variable.setBinding refuses an embedded image only for an image variable, so a script can bind an embedded image's visibility. - A Dataset object stays usable after dataset.import, variable.loadLibrary or variable.import leaves the document's data sets as they were, or the import is put back; it goes stale only when the data sets change. - script.check reports a document's exportVariables as filesWritten and file.write, and the script host test lists the variable and data set names the host adds. Docs: docs/mcp.md.
This commit is contained in:
1 parent
90752660ad
commit
6db0f6c1ad
10 files changed
+81
-13
No files matched your search
@@ -42,9 +42,15 @@ mod variables;
|
||||
|
||||
pub use variables::{MAX_HANDLES, STALE_DATASET, STALE_VARIABLE};
|
||||
|
||||
/// What [`after_command`] compares with, taken before each command a script runs
|
||||
/// ([`variables::before_command`]).
|
||||
pub(super) fn before_command(h: &Host, id: &str) -> Option<(u64, Vec<crate::doc::DataSet>)> {
|
||||
variables::before_command(h, id)
|
||||
}
|
||||
|
||||
/// After each command a script runs ([`variables::after_command`]).
|
||||
pub(super) fn after_command(h: &Host, id: &str) {
|
||||
variables::after_command(h, id);
|
||||
pub(super) fn after_command(h: &Host, before: Option<(u64, Vec<crate::doc::DataSet>)>) {
|
||||
variables::after_command(h, before);
|
||||
}
|
||||
|
||||
/// The error for a member read or called with no document open.
|
||||
|
||||
@@ -46,7 +46,7 @@ use super::{
|
||||
Answer, Doc, Host, Link, MAX_LISTED, MAX_TEXT_BYTES, NOT_FOUND, arg, class, command, doc_of, doc_ref, edit_item, find_doc, float, function, get,
|
||||
handle_of, item_of, item_ref, method, named, num, on_doc, property, text, typename, whole, with_doc,
|
||||
};
|
||||
use crate::doc::{Node, Variable, VariableKind};
|
||||
use crate::doc::{DataSet, Node, Variable, VariableKind};
|
||||
use crate::file_access::Access;
|
||||
|
||||
/// The error for a `Variable` whose variable left the document, or that came from an earlier run.
|
||||
@@ -138,16 +138,28 @@ pub(super) struct Handles {
|
||||
seen: RefCell<Vec<(u64, u64)>>,
|
||||
}
|
||||
|
||||
/// The data sets of the active document before a command that may replace them (an import), while
|
||||
/// the run holds a `Dataset`; `None` for any other command.
|
||||
pub(super) fn before_command(h: &Host, id: &str) -> Option<(u64, Vec<DataSet>)> {
|
||||
if !matches!(id, "dataset.import" | "variable.loadLibrary" | "variable.import") || h.dom.vars.sets.borrow().live.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let s = h.session.try_borrow().ok()?;
|
||||
s.active().map(|st| (st.uid, st.doc.variables.datasets.clone()))
|
||||
}
|
||||
|
||||
/// After each command a script runs: the objects whose variable or data set it removed stop
|
||||
/// working, so one made again under the same name does not bring them back. A command that replaces
|
||||
/// the data sets (an import) stops every `Dataset` of its document.
|
||||
pub(super) fn after_command(h: &Host, id: &str) {
|
||||
/// working, so one made again under the same name does not bring them back. An import that leaves
|
||||
/// its document with other data sets than `before` ([`before_command`]) holds stops every `Dataset`
|
||||
/// of that document, also when the new data sets reuse the names; an import that failed, was put
|
||||
/// back or left the data sets as they were stops none.
|
||||
pub(super) fn after_command(h: &Host, before: Option<(u64, Vec<DataSet>)>) {
|
||||
let hs = &h.dom.vars;
|
||||
if hs.vars.borrow().live.is_empty() && hs.sets.borrow().live.is_empty() {
|
||||
return;
|
||||
}
|
||||
let Ok(s) = h.session.try_borrow() else { return };
|
||||
let replaced = matches!(id, "dataset.import" | "variable.loadLibrary" | "variable.import").then(|| s.active().map(|st| st.uid)).flatten();
|
||||
let replaced = before.and_then(|(uid, sets)| s.documents().iter().find(|d| d.uid == uid && d.doc.variables.datasets != sets).map(|d| d.uid));
|
||||
let now: Vec<(u64, u64)> = s.documents().iter().map(|d| (d.uid, d.revision)).collect();
|
||||
let changed: HashSet<u64> = {
|
||||
let seen = hs.seen.borrow();
|
||||
|
||||
@@ -1313,6 +1313,7 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
let params = params.clone();
|
||||
let session = host.session.clone();
|
||||
let started = clock();
|
||||
let before = dom::before_command(host, &id);
|
||||
let r = {
|
||||
let mut s = session.borrow_mut();
|
||||
step(&mut s, host, &id, ¶ms)
|
||||
@@ -1346,7 +1347,7 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<(
|
||||
return Err(HostError::Abort(HISTORY_LOST.to_string()));
|
||||
}
|
||||
}
|
||||
dom::after_command(host, &id);
|
||||
dom::after_command(host, before);
|
||||
crossed.map(|data| (data, value))
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user