Skip to content

Commit 33de5a7

Browse files
author
skhmt
committed
confirmation when dropping project
1 parent 7b33f47 commit 33de5a7

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
lines changed

src/FileDropHandler.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import {useEditor, useToasts} from "@tldraw/tldraw";
2-
import {parseDecompressBlob, parseString} from "./fileLoaderUtils.ts";
3-
import {useEffect} from "react";
1+
import { Editor, useDialogs, useEditor, useToasts } from "@tldraw/tldraw";
2+
import { parseDecompressBlob, parseString } from "./fileLoaderUtils.ts";
3+
import { useEffect } from "react";
4+
import { OpenDialog } from "./createOpenDialog.tsx";
45

56
export default function FileDropHandler() {
67
const editor = useEditor();
78
const { addToast } = useToasts();
9+
const { addDialog } = useDialogs();
810

911
useEffect(() => {
1012
// @ts-expect-error editor.externalContentHandlers does actually exist
@@ -16,14 +18,10 @@ export default function FileDropHandler() {
1618
if (!file) return;
1719

1820
if (file.name.endsWith('.tldr')) {
19-
const json = await file.text();
20-
parseString(json, editor, addToast);
21-
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
21+
addDialog({ component: createTldrDialog(editor, file, addToast) });
2222
}
2323
else if (file.name.endsWith('.tldz')) {
24-
const blob = new Blob([file]);
25-
await parseDecompressBlob(blob, editor, addToast);
26-
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
24+
addDialog({ component: createTldzDialog(editor, file, addToast) });
2725
}
2826
else {
2927
defaultOnDrop?.(content);
@@ -32,4 +30,26 @@ export default function FileDropHandler() {
3230
}, [editor, addToast]);
3331

3432
return null;
33+
}
34+
35+
function createTldrDialog(editor: Editor, file: File, addToast: any) {
36+
return ({ onClose }: { onClose(): void }) => {
37+
return OpenDialog(onClose, async () => {
38+
const json = await file.text();
39+
parseString(json, editor, addToast);
40+
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
41+
onClose();
42+
})
43+
}
44+
}
45+
46+
function createTldzDialog(editor: Editor, file: File, addToast: any) {
47+
return ({ onClose }: { onClose(): void }) => {
48+
return OpenDialog(onClose, async () => {
49+
const blob = new Blob([file]);
50+
await parseDecompressBlob(blob, editor, addToast);
51+
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
52+
onClose();
53+
})
54+
}
3555
}

src/createOpenDialog.tsx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TldrawUiDialogHeader,
99
TldrawUiDialogTitle
1010
} from "@tldraw/tldraw";
11-
import {parseDecompressBlob, parseString} from "./fileLoaderUtils.ts";
11+
import { parseDecompressBlob, parseString } from "./fileLoaderUtils.ts";
1212
import { useToasts } from "@tldraw/tldraw";
1313

1414
export function createOpenDialog(editor: Editor) {
@@ -59,28 +59,32 @@ export function createOpenDialog(editor: Editor) {
5959
onClose();
6060
}
6161

62-
return (
63-
<>
64-
<TldrawUiDialogHeader>
65-
<TldrawUiDialogTitle>
66-
<strong>Open new project ? </strong>
67-
</TldrawUiDialogTitle>
68-
< TldrawUiDialogCloseButton />
69-
</TldrawUiDialogHeader>
70-
< TldrawUiDialogBody style={{ maxWidth: 350 }}>
71-
Opening a project will replace your current
72-
project and any unsaved changes will be lost.
73-
Are you sure you want to do this ?
74-
</TldrawUiDialogBody>
75-
< TldrawUiDialogFooter className="tlui-dialog__footer__actions" >
76-
<TldrawUiButton type="normal" onClick={onClose} >
77-
<TldrawUiButtonLabel>Cancel </TldrawUiButtonLabel>
78-
</TldrawUiButton>
79-
< TldrawUiButton type="primary" onClick={open} >
80-
<TldrawUiButtonLabel>Open project </TldrawUiButtonLabel>
81-
</TldrawUiButton>
82-
</TldrawUiDialogFooter>
83-
</>
84-
)
62+
return OpenDialog(onClose, open)
8563
}
64+
}
65+
66+
export function OpenDialog(onCancel: () => void, onOpen: () => void) {
67+
return (
68+
<>
69+
<TldrawUiDialogHeader>
70+
<TldrawUiDialogTitle>
71+
<strong>Open new project ? </strong>
72+
</TldrawUiDialogTitle>
73+
< TldrawUiDialogCloseButton />
74+
</TldrawUiDialogHeader>
75+
< TldrawUiDialogBody style={{ maxWidth: 350 }}>
76+
Opening a project will replace your current
77+
project and any unsaved changes will be lost.
78+
Are you sure you want to do this ?
79+
</TldrawUiDialogBody>
80+
< TldrawUiDialogFooter className="tlui-dialog__footer__actions" >
81+
<TldrawUiButton type="normal" onClick={onCancel} >
82+
<TldrawUiButtonLabel>Cancel </TldrawUiButtonLabel>
83+
</TldrawUiButton>
84+
< TldrawUiButton type="primary" onClick={onOpen} >
85+
<TldrawUiButtonLabel>Open project </TldrawUiButtonLabel>
86+
</TldrawUiButton>
87+
</TldrawUiDialogFooter>
88+
</>
89+
)
8690
}

src/mainMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { createSaveDialog } from './createSaveDialog';
1515
import { createNewDialog } from './createNewDialog';
1616

1717
import welcomeProject from './welcomeProject'
18-
import {getTimestamp} from "./getTimestamp.ts";
18+
import { getTimestamp } from "./getTimestamp.ts";
1919

2020
// override the MainMenu component with most of the defaults plus the local file menu
2121
export const MainMenuFileComponent: TLComponents = {
@@ -86,7 +86,7 @@ export const actionOverrides: TLUiOverrides = {
8686
}
8787
}
8888
else {
89-
addDialog({component: createSaveDialog(editor)});
89+
addDialog({ component: createSaveDialog(editor) });
9090
}
9191
}
9292
};

0 commit comments

Comments
 (0)