diff --git a/public/smartcomponent/svg-layout-designer.ts b/public/smartcomponent/svg-layout-designer.ts index 520dd30..36fb417 100644 --- a/public/smartcomponent/svg-layout-designer.ts +++ b/public/smartcomponent/svg-layout-designer.ts @@ -152,6 +152,32 @@ const component = this.GetAppComponent(); component.dispatchEvent(new CustomEvent(eventType, { detail: isLoaded })) } + + /** + * Revive the references in the editor state by mutation + * Useful after using JSON.stringify with a replacer + * @param editorState Editor state to revive + * @param callback Callback with the revived state + */ + public ReviveEditorState(editorState: IEditorState, callback: (state: IEditorState) => void) { + const eventType = 'reviveEditorState'; + this.app.AddEventListener(eventType, callback); + const component = this.GetAppComponent(); + component.dispatchEvent(new CustomEvent(eventType, { detail: editorState })); + } + + /** + * Revive the references in the history by mutation + * Useful after using JSON.stringify with a replacer + * @param history History to revive + * @param callback Callback with the revived state + */ + public ReviveHistory(history: IHistoryState[], callback: (state: IHistoryState[]) => void) { + const eventType = 'reviveHistory'; + this.app.AddEventListener(eventType, callback); + const component = this.GetAppComponent(); + component.dispatchEvent(new CustomEvent(eventType, { detail: history })); + } } @@ -206,7 +232,7 @@ /** * Return in a callback the current history of the editor as string - * @param callback + * @param callback */ public GetEditorStateAsString(callback: (state: string) => void) { const eventType = 'getEditorStateAsString'; @@ -227,32 +253,6 @@ component.dispatchEvent(new CustomEvent(eventType, { detail: history })); } - /** - * Revive the references in the editor state by mutation - * Useful after using JSON.stringify with a replacer - * @param editorState Editor state to revive - * @param callback Callback with the revived state - */ - public ReviveEditorState(editorState: IEditorState, callback: (state: IEditorState) => void) { - const eventType = 'reviveEditorState'; - this.app.AddEventListener(eventType, callback); - const component = this.GetEditorComponent(); - component.dispatchEvent(new CustomEvent(eventType, { detail: editorState })); - } - - /** - * Revive the references in the history by mutation - * Useful after using JSON.stringify with a replacer - * @param history History to revive - * @param callback Callback with the revived state - */ - public ReviveHistory(history: IHistoryState[], callback: (state: IHistoryState[]) => void) { - const eventType = 'reviveHistory'; - this.app.AddEventListener(eventType, callback); - const component = this.GetEditorComponent(); - component.dispatchEvent(new CustomEvent(eventType, { detail: history })); - } - /** * Add a new state to the editor * @param historyState New history state to append diff --git a/src/Events/AppEvents.ts b/src/Events/AppEvents.ts index e85eab9..de1e45a 100644 --- a/src/Events/AppEvents.ts +++ b/src/Events/AppEvents.ts @@ -1,4 +1,6 @@ import { IEditorState } from '../Interfaces/IEditorState'; +import { IHistoryState } from '../Interfaces/IHistoryState'; +import { Revive, ReviveHistory as ReviveHistoryAction } from '../utils/saveload'; export interface IAppEvent { name: string @@ -12,7 +14,9 @@ export interface IAppEvent { export const events: IAppEvent[] = [ { name: 'setEditor', func: SetEditor }, - { name: 'setLoaded', func: SetLoaded } + { name: 'setLoaded', func: SetLoaded }, + { name: 'reviveEditorState', func: ReviveEditorState }, + { name: 'reviveHistory', func: ReviveHistory } ]; function SetEditor( @@ -36,3 +40,25 @@ function SetLoaded( const isLoaded: boolean = eventInitDict?.detail; setLoaded(isLoaded); } + +function ReviveEditorState( + root: Element | Document, + setEditor: (newState: IEditorState) => void, + setLoaded: (loaded: boolean) => void, + eventInitDict?: CustomEventInit): void { + const anEditorState: IEditorState = eventInitDict?.detail; + Revive(anEditorState); + const customEvent = new CustomEvent('reviveEditorState', { detail: anEditorState }); + root.dispatchEvent(customEvent); +} + +function ReviveHistory( + root: Element | Document, + setEditor: (newState: IEditorState) => void, + setLoaded: (loaded: boolean) => void, + eventInitDict?: CustomEventInit): void { + const history: IHistoryState[] = eventInitDict?.detail; + ReviveHistoryAction(history); + const customEvent = new CustomEvent('reviveHistory', { detail: history }); + root.dispatchEvent(customEvent); +} diff --git a/src/Events/EditorEvents.ts b/src/Events/EditorEvents.ts index 9006e92..e1c2143 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 { GetCircularReplacer, Revive, ReviveHistory as ReviveHistoryAction } from '../utils/saveload'; +import { GetCircularReplacer, ReviveHistory as ReviveHistoryAction } from '../utils/saveload'; export interface IEditorEvent { name: string @@ -21,8 +21,6 @@ export const events: IEditorEvent[] = [ { name: 'getEditorState', func: GetEditorState }, { name: 'getEditorStateAsString', func: GetEditorStateAsString }, { name: 'setHistory', func: SetHistory }, - { name: 'reviveEditorState', func: ReviveEditorState }, - { name: 'reviveHistory', func: ReviveHistory }, { name: 'getCurrentHistoryState', func: GetCurrentHistoryState }, { name: 'appendNewState', func: AppendNewState }, { name: 'addContainer', func: AddContainer }, @@ -61,28 +59,6 @@ function SetHistory(root: Element | Document, 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('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('reviveHistory', { detail: history }); - root.dispatchEvent(customEvent); -} - function GetCurrentHistoryState(root: Element | Document, editorState: IEditorState): void { const customEvent = new CustomEvent(