Skip to content

Commit 475c824

Browse files
authored
Merge pull request #89 from ghiscoding/chore/more-interface
chore: add more interfaces/types
2 parents ff8f878 + 56e5aa0 commit 475c824

File tree

9 files changed

+87
-62
lines changed

9 files changed

+87
-62
lines changed

packages/demo/src/examples/example13.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class Example {
3838
albumTable.setReferenceRange([1, 1], [3, originalData.length]);
3939
albumTable.setTableColumns(['Artist', 'Album', 'Price']);
4040

41-
// worksheet.sheetView.showGridLines = false;
41+
worksheet.sheetView.showGridLines = false;
4242
worksheet.setData(originalData);
4343
workbook.addWorksheet(worksheet);
4444

packages/excel-builder-vanilla/src/Excel/Drawing/AbsoluteAnchor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AnchorOption } from './Drawing.js';
12
import { Util } from '../Util.js';
23
import type { XMLDOM } from '../XMLDOM.js';
34

@@ -16,7 +17,7 @@ export class AbsoluteAnchor {
1617
width: number | null = null;
1718
height: number | null = null;
1819

19-
constructor(config: any) {
20+
constructor(config: AnchorOption) {
2021
if (config) {
2122
this.setPos(config.x, config.y);
2223
this.setDimensions(config.width, config.height);

packages/excel-builder-vanilla/src/Excel/Drawing/Drawing.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,60 @@
11
import { uniqueId } from '../../utilities/uniqueId.js';
22
import { AbsoluteAnchor } from './AbsoluteAnchor.js';
3-
import { Chart } from './Chart.js';
43
import { OneCellAnchor } from './OneCellAnchor.js';
54
import { TwoCellAnchor } from './TwoCellAnchor.js';
65
// import { Picture } from './Picture.js';
76

7+
export interface AnchorOption {
8+
/** X offset in EMU's */
9+
x: number;
10+
/** Y offset in EMU's */
11+
y: number;
12+
xOff?: boolean;
13+
yOff?: boolean;
14+
/** Width in EMU's */
15+
height: number;
16+
/** Height in EMU's */
17+
width: number;
18+
drawing?: Drawing;
19+
}
20+
21+
export interface DualAnchorOption {
22+
to: AnchorOption;
23+
from: AnchorOption;
24+
drawing?: Drawing;
25+
}
26+
827
/**
928
* This is mostly a global spot where all of the relationship managers can get and set
1029
* path information from/to.
1130
* @module Excel/Drawing
1231
*/
1332
export class Drawing {
14-
anchor: any;
33+
anchor!: AbsoluteAnchor | OneCellAnchor | TwoCellAnchor;
1534
id = uniqueId('Drawing');
1635

17-
get AbsoluteAnchor() {
18-
return AbsoluteAnchor;
19-
}
20-
get Chart() {
21-
return Chart;
22-
}
23-
get OneCellAnchor() {
24-
return OneCellAnchor;
25-
}
26-
// get Picture() {
27-
// return Picture;
28-
// }
29-
get TwoCellAnchor() {
30-
return TwoCellAnchor;
31-
}
32-
3336
/**
3437
*
3538
* @param {String} type Can be 'absoluteAnchor', 'oneCellAnchor', or 'twoCellAnchor'.
3639
* @param {Object} config Shorthand - pass the created anchor coords that can normally be used to construct it.
3740
* @returns {Anchor}
3841
*/
39-
createAnchor(type: string, config: any) {
40-
config = config || {};
42+
// TODO: couldn't get function override working, but hopefully in the future
43+
// createAnchor(type: 'absoluteAnchor', config: AnchorOption): AbsoluteAnchor;
44+
// createAnchor(type: 'oneCellAnchor', config: AnchorOption): OneCellAnchor;
45+
// createAnchor(type: 'twoCellAnchor', config: DualAnchorOption): TwoCellAnchor;
46+
createAnchor(type: 'absoluteAnchor' | 'oneCellAnchor' | 'twoCellAnchor', config: any): AbsoluteAnchor | OneCellAnchor | TwoCellAnchor {
47+
config ??= {} as AnchorOption | DualAnchorOption;
4148
config.drawing = this;
4249
switch (type) {
4350
case 'absoluteAnchor':
44-
this.anchor = new AbsoluteAnchor(config);
51+
this.anchor = new AbsoluteAnchor(config as AnchorOption);
4552
break;
4653
case 'oneCellAnchor':
47-
this.anchor = new OneCellAnchor(config);
54+
this.anchor = new OneCellAnchor(config as AnchorOption);
4855
break;
4956
case 'twoCellAnchor':
50-
this.anchor = new TwoCellAnchor(config);
57+
this.anchor = new TwoCellAnchor(config as DualAnchorOption);
5158
break;
5259
}
5360
return this.anchor;

packages/excel-builder-vanilla/src/Excel/Drawing/OneCellAnchor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AnchorOption } from './Drawing.js';
12
import { Util } from '../Util.js';
23
import type { XMLDOM } from '../XMLDOM.js';
34

@@ -18,7 +19,7 @@ export class OneCellAnchor {
1819
width: number | null = null;
1920
height: number | null = null;
2021

21-
constructor(config: any) {
22+
constructor(config: AnchorOption) {
2223
if (config) {
2324
this.setPos(config.x, config.y, config.xOff, config.yOff);
2425
this.setDimensions(config.width, config.height);

packages/excel-builder-vanilla/src/Excel/Drawing/Picture.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import { Drawing } from './Drawing.js';
12
import { uniqueId } from '../../utilities/uniqueId.js';
23
import { Util } from '../Util.js';
4+
import type { MediaMeta } from '../Workbook.js';
35
import type { XMLDOM } from '../XMLDOM.js';
4-
import { Drawing } from './Drawing.js';
56

67
export class Picture extends Drawing {
7-
media: any = null;
88
id = uniqueId('Picture');
99
pictureId = Util.uniqueId('Picture');
1010
fill: any = {};
11-
mediaData: any = null;
11+
mediaData: MediaMeta | null = null;
1212
description = '';
1313

1414
constructor() {
@@ -18,7 +18,7 @@ export class Picture extends Drawing {
1818
this.pictureId = Util.uniqueId('Picture');
1919
}
2020

21-
setMedia(mediaRef: any) {
21+
setMedia(mediaRef: MediaMeta) {
2222
this.mediaData = mediaRef;
2323
}
2424

@@ -39,11 +39,11 @@ export class Picture extends Drawing {
3939
}
4040

4141
getMediaData() {
42-
return this.mediaData;
42+
return this.mediaData as MediaMeta;
4343
}
4444

4545
setRelationshipId(rId: string) {
46-
this.mediaData.rId = rId;
46+
this.mediaData!.rId = rId;
4747
}
4848

4949
toXML(xmlDoc: XMLDOM) {
@@ -53,7 +53,7 @@ export class Picture extends Drawing {
5353

5454
const nameProperties = Util.createElement(xmlDoc, 'xdr:cNvPr', [
5555
['id', this.pictureId],
56-
['name', this.mediaData.fileName],
56+
['name', this.mediaData!.fileName],
5757
['descr', this.description || ''],
5858
]);
5959
nonVisibleProperties.appendChild(nameProperties);
@@ -70,7 +70,7 @@ export class Picture extends Drawing {
7070
pictureFill.appendChild(
7171
Util.createElement(xmlDoc, 'a:blip', [
7272
['xmlns:r', Util.schemas.relationships],
73-
['r:embed', this.mediaData.rId],
73+
['r:embed', this.mediaData!.rId],
7474
]),
7575
);
7676
pictureFill.appendChild(Util.createElement(xmlDoc, 'a:srcRect'));

packages/excel-builder-vanilla/src/Excel/Drawing/TwoCellAnchor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import type { DualAnchorOption } from './Drawing.js';
12
import { Util } from '../Util.js';
23
import type { XMLDOM } from '../XMLDOM.js';
34

45
export class TwoCellAnchor {
56
from: any = { xOff: 0, yOff: 0 };
67
to: any = { xOff: 0, yOff: 0 };
78

8-
constructor(config: any) {
9+
constructor(config: DualAnchorOption) {
910
if (config) {
1011
this.setFrom(config.from.x, config.from.y, config.to.xOff, config.to.yOff);
1112
this.setTo(config.to.x, config.to.y, config.to.xOff, config.to.yOff);

packages/excel-builder-vanilla/src/Excel/RelationshipManager.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ import { uniqueId } from '../utilities/uniqueId.js';
22
import { Paths } from './Paths.js';
33
import { Util } from './Util.js';
44

5+
type Relation = {
6+
[id: string]: {
7+
id: string;
8+
schema: string;
9+
object: any;
10+
data?: {
11+
id: number;
12+
schema: string;
13+
object: any;
14+
};
15+
};
16+
};
17+
518
/**
619
* @module Excel/RelationshipManager
720
*/
821
export class RelationshipManager {
9-
relations: {
10-
[id: string]: {
11-
id: string;
12-
schema: string;
13-
object: any;
14-
data?: { id: number; schema: string; object: any };
15-
};
16-
} = {};
22+
relations: Relation = {};
1723
lastId = 1;
1824

1925
constructor() {
2026
uniqueId('rId'); // priming
2127
}
2228

23-
importData(data: any) {
29+
importData(data: { relations: Relation; lastId: number }) {
2430
this.relations = data.relations;
2531
this.lastId = data.lastId;
2632
}

packages/excel-builder-vanilla/src/Excel/SheetView.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ interface SheetViewOption {
1313
*/
1414
export class SheetView {
1515
pane: Pane;
16-
showZeros = null; // Default
17-
defaultGridColor = null;
18-
colorId = null;
19-
rightToLeft = null;
20-
showFormulas = null;
21-
showGridLines = null;
22-
showOutlineSymbols = null;
23-
showRowColHeaders = null;
24-
showRuler = null;
25-
showWhiteSpace = null;
26-
tabSelected = null;
27-
topLeftCell = null;
16+
showZeros: boolean | null = null; // Default
17+
defaultGridColor: string | null = null;
18+
colorId: number | null = null;
19+
rightToLeft: boolean | null = null;
20+
showFormulas: boolean | null = null;
21+
showGridLines: boolean | null = null;
22+
showOutlineSymbols: boolean | null = null;
23+
showRowColHeaders: boolean | null = null;
24+
showRuler: boolean | null = null;
25+
showWhiteSpace: boolean | null = null;
26+
tabSelected: boolean | null = null;
27+
topLeftCell: boolean | null = null;
2828
viewType = null; // http://www.datypic.com/sc/ooxml/t-ssml_ST_SheetViewType.html
29-
windowProtection = null;
30-
zoomScale = null;
31-
zoomScaleNormal = null;
32-
zoomScalePageLayoutView = null;
33-
zoomScaleSheetLayoutView = null;
29+
windowProtection: boolean | null = null;
30+
zoomScale: boolean | null = null;
31+
zoomScaleNormal: any = null;
32+
zoomScalePageLayoutView: any = null;
33+
zoomScaleSheetLayoutView: any = null;
3434

3535
constructor(config?: SheetViewOption) {
3636
const conf = config || {};

packages/excel-builder-vanilla/src/Excel/Workbook.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import { Util } from './Util.js';
99
import { Worksheet } from './Worksheet.js';
1010
import { XMLDOM } from './XMLDOM.js';
1111

12+
export interface MediaMeta {
13+
id: string;
14+
data: string;
15+
fileName: string;
16+
contentType: string | null;
17+
extension: string;
18+
rId?: string;
19+
}
20+
1221
/**
1322
* @module Excel/Workbook
1423
*/
@@ -21,7 +30,7 @@ export class Workbook {
2130
worksheets: Worksheet[] = [];
2231
tables: Table[] = [];
2332
drawings: Drawings[] = [];
24-
media: any = {};
33+
media: { [filename: string]: MediaMeta } = {};
2534
printTitles: any;
2635

2736
constructor() {

0 commit comments

Comments
 (0)