From 893876ddaa24cc8c1a3bc1a6ac55651566afc4fd Mon Sep 17 00:00:00 2001 From: Siklos Date: Fri, 5 Aug 2022 13:49:22 +0200 Subject: [PATCH] Allow LoadEditor through external resource --- src/App.tsx | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e6ea857..58e73c5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -40,6 +40,22 @@ export class App extends React.Component { }; } + componentDidMount() { + const queryString = window.location.search; + const urlParams = new URLSearchParams(queryString); + const state = urlParams.get('state'); + + if (state === null) { + return; + } + + fetch(state) + .then((response) => response.json()) + .then((data: IEditorState) => { + this.LoadState(data); + }); + } + public NewEditor() { // Fetch the configuration from the API fetchConfiguration().then((configuration: Configuration) => { @@ -86,18 +102,22 @@ export class App extends React.Component { const result = reader.result as string; const editorState: IEditorState = JSON.parse(result); - Revive(editorState); - - this.setState({ - configuration: editorState.configuration, - history: editorState.history, - historyCurrentStep: editorState.historyCurrentStep, - isLoaded: true - } as IAppState); + this.LoadState(editorState); }); reader.readAsText(file); } + private LoadState(editorState: IEditorState) { + Revive(editorState); + + this.setState({ + configuration: editorState.configuration, + history: editorState.history, + historyCurrentStep: editorState.historyCurrentStep, + isLoaded: true + } as IAppState); + } + public render() { if (this.state.isLoaded) { return (