|
| 1 | +/** |
| 2 | + * @file |
| 3 | + */ |
| 4 | +import produce from 'immer'; |
| 5 | + |
| 6 | +export function getRootPath(path: string) { |
| 7 | + if (typeof path !== 'string') { |
| 8 | + return ''; |
| 9 | + } |
| 10 | + const lastIndex = path.lastIndexOf('/'); |
| 11 | + let rootPath = path; |
| 12 | + if (path.indexOf('.') > 0) { |
| 13 | + rootPath = path.substring(0, lastIndex); |
| 14 | + } |
| 15 | + return rootPath; |
| 16 | +} |
| 17 | + |
| 18 | +export function updateNodeData(nodeData: Record<string, any>, newName: string) { |
| 19 | + if (!nodeData || !nodeData.path || !newName) { |
| 20 | + return; |
| 21 | + } |
| 22 | + const lastIndexPath = nodeData.path.lastIndexOf('/'); |
| 23 | + const parentPath = nodeData.path.substring(0, lastIndexPath); |
| 24 | + const newPath = `${parentPath}/${newName}`; |
| 25 | + const lastIndexExt = newName.lastIndexOf('.'); |
| 26 | + nodeData.extension = lastIndexExt > 0 ? newName.substring(newName.lastIndexOf('.')) : ''; |
| 27 | + nodeData.key = newPath; |
| 28 | + nodeData.path = newPath; |
| 29 | + nodeData.title = newName; |
| 30 | + nodeData.name = newName; |
| 31 | + nodeData.exist = true; |
| 32 | +} |
| 33 | + |
| 34 | +function createFile(path: string, index: number) { |
| 35 | + return { |
| 36 | + extension: '.md', |
| 37 | + index: [], |
| 38 | + isLeaf: true, |
| 39 | + key: `${path}/Untitled${index}.md`, |
| 40 | + name: `Untitled${index}.md`, |
| 41 | + path: `${path}/Untitled${index}.md`, |
| 42 | + size: 0, |
| 43 | + title: `Untitled${index}.md`, |
| 44 | + type: 'file', |
| 45 | + exist: false |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function createDir(path: string, index: number) { |
| 50 | + return { |
| 51 | + children: [], |
| 52 | + exist: false, |
| 53 | + size: 0, |
| 54 | + name: `Untitled Folder${index}`, |
| 55 | + title: `Untitled Folder${index}`, |
| 56 | + key: `${path}/Untitled Folder${index}`, |
| 57 | + path: `${path}/Untitled Folder${index}`, |
| 58 | + type: 'directory' |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +export function addToTreeData(tree: any, path: string, type: 'file' | 'directory') { |
| 63 | + let addedNode: Record<string, any> = {}; |
| 64 | + let nextTree = produce(tree, (draftTree: any) => { |
| 65 | + let item = null; |
| 66 | + let root = draftTree[0]; |
| 67 | + if (root) { |
| 68 | + let stack: Record<string, any> = [root]; |
| 69 | + while (stack.length > 0) { |
| 70 | + let node = stack.pop(); |
| 71 | + if (node.type === 'directory' && Array.isArray(node.children) && node.path === path) { |
| 72 | + let maxIndex = 1; |
| 73 | + for (let i of node.children) { |
| 74 | + let reg = type === 'file' ? /Untitled([^/]*)\.md$/ : /Untitled\sFolder([^/]*)$/; |
| 75 | + const match = reg.exec(i.path); |
| 76 | + if (match && match[1] && typeof +match[1] === 'number') { |
| 77 | + let curIndex = +match[1]; |
| 78 | + if (!Number.isNaN(curIndex) && typeof curIndex === 'number' && curIndex > maxIndex) { |
| 79 | + maxIndex = curIndex; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + maxIndex += 1; |
| 84 | + item = type === 'file' ? createFile(path, maxIndex) : createDir(path, maxIndex); |
| 85 | + node.children.push(item); |
| 86 | + addedNode = item; |
| 87 | + stack.length = 0; |
| 88 | + return; |
| 89 | + } |
| 90 | + if (node.children && node.children.length > 0) { |
| 91 | + let len = node.children.length; |
| 92 | + for (let i = 0; i < len; i++) { |
| 93 | + stack.push(node.children[i]); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + return { |
| 100 | + nextTree, |
| 101 | + node: addedNode |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +export function deleteToTreeData(tree: any, path: string) { |
| 106 | + let rootPath = getRootPath(path); |
| 107 | + let selectedFile = ''; |
| 108 | + let nextTree = produce(tree, (draftTree: any) => { |
| 109 | + let root = draftTree[0]; |
| 110 | + if (root) { |
| 111 | + let stack: Record<string, any> = [root]; |
| 112 | + while (stack.length > 0) { |
| 113 | + let node = stack.pop(); |
| 114 | + if (Array.isArray(node.children) && node.path === rootPath) { |
| 115 | + let len = node.children.length; |
| 116 | + for (let i = 0; i < len; i++) { |
| 117 | + if (node.children[i].path === path) { |
| 118 | + if (node.children[i + 1]) { |
| 119 | + selectedFile = node.children[i + 1].key; |
| 120 | + } else if (node.children[i - 1]) { |
| 121 | + selectedFile = node.children[i - 1].key; |
| 122 | + } else { |
| 123 | + selectedFile = node.key; |
| 124 | + } |
| 125 | + node.children.splice(i, 1); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + stack.length = 0; |
| 130 | + return; |
| 131 | + } |
| 132 | + if (node.children && node.children.length > 0) { |
| 133 | + let len = node.children.length; |
| 134 | + for (let i = 0; i < len; i++) { |
| 135 | + stack.push(node.children[i]); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + }); |
| 141 | + return { |
| 142 | + nextTree, |
| 143 | + selectedFile |
| 144 | + }; |
| 145 | +} |
0 commit comments