2023-07-21 08:23:18 +03:00
|
|
|
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;
|
|
|
|
}
|
2023-07-25 19:56:48 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
};
|