mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 14:34:34 +00:00
Merge pull request #8624 from dotCooCoo/fix/followup-8408
Type: the missing-font question takes its answer through tool.setOption
This commit is contained in:
@@ -2236,6 +2236,22 @@ fn a_click_on_type_in_a_missing_font_asks_before_editing() {
|
||||
assert_eq!((tool.editing, tool.caret), (Some(id), 1));
|
||||
}
|
||||
|
||||
/// `tool.setOption` checks each option first: the answer to the question is a whole-number token
|
||||
/// and a true or false `ok`, so a client can give it through that command.
|
||||
#[test]
|
||||
fn the_answer_to_the_missing_font_question_passes_the_option_check() {
|
||||
let tool = TypeTool::new("type");
|
||||
assert_eq!(tool.check_option("missingFontEdit", &json!({"token": 7, "ok": true})), Ok(()));
|
||||
assert_eq!(tool.check_option("missingFontEdit", &json!({"token": 7, "ok": false})), Ok(()));
|
||||
for bad in [json!({"token": "7", "ok": true}), json!({"token": 7, "ok": 1}), json!({"ok": true}), json!({"token": -1, "ok": true}), json!(null)] {
|
||||
assert_eq!(
|
||||
tool.check_option("missingFontEdit", &bad),
|
||||
Err("`missingFontEdit` must be {token, ok} with a whole number and true or false".to_string()),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel starts no edit, and the edit in progress on other type goes on; an answer that names
|
||||
/// another question, or one given to a document changed since, starts nothing either.
|
||||
#[test]
|
||||
|
||||
@@ -629,7 +629,8 @@ impl Tool for TypeTool {
|
||||
}
|
||||
}
|
||||
/// The calls [`Self::set_option`] takes: `commitTyping`, `endTyping`, `selectAll` and
|
||||
/// `stopEditing` take any value, the others the shapes they read.
|
||||
/// `stopEditing` take any value, the others the shapes they read (`missingFontEdit` a whole-number
|
||||
/// `token` and a true or false `ok`).
|
||||
fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
|
||||
let index = |a: &str, b: &str| v.get(a).or_else(|| v.get(b)).and_then(Value::as_u64).is_some();
|
||||
let ok = match key {
|
||||
@@ -638,11 +639,13 @@ impl Tool for TypeTool {
|
||||
"prevTool" | "previousTool" => return crate::option_check::string_or_null(key, v),
|
||||
"copy" => serde_json::from_value::<Vec<TextRun>>(v.clone()).is_ok(),
|
||||
"commitTyping" | "endTyping" | "selectAll" | "stopEditing" => true,
|
||||
"missingFontEdit" => v.get("token").and_then(Value::as_u64).is_some() && v.get("ok").and_then(Value::as_bool).is_some(),
|
||||
_ => return Err(crate::option_check::unknown(key)),
|
||||
};
|
||||
let shape = match key {
|
||||
"textSelection" => "{editing, anchor, caret} with whole numbers",
|
||||
"select" => "{start, end} with whole numbers",
|
||||
"missingFontEdit" => "{token, ok} with a whole number and true or false",
|
||||
_ => "a list of text runs",
|
||||
};
|
||||
if ok { Ok(()) } else { Err(format!("`{key}` must be {shape}")) }
|
||||
|
||||
Reference in New Issue
Block a user