Add GetEditorStateAsString in events

This commit is contained in:
Eric NGUYEN 2022-10-10 10:44:12 +02:00 committed by Eric Nguyen
parent 1f2809193f
commit 710cbd0312
2 changed files with 22 additions and 2 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 { Revive, ReviveHistory as ReviveHistoryAction } from '../utils/saveload';
import { GetCircularReplacer, Revive, ReviveHistory as ReviveHistoryAction } from '../utils/saveload';
export interface IEditorEvent {
name: string
@ -19,6 +19,7 @@ export interface IEditorEvent {
export const events: IEditorEvent[] = [
{ name: 'getEditorState', func: GetEditorState },
{ name: 'getEditorStateAsString', func: GetEditorStateAsString },
{ name: 'setHistory', func: SetHistory },
{ name: 'reviveEditorState', func: ReviveEditorState },
{ name: 'reviveHistory', func: ReviveHistory },
@ -41,6 +42,14 @@ function GetEditorState(root: Element | Document,
root.dispatchEvent(customEvent);
}
function GetEditorStateAsString(root: Element | Document,
editorState: IEditorState): void {
const spaces = import.meta.env.DEV ? 4 : 0;
const data = JSON.stringify(editorState, GetCircularReplacer(), spaces);
const customEvent = new CustomEvent<string>('getEditorStateAsString', { detail: data });
root.dispatchEvent(customEvent);
}
function SetHistory(root: Element | Document,
editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void,
@ -70,7 +79,7 @@ function ReviveHistory(
eventInitDict?: CustomEventInit): void {
const history: IHistoryState[] = eventInitDict?.detail;
ReviveHistoryAction(history);
const customEvent = new CustomEvent<IHistoryState[]>('reviveEditorState', { detail: history });
const customEvent = new CustomEvent<IHistoryState[]>('reviveHistory', { detail: history });
root.dispatchEvent(customEvent);
}