2023-10-16 13:28:41 -04:00
|
|
|
import { useMachine } from '@xstate/react'
|
|
|
|
import { useNavigate, useRouteLoaderData } from 'react-router-dom'
|
2024-02-11 12:59:00 +11:00
|
|
|
import { type IndexLoaderData } from 'lib/types'
|
|
|
|
import { paths } from 'lib/paths'
|
2023-10-16 13:28:41 -04:00
|
|
|
import React, { createContext } from 'react'
|
|
|
|
import { toast } from 'react-hot-toast'
|
|
|
|
import {
|
|
|
|
AnyStateMachine,
|
|
|
|
ContextFrom,
|
|
|
|
EventFrom,
|
|
|
|
InterpreterFrom,
|
|
|
|
Prop,
|
|
|
|
StateFrom,
|
|
|
|
} from 'xstate'
|
|
|
|
import { useCommandsContext } from 'hooks/useCommandsContext'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { fileMachine } from 'machines/fileMachine'
|
2024-04-09 08:04:36 -04:00
|
|
|
import { mkdir, remove, rename, create } from '@tauri-apps/plugin-fs'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { readProject } from 'lib/tauriFS'
|
2023-10-16 13:28:41 -04:00
|
|
|
import { isTauri } from 'lib/isTauri'
|
2024-04-09 08:04:36 -04:00
|
|
|
import { join, sep } from '@tauri-apps/api/path'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { DEFAULT_FILE_NAME, FILE_EXT } from 'lib/constants'
|
2023-10-16 13:28:41 -04:00
|
|
|
|
|
|
|
type MachineContext<T extends AnyStateMachine> = {
|
|
|
|
state: StateFrom<T>
|
|
|
|
context: ContextFrom<T>
|
|
|
|
send: Prop<InterpreterFrom<T>, 'send'>
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FileContext = createContext(
|
|
|
|
{} as MachineContext<typeof fileMachine>
|
|
|
|
)
|
|
|
|
|
|
|
|
export const FileMachineProvider = ({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigate()
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
const { commandBarSend } = useCommandsContext()
|
2023-10-16 13:28:41 -04:00
|
|
|
const { project } = useRouteLoaderData(paths.FILE) as IndexLoaderData
|
|
|
|
|
|
|
|
const [state, send] = useMachine(fileMachine, {
|
|
|
|
context: {
|
|
|
|
project,
|
|
|
|
selectedDirectory: project,
|
|
|
|
},
|
|
|
|
actions: {
|
2024-03-05 20:37:48 -05:00
|
|
|
navigateToFile: (context, event) => {
|
2023-10-16 13:28:41 -04:00
|
|
|
if (event.data && 'name' in event.data) {
|
Command bar: add extrude command, nonlinear editing, etc (#1204)
* Tweak toaster look and feel
* Add icons, tweak plus icon names
* Rename commandBarMeta to commandBarConfig
* Refactor command bar, add support for icons
* Create a tailwind plugin for aria-pressed button state
* Remove overlay from behind command bar
* Clean up toolbar
* Button and other style tweaks
* Icon tweaks follow-up: make old icons work with new sizing
* Delete unused static icons
* More CSS tweaks
* Small CSS tweak to project sidebar
* Add command bar E2E test
* fumpt
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* fix typo in a comment
* Fix icon padding (built version only)
* Update onboarding and warning banner icons padding
* Misc minor style fixes
* Get Extrude opening and canceling from command bar
* Iconography tweaks
* Get extrude kind of working
* Refactor command bar config types and organization
* Move command bar configs to be co-located with each other
* Start building a state machine for the command bar
* Start converting command bar to state machine
* Add support for multiple args, confirmation step
* Submission behavior, hotkeys, code organization
* Add new test for extruding from command bar
* Polish step back and selection hotkeys, CSS tweaks
* Loading style tweaks
* Validate selection inputs, polish UX of args re-editing
* Prevent submission with multiple selection on singlular arg
* Remove stray console logs
* Tweak test, CSS nit, remove extrude "result" argument
* Fix linting warnings
* Show Ctrl+/ instead of ⌘K on all platforms but Mac
* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)
* Add "Enter sketch" to command bar
* fix command bar test
* Fix flaky cmd bar extrude test by waiting for engine select response
* Cover both button labels '⌘K' and 'Ctrl+/' in test
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2023-12-13 12:49:01 -05:00
|
|
|
commandBarSend({ type: 'Close' })
|
2023-10-16 13:28:41 -04:00
|
|
|
navigate(
|
|
|
|
`${paths.FILE}/${encodeURIComponent(
|
2024-04-09 08:04:36 -04:00
|
|
|
context.selectedDirectory + sep() + event.data.name
|
2023-10-16 13:28:41 -04:00
|
|
|
)}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
toastSuccess: (_, event) =>
|
|
|
|
event.data && toast.success((event.data || '') + ''),
|
|
|
|
toastError: (_, event) => toast.error((event.data || '') + ''),
|
|
|
|
},
|
|
|
|
services: {
|
|
|
|
readFiles: async (context: ContextFrom<typeof fileMachine>) => {
|
|
|
|
const newFiles = isTauri()
|
|
|
|
? await readProject(context.project.path)
|
|
|
|
: []
|
|
|
|
return {
|
|
|
|
...context.project,
|
|
|
|
children: newFiles,
|
|
|
|
}
|
|
|
|
},
|
2024-03-05 20:37:48 -05:00
|
|
|
createFile: async (context, event) => {
|
2023-10-16 13:28:41 -04:00
|
|
|
let name = event.data.name.trim() || DEFAULT_FILE_NAME
|
|
|
|
|
|
|
|
if (event.data.makeDir) {
|
2024-04-09 08:04:36 -04:00
|
|
|
await mkdir(await join(context.selectedDirectory.path, name))
|
2023-10-16 13:28:41 -04:00
|
|
|
} else {
|
2024-04-09 08:04:36 -04:00
|
|
|
await create(
|
2023-10-16 13:28:41 -04:00
|
|
|
context.selectedDirectory.path +
|
2024-04-09 08:04:36 -04:00
|
|
|
sep() +
|
2023-10-16 13:28:41 -04:00
|
|
|
name +
|
2024-04-09 08:04:36 -04:00
|
|
|
(name.endsWith(FILE_EXT) ? '' : FILE_EXT)
|
2023-10-16 13:28:41 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return `Successfully created "${name}"`
|
|
|
|
},
|
|
|
|
renameFile: async (
|
|
|
|
context: ContextFrom<typeof fileMachine>,
|
|
|
|
event: EventFrom<typeof fileMachine, 'Rename file'>
|
|
|
|
) => {
|
|
|
|
const { oldName, newName, isDir } = event.data
|
|
|
|
let name = newName ? newName : DEFAULT_FILE_NAME
|
|
|
|
|
2024-04-09 08:04:36 -04:00
|
|
|
await rename(
|
|
|
|
await join(context.selectedDirectory.path, oldName),
|
|
|
|
(await join(context.selectedDirectory.path, name)) +
|
|
|
|
(name.endsWith(FILE_EXT) || isDir ? '' : FILE_EXT),
|
|
|
|
{}
|
2023-10-16 13:28:41 -04:00
|
|
|
)
|
|
|
|
return (
|
|
|
|
oldName !== name && `Successfully renamed "${oldName}" to "${name}"`
|
|
|
|
)
|
|
|
|
},
|
|
|
|
deleteFile: async (
|
|
|
|
context: ContextFrom<typeof fileMachine>,
|
|
|
|
event: EventFrom<typeof fileMachine, 'Delete file'>
|
|
|
|
) => {
|
|
|
|
const isDir = !!event.data.children
|
|
|
|
|
|
|
|
if (isDir) {
|
2024-04-09 08:04:36 -04:00
|
|
|
await remove(event.data.path, {
|
2023-10-16 13:28:41 -04:00
|
|
|
recursive: true,
|
|
|
|
}).catch((e) => console.error('Error deleting directory', e))
|
|
|
|
} else {
|
2024-04-09 08:04:36 -04:00
|
|
|
await remove(event.data.path).catch((e) =>
|
2023-10-16 13:28:41 -04:00
|
|
|
console.error('Error deleting file', e)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return `Successfully deleted ${isDir ? 'folder' : 'file'} "${
|
|
|
|
event.data.name
|
|
|
|
}"`
|
|
|
|
},
|
|
|
|
},
|
|
|
|
guards: {
|
|
|
|
'Has at least 1 file': (_, event: EventFrom<typeof fileMachine>) => {
|
|
|
|
if (event.type !== 'done.invoke.read-files') return false
|
|
|
|
return !!event?.data?.children && event.data.children.length > 0
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FileContext.Provider
|
|
|
|
value={{
|
|
|
|
send,
|
|
|
|
state,
|
|
|
|
context: state.context, // just a convenience, can remove if we need to save on memory
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</FileContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileMachineProvider
|