2021-11-05 14:13:14 +00:00
|
|
|
import * as React from 'react'
|
|
|
|
import { TLDrawState } from '@tldraw/tldraw'
|
|
|
|
|
2021-11-08 14:21:37 +00:00
|
|
|
export function useFileSystem(state: TLDrawState) {
|
2021-11-05 14:13:14 +00:00
|
|
|
const promptSaveBeforeChange = React.useCallback(async () => {
|
2021-11-08 14:21:37 +00:00
|
|
|
if (state.isDirty) {
|
|
|
|
if (state.fileSystemHandle) {
|
2021-11-05 14:13:14 +00:00
|
|
|
if (window.confirm('Do you want to save changes to your current project?')) {
|
2021-11-08 14:21:37 +00:00
|
|
|
await state.saveProject()
|
2021-11-05 14:13:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (window.confirm('Do you want to save your current project?')) {
|
2021-11-08 14:21:37 +00:00
|
|
|
await state.saveProject()
|
2021-11-05 14:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 14:21:37 +00:00
|
|
|
}, [state])
|
2021-11-05 14:13:14 +00:00
|
|
|
|
|
|
|
const onNewProject = React.useCallback(async () => {
|
|
|
|
await promptSaveBeforeChange()
|
2021-11-08 14:21:37 +00:00
|
|
|
state.newProject()
|
|
|
|
}, [state, promptSaveBeforeChange])
|
2021-11-05 14:13:14 +00:00
|
|
|
|
|
|
|
const onSaveProject = React.useCallback(() => {
|
2021-11-08 14:21:37 +00:00
|
|
|
state.saveProject()
|
|
|
|
}, [state])
|
2021-11-05 14:13:14 +00:00
|
|
|
|
|
|
|
const onSaveProjectAs = React.useCallback(() => {
|
2021-11-08 14:21:37 +00:00
|
|
|
state.saveProjectAs()
|
|
|
|
}, [state])
|
2021-11-05 14:13:14 +00:00
|
|
|
|
|
|
|
const onOpenProject = React.useCallback(async () => {
|
|
|
|
await promptSaveBeforeChange()
|
2021-11-08 14:21:37 +00:00
|
|
|
state.openProject()
|
|
|
|
}, [state, promptSaveBeforeChange])
|
2021-11-05 14:13:14 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
onNewProject,
|
|
|
|
onSaveProject,
|
|
|
|
onSaveProjectAs,
|
|
|
|
onOpenProject,
|
|
|
|
}
|
|
|
|
}
|