Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Proxy to prevent modification of the original view model. #236

Merged
merged 5 commits into from
Nov 20, 2023
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions packages/xlsx-renderer/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Workbook } from 'exceljs';

import { Scope } from './Scope';
import { CellTemplatePool } from './CellTemplatePool';
import { createVmProxyHandler } from './ViewModel';

export class Renderer {
constructor(private cellTemplatePool: CellTemplatePool = new CellTemplatePool()) {}
Expand All @@ -10,10 +11,7 @@ export class Renderer {
const template = await templateFactory();
const output = await templateFactory();

// todo Temporary fixation for VM mutating problem, @see https://github.com/Siemienik/XToolset/issues/137
const vmCopy = JSON.parse(JSON.stringify(vm));

const scope = new Scope(template, output, vmCopy);
const scope = new Scope(template, output, new Proxy(vm, createVmProxyHandler()));

while (!scope.isFinished()) {
this.cellTemplatePool.match(scope.getCurrentTemplateCell()).apply(scope);
Expand All @@ -23,20 +21,16 @@ export class Renderer {
}

public async renderFromFile(templatePath: string, viewModel: unknown): Promise<Workbook> {
const result = await this.render(async () => {
return this.render(async () => {
const template = new Workbook();
return await template.xlsx.readFile(templatePath);
}, viewModel);

return await result;
}

public async renderFromArrayBuffer(templateArrayBuffer: ArrayBuffer, viewModel: unknown): Promise<Workbook> {
const result = await this.render(async () => {
return this.render(async () => {
const template = new Workbook();
return await template.xlsx.load(templateArrayBuffer);
}, viewModel);

return await result;
}
}
15 changes: 15 additions & 0 deletions packages/xlsx-renderer/src/ViewModel.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export type ViewModel = any;

export const createVmProxyHandler = ()=> {
const data: Record<string | number, unknown> = {};

return {
get(target: any, p: PropertyKey): any {
return p in data ? data[p as string] : target[p]
},
set(target: any, p: PropertyKey, value: unknown): boolean {
data[p as string] = value

return true
},
}
}
24 changes: 24 additions & 0 deletions packages/xlsx-renderer/tests/spec/cell-value-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as chai from 'chai';
import { ValueType, Workbook } from 'exceljs';
import { Renderer } from '../../src/Renderer';

const WS_NAME = 'test ws';

describe('BaseCell unit tests', () => {
const factory = async ():Promise<Workbook> =>{
const template = new Workbook();
template.addWorksheet(WS_NAME).addRow(['## testVar', "#! FINISH"]);

return template;
}

it('DateTime', async () => {
const viewModel = {
testVar: new Date(2023, 11, 17, 21, 37)
}
const renderer = new Renderer();
const output = await renderer.render(factory, viewModel)

chai.expect(output.getWorksheet(WS_NAME)?.getCell('A1').type).equals(ValueType.Date);
});
});
Loading