mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: finish file-graph-algorithm (check required)
This commit is contained in:
1 parent
4255eb5910
commit
ab570e9c1b
2 files changed
+41
No files matched your search
@@ -9,18 +9,41 @@ export default class FileGraph {
|
|||||||
// Logic to add the file node to the graph
|
// Logic to add the file node to the graph
|
||||||
// You can implement the logic to find the appropriate location in the file graph
|
// You can implement the logic to find the appropriate location in the file graph
|
||||||
// If the file node already exists, throw an error
|
// If the file node already exists, throw an error
|
||||||
|
|
||||||
|
const newNode = new FileNode(file)
|
||||||
|
this.root.addChild(newNode, { makeParents: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(file) {
|
remove(file) {
|
||||||
// Logic to remove a file node from the graph
|
// Logic to remove a file node from the graph
|
||||||
// If the file node is a folder, also remove all children
|
// If the file node is a folder, also remove all children
|
||||||
// If the file node doesn't exist, throw an error
|
// If the file node doesn't exist, throw an error
|
||||||
|
|
||||||
|
const nodeToRemove = this.findNode(file)
|
||||||
|
if (nodeToRemove) {
|
||||||
|
const parentNode = this.findParentNode(file)
|
||||||
|
if (parentNode) {
|
||||||
|
parentNode.children = parentNode.children.filter(
|
||||||
|
(child) => child !== nodeToRemove
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(`File node not found: ${file.path}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
discover(eventFiles) {
|
discover(eventFiles) {
|
||||||
// Logic to handle the discover event
|
// Logic to handle the discover event
|
||||||
// Overwrite the file graph with the provided files
|
// Overwrite the file graph with the provided files
|
||||||
// Remove file nodes that don't exist anymore and add new ones
|
// Remove file nodes that don't exist anymore and add new ones
|
||||||
|
|
||||||
|
const updatedNodes = eventFiles.map((file) => new FileNode(file))
|
||||||
|
this.root = new FileNode({
|
||||||
|
path: '/',
|
||||||
|
type: 'folder',
|
||||||
|
discovered: true,
|
||||||
|
children: updatedNodes,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEvent(event) {
|
handleEvent(event) {
|
||||||
|
|||||||
@@ -92,4 +92,22 @@ export default class FileNode extends File {
|
|||||||
.join('')
|
.join('')
|
||||||
return `${indent}${this.name}${suffix}\n${childrenString}`
|
return `${indent}${this.name}${suffix}\n${childrenString}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findNode(fileToFind) {
|
||||||
|
const pathFolders = fileToFind.path
|
||||||
|
.split('/')
|
||||||
|
.filter((folder) => folder.length > 0)
|
||||||
|
let currentNode = this
|
||||||
|
|
||||||
|
for (const folder of pathFolders) {
|
||||||
|
currentNode = currentNode.children.find(
|
||||||
|
(child) => child.name === folder && child.type === 'folder'
|
||||||
|
)
|
||||||
|
if (!currentNode) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentNode
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user