1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-16 19:46:19 +02:00

add drag n drop to files explorer 🔥

This commit is contained in:
Kushagra Gour
2018-10-12 20:06:14 +05:30
parent 006a37f8ef
commit d3f06ac890
8 changed files with 332 additions and 114 deletions

87
src/fileUtils.js Normal file
View File

@@ -0,0 +1,87 @@
import { deferred } from './deferred';
const esprima = require('esprima');
/**
* Returns a linear file list from a nested file strcuture.
* It excludes the folders from the returned list.
* @param {array} files Nested file structure
*/
export function linearizeFiles(files) {
function reduceToLinearFiles(files) {
return files.reduce((list, currentFile) => {
if (currentFile.isFolder) {
return [...list, ...reduceToLinearFiles(currentFile.children)];
} else {
return [...list, currentFile];
}
}, []);
}
return reduceToLinearFiles(files);
}
/**
* Recursively iterates and assigns the `path` property to the files in passed files
* array.
* @param {array} files files structure for an item
* @param {string} parentPath Parent path to prefix with all processed files
*/
export function assignFilePaths(files, parentPath = '') {
files.forEach(file => {
file.path = parentPath ? `${parentPath}/${file.name}` : file.name;
if (file.isFolder) {
assignFilePaths(
file.children,
parentPath ? `${parentPath}/${file.name}` : file.name
);
}
});
return files;
}
/**
* Returns the file object and it's index that is direct child of passed files array with name as passed fileName.
* If not found, returns -1
* @param {array} files files structure for an item
* @param {string} fileName File/folder name
*/
export function getChildFileFromName(files, fileName) {
const index = files.findIndex(file => file.name === fileName);
return { index, file: files[index] };
}
/**
* Returns the file object and it's index in its parent for the passed path.
* If not found, returns {index:-1}
* @param {array} files files structure for an item
* @param {string} path Path of file to search
*/
export function getFileFromPath(files, path) {
let currentFolder = files;
const pathPieces = path.split('/');
while (pathPieces.length > 1) {
let folderName = pathPieces.shift();
currentFolder = getChildFileFromName(currentFolder, folderName).file
.children;
}
// now we should be left with just one value in the pathPieces array - the actual file name
return getChildFileFromName(currentFolder, pathPieces[0]);
}
/**
* Returns the file object and it's index in its parent for the passed path.
* If not found, returns {index:-1}
* @param {array} files files structure for an item
* @param {string} path Path of file to search
*/
export function removeFileAtPath(files, path) {
let currentFolder = files;
const pathPieces = path.split('/');
while (pathPieces.length > 1) {
let folderName = pathPieces.shift();
currentFolder = getChildFileFromName(currentFolder, folderName).file
.children;
}
// now we should be left with just one value in the pathPieces array - the actual file name
const { index } = getChildFileFromName(currentFolder, pathPieces[0]);
currentFolder.splice(index, 1);
}