From f855b5fd25e88d159fb095a9aa85e7a2b381c23a Mon Sep 17 00:00:00 2001 From: MarsLuay <70299537+MarsLuay@users.noreply.github.com> Date: Tue, 25 Aug 2026 09:27:56 +0000 Subject: [PATCH] refactor(shape-clipboard): flatten deeply nested relationship copying logic --- src/ShapeClipboard.ts | 73 ++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/src/ShapeClipboard.ts b/src/ShapeClipboard.ts index 5900d9c7..82a69ff7 100644 --- a/src/ShapeClipboard.ts +++ b/src/ShapeClipboard.ts @@ -242,29 +242,66 @@ async function copyShapeRelationships( : createRelationshipsDocument(); for (const attribute of relationshipAttributes) { - const sourceRelationship = findRelationship(sourceRelationships, attribute.value); - if (!sourceRelationship) continue; + await copySingleShapeRelationship( + attribute, + sourceRelationships, + destinationRelationships, + sourceSlidePath, + destinationSlidePath, + context + ); + } - const clonedRelationship = destinationRelationships.importNode(sourceRelationship, true); - const relationshipId = nextRelationshipId(destinationRelationships); - clonedRelationship.setAttribute('Id', relationshipId); - attribute.value = relationshipId; + context.textModifications.set(destinationRelationshipsPath, serializeXml(destinationRelationships)); +} - const target = clonedRelationship.getAttribute('Target'); - if (target && clonedRelationship.getAttribute('TargetMode') !== 'External') { - const sourceTargetPath = resolvePartPath(sourceSlidePath, target); - const destinationTargetPath = await ensureRelatedPart( - sourceTargetPath, - context, - isChartRelationship(clonedRelationship) - ); - clonedRelationship.setAttribute('Target', getRelativePartPath(destinationSlidePath, destinationTargetPath)); - } +async function copySingleShapeRelationship( + attribute: Attr, + sourceRelationships: XMLDocument, + destinationRelationships: XMLDocument, + sourceSlidePath: string, + destinationSlidePath: string, + context: PasteContext +): Promise { + const sourceRelationship = findRelationship(sourceRelationships, attribute.value); + if (!sourceRelationship) return; + + const clonedRelationship = destinationRelationships.importNode(sourceRelationship, true); + const relationshipId = nextRelationshipId(destinationRelationships); + clonedRelationship.setAttribute('Id', relationshipId); + attribute.value = relationshipId; + + await updateRelationshipTarget( + clonedRelationship, + sourceSlidePath, + destinationSlidePath, + context + ); + + destinationRelationships.documentElement.appendChild(clonedRelationship); +} - destinationRelationships.documentElement.appendChild(clonedRelationship); +async function updateRelationshipTarget( + clonedRelationship: Element, + sourceSlidePath: string, + destinationSlidePath: string, + context: PasteContext +): Promise { + const target = clonedRelationship.getAttribute('Target'); + if (!target || clonedRelationship.getAttribute('TargetMode') === 'External') { + return; } - context.textModifications.set(destinationRelationshipsPath, serializeXml(destinationRelationships)); + const sourceTargetPath = resolvePartPath(sourceSlidePath, target); + const destinationTargetPath = await ensureRelatedPart( + sourceTargetPath, + context, + isChartRelationship(clonedRelationship) + ); + clonedRelationship.setAttribute( + 'Target', + getRelativePartPath(destinationSlidePath, destinationTargetPath) + ); } function getRelationshipAttributes(element: Element): Attr[] {