feature(cloudcmd) add ability to set current file closer to removed files

This commit is contained in:
coderaiser 2018-06-11 15:59:22 +03:00
parent 0202192fc0
commit cfa7a8b640
3 changed files with 72 additions and 3 deletions

View file

@ -0,0 +1,19 @@
'use strict';
const currify = require('currify/legacy');
const not = currify((array, value) => !~array.indexOf(value));
const notOneOf = currify((a, b) => a.filter(not(b)));
module.exports = (currentName, names, removedNames) => {
const i = names.indexOf(currentName);
const nextNames = notOneOf(names, removedNames);
const length = nextNames.length;
if (nextNames[i])
return nextNames[i];
return nextNames[length - 1];
};

View file

@ -0,0 +1,43 @@
'use strict';
const test = require('tape');
const getNextCurrentName = require('./get-next-current-name');
test('get-next-current-name', (t) => {
const names = [
'..',
'hello',
'1',
'2',
];
const removedNames = [
'1'
];
const name = getNextCurrentName('hello', names, removedNames);
t.equal(name, 'hello', 'should equal');
t.end();
});
test('get-next-current-name: all files removed', (t) => {
const names = [
'..',
'hello',
'1',
'2',
];
const removedNames = [
'1',
'2',
'hello',
];
const name = getNextCurrentName('2', names, removedNames);
t.equal(name, '..', 'should equal');
t.end();
});

View file

@ -18,6 +18,7 @@ const {
const RESTful = require('../../dom/rest');
const removeExtension = require('./remove-extension');
const setListeners = require('./set-listeners');
const getNextCurrentName = require('./get-next-current-name');
const removeQuery = (a) => a.replace(/\?.*/, '');
@ -258,13 +259,19 @@ function OperationProto(operation, data) {
showLoad();
const names = DOM.getFilenames(files);
const removedNames = DOM.getFilenames(files);
const names = DOM.CurrentInfo.files.map(DOM.getCurrentName);
const prevCurrent = DOM.getCurrentName();
const currentName = getNextCurrentName(prevCurrent, names, removedNames);
deleteFn(path + query, names, () => {
deleteFn(path + query, removedNames, (e) => {
const Storage = DOM.Storage;
const dirPath = Info.dirPath;
CloudCmd.refresh();
!e && CloudCmd.refresh({
currentName
});
Storage.removeMatch(dirPath);
});
}