fix: wait for window focus until prompting for library install (#5751)
This commit is contained in:
parent
d1441afec9
commit
fdc462ec01
@ -365,13 +365,46 @@ export const useHandleLibrary = ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const importLibraryFromURL = ({
|
const importLibraryFromURL = async ({
|
||||||
libraryUrl,
|
libraryUrl,
|
||||||
idToken,
|
idToken,
|
||||||
}: {
|
}: {
|
||||||
libraryUrl: string;
|
libraryUrl: string;
|
||||||
idToken: string | null;
|
idToken: string | null;
|
||||||
}) => {
|
}) => {
|
||||||
|
const libraryPromise = 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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)) {
|
if (window.location.hash.includes(URL_HASH_KEYS.addLibrary)) {
|
||||||
const hash = new URLSearchParams(window.location.hash.slice(1));
|
const hash = new URLSearchParams(window.location.hash.slice(1));
|
||||||
hash.delete(URL_HASH_KEYS.addLibrary);
|
hash.delete(URL_HASH_KEYS.addLibrary);
|
||||||
@ -381,22 +414,7 @@ export const useHandleLibrary = ({
|
|||||||
query.delete(URL_QUERY_KEYS.addLibrary);
|
query.delete(URL_QUERY_KEYS.addLibrary);
|
||||||
window.history.replaceState({}, APP_NAME, `?${query.toString()}`);
|
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 onHashChange = (event: HashChangeEvent) => {
|
const onHashChange = (event: HashChangeEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user