Thanks again @ben-thompson . You're right. Actually registering on "input" doesn't work as I thought, since between the autosave start and the moment the form is refreshed, there are a few milliseconds, while the user is typing, so he would lose some chars.
I'll use the workarround you suggested: I register on "change" of the input, and in updateView() I ignore the value if it's the old one. So using this workarround the value is not lost after autosave, but it's still not saved until the user change the focus outside the control. So if another script on the form is navigating away or closing the window, the user loses the introduced text.
But I've checked: an out-of-the-box control doesn't their values, they automatically save it somehow. Since I have no influence on the other scripts in the form, I have to be sure that I don't lose the introduced data .
I've made a test by defining in the console the following function (it only saves the form if necessary, and closes the form). I try to simulate another scripts running on the form:
function checkSavingOnLeave(fieldName){
const value = Xrm.Page.getAttribute(fieldName).getValue();
console.log(`value at start: ${value}`);
window.setTimeout( async () => {
console.log(Xrm.Page.getAttribute(fieldName).getValue());
console.log(`dirty: ${Xrm.Page.data.getIsDirty()}`);
const saved = !Xrm.Page.data.getIsDirty() || await Xrm.Page.data.save();
if(saved===true) {
console.log(`value before close: ${Xrm.Page.getAttribute(fieldName).getValue()}`);
Xrm.Page.ui.close();
}
else
console.log("not saved");
}, 7000);
}
So I call the function for my pcf control (while the form is not dirty) then I go to my PCF control and I change the text, but don't leave the control for a few seconds, until my script is triggered.
I get this console output:
checkSavingOnLeave("orb_textarea")
value at start: PCF
PCF
dirty: false
value before close: PCF
The form didn't get the change, it gets closed, but the text I typed is lost.
If I call the same function for a standard input control (like the name control) I get the same log
checkSavingOnLeave("orb_name")
value at start: PCF
PCF
dirty: false
value before close: PCF
The difference is that the orb_name (which is a standard control) doesn't loose the changed text, even if the value after the Xrm.Page.data.save() is still the old one, while my PCF control loses the changed text.
Here is my updateView(), in case I do something wrong:
public updateView(context: ComponentFramework.Context<IInputs>): void
{
const maybeNewValue = context.parameters.myTextarea.raw || "";
if(maybeNewValue!== this.value){
this._inputWindowElement.value = maybeNewValue;
this.value = maybeNewValue;
console.log(`value set to ${maybeNewValue}`);
}
this._inputWindowElement.disabled = context.mode.isControlDisabled;
}
This is not a problem related only to textarea, it happens also with a text input.
It's a very simple example of PCF control: just an input control, nothing fancy. Since the standard controls are made using the same framework, Microsoft must have a solution for that already. Maybe somebody from the team can help me...