From ef0a89c3ea38221918101b80ef891963cb3cbeec Mon Sep 17 00:00:00 2001 From: Eric NGUYEN Date: Wed, 1 Mar 2023 15:04:10 +0100 Subject: [PATCH] Implement migration fonction --- src/Events/AppEvents.ts | 2 - src/Events/EditorEvents.ts | 1 - src/utils/default.ts | 1 - src/utils/migration.ts | 116 +++++++++++++++++++++++++++++++++++++ src/utils/saveload.ts | 6 +- 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 src/utils/migration.ts diff --git a/src/Events/AppEvents.ts b/src/Events/AppEvents.ts index 20f64a9..10d2468 100644 --- a/src/Events/AppEvents.ts +++ b/src/Events/AppEvents.ts @@ -100,8 +100,6 @@ export function UseCustomEvents( root.dispatchEvent(callback); } }); - - } function SetEditor({ diff --git a/src/Events/EditorEvents.ts b/src/Events/EditorEvents.ts index 42f2deb..6336f8f 100644 --- a/src/Events/EditorEvents.ts +++ b/src/Events/EditorEvents.ts @@ -10,7 +10,6 @@ import { DeleteSymbol as DeleteSymbolAction } from '../Components/Editor/Actions/SymbolOperations'; import { GetCurrentHistory } from '../Components/Editor/Editor'; -import { type IConfiguration } from '../Interfaces/IConfiguration'; import { type IEditorState } from '../Interfaces/IEditorState'; import { type IHistoryState } from '../Interfaces/IHistoryState'; import { FindContainerById } from '../utils/itertools'; diff --git a/src/utils/default.ts b/src/utils/default.ts index d1d11d3..03d3ae0 100644 --- a/src/utils/default.ts +++ b/src/utils/default.ts @@ -10,7 +10,6 @@ import { Orientation } from '../Enums/Orientation'; import { AppState } from '../Enums/AppState'; import { type IDimensionOptions } from '../Interfaces/IDimensionOptions'; import { type IDimensionStyle } from '../Components/SVG/Elements/Dimension'; -import { version } from 'react'; /// EDITOR DEFAULTS /// diff --git a/src/utils/migration.ts b/src/utils/migration.ts new file mode 100644 index 0000000..4b56800 --- /dev/null +++ b/src/utils/migration.ts @@ -0,0 +1,116 @@ +import { type Orientation } from '../Enums/Orientation'; +import { type Position } from '../Enums/Position'; +import { type IDimensions } from '../Interfaces/IDimensions'; +import { DEFAULT_DIMENSION_OPTION } from './default'; + +// V1 Model + +interface V1_IEditorState { + history: Array<{ + containers: Map + }> + configuration: { + AvailableContainers: Array<{ + ShowSelfDimensions?: Position[] + ShowChildrenDimensions?: Position[] + MarkPosition?: Orientation[] + ShowDimensionWithMarks?: Position[] + }> + } +} + +// V2 Changes + +interface V2_IContainerModel { + properties: { + dimensionOptions: IDimensions + + // Call delete on these properties vvv + showSelfDimensions: Position[] | undefined + showChildrenDimensions: Position[] | undefined + markPosition: Orientation[] | undefined + showDimensionWithMarks: Position[] | undefined + } +} + +interface V2_IAvailableContainer { + DimensionOptions: IDimensions + + // Call delete on these properties vvv + ShowSelfDimensions?: Position[] + ShowChildrenDimensions?: Position[] + MarkPosition?: Orientation[] + ShowDimensionWithMarks?: Position[] +} + +export function TryMigration(editorState: any): void { + if (!('version' in editorState)) { + console.debug('Migrating v1.0.0 Editor State to : v2.0.0.'); + MigrateV1ToV2(editorState); + return; + } + + // version 1+ + console.debug('Editor State is at the latest version.'); +} + +export function MigrateV1ToV2(v1EditorState: V1_IEditorState): void { + // Fix breaking change showSelfDimension in all containers + for (const historyState of v1EditorState.history) { + historyState.containers.forEach((v1Container) => { + const v2Container = (v1Container as any) as V2_IContainerModel; + v2Container.properties.dimensionOptions = { + selfDimensions: { + ...DEFAULT_DIMENSION_OPTION, + positions: v1Container.properties.showSelfDimensions + }, + childrenDimensions: { + ...DEFAULT_DIMENSION_OPTION, + positions: v1Container.properties.showChildrenDimensions + }, + dimensionWithMarks: { + ...DEFAULT_DIMENSION_OPTION, + positions: v1Container.properties.showDimensionWithMarks + }, + selfMarginsDimensions: DEFAULT_DIMENSION_OPTION, + markPosition: v1Container.properties.markPosition + }; + + delete v2Container.properties.showSelfDimensions; + delete v2Container.properties.showChildrenDimensions; + delete v2Container.properties.showDimensionWithMarks; + delete v2Container.properties.markPosition; + }); + } + + v1EditorState.configuration.AvailableContainers.forEach((v1AvailableContainer) => { + const v2AvailableContainer = (v1AvailableContainer as any) as V2_IAvailableContainer; + v2AvailableContainer.DimensionOptions = { + selfDimensions: { + positions: v1AvailableContainer.ShowSelfDimensions ?? [] + }, + childrenDimensions: { + positions: v1AvailableContainer.ShowChildrenDimensions ?? [] + }, + dimensionWithMarks: { + positions: v1AvailableContainer.ShowDimensionWithMarks ?? [] + }, + selfMarginsDimensions: { + positions: [] + }, + markPosition: v1AvailableContainer.MarkPosition ?? [] + }; + + delete v2AvailableContainer.ShowSelfDimensions; + delete v2AvailableContainer.ShowChildrenDimensions; + delete v2AvailableContainer.ShowDimensionWithMarks; + delete v2AvailableContainer.MarkPosition; + }); +} diff --git a/src/utils/saveload.ts b/src/utils/saveload.ts index 7c4b862..a585a34 100644 --- a/src/utils/saveload.ts +++ b/src/utils/saveload.ts @@ -3,6 +3,7 @@ import { type IEditorState } from '../Interfaces/IEditorState'; import { type IHistoryState } from '../Interfaces/IHistoryState'; import { type IContainerModel } from '../Interfaces/IContainerModel'; import { type ISymbolModel } from '../Interfaces/ISymbolModel'; +import { TryMigration } from './migration'; /** * Revive the Editor state @@ -12,8 +13,11 @@ import { type ISymbolModel } from '../Interfaces/ISymbolModel'; export function Revive(editorState: IEditorState): void { const history = editorState.history; - // restore the parents and the selected container + // Restore the parents and the selected container ReviveHistory(history); + + // Update the editor state to the current version + TryMigration(editorState); } export function ReviveHistory(history: IHistoryState[]): void {