From 9a364fa2614928044ee5978c0db7867da38b1e0a Mon Sep 17 00:00:00 2001 From: Jesus Sanz Date: Sun, 3 Nov 2024 10:55:39 +0100 Subject: [PATCH] #503 - BugFix - Updated Version on Save File to 0.2. - Created new function to handle V0.1 Saves into one page. --- .../local-disk/shapes-to-document.mapper.ts | 27 ++++++++++++++++++- src/core/local-disk/use-local-disk.hook.ts | 16 ++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/core/local-disk/shapes-to-document.mapper.ts b/src/core/local-disk/shapes-to-document.mapper.ts index 4fdd81f4..e32bbe69 100644 --- a/src/core/local-disk/shapes-to-document.mapper.ts +++ b/src/core/local-disk/shapes-to-document.mapper.ts @@ -1,3 +1,4 @@ +import { ShapeModel } from '../model'; import { DocumentModel } from '../providers/canvas/canvas.model'; import { QuickMockFileContract } from './local-disk.model'; @@ -6,7 +7,7 @@ export const mapFromShapesArrayToQuickMockFileDocument = ( ): QuickMockFileContract => { // TODO: Serialize the activePageIndex? return { - version: '0.1', + version: '0.2', pages: fullDocument.pages, }; }; @@ -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( + (acc: ShapeModel[], page) => { + return acc.concat(page.shapes); + }, + [] + ); + + return { + activePageIndex: 0, + pages: [ + { + id: '1', + name: 'default', + shapes: combinedShapes, + }, + ], + }; +}; diff --git a/src/core/local-disk/use-local-disk.hook.ts b/src/core/local-disk/use-local-disk.hook.ts index 66cc2bb0..1d5bd4ea 100644 --- a/src/core/local-disk/use-local-disk.hook.ts +++ b/src/core/local-disk/use-local-disk.hook.ts @@ -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'; @@ -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); };