forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
68 lines (51 loc) · 1.6 KB
/
utils.js
File metadata and controls
68 lines (51 loc) · 1.6 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
export function sortingFunction(a, b) {
// directories come first, sorted alphabetically
// then files, also sorted alphabetically
let first;
if (a.type === b.type) {
if (a.title < b.title) first = a;
else first = b;
} else if (a.type === 'directory') {
first = a;
} else {
first = b;
}
// js be weird
if (first === a) return -1;
return 1;
}
export function isRootLevel(files, file) {
// find out if parent directory is in sub-tree
const parentId = file.directory;
if (!parentId) return true;
const parent = files.find(f => f.id === parentId);
if (!parent) return true;
return false;
}
export function getParentDirectory(allFiles, file) {
if (!file.directory) return null;
return allFiles.find(parent => parent.id === file.directory);
}
export function getDepth(allFiles, file) {
let depth = 0;
let parent = getParentDirectory(allFiles, file);
while (parent) {
depth++;
parent = getParentDirectory(allFiles, parent);
}
return depth;
}
export function getFilesInSubTree(allFiles, selectedFile) {
const currentModuleTree = [selectedFile];
let parentDirectory = getParentDirectory(allFiles, selectedFile);
while (parentDirectory) {
currentModuleTree.push(parentDirectory);
// get parent directory of the parent directory
parentDirectory = getParentDirectory(allFiles, parentDirectory);
}
return currentModuleTree;
}
export function isChildSelected({ allFiles, directory, selectedFile }) {
const filesInCurrentSubTree = getFilesInSubTree(allFiles, selectedFile);
return filesInCurrentSubTree.find(file => file.id === directory.id);
}