diff --git a/packages/excel-builder-vanilla/package.json b/packages/excel-builder-vanilla/package.json index 49e55c1..fbc8217 100644 --- a/packages/excel-builder-vanilla/package.json +++ b/packages/excel-builder-vanilla/package.json @@ -31,6 +31,9 @@ "access": "public" }, "type": "module", + "files": [ + "dist" + ], "main": "./dist/index.js", "exports": { ".": { diff --git a/packages/excel-builder-vanilla/src/Excel/Drawings.ts b/packages/excel-builder-vanilla/src/Excel/Drawings.ts index e895f82..abfd761 100644 --- a/packages/excel-builder-vanilla/src/Excel/Drawings.ts +++ b/packages/excel-builder-vanilla/src/Excel/Drawings.ts @@ -1,9 +1,16 @@ import { uniqueId } from '../utilities/uniqueId.js'; -import { Chart } from './Drawing/Chart.js'; import type { Drawing } from './Drawing/Drawing.js'; -import { Picture } from './Drawing/Picture.js'; import { RelationshipManager } from './RelationshipManager.js'; import { Util } from './Util.js'; +import type { XMLDOM, XMLNode } from './XMLDOM.js'; + +type RelationshipTarget = Parameters[0]; +type RelationshipDrawing = Drawing & { + getMediaData?: () => RelationshipTarget; + getMediaType: () => Parameters[1]; + setRelationshipId: (rId: string) => void; + toXML: (doc: XMLDOM) => XMLNode; +}; /** * @module Excel/Drawings @@ -36,22 +43,15 @@ export class Drawings { drawings.setAttribute('xmlns:xdr', Util.schemas.spreadsheetDrawing); for (let i = 0, l = this.drawings.length; i < l; i++) { - const item = this.drawings[i]; - if (item instanceof Picture) { - let rId = this.relations.getRelationshipId(item.getMediaData()); - if (!rId) { - rId = this.relations.addRelation(item.getMediaData(), item.getMediaType()); - } - item.setRelationshipId(rId); - drawings.appendChild(item.toXML(doc)); - } else if (item instanceof Chart) { - let rId = this.relations.getRelationshipId(item); - if (!rId) { - rId = this.relations.addRelation(item, item.getMediaType()); - } - item.setRelationshipId(rId); - drawings.appendChild(item.toXML(doc)); + const item = this.drawings[i] as Partial; + if (!item.getMediaType || !item.setRelationshipId || !item.toXML) { + continue; } + const target = (item.getMediaData?.() ?? item) as RelationshipTarget; + const mediaType = item.getMediaType(); + const rId = this.relations.getRelationshipId(target) || this.relations.addRelation(target, mediaType); + item.setRelationshipId(rId); + drawings.appendChild(item.toXML(doc)); } return doc; } diff --git a/packages/excel-builder-vanilla/src/Excel/__tests__/Drawings.spec.ts b/packages/excel-builder-vanilla/src/Excel/__tests__/Drawings.spec.ts index 36039fb..31be38e 100644 --- a/packages/excel-builder-vanilla/src/Excel/__tests__/Drawings.spec.ts +++ b/packages/excel-builder-vanilla/src/Excel/__tests__/Drawings.spec.ts @@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest'; import { createWorkbook } from '../../factory.js'; import { Chart } from '../Drawing/Chart.js'; +import { Drawing } from '../Drawing/Drawing.js'; import { Picture } from '../Drawing/Picture.js'; import { Drawings } from '../Drawings.js'; import { Positioning } from '../Positioning.js'; @@ -64,9 +65,12 @@ describe('Drawings', () => { const file = await fruitWorkbook.generateFiles(); const dwgs = fruitWorkbook.drawings; + const drawingRelations = Object.values(drawings.relations.relations); expect(file).toBeTruthy(); expect(dwgs[0].drawings.length).toBe(3); + expect(drawingRelations).toHaveLength(1); + expect(drawingRelations[0].object).toBe(picRef1); // print titles offset of 2 => left B and top 2 fruitWorkbook.setPrintTitleLeft('sheet1', 2); @@ -92,6 +96,13 @@ describe('Drawings', () => { expect(() => d.toXML()).not.toThrow(); }); + test('toXML ignores unsupported base drawings', () => { + const d = new Drawings(); + d.addDrawing(new Drawing()); + + expect(d.toXML().documentElement.children).toHaveLength(0); + }); + test('toXML chart branch assigns relationship and appends XML', () => { const d = new Drawings(); const chart = new Chart({ @@ -104,6 +115,7 @@ describe('Drawings', () => { d.addDrawing(chart); const xmlDoc = d.toXML(); expect(chart.relId).toMatch(/^rId\d+$/); + expect(Object.values(d.relations.relations)[0].object).toBe(chart); const xmlStr = xmlDoc.toString(); expect(xmlStr).toContain('ChartRel'); expect(xmlStr).toContain('