Add GetEditorStateAsString in events
This commit is contained in:
parent
1f2809193f
commit
710cbd0312
2 changed files with 22 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue