diff --git a/public/smartcomponent/svg-layout-designer.ts b/public/smartcomponent/svg-layout-designer.ts index d054577..520dd30 100644 --- a/public/smartcomponent/svg-layout-designer.ts +++ b/public/smartcomponent/svg-layout-designer.ts @@ -204,6 +204,17 @@ 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 * @param history Whole history of the editor diff --git a/src/Events/EditorEvents.ts b/src/Events/EditorEvents.ts index b199c6a..91b7d2b 100644 --- a/src/Events/EditorEvents.ts +++ b/src/Events/EditorEvents.ts @@ -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('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('reviveEditorState', { detail: history }); + const customEvent = new CustomEvent('reviveHistory', { detail: history }); root.dispatchEvent(customEvent); }