picosh 0.1.2 → 0.1.4

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