tldraw/www/hooks/useFileSystem.ts
Steve Ruiz fa38c0ef0d
[improvement] examples (#264)
* Example project, fix bugs in readonly mode

* Adds ui options
2021-11-11 11:37:57 +00:00

49 lines
1.2 KiB
TypeScript

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