From 3ac7ee4b76c93f5b5a57b402523c97236ab1c8ae Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 22 Aug 2019 18:30:38 +0300 Subject: [PATCH] fix(buffer) copy, cut --- client/client.js | 14 ++++++++------ client/dom/buffer.js | 34 +++++++++++++++++----------------- client/dom/index.js | 8 +------- client/dom/storage.js | 14 ++++++++++++-- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/client/client.js b/client/client.js index 503b4928..78da2d75 100644 --- a/client/client.js +++ b/client/client.js @@ -16,7 +16,6 @@ const isDev = process.env.NODE_ENV === 'development'; const Images = require('./dom/images'); const {unregisterSW} = require('./sw/register'); -const jonny = require('jonny/legacy'); const currify = require('currify/legacy'); const noJS = (a) => a.replace(/.js$/, ''); @@ -345,15 +344,18 @@ function CloudCmdProto(DOM) { * */ function ajaxLoad(path, options, panel, callback) { + const {Dialog, RESTful} = DOM; + const create = async (error, json) => { - const {RESTful} = DOM; + if (error) + return Dialog.alert(`Can't get from store: ${e.message}`); + const name = options.currentName || Info.name; - const obj = jonny.parse(json); const isRefresh = options.refresh; const {noCurrent} = options; if (!isRefresh && json) - return createFileTable(obj, panel, options, callback); + return createFileTable(json, panel, options, callback); const position = DOM.getPanelPosition(panel); const sort = CloudCmd.sort[position]; @@ -364,10 +366,10 @@ function CloudCmdProto(DOM) { order, }); - const [, newObj] = await RESTful.read(path + query, 'json'); + const [e, newObj] = await RESTful.read(path + query, 'json'); if (!newObj) - return; + return Dialog.alert(`Can't read: ${e.message}`); options.sort = sort; options.order = order; diff --git a/client/dom/buffer.js b/client/dom/buffer.js index d99429e5..f3df5981 100644 --- a/client/dom/buffer.js +++ b/client/dom/buffer.js @@ -2,8 +2,6 @@ /* global CloudCmd */ -const jonny = require('jonny/legacy'); - const tryToPromiseAll = require('../../common/try-to-promise-all'); const Storage = require('./storage'); const DOM = require('./'); @@ -59,14 +57,15 @@ function BufferProto() { } async function readBuffer() { - const [e, result] = await tryToPromiseAll([ + const [e, cp, ct] = await tryToPromiseAll([ Storage.get(COPY), Storage.get(CUT), ]); return [ e, - ...result, + cp, + ct, ]; } @@ -74,7 +73,7 @@ function BufferProto() { const names = getNames(); const from = Info.dirPath; - clear(); + await clear(); if (!names.length) return; @@ -90,7 +89,7 @@ function BufferProto() { const names = getNames(); const from = Info.dirPath; - clear(); + await clear(); if (!names.length) return; @@ -103,9 +102,9 @@ function BufferProto() { }); } - function clear() { - Storage.remove(COPY) - .remove(CUT); + async function clear() { + await Storage.remove(COPY); + await Storage.remove(CUT); rmCutClass(); } @@ -117,19 +116,20 @@ function BufferProto() { return showMessage(error || 'Buffer is empty!'); const opStr = cp ? 'copy' : 'move'; - const opData = cp || ct; + const data = cp || ct; const {Operation} = CloudCmd; const msg = 'Path is same!'; - const path = Info.dirPath; + const to = Info.dirPath; - const data = jonny.parse(opData); - data.to = path; - - if (data.from === path) + if (data.from === to) return showMessage(msg); - Operation.show(opStr, data); - clear(); + Operation.show(opStr, { + ...data, + to, + }); + + await clear(); } return Buffer; diff --git a/client/dom/index.js b/client/dom/index.js index 6f506987..44eca2e2 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -2,8 +2,6 @@ 'use strict'; -const itype = require('itype/legacy'); -const jonny = require('jonny/legacy'); const tryToPromiseAll = require('../../common/try-to-promise-all'); const Util = require('../../common/util'); @@ -341,16 +339,12 @@ function CmdProto() { if (hash === hashNew) return await Storage.get(`${path}-data`); - let [e, data] = await RESTful.read(path); + const [e, data] = await RESTful.read(path); if (e) return; const ONE_MEGABYTE = 1024 * 1024 * 1024; - - if (itype.object(data)) - data = jonny.stringify(data); - const {length} = data; if (hash && length < ONE_MEGABYTE) diff --git a/client/dom/storage.js b/client/dom/storage.js index 628bc9bd..c8d427cd 100644 --- a/client/dom/storage.js +++ b/client/dom/storage.js @@ -1,11 +1,21 @@ 'use strict'; +const tryCatch = require('try-catch'); + +const {parse, stringify} = JSON; +const isObj = (a) => typeof a === 'object'; + module.exports.set = async (name, data) => { - localStorage.setItem(name, data); + const primitive = !isObj(data) ? data : stringify(data); + + localStorage.setItem(name, primitive); }; module.exports.get = async (name) => { - return localStorage.getItem(name); + const data = localStorage.getItem(name); + const [, result = data] = tryCatch(parse, data); + + return result; }; module.exports.clear = async () => {