chore: unable to implement insert at index, need to come back to this, wasting time
This commit is contained in:
@ -9,6 +9,7 @@ import { sortFilesAndDirectories } from '@src/lib/desktopFS'
|
|||||||
export interface FileExplorerEntry extends FileEntry {
|
export interface FileExplorerEntry extends FileEntry {
|
||||||
parentPath: string
|
parentPath: string
|
||||||
level: number
|
level: number
|
||||||
|
index: number
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FileExplorerRow extends FileExplorerEntry {
|
interface FileExplorerRow extends FileExplorerEntry {
|
||||||
@ -63,11 +64,13 @@ const flattenProjectHelper = (
|
|||||||
parentPath: string, // the parentPath for the given f:FileEntry passed in
|
parentPath: string, // the parentPath for the given f:FileEntry passed in
|
||||||
level: number // the level within the tree for the given f:FileEntry, level starts at 0 goes to positive N
|
level: number // the level within the tree for the given f:FileEntry, level starts at 0 goes to positive N
|
||||||
) => {
|
) => {
|
||||||
|
const index = list.length
|
||||||
// mark the parent and level of the FileEntry
|
// mark the parent and level of the FileEntry
|
||||||
const content: FileExplorerEntry = {
|
const content: FileExplorerEntry = {
|
||||||
...f,
|
...f,
|
||||||
parentPath,
|
parentPath,
|
||||||
level,
|
level,
|
||||||
|
index
|
||||||
}
|
}
|
||||||
// keep track of the file once within the recursive list that will be built up
|
// keep track of the file once within the recursive list that will be built up
|
||||||
list.push(content)
|
list.push(content)
|
||||||
@ -112,51 +115,23 @@ const flattenProject = (
|
|||||||
return flattenTreeInOrder
|
return flattenTreeInOrder
|
||||||
}
|
}
|
||||||
|
|
||||||
const insertFakeRowAtFirstPositionUnderParentAfterFolder = (fileExplorerEntries: FileExplorerEntry[], fakeRow: {path: string, isFile: boolean} | null) : void => {
|
const insertFakeRowAtFirstPositionUnderParentAfterFolder = (
|
||||||
|
fileExplorerEntries: FileExplorerEntry[],
|
||||||
|
fakeRow: {entry: FileExplorerEntry | null, isFile: boolean} | null
|
||||||
|
): void => {
|
||||||
if (!fakeRow) {
|
if (!fakeRow) {
|
||||||
// no op
|
// no op
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let insertIndex = -1
|
console.log(fakeRow.entry)
|
||||||
let foundEntry = null
|
|
||||||
for (let index = 0; index < fileExplorerEntries.length; index++) {
|
|
||||||
const entry = fileExplorerEntries[index]
|
|
||||||
const path = entry.parentPath
|
|
||||||
const isFolder = entry.children !== null
|
|
||||||
const nextEntry = index + 1 < fileExplorerEntries.length ? fileExplorerEntries[index+1] : null
|
|
||||||
|
|
||||||
// Found first folder and my fake row is a folder
|
// const requestedEntry: FileExplorerEntry = {
|
||||||
if (path === fakeRow.path && isFolder && !fakeRow.isFile) {
|
// ...fakeRow,
|
||||||
console.log('exited1')
|
|
||||||
insertIndex = index
|
|
||||||
foundEntry = entry
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fakeRow.isFile && (nextEntry === null || nextEntry.path !== fakeRow.path)) {
|
|
||||||
insertIndex = index
|
|
||||||
foundEntry = entry
|
|
||||||
console.log('inserting file', insertIndex)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// if (path === fakeRow.path && isFolder && fakeRow.isFile && nextEntry && nextEntry.path !== fakeRow.path) {
|
|
||||||
// console.log('exited2')
|
|
||||||
// insertIndex = index
|
|
||||||
// foundEntry = entry
|
|
||||||
// break
|
|
||||||
// }
|
// }
|
||||||
}
|
// fileExplorerEntries.splice(insertIndex, 0, requestedEntry)
|
||||||
|
|
||||||
|
|
||||||
if (insertIndex >= 0 && foundEntry) {
|
|
||||||
const requestedEntry : FileExplorerEntry = {
|
|
||||||
...foundEntry,
|
|
||||||
children: fakeRow.isFile ? null : [],
|
|
||||||
name: 'dog'
|
|
||||||
}
|
|
||||||
console.log(requestedEntry)
|
|
||||||
fileExplorerEntries.splice(insertIndex, 0, requestedEntry);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -164,7 +139,7 @@ const insertFakeRowAtFirstPositionUnderParentAfterFolder = (fileExplorerEntries:
|
|||||||
* each row is rendered one after another in the same parent DOM element
|
* each row is rendered one after another in the same parent DOM element
|
||||||
* rows will have aria support to understand the linear div soup layout
|
* rows will have aria support to understand the linear div soup layout
|
||||||
*
|
*
|
||||||
* externall control the openedRows and selectedRows since actions need to know
|
|
||||||
* what is opened and selected outside of this logic level.
|
* what is opened and selected outside of this logic level.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -173,13 +148,13 @@ export const FileExplorer = ({
|
|||||||
openedRows,
|
openedRows,
|
||||||
selectedRow,
|
selectedRow,
|
||||||
onRowClickCallback,
|
onRowClickCallback,
|
||||||
fakeRow
|
fakeRow,
|
||||||
}: {
|
}: {
|
||||||
parentProject: Project
|
parentProject: Project
|
||||||
openedRows: { [key: string]: boolean }
|
openedRows: { [key: string]: boolean }
|
||||||
selectedRow: FileEntry | null
|
selectedRow: FileEntry | null
|
||||||
onRowClickCallback: (file: FileExplorerEntry) => void
|
onRowClickCallback: (file: FileExplorerEntry) => void
|
||||||
fakeRow: {path: string, isFile: boolean} | null
|
fakeRow: {entry: FileExplorerEntry | null, isFile: boolean} | null
|
||||||
}) => {
|
}) => {
|
||||||
// Wrap the FileEntry in a FileExplorerEntry to keep track for more metadata
|
// Wrap the FileEntry in a FileExplorerEntry to keep track for more metadata
|
||||||
let flattenedData: FileExplorerEntry[] = []
|
let flattenedData: FileExplorerEntry[] = []
|
||||||
@ -187,10 +162,8 @@ export const FileExplorer = ({
|
|||||||
if (parentProject && parentProject.children) {
|
if (parentProject && parentProject.children) {
|
||||||
// moves all folders up and files down, files are sorted within folders
|
// moves all folders up and files down, files are sorted within folders
|
||||||
const sortedData = sortFilesAndDirectories(parentProject.children)
|
const sortedData = sortFilesAndDirectories(parentProject.children)
|
||||||
console.log(sortedData)
|
|
||||||
// pre order traversal of the tree
|
// pre order traversal of the tree
|
||||||
flattenedData = flattenProject(sortedData, parentProject.name)
|
flattenedData = flattenProject(sortedData, parentProject.name)
|
||||||
|
|
||||||
// insert fake row if one is present
|
// insert fake row if one is present
|
||||||
// insertFakeRowAtFirstPositionUnderParentAfterFolder(flattenedData, fakeRow)
|
// insertFakeRowAtFirstPositionUnderParentAfterFolder(flattenedData, fakeRow)
|
||||||
}
|
}
|
||||||
@ -240,7 +213,7 @@ export const FileExplorer = ({
|
|||||||
rowClicked: () => {
|
rowClicked: () => {
|
||||||
onRowClickCallback(child)
|
onRowClickCallback(child)
|
||||||
},
|
},
|
||||||
isFake: false
|
isFake: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
return row
|
return row
|
||||||
|
|||||||
@ -31,7 +31,7 @@ export const ProjectExplorer = ({
|
|||||||
|
|
||||||
// fake row is used for new files or folders
|
// fake row is used for new files or folders
|
||||||
// you should not be able to have multiple fake rows for creation
|
// you should not be able to have multiple fake rows for creation
|
||||||
const [fakeRow, setFakeRow] = useState<{path: string, isFile: boolean} | null>(null)
|
const [fakeRow, setFakeRow] = useState<{entry: FileExplorerEntry | null, isFile: boolean} | null>(null)
|
||||||
|
|
||||||
const onRowClickCallback = (file: FileExplorerEntry) => {
|
const onRowClickCallback = (file: FileExplorerEntry) => {
|
||||||
const newOpenedRows = { ...openedRows }
|
const newOpenedRows = { ...openedRows }
|
||||||
@ -53,12 +53,7 @@ export const ProjectExplorer = ({
|
|||||||
<div className="h-6 flex flex-row gap-1">
|
<div className="h-6 flex flex-row gap-1">
|
||||||
<FileExplorerHeaderActions
|
<FileExplorerHeaderActions
|
||||||
onCreateFile={() => {
|
onCreateFile={() => {
|
||||||
// Use the selected level within the file tree or the root level of the project
|
setFakeRow({entry: selectedRow, isFile: true})
|
||||||
const folderPath = selectedRow?.parentPath ? constructPath({parentPath: selectedRow?.parentPath, name: selectedRow.name}) : null
|
|
||||||
const parentPath = selectedRow?.parentPath
|
|
||||||
const isFile = selectedRow?.children === null
|
|
||||||
const path = (isFile ? parentPath : folderPath) || project.name
|
|
||||||
setFakeRow({path, isFile})
|
|
||||||
}}
|
}}
|
||||||
onCreateFolder={() => {
|
onCreateFolder={() => {
|
||||||
console.log('onCreateFolder TODO')
|
console.log('onCreateFolder TODO')
|
||||||
|
|||||||
Reference in New Issue
Block a user