Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/excel-builder-vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"access": "public"
},
"type": "module",
"files": [
"dist"
],
"main": "./dist/index.js",
"exports": {
".": {
Expand Down
34 changes: 17 additions & 17 deletions packages/excel-builder-vanilla/src/Excel/Drawings.ts
Original file line number Diff line number Diff line change
@@ -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<RelationshipManager['getRelationshipId']>[0];
type RelationshipDrawing = Drawing & {
getMediaData?: () => RelationshipTarget;
getMediaType: () => Parameters<RelationshipManager['addRelation']>[1];
setRelationshipId: (rId: string) => void;
toXML: (doc: XMLDOM) => XMLNode;
};

/**
* @module Excel/Drawings
Expand Down Expand Up @@ -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<RelationshipDrawing>;
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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({
Expand All @@ -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('<c:chart '); // chart branch executed
Expand Down