Skip to content

Commit 3b6b0b5

Browse files
committed
feature: client: buffer: migrate to ESM
1 parent 8876f05 commit 3b6b0b5

5 files changed

Lines changed: 352 additions & 366 deletions

File tree

client/dom/buffer.js

Lines changed: 0 additions & 135 deletions
This file was deleted.

client/dom/buffer.mjs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* global CloudCmd */
2+
import tryToPromiseAll from '../../common/try-to-promise-all.js';
3+
import Storage from './storage.js';
4+
import DOM from './index.js';
5+
6+
const Info = DOM.CurrentInfo;
7+
const CLASS = 'cut-file';
8+
const COPY = 'copy';
9+
const CUT = 'cut';
10+
11+
function showMessage(msg) {
12+
DOM.Dialog.alert(msg);
13+
}
14+
15+
function getNames() {
16+
const files = DOM.getActiveFiles();
17+
18+
return DOM.getFilenames(files);
19+
}
20+
21+
function addCutClass() {
22+
const files = DOM.getActiveFiles();
23+
24+
for (const element of files) {
25+
element.classList.add(CLASS);
26+
}
27+
}
28+
29+
function rmCutClass() {
30+
const files = DOM.getByClassAll(CLASS);
31+
32+
for (const element of files) {
33+
element.classList.remove(CLASS);
34+
}
35+
}
36+
37+
const checkEnabled = (fn) => () => {
38+
const is = CloudCmd.config('buffer');
39+
40+
if (is)
41+
return fn();
42+
43+
showMessage('Buffer disabled in config!');
44+
};
45+
46+
async function readBuffer() {
47+
const [e, cp, ct] = await tryToPromiseAll([
48+
Storage.getJson(COPY),
49+
Storage.getJson(CUT),
50+
]);
51+
52+
return [
53+
e,
54+
cp,
55+
ct,
56+
];
57+
}
58+
59+
export const copy = checkEnabled(async () => {
60+
const names = getNames();
61+
const from = Info.dirPath;
62+
63+
await clear();
64+
65+
if (!names.length)
66+
return;
67+
68+
await Storage.remove(CUT);
69+
await Storage.setJson(COPY, {
70+
from,
71+
names,
72+
});
73+
});
74+
75+
export const cut = checkEnabled(async () => {
76+
const names = getNames();
77+
const from = Info.dirPath;
78+
79+
await clear();
80+
81+
if (!names.length)
82+
return;
83+
84+
addCutClass();
85+
86+
await Storage.setJson(CUT, {
87+
from,
88+
names,
89+
});
90+
});
91+
92+
export const clear = checkEnabled(async () => {
93+
await Storage.remove(COPY);
94+
await Storage.remove(CUT);
95+
96+
rmCutClass();
97+
});
98+
99+
export const paste = checkEnabled(async () => {
100+
const [error, cp, ct] = await readBuffer();
101+
102+
if (error || !cp && !ct)
103+
return showMessage(error || 'Buffer is empty!');
104+
105+
const opStr = cp ? 'copy' : 'move';
106+
const data = cp || ct;
107+
const {Operation} = CloudCmd;
108+
const msg = 'Path is same!';
109+
const to = Info.dirPath;
110+
111+
if (data.from === to)
112+
return showMessage(msg);
113+
114+
Operation.show(opStr, {
115+
...data,
116+
to,
117+
});
118+
119+
await clear();
120+
});

client/dom/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ DOM.CurrentInfo = CurrentInfo;
3232
module.exports = DOM;
3333

3434
DOM.uploadDirectory = require('./directory');
35-
DOM.Buffer = require('./buffer');
35+
DOM.Buffer = require('./buffer.mjs');
3636
DOM.Events = require('#dom/events');
3737

3838
const loadRemote = require('./load-remote');

client/key/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const clipboard = require('@cloudcmd/clipboard');
55
const {fullstore} = require('fullstore');
66

7-
const Buffer = require('../dom/buffer');
7+
const Buffer = require('../dom/buffer.mjs');
88
const Events = require('#dom/events');
99
const KEY = require('./key');
1010

0 commit comments

Comments
 (0)