feature(cloudcmd) improve error handling when viewing or editing a file

This commit is contained in:
coderaiser 2021-01-30 11:19:10 +02:00
parent b3b618b2ca
commit 1b5a6a3721
6 changed files with 72 additions and 77 deletions

View file

@ -1,11 +1,8 @@
/* global CloudCmd */
'use strict';
const tryToPromiseAll = require('../../common/try-to-promise-all');
/* global CloudCmd */
const Util = require('../../common/util');
const callbackify = require('../../common/callbackify');
const Images = require('./images');
const load = require('./load');
@ -302,15 +299,12 @@ module.exports.getCurrentOwner = (currentFile) => {
return owner.textContent;
};
const mixArgs = (f) => (a, b) => f(b, a);
/**
* unified way to get current file content
*
* @param callback
* @param currentFile
*/
module.exports.getCurrentData = callbackify(mixArgs(async (callback, currentFile) => {
module.exports.getCurrentData = async (currentFile) => {
const {Dialog} = DOM;
const Info = DOM.CurrentInfo;
const current = currentFile || DOM.getCurrentFile();
@ -319,27 +313,24 @@ module.exports.getCurrentData = callbackify(mixArgs(async (callback, currentFile
if (Info.name === '..') {
Dialog.alert.noFiles();
return callback(Error('No files selected!'));
return [Error('No Files')];
}
if (isDir) {
const [e, data] = await RESTful.read(path);
if (e)
throw e;
return data;
}
if (isDir)
return await RESTful.read(path);
const [hashNew, hash] = await DOM.checkStorageHash(path);
if (!hashNew)
return [Error(`Can't get hash of a file`)];
if (hash === hashNew)
return await Storage.get(`${path}-data`);
return [null, await Storage.get(`${path}-data`)];
const [e, data] = await RESTful.read(path);
if (e)
return;
return [e, null];
const ONE_MEGABYTE = 1024 * 1024 * 1024;
const {length} = data;
@ -347,8 +338,8 @@ module.exports.getCurrentData = callbackify(mixArgs(async (callback, currentFile
if (hash && length < ONE_MEGABYTE)
await DOM.saveDataToStorage(path, data, hashNew);
return data;
}));
return [null, data];
};
/**
* unified way to get RefreshButton
@ -499,14 +490,11 @@ module.exports.checkStorageHash = async (name) => {
if (typeof name !== 'string')
throw Error('name should be a string!');
const [error, loadHash, storeHash] = await tryToPromiseAll([
const [loadHash, storeHash] = await Promise.all([
DOM.loadCurrentHash(),
Storage.get(nameHash),
]);
if (error)
throw error;
return [loadHash, storeHash];
};
@ -838,8 +826,6 @@ module.exports.updateCurrentInfo = (currentFile) => {
const filesPassive = DOM.getFiles(panelPassive);
const name = DOM.getCurrentName(current);
/* eslint no-multi-spaces:0 */
info.dir = DOM.getCurrentDirName();
info.dirPath = DOM.getCurrentDirPath();
info.parentDirPath = DOM.getParentDirPath();

View file

@ -20,11 +20,13 @@ module.exports.init = async () => {
await CloudCmd.EditFile();
};
module.exports.show = () => {
module.exports.show = async () => {
Events.addKey(listener);
CloudCmd.EditFile
.show(ConfigView)
const editFile = await CloudCmd.EditFile
.show(ConfigView);
editFile
.getEditor()
.setKeyMap('vim');
};

View file

@ -51,7 +51,7 @@ function getName() {
return name;
}
module.exports.show = (options) => {
module.exports.show = async (options) => {
if (isLoading())
return;
@ -69,23 +69,24 @@ module.exports.show = (options) => {
.getEditor()
.setOption('keyMap', 'default');
Info.getData((error, data) => {
const {path} = Info;
const name = getName();
if (error)
return Images.hide();
setMsgChanged(name);
CloudCmd.Edit
.getEditor()
.setValueFirst(path, data)
.setModeForPath(name)
.enableKey();
CloudCmd.Edit.show(optionsEdit);
});
const [error, data] = await Info.getData();
if (error) {
Images.hide();
return CloudCmd.Edit;
}
const {path} = Info;
const name = getName();
setMsgChanged(name);
CloudCmd.Edit
.getEditor()
.setValueFirst(path, data)
.setModeForPath(name)
.enableKey();
CloudCmd.Edit.show(optionsEdit);
return CloudCmd.Edit;
};

View file

@ -137,14 +137,16 @@ function getMenuData(isAuth) {
function getFileMenuData() {
const isAuth = CloudCmd.config('auth');
const show = wrap((name) => {
CloudCmd[name].show();
});
const menuBottom = getMenuData(isAuth);
const menuTop = {
'View': show('View'),
'Edit': show('EditFile'),
'View': () => {
CloudCmd.View.show();
},
'Edit': () => {
const name = config('vim') ? 'EditFileVim' : 'EditFile';
CloudCmd[name].show();
},
'Rename': () => {
setTimeout(DOM.renameCurrent, 100);
},
@ -228,16 +230,15 @@ function beforeClick(name) {
return MenuShowedName !== name;
}
function _uploadTo(nameModule) {
Info.getData((error, data) => {
if (error)
return;
const {name} = Info;
CloudCmd.execFromModule(nameModule, 'uploadFile', name, data);
});
async function _uploadTo(nameModule) {
const [error, data] = await Info.getData();
if (error)
return;
const {name} = Info;
CloudCmd.execFromModule(nameModule, 'uploadFile', name, data);
CloudCmd.log('Uploading to ' + name + '...');
}

View file

@ -128,7 +128,7 @@ async function show(data, options = {}) {
switch(type) {
default:
return viewFile();
return await viewFile();
case 'markdown':
return await CloudCmd.Markdown.show(Info.path);
@ -199,20 +199,20 @@ async function viewMedia(path) {
modal.open(element, allConfig);
}
function viewFile() {
Info.getData((error, data) => {
if (error)
return Images.hide();
const element = document.createTextNode(data);
const options = Config;
if (CloudCmd.config('showFileName'))
options.title = Info.name;
El.append(element);
modal.open(El, options);
});
async function viewFile() {
const [error, data] = await Info.getData();
if (error)
return Images.hide();
const element = document.createTextNode(data);
const options = Config;
if (CloudCmd.config('showFileName'))
options.title = Info.name;
El.append(element);
modal.open(El, options);
}
const copy = (a) => assign({}, a);