overleaf/services/web/frontend/js/features/source-editor/utils/file.ts
Alf Eaton c1c098e2f9 Allow files to be dragged from the file tree into the editor (#15028)
GitOrigin-RevId: f926666c032d1398a0e3f72a298116a3c7a9cd75
2024-05-09 08:04:46 +00:00

67 lines
1.6 KiB
TypeScript

import { Entity } from '../../file-tree/hooks/use-project-entities'
import { OutputEntity } from '../../file-tree/hooks/use-project-output-files'
export type FileOrDirectory = {
name: string
id: string
type: 'file' | 'doc' | 'folder'
children?: FileOrDirectory[]
}
export type File = {
path: string
name: string
id: string
}
function filterByType(type: 'file' | 'doc' | 'folder') {
return (
tree: FileOrDirectory,
path = '',
list: undefined | File[] = undefined
) => {
if (!tree) {
return list
}
if (list === undefined) {
list = []
}
const isRootFolder = tree.name === 'rootFolder' && path === ''
if (tree.children) {
for (const child of tree.children) {
filterByType(type)(
child,
`${isRootFolder ? '' : `${path ? path + '/' : path}${tree.name}/`}`,
list
)
}
}
if (tree.type === type) {
list.push({ path, id: tree.id, name: tree.name })
}
return list
}
}
export const filterFiles = filterByType('file')
export const filterDocs = filterByType('doc')
export const filterFolders = filterByType('folder')
const IMAGE_FILE_EXTENSIONS = ['png', 'jpg', 'jpeg', 'pdf']
export const hasImageExtension = (filename: string) => {
const parts = filename.split('.')
if (parts.length < 2) {
return false
}
const extension = parts[parts.length - 1].toLowerCase()
return IMAGE_FILE_EXTENSIONS.includes(extension)
}
export function isImageFile(file: File) {
return hasImageExtension(file.name)
}
export function isImageEntity(file: Entity | OutputEntity) {
return hasImageExtension(file.path)
}