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

@ -204,6 +204,17 @@
component.dispatchEvent(new CustomEvent(eventType)); component.dispatchEvent(new CustomEvent(eventType));
} }
/**
* Return in a callback the current history of the editor as string
* @param callback
*/
public GetEditorStateAsString(callback: (state: string) => void) {
const eventType = 'getEditorStateAsString';
this.app.AddEventListener(eventType, callback);
const component = this.GetEditorComponent();
component.dispatchEvent(new CustomEvent(eventType));
}
/** /**
* Set the current history of the editor * Set the current history of the editor
* @param history Whole history of the editor * @param history Whole history of the editor

View file

@ -5,7 +5,7 @@ import { GetCurrentHistory } from '../Components/Editor/Editor';
import { IEditorState } from '../Interfaces/IEditorState'; import { IEditorState } from '../Interfaces/IEditorState';
import { IHistoryState } from '../Interfaces/IHistoryState'; import { IHistoryState } from '../Interfaces/IHistoryState';
import { FindContainerById } from '../utils/itertools'; 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 { export interface IEditorEvent {
name: string name: string
@ -19,6 +19,7 @@ export interface IEditorEvent {
export const events: IEditorEvent[] = [ export const events: IEditorEvent[] = [
{ name: 'getEditorState', func: GetEditorState }, { name: 'getEditorState', func: GetEditorState },
{ name: 'getEditorStateAsString', func: GetEditorStateAsString },
{ name: 'setHistory', func: SetHistory }, { name: 'setHistory', func: SetHistory },
{ name: 'reviveEditorState', func: ReviveEditorState }, { name: 'reviveEditorState', func: ReviveEditorState },
{ name: 'reviveHistory', func: ReviveHistory }, { name: 'reviveHistory', func: ReviveHistory },
@ -41,6 +42,14 @@ function GetEditorState(root: Element | Document,
root.dispatchEvent(customEvent); 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, function SetHistory(root: Element | Document,
editorState: IEditorState, editorState: IEditorState,
setNewHistory: (newHistory: IHistoryState[]) => void, setNewHistory: (newHistory: IHistoryState[]) => void,
@ -70,7 +79,7 @@ function ReviveHistory(
eventInitDict?: CustomEventInit): void { eventInitDict?: CustomEventInit): void {
const history: IHistoryState[] = eventInitDict?.detail; const history: IHistoryState[] = eventInitDict?.detail;
ReviveHistoryAction(history); ReviveHistoryAction(history);
const customEvent = new CustomEvent<IHistoryState[]>('reviveEditorState', { detail: history }); const customEvent = new CustomEvent<IHistoryState[]>('reviveHistory', { detail: history });
root.dispatchEvent(customEvent); root.dispatchEvent(customEvent);
} }