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

43 lines
1.3 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";
2023-03-16 01:55:18 +00:00
const getIdFromRelationshipId = (relationshipId: string): number => {
const output = parseInt(relationshipId.substring(3), 10);
return isNaN(output) ? 0 : output;
};
2023-03-03 23:47:50 +00:00
export const getNextRelationshipIndex = (relationships: Element): number => {
const relationshipElements = getFirstLevelElements(relationships, "Relationships");
return (
2023-03-16 01:55:18 +00:00
relationshipElements
2023-03-03 23:47:50 +00:00
.map((e) => getIdFromRelationshipId(e.attributes?.Id?.toString() ?? ""))
2023-03-16 01:55:18 +00:00
.reduce((acc, curr) => Math.max(acc, curr), 0) + 1
2023-03-03 23:47:50 +00:00
);
};
2023-03-08 23:30:51 +00:00
export const appendRelationship = (
relationships: Element,
id: number | string,
type: RelationshipType,
target: string,
targetMode?: TargetModeType,
2023-03-15 02:46:39 +00:00
): readonly Element[] => {
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",
});
2023-03-15 02:46:39 +00:00
return relationshipElements;
2023-03-03 23:47:50 +00:00
};