forked from wulkano/Kap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare-service-context.js
113 lines (89 loc) · 2.11 KB
/
share-service-context.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
const {Notification, clipboard} = require('electron');
const got = require('got');
const prettifyFormat = format => {
const formats = new Map([
['apng', 'APNG'],
['gif', 'GIF'],
['mp4', 'MP4'],
['webm', 'WebM']
]);
return formats.get(format);
};
class ShareServiceContext {
constructor(options) {
this._isBuiltin = options._isBuiltin;
this.format = options.format;
this.prettyFormat = prettifyFormat(this.format);
this.defaultFileName = options.defaultFileName;
this.filePath = options.convert;
this.config = options.config;
this.onCancel = options.onCancel;
this.onProgress = options.onProgress;
this.pluginName = options.pluginName;
this.canceled = false;
this.requests = [];
this.request = this.request.bind(this);
this.cancel = this.cancel.bind(this);
this.copyToClipboard = this.copyToClipboard.bind(this);
this.notify = this.notify.bind(this);
this.setProgress = this.setProgress.bind(this);
this.openConfigFile = this.openConfigFile.bind(this);
}
request(url, options) {
if (this.canceled) {
return;
}
const request = got(url, options);
this.requests.push(request);
return request;
}
cancel() {
this.canceled = true;
this.onCancel();
for (const request of this.requests) {
request.cancel();
}
}
clear() {
this.canceled = true;
for (const req of this.requests) {
req.cancel();
}
}
copyToClipboard(text) {
if (this.canceled) {
return;
}
clipboard.writeText(text);
}
notify(text) {
if (this.canceled) {
return;
}
let options = {
title: this.pluginName,
body: text
};
if (this._isBuiltin) {
options = {
body: text
};
}
const notification = new Notification(options);
notification.show();
}
setProgress(text, percentage) {
if (this.canceled) {
return;
}
this.onProgress(text, percentage);
}
openConfigFile() {
if (this.canceled) {
return;
}
this.config.openInEditor();
}
}
module.exports = ShareServiceContext;