Hello everyone!
I’m integrating API which will check text directly in indesign and give you recommendations to correct it. (like Grammarly).
So, after receiving response with typos my app starts to select words (typos) and highlight them with color. Each typo in UI have delete button, and while my plugin deleting typo from UI I also want to reset text highlighted in Indesign. For that I have this function:
function resetCharacterStyles(texts: Array<Text> ) {
const defaultCharStyle = app.activeDocument.characterStyles.itemByName("[None]");
texts.forEach((text) => {
if (!text.isValid) {
// Will have more logic to notify user if some text failed to reset highlighting (Maybe the deleted this text by themselves).
return;
}
app.select(text);
text.applyCharacterStyle(defaultCharStyle);
text.clearOverrides(indesign.OverrideType.CHARACTER_ONLY);
})
}
Here is the interesting part, lets say, user added few more words before highlighted typo. If I’m accessing isValid property in highlighted typo, the text object which I will select next will have updated reference… Let me explain it below.
// Let's say, plugin selected this sentence: "Your the one who should".
// Plugin stores selected typos in object appData = {typo: {...otherInfo, selectedInIndesign: object[] }}
const selection = appData.typo.selectedInIndesign[0];
console.log(selection.contents) // Your the one who should
// Now let's add few more words in Indesign. Our text in Indesign now looks like this -> "Added few more words Your the one who should".
// Still selects "Your the one who should." even though we've added more words and text shifted.
console.log(selection.contents)
// Accessing isValid property
selection.isValid
// Now it will show "Added few more words Your"
console.log(selection.contents)
Thats how it looks in Indesign.

Is this a bug or does isValid property works like that? And is there any other option to check text validity without making it to lose reference?
P.S: Sorry for possible mistakes.
Thank you.