From 6b87278a0fea588d2486c7cdd749e7e01e52deea Mon Sep 17 00:00:00 2001 From: Thomas Steiner Date: Mon, 8 Jun 2020 13:02:06 +0200 Subject: [PATCH] Add file handling (#1736) * Add file handling https://github.com/WICG/file-handling/blob/master/explainer.md#example * Only trigger on `.excalidraw` for now --- public/manifest.json | 10 +++++++++- src/actions/actionCanvas.tsx | 4 +--- src/index.tsx | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/public/manifest.json b/public/manifest.json index 174332f2..629b6fba 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -17,5 +17,13 @@ "start_url": ".", "display": "standalone", "theme_color": "#000000", - "background_color": "#ffffff" + "background_color": "#ffffff", + "file_handlers": [ + { + "action": "/", + "accept": { + "application/vnd.excalidraw+json": [".excalidraw"] + } + } + ] } diff --git a/src/actions/actionCanvas.tsx b/src/actions/actionCanvas.tsx index d66bdd37..108fcee1 100644 --- a/src/actions/actionCanvas.tsx +++ b/src/actions/actionCanvas.tsx @@ -58,9 +58,7 @@ export const actionClearCanvas = register({ showAriaLabel={useIsMobile()} onClick={() => { if (window.confirm(t("alerts.clearReset"))) { - // TODO: Defined globally, since file handles aren't yet serializable. - // Once `FileSystemFileHandle` can be serialized, make this - // part of `AppState`. + // TODO: Make this part of `AppState`. (window as any).handle = null; updateData(null); } diff --git a/src/index.tsx b/src/index.tsx index e15575b1..e888ea3d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,6 +10,7 @@ import App from "./components/App"; import { register as registerServiceWorker } from "./serviceWorker"; import "./css/styles.scss"; +import { loadFromBlob } from "./data"; // On Apple mobile devices add the proprietary app icon and splashscreen markup. // No one should have to do this manually, and eventually this annoyance will @@ -88,3 +89,16 @@ registerServiceWorker({ } }, }); + +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(); + loadFromBlob(blob); + }, + ); +}