Fix style being missed on patchDocument

There were scenarios in which patching a document would result in loss
of style for the template runs and, possibly, their right-adjacent run
as well (post-splitting). That was due to the run style elements not
being added to the newly created runs.

This commit addresses this issue by introducing a new, optional, flag
for the `patchDocument` function: `keepOriginalStyles`. It defaults to
`false` (current behavior) and, when `true`, ensures that there is no
loss of styling.

This should address https://github.com/dolanmiu/docx/issues/2293.
This commit is contained in:
Wilk Maia
2023-10-18 12:58:01 -03:00
parent d5d80550e7
commit a89ee463e6
3 changed files with 134 additions and 2 deletions

View File

@ -20,6 +20,7 @@ export const replacer = (
patchText: string,
renderedParagraphs: readonly IRenderedParagraphNode[],
context: IContext,
keepOriginalStyles: boolean = false,
): Element => {
for (const renderedParagraph of renderedParagraphs) {
const textJson = patch.children
@ -47,9 +48,29 @@ export const replacer = (
const index = findRunElementIndexWithToken(paragraphElement, SPLIT_TOKEN);
const { left, right } = splitRunElement(paragraphElement.elements![index], SPLIT_TOKEN);
const runElementToBeReplaced = paragraphElement.elements![index];
const { left, right } = splitRunElement(runElementToBeReplaced, SPLIT_TOKEN);
let newRunElements = textJson;
let patchedRightElement = right;
if (keepOriginalStyles) {
const runElementNonTextualElements =
runElementToBeReplaced.elements?.filter((e) => e.type === "element" && e.name !== "w:t") ?? [];
newRunElements = textJson.map((e) => ({
...e,
elements: [...runElementNonTextualElements, ...(e.elements ?? [])],
}));
patchedRightElement = {
...right,
elements: [...runElementNonTextualElements, ...(right.elements ?? [])],
};
}
// eslint-disable-next-line functional/immutable-data
paragraphElement.elements!.splice(index, 1, left, ...textJson, right);
paragraphElement.elements!.splice(index, 1, left, ...newRunElements, patchedRightElement);
break;
}
}