Implement save/load events

This commit is contained in:
Eric NGUYEN 2022-09-23 11:01:31 +02:00
parent 614016462f
commit da3bd0a323
3 changed files with 127 additions and 17 deletions

View file

@ -5,7 +5,7 @@ import { GetCurrentHistory } from '../Components/Editor/Editor';
import { IEditorState } from '../Interfaces/IEditorState';
import { IHistoryState } from '../Interfaces/IHistoryState';
import { FindContainerById } from '../utils/itertools';
import { ReviveState } from '../utils/saveload';
import { Revive, ReviveHistory as ReviveHistoryAction } from '../utils/saveload';
function GetEditorState(root: Element | Document,
editorState: IEditorState): void {
@ -13,6 +13,40 @@ function GetEditorState(root: Element | Document,
root.dispatchEvent(customEvent);
}
// TODO: In the near future, implement a real SetEditorState which also change IConfiguration
function SetEditorState(root: Element | Document,
editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void,
eventInitDict?: CustomEventInit): void {
const history: IHistoryState[] = eventInitDict?.detail;
ReviveHistoryAction(history);
setNewHistory(history);
const customEvent = new CustomEvent<IEditorState>('setEditorState', { detail: editorState });
root.dispatchEvent(customEvent);
}
function ReviveEditorState(
root: Element | Document,
editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void,
eventInitDict?: CustomEventInit): void {
const anEditorState: IEditorState = eventInitDict?.detail;
Revive(anEditorState);
const customEvent = new CustomEvent<IEditorState>('reviveEditorState', { detail: anEditorState });
root.dispatchEvent(customEvent);
}
function ReviveHistory(
root: Element | Document,
editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void,
eventInitDict?: CustomEventInit): void {
const history: IHistoryState[] = eventInitDict?.detail;
ReviveHistoryAction(history);
const customEvent = new CustomEvent<IHistoryState[]>('reviveEditorState', { detail: history });
root.dispatchEvent(customEvent);
}
function GetCurrentHistoryState(root: Element | Document,
editorState: IEditorState): void {
const customEvent = new CustomEvent<IHistoryState>(
@ -25,8 +59,7 @@ function AppendNewState(root: Element | Document,
editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void,
eventInitDict?: CustomEventInit): void {
const state: IHistoryState = JSON.parse(eventInitDict?.detail.state);
ReviveState(state);
const state: IHistoryState = eventInitDict?.detail.state;
const history = GetCurrentHistory(editorState.history, editorState.historyCurrentStep);
history.push(state);
@ -283,6 +316,9 @@ export interface IEditorEvent {
export const events: IEditorEvent[] = [
{ name: 'getEditorState', func: GetEditorState },
{ name: 'setEditorState', func: SetEditorState },
{ name: 'reviveEditorState', func: ReviveEditorState },
{ name: 'reviveHistory', func: ReviveHistory },
{ name: 'getCurrentHistoryState', func: GetCurrentHistoryState },
{ name: 'appendNewState', func: AppendNewState },
{ name: 'addContainer', func: AddContainer },