Make File Handling actually work (#2181)

Follow-up from #1736
This commit is contained in:
Thomas Steiner 2020-09-22 15:21:22 +02:00 committed by GitHub
parent 68bdfaefbe
commit b2822f3538
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 18 deletions

View file

@ -493,6 +493,33 @@ class App extends React.Component<ExcalidrawProps, AppState> {
}
private initializeScene = async () => {
if ("launchQueue" in window && "LaunchParams" in window) {
(window as any).launchQueue.setConsumer(
async (launchParams: { files: any[] }) => {
if (!launchParams.files.length) {
return;
}
const fileHandle = launchParams.files[0];
const blob = await fileHandle.getFile();
blob.handle = fileHandle;
loadFromBlob(blob, this.state)
.then(({ elements, appState }) =>
this.syncActionResult({
elements,
appState: {
...(appState || this.state),
isLoading: false,
},
commitToHistory: true,
}),
)
.catch((error) => {
this.setState({ isLoading: false, errorMessage: error.message });
});
},
);
}
const searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get("id");
const jsonMatch = window.location.hash.match(
@ -3656,6 +3683,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
// This will only work as of Chrome 86,
// but can be safely ignored on older releases.
const item = event.dataTransfer.items[0];
// TODO: Make this part of `AppState`.
(window as any).handle = await (item as any).getAsFileSystemHandle();
} catch (error) {
console.warn(error.name, error.message);