#2028 Fix patcher offset indexes

This commit is contained in:
Dolan Miu
2023-03-23 21:01:24 +00:00
parent 528008406a
commit 16bfc78d8d
5 changed files with 173 additions and 2 deletions

View File

@ -30,9 +30,15 @@ export const replaceTokenInParagraphElement = ({
switch (replaceMode) {
case ReplaceMode.START:
if (startIndex >= start) {
const partToReplace = run.text.substring(Math.max(startIndex, start), Math.min(endIndex, end) + 1);
const offsetStartIndex = startIndex - start;
const offsetEndIndex = Math.min(endIndex, end) - start;
const partToReplace = run.text.substring(offsetStartIndex, offsetEndIndex + 1);
// We use a token to split the text if the replacement is within the same run
// If not, we just add text to the middle of the run later
if (partToReplace === "") {
continue;
}
const firstPart = text.replace(partToReplace, replacementText);
patchTextElement(paragraphElement.elements![run.index].elements![index], firstPart);
replaceMode = ReplaceMode.MIDDLE;