MCPcopy
hub / github.com/marktext/marktext / addFile

Function addFile

packages/desktop/src/renderer/src/store/treeCtrl.ts:68–125  ·  view source on GitHub ↗
(tree: TreeFolder, file: any, sortBy: string = 'title', sortOrder: string = 'asc')

Source from the content-addressed store, hash-verified

66 */
67// eslint-disable-next-line @typescript-eslint/no-explicit-any
68export const addFile = (tree: TreeFolder, file: any, sortBy: string = 'title', sortOrder: string = 'asc'): void => {
69 const { pathname, name } = file
70 const dirname = window.path.dirname(pathname)
71 const subDirectories = getSubdirectoriesFromRoot(tree.pathname, dirname)
72
73 let currentPath = tree.pathname
74 let currentFolder: TreeFolder = tree
75 let currentSubFolders: TreeFolder[] = tree.folders
76 for (const directoryName of subDirectories) {
77 let childFolder = currentSubFolders.find((f) => f.name === directoryName)
78 if (!childFolder) {
79 childFolder = {
80 id: getUniqueId(),
81 pathname: `${currentPath}${PATH_SEPARATOR}${directoryName}`,
82 name: directoryName,
83 isCollapsed: true,
84 isDirectory: true,
85 isFile: false,
86 isMarkdown: false,
87 folders: [],
88 files: []
89 }
90 const idx = currentSubFolders.findIndex((f) => naturalCompare(f.name, directoryName) > 0)
91 if (idx !== -1) {
92 currentSubFolders.splice(idx, 0, childFolder)
93 } else {
94 currentSubFolders.push(childFolder)
95 }
96 }
97
98 currentPath = `${currentPath}${PATH_SEPARATOR}${directoryName}`
99 currentFolder = childFolder
100 currentSubFolders = childFolder.folders
101 }
102
103 // Add file to related directory.
104 if (!currentFolder.files.find((f) => f.name === name)) {
105 // Remove file content from object.
106 const fileCopy: TreeFile = {
107 id: getUniqueId(),
108 birthTime: file.birthTime,
109 mtimeMs: file.mtimeMs,
110 isDirectory: file.isDirectory,
111 isFile: file.isFile,
112 isMarkdown: file.isMarkdown,
113 name: file.name,
114 pathname: file.pathname
115 }
116
117 const comparator = makeFileComparator(sortBy, sortOrder)
118 const idx = currentFolder.files.findIndex((f) => comparator(f, fileCopy) > 0)
119 if (idx !== -1) {
120 currentFolder.files.splice(idx, 0, fileCopy)
121 } else {
122 currentFolder.files.push(fileCopy)
123 }
124 }
125}

Callers 1

_processTreeEventFunction · 0.90

Calls 8

getUniqueIdFunction · 0.90
naturalCompareFunction · 0.85
makeFileComparatorFunction · 0.85
dirnameMethod · 0.80
findIndexMethod · 0.80
pushMethod · 0.80
findMethod · 0.45

Tested by

no test coverage detected