|
| 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 | +}); |
0 commit comments