Implement migration fonction

This commit is contained in:
Eric NGUYEN 2023-03-01 15:04:10 +01:00
parent afd303f8cb
commit ef0a89c3ea
5 changed files with 121 additions and 5 deletions

View file

@ -100,8 +100,6 @@ export function UseCustomEvents(
root.dispatchEvent(callback);
}
});
}
function SetEditor({

View file

@ -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';

View file

@ -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 ///

116
src/utils/migration.ts Normal file
View file

@ -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<string, {
properties: {
showSelfDimensions: Position[]
showChildrenDimensions: Position[]
markPosition: Orientation[]
showDimensionWithMarks: Position[]
}
}>
}>
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;
});
}

View file

@ -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 {