picosh 0.1.1 → 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.
Files changed (2) hide show
  1. package/index.js +42 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -3,17 +3,43 @@
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
- function saveClipboardImage(clipboard) {
10
- const img = clipboard.readImage();
11
- if (img.isEmpty()) return null;
11
+ function hasImage(clipboard) {
12
+ if (!clipboard.readImage().isEmpty()) return {type: 'bitmap'};
13
+ const html = clipboard.readHTML();
14
+ if (html) {
15
+ const match = html.match(/<img[^>]+src=["']([^"']+)["']/i);
16
+ if (match) return {type: 'url', src: match[1]};
17
+ }
18
+ return null;
19
+ }
12
20
 
21
+ function saveBitmap(clipboard) {
13
22
  fs.mkdirSync(SAVE_DIR, {recursive: true});
14
23
  const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
15
- fs.writeFileSync(filepath, img.toPNG());
16
- return filepath;
24
+ fs.writeFileSync(filepath, clipboard.readImage().toPNG());
25
+ return Promise.resolve(filepath);
26
+ }
27
+
28
+ function downloadImage(url) {
29
+ return new Promise((resolve) => {
30
+ fs.mkdirSync(SAVE_DIR, {recursive: true});
31
+ const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
32
+ const file = fs.createWriteStream(filepath);
33
+ const client = url.startsWith('https') ? https : http;
34
+
35
+ client.get(url, (res) => {
36
+ res.pipe(file);
37
+ file.on('finish', () => file.close(() => resolve(filepath)));
38
+ }).on('error', () => {
39
+ fs.unlink(filepath, () => {});
40
+ resolve(null);
41
+ });
42
+ });
17
43
  }
18
44
 
19
45
  exports.decorateTerm = (Term, {React}) => {
@@ -24,15 +50,21 @@ exports.decorateTerm = (Term, {React}) => {
24
50
 
25
51
  try {
26
52
  const {clipboard} = require('electron');
27
- const filepath = saveClipboardImage(clipboard);
28
- if (!filepath) return;
53
+ const found = hasImage(clipboard);
54
+ if (!found) return;
29
55
 
30
56
  e.preventDefault();
31
57
  e.stopPropagation();
32
58
 
33
- if (this.props.onData) {
34
- this.props.onData(filepath);
35
- }
59
+ const save = found.type === 'bitmap'
60
+ ? saveBitmap(clipboard)
61
+ : downloadImage(found.src);
62
+
63
+ save.then((filepath) => {
64
+ if (filepath && this.props.onData) {
65
+ this.props.onData(filepath);
66
+ }
67
+ });
36
68
  } catch (_) {}
37
69
  };
38
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picosh",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Hyper plugin: paste clipboard images as file paths",
5
5
  "main": "index.js",
6
6
  "license": "MIT",