picosh 0.1.2 → 0.1.3
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 +22 -18
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -8,24 +8,21 @@ const http = require('http');
|
|
|
8
8
|
|
|
9
9
|
const SAVE_DIR = path.join(os.tmpdir(), 'picosh');
|
|
10
10
|
|
|
11
|
-
function
|
|
12
|
-
|
|
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
|
-
|
|
11
|
+
function hasImage(clipboard) {
|
|
12
|
+
if (!clipboard.readImage().isEmpty()) return {type: 'bitmap'};
|
|
20
13
|
const html = clipboard.readHTML();
|
|
21
14
|
if (html) {
|
|
22
15
|
const match = html.match(/<img[^>]+src=["']([^"']+)["']/i);
|
|
23
|
-
if (match) {
|
|
24
|
-
return downloadImage(match[1]);
|
|
25
|
-
}
|
|
16
|
+
if (match) return {type: 'url', src: match[1]};
|
|
26
17
|
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
27
20
|
|
|
28
|
-
|
|
21
|
+
function saveBitmap(clipboard) {
|
|
22
|
+
fs.mkdirSync(SAVE_DIR, {recursive: true});
|
|
23
|
+
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
24
|
+
fs.writeFileSync(filepath, clipboard.readImage().toPNG());
|
|
25
|
+
return Promise.resolve(filepath);
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
function downloadImage(url) {
|
|
@@ -53,11 +50,18 @@ exports.decorateTerm = (Term, {React}) => {
|
|
|
53
50
|
|
|
54
51
|
try {
|
|
55
52
|
const {clipboard} = require('electron');
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
const found = hasImage(clipboard);
|
|
54
|
+
if (!found) return;
|
|
55
|
+
|
|
56
|
+
e.preventDefault();
|
|
57
|
+
e.stopPropagation();
|
|
58
|
+
|
|
59
|
+
const save = found.type === 'bitmap'
|
|
60
|
+
? saveBitmap(clipboard)
|
|
61
|
+
: downloadImage(found.src);
|
|
62
|
+
|
|
63
|
+
save.then((filepath) => {
|
|
64
|
+
if (filepath && this.props.onData) {
|
|
61
65
|
this.props.onData(filepath);
|
|
62
66
|
}
|
|
63
67
|
});
|