M12: the editor follows each focus script in turn, so an exit then an enter that both change the field leave it on the last value

This commit is contained in:
Eric Kaden committed 2026-10-23 23:20:30 -04:00
1 parent be088865fc
commit 7e00c61c7f
3 files changed
+19 -5

No files matched your search

+5 -3
View File
@@ -485,11 +485,13 @@ impl PdfCraftApp {
}
let left = std::mem::replace(&mut view.forms.focused, now.clone());
let id = view.id;
// The edited field's value before the scripts, to tell its untouched draft from typing.
let before: Option<Vec<String>> =
now.as_ref().and_then(|(name, _)| self.session.get(id)?.form.iter().find(|f| &f.name == name).map(|f| f.value.clone()));
let edited = now.as_ref().map(|(name, _)| name.clone());
use pdfcraft_engine::js::FieldFocus;
for ((name, widget), focus) in [left.map(|l| (l, FieldFocus::Exit)), now.map(|n| (n, FieldFocus::Enter))].into_iter().flatten() {
// The edited field's value before this script, to tell its untouched draft from typing:
// the exit of the field left may have changed it already, and the draft with it.
let before: Option<Vec<String>> =
edited.as_ref().and_then(|e| self.session.get(id)?.form.iter().find(|f| &f.name == e).map(|f| f.value.clone()));
match self.session.field_focus(id, &name, widget, focus) {
Ok(changed) => {
if changed
+11 -1
View File
@@ -190,7 +190,8 @@ fn xfa_enter_and_exit_scripts_run_whenever_the_focus_moves() {
/// An enter script that changes the field it enters (clearing a "Type here") shows in the editor,
/// so leaving the field keeps the script's value rather than writing the old one back; typing
/// after it is kept as usual.
/// after it is kept as usual. When the exit of the field left changed it first (`relay` puts the
/// "Type here" back), the editor follows both scripts and ends on the enter script's value.
#[test]
fn an_enter_script_that_changes_its_field_shows_in_the_editor() {
use egui::{Key, Modifiers};
@@ -213,6 +214,15 @@ fn an_enter_script_that_changes_its_field_shows_in_the_editor() {
h.key_press_modifiers(Modifiers::NONE, Key::Tab);
h.run_steps(4);
assert_eq!(value(&h), "Ann");
// From `relay` back to `hint`: relay's exit puts "Type here" back over "Ann", then hint's enter
// clears it. The editor follows both, so it holds the last value, and Tab keeps it.
click_field(&mut h, "relay");
click_field(&mut h, "hint");
assert_eq!(value(&h), "", "relay's exit, then hint's enter");
assert_eq!(h.state().views[0].forms.focus.as_ref().map(|f| f.text.clone()).as_deref(), Some(""), "the editor holds the enter script's value");
h.key_press_modifiers(Modifiers::NONE, Key::Tab);
h.run_steps(4);
assert_eq!(value(&h), "");
}
#[test]
+3 -1
View File
@@ -662,7 +662,8 @@ if (this.rawValue == "1") {
/// A form whose text field `probe` counts its enters in `enters` and its exits in `exits`, whose
/// text field `quiet` only shows a message when it is left, and whose radio button `size.large`
/// counts its enters in `picks` and its exits in `drops` (each button of a radio group is a field
/// with scripts of its own), and whose text field `hint` clears its "Type here" when it gains focus.
/// with scripts of its own), whose text field `hint` clears its "Type here" when it gains focus, and
/// whose text field `relay` puts that "Type here" back in `hint` when it loses focus.
pub fn focus_template() -> String {
let js = "application/x-javascript";
let count = |name: &str| format!(r#"<script contentType="{js}">{name}.rawValue = Number({name}.rawValue) + 1;</script>"#);
@@ -682,6 +683,7 @@ pub fn focus_template() -> String {
</exclGroup>
{}{}{}{}
<field name="hint" w="2in" h="0.3in"><ui><textEdit/></ui><value><text>Type here</text></value><event activity="enter"><script contentType="{js}">if (this.rawValue == "Type here") this.rawValue = "";</script></event></field>
<field name="relay" w="2in" h="0.3in"><ui><textEdit/></ui><event activity="exit"><script contentType="{js}">hint.rawValue = "Type here";</script></event></field>
</subform>
</template>
</xdp:xdp>