2021-01-27 04:54:15 -05:00
|
|
|
import OError from '@overleaf/o-error'
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
export function findInTreeOrThrow(tree, id) {
|
|
|
|
const found = findInTree(tree, id)
|
|
|
|
if (found) return found
|
2021-01-27 04:54:15 -05:00
|
|
|
throw new OError('Entity not found in tree', { entityId: id })
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findAllInTreeOrThrow(tree, ids) {
|
|
|
|
let list = new Set()
|
|
|
|
ids.forEach(id => {
|
|
|
|
list.add(findInTreeOrThrow(tree, id))
|
|
|
|
})
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findAllFolderIdsInFolder(folder) {
|
|
|
|
const list = new Set([folder._id])
|
|
|
|
for (const index in folder.folders) {
|
|
|
|
const subFolder = folder.folders[index]
|
|
|
|
findAllFolderIdsInFolder(subFolder).forEach(subFolderId => {
|
|
|
|
list.add(subFolderId)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findAllFolderIdsInFolders(folders) {
|
|
|
|
let list = new Set()
|
|
|
|
folders.forEach(folder => {
|
|
|
|
findAllFolderIdsInFolder(folder).forEach(folderId => {
|
|
|
|
list.add(folderId)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:22:40 -05:00
|
|
|
export function findInTree(tree, id, path) {
|
|
|
|
if (!path) {
|
|
|
|
path = [tree._id]
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
for (const index in tree.docs) {
|
|
|
|
const doc = tree.docs[index]
|
|
|
|
if (doc._id === id) {
|
|
|
|
return {
|
|
|
|
entity: doc,
|
|
|
|
type: 'doc',
|
|
|
|
parent: tree.docs,
|
|
|
|
parentFolderId: tree._id,
|
2021-01-07 09:22:40 -05:00
|
|
|
path,
|
2021-04-27 03:52:58 -04:00
|
|
|
index,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const index in tree.fileRefs) {
|
|
|
|
const file = tree.fileRefs[index]
|
|
|
|
if (file._id === id) {
|
|
|
|
return {
|
|
|
|
entity: file,
|
|
|
|
type: 'fileRef',
|
|
|
|
parent: tree.fileRefs,
|
|
|
|
parentFolderId: tree._id,
|
2021-01-07 09:22:40 -05:00
|
|
|
path,
|
2021-04-27 03:52:58 -04:00
|
|
|
index,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const index in tree.folders) {
|
|
|
|
const folder = tree.folders[index]
|
|
|
|
if (folder._id === id) {
|
|
|
|
return {
|
|
|
|
entity: folder,
|
|
|
|
type: 'folder',
|
|
|
|
parent: tree.folders,
|
|
|
|
parentFolderId: tree._id,
|
2021-01-07 09:22:40 -05:00
|
|
|
path,
|
2021-04-27 03:52:58 -04:00
|
|
|
index,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-07 09:22:40 -05:00
|
|
|
const found = findInTree(folder, id, path.concat(folder._id))
|
2020-11-26 09:22:30 -05:00
|
|
|
if (found) return found
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|