Skip to content

Commit

Permalink
fix: the retention of unnecessary image data (#95)
Browse files Browse the repository at this point in the history
* fix: image data handling

* Refactor insertImage method signature

* Remove TODO comment for supporting other image formats
  • Loading branch information
motinados authored Mar 30, 2024
1 parent 24d6c4e commit 0b5115c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
27 changes: 17 additions & 10 deletions src/imageModule.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { v4 as uuidv4 } from "uuid";
import { DrawingRels } from "./drawingRels";
import { Image } from "./worksheet";
import { XlsxImage } from "./xml/worksheetXml";
import { ImageInfo } from "./worksheet";

export type ImageModule = {
name: string;
add(image: Image): void;
getImages(): Image[];
createXlsxImage(image: Image, drawingRels: DrawingRels): XlsxImage;
add(image: ImageInfo): void;
getImageInfos(): ImageInfo[];
createXlsxImage(image: ImageInfo, drawingRels: DrawingRels): XlsxImage;
makeXmlElm(xlsxImages: XlsxImage[]): string;
};

export function imageModule(): ImageModule {
const images: Image[] = [];
// imageModule does not store image data.
const imageInfos: ImageInfo[] = [];
return {
name: "image",
add(image: Image) {
images.push(image);
add(image: ImageInfo) {
imageInfos.push({
displayName: image.displayName,
extension: image.extension,
from: image.from,
width: image.width,
height: image.height,
});
},
getImages() {
return images;
getImageInfos() {
return imageInfos;
},
createXlsxImage(image: Image, drawingRels: DrawingRels): XlsxImage {
createXlsxImage(image: ImageInfo, drawingRels: DrawingRels): XlsxImage {
const num = drawingRels.length + 1;
const rId = drawingRels.addDrawingRel({
target: `../media/image${num}.${image.extension}`,
Expand Down
14 changes: 7 additions & 7 deletions src/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,16 @@ export type Image = {
col: number;
row: number;
};
// TODO: Support other image formats.
// In online Excel, when a bmp is inserted, it is converted a jpeg.
// In online Excel, when a tiff is inserted, it is converted a png.
extension: "png" | "jpeg" | "gif";
// FIXME: The data is also stored in the image store.
data: Uint8Array;
width: number;
height: number;
};

export type ImageInfo = Omit<Image, "data">;

export type WorksheetProps = {
defaultColWidth?: number;
defaultRowHeight?: number;
Expand All @@ -176,7 +176,7 @@ export type WorksheetType = {
freezePane: FreezePane | null;
mergeCellsModule: MergeCellsModule | null;
conditionalFormattingModule: ConditionalFormattingModule | null;
images: Image[];
imageInfos: ImageInfo[];
imageStore: ImageStore | null;
imageModule: ImageModule | null;
getCell(rowIndex: number, colIndex: number): NullableCell;
Expand Down Expand Up @@ -262,8 +262,8 @@ export class Worksheet implements WorksheetType {
return this._conditionalFormattingModule;
}

get images() {
return this._imageModule.getImages();
get imageInfos() {
return this._imageModule.getImageInfos();
}

get imageModule() {
Expand Down Expand Up @@ -324,7 +324,7 @@ export class Worksheet implements WorksheetType {
this._conditionalFormattingModule.add(conditionalFormatting);
}

async insertImage(image: Omit<Image, "fileBasename">) {
async insertImage(image: Image) {
await this._imageStore.addImage(image.data, image.extension);
this._imageModule.add(image);
}
Expand Down Expand Up @@ -399,7 +399,7 @@ export class WorksheetS implements WorksheetType {
return this._conditionalFormattingModule;
}

get images() {
get imageInfos() {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function createExcelFiles(worksheets: WorksheetType[]) {
const imageExtensions = Array.from(
new Set(
worksheets.flatMap((worksheet) =>
worksheet.images.map((image) => image.extension)
worksheet.imageInfos.map((image) => image.extension)
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/xml/worksheetXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export function makeWorksheetXml(

const xlsxImages: XlsxImage[] = [];
if (worksheet.imageModule) {
const images = worksheet.imageModule.getImages();
const images = worksheet.imageModule.getImageInfos();
for (const image of images) {
xlsxImages.push(
worksheet.imageModule.createXlsxImage(image, drawingRels)
Expand Down

0 comments on commit 0b5115c

Please sign in to comment.