Skip to content

Commit

Permalink
#503 - BugFix
Browse files Browse the repository at this point in the history
- Updated Version on Save File to 0.2.
- Created new function to handle V0.1 Saves into one page.
  • Loading branch information
jsanzdev committed Nov 3, 2024
1 parent e12b3bb commit 9a364fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
27 changes: 26 additions & 1 deletion src/core/local-disk/shapes-to-document.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ShapeModel } from '../model';
import { DocumentModel } from '../providers/canvas/canvas.model';
import { QuickMockFileContract } from './local-disk.model';

Expand All @@ -6,7 +7,7 @@ export const mapFromShapesArrayToQuickMockFileDocument = (
): QuickMockFileContract => {
// TODO: Serialize the activePageIndex?
return {
version: '0.1',
version: '0.2',
pages: fullDocument.pages,
};
};
Expand All @@ -19,3 +20,27 @@ export const mapFromQuickMockFileDocumentToApplicationDocument = (
pages: fileDocument.pages,
};
};

// Example function to handle version 0.1 parsing
export const mapFromQuickMockFileDocumentToApplicationDocumentV0_1 = (
fileDocument: QuickMockFileContract
): DocumentModel => {
// Combine all shapes into a single page
const combinedShapes: ShapeModel[] = fileDocument.pages.reduce<ShapeModel[]>(
(acc: ShapeModel[], page) => {
return acc.concat(page.shapes);
},
[]
);

return {
activePageIndex: 0,
pages: [
{
id: '1',
name: 'default',
shapes: combinedShapes,
},
],
};
};
16 changes: 13 additions & 3 deletions src/core/local-disk/use-local-disk.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCanvasContext } from '../providers';
import {
mapFromShapesArrayToQuickMockFileDocument,
mapFromQuickMockFileDocumentToApplicationDocument,
mapFromQuickMockFileDocumentToApplicationDocumentV0_1,
} from './shapes-to-document.mapper';
import { fileInput, OnFileSelectedCallback } from '@/common/file-input';
import { QuickMockFileContract } from './local-disk.model';
Expand Down Expand Up @@ -57,9 +58,18 @@ export const useLocalDisk = () => {
reader.onload = () => {
const content = reader.result as string;
const parseData: QuickMockFileContract = JSON.parse(content);
const appDocument =
mapFromQuickMockFileDocumentToApplicationDocument(parseData);
loadDocument(appDocument);

if (parseData.version === '0.1') {
// Handle version 0.1 parsing
const appDocument =
mapFromQuickMockFileDocumentToApplicationDocumentV0_1(parseData);
loadDocument(appDocument);
} else {
// Handle other versions
const appDocument =
mapFromQuickMockFileDocumentToApplicationDocument(parseData);
loadDocument(appDocument);
}
};
reader.readAsText(file);
};
Expand Down

0 comments on commit 9a364fa

Please sign in to comment.