73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import type { DoclibFolder } from '@/api/doclib'
|
|
|
|
export function actionsFor(rows: Array<{ canManage: boolean }>) {
|
|
return {
|
|
share: rows.length > 0 && rows.every((row) => row.canManage),
|
|
download: rows.length > 0,
|
|
delete: rows.length > 0 && rows.every((row) => row.canManage)
|
|
}
|
|
}
|
|
|
|
/** 多选打开分享弹窗时,初始勾选为各文档现有分享人的交集。 */
|
|
export function shareUserIntersection(
|
|
documents: Array<{ shares?: Array<{ id: string }> | null }>
|
|
): string[] {
|
|
if (!documents.length) return []
|
|
let ids = new Set(
|
|
(documents[0].shares || []).map((person) => String(person.id || '').trim()).filter(Boolean)
|
|
)
|
|
for (const doc of documents.slice(1)) {
|
|
const next = new Set(
|
|
(doc.shares || []).map((person) => String(person.id || '').trim()).filter(Boolean)
|
|
)
|
|
ids = new Set([...ids].filter((id) => next.has(id)))
|
|
}
|
|
return [...ids]
|
|
}
|
|
|
|
export function findFolder(nodes: DoclibFolder[], id: string): DoclibFolder | undefined {
|
|
for (const node of nodes) {
|
|
if (node.id === id) return node
|
|
const child = findFolder(node.children || [], id)
|
|
if (child) return child
|
|
}
|
|
}
|
|
|
|
export function formatFileSize(size: number) {
|
|
if (size < 1024) return `${size} B`
|
|
if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`
|
|
return `${(size / 1024 / 1024).toFixed(1)} MB`
|
|
}
|
|
|
|
export function validateUpload(file: { name: string; size: number }, maxBytes: number) {
|
|
if (file.size === 0) return '不能上传空文件'
|
|
if (file.size > maxBytes) return `文件大小不能超过 ${formatFileSize(maxBytes)}`
|
|
return ''
|
|
}
|
|
|
|
export function createLatestRequest() {
|
|
let sequence = 0
|
|
return {
|
|
start: () => ++sequence,
|
|
isCurrent: (id: number) => id === sequence,
|
|
invalidate: () => {
|
|
sequence++
|
|
}
|
|
}
|
|
}
|
|
|
|
export function folderOptions(nodes: DoclibFolder[]) {
|
|
const options = [{ value: '', label: '我的文档 / 根目录', name: '根目录', depth: 0 }]
|
|
const visit = (items: DoclibFolder[], depth: number) => {
|
|
for (const item of items) {
|
|
if (item.scope !== 'mine') continue
|
|
if (item.folderId && item.canManage) {
|
|
options.push({ value: item.folderId, label: item.fullPath, name: item.name, depth })
|
|
}
|
|
visit(item.children || [], item.folderId ? depth + 1 : depth)
|
|
}
|
|
}
|
|
visit(nodes, 0)
|
|
return options
|
|
}
|