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'
|
2024-08-09 02:47:25 -04:00
|
|
|
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,
|
2024-05-24 18:12:39 -04:00
|
|
|
assign,
|
2023-10-16 13:28:41 -04:00
|
|
|
} 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'
|
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'
|
2024-04-25 00:13:09 -07:00
|
|
|
import { getProjectInfo } from 'lib/tauri'
|
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()
|
2024-08-09 02:47:25 -04:00
|
|
|
const { project, file } = useRouteLoaderData(PATHS.FILE) as IndexLoaderData
|
2023-10-16 13:28:41 -04:00
|
|
|
|
|
|
|
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(
|
2024-08-09 02:47:25 -04:00
|
|
|
`${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
|
|
|
)}`
|
|
|
|
)
|
2024-05-24 18:12:39 -04:00
|
|
|
} else if (
|
|
|
|
event.data &&
|
|
|
|
'path' in event.data &&
|
|
|
|
event.data.path.endsWith(FILE_EXT)
|
|
|
|
) {
|
|
|
|
// Don't navigate to newly created directories
|
2024-08-09 02:47:25 -04:00
|
|
|
navigate(`${PATHS.FILE}/${encodeURIComponent(event.data.path)}`)
|
2023-10-16 13:28:41 -04:00
|
|
|
}
|
|
|
|
},
|
2024-05-24 18:12:39 -04:00
|
|
|
addFileToRenamingQueue: assign({
|
|
|
|
itemsBeingRenamed: (context, event) => [
|
|
|
|
...context.itemsBeingRenamed,
|
|
|
|
event.data.path,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
removeFileFromRenamingQueue: assign({
|
|
|
|
itemsBeingRenamed: (
|
|
|
|
context,
|
|
|
|
event: EventFrom<typeof fileMachine, 'done.invoke.rename-file'>
|
|
|
|
) =>
|
|
|
|
context.itemsBeingRenamed.filter(
|
|
|
|
(path) => path !== event.data.oldPath
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
renameToastSuccess: (_, event) => toast.success(event.data.message),
|
|
|
|
createToastSuccess: (_, event) => toast.success(event.data.message),
|
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()
|
2024-04-25 05:52:08 -07:00
|
|
|
? (await getProjectInfo(context.project.path)).children
|
2023-10-16 13:28:41 -04:00
|
|
|
: []
|
|
|
|
return {
|
|
|
|
...context.project,
|
|
|
|
children: newFiles,
|
|
|
|
}
|
|
|
|
},
|
2024-03-05 20:37:48 -05:00
|
|
|
createFile: async (context, event) => {
|
2024-05-24 18:12:39 -04:00
|
|
|
let createdName = event.data.name.trim() || DEFAULT_FILE_NAME
|
|
|
|
let createdPath: string
|
2023-10-16 13:28:41 -04:00
|
|
|
|
|
|
|
if (event.data.makeDir) {
|
2024-05-24 18:12:39 -04:00
|
|
|
createdPath = await join(context.selectedDirectory.path, createdName)
|
|
|
|
await mkdir(createdPath)
|
2023-10-16 13:28:41 -04:00
|
|
|
} else {
|
2024-05-24 18:12:39 -04:00
|
|
|
createdPath =
|
2023-10-16 13:28:41 -04:00
|
|
|
context.selectedDirectory.path +
|
2024-05-24 18:12:39 -04:00
|
|
|
sep() +
|
|
|
|
createdName +
|
|
|
|
(createdName.endsWith(FILE_EXT) ? '' : FILE_EXT)
|
|
|
|
await create(createdPath)
|
2023-10-16 13:28:41 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:12:39 -04:00
|
|
|
return {
|
|
|
|
message: `Successfully created "${createdName}"`,
|
|
|
|
path: createdPath,
|
|
|
|
}
|
2023-10-16 13:28:41 -04:00
|
|
|
},
|
|
|
|
renameFile: async (
|
|
|
|
context: ContextFrom<typeof fileMachine>,
|
|
|
|
event: EventFrom<typeof fileMachine, 'Rename file'>
|
|
|
|
) => {
|
|
|
|
const { oldName, newName, isDir } = event.data
|
2024-05-24 18:12:39 -04:00
|
|
|
const name = newName ? newName : DEFAULT_FILE_NAME
|
|
|
|
const oldPath = await join(context.selectedDirectory.path, oldName)
|
|
|
|
const newDirPath = await join(context.selectedDirectory.path, name)
|
|
|
|
const newPath =
|
|
|
|
newDirPath + (name.endsWith(FILE_EXT) || isDir ? '' : FILE_EXT)
|
|
|
|
|
|
|
|
await rename(oldPath, newPath, {})
|
2023-10-16 13:28:41 -04:00
|
|
|
|
2024-05-24 18:12:39 -04:00
|
|
|
if (oldPath === file?.path && project?.path) {
|
|
|
|
// If we just renamed the current file, navigate to the new path
|
2024-08-09 02:47:25 -04:00
|
|
|
navigate(PATHS.FILE + '/' + encodeURIComponent(newPath))
|
2024-05-24 18:12:39 -04:00
|
|
|
} else if (file?.path.includes(oldPath)) {
|
|
|
|
// If we just renamed a directory that the current file is in, navigate to the new path
|
|
|
|
navigate(
|
2024-08-09 02:47:25 -04:00
|
|
|
PATHS.FILE +
|
2024-05-24 18:12:39 -04:00
|
|
|
'/' +
|
|
|
|
encodeURIComponent(file.path.replace(oldPath, newDirPath))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
message: `Successfully renamed "${oldName}" to "${name}"`,
|
|
|
|
newPath,
|
|
|
|
oldPath,
|
|
|
|
}
|
2023-10-16 13:28:41 -04:00
|
|
|
},
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
}
|
2024-05-24 18:12:39 -04:00
|
|
|
|
|
|
|
// If we just deleted the current file or one of its parent directories,
|
|
|
|
// navigate to the project root
|
|
|
|
if (
|
|
|
|
(event.data.path === file?.path ||
|
|
|
|
file?.path.includes(event.data.path)) &&
|
|
|
|
project?.path
|
|
|
|
) {
|
2024-08-09 02:47:25 -04:00
|
|
|
navigate(PATHS.FILE + '/' + encodeURIComponent(project.path))
|
2024-05-24 18:12:39 -04:00
|
|
|
}
|
|
|
|
|
2023-10-16 13:28:41 -04:00
|
|
|
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
|