forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeCtrl.js
More file actions
161 lines (146 loc) · 4.59 KB
/
treeCtrl.js
File metadata and controls
161 lines (146 loc) · 4.59 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import path from 'path'
import { getUniqueId } from '../util'
import { PATH_SEPARATOR } from '../config'
/**
* Return all sub-directories relative to the root directory.
*
* @param {string} rootPath Root directory path
* @param {string} pathname Full directory path
* @returns {Array<string>} Sub-directories relative to root.
*/
const getSubdirectoriesFromRoot = (rootPath, pathname) => {
if (!path.isAbsolute(pathname)) {
throw new Error('Invalid path!')
}
const relativePath = path.relative(rootPath, pathname)
return relativePath ? relativePath.split(PATH_SEPARATOR) : []
}
/**
* Add a new file to the tree list.
*
* @param {*} tree Root file tree
* @param {*} file The file that should be added
*/
export const addFile = (tree, file) => {
const { pathname, name } = file
const dirname = path.dirname(pathname)
const subDirectories = getSubdirectoriesFromRoot(tree.pathname, dirname)
let currentPath = tree.pathname
let currentFolder = tree
let currentSubFolders = tree.folders
for (const directoryName of subDirectories) {
let childFolder = currentSubFolders.find(f => f.name === directoryName)
if (!childFolder) {
childFolder = {
id: getUniqueId(),
pathname: `${currentPath}${PATH_SEPARATOR}${directoryName}`,
name: directoryName,
isCollapsed: true,
isDirectory: true,
isFile: false,
isMarkdown: false,
folders: [],
files: []
}
currentSubFolders.push(childFolder)
}
currentPath = `${currentPath}${PATH_SEPARATOR}${directoryName}`
currentFolder = childFolder
currentSubFolders = childFolder.folders
}
// Add file to related directory
if (!currentFolder.files.find(f => f.name === name)) {
// Remove file content from object.
const fileCopy = {
id: getUniqueId(),
birthTime: file.birthTime,
isDirectory: file.isDirectory,
isFile: file.isFile,
isMarkdown: file.isMarkdown,
name: file.name,
pathname: file.pathname
}
const idx = currentFolder.files.findIndex(f => {
return f.name.localeCompare(name) > 0
})
if (idx !== -1) {
currentFolder.files.splice(idx, 0, fileCopy)
} else {
currentFolder.files.push(fileCopy)
}
}
}
/**
* Add a new directory to the tree list.
*
* @param {*} tree Root file tree
* @param {*} dir The directory that should be added
*/
export const addDirectory = (tree, dir) => {
const subDirectories = getSubdirectoriesFromRoot(tree.pathname, dir.pathname)
let currentPath = tree.pathname
let currentSubFolders = tree.folders
for (const directoryName of subDirectories) {
let childFolder = currentSubFolders.find(f => f.name === directoryName)
if (!childFolder) {
childFolder = {
id: getUniqueId(),
pathname: `${currentPath}${PATH_SEPARATOR}${directoryName}`,
name: directoryName,
isCollapsed: true,
isDirectory: true,
isFile: false,
isMarkdown: false,
folders: [],
files: []
}
currentSubFolders.push(childFolder)
}
currentPath = `${currentPath}${PATH_SEPARATOR}${directoryName}`
currentSubFolders = childFolder.folders
}
}
/**
* Remove the given file from the tree list.
*
* @param {*} tree Root file tree
* @param {*} file The file that should be deleted
*/
export const unlinkFile = (tree, file) => {
const { pathname } = file
const dirname = path.dirname(pathname)
const subDirectories = getSubdirectoriesFromRoot(tree.pathname, dirname)
let currentFolder = tree
let currentSubFolders = tree.folders
for (const directoryName of subDirectories) {
const childFolder = currentSubFolders.find(f => f.name === directoryName)
if (!childFolder) return
currentFolder = childFolder
currentSubFolders = childFolder.folders
}
const index = currentFolder.files.findIndex(f => f.pathname === pathname)
if (index !== -1) {
currentFolder.files.splice(index, 1)
}
}
/**
* Remove the given directory from the tree list.
*
* @param {*} tree Root file tree
* @param {*} dir The directory that should be deleted
*/
export const unlinkDirectory = (tree, dir) => {
const { pathname } = dir
const subDirectories = getSubdirectoriesFromRoot(tree.pathname, pathname)
subDirectories.pop()
let currentFolder = tree.folders
for (const directoryName of subDirectories) {
const childFolder = currentFolder.find(f => f.name === directoryName)
if (!childFolder) return
currentFolder = childFolder.folders
}
const index = currentFolder.findIndex(f => f.pathname === pathname)
if (index !== -1) {
currentFolder.splice(index, 1)
}
}