mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-04 23:15:57 -05:00
6f34a84ebd
Add docs to FileTreeDataContext to replace 'docs' scope value in React code GitOrigin-RevId: 430f795eb0cd17f0f4fab9c61e46fb04ff3030b3
31 lines
966 B
TypeScript
31 lines
966 B
TypeScript
import { Folder } from '../../../../../types/folder'
|
|
import { DocId, MainDocument } from '../../../../../types/project-settings'
|
|
|
|
function findAllDocsInFolder(folder: Folder, path = '') {
|
|
const docs = folder.docs.map<MainDocument>(doc => ({
|
|
doc: { id: doc._id as DocId, name: doc.name },
|
|
path: path + doc.name,
|
|
}))
|
|
for (const subFolder of folder.folders) {
|
|
docs.push(...findAllDocsInFolder(subFolder, `${path}${subFolder.name}/`))
|
|
}
|
|
return docs
|
|
}
|
|
|
|
export function docsInFolder(folder: Folder) {
|
|
const docsInTree = findAllDocsInFolder(folder)
|
|
docsInTree.sort(function (a, b) {
|
|
const aDepth = (a.path.match(/\//g) || []).length
|
|
const bDepth = (b.path.match(/\//g) || []).length
|
|
if (aDepth - bDepth !== 0) {
|
|
return -(aDepth - bDepth) // Deeper path == folder first
|
|
} else if (a.path < b.path) {
|
|
return -1
|
|
} else if (a.path > b.path) {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
})
|
|
return docsInTree
|
|
}
|