debian-mirror-gitlab/app/assets/javascripts/ide/lib/files.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import { decorateData, sortTree } from '../stores/utils';
2019-07-07 11:18:12 +05:30
2021-03-08 18:12:59 +05:30
export const splitParent = (path) => {
2019-07-07 11:18:12 +05:30
const idx = path.lastIndexOf('/');
return {
parent: idx >= 0 ? path.substring(0, idx) : null,
name: idx >= 0 ? path.substring(idx + 1) : path,
};
};
/**
* Create file objects from a list of file paths.
2021-03-08 18:12:59 +05:30
*
* @param {Array} options.data Array of blob paths to parse and create a file tree from.
* @param {Boolean} options.tempFile Web IDE flag for whether this is a "new" file or not.
* @param {String} options.content Content to initialize the new blob with.
* @param {String} options.rawPath Raw path used for the new blob.
* @param {Object} options.blobData Extra values to initialize each blob with.
2019-07-07 11:18:12 +05:30
*/
2021-03-08 18:12:59 +05:30
export const decorateFiles = ({
data,
tempFile = false,
content = '',
rawPath = '',
blobData = {},
}) => {
2019-07-07 11:18:12 +05:30
const treeList = [];
const entries = {};
// These mutable variable references end up being exported and used by `createTempEntry`
let file;
let parentPath;
2021-03-08 18:12:59 +05:30
const insertParent = (path) => {
2019-07-07 11:18:12 +05:30
if (!path) {
return null;
} else if (entries[path]) {
return entries[path];
}
const { parent, name } = splitParent(path);
const parentFolder = parent && insertParent(parent);
parentPath = parentFolder && parentFolder.path;
const tree = decorateData({
id: path,
name,
path,
type: 'tree',
tempFile,
changed: tempFile,
opened: tempFile,
parentPath,
});
Object.assign(entries, {
[path]: tree,
});
if (parentFolder) {
parentFolder.tree.push(tree);
} else {
treeList.push(tree);
}
return tree;
};
2021-03-08 18:12:59 +05:30
data.forEach((path) => {
2019-07-07 11:18:12 +05:30
const { parent, name } = splitParent(path);
const fileFolder = parent && insertParent(parent);
if (name) {
parentPath = fileFolder && fileFolder.path;
file = decorateData({
id: path,
name,
path,
type: 'blob',
tempFile,
changed: tempFile,
content,
2019-07-31 22:56:46 +05:30
rawPath,
2019-07-07 11:18:12 +05:30
parentPath,
2021-03-08 18:12:59 +05:30
...blobData,
2019-07-07 11:18:12 +05:30
});
Object.assign(entries, {
[path]: file,
});
if (fileFolder) {
fileFolder.tree.push(file);
} else {
treeList.push(file);
}
}
});
return {
entries,
treeList: sortTree(treeList),
file,
parentPath,
};
};