mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-21 03:51:38 +03:00
31 lines
705 B
TypeScript
31 lines
705 B
TypeScript
import type { Doc } from 'yjs';
|
|
|
|
export function getDoc(doc: Doc, guid: string): Doc | undefined {
|
|
if (doc.guid === guid) {
|
|
return doc;
|
|
}
|
|
for (const subdoc of doc.subdocs) {
|
|
const found = getDoc(subdoc, guid);
|
|
if (found) {
|
|
return found;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
const saveAlert = (event: BeforeUnloadEvent) => {
|
|
event.preventDefault();
|
|
return (event.returnValue =
|
|
'Data is not saved. Are you sure you want to leave?');
|
|
};
|
|
|
|
export const writeOperation = async (op: Promise<unknown>) => {
|
|
window.addEventListener('beforeunload', saveAlert, {
|
|
capture: true,
|
|
});
|
|
await op;
|
|
window.removeEventListener('beforeunload', saveAlert, {
|
|
capture: true,
|
|
});
|
|
};
|