Skip to content

Commit

Permalink
confirmation when dropping project
Browse files Browse the repository at this point in the history
  • Loading branch information
skhmt committed Dec 3, 2024
1 parent 7b33f47 commit 33de5a7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
38 changes: 29 additions & 9 deletions src/FileDropHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {useEditor, useToasts} from "@tldraw/tldraw";
import {parseDecompressBlob, parseString} from "./fileLoaderUtils.ts";
import {useEffect} from "react";
import { Editor, useDialogs, useEditor, useToasts } from "@tldraw/tldraw";
import { parseDecompressBlob, parseString } from "./fileLoaderUtils.ts";
import { useEffect } from "react";
import { OpenDialog } from "./createOpenDialog.tsx";

export default function FileDropHandler() {
const editor = useEditor();
const { addToast } = useToasts();
const { addDialog } = useDialogs();

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

if (file.name.endsWith('.tldr')) {
const json = await file.text();
parseString(json, editor, addToast);
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
addDialog({ component: createTldrDialog(editor, file, addToast) });
}
else if (file.name.endsWith('.tldz')) {
const blob = new Blob([file]);
await parseDecompressBlob(blob, editor, addToast);
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
addDialog({ component: createTldzDialog(editor, file, addToast) });
}
else {
defaultOnDrop?.(content);
Expand All @@ -32,4 +30,26 @@ export default function FileDropHandler() {
}, [editor, addToast]);

return null;
}

function createTldrDialog(editor: Editor, file: File, addToast: any) {
return ({ onClose }: { onClose(): void }) => {
return OpenDialog(onClose, async () => {
const json = await file.text();
parseString(json, editor, addToast);
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
onClose();
})
}
}

function createTldzDialog(editor: Editor, file: File, addToast: any) {
return ({ onClose }: { onClose(): void }) => {
return OpenDialog(onClose, async () => {
const blob = new Blob([file]);
await parseDecompressBlob(blob, editor, addToast);
addToast({ title: `"${file.name}" loaded`, severity: 'success' });
onClose();
})
}
}
52 changes: 28 additions & 24 deletions src/createOpenDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TldrawUiDialogHeader,
TldrawUiDialogTitle
} from "@tldraw/tldraw";
import {parseDecompressBlob, parseString} from "./fileLoaderUtils.ts";
import { parseDecompressBlob, parseString } from "./fileLoaderUtils.ts";
import { useToasts } from "@tldraw/tldraw";

export function createOpenDialog(editor: Editor) {
Expand Down Expand Up @@ -59,28 +59,32 @@ export function createOpenDialog(editor: Editor) {
onClose();
}

return (
<>
<TldrawUiDialogHeader>
<TldrawUiDialogTitle>
<strong>Open new project ? </strong>
</TldrawUiDialogTitle>
< TldrawUiDialogCloseButton />
</TldrawUiDialogHeader>
< TldrawUiDialogBody style={{ maxWidth: 350 }}>
Opening a project will replace your current
project and any unsaved changes will be lost.
Are you sure you want to do this ?
</TldrawUiDialogBody>
< TldrawUiDialogFooter className="tlui-dialog__footer__actions" >
<TldrawUiButton type="normal" onClick={onClose} >
<TldrawUiButtonLabel>Cancel </TldrawUiButtonLabel>
</TldrawUiButton>
< TldrawUiButton type="primary" onClick={open} >
<TldrawUiButtonLabel>Open project </TldrawUiButtonLabel>
</TldrawUiButton>
</TldrawUiDialogFooter>
</>
)
return OpenDialog(onClose, open)
}
}

export function OpenDialog(onCancel: () => void, onOpen: () => void) {
return (
<>
<TldrawUiDialogHeader>
<TldrawUiDialogTitle>
<strong>Open new project ? </strong>
</TldrawUiDialogTitle>
< TldrawUiDialogCloseButton />
</TldrawUiDialogHeader>
< TldrawUiDialogBody style={{ maxWidth: 350 }}>
Opening a project will replace your current
project and any unsaved changes will be lost.
Are you sure you want to do this ?
</TldrawUiDialogBody>
< TldrawUiDialogFooter className="tlui-dialog__footer__actions" >
<TldrawUiButton type="normal" onClick={onCancel} >
<TldrawUiButtonLabel>Cancel </TldrawUiButtonLabel>
</TldrawUiButton>
< TldrawUiButton type="primary" onClick={onOpen} >
<TldrawUiButtonLabel>Open project </TldrawUiButtonLabel>
</TldrawUiButton>
</TldrawUiDialogFooter>
</>
)
}
4 changes: 2 additions & 2 deletions src/mainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createSaveDialog } from './createSaveDialog';
import { createNewDialog } from './createNewDialog';

import welcomeProject from './welcomeProject'
import {getTimestamp} from "./getTimestamp.ts";
import { getTimestamp } from "./getTimestamp.ts";

// override the MainMenu component with most of the defaults plus the local file menu
export const MainMenuFileComponent: TLComponents = {
Expand Down Expand Up @@ -86,7 +86,7 @@ export const actionOverrides: TLUiOverrides = {
}
}
else {
addDialog({component: createSaveDialog(editor)});
addDialog({ component: createSaveDialog(editor) });
}
}
};
Expand Down

0 comments on commit 33de5a7

Please sign in to comment.