You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useFileSystem.ts 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import * as React from 'react'
  2. import { TLDrawState } from '@tldraw/tldraw'
  3. export function useFileSystem(tlstate: TLDrawState) {
  4. const promptSaveBeforeChange = React.useCallback(async () => {
  5. if (tlstate.isDirty) {
  6. if (tlstate.fileSystemHandle) {
  7. if (window.confirm('Do you want to save changes to your current project?')) {
  8. await tlstate.saveProject()
  9. }
  10. } else {
  11. if (window.confirm('Do you want to save your current project?')) {
  12. await tlstate.saveProject()
  13. }
  14. }
  15. }
  16. }, [tlstate])
  17. const onNewProject = React.useCallback(async () => {
  18. await promptSaveBeforeChange()
  19. tlstate.newProject()
  20. }, [tlstate, promptSaveBeforeChange])
  21. const onSaveProject = React.useCallback(() => {
  22. tlstate.saveProject()
  23. }, [tlstate])
  24. const onSaveProjectAs = React.useCallback(() => {
  25. tlstate.saveProjectAs()
  26. }, [tlstate])
  27. const onOpenProject = React.useCallback(async () => {
  28. await promptSaveBeforeChange()
  29. tlstate.openProject()
  30. }, [tlstate, promptSaveBeforeChange])
  31. return {
  32. onNewProject,
  33. onSaveProject,
  34. onSaveProjectAs,
  35. onOpenProject,
  36. }
  37. }