瀏覽代碼

Merge pull request #1 from sonicblis/paste-clipboard-image

Paste image content from the clipboard
CJ 5 年之前
父節點
當前提交
a838062564
共有 1 個文件被更改,包括 20 次插入2 次删除
  1. 20 2
      client/scripts/ui.js

+ 20 - 2
client/scripts/ui.js

@@ -11,7 +11,8 @@ class PeersUI {
         Events.on('peer-joined', e => this._onPeerJoined(e.detail));
         Events.on('peer-left', e => this._onPeerLeft(e.detail));
         Events.on('peers', e => this._onPeers(e.detail));
-        Events.on('file-progress', e => this._onFileProgress(e.detail));
+        Events.on('file-progress', e => this._onFileProgress(e.detail));        
+        window.addEventListener('paste', e => this._onPaste(e));
     }
 
     _onPeerJoined(peer) {
@@ -40,6 +41,23 @@ class PeersUI {
 
     _clearPeers() {
         const $peers = $$('x-peers').innerHTML = '';
+    }   
+    
+    _onPaste(e) {        
+        const files = e.clipboardData.items
+            .filter(i => i.type.indexOf('image') > -1)
+            .map(i => i.getAsFile());
+        
+        // send the pasted image content to the only peer if there is one
+        // otherwise, select the peer somehow by notifying the client that
+        // "image data has been pasted, click the client to which to send it"
+        // not implemented
+        if (files.length > 0 && $$('x-peer').length === 1) {
+            Events.fire('files-selected', {
+                files: files,
+                to: $$('x-peer').id
+            });
+        }
     }
 }
 
@@ -613,4 +631,4 @@ document.body.onclick = e => { // safari hack to fix audio
     document.body.onclick = null;
     if (!(/.*Version.*Safari.*/.test(navigator.userAgent))) return;
     blop.play();
-}
+}