wikiFiles.ts
2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Wiki 文件列表/预览相关的纯工具:文件树构建、分页页码、预览类型判定。
// 从 app/page.tsx 抽出。
import type { TreeFolder, TreeFile } from "@/types/wiki";
export const canDirectIframePreview = (name: string) => {
const lowerName = name.toLowerCase();
return lowerName.endsWith(".pdf");
};
export const needsControlledPreview = (name: string) => {
const lowerName = name.toLowerCase();
return lowerName.endsWith(".ppt") || lowerName.endsWith(".pptx");
};
export const needsSpreadsheetPreview = (name: string) => {
const lowerName = name.toLowerCase();
return lowerName.endsWith(".xlsx") || lowerName.endsWith(".xls");
};
export const buildTree = (links: string[]) => {
const root: TreeFolder = { name: "root", type: "folder", children: {} };
links.forEach(link => {
// 展示路径:去掉库根前缀(base=ware/wiki/,部门库=ware/kb/dept/<id>/wiki/)到 /wiki/ 为止;
// 但节点 path 保留**完整项目根相对路径**,供后端按项目根解析(部门库文件才不会 404)。
const display = link.replace(/^.*\/wiki\//, '');
const parts = display.split('/');
let current = root;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i === parts.length - 1) {
current.children[part] = { name: part, type: "file", path: link };
} else {
if (!current.children[part]) {
current.children[part] = { name: part, type: "folder", children: {} };
}
current = current.children[part] as TreeFolder;
}
}
});
return root;
};
export const PAGE_SIZE_OPTIONS = [10, 20, 50, 100];
// 生成分页页码列表,用 "ellipsis" 表示省略号
export const getPaginationRange = (currentPage: number, totalPages: number): (number | "ellipsis")[] => {
if (totalPages <= 7) {
return Array.from({ length: totalPages }, (_, i) => i + 1);
}
const pages = new Set<number>([1, totalPages, currentPage, currentPage - 1, currentPage + 1]);
const sorted = Array.from(pages).filter(p => p >= 1 && p <= totalPages).sort((a, b) => a - b);
const result: (number | "ellipsis")[] = [];
sorted.forEach((page, idx) => {
if (idx > 0 && page - sorted[idx - 1] > 1) {
result.push("ellipsis");
}
result.push(page);
});
return result;
};
export type { TreeFolder, TreeFile };