From 1b5a6a3721ba7a5e07a741d6a2899afd7e5bedd3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 30 Jan 2021 11:19:10 +0200 Subject: [PATCH] feature(cloudcmd) improve error handling when viewing or editing a file --- .eslintrc.js | 5 +++++ client/dom/index.js | 40 +++++++++++---------------------- client/modules/edit-file-vim.js | 8 ++++--- client/modules/edit-file.js | 37 +++++++++++++++--------------- client/modules/menu.js | 29 ++++++++++++------------ client/modules/view/index.js | 30 ++++++++++++------------- 6 files changed, 72 insertions(+), 77 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d2cc8b7b..7b6b53ac 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -20,6 +20,11 @@ module.exports = { extends: [ 'plugin:node/recommended', ], + }, { + files: ['client/dom/index.js'], + rules: { + 'no-multi-spaces': 'off', + }, }, { files: ['bin/cloudcmd.js'], rules: { diff --git a/client/dom/index.js b/client/dom/index.js index 3c2edf73..f1dce437 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -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(); diff --git a/client/modules/edit-file-vim.js b/client/modules/edit-file-vim.js index fe8198af..c9b9c891 100644 --- a/client/modules/edit-file-vim.js +++ b/client/modules/edit-file-vim.js @@ -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'); }; diff --git a/client/modules/edit-file.js b/client/modules/edit-file.js index ecde15c7..b101ddbb 100644 --- a/client/modules/edit-file.js +++ b/client/modules/edit-file.js @@ -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; }; diff --git a/client/modules/menu.js b/client/modules/menu.js index 66a4dbcc..4ecc17cb 100644 --- a/client/modules/menu.js +++ b/client/modules/menu.js @@ -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 + '...'); } diff --git a/client/modules/view/index.js b/client/modules/view/index.js index aa167d02..0d34df22 100644 --- a/client/modules/view/index.js +++ b/client/modules/view/index.js @@ -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);