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.
Files changed (2) hide show
  1. package/index.js +54 -35
  2. 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()) 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
+ }
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
- 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;
28
+ return Promise.resolve(null);
17
29
  }
18
30
 
19
- exports.middleware = (store) => (next) => (action) => {
20
- if (action.type === 'SESSION_PTY_DATA') {
21
- return next(action);
22
- }
23
- return next(action);
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
- exports.decorateTerm = (Term, {React, notify}) => {
27
- return class extends React.Component {
28
- constructor(props) {
29
- super(props);
30
- this._handleKeyDown = this._handleKeyDown.bind(this);
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
- _handleKeyDown(e) {
34
- const isCtrlV = (e.ctrlKey || e.metaKey) && e.key === 'v';
35
- if (!isCtrlV) return;
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
- try {
38
- const {clipboard} = require('electron');
39
- const filepath = saveClipboardImage(clipboard);
40
- if (!filepath) return;
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
- e.preventDefault();
43
- e.stopPropagation();
67
+ document.addEventListener('keydown', this._onKeyDown, true);
68
+ }
44
69
 
45
- if (this.props.onData) {
46
- this.props.onData(filepath);
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, Object.assign({}, this.props, {
55
- onKeyDown: this._handleKeyDown,
56
- }));
75
+ return React.createElement(Term, this.props);
57
76
  }
58
77
  };
59
78
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "picosh",
3
- "version": "0.1.0",
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",