Files
docx-js/src/patcher/replacer.ts

75 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-18 20:36:24 +00:00
import { Element } from "xml-js";
import * as xml from "xml";
2023-02-23 19:43:19 +00:00
import { Formatter } from "@export/formatter";
2023-02-25 19:33:12 +00:00
import { XmlComponent } from "@file/xml-components";
2023-02-23 19:43:19 +00:00
2023-02-25 19:33:12 +00:00
import { IPatch, PatchType } from "./from-docx";
2023-02-18 20:36:24 +00:00
import { toJson } from "./util";
import { IRenderedParagraphNode } from "./run-renderer";
2023-02-23 19:43:19 +00:00
import { replaceTokenInParagraphElement } from "./paragraph-token-replacer";
2023-02-24 01:05:43 +00:00
import { findRunElementIndexWithToken, splitRunElement } from "./paragraph-split-inject";
2023-02-18 20:36:24 +00:00
const formatter = new Formatter();
2023-02-16 20:17:48 +00:00
2023-02-23 19:43:19 +00:00
const SPLIT_TOKEN = "ɵ";
2023-02-25 22:18:56 +00:00
export const replacer = (
json: Element,
patch: IPatch,
patchText: string,
renderedParagraphs: readonly IRenderedParagraphNode[],
): Element => {
2023-02-25 19:33:12 +00:00
for (const renderedParagraph of renderedParagraphs) {
2023-02-25 22:18:31 +00:00
const textJson = patch.children.map((c) => toJson(xml(formatter.format(c as XmlComponent)))).map((c) => c.elements![0]);
2023-02-25 19:33:12 +00:00
2023-02-25 22:18:31 +00:00
if (patch.type === PatchType.DOCUMENT) {
2023-02-25 19:33:12 +00:00
const parentElement = goToParentElementFromPath(json, renderedParagraph.path);
const elementIndex = getLastElementIndexFromPath(renderedParagraph.path);
// eslint-disable-next-line functional/immutable-data, prefer-destructuring
parentElement.elements?.splice(elementIndex, 1, ...textJson);
2023-02-25 22:18:31 +00:00
} else if (patch.type === PatchType.PARAGRAPH) {
2023-02-25 19:33:12 +00:00
const paragraphElement = goToElementFromPath(json, renderedParagraph.path);
2023-02-25 22:18:31 +00:00
replaceTokenInParagraphElement({
paragraphElement,
renderedParagraph,
originalText: patchText,
replacementText: SPLIT_TOKEN,
});
const index = findRunElementIndexWithToken(paragraphElement, SPLIT_TOKEN);
const { left, right } = splitRunElement(paragraphElement.elements![index], SPLIT_TOKEN);
// eslint-disable-next-line functional/immutable-data
paragraphElement.elements!.splice(index, 1, left, ...textJson, right);
2023-02-16 20:17:48 +00:00
}
}
return json;
};
2023-02-18 20:36:24 +00:00
const goToElementFromPath = (json: Element, path: readonly number[]): Element => {
let element = json;
// We start from 1 because the first element is the root element
// Which we do not want to double count
for (let i = 1; i < path.length; i++) {
const index = path[i];
const nextElements = element.elements;
if (!nextElements) {
throw new Error("Could not find element");
}
element = nextElements[index];
}
return element;
};
2023-02-25 19:33:12 +00:00
const goToParentElementFromPath = (json: Element, path: readonly number[]): Element =>
goToElementFromPath(json, path.slice(0, path.length - 1));
const getLastElementIndexFromPath = (path: readonly number[]): number => path[path.length - 1];