picosh 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +54 -35
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,57 +3,76 @@
|
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const os = require('os');
|
|
6
|
+
const https = require('https');
|
|
7
|
+
const http = require('http');
|
|
6
8
|
|
|
7
9
|
const SAVE_DIR = path.join(os.tmpdir(), 'picosh');
|
|
8
10
|
|
|
9
11
|
function saveClipboardImage(clipboard) {
|
|
10
12
|
const img = clipboard.readImage();
|
|
11
|
-
if (img.isEmpty())
|
|
13
|
+
if (!img.isEmpty()) {
|
|
14
|
+
fs.mkdirSync(SAVE_DIR, {recursive: true});
|
|
15
|
+
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
16
|
+
fs.writeFileSync(filepath, img.toPNG());
|
|
17
|
+
return Promise.resolve(filepath);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const html = clipboard.readHTML();
|
|
21
|
+
if (html) {
|
|
22
|
+
const match = html.match(/<img[^>]+src=["']([^"']+)["']/i);
|
|
23
|
+
if (match) {
|
|
24
|
+
return downloadImage(match[1]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
12
27
|
|
|
13
|
-
|
|
14
|
-
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
15
|
-
fs.writeFileSync(filepath, img.toPNG());
|
|
16
|
-
return filepath;
|
|
28
|
+
return Promise.resolve(null);
|
|
17
29
|
}
|
|
18
30
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
function downloadImage(url) {
|
|
32
|
+
return new Promise((resolve) => {
|
|
33
|
+
fs.mkdirSync(SAVE_DIR, {recursive: true});
|
|
34
|
+
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
35
|
+
const file = fs.createWriteStream(filepath);
|
|
36
|
+
const client = url.startsWith('https') ? https : http;
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
client.get(url, (res) => {
|
|
39
|
+
res.pipe(file);
|
|
40
|
+
file.on('finish', () => file.close(() => resolve(filepath)));
|
|
41
|
+
}).on('error', () => {
|
|
42
|
+
fs.unlink(filepath, () => {});
|
|
43
|
+
resolve(null);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
48
|
+
exports.decorateTerm = (Term, {React}) => {
|
|
49
|
+
return class extends React.Component {
|
|
50
|
+
componentDidMount() {
|
|
51
|
+
this._onKeyDown = (e) => {
|
|
52
|
+
if (!((e.ctrlKey || e.metaKey) && e.key === 'v')) return;
|
|
36
53
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
54
|
+
try {
|
|
55
|
+
const {clipboard} = require('electron');
|
|
56
|
+
saveClipboardImage(clipboard).then((filepath) => {
|
|
57
|
+
if (!filepath) return;
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
e.stopPropagation();
|
|
60
|
+
if (this.props.onData) {
|
|
61
|
+
this.props.onData(filepath);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
} catch (_) {}
|
|
65
|
+
};
|
|
41
66
|
|
|
42
|
-
|
|
43
|
-
|
|
67
|
+
document.addEventListener('keydown', this._onKeyDown, true);
|
|
68
|
+
}
|
|
44
69
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
} catch (err) {
|
|
49
|
-
// clipboard had no image, let default paste happen
|
|
50
|
-
}
|
|
70
|
+
componentWillUnmount() {
|
|
71
|
+
document.removeEventListener('keydown', this._onKeyDown, true);
|
|
51
72
|
}
|
|
52
73
|
|
|
53
74
|
render() {
|
|
54
|
-
return React.createElement(Term,
|
|
55
|
-
onKeyDown: this._handleKeyDown,
|
|
56
|
-
}));
|
|
75
|
+
return React.createElement(Term, this.props);
|
|
57
76
|
}
|
|
58
77
|
};
|
|
59
78
|
};
|