fix: wait for window focus until prompting for library install (#5751)

This commit is contained in:
David Luzar 2022-10-10 16:08:13 +02:00 committed by GitHub
parent d1441afec9
commit fdc462ec01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 25 deletions

View File

@ -365,38 +365,56 @@ export const useHandleLibrary = ({
return; return;
} }
const importLibraryFromURL = ({ const importLibraryFromURL = async ({
libraryUrl, libraryUrl,
idToken, idToken,
}: { }: {
libraryUrl: string; libraryUrl: string;
idToken: string | null; idToken: string | null;
}) => { }) => {
if (window.location.hash.includes(URL_HASH_KEYS.addLibrary)) { const libraryPromise = new Promise<Blob>(async (resolve, reject) => {
const hash = new URLSearchParams(window.location.hash.slice(1)); try {
hash.delete(URL_HASH_KEYS.addLibrary); const request = await fetch(decodeURIComponent(libraryUrl));
window.history.replaceState({}, APP_NAME, `#${hash.toString()}`); const blob = await request.blob();
} else if (window.location.search.includes(URL_QUERY_KEYS.addLibrary)) { resolve(blob);
const query = new URLSearchParams(window.location.search); } catch (error: any) {
query.delete(URL_QUERY_KEYS.addLibrary); reject(error);
window.history.replaceState({}, APP_NAME, `?${query.toString()}`); }
}
excalidrawAPI.updateLibrary({
libraryItems: new Promise<Blob>(async (resolve, reject) => {
try {
const request = await fetch(decodeURIComponent(libraryUrl));
const blob = await request.blob();
resolve(blob);
} catch (error: any) {
reject(error);
}
}),
prompt: idToken !== excalidrawAPI.id,
merge: true,
defaultStatus: "published",
openLibraryMenu: true,
}); });
const shouldPrompt = idToken !== excalidrawAPI.id;
// wait for the tab to be focused before continuing in case we'll prompt
// for confirmation
await (shouldPrompt && document.hidden
? new Promise<void>((resolve) => {
window.addEventListener("focus", () => resolve(), {
once: true,
});
})
: null);
try {
await excalidrawAPI.updateLibrary({
libraryItems: libraryPromise,
prompt: shouldPrompt,
merge: true,
defaultStatus: "published",
openLibraryMenu: true,
});
} catch (error) {
throw error;
} finally {
if (window.location.hash.includes(URL_HASH_KEYS.addLibrary)) {
const hash = new URLSearchParams(window.location.hash.slice(1));
hash.delete(URL_HASH_KEYS.addLibrary);
window.history.replaceState({}, APP_NAME, `#${hash.toString()}`);
} else if (window.location.search.includes(URL_QUERY_KEYS.addLibrary)) {
const query = new URLSearchParams(window.location.search);
query.delete(URL_QUERY_KEYS.addLibrary);
window.history.replaceState({}, APP_NAME, `?${query.toString()}`);
}
}
}; };
const onHashChange = (event: HashChangeEvent) => { const onHashChange = (event: HashChangeEvent) => {
event.preventDefault(); event.preventDefault();