import { Dispatch, SetStateAction } from 'react'; import { getCurrentHistory } from '../Components/Editor/Editor'; import { IConfiguration } from '../Interfaces/IConfiguration'; import { IEditorState } from '../Interfaces/IEditorState'; import { IHistoryState } from '../Interfaces/IHistoryState'; import { ReviveState } from '../utils/saveload'; const initEditor = (configuration: IConfiguration): void => { // faire comme la callback de fetch } const getEditorState = (editorState: IEditorState): void => { const customEvent = new CustomEvent('getEditorState', { detail: editorState }); document.dispatchEvent(customEvent); }; const getCurrentHistoryState = (editorState: IEditorState): void => { const customEvent = new CustomEvent( 'getCurrentHistoryState', { detail: editorState.history[editorState.historyCurrentStep] }); document.dispatchEvent(customEvent); }; const appendNewState = ( editorState: IEditorState, setHistory: Dispatch>, setHistoryCurrentStep: Dispatch>, eventInitDict?: CustomEventInit ): void => { const state: IHistoryState = JSON.parse(eventInitDict?.detail.state); ReviveState(state); const history = getCurrentHistory(editorState.history, editorState.historyCurrentStep); history.push(state); setHistory(history); setHistoryCurrentStep(history.length - 1); }; export interface IEditorEvent { name: string func: ( editorState: IEditorState, setHistory: Dispatch>, setHistoryCurrentStep: Dispatch>, eventInitDict?: CustomEventInit ) => void } const events: IEditorEvent[] = [ { name: 'getEditorState', func: getEditorState }, { name: 'getCurrentHistoryState', func: getCurrentHistoryState }, { name: 'appendNewState', func: appendNewState } ]; export default events;