Okay, I think I have a reasonable grip on what’s going on. let’s walk it through.,
The initial template population action runs against a draft, let’s call it D1 for convenience, but the draft it builds is another draft, let’s call it D2. At the end of that action D1 is unloaded from the Drafts editor, and D2 is loaded in its place.
Now the action is still running against the original D1 draft when the second included action is triggered.
The action contains the following code.
// Function to insert cursor after defined string in draft
// uses function to escape any special characters possibly in the string
function cursorInsert(stringToInsertAfter) {
var textToFind = stringToInsertAfter;
var stringToGoIntoTheRegex = escapeRegExp(textToFind); //escape the string
var textName = new RegExp(stringToGoIntoTheRegex); //regex for the string
var textLength = textToFind.length;
var d = draft.content;
var textStart = d.search(textName); //find start of the string
editor.setSelectedRange(textStart+textLength,""); //insert cursor at string end
editor.activate();
}
What I’m going to focus on initially are the following lines:
var d = draft.content;
var textStart = d.search(textName); //find start of the string
We can see d
being set to the content of draft
. Now draft
is the draft the overall action was being run against, which is D1. Importantly, that is not D2, which is the draft we want it to be running against.
The first mistake is essentially that even though this function is operating on the editor, the code initialises based on the draft; which we can see is not always the same thing.
A simple change to the code to use the editor object instead resolves this and gets more or less everything working.
// Function to insert cursor after defined string in draft
// uses function to escape any special characters possibly in the string
function cursorInsert(stringToInsertAfter) {
var textToFind = stringToInsertAfter;
var stringToGoIntoTheRegex = escapeRegExp(textToFind); //escape the string
var textName = new RegExp(stringToGoIntoTheRegex); //regex for the string
var textLength = textToFind.length;
let textStart = editor.getText().search(textName); //find start of the string
editor.setSelectedRange(textStart+textLength,""); //insert cursor at string end
editor.activate();
}
But, that isn’t quite the whole picture. When I originally ran the action, I got this in the first couple of lines.
**D Sundayaily Stuff**
Date:2021-02-21
i.e. Sunday
had being inserted after the first three characters of the editor content (draft D2).
The logical assumption might then be that the original draft (draft D1) had “Day:” at the start of it (“Day:” being the parameter passed into the function above, but that’s four characters long, and my D1 was actually blank.
Repeating but with a starting draft containing “Day:”, the insertion was applied at the expected location. Therefore, the issue lies when “Day:” isn’t found.
The key point is that the search()
function returns -1
if the searched for text is not found. But that isn’t catered for in the function above, and since the length of “Day:” is four characters, that gives us a positive offset of three characters, hence the insertion three characters after the start of the editor content. While this might not matter too much with the fix above as the template would always have that text in, when run stand alone, that is not always the case.
On that basis , I think that the code for this action, could be amended and rationalised to something like the following.
// Function returns current day of week
function findDayofWeek() {
let d = new Date();
let dayOfWeek = [" Sunday", " Monday", " Tuesday", " Wednesday", " Thursday", " Friday", " Saturday"];
return dayOfWeek[d.getDay()]; //current day of week
}
// Function to escape special characters
function escapeRegExp(stringToGoIntoTheRegex) {
return stringToGoIntoTheRegex.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
// Function to insert cursor after defined string in draft
// uses function to escape any special characters possibly in the string
function cursorInsert(stringToInsertAfter) {
let textName = new RegExp(escapeRegExp(stringToInsertAfter)); //regex for the string
let textStart = editor.getText().search(textName); //find start of the string
if(textStart == -1) return false;
editor.setSelectedRange(textStart + stringToInsertAfter.length, ""); //insert cursor at string end
return true;
}
let stringToInsertAfter = "Day:"
if (cursorInsert(stringToInsertAfter)) editor.setSelectedText(findDayofWeek());
editor.activate();
I think that covers everything for this particular instance. It may be that other actions are giving you issues for different reasons. If you can’t spot why, post back with the details.