Files
docx-js/src/patcher/relationship-manager.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-03 23:47:50 +00:00
import { Element } from "xml-js";
2023-03-08 23:30:51 +00:00
import { RelationshipType, TargetModeType } from "@file/relationships/relationship/relationship";
2023-03-03 23:47:50 +00:00
import { getFirstLevelElements } from "./util";
const getIdFromRelationshipId = (relationshipId: string): number => parseInt(relationshipId.substring(3), 10);
export const getNextRelationshipIndex = (relationships: Element): number => {
const relationshipElements = getFirstLevelElements(relationships, "Relationships");
return (
(relationshipElements
.map((e) => getIdFromRelationshipId(e.attributes?.Id?.toString() ?? ""))
.reduce((acc, curr) => Math.max(acc, curr), 0) ?? 0) + 1
);
};
2023-03-08 23:30:51 +00:00
export const appendRelationship = (
relationships: Element,
id: number | string,
type: RelationshipType,
target: string,
targetMode?: TargetModeType,
): void => {
2023-03-03 23:47:50 +00:00
const relationshipElements = getFirstLevelElements(relationships, "Relationships");
// eslint-disable-next-line functional/immutable-data
relationshipElements.push({
attributes: {
Id: `rId${id}`,
Type: type,
Target: target,
2023-03-08 23:30:51 +00:00
TargetMode: targetMode,
2023-03-03 23:47:50 +00:00
},
name: "Relationship",
type: "element",
});
};