-
Notifications
You must be signed in to change notification settings - Fork 44
/
clipboard.service.ts
98 lines (84 loc) · 2.35 KB
/
clipboard.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { NothingFlow, CombErr } from 'src/app/@dataflow/core';
import { mapTo } from 'rxjs/operators';
import { NavigationFlowOutNode } from 'src/app/@dataflow/extra';
import { OperationsListFlowOutItemNode } from 'src/app/@dataflow/rclone';
export type IManipulate = 'copy' | 'move' | 'del';
export interface ClipboardItem {
oper: IManipulate;
key: string;
srcRemote: string;
srcItem: OperationsListFlowOutItemNode;
dst?: NavigationFlowOutNode;
}
export class Clipboard {
private data = new Map<string, ClipboardItem>();
public add(
o: IManipulate,
remote: string,
row: OperationsListFlowOutItemNode,
dst?: NavigationFlowOutNode
) {
const key = Clipboard.genKey(remote, row.Path);
this.data.set(key, {
oper: o,
key: key,
srcItem: { ...row },
srcRemote: remote,
dst: { ...dst },
});
}
public pop(remote: string, path: string): ClipboardItem {
const key = Clipboard.genKey(remote, path);
if (!this.data.has(key)) return undefined;
const ans = this.data.get(key);
this.data.delete(key);
return ans;
}
public getManipulation(remote: string, path: string): IManipulate {
const key = Clipboard.genKey(remote, path);
if (!this.data.has(key)) return undefined;
return this.data.get(key).oper;
}
public countManipulation(o: IManipulate): number {
let cnt = 0;
this.data.forEach((x) => (x.oper === o ? cnt++ : null));
return cnt;
}
public get values(): ClipboardItem[] {
return Array.from(this.data.values());
}
public get size(): number {
return this.data.size;
}
public clear() {
this.data.clear();
}
public static genKey(remote: string, path: string) {
return JSON.stringify({ r: remote, p: path });
}
}
export abstract class ClipboardFlow extends NothingFlow<{ clipboard: Clipboard }> {}
@Injectable({
providedIn: 'root',
})
export class ClipboardService extends Clipboard {
private trigger = new Subject<number>();
clipboard$: ClipboardFlow;
public commit() {
this.trigger.next(1);
}
constructor() {
super();
const outer = this;
this.clipboard$ = new (class extends ClipboardFlow {
public prerequest$ = outer.trigger.pipe(
mapTo<number, CombErr<{ clipboard: Clipboard }>>([{ clipboard: outer }, []])
);
})();
this.clipboard$.deploy();
this.clipboard$.getOutput().subscribe();
this.trigger.next(1);
}
}