picosh 0.1.1 → 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.
Files changed (2) hide show
  1. package/index.js +42 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -3,17 +3,46 @@
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()) return null;
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
+ }
12
19
 
13
- fs.mkdirSync(SAVE_DIR, {recursive: true});
14
- const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
15
- fs.writeFileSync(filepath, img.toPNG());
16
- return filepath;
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
+ }
27
+
28
+ return Promise.resolve(null);
29
+ }
30
+
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;
37
+
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
+ });
17
46
  }
18
47
 
19
48
  exports.decorateTerm = (Term, {React}) => {
@@ -24,15 +53,14 @@ exports.decorateTerm = (Term, {React}) => {
24
53
 
25
54
  try {
26
55
  const {clipboard} = require('electron');
27
- const filepath = saveClipboardImage(clipboard);
28
- if (!filepath) return;
29
-
30
- e.preventDefault();
31
- e.stopPropagation();
32
-
33
- if (this.props.onData) {
34
- this.props.onData(filepath);
35
- }
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
+ });
36
64
  } catch (_) {}
37
65
  };
38
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picosh",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Hyper plugin: paste clipboard images as file paths",
5
5
  "main": "index.js",
6
6
  "license": "MIT",